ohair@286: /* ohair@286: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.server.provider; ohair@286: ohair@286: import com.sun.istack.internal.Nullable; ohair@286: import com.sun.xml.internal.ws.api.SOAPVersion; ohair@286: import com.sun.xml.internal.ws.api.WSBinding; ohair@286: import com.sun.xml.internal.ws.api.message.Message; ohair@286: import com.sun.xml.internal.ws.api.message.Messages; ohair@286: import com.sun.xml.internal.ws.api.message.Packet; ohair@286: import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; ohair@286: import com.sun.xml.internal.ws.fault.SOAPFaultBuilder; ohair@286: import com.sun.xml.internal.ws.resources.ServerMessages; ohair@286: ohair@286: import javax.xml.soap.MimeHeader; ohair@286: import javax.xml.soap.MimeHeaders; ohair@286: import javax.xml.soap.SOAPException; ohair@286: import javax.xml.soap.SOAPMessage; ohair@286: import javax.xml.transform.Source; ohair@286: import javax.xml.ws.Service; ohair@286: import javax.xml.ws.WebServiceException; ohair@286: import java.util.ArrayList; ohair@286: import java.util.HashMap; ohair@286: import java.util.Iterator; ohair@286: import java.util.List; ohair@286: import java.util.Map; ohair@286: ohair@286: /** ohair@286: * @author Jitendra Kotamraju ohair@286: */ ohair@286: abstract class SOAPProviderArgumentBuilder extends ProviderArgumentsBuilder { ohair@286: protected final SOAPVersion soapVersion; ohair@286: ohair@286: private SOAPProviderArgumentBuilder(SOAPVersion soapVersion) { ohair@286: this.soapVersion = soapVersion; ohair@286: } ohair@286: ohair@286: static ProviderArgumentsBuilder create(ProviderEndpointModel model, SOAPVersion soapVersion) { ohair@286: if (model.mode == Service.Mode.PAYLOAD) { ohair@286: return new PayloadSource(soapVersion); ohair@286: } else { ohair@286: if(model.datatype==Source.class) ohair@286: return new MessageSource(soapVersion); ohair@286: if(model.datatype==SOAPMessage.class) ohair@286: return new SOAPMessageParameter(soapVersion); ohair@286: if(model.datatype==Message.class) ohair@286: return new MessageProviderArgumentBuilder(soapVersion); ohair@286: throw new WebServiceException(ServerMessages.PROVIDER_INVALID_PARAMETER_TYPE(model.implClass,model.datatype)); ohair@286: } ohair@286: } ohair@286: ohair@286: private static final class PayloadSource extends SOAPProviderArgumentBuilder { ohair@286: PayloadSource(SOAPVersion soapVersion) { ohair@286: super(soapVersion); ohair@286: } ohair@286: ohair@286: protected Source getParameter(Packet packet) { ohair@286: return packet.getMessage().readPayloadAsSource(); ohair@286: } ohair@286: ohair@286: protected Message getResponseMessage(Source source) { ohair@286: return Messages.createUsingPayload(source, soapVersion); ohair@286: } ohair@286: ohair@286: protected Message getResponseMessage(Exception e) { ohair@286: return SOAPFaultBuilder.createSOAPFaultMessage(soapVersion, null, e); ohair@286: } ohair@286: ohair@286: } ohair@286: ohair@286: private static final class MessageSource extends SOAPProviderArgumentBuilder { ohair@286: MessageSource(SOAPVersion soapVersion) { ohair@286: super(soapVersion); ohair@286: } ohair@286: ohair@286: protected Source getParameter(Packet packet) { ohair@286: return packet.getMessage().readEnvelopeAsSource(); ohair@286: } ohair@286: ohair@286: protected Message getResponseMessage(Source source) { ohair@286: return Messages.create(source, soapVersion); ohair@286: } ohair@286: ohair@286: protected Message getResponseMessage(Exception e) { ohair@286: return SOAPFaultBuilder.createSOAPFaultMessage(soapVersion, null, e); ohair@286: } ohair@286: } ohair@286: ohair@286: private static final class SOAPMessageParameter extends SOAPProviderArgumentBuilder { ohair@286: SOAPMessageParameter(SOAPVersion soapVersion) { ohair@286: super(soapVersion); ohair@286: } ohair@286: ohair@286: protected SOAPMessage getParameter(Packet packet) { ohair@286: try { ohair@286: return packet.getMessage().readAsSOAPMessage(packet, true); ohair@286: } catch (SOAPException se) { ohair@286: throw new WebServiceException(se); ohair@286: } ohair@286: } ohair@286: ohair@286: protected Message getResponseMessage(SOAPMessage soapMsg) { ohair@286: return Messages.create(soapMsg); ohair@286: } ohair@286: ohair@286: protected Message getResponseMessage(Exception e) { ohair@286: return SOAPFaultBuilder.createSOAPFaultMessage(soapVersion, null, e); ohair@286: } ohair@286: ohair@286: @Override ohair@286: protected Packet getResponse(Packet request, @Nullable SOAPMessage returnValue, WSDLPort port, WSBinding binding) { ohair@286: Packet response = super.getResponse(request, returnValue, port, binding); ohair@286: // Populate SOAPMessage's transport headers ohair@286: if (returnValue != null && response.supports(Packet.OUTBOUND_TRANSPORT_HEADERS)) { ohair@286: MimeHeaders hdrs = returnValue.getMimeHeaders(); ohair@286: Map> headers = new HashMap>(); ohair@286: Iterator i = hdrs.getAllHeaders(); ohair@286: while(i.hasNext()) { ohair@286: MimeHeader header = (MimeHeader)i.next(); ohair@286: if(header.getName().equalsIgnoreCase("SOAPAction")) ohair@286: // SAAJ sets this header automatically, but it interferes with the correct operation of JAX-WS. ohair@286: // so ignore this header. ohair@286: continue; ohair@286: ohair@286: List list = headers.get(header.getName()); ohair@286: if (list == null) { ohair@286: list = new ArrayList(); ohair@286: headers.put(header.getName(), list); ohair@286: } ohair@286: list.add(header.getValue()); ohair@286: } ohair@286: response.put(Packet.OUTBOUND_TRANSPORT_HEADERS, headers); ohair@286: } ohair@286: return response; ohair@286: } ohair@286: ohair@286: } ohair@286: ohair@286: }