Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.fabric8.docker.client.impl.ContainerLog Maven / Gradle / Ivy
/*
* Copyright (C) 2016 Original Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.fabric8.docker.client.impl;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.ws.WebSocketCall;
import io.fabric8.docker.client.Config;
import io.fabric8.docker.client.DockerClientException;
import io.fabric8.docker.dsl.OutputHandle;
import io.fabric8.docker.dsl.container.FollowOrDisplayInterface;
import io.fabric8.docker.dsl.container.FollowOrDisplayOrContainerErrorOrTimestampsOrTailingLinesInterface;
import io.fabric8.docker.dsl.container.FollowOrDisplayOrContainerOutputOrContainerErrorOrTimestampsOrTailingLinesInterface;
import io.fabric8.docker.dsl.container.FollowOrDisplayOrTailingLinesInterface;
import io.fabric8.docker.dsl.container.FollowOrDisplayOrTimestampsOrTailingLinesInterface;
import io.fabric8.docker.dsl.container.SinceOrFollowOrDisplayOrContainerOutputOrContainerErrorOrTimestampsOrTailingLinesInterface;
import io.fabric8.docker.client.utils.URLUtils;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.util.concurrent.TimeUnit;
import static io.fabric8.docker.client.utils.Utils.isNotNullOrEmpty;
public class ContainerLog extends BaseContainerOperation implements
SinceOrFollowOrDisplayOrContainerOutputOrContainerErrorOrTimestampsOrTailingLinesInterface,
FollowOrDisplayOrTimestampsOrTailingLinesInterface,
FollowOrDisplayOrContainerErrorOrTimestampsOrTailingLinesInterface,
FollowOrDisplayOrTailingLinesInterface,
FollowOrDisplayInterface {
private static final String LOG = "log";
private static final String FOLLOW = "follow";
private static final String STDOUT = "stdout";
private static final String STDERR = "stderr";
private static final String SINCE = "since";
private static final String TAIL = "tail";
private static final String ALL = "all";
private static final String TIMESTAMPS = "timestamps";
private final OutputStream out;
private final OutputStream err;
private final PipedInputStream outPipe;
private final PipedInputStream errPipe;
private final String since;
private final int lines;
private final Boolean timestampsEnabled;
public ContainerLog(OkHttpClient client, Config config, String name, OutputStream out, OutputStream err, PipedInputStream outPipe, PipedInputStream errPipe, String since, int lines, Boolean timestampsEnabled) {
super(client, config, name, LOG);
this.out = out;
this.err = err;
this.outPipe = outPipe;
this.errPipe = errPipe;
this.since = since;
this.lines = lines;
this.timestampsEnabled = timestampsEnabled;
}
private OutputHandle doGetLogHandle(Boolean follow) {
try {
StringBuilder sb = new StringBuilder();
sb.append(URLUtils.join(getOperationUrl().toString(), "ws"));
sb.append("?").append(FOLLOW).append("=").append(follow);
sb.append("&").append(STDOUT).append("=").append(out != null || outPipe != null);
sb.append("&").append(STDERR).append("=").append(err != null || errPipe != null);
if (isNotNullOrEmpty(since)) {
sb.append("&").append(SINCE).append("=").append(since);
}
if (lines > 0) {
sb.append("&").append(TAIL).append("=").append(lines);
} else {
sb.append("&").append(SINCE).append("=").append(ALL);
}
if(timestampsEnabled) {
sb.append("&").append(TIMESTAMPS).append("=").append(true);
}
Request.Builder r = new Request.Builder().url(sb.toString()).get();
OkHttpClient clone = client.clone();
clone.setReadTimeout(0, TimeUnit.MILLISECONDS);
WebSocketCall webSocketCall = WebSocketCall.create(clone, r.build());
final ContainerOutputHandle handle = new ContainerOutputHandle(out, err, outPipe, errPipe);
webSocketCall.enqueue(handle);
handle.waitUntilReady();
return handle;
} catch (Throwable t) {
throw DockerClientException.launderThrowable(t);
}
}
@Override
public OutputHandle display() {
return doGetLogHandle(false);
}
@Override
public OutputHandle follow() {
return doGetLogHandle(true);
}
@Override
public FollowOrDisplayOrTimestampsOrTailingLinesInterface readingError(PipedInputStream errPipe) {
return new ContainerLog(client, config, name, out, err, outPipe, errPipe, since, lines, timestampsEnabled);
}
@Override
public FollowOrDisplayOrTimestampsOrTailingLinesInterface writingError(OutputStream err) {
return new ContainerLog(client, config, name, out, err, outPipe, errPipe, since, lines, timestampsEnabled);
}
@Override
public FollowOrDisplayOrTimestampsOrTailingLinesInterface redirectingError() {
return readingError(new PipedInputStream());
}
@Override
public FollowOrDisplayOrContainerErrorOrTimestampsOrTailingLinesInterface readingOutput(PipedInputStream outPipe) {
return new ContainerLog(client, config, name, out, err, outPipe, errPipe, since, lines, timestampsEnabled);
}
@Override
public FollowOrDisplayOrContainerErrorOrTimestampsOrTailingLinesInterface writingOutput(OutputStream out) {
return new ContainerLog(client, config, name, out, err, outPipe, errPipe, since, lines, timestampsEnabled);
}
@Override
public FollowOrDisplayOrContainerErrorOrTimestampsOrTailingLinesInterface redirectingOutput() {
return readingOutput(new PipedInputStream());
}
@Override
public FollowOrDisplayOrContainerOutputOrContainerErrorOrTimestampsOrTailingLinesInterface since(String since) {
return null;
}
@Override
public FollowOrDisplayInterface tailingLines(int lines) {
return new ContainerLog(client, config, name, out, err, outPipe, errPipe, since, lines, timestampsEnabled);
}
@Override
public FollowOrDisplayOrTailingLinesInterface withTimestamps() {
return new ContainerLog(client, config, name, out, err, outPipe, errPipe, since, lines, true);
}
}