View Javadoc

1   /*
2    * Copyright 2005-2010 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.ByteArrayOutputStream;
20  import java.io.FilterOutputStream;
21  import java.io.IOException;
22  import javax.jms.JMSException;
23  import javax.jms.TextMessage;
24  
25  import org.springframework.util.Assert;
26  
27  /**
28   * Writer that wraps a {@link javax.jms.TextMessage}.
29   *
30   * @author Arjen Poutsma
31   * @since 1.5.3
32   */
33  class TextMessageOutputStream extends FilterOutputStream {
34  
35      private final TextMessage message;
36  
37      private final String encoding;
38  
39      TextMessageOutputStream(TextMessage message, String encoding) {
40          super(new ByteArrayOutputStream());
41          Assert.notNull(message, "'message' must not be null");
42          Assert.notNull(encoding, "'encoding' must not be null");
43          this.message = message;
44          this.encoding = encoding;
45      }
46  
47      @Override
48      public void flush() throws IOException {
49          super.flush();
50          try {
51              ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
52              String text = new String(baos.toByteArray(), encoding);
53              message.setText(text);
54          }
55          catch (JMSException ex) {
56              throw new JmsTransportException(ex);
57          }
58      }
59  }