View Javadoc

1   package org.codehaus.xfire.addressing;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import javax.xml.namespace.QName;
7   
8   import org.codehaus.yom.Attribute;
9   import org.codehaus.yom.Element;
10  import org.codehaus.yom.Elements;
11  
12  /***
13   * A WS-Addressing endpoint reference.
14   * 
15   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
16   */
17  public class AddressingHeadersFactory200408
18      extends AbstactAddressingHeadersFactory
19  {
20  
21      public AddressingHeaders createHeaders(Element root)
22      {
23          AddressingHeaders headers = new AddressingHeaders();
24          
25          Element from = root.getFirstChildElement(WSA_FROM, WSA_NAMESPACE_200408);
26          if (from != null)
27          {
28              headers.setFrom(createEPR(from));
29          }
30          
31          Element replyTo = root.getFirstChildElement(WSA_REPLY_TO, WSA_NAMESPACE_200408);
32          if (replyTo != null)
33          {
34              headers.setReplyTo(createEPR(replyTo));
35          }
36  
37          Element faultTo = root.getFirstChildElement(WSA_FAULT_TO, WSA_NAMESPACE_200408);
38          if (faultTo != null)
39          {
40              headers.setFaultTo(createEPR(faultTo));
41          }
42          
43          headers.setMessageID(getChildValue(root, WSA_MESSAGE_ID, WSA_NAMESPACE_200408));
44  
45          Element relatesTo = root.getFirstChildElement(WSA_RELATES_TO, WSA_NAMESPACE_200408);
46          if (relatesTo != null)
47          {
48              headers.setRelatesTo(relatesTo.getValue());
49              String relation = relatesTo.getAttributeValue("RelationshipType");
50              if (relation != null)
51              {
52                  headers.setRelationshipType(stringToQName(relatesTo, relation));
53              }
54              else
55              {
56                  headers.setRelationshipType(new QName(WSA_NAMESPACE_200408, "Reply"));
57              }
58          }
59          
60          headers.setTo(getChildValue(root, WSA_TO, WSA_NAMESPACE_200408));
61          headers.setAction(getChildValue(root, WSA_ACTION, WSA_NAMESPACE_200408));
62          
63          return headers;
64      }
65      
66      public EndpointReference createEPR(Element eprElement)
67      {
68          EndpointReference epr = new EndpointReference();
69          
70          List anyContent = null;
71          
72          Elements elements = eprElement.getChildElements();
73          String version = eprElement.getNamespaceURI();
74          
75          for (int i = 0; i < elements.size(); i++)
76          {
77              Element e = elements.get(i);
78              if (e.getNamespaceURI().equals(version))
79              {
80                  if (e.getLocalName().equals(WSA_ADDRESS))
81                  {
82                      epr.setAddress(e.getValue());
83                  }
84                  else if (e.getLocalName().equals(WSA_SERVICE_NAME))
85                  {
86                      epr.setServiceName(elementToQName(e));
87                      epr.setEndpointName(e.getAttributeValue(WSA_ENDPOINT_NAME, version));
88                  }
89                  else if (e.getLocalName().equals(WSA_INTERFACE_NAME))
90                  {
91                      epr.setInterfaceName(elementToQName(e));
92                  }
93                  else if (e.getLocalName().equals(WSA_POLICIES))
94                  {
95                      List policies = new ArrayList();
96                      
97                      Elements polEls = e.getChildElements();
98                      for (int j = 0; j < polEls.size(); j++)
99                      {
100                         policies.add(polEls.get(j));
101                     }
102                     epr.setPolicies(policies);
103                 }
104                 else if (e.getLocalName().equals(WSA_REFERENCE_PROPERTIES))
105                 {
106                     List props = new ArrayList();
107                     
108                     Elements polEls = e.getChildElements();
109                     for (int j = 0; j < polEls.size(); j++)
110                     {
111                         props.add(polEls.get(j));
112                     }
113                     
114                     epr.setReferenceProperties(props);
115                 }
116                 else if (e.getLocalName().equals(WSA_REFERENCE_PARAMETERS))
117                 {
118                     List params = new ArrayList();
119                     
120                     Elements polEls = e.getChildElements();
121                     for (int j = 0; j < polEls.size(); j++)
122                     {
123                         params.add(polEls.get(j));
124                     }
125                     
126                     epr.setReferenceParameters(params);
127                 }
128                 else
129                 {
130                     if (anyContent == null)
131                         anyContent = new ArrayList();
132                     
133                     anyContent.add(e);
134                 }
135             }
136             
137         }
138         
139         if (anyContent != null)
140         {
141             epr.setAny(anyContent);
142         }
143         
144         return epr;
145     }
146 
147     public boolean hasHeaders(Element root)
148     {
149         return root.getFirstChildElement(WSA_ACTION, WSA_NAMESPACE_200408) != null;
150     }
151 /*
152     public void writeToElement(Element root)
153     {
154         Element addEl = new Element(WSA_ADDRESS_QNAME, getVersion());
155         addEl.appendChild(address);
156         root.appendChild(addEl);
157         
158         if (interfaceName != null)
159         {
160             Element intfEl = new Element(WSA_INTERFACE_NAME_QNAME, getVersion());
161             intfEl.appendChild(qnameToString(root, getInterfaceName()));
162             root.appendChild(intfEl);
163         }
164         
165         if (serviceName != null)
166         {
167             Element svcEl = new Element(WSA_SERVICE_NAME_QNAME, getVersion());
168             svcEl.appendChild(qnameToString(root, getServiceName()));
169             root.appendChild(svcEl);
170         }
171 //    }*/
172 
173 
174     public void writeHeaders(Element root, AddressingHeaders headers)
175     {
176         final String ns = WSA_NAMESPACE_200408;
177         root.addNamespaceDeclaration(WSA_PREFIX, WSA_NAMESPACE_200408);
178         
179         if (headers.getTo() != null)
180         {
181             Element to = new Element(WSA_TO_QNAME, ns);
182             to.appendChild(headers.getTo());
183             root.appendChild(to);
184         }
185         
186         if (headers.getAction() != null)
187         {
188             Element action = new Element(WSA_ACTION_QNAME, ns);
189             action.appendChild(headers.getAction());
190             root.appendChild(action);
191         }
192         
193         if (headers.getFaultTo() != null)
194         {
195             Element faultTo = new Element(WSA_FAULT_TO_QNAME, ns);
196             root.appendChild(faultTo);
197             
198             writeEPR(faultTo, headers.getFaultTo());
199         }
200 
201         if (headers.getFrom() != null)
202         {
203             Element from = new Element(WSA_FROM_QNAME, ns);
204             root.appendChild(from);
205             
206             writeEPR(from, headers.getFrom());
207         }
208 
209         if (headers.getMessageID() != null)
210         {
211             Element messageId = new Element(WSA_MESSAGE_ID_QNAME, ns);
212             messageId.appendChild(headers.getMessageID());
213             root.appendChild(messageId);
214         }
215 
216         if (headers.getRelatesTo() != null)
217         {
218             Element relatesTo = new Element(WSA_RELATES_TO_QNAME, ns);
219             relatesTo.appendChild(headers.getRelatesTo());
220             root.appendChild(relatesTo);
221             
222             if (headers.getRelationshipType() != null)
223             {
224                 String value = qnameToString(root, headers.getRelationshipType());
225                 relatesTo.addAttribute(new Attribute(WSA_RELATIONSHIP_TYPE, value));
226             }
227         }
228         
229         if (headers.getReplyTo() != null)
230         {
231             Element replyTo = new Element(WSA_REPLY_TO_QNAME, ns);
232             root.appendChild(replyTo);
233             
234             writeEPR(replyTo, headers.getReplyTo());
235         }
236     }
237 
238     public void writeEPR(Element root, EndpointReference epr)
239     {
240         final String ns = WSA_NAMESPACE_200408;
241         
242         Element address = new Element(WSA_ADDRESS_QNAME, ns);
243         address.appendChild(epr.getAddress());
244         root.appendChild(address);
245         
246         if (epr.getServiceName() != null)
247         {
248             Element serviceName = new Element(WSA_SERVICE_NAME_QNAME, ns);
249             serviceName.appendChild(qnameToString((Element) root.getParent(), epr.getServiceName()));
250             root.appendChild(serviceName);
251             
252             if (epr.getInterfaceName() != null)
253             {
254                 String value = qnameToString((Element) root.getParent(), epr.getInterfaceName());
255                 serviceName.addAttribute(new Attribute("PortType", value));
256             }
257         }
258     }
259 
260     public String getAnonymousUri()
261     {
262         return WSA_200408_ANONYMOUS_URI;
263     }
264 }