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