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.pipe.TubeCloner;
31import com.sun.xml.internal.ws.api.pipe.Tube;
32import com.sun.xml.internal.ws.api.pipe.helper.AbstractFilterTubeImpl;
33import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
34import com.sun.xml.internal.ws.api.model.SEIModel;
35import com.sun.xml.internal.ws.binding.BindingImpl;
36import com.sun.xml.internal.ws.model.AbstractSEIModelImpl;
37import com.sun.xml.internal.ws.spi.db.BindingContext;
38
39import javax.xml.ws.handler.LogicalHandler;
40import javax.xml.ws.handler.MessageContext;
41import javax.xml.ws.handler.Handler;
42import javax.xml.ws.WebServiceException;
43import java.util.List;
44import java.util.ArrayList;
45
46/**
47 *
48 * @author WS Development Team
49 */
50public class ClientLogicalHandlerTube extends HandlerTube {
51
52    private SEIModel seiModel;
53
54    /**
55     * Creates a new instance of LogicalHandlerTube
56     */
57    public ClientLogicalHandlerTube(WSBinding binding, SEIModel seiModel, WSDLPort port, Tube next) {
58        super(next, port, binding);
59        this.seiModel = seiModel;
60    }
61
62    /**
63     * This constructor is used on client-side where, SOAPHandlerTube is created
64     * first and then a LogicalHandlerTube is created with a handler to that
65     * SOAPHandlerTube.
66     * With this handle, LogicalHandlerTube can call
67     * SOAPHandlerTube.closeHandlers()
68     */
69    public ClientLogicalHandlerTube(WSBinding binding, SEIModel seiModel, Tube next, HandlerTube cousinTube) {
70        super(next, cousinTube, binding);
71        this.seiModel = seiModel;
72    }
73
74    /**
75     * Copy constructor for {@link com.sun.xml.internal.ws.api.pipe.Tube#copy(com.sun.xml.internal.ws.api.pipe.TubeCloner)}.
76     */
77
78    private ClientLogicalHandlerTube(ClientLogicalHandlerTube that, TubeCloner cloner) {
79        super(that, cloner);
80        this.seiModel = that.seiModel;
81    }
82
83    //should be overridden by DriverHandlerTubes
84    @Override
85    protected void initiateClosing(MessageContext mc) {
86      close(mc);
87      super.initiateClosing(mc);
88    }
89
90    public AbstractFilterTubeImpl copy(TubeCloner cloner) {
91        return new ClientLogicalHandlerTube(this, cloner);
92    }
93
94    void setUpProcessor() {
95        if (handlers == null) {
96                // Take a snapshot, User may change chain after invocation, Same chain
97                // should be used for the entire MEP
98                handlers = new ArrayList<Handler>();
99                WSBinding binding = getBinding();
100                List<LogicalHandler> logicalSnapShot= ((BindingImpl) binding).getHandlerConfig().getLogicalHandlers();
101                if (!logicalSnapShot.isEmpty()) {
102                    handlers.addAll(logicalSnapShot);
103                    if (binding.getSOAPVersion() == null) {
104                        processor = new XMLHandlerProcessor(this, binding,
105                                handlers);
106                    } else {
107                        processor = new SOAPHandlerProcessor(true, this, binding,
108                                handlers);
109                    }
110                }
111        }
112    }
113
114
115    MessageUpdatableContext getContext(Packet packet) {
116        return new LogicalMessageContextImpl(getBinding(), getBindingContext(), packet);
117    }
118
119    private BindingContext getBindingContext() {
120        return (seiModel!= null && seiModel instanceof AbstractSEIModelImpl) ?
121                ((AbstractSEIModelImpl)seiModel).getBindingContext() : null;
122        }
123
124        boolean callHandlersOnRequest(MessageUpdatableContext context, boolean isOneWay) {
125
126        boolean handlerResult;
127        try {
128
129            //CLIENT-SIDE
130            handlerResult = processor.callHandlersRequest(HandlerProcessor.Direction.OUTBOUND, context, !isOneWay);
131        } catch (WebServiceException wse) {
132            remedyActionTaken = true;
133            //no rewrapping
134            throw wse;
135        } catch (RuntimeException re) {
136            remedyActionTaken = true;
137
138            throw new WebServiceException(re);
139
140        }
141        if (!handlerResult) {
142            remedyActionTaken = true;
143        }
144        return handlerResult;
145    }
146
147    void callHandlersOnResponse(MessageUpdatableContext context, boolean handleFault) {
148        try {
149
150            //CLIENT-SIDE
151            processor.callHandlersResponse(HandlerProcessor.Direction.INBOUND, context, handleFault);
152
153        } catch (WebServiceException wse) {
154            //no rewrapping
155            throw wse;
156        } catch (RuntimeException re) {
157
158            throw new WebServiceException(re);
159
160        }
161    }
162    void closeHandlers(MessageContext mc) {
163        closeClientsideHandlers(mc);
164
165    }
166}
167