001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.cxf;
019    
020    import org.apache.cxf.message.Message;
021    import org.apache.cxf.message.MessageImpl;
022    
023    import java.io.InputStream;
024    import java.util.Set;
025    
026    /**
027     * The binding of how Camel messages get mapped to Apache CXF and back again
028     *
029     * @version $Revision: 525898 $
030     */
031    public class CxfBinding {
032    
033        public Object extractBodyFromCxf(CxfExchange exchange, Message message) {
034            //  TODO how do we choose a format?
035            return getBody(message);
036        }
037    
038        protected Object getBody(Message message) {
039            Set<Class<?>> contentFormats = message.getContentFormats();
040            for (Class<?> contentFormat : contentFormats) {
041                Object answer = message.getContent(contentFormat);
042                if (answer != null) {
043                    return answer;
044                }
045            }
046            return null;
047        }
048    
049        public MessageImpl createCxfMessage(CxfExchange exchange) {
050            MessageImpl answer = (MessageImpl) exchange.getInMessage();
051    
052            // TODO is InputStream the best type to give to CXF?
053            CxfMessage in = exchange.getIn();
054            Object body = in.getBody(InputStream.class);
055            if (body == null) {
056                body = in.getBody();
057            }
058            answer.setContent(InputStream.class, body);
059    
060            // no need to process headers as we reuse the CXF message
061            /*
062            // set the headers
063            Set<Map.Entry<String, Object>> entries = in.getHeaders().entrySet();
064            for (Map.Entry<String, Object> entry : entries) {
065                answer.put(entry.getKey(), entry.getValue());
066            }
067            */
068            return answer;
069        }
070    
071        public void storeCxfResponse(CxfExchange exchange, Message response) {
072            // no need to process headers as we use the CXF message
073            CxfMessage out = exchange.getOut();
074            if (response != null) {
075                out.setMessage(response);
076                out.setBody(getBody(response));
077            }
078        }
079    
080        public void storeCxfResponse(CxfExchange exchange, Object response) {
081            CxfMessage out = exchange.getOut();
082            if (response != null) {
083                out.setBody(response);
084            }
085        }
086    }