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();