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