View Javadoc

1   /*
2    * Copyright 2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.transport.jms;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.FilterInputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import javax.jms.JMSException;
24  import javax.jms.TextMessage;
25  
26  import org.springframework.util.Assert;
27  
28  /**
29   * Input stream that wraps a [email protected] javax.jms.TextMessage}.
30   *
31   * @author Arjen Poutsma
32   * @since 1.5.3
33   */
34  class TextMessageInputStream extends FilterInputStream {
35  
36      TextMessageInputStream(TextMessage message, String encoding) throws IOException {
37          super(createInputStream(message, encoding));
38      }
39  
40      private static InputStream createInputStream(TextMessage message, String encoding) throws IOException {
41          Assert.notNull(message, "'message' must not be null");
42          Assert.notNull(encoding, "'encoding' must not be null");
43          try {
44              String text = message.getText();
45              byte[] contents = text != null ? text.getBytes(encoding) : new byte[0];
46              return new ByteArrayInputStream(contents);
47          }
48          catch (JMSException ex) {
49              throw new JmsTransportException(ex);
50          }
51      }
52  }