Example usage for org.apache.http.impl.client HttpClients createMinimal

List of usage examples for org.apache.http.impl.client HttpClients createMinimal

Introduction

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

Prototype

public static CloseableHttpClient createMinimal() 

Source Link

Document

Creates CloseableHttpClient instance that implements the most basic HTTP protocol support.

Usage

From source file:org.basinmc.maven.plugins.minecraft.launcher.VersionIndex.java

/**
 * Fetches a version metadata object from the URL indicated by a supplied descriptor.
 *//*www.  j  ava  2  s .  c  o  m*/
@Nonnull
private VersionMetadata fetchMetadata(@Nonnull VersionDescriptor descriptor) {
    HttpClient client = HttpClients.createMinimal();

    try {
        HttpGet request = new HttpGet(descriptor.getUrl().toURI());
        HttpResponse response = client.execute(request);

        StatusLine status = response.getStatusLine();

        if (status.getStatusCode() != 200) {
            throw new IOException(
                    "Unexpected status code: " + status.getStatusCode() + " - " + status.getReasonPhrase());
        }

        try (InputStream inputStream = response.getEntity().getContent()) {
            return READER.forType(VersionMetadata.class).readValue(inputStream);
        }
    } catch (IOException ex) {
        throw new WrapperException(ex);
    } catch (URISyntaxException ex) {
        throw new IllegalStateException("Invalid metadata URL: " + ex.getMessage(), ex);
    }
}

From source file:com.linecorp.armeria.server.http.file.HttpFileServiceTest.java

@Test
public void testClassPathGet() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        final String lastModified;
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/foo.txt")))) {
            lastModified = assert200Ok(res, "text/plain", "foo");
        }//from   www.  java2 s . c o m

        // Test if the 'If-Modified-Since' header works as expected.
        HttpUriRequest req = new HttpGet(newUri("/foo.txt"));
        req.setHeader(HttpHeaders.IF_MODIFIED_SINCE, currentHttpDate());
        req.setHeader(HttpHeaders.CONNECTION, "close");

        try (CloseableHttpResponse res = hc.execute(req)) {
            assert304NotModified(res, lastModified);
        }
    }
}

From source file:com.linecorp.armeria.server.http.WebAppContainerTest.java

@Test
public void testAddressesAndPorts() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        try (CloseableHttpResponse res = hc.execute(new HttpGet(uri("/jsp/addrs_and_ports.jsp")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(res.getFirstHeader(HttpHeaderNames.CONTENT_TYPE.toString()).getValue(),
                    startsWith("text/html"));
            final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity())).replaceAll("");

            assertTrue(actualContent,/*  w ww .  j a  va 2s .  c o  m*/
                    actualContent.matches("<html><body>" + "<p>RemoteAddr: 127\\.0\\.0\\.1</p>"
                            + "<p>RemoteHost: 127\\.0\\.0\\.1</p>" + "<p>RemotePort: [1-9][0-9]+</p>"
                            + "<p>LocalAddr: (?!null)[^<]+</p>" + "<p>LocalName: " + server().defaultHostname()
                            + "</p>" + "<p>LocalPort: " + server().activePort().get().localAddress().getPort()
                            + "</p>" + "</body></html>"));
        }
    }
}

From source file:com.linecorp.armeria.server.ServerTest.java

private static void testInvocation0(String path) throws IOException {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        final HttpPost req = new HttpPost(uri(path));
        req.setEntity(new StringEntity("Hello, world!", StandardCharsets.UTF_8));

        try (CloseableHttpResponse res = hc.execute(req)) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Hello, world!"));
        }//www .j  ava2s.  c  o  m
    }
}

From source file:com.linecorp.armeria.testing.server.webapp.WebAppContainerTest.java

@Test
public void japanesePath() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        try (CloseableHttpResponse res = hc.execute(
                new HttpGet(server().uri("/jsp/" + URLEncoder.encode("", "UTF-8") + "/index.jsp")))) {
            assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK");
            assertThat(res.getFirstHeader(HttpHeaderNames.CONTENT_TYPE.toString()).getValue())
                    .startsWith("text/html");
            final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity())).replaceAll("");
            assertThat(actualContent).isEqualTo("<html><body>" + "<p>Hello, Armerian World!</p>"
                    + "<p>Have you heard about the class 'io.netty.buffer.ByteBuf'?</p>"
                    + "<p>Context path: </p>" + // ROOT context path
                    "<p>Request URI: /%E6%97%A5%E6%9C%AC%E8%AA%9E/index.jsp</p>"
                    + "<p>Servlet Path: //index.jsp</p>" + "</body></html>");
        }/*from   w  w w. j av  a  2 s .  c  om*/
    }
}

From source file:com.linecorp.armeria.server.http.file.HttpFileServiceTest.java

@Test
public void testClassPathOrElseGet() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal();
            CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/bar.txt")))) {
        assert200Ok(res, "text/plain", "bar");
    }/*from  w  w  w.j a v  a2 s  .c o  m*/
}

From source file:com.linecorp.armeria.server.http.file.HttpFileServiceTest.java

@Test
public void testUnknownMediaType() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal();
            CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/bar.unknown")))) {
        final String lastModified = assert200Ok(res, null, "Unknown Media Type");

        HttpUriRequest req = new HttpGet(newUri("/bar.unknown"));
        req.setHeader(HttpHeaders.IF_MODIFIED_SINCE, currentHttpDate());
        req.setHeader(HttpHeaders.CONNECTION, "close");

        try (CloseableHttpResponse resCached = hc.execute(req)) {
            assert304NotModified(resCached, lastModified);
        }/*from w w  w .j  av a2 s.c  o  m*/
    }
}

From source file:com.linecorp.armeria.server.http.auth.AuthServiceTest.java

@Test
public void testAuth() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        try (CloseableHttpResponse res = hc.execute(getRequest("/", "unit test"))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
        }//from   ww w .  j  a  v  a  2s  .  c  o  m
        try (CloseableHttpResponse res = hc.execute(getRequest("/", "UNIT TEST"))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 401 Unauthorized"));
        }
    }
}

From source file:org.rapidpm.microservice.optionals.service.ServiceWrapper.java

private static String callRest(String restBaseUrl, String adminRestPath) throws IOException {
    CloseableHttpClient httpClient = HttpClients.createMinimal();
    HttpGet httpGet = new HttpGet(restBaseUrl + adminRestPath + "/" + DELAY);
    final CloseableHttpResponse response = httpClient.execute(httpGet);
    return IOUtils.toString(response.getEntity().getContent());
}