1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.ws.handler;
27
28import com.sun.xml.internal.ws.api.WSBinding;
29import com.sun.xml.internal.ws.api.message.Packet;
30import com.sun.xml.internal.ws.api.message.AttachmentSet;
31import com.sun.xml.internal.ws.api.message.Attachment;
32import com.sun.xml.internal.ws.api.pipe.TubeCloner;
33import com.sun.xml.internal.ws.api.pipe.Tube;
34import com.sun.xml.internal.ws.api.pipe.helper.AbstractFilterTubeImpl;
35import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
36import com.sun.xml.internal.ws.client.HandlerConfiguration;
37import com.sun.xml.internal.ws.binding.BindingImpl;
38import com.sun.xml.internal.ws.message.DataHandlerAttachment;
39
40import javax.xml.ws.handler.soap.SOAPHandler;
41import javax.xml.ws.handler.MessageContext;
42import javax.xml.ws.handler.Handler;
43import javax.xml.ws.WebServiceException;
44import javax.activation.DataHandler;
45import java.util.*;
46
47/**
48 *
49 * @author WS Development Team
50 */
51public class ServerSOAPHandlerTube extends HandlerTube {
52
53    private Set<String> roles;
54
55    /**
56     * Creates a new instance of SOAPHandlerTube
57     */
58    public ServerSOAPHandlerTube(WSBinding binding, WSDLPort port, Tube next) {
59        super(next, port, binding);
60        if (binding.getSOAPVersion() != null) {
61            // SOAPHandlerTube should n't be used for bindings other than SOAP.
62            // TODO: throw Exception
63        }
64        setUpHandlersOnce();
65    }
66
67    // Handle to LogicalHandlerTube means its used on SERVER-SIDE
68
69    /**
70     * This constructor is used on client-side where, LogicalHandlerTube is created
71     * first and then a SOAPHandlerTube is created with a handler to that
72     * LogicalHandlerTube.
73     * With this handle, SOAPHandlerTube can call LogicalHandlerTube.closeHandlers()
74     */
75    public ServerSOAPHandlerTube(WSBinding binding, Tube next, HandlerTube cousinTube) {
76        super(next, cousinTube, binding);
77        setUpHandlersOnce();
78    }
79
80    /**
81     * Copy constructor for {@link com.sun.xml.internal.ws.api.pipe.Tube#copy(com.sun.xml.internal.ws.api.pipe.TubeCloner)}.
82     */
83    private ServerSOAPHandlerTube(ServerSOAPHandlerTube that, TubeCloner cloner) {
84        super(that, cloner);
85        this.handlers = that.handlers;
86        this.roles = that.roles;
87    }
88
89
90    public AbstractFilterTubeImpl copy(TubeCloner cloner) {
91        return new ServerSOAPHandlerTube(this, cloner);
92    }
93
94    private void setUpHandlersOnce() {
95        handlers = new ArrayList<Handler>();
96        HandlerConfiguration handlerConfig = ((BindingImpl) getBinding()).getHandlerConfig();
97        List<SOAPHandler> soapSnapShot= handlerConfig.getSoapHandlers();
98        if (!soapSnapShot.isEmpty()) {
99            handlers.addAll(soapSnapShot);
100            roles = new HashSet<String>();
101            roles.addAll(handlerConfig.getRoles());
102        }
103    }
104
105    protected void resetProcessor() {
106        processor = null;
107    }
108
109    void setUpProcessor() {
110        if(!handlers.isEmpty() && processor == null)
111            processor = new SOAPHandlerProcessor(false, this, getBinding(), handlers);
112    }
113    MessageUpdatableContext getContext(Packet packet) {
114        SOAPMessageContextImpl context = new SOAPMessageContextImpl(getBinding(), packet,roles);
115        return context;
116    }
117
118    boolean callHandlersOnRequest(MessageUpdatableContext context, boolean isOneWay) {
119
120        boolean handlerResult;
121        try {
122            //SERVER-SIDE
123            handlerResult = processor.callHandlersRequest(HandlerProcessor.Direction.INBOUND, context, !isOneWay);
124
125        } catch (RuntimeException re) {
126            remedyActionTaken = true;
127            throw re;
128
129        }
130        if (!handlerResult) {
131            remedyActionTaken = true;
132        }
133        return handlerResult;
134    }
135
136    void callHandlersOnResponse(MessageUpdatableContext context, boolean handleFault) {
137
138        //Lets copy all the MessageContext.OUTBOUND_ATTACHMENT_PROPERTY to the message
139        Map<String, DataHandler> atts = (Map<String, DataHandler>) context.get(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS);
140        AttachmentSet attSet = context.packet.getMessage().getAttachments();
141        for (Map.Entry<String, DataHandler> entry : atts.entrySet()) {
142            String cid = entry.getKey();
143            if (attSet.get(cid) == null) { // Otherwise we would be adding attachments twice
144                Attachment att = new DataHandlerAttachment(cid, atts.get(cid));
145                attSet.add(att);
146            }
147        }
148
149        try {
150            //SERVER-SIDE
151            processor.callHandlersResponse(HandlerProcessor.Direction.OUTBOUND, context, handleFault);
152
153        } catch (WebServiceException wse) {
154            //no rewrapping
155            throw wse;
156        } catch (RuntimeException re) {
157            throw re;
158
159        }
160    }
161
162    void closeHandlers(MessageContext mc) {
163        closeServersideHandlers(mc);
164
165    }
166}
167