001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.transport;
018:
019: import java.io.IOException;
020:
021: import org.springframework.ws.WebServiceMessage;
022: import org.springframework.ws.WebServiceMessageFactory;
023:
024: /**
025: * Abstract base class for {@link WebServiceConnection} implementations.
026: *
027: * @author Arjen Poutsma
028: * @since 1.0.0
029: */
030: public abstract class AbstractWebServiceConnection implements
031: WebServiceConnection {
032:
033: public final void send(WebServiceMessage message)
034: throws IOException {
035: onSendBeforeWrite(message);
036: TransportOutputStream tos = createTransportOutputStream();
037: try {
038: message.writeTo(tos);
039: tos.flush();
040: } finally {
041: tos.close();
042: }
043: onSendAfterWrite(message);
044: }
045:
046: public final WebServiceMessage receive(
047: WebServiceMessageFactory messageFactory) throws IOException {
048: onReceiveBeforeRead();
049: TransportInputStream tis = createTransportInputStream();
050: if (tis == null) {
051: return null;
052: }
053: WebServiceMessage message = null;
054: try {
055: message = messageFactory.createWebServiceMessage(tis);
056: } finally {
057: tis.close();
058: }
059: onReceiveAfterRead(message);
060: return message;
061: }
062:
063: /**
064: * Returns a <code>TransportOutputStream</code> for the given message. Called from {@link
065: * #send(WebServiceMessage)}.
066: *
067: * @return the output stream
068: * @throws IOException when an I/O exception occurs
069: */
070: protected abstract TransportOutputStream createTransportOutputStream()
071: throws IOException;
072:
073: /**
074: * Called before the given message has been written to the <code>TransportOutputStream</code>. Called from {@link
075: * #send(WebServiceMessage)}.
076: * <p/>
077: * Default implementation does nothing.
078: *
079: * @param message the message
080: * @throws IOException when an I/O exception occurs
081: */
082: protected void onSendBeforeWrite(WebServiceMessage message)
083: throws IOException {
084: }
085:
086: /**
087: * Called after the given message has been written to the <code>TransportOutputStream</code>. Called from {@link
088: * #send(WebServiceMessage)}.
089: * <p/>
090: * Default implementation does nothing.
091: *
092: * @param message the message
093: * @throws IOException when an I/O exception occurs
094: */
095: protected void onSendAfterWrite(WebServiceMessage message)
096: throws IOException {
097: }
098:
099: /**
100: * Returns a <code>TransportInputStream</code>. Called from {@link #receive(WebServiceMessageFactory)}.
101: *
102: * @return the input stream, or <code>null</code> if no response can be read
103: * @throws IOException when an I/O exception occurs
104: */
105: protected abstract TransportInputStream createTransportInputStream()
106: throws IOException;
107:
108: /**
109: * Called before a message has been read from the <code>TransportInputStream</code>. Called from {@link
110: * #receive(WebServiceMessageFactory)}.
111: * <p/>
112: * Default implementation does nothing.
113: *
114: * @throws IOException when an I/O exception occurs
115: */
116: protected void onReceiveBeforeRead() throws IOException {
117: }
118:
119: /**
120: * Called when the given message has been read from the <code>TransportInputStream</code>. Called from {@link
121: * #receive(WebServiceMessageFactory)}.
122: * <p/>
123: * Default implementation does nothing.
124: *
125: * @param message the message
126: * @throws IOException when an I/O exception occurs
127: */
128: protected void onReceiveAfterRead(WebServiceMessage message)
129: throws IOException {
130: }
131:
132: }
|