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
21 import org.springframework.ws.WebServiceMessage;
22 import org.springframework.ws.WebServiceMessageFactory;
23
24 /**
25 * Abstract base class for {@link WebServiceConnection} implementations.
26 *
27 * @author Arjen Poutsma
28 * @since 1.0.0
29 */
30 public abstract class AbstractWebServiceConnection implements WebServiceConnection {
31
32 private TransportInputStream tis;
33
34 private TransportOutputStream tos;
35
36 public final void send(WebServiceMessage message) throws IOException {
37 onSendBeforeWrite(message);
38 tos = createTransportOutputStream();
39 if (tos == null) {
40 return;
41 }
42 message.writeTo(tos);
43 tos.flush();
44 onSendAfterWrite(message);
45 }
46
47 /**
48 * Called before the given message has been written to the <code>TransportOutputStream</code>. Called from {@link
49 * #send(WebServiceMessage)}.
50 * <p/>
51 * Default implementation does nothing.
52 *
53 * @param message the message
54 * @throws IOException when an I/O exception occurs
55 */
56 protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
57 }
58
59 /**
60 * Returns a <code>TransportOutputStream</code> for the given message. Called from {@link
61 * #send(WebServiceMessage)}.
62 *
63 * @return the output stream
64 * @throws IOException when an I/O exception occurs
65 */
66 protected abstract TransportOutputStream createTransportOutputStream() throws IOException;
67
68 /**
69 * Called after the given message has been written to the <code>TransportOutputStream</code>. Called from {@link
70 * #send(WebServiceMessage)}.
71 * <p/>
72 * Default implementation does nothing.
73 *
74 * @param message the message
75 * @throws IOException when an I/O exception occurs
76 */
77 protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
78 }
79
80 public final WebServiceMessage receive(WebServiceMessageFactory messageFactory) throws IOException {
81 onReceiveBeforeRead();
82 tis = createTransportInputStream();
83 if (tis == null) {
84 return null;
85 }
86 WebServiceMessage message = messageFactory.createWebServiceMessage(tis);
87 onReceiveAfterRead(message);
88 return message;
89 }
90
91 /**
92 * Called before a message has been read from the <code>TransportInputStream</code>. Called from {@link
93 * #receive(WebServiceMessageFactory)}.
94 * <p/>
95 * Default implementation does nothing.
96 *
97 * @throws IOException when an I/O exception occurs
98 */
99 protected void onReceiveBeforeRead() throws IOException {
100 }
101
102 /**
103 * Returns a <code>TransportInputStream</code>. Called from {@link #receive(WebServiceMessageFactory)}.
104 *
105 * @return the input stream, or <code>null</code> if no response can be read
106 * @throws IOException when an I/O exception occurs
107 */
108 protected abstract TransportInputStream createTransportInputStream() throws IOException;
109
110 /**
111 * Called when the given message has been read from the <code>TransportInputStream</code>. Called from {@link
112 * #receive(WebServiceMessageFactory)}.
113 * <p/>
114 * Default implementation does nothing.
115 *
116 * @param message the message
117 * @throws IOException when an I/O exception occurs
118 */
119 protected void onReceiveAfterRead(WebServiceMessage message) throws IOException {
120 }
121
122 public final void close() throws IOException {
123 IOException ioex = null;
124 if (tis != null) {
125 try {
126 tis.close();
127 }
128 catch (IOException ex) {
129 ioex = ex;
130 }
131 }
132 if (tos != null) {
133 try {
134 tos.close();
135 }
136 catch (IOException ex) {
137 ioex = ex;
138 }
139 }
140 onClose();
141 if (ioex != null) {
142 throw ioex;
143 }
144 }
145
146 /**
147 * Template method invoked from {@link #close()}. Default implementation is empty.
148 *
149 * @throws IOException if an I/O error occurs when closing this connection
150 */
151 protected void onClose() throws IOException {
152 }
153
154 }