ohair@286: /* alanb@368: * Copyright (c) 1997, 2012, 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.handler; ohair@286: ohair@286: import com.sun.xml.internal.ws.api.WSBinding; ohair@286: import com.sun.xml.internal.ws.api.message.Packet; ohair@286: import com.sun.xml.internal.ws.api.message.AttachmentSet; ohair@286: import com.sun.xml.internal.ws.api.message.Attachment; ohair@286: import com.sun.xml.internal.ws.api.pipe.TubeCloner; ohair@286: import com.sun.xml.internal.ws.api.pipe.Tube; ohair@286: import com.sun.xml.internal.ws.api.pipe.helper.AbstractFilterTubeImpl; ohair@286: import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; ohair@286: import com.sun.xml.internal.ws.client.HandlerConfiguration; ohair@286: import com.sun.xml.internal.ws.binding.BindingImpl; ohair@286: import com.sun.xml.internal.ws.message.DataHandlerAttachment; ohair@286: ohair@286: import javax.xml.ws.handler.soap.SOAPHandler; ohair@286: import javax.xml.ws.handler.MessageContext; ohair@286: import javax.xml.ws.handler.Handler; ohair@286: import javax.xml.ws.WebServiceException; ohair@286: import javax.activation.DataHandler; ohair@286: import java.util.*; ohair@286: ohair@286: /** ohair@286: * ohair@286: * @author WS Development Team ohair@286: */ ohair@286: public class ServerSOAPHandlerTube extends HandlerTube { ohair@286: ohair@286: private Set roles; ohair@286: ohair@286: /** ohair@286: * Creates a new instance of SOAPHandlerTube ohair@286: */ ohair@286: public ServerSOAPHandlerTube(WSBinding binding, WSDLPort port, Tube next) { ohair@286: super(next, port, binding); ohair@286: if (binding.getSOAPVersion() != null) { ohair@286: // SOAPHandlerTube should n't be used for bindings other than SOAP. ohair@286: // TODO: throw Exception ohair@286: } ohair@286: setUpHandlersOnce(); ohair@286: } ohair@286: ohair@286: // Handle to LogicalHandlerTube means its used on SERVER-SIDE ohair@286: ohair@286: /** ohair@286: * This constructor is used on client-side where, LogicalHandlerTube is created ohair@286: * first and then a SOAPHandlerTube is created with a handler to that ohair@286: * LogicalHandlerTube. ohair@286: * With this handle, SOAPHandlerTube can call LogicalHandlerTube.closeHandlers() ohair@286: */ ohair@286: public ServerSOAPHandlerTube(WSBinding binding, Tube next, HandlerTube cousinTube) { ohair@286: super(next, cousinTube, binding); ohair@286: setUpHandlersOnce(); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Copy constructor for {@link com.sun.xml.internal.ws.api.pipe.Tube#copy(com.sun.xml.internal.ws.api.pipe.TubeCloner)}. ohair@286: */ ohair@286: private ServerSOAPHandlerTube(ServerSOAPHandlerTube that, TubeCloner cloner) { ohair@286: super(that, cloner); ohair@286: this.handlers = that.handlers; ohair@286: this.roles = that.roles; ohair@286: } ohair@286: ohair@286: ohair@286: public AbstractFilterTubeImpl copy(TubeCloner cloner) { ohair@286: return new ServerSOAPHandlerTube(this, cloner); ohair@286: } ohair@286: ohair@286: private void setUpHandlersOnce() { ohair@286: handlers = new ArrayList(); ohair@286: HandlerConfiguration handlerConfig = ((BindingImpl) getBinding()).getHandlerConfig(); ohair@286: List soapSnapShot= handlerConfig.getSoapHandlers(); ohair@286: if (!soapSnapShot.isEmpty()) { ohair@286: handlers.addAll(soapSnapShot); ohair@286: roles = new HashSet(); ohair@286: roles.addAll(handlerConfig.getRoles()); ohair@286: } ohair@286: } ohair@286: ohair@286: protected void resetProcessor() { ohair@286: processor = null; ohair@286: } ohair@286: ohair@286: void setUpProcessor() { ohair@286: if(!handlers.isEmpty() && processor == null) ohair@286: processor = new SOAPHandlerProcessor(false, this, getBinding(), handlers); ohair@286: } ohair@286: MessageUpdatableContext getContext(Packet packet) { ohair@286: SOAPMessageContextImpl context = new SOAPMessageContextImpl(getBinding(), packet,roles); ohair@286: return context; ohair@286: } ohair@286: ohair@286: boolean callHandlersOnRequest(MessageUpdatableContext context, boolean isOneWay) { ohair@286: ohair@286: boolean handlerResult; ohair@286: try { ohair@286: //SERVER-SIDE ohair@286: handlerResult = processor.callHandlersRequest(HandlerProcessor.Direction.INBOUND, context, !isOneWay); ohair@286: ohair@286: } catch (RuntimeException re) { ohair@286: remedyActionTaken = true; ohair@286: throw re; ohair@286: ohair@286: } ohair@286: if (!handlerResult) { ohair@286: remedyActionTaken = true; ohair@286: } ohair@286: return handlerResult; ohair@286: } ohair@286: ohair@286: void callHandlersOnResponse(MessageUpdatableContext context, boolean handleFault) { ohair@286: ohair@286: //Lets copy all the MessageContext.OUTBOUND_ATTACHMENT_PROPERTY to the message ohair@286: Map atts = (Map) context.get(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS); ohair@286: AttachmentSet attSet = context.packet.getMessage().getAttachments(); alanb@368: for (Map.Entry entry : atts.entrySet()) { alanb@368: String cid = entry.getKey(); ohair@286: if (attSet.get(cid) == null) { // Otherwise we would be adding attachments twice ohair@286: Attachment att = new DataHandlerAttachment(cid, atts.get(cid)); ohair@286: attSet.add(att); ohair@286: } ohair@286: } ohair@286: ohair@286: try { ohair@286: //SERVER-SIDE ohair@286: processor.callHandlersResponse(HandlerProcessor.Direction.OUTBOUND, context, handleFault); ohair@286: ohair@286: } catch (WebServiceException wse) { ohair@286: //no rewrapping ohair@286: throw wse; ohair@286: } catch (RuntimeException re) { ohair@286: throw re; ohair@286: ohair@286: } ohair@286: } ohair@286: ohair@286: void closeHandlers(MessageContext mc) { ohair@286: closeServersideHandlers(mc); ohair@286: ohair@286: } ohair@286: }