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.util.ArrayList;
020 import java.util.HashMap;
021 import java.util.List;
022 import java.util.Map;
023
024 import org.apache.camel.Exchange;
025 import org.apache.camel.spi.HeaderFilterStrategy;
026 import org.apache.cxf.endpoint.Client;
027 import org.apache.cxf.helpers.CastUtils;
028 import org.apache.cxf.message.Message;
029
030 /**
031 * Utility class to propagate headers to and from CXF message.
032 *
033 * @version $Revision: 15191 $
034 */
035 public final class CxfHeaderHelper {
036
037 /**
038 * Utility class does not have public constructor
039 */
040 private CxfHeaderHelper() {
041 }
042
043 /**
044 * Progagates Camel headers to CXF message.
045 *
046 * @param strategy header filter strategy
047 * @param headers Camel header
048 * @param message CXF meassage
049 * @param exchange provides context for filtering
050 */
051 public static void propagateCamelToCxf(HeaderFilterStrategy strategy,
052 Map<String, Object> headers, Message message, Exchange exchange) {
053
054 Map<String, List<String>> cxfHeaders =
055 CastUtils.cast((Map)message.get(Message.PROTOCOL_HEADERS));
056
057 if (cxfHeaders == null) {
058 cxfHeaders = new HashMap<String, List<String>>();
059 message.put(Message.PROTOCOL_HEADERS, cxfHeaders);
060 }
061
062 for (Map.Entry<String, Object> entry : headers.entrySet()) {
063 if (strategy != null
064 && !strategy.applyFilterToCamelHeaders(entry.getKey(), entry.getValue(), exchange)) {
065
066 if (Exchange.CONTENT_TYPE.equals(entry.getKey())) {
067 message.put(Message.CONTENT_TYPE, entry.getValue());
068 } else if (Client.REQUEST_CONTEXT.equals(entry.getKey())
069 || Client.RESPONSE_CONTEXT.equals(entry.getKey())
070 || Message.RESPONSE_CODE.equals(entry.getKey())) {
071 message.put(entry.getKey(), entry.getValue());
072 } else {
073 List<String> listValue = new ArrayList<String>();
074 listValue.add(entry.getValue().toString());
075 cxfHeaders.put(entry.getKey(), listValue);
076 }
077 }
078 }
079 }
080
081 public static void propagateCxfToCamel(HeaderFilterStrategy strategy,
082 Message message, Map<String, Object> headers, Exchange exchange) {
083
084 if (strategy == null) {
085 return;
086 }
087
088 Map<String, List<String>> cxfHeaders =
089 CastUtils.cast((Map)message.get(Message.PROTOCOL_HEADERS));
090
091 if (cxfHeaders != null) {
092 for (Map.Entry<String, List<String>> entry : cxfHeaders.entrySet()) {
093 if (!strategy.applyFilterToExternalHeaders(entry.getKey(), entry.getValue(), exchange)) {
094 headers.put(entry.getKey(), entry.getValue().get(0));
095 }
096 }
097 }
098
099 // propagate content type
100 String key = Message.CONTENT_TYPE;
101 Object value = message.get(key);
102 if (value != null && !strategy.applyFilterToExternalHeaders(key, value, exchange)) {
103 headers.put(Exchange.CONTENT_TYPE, value);
104 }
105
106 // propagate request context
107 key = Client.REQUEST_CONTEXT;
108 value = message.get(key);
109 if (value != null && !strategy.applyFilterToExternalHeaders(key, value, exchange)) {
110 headers.put(key, value);
111 }
112
113 // propagate response context
114 key = Client.RESPONSE_CONTEXT;
115 value = message.get(key);
116 if (value != null && !strategy.applyFilterToExternalHeaders(key, value, exchange)) {
117 headers.put(key, value);
118 }
119 }
120 }