org.commonjava.maven.firth.client.ArtifactClient.java Source code

Java tutorial

Introduction

Here is the source code for org.commonjava.maven.firth.client.ArtifactClient.java

Source

/*******************************************************************************
 * Copyright (c) 2014 Red Hat, Inc..
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 *   
 * Contributors:
 *     Red Hat, Inc. - initial API and implementation
 *******************************************************************************/
package org.commonjava.maven.firth.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.commonjava.maven.firth.model.dto.SourceComposition;
import org.commonjava.maven.firth.model.dto.SourceManifest;
import org.commonjava.maven.firth.model.index.ArtifactMap;
import org.commonjava.maven.firth.util.PathUtils;

import com.fasterxml.jackson.databind.ObjectMapper;

public class ArtifactClient {
    private final HttpClient http;

    private final String baseUrl;

    private final ArtifactMap artifactMap;

    public static ArtifactClient load(final String baseUrl, final String mapsBasePath,
            final String packagePathFormat, final String source, final String version) throws IOException {
        final HttpClient http = HttpClientBuilder.create().build();

        // Assume something like: http://download.devel.redhat.com/brewroot/artifact-maps/rcm-mw-tools-build/1234/composition.json
        final String compsUrl = PathUtils.normalize(baseUrl, mapsBasePath, source, version, "composition.json");

        final ObjectMapper om = new ObjectMapper();

        HttpGet request = new HttpGet(compsUrl);
        HttpResponse response = http.execute(request);

        SourceComposition comp;
        if (response.getStatusLine().getStatusCode() == 200) {
            comp = om.readValue(response.getEntity().getContent(), SourceComposition.class);
        } else {
            throw new IOException("Failed to read composition from: " + compsUrl);
        }

        final List<String> manifestSources = new ArrayList<String>();
        manifestSources.add(source);
        manifestSources.addAll(comp.getComposedOf());

        final List<SourceManifest> manifests = new ArrayList<SourceManifest>();
        for (final String src : manifestSources) {
            // Assume something like: http://download.devel.redhat.com/brewroot/artifact-maps/rcm-mw-tools-build/1234/manifest.json
            final String manifestUrl = PathUtils.normalize(baseUrl, mapsBasePath, src, version, "manifest.json");
            request = new HttpGet(manifestUrl);
            response = http.execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                final SourceManifest manifest = om.readValue(response.getEntity().getContent(),
                        SourceManifest.class);

                manifests.add(manifest);
            } else {
                throw new IOException("Failed to read manifest from: " + manifestUrl);
            }
        }

        final ArtifactMap am = new ArtifactMap(packagePathFormat, manifests);

        return new ArtifactClient(baseUrl, am, http);
    }

    private ArtifactClient(final String baseUrl, final ArtifactMap artifactMap, final HttpClient http) {
        this.baseUrl = baseUrl;
        this.artifactMap = artifactMap;
        this.http = http;
    }

    public ArtifactTransfer newTransfer(final String path) {
        final String url = artifactMap.buildArtifactUrl(baseUrl, path);
        return url == null ? null : new ArtifactTransfer(http, url);
    }
}