Changeset 6867 in josm


Ignore:
Timestamp:
2014-02-18T00:35:15+01:00 (10 years ago)
Author:
Don-vip
Message:

fix #9715 - Set HTTP Accept header to expected MIME types when downloading presets, styles, plugins

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r6830 r6867  
    1010import java.util.Arrays;
    1111import java.util.Collection;
     12import java.util.HashSet;
    1213import java.util.LinkedList;
    1314import java.util.List;
     15import java.util.Set;
    1416import java.util.concurrent.CopyOnWriteArrayList;
    1517
     
    217219        MirroredInputStream in = null;
    218220        try {
    219             in = new MirroredInputStream(entry.url);
     221            Set<String> mimes = new HashSet<String>();
     222            mimes.addAll(Arrays.asList(XmlStyleSource.XML_STYLE_MIME_TYPES.split(", ")));
     223            mimes.addAll(Arrays.asList(MapCSSStyleSource.MAPCSS_STYLE_MIME_TYPES.split(", ")));
     224            in = new MirroredInputStream(entry.url, null, Utils.join(", ", mimes));
    220225            String zipEntryPath = in.findZipEntryPath("mapcss", "style");
    221226            if (zipEntryPath != null) {
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r6736 r6867  
    3434
    3535public class MapCSSStyleSource extends StyleSource {
     36
     37    /**
     38     * The accepted MIME types sent in the HTTP Accept header.
     39     * @since 6867
     40     */
     41    public static final String MAPCSS_STYLE_MIME_TYPES = "text/mapcss, text/css; q=0.9, text/plain; q=0.8, application/zip, application/octet-stream; q=0.5";
     42
    3643    public final List<MapCSSRule> rules;
    3744    private Color backgroundColorOverride;
     
    97104            return new ByteArrayInputStream(css.getBytes(Utils.UTF_8));
    98105        }
    99         MirroredInputStream in = new MirroredInputStream(url);
     106        MirroredInputStream in = new MirroredInputStream(url, null, MAPCSS_STYLE_MIME_TYPES);
    100107        if (isZip) {
    101108            File file = in.getFile();
  • trunk/src/org/openstreetmap/josm/gui/mappaint/xml/XmlStyleSource.java

    r6643 r6867  
    3535public class XmlStyleSource extends StyleSource implements StyleKeys {
    3636
     37    /**
     38     * The accepted MIME types sent in the HTTP Accept header.
     39     * @since 6867
     40     */
     41    public static final String XML_STYLE_MIME_TYPES = "application/xml, text/xml, text/plain; q=0.8, application/zip, application/octet-stream; q=0.5";
     42
    3743    protected final Map<String, IconPrototype> icons = new HashMap<String, IconPrototype>();
    3844    protected final Map<String, LinePrototype> lines = new HashMap<String, LinePrototype>();
     
    98104    @Override
    99105    public InputStream getSourceInputStream() throws IOException {
    100         MirroredInputStream in = new MirroredInputStream(url);
     106        MirroredInputStream in = new MirroredInputStream(url, null, XML_STYLE_MIME_TYPES);
    101107        InputStream zip = in.findZipEntryInputStream("xml", "style");
    102108        if (zip != null) {
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetReader.java

    r6856 r6867  
    3535public final class TaggingPresetReader {
    3636
     37    /**
     38     * The accepted MIME types sent in the HTTP Accept header.
     39     * @since 6867
     40     */
     41    public static final String PRESET_MIME_TYPES = "application/xml, text/xml, text/plain; q=0.8, application/zip, application/octet-stream; q=0.5";
     42   
    3743    private TaggingPresetReader() {
    3844        // Hide default constructor for utils classes
     
    219225    public static Collection<TaggingPreset> readAll(String source, boolean validate) throws SAXException, IOException {
    220226        Collection<TaggingPreset> tp;
    221         MirroredInputStream s = new MirroredInputStream(source);
     227        MirroredInputStream s = new MirroredInputStream(source, null, PRESET_MIME_TYPES);
    222228        try {
    223229            InputStream zip = s.findZipEntryInputStream("xml","preset");
  • trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java

    r6642 r6867  
    3636    public final static long DEFAULT_MAXTIME = -1L;
    3737
     38    /**
     39     * Constructs an input stream from a given filename, URL or internal resource.
     40     *
     41     * @param name can be:<ul>
     42     *  <li>relative or absolute file name</li>
     43     *  <li>{@code file:///SOME/FILE} the same as above</li>
     44     *  <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
     45     *  <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
     46     * @throws IOException when the resource with the given name could not be retrieved
     47     */
    3848    public MirroredInputStream(String name) throws IOException {
    39         this(name, null, DEFAULT_MAXTIME);
    40     }
    41 
     49        this(name, null, DEFAULT_MAXTIME, null);
     50    }
     51
     52    /**
     53     * Constructs an input stream from a given filename, URL or internal resource.
     54     *
     55     * @param name can be:<ul>
     56     *  <li>relative or absolute file name</li>
     57     *  <li>{@code file:///SOME/FILE} the same as above</li>
     58     *  <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
     59     *  <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
     60     * @param maxTime the maximum age of the cache file (in seconds)
     61     * @throws IOException when the resource with the given name could not be retrieved
     62     */
    4263    public MirroredInputStream(String name, long maxTime) throws IOException {
    43         this(name, null, maxTime);
    44     }
    45 
     64        this(name, null, maxTime, null);
     65    }
     66
     67    /**
     68     * Constructs an input stream from a given filename, URL or internal resource.
     69     *
     70     * @param name can be:<ul>
     71     *  <li>relative or absolute file name</li>
     72     *  <li>{@code file:///SOME/FILE} the same as above</li>
     73     *  <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
     74     *  <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
     75     * @param destDir the destination directory for the cache file. Only applies for URLs.
     76     * @throws IOException when the resource with the given name could not be retrieved
     77     */
    4678    public MirroredInputStream(String name, String destDir) throws IOException {
    47         this(name, destDir, DEFAULT_MAXTIME);
    48     }
    49 
    50     /**
    51      * Get an inputstream from a given filename, url or internal resource.
    52      * @param name can be
    53      *  - relative or absolute file name
    54      *  - file:///SOME/FILE the same as above
    55      *  - resource://SOME/FILE file from the classpath (usually in the current *.jar)
    56      *  - http://... a url. It will be cached on disk.
    57      * @param destDir the destination directory for the cache file. only applies for urls.
     79        this(name, destDir, DEFAULT_MAXTIME, null);
     80    }
     81
     82    /**
     83     * Constructs an input stream from a given filename, URL or internal resource.
     84     *
     85     * @param name can be:<ul>
     86     *  <li>relative or absolute file name</li>
     87     *  <li>{@code file:///SOME/FILE} the same as above</li>
     88     *  <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
     89     *  <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
     90     * @param destDir the destination directory for the cache file. Only applies for URLs.
    5891     * @param maxTime the maximum age of the cache file (in seconds)
    5992     * @throws IOException when the resource with the given name could not be retrieved
    6093     */
    6194    public MirroredInputStream(String name, String destDir, long maxTime) throws IOException {
     95        this(name, destDir, maxTime, null);
     96    }
     97
     98    /**
     99     * Constructs an input stream from a given filename, URL or internal resource.
     100     *
     101     * @param name can be:<ul>
     102     *  <li>relative or absolute file name</li>
     103     *  <li>{@code file:///SOME/FILE} the same as above</li>
     104     *  <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
     105     *  <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
     106     * @param destDir the destination directory for the cache file. Only applies for URLs.
     107     * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Only applies for URLs.
     108     * @throws IOException when the resource with the given name could not be retrieved
     109     * @since 6867
     110     */
     111    public MirroredInputStream(String name, String destDir, String httpAccept) throws IOException {
     112        this(name, destDir, DEFAULT_MAXTIME, httpAccept);
     113    }
     114
     115    /**
     116     * Constructs an input stream from a given filename, URL or internal resource.
     117     *
     118     * @param name can be:<ul>
     119     *  <li>relative or absolute file name</li>
     120     *  <li>{@code file:///SOME/FILE} the same as above</li>
     121     *  <li>{@code resource://SOME/FILE} file from the classpath (usually in the current *.jar)</li>
     122     *  <li>{@code http://...} a URL. It will be cached on disk.</li></ul>
     123     * @param destDir the destination directory for the cache file. Only applies for URLs.
     124     * @param maxTime the maximum age of the cache file (in seconds)
     125     * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Only applies for URLs.
     126     * @throws IOException when the resource with the given name could not be retrieved
     127     * @since 6867
     128     */
     129    public MirroredInputStream(String name, String destDir, long maxTime, String httpAccept) throws IOException {
    62130        URL url;
    63131        try {
     
    73141                    file = new File(url.getFile());
    74142                } else {
    75                     file = checkLocal(url, destDir, maxTime);
     143                    file = checkLocal(url, destDir, maxTime, httpAccept);
    76144                }
    77145            }
     
    201269    }
    202270
    203     private File checkLocal(URL url, String destDir, long maxTime) throws IOException {
     271    private File checkLocal(URL url, String destDir, long maxTime, String httpAccept) throws IOException {
    204272        String prefKey = getPrefKey(url, destDir);
    205273        long age = 0L;
     
    237305        BufferedInputStream bis = null;
    238306        try {
    239             HttpURLConnection con = connectFollowingRedirect(url);
     307            HttpURLConnection con = connectFollowingRedirect(url, httpAccept);
    240308            bis = new BufferedInputStream(con.getInputStream());
    241309            FileOutputStream fos = new FileOutputStream(destDirFile);
     
    284352     *
    285353     * @param downloadUrl The resource URL to download
     354     * @param httpAccept The accepted MIME types sent in the HTTP Accept header. Can be {@code null}
    286355     * @return The HTTP connection effectively linked to the resource, after all potential redirections
    287356     * @throws MalformedURLException If a redirected URL is wrong
    288357     * @throws IOException If any I/O operation goes wrong
    289      * @since 6073
    290      */
    291     public static HttpURLConnection connectFollowingRedirect(URL downloadUrl) throws MalformedURLException, IOException {
     358     * @since 6867
     359     */
     360    public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept) throws MalformedURLException, IOException {
    292361        HttpURLConnection con = null;
    293362        int numRedirects = 0;
     
    297366            con.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
    298367            con.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000);
     368            Main.debug("GET "+downloadUrl);
     369            if (httpAccept != null) {
     370                Main.debug("Accept: "+httpAccept);
     371                con.setRequestProperty("Accept", httpAccept);
     372            }
    299373            try {
    300374                con.connect();
  • trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java

    r6643 r6867  
    3636 */
    3737public class PluginDownloadTask extends PleaseWaitRunnable{
     38
     39    /**
     40     * The accepted MIME types sent in the HTTP Accept header.
     41     * @since 6867
     42     */
     43    public static final String PLUGIN_MIME_TYPES = "application/java-archive, application/zip; q=0.9, application/octet-stream; q=0.5";
     44   
    3845    private final Collection<PluginInformation> toUpdate = new LinkedList<PluginInformation>();
    3946    private final Collection<PluginInformation> failed = new LinkedList<PluginInformation>();
     
    120127            URL url = new URL(pi.downloadlink);
    121128            synchronized(this) {
    122                 downloadConnection = MirroredInputStream.connectFollowingRedirect(url);
     129                downloadConnection = MirroredInputStream.connectFollowingRedirect(url, PLUGIN_MIME_TYPES);
    123130            }
    124131            in = downloadConnection.getInputStream();
Note: See TracChangeset for help on using the changeset viewer.