summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060522/java/sca/bindings/binding.axis2/src/main/java/org/apache/tuscany/binding/axis2/util/WebServicePortMetaData.java
blob: 694a8fbf5fe7eab08ab019c61f5e0c5668efaf6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.binding.axis2.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.wsdl.Binding;
import javax.wsdl.BindingOperation;
import javax.wsdl.Definition;
import javax.wsdl.Operation;
import javax.wsdl.Port;
import javax.wsdl.PortType;
import javax.wsdl.Service;
import javax.wsdl.extensions.soap.SOAPAddress;
import javax.wsdl.extensions.soap.SOAPBinding;
import javax.xml.namespace.QName;

import org.apache.tuscany.model.types.wsdl.WSDLServiceContract;

/**
 * Metadata for a WSDL port
 * 
 */
public class WebServicePortMetaData {

    private Service wsdlService;

    private QName wsdlServiceName;

    private Port wsdlPort;

    private Binding wsdlBinding;

    private QName wsdlPortName;

    private PortType wsdlPortType;

    private QName wsdlPortTypeName;

    private String endpoint;

    private boolean managed;

    private List<WebServiceOperationMetaData> allOperationMetaData;

    private WSDLServiceContract interfaceType;

    /**
     * Constructor
     * 
     * @param wsdlDefinition
     * @param portName
     */
    public WebServicePortMetaData(Definition wsdlDefinition, Port wsdlPort, String endpoint, boolean managed) {

        // Lookup the named port
        this.wsdlPort = wsdlPort;
        wsdlPortName = new QName(wsdlDefinition.getTargetNamespace(), wsdlPort.getName());

        Collection services = wsdlDefinition.getServices().values();
        for (Object serviceObj : services) {
            Service service = (Service) serviceObj;
            if (service.getPorts().containsValue(wsdlPort)) {
                wsdlService = service;
                wsdlServiceName = service.getQName();
                break;
            }
        }

        // Save the binding
        wsdlBinding = wsdlPort.getBinding();
        if (wsdlBinding == null) {
            throw new IllegalArgumentException("WSDL binding cannot be found for " + wsdlPortName);
        }

        // Save the portType
        wsdlPortType = wsdlBinding.getPortType();
        if (wsdlPortType == null) {
            throw new IllegalArgumentException("WSDL portType cannot be found for " + wsdlPortName);
        }
        wsdlPortTypeName = wsdlPortType.getQName();

        // Save the endpoint
        this.endpoint = endpoint;

        // Track if this endpoint is managed or not
        this.managed = managed;
    }

    /**
     * Constructor
     * 
     * @param serviceName
     * @param portName
     * @param portType
     */
    public WebServicePortMetaData(QName serviceName, String portName, QName portTypeName, String endpoint) {
        wsdlServiceName = serviceName;
        wsdlPortName = new QName(serviceName.getNamespaceURI(), portName);
        wsdlPortTypeName = portTypeName;
        this.endpoint = endpoint;
    }

    /**
     * @return Returns the wsdlPort.
     */
    public javax.wsdl.Port getPort() {
        return wsdlPort;
    }

    /**
     * @return Returns the wsdlService.
     */
    public QName getServiceName() {
        return wsdlServiceName;
    }

    /**
     * @return Returns the wsdlService.
     */
    public javax.wsdl.Service getService() {
        return wsdlService;
    }

    /**
     * @return Returns the wsdlPortType.
     */
    public PortType getPortType() {
        return wsdlPortType;
    }

    /**
     * @return Returns the wsdlPortType.
     */
    public QName getPortTypeName() {
        return wsdlPortTypeName;
    }

    /**
     * @return Returns the wsdlBinding.
     */
    public Binding getBinding() {
        return wsdlBinding;
    }

    /**
     * @return Returns the wsdlPortName.
     */
    public QName getPortName() {
        return wsdlPortName;
    }

    /**
     * Returns the endpoint of a given port.
     * 
     * @param wsdlPort
     * @return
     */
    public String getEndpoint() {

        // Return the specified endpoint
        if (endpoint != null) {
            return endpoint;
        }

        // Find the target endpoint on the port
        if (wsdlPort != null) {
            final List wsdlPortExtensions = wsdlPort.getExtensibilityElements();
            for (Iterator i = wsdlPortExtensions.iterator(); i.hasNext();) {
                final Object extension = i.next();
                if (extension instanceof SOAPAddress) {
                    return ((SOAPAddress) extension).getLocationURI();
                }
            }
        }

        return null;
    }

    /**
     * Returns the SOAP binding style.
     * 
     * @return
     */
    public String getStyle() {

        // Find the binding style
        String style = null;
        if (wsdlBinding != null) {
            final List wsdlBindingExtensions = wsdlBinding.getExtensibilityElements();
            SOAPBinding soapBinding = getExtensibilityElement(wsdlBindingExtensions, SOAPBinding.class);
            if (soapBinding != null) {
                style = soapBinding.getStyle();
            }
        }

        // Default to document
        return (style == null) ? "document" : style;
    }

    /**
     * Returns the use attribute
     * 
     * @return
     */
    public String getUse() {
        List<WebServiceOperationMetaData> list = getAllOperationMetaData();
        return list.get(0).getUse();
    }

    /**
     * Returns the encoding attribute
     * 
     * @return
     */
    public String getEncoding() {
        List<WebServiceOperationMetaData> list = getAllOperationMetaData();
        return list.get(0).getEncoding();
    }

    /**
     * @return Returns true if this is a managed web service.
     */
    public boolean isManaged() {
        return managed;
    }

    /**
     * Returns the first extensibility element of the given type.
     * 
     * @param elements
     * @param type
     * @return
     */
    public static <T> T getExtensibilityElement(List elements, Class<T> type) {
        for (Iterator i = elements.iterator(); i.hasNext();) {
            Object element = i.next();
            if (type.isInstance(element)) {
                return type.cast(element);
            }
        }
        return null;
    }

    /**
     * Returns the extensibility elements of the given type.
     * 
     * @param elements
     * @param type
     * @return
     */
    public static <T> List<T> getExtensibilityElements(List elements, Class<T> type) {
        List<T> result = new ArrayList<T>();
        for (Iterator i = elements.iterator(); i.hasNext();) {
            Object element = i.next();
            if (type.isInstance(element)) {
                result.add(type.cast(element));
            }
        }
        return result;
    }

    /**
     * Get the operation signature from the SOAP Body
     * 
     * @param body
     * @return A list of QNames
     */
    // public static List getOperationSignature(javax.xml.soap.SOAPBody body) {
    // List signature = new ArrayList();
    // for (Iterator i = body.getChildElements(); i.hasNext();) {
    // Object child = i.next();
    // if (child instanceof SOAPBodyElement) {
    // Name name = ((SOAPBodyElement) child).getElementName();
    // QName qname = new QName(name.getURI(), name.getLocalName(), name.getPrefix());
    // signature.add(qname);
    // }
    // }
    // return signature;
    // }
    // public static List getRPCOperationSignature(javax.xml.soap.SOAPBody body) {
    // List signature = new ArrayList();
    // for (Iterator i = body.getChildElements(); i.hasNext();) {
    // Object child = i.next();
    // if (child instanceof SOAPBodyElement) {
    // SOAPBodyElement op = ((SOAPBodyElement) child);
    // for (Iterator j = op.getChildElements(); j.hasNext();) {
    // Object part = i.next();
    // if (part instanceof SOAPElement) {
    // SOAPElement p = (SOAPElement) part;
    // signature.add(p.getLocalName());
    // }
    // }
    // }
    // }
    // return signature;
    // }
    // public WebServiceOperationMetaData getOperationMetaData(javax.xml.soap.SOAPBody body) {
    // List s1 = getOperationSignature(body);
    // // List rpcParts = getRPCOperationSignature(body);
    // for (Iterator it = getAllOperationMetaData().iterator(); it.hasNext();) {
    // WebServiceOperationMetaData descriptor = (WebServiceOperationMetaData) it.next();
    //
    // String style = descriptor.getStyle();
    //
    // if (style.equals("document")) {
    // List s2 = descriptor.getOperationSignature();
    // if (s1.equals(s2))
    // return descriptor;
    // } else {
    // QName op1 = (QName) s1.get(0);
    // QName op2 = descriptor.getRPCOperationName();
    // if (op1.equals(op2)) {
    // /*
    // * // FIXME: [rfeng] We don't support method overloading
    // * List partNames = getOperationSignature(binding,
    // * bindingOperation); if (rpcParts.equals(partNames))
    // */
    // return descriptor;
    // }
    // }
    // }
    // return null;
    // }
    public List<WebServiceOperationMetaData> getAllOperationMetaData() {
        if (allOperationMetaData == null) {
            allOperationMetaData = new ArrayList<WebServiceOperationMetaData>();
            for (Iterator it = wsdlBinding.getBindingOperations().iterator(); it.hasNext();) {
                final BindingOperation bindingOperation = (BindingOperation) it.next();
                if (bindingOperation.getOperation() != null) {
                    allOperationMetaData.add(new WebServiceOperationMetaData(wsdlBinding, bindingOperation));
                }
            }
        }
        return allOperationMetaData;
    }

    public WebServiceOperationMetaData getOperationMetaData(String operationName) {
        StringBuilder sb = new StringBuilder(operationName);
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        String capatalizedOpName = sb.toString();
        
        for (Iterator it = getAllOperationMetaData().iterator(); it.hasNext();) {
            WebServiceOperationMetaData descriptor = (WebServiceOperationMetaData) it.next();
            String opName = descriptor.getBindingOperation().getOperation().getName();

            if (opName.equals(operationName) || opName.equals(capatalizedOpName)) {
                return descriptor;
            }
        }
        return null;
    }

    /**
     * Returns the WSDL service contract
     * 
     * @return
     */
    public WSDLServiceContract getInterfaceType() {
        return interfaceType;
    }

    /**
     * Get the WSDL operation name for a Java method name
     */
    public String getWSDLOperationName(String methodName) {
        StringBuilder sb = new StringBuilder(methodName);
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        String capatalizedOpName = sb.toString();
        for (Object o : wsdlPortType.getOperations()) {
            Operation operation = (Operation) o;
            String wsdlOpName = operation.getName();
            if (wsdlOpName.equals(methodName)) {
                return wsdlOpName;
            }
            if (wsdlOpName.equals(capatalizedOpName)) {
                return wsdlOpName;
            }
        }
        return null;
    }

}