|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2008 the original author or authors. |
| 2 | + * Copyright 2002-2011 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
45 | 45 | */
|
46 | 46 | public class SimpleHttpInvokerRequestExecutor extends AbstractHttpInvokerRequestExecutor {
|
47 | 47 |
|
| 48 | + private int connectTimeout = -1; |
| 49 | + |
| 50 | + private int readTimeout = -1; |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * Set the underlying URLConnection's connect timeout (in milliseconds). |
| 55 | + * A timeout value of 0 specifies an infinite timeout. |
| 56 | + * <p>Default is the system's default timeout. |
| 57 | + * @see URLConnection#setConnectTimeout(int) |
| 58 | + */ |
| 59 | + public void setConnectTimeout(int connectTimeout) { |
| 60 | + this.connectTimeout = connectTimeout; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Set the underlying URLConnection's read timeout (in milliseconds). |
| 65 | + * A timeout value of 0 specifies an infinite timeout. |
| 66 | + * <p>Default is the system's default timeout. |
| 67 | + * @see URLConnection#setReadTimeout(int) |
| 68 | + */ |
| 69 | + public void setReadTimeout(int readTimeout) { |
| 70 | + this.readTimeout = readTimeout; |
| 71 | + } |
| 72 | + |
| 73 | + |
48 | 74 | /**
|
49 | 75 | * Execute the given request through a standard J2SE HttpURLConnection.
|
50 | 76 | * <p>This method implements the basic processing workflow:
|
@@ -90,23 +116,29 @@ protected HttpURLConnection openConnection(HttpInvokerClientConfiguration config
|
90 | 116 | * <p>The default implementation specifies POST as method,
|
91 | 117 | * "application/x-java-serialized-object" as "Content-Type" header,
|
92 | 118 | * and the given content length as "Content-Length" header.
|
93 |
| - * @param con the HTTP connection to prepare |
| 119 | + * @param connection the HTTP connection to prepare |
94 | 120 | * @param contentLength the length of the content to send
|
95 | 121 | * @throws IOException if thrown by HttpURLConnection methods
|
96 | 122 | * @see java.net.HttpURLConnection#setRequestMethod
|
97 | 123 | * @see java.net.HttpURLConnection#setRequestProperty
|
98 | 124 | */
|
99 |
| - protected void prepareConnection(HttpURLConnection con, int contentLength) throws IOException { |
100 |
| - con.setDoOutput(true); |
101 |
| - con.setRequestMethod(HTTP_METHOD_POST); |
102 |
| - con.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType()); |
103 |
| - con.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength)); |
| 125 | + protected void prepareConnection(HttpURLConnection connection, int contentLength) throws IOException { |
| 126 | + if (this.connectTimeout >= 0) { |
| 127 | + connection.setConnectTimeout(this.connectTimeout); |
| 128 | + } |
| 129 | + if (this.readTimeout >= 0) { |
| 130 | + connection.setReadTimeout(this.readTimeout); |
| 131 | + } |
| 132 | + connection.setDoOutput(true); |
| 133 | + connection.setRequestMethod(HTTP_METHOD_POST); |
| 134 | + connection.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType()); |
| 135 | + connection.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength)); |
104 | 136 | LocaleContext locale = LocaleContextHolder.getLocaleContext();
|
105 | 137 | if (locale != null) {
|
106 |
| - con.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale.getLocale())); |
| 138 | + connection.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale.getLocale())); |
107 | 139 | }
|
108 | 140 | if (isAcceptGzipEncoding()) {
|
109 |
| - con.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP); |
| 141 | + connection.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP); |
110 | 142 | }
|
111 | 143 | }
|
112 | 144 |
|
@@ -187,7 +219,7 @@ protected InputStream readResponseBody(HttpInvokerClientConfiguration config, Ht
|
187 | 219 | */
|
188 | 220 | protected boolean isGzipResponse(HttpURLConnection con) {
|
189 | 221 | String encodingHeader = con.getHeaderField(HTTP_HEADER_CONTENT_ENCODING);
|
190 |
| - return (encodingHeader != null && encodingHeader.toLowerCase().indexOf(ENCODING_GZIP) != -1); |
| 222 | + return (encodingHeader != null && encodingHeader.toLowerCase().contains(ENCODING_GZIP)); |
191 | 223 | }
|
192 | 224 |
|
193 | 225 | }
|
0 commit comments