Search in sources :

Example 86 with HttpClient

use of org.apache.http.client.HttpClient in project camel by apache.

the class CMSenderOneMessageImpl method doHttpPost.

private void doHttpPost(final String urlString, final String requestString) {
    final HttpClient client = HttpClientBuilder.create().build();
    final HttpPost post = new HttpPost(urlString);
    post.setEntity(new StringEntity(requestString, Charset.forName("UTF-8")));
    try {
        final HttpResponse response = client.execute(post);
        final int statusCode = response.getStatusLine().getStatusCode();
        LOG.debug("Response Code : {}", statusCode);
        if (statusCode == 400) {
            throw new CMDirectException("CM Component and CM API show some kind of inconsistency. " + "CM is complaining about not using a post method for the request. And this component only uses POST requests. What happens?");
        }
        if (statusCode != 200) {
            throw new CMDirectException("CM Component and CM API show some kind of inconsistency. The component expects the status code to be 200 or 400. New api released? ");
        }
        // So we have 200 status code...
        // The response type is 'text/plain' and contains the actual
        // result of the request processing.
        // We obtaing the result text
        final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        final StringBuffer result = new StringBuffer();
        String line = null;
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        // ... and process it
        line = result.toString();
        if (!line.isEmpty()) {
            // Line is not empty = error
            LOG.debug("Result of the request processing: FAILED\n{}", line);
            if (line.contains(CMConstants.ERROR_UNKNOWN)) {
                throw new UnknownErrorException();
            } else if (line.contains(CMConstants.ERROR_NO_ACCOUNT)) {
                throw new NoAccountFoundForProductTokenException();
            } else if (line.contains(CMConstants.ERROR_INSUFICIENT_BALANCE)) {
                throw new InsufficientBalanceException();
            } else if (line.contains(CMConstants.ERROR_UNROUTABLE_MESSAGE)) {
                throw new UnroutableMessageException();
            } else if (line.contains(CMConstants.ERROR_INVALID_PRODUCT_TOKEN)) {
                throw new InvalidProductTokenException();
            } else {
                // responsibility
                throw new CMResponseException("CHECK ME. I am not expecting this. ");
            }
        }
        // Ok. Line is EMPTY - successfully submitted
        LOG.debug("Result of the request processing: Successfully submited");
    } catch (final IOException io) {
        throw new CMDirectException(io);
    } catch (Throwable t) {
        if (!(t instanceof CMDirectException)) {
            // Chain it
            t = new CMDirectException(t);
        }
        throw (CMDirectException) t;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CMDirectException(org.apache.camel.component.cm.exceptions.CMDirectException) UnknownErrorException(org.apache.camel.component.cm.exceptions.cmresponse.UnknownErrorException) InputStreamReader(java.io.InputStreamReader) CMResponseException(org.apache.camel.component.cm.exceptions.cmresponse.CMResponseException) InsufficientBalanceException(org.apache.camel.component.cm.exceptions.cmresponse.InsufficientBalanceException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) UnroutableMessageException(org.apache.camel.component.cm.exceptions.cmresponse.UnroutableMessageException) InvalidProductTokenException(org.apache.camel.component.cm.exceptions.cmresponse.InvalidProductTokenException) HttpClient(org.apache.http.client.HttpClient) BufferedReader(java.io.BufferedReader) NoAccountFoundForProductTokenException(org.apache.camel.component.cm.exceptions.cmresponse.NoAccountFoundForProductTokenException)

Example 87 with HttpClient

use of org.apache.http.client.HttpClient in project eap-additional-testsuite by jboss-set.

the class UndertowTwoWaySslTestCase method testSendingTrustedClientCertificate.

@Test
public void testSendingTrustedClientCertificate() {
    HttpClient client = SSLTruststoreUtil.getHttpClientWithSSL(CLIENT_KEYSTORE_FILE, PASSWORD, CLIENT_TRUSTSTORE_FILE, PASSWORD);
    assertConnectionToServer(client, SC_OK);
    closeClient(client);
}
Also used : HttpClient(org.apache.http.client.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Test(org.junit.Test)

Example 88 with HttpClient

use of org.apache.http.client.HttpClient in project eap-additional-testsuite by jboss-set.

the class UndertowTwoWaySslTestCase method testSendingNonTrustedClientCertificate.

@Test
public void testSendingNonTrustedClientCertificate() {
    HttpClient client = SSLTruststoreUtil.getHttpClientWithSSL(UNTRUSTED_STORE_FILE, PASSWORD, CLIENT_TRUSTSTORE_FILE, PASSWORD);
    assertConnectionToServer(client, SC_OK);
    closeClient(client);
}
Also used : HttpClient(org.apache.http.client.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Test(org.junit.Test)

Example 89 with HttpClient

use of org.apache.http.client.HttpClient in project eap-additional-testsuite by jboss-set.

the class DeploymentOverlayTestCase method performHttpCall.

private String performHttpCall(DomainClient client, String host, String server, String socketBindingGroup) throws Exception {
    ModelNode op = new ModelNode();
    op.get(OP).set(READ_RESOURCE_OPERATION);
    op.get(OP_ADDR).add(HOST, host).add(SERVER, server).add(SOCKET_BINDING_GROUP, socketBindingGroup).add(SOCKET_BINDING, "http");
    op.get(INCLUDE_RUNTIME).set(true);
    ModelNode socketBinding = validateResponse(client.execute(op));
    URL url = new URL("http", TestSuiteEnvironment.formatPossibleIpv6Address(socketBinding.get("bound-address").asString()), socketBinding.get("bound-port").asInt(), "/test/");
    HttpGet get = new HttpGet(url.toURI());
    HttpClient httpClient = HttpClients.createDefault();
    HttpResponse response = httpClient.execute(get);
    return getContent(response);
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) ModelNode(org.jboss.dmr.ModelNode) URL(java.net.URL)

Example 90 with HttpClient

use of org.apache.http.client.HttpClient in project eap-additional-testsuite by jboss-set.

the class UndertowSslSecurityDomainTestCase method testProtectedAccess.

/**
 * Tests access to resource that requires authentication and authorization.
 */
@Test
public void testProtectedAccess() {
    HttpClient client = SSLTruststoreUtil.getHttpClientWithSSL(CLIENT_KEYSTORE_FILE, PASSWORD, CLIENT_TRUSTSTORE_FILE, PASSWORD);
    assertProtectedAccess(client, SC_OK);
    closeClient(client);
}
Also used : HttpClient(org.apache.http.client.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Test(org.junit.Test)

Aggregations

HttpClient (org.apache.http.client.HttpClient)878 HttpResponse (org.apache.http.HttpResponse)548 HttpGet (org.apache.http.client.methods.HttpGet)394 Test (org.junit.Test)273 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)264 IOException (java.io.IOException)258 HttpPost (org.apache.http.client.methods.HttpPost)197 HttpEntity (org.apache.http.HttpEntity)118 URI (java.net.URI)87 InputStream (java.io.InputStream)81 ArrayList (java.util.ArrayList)64 ClientProtocolException (org.apache.http.client.ClientProtocolException)61 InputStreamReader (java.io.InputStreamReader)59 StringEntity (org.apache.http.entity.StringEntity)58 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)58 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)56 MockResponse (com.google.mockwebserver.MockResponse)48 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)48 BufferedReader (java.io.BufferedReader)46 URISyntaxException (java.net.URISyntaxException)45