Ignore:
Timestamp:
2013-04-15T19:12:01+02:00 (11 years ago)
Author:
stoecker
Message:

see #8606 - use JOSM agent also for WebStart, join help browser and WikiReader

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r5819 r5868  
    1010import java.awt.datatransfer.Transferable;
    1111import java.awt.datatransfer.UnsupportedFlavorException;
     12import java.io.BufferedReader;
    1213import java.io.File;
    1314import java.io.IOException;
    1415import java.io.InputStream;
     16import java.io.InputStreamReader;
    1517import java.io.OutputStream;
    1618import java.io.Reader;
     
    1820import java.net.HttpURLConnection;
    1921import java.net.URL;
     22import java.net.URLConnection;
    2023import java.security.MessageDigest;
    2124import java.security.NoSuchAlgorithmException;
     
    2831import java.util.List;
    2932
     33import org.openstreetmap.josm.Main;
    3034import org.openstreetmap.josm.data.Version;
    3135
     
    554558        }
    555559        HttpURLConnection connection = (HttpURLConnection) httpURL.openConnection();
     560        connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString());
    556561        return connection;
    557562    }
    558563   
     564    /**
     565     * Opens a connection to the given URL and sets the User-Agent property to JOSM's one.
     566     * @param url The url to open
     567     * @return An stream for the given URL
     568     * @throws IOException if an I/O exception occurs.
     569     * @since 5867
     570     */
     571    public static InputStream openURL(URL url) throws IOException {
     572        URLConnection connection = url.openConnection();
     573        connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString());
     574        connection.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
     575        connection.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000);
     576        return connection.getInputStream();
     577    }
     578
     579    /**
     580     * Opens a connection to the given URL and sets the User-Agent property to JOSM's one.
     581     * @param url The url to open
     582     * @return An buffered stream reader for the given URL (using UTF-8)
     583     * @throws IOException if an I/O exception occurs.
     584     * @since 5867
     585     */
     586    public static BufferedReader openURLReader(URL url) throws IOException {
     587        return new BufferedReader(new InputStreamReader(openURL(url), "utf-8"));
     588    }
     589
    559590    /**
    560591     * Opens a HTTP connection to the given URL, sets the User-Agent property to JOSM's one and optionnaly disables Keep-Alive.
  • trunk/src/org/openstreetmap/josm/tools/WikiReader.java

    r5834 r5868  
    55import java.io.IOException;
    66import java.io.InputStream;
    7 import java.io.InputStreamReader;
    87import java.net.URL;
    98
     
    3635     */
    3736    public String read(String url) throws IOException {
    38         BufferedReader in = new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8"));
     37        BufferedReader in = Utils.openURLReader(new URL(url));
    3938        try {
    4039            if (url.startsWith(baseurl) && !url.endsWith("?format=txt"))
     
    6059
    6160    private String readLang(URL url) throws IOException {
    62         InputStream in = url.openStream();
     61        BufferedReader in = Utils.openURLReader(url);
    6362        try {
    64             return readFromTrac(new BufferedReader(new InputStreamReader(in, "utf-8")));
     63            return readFromTrac(in);
    6564        } finally {
    6665            in.close();
     
    7877    }
    7978
    80     private String readFromTrac(BufferedReader in) throws IOException {
     79    protected String readFromTrac(BufferedReader in) throws IOException {
    8180        boolean inside = false;
    8281        boolean transl = false;
Note: See TracChangeset for help on using the changeset viewer.