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:com.linecorp.armeria.server.http.auth.HttpAuthServiceTest.java

@Test
public void testOnSuccessException() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        try (CloseableHttpResponse res = hc.execute(new HttpGet(server.uri("/on_success_exception")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 500 Internal Server Error"));
        }//www .  j  ava 2  s.c  om
    }
}

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

@Test
public void testDynamicHttpServices() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        // Run case 1.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/1/int/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Integer: 42"));
        }//w ww  .j  a v a  2s  .c  o m
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/1/int-async/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Integer: 43"));
        }
        // Run case 2.
        try (CloseableHttpResponse res = hc.execute(new HttpPost(rule.httpUri("/1/long/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Number[42]"));
        }
        // Run case 3.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/1/string/blah")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("String: blah"));
        }
        // Run case 1 but illegal parameter.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/1/int/fourty-two")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 500 Internal Server Error"));
        }
        // Run case 2 but without parameter (non-existing url).
        try (CloseableHttpResponse res = hc.execute(new HttpPost(rule.httpUri("/1/long/")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 404 Not Found"));
        }
        // Run case 3 but with not-mapped HTTP method (Post).
        try (CloseableHttpResponse res = hc.execute(new HttpPost(rule.httpUri("/1/string/blah")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 405 Method Not Allowed"));
        }
        // Get a requested path as typed string from ServiceRequestContext or HttpRequest
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/1/path/ctx/async/1")))) {
            assertThat(EntityUtils.toString(res.getEntity()), is("String[/1/path/ctx/async/1]"));
        }
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/1/path/req/async/1")))) {
            assertThat(EntityUtils.toString(res.getEntity()), is("String[/1/path/req/async/1]"));
        }
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/1/path/ctx/sync/1")))) {
            assertThat(EntityUtils.toString(res.getEntity()), is("String[/1/path/ctx/sync/1]"));
        }
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/1/path/req/sync/1")))) {
            assertThat(EntityUtils.toString(res.getEntity()), is("String[/1/path/req/sync/1]"));
        }
        // Exceptions in business logic
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/1/exception/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 500 Internal Server Error"));
        }
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/1/exception-async/1")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 500 Internal Server Error"));
        }

        // Run case 4.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/2/int/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Number[42]"));
        }
        // Run case 5.
        try (CloseableHttpResponse res = hc.execute(new HttpPost(rule.httpUri("/2/long/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Number[42]"));
        }
        // Run case 6.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/2/string/blah")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("String: blah"));
        }
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/2/boolean/true")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("String[true]"));
        }
        // Run case 4 but illegal parameter.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/2/int/fourty-two")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 500 Internal Server Error"));
        }
        // Run case 5 but without parameter (non-existing url).
        try (CloseableHttpResponse res = hc.execute(new HttpPost(rule.httpUri("/2/long/")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 404 Not Found"));
        }
        // Run case 6 but with not-mapped HTTP method (Post).
        try (CloseableHttpResponse res = hc.execute(new HttpPost(rule.httpUri("/2/string/blah")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 405 Method Not Allowed"));
        }

        // Test the case where multiple annotated services are bound under the same path prefix.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/3/int/42")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("Number[42]"));
        }
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/3/string/blah")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("String[blah]"));
        }
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/3/no-path-param")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("String[no-path-param]"));
        }
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/3/undefined")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 404 Not Found"));
        }
    }
}

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

@Test
public void addressesAndPorts_localhost() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        HttpGet request = new HttpGet(server().uri("/jsp/addrs_and_ports.jsp"));
        request.setHeader("Host", "localhost:1111");
        try (CloseableHttpResponse res = hc.execute(request)) {
            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).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().server().defaultHostname()
                    + "</p>" + "<p>LocalPort: " + server().httpPort() + "</p>" + "<p>ServerName: localhost</p>"
                    + "<p>ServerPort: 1111</p>" + "</body></html>");
        }//from ww  w . j  a  v a2  s .c  o  m
    }
}

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

