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.jivesoftware.smack.packet.Message;
027    
028    /**
029     * A Strategy used to convert between a Camel {@link XmppExchange} and {@link XmppMessage} to and from a
030     * XMPP {@link Message}
031     *
032     * @version $Revision: 776202 $
033     */
034    public class XmppBinding {
035    
036        private HeaderFilterStrategy headerFilterStrategy;
037    
038        public XmppBinding() {
039            this.headerFilterStrategy = new DefaultHeaderFilterStrategy();
040        }
041    
042        public XmppBinding(HeaderFilterStrategy headerFilterStrategy) {
043            this.headerFilterStrategy = headerFilterStrategy;
044        }
045    
046        /**
047         * Populates the given XMPP message from the inbound exchange
048         */
049        public void populateXmppMessage(Message message, Exchange exchange) {
050            message.setBody(exchange.getIn().getBody(String.class));
051    
052            Set<Map.Entry<String, Object>> entries = exchange.getIn().getHeaders().entrySet();
053            for (Map.Entry<String, Object> entry : entries) {
054                String name = entry.getKey();
055                Object value = entry.getValue();
056                // BUG?
057                if (headerFilterStrategy != null
058                        && !headerFilterStrategy.applyFilterToCamelHeaders(name, value)) {
059    
060                    if ("subject".equalsIgnoreCase(name)) {
061                        // special for subject
062                        String subject = exchange.getContext().getTypeConverter().convertTo(String.class, value);
063                        message.setSubject(subject);
064                    } else if ("language".equalsIgnoreCase(name)) {
065                        // special for language
066                        String language = exchange.getContext().getTypeConverter().convertTo(String.class, value);
067                        message.setLanguage(language);
068                    } else {
069                        message.setProperty(name, value);
070                    }
071                }
072            }
073            String id = exchange.getExchangeId();
074            if (id != null) {
075                message.setProperty("exchangeId", id);
076            }
077        }
078    
079        /**
080         * Extracts the body from the XMPP message
081         */
082        public Object extractBodyFromXmpp(XmppExchange exchange, Message message) {
083            return message.getBody();
084        }
085    
086        public Map<String, Object> extractHeadersFromXmpp(Message xmppMessage) {
087            Map<String, Object> answer = new HashMap<String, Object>();
088    
089            for (String name : xmppMessage.getPropertyNames()) {
090                Object value = xmppMessage.getProperty(name);
091    
092                if (headerFilterStrategy != null
093                        && !headerFilterStrategy.applyFilterToExternalHeaders(name, value)) {
094                    answer.put(name, value);
095                }
096            }
097            return answer;
098        }
099    }