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.api.model.SEIModel; ohair@286: import com.sun.xml.internal.ws.binding.BindingImpl; ohair@286: import com.sun.xml.internal.ws.message.DataHandlerAttachment; ohair@286: import com.sun.xml.internal.ws.model.AbstractSEIModelImpl; ohair@286: import com.sun.xml.internal.ws.spi.db.BindingContext; ohair@286: ohair@286: import javax.xml.ws.handler.LogicalHandler; 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.List; ohair@286: import java.util.ArrayList; ohair@286: import java.util.Map; alanb@368: import java.util.Map.Entry; ohair@286: ohair@286: /** ohair@286: * ohair@286: * @author WS Development Team ohair@286: */ ohair@286: public class ServerLogicalHandlerTube extends HandlerTube { ohair@286: ohair@286: private SEIModel seiModel; ohair@286: ohair@286: /** ohair@286: * Creates a new instance of LogicalHandlerTube ohair@286: */ ohair@286: public ServerLogicalHandlerTube(WSBinding binding, SEIModel seiModel, WSDLPort port, Tube next) { ohair@286: super(next, port, binding); ohair@286: this.seiModel = seiModel; ohair@286: setUpHandlersOnce(); ohair@286: } ohair@286: ohair@286: /** ohair@286: * This constructor is used on client-side where, SOAPHandlerTube is created ohair@286: * first and then a LogicalHandlerTube is created with a handler to that ohair@286: * SOAPHandlerTube. ohair@286: * With this handle, LogicalHandlerTube can call ohair@286: * SOAPHandlerTube.closeHandlers() ohair@286: */ ohair@286: public ServerLogicalHandlerTube(WSBinding binding, SEIModel seiModel, Tube next, HandlerTube cousinTube) { ohair@286: super(next, cousinTube, binding); ohair@286: this.seiModel = seiModel; 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: ohair@286: private ServerLogicalHandlerTube(ServerLogicalHandlerTube that, TubeCloner cloner) { ohair@286: super(that, cloner); ohair@286: this.seiModel = that.seiModel; ohair@286: this.handlers = that.handlers; ohair@286: } ohair@286: ohair@286: //should be overridden by DriverHandlerTubes ohair@286: @Override ohair@286: protected void initiateClosing(MessageContext mc) { ohair@286: if (getBinding().getSOAPVersion() != null) { ohair@286: super.initiateClosing(mc); ohair@286: } else { ohair@286: close(mc); ohair@286: super.initiateClosing(mc); ohair@286: } ohair@286: } ohair@286: ohair@286: public AbstractFilterTubeImpl copy(TubeCloner cloner) { ohair@286: return new ServerLogicalHandlerTube(this, cloner); ohair@286: } ohair@286: ohair@286: private void setUpHandlersOnce() { ohair@286: handlers = new ArrayList(); ohair@286: List logicalSnapShot= ((BindingImpl) getBinding()).getHandlerConfig().getLogicalHandlers(); ohair@286: if (!logicalSnapShot.isEmpty()) { ohair@286: handlers.addAll(logicalSnapShot); 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: if (getBinding().getSOAPVersion() == null) { ohair@286: processor = new XMLHandlerProcessor(this, getBinding(), ohair@286: handlers); ohair@286: } else { ohair@286: processor = new SOAPHandlerProcessor(false, this, getBinding(), handlers); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: MessageUpdatableContext getContext(Packet packet) { ohair@286: return new LogicalMessageContextImpl(getBinding(), getBindingContext(), packet); ohair@286: } ohair@286: ohair@286: private BindingContext getBindingContext() { ohair@286: return (seiModel!= null && seiModel instanceof AbstractSEIModelImpl) ? ohair@286: ((AbstractSEIModelImpl)seiModel).getBindingContext() : null; 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: 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: //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 (Entry entry : atts.entrySet()) { alanb@368: String cid = entry.getKey(); ohair@286: Attachment att = new DataHandlerAttachment(cid, atts.get(cid)); ohair@286: attSet.add(att); 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: void closeHandlers(MessageContext mc) { ohair@286: closeServersideHandlers(mc); ohair@286: ohair@286: } ohair@286: }