001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.soap.marshalers;
018    
019    import java.net.URI;
020    import java.util.Iterator;
021    import java.util.Map;
022    import java.util.Set;
023    
024    import javax.activation.DataHandler;
025    import javax.jbi.messaging.Fault;
026    import javax.jbi.messaging.NormalizedMessage;
027    import javax.xml.namespace.QName;
028    
029    import org.apache.servicemix.common.JbiConstants;
030    import org.apache.servicemix.soap.SoapFault;
031    import org.w3c.dom.DocumentFragment;
032    
033    /**
034     * 
035     * @author Guillaume Nodet
036     * @version $Revision: 1.5 $
037     * @since 3.0
038     */
039    public class JBIMarshaler {
040    
041        public static final String SOAP_FAULT_CODE = "org.apache.servicemix.soap.fault.code";
042        public static final String SOAP_FAULT_SUBCODE = "org.apache.servicemix.soap.fault.subcode";
043        public static final String SOAP_FAULT_REASON = "org.apache.servicemix.soap.fault.reason";
044        public static final String SOAP_FAULT_NODE = "org.apache.servicemix.soap.fault.node";
045        public static final String SOAP_FAULT_ROLE = "org.apache.servicemix.soap.fault.role";
046        
047            public void toNMS(NormalizedMessage normalizedMessage, SoapMessage soapMessage) throws Exception {
048            if (soapMessage.hasHeaders()) {
049                    normalizedMessage.setProperty(JbiConstants.SOAP_HEADERS, soapMessage.getHeaders());
050            }
051            if (soapMessage.hasAttachments()) {
052                    Map attachments = soapMessage.getAttachments();
053                    for (Iterator it = attachments.entrySet().iterator(); it.hasNext();) {
054                            Map.Entry entry = (Map.Entry) it.next();
055                            normalizedMessage.addAttachment((String) entry.getKey(), 
056                                                                                            (DataHandler) entry.getValue());
057                    }
058            }
059            normalizedMessage.setSecuritySubject(soapMessage.getSubject());
060            if (soapMessage.getFault() != null) {
061                if (normalizedMessage instanceof Fault == false) {
062                    throw new IllegalStateException("The soap message is a fault but the jbi message is not");
063                }
064                SoapFault fault = soapMessage.getFault();
065                normalizedMessage.setProperty(SOAP_FAULT_CODE, fault.getCode());
066                normalizedMessage.setProperty(SOAP_FAULT_SUBCODE, fault.getSubcode());
067                normalizedMessage.setProperty(SOAP_FAULT_REASON, fault.getReason());
068                normalizedMessage.setProperty(SOAP_FAULT_NODE, fault.getNode());
069                normalizedMessage.setProperty(SOAP_FAULT_ROLE, fault.getRole());
070                normalizedMessage.setContent(fault.getDetails());
071            } else {
072                normalizedMessage.setContent(soapMessage.getSource());
073            }
074            }
075            
076            public void fromNMS(SoapMessage soapMessage, NormalizedMessage normalizedMessage) {
077                    if (normalizedMessage.getProperty(JbiConstants.SOAP_HEADERS) != null) {
078                            Map headers = (Map) normalizedMessage.getProperty(JbiConstants.SOAP_HEADERS);
079                    for (Iterator it = headers.entrySet().iterator(); it.hasNext();) {
080                            Map.Entry entry = (Map.Entry) it.next();
081                            soapMessage.addHeader((QName) entry.getKey(), (DocumentFragment) entry.getValue());
082                    }
083                    }
084                    Set attachmentNames = normalizedMessage.getAttachmentNames();
085                    for (Iterator it = attachmentNames.iterator(); it.hasNext();) {
086                            String id = (String) it.next();
087                            DataHandler handler = normalizedMessage.getAttachment(id);
088                            soapMessage.addAttachment(id, handler);
089                    }
090            soapMessage.setSubject(normalizedMessage.getSecuritySubject());
091            if (normalizedMessage instanceof Fault) {
092                QName code = (QName) normalizedMessage.getProperty(SOAP_FAULT_CODE);
093                QName subcode = (QName) normalizedMessage.getProperty(SOAP_FAULT_SUBCODE);
094                String reason = (String) normalizedMessage.getProperty(SOAP_FAULT_REASON);
095                URI node = (URI) normalizedMessage.getProperty(SOAP_FAULT_NODE);
096                URI role = (URI) normalizedMessage.getProperty(SOAP_FAULT_ROLE);
097                SoapFault fault = new SoapFault(code, subcode, reason, node, role, normalizedMessage.getContent());
098                soapMessage.setFault(fault);
099            } else {
100                soapMessage.setSource(normalizedMessage.getContent());
101            }
102            }
103    
104    }