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.cxf.common.message;
018    
019    import java.io.InputStream;
020    
021    import org.apache.camel.component.cxf.common.header.CxfHeaderHelper;
022    import org.apache.camel.component.cxf.transport.CamelTransportConstants;
023    import org.apache.camel.spi.HeaderFilterStrategy;
024    import org.apache.cxf.message.ExchangeImpl;
025    import org.apache.cxf.message.MessageImpl;
026    
027    public final class CxfMessageHelper {
028    
029        private CxfMessageHelper() {
030            //Helper class
031        }
032        
033        public static org.apache.cxf.message.Message getCxfInMessage(HeaderFilterStrategy headerFilterStrategy,
034                                                                     org.apache.camel.Exchange exchange,
035                                                                     boolean isClient) {
036            MessageImpl answer = new MessageImpl();
037            org.apache.cxf.message.Exchange cxfExchange = exchange
038                .getProperty(CamelTransportConstants.CXF_EXCHANGE, org.apache.cxf.message.Exchange.class);
039            org.apache.camel.Message message;
040            if (isClient && exchange.hasOut()) {
041                message = exchange.getOut();
042            } else {
043                message = exchange.getIn();
044            }
045            assert message != null;
046            if (cxfExchange == null) {
047                cxfExchange = new ExchangeImpl();
048                exchange.setProperty(CamelTransportConstants.CXF_EXCHANGE, cxfExchange);
049            }
050    
051            CxfHeaderHelper.propagateCamelToCxf(headerFilterStrategy, message.getHeaders(), answer, exchange);
052    
053            // body can be empty in case of GET etc.
054            InputStream body = message.getBody(InputStream.class);
055            if (body != null) {
056                answer.setContent(InputStream.class, body);
057            } else if (message.getBody() != null) {
058                // fallback and set the body as what it is
059                answer.setContent(Object.class, body);
060            }
061    
062            answer.putAll(message.getHeaders());
063            answer.setExchange(cxfExchange);
064            cxfExchange.setInMessage(answer);
065            return answer;
066        }
067    
068    }