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.camel.impl;
018    
019    import java.io.Serializable;
020    import java.util.LinkedHashMap;
021    import java.util.Map;
022    
023    import org.apache.camel.Exchange;
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    
027    /**
028     * Holder object for sending an exchange over a remote wire as a serialized object.
029     * This is usually configured using the <tt>transferExchange=true</tt> option on the endpoint.
030     * <p/>
031     * As opposed to normal usage where only the body part of the exchange is transfered over the wire,
032     * this holder object serializes the following fields over the wire:
033     * <ul>
034     * <li>in body</li>
035     * <li>out body</li>
036     * <li>in headers</li>
037     * <li>out headers</li>
038     * <li>fault body </li>
039     * <li>fault headers</li>
040     * <li>exchange properties</li>
041     * <li>exception</li>
042     * </ul>
043     * Any object that is not serializable will be skipped and Camel will log this at WARN level.
044     *
045     * @version $Revision: 812510 $
046     */
047    public class DefaultExchangeHolder implements Serializable {
048    
049        private static final long serialVersionUID = 1L;
050        private static final transient Log LOG = LogFactory.getLog(DefaultExchangeHolder.class);
051    
052        private Object inBody;
053        private Object outBody;
054        private Boolean outFaultFlag = Boolean.FALSE;
055        private final Map<String, Object> inHeaders = new LinkedHashMap<String, Object>();
056        private final Map<String, Object> outHeaders = new LinkedHashMap<String, Object>();
057        private final Map<String, Object> properties = new LinkedHashMap<String, Object>();
058        private Exception exception;
059    
060        /**
061         * Creates a payload object with the information from the given exchange.
062         * Only marshal the Serializable object
063         *
064         * @param exchange the exchange
065         * @return the holder object with information copied form the exchange
066         */
067        public static DefaultExchangeHolder marshal(Exchange exchange) {
068            DefaultExchangeHolder payload = new DefaultExchangeHolder();
069    
070            payload.inBody = checkSerializableObject("in body", exchange, exchange.getIn().getBody());
071            payload.inHeaders.putAll(checkMapSerializableObjects("in headers", exchange, exchange.getIn().getHeaders()));
072            if (exchange.hasOut()) {
073                payload.outBody = checkSerializableObject("out body", exchange, exchange.getOut().getBody());
074                payload.outHeaders.putAll(checkMapSerializableObjects("out headers", exchange, exchange.getOut().getHeaders()));
075                payload.outFaultFlag = exchange.getOut().isFault();
076            }
077            payload.properties.putAll(checkMapSerializableObjects("exchange properties", exchange, exchange.getProperties()));
078            payload.exception = exchange.getException();
079    
080            return payload;
081        }
082    
083        /**
084         * Transfers the information from the payload to the exchange.
085         *
086         * @param exchange the exchange to set values from the payload
087         * @param payload  the payload with the values
088         */
089        public static void unmarshal(Exchange exchange, DefaultExchangeHolder payload) {
090            exchange.getIn().setBody(payload.inBody);
091            exchange.getIn().setHeaders(payload.inHeaders);
092            if (payload.outBody != null) {
093                exchange.getOut().setBody(payload.outBody);
094                exchange.getOut().setHeaders(payload.outHeaders);
095                exchange.getOut().setFault(payload.outFaultFlag.booleanValue());
096            }
097            for (String key : payload.properties.keySet()) {
098                exchange.setProperty(key, payload.properties.get(key));
099            }
100            exchange.setException(payload.exception);
101        }
102    
103        public String toString() {
104            StringBuilder sb = new StringBuilder("DefaultExchangeHolder[");
105            sb.append("inBody=").append(inBody).append(", outBody=").append(outBody);
106            sb.append(", inHeaders=").append(inHeaders).append(", outHeaders=").append(outHeaders);
107            sb.append(", properties=").append(properties).append(", exception=").append(exception);
108            return sb.append(']').toString();
109        }
110    
111        private static Object checkSerializableObject(String type, Exchange exchange, Object object) {
112            if (object == null) {
113                return null;
114            }
115    
116            Serializable converted = exchange.getContext().getTypeConverter().convertTo(Serializable.class, exchange, object);
117            if (converted != null) {
118                return converted;
119            } else {
120                LOG.warn(type + " containing object: " + object + " of type: " + object.getClass().getCanonicalName() + " cannot be serialized, it will be excluded by the holder.");
121                return null;
122            }
123        }
124    
125        private static Map<String, Object> checkMapSerializableObjects(String type, Exchange exchange, Map<String, Object> map) {
126            if (map == null) {
127                return null;
128            }
129    
130            Map<String, Object> result = new LinkedHashMap<String, Object>();
131            for (Map.Entry<String, Object> entry : map.entrySet()) {
132                Serializable converted = exchange.getContext().getTypeConverter().convertTo(Serializable.class, exchange, entry.getValue());
133                if (converted != null) {
134                    result.put(entry.getKey(), converted);
135                } else {
136                    LOG.warn(type + " containing object: " + entry.getValue() + " with key: " + entry.getKey()
137                            + " cannot be serialized, it will be excluded by the holder.");
138                }
139            }
140    
141            return result;
142        }
143    }