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.model.wsdl;
27
28import com.sun.istack.internal.NotNull;
29import com.sun.xml.internal.ws.api.BindingID;
30import com.sun.xml.internal.ws.api.SOAPVersion;
31import com.sun.xml.internal.ws.api.model.ParameterBinding;
32import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation;
33import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundPortType;
34import com.sun.xml.internal.ws.api.model.wsdl.editable.EditableWSDLBoundOperation;
35import com.sun.xml.internal.ws.api.model.wsdl.editable.EditableWSDLBoundPortType;
36import com.sun.xml.internal.ws.api.model.wsdl.editable.EditableWSDLModel;
37import com.sun.xml.internal.ws.api.model.wsdl.editable.EditableWSDLPortType;
38import com.sun.xml.internal.ws.resources.ClientMessages;
39import com.sun.xml.internal.ws.util.QNameMap;
40import com.sun.xml.internal.ws.util.exception.LocatableWebServiceException;
41
42import javax.jws.WebParam.Mode;
43import javax.jws.soap.SOAPBinding;
44import javax.jws.soap.SOAPBinding.Style;
45import javax.xml.namespace.QName;
46import javax.xml.stream.XMLStreamReader;
47import javax.xml.ws.soap.MTOMFeature;
48
49/**
50 * Implementation of {@link WSDLBoundPortType}
51 *
52 * @author Vivek Pandey
53 */
54public final class WSDLBoundPortTypeImpl extends AbstractFeaturedObjectImpl implements EditableWSDLBoundPortType {
55    private final QName name;
56    private final QName portTypeName;
57    private EditableWSDLPortType portType;
58    private BindingID bindingId;
59    private final @NotNull EditableWSDLModel owner;
60    private final QNameMap<EditableWSDLBoundOperation> bindingOperations = new QNameMap<EditableWSDLBoundOperation>();
61
62    /**
63     * Operations keyed by the payload tag name.
64     */
65    private QNameMap<EditableWSDLBoundOperation> payloadMap;
66    /**
67     * {@link #payloadMap} doesn't allow null key, so we store the value for it here.
68     */
69    private EditableWSDLBoundOperation emptyPayloadOperation;
70
71
72
73    public WSDLBoundPortTypeImpl(XMLStreamReader xsr,@NotNull EditableWSDLModel owner, QName name, QName portTypeName) {
74        super(xsr);
75        this.owner = owner;
76        this.name = name;
77        this.portTypeName = portTypeName;
78        owner.addBinding(this);
79    }
80
81    public QName getName() {
82        return name;
83    }
84
85    public @NotNull EditableWSDLModel getOwner() {
86        return owner;
87    }
88
89    public EditableWSDLBoundOperation get(QName operationName) {
90        return bindingOperations.get(operationName);
91    }
92
93    /**
94     * Populates the Map that holds operation name as key and {@link WSDLBoundOperation} as the value.
95     *
96     * @param opName Must be non-null
97     * @param ptOp   Must be non-null
98     * @throws NullPointerException if either opName or ptOp is null
99     */
100    public void put(QName opName, EditableWSDLBoundOperation ptOp) {
101        bindingOperations.put(opName,ptOp);
102    }
103
104    public QName getPortTypeName() {
105        return portTypeName;
106    }
107
108    public EditableWSDLPortType getPortType() {
109        return portType;
110    }
111
112    public Iterable<EditableWSDLBoundOperation> getBindingOperations() {
113        return bindingOperations.values();
114    }
115
116    public BindingID getBindingId() {
117        //Should the default be SOAP1.1/HTTP binding? For now lets keep it for
118        //JBI bug 6509800
119        return (bindingId==null)?BindingID.SOAP11_HTTP:bindingId;
120    }
121
122    public void setBindingId(BindingID bindingId) {
123        this.bindingId = bindingId;
124    }
125
126    /**
127     * sets whether the {@link WSDLBoundPortType} is rpc or lit
128     */
129    private Style style = Style.DOCUMENT;
130    public void setStyle(Style style){
131        this.style = style;
132    }
133
134    public SOAPBinding.Style getStyle() {
135        return style;
136    }
137
138    public boolean isRpcLit(){
139        return Style.RPC==style;
140    }
141
142    public boolean isDoclit(){
143        return Style.DOCUMENT==style;
144    }
145
146
147    /**
148     * Gets the {@link ParameterBinding} for a given operation, part name and the direction - IN/OUT
149     *
150     * @param operation wsdl:operation@name value. Must be non-null.
151     * @param part      wsdl:part@name such as value of soap:header@part. Must be non-null.
152     * @param mode      {@link Mode#IN} or {@link Mode#OUT}. Must be non-null.
153     * @return null if the binding could not be resolved for the part.
154     */
155    public ParameterBinding getBinding(QName operation, String part, Mode mode) {
156        EditableWSDLBoundOperation op = get(operation);
157        if (op == null) {
158            //TODO throw exception
159            return null;
160        }
161        if ((Mode.IN == mode) || (Mode.INOUT == mode))
162            return op.getInputBinding(part);
163        else
164            return op.getOutputBinding(part);
165    }
166
167    public EditableWSDLBoundOperation getOperation(String namespaceUri, String localName) {
168        if(namespaceUri==null && localName == null)
169            return emptyPayloadOperation;
170        else{
171            return payloadMap.get((namespaceUri==null)?"":namespaceUri,localName);
172        }
173    }
174
175    public void freeze() {
176        portType = owner.getPortType(portTypeName);
177        if(portType == null){
178            throw new LocatableWebServiceException(
179                    ClientMessages.UNDEFINED_PORT_TYPE(portTypeName), getLocation());
180        }
181        portType.freeze();
182
183        for (EditableWSDLBoundOperation op : bindingOperations.values()) {
184            op.freeze(owner);
185        }
186
187        freezePayloadMap();
188        owner.finalizeRpcLitBinding(this);
189    }
190
191    private void freezePayloadMap() {
192        if(style== Style.RPC) {
193            payloadMap = new QNameMap<EditableWSDLBoundOperation>();
194            for(EditableWSDLBoundOperation op : bindingOperations.values()){
195                payloadMap.put(op.getRequestPayloadName(), op);
196            }
197        } else {
198            payloadMap = new QNameMap<EditableWSDLBoundOperation>();
199            // For doclit The tag will be the operation that has the same input part descriptor value
200            for(EditableWSDLBoundOperation op : bindingOperations.values()){
201                QName name = op.getRequestPayloadName();
202                if(name == null){
203                    //empty payload
204                    emptyPayloadOperation = op;
205                    continue;
206                }
207
208                payloadMap.put(name, op);
209            }
210        }
211    }
212}
213