View Javadoc

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