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.interceptors.jbi;
018    
019    import java.util.Iterator;
020    import java.util.Map;
021    import java.util.Set;
022    import java.net.URI;
023    
024    import javax.activation.DataHandler;
025    import javax.jbi.messaging.MessageExchange;
026    import javax.jbi.messaging.NormalizedMessage;
027    import javax.xml.namespace.QName;
028    import javax.xml.transform.Source;
029    
030    import org.apache.servicemix.soap.api.Fault;
031    import org.apache.servicemix.soap.api.Message;
032    import org.apache.servicemix.soap.api.model.Binding;
033    import org.apache.servicemix.soap.api.model.Operation;
034    import org.apache.servicemix.soap.bindings.soap.SoapConstants;
035    import org.apache.servicemix.soap.bindings.soap.model.SoapOperation;
036    import org.apache.servicemix.soap.core.AbstractInterceptor;
037    import org.apache.servicemix.soap.util.QNameUtil;
038    import org.w3c.dom.DocumentFragment;
039    
040    /**
041     * @author <a href="mailto:gnodet [at] gmail.com">Guillaume Nodet</a>
042     */
043    public class JbiOutInterceptor extends AbstractInterceptor {
044    
045        private final boolean server;
046        
047        public JbiOutInterceptor(boolean server) {
048            this.server = server;
049        }
050        
051        public void handleMessage(Message message) {
052            NormalizedMessage nm = message.getContent(NormalizedMessage.class);
053            message.setContent(Source.class, nm.getContent());
054            fromNMSAttachments(message, nm);
055            fromNMSHeaders(message, nm);
056    
057            if (!server) {
058                MessageExchange me = message.getContent(MessageExchange.class);
059                Binding binding = message.get(Binding.class);
060                Operation operation = binding.getOperation(me.getOperation());
061                if (operation != null) {
062                    if (!areMepsEquals(me.getPattern(), operation.getMep())) {
063                        throw new Fault("Received incorrect exchange mep.  Received " + me.getPattern()
064                                        + " but expected " + operation.getMep() + " for operation "
065                                        + operation.getName());
066                    }
067                    message.put(Operation.class, operation);
068                }
069            }
070        }
071    
072        private boolean areMepsEquals(URI mep1, URI mep2) {
073            String s1 = mep1 != null ? mep1.toString() : "";
074            String s2 = mep2 != null ? mep2.toString() : "";
075            int i1 = s1.lastIndexOf('/');
076            int i2 = s2.lastIndexOf('/');
077            if (i1 >= 0 && i2 >= 0) {
078                return s1.substring(i1).equals(s2.substring(i2));
079            }
080            return false;
081        }
082    
083        /**
084         * Copy NormalizedMessage attachments to SoapMessage attachments
085         */
086        private void fromNMSAttachments(Message message, NormalizedMessage normalizedMessage) {
087            Set attachmentNames = normalizedMessage.getAttachmentNames();
088            for (Iterator it = attachmentNames.iterator(); it.hasNext();) {
089                String id = (String) it.next();
090                DataHandler handler = normalizedMessage.getAttachment(id);
091                message.getAttachments().put(id, handler);
092            }
093        }
094    
095        /**
096         * Copy NormalizedMessage headers to SoapMessage headers
097         */
098        @SuppressWarnings("unchecked")
099        private void fromNMSHeaders(Message message, NormalizedMessage normalizedMessage) {
100            if (normalizedMessage.getProperty(JbiConstants.PROTOCOL_HEADERS) != null) {
101                Map<String, ?> headers = (Map<String, ?>) normalizedMessage.getProperty(JbiConstants.PROTOCOL_HEADERS);
102                for (Map.Entry<String, ?> entry : headers.entrySet()) {
103                    QName name = QNameUtil.parse(entry.getKey());
104                    if (name != null) {
105                        message.getSoapHeaders().put(name, (DocumentFragment) entry.getValue());
106                    } else {
107                        message.getTransportHeaders().put(entry.getKey(), (String) entry.getValue());
108                    }
109                }
110            }
111        }
112    
113    }