JMSTextMessage.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.client.message;
022 
023 import java.io.UnsupportedEncodingException;
024 import java.nio.charset.CharacterCodingException;
025 import java.nio.charset.Charset;
026 
027 import javax.jms.JMSException;
028 
029 import org.apache.mina.common.ByteBuffer;
030 import org.apache.qpid.AMQException;
031 import org.apache.qpid.client.CustomJMSXProperty;
032 import org.apache.qpid.framing.AMQShortString;
033 import org.apache.qpid.framing.BasicContentHeaderProperties;
034 import org.apache.qpid.util.Strings;
035 
036 public class JMSTextMessage extends AbstractJMSMessage implements javax.jms.TextMessage
037 {
038     private static final String MIME_TYPE = "text/plain";
039 
040     private String _decodedValue;
041 
042     /**
043      * This constant represents the name of a property that is set when the message payload is null.
044      */
045     private static final String PAYLOAD_NULL_PROPERTY = CustomJMSXProperty.JMS_AMQP_NULL.toString();
046     private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
047 
048     public JMSTextMessage(AMQMessageDelegateFactory delegateFactorythrows JMSException
049     {
050         this(delegateFactory, null, null);
051     }
052 
053     JMSTextMessage(AMQMessageDelegateFactory delegateFactory, ByteBuffer data, String encodingthrows JMSException
054     {
055         super(delegateFactory, data)// this instantiates a content header
056         setContentType(getMimeType());
057         setEncoding(encoding);
058     }
059 
060     JMSTextMessage(AMQMessageDelegate delegate, ByteBuffer data)
061             throws AMQException
062     {
063         super(delegate, data);
064         setContentType(getMimeType());
065         _data = data;
066     }
067 
068 
069     public void clearBodyImpl() throws JMSException
070     {
071         if (_data != null)
072         {
073             _data.release();
074             _data = null;
075         }
076 
077         _decodedValue = null;
078     }
079 
080     public String toBodyString() throws JMSException
081     {
082         return getText();
083     }
084 
085     protected String getMimeType()
086     {
087         return MIME_TYPE;
088     }
089 
090     public void setText(String textthrows JMSException
091     {
092         checkWritable();
093 
094         clearBody();
095         try
096         {
097             if (text != null)
098             {
099                 final String encoding = getEncoding();
100                 if (encoding == null || encoding.equalsIgnoreCase("UTF-8"))
101                 {
102                     _data = ByteBuffer.wrap(Strings.toUTF8(text));
103                 }
104                 else
105                 {
106                     _data = ByteBuffer.wrap(text.getBytes(encoding));
107                 }
108                 _data.position(_data.limit());
109                 _changedData=true;
110             }
111             _decodedValue = text;
112         }
113         catch (UnsupportedEncodingException e)
114         {
115             // should never occur
116             JMSException jmse = new JMSException("Unable to decode text data");
117             jmse.setLinkedException(e);
118             throw jmse;
119         }
120     }
121 
122     public String getText() throws JMSException
123     {
124         if (_data == null && _decodedValue == null)
125         {
126             return null;
127         }
128         else if (_decodedValue != null)
129         {
130             return _decodedValue;
131         }
132         else
133         {
134             _data.rewind();
135 
136             if (propertyExists(PAYLOAD_NULL_PROPERTY&& getBooleanProperty(PAYLOAD_NULL_PROPERTY))
137             {
138                 return null;
139             }
140             if (getEncoding() != null)
141             {
142                 try
143                 {
144                     _decodedValue = _data.getString(Charset.forName(getEncoding()).newDecoder());
145                 }
146                 catch (CharacterCodingException e)
147                 {
148                     JMSException je = new JMSException("Could not decode string data: " + e);
149                     je.setLinkedException(e);
150                     throw je;
151                 }
152             }
153             else
154             {
155                 try
156                 {
157                     _decodedValue = _data.getString(DEFAULT_CHARSET.newDecoder());
158                 }
159                 catch (CharacterCodingException e)
160                 {
161                     JMSException je = new JMSException("Could not decode string data: " + e);
162                     je.setLinkedException(e);
163                     throw je;
164                 }
165             }
166             return _decodedValue;
167         }
168     }
169 
170     @Override
171     public void prepareForSending() throws JMSException
172     {
173         super.prepareForSending();
174         if (_data == null)
175         {
176             setBooleanProperty(PAYLOAD_NULL_PROPERTY, true);
177         }
178         else
179         {
180             removeProperty(PAYLOAD_NULL_PROPERTY);
181         }
182     }
183 
184 
185 }