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.component.mina;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.impl.DefaultExchangeHolder;
021    
022    /**
023     * Helper to get and set the correct payload when transferring data using camel-mina.
024     * Always use this helper instead of direct access on the exchange object.
025     * <p/>
026     * This helper ensures that we can also transfer exchange objects over the wire using the
027     * <tt>transferExchange=true</tt> option.
028     *
029     * @version $Revision: 19199 $
030     */
031    public final class MinaPayloadHelper {
032    
033        private MinaPayloadHelper() {
034            //Utility Class
035        }
036    
037        public static Object getIn(MinaEndpoint endpoint, Exchange exchange) {
038            if (endpoint.getConfiguration().isTransferExchange()) {
039                // we should transfer the entire exchange over the wire (includes in/out)
040                return DefaultExchangeHolder.marshal(exchange);
041            } else {
042                // normal transfer using the body only
043                return exchange.getIn().getBody();
044            }
045        }
046    
047        public static Object getOut(MinaEndpoint endpoint, Exchange exchange) {
048            if (endpoint.getConfiguration().isTransferExchange()) {
049                // we should transfer the entire exchange over the wire (includes in/out)
050                return DefaultExchangeHolder.marshal(exchange);
051            } else {
052                // normal transfer using the body only
053                return exchange.getOut().getBody();
054            }
055        }
056    
057        public static void setIn(Exchange exchange, Object payload) {
058            if (payload instanceof DefaultExchangeHolder) {
059                DefaultExchangeHolder.unmarshal(exchange, (DefaultExchangeHolder) payload);
060            } else {
061                // normal transfer using the body only
062                exchange.getIn().setBody(payload);
063            }
064        }
065    
066        public static void setOut(Exchange exchange, Object payload) {
067            if (payload instanceof DefaultExchangeHolder) {
068                DefaultExchangeHolder.unmarshal(exchange, (DefaultExchangeHolder) payload);
069            } else {
070                // normal transfer using the body only and preserve the headers
071                exchange.getOut().setHeaders(exchange.getIn().getHeaders());
072                exchange.getOut().setBody(payload);
073            }
074        }
075    }