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