Changeset 1875 in josm for trunk/src/org


Ignore:
Timestamp:
2009-07-31T07:29:19+02:00 (15 years ago)
Author:
jttt
Message:

Fix #3134

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/progress/AbstractProgressMonitor.java

    r1872 r1875  
    9898
    9999    public synchronized void invalidate() {
    100         checkState(State.INIT);
    101         state = State.FINISHED;
    102         doFinishTask();
     100        if (state == State.INIT) {
     101            state = State.FINISHED;
     102            doFinishTask();
     103        }
    103104    }
    104105
  • trunk/src/org/openstreetmap/josm/gui/progress/ProgressMonitor.java

    r1812 r1875  
    5959     * Can be used if method receive ProgressMonitor but it's not interested progress monitoring.
    6060     * Basically replaces {@link #beginTask(String)} and {@link #finishTask()}
     61     *
     62     * This method can be also used in finally section if method expects that some exception
     63     * might prevent it from passing progressMonitor away. If {@link #beginTask(String)} was
     64     * already called then this method does nothing.
    6165     */
    6266    void invalidate();
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r1811 r1875  
    3939     */
    4040    protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException  {
    41         api.initialize();
    42         urlStr = api.getBaseUrl() + urlStr;
    43         return getInputStreamRaw(urlStr, progressMonitor);
     41        try {
     42            api.initialize();
     43            urlStr = api.getBaseUrl() + urlStr;
     44            return getInputStreamRaw(urlStr, progressMonitor);
     45        } finally {
     46            progressMonitor.invalidate();
     47        }
    4448    }
    4549
    4650    protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
    47         URL url = null;
    4851        try {
    49             url = new URL(urlStr);
    50         } catch(MalformedURLException e) {
    51             throw new OsmTransferException(e);
    52         }
    53         try {
    54             activeConnection = (HttpURLConnection)url.openConnection();
    55         } catch(Exception e) {
    56             throw new OsmTransferException(tr("Failed to open connection to API {0}", url.toExternalForm()), e);
    57         }
    58         if (cancel) {
    59             activeConnection.disconnect();
    60             return null;
    61         }
     52            URL url = null;
     53            try {
     54                url = new URL(urlStr);
     55            } catch(MalformedURLException e) {
     56                throw new OsmTransferException(e);
     57            }
     58            try {
     59                activeConnection = (HttpURLConnection)url.openConnection();
     60            } catch(Exception e) {
     61                throw new OsmTransferException(tr("Failed to open connection to API {0}", url.toExternalForm()), e);
     62            }
     63            if (cancel) {
     64                activeConnection.disconnect();
     65                return null;
     66            }
    6267
    63         if (Main.pref.getBoolean("osm-server.use-compression", true)) {
    64             activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
    65         }
     68            if (Main.pref.getBoolean("osm-server.use-compression", true)) {
     69                activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
     70            }
    6671
    67         activeConnection.setConnectTimeout(15000);
     72            activeConnection.setConnectTimeout(15000);
    6873
    69         try {
    70             System.out.println("GET " + url);
    71             activeConnection.connect();
    72         } catch (Exception e) {
    73             throw new OsmTransferException(tr("Couldn't connect to the osm server. Please check your internet connection."), e);
    74         }
    75         try {
    76             if (isAuthCancelled() && activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
    77                 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED,null,null);
     74            try {
     75                System.out.println("GET " + url);
     76                activeConnection.connect();
     77            } catch (Exception e) {
     78                throw new OsmTransferException(tr("Couldn't connect to the osm server. Please check your internet connection."), e);
     79            }
     80            try {
     81                if (isAuthCancelled() && activeConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
     82                    throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED,null,null);
    7883
    79             if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
    80                 String errorHeader = activeConnection.getHeaderField("Error");
    81                 InputStream i = null;
    82                 i = activeConnection.getErrorStream();
    83                 StringBuilder errorBody = new StringBuilder();
    84                 if (i != null) {
    85                     BufferedReader in = new BufferedReader(new InputStreamReader(i));
    86                     String s;
    87                     while((s = in.readLine()) != null) {
    88                         errorBody.append(s);
    89                         errorBody.append("\n");
     84                if (activeConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
     85                    String errorHeader = activeConnection.getHeaderField("Error");
     86                    InputStream i = null;
     87                    i = activeConnection.getErrorStream();
     88                    StringBuilder errorBody = new StringBuilder();
     89                    if (i != null) {
     90                        BufferedReader in = new BufferedReader(new InputStreamReader(i));
     91                        String s;
     92                        while((s = in.readLine()) != null) {
     93                            errorBody.append(s);
     94                            errorBody.append("\n");
     95                        }
    9096                    }
     97
     98                    throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString());
    9199                }
    92100
    93                 throw new OsmApiException(activeConnection.getResponseCode(), errorHeader, errorBody.toString());
     101                String encoding = activeConnection.getContentEncoding();
     102                InputStream inputStream = new ProgressInputStream(activeConnection, progressMonitor);
     103                if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
     104                    inputStream = new GZIPInputStream(inputStream);
     105                }
     106                else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
     107                    inputStream = new InflaterInputStream(inputStream, new Inflater(true));
     108                }
     109                return inputStream;
     110            } catch(Exception e) {
     111                if (e instanceof OsmTransferException)
     112                    throw (OsmTransferException)e;
     113                else
     114                    throw new OsmTransferException(e);
     115
    94116            }
    95 
    96             String encoding = activeConnection.getContentEncoding();
    97             InputStream inputStream = new ProgressInputStream(activeConnection, progressMonitor);
    98             if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
    99                 inputStream = new GZIPInputStream(inputStream);
    100             }
    101             else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
    102                 inputStream = new InflaterInputStream(inputStream, new Inflater(true));
    103             }
    104             return inputStream;
    105         } catch(Exception e) {
    106             if (e instanceof OsmTransferException)
    107                 throw (OsmTransferException)e;
    108             else
    109                 throw new OsmTransferException(e);
    110 
     117        } finally {
     118            progressMonitor.invalidate();
    111119        }
    112120    }
  • trunk/src/org/openstreetmap/josm/io/ProgressInputStream.java

    r1811 r1875  
    3636            this.in = con.getInputStream();
    3737        } catch (IOException e) {
     38            progressMonitor.finishTask();
    3839            if (con.getHeaderField("Error") != null)
    3940                throw new OsmTransferException(tr(con.getHeaderField("Error")));
Note: See TracChangeset for help on using the changeset viewer.