|
| 1 | +package com.github.dockerjava.ahc; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.net.URL; |
| 5 | +import java.util.concurrent.Future; |
| 6 | + |
| 7 | +import com.github.dockerjava.api.async.ResultCallback; |
| 8 | +import com.github.dockerjava.api.command.AttachContainerCmd; |
| 9 | +import com.github.dockerjava.api.model.Frame; |
| 10 | +import com.github.dockerjava.core.DockerClientConfig; |
| 11 | +import com.github.dockerjava.core.async.FrameStreamProcessor; |
| 12 | +import com.github.dockerjava.jaxrs.async.AbstractCallbackNotifier; |
| 13 | +import com.github.dockerjava.jaxrs.async.POSTCallbackNotifier; |
| 14 | + |
| 15 | +import org.asynchttpclient.*; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +import javax.ws.rs.client.WebTarget; |
| 20 | + |
| 21 | +public class AttachContainerCmdExec implements AttachContainerCmd.Exec { |
| 22 | + |
| 23 | + private static final Logger LOGGER = LoggerFactory.getLogger(AttachContainerCmdExec.class); |
| 24 | + |
| 25 | + private URL baseResource; |
| 26 | + |
| 27 | + private DockerClientConfig dockerClientConfig; |
| 28 | + |
| 29 | + public AttachContainerCmdExec(URL baseResource, DockerClientConfig dockerClientConfig) { |
| 30 | + this.baseResource = baseResource; |
| 31 | + this.dockerClientConfig = dockerClientConfig; |
| 32 | + } |
| 33 | + |
| 34 | + // @Override |
| 35 | + // protected AbstractCallbackNotifier<Frame> callbackNotifier(AttachContainerCmd command, |
| 36 | + // ResultCallback<Frame> resultCallback) { |
| 37 | + // |
| 38 | + // WebTarget webTarget = getBaseResource().path("/containers/{id}/attach").resolveTemplate("id", |
| 39 | + // command.getContainerId()); |
| 40 | + // |
| 41 | + // webTarget = booleanQueryParam(webTarget, "logs", command.hasLogsEnabled()); |
| 42 | + // webTarget = booleanQueryParam(webTarget, "stdout", command.hasStdoutEnabled()); |
| 43 | + // // webTarget = booleanQueryParam(webTarget, "stdin", command.hasStdinEnabled()); |
| 44 | + // webTarget = booleanQueryParam(webTarget, "stderr", command.hasStderrEnabled()); |
| 45 | + // webTarget = booleanQueryParam(webTarget, "stream", command.hasFollowStreamEnabled()); |
| 46 | + // |
| 47 | + // LOGGER.trace("POST: {}", webTarget); |
| 48 | + // |
| 49 | + // return new POSTCallbackNotifier<Frame>(new FrameStreamProcessor(), resultCallback, webTarget.request(), null); |
| 50 | + // } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Void exec(AttachContainerCmd command, ResultCallback<Frame> resultCallback) { |
| 54 | + AsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder().build(); |
| 55 | + |
| 56 | + AsyncHttpClient c = new DefaultAsyncHttpClient(config); |
| 57 | + c.prepareGet("http://www.ning.com/").execute(new AsyncHandler<String>() { |
| 58 | + private ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 59 | + |
| 60 | + @Override |
| 61 | + public State onStatusReceived(HttpResponseStatus status) throws Exception { |
| 62 | + int statusCode = status.getStatusCode(); |
| 63 | + // The Status have been read |
| 64 | + // If you don't want to read the headers,body or stop processing the response |
| 65 | + if (statusCode >= 500) { |
| 66 | + return State.ABORT; |
| 67 | + } |
| 68 | + |
| 69 | + return State.CONTINUE; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public State onHeadersReceived(HttpResponseHeaders h) throws Exception { |
| 74 | + |
| 75 | + // The headers have been read |
| 76 | + // If you don't want to read the body, or stop processing the response |
| 77 | + return State.CONTINUE; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { |
| 82 | + bytes.write(bodyPart.getBodyPartBytes()); |
| 83 | + return State.CONTINUE; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public String onCompleted() throws Exception { |
| 88 | + // Will be invoked once the response has been fully read or a ResponseComplete exception |
| 89 | + // has been thrown. |
| 90 | + // NOTE: should probably use Content-Encoding from headers |
| 91 | + return bytes.toString("UTF-8"); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onThrowable(Throwable t) { |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + return null; |
| 100 | + } |
| 101 | +} |
0 commit comments