View Javadoc

1   /*
2    * Copyright 2006 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 {@link
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      public void close() throws IOException {
48          getOutputStream().close();
49      }
50  
51      public void flush() throws IOException {
52          getOutputStream().flush();
53      }
54  
55      public void write(byte b[]) throws IOException {
56          getOutputStream().write(b);
57      }
58  
59      public void write(byte b[], int off, int len) throws IOException {
60          getOutputStream().write(b, off, len);
61      }
62  
63      public void write(int b) throws IOException {
64          getOutputStream().write(b);
65      }
66  
67      /**
68       * Adds a header with the given name and value. This method can be called multiple times, to allow for headers with
69       * multiple values.
70       *
71       * @param name  the name of the header
72       * @param value the value of the header
73       */
74      public abstract void addHeader(String name, String value) throws IOException;
75  
76      /** Returns the output stream to write to. */
77      protected abstract OutputStream createOutputStream() throws IOException;
78  }