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.spring.integration.converter;
018    
019    import org.apache.camel.Converter;
020    import org.apache.camel.Endpoint;
021    import org.apache.camel.component.spring.integration.SpringIntegrationEndpoint;
022    import org.apache.camel.component.spring.integration.SpringIntegrationMessage;
023    import org.springframework.integration.core.MessageChannel;
024    import org.springframework.integration.core.MessageHeaders;
025    import org.springframework.integration.message.GenericMessage;
026    
027    /**
028     * The <a href="http://camel.apache.org/type-converter.html">Type Converters</a>
029     * for turning the Spring Integration types into Camel native type.
030     *
031     * @version $Revision: 738604 $
032     */
033    @Converter
034    public final class SpringIntegrationConverter {
035    
036        private SpringIntegrationConverter() {
037            // Helper class
038        }
039    
040        /**
041         * @param Spring Integration MessageChannel
042         * @return an Camel Endpoint
043         * @throws Exception
044         */
045        @Converter
046        public static Endpoint toEndpoint(final MessageChannel channel) throws Exception {
047            if (channel == null) {
048                throw new IllegalArgumentException("The MessageChannel is null");
049            }
050            Endpoint answer = new SpringIntegrationEndpoint("URL", channel, null);
051            // check the channel
052            return answer;
053        }
054    
055    
056        @SuppressWarnings("unchecked")
057        @Converter
058        public static org.springframework.integration.core.Message toSpringMessage(final org.apache.camel.Message camelMessage) throws Exception {
059            if (camelMessage instanceof SpringIntegrationMessage) {
060                SpringIntegrationMessage siMessage = (SpringIntegrationMessage)camelMessage;
061                org.springframework.integration.core.Message message =  siMessage.getMessage();
062                if (message != null) {
063                    return message;
064                }
065            }
066    
067            // Create a new spring message and copy the attributes and body from the camel message
068            MessageHeaders messageHeaders = new MessageHeaders(camelMessage.getHeaders());
069            return new GenericMessage(camelMessage.getBody(), messageHeaders);
070        }
071    
072        @Converter
073        public static org.apache.camel.Message toCamelMessage(final org.springframework.integration.core.Message springMessage) throws Exception {
074            return new SpringIntegrationMessage(springMessage);
075        }
076    
077    
078    
079    }