Ticket #8606: 8606_ugly.patch

File 8606_ugly.patch, 6.9 KB (added by Don-vip, 13 years ago)

Worst Java code I ever wrote

  • core/src/org/openstreetmap/josm/gui/MainApplication.java

     
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55import static org.openstreetmap.josm.tools.I18n.trn;
     6import gnu.getopt.Getopt;
     7import gnu.getopt.LongOpt;
    68
    79import java.awt.Image;
    810import java.awt.Toolkit;
    911import java.awt.event.WindowAdapter;
    1012import java.awt.event.WindowEvent;
    1113import java.io.File;
     14import java.io.IOException;
     15import java.lang.reflect.Field;
     16import java.lang.reflect.Method;
    1217import java.net.Authenticator;
    1318import java.net.ProxySelector;
    1419import java.net.URL;
     20import java.net.URLConnection;
     21import java.net.URLStreamHandler;
     22import java.net.URLStreamHandlerFactory;
    1523import java.security.AllPermission;
    1624import java.security.CodeSource;
    1725import java.security.PermissionCollection;
     
    2028import java.util.ArrayList;
    2129import java.util.Collection;
    2230import java.util.HashMap;
     31import java.util.Hashtable;
    2332import java.util.LinkedList;
    2433import java.util.List;
    2534import java.util.Map;
     
    2837import javax.swing.RepaintManager;
    2938import javax.swing.SwingUtilities;
    3039
    31 import gnu.getopt.Getopt;
    32 import gnu.getopt.LongOpt;
    33 
    3440import org.jdesktop.swinghelper.debug.CheckThreadViolationRepaintManager;
    3541import org.openstreetmap.josm.Main;
    3642import org.openstreetmap.josm.data.AutosaveTask;
     
    283289            I18n.set(Main.pref.get("language", null));
    284290        }
    285291        Main.pref.updateSystemProperties();
     292       
     293        checkHttpUserAgent();
    286294
    287295        final JFrame mainFrame = new JFrame(tr("Java OpenStreetMap Editor"));
    288296        Main.parent = mainFrame;
     
    421429            RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager());
    422430        }
    423431    }
     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    }
    424494}
  • core/src/org/openstreetmap/josm/tools/Utils.java

     
    585585     * @since 5867
    586586     */
    587587    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;
    593604    }
    594605
    595606    /**