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 public final void send(WebServiceMessage message) throws IOException { 33 onSendBeforeWrite(message); 34 TransportOutputStream tos = createTransportOutputStream(); 35 try { 36 message.writeTo(tos); 37 tos.flush(); 38 } 39 finally { 40 tos.close(); 41 } 42 onSendAfterWrite(message); 43 } 44 45 public final WebServiceMessage receive(WebServiceMessageFactory messageFactory) throws IOException { 46 onReceiveBeforeRead(); 47 TransportInputStream tis = createTransportInputStream(); 48 if (tis == null) { 49 return null; 50 } 51 WebServiceMessage message = null; 52 try { 53 message = messageFactory.createWebServiceMessage(tis); 54 } 55 finally { 56 tis.close(); 57 } 58 onReceiveAfterRead(message); 59 return message; 60 } 61 62 /** 63 * Returns a <code>TransportOutputStream</code> for the given message. Called from {@link 64 * #send(WebServiceMessage)}. 65 * 66 * @return the output stream 67 * @throws IOException when an I/O exception occurs 68 */ 69 protected abstract TransportOutputStream createTransportOutputStream() throws IOException; 70 71 /** 72 * Called before the given message has been written to the <code>TransportOutputStream</code>. Called from {@link 73 * #send(WebServiceMessage)}. 74 * <p/> 75 * Default implementation does nothing. 76 * 77 * @param message the message 78 * @throws IOException when an I/O exception occurs 79 */ 80 protected void onSendBeforeWrite(WebServiceMessage message) throws IOException { 81 } 82 83 /** 84 * Called after the given message has been written to the <code>TransportOutputStream</code>. Called from {@link 85 * #send(WebServiceMessage)}. 86 * <p/> 87 * Default implementation does nothing. 88 * 89 * @param message the message 90 * @throws IOException when an I/O exception occurs 91 */ 92 protected void onSendAfterWrite(WebServiceMessage message) throws IOException { 93 } 94 95 /** 96 * Returns a <code>TransportInputStream</code>. Called from {@link #receive(WebServiceMessageFactory)}. 97 * 98 * @return the input stream, or <code>null</code> if no response can be read 99 * @throws IOException when an I/O exception occurs 100 */ 101 protected abstract TransportInputStream createTransportInputStream() throws IOException; 102 103 /** 104 * Called before a message has been read from the <code>TransportInputStream</code>. Called from {@link 105 * #receive(WebServiceMessageFactory)}. 106 * <p/> 107 * Default implementation does nothing. 108 * 109 * @throws IOException when an I/O exception occurs 110 */ 111 protected void onReceiveBeforeRead() throws IOException { 112 } 113 114 /** 115 * Called when the given message has been read from the <code>TransportInputStream</code>. Called from {@link 116 * #receive(WebServiceMessageFactory)}. 117 * <p/> 118 * Default implementation does nothing. 119 * 120 * @param message the message 121 * @throws IOException when an I/O exception occurs 122 */ 123 protected void onReceiveAfterRead(WebServiceMessage message) throws IOException { 124 } 125 126 }