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 org.apache.activemq.command.ActiveMQDestination;
020 import org.apache.activemq.command.ActiveMQMessage;
021
022 import javax.jms.Destination;
023 import javax.jms.JMSException;
024 import java.io.IOException;
025 import java.util.HashMap;
026 import java.util.Map;
027
028 /**
029 * Implementations of this interface are used to map back and forth from Stomp
030 * to ActiveMQ. There are several standard mappings which are semantically the
031 * same, the inner class, Helper, provides functions to copy those properties
032 * from one to the other
033 */
034 public interface FrameTranslator {
035 ActiveMQMessage convertFrame(ProtocolConverter converter, StompFrame frame) throws JMSException, ProtocolException;
036
037 StompFrame convertMessage(ProtocolConverter converter, ActiveMQMessage message) throws IOException, JMSException;
038
039 String convertDestination(ProtocolConverter converter, Destination d);
040
041 ActiveMQDestination convertDestination(ProtocolConverter converter, String name, boolean forceFallback) throws ProtocolException;
042
043 /**
044 * Helper class which holds commonly needed functions used when implementing
045 * FrameTranslators
046 */
047 static final class Helper {
048
049 private Helper() {
050 }
051
052 public static void copyStandardHeadersFromMessageToFrame(ProtocolConverter converter, ActiveMQMessage message, StompFrame command, FrameTranslator ft) throws IOException {
053 final Map<String, String> headers = command.getHeaders();
054 headers.put(Stomp.Headers.Message.DESTINATION, ft.convertDestination(converter, message.getDestination()));
055 headers.put(Stomp.Headers.Message.MESSAGE_ID, message.getJMSMessageID());
056
057 if (message.getJMSCorrelationID() != null) {
058 headers.put(Stomp.Headers.Message.CORRELATION_ID, message.getJMSCorrelationID());
059 }
060 headers.put(Stomp.Headers.Message.EXPIRATION_TIME, "" + message.getJMSExpiration());
061
062 if (message.getJMSRedelivered()) {
063 headers.put(Stomp.Headers.Message.REDELIVERED, "true");
064 }
065 headers.put(Stomp.Headers.Message.PRORITY, "" + message.getJMSPriority());
066
067 if (message.getJMSReplyTo() != null) {
068 headers.put(Stomp.Headers.Message.REPLY_TO, ft.convertDestination(converter, message.getJMSReplyTo()));
069 }
070 headers.put(Stomp.Headers.Message.TIMESTAMP, "" + message.getJMSTimestamp());
071
072 if (message.getJMSType() != null) {
073 headers.put(Stomp.Headers.Message.TYPE, message.getJMSType());
074 }
075
076 if (message.getUserID() != null) {
077 headers.put(Stomp.Headers.Message.USERID, message.getUserID());
078 }
079
080 if (message.getOriginalDestination() != null) {
081 headers.put(Stomp.Headers.Message.ORIGINAL_DESTINATION, ft.convertDestination(converter, message.getOriginalDestination()));
082 }
083
084 if (message.isPersistent()) {
085 headers.put(Stomp.Headers.Message.PERSISTENT, Stomp.TRUE);
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, true));
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 try {
125 ActiveMQDestination dest = ft.convertDestination(converter, (String)o, false);
126 msg.setJMSReplyTo(dest);
127 } catch (ProtocolException pe) {
128 msg.setStringProperty("reply-to", (String)o);
129 }
130 }
131
132 o = headers.remove(Stomp.Headers.Send.PERSISTENT);
133 if (o != null) {
134 msg.setPersistent("true".equals(o));
135 }
136
137 // Stomp specific headers
138 headers.remove(Stomp.Headers.RECEIPT_REQUESTED);
139
140 // Since we take the rest of the header and put them in properties which could then
141 // be sent back to a STOMP consumer we need to sanitize anything which could be in
142 // Stomp.Headers.Message and might get passed through to the consumer
143 headers.remove(Stomp.Headers.Message.MESSAGE_ID);
144 headers.remove(Stomp.Headers.Message.TIMESTAMP);
145 headers.remove(Stomp.Headers.Message.REDELIVERED);
146 headers.remove(Stomp.Headers.Message.SUBSCRIPTION);
147 headers.remove(Stomp.Headers.Message.USERID);
148
149 // now the general headers
150 msg.setProperties(headers);
151 }
152 }
153 }