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 */
017package org.apache.activemq.transport.stomp;
018
019import java.io.IOException;
020import java.util.HashMap;
021import java.util.Map;
022
023import javax.jms.Destination;
024import javax.jms.JMSException;
025import javax.jms.MessageListener;
026import javax.jms.MessageProducer;
027
028import org.apache.activemq.command.ActiveMQDestination;
029import org.apache.activemq.command.ActiveMQMessage;
030import org.apache.activemq.command.Message;
031
032/**
033 * Implementations of this interface are used to map back and forth from Stomp
034 * to ActiveMQ. There are several standard mappings which are semantically the
035 * same, the inner class, Helper, provides functions to copy those properties
036 * from one to the other
037 */
038public interface FrameTranslator {
039    ActiveMQMessage convertFrame(ProtocolConverter converter, StompFrame frame) throws JMSException, ProtocolException;
040
041    StompFrame convertMessage(ProtocolConverter converter, ActiveMQMessage message) throws IOException, JMSException;
042
043    String convertDestination(ProtocolConverter converter, Destination d);
044
045    ActiveMQDestination convertDestination(ProtocolConverter converter, String name) throws ProtocolException;
046
047    /**
048     * Helper class which holds commonly needed functions used when implementing
049     * FrameTranslators
050     */
051    static final class Helper {
052
053        private Helper() {
054        }
055
056        public static void copyStandardHeadersFromMessageToFrame(ProtocolConverter converter, ActiveMQMessage message, StompFrame command, FrameTranslator ft) throws IOException {
057            final Map<String, String> headers = command.getHeaders();
058            headers.put(Stomp.Headers.Message.DESTINATION, ft.convertDestination(converter, message.getDestination()));
059            headers.put(Stomp.Headers.Message.MESSAGE_ID, message.getJMSMessageID());
060
061            if (message.getJMSCorrelationID() != null) {
062                headers.put(Stomp.Headers.Message.CORRELATION_ID, message.getJMSCorrelationID());
063            }
064            headers.put(Stomp.Headers.Message.EXPIRATION_TIME, "" + message.getJMSExpiration());
065
066            if (message.getJMSRedelivered()) {
067                headers.put(Stomp.Headers.Message.REDELIVERED, "true");
068            }
069            headers.put(Stomp.Headers.Message.PRORITY, "" + message.getJMSPriority());
070
071            if (message.getJMSReplyTo() != null) {
072                headers.put(Stomp.Headers.Message.REPLY_TO, ft.convertDestination(converter, message.getJMSReplyTo()));
073            }
074            headers.put(Stomp.Headers.Message.TIMESTAMP, "" + message.getJMSTimestamp());
075
076            if (message.getJMSType() != null) {
077                headers.put(Stomp.Headers.Message.TYPE, message.getJMSType());
078            }
079
080            if (message.getUserID() != null) {
081                headers.put(Stomp.Headers.Message.USERID, message.getUserID());
082            }
083
084            if (message.getOriginalDestination() != null) {
085                headers.put(Stomp.Headers.Message.ORIGINAL_DESTINATION, ft.convertDestination(converter, message.getOriginalDestination()));
086            }
087
088            // now lets add all the message headers
089            final Map<String, Object> properties = message.getProperties();
090            if (properties != null) {
091                for (Map.Entry<String, Object> prop : properties.entrySet()) {
092                    headers.put(prop.getKey(), "" + prop.getValue());
093                }
094            }
095        }
096
097        public static void copyStandardHeadersFromFrameToMessage(ProtocolConverter converter, StompFrame command, ActiveMQMessage msg, FrameTranslator ft) throws ProtocolException, JMSException {
098            final Map<String, String> headers = new HashMap<String, String>(command.getHeaders());
099            final String destination = headers.remove(Stomp.Headers.Send.DESTINATION);
100            msg.setDestination(ft.convertDestination(converter, destination));
101
102            // the standard JMS headers
103            msg.setJMSCorrelationID(headers.remove(Stomp.Headers.Send.CORRELATION_ID));
104
105            Object o = headers.remove(Stomp.Headers.Send.EXPIRATION_TIME);
106            if (o != null) {
107                msg.setJMSExpiration(Long.parseLong((String)o));
108            }
109
110            o = headers.remove(Stomp.Headers.Send.PRIORITY);
111            if (o != null) {
112                msg.setJMSPriority(Integer.parseInt((String)o));
113            } else {
114                msg.setJMSPriority(javax.jms.Message.DEFAULT_PRIORITY);
115            }
116
117            o = headers.remove(Stomp.Headers.Send.TYPE);
118            if (o != null) {
119                msg.setJMSType((String)o);
120            }
121
122            o = headers.remove(Stomp.Headers.Send.REPLY_TO);
123            if (o != null) {
124                msg.setJMSReplyTo(ft.convertDestination(converter, (String)o));
125            }
126
127            o = headers.remove(Stomp.Headers.Send.PERSISTENT);
128            if (o != null) {
129                msg.setPersistent("true".equals(o));
130            }
131
132            // Stomp specific headers
133            headers.remove(Stomp.Headers.RECEIPT_REQUESTED);
134
135            // Since we take the rest of the header and put them in properties which could then
136            // be sent back to a STOMP consumer we need to sanitize anything which could be in
137            // Stomp.Headers.Message and might get passed through to the consumer
138            headers.remove(Stomp.Headers.Message.MESSAGE_ID);
139            headers.remove(Stomp.Headers.Message.TIMESTAMP);
140            headers.remove(Stomp.Headers.Message.REDELIVERED);
141            headers.remove(Stomp.Headers.Message.SUBSCRIPTION);
142            headers.remove(Stomp.Headers.Message.USERID);
143
144            // now the general headers
145            msg.setProperties(headers);
146        }
147    }
148}