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.namespace.QName;
19  import javax.xml.soap.Name;
20  import javax.xml.soap.SOAPElement;
21  import javax.xml.soap.SOAPEnvelope;
22  import javax.xml.soap.SOAPException;
23  import javax.xml.soap.SOAPHeader;
24  
25  import org.apache.axis.message.SOAPHeaderElement;
26  import org.apache.axis.message.addressing.util.AddressingUtils;
27  import org.apache.axis.message.addressing.util.TextExtractor;
28  import org.apache.axis.types.URI;
29  import org.apache.axis.types.URI.MalformedURIException;
30  
31  /**
32   * Java content class for RelatesTo element declaration.
33   * <p>
34   * The following schema fragment specifies the expected content contained within
35   * this java content object. (defined at
36   * http://schemas.xmlsoap.org/ws/2004/08/addressing line 77)
37   * <p>
38   *
39   * <pre>
40   * &lt;element
41   *   name=&quot;RelatesTo&quot;
42   *   type=&quot;{http://schemas.xmlsoap.org/ws/2004/08/addressing}Relationship&quot;/&gt;
43   * </pre>
44   *
45   * @author Davanum Srinivas
46   * @version $Revision: 14 $
47   */
48  public class RelatesTo extends Relationship implements AddressingHeaderItem {
49  
50    /**
51     * Constructor RelatesTo.
52     *
53     * @param element Source element
54     * @throws MalformedURIException If an error occurs
55     */
56    public RelatesTo(SOAPElement element) throws MalformedURIException {
57      super();
58      AddressingVersion version = AddressingUtils.getAddressingVersion(
59        element.getNamespaceURI());
60  
61      this.setURI(new URI(TextExtractor.getText(element)));
62      String value = element.getAttribute(Constants.RELATIONSHIP_TYPE);
63      if (value != null && value.length() > 0) {
64        if (version.isW3C()) {
65          this.setTypeIRI(value);
66        } else {
67          int pos = value.indexOf(':');
68          if (pos == -1) {
69            this.setType(new QName(value));
70          } else {
71            String namespace = element.getNamespaceURI(value.substring(0, pos));
72            this.setType(new QName(namespace, value.substring(pos + 1)));
73          }
74        }
75      } else {
76        this.setType(version.getResponseRelationshipType());
77      }
78    }
79  
80    /**
81     * Creates an instance.
82     *
83     * @param uri  Target URI
84     * @param type URI type
85     * @throws URI.MalformedURIException If the URI is not valid
86     */
87    public RelatesTo(String uri, QName type) throws URI.MalformedURIException {
88      super(new URI(uri), type);
89    }
90  
91    /**
92     * Creates an instance.
93     *
94     * @param uri  Target URI
95     * @param type Type attribute
96     * @throws URI.MalformedURIException If the URI is not valid
97     */
98    public RelatesTo(String uri, String type) throws URI.MalformedURIException {
99      super(uri, type);
100   }
101 
102   /**
103    * Creates an instance.
104    *
105    * @param uri  Target URI
106    * @param type Type attribute
107    */
108   public RelatesTo(URI uri, QName type) {
109     super(uri, type);
110   }
111 
112   /**
113    * {@inheritDoc}
114    */
115   public SOAPHeaderElement toSOAPHeaderElement(SOAPEnvelope env, String actorURI)
116     throws SOAPException {
117     AddressingVersion version = AddressingUtils.getAddressingVersion();
118     return toSOAPHeaderElement(version, env, actorURI);
119   }
120 
121   /**
122    * {@inheritDoc}
123    */
124   public SOAPHeaderElement toSOAPHeaderElement(AddressingVersion version,
125     SOAPEnvelope env, String actorURI) throws SOAPException {
126     Name nm = env.createName(Constants.RELATES_TO, Constants.NS_PREFIX_ADDRESSING,
127       version.getNamespace());
128     SOAPHeader header = env.getHeader();
129     if (header == null) {
130       header = env.addHeader();
131     }
132 
133     SOAPHeaderElement headerElement = (SOAPHeaderElement) header.addHeaderElement(nm);
134     headerElement.setActor(actorURI);
135 
136     String typeIRI = this.getTypeIRI();
137     QName type = this.getType();
138 
139     if (typeIRI != null) {
140       headerElement.addAttribute("", Constants.RELATIONSHIP_TYPE, typeIRI);
141     } else if (type != null) {
142       headerElement.addAttribute("", Constants.RELATIONSHIP_TYPE, type);
143     }
144 
145     headerElement.addTextNode(this.getURI().toString());
146 
147     return headerElement;
148   }
149 
150 }