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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial