src/share/jaxws_classes/com/sun/xml/internal/ws/transport/http/client/HttpClientTransport.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.ws.transport.http.client;
aoqi@0 27
aoqi@0 28 import com.sun.xml.internal.ws.api.EndpointAddress;
aoqi@0 29 import com.sun.xml.internal.ws.api.message.Packet;
aoqi@0 30 import com.sun.xml.internal.ws.client.BindingProviderProperties;
aoqi@0 31 import static com.sun.xml.internal.ws.client.BindingProviderProperties.*;
aoqi@0 32 import com.sun.xml.internal.ws.client.ClientTransportException;
aoqi@0 33 import com.sun.xml.internal.ws.resources.ClientMessages;
aoqi@0 34 import com.sun.xml.internal.ws.transport.Headers;
aoqi@0 35 import com.sun.xml.internal.ws.developer.JAXWSProperties;
aoqi@0 36 import com.sun.istack.internal.Nullable;
aoqi@0 37 import com.sun.istack.internal.NotNull;
aoqi@0 38
aoqi@0 39 import javax.net.ssl.SSLSocketFactory;
aoqi@0 40 import javax.net.ssl.HttpsURLConnection;
aoqi@0 41 import javax.net.ssl.HostnameVerifier;
aoqi@0 42 import javax.net.ssl.SSLSession;
aoqi@0 43 import javax.xml.bind.JAXBContext;
aoqi@0 44 import javax.xml.bind.JAXBException;
aoqi@0 45 import javax.xml.ws.WebServiceException;
aoqi@0 46 import javax.xml.ws.handler.MessageContext;
aoqi@0 47 import java.io.FilterInputStream;
aoqi@0 48 import java.io.FilterOutputStream;
aoqi@0 49 import java.io.IOException;
aoqi@0 50 import java.io.InputStream;
aoqi@0 51 import java.io.OutputStream;
aoqi@0 52 import java.net.HttpURLConnection;
aoqi@0 53 import java.util.List;
aoqi@0 54 import java.util.Map;
aoqi@0 55 import java.util.zip.GZIPOutputStream;
aoqi@0 56 import java.util.zip.GZIPInputStream;
aoqi@0 57
aoqi@0 58 /**
aoqi@0 59 *
aoqi@0 60 * @author WS Development Team
aoqi@0 61 */
aoqi@0 62 public class HttpClientTransport {
aoqi@0 63
aoqi@0 64 private static final byte[] THROW_AWAY_BUFFER = new byte[8192];
aoqi@0 65
aoqi@0 66 // Need to use JAXB first to register DatatypeConverter
aoqi@0 67 static {
aoqi@0 68 try {
aoqi@0 69 JAXBContext.newInstance().createUnmarshaller();
aoqi@0 70 } catch(JAXBException je) {
aoqi@0 71 // Nothing much can be done. Intentionally left empty
aoqi@0 72 }
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 /*package*/ int statusCode;
aoqi@0 76 /*package*/ String statusMessage;
aoqi@0 77 /*package*/ int contentLength;
aoqi@0 78 private final Map<String, List<String>> reqHeaders;
aoqi@0 79 private Map<String, List<String>> respHeaders = null;
aoqi@0 80
aoqi@0 81 private OutputStream outputStream;
aoqi@0 82 private boolean https;
aoqi@0 83 private HttpURLConnection httpConnection = null;
aoqi@0 84 private final EndpointAddress endpoint;
aoqi@0 85 private final Packet context;
aoqi@0 86 private final Integer chunkSize;
aoqi@0 87
aoqi@0 88
aoqi@0 89 public HttpClientTransport(@NotNull Packet packet, @NotNull Map<String,List<String>> reqHeaders) {
aoqi@0 90 endpoint = packet.endpointAddress;
aoqi@0 91 context = packet;
aoqi@0 92 this.reqHeaders = reqHeaders;
aoqi@0 93 chunkSize = (Integer)context.invocationProperties.get(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE);
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 /*
aoqi@0 97 * Prepare the stream for HTTP request
aoqi@0 98 */
aoqi@0 99 OutputStream getOutput() {
aoqi@0 100 try {
aoqi@0 101 createHttpConnection();
aoqi@0 102 // for "GET" request no need to get outputStream
aoqi@0 103 if (requiresOutputStream()) {
aoqi@0 104 outputStream = httpConnection.getOutputStream();
aoqi@0 105 if (chunkSize != null) {
aoqi@0 106 outputStream = new WSChunkedOuputStream(outputStream, chunkSize);
aoqi@0 107 }
aoqi@0 108 List<String> contentEncoding = reqHeaders.get("Content-Encoding");
aoqi@0 109 // TODO need to find out correct encoding based on q value - RFC 2616
aoqi@0 110 if (contentEncoding != null && contentEncoding.get(0).contains("gzip")) {
aoqi@0 111 outputStream = new GZIPOutputStream(outputStream);
aoqi@0 112 }
aoqi@0 113 }
aoqi@0 114 httpConnection.connect();
aoqi@0 115 } catch (Exception ex) {
aoqi@0 116 throw new ClientTransportException(
aoqi@0 117 ClientMessages.localizableHTTP_CLIENT_FAILED(ex),ex);
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 return outputStream;
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 void closeOutput() throws IOException {
aoqi@0 124 if (outputStream != null) {
aoqi@0 125 outputStream.close();
aoqi@0 126 outputStream = null;
aoqi@0 127 }
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 /*
aoqi@0 131 * Get the response from HTTP connection and prepare the input stream for response
aoqi@0 132 */
aoqi@0 133 @Nullable InputStream getInput() {
aoqi@0 134 // response processing
aoqi@0 135
aoqi@0 136 InputStream in;
aoqi@0 137 try {
aoqi@0 138 in = readResponse();
aoqi@0 139 if (in != null) {
aoqi@0 140 String contentEncoding = httpConnection.getContentEncoding();
aoqi@0 141 if (contentEncoding != null && contentEncoding.contains("gzip")) {
aoqi@0 142 in = new GZIPInputStream(in);
aoqi@0 143 }
aoqi@0 144 }
aoqi@0 145 } catch (IOException e) {
aoqi@0 146 throw new ClientTransportException(ClientMessages.localizableHTTP_STATUS_CODE(statusCode, statusMessage), e);
aoqi@0 147 }
aoqi@0 148 return in;
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 public Map<String, List<String>> getHeaders() {
aoqi@0 152 if (respHeaders != null) {
aoqi@0 153 return respHeaders;
aoqi@0 154 }
aoqi@0 155 respHeaders = new Headers();
aoqi@0 156 respHeaders.putAll(httpConnection.getHeaderFields());
aoqi@0 157 return respHeaders;
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 protected @Nullable InputStream readResponse() {
aoqi@0 161 InputStream is;
aoqi@0 162 try {
aoqi@0 163 is = httpConnection.getInputStream();
aoqi@0 164 } catch(IOException ioe) {
aoqi@0 165 is = httpConnection.getErrorStream();
aoqi@0 166 }
aoqi@0 167 if (is == null) {
aoqi@0 168 return is;
aoqi@0 169 }
aoqi@0 170 // Since StreamMessage doesn't read </s:Body></s:Envelope>, there
aoqi@0 171 // are some bytes left in the InputStream. This confuses JDK and may
aoqi@0 172 // not reuse underlying sockets. Hopefully JDK fixes it in its code !
aoqi@0 173 final InputStream temp = is;
aoqi@0 174 return new FilterInputStream(temp) {
aoqi@0 175 // Workaround for "SJSXP XMLStreamReader.next() closes stream".
aoqi@0 176 // So it doesn't read from the closed stream
aoqi@0 177 boolean closed;
aoqi@0 178 @Override
aoqi@0 179 public void close() throws IOException {
aoqi@0 180 if (!closed) {
aoqi@0 181 closed = true;
aoqi@0 182 while(temp.read(THROW_AWAY_BUFFER) != -1);
aoqi@0 183 super.close();
aoqi@0 184 }
aoqi@0 185 }
aoqi@0 186 };
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 protected void readResponseCodeAndMessage() {
aoqi@0 190 try {
aoqi@0 191 statusCode = httpConnection.getResponseCode();
aoqi@0 192 statusMessage = httpConnection.getResponseMessage();
aoqi@0 193 contentLength = httpConnection.getContentLength();
aoqi@0 194 } catch(IOException ioe) {
aoqi@0 195 throw new WebServiceException(ioe);
aoqi@0 196 }
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 protected HttpURLConnection openConnection(Packet packet) {
aoqi@0 200 // default do nothing
aoqi@0 201 return null;
aoqi@0 202 }
aoqi@0 203
aoqi@0 204 protected boolean checkHTTPS(HttpURLConnection connection) {
aoqi@0 205 if (connection instanceof HttpsURLConnection) {
aoqi@0 206
aoqi@0 207 // TODO The above property needs to be removed in future version as the semantics of this property are not preoperly defined.
aoqi@0 208 // One should use JAXWSProperties.HOSTNAME_VERIFIER to control the behavior
aoqi@0 209
aoqi@0 210 // does the client want client hostname verification by the service
aoqi@0 211 String verificationProperty =
aoqi@0 212 (String) context.invocationProperties.get(HOSTNAME_VERIFICATION_PROPERTY);
aoqi@0 213 if (verificationProperty != null) {
aoqi@0 214 if (verificationProperty.equalsIgnoreCase("true")) {
aoqi@0 215 ((HttpsURLConnection) connection).setHostnameVerifier(new HttpClientVerifier());
aoqi@0 216 }
aoqi@0 217 }
aoqi@0 218
aoqi@0 219 // Set application's HostNameVerifier for this connection
aoqi@0 220 HostnameVerifier verifier =
aoqi@0 221 (HostnameVerifier) context.invocationProperties.get(JAXWSProperties.HOSTNAME_VERIFIER);
aoqi@0 222 if (verifier != null) {
aoqi@0 223 ((HttpsURLConnection) connection).setHostnameVerifier(verifier);
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 // Set application's SocketFactory for this connection
aoqi@0 227 SSLSocketFactory sslSocketFactory =
aoqi@0 228 (SSLSocketFactory) context.invocationProperties.get(JAXWSProperties.SSL_SOCKET_FACTORY);
aoqi@0 229 if (sslSocketFactory != null) {
aoqi@0 230 ((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory);
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 return true;
aoqi@0 234 }
aoqi@0 235 return false;
aoqi@0 236 }
aoqi@0 237
aoqi@0 238 private void createHttpConnection() throws IOException {
aoqi@0 239 httpConnection = openConnection(context);
aoqi@0 240
aoqi@0 241 if (httpConnection == null)
aoqi@0 242 httpConnection = (HttpURLConnection) endpoint.openConnection();
aoqi@0 243
aoqi@0 244 String scheme = endpoint.getURI().getScheme();
aoqi@0 245 if (scheme.equals("https")) {
aoqi@0 246 https = true;
aoqi@0 247 }
aoqi@0 248 if (checkHTTPS(httpConnection))
aoqi@0 249 https = true;
aoqi@0 250
aoqi@0 251 // allow interaction with the web page - user may have to supply
aoqi@0 252 // username, password id web page is accessed from web browser
aoqi@0 253 httpConnection.setAllowUserInteraction(true);
aoqi@0 254
aoqi@0 255 // enable input, output streams
aoqi@0 256 httpConnection.setDoOutput(true);
aoqi@0 257 httpConnection.setDoInput(true);
aoqi@0 258
aoqi@0 259 String requestMethod = (String) context.invocationProperties.get(MessageContext.HTTP_REQUEST_METHOD);
aoqi@0 260 String method = (requestMethod != null) ? requestMethod : "POST";
aoqi@0 261 httpConnection.setRequestMethod(method);
aoqi@0 262
aoqi@0 263 //this code or something similiar needs t be moved elsewhere for error checking
aoqi@0 264 /*if (context.invocationProperties.get(BindingProviderProperties.BINDING_ID_PROPERTY).equals(HTTPBinding.HTTP_BINDING)){
aoqi@0 265 method = (requestMethod != null)?requestMethod:method;
aoqi@0 266 } else if
aoqi@0 267 (context.invocationProperties.get(BindingProviderProperties.BINDING_ID_PROPERTY).equals(SOAPBinding.SOAP12HTTP_BINDING) &&
aoqi@0 268 "GET".equalsIgnoreCase(requestMethod)) {
aoqi@0 269 }
aoqi@0 270 */
aoqi@0 271
aoqi@0 272 Integer reqTimeout = (Integer)context.invocationProperties.get(BindingProviderProperties.REQUEST_TIMEOUT);
aoqi@0 273 if (reqTimeout != null) {
aoqi@0 274 httpConnection.setReadTimeout(reqTimeout);
aoqi@0 275 }
aoqi@0 276
aoqi@0 277 Integer connectTimeout = (Integer)context.invocationProperties.get(JAXWSProperties.CONNECT_TIMEOUT);
aoqi@0 278 if (connectTimeout != null) {
aoqi@0 279 httpConnection.setConnectTimeout(connectTimeout);
aoqi@0 280 }
aoqi@0 281
aoqi@0 282 Integer chunkSize = (Integer)context.invocationProperties.get(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE);
aoqi@0 283 if (chunkSize != null) {
aoqi@0 284 httpConnection.setChunkedStreamingMode(chunkSize);
aoqi@0 285 }
aoqi@0 286
aoqi@0 287 // set the properties on HttpURLConnection
aoqi@0 288 for (Map.Entry<String, List<String>> entry : reqHeaders.entrySet()) {
aoqi@0 289 if ("Content-Length".equals(entry.getKey())) continue;
aoqi@0 290 for(String value : entry.getValue()) {
aoqi@0 291 httpConnection.addRequestProperty(entry.getKey(), value);
aoqi@0 292 }
aoqi@0 293 }
aoqi@0 294 }
aoqi@0 295
aoqi@0 296 boolean isSecure() {
aoqi@0 297 return https;
aoqi@0 298 }
aoqi@0 299
aoqi@0 300 protected void setStatusCode(int statusCode) {
aoqi@0 301 this.statusCode = statusCode;
aoqi@0 302 }
aoqi@0 303
aoqi@0 304 private boolean requiresOutputStream() {
aoqi@0 305 return !(httpConnection.getRequestMethod().equalsIgnoreCase("GET") ||
aoqi@0 306 httpConnection.getRequestMethod().equalsIgnoreCase("HEAD") ||
aoqi@0 307 httpConnection.getRequestMethod().equalsIgnoreCase("DELETE"));
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 @Nullable String getContentType() {
aoqi@0 311 return httpConnection.getContentType();
aoqi@0 312 }
aoqi@0 313
aoqi@0 314 public int getContentLength() {
aoqi@0 315 return httpConnection.getContentLength();
aoqi@0 316 }
aoqi@0 317
aoqi@0 318 // overide default SSL HttpClientVerifier to always return true
aoqi@0 319 // effectively overiding Hostname client verification when using SSL
aoqi@0 320 private static class HttpClientVerifier implements HostnameVerifier {
aoqi@0 321 public boolean verify(String s, SSLSession sslSession) {
aoqi@0 322 return true;
aoqi@0 323 }
aoqi@0 324 }
aoqi@0 325
aoqi@0 326 private static class LocalhostHttpClientVerifier implements HostnameVerifier {
aoqi@0 327 public boolean verify(String s, SSLSession sslSession) {
aoqi@0 328 return "localhost".equalsIgnoreCase(s) || "127.0.0.1".equals(s);
aoqi@0 329 }
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 /**
aoqi@0 333 * HttpURLConnection.getOuputStream() returns sun.net.www.http.ChunkedOuputStream in chunked
aoqi@0 334 * streaming mode. If you call ChunkedOuputStream.write(byte[20MB], int, int), then the whole data
aoqi@0 335 * is kept in memory. This wraps the ChunkedOuputStream so that it writes only small
aoqi@0 336 * chunks.
aoqi@0 337 */
aoqi@0 338 private static final class WSChunkedOuputStream extends FilterOutputStream {
aoqi@0 339 final int chunkSize;
aoqi@0 340
aoqi@0 341 WSChunkedOuputStream(OutputStream actual, int chunkSize) {
aoqi@0 342 super(actual);
aoqi@0 343 this.chunkSize = chunkSize;
aoqi@0 344 }
aoqi@0 345
aoqi@0 346 @Override
aoqi@0 347 public void write(byte b[], int off, int len) throws IOException {
aoqi@0 348 while(len > 0) {
aoqi@0 349 int sent = (len > chunkSize) ? chunkSize : len;
aoqi@0 350 out.write(b, off, sent); // don't use super.write() as it writes byte-by-byte
aoqi@0 351 len -= sent;
aoqi@0 352 off += sent;
aoqi@0 353 }
aoqi@0 354 }
aoqi@0 355
aoqi@0 356 }
aoqi@0 357
aoqi@0 358 }

mercurial