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.xmpp;
018
019 import java.util.HashMap;
020 import java.util.Map;
021 import java.util.Set;
022
023 import org.apache.camel.Exchange;
024 import org.apache.camel.impl.DefaultHeaderFilterStrategy;
025 import org.apache.camel.spi.HeaderFilterStrategy;
026 import org.apache.camel.util.ObjectHelper;
027
028 import org.apache.commons.logging.Log;
029 import org.apache.commons.logging.LogFactory;
030
031 import org.jivesoftware.smack.packet.Message;
032
033 /**
034 * A Strategy used to convert between a Camel {@link Exchange} and {@link XmppMessage} to and from a
035 * XMPP {@link Message}
036 *
037 * @version $Revision: 20398 $
038 */
039 public class XmppBinding {
040
041 private static final transient Log LOG = LogFactory.getLog(XmppBinding.class);
042 private HeaderFilterStrategy headerFilterStrategy;
043
044 public XmppBinding() {
045 this.headerFilterStrategy = new DefaultHeaderFilterStrategy();
046 }
047
048 public XmppBinding(HeaderFilterStrategy headerFilterStrategy) {
049 ObjectHelper.notNull(headerFilterStrategy, "headerFilterStrategy");
050 this.headerFilterStrategy = headerFilterStrategy;
051 }
052
053 /**
054 * Populates the given XMPP message from the inbound exchange
055 */
056 public void populateXmppMessage(Message message, Exchange exchange) {
057 message.setBody(exchange.getIn().getBody(String.class));
058
059 Set<Map.Entry<String, Object>> entries = exchange.getIn().getHeaders().entrySet();
060 for (Map.Entry<String, Object> entry : entries) {
061 String name = entry.getKey();
062 Object value = entry.getValue();
063 if (!headerFilterStrategy.applyFilterToCamelHeaders(name, value, exchange)) {
064
065 if ("subject".equalsIgnoreCase(name)) {
066 // special for subject
067 String subject = exchange.getContext().getTypeConverter().convertTo(String.class, value);
068 message.setSubject(subject);
069 } else if ("language".equalsIgnoreCase(name)) {
070 // special for language
071 String language = exchange.getContext().getTypeConverter().convertTo(String.class, value);
072 message.setLanguage(language);
073 } else {
074 try {
075 message.setProperty(name, value);
076 if (LOG.isTraceEnabled()) {
077 LOG.trace("Added property name: " + name + " value: " + value.toString());
078 }
079 } catch (IllegalArgumentException iae) {
080 if (LOG.isDebugEnabled()) {
081 LOG.debug("Cannot add property " + name + " to XMPP message due: ", iae);
082 }
083 }
084 }
085 }
086 }
087
088 String id = exchange.getExchangeId();
089 if (id != null) {
090 message.setProperty("exchangeId", id);
091 }
092 }
093
094 /**
095 * Extracts the body from the XMPP message
096 */
097 public Object extractBodyFromXmpp(Exchange exchange, Message message) {
098 return message.getBody();
099 }
100
101 public Map<String, Object> extractHeadersFromXmpp(Message xmppMessage, Exchange exchange) {
102 Map<String, Object> answer = new HashMap<String, Object>();
103
104 for (String name : xmppMessage.getPropertyNames()) {
105 Object value = xmppMessage.getProperty(name);
106
107 if (!headerFilterStrategy.applyFilterToExternalHeaders(name, value, exchange)) {
108 answer.put(name, value);
109 }
110 }
111
112 return answer;
113 }
114 }