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

io.fabric8.docker.client.impl.ContainerList 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 com.squareup.okhttp.OkHttpClient;
import io.fabric8.docker.api.model.Container;
import io.fabric8.docker.client.Config;
import io.fabric8.docker.client.DockerClientException;
import io.fabric8.docker.dsl.container.AllOrRunningOrFiltersInterface;
import io.fabric8.docker.dsl.container.BeforeOrSizeOrFiltersOrAllOrRunningInterface;
import io.fabric8.docker.dsl.container.LimitOrSinceOrBeforeOrSizeOrFiltersOrAllOrRunningInterface;
import io.fabric8.docker.dsl.container.SinceOrBeforeOrSizeOrFiltersOrAllOrRunningInterface;
import io.fabric8.docker.dsl.container.SizeOrFiltersOrAllOrRunningInterface;
import io.fabric8.docker.client.utils.Utils;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContainerList extends BaseContainerOperation implements
        LimitOrSinceOrBeforeOrSizeOrFiltersOrAllOrRunningInterface>,
        SizeOrFiltersOrAllOrRunningInterface>,
        BeforeOrSizeOrFiltersOrAllOrRunningInterface>,
        SinceOrBeforeOrSizeOrFiltersOrAllOrRunningInterface>,
        AllOrRunningOrFiltersInterface> {

    private final String before;
    private final String since;
    private final String size;
    private final Map filters;
    private final int limit;


    public ContainerList(OkHttpClient client, Config config, String before, String since, String size, Map filters, int limit) {
        super(client, config, null, JSON);
        this.before = before;
        this.since = since;
        this.size = size;
        this.filters = filters;
        this.limit = limit;
    }


    private List doList(Boolean all) {
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(getOperationUrl().toString());
            sb.append("?all=").append(all);

            if (Utils.isNotNullOrEmpty(before)) {
                sb.append("&before=").append(before);
            }

            if (Utils.isNotNullOrEmpty(since)) {
                sb.append("&since=").append(since);
            }

            if (limit > 0) {
                sb.append("&limit=").append(limit);
            }

            if (filters != null && !filters.isEmpty()) {
                sb.append("&filters=");
               /* boolean first = true;
                for (Map.Entry entry : filters.entrySet()) {
                    if (first) {
                        first=false;
                    } else {
                        sb.append(",");
                    }
                   sb.append(entry.getKey()).append(":").append(entry.getValue());
                }*/
                sb.append(JSON_MAPPER.writeValueAsString(filters));
            }

            URL requestUrl = new URL(sb.toString());
            return handleList(requestUrl, Container.class);
        } catch (Exception e) {
            throw DockerClientException.launderThrowable(e);
        }
    }

    @Override
    public List all() {
        return doList(true);
    }

    @Override
    public List running() {
        return doList(false);
    }


    @Override
    public SizeOrFiltersOrAllOrRunningInterface> before(String before) {
        return new ContainerList(client, config, before, since, size, filters, limit);
    }

    @Override
    public AllOrRunningOrFiltersInterface> filters(String key, String value) {
        Map newFilters = new HashMap<>(this.filters);
        newFilters.put(key, new String[]{value});
        return new ContainerList(client, config, before, since, size, newFilters, limit);
    }

    @Override
    public SinceOrBeforeOrSizeOrFiltersOrAllOrRunningInterface> limit(int limit) {
        return new ContainerList(client, config, before, since, size, filters, limit);
    }

    @Override
    public BeforeOrSizeOrFiltersOrAllOrRunningInterface> since(String since) {
        return new ContainerList(client, config, before, since, size, filters, limit);
    }

    @Override
    public AllOrRunningOrFiltersInterface> size(String id) {
        return new ContainerList(client, config, before, since, size, filters, limit);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy