Changed ClientHttpRequestInterceptor array to List
This commit is contained in:
parent
4a669d1a0a
commit
b06de49c72
|
@ -18,8 +18,8 @@ package org.springframework.http.client;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
@ -36,14 +36,14 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
|
|||
|
||||
private final ClientHttpRequestFactory requestFactory;
|
||||
|
||||
private final ClientHttpRequestInterceptor[] interceptors;
|
||||
private final List<ClientHttpRequestInterceptor> interceptors;
|
||||
|
||||
private HttpMethod method;
|
||||
|
||||
private URI uri;
|
||||
|
||||
protected InterceptingClientHttpRequest(ClientHttpRequestFactory requestFactory,
|
||||
ClientHttpRequestInterceptor[] interceptors,
|
||||
List<ClientHttpRequestInterceptor> interceptors,
|
||||
URI uri,
|
||||
HttpMethod method) {
|
||||
this.requestFactory = requestFactory;
|
||||
|
@ -72,7 +72,7 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
|
|||
private final Iterator<ClientHttpRequestInterceptor> iterator;
|
||||
|
||||
private RequestExecution() {
|
||||
this.iterator = Arrays.asList(interceptors).iterator();
|
||||
this.iterator = interceptors.iterator();
|
||||
}
|
||||
|
||||
public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException {
|
||||
|
|
|
@ -17,9 +17,10 @@
|
|||
package org.springframework.http.client;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Wrapper for a {@link ClientHttpRequestFactory} that has support for {@link ClientHttpRequestInterceptor}s.
|
||||
|
@ -29,7 +30,7 @@ import org.springframework.util.Assert;
|
|||
*/
|
||||
public class InterceptingClientHttpRequestFactory extends AbstractClientHttpRequestFactoryWrapper {
|
||||
|
||||
private final ClientHttpRequestInterceptor[] interceptors;
|
||||
private final List<ClientHttpRequestInterceptor> interceptors;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the {@code InterceptingClientHttpRequestFactory} with the given parameters.
|
||||
|
@ -38,10 +39,9 @@ public class InterceptingClientHttpRequestFactory extends AbstractClientHttpRequ
|
|||
* @param interceptors the interceptors that are to be applied. Can be {@code null}.
|
||||
*/
|
||||
public InterceptingClientHttpRequestFactory(ClientHttpRequestFactory requestFactory,
|
||||
ClientHttpRequestInterceptor[] interceptors) {
|
||||
List<ClientHttpRequestInterceptor> interceptors) {
|
||||
super(requestFactory);
|
||||
Assert.notNull(requestFactory, "'requestFactory' must not be null");
|
||||
this.interceptors = interceptors != null ? interceptors : new ClientHttpRequestInterceptor[0];
|
||||
this.interceptors = interceptors != null ? interceptors : Collections.<ClientHttpRequestInterceptor>emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -16,10 +16,12 @@
|
|||
|
||||
package org.springframework.http.client.support;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Base class for {@link org.springframework.web.client.RestTemplate} and other HTTP accessing gateway helpers, adding
|
||||
|
@ -31,26 +33,26 @@ import org.springframework.util.ObjectUtils;
|
|||
*/
|
||||
public abstract class InterceptingHttpAccessor extends HttpAccessor {
|
||||
|
||||
private ClientHttpRequestInterceptor[] interceptors;
|
||||
private List<ClientHttpRequestInterceptor> interceptors;
|
||||
|
||||
/**
|
||||
* Sets the request interceptors that this accessor should use.
|
||||
*/
|
||||
public void setInterceptors(ClientHttpRequestInterceptor[] interceptors) {
|
||||
public void setInterceptors(List<ClientHttpRequestInterceptor> interceptors) {
|
||||
this.interceptors = interceptors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the request interceptor that this accessor uses.
|
||||
*/
|
||||
public ClientHttpRequestInterceptor[] getInterceptors() {
|
||||
public List<ClientHttpRequestInterceptor> getInterceptors() {
|
||||
return interceptors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpRequestFactory getRequestFactory() {
|
||||
ClientHttpRequestFactory delegate = super.getRequestFactory();
|
||||
if (!ObjectUtils.isEmpty(getInterceptors())) {
|
||||
if (!CollectionUtils.isEmpty(getInterceptors())) {
|
||||
return new InterceptingClientHttpRequestFactory(delegate, getInterceptors());
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -21,7 +21,10 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -34,10 +37,8 @@ import org.springframework.http.client.support.HttpRequestWrapper;
|
|||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class InterceptingClientHttpRequestFactoryTest {
|
||||
/** @author Arjen Poutsma */
|
||||
public class InterceptingClientHttpRequestFactoryTests {
|
||||
|
||||
private InterceptingClientHttpRequestFactory requestFactory;
|
||||
|
||||
|
@ -58,39 +59,39 @@ public class InterceptingClientHttpRequestFactoryTest {
|
|||
|
||||
@Test
|
||||
public void basic() throws Exception {
|
||||
NoOpInterceptor interceptor1 = new NoOpInterceptor();
|
||||
NoOpInterceptor interceptor2 = new NoOpInterceptor();
|
||||
NoOpInterceptor interceptor3 = new NoOpInterceptor();
|
||||
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
|
||||
new ClientHttpRequestInterceptor[]{interceptor1, interceptor2, interceptor3});
|
||||
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
|
||||
interceptors.add(new NoOpInterceptor());
|
||||
interceptors.add(new NoOpInterceptor());
|
||||
interceptors.add(new NoOpInterceptor());
|
||||
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, interceptors);
|
||||
|
||||
ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
|
||||
ClientHttpResponse response = request.execute();
|
||||
|
||||
assertTrue(interceptor1.invoked);
|
||||
assertTrue(interceptor2.invoked);
|
||||
assertTrue(interceptor3.invoked);
|
||||
assertTrue(((NoOpInterceptor) interceptors.get(0)).invoked);
|
||||
assertTrue(((NoOpInterceptor) interceptors.get(1)).invoked);
|
||||
assertTrue(((NoOpInterceptor) interceptors.get(2)).invoked);
|
||||
assertTrue(requestMock.executed);
|
||||
assertSame(responseMock, response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noExecution() throws Exception {
|
||||
ClientHttpRequestInterceptor interceptor1 = new ClientHttpRequestInterceptor() {
|
||||
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
|
||||
interceptors.add(new ClientHttpRequestInterceptor() {
|
||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
|
||||
throws IOException {
|
||||
return responseMock;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
NoOpInterceptor interceptor2 = new NoOpInterceptor();
|
||||
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
|
||||
new ClientHttpRequestInterceptor[]{interceptor1, interceptor2});
|
||||
interceptors.add(new NoOpInterceptor());
|
||||
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, interceptors);
|
||||
|
||||
ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
|
||||
ClientHttpResponse response = request.execute();
|
||||
|
||||
assertFalse(interceptor2.invoked);
|
||||
assertFalse(((NoOpInterceptor) interceptors.get(1)).invoked);
|
||||
assertFalse(requestMock.executed);
|
||||
assertSame(responseMock, response);
|
||||
}
|
||||
|
@ -122,8 +123,8 @@ public class InterceptingClientHttpRequestFactoryTest {
|
|||
}
|
||||
};
|
||||
|
||||
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
|
||||
new ClientHttpRequestInterceptor[]{interceptor});
|
||||
requestFactory =
|
||||
new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
|
||||
|
||||
ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
|
||||
request.execute();
|
||||
|
@ -154,8 +155,8 @@ public class InterceptingClientHttpRequestFactoryTest {
|
|||
}
|
||||
};
|
||||
|
||||
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
|
||||
new ClientHttpRequestInterceptor[]{interceptor});
|
||||
requestFactory =
|
||||
new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
|
||||
|
||||
ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
|
||||
request.execute();
|
||||
|
@ -186,8 +187,8 @@ public class InterceptingClientHttpRequestFactoryTest {
|
|||
}
|
||||
};
|
||||
|
||||
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
|
||||
new ClientHttpRequestInterceptor[]{interceptor});
|
||||
requestFactory =
|
||||
new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
|
||||
|
||||
ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
|
||||
request.execute();
|
||||
|
@ -204,8 +205,8 @@ public class InterceptingClientHttpRequestFactoryTest {
|
|||
}
|
||||
};
|
||||
|
||||
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
|
||||
new ClientHttpRequestInterceptor[]{interceptor});
|
||||
requestFactory =
|
||||
new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
|
||||
|
||||
ClientHttpRequest request = requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
|
||||
request.execute();
|
Loading…
Reference in New Issue