[9168] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.io.BufferedOutputStream;
|
---|
| 7 | import java.io.BufferedReader;
|
---|
[11250] | 8 | import java.io.ByteArrayInputStream;
|
---|
[9168] | 9 | import java.io.IOException;
|
---|
| 10 | import java.io.InputStream;
|
---|
| 11 | import java.io.OutputStream;
|
---|
[9935] | 12 | import java.net.CookieHandler;
|
---|
| 13 | import java.net.CookieManager;
|
---|
[9168] | 14 | import java.net.HttpURLConnection;
|
---|
| 15 | import java.net.URL;
|
---|
[9807] | 16 | import java.util.Collections;
|
---|
[9172] | 17 | import java.util.List;
|
---|
[9274] | 18 | import java.util.Locale;
|
---|
[9168] | 19 | import java.util.Map;
|
---|
[9807] | 20 | import java.util.Map.Entry;
|
---|
[11553] | 21 | import java.util.Optional;
|
---|
[9171] | 22 | import java.util.Scanner;
|
---|
[9172] | 23 | import java.util.TreeMap;
|
---|
[11288] | 24 | import java.util.concurrent.TimeUnit;
|
---|
[9172] | 25 | import java.util.regex.Matcher;
|
---|
| 26 | import java.util.regex.Pattern;
|
---|
[9168] | 27 | import java.util.zip.GZIPInputStream;
|
---|
| 28 |
|
---|
| 29 | import org.openstreetmap.josm.Main;
|
---|
| 30 | import org.openstreetmap.josm.data.Version;
|
---|
[9185] | 31 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
[9179] | 32 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[9169] | 33 | import org.openstreetmap.josm.io.Compression;
|
---|
[9185] | 34 | import org.openstreetmap.josm.io.ProgressInputStream;
|
---|
| 35 | import org.openstreetmap.josm.io.ProgressOutputStream;
|
---|
[9171] | 36 | import org.openstreetmap.josm.io.UTFInputStreamReader;
|
---|
[9396] | 37 | import org.openstreetmap.josm.io.auth.DefaultAuthenticator;
|
---|
[9168] | 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Provides a uniform access for a HTTP/HTTPS server. This class should be used in favour of {@link HttpURLConnection}.
|
---|
[9177] | 41 | * @since 9168
|
---|
[9168] | 42 | */
|
---|
[9173] | 43 | public final class HttpClient {
|
---|
[9168] | 44 |
|
---|
| 45 | private URL url;
|
---|
| 46 | private final String requestMethod;
|
---|
[11288] | 47 | private int connectTimeout = (int) TimeUnit.SECONDS.toMillis(Main.pref.getInteger("socket.timeout.connect", 15));
|
---|
| 48 | private int readTimeout = (int) TimeUnit.SECONDS.toMillis(Main.pref.getInteger("socket.timeout.read", 30));
|
---|
[9168] | 49 | private byte[] requestBody;
|
---|
| 50 | private long ifModifiedSince;
|
---|
[9172] | 51 | private final Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
---|
[9168] | 52 | private int maxRedirects = Main.pref.getInteger("socket.maxredirects", 5);
|
---|
[9171] | 53 | private boolean useCache;
|
---|
[9172] | 54 | private String reasonForRequest;
|
---|
[12711] | 55 | private String outputMessage = tr("Uploading data ...");
|
---|
[9977] | 56 | private HttpURLConnection connection; // to allow disconnecting before `response` is set
|
---|
| 57 | private Response response;
|
---|
[10302] | 58 | private boolean finishOnCloseOutput = true;
|
---|
[9168] | 59 |
|
---|
[9935] | 60 | static {
|
---|
| 61 | CookieHandler.setDefault(new CookieManager());
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[9168] | 64 | private HttpClient(URL url, String requestMethod) {
|
---|
| 65 | this.url = url;
|
---|
| 66 | this.requestMethod = requestMethod;
|
---|
[9172] | 67 | this.headers.put("Accept-Encoding", "gzip");
|
---|
[9168] | 68 | }
|
---|
| 69 |
|
---|
[9177] | 70 | /**
|
---|
| 71 | * Opens the HTTP connection.
|
---|
| 72 | * @return HTTP response
|
---|
| 73 | * @throws IOException if any I/O error occurs
|
---|
| 74 | */
|
---|
[9168] | 75 | public Response connect() throws IOException {
|
---|
[9179] | 76 | return connect(null);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * Opens the HTTP connection.
|
---|
[9185] | 81 | * @param progressMonitor progress monitor
|
---|
[9179] | 82 | * @return HTTP response
|
---|
| 83 | * @throws IOException if any I/O error occurs
|
---|
| 84 | * @since 9179
|
---|
| 85 | */
|
---|
[9185] | 86 | public Response connect(ProgressMonitor progressMonitor) throws IOException {
|
---|
| 87 | if (progressMonitor == null) {
|
---|
| 88 | progressMonitor = NullProgressMonitor.INSTANCE;
|
---|
| 89 | }
|
---|
[9168] | 90 | final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
---|
[9309] | 91 | this.connection = connection;
|
---|
[9172] | 92 | connection.setRequestMethod(requestMethod);
|
---|
[9168] | 93 | connection.setRequestProperty("User-Agent", Version.getInstance().getFullAgentString());
|
---|
| 94 | connection.setConnectTimeout(connectTimeout);
|
---|
| 95 | connection.setReadTimeout(readTimeout);
|
---|
[9255] | 96 | connection.setInstanceFollowRedirects(false); // we do that ourselves
|
---|
[9168] | 97 | if (ifModifiedSince > 0) {
|
---|
| 98 | connection.setIfModifiedSince(ifModifiedSince);
|
---|
| 99 | }
|
---|
[9171] | 100 | connection.setUseCaches(useCache);
|
---|
| 101 | if (!useCache) {
|
---|
| 102 | connection.setRequestProperty("Cache-Control", "no-cache");
|
---|
| 103 | }
|
---|
[9168] | 104 | for (Map.Entry<String, String> header : headers.entrySet()) {
|
---|
[9172] | 105 | if (header.getValue() != null) {
|
---|
| 106 | connection.setRequestProperty(header.getKey(), header.getValue());
|
---|
| 107 | }
|
---|
[9168] | 108 | }
|
---|
| 109 |
|
---|
[9185] | 110 | progressMonitor.beginTask(tr("Contacting Server..."), 1);
|
---|
| 111 | progressMonitor.indeterminateSubTask(null);
|
---|
[9179] | 112 |
|
---|
[9172] | 113 | if ("PUT".equals(requestMethod) || "POST".equals(requestMethod) || "DELETE".equals(requestMethod)) {
|
---|
[12620] | 114 | Logging.info("{0} {1} ({2}) ...", requestMethod, url, Utils.getSizeString(requestBody.length, Locale.getDefault()));
|
---|
[9314] | 115 | connection.setFixedLengthStreamingMode(requestBody.length);
|
---|
[9172] | 116 | connection.setDoOutput(true);
|
---|
[9185] | 117 | try (OutputStream out = new BufferedOutputStream(
|
---|
[12711] | 118 | new ProgressOutputStream(connection.getOutputStream(), requestBody.length,
|
---|
| 119 | progressMonitor, outputMessage, finishOnCloseOutput))) {
|
---|
[9172] | 120 | out.write(requestBody);
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[9168] | 124 | boolean successfulConnection = false;
|
---|
| 125 | try {
|
---|
| 126 | try {
|
---|
| 127 | connection.connect();
|
---|
[9200] | 128 | final boolean hasReason = reasonForRequest != null && !reasonForRequest.isEmpty();
|
---|
[12620] | 129 | Logging.info("{0} {1}{2} -> {3}{4}",
|
---|
[10300] | 130 | requestMethod, url, hasReason ? (" (" + reasonForRequest + ')') : "",
|
---|
[9184] | 131 | connection.getResponseCode(),
|
---|
[9274] | 132 | connection.getContentLengthLong() > 0
|
---|
[10300] | 133 | ? (" (" + Utils.getSizeString(connection.getContentLengthLong(), Locale.getDefault()) + ')')
|
---|
[9274] | 134 | : ""
|
---|
[9184] | 135 | );
|
---|
[12620] | 136 | if (Logging.isDebugEnabled()) {
|
---|
| 137 | Logging.debug("RESPONSE: {0}", connection.getHeaderFields());
|
---|
[9172] | 138 | }
|
---|
[9396] | 139 | if (DefaultAuthenticator.getInstance().isEnabled() && connection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
|
---|
| 140 | DefaultAuthenticator.getInstance().addFailedCredentialHost(url.getHost());
|
---|
| 141 | }
|
---|
[9168] | 142 | } catch (IOException e) {
|
---|
[12620] | 143 | Logging.info("{0} {1} -> !!!", requestMethod, url);
|
---|
| 144 | Logging.warn(e);
|
---|
[9168] | 145 | //noinspection ThrowableResultOfMethodCallIgnored
|
---|
| 146 | Main.addNetworkError(url, Utils.getRootCause(e));
|
---|
| 147 | throw e;
|
---|
| 148 | }
|
---|
| 149 | if (isRedirect(connection.getResponseCode())) {
|
---|
| 150 | final String redirectLocation = connection.getHeaderField("Location");
|
---|
| 151 | if (redirectLocation == null) {
|
---|
| 152 | /* I18n: argument is HTTP response code */
|
---|
[11553] | 153 | throw new IOException(tr("Unexpected response from HTTP server. Got {0} response without ''Location'' header." +
|
---|
| 154 | " Can''t redirect. Aborting.", connection.getResponseCode()));
|
---|
[9168] | 155 | } else if (maxRedirects > 0) {
|
---|
[9255] | 156 | url = new URL(url, redirectLocation);
|
---|
[9168] | 157 | maxRedirects--;
|
---|
[12620] | 158 | Logging.info(tr("Download redirected to ''{0}''", redirectLocation));
|
---|
[9168] | 159 | return connect();
|
---|
[9172] | 160 | } else if (maxRedirects == 0) {
|
---|
[9168] | 161 | String msg = tr("Too many redirects to the download URL detected. Aborting.");
|
---|
| 162 | throw new IOException(msg);
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
[9309] | 165 | response = new Response(connection, progressMonitor);
|
---|
[9168] | 166 | successfulConnection = true;
|
---|
| 167 | return response;
|
---|
| 168 | } finally {
|
---|
| 169 | if (!successfulConnection) {
|
---|
| 170 | connection.disconnect();
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /**
|
---|
[9309] | 176 | * Returns the HTTP response which is set only after calling {@link #connect()}.
|
---|
| 177 | * Calling this method again, returns the identical object (unless another {@link #connect()} is performed).
|
---|
| 178 | *
|
---|
| 179 | * @return the HTTP response
|
---|
| 180 | * @since 9309
|
---|
| 181 | */
|
---|
| 182 | public Response getResponse() {
|
---|
| 183 | return response;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /**
|
---|
[9168] | 187 | * A wrapper for the HTTP response.
|
---|
| 188 | */
|
---|
[9173] | 189 | public static final class Response {
|
---|
[9168] | 190 | private final HttpURLConnection connection;
|
---|
[9185] | 191 | private final ProgressMonitor monitor;
|
---|
[9168] | 192 | private final int responseCode;
|
---|
[9172] | 193 | private final String responseMessage;
|
---|
[9169] | 194 | private boolean uncompress;
|
---|
[9172] | 195 | private boolean uncompressAccordingToContentDisposition;
|
---|
[11277] | 196 | private String responseData;
|
---|
[9168] | 197 |
|
---|
[9185] | 198 | private Response(HttpURLConnection connection, ProgressMonitor monitor) throws IOException {
|
---|
[9172] | 199 | CheckParameterUtil.ensureParameterNotNull(connection, "connection");
|
---|
[9185] | 200 | CheckParameterUtil.ensureParameterNotNull(monitor, "monitor");
|
---|
[9168] | 201 | this.connection = connection;
|
---|
[9185] | 202 | this.monitor = monitor;
|
---|
[9168] | 203 | this.responseCode = connection.getResponseCode();
|
---|
[9172] | 204 | this.responseMessage = connection.getResponseMessage();
|
---|
[11250] | 205 | if (this.responseCode >= 300) {
|
---|
| 206 | String contentType = getContentType();
|
---|
| 207 | if (contentType == null || (
|
---|
| 208 | contentType.contains("text") ||
|
---|
| 209 | contentType.contains("html") ||
|
---|
| 210 | contentType.contains("xml"))
|
---|
| 211 | ) {
|
---|
| 212 | String content = this.fetchContent();
|
---|
[11381] | 213 | if (content.isEmpty()) {
|
---|
[12620] | 214 | Logging.debug("Server did not return any body");
|
---|
[11250] | 215 | } else {
|
---|
[12620] | 216 | Logging.debug("Response body: ");
|
---|
| 217 | Logging.debug(this.fetchContent());
|
---|
[11250] | 218 | }
|
---|
| 219 | } else {
|
---|
[12620] | 220 | Logging.debug("Server returned content: {0} of length: {1}. Not printing.", contentType, this.getContentLength());
|
---|
[11250] | 221 | }
|
---|
| 222 | }
|
---|
[9168] | 223 | }
|
---|
| 224 |
|
---|
| 225 | /**
|
---|
[9169] | 226 | * Sets whether {@link #getContent()} should uncompress the input stream if necessary.
|
---|
| 227 | *
|
---|
| 228 | * @param uncompress whether the input stream should be uncompressed if necessary
|
---|
| 229 | * @return {@code this}
|
---|
| 230 | */
|
---|
| 231 | public Response uncompress(boolean uncompress) {
|
---|
| 232 | this.uncompress = uncompress;
|
---|
| 233 | return this;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[9177] | 236 | /**
|
---|
| 237 | * Sets whether {@link #getContent()} should uncompress the input stream according to {@code Content-Disposition}
|
---|
| 238 | * HTTP header.
|
---|
| 239 | * @param uncompressAccordingToContentDisposition whether the input stream should be uncompressed according to
|
---|
| 240 | * {@code Content-Disposition}
|
---|
| 241 | * @return {@code this}
|
---|
[9178] | 242 | * @since 9172
|
---|
[9177] | 243 | */
|
---|
[9172] | 244 | public Response uncompressAccordingToContentDisposition(boolean uncompressAccordingToContentDisposition) {
|
---|
| 245 | this.uncompressAccordingToContentDisposition = uncompressAccordingToContentDisposition;
|
---|
| 246 | return this;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[9169] | 249 | /**
|
---|
[9177] | 250 | * Returns the URL.
|
---|
| 251 | * @return the URL
|
---|
[9172] | 252 | * @see HttpURLConnection#getURL()
|
---|
[9178] | 253 | * @since 9172
|
---|
[9172] | 254 | */
|
---|
| 255 | public URL getURL() {
|
---|
| 256 | return connection.getURL();
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | /**
|
---|
[9177] | 260 | * Returns the request method.
|
---|
| 261 | * @return the HTTP request method
|
---|
[9172] | 262 | * @see HttpURLConnection#getRequestMethod()
|
---|
[9178] | 263 | * @since 9172
|
---|
[9172] | 264 | */
|
---|
| 265 | public String getRequestMethod() {
|
---|
| 266 | return connection.getRequestMethod();
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | /**
|
---|
[9168] | 270 | * Returns an input stream that reads from this HTTP connection, or,
|
---|
| 271 | * error stream if the connection failed but the server sent useful data.
|
---|
[9177] | 272 | * <p>
|
---|
[9172] | 273 | * Note: the return value can be null, if both the input and the error stream are null.
|
---|
| 274 | * Seems to be the case if the OSM server replies a 401 Unauthorized, see #3887
|
---|
[9177] | 275 | * @return input or error stream
|
---|
| 276 | * @throws IOException if any I/O error occurs
|
---|
[9172] | 277 | *
|
---|
[9168] | 278 | * @see HttpURLConnection#getInputStream()
|
---|
| 279 | * @see HttpURLConnection#getErrorStream()
|
---|
| 280 | */
|
---|
[9227] | 281 | @SuppressWarnings("resource")
|
---|
[9168] | 282 | public InputStream getContent() throws IOException {
|
---|
| 283 | InputStream in;
|
---|
| 284 | try {
|
---|
| 285 | in = connection.getInputStream();
|
---|
| 286 | } catch (IOException ioe) {
|
---|
[12620] | 287 | Logging.debug(ioe);
|
---|
[11553] | 288 | in = Optional.ofNullable(connection.getErrorStream()).orElseGet(() -> new ByteArrayInputStream(new byte[]{}));
|
---|
[9168] | 289 | }
|
---|
[11381] | 290 | in = new ProgressInputStream(in, getContentLength(), monitor);
|
---|
| 291 | in = "gzip".equalsIgnoreCase(getContentEncoding()) ? new GZIPInputStream(in) : in;
|
---|
| 292 | Compression compression = Compression.NONE;
|
---|
| 293 | if (uncompress) {
|
---|
| 294 | final String contentType = getContentType();
|
---|
[12620] | 295 | Logging.debug("Uncompressing input stream according to Content-Type header: {0}", contentType);
|
---|
[11381] | 296 | compression = Compression.forContentType(contentType);
|
---|
| 297 | }
|
---|
| 298 | if (uncompressAccordingToContentDisposition && Compression.NONE.equals(compression)) {
|
---|
| 299 | final String contentDisposition = getHeaderField("Content-Disposition");
|
---|
| 300 | final Matcher matcher = Pattern.compile("filename=\"([^\"]+)\"").matcher(
|
---|
| 301 | contentDisposition != null ? contentDisposition : "");
|
---|
| 302 | if (matcher.find()) {
|
---|
[12620] | 303 | Logging.debug("Uncompressing input stream according to Content-Disposition header: {0}", contentDisposition);
|
---|
[11381] | 304 | compression = Compression.byExtension(matcher.group(1));
|
---|
[9172] | 305 | }
|
---|
| 306 | }
|
---|
[11381] | 307 | in = compression.getUncompressedInputStream(in);
|
---|
[9172] | 308 | return in;
|
---|
[9168] | 309 | }
|
---|
| 310 |
|
---|
| 311 | /**
|
---|
[9171] | 312 | * Returns {@link #getContent()} wrapped in a buffered reader.
|
---|
| 313 | *
|
---|
| 314 | * Detects Unicode charset in use utilizing {@link UTFInputStreamReader}.
|
---|
[9177] | 315 | * @return buffered reader
|
---|
| 316 | * @throws IOException if any I/O error occurs
|
---|
[9168] | 317 | */
|
---|
| 318 | public BufferedReader getContentReader() throws IOException {
|
---|
[9171] | 319 | return new BufferedReader(
|
---|
| 320 | UTFInputStreamReader.create(getContent())
|
---|
| 321 | );
|
---|
[9168] | 322 | }
|
---|
| 323 |
|
---|
| 324 | /**
|
---|
[9171] | 325 | * Fetches the HTTP response as String.
|
---|
| 326 | * @return the response
|
---|
[9177] | 327 | * @throws IOException if any I/O error occurs
|
---|
[9171] | 328 | */
|
---|
[11250] | 329 | public synchronized String fetchContent() throws IOException {
|
---|
| 330 | if (responseData == null) {
|
---|
| 331 | try (Scanner scanner = new Scanner(getContentReader()).useDelimiter("\\A")) { // \A - beginning of input
|
---|
| 332 | responseData = scanner.hasNext() ? scanner.next() : "";
|
---|
| 333 | }
|
---|
[9171] | 334 | }
|
---|
[11250] | 335 | return responseData;
|
---|
[9171] | 336 | }
|
---|
| 337 |
|
---|
| 338 | /**
|
---|
[9168] | 339 | * Gets the response code from this HTTP connection.
|
---|
[9177] | 340 | * @return HTTP response code
|
---|
[9168] | 341 | *
|
---|
| 342 | * @see HttpURLConnection#getResponseCode()
|
---|
| 343 | */
|
---|
| 344 | public int getResponseCode() {
|
---|
| 345 | return responseCode;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | /**
|
---|
[9172] | 349 | * Gets the response message from this HTTP connection.
|
---|
[9177] | 350 | * @return HTTP response message
|
---|
[9172] | 351 | *
|
---|
| 352 | * @see HttpURLConnection#getResponseMessage()
|
---|
[9178] | 353 | * @since 9172
|
---|
[9172] | 354 | */
|
---|
| 355 | public String getResponseMessage() {
|
---|
| 356 | return responseMessage;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | /**
|
---|
[9168] | 360 | * Returns the {@code Content-Encoding} header.
|
---|
[9177] | 361 | * @return {@code Content-Encoding} HTTP header
|
---|
[9232] | 362 | * @see HttpURLConnection#getContentEncoding()
|
---|
[9168] | 363 | */
|
---|
| 364 | public String getContentEncoding() {
|
---|
| 365 | return connection.getContentEncoding();
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | /**
|
---|
| 369 | * Returns the {@code Content-Type} header.
|
---|
[9177] | 370 | * @return {@code Content-Type} HTTP header
|
---|
[9168] | 371 | */
|
---|
| 372 | public String getContentType() {
|
---|
| 373 | return connection.getHeaderField("Content-Type");
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | /**
|
---|
[9232] | 377 | * Returns the {@code Expire} header.
|
---|
| 378 | * @return {@code Expire} HTTP header
|
---|
| 379 | * @see HttpURLConnection#getExpiration()
|
---|
[9249] | 380 | * @since 9232
|
---|
[9232] | 381 | */
|
---|
| 382 | public long getExpiration() {
|
---|
| 383 | return connection.getExpiration();
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | /**
|
---|
| 387 | * Returns the {@code Last-Modified} header.
|
---|
| 388 | * @return {@code Last-Modified} HTTP header
|
---|
| 389 | * @see HttpURLConnection#getLastModified()
|
---|
| 390 | * @since 9232
|
---|
| 391 | */
|
---|
| 392 | public long getLastModified() {
|
---|
| 393 | return connection.getLastModified();
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | /**
|
---|
[9171] | 397 | * Returns the {@code Content-Length} header.
|
---|
[9177] | 398 | * @return {@code Content-Length} HTTP header
|
---|
[9232] | 399 | * @see HttpURLConnection#getContentLengthLong()
|
---|
[9171] | 400 | */
|
---|
| 401 | public long getContentLength() {
|
---|
| 402 | return connection.getContentLengthLong();
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | /**
|
---|
[9177] | 406 | * Returns the value of the named header field.
|
---|
| 407 | * @param name the name of a header field
|
---|
| 408 | * @return the value of the named header field, or {@code null} if there is no such field in the header
|
---|
[9172] | 409 | * @see HttpURLConnection#getHeaderField(String)
|
---|
[9178] | 410 | * @since 9172
|
---|
[9172] | 411 | */
|
---|
| 412 | public String getHeaderField(String name) {
|
---|
| 413 | return connection.getHeaderField(name);
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | /**
|
---|
[9232] | 417 | * Returns an unmodifiable Map mapping header keys to a List of header values.
|
---|
[9807] | 418 | * As per RFC 2616, section 4.2 header names are case insensitive, so returned map is also case insensitive
|
---|
[9232] | 419 | * @return unmodifiable Map mapping header keys to a List of header values
|
---|
[9172] | 420 | * @see HttpURLConnection#getHeaderFields()
|
---|
[9232] | 421 | * @since 9232
|
---|
[9172] | 422 | */
|
---|
[9232] | 423 | public Map<String, List<String>> getHeaderFields() {
|
---|
[9807] | 424 | // returned map from HttpUrlConnection is case sensitive, use case insensitive TreeMap to conform to RFC 2616
|
---|
| 425 | Map<String, List<String>> ret = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
---|
[9810] | 426 | for (Entry<String, List<String>> e: connection.getHeaderFields().entrySet()) {
|
---|
[9807] | 427 | if (e.getKey() != null) {
|
---|
| 428 | ret.put(e.getKey(), e.getValue());
|
---|
| 429 | }
|
---|
| 430 | }
|
---|
| 431 | return Collections.unmodifiableMap(ret);
|
---|
[9172] | 432 | }
|
---|
| 433 |
|
---|
| 434 | /**
|
---|
[9168] | 435 | * @see HttpURLConnection#disconnect()
|
---|
| 436 | */
|
---|
| 437 | public void disconnect() {
|
---|
[9309] | 438 | HttpClient.disconnect(connection);
|
---|
[9168] | 439 | }
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | /**
|
---|
| 443 | * Creates a new instance for the given URL and a {@code GET} request
|
---|
| 444 | *
|
---|
| 445 | * @param url the URL
|
---|
| 446 | * @return a new instance
|
---|
| 447 | */
|
---|
| 448 | public static HttpClient create(URL url) {
|
---|
| 449 | return create(url, "GET");
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | /**
|
---|
| 453 | * Creates a new instance for the given URL and a {@code GET} request
|
---|
| 454 | *
|
---|
[9172] | 455 | * @param url the URL
|
---|
[9168] | 456 | * @param requestMethod the HTTP request method to perform when calling
|
---|
| 457 | * @return a new instance
|
---|
| 458 | */
|
---|
| 459 | public static HttpClient create(URL url, String requestMethod) {
|
---|
| 460 | return new HttpClient(url, requestMethod);
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | /**
|
---|
[9172] | 464 | * Returns the URL set for this connection.
|
---|
[9177] | 465 | * @return the URL
|
---|
[9172] | 466 | * @see #create(URL)
|
---|
| 467 | * @see #create(URL, String)
|
---|
[9178] | 468 | * @since 9172
|
---|
[9172] | 469 | */
|
---|
| 470 | public URL getURL() {
|
---|
| 471 | return url;
|
---|
| 472 | }
|
---|
| 473 |
|
---|
| 474 | /**
|
---|
| 475 | * Returns the request method set for this connection.
|
---|
[9177] | 476 | * @return the HTTP request method
|
---|
[9172] | 477 | * @see #create(URL, String)
|
---|
[9178] | 478 | * @since 9172
|
---|
[9172] | 479 | */
|
---|
| 480 | public String getRequestMethod() {
|
---|
| 481 | return requestMethod;
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | /**
|
---|
| 485 | * Returns the set value for the given {@code header}.
|
---|
[9177] | 486 | * @param header HTTP header name
|
---|
| 487 | * @return HTTP header value
|
---|
[9178] | 488 | * @since 9172
|
---|
[9172] | 489 | */
|
---|
| 490 | public String getRequestHeader(String header) {
|
---|
| 491 | return headers.get(header);
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 | /**
|
---|
[9171] | 495 | * Sets whether not to set header {@code Cache-Control=no-cache}
|
---|
| 496 | *
|
---|
| 497 | * @param useCache whether not to set header {@code Cache-Control=no-cache}
|
---|
[9168] | 498 | * @return {@code this}
|
---|
[9171] | 499 | * @see HttpURLConnection#setUseCaches(boolean)
|
---|
| 500 | */
|
---|
| 501 | public HttpClient useCache(boolean useCache) {
|
---|
| 502 | this.useCache = useCache;
|
---|
| 503 | return this;
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | /**
|
---|
| 507 | * Sets whether not to set header {@code Connection=close}
|
---|
[9230] | 508 | * <p>
|
---|
[9173] | 509 | * This might fix #7640, see
|
---|
| 510 | * <a href='https://web.archive.org/web/20140118201501/http://www.tikalk.com/java/forums/httpurlconnection-disable-keep-alive'>here</a>.
|
---|
[9171] | 511 | *
|
---|
| 512 | * @param keepAlive whether not to set header {@code Connection=close}
|
---|
| 513 | * @return {@code this}
|
---|
| 514 | */
|
---|
| 515 | public HttpClient keepAlive(boolean keepAlive) {
|
---|
[9172] | 516 | return setHeader("Connection", keepAlive ? null : "close");
|
---|
[9171] | 517 | }
|
---|
| 518 |
|
---|
| 519 | /**
|
---|
[9177] | 520 | * Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced
|
---|
| 521 | * by this URLConnection. If the timeout expires before the connection can be established, a
|
---|
| 522 | * {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite timeout.
|
---|
| 523 | * @param connectTimeout an {@code int} that specifies the connect timeout value in milliseconds
|
---|
[9171] | 524 | * @return {@code this}
|
---|
[9168] | 525 | * @see HttpURLConnection#setConnectTimeout(int)
|
---|
| 526 | */
|
---|
| 527 | public HttpClient setConnectTimeout(int connectTimeout) {
|
---|
| 528 | this.connectTimeout = connectTimeout;
|
---|
| 529 | return this;
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | /**
|
---|
[9177] | 533 | * Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from
|
---|
| 534 | * input stream when a connection is established to a resource. If the timeout expires before there is data available for
|
---|
| 535 | * read, a {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite timeout.
|
---|
| 536 | * @param readTimeout an {@code int} that specifies the read timeout value in milliseconds
|
---|
[9168] | 537 | * @return {@code this}
|
---|
[9178] | 538 | * @see HttpURLConnection#setReadTimeout(int)
|
---|
[9168] | 539 | */
|
---|
| 540 | public HttpClient setReadTimeout(int readTimeout) {
|
---|
| 541 | this.readTimeout = readTimeout;
|
---|
| 542 | return this;
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | /**
|
---|
| 546 | * Sets the {@code Accept} header.
|
---|
[9177] | 547 | * @param accept header value
|
---|
[9168] | 548 | *
|
---|
| 549 | * @return {@code this}
|
---|
| 550 | */
|
---|
| 551 | public HttpClient setAccept(String accept) {
|
---|
[9172] | 552 | return setHeader("Accept", accept);
|
---|
[9168] | 553 | }
|
---|
| 554 |
|
---|
| 555 | /**
|
---|
| 556 | * Sets the request body for {@code PUT}/{@code POST} requests.
|
---|
[9177] | 557 | * @param requestBody request body
|
---|
[9168] | 558 | *
|
---|
| 559 | * @return {@code this}
|
---|
| 560 | */
|
---|
| 561 | public HttpClient setRequestBody(byte[] requestBody) {
|
---|
[11879] | 562 | this.requestBody = Utils.copyArray(requestBody);
|
---|
[9168] | 563 | return this;
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 | /**
|
---|
| 567 | * Sets the {@code If-Modified-Since} header.
|
---|
[9177] | 568 | * @param ifModifiedSince header value
|
---|
[9168] | 569 | *
|
---|
| 570 | * @return {@code this}
|
---|
| 571 | */
|
---|
| 572 | public HttpClient setIfModifiedSince(long ifModifiedSince) {
|
---|
| 573 | this.ifModifiedSince = ifModifiedSince;
|
---|
| 574 | return this;
|
---|
| 575 | }
|
---|
| 576 |
|
---|
| 577 | /**
|
---|
| 578 | * Sets the maximum number of redirections to follow.
|
---|
| 579 | *
|
---|
[9172] | 580 | * Set {@code maxRedirects} to {@code -1} in order to ignore redirects, i.e.,
|
---|
| 581 | * to not throw an {@link IOException} in {@link #connect()}.
|
---|
[9177] | 582 | * @param maxRedirects header value
|
---|
[9172] | 583 | *
|
---|
[9168] | 584 | * @return {@code this}
|
---|
| 585 | */
|
---|
| 586 | public HttpClient setMaxRedirects(int maxRedirects) {
|
---|
| 587 | this.maxRedirects = maxRedirects;
|
---|
| 588 | return this;
|
---|
| 589 | }
|
---|
| 590 |
|
---|
| 591 | /**
|
---|
| 592 | * Sets an arbitrary HTTP header.
|
---|
[9177] | 593 | * @param key header name
|
---|
| 594 | * @param value header value
|
---|
[9168] | 595 | *
|
---|
| 596 | * @return {@code this}
|
---|
| 597 | */
|
---|
| 598 | public HttpClient setHeader(String key, String value) {
|
---|
| 599 | this.headers.put(key, value);
|
---|
| 600 | return this;
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 | /**
|
---|
| 604 | * Sets arbitrary HTTP headers.
|
---|
[9177] | 605 | * @param headers HTTP headers
|
---|
[9168] | 606 | *
|
---|
| 607 | * @return {@code this}
|
---|
| 608 | */
|
---|
| 609 | public HttpClient setHeaders(Map<String, String> headers) {
|
---|
| 610 | this.headers.putAll(headers);
|
---|
| 611 | return this;
|
---|
| 612 | }
|
---|
| 613 |
|
---|
[9172] | 614 | /**
|
---|
| 615 | * Sets a reason to show on console. Can be {@code null} if no reason is given.
|
---|
[9177] | 616 | * @param reasonForRequest Reason to show
|
---|
| 617 | * @return {@code this}
|
---|
[9178] | 618 | * @since 9172
|
---|
[9172] | 619 | */
|
---|
| 620 | public HttpClient setReasonForRequest(String reasonForRequest) {
|
---|
| 621 | this.reasonForRequest = reasonForRequest;
|
---|
| 622 | return this;
|
---|
| 623 | }
|
---|
| 624 |
|
---|
[10302] | 625 | /**
|
---|
[12711] | 626 | * Sets the output message to be displayed in progress monitor for {@code PUT}, {@code POST} and {@code DELETE} methods.
|
---|
| 627 | * Defaults to "Uploading data ..." (translated). Has no effect for {@code GET} or any other method.
|
---|
| 628 | * @param outputMessage message to be displayed in progress monitor
|
---|
| 629 | * @return {@code this}
|
---|
| 630 | * @since 12711
|
---|
| 631 | */
|
---|
| 632 | public HttpClient setOutputMessage(String outputMessage) {
|
---|
| 633 | this.outputMessage = outputMessage;
|
---|
| 634 | return this;
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | /**
|
---|
[10302] | 638 | * Sets whether the progress monitor task will be finished when the output stream is closed. This is {@code true} by default.
|
---|
| 639 | * @param finishOnCloseOutput whether the progress monitor task will be finished when the output stream is closed
|
---|
| 640 | * @return {@code this}
|
---|
| 641 | * @since 10302
|
---|
| 642 | */
|
---|
| 643 | public HttpClient setFinishOnCloseOutput(boolean finishOnCloseOutput) {
|
---|
| 644 | this.finishOnCloseOutput = finishOnCloseOutput;
|
---|
| 645 | return this;
|
---|
| 646 | }
|
---|
| 647 |
|
---|
[9168] | 648 | private static boolean isRedirect(final int statusCode) {
|
---|
| 649 | switch (statusCode) {
|
---|
| 650 | case HttpURLConnection.HTTP_MOVED_PERM: // 301
|
---|
| 651 | case HttpURLConnection.HTTP_MOVED_TEMP: // 302
|
---|
| 652 | case HttpURLConnection.HTTP_SEE_OTHER: // 303
|
---|
| 653 | case 307: // TEMPORARY_REDIRECT:
|
---|
| 654 | case 308: // PERMANENT_REDIRECT:
|
---|
| 655 | return true;
|
---|
| 656 | default:
|
---|
| 657 | return false;
|
---|
| 658 | }
|
---|
| 659 | }
|
---|
[9309] | 660 |
|
---|
| 661 | /**
|
---|
| 662 | * @see HttpURLConnection#disconnect()
|
---|
| 663 | * @since 9309
|
---|
| 664 | */
|
---|
| 665 | public void disconnect() {
|
---|
| 666 | HttpClient.disconnect(connection);
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | private static void disconnect(final HttpURLConnection connection) {
|
---|
[12590] | 670 | if (connection != null) {
|
---|
| 671 | // Fix upload aborts - see #263
|
---|
| 672 | connection.setConnectTimeout(100);
|
---|
| 673 | connection.setReadTimeout(100);
|
---|
| 674 | try {
|
---|
| 675 | Thread.sleep(100);
|
---|
| 676 | } catch (InterruptedException ex) {
|
---|
[12620] | 677 | Logging.warn("InterruptedException in " + HttpClient.class + " during cancel");
|
---|
[12590] | 678 | Thread.currentThread().interrupt();
|
---|
| 679 | }
|
---|
| 680 | connection.disconnect();
|
---|
[9309] | 681 | }
|
---|
| 682 | }
|
---|
[9168] | 683 | }
|
---|