Ignore:
Timestamp:
2016-01-04T20:51:45+01:00 (9 years ago)
Author:
simon04
Message:

see #12292 - Allow to disconnect HttpClient in connecting phase

File:
1 edited

Legend:

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

    r9274 r9309  
    4747    private boolean useCache;
    4848    private String reasonForRequest;
     49    private transient HttpURLConnection connection; // to allow disconnecting before `response` is set
     50    private transient Response response;
    4951
    5052    private HttpClient(URL url, String requestMethod) {
     
    7577        }
    7678        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
     79        this.connection = connection;
    7780        connection.setRequestMethod(requestMethod);
    7881        connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString());
     
    148151                }
    149152            }
    150             Response response = new Response(connection, progressMonitor);
     153            response = new Response(connection, progressMonitor);
    151154            successfulConnection = true;
    152155            return response;
     
    156159            }
    157160        }
     161    }
     162
     163    /**
     164     * Returns the HTTP response which is set only after calling {@link #connect()}.
     165     * Calling this method again, returns the identical object (unless another {@link #connect()} is performed).
     166     *
     167     * @return the HTTP response
     168     * @since 9309
     169     */
     170    public Response getResponse() {
     171        return response;
    158172    }
    159173
     
    381395         */
    382396        public void disconnect() {
    383             // TODO is this block necessary for disconnecting?
    384             // Fix upload aborts - see #263
    385             connection.setConnectTimeout(100);
    386             connection.setReadTimeout(100);
    387             try {
    388                 Thread.sleep(100);
    389             } catch (InterruptedException ex) {
    390                 Main.warn("InterruptedException in " + getClass().getSimpleName() + " during cancel");
    391             }
    392 
    393             connection.disconnect();
     397            HttpClient.disconnect(connection);
    394398        }
    395399    }
     
    611615        }
    612616    }
     617
     618    /**
     619     * @see HttpURLConnection#disconnect()
     620     * @since 9309
     621     */
     622    public void disconnect() {
     623        HttpClient.disconnect(connection);
     624    }
     625
     626    private static void disconnect(final HttpURLConnection connection) {
     627        // Fix upload aborts - see #263
     628        connection.setConnectTimeout(100);
     629        connection.setReadTimeout(100);
     630        try {
     631            Thread.sleep(100);
     632        } catch (InterruptedException ex) {
     633            Main.warn("InterruptedException in " + HttpClient.class + " during cancel");
     634        }
     635        connection.disconnect();
     636    }
    613637}
Note: See TracChangeset for help on using the changeset viewer.