KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > ws > addressing > soap > VersionTransformer


1 package org.objectweb.celtix.bus.ws.addressing.soap;
2
3
4 import java.util.Collections JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.Set JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8
9 import javax.xml.bind.JAXBException;
10 import javax.xml.bind.Marshaller;
11 import javax.xml.bind.Unmarshaller;
12 import javax.xml.namespace.QName JavaDoc;
13 import javax.xml.soap.SOAPHeader JavaDoc;
14 import javax.xml.soap.SOAPHeaderElement JavaDoc;
15
16 import org.objectweb.celtix.bus.ws.addressing.Names;
17 import org.objectweb.celtix.common.logging.LogUtils;
18
19 // importation convention: if the same class name is used for
20
// 2005/08 and 2004/08, then the former version is imported
21
// and the latter is fully qualified when used
22
import org.objectweb.celtix.ws.addressing.AttributedURIType;
23 import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
24 import org.objectweb.celtix.ws.addressing.RelatesToType;
25 import org.objectweb.celtix.ws.addressing.v200408.AttributedURI;
26 import org.objectweb.celtix.ws.addressing.v200408.Relationship;
27
28
29 /**
30  * This class is responsible for transforming between the native
31  * WS-Addressing schema version (i.e. 2005/08) and exposed
32  * version (currently may be 2005/08 or 2004/08).
33  * <p>
34  * The native version is that used throughout the stack, were the
35  * WS-A types are represented via the JAXB generated types for the
36  * 2005/08 schema.
37  * <p>
38  * The exposed version is that used when the WS-A types are
39  * externalized, i.e. are encoded in the headers of outgoing
40  * messages. For outgoing requests, the exposed version is
41  * determined from configuration. For outgoing responses, the
42  * exposed version is determined by the exposed version of
43  * the corresponding request.
44  * <p>
45  * The motivation for using different native and exposed types
46  * is usually to facilitate a WS-* standard based on an earlier
47  * version of WS-Adressing (for example WS-RM depends on the
48  * 2004/08 version).
49  */

50 public class VersionTransformer
51     extends org.objectweb.celtix.bus.ws.addressing.VersionTransformer {
52
53     public static final Set JavaDoc<QName JavaDoc> HEADERS;
54     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(VersionTransformer.class);
55     
56     protected MAPCodec codec;
57     
58     /**
59      * Constructor.
60      *
61      * @param mapCodec the MAPCodec to use
62      */

63     public VersionTransformer(MAPCodec mapCodec) {
64         codec = mapCodec;
65     }
66     
67     /**
68      * Encode message in exposed version.
69      *
70      * @param exposeAs specifies the WS-Addressing version to expose
71      * @param value the value to encode
72      * @param localName the localName for the header
73      * @param clz the class
74      * @param header the SOAP header
75      * @param marshaller the JAXB marshaller to use
76      */

77     public <T> void encodeAsExposed(String JavaDoc exposeAs,
78                                     T value,
79                                     String JavaDoc localName,
80                                     Class JavaDoc<T> clz,
81                                     SOAPHeader JavaDoc header,
82                                     Marshaller marshaller)
83         throws JAXBException {
84         if (value != null) {
85             if (NATIVE_VERSION.equals(exposeAs)) {
86                 codec.encodeMAP(value,
87                                 new QName JavaDoc(exposeAs, localName),
88                                 clz,
89                                 header,
90                                 marshaller);
91             } else if (Names200408.WSA_NAMESPACE_NAME.equals(exposeAs)) {
92                 if (AttributedURIType.class.equals(clz)) {
93                     codec.encodeMAP(convert((AttributedURIType)value),
94                                     new QName JavaDoc(exposeAs, localName),
95                                     AttributedURI.class,
96                                     header,
97                                     marshaller);
98                 } else if (EndpointReferenceType.class.equals(clz)) {
99                     codec.encodeMAP(convert((EndpointReferenceType)value),
100                                     new QName JavaDoc(exposeAs, localName),
101                                     Names200408.EPR_TYPE,
102                                     header,
103                                     marshaller);
104                     
105                 } else if (RelatesToType.class.equals(clz)) {
106                     codec.encodeMAP(convert((RelatesToType)value),
107                                     new QName JavaDoc(exposeAs, localName),
108                                     Relationship.class,
109                                     header,
110                                     marshaller);
111                     
112                 }
113             }
114         }
115     }
116     
117     /**
118      * Decodes a MAP from a exposed version.
119      *
120      * @param encodedAs specifies the encoded version
121      * @param clz the class
122      * @param headerElement the SOAP header element
123      * @param marshaller the JAXB marshaller to use
124      * @return the decoded value
125      */

126     @SuppressWarnings JavaDoc("unchecked")
127     public <T> T decodeAsNative(String JavaDoc encodedAs,
128                                 Class JavaDoc<T> clz,
129                                 SOAPHeaderElement JavaDoc headerElement,
130                                 Unmarshaller unmarshaller)
131         throws JAXBException {
132         T ret = null;
133         LOG.fine("decodeAsNative: encodedAs: " + encodedAs);
134         LOG.fine(" class: " + clz.getName());
135
136         if (NATIVE_VERSION.equals(encodedAs)) {
137             ret = codec.decodeMAP(clz, headerElement, unmarshaller);
138         } else if (Names200408.WSA_NAMESPACE_NAME.equals(encodedAs)) {
139             if (AttributedURIType.class.equals(clz)) {
140                 return (T)convert(codec.decodeMAP(AttributedURI.class,
141                                                   headerElement,
142                                                   unmarshaller));
143             } else if (EndpointReferenceType.class.equals(clz)) {
144                 return (T)convert(codec.decodeMAP(Names200408.EPR_TYPE,
145                                                   headerElement,
146                                                   unmarshaller));
147             } else if (RelatesToType.class.equals(clz)) {
148                 return (T)convert(codec.decodeMAP(Relationship.class,
149                                                   headerElement,
150                                                   unmarshaller));
151             }
152         }
153         return ret;
154     }
155            
156     /**
157      * Augment the set of headers understood by the protocol binding
158      * with the 2004/08 header QNames.
159      */

160     static {
161         Set JavaDoc<QName JavaDoc> headers = new HashSet JavaDoc<QName JavaDoc>();
162         headers.addAll(Names.HEADERS);
163         Names200408.addHeaders(headers);
164         HEADERS = Collections.unmodifiableSet(headers);
165     }
166     
167     /**
168      * Holder for 2004/08 Names
169      */

170     public static final class Names200408
171         extends org.objectweb.celtix.bus.ws.addressing.VersionTransformer.Names200408 {
172         
173         protected Names200408() {
174         }
175         
176         /**
177          * Adds 2004/08 headers to set.
178          *
179          * @param headers set of headers
180          */

181         private static void addHeaders(Set JavaDoc<QName JavaDoc> headers) {
182             headers.add(new QName JavaDoc(WSA_NAMESPACE_NAME,
183                                   Names.WSA_FROM_NAME));
184             headers.add(new QName JavaDoc(WSA_NAMESPACE_NAME,
185                                   Names.WSA_TO_NAME));
186             headers.add(new QName JavaDoc(WSA_NAMESPACE_NAME,
187                                   Names.WSA_REPLYTO_NAME));
188             headers.add(new QName JavaDoc(WSA_NAMESPACE_NAME,
189                                   Names.WSA_FAULTTO_NAME));
190             headers.add(new QName JavaDoc(WSA_NAMESPACE_NAME,
191                                   Names.WSA_ACTION_NAME));
192             headers.add(new QName JavaDoc(WSA_NAMESPACE_NAME,
193                                   Names.WSA_MESSAGEID_NAME));
194         }
195     }
196 }
197
Popular Tags