Skip to content
Closed
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
71 changes: 55 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@

<version.slf4j>1.6.1</version.slf4j>

<jersey.version>2.11</jersey.version>
<jersey.version>1.18</jersey.version>
<jersey-apache-client4.version>1.9</jersey-apache-client4.version>

<jackson-jaxrs.version>2.3.3</jackson-jaxrs.version>

<httpclient.version>4.2.5</httpclient.version>
<commons-compress.version>1.5</commons-compress.version>
<commons-codec.version>1.8</commons-codec.version>
<commons-codec.version>1.8</commons-codec.version>
<commons-io.version>2.3</commons-io.version>
<commons-lang.version>2.6</commons-lang.version>
<slf4j-api.version>1.7.5</slf4j-api.version>
Expand Down Expand Up @@ -87,16 +87,44 @@
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>${jackson-jaxrs.version}</version>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.glassfish.jersey.connectors</groupId> -->
<!-- <artifactId>jersey-jetty-connector</artifactId> -->
<!-- <version>${jersey.version}</version> -->
<!-- </dependency> -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>

<!-- Jersey 2.x dependencies
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
-->

<!-- Begin Jersey 1.18 dependencies -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>

<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-apache-client4</artifactId>
<version>${jersey-apache-client4.version}</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<!-- End Jersey 1.18 dependencies -->

<dependency>
<groupId>org.apache.commons</groupId>
Expand Down Expand Up @@ -138,10 +166,10 @@
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>

<!-- /// Test /////////////////////////// -->
<dependency>
Expand Down Expand Up @@ -240,6 +268,15 @@
<encoding>ISO-8859-1</encoding>
<debug>${jdk.debug}</debug>
<optimize>${jdk.optimize}</optimize>

<!-- Exclude the Jersey 2.x code until both it and the 1.18 code can coexist -->
<excludes>
<exclude>**/jaxrs2/*.java</exclude>
<exclude>**/jaxrs2/util/*.java</exclude>
</excludes>
<testExcludes>
<exclude>**/jaxrs2/*.java</exclude>
</testExcludes>
</configuration>
</plugin>

Expand Down Expand Up @@ -338,6 +375,7 @@
<id>release</id>
<build>
<plugins>
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
Expand All @@ -351,6 +389,7 @@
</execution>
</executions>
</plugin>
-->

<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ public interface StartContainerCmd extends DockerCmd<Void> {
* "CHOWN" prevents the container from changing the owner of any files.
*/
public StartContainerCmd withCapDrop(String... capDrop);

/**
* Perform the start using the existing host configuration for the container.
*
* @param withExisting <code>true</code> if the container's existing configuration should be used.
*
* @return
*/
public StartContainerCmd withExistingConfig(boolean withExisting);

/**
* @return <code>true</code> if the container should use its existing configuration.
*/
public boolean useExistingConfig();

