Changeset 5868 in josm


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

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

Location:
trunk/src/org/openstreetmap/josm
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Version.java

    r5850 r5868  
    66import java.io.BufferedReader;
    77import java.io.IOException;
    8 import java.io.InputStreamReader;
    98import java.net.URL;
    109import java.util.HashMap;
     
    1514import org.openstreetmap.josm.Main;
    1615import org.openstreetmap.josm.tools.LanguageInfo;
     16import org.openstreetmap.josm.tools.Utils;
    1717
    1818/**
     
    3535    static public String loadResourceFile(URL resource) {
    3636        if (resource == null) return null;
    37         BufferedReader in;
    3837        String s = null;
    3938        try {
    40             in = new BufferedReader(new InputStreamReader(resource.openStream(), "UTF-8"));
     39            BufferedReader in = Utils.openURLReader(resource);
    4140            StringBuffer sb = new StringBuffer();
    4241            try {
     
    227226        return "JOSM/1.5 ("+ s+" "+LanguageInfo.getJOSMLocaleCode()+") " + Main.platform.getOSDescription();
    228227    }
     228
     229    /**
     230     * Returns the full User-Agent string
     231     * @return The User-Agent
     232     * @since 5866
     233     */
     234    public String getFullAgentString() {
     235        return getAgentString() + " Java/"+System.getProperty("java.version");
     236    }
    229237}
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r5831 r5868  
    5151import org.openstreetmap.josm.tools.I18n;
    5252import org.openstreetmap.josm.tools.ImageProvider;
     53import org.openstreetmap.josm.tools.Utils;
    5354
    5455/**
     
    292293                System.out.println("Reading preferences from " + i);
    293294                try {
    294                     URL url = new URL(i);
    295                     config.openAndReadXML(url.openStream());
     295                    config.openAndReadXML(Utils.openURL(new URL(i)));
    296296                } catch (Exception ex) {
    297297                    throw new RuntimeException(ex);
  • trunk/src/org/openstreetmap/josm/gui/help/HelpContentReader.java

    r5587 r5868  
    2121 * It also has to be <strong>transformed</strong> because the internal help browser required slightly
    2222 * different HTML than what is provided by the Wiki.
    23  *
    24  * @see WikiReader
    2523 */
    26 public class HelpContentReader {
    27 
    28     /** the base url */
    29     private String baseUrl;
     24public class HelpContentReader extends WikiReader {
    3025
    3126    /**
     
    3530     */
    3631    public HelpContentReader(String baseUrl) {
    37         this.baseUrl = baseUrl;
     32        super(baseUrl);
    3833    }
    3934
     
    9085     */
    9186    protected String prepareHelpContent(BufferedReader in, boolean dotest) throws HelpContentReaderException {
    92         boolean isInContent = false;
    93         boolean isInTranslationsSideBar = false;
    94         boolean isExistingHelpPage = false;
    95         StringBuffer sball = new StringBuffer();
    96         StringBuffer sb = new StringBuffer();
     87        String s = "";
    9788        try {
    98             for (String line = in.readLine(); line != null; line = in.readLine()) {
    99                 sball.append(line);
    100                 sball.append("\n");
    101                 if (line.contains("<div id=\"searchable\">")) {
    102                     isInContent = true;
    103                 } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) {
    104                     isInTranslationsSideBar = true;
    105                 } else if (line.contains("<div class=\"wikipage searchable\">")) {
    106                     isInContent = true;
    107                 } else if (line.contains("<div class=\"buttons\">")) {
    108                     isInContent = false;
    109                 } else if (line.contains("<h3>Attachments</h3>")) {
    110                     isInContent = false;
    111                 } else if (line.contains("<div id=\"attachments\">")) {
    112                     isInContent = false;
    113                 } else if (line.contains("<div class=\"trac-modifiedby\">")) {
    114                     continue;
    115                 } else if (line.contains("<input type=\"submit\" name=\"attachfilebutton\"")) {
    116                     // heuristic: if we find a button for uploading images we are in an
    117                     // existing pages. Otherwise this is probably the stub page for a not yet
    118                     // existing help page
    119                     isExistingHelpPage = true;
    120                 }
    121                 if (isInContent && !isInTranslationsSideBar) {
    122                     // add a border="0" attribute to images, otherwise the internal help browser
    123                     // will render a thick  border around images inside an <a> element
    124                     //
    125                     // Also make sure image URLs are absolute
    126                     //
    127                     line = line.replaceAll("<img ([^>]*)src=\"/", "<img border=\"0\" \\1src=\"" + baseUrl + "/").replaceAll("href=\"/",
    128                             "href=\"" + baseUrl + "/").replaceAll(" />", ">");
    129                     sb.append(line);
    130                     sb.append("\n");
    131                 } else if (isInTranslationsSideBar && line.contains("</div>")) {
    132                     isInTranslationsSideBar = false;
    133                 }
    134             }
     89            s = readFromTrac(in);
    13590        } catch(IOException e) {
    13691            throw new HelpContentReaderException(e);
    13792        }
    138         if(!dotest && sb.length() == 0)
    139             sb = sball;
    140         else if (dotest && !isExistingHelpPage)
     93        if(dotest && s.isEmpty())
    14194            throw new MissingHelpContentException();
    142         sb.insert(0, "<html>");
    143         sb.append("<html>");
    144         return sb.toString();
     95        return s;
    14596    }
    14697}
  • trunk/src/org/openstreetmap/josm/gui/layer/TMSLayer.java

    r5839 r5868  
    7777import org.openstreetmap.josm.io.OsmTransferException;
    7878import org.openstreetmap.josm.io.UTFInputStreamReader;
     79import org.openstreetmap.josm.tools.Utils;
    7980import org.xml.sax.InputSource;
    8081import org.xml.sax.SAXException;
     
    321322            protected byte[] updateData() throws IOException {
    322323                URL u = getAttributionUrl();
    323                 UTFInputStreamReader in = UTFInputStreamReader.create(u.openStream(), "utf-8");
     324                UTFInputStreamReader in = UTFInputStreamReader.create(Utils.openURL(u), "utf-8");
    324325                String r = new Scanner(in).useDelimiter("\\A").next();
    325326                in.close();
  • trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java

    r5587 r5868  
    7171            } else {
    7272                if (Main.applet) {
    73                     URLConnection conn = url.openConnection();
    74                     conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
    75                     conn.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000);
    76                     fs = new BufferedInputStream(conn.getInputStream());
     73                    fs = new BufferedInputStream(Utils.openURL(url));
    7774                    file = new File(url.getFile());
    7875                } else {
  • 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.