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.cxfbc.interceptors;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import javax.activation.DataHandler;
027: import javax.jbi.messaging.MessageExchange;
028: import javax.jbi.messaging.NormalizedMessage;
029: import javax.xml.namespace.QName;
030:
031: import org.apache.cxf.attachment.AttachmentImpl;
032: import org.apache.cxf.binding.soap.SoapMessage;
033: import org.apache.cxf.headers.Header;
034: import org.apache.cxf.message.Attachment;
035: import org.apache.cxf.message.Message;
036: import org.apache.cxf.phase.AbstractPhaseInterceptor;
037: import org.apache.cxf.phase.Phase;
038: import org.apache.cxf.ws.addressing.AddressingProperties;
039: import org.apache.servicemix.cxfbc.WSAUtils;
040: import org.apache.servicemix.soap.util.QNameUtil;
041:
042: public class JbiOutInterceptor extends
043: AbstractPhaseInterceptor<Message> {
044:
045: public JbiOutInterceptor() {
046: super (Phase.PRE_STREAM);
047: }
048:
049: public void handleMessage(Message message) {
050: MessageExchange me = message.get(MessageExchange.class);
051: NormalizedMessage nm = me.getMessage("in");
052: fromNMSAttachments(message, nm);
053: fromNMSHeaders(message, nm);
054:
055: }
056:
057: /**
058: * Copy NormalizedMessage attachments to SoapMessage attachments
059: */
060: private void fromNMSAttachments(Message message,
061: NormalizedMessage normalizedMessage) {
062: Set attachmentNames = normalizedMessage.getAttachmentNames();
063: Collection<Attachment> attachmentList = new ArrayList<Attachment>();
064: for (Iterator it = attachmentNames.iterator(); it.hasNext();) {
065: String id = (String) it.next();
066: DataHandler handler = normalizedMessage.getAttachment(id);
067: Attachment attachment = new AttachmentImpl(id, handler);
068: attachmentList.add(attachment);
069: }
070: message.setAttachments(attachmentList);
071: message.put(Message.CONTENT_TYPE, "application/soap+xml");
072: }
073:
074: /**
075: * Copy NormalizedMessage headers to SoapMessage headers
076: */
077: @SuppressWarnings("unchecked")
078: private void fromNMSHeaders(Message message,
079: NormalizedMessage normalizedMessage) {
080:
081: if (message instanceof SoapMessage) {
082:
083: Map<String, String> map = (Map<String, String>) normalizedMessage
084: .getProperty(WSAUtils.WSA_HEADERS_INBOUND);
085:
086: if (map != null) {
087: AddressingProperties addressingProperties = WSAUtils
088: .getCXFAddressingPropertiesFromMap(map);
089: ((SoapMessage) message).put(
090: WSAUtils.WSA_HEADERS_INBOUND,
091: addressingProperties);
092: }
093: }
094:
095: if (normalizedMessage
096: .getProperty(JbiConstants.PROTOCOL_HEADERS) != null) {
097: Map<String, ?> headers = (Map<String, ?>) normalizedMessage
098: .getProperty(JbiConstants.PROTOCOL_HEADERS);
099: for (Map.Entry<String, ?> entry : headers.entrySet()) {
100: QName name = QNameUtil.parse(entry.getKey());
101: if (name != null) {
102:
103: Header header = new Header(name, entry.getValue());
104:
105: if (message instanceof SoapMessage) {
106: List<Header> headerList = ((SoapMessage) message)
107: .getHeaders();
108: headerList.add(header);
109: }
110:
111: }
112: }
113: }
114: }
115:
116: }
|