/**
* @throws NotFoundException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,4 @@ public InputStream exec() throws NotFoundException {
return super.exec();
}

// protected InputStream impl() throws DockerException {
//
// CopyFileFromContainerCmd command = this;
//
// WebTarget webResource =
// baseResource.path("/containers/{id}/copy").resolveTemplate("id", command.getContainerId());
//
// LOGGER.trace("POST: " + webResource.toString());
//
// return webResource.request().accept(MediaType.APPLICATION_OCTET_STREAM_TYPE).post(entity(command, MediaType.APPLICATION_JSON), Response.class).readEntity(InputStream.class);
// }


}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public class StartContainerCmdImpl extends AbstrDockerCmd<StartContainerCmd, Voi
@JsonProperty("CapDrop")
private String[] capDrop;

@JsonIgnore
private boolean useExistingConfig = false;

public StartContainerCmdImpl(StartContainerCmd.Exec exec, String containerId) {
super(exec);
withContainerId(containerId);
Expand Down Expand Up @@ -255,6 +258,17 @@ public StartContainerCmd withCapDrop(String... capDrop) {
this.capDrop = capDrop;
return this;
}

@Override
public StartContainerCmd withExistingConfig(boolean useExisting) {
this.useExistingConfig = useExisting;
return this;
}

@Override
public boolean useExistingConfig() {
return this.useExistingConfig;
}

@Override
public String toString() {
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/com/github/dockerjava/jaxrs1/AbstrDockerCmdExec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.github.dockerjava.jaxrs1;

import java.io.IOException;

import org.apache.commons.codec.binary.Base64;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.DockerException;
import com.github.dockerjava.api.command.DockerCmd;
import com.github.dockerjava.api.command.DockerCmdExec;
import com.github.dockerjava.api.model.AuthConfig;
import com.google.common.base.Preconditions;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.uri.UriTemplate;

public abstract class AbstrDockerCmdExec<CMD_T extends DockerCmd<RES_T>, RES_T>
implements DockerCmdExec<CMD_T, RES_T> {

private WebResource baseResource;

public AbstrDockerCmdExec(WebResource baseResource) {
Preconditions.checkNotNull(baseResource,
"baseResource was not specified");
this.baseResource = baseResource;
}

protected WebResourceBuilder getBaseResource() {
return new WebResourceBuilder(baseResource);
}

public static class WebResourceBuilder {
WebResource resource;

private WebResourceBuilder(WebResource resource) {
this.resource = resource;
}

public WebResourceBuilder path(String pathStr) {
this.resource = this.resource.path(pathStr);
return this;
}

public WebResourceBuilder resolveTemplate(String path, String... values) {
UriTemplate tmplt = new UriTemplate(path);
this.resource = this.resource.path(tmplt.createURI(values));
return this;
}

public WebResourceBuilder queryParam(String name, String value) {
if (value != null) {
this.resource = this.resource.queryParam(name, value);
}
return this;
}

public WebResource build() {
return this.resource;
}
}

protected String registryAuth(AuthConfig authConfig) {
try {
return Base64.encodeBase64String(new ObjectMapper()
.writeValueAsString(authConfig).getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public RES_T exec(CMD_T command) {
// this hack works because of ResponseStatusExceptionFilter
RES_T result;
try {
result = execute(command);
} catch (ClientHandlerException e) {
if(e.getCause() instanceof DockerException) {
throw (DockerException)e.getCause();
} else {
throw e;
}
}
return result;
}

protected abstract RES_T execute(CMD_T command);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.dockerjava.jaxrs1;

import java.io.InputStream;

import javax.ws.rs.core.MediaType;

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

import com.github.dockerjava.api.command.AttachContainerCmd;
import com.sun.jersey.api.client.WebResource;

public class AttachContainerCmdExec extends AbstrDockerCmdExec<AttachContainerCmd, InputStream> implements AttachContainerCmd.Exec {

private static final Logger LOGGER = LoggerFactory
.getLogger(AttachContainerCmdExec.class);

public AttachContainerCmdExec(WebResource baseResource) {
super(baseResource);
}

@Override
protected InputStream execute(AttachContainerCmd command) {
WebResource webResource = getBaseResource()
.resolveTemplate("/containers/{id}/attach", command.getContainerId())
.queryParam("logs", command.hasLogsEnabled() ? "1" : "0")
.queryParam("stdout", command.hasStdoutEnabled() ? "1" : "0")
.queryParam("stderr", command.hasStderrEnabled() ? "1" : "0")
.queryParam("stream", command.hasFollowStreamEnabled() ? "1" : "0")
.build();

LOGGER.trace("POST: {}", webResource);

return webResource.accept(MediaType.APPLICATION_OCTET_STREAM_TYPE)
.entity(null, MediaType.APPLICATION_JSON)
.post(InputStream.class);
}

}
38 changes: 38 additions & 0 deletions src/main/java/com/github/dockerjava/jaxrs1/AuthCmdExec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.dockerjava.jaxrs1;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

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

import com.github.dockerjava.api.UnauthorizedException;
import com.github.dockerjava.api.command.AuthCmd;
import com.sun.jersey.api.client.WebResource;

public class AuthCmdExec extends AbstrDockerCmdExec<AuthCmd, Void> implements AuthCmd.Exec {

private static final Logger LOGGER = LoggerFactory
.getLogger(AuthCmdExec.class);

public AuthCmdExec(WebResource baseResource) {
super(baseResource);
}

@Override
protected Void execute(AuthCmd command) {
WebResource webResource = getBaseResource().path("/auth").build();
LOGGER.trace("POST: {}", webResource);
Response response = webResource
.accept(MediaType.APPLICATION_JSON)
.entity(command.getAuthConfig(), MediaType.APPLICATION_JSON)
.post(Response.class);

if(response.getStatus() == 401) {
throw new UnauthorizedException("Unauthorized");
};

return null;
}

}
Loading