SWS-894 - Introduce interface to offer WebServiceConnection headers

* Created an interface to mark where request and response headers may be accessed
* Updated related classes to use the interface, and in turn, update method visibility
* There are sets of classes aimed at requests and another at repsonses. Have interfaces to serve both, for symmetry.
This commit is contained in:
Greg Turnquist
2015-04-20 13:50:41 -05:00
parent b8e4b806fa
commit 4b179e84e3
16 changed files with 152 additions and 80 deletions

View File

@@ -25,9 +25,11 @@ import java.util.Iterator;
* Abstract base class for {@link WebServiceConnection} implementations used for receiving requests.
*
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 1.0.0
*/
public abstract class AbstractReceiverConnection extends AbstractWebServiceConnection {
public abstract class AbstractReceiverConnection extends AbstractWebServiceConnection implements
HeadersAwareReceiverWebServiceConnection {
private TransportInputStream requestInputStream;
@@ -58,30 +60,9 @@ public abstract class AbstractReceiverConnection extends AbstractWebServiceConne
protected void onClose() throws IOException {
}
/**
* Returns an iteration over all the header names this request contains. Returns an empty {@code Iterator} if
* there are no headers.
*/
protected abstract Iterator<String> getRequestHeaderNames() throws IOException;
/**
* Returns an iteration over all the string values of the specified header. Returns an empty {@code Iterator}
* if there are no headers of the specified name.
*/
protected abstract Iterator<String> getRequestHeaders(String name) throws IOException;
/** Returns the input stream to read the response from. */
protected abstract InputStream getRequestInputStream() throws IOException;
/**
* Adds a response header with the given name and value. This method can be called multiple times, to allow for
* headers with multiple values.
*
* @param name the name of the header
* @param value the value of the header
*/
protected abstract void addResponseHeader(String name, String value) throws IOException;
/** Returns the output stream to write the request to. */
protected abstract OutputStream getResponseOutputStream() throws IOException;

View File

@@ -25,9 +25,11 @@ import java.util.Iterator;
* Abstract base class for {@link WebServiceConnection} implementations used for sending requests.
*
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 1.0.0
*/
public abstract class AbstractSenderConnection extends AbstractWebServiceConnection {
public abstract class AbstractSenderConnection extends AbstractWebServiceConnection implements
HeadersAwareSenderWebServiceConnection {
private TransportOutputStream requestOutputStream;
@@ -66,30 +68,9 @@ public abstract class AbstractSenderConnection extends AbstractWebServiceConnect
/** Indicates whether this connection has a response. */
protected abstract boolean hasResponse() throws IOException;
/**
* Adds a request header with the given name and value. This method can be called multiple times, to allow for
* headers with multiple values.
*
* @param name the name of the header
* @param value the value of the header
*/
protected abstract void addRequestHeader(String name, String value) throws IOException;
/** Returns the output stream to write the request to. */
protected abstract OutputStream getRequestOutputStream() throws IOException;
/**
* Returns an iteration over all the header names this request contains. Returns an empty {@code Iterator} if
* there are no headers.
*/
protected abstract Iterator<String> getResponseHeaderNames() throws IOException;
/**
* Returns an iteration over all the string values of the specified header. Returns an empty {@code Iterator}
* if there are no headers of the specified name.
*/
protected abstract Iterator<String> getResponseHeaders(String name) throws IOException;
/** Returns the input stream to read the response from. */
protected abstract InputStream getResponseInputStream() throws IOException;

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2005-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.transport;
import java.io.IOException;
import java.util.Iterator;
/**
* Interface to define access to header information for certain {@link WebServiceConnection} implementations.
*
* @author Greg Turnquist
* @since 2.3
*/
public interface HeadersAwareReceiverWebServiceConnection {
/**
* Returns an iteration over all the header names this request contains. Returns an empty {@code Iterator} if
* there are no headers.
*/
Iterator<String> getRequestHeaderNames() throws IOException;
/**
* Returns an iteration over all the string values of the specified header. Returns an empty {@code Iterator}
* if there are no headers of the specified name.
*/
Iterator<String> getRequestHeaders(String name) throws IOException;
/**
* Adds a response header with the given name and value. This method can be called multiple times, to allow for
* headers with multiple values.
*
* @param name the name of the header
* @param value the value of the header
*/
void addResponseHeader(String name, String value) throws IOException;
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2005-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.transport;
import java.io.IOException;
import java.util.Iterator;
/**
* @author Greg Turnquist
* @since 2.3
*/
public interface HeadersAwareSenderWebServiceConnection {
/**
* Returns an iteration over all the header names this request contains. Returns an empty {@code Iterator} if
* there are no headers.
*/
Iterator<String> getResponseHeaderNames() throws IOException;
/**
* Returns an iteration over all the string values of the specified header. Returns an empty {@code Iterator}
* if there are no headers of the specified name.
*/
Iterator<String> getResponseHeaders(String name) throws IOException;
/**
* Adds a request header with the given name and value. This method can be called multiple times, to allow for
* headers with multiple values.
*
* @param name the name of the header
* @param value the value of the header
*/
void addRequestHeader(String name, String value) throws IOException;
}

View File

@@ -36,6 +36,7 @@ import org.springframework.ws.WebServiceMessage;
*
* @author Krzysztof Trojan
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 2.2
*/
public class ClientHttpRequestConnection extends AbstractHttpSenderConnection {
@@ -67,7 +68,7 @@ public class ClientHttpRequestConnection extends AbstractHttpSenderConnection {
// Sending request
@Override
protected void addRequestHeader(String name, String value) throws IOException {
public void addRequestHeader(String name, String value) throws IOException {
request.getHeaders().add(name, value);
}
@@ -89,12 +90,12 @@ public class ClientHttpRequestConnection extends AbstractHttpSenderConnection {
}
@Override
protected Iterator<String> getResponseHeaderNames() throws IOException {
public Iterator<String> getResponseHeaderNames() throws IOException {
return response.getHeaders().keySet().iterator();
}
@Override
protected Iterator<String> getResponseHeaders(String name) throws IOException {
public Iterator<String> getResponseHeaders(String name) throws IOException {
List<String> headers = response.getHeaders().get(name);
return headers != null ? headers.iterator() :
Collections.<String>emptyList().iterator();

View File

@@ -41,6 +41,7 @@ import org.springframework.ws.transport.WebServiceConnection;
* PostMethod}.
*
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 1.0.0
* @deprecated In favor of {@link HttpComponentsConnection}
*/
@@ -98,7 +99,7 @@ public class CommonsHttpConnection extends AbstractHttpSenderConnection {
}
@Override
protected void addRequestHeader(String name, String value) throws IOException {
public void addRequestHeader(String name, String value) throws IOException {
postMethod.addRequestHeader(name, value);
}
@@ -151,7 +152,7 @@ public class CommonsHttpConnection extends AbstractHttpSenderConnection {
}
@Override
protected Iterator<String> getResponseHeaderNames() throws IOException {
public Iterator<String> getResponseHeaderNames() throws IOException {
Header[] headers = postMethod.getResponseHeaders();
String[] names = new String[headers.length];
for (int i = 0; i < headers.length; i++) {
@@ -161,7 +162,7 @@ public class CommonsHttpConnection extends AbstractHttpSenderConnection {
}
@Override
protected Iterator<String> getResponseHeaders(String name) throws IOException {
public Iterator<String> getResponseHeaders(String name) throws IOException {
Header[] headers = postMethod.getResponseHeaders(name);
String[] values = new String[headers.length];
for (int i = 0; i < headers.length; i++) {

View File

@@ -45,6 +45,7 @@ import org.springframework.ws.transport.WebServiceConnection;
* @author Alan Stewart
* @author Barry Pitman
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 2.1.0
*/
public class HttpComponentsConnection extends AbstractHttpSenderConnection {
@@ -100,7 +101,7 @@ public class HttpComponentsConnection extends AbstractHttpSenderConnection {
}
@Override
protected void addRequestHeader(String name, String value) throws IOException {
public void addRequestHeader(String name, String value) throws IOException {
httpPost.addHeader(name, value);
}
@@ -154,7 +155,7 @@ public class HttpComponentsConnection extends AbstractHttpSenderConnection {
}
@Override
protected Iterator<String> getResponseHeaderNames() throws IOException {
public Iterator<String> getResponseHeaderNames() throws IOException {
Header[] headers = httpResponse.getAllHeaders();
String[] names = new String[headers.length];
for (int i = 0; i < headers.length; i++) {
@@ -164,7 +165,7 @@ public class HttpComponentsConnection extends AbstractHttpSenderConnection {
}
@Override
protected Iterator<String> getResponseHeaders(String name) throws IOException {
public Iterator<String> getResponseHeaders(String name) throws IOException {
Header[] headers = httpResponse.getHeaders(name);
String[] values = new String[headers.length];
for (int i = 0; i < headers.length; i++) {

View File

@@ -38,6 +38,7 @@ import org.springframework.ws.transport.support.EnumerationIterator;
* Implementation of {@link WebServiceConnection} that is based on the Servlet API.
*
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 1.0.0
*/
public class HttpServletConnection extends AbstractReceiverConnection
@@ -105,13 +106,13 @@ public class HttpServletConnection extends AbstractReceiverConnection
@Override
@SuppressWarnings("unchecked")
protected Iterator<String> getRequestHeaderNames() throws IOException {
public Iterator<String> getRequestHeaderNames() throws IOException {
return new EnumerationIterator<String>(getHttpServletRequest().getHeaderNames());
}
@Override
@SuppressWarnings("unchecked")
protected Iterator<String> getRequestHeaders(String name) throws IOException {
public Iterator<String> getRequestHeaders(String name) throws IOException {
return new EnumerationIterator<String>(getHttpServletRequest().getHeaders(name));
}
@@ -125,7 +126,7 @@ public class HttpServletConnection extends AbstractReceiverConnection
*/
@Override
protected void addResponseHeader(String name, String value) throws IOException {
public void addResponseHeader(String name, String value) throws IOException {
getHttpServletResponse().addHeader(name, value);
}

View File

@@ -37,6 +37,7 @@ import org.springframework.ws.transport.WebServiceConnection;
* Implementation of the {@link WebServiceConnection} interface that uses a {@link HttpURLConnection}.
*
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 1.0.0
*/
public class HttpUrlConnection extends AbstractHttpSenderConnection {
@@ -76,7 +77,7 @@ public class HttpUrlConnection extends AbstractHttpSenderConnection {
*/
@Override
protected void addRequestHeader(String name, String value) throws IOException {
public void addRequestHeader(String name, String value) throws IOException {
connection.addRequestProperty(name, value);
}
@@ -100,7 +101,7 @@ public class HttpUrlConnection extends AbstractHttpSenderConnection {
}
@Override
protected Iterator<String> getResponseHeaderNames() throws IOException {
public Iterator<String> getResponseHeaderNames() throws IOException {
List<String> headerNames = new ArrayList<String>();
// Header field 0 is the status line, so we start at 1
int i = 1;
@@ -116,7 +117,7 @@ public class HttpUrlConnection extends AbstractHttpSenderConnection {
}
@Override
protected Iterator<String> getResponseHeaders(String name) throws IOException {
public Iterator<String> getResponseHeaders(String name) throws IOException {
String headerField = connection.getHeaderField(name);
if (headerField == null) {
return Collections.<String>emptyList().iterator();

View File

@@ -42,6 +42,7 @@ import org.springframework.ws.transport.WebServiceConnection;
* Implementation of {@link WebServiceConnection} that is based on the Java 6 HttpServer {@link HttpExchange}.
*
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 1.5.0
*/
public class HttpExchangeConnection extends AbstractReceiverConnection
@@ -99,12 +100,12 @@ public class HttpExchangeConnection extends AbstractReceiverConnection
*/
@Override
protected Iterator<String> getRequestHeaderNames() throws IOException {
public Iterator<String> getRequestHeaderNames() throws IOException {
return httpExchange.getRequestHeaders().keySet().iterator();
}
@Override
protected Iterator<String> getRequestHeaders(String name) throws IOException {
public Iterator<String> getRequestHeaders(String name) throws IOException {
List<String> headers = httpExchange.getRequestHeaders().get(name);
return headers != null ? headers.iterator() : Collections.<String>emptyList().iterator();
}
@@ -119,7 +120,7 @@ public class HttpExchangeConnection extends AbstractReceiverConnection
*/
@Override
protected void addResponseHeader(String name, String value) throws IOException {
public void addResponseHeader(String name, String value) throws IOException {
httpExchange.getResponseHeaders().add(name, value);
}

View File

@@ -46,6 +46,7 @@ import org.springframework.ws.transport.jms.support.JmsTransportUtils;
* {@code TextMessage} response is created.
*
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 1.5.0
*/
public class JmsReceiverConnection extends AbstractReceiverConnection {
@@ -140,7 +141,7 @@ public class JmsReceiverConnection extends AbstractReceiverConnection {
*/
@Override
protected Iterator<String> getRequestHeaderNames() throws IOException {
public Iterator<String> getRequestHeaderNames() throws IOException {
try {
return JmsTransportUtils.getHeaderNames(requestMessage);
}
@@ -150,7 +151,7 @@ public class JmsReceiverConnection extends AbstractReceiverConnection {
}
@Override
protected Iterator<String> getRequestHeaders(String name) throws IOException {
public Iterator<String> getRequestHeaders(String name) throws IOException {
try {
return JmsTransportUtils.getHeaders(requestMessage, name);
}
@@ -200,7 +201,7 @@ public class JmsReceiverConnection extends AbstractReceiverConnection {
}
@Override
protected void addResponseHeader(String name, String value) throws IOException {
public void addResponseHeader(String name, String value) throws IOException {
try {
JmsTransportUtils.addHeader(responseMessage, name, value);
}

View File

@@ -48,6 +48,7 @@ import org.springframework.ws.transport.jms.support.JmsTransportUtils;
* BytesMessage} request and response message.
*
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 1.5.0
*/
public class JmsSenderConnection extends AbstractSenderConnection {
@@ -182,7 +183,7 @@ public class JmsSenderConnection extends AbstractSenderConnection {
*/
@Override
protected void addRequestHeader(String name, String value) throws IOException {
public void addRequestHeader(String name, String value) throws IOException {
try {
JmsTransportUtils.addHeader(requestMessage, name, value);
}
@@ -288,7 +289,7 @@ public class JmsSenderConnection extends AbstractSenderConnection {
}
@Override
protected Iterator<String> getResponseHeaderNames() throws IOException {
public Iterator<String> getResponseHeaderNames() throws IOException {
try {
return JmsTransportUtils.getHeaderNames(responseMessage);
}
@@ -298,7 +299,7 @@ public class JmsSenderConnection extends AbstractSenderConnection {
}
@Override
protected Iterator<String> getResponseHeaders(String name) throws IOException {
public Iterator<String> getResponseHeaders(String name) throws IOException {
try {
return JmsTransportUtils.getHeaders(responseMessage, name);
}

View File

@@ -52,6 +52,7 @@ import org.springframework.ws.transport.mail.support.MailTransportUtils;
* request and response message.
*
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 1.5.0
*/
public class MailReceiverConnection extends AbstractReceiverConnection {
@@ -137,7 +138,7 @@ public class MailReceiverConnection extends AbstractReceiverConnection {
*/
@Override
protected Iterator<String> getRequestHeaderNames() throws IOException {
public Iterator<String> getRequestHeaderNames() throws IOException {
try {
List<String> headers = new ArrayList<String>();
Enumeration<?> enumeration = requestMessage.getAllHeaders();
@@ -153,7 +154,7 @@ public class MailReceiverConnection extends AbstractReceiverConnection {
}
@Override
protected Iterator<String> getRequestHeaders(String name) throws IOException {
public Iterator<String> getRequestHeaders(String name) throws IOException {
try {
String[] headers = requestMessage.getHeader(name);
return Arrays.asList(headers).iterator();
@@ -174,7 +175,7 @@ public class MailReceiverConnection extends AbstractReceiverConnection {
}
@Override
protected void addResponseHeader(String name, String value) throws IOException {
public void addResponseHeader(String name, String value) throws IOException {
try {
responseMessage.addHeader(name, value);
if (TransportConstants.HEADER_CONTENT_TYPE.equals(name)) {

View File

@@ -60,6 +60,7 @@ import org.springframework.ws.transport.mail.support.MailTransportUtils;
* request and response message.
*
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 1.5.0
*/
@@ -165,7 +166,7 @@ public class MailSenderConnection extends AbstractSenderConnection {
}
@Override
protected void addRequestHeader(String name, String value) throws IOException {
public void addRequestHeader(String name, String value) throws IOException {
try {
requestMessage.addHeader(name, value);
if (TransportConstants.HEADER_CONTENT_TYPE.equals(name)) {
@@ -256,7 +257,7 @@ public class MailSenderConnection extends AbstractSenderConnection {
}
@Override
protected Iterator<String> getResponseHeaderNames() throws IOException {
public Iterator<String> getResponseHeaderNames() throws IOException {
try {
List<String> headers = new ArrayList<String>();
Enumeration<?> enumeration = responseMessage.getAllHeaders();
@@ -272,7 +273,7 @@ public class MailSenderConnection extends AbstractSenderConnection {
}
@Override
protected Iterator<String> getResponseHeaders(String name) throws IOException {
public Iterator<String> getResponseHeaders(String name) throws IOException {
try {
String[] headers = responseMessage.getHeader(name);
return Arrays.asList(headers).iterator();

View File

@@ -37,6 +37,7 @@ import org.springframework.ws.transport.xmpp.support.XmppTransportUtils;
*
* @author Gildas Cuisinier
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 2.0
*/
public class XmppReceiverConnection extends AbstractReceiverConnection {
@@ -102,12 +103,12 @@ public class XmppReceiverConnection extends AbstractReceiverConnection {
*/
@Override
protected Iterator<String> getRequestHeaderNames() throws IOException {
public Iterator<String> getRequestHeaderNames() throws IOException {
return XmppTransportUtils.getHeaderNames(requestMessage);
}
@Override
protected Iterator<String> getRequestHeaders(String name) throws IOException {
public Iterator<String> getRequestHeaders(String name) throws IOException {
return XmppTransportUtils.getHeaders(requestMessage, name);
}
@@ -128,7 +129,7 @@ public class XmppReceiverConnection extends AbstractReceiverConnection {
}
@Override
protected void addResponseHeader(String name, String value) throws IOException {
public void addResponseHeader(String name, String value) throws IOException {
XmppTransportUtils.addHeader(responseMessage, name, value);
}

View File

@@ -43,6 +43,7 @@ import org.springframework.ws.transport.xmpp.support.XmppTransportUtils;
*
* @author Gildas Cuisinier
* @author Arjen Poutsma
* @author Greg Turnquist
* @since 2.0
*/
public class XmppSenderConnection extends AbstractSenderConnection {
@@ -116,7 +117,7 @@ public class XmppSenderConnection extends AbstractSenderConnection {
*/
@Override
protected void addRequestHeader(String name, String value) {
public void addRequestHeader(String name, String value) {
XmppTransportUtils.addHeader(requestMessage, name, value);
}
@@ -163,12 +164,12 @@ public class XmppSenderConnection extends AbstractSenderConnection {
}
@Override
protected Iterator<String> getResponseHeaderNames() {
public Iterator<String> getResponseHeaderNames() {
return XmppTransportUtils.getHeaderNames(responseMessage);
}
@Override
protected Iterator<String> getResponseHeaders(String name) throws IOException {
public Iterator<String> getResponseHeaders(String name) throws IOException {
return XmppTransportUtils.getHeaders(responseMessage, name);
}