Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/main/java/com/github/dockerjava/core/DockerClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class DockerClientConfig implements Serializable {
private static final String DOCKER_IO_READ_TIMEOUT_PROPERTY = "docker.io.readTimeout";
// this is really confusing, as there are two ways to spell it
private static final String DOCKER_IO_ENABLE_LOGGING_FILTER_PROPERTY = "docker.io.enableLoggingFilter";
private static final String DOCKER_IO_FOLLOW_REDIRECTS_FILTER_PROPERTY = "docker.io.followRedirectsFilter";
private static final String DOCKER_IO_DOCKER_CERT_PATH_PROPERTY = "docker.io.dockerCertPath";
private static final String DOCKER_IO_DOCKER_CFG_PATH_PROPERTY = "docker.io.dockerCfgPath";
// connection pooling properties
Expand All @@ -53,6 +54,7 @@ public class DockerClientConfig implements Serializable {
m.put("DOCKER_SERVER_ADDRESS", DOCKER_IO_SERVER_ADDRESS_PROPERTY);
m.put("DOCKER_READ_TIMEOUT", DOCKER_IO_READ_TIMEOUT_PROPERTY);
m.put("DOCKER_LOGGING_FILTER_ENABLED", DOCKER_IO_ENABLE_LOGGING_FILTER_PROPERTY);
m.put("DOCKER_FOLLOW_REDIRECTS_FILTER_ENABLED", DOCKER_IO_FOLLOW_REDIRECTS_FILTER_PROPERTY);
m.put(DOCKER_CERT_PATH_PROPERTY, DOCKER_IO_DOCKER_CERT_PATH_PROPERTY);
m.put("DOCKER_CFG_PATH", DOCKER_IO_DOCKER_CFG_PATH_PROPERTY);
ENV_NAME_TO_IO_NAME = Collections.unmodifiableMap(m);
Expand All @@ -63,14 +65,15 @@ public class DockerClientConfig implements Serializable {
private final String version, username, password, email, serverAddress, dockerCfgPath;
private final Integer readTimeout;
private final boolean loggingFilterEnabled;
private final boolean followRedirectsFilterEnabled;
private final SSLConfig sslConfig;

private final int maxTotalConnections;
private final int maxPerRouteConnections;

DockerClientConfig(URI uri, String version, String username, String password, String email, String serverAddress,
String dockerCfgPath, Integer readTimeout, boolean loggingFilterEnabled, SSLConfig sslConfig,
int maxTotalConns, int maxPerRouteConns) {
String dockerCfgPath, Integer readTimeout, boolean loggingFilterEnabled, boolean followRedirectsFilterEnabled,
SSLConfig sslConfig, int maxTotalConns, int maxPerRouteConns) {
this.uri = uri;
this.version = version;
this.username = username;
Expand All @@ -80,6 +83,7 @@ public class DockerClientConfig implements Serializable {
this.dockerCfgPath = dockerCfgPath;
this.readTimeout = readTimeout;
this.loggingFilterEnabled = loggingFilterEnabled;
this.followRedirectsFilterEnabled = followRedirectsFilterEnabled;
this.sslConfig = sslConfig;
this.maxTotalConnections = maxTotalConns;
this.maxPerRouteConnections = maxPerRouteConns;
Expand Down Expand Up @@ -182,6 +186,7 @@ private static Properties overrideDockerPropertiesWithSystemProperties(Propertie
DOCKER_IO_SERVER_ADDRESS_PROPERTY,
DOCKER_IO_READ_TIMEOUT_PROPERTY,
DOCKER_IO_ENABLE_LOGGING_FILTER_PROPERTY,
DOCKER_IO_FOLLOW_REDIRECTS_FILTER_PROPERTY,
DOCKER_IO_DOCKER_CERT_PATH_PROPERTY,
DOCKER_IO_DOCKER_CFG_PATH_PROPERTY,
}) {
Expand Down Expand Up @@ -243,6 +248,10 @@ public boolean isLoggingFilterEnabled() {
return loggingFilterEnabled;
}

public boolean followRedirectsFilterEnabled() {
return followRedirectsFilterEnabled;
}

public SSLConfig getSslConfig() {
return sslConfig;
}
Expand Down Expand Up @@ -345,14 +354,15 @@ public String toString() {
", sslConfig='" + sslConfig + '\'' +
", readTimeout=" + readTimeout +
", loggingFilterEnabled=" + loggingFilterEnabled +
", followRedirectsFilterEnabled=" + followRedirectsFilterEnabled +
'}';
}

public static class DockerClientConfigBuilder {
private URI uri;
private String version, username, password, email, serverAddress, dockerCfgPath;
private Integer readTimeout, maxTotalConnections, maxPerRouteConnections;
private boolean loggingFilterEnabled;
private boolean loggingFilterEnabled, followRedirectsFilterEnabled;
private SSLConfig sslConfig;

/**
Expand All @@ -370,6 +380,7 @@ public DockerClientConfigBuilder withProperties(Properties p) {
.withServerAddress(p.getProperty(DOCKER_IO_SERVER_ADDRESS_PROPERTY))
.withReadTimeout(Integer.valueOf(p.getProperty(DOCKER_IO_READ_TIMEOUT_PROPERTY, "0")))
.withLoggingFilter(Boolean.valueOf(p.getProperty(DOCKER_IO_ENABLE_LOGGING_FILTER_PROPERTY, "true")))
.withFollowRedirectsFilter(Boolean.valueOf(p.getProperty(DOCKER_IO_FOLLOW_REDIRECTS_FILTER_PROPERTY, "false")))
.withDockerCertPath(p.getProperty(DOCKER_IO_DOCKER_CERT_PATH_PROPERTY))
.withDockerCfgPath(p.getProperty(DOCKER_IO_DOCKER_CFG_PATH_PROPERTY))
.withMaxPerRouteConnections(Integer.valueOf(p.getProperty(DOCKER_IO_MAX_PER_ROUTE_PROPERTY, "2")))
Expand Down Expand Up @@ -428,6 +439,11 @@ public final DockerClientConfigBuilder withLoggingFilter(boolean loggingFilterEn
return this;
}

public final DockerClientConfigBuilder withFollowRedirectsFilter(boolean followRedirectsFilterEnabled) {
this.followRedirectsFilterEnabled = followRedirectsFilterEnabled;
return this;
}

public final DockerClientConfigBuilder withDockerCertPath(String dockerCertPath) {
this.sslConfig = new LocalDirectorySSLConfig(dockerCertPath);
return this;
Expand Down Expand Up @@ -455,6 +471,7 @@ public DockerClientConfig build() {
dockerCfgPath,
readTimeout,
loggingFilterEnabled,
followRedirectsFilterEnabled,
sslConfig,
maxTotalConnections,
maxPerRouteConnections
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.dockerjava.core.util;

import java.io.IOException;
import java.io.InputStream;

import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.client.ClientResponseFilter;
import javax.ws.rs.core.Response;

/**
* Default implementation of RedirectStrategy honors the restrictions
* on automatic redirection of entity enclosing methods such as POST
* and PUT imposed by the HTTP specification. 302 Moved Temporarily,
* 301 Moved Permanently and 307 Temporary Redirect status codes will
* result in an automatic redirect of HEAD and GET methods only.
*
* {@link org.apache.http.impl.client.DefaultRedirectStrategy}
*
* This filter allows arbitrary redirection for other methods.
*/
public class FollowRedirectsFilter implements ClientResponseFilter {

@Override
public void filter(ClientRequestContext requestContext,
ClientResponseContext responseContext) throws IOException {
if (!responseContext.getStatusInfo().getFamily().equals(Response.Status.Family.REDIRECTION)) {
return;
}

Response resp = requestContext.getClient().target(responseContext.getLocation())
.request().method(requestContext.getMethod());
responseContext.setEntityStream((InputStream) resp.getEntity());
responseContext.setStatusInfo(resp.getStatusInfo());
responseContext.setStatus(resp.getStatus());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re
case 200:
case 201:
case 204:
case 301:
return;
case 304:
throw new NotModifiedException(getBodyAsMessage(responseContext));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.URI;

import com.github.dockerjava.api.command.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -28,6 +29,7 @@
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.github.dockerjava.api.DockerClientException;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.util.FollowRedirectsFilter;
import com.github.dockerjava.core.util.JsonClientFilter;
import com.github.dockerjava.core.util.ResponseStatusExceptionFilter;
import com.github.dockerjava.core.util.SelectiveLoggingFilter;
Expand All @@ -46,6 +48,9 @@ public void init(DockerClientConfig dockerClientConfig) {
clientConfig.connectorProvider(new ApacheConnectorProvider());
clientConfig.property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true);

if (dockerClientConfig.followRedirectsFilterEnabled()) {
clientConfig.register(FollowRedirectsFilter.class);
}
clientConfig.register(ResponseStatusExceptionFilter.class);
clientConfig.register(JsonClientFilter.class);
clientConfig.register(JacksonJsonProvider.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public class DockerClientConfigTest {
public static final DockerClientConfig EXAMPLE_CONFIG = newExampleConfig();

private static DockerClientConfig newExampleConfig() {
return new DockerClientConfig(URI.create("http://foo"), "bar", "baz", "qux", "blam", "wham", "flam", 877, false, new LocalDirectorySSLConfig("flim"), 20, 2);
return new DockerClientConfig(URI.create("http://foo"), "bar", "baz", "qux", "blam", "wham", "flam", 877, false, false, new LocalDirectorySSLConfig("flim"), 20, 2);
}

@Test
public void string() throws Exception {
assertEquals("DockerClientConfig{uri=http://foo, version='bar', username='baz', password='qux', email='blam', serverAddress='wham', dockerCfgPath='flam', sslConfig='LocalDirectorySSLConfig{dockerCertPath=flim}', readTimeout=877, loggingFilterEnabled=false}",
assertEquals("DockerClientConfig{uri=http://foo, version='bar', username='baz', password='qux', email='blam', serverAddress='wham', dockerCfgPath='flam', sslConfig='LocalDirectorySSLConfig{dockerCertPath=flim}', readTimeout=877, loggingFilterEnabled=false, followRedirectsFilterEnabled=false}",
EXAMPLE_CONFIG.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class DockerClientImplTest {
@Test
public void configuredInstanceAuthConfig() throws Exception {
// given a config with null serverAddress
DockerClientConfig dockerClientConfig = new DockerClientConfig(null, null, "", "", "", null, null, 0, false, null, 20, 2);
DockerClientConfig dockerClientConfig = new DockerClientConfig(null, null, "", "", "", null, null, 0, false, false, null, 20, 2);
DockerClientImpl dockerClient = DockerClientImpl.getInstance(dockerClientConfig);

// when we get the auth config
Expand Down