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;
18  
19  import java.io.IOException;
20  import java.io.OutputStream;
21  
22  import org.springframework.util.Assert;
23  
24  /**
25   * A <code>TransportOutputStream</code> is an output stream with MIME input headers. It is used to write [email protected]
26   * org.springframework.ws.WebServiceMessage WebServiceMessages} to a transport.
27   *
28   * @author Arjen Poutsma
29   * @see #addHeader(String,String)
30   * @since 1.0.0
31   */
32  public abstract class TransportOutputStream extends OutputStream {
33  
34      private OutputStream outputStream;
35  
36      protected TransportOutputStream() {
37      }
38  
39      private OutputStream getOutputStream() throws IOException {
40          if (outputStream == null) {
41              outputStream = createOutputStream();
42              Assert.notNull(outputStream, "outputStream must not be null");
43          }
44          return outputStream;
45      }
46  
47      @Override
48      public void close() throws IOException {
49          getOutputStream().close();
50      }
51  
52      @Override
53      public void flush() throws IOException {
54          getOutputStream().flush();
55      }
56  
57      @Override
58      public void write(byte b[]) throws IOException {
59          getOutputStream().write(b);
60      }
61  
62      @Override
63      public void write(byte b[], int off, int len) throws IOException {
64          getOutputStream().write(b, off, len);
65      }
66  
67      @Override
68      public void write(int b) throws IOException {
69          getOutputStream().write(b);
70      }
71  
72      /**
73       * Adds a header with the given name and value. This method can be called multiple times, to allow for headers with
74       * multiple values.
75       *
76       * @param name  the name of the header
77       * @param value the value of the header
78       */
79      public abstract void addHeader(String name, String value) throws IOException;
80  
81      /** Returns the output stream to write to. */
82      protected abstract OutputStream createOutputStream() throws IOException;
83  }