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.server.provider;
037:
038: import com.sun.istack.Nullable;
039: import com.sun.xml.ws.api.SOAPVersion;
040: import com.sun.xml.ws.api.WSBinding;
041: import com.sun.xml.ws.api.message.Message;
042: import com.sun.xml.ws.api.message.Messages;
043: import com.sun.xml.ws.api.message.Packet;
044: import com.sun.xml.ws.api.model.wsdl.WSDLPort;
045: import com.sun.xml.ws.fault.SOAPFaultBuilder;
046: import com.sun.xml.ws.resources.ServerMessages;
047:
048: import javax.xml.soap.MimeHeader;
049: import javax.xml.soap.MimeHeaders;
050: import javax.xml.soap.SOAPException;
051: import javax.xml.soap.SOAPMessage;
052: import javax.xml.transform.Source;
053: import javax.xml.ws.Service;
054: import javax.xml.ws.WebServiceException;
055: import java.util.ArrayList;
056: import java.util.HashMap;
057: import java.util.Iterator;
058: import java.util.List;
059: import java.util.Map;
060:
061: /**
062: * @author Jitendra Kotamraju
063: */
064: abstract class SOAPProviderArgumentBuilder<T> extends
065: ProviderArgumentsBuilder<T> {
066: protected final SOAPVersion soapVersion;
067:
068: private SOAPProviderArgumentBuilder(SOAPVersion soapVersion) {
069: this .soapVersion = soapVersion;
070: }
071:
072: static ProviderArgumentsBuilder create(ProviderEndpointModel model,
073: SOAPVersion soapVersion) {
074: if (model.mode == Service.Mode.PAYLOAD) {
075: return new PayloadSource(soapVersion);
076: } else {
077: if (model.datatype == Source.class)
078: return new MessageSource(soapVersion);
079: if (model.datatype == SOAPMessage.class)
080: return new SOAPMessageParameter(soapVersion);
081: if (model.datatype == Message.class)
082: return new MessageProviderArgumentBuilder(soapVersion);
083: throw new WebServiceException(ServerMessages
084: .PROVIDER_INVALID_PARAMETER_TYPE(model.implClass,
085: model.datatype));
086: }
087: }
088:
089: private static final class PayloadSource extends
090: SOAPProviderArgumentBuilder<Source> {
091: PayloadSource(SOAPVersion soapVersion) {
092: super (soapVersion);
093: }
094:
095: protected Source getParameter(Packet packet) {
096: return packet.getMessage().readPayloadAsSource();
097: }
098:
099: protected Message getResponseMessage(Source source) {
100: return Messages.createUsingPayload(source, soapVersion);
101: }
102:
103: protected Message getResponseMessage(Exception e) {
104: return SOAPFaultBuilder.createSOAPFaultMessage(soapVersion,
105: null, e);
106: }
107:
108: }
109:
110: private static final class MessageSource extends
111: SOAPProviderArgumentBuilder<Source> {
112: MessageSource(SOAPVersion soapVersion) {
113: super (soapVersion);
114: }
115:
116: protected Source getParameter(Packet packet) {
117: return packet.getMessage().readEnvelopeAsSource();
118: }
119:
120: protected Message getResponseMessage(Source source) {
121: return Messages.create(source, soapVersion);
122: }
123:
124: protected Message getResponseMessage(Exception e) {
125: return SOAPFaultBuilder.createSOAPFaultMessage(soapVersion,
126: null, e);
127: }
128: }
129:
130: private static final class SOAPMessageParameter extends
131: SOAPProviderArgumentBuilder<SOAPMessage> {
132: SOAPMessageParameter(SOAPVersion soapVersion) {
133: super (soapVersion);
134: }
135:
136: protected SOAPMessage getParameter(Packet packet) {
137: try {
138: return packet.getMessage().readAsSOAPMessage(packet,
139: true);
140: } catch (SOAPException se) {
141: throw new WebServiceException(se);
142: }
143: }
144:
145: protected Message getResponseMessage(SOAPMessage soapMsg) {
146: return Messages.create(soapMsg);
147: }
148:
149: protected Message getResponseMessage(Exception e) {
150: return SOAPFaultBuilder.createSOAPFaultMessage(soapVersion,
151: null, e);
152: }
153:
154: @Override
155: protected Packet getResponse(Packet request, @Nullable
156: SOAPMessage returnValue, WSDLPort port, WSBinding binding) {
157: Packet response = super .getResponse(request, returnValue,
158: port, binding);
159: // Populate SOAPMessage's transport headers
160: if (returnValue != null
161: && response
162: .supports(Packet.OUTBOUND_TRANSPORT_HEADERS)) {
163: MimeHeaders hdrs = returnValue.getMimeHeaders();
164: Map<String, List<String>> headers = new HashMap<String, List<String>>();
165: Iterator i = hdrs.getAllHeaders();
166: while (i.hasNext()) {
167: MimeHeader header = (MimeHeader) i.next();
168: if (header.getName().equalsIgnoreCase("SOAPAction"))
169: // SAAJ sets this header automatically, but it interferes with the correct operation of JAX-WS.
170: // so ignore this header.
171: continue;
172:
173: List<String> list = headers.get(header.getName());
174: if (list == null) {
175: list = new ArrayList<String>();
176: headers.put(header.getName(), list);
177: }
178: list.add(header.getValue());
179: }
180: response
181: .put(Packet.OUTBOUND_TRANSPORT_HEADERS, headers);
182: }
183: return response;
184: }
185:
186: }
187:
188: }
|