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