Ticket #8606: 8606_ugly.patch
| File 8606_ugly.patch, 6.9 KB (added by , 13 years ago) |
|---|
-
core/src/org/openstreetmap/josm/gui/MainApplication.java
3 3 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 import static org.openstreetmap.josm.tools.I18n.trn; 6 import gnu.getopt.Getopt; 7 import gnu.getopt.LongOpt; 6 8 7 9 import java.awt.Image; 8 10 import java.awt.Toolkit; 9 11 import java.awt.event.WindowAdapter; 10 12 import java.awt.event.WindowEvent; 11 13 import java.io.File; 14 import java.io.IOException; 15 import java.lang.reflect.Field; 16 import java.lang.reflect.Method; 12 17 import java.net.Authenticator; 13 18 import java.net.ProxySelector; 14 19 import java.net.URL; 20 import java.net.URLConnection; 21 import java.net.URLStreamHandler; 22 import java.net.URLStreamHandlerFactory; 15 23 import java.security.AllPermission; 16 24 import java.security.CodeSource; 17 25 import java.security.PermissionCollection; … … 20 28 import java.util.ArrayList; 21 29 import java.util.Collection; 22 30 import java.util.HashMap; 31 import java.util.Hashtable; 23 32 import java.util.LinkedList; 24 33 import java.util.List; 25 34 import java.util.Map; … … 28 37 import javax.swing.RepaintManager; 29 38 import javax.swing.SwingUtilities; 30 39 31 import gnu.getopt.Getopt;32 import gnu.getopt.LongOpt;33 34 40 import org.jdesktop.swinghelper.debug.CheckThreadViolationRepaintManager; 35 41 import org.openstreetmap.josm.Main; 36 42 import org.openstreetmap.josm.data.AutosaveTask; … … 283 289 I18n.set(Main.pref.get("language", null)); 284 290 } 285 291 Main.pref.updateSystemProperties(); 292 293 checkHttpUserAgent(); 286 294 287 295 final JFrame mainFrame = new JFrame(tr("Java OpenStreetMap Editor")); 288 296 Main.parent = mainFrame; … … 421 429 RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager()); 422 430 } 423 431 } 432 433 private static void checkHttpUserAgent() { 434 // Check sun.net.www.protocol.http.HttpURLConnection.userAgent existence and validity. 435 // This public static final User-Agent is initialized after the value of system property "http.agent" we have set before. 436 // But, with Java Web Start, this field is initialized before JOSM main() method, and we cannot change it after 437 try { 438 Field field = Class.forName("sun.net.www.protocol.http.HttpURLConnection").getField("userAgent"); 439 440 if (!field.get(null).toString().equals(Version.getInstance().getFullAgentString())) { 441 442 // Access to internal Java handlers 443 Field handlersField = URL.class.getDeclaredField("handlers"); 444 handlersField.setAccessible(true); 445 Hashtable<?,?> handlers = (Hashtable<?,?>) handlersField.get(null); 446 447 // Default protocols supported by the JVM (in package sun.net.www.protocol), except gopher (we don't care) 448 for (String protocol : new String[]{"file","ftp","http","jar","mailto","netdoc"}) { 449 try { 450 // Makes Java load the default handler and put it in its handlers hashtable 451 if (protocol.equals("jar")) { 452 new URL(protocol+":file://filename.jar!/MANIFEST.MF"); 453 } else { 454 new URL(protocol+"://localhost"); 455 } 456 assert handlers.get(protocol) != null; 457 } catch (Throwable t) { 458 // If something goes wrong for a protocol, cancel everything 459 System.err.println(protocol + ": " + t.getMessage()); 460 return; 461 } 462 } 463 464 final Map<?,?> javaHandlers = new HashMap<Object, Object>(handlers); 465 466 // Set our own URL stream handler factory 467 URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() { 468 private final URLStreamHandler javaHttpHandler = (URLStreamHandler) javaHandlers.get("http"); 469 private final URLStreamHandler josmHttpHandler = new URLStreamHandler() { 470 @Override protected URLConnection openConnection(URL url) throws IOException { 471 try { 472 Method open = URLStreamHandler.class.getDeclaredMethod("openConnection", URL.class); 473 open.setAccessible(true); 474 return Utils.setupURLConnection((URLConnection) open.invoke(javaHttpHandler, url)); 475 } catch (Exception e) { 476 throw new IOException(e); 477 } 478 } 479 }; 480 @Override 481 public URLStreamHandler createURLStreamHandler(String protocol) { 482 if (protocol.equals("http")) { 483 return josmHttpHandler; 484 } else { 485 return (URLStreamHandler) javaHandlers.get(protocol); 486 } 487 } 488 }); 489 } 490 } catch (Throwable t) { 491 t.printStackTrace(); 492 } 493 } 424 494 } -
core/src/org/openstreetmap/josm/tools/Utils.java
585 585 * @since 5867 586 586 */ 587 587 public static InputStream openURL(URL url) throws IOException { 588 URLConnection connection = url.openConnection(); 589 connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString()); 590 connection.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000); 591 connection.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000); 592 return connection.getInputStream(); 588 return setupURLConnection(url.openConnection()).getInputStream(); 589 } 590 591 /*** 592 * Setups the given URL connection to match JOSM needs by setting its User-Agent and timeout properties. 593 * @param connection The connection to setup 594 * @return {@code connection}, with updated properties 595 * @since 596 */ 597 public static URLConnection setupURLConnection(URLConnection connection) { 598 if (connection != null) { 599 connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString()); 600 connection.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000); 601 connection.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000); 602 } 603 return connection; 593 604 } 594 605 595 606 /**
