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 {@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 @Override
37 protected final TransportInputStream createTransportInputStream() throws IOException {
38 if (requestInputStream == null) {
39 requestInputStream = new RequestTransportInputStream();
40 }
41 return requestInputStream;
42 }
43
44 @Override
45 protected final TransportOutputStream createTransportOutputStream() throws IOException {
46 if (responseOutputStream == null) {
47 responseOutputStream = new ResponseTransportOutputStream();
48 }
49 return responseOutputStream;
50 }
51
52 /**
53 * Template method invoked from {@link #close()}. Default implementation is empty.
54 *
55 * @throws IOException if an I/O error occurs when closing this connection
56 */
57 @Override
58 protected void onClose() throws IOException {
59 }
60
61 /**
62 * Returns an iteration over all the header names this request contains. Returns an empty <code>Iterator</code> if
63 * there are no headers.
64 */
65 protected abstract Iterator<String> getRequestHeaderNames() throws IOException;
66
67 /**
68 * Returns an iteration over all the string values of the specified header. Returns an empty <code>Iterator</code>
69 * if there are no headers of the specified name.
70 */
71 protected abstract Iterator<String> getRequestHeaders(String name) throws IOException;
72
73 /** Returns the input stream to read the response from. */
74 protected abstract InputStream getRequestInputStream() throws IOException;
75
76 /**
77 * Adds a response header with the given name and value. This method can be called multiple times, to allow for
78 * headers with multiple values.
79 *
80 * @param name the name of the header
81 * @param value the value of the header
82 */
83 protected abstract void addResponseHeader(String name, String value) throws IOException;
84
85 /** Returns the output stream to write the request to. */
86 protected abstract OutputStream getResponseOutputStream() throws IOException;
87
88 /** Implementation of <code>TransportInputStream</code> for receiving-side connections. */
89 private class RequestTransportInputStream extends TransportInputStream {
90
91 @Override
92 protected InputStream createInputStream() throws IOException {
93 return getRequestInputStream();
94 }
95
96 @Override
97 public Iterator<String> getHeaderNames() throws IOException {
98 return getRequestHeaderNames();
99 }
100
101 @Override
102 public Iterator<String> getHeaders(String name) throws IOException {
103 return getRequestHeaders(name);
104 }
105
106 }
107
108 /** Implementation of <code>TransportOutputStream</code> for sending-side connections. */
109 private class ResponseTransportOutputStream extends TransportOutputStream {
110
111 @Override
112 public void addHeader(String name, String value) throws IOException {
113 addResponseHeader(name, value);
114 }
115
116 @Override
117 protected OutputStream createOutputStream() throws IOException {
118 return getResponseOutputStream();
119 }
120
121 }
122
123
124 }