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:
023: import javax.activation.DataHandler;
024: import javax.jbi.messaging.MessageExchange;
025: import javax.jbi.messaging.NormalizedMessage;
026: import javax.xml.namespace.QName;
027: import javax.xml.transform.Source;
028:
029: import org.apache.servicemix.jbi.FaultException;
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
053: .getContent(NormalizedMessage.class);
054: message.setContent(Source.class, nm.getContent());
055: fromNMSAttachments(message, nm);
056: fromNMSHeaders(message, nm);
057:
058: if (!server) {
059: MessageExchange me = message
060: .getContent(MessageExchange.class);
061: Binding binding = message.get(Binding.class);
062: Operation operation = binding.getOperation(me
063: .getOperation());
064: if (operation != null) {
065: if (!me.getPattern().equals(operation.getMep())) {
066: throw new Fault(
067: "Received incorrect exchange mep. Received "
068: + me.getPattern()
069: + " but expected "
070: + operation.getMep()
071: + " for operation "
072: + operation.getName());
073: }
074: message.put(Operation.class, operation);
075: if (operation instanceof SoapOperation<?>) {
076: String soapAction = ((SoapOperation<?>) operation)
077: .getSoapAction();
078: message.getTransportHeaders().put(
079: SoapConstants.SOAP_ACTION_HEADER,
080: soapAction);
081: }
082: }
083: }
084: }
085:
086: /**
087: * Copy NormalizedMessage attachments to SoapMessage attachments
088: */
089: private void fromNMSAttachments(Message message,
090: NormalizedMessage normalizedMessage) {
091: Set attachmentNames = normalizedMessage.getAttachmentNames();
092: for (Iterator it = attachmentNames.iterator(); it.hasNext();) {
093: String id = (String) it.next();
094: DataHandler handler = normalizedMessage.getAttachment(id);
095: message.getAttachments().put(id, handler);
096: }
097: }
098:
099: /**
100: * Copy NormalizedMessage headers to SoapMessage headers
101: */
102: @SuppressWarnings("unchecked")
103: private void fromNMSHeaders(Message message,
104: NormalizedMessage normalizedMessage) {
105: if (normalizedMessage
106: .getProperty(JbiConstants.PROTOCOL_HEADERS) != null) {
107: Map<String, ?> headers = (Map<String, ?>) normalizedMessage
108: .getProperty(JbiConstants.PROTOCOL_HEADERS);
109: for (Map.Entry<String, ?> entry : headers.entrySet()) {
110: QName name = QNameUtil.parse(entry.getKey());
111: if (name != null) {
112: message.getSoapHeaders().put(name,
113: (DocumentFragment) entry.getValue());
114: } else {
115: message.getTransportHeaders().put(entry.getKey(),
116: (String) entry.getValue());
117: }
118: }
119: }
120: }
121:
122: }
|