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.MessageChannel;
024    import org.springframework.integration.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 
032     */
033    @Converter
034    public final class SpringIntegrationConverter {
035    
036        private SpringIntegrationConverter() {
037            // Helper class
038        }
039    
040        @Converter
041        public static Endpoint toEndpoint(final MessageChannel channel) throws Exception {
042            Endpoint answer = new SpringIntegrationEndpoint("spring-integration://" + channel.toString(), channel, null);
043            return answer;
044        }
045    
046        @SuppressWarnings("unchecked")
047        @Converter
048        public static org.springframework.integration.Message toSpringMessage(final org.apache.camel.Message camelMessage) throws Exception {
049            if (camelMessage instanceof SpringIntegrationMessage) {
050                SpringIntegrationMessage siMessage = (SpringIntegrationMessage)camelMessage;
051                org.springframework.integration.Message message =  siMessage.getMessage();
052                if (message != null) {
053                    return message;
054                }
055            }
056    
057            // Create a new spring message and copy the attributes and body from the camel message
058            MessageHeaders messageHeaders = new MessageHeaders(camelMessage.getHeaders());
059            return new GenericMessage(camelMessage.getBody(), messageHeaders);
060        }
061    
062        @Converter
063        public static org.apache.camel.Message toCamelMessage(final org.springframework.integration.Message springMessage) throws Exception {
064            return new SpringIntegrationMessage(springMessage);
065        }
066    
067    }