View Javadoc

1   /*
2    * Copyright  1999-2004 The Apache Software Foundation.
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   */
17  
18  package org.apache.axis.message.addressing;
19  
20  import org.apache.axis.message.addressing.util.TextExtractor;
21  
22  import org.apache.axis.types.URI;
23  import org.apache.axis.utils.XMLUtils;
24  import org.apache.axis.message.MessageElement;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import org.w3c.dom.Document;
30  import org.w3c.dom.Element;
31  import org.w3c.dom.Node;
32  import org.w3c.dom.NodeList;
33  
34  import java.util.Iterator;
35  
36  import javax.xml.namespace.QName;
37  import javax.xml.soap.SOAPElement;
38  import javax.xml.soap.SOAPEnvelope;
39  import javax.xml.soap.SOAPHeaderElement;
40  import javax.xml.soap.SOAPHeader;
41  import javax.xml.parsers.ParserConfigurationException;
42  
43  /***
44   * Java content class for EndpointReference element declaration.
45   * <p>The following schema fragment specifies the expected content contained within this java content object. (defined at http://schemas.xmlsoap.org/ws/2004/08/addressing line 45)
46   * <p>
47   * <pre>
48   * &lt;element name="EndpointReference" type="{http://schemas.xmlsoap.org/ws/2004/08/addressing}EndpointReferenceType"/>
49   * </pre>
50   *
51   * @author Davanum Srinivas (dims@yahoo.com)
52   */
53  public class EndpointReference extends EndpointReferenceType {
54      
55      private static Log log =
56          LogFactory.getLog(EndpointReference.class.getName());
57  
58      /***
59       * Constructor EndpointReference
60       *
61       * @param address
62       */
63      public EndpointReference(Address address) {
64          setAddress(address);
65      }
66  
67      /***
68       * Constructor EndpointReference
69       *
70       * @param uri
71       */
72      public EndpointReference(URI uri) {
73          setAddress(new Address(uri));
74      }
75  
76      /***
77       * Constructor EndpointReference
78       *
79       * @param endpoint
80       */
81      public EndpointReference(EndpointReferenceType endpoint) {
82          super(endpoint);
83      }
84  
85      /***
86       * Constructor EndpointReference
87       *
88       * @param endpoint
89       * @throws URI.MalformedURIException
90       */
91      public EndpointReference(final String endpoint)
92              throws URI.MalformedURIException {
93          setAddress(new Address(new URI(endpoint)));
94      }
95  
96      /***
97       * Constructor EndpointReference
98       *
99       * @param el
100      * @throws Exception
101      */
102     public EndpointReference(SOAPElement element)
103         throws Exception {
104         Iterator iter = element.getChildElements();
105         while(iter.hasNext()) {
106             javax.xml.soap.Node child = (javax.xml.soap.Node)iter.next();
107             if (child instanceof SOAPElement) {
108                 if (!Constants.NS_URI_ADDRESSING_DEFAULT.equals(
109                             child.getNamespaceURI())) {
110                     // skip now. does not handle extensibiliy elements now
111                     continue;
112                 }
113                 String localName = child.getLocalName();
114                 if (Constants.ADDRESS.equals(localName)) {
115                     Address address =
116                         Address.fromSOAPElement((SOAPElement)child);
117                     setAddress(address);
118                 } else if (Constants.PORT_TYPE.equals(localName)) {
119                     PortType portType = 
120                         PortType.fromSOAPElement((SOAPElement)child);
121                     setPortType(portType);
122                 } else if (Constants.SERVICE_NAME.equals(localName)) {
123                     ServiceNameType serviceName = 
124                         ServiceNameType.fromSOAPElement((SOAPElement)child);
125                     setServiceName(serviceName);
126                 } else if (Constants.REFERENCE_PROPERTIES.equals(localName)) {
127                     ReferencePropertiesType props = 
128                         ReferencePropertiesType.fromSOAPElement((SOAPElement)child);
129                     setProperties(props);
130                 } else {
131                     // ignore others
132                 }
133             } 
134         }
135     }
136 
137     /***
138      * Constructor EndpointReference
139      *
140      * @param el
141      * @throws Exception
142      */
143     public EndpointReference(final Element element)
144         throws Exception {
145         NodeList children = element.getChildNodes();
146         for (int i=0;i<children.getLength();i++) {
147             Node child = (Node)children.item(i);
148             if (child instanceof Element) {
149                 if (!Constants.NS_URI_ADDRESSING_DEFAULT.equals(
150                             child.getNamespaceURI())) {
151                     // skip now. does not handle extensibiliy elements now
152                     continue;
153                 }
154                 String localName = child.getLocalName();
155                 if (Constants.ADDRESS.equals(localName)) {
156                     Address address =
157                         Address.fromElement((Element)child);
158                     setAddress(address);
159                 } else if (Constants.PORT_TYPE.equals(localName)) {
160                     PortType portType = 
161                         PortType.fromElement((Element)child);
162                     setPortType(portType);
163                 } else if (Constants.SERVICE_NAME.equals(localName)) {
164                     ServiceNameType serviceName = 
165                         ServiceNameType.fromElement((Element)child);
166                     setServiceName(serviceName);
167                 } else if (Constants.REFERENCE_PROPERTIES.equals(localName)) {
168                     ReferencePropertiesType props = 
169                         ReferencePropertiesType.fromElement((Element)child);
170                     setProperties(props);
171                 } else {
172                     // ignore others
173                 }
174             } 
175         }
176     }
177 
178     /***
179      * Method toDOM.
180      *
181      * @param doc
182      */
183     public Element toDOM(Document doc) {
184         return toDOM(doc, Constants.ENDPOINT_REFERENCE);
185     }
186 
187     /***
188      * Method toDOM.
189      *
190      * @param doc
191      * @param name
192      */
193     public Element toDOM(Document doc, String elementName) {
194         if (doc == null) {
195             try {
196                 doc = XMLUtils.newDocument();
197             } catch (ParserConfigurationException e) {
198                 return null;
199             }
200         }
201 
202         Element parent = doc.createElementNS(Constants.NS_URI_ADDRESSING_DEFAULT,
203                                              elementName);
204         if (doc.getDocumentElement() == null) {
205             doc.appendChild(parent);
206         } else {
207             doc.getDocumentElement().appendChild(parent);
208         }
209 
210         if (getAddress() != null) {
211             getAddress().append(parent, Constants.ADDRESS);
212         }
213         if (getPortType() != null) {
214             getPortType().append(parent, Constants.PORT_TYPE);
215         }
216         if (getServiceName() != null) {
217             getServiceName().append(parent, Constants.SERVICE_NAME);
218         }
219         ReferencePropertiesType referenceProperties = getProperties();
220         if (referenceProperties != null && referenceProperties.size() > 0) {
221             referenceProperties.append(parent, Constants.REFERENCE_PROPERTIES);
222         }
223         MessageElement [] any = get_any();
224         if (any != null && any.length > 0) {
225             for (int i = 0; i < any.length; i++) {
226                 try {
227                     parent.appendChild(doc.importNode(any[i].getAsDOM(), 
228                                                       true));
229                 } catch (Exception e) {
230                     log.debug("", e);
231                 }
232             }
233         }
234         return parent;
235     }
236 
237     public SOAPHeaderElement toSOAPHeaderElement(SOAPEnvelope env,
238                                                  String actorURI)
239         throws Exception {
240         return toSOAPHeaderElement(env, actorURI, 
241                                    Constants.ENDPOINT_REFERENCE);
242     }
243 
244     protected SOAPHeaderElement toSOAPHeaderElement(SOAPEnvelope env,
245                                                     String actorURI,
246                                                     String name)
247         throws Exception {
248         if (name == null) {
249             throw new IllegalArgumentException();
250         }
251         if (!(env instanceof org.apache.axis.message.SOAPEnvelope)) {
252             throw new Exception("Not supported");
253         }
254 
255         SOAPHeader header = env.getHeader();
256         if (header == null) {
257             header = env.addHeader();
258         }
259 
260         Document doc = XMLUtils.newDocument();
261 
262         org.apache.axis.message.SOAPHeaderElement headerElem =
263             new org.apache.axis.message.SOAPHeaderElement(toDOM(doc, name));
264         headerElem.setActor(actorURI);
265         ((org.apache.axis.message.SOAPEnvelope)env).addHeader(headerElem);
266 
267         return headerElem;
268     }
269 
270 }