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