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.command; 018 019 import java.io.DataInputStream; 020 import java.io.DataOutputStream; 021 import java.io.IOException; 022 import java.io.InputStream; 023 import java.io.OutputStream; 024 import java.util.HashMap; 025 import java.util.zip.DeflaterOutputStream; 026 import java.util.zip.InflaterInputStream; 027 028 import javax.jms.JMSException; 029 import javax.jms.MessageNotWriteableException; 030 import javax.jms.TextMessage; 031 032 import org.apache.activemq.ActiveMQConnection; 033 import org.apache.activemq.util.ByteArrayInputStream; 034 import org.apache.activemq.util.ByteArrayOutputStream; 035 import org.apache.activemq.util.ByteSequence; 036 import org.apache.activemq.util.JMSExceptionSupport; 037 import org.apache.activemq.util.MarshallingSupport; 038 import org.apache.activemq.wireformat.WireFormat; 039 040 /** 041 * @openwire:marshaller code="28" 042 * @version $Revision$ 043 */ 044 public class ActiveMQTextMessage extends ActiveMQMessage implements TextMessage { 045 046 public static final byte DATA_STRUCTURE_TYPE = CommandTypes.ACTIVEMQ_TEXT_MESSAGE; 047 048 protected String text; 049 050 public Message copy() { 051 ActiveMQTextMessage copy = new ActiveMQTextMessage(); 052 copy(copy); 053 return copy; 054 } 055 056 private void copy(ActiveMQTextMessage copy) { 057 super.copy(copy); 058 copy.text = text; 059 } 060 061 public byte getDataStructureType() { 062 return DATA_STRUCTURE_TYPE; 063 } 064 065 public String getJMSXMimeType() { 066 return "jms/text-message"; 067 } 068 069 public void setText(String text) throws MessageNotWriteableException { 070 checkReadOnlyBody(); 071 this.text = text; 072 setContent(null); 073 } 074 075 public String getText() throws JMSException { 076 if (text == null && getContent() != null) { 077 InputStream is = null; 078 try { 079 ByteSequence bodyAsBytes = getContent(); 080 if (bodyAsBytes != null) { 081 is = new ByteArrayInputStream(bodyAsBytes); 082 if (isCompressed()) { 083 is = new InflaterInputStream(is); 084 } 085 DataInputStream dataIn = new DataInputStream(is); 086 text = MarshallingSupport.readUTF8(dataIn); 087 dataIn.close(); 088 setContent(null); 089 } 090 } catch (IOException ioe) { 091 throw JMSExceptionSupport.create(ioe); 092 } finally { 093 if (is != null) { 094 try { 095 is.close(); 096 } catch (IOException e) { 097 // ignore 098 } 099 } 100 } 101 } 102 return text; 103 } 104 105 public void beforeMarshall(WireFormat wireFormat) throws IOException { 106 super.beforeMarshall(wireFormat); 107 108 ByteSequence content = getContent(); 109 if (content == null && text != null) { 110 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); 111 OutputStream os = bytesOut; 112 ActiveMQConnection connection = getConnection(); 113 if (connection != null && connection.isUseCompression()) { 114 compressed = true; 115 os = new DeflaterOutputStream(os); 116 } 117 DataOutputStream dataOut = new DataOutputStream(os); 118 MarshallingSupport.writeUTF8(dataOut, this.text); 119 dataOut.close(); 120 setContent(bytesOut.toByteSequence()); 121 //see https://issues.apache.org/activemq/browse/AMQ-2103 122 this.text=null; 123 } 124 } 125 126 /** 127 * Clears out the message body. Clearing a message's body does not clear its 128 * header values or property entries. <p/> 129 * <P> 130 * If this message body was read-only, calling this method leaves the 131 * message body in the same state as an empty body in a newly created 132 * message. 133 * 134 * @throws JMSException if the JMS provider fails to clear the message body 135 * due to some internal error. 136 */ 137 public void clearBody() throws JMSException { 138 super.clearBody(); 139 this.text = null; 140 } 141 142 public int getSize() { 143 if (size == 0 && content == null && text != null) { 144 size = getMinimumMessageSize(); 145 if (marshalledProperties != null) { 146 size += marshalledProperties.getLength(); 147 } 148 size = text.length() * 2; 149 } 150 return super.getSize(); 151 } 152 153 public String toString() { 154 try { 155 String text = getText(); 156 if (text != null && text.length() > 63) { 157 text = text.substring(0, 45) + "..." + text.substring(text.length() - 12); 158 HashMap<String, Object> overrideFields = new HashMap<String, Object>(); 159 overrideFields.put("text", text); 160 return super.toString(overrideFields); 161 } 162 } catch (JMSException e) { 163 } 164 return super.toString(); 165 } 166 }