aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.transport.http.client; aoqi@0: aoqi@0: import com.sun.xml.internal.ws.api.EndpointAddress; aoqi@0: import com.sun.xml.internal.ws.api.message.Packet; aoqi@0: import com.sun.xml.internal.ws.client.BindingProviderProperties; aoqi@0: import static com.sun.xml.internal.ws.client.BindingProviderProperties.*; aoqi@0: import com.sun.xml.internal.ws.client.ClientTransportException; aoqi@0: import com.sun.xml.internal.ws.resources.ClientMessages; aoqi@0: import com.sun.xml.internal.ws.transport.Headers; aoqi@0: import com.sun.xml.internal.ws.developer.JAXWSProperties; aoqi@0: import com.sun.istack.internal.Nullable; aoqi@0: import com.sun.istack.internal.NotNull; aoqi@0: aoqi@0: import javax.net.ssl.SSLSocketFactory; aoqi@0: import javax.net.ssl.HttpsURLConnection; aoqi@0: import javax.net.ssl.HostnameVerifier; aoqi@0: import javax.net.ssl.SSLSession; aoqi@0: import javax.xml.bind.JAXBContext; aoqi@0: import javax.xml.bind.JAXBException; aoqi@0: import javax.xml.ws.WebServiceException; aoqi@0: import javax.xml.ws.handler.MessageContext; aoqi@0: import java.io.FilterInputStream; aoqi@0: import java.io.FilterOutputStream; aoqi@0: import java.io.IOException; aoqi@0: import java.io.InputStream; aoqi@0: import java.io.OutputStream; aoqi@0: import java.net.HttpURLConnection; aoqi@0: import java.util.List; aoqi@0: import java.util.Map; aoqi@0: import java.util.zip.GZIPOutputStream; aoqi@0: import java.util.zip.GZIPInputStream; aoqi@0: aoqi@0: /** aoqi@0: * aoqi@0: * @author WS Development Team aoqi@0: */ aoqi@0: public class HttpClientTransport { aoqi@0: aoqi@0: private static final byte[] THROW_AWAY_BUFFER = new byte[8192]; aoqi@0: aoqi@0: // Need to use JAXB first to register DatatypeConverter aoqi@0: static { aoqi@0: try { aoqi@0: JAXBContext.newInstance().createUnmarshaller(); aoqi@0: } catch(JAXBException je) { aoqi@0: // Nothing much can be done. Intentionally left empty aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /*package*/ int statusCode; aoqi@0: /*package*/ String statusMessage; aoqi@0: /*package*/ int contentLength; aoqi@0: private final Map> reqHeaders; aoqi@0: private Map> respHeaders = null; aoqi@0: aoqi@0: private OutputStream outputStream; aoqi@0: private boolean https; aoqi@0: private HttpURLConnection httpConnection = null; aoqi@0: private final EndpointAddress endpoint; aoqi@0: private final Packet context; aoqi@0: private final Integer chunkSize; aoqi@0: aoqi@0: aoqi@0: public HttpClientTransport(@NotNull Packet packet, @NotNull Map> reqHeaders) { aoqi@0: endpoint = packet.endpointAddress; aoqi@0: context = packet; aoqi@0: this.reqHeaders = reqHeaders; aoqi@0: chunkSize = (Integer)context.invocationProperties.get(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE); aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Prepare the stream for HTTP request aoqi@0: */ aoqi@0: OutputStream getOutput() { aoqi@0: try { aoqi@0: createHttpConnection(); aoqi@0: // for "GET" request no need to get outputStream aoqi@0: if (requiresOutputStream()) { aoqi@0: outputStream = httpConnection.getOutputStream(); aoqi@0: if (chunkSize != null) { aoqi@0: outputStream = new WSChunkedOuputStream(outputStream, chunkSize); aoqi@0: } aoqi@0: List contentEncoding = reqHeaders.get("Content-Encoding"); aoqi@0: // TODO need to find out correct encoding based on q value - RFC 2616 aoqi@0: if (contentEncoding != null && contentEncoding.get(0).contains("gzip")) { aoqi@0: outputStream = new GZIPOutputStream(outputStream); aoqi@0: } aoqi@0: } aoqi@0: httpConnection.connect(); aoqi@0: } catch (Exception ex) { aoqi@0: throw new ClientTransportException( aoqi@0: ClientMessages.localizableHTTP_CLIENT_FAILED(ex),ex); aoqi@0: } aoqi@0: aoqi@0: return outputStream; aoqi@0: } aoqi@0: aoqi@0: void closeOutput() throws IOException { aoqi@0: if (outputStream != null) { aoqi@0: outputStream.close(); aoqi@0: outputStream = null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Get the response from HTTP connection and prepare the input stream for response aoqi@0: */ aoqi@0: @Nullable InputStream getInput() { aoqi@0: // response processing aoqi@0: aoqi@0: InputStream in; aoqi@0: try { aoqi@0: in = readResponse(); aoqi@0: if (in != null) { aoqi@0: String contentEncoding = httpConnection.getContentEncoding(); aoqi@0: if (contentEncoding != null && contentEncoding.contains("gzip")) { aoqi@0: in = new GZIPInputStream(in); aoqi@0: } aoqi@0: } aoqi@0: } catch (IOException e) { aoqi@0: throw new ClientTransportException(ClientMessages.localizableHTTP_STATUS_CODE(statusCode, statusMessage), e); aoqi@0: } aoqi@0: return in; aoqi@0: } aoqi@0: aoqi@0: public Map> getHeaders() { aoqi@0: if (respHeaders != null) { aoqi@0: return respHeaders; aoqi@0: } aoqi@0: respHeaders = new Headers(); aoqi@0: respHeaders.putAll(httpConnection.getHeaderFields()); aoqi@0: return respHeaders; aoqi@0: } aoqi@0: aoqi@0: protected @Nullable InputStream readResponse() { aoqi@0: InputStream is; aoqi@0: try { aoqi@0: is = httpConnection.getInputStream(); aoqi@0: } catch(IOException ioe) { aoqi@0: is = httpConnection.getErrorStream(); aoqi@0: } aoqi@0: if (is == null) { aoqi@0: return is; aoqi@0: } aoqi@0: // Since StreamMessage doesn't read , there aoqi@0: // are some bytes left in the InputStream. This confuses JDK and may aoqi@0: // not reuse underlying sockets. Hopefully JDK fixes it in its code ! aoqi@0: final InputStream temp = is; aoqi@0: return new FilterInputStream(temp) { aoqi@0: // Workaround for "SJSXP XMLStreamReader.next() closes stream". aoqi@0: // So it doesn't read from the closed stream aoqi@0: boolean closed; aoqi@0: @Override aoqi@0: public void close() throws IOException { aoqi@0: if (!closed) { aoqi@0: closed = true; aoqi@0: while(temp.read(THROW_AWAY_BUFFER) != -1); aoqi@0: super.close(); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: aoqi@0: protected void readResponseCodeAndMessage() { aoqi@0: try { aoqi@0: statusCode = httpConnection.getResponseCode(); aoqi@0: statusMessage = httpConnection.getResponseMessage(); aoqi@0: contentLength = httpConnection.getContentLength(); aoqi@0: } catch(IOException ioe) { aoqi@0: throw new WebServiceException(ioe); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: protected HttpURLConnection openConnection(Packet packet) { aoqi@0: // default do nothing aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: protected boolean checkHTTPS(HttpURLConnection connection) { aoqi@0: if (connection instanceof HttpsURLConnection) { aoqi@0: aoqi@0: // TODO The above property needs to be removed in future version as the semantics of this property are not preoperly defined. aoqi@0: // One should use JAXWSProperties.HOSTNAME_VERIFIER to control the behavior aoqi@0: aoqi@0: // does the client want client hostname verification by the service aoqi@0: String verificationProperty = aoqi@0: (String) context.invocationProperties.get(HOSTNAME_VERIFICATION_PROPERTY); aoqi@0: if (verificationProperty != null) { aoqi@0: if (verificationProperty.equalsIgnoreCase("true")) { aoqi@0: ((HttpsURLConnection) connection).setHostnameVerifier(new HttpClientVerifier()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Set application's HostNameVerifier for this connection aoqi@0: HostnameVerifier verifier = aoqi@0: (HostnameVerifier) context.invocationProperties.get(JAXWSProperties.HOSTNAME_VERIFIER); aoqi@0: if (verifier != null) { aoqi@0: ((HttpsURLConnection) connection).setHostnameVerifier(verifier); aoqi@0: } aoqi@0: aoqi@0: // Set application's SocketFactory for this connection aoqi@0: SSLSocketFactory sslSocketFactory = aoqi@0: (SSLSocketFactory) context.invocationProperties.get(JAXWSProperties.SSL_SOCKET_FACTORY); aoqi@0: if (sslSocketFactory != null) { aoqi@0: ((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory); aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: private void createHttpConnection() throws IOException { aoqi@0: httpConnection = openConnection(context); aoqi@0: aoqi@0: if (httpConnection == null) aoqi@0: httpConnection = (HttpURLConnection) endpoint.openConnection(); aoqi@0: aoqi@0: String scheme = endpoint.getURI().getScheme(); aoqi@0: if (scheme.equals("https")) { aoqi@0: https = true; aoqi@0: } aoqi@0: if (checkHTTPS(httpConnection)) aoqi@0: https = true; aoqi@0: aoqi@0: // allow interaction with the web page - user may have to supply aoqi@0: // username, password id web page is accessed from web browser aoqi@0: httpConnection.setAllowUserInteraction(true); aoqi@0: aoqi@0: // enable input, output streams aoqi@0: httpConnection.setDoOutput(true); aoqi@0: httpConnection.setDoInput(true); aoqi@0: aoqi@0: String requestMethod = (String) context.invocationProperties.get(MessageContext.HTTP_REQUEST_METHOD); aoqi@0: String method = (requestMethod != null) ? requestMethod : "POST"; aoqi@0: httpConnection.setRequestMethod(method); aoqi@0: aoqi@0: //this code or something similiar needs t be moved elsewhere for error checking aoqi@0: /*if (context.invocationProperties.get(BindingProviderProperties.BINDING_ID_PROPERTY).equals(HTTPBinding.HTTP_BINDING)){ aoqi@0: method = (requestMethod != null)?requestMethod:method; aoqi@0: } else if aoqi@0: (context.invocationProperties.get(BindingProviderProperties.BINDING_ID_PROPERTY).equals(SOAPBinding.SOAP12HTTP_BINDING) && aoqi@0: "GET".equalsIgnoreCase(requestMethod)) { aoqi@0: } aoqi@0: */ aoqi@0: aoqi@0: Integer reqTimeout = (Integer)context.invocationProperties.get(BindingProviderProperties.REQUEST_TIMEOUT); aoqi@0: if (reqTimeout != null) { aoqi@0: httpConnection.setReadTimeout(reqTimeout); aoqi@0: } aoqi@0: aoqi@0: Integer connectTimeout = (Integer)context.invocationProperties.get(JAXWSProperties.CONNECT_TIMEOUT); aoqi@0: if (connectTimeout != null) { aoqi@0: httpConnection.setConnectTimeout(connectTimeout); aoqi@0: } aoqi@0: aoqi@0: Integer chunkSize = (Integer)context.invocationProperties.get(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE); aoqi@0: if (chunkSize != null) { aoqi@0: httpConnection.setChunkedStreamingMode(chunkSize); aoqi@0: } aoqi@0: aoqi@0: // set the properties on HttpURLConnection aoqi@0: for (Map.Entry> entry : reqHeaders.entrySet()) { aoqi@0: if ("Content-Length".equals(entry.getKey())) continue; aoqi@0: for(String value : entry.getValue()) { aoqi@0: httpConnection.addRequestProperty(entry.getKey(), value); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: boolean isSecure() { aoqi@0: return https; aoqi@0: } aoqi@0: aoqi@0: protected void setStatusCode(int statusCode) { aoqi@0: this.statusCode = statusCode; aoqi@0: } aoqi@0: aoqi@0: private boolean requiresOutputStream() { aoqi@0: return !(httpConnection.getRequestMethod().equalsIgnoreCase("GET") || aoqi@0: httpConnection.getRequestMethod().equalsIgnoreCase("HEAD") || aoqi@0: httpConnection.getRequestMethod().equalsIgnoreCase("DELETE")); aoqi@0: } aoqi@0: aoqi@0: @Nullable String getContentType() { aoqi@0: return httpConnection.getContentType(); aoqi@0: } aoqi@0: aoqi@0: public int getContentLength() { aoqi@0: return httpConnection.getContentLength(); aoqi@0: } aoqi@0: aoqi@0: // overide default SSL HttpClientVerifier to always return true aoqi@0: // effectively overiding Hostname client verification when using SSL aoqi@0: private static class HttpClientVerifier implements HostnameVerifier { aoqi@0: public boolean verify(String s, SSLSession sslSession) { aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static class LocalhostHttpClientVerifier implements HostnameVerifier { aoqi@0: public boolean verify(String s, SSLSession sslSession) { aoqi@0: return "localhost".equalsIgnoreCase(s) || "127.0.0.1".equals(s); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * HttpURLConnection.getOuputStream() returns sun.net.www.http.ChunkedOuputStream in chunked aoqi@0: * streaming mode. If you call ChunkedOuputStream.write(byte[20MB], int, int), then the whole data aoqi@0: * is kept in memory. This wraps the ChunkedOuputStream so that it writes only small aoqi@0: * chunks. aoqi@0: */ aoqi@0: private static final class WSChunkedOuputStream extends FilterOutputStream { aoqi@0: final int chunkSize; aoqi@0: aoqi@0: WSChunkedOuputStream(OutputStream actual, int chunkSize) { aoqi@0: super(actual); aoqi@0: this.chunkSize = chunkSize; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void write(byte b[], int off, int len) throws IOException { aoqi@0: while(len > 0) { aoqi@0: int sent = (len > chunkSize) ? chunkSize : len; aoqi@0: out.write(b, off, sent); // don't use super.write() as it writes byte-by-byte aoqi@0: len -= sent; aoqi@0: off += sent; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: }