Search in sources :

Example 1 with HttpResources

use of org.commonjava.indy.client.core.helper.HttpResources in project indy by Commonjava.

the class DownloadContentHasLengthHeaderTest method proxyRemoteArtifact.

@Test
public void proxyRemoteArtifact() throws Exception {
    byte[] data = ("This is a test: " + System.nanoTime()).getBytes();
    final InputStream stream = new ByteArrayInputStream(data);
    final String path = "org/foo/foo-project/1/foo-1.txt";
    server.expect(server.formatUrl(STORE, path), 200, stream);
    client.stores().create(new RemoteRepository(STORE, server.formatUrl(STORE)), "adding test proxy", RemoteRepository.class);
    try (HttpResources httpResources = client.module(IndyRawHttpModule.class).getHttp().getRaw(client.content().contentPath(remote, STORE, path))) {
        HttpResponse response = httpResources.getResponse();
        String contentLength = response.getFirstHeader("Content-Length").getValue();
        assertThat("Wrong content-length for download: " + contentLength + " (should have been: " + data.length + ")", contentLength, equalTo(Integer.toString(data.length)));
    }
    final PathInfo result = client.content().getInfo(remote, STORE, path);
    assertThat("no result", result, notNullValue());
    assertThat("doesn't exist", result.exists(), equalTo(true));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) HttpResponse(org.apache.http.HttpResponse) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) PathInfo(org.commonjava.indy.client.core.helper.PathInfo) Test(org.junit.Test) AbstractContentManagementTest(org.commonjava.indy.ftest.core.AbstractContentManagementTest)

Example 2 with HttpResources

use of org.commonjava.indy.client.core.helper.HttpResources in project indy by Commonjava.

the class IndyClientHttp method getRaw.

public HttpResources getRaw(final String path, final Map<String, String> headers) throws IndyClientException {
    connect();
    CloseableHttpResponse response = null;
    try {
        final HttpGet req = newRawGet(buildUrl(baseUrl, path));
        final CloseableHttpClient client = newClient();
        response = client.execute(req, newContext());
        return new HttpResources(req, response, client);
    } catch (final IOException e) {
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } finally {
    // DO NOT CLOSE!!!! We're handing off control of the response to the caller!
    //            closeQuietly( response );
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Example 3 with HttpResources

use of org.commonjava.indy.client.core.helper.HttpResources in project indy by Commonjava.

the class IndyClientHttp method getRaw.

public HttpResources getRaw(final HttpGet req) throws IndyClientException {
    connect();
    CloseableHttpResponse response = null;
    try {
        final CloseableHttpClient client = newClient();
        response = client.execute(req, newContext());
        return new HttpResources(req, response, client);
    } catch (final IOException e) {
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } finally {
    // DO NOT CLOSE!!!! We're handing off control of the response to the caller!
    //            closeQuietly( response );
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Example 4 with HttpResources

use of org.commonjava.indy.client.core.helper.HttpResources in project indy by Commonjava.

the class SettingsGeneratedForRemoteRepoTest method generateSettingsXml.

@Test
public void generateSettingsXml() throws Exception {
    final IndyClientHttp http = getHttp();
    // all mavdav requests are siblings of the default base-url suffix '/api/'
    final String url = getDotMavenUrl("settings/remote/settings-central.xml");
    System.out.println("Requesting: " + url);
    final HttpResources resources = http.getRaw(new HttpGet(url));
    InputStream stream = null;
    Settings settings = null;
    try {
        stream = resources.getResponseStream();
        settings = new SettingsXpp3Reader().read(stream);
    } finally {
        closeQuietly(stream);
        closeQuietly(resources);
    }
    assertThat(settings.getLocalRepository(), equalTo("${user.home}/.m2/repo-remote-central"));
    assertThat(settings.getMirrors(), notNullValue());
    assertThat(settings.getMirrors().size(), equalTo(1));
    final Mirror mirror = settings.getMirrors().get(0);
    assertThat(mirror.getUrl(), equalTo(http.toIndyUrl("remote/central")));
}
Also used : InputStream(java.io.InputStream) IndyClientHttp(org.commonjava.indy.client.core.IndyClientHttp) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) HttpGet(org.apache.http.client.methods.HttpGet) SettingsXpp3Reader(org.apache.maven.settings.io.xpp3.SettingsXpp3Reader) Mirror(org.apache.maven.settings.Mirror) Settings(org.apache.maven.settings.Settings) Test(org.junit.Test)

Example 5 with HttpResources

use of org.commonjava.indy.client.core.helper.HttpResources in project indy by Commonjava.

the class IndyFoloAdminClientModule method sealTrackingRecord.

public boolean sealTrackingRecord(String trackingId) throws IndyClientException {
    http.connect();
    HttpPost request = http.newRawPost(UrlUtils.buildUrl(http.getBaseUrl(), "/folo/admin", trackingId, "record"));
    HttpResources resources = null;
    try {
        resources = http.execute(request);
        HttpResponse response = resources.getResponse();
        StatusLine sl = response.getStatusLine();
        if (sl.getStatusCode() != 200) {
            if (sl.getStatusCode() == 404) {
                return false;
            }
            throw new IndyClientException(sl.getStatusCode(), "Error sealing tracking record %s.\n%s", trackingId, new IndyResponseErrorDetails(response));
        }
        return true;
    } finally {
        IOUtils.closeQuietly(resources);
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpPost(org.apache.http.client.methods.HttpPost) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) HttpResponse(org.apache.http.HttpResponse) IndyClientException(org.commonjava.indy.client.core.IndyClientException) IndyResponseErrorDetails(org.commonjava.indy.client.core.IndyResponseErrorDetails)

Aggregations

HttpResources (org.commonjava.indy.client.core.helper.HttpResources)10 IOException (java.io.IOException)6 HttpResponse (org.apache.http.HttpResponse)4 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)4 HttpGet (org.apache.http.client.methods.HttpGet)3 IndyClientException (org.commonjava.indy.client.core.IndyClientException)3 InputStream (java.io.InputStream)2 StatusLine (org.apache.http.StatusLine)2 HttpPost (org.apache.http.client.methods.HttpPost)2 IndyClientHttp (org.commonjava.indy.client.core.IndyClientHttp)2 IndyResponseErrorDetails (org.commonjava.indy.client.core.IndyResponseErrorDetails)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ZipInputStream (java.util.zip.ZipInputStream)1 Header (org.apache.http.Header)1 StringEntity (org.apache.http.entity.StringEntity)1 Mirror (org.apache.maven.settings.Mirror)1 Settings (org.apache.maven.settings.Settings)1 SettingsXpp3Reader (org.apache.maven.settings.io.xpp3.SettingsXpp3Reader)1