Skip to content

Commit

Permalink
ConnectorProvider should be idempotent
Browse files Browse the repository at this point in the history
Resolve #916

ConnectorProvider should be idempotent because it can be called
many times, if the user specified custom filters on a web target.
  • Loading branch information
Artem Prigoda committed Mar 5, 2015
1 parent 8d09280 commit a20aff3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Expand Up @@ -16,6 +16,7 @@
import org.apache.http.conn.DnsResolver;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.client.spi.ConnectorProvider;
Expand Down Expand Up @@ -102,7 +103,7 @@ public JerseyClientBuilder withProvider(Class<?> klass) {

/**
* Sets the state of the given Jersey property.
*
* <p/>
* <p/><b>WARNING:</b> The default connector ignores Jersey properties.
* Use {@link JerseyClientConfiguration} instead.
*
Expand Down Expand Up @@ -313,11 +314,11 @@ private Configuration buildConfig(final String name, final ExecutorService threa

config.register(new DropwizardExecutorProvider(threadPool));
if (connectorProvider == null) {
final CloseableHttpClient apacheHttpClient = apacheHttpClientBuilder.build(name);
connectorProvider = new ConnectorProvider() {
@Override
public Connector getConnector(Client client, Configuration runtimeConfig) {
return new DropwizardApacheConnector(apacheHttpClientBuilder.build(name),
configuration.isChunkedEncodingEnabled());
return new DropwizardApacheConnector(apacheHttpClient, configuration.isChunkedEncodingEnabled());
}
};
}
Expand Down
Expand Up @@ -11,12 +11,14 @@
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import io.dropwizard.jackson.Jackson;
import org.glassfish.jersey.filter.LoggingFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import java.io.IOException;
Expand All @@ -28,6 +30,7 @@
import java.util.zip.GZIPOutputStream;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -241,6 +244,46 @@ public void handle(HttpExchange httpExchange) throws IOException {
assertThat(text).isEqualTo("Hello World!");
}

/**
* Test for ConnectorProvider idempotency
*/
@Test
public void testFilterOnAWebTarget() {
httpServer.createContext("/test", new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
try {
httpExchange.getResponseHeaders().add(HttpHeaders.CONTENT_TYPE, TEXT_PLAIN);
httpExchange.sendResponseHeaders(200, 0);
httpExchange.getResponseBody().write("Hello World!".getBytes(Charsets.UTF_8));
} finally {
httpExchange.close();
}
}
});
httpServer.start();

Client jersey = new JerseyClientBuilder(new MetricRegistry())
.using(Executors.newSingleThreadExecutor(), JSON_MAPPER)
.build("test-jersey-client");
String uri = "http://127.0.0.1:" + httpServer.getAddress().getPort() + "/test";

WebTarget target = jersey.target(uri);
target.register(new LoggingFilter());
String firstResponse = target.request()
.buildGet()
.invoke()
.readEntity(String.class);
assertThat(firstResponse).isEqualTo("Hello World!");

String secondResponse = jersey.target(uri)
.request()
.buildGet()
.invoke()
.readEntity(String.class);
assertThat(secondResponse).isEqualTo("Hello World!");
}

static class Person {

@JsonProperty("email")
Expand Down

0 comments on commit a20aff3

Please sign in to comment.