KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > soap > handlers > addressing > AddressingHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.soap.handlers.addressing;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.xml.namespace.QName JavaDoc;
23
24 import org.apache.activemq.util.IdGenerator;
25 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
26 import org.apache.servicemix.jbi.util.DOMUtil;
27 import org.apache.servicemix.soap.Context;
28 import org.apache.servicemix.soap.SoapFault;
29 import org.apache.servicemix.soap.handlers.AbstractHandler;
30 import org.apache.servicemix.soap.marshalers.SoapMessage;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.DocumentFragment JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34
35 /**
36  *
37  * @author Guillaume Nodet
38  * @version $Revision: 1.5 $
39  * @since 3.0
40  *
41  * @org.apache.xbean.XBean element="ws-addressing"
42  */

43 public class AddressingHandler extends AbstractHandler {
44
45     public static final String JavaDoc WSA_NAMESPACE_200303 = "http://schemas.xmlsoap.org/ws/2003/03/addressing";
46     public static final String JavaDoc WSA_NAMESPACE_200403 = "http://schemas.xmlsoap.org/ws/2004/03/addressing";
47     public static final String JavaDoc WSA_NAMESPACE_200408 = "http://schemas.xmlsoap.org/ws/2004/08/addressing";
48     public static final String JavaDoc WSA_NAMESPACE_200508 = "http://www.w3.org/2005/08/addressing";
49     
50     public static final String JavaDoc WSA_PREFIX = "wsa";
51     
52     public static final String JavaDoc EL_ACTION = "Action";
53     public static final String JavaDoc EL_ADDRESS = "Address";
54     public static final String JavaDoc EL_FAULT_TO = "FaultTo";
55     public static final String JavaDoc EL_FROM = "From";
56     public static final String JavaDoc EL_MESSAGE_ID = "MessageID";
57     public static final String JavaDoc EL_METADATA = "Metadata";
58     public static final String JavaDoc EL_REFERENCE_PARAMETERS = "ReferenceParameters";
59     public static final String JavaDoc EL_RELATES_TO = "RelatesTo";
60     public static final String JavaDoc EL_REPLY_TO = "ReplyTo";
61     public static final String JavaDoc EL_TO = "To";
62     
63     protected final SourceTransformer sourceTransformer = new SourceTransformer();
64     protected final IdGenerator idGenerator = new IdGenerator();
65     
66     public void onReceive(Context context) throws Exception JavaDoc {
67         SoapMessage message = context.getInMessage();
68         String JavaDoc action = null;
69         String JavaDoc to = null;
70         String JavaDoc nsUri = null;
71         Map JavaDoc headers = message.getHeaders();
72         if (headers != null) {
73             for (Iterator JavaDoc it = headers.keySet().iterator(); it.hasNext();) {
74                 QName JavaDoc qname = (QName JavaDoc) it.next();
75                 Object JavaDoc value = headers.get(qname);
76                 if (isWSANamespace(qname.getNamespaceURI())) {
77                     if (nsUri == null) {
78                         nsUri = qname.getNamespaceURI();
79                     } else if (!nsUri.equals(qname.getNamespaceURI())) {
80                         throw new SoapFault(SoapFault.SENDER, "Inconsistent use of wsa namespaces");
81                     }
82                     if (EL_ACTION.equals(qname.getLocalPart())) {
83                         action = getHeaderText(value);
84                         String JavaDoc[] parts = split(action);
85                         context.setProperty(Context.INTERFACE, new QName JavaDoc(parts[0], parts[1]));
86                         context.setProperty(Context.OPERATION, new QName JavaDoc(parts[0], parts[2]));
87                     } else if (EL_TO.equals(qname.getLocalPart())) {
88                         to = getHeaderText(value);
89                         String JavaDoc[] parts = split(to);
90                         context.setProperty(Context.SERVICE, new QName JavaDoc(parts[0], parts[1]));
91                         context.setProperty(Context.ENDPOINT, parts[2]);
92                     } else {
93                         // TODO: what ?
94
}
95                 }
96             }
97         }
98     }
99     
100     public void onReply(Context context) throws Exception JavaDoc {
101         SoapMessage in = context.getInMessage();
102         SoapMessage out = context.getOutMessage();
103         Map JavaDoc headers = in.getHeaders();
104         if (headers != null) {
105             for (Iterator JavaDoc it = headers.keySet().iterator(); it.hasNext();) {
106                 QName JavaDoc qname = (QName JavaDoc) it.next();
107                 Object JavaDoc value = headers.get(qname);
108                 if (isWSANamespace(qname.getNamespaceURI())) {
109                     if (EL_MESSAGE_ID.equals(qname.getLocalPart())) {
110                         QName JavaDoc name = new QName JavaDoc(qname.getNamespaceURI(), EL_MESSAGE_ID, qname.getPrefix() != null ? qname.getPrefix() : WSA_PREFIX);
111                         DocumentFragment JavaDoc df = createHeader(name, idGenerator.generateSanitizedId());
112                         out.addHeader(name, df);
113                         name = new QName JavaDoc(qname.getNamespaceURI(), EL_RELATES_TO, qname.getPrefix() != null ? qname.getPrefix() : WSA_PREFIX);
114                         df = createHeader(name, getHeaderText(value));
115                         out.addHeader(name, df);
116                     }
117                 }
118             }
119         }
120     }
121     
122     protected boolean isWSANamespace(String JavaDoc ns) {
123         return WSA_NAMESPACE_200303.equals(ns) ||
124                WSA_NAMESPACE_200403.equals(ns) ||
125                WSA_NAMESPACE_200408.equals(ns) ||
126                WSA_NAMESPACE_200508.equals(ns);
127     }
128     
129     protected String JavaDoc getHeaderText(Object JavaDoc header) {
130         Element JavaDoc el = (Element JavaDoc) ((DocumentFragment JavaDoc) header).getFirstChild();
131         return DOMUtil.getElementText(el);
132     }
133     
134     protected DocumentFragment JavaDoc createHeader(QName JavaDoc name, String JavaDoc value) throws Exception JavaDoc {
135         Document JavaDoc doc = new SourceTransformer().createDocument();
136         DocumentFragment JavaDoc df = doc.createDocumentFragment();
137         Element JavaDoc el = doc.createElementNS(name.getNamespaceURI(), name.getPrefix() + ":" + name.getLocalPart());
138         el.appendChild(doc.createTextNode(value));
139         df.appendChild(el);
140         return df;
141     }
142     
143     protected String JavaDoc[] split(String JavaDoc uri) {
144         char sep;
145         uri = uri.trim();
146         if (uri.indexOf('/') > 0) {
147             sep = '/';
148         } else {
149             sep = ':';
150         }
151         int idx1 = uri.lastIndexOf(sep);
152         int idx2 = uri.lastIndexOf(sep, idx1 - 1);
153         String JavaDoc epName = uri.substring(idx1 + 1);
154         String JavaDoc svcName = uri.substring(idx2 + 1, idx1);
155         String JavaDoc nsUri = uri.substring(0, idx2);
156         return new String JavaDoc[] { nsUri, svcName, epName };
157     }
158     
159 }
160
Popular Tags