View Javadoc

1   /*
2    * Copyright 2007 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.io.OutputStream;
22  import java.util.Iterator;
23  
24  /**
25   * Abstract base class for {@link WebServiceConnection} implementations used for receiving requests.
26   *
27   * @author Arjen Poutsma
28   * @since 1.0.0
29   */
30  public abstract class AbstractReceiverConnection extends AbstractWebServiceConnection {
31  
32      private TransportInputStream requestInputStream;
33  
34      private TransportOutputStream responseOutputStream;
35  
36      protected final TransportInputStream createTransportInputStream() throws IOException {
37          if (requestInputStream == null) {
38              requestInputStream = new RequestTransportInputStream();
39          }
40          return requestInputStream;
41      }
42  
43      protected final TransportOutputStream createTransportOutputStream() throws IOException {
44          if (responseOutputStream == null) {
45              responseOutputStream = new ResponseTransportOutputStream();
46          }
47          return responseOutputStream;
48      }
49  
50      public final void close() throws IOException {
51          try {
52              if (requestInputStream != null) {
53                  requestInputStream.close();
54              }
55          }
56          finally {
57              onClose();
58          }
59      }
60  
61      /**
62       * Template method invoked from {@link #close()}. Default implementation is empty.
63       *
64       * @throws IOException if an I/O error occurs when closing this connection
65       */
66      protected void onClose() throws IOException {
67      }
68  
69      /**
70       * Returns an iteration over all the header names this request contains. Returns an empty <code>Iterator</code> if
71       * there areno headers.
72       */
73      protected abstract Iterator getRequestHeaderNames() throws IOException;
74  
75      /**
76       * Returns an iteration over all the string values of the specified header. Returns an empty <code>Iterator</code>
77       * if there are no headers of the specified name.
78       */
79      protected abstract Iterator getRequestHeaders(String name) throws IOException;
80  
81      /** Returns the input stream to read the response from. */
82      protected abstract InputStream getRequestInputStream() throws IOException;
83  
84      /**
85       * Adds a response header with the given name and value. This method can be called multiple times, to allow for
86       * headers with multiple values.
87       *
88       * @param name  the name of the header
89       * @param value the value of the header
90       */
91      protected abstract void addResponseHeader(String name, String value) throws IOException;
92  
93      /** Returns the output stream to write the request to. */
94      protected abstract OutputStream getResponseOutputStream() throws IOException;
95  
96      /** Implementation of <code>TransportInputStream</code> for receiving-side connections. */
97      private class RequestTransportInputStream extends TransportInputStream {
98  
99          protected InputStream createInputStream() throws IOException {
100             return getRequestInputStream();
101         }
102 
103         public Iterator getHeaderNames() throws IOException {
104             return getRequestHeaderNames();
105         }
106 
107         public Iterator getHeaders(String name) throws IOException {
108             return getRequestHeaders(name);
109         }
110 
111         public void close() throws IOException {
112             // defer close, some SoapMessage implementations (Axis) lazy-initialize the SOAPMessage
113         }
114 
115 
116     }
117 
118     /** Implementation of <code>TransportOutputStream</code> for sending-side connections. */
119     private class ResponseTransportOutputStream extends TransportOutputStream {
120 
121         public void addHeader(String name, String value) throws IOException {
122             addResponseHeader(name, value);
123         }
124 
125         protected OutputStream createOutputStream() throws IOException {
126             return getResponseOutputStream();
127         }
128 
129     }
130 
131 
132 }