001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.xml.ws.transport.http.client;
037:
038: import com.sun.istack.NotNull;
039: import com.sun.xml.ws.api.message.Packet;
040: import com.sun.xml.ws.api.pipe.Codec;
041: import com.sun.xml.ws.api.pipe.ContentType;
042: import com.sun.xml.ws.api.pipe.NextAction;
043: import com.sun.xml.ws.api.pipe.Pipe;
044: import com.sun.xml.ws.api.pipe.Tube;
045: import com.sun.xml.ws.api.pipe.TubeCloner;
046: import com.sun.xml.ws.api.pipe.helper.AbstractTubeImpl;
047: import com.sun.xml.ws.transport.http.WSHTTPConnection;
048: import com.sun.xml.ws.util.ByteArrayBuffer;
049:
050: import javax.xml.ws.BindingProvider;
051: import javax.xml.ws.WebServiceException;
052: import javax.xml.ws.handler.MessageContext;
053: import java.io.IOException;
054: import java.io.InputStream;
055: import java.io.OutputStream;
056: import java.util.Collections;
057: import java.util.HashMap;
058: import java.util.List;
059: import java.util.Map;
060: import java.util.Map.Entry;
061:
062: /**
063: * {@link Pipe} and {@link Tube} that sends a request to a remote HTTP server.
064: *
065: * @author Jitendra Kotamraju
066: */
067: public class HttpTransportPipe extends AbstractTubeImpl {
068:
069: private final Codec codec;
070:
071: public HttpTransportPipe(Codec codec) {
072: this .codec = codec;
073: }
074:
075: /**
076: * Copy constructor for {@link Tube#copy(TubeCloner)}.
077: */
078: private HttpTransportPipe(HttpTransportPipe that, TubeCloner cloner) {
079: this (that.codec.copy());
080: cloner.add(that, this );
081: }
082:
083: public NextAction processException(@NotNull
084: Throwable t) {
085: throw new IllegalStateException(
086: "HttpTransportPipe's processException shouldn't be called.");
087: }
088:
089: public NextAction processRequest(@NotNull
090: Packet request) {
091: return doReturnWith(process(request));
092: }
093:
094: public NextAction processResponse(@NotNull
095: Packet response) {
096: throw new IllegalStateException(
097: "HttpTransportPipe's processResponse shouldn't be called.");
098: }
099:
100: public Packet process(Packet request) {
101: HttpClientTransport con;
102: try {
103: // get transport headers from message
104: Map<String, List<String>> reqHeaders = (Map<String, List<String>>) request.invocationProperties
105: .get(MessageContext.HTTP_REQUEST_HEADERS);
106: //assign empty map if its null
107: if (reqHeaders == null) {
108: reqHeaders = new HashMap<String, List<String>>();
109: }
110:
111: con = new HttpClientTransport(request, reqHeaders);
112: request.addSatellite(new HttpResponseProperties(con));
113:
114: ContentType ct = codec.getStaticContentType(request);
115: if (ct == null) {
116: ByteArrayBuffer buf = new ByteArrayBuffer();
117:
118: ct = codec.encode(request, buf);
119: // data size is available, set it as Content-Length
120: reqHeaders.put("Content-Length", Collections
121: .singletonList(Integer.toString(buf.size())));
122: reqHeaders.put("Content-Type", Collections
123: .singletonList(ct.getContentType()));
124: if (ct.getAcceptHeader() != null) {
125: reqHeaders.put("Accept", Collections
126: .singletonList(ct.getAcceptHeader()));
127: }
128: writeSOAPAction(reqHeaders, ct.getSOAPActionHeader(),
129: request);
130:
131: if (dump)
132: dump(buf, "HTTP request", reqHeaders);
133:
134: buf.writeTo(con.getOutput());
135: } else {
136: // Set static Content-Type
137: reqHeaders.put("Content-Type", Collections
138: .singletonList(ct.getContentType()));
139: if (ct.getAcceptHeader() != null) {
140: reqHeaders.put("Accept", Collections
141: .singletonList(ct.getAcceptHeader()));
142: }
143: writeSOAPAction(reqHeaders, ct.getSOAPActionHeader(),
144: request);
145:
146: if (dump) {
147: ByteArrayBuffer buf = new ByteArrayBuffer();
148: codec.encode(request, buf);
149: dump(buf, "HTTP request", reqHeaders);
150: OutputStream out = con.getOutput();
151: if (out != null) {
152: buf.writeTo(out);
153: }
154: } else {
155: OutputStream os = con.getOutput();
156: if (os != null) {
157: codec.encode(request, os);
158: }
159: }
160: }
161:
162: con.closeOutput();
163:
164: con.checkResponseCode();
165: InputStream response = con.getInput();
166: if (dump) {
167: ByteArrayBuffer buf = new ByteArrayBuffer();
168: buf.write(response);
169: dump(buf, "HTTP response " + con.statusCode, con
170: .getHeaders());
171: response = buf.newInputStream();
172: }
173:
174: if (con.statusCode == WSHTTPConnection.ONEWAY
175: || (request.expectReply != null && !request.expectReply)) {
176: return request.createClientResponse(null); // one way. no response given.
177: }
178: String contentType = con.getContentType();
179: if (contentType == null) {
180: throw new WebServiceException(
181: "No Content-type in the header!");
182: }
183:
184: // TODO check if returned MIME type is the same as that which was sent
185: // or is acceptable if an Accept header was used
186: Packet reply = request.createClientResponse(null);
187: //reply.addSatellite(new HttpResponseProperties(con));
188: reply.wasTransportSecure = con.isSecure();
189: codec.decode(response, contentType, reply);
190: return reply;
191: } catch (WebServiceException wex) {
192: throw wex;
193: } catch (Exception ex) {
194: throw new WebServiceException(ex);
195: }
196: }
197:
198: /**
199: * write SOAPAction header if the soapAction parameter is non-null or BindingProvider properties set.
200: * BindingProvider properties take precedence.
201: */
202: private void writeSOAPAction(Map<String, List<String>> reqHeaders,
203: String soapAction, Packet packet) {
204: if (soapAction != null)
205: reqHeaders.put("SOAPAction", Collections
206: .singletonList(soapAction));
207: else
208: reqHeaders.put("SOAPAction", Collections
209: .singletonList("\"\""));
210: }
211:
212: public void preDestroy() {
213: // nothing to do. Intentionally left empty.
214: }
215:
216: public HttpTransportPipe copy(TubeCloner cloner) {
217: return new HttpTransportPipe(this , cloner);
218: }
219:
220: private void dump(ByteArrayBuffer buf, String caption,
221: Map<String, List<String>> headers) throws IOException {
222: System.out.println("---[" + caption + "]---");
223: for (Entry<String, List<String>> header : headers.entrySet()) {
224: if (header.getValue().isEmpty()) {
225: // I don't think this is legal, but let's just dump it,
226: // as the point of the dump is to uncover problems.
227: System.out.println(header.getValue());
228: } else {
229: for (String value : header.getValue()) {
230: System.out.println(header.getKey() + ": " + value);
231: }
232: }
233: }
234:
235: buf.writeTo(System.out);
236: System.out.println("--------------------");
237: }
238:
239: /**
240: * Dumps what goes across HTTP transport.
241: */
242: public static boolean dump;
243:
244: static {
245: boolean b;
246: try {
247: b = Boolean.getBoolean(HttpTransportPipe.class.getName()
248: + ".dump");
249: } catch (Throwable t) {
250: b = false;
251: }
252: dump = b;
253: }
254: }
|