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; 018 019 import org.apache.cxf.message.Message; 020 import org.apache.cxf.message.MessageImpl; 021 022 import java.io.InputStream; 023 import java.util.Set; 024 025 /** 026 * The binding of how Camel messages get mapped to Apache CXF and back again 027 * 028 * @version $Revision: 563822 $ 029 */ 030 public class CxfBinding { 031 public Object extractBodyFromCxf(CxfExchange exchange, Message message) { 032 // TODO how do we choose a format? 033 return getBody(message); 034 } 035 036 protected Object getBody(Message message) { 037 Set<Class<?>> contentFormats = message.getContentFormats(); 038 for (Class<?> contentFormat : contentFormats) { 039 Object answer = message.getContent(contentFormat); 040 if (answer != null) { 041 return answer; 042 } 043 } 044 return null; 045 } 046 047 public MessageImpl createCxfMessage(CxfExchange exchange) { 048 MessageImpl answer = (MessageImpl) exchange.getInMessage(); 049 050 // CXF uses the stax which is based on the stream API to parser the XML, so 051 // the CXF transport is also based on the stream API. 052 // And the interceptors are also based on the stream API, 053 // so lets use an InputStream to host the CXF on wire message. 054 CxfMessage in = exchange.getIn(); 055 Object body = in.getBody(InputStream.class); 056 if (body == null) { 057 body = in.getBody(); 058 } 059 answer.setContent(InputStream.class, body); 060 061 // no need to process headers as we reuse the CXF message 062 /* 063 // set the headers 064 Set<Map.Entry<String, Object>> entries = in.getHeaders().entrySet(); 065 for (Map.Entry<String, Object> entry : entries) { 066 answer.put(entry.getKey(), entry.getValue()); 067 } 068 */ 069 return answer; 070 } 071 072 public void storeCxfResponse(CxfExchange exchange, Message response) { 073 // no need to process headers as we use the CXF message 074 CxfMessage out = exchange.getOut(); 075 if (response != null) { 076 out.setMessage(response); 077 out.setBody(getBody(response)); 078 } 079 } 080 081 public void storeCxfResponse(CxfExchange exchange, Object response) { 082 CxfMessage out = exchange.getOut(); 083 if (response != null) { 084 out.setBody(response); 085 } 086 } 087 }