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.InputStream;
21  import java.util.Iterator;
22  
23  import org.springframework.util.Assert;
24  
25  /**
26   * A <code>TransportInputStream</code> is an input stream with MIME input headers. It is used to construct [email protected]
27   * org.springframework.ws.WebServiceMessage WebServiceMessages} from a transport.
28   *
29   * @author Arjen Poutsma
30   * @see #getHeaderNames()
31   * @see #getHeaders(String)
32   * @since 1.0.0
33   */
34  public abstract class TransportInputStream extends InputStream {
35  
36      private InputStream inputStream;
37  
38      protected TransportInputStream() {
39      }
40  
41      private InputStream getInputStream() throws IOException {
42          if (inputStream == null) {
43              inputStream = createInputStream();
44              Assert.notNull(inputStream, "inputStream must not be null");
45          }
46          return inputStream;
47      }
48  
49      @Override
50      public void close() throws IOException {
51          getInputStream().close();
52      }
53  
54      @Override
55      public int available() throws IOException {
56          return getInputStream().available();
57      }
58  
59      @Override
60      public synchronized void mark(int readlimit) {
61          try {
62              getInputStream().mark(readlimit);
63          }
64          catch (IOException e) {
65              // ignored
66          }
67      }
68  
69      @Override
70      public boolean markSupported() {
71          try {
72              return getInputStream().markSupported();
73          }
74          catch (IOException e) {
75              return false;
76          }
77      }
78  
79      @Override
80      public int read(byte b[]) throws IOException {
81          return getInputStream().read(b);
82      }
83  
84      @Override
85      public int read(byte b[], int off, int len) throws IOException {
86          return getInputStream().read(b, off, len);
87      }
88  
89      @Override
90      public synchronized void reset() throws IOException {
91          getInputStream().reset();
92      }
93  
94      @Override
95      public long skip(long n) throws IOException {
96          return getInputStream().skip(n);
97      }
98  
99      @Override
100     public int read() throws IOException {
101         return getInputStream().read();
102     }
103 
104     /** Returns the input stream to read from. */
105     protected abstract InputStream createInputStream() throws IOException;
106 
107     /**
108      * Returns an iteration over all the header names this stream contains. Returns an empty <code>Iterator</code> if
109      * there are no headers.
110      */
111     public abstract Iterator<String> getHeaderNames() throws IOException;
112 
113     /**
114      * Returns an iteration over all the string values of the specified header. Returns an empty <code>Iterator</code>
115      * if there are no headers of the specified name.
116      */
117     public abstract Iterator<String> getHeaders(String name) throws IOException;
118 }