@Test
public void testContentLength() throws Exception {
    // Test if the server responds with the 'content-length' header
    // even if it is the last response of the connection.
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        HttpUriRequest req = new HttpGet(newUri("/200"));
        req.setHeader("Connection", "Close");
        try (CloseableHttpResponse res = hc.execute(req)) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(res.containsHeader("Content-Length"), is(true));
            assertThat(res.getHeaders("Content-Length").length, is(1));
            assertThat(res.getHeaders("Content-Length")[0].getValue(), is("6"));
            assertThat(EntityUtils.toString(res.getEntity()), is("200 OK"));
        }//from  ww  w.j a  v a  2s . c  o  m
    }

    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        // Ensure the HEAD response does not have content.
        try (CloseableHttpResponse res = hc.execute(new HttpHead(newUri("/200")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(res.getEntity(), is(nullValue()));
        }

        // Ensure the 204 response does not have content.
        try (CloseableHttpResponse res = hc.execute(new HttpGet(newUri("/204")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 204 No Content"));
            assertThat(res.getEntity(), is(nullValue()));
        }
    }
}

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

@Test
public void testNonDefaultPathMappings() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        // Exact pattern
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/4/exact")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("String[exact:/4/exact]"));
        }/*  w  ww .j a v  a 2s .c  o m*/

        // Prefix pattern
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/4/prefix/foo")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("String[prefix:/4/prefix/foo:/foo]"));
        }

        // Glob pattern
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/4/glob1/bar")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("String[glob1:/4/glob1/bar]"));
        }
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/4/baz/glob2")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("String[glob2:/4/baz/glob2:0]"));
        }

        // Regex pattern
        try (CloseableHttpResponse res = hc.execute(new HttpGet(rule.httpUri("/4/regex/foo/bar")))) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is("String[regex:/4/regex/foo/bar:foo/bar]"));
        }
    }
}

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

@Test
public void testDynamicHttpService_aggregation() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        HttpPost httpPost;/*from   w ww.j a v  a  2s  . c o m*/

        httpPost = newHttpPost("/3/a/string");
        try (CloseableHttpResponse res = hc.execute(httpPost)) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is(EntityUtils.toString(httpPost.getEntity())));
        }
        httpPost = newHttpPost("/3/a/string-async1");
        try (CloseableHttpResponse res = hc.execute(httpPost)) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is(EntityUtils.toString(httpPost.getEntity())));
        }
        httpPost = newHttpPost("/3/a/string-async2");
        try (CloseableHttpResponse res = hc.execute(httpPost)) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is(EntityUtils.toString(httpPost.getEntity())));
        }
        httpPost = newHttpPost("/3/a/string-aggregate-response1");
        try (CloseableHttpResponse res = hc.execute(httpPost)) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is(EntityUtils.toString(httpPost.getEntity())));
        }
        httpPost = newHttpPost("/3/a/string-aggregate-response2");
        try (CloseableHttpResponse res = hc.execute(httpPost)) {
            assertThat(res.getStatusLine().toString(), is("HTTP/1.1 200 OK"));
            assertThat(EntityUtils.toString(res.getEntity()), is(EntityUtils.toString(httpPost.getEntity())));
        }
    }
}

From source file:org.basinmc.maven.plugins.bsdiff.DiffMojo.java

/**
 * Retrieves a remote artifact and stores it in a pre-defined cache directory.
 *//* w w w.j  a va2 s .  co  m*/
@Nonnull
private Path retrieveSourceFile() throws MojoFailureException {
    HttpClient client = HttpClients.createMinimal();
    String fileName;

    {
        String path = this.sourceURL.getPath();

        int i = path.lastIndexOf('/');
        fileName = path.substring(i + 1);
    }

    try {
        this.getLog().info("Downloading source artifact from " + this.sourceURL.toExternalForm());

        HttpGet request = new HttpGet(this.sourceURL.toURI());
        HttpResponse response = client.execute(request);

        if (response.containsHeader("Content-Disposition")) {
            String disposition = response.getLastHeader("Content-Disposition").getValue();
            Matcher matcher = DISPOSITION_PATTERN.matcher(disposition);

            if (matcher.matches()) {
                fileName = URLDecoder.decode(matcher.group(1), "UTF-8");
            }
        }

        this.getLog().info("Storing " + fileName + " in cache directory");
        Path outputPath = this.cacheDirectory.toPath().resolve(fileName);

        if (!Files.isDirectory(outputPath.getParent())) {
            Files.createDirectories(outputPath.getParent());
        }

        try (InputStream inputStream = response.getEntity().getContent()) {
            try (ReadableByteChannel inputChannel = Channels.newChannel(inputStream)) {
                try (FileChannel outputChannel = FileChannel.open(outputPath, StandardOpenOption.CREATE,
                        StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
                    outputChannel.transferFrom(inputChannel, 0, Long.MAX_VALUE);
                }
            }
        }

        return outputPath;
    } catch (IOException ex) {
        throw new MojoFailureException("Failed to read/write source artifact: " + ex.getMessage(), ex);
    } catch (URISyntaxException ex) {
        throw new MojoFailureException("Invalid source URI: " + ex.getMessage(), ex);
    }
}