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