Example usage for org.apache.http.impl.client HttpClientBuilder create

List of usage examples for org.apache.http.impl.client HttpClientBuilder create

Introduction

In this page you can find the example usage for org.apache.http.impl.client HttpClientBuilder create.

Prototype

public static HttpClientBuilder create() 

Source Link

Usage

From source file:org.commonjava.maven.firth.client.ArtifactClient.java

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;//from www.ja v  a 2s .  c  o  m
    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);
}

From source file:org.fcrepo.it.BasicIT.java

private static HttpClient createClient() {
    return HttpClientBuilder.create().setMaxConnPerRoute(MAX_VALUE).setMaxConnTotal(MAX_VALUE).build();
}

From source file:steamdb.parser.HttpGetRequest.java

HttpGetRequest() {
    client = HttpClientBuilder.create().build();
}

From source file:coolmapplugin.util.HTTPRequestUtil.java

public static boolean executePut(String targetURL, String jsonBody) {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

        HttpPut request = new HttpPut(targetURL);

        if (jsonBody != null && !jsonBody.isEmpty()) {
            StringEntity params = new StringEntity(jsonBody);
            request.addHeader("content-type", "application/json");
            request.setEntity(params);/* ww  w .  j a va2s  .c o  m*/
        }

        HttpResponse result = httpClient.execute(request);

        if (result.getStatusLine().getStatusCode() == 412 || result.getStatusLine().getStatusCode() == 500) {
            return false;
        }

    } catch (IOException e) {
        return false;
    }

    return true;
}

From source file:com.gooddata.http.client.SimpleSSTRetrievalStrategyTest.java

@Before
public void setUp() {
    httpClient = HttpClientBuilder.create().build();
    host = new HttpHost("server.com", 666);
}

From source file:com.igormaznitsa.zxpoly.utils.ROMLoader.java

static byte[] loadHTTPArchive(final String url) throws IOException {
    final HttpClient client = HttpClientBuilder.create().build();
    final HttpContext context = HttpClientContext.create();
    final HttpGet get = new HttpGet(url);
    final HttpResponse response = client.execute(get, context);

    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        InputStream in = null;// ww  w  . j ava 2 s .  c o m
        try {
            final HttpEntity entity = response.getEntity();
            in = entity.getContent();
            final byte[] data = entity.getContentLength() < 0L ? IOUtils.toByteArray(entity.getContent())
                    : IOUtils.toByteArray(entity.getContent(), entity.getContentLength());
            return data;
        } finally {
            IOUtils.closeQuietly(in);
        }
    } else {
        throw new IOException("Can't download from http '" + url + "' code [" + url + ']');
    }
}

From source file:org.jitsi.meet.test.util.JvbUtil.java

/**
 * Triggers either force or graceful bridge shutdown and waits for it to
 * complete./*from   www .  j  ava 2  s. c om*/
 *
 * @param jvbEndpoint the REST API endpoint of the bridge to be turned off.
 * @param force <tt>true</tt> if force shutdown should be performed or
 *              <tt>false</tt> to shutdown the bridge gracefully.
 *
 * @throws IOException if something goes wrong
 * @throws InterruptedException if the waiting thread gets interrupted at
 *                              any point.
 */
static public void shutdownBridge(String jvbEndpoint, boolean force) throws IOException, InterruptedException {
    CloseableHttpClient client = HttpClientBuilder.create().build();

    try {
        triggerShutdown(client, jvbEndpoint, force);

        try {
            waitForBridgeShutdown(client, jvbEndpoint);
        } catch (HttpHostConnectException connectException) {
            // We ignore connect exception as JVB endpoint
            // dies on shutdown and may not always send the OK response
        }
    } finally {
        client.close();
    }
}

From source file:com.blacklocus.jres.http.HttpClientFactory.java

/**
 * Construct a new HttpClient which uses the {@link #POOL_MGR default connection pool}.
 *
 * @param connectionTimeoutMs highly sensitive to application so must be specified
 * @param socketTimeoutMs     highly sensitive to application so must be specified
 *///from  w w  w .jav a  2 s  . c  o m
public static HttpClient create(final int connectionTimeoutMs, final int socketTimeoutMs) {
    return HttpClientBuilder.create().setConnectionManager(POOL_MGR)
            .setDefaultRequestConfig(RequestConfig.copy(RequestConfig.DEFAULT)
                    .setConnectTimeout(connectionTimeoutMs).setSocketTimeout(socketTimeoutMs).build())
            .build();
}

From source file:net.locosoft.fold.neo4j.internal.Neo4jRestUtil.java

public static JsonObject doPostJson(String uri, JsonObject content) {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        HttpPost httpPost = new HttpPost(uri);
        httpPost.addHeader("Content-Type", "application/json");
        StringEntity stringEntity = new StringEntity(content.toString(), "UTF-8");
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse response = httpClient.execute(httpPost);

        String bodyText = EntityUtils.toString(response.getEntity());
        JsonObject jsonObject = JsonObject.readFrom(bodyText);
        return jsonObject;
    } catch (Exception ex) {
        ex.printStackTrace();//from w  w  w  .  j  av a 2 s . c om
    }
    return null;
}

From source file:org.eclipse.cbi.common.signing.Signer.java

public static void signFile(File source, File target, String signerUrl)
        throws IOException, MojoExecutionException {
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(signerUrl);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

    builder.addPart("file", new FileBody(source));
    post.setEntity(builder.build());//  ww  w.  ja va  2 s .c  o m

    HttpResponse response = client.execute(post);
    int statusCode = response.getStatusLine().getStatusCode();

    HttpEntity resEntity = response.getEntity();

    if (statusCode >= 200 && statusCode <= 299 && resEntity != null) {
        InputStream is = resEntity.getContent();
        try {
            FileUtils.copyStreamToFile(new RawInputStreamFacade(is), target);
        } finally {
            IOUtil.close(is);
        }
    } else if (statusCode >= 500 && statusCode <= 599) {
        InputStream is = resEntity.getContent();
        String message = IOUtil.toString(is, "UTF-8");
        throw new NoHttpResponseException("Server failed with " + message);
    } else {
        throw new MojoExecutionException("Signer replied " + response.getStatusLine());
    }
}