All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fabric8.docker.client.impl.EventOperationImpl Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
/*
 * 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 okhttp3.OkHttpClient;
import okhttp3.Request;
import io.fabric8.docker.client.Config;
import io.fabric8.docker.client.DockerClientException;
import io.fabric8.docker.client.utils.Utils;
import io.fabric8.docker.dsl.OutputHandle;
import io.fabric8.docker.dsl.misc.EventsInterface;
import io.fabric8.docker.dsl.misc.ListFiltersInterface;
import io.fabric8.docker.dsl.misc.UntilFiltersListInterface;

import java.io.PipedOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class EventOperationImpl extends OperationSupport implements
        EventsInterface,
        ListFiltersInterface,
        UntilFiltersListInterface {

    private static final String EVENTS_RESOURCE = "events";
    private static final String SINCE = "since";
    private static final String UNTIL = "until";
    private static final String FILTERS = "filters";

    private final String since;
    private final String until;
    private final Map filters;

    public EventOperationImpl(OkHttpClient client, Config config) {
        this(client, config, null, null, new HashMap());
    }

    public EventOperationImpl(OkHttpClient client, Config config, String since, String until, Map filters) {
        super(client, config, EVENTS_RESOURCE);
        this.since = since;
        this.until = until;
        this.filters = filters;
    }


    @Override
    public OutputHandle list() {
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(getOperationUrl())
                    .append(Q).append(FILTERS).append(EQUALS).append(JSON_MAPPER.writeValueAsString(filters));
            
            if (Utils.isNotNullOrEmpty(since)) {
                sb.append(A).append(SINCE).append(EQUALS).append(since);
            }

            if (Utils.isNotNullOrEmpty(until)) {
                sb.append(A).append(UNTIL).append(EQUALS).append(until);
            }


            Request request = new Request.Builder().get().url(sb.toString()).build();
            OkHttpClient clone = client.newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
            EventHandle handle = new EventHandle(new PipedOutputStream(), config.getRequestTimeout(), TimeUnit.MILLISECONDS);
            clone.newCall(request).enqueue(handle);
            return handle;
        } catch (Exception e) {
            throw DockerClientException.launderThrowable(e);
        }
    }

    @Override
    public ListFiltersInterface filters(String key, String value) {
        Map newFilters = this.filters != null
                ? new HashMap<>(this.filters)
                : new HashMap();

        newFilters.put(key, new String[]{value});
        return new EventOperationImpl(client, config, since, until, newFilters);
    }

    @Override
    public UntilFiltersListInterface since(String since) {
        return new EventOperationImpl(client, config, since, until, filters);
    }

    @Override
    public ListFiltersInterface until(String until) {
        return new EventOperationImpl(client, config, since, until, filters);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy