X Tutup
Skip to content

Commit 19ec0db

Browse files
author
Gabe Ki
committed
Add redirection for 301 - Issue #147
1 parent 0c6f2b4 commit 19ec0db

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

src/main/java/com/github/dockerjava/core/DockerClientConfig.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class DockerClientConfig implements Serializable {
3333
private static final String DOCKER_IO_READ_TIMEOUT_PROPERTY = "docker.io.readTimeout";
3434
// this is really confusing, as there are two ways to spell it
3535
private static final String DOCKER_IO_ENABLE_LOGGING_FILTER_PROPERTY = "docker.io.enableLoggingFilter";
36+
private static final String DOCKER_IO_FOLLOW_REDIRECTS_FILTER_PROPERTY = "docker.io.followRedirectsFilterEnabled";
3637
private static final String DOCKER_IO_DOCKER_CERT_PATH_PROPERTY = "docker.io.dockerCertPath";
3738
private static final String DOCKER_IO_DOCKER_CFG_PATH_PROPERTY = "docker.io.dockerCfgPath";
3839
// connection pooling properties
@@ -53,6 +54,7 @@ public class DockerClientConfig implements Serializable {
5354
m.put("DOCKER_SERVER_ADDRESS", DOCKER_IO_SERVER_ADDRESS_PROPERTY);
5455
m.put("DOCKER_READ_TIMEOUT", DOCKER_IO_READ_TIMEOUT_PROPERTY);
5556
m.put("DOCKER_LOGGING_FILTER_ENABLED", DOCKER_IO_ENABLE_LOGGING_FILTER_PROPERTY);
57+
m.put("DOCKER_IO_FOLLOW_REDIRECTS_FILTER_PROPERTY", DOCKER_IO_FOLLOW_REDIRECTS_FILTER_PROPERTY);
5658
m.put(DOCKER_CERT_PATH_PROPERTY, DOCKER_IO_DOCKER_CERT_PATH_PROPERTY);
5759
m.put("DOCKER_CFG_PATH", DOCKER_IO_DOCKER_CFG_PATH_PROPERTY);
5860
ENV_NAME_TO_IO_NAME = Collections.unmodifiableMap(m);
@@ -63,14 +65,15 @@ public class DockerClientConfig implements Serializable {
6365
private final String version, username, password, email, serverAddress, dockerCfgPath;
6466
private final Integer readTimeout;
6567
private final boolean loggingFilterEnabled;
68+
private final boolean followRedirectsFilterEnabled;
6669
private final SSLConfig sslConfig;
6770

6871
private final int maxTotalConnections;
6972
private final int maxPerRouteConnections;
7073

7174
DockerClientConfig(URI uri, String version, String username, String password, String email, String serverAddress,
72-
String dockerCfgPath, Integer readTimeout, boolean loggingFilterEnabled, SSLConfig sslConfig,
73-
int maxTotalConns, int maxPerRouteConns) {
75+
String dockerCfgPath, Integer readTimeout, boolean loggingFilterEnabled, boolean followRedirectsFilterEnabled,
76+
SSLConfig sslConfig, int maxTotalConns, int maxPerRouteConns) {
7477
this.uri = uri;
7578
this.version = version;
7679
this.username = username;
@@ -80,6 +83,7 @@ public class DockerClientConfig implements Serializable {
8083
this.dockerCfgPath = dockerCfgPath;
8184
this.readTimeout = readTimeout;
8285
this.loggingFilterEnabled = loggingFilterEnabled;
86+
this.followRedirectsFilterEnabled = followRedirectsFilterEnabled;
8387
this.sslConfig = sslConfig;
8488
this.maxTotalConnections = maxTotalConns;
8589
this.maxPerRouteConnections = maxPerRouteConns;
@@ -243,6 +247,10 @@ public boolean isLoggingFilterEnabled() {
243247
return loggingFilterEnabled;
244248
}
245249

250+
public boolean followRedirectsFilterEnabled() {
251+
return followRedirectsFilterEnabled;
252+
}
253+
246254
public SSLConfig getSslConfig() {
247255
return sslConfig;
248256
}
@@ -345,14 +353,15 @@ public String toString() {
345353
", sslConfig='" + sslConfig + '\'' +
346354
", readTimeout=" + readTimeout +
347355
", loggingFilterEnabled=" + loggingFilterEnabled +
356+
", followRedirectsFilterEnabled=" + followRedirectsFilterEnabled +
348357
'}';
349358
}
350359

351360
public static class DockerClientConfigBuilder {
352361
private URI uri;
353362
private String version, username, password, email, serverAddress, dockerCfgPath;
354363
private Integer readTimeout, maxTotalConnections, maxPerRouteConnections;
355-
private boolean loggingFilterEnabled;
364+
private boolean loggingFilterEnabled, followRedirectsFilterEnabled;
356365
private SSLConfig sslConfig;
357366

358367
/**
@@ -370,6 +379,7 @@ public DockerClientConfigBuilder withProperties(Properties p) {
370379
.withServerAddress(p.getProperty(DOCKER_IO_SERVER_ADDRESS_PROPERTY))
371380
.withReadTimeout(Integer.valueOf(p.getProperty(DOCKER_IO_READ_TIMEOUT_PROPERTY, "0")))
372381
.withLoggingFilter(Boolean.valueOf(p.getProperty(DOCKER_IO_ENABLE_LOGGING_FILTER_PROPERTY, "true")))
382+
.withFollowRedirectsFilter(Boolean.valueOf(p.getProperty(DOCKER_IO_FOLLOW_REDIRECTS_FILTER_PROPERTY, "true")))
373383
.withDockerCertPath(p.getProperty(DOCKER_IO_DOCKER_CERT_PATH_PROPERTY))
374384
.withDockerCfgPath(p.getProperty(DOCKER_IO_DOCKER_CFG_PATH_PROPERTY))
375385
.withMaxPerRouteConnections(Integer.valueOf(p.getProperty(DOCKER_IO_MAX_PER_ROUTE_PROPERTY, "2")))
@@ -428,6 +438,11 @@ public final DockerClientConfigBuilder withLoggingFilter(boolean loggingFilterEn
428438
return this;
429439
}
430440

441+
public final DockerClientConfigBuilder withFollowRedirectsFilter(boolean followRedirectsFilterEnabled) {
442+
this.followRedirectsFilterEnabled = followRedirectsFilterEnabled;
443+
return this;
444+
}
445+
431446
public final DockerClientConfigBuilder withDockerCertPath(String dockerCertPath) {
432447
this.sslConfig = new LocalDirectorySSLConfig(dockerCertPath);
433448
return this;
@@ -455,6 +470,7 @@ public DockerClientConfig build() {
455470
dockerCfgPath,
456471
readTimeout,
457472
loggingFilterEnabled,
473+
followRedirectsFilterEnabled,
458474
sslConfig,
459475
maxTotalConnections,
460476
maxPerRouteConnections
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.dockerjava.core.util;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
import javax.ws.rs.client.ClientRequestContext;
7+
import javax.ws.rs.client.ClientResponseContext;
8+
import javax.ws.rs.client.ClientResponseFilter;
9+
import javax.ws.rs.core.Response;
10+
11+
/**
12+
* Default implementation of RedirectStrategy honors the restrictions
13+
* on automatic redirection of entity enclosing methods such as POST
14+
* and PUT imposed by the HTTP specification. 302 Moved Temporarily,
15+
* 301 Moved Permanently and 307 Temporary Redirect status codes will
16+
* result in an automatic redirect of HEAD and GET methods only.
17+
*
18+
* {@link org.apache.http.impl.client.DefaultRedirectStrategy}
19+
*
20+
* This filter allows arbitrary redirection for other methods.
21+
*/
22+
public class FollowRedirectsFilter implements ClientResponseFilter {
23+
24+
@Override
25+
public void filter(ClientRequestContext requestContext,
26+
ClientResponseContext responseContext) throws IOException {
27+
if (!responseContext.getStatusInfo().getFamily().equals(Response.Status.Family.REDIRECTION)) {
28+
return;
29+
}
30+
31+
Response resp = requestContext.getClient().target(responseContext.getLocation())
32+
.request().method(requestContext.getMethod());
33+
responseContext.setEntityStream((InputStream) resp.getEntity());
34+
responseContext.setStatusInfo(resp.getStatusInfo());
35+
responseContext.setStatus(resp.getStatus());
36+
}
37+
}

src/main/java/com/github/dockerjava/core/util/ResponseStatusExceptionFilter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re
3737
case 200:
3838
case 201:
3939
case 204:
40-
case 301:
4140
return;
4241
case 304:
4342
throw new NotModifiedException(getBodyAsMessage(responseContext));

src/main/java/com/github/dockerjava/jaxrs/DockerCmdExecFactoryImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.net.URI;
77

88
import com.github.dockerjava.api.command.*;
9+
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112

@@ -28,6 +29,7 @@
2829
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
2930
import com.github.dockerjava.api.DockerClientException;
3031
import com.github.dockerjava.core.DockerClientConfig;
32+
import com.github.dockerjava.core.util.FollowRedirectsFilter;
3133
import com.github.dockerjava.core.util.JsonClientFilter;
3234
import com.github.dockerjava.core.util.ResponseStatusExceptionFilter;
3335
import com.github.dockerjava.core.util.SelectiveLoggingFilter;
@@ -49,6 +51,9 @@ public void init(DockerClientConfig dockerClientConfig) {
4951
clientConfig.register(ResponseStatusExceptionFilter.class);
5052
clientConfig.register(JsonClientFilter.class);
5153
clientConfig.register(JacksonJsonProvider.class);
54+
if (dockerClientConfig.followRedirectsFilterEnabled()) {
55+
clientConfig.register(FollowRedirectsFilter.class);
56+
}
5257

5358
if (dockerClientConfig.isLoggingFilterEnabled()) {
5459
clientConfig.register(new SelectiveLoggingFilter(LOGGER, true));

0 commit comments

Comments
 (0)
X Tutup