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.util;
018    
019    import java.io.InputStream;
020    
021    import javax.xml.transform.Source;
022    
023    import org.apache.camel.InvalidPayloadException;
024    import org.apache.camel.component.cxf.CxfConstants;
025    import org.apache.camel.spi.HeaderFilterStrategy;
026    import org.apache.cxf.message.ExchangeImpl;
027    import org.apache.cxf.message.MessageImpl;
028    
029    public final class CxfMessageHelper {
030        private CxfMessageHelper() {
031            //Helper class
032        }
033        
034        public static org.apache.cxf.message.Message getCxfInMessage(HeaderFilterStrategy headerFilterStrategy,
035                                                                     org.apache.camel.Exchange exchange,
036                                                                     boolean isClient) {
037            MessageImpl answer = new MessageImpl();
038            org.apache.cxf.message.Exchange cxfExchange = exchange
039                .getProperty(CxfConstants.CXF_EXCHANGE, org.apache.cxf.message.Exchange.class);
040            org.apache.camel.Message message;
041            if (isClient) {
042                message = exchange.getOut();
043            } else {
044                message = exchange.getIn();
045            }
046            assert message != null;
047            if (cxfExchange == null) {
048                cxfExchange = new ExchangeImpl();
049                exchange.setProperty(CxfConstants.CXF_EXCHANGE, cxfExchange);
050            }
051    
052            CxfHeaderHelper.propagateCamelToCxf(headerFilterStrategy, message.getHeaders(), answer, exchange);
053    
054            // body can be empty in case of GET etc.
055            InputStream body = message.getBody(InputStream.class);
056            if (body != null) {
057                answer.setContent(InputStream.class, body);
058            } else if (message.getBody() != null) {
059                // fallback and set the body as what it is
060                answer.setContent(Object.class, body);
061            }
062    
063            answer.putAll(message.getHeaders());
064            answer.setExchange(cxfExchange);
065            cxfExchange.setInMessage(answer);
066            return answer;
067        }
068    
069        public static org.apache.cxf.message.Message getCxfOutMessage(HeaderFilterStrategy headerFilterStrategy,
070                                                                      org.apache.camel.Exchange exchange,
071                                                                      boolean isClient)
072            throws InvalidPayloadException {
073            org.apache.cxf.message.Exchange cxfExchange = exchange
074                .getProperty(CxfConstants.CXF_EXCHANGE, org.apache.cxf.message.Exchange.class);
075            assert cxfExchange != null;
076            org.apache.cxf.endpoint.Endpoint cxfEndpoint = cxfExchange
077                .get(org.apache.cxf.endpoint.Endpoint.class);
078            org.apache.cxf.message.Message outMessage = cxfEndpoint.getBinding().createMessage();
079            outMessage.setExchange(cxfExchange);
080            cxfExchange.setOutMessage(outMessage);
081    
082            org.apache.camel.Message message;
083            if (isClient) {
084                message = exchange.getIn();
085            } else {
086                message = exchange.getOut();
087            }
088    
089            CxfHeaderHelper.propagateCamelToCxf(headerFilterStrategy, message.getHeaders(), outMessage, exchange);
090    
091            // send the body back
092            Source body = message.getMandatoryBody(Source.class);
093            outMessage.setContent(Source.class, body);
094            outMessage.putAll(message.getHeaders());
095            return outMessage;
096        }
097    
098    }