View Javadoc

1   /*
2    *  Copyright (c) 2008 Rodrigo Ruiz
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.apache.axis.message.addressing;
17  
18  import java.io.Serializable;
19  
20  import javax.xml.namespace.QName;
21  
22  import org.apache.axis.description.ElementDesc;
23  import org.apache.axis.description.TypeDesc;
24  import org.apache.axis.encoding.AnyContentType;
25  import org.apache.axis.encoding.Deserializer;
26  import org.apache.axis.encoding.Serializer;
27  import org.apache.axis.encoding.ser.BeanDeserializer;
28  import org.apache.axis.encoding.ser.BeanSerializer;
29  import org.apache.axis.message.MessageElement;
30  import org.apache.axis.types.URI;
31  
32  /**
33   * Java content class for EndpointReferenceType complex type.
34   * <p>
35   * The following schema fragment specifies the expected content contained within
36   * this java content object. (defined at
37   * http://schemas.xmlsoap.org/ws/2004/08/addressing line 46)
38   * <p>
39   *
40   * <pre>
41   * &lt;complexType name=&quot;EndpointReferenceType&quot;
42   *   xmlns:wsa=&quot;http://schemas.xmlsoap.org/ws/2004/08/addressing&quot;
43   *   xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
44   *   &lt;complexContent&gt;
45   *     &lt;restriction base=&quot;xsd:anyType&quot;&gt;
46   *       &lt;sequence&gt;
47   *         &lt;element name=&quot;Address&quot; type=&quot;wsa:AttributedURI&quot;/&gt;
48   *         &lt;element name=&quot;ReferenceProperties&quot;
49   *            type=&quot;wsa:ReferencePropertiesType&quot;
50   *            minOccurs=&quot;0&quot;/&gt;
51   *         &lt;element name=&quot;ReferenceParameters&quot;
52   *            type=&quot;wsa:ReferenceParametersType&quot;
53   *            minOccurs=&quot;0&quot;/&gt;
54   *         &lt;element name=&quot;PortType&quot;
55   *            type=&quot;wsa:AttributedQName&quot;
56   *            minOccurs=&quot;0&quot;/&gt;
57   *         &lt;element name=&quot;ServiceName&quot;
58   *            type=&quot;wsa:ServiceNameType&quot;
59   *            minOccurs=&quot;0&quot;/&gt;
60   *         &lt;any/&gt;
61   *       &lt;/sequence&gt;
62   *     &lt;/restriction&gt;
63   *   &lt;/complexContent&gt;
64   * &lt;/complexType&gt;
65   * </pre>
66   *
67   * @author Davanum Srinivas
68   * @author Rodrigo Ruiz
69   * @version $Revision: 14 $
70   */
71  public class EndpointReferenceType implements AnyContentType, Serializable {
72    /**
73     * <code>serialVersionUID</code> attribute.
74     */
75    private static final long serialVersionUID = 16342863069452848L;
76  
77    /**
78     * Type meta-data.
79     */
80    private static final TypeDesc TYPE_DESC = new TypeDesc(EndpointReferenceType.class,
81      true);
82  
83    static {
84      String ns = Constants.NS_URI_ADDRESSING_DEFAULT;
85      TYPE_DESC.setXmlType(new QName(ns, "EndpointReferenceType"));
86  
87      ElementDesc elemField = new ElementDesc();
88      elemField.setFieldName("address");
89      elemField.setXmlName(new QName(ns, "Address"));
90      elemField.setXmlType(new QName(ns, "Address"));
91      TYPE_DESC.addFieldDesc(elemField);
92      elemField = new ElementDesc();
93      elemField.setFieldName("properties");
94      elemField.setXmlName(new QName(ns, "ReferenceProperties"));
95      elemField.setXmlType(new QName(ns, "ReferencePropertiesType"));
96      elemField.setMinOccurs(0);
97      TYPE_DESC.addFieldDesc(elemField);
98      elemField = new ElementDesc();
99      elemField.setFieldName("parameters");
100     elemField.setXmlName(new QName(ns, "ReferenceParameters"));
101     elemField.setXmlType(new QName(ns, "ReferenceParametersType"));
102     elemField.setMinOccurs(0);
103     TYPE_DESC.addFieldDesc(elemField);
104     elemField = new ElementDesc();
105     elemField.setFieldName("portType");
106     elemField.setXmlName(new QName(ns, "PortType"));
107     elemField.setXmlType(new QName(ns, "PortType"));
108     elemField.setMinOccurs(0);
109     TYPE_DESC.addFieldDesc(elemField);
110     elemField = new ElementDesc();
111     elemField.setFieldName("serviceName");
112     elemField.setXmlName(new QName(ns, "ServiceName"));
113     elemField.setXmlType(new QName(ns, "ServiceNameType"));
114     elemField.setMinOccurs(0);
115     TYPE_DESC.addFieldDesc(elemField);
116     elemField = new ElementDesc();
117     elemField.setFieldName("metadata");
118     elemField.setXmlName(new QName(ns, "Metadata"));
119     elemField.setXmlType(new QName(ns, "MetadataType"));
120     elemField.setMinOccurs(0);
121     TYPE_DESC.addFieldDesc(elemField);
122   }
123 
124   /**
125    * Gets a custom Deserializer.
126    *
127    * @param mType not used
128    * @param jType Java type
129    * @param xType XML type
130    * @return A Deserializer instance
131    */
132   public static Deserializer getDeserializer(String mType, Class<?> jType, QName xType) {
133     return new BeanDeserializer(jType, xType, TYPE_DESC);
134   }
135 
136   /**
137    * Gets a custom Serializer.
138    *
139    * @param mType not used
140    * @param jType Java type
141    * @param xType XML type
142    * @return A Serializer instance
143    */
144   public static Serializer getSerializer(String mType, Class<?> jType, QName xType) {
145     return new BeanSerializer(jType, xType, TYPE_DESC);
146   }
147 
148   /**
149    * Return the type description object.
150    *
151    * @return Type type description for this class
152    */
153   public static TypeDesc getTypeDesc() {
154     return TYPE_DESC;
155   }
156 
157   /**
158    * Field address.
159    */
160   private Address address;
161 
162   /**
163    * Field portType.
164    */
165   private PortType portType;
166 
167   /**
168    * Field serviceName.
169    */
170   private ServiceNameType serviceName;
171 
172   /**
173    * Field properties.
174    */
175   private ReferencePropertiesType properties;
176 
177   /**
178    * Field parameters.
179    */
180   private ReferenceParametersType parameters;
181 
182   /**
183    * Meta-data (for W3C version only).
184    */
185   protected MetaDataType metaData;
186 
187   /**
188    * Field extensibility elements.
189    */
190   private MessageElement[] any;
191 
192   /**
193    * Constructor EndpointReferenceType.
194    */
195   public EndpointReferenceType() {
196   }
197 
198   /**
199    * Constructor EndpointReferenceType.
200    *
201    * @param address EndPoint URI
202    */
203   public EndpointReferenceType(Address address) {
204     this.address = address;
205   }
206 
207   /**
208    * Constructor EndpointReferenceType (performs shallow copy).
209    *
210    * @param endpoint Instance to copy
211    */
212   public EndpointReferenceType(EndpointReferenceType endpoint) {
213     this(endpoint, false);
214   }
215 
216   /**
217    * Constructor EndpointReferenceType.
218    *
219    * @param endpoint Instance to copy
220    * @param deepCopy Whether to perform a deep copy or not
221    */
222   public EndpointReferenceType(EndpointReferenceType endpoint, boolean deepCopy) {
223     if (deepCopy) {
224       this.deepCopy(endpoint);
225     } else {
226       this.shallowCopy(endpoint);
227     }
228   }
229 
230   /**
231    * Constructor EndpointReferenceType.
232    *
233    * @param addr Address URI
234    */
235   public EndpointReferenceType(URI addr) {
236     this.address = new Address(addr);
237   }
238 
239   /**
240    * Performs a copy of the specified elements.
241    *
242    * @param elems Element array to copy
243    * @param deep  Whether to perform a deep or shallow copy
244    * @return The copy
245    */
246   private MessageElement[] copy(MessageElement[] elems, boolean deep) {
247     if (elems == null || elems.length == 0) {
248       return new MessageElement[0];
249     } else if (deep) {
250       MessageElement[] copy = new MessageElement[elems.length];
251       for (int i = 0; i < elems.length; i++) {
252         MessageElement elem = elems[i];
253         try {
254           copy[i] = new MessageElement(elem.getAsDOM());
255         } catch (Exception e) {
256           // if failed just copy reference
257           copy[i] = elem;
258         }
259       }
260       return copy;
261     } else {
262       MessageElement[] copy = new MessageElement[elems.length];
263       System.arraycopy(elems, 0, copy, 0, elems.length);
264       return copy;
265     }
266   }
267 
268   /**
269    * Performs a deep copy of the specified instance.
270    *
271    * @param endpoint Instance to copy
272    */
273   private void deepCopy(EndpointReferenceType endpoint) {
274     AttributedURI addr = endpoint.getAddress();
275     if (addr != null) {
276       this.setAddress(new Address(addr));
277     }
278     AttributedQName port = endpoint.getPortType();
279     if (port != null) {
280       this.setPortType(new PortType(port));
281     }
282     ServiceNameType sName = endpoint.getServiceName();
283     if (sName != null) {
284       this.setServiceName(new ServiceNameType(sName));
285     }
286     ReferencePropertiesType props = endpoint.getProperties();
287     if (props != null) {
288       this.setProperties(new ReferencePropertiesType(props, true));
289     }
290     ReferenceParametersType params = endpoint.getParameters();
291     if (params != null) {
292       this.setParameters(params);
293     }
294     MetaDataType metadata = endpoint.getMetadata();
295     if (metadata != null) {
296       this.setMetadata(metadata);
297     }
298 
299     // handle extensibility
300     MessageElement[] elems = endpoint.get_any();
301     this.set_any(copy(elems, true));
302   }
303 
304   /**
305    * {@inheritDoc}
306    */
307   // CHECKSTYLE:OFF (get_any cannot be renamed, as it is part of the Axis API)
308   public MessageElement[] get_any() {
309   // CHECKSTYLE:ON
310     return (this.any == null) ? null : this.any.clone();
311   }
312 
313   /**
314    * Gets the remote address URI.
315    *
316    * @return the remote address
317    */
318   public AttributedURI getAddress() {
319     return this.address;
320   }
321 
322   /**
323    * Get the meta-data (if any).
324    *
325    * @return a List of MessageElements, or null
326    */
327   public MetaDataType getMetadata() {
328     return this.metaData;
329   }
330 
331   /**
332    * Method getParameters.
333    *
334    * @return The reference parameters field
335    */
336   public ReferenceParametersType getParameters() {
337     if (this.parameters == null) {
338       this.parameters = new ReferenceParametersType();
339     }
340     return this.parameters;
341   }
342 
343   /**
344    * Method getPortType.
345    *
346    * @return The port type name
347    */
348   public AttributedQName getPortType() {
349     return this.portType;
350   }
351 
352   /**
353    * Method getProperties.
354    *
355    * @return The reference properties field
356    */
357   public ReferencePropertiesType getProperties() {
358     if (this.properties == null) {
359       this.properties = new ReferencePropertiesType();
360     }
361     return this.properties;
362   }
363 
364   /**
365    * Method getServiceName.
366    *
367    * @return The service name
368    */
369   public ServiceNameType getServiceName() {
370     return this.serviceName;
371   }
372 
373   /**
374    * {@inheritDoc}
375    */
376   // CHECKSTYLE:OFF (set_any cannot be renamed, as it is part of the Axis API)
377   public void set_any(MessageElement[] any) {
378   // CHECKSTYLE:ON
379     this.any = (any == null) ? null : any.clone();
380   }
381 
382   /**
383    * Method setAddress.
384    *
385    * @param addr The new address
386    */
387   public void setAddress(AttributedURI addr) {
388     if (addr instanceof Address) {
389       this.address = (Address)addr;
390     } else {
391       this.address = (addr == null) ? null : new Address(addr);
392     }
393   }
394 
395   /**
396    * Set the meta-data.
397    *
398    * @param metaData a List of MessageElements
399    */
400   public void setMetadata(MetaDataType metaData) {
401     this.metaData = metaData;
402   }
403 
404   /**
405    * Method setParameters.
406    *
407    * @param parameters The reference parameters to set
408    */
409   public void setParameters(ReferenceParametersType parameters) {
410     this.parameters = parameters;
411   }
412 
413   /**
414    * Method setPortType.
415    *
416    * @param portType The service port type name
417    */
418   public void setPortType(AttributedQName portType) {
419     if (portType instanceof PortType) {
420       this.portType = (PortType)portType;
421     } else {
422       this.portType = (portType == null) ? null : new PortType(portType);
423     }
424   }
425 
426   /**
427    * Method setProperties.
428    *
429    * @param properties The reference properties field
430    */
431   public void setProperties(ReferencePropertiesType properties) {
432     this.properties = properties;
433   }
434 
435   /**
436    * Method setServiceName.
437    *
438    * @param serviceName The service name
439    */
440   public void setServiceName(ServiceNameType serviceName) {
441     this.serviceName = serviceName;
442   }
443 
444   /**
445    * Performs a shallow copy.
446    *
447    * @param endpoint Instance to copy
448    */
449   private void shallowCopy(EndpointReferenceType endpoint) {
450     this.setAddress(endpoint.getAddress());
451     this.setPortType(endpoint.getPortType());
452     this.setServiceName(endpoint.getServiceName());
453     ReferencePropertiesType props = endpoint.getProperties();
454     if (props != null) {
455       this.setProperties(new ReferencePropertiesType(props, false));
456     }
457     ReferenceParametersType params = endpoint.getParameters();
458     if (params != null) {
459       this.setParameters(new ReferenceParametersType(params, false));
460     }
461     MetaDataType metadata = endpoint.getMetadata();
462     if (metadata != null) {
463       this.setMetadata(new MetaDataType(metadata, false));
464     }
465 
466     // handle extensibility
467     MessageElement[] elems = endpoint.get_any();
468     this.set_any(elems);
469   }
470 
471   /**
472    * {@inheritDoc}
473    */
474   @Override
475   public String toString() {
476     StringBuffer buf = new StringBuffer();
477     if (this.address != null) {
478       buf.append("Address: ").append(this.address);
479       buf.append("\n");
480     }
481     if (this.portType != null) {
482       buf.append("Port Type: ").append(this.portType);
483       buf.append("\n");
484     }
485     if (this.serviceName != null) {
486       buf.append("Service Name: ").append(this.serviceName);
487       buf.append("\n");
488       if (this.serviceName.getPort() != null) {
489         buf.append("Port Name: ").append(this.serviceName.getPort());
490         buf.append("\n");
491       }
492     }
493     if (this.properties != null) {
494       buf.append(this.properties);
495       buf.append("\n");
496     }
497     if (this.parameters != null) {
498       buf.append(this.parameters);
499       buf.append("\n");
500     }
501     if (this.metaData != null) {
502       buf.append(this.metaData);
503       buf.append("\n");
504     }
505 
506     if (this.any != null) {
507       for (int i = 0; i < this.any.length; i++) {
508         buf.append("Extensibility Element[").append(i).append("]:\n");
509         try {
510           buf.append(this.any[i].getAsString());
511         } catch (Exception e) {
512           buf.append("<error converting: ").append(e.getMessage()).append(">");
513         }
514         buf.append("\n");
515       }
516     }
517     return buf.toString();
518   }
519 }