KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > soap > marshalers > JBIMarshaler


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.marshalers;
18
19 import java.net.URI JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.activation.DataHandler JavaDoc;
25 import javax.jbi.messaging.Fault;
26 import javax.jbi.messaging.NormalizedMessage;
27 import javax.xml.namespace.QName JavaDoc;
28
29 import org.apache.servicemix.JbiConstants;
30 import org.apache.servicemix.soap.SoapFault;
31 import org.w3c.dom.DocumentFragment JavaDoc;
32
33 /**
34  *
35  * @author Guillaume Nodet
36  * @version $Revision: 1.5 $
37  * @since 3.0
38  */

39 public class JBIMarshaler {
40
41     public static final String JavaDoc SOAP_FAULT_CODE = "org.apache.servicemix.soap.fault.code";
42     public static final String JavaDoc SOAP_FAULT_SUBCODE = "org.apache.servicemix.soap.fault.subcode";
43     public static final String JavaDoc SOAP_FAULT_REASON = "org.apache.servicemix.soap.fault.reason";
44     public static final String JavaDoc SOAP_FAULT_NODE = "org.apache.servicemix.soap.fault.node";
45     public static final String JavaDoc SOAP_FAULT_ROLE = "org.apache.servicemix.soap.fault.role";
46     
47     public void toNMS(NormalizedMessage normalizedMessage, SoapMessage soapMessage) throws Exception JavaDoc {
48         if (soapMessage.hasHeaders()) {
49             normalizedMessage.setProperty(JbiConstants.SOAP_HEADERS, soapMessage.getHeaders());
50         }
51         if (soapMessage.hasAttachments()) {
52             Map JavaDoc attachments = soapMessage.getAttachments();
53             for (Iterator JavaDoc it = attachments.entrySet().iterator(); it.hasNext();) {
54                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
55                 normalizedMessage.addAttachment((String JavaDoc) entry.getKey(),
56                                                 (DataHandler JavaDoc) entry.getValue());
57             }
58         }
59         normalizedMessage.setSecuritySubject(soapMessage.getSubject());
60         if (soapMessage.getFault() != null) {
61             if (normalizedMessage instanceof Fault == false) {
62                 throw new IllegalStateException JavaDoc("The soap message is a fault but the jbi message is not");
63             }
64             SoapFault fault = soapMessage.getFault();
65             normalizedMessage.setProperty(SOAP_FAULT_CODE, fault.getCode());
66             normalizedMessage.setProperty(SOAP_FAULT_SUBCODE, fault.getSubcode());
67             normalizedMessage.setProperty(SOAP_FAULT_REASON, fault.getReason());
68             normalizedMessage.setProperty(SOAP_FAULT_NODE, fault.getNode());
69             normalizedMessage.setProperty(SOAP_FAULT_ROLE, fault.getRole());
70             normalizedMessage.setContent(fault.getDetails());
71         } else {
72             normalizedMessage.setContent(soapMessage.getSource());
73         }
74     }
75     
76     public void fromNMS(SoapMessage soapMessage, NormalizedMessage normalizedMessage) {
77         if (normalizedMessage.getProperty(JbiConstants.SOAP_HEADERS) != null) {
78             Map JavaDoc headers = (Map JavaDoc) normalizedMessage.getProperty(JbiConstants.SOAP_HEADERS);
79             for (Iterator JavaDoc it = headers.entrySet().iterator(); it.hasNext();) {
80                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
81                 soapMessage.addHeader((QName JavaDoc) entry.getKey(), (DocumentFragment JavaDoc) entry.getValue());
82             }
83         }
84         Set JavaDoc attachmentNames = normalizedMessage.getAttachmentNames();
85         for (Iterator JavaDoc it = attachmentNames.iterator(); it.hasNext();) {
86             String JavaDoc id = (String JavaDoc) it.next();
87             DataHandler JavaDoc handler = normalizedMessage.getAttachment(id);
88             soapMessage.addAttachment(id, handler);
89         }
90         if (normalizedMessage instanceof Fault) {
91             QName JavaDoc code = (QName JavaDoc) normalizedMessage.getProperty(SOAP_FAULT_CODE);
92             QName JavaDoc subcode = (QName JavaDoc) normalizedMessage.getProperty(SOAP_FAULT_SUBCODE);
93             String JavaDoc reason = (String JavaDoc) normalizedMessage.getProperty(SOAP_FAULT_REASON);
94             URI JavaDoc node = (URI JavaDoc) normalizedMessage.getProperty(SOAP_FAULT_NODE);
95             URI JavaDoc role = (URI JavaDoc) normalizedMessage.getProperty(SOAP_FAULT_ROLE);
96             SoapFault fault = new SoapFault(code, subcode, reason, node, role, normalizedMessage.getContent());
97             soapMessage.setFault(fault);
98         } else {
99             soapMessage.setSource(normalizedMessage.getContent());
100         }
101     }
102
103 }
104
Popular Tags