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 javax.xml.soap.Name;
19  import javax.xml.soap.SOAPEnvelope;
20  import javax.xml.soap.SOAPException;
21  import javax.xml.soap.SOAPHeader;
22  import javax.xml.soap.SOAPHeaderElement;
23  
24  import org.apache.axis.message.addressing.util.AddressingUtils;
25  import org.apache.axis.message.addressing.util.TextExtractor;
26  import org.apache.axis.types.URI;
27  import org.w3c.dom.Document;
28  import org.w3c.dom.Element;
29  
30  /**
31   * Java content class for AttributedURI complex type.
32   * <p>
33   * The following schema fragment specifies the expected content contained within
34   * this java content object. (defined at
35   * http://schemas.xmlsoap.org/ws/2004/08/addressing line 105)
36   *
37   * <pre>
38   * &lt;complexType name=&quot;AttributedURI&quot;&gt;
39   *   &lt;simpleContent&gt;
40   *     &lt;extension base=&quot;&lt;http://www.w3.org/2001/XMLSchema&gt;anyURI&quot;&gt;
41   *     &lt;/extension&gt;
42   *   &lt;/simpleContent&gt;
43   * &lt;/complexType&gt;
44   * </pre>
45   *
46   * @author Davanum Srinivas
47   * @author Rodrigo Ruiz
48   * @version $Revision: 14 $
49   */
50  public class AttributedURI extends URI
51    implements DOMAppendable, AddressingHeaderItem {
52  
53    /**
54     * <code>serialVersionUID</code> attribute.
55     */
56    private static final long serialVersionUID = -3363815864897359681L;
57  
58    /**
59     * Constructor AttributedURI.
60     */
61    public AttributedURI() {
62    }
63  
64    /**
65     * Constructor AttributedURI.
66     *
67     * @param el Element to extract the URI from
68     * @throws URI.MalformedURIException If the extracted URI is not valid
69     */
70    public AttributedURI(SOAPHeaderElement el) throws URI.MalformedURIException {
71      this(TextExtractor.getText(el).trim());
72    }
73  
74    /**
75     * Constructor Address.
76     *
77     * @param uri URI
78     * @throws MalformedURIException If the URI is not valid
79     */
80    public AttributedURI(String uri) throws MalformedURIException {
81      super(new URI(uri.trim()));
82    }
83  
84    /**
85     * Constructor AttributedURI.
86     *
87     * @param uri URI
88     */
89    public AttributedURI(URI uri) {
90      super(uri);
91    }
92  
93    /**
94     * Gets the default name of the element to create in DOMAppendable and
95     * AddressingHeaderItem methods.
96     *
97     * @return Element name
98     */
99    public String getDefaultElementName() {
100     return "";
101   }
102 
103   /**
104    * {@inheritDoc}
105    */
106   public final void append(Element parent) {
107     append(parent, getDefaultElementName());
108   }
109 
110   /**
111    * {@inheritDoc}
112    */
113   public final void append(Element parent, String elementName) {
114     append(AddressingUtils.getAddressingVersion(), parent, elementName);
115   }
116 
117   /**
118    * {@inheritDoc}
119    */
120   public final void append(AddressingVersion version, Element parent, String elemName) {
121     Document doc = parent.getOwnerDocument();
122     Element child = doc.createElementNS(version.getNamespace(), elemName);
123     child.appendChild(doc.createTextNode(this.toString()));
124     parent.appendChild(child);
125   }
126 
127   /**
128    * Converts this instance into a SOAP header element.
129    *
130    * @param version  WS-Addressing version to use
131    * @param env      Envelope to put the header into
132    * @param actorURI Actor URI
133    * @return Created element
134    * @throws SOAPException If an error occurs
135    */
136   public final SOAPHeaderElement toSOAPHeaderElement(AddressingVersion version,
137     SOAPEnvelope env, String actorURI) throws SOAPException {
138     return toSOAPHeaderElement(version, env, actorURI, getDefaultElementName());
139   }
140 
141   /**
142    * Converts this instance into a SOAP header element.
143    *
144    * @param version  WS-Addressing version to use
145    * @param env      Envelope to put the header into
146    * @param actorURI Actor URI
147    * @param name     Element name
148    * @return Created element
149    * @throws SOAPException If an error occurs
150    */
151   protected SOAPHeaderElement toSOAPHeaderElement(AddressingVersion version,
152     SOAPEnvelope env, String actorURI, String name) throws SOAPException {
153 
154     if (name == null) {
155       throw new IllegalArgumentException("A null name is not allowed.");
156     }
157     Name qname = env.createName(name, Constants.NS_PREFIX_ADDRESSING, version
158       .getNamespace());
159     SOAPHeader header = env.getHeader();
160     if (header == null) {
161       header = env.addHeader();
162     }
163 
164     // add the new header element
165     SOAPHeaderElement headerElement = header.addHeaderElement(qname);
166     headerElement.setActor(actorURI);
167     headerElement.addTextNode(this.toString());
168     return headerElement;
169   }
170 
171   /**
172    * {@inheritDoc}
173    */
174   public final SOAPHeaderElement toSOAPHeaderElement(SOAPEnvelope env, String actorURI)
175     throws SOAPException {
176     AddressingVersion version = AddressingUtils.getAddressingVersion();
177     return this.toSOAPHeaderElement(version, env, actorURI);
178   }
179 
180 }