Search in sources :

Example 11 with HttpGet

use of org.apache.http.client.methods.HttpGet in project hive by apache.

the class TestHS2HttpServer method testConfStrippedFromWebUI.

@Test
public void testConfStrippedFromWebUI() throws Exception {
    String pwdValFound = null;
    String pwdKeyFound = null;
    CloseableHttpClient httpclient = null;
    try {
        httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://localhost:" + webUIPort + "/conf");
        CloseableHttpResponse response1 = httpclient.execute(httpGet);
        try {
            HttpEntity entity1 = response1.getEntity();
            BufferedReader br = new BufferedReader(new InputStreamReader(entity1.getContent()));
            String line;
            while ((line = br.readLine()) != null) {
                if (line.contains(metastorePasswd)) {
                    pwdValFound = line;
                }
                if (line.contains(ConfVars.METASTOREPWD.varname)) {
                    pwdKeyFound = line;
                }
            }
            EntityUtils.consume(entity1);
        } finally {
            response1.close();
        }
    } finally {
        if (httpclient != null) {
            httpclient.close();
        }
    }
    assertNotNull(pwdKeyFound);
    assertNull(pwdValFound);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader) Test(org.junit.Test)

Example 12 with HttpGet

use of org.apache.http.client.methods.HttpGet in project hive by apache.

the class PTestClient method downloadTestResults.

private void downloadTestResults(String testHandle, String testOutputDir) throws Exception {
    HttpGet request = new HttpGet(mLogsEndpoint + testHandle + "/test-results.tar.gz");
    FileOutputStream output = null;
    try {
        HttpResponse httpResponse = mHttpClient.execute(request);
        StatusLine statusLine = httpResponse.getStatusLine();
        if (statusLine.getStatusCode() != 200) {
            throw new RuntimeException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
        }
        output = new FileOutputStream(new File(testOutputDir, "test-results.tar.gz"));
        IOUtils.copyLarge(httpResponse.getEntity().getContent(), output);
        output.flush();
    } finally {
        request.abort();
        if (output != null) {
            output.close();
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpGet(org.apache.http.client.methods.HttpGet) FileOutputStream(java.io.FileOutputStream) HttpResponse(org.apache.http.HttpResponse) File(java.io.File)

Example 13 with HttpGet

use of org.apache.http.client.methods.HttpGet in project cas by apereo.

the class SimpleHttpClient method sendMessageToEndPoint.

@Override
public HttpMessage sendMessageToEndPoint(final URL url) {
    Assert.notNull(this.httpClient);
    HttpEntity entity = null;
    try (CloseableHttpResponse response = this.httpClient.execute(new HttpGet(url.toURI()))) {
        final int responseCode = response.getStatusLine().getStatusCode();
        for (final int acceptableCode : this.acceptableCodes) {
            if (responseCode == acceptableCode) {
                LOGGER.debug("Response code received from server matched [{}].", responseCode);
                entity = response.getEntity();
                final HttpMessage msg = new HttpMessage(url, IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8));
                msg.setContentType(entity.getContentType().getValue());
                msg.setResponseCode(responseCode);
                return msg;
            }
        }
        LOGGER.warn("Response code [{}] from [{}] did not match any of the acceptable response codes.", responseCode, url);
        if (responseCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
            final String value = response.getStatusLine().getReasonPhrase();
            LOGGER.error("There was an error contacting the endpoint: [{}]; The error:\n[{}]", url.toExternalForm(), value);
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        EntityUtils.consumeQuietly(entity);
    }
    return null;
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MalformedURLException(java.net.MalformedURLException)

Example 14 with HttpGet

use of org.apache.http.client.methods.HttpGet in project cas by apereo.

the class SimpleHttpClient method isValidEndPoint.

@Override
public boolean isValidEndPoint(final URL url) {
    Assert.notNull(this.httpClient);
    HttpEntity entity = null;
    try (CloseableHttpResponse response = this.httpClient.execute(new HttpGet(url.toURI()))) {
        final int responseCode = response.getStatusLine().getStatusCode();
        final int idx = Collections.binarySearch(this.acceptableCodes, responseCode);
        if (idx >= 0) {
            LOGGER.debug("Response code from server matched [{}].", responseCode);
            return true;
        }
        LOGGER.debug("Response code did not match any of the acceptable response codes. Code returned was [{}]", responseCode);
        if (responseCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
            final String value = response.getStatusLine().getReasonPhrase();
            LOGGER.error("There was an error contacting the endpoint: [{}]; The error was:\n[{}]", url.toExternalForm(), value);
        }
        entity = response.getEntity();
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        EntityUtils.consumeQuietly(entity);
    }
    return false;
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MalformedURLException(java.net.MalformedURLException)

Example 15 with HttpGet

use of org.apache.http.client.methods.HttpGet in project weixin-java-tools by chanjarster.

the class SimpleGetRequestExecutor method execute.

@Override
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException {
    if (queryParam != null) {
        if (uri.indexOf('?') == -1) {
            uri += '?';
        }
        uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
    }
    HttpGet httpGet = new HttpGet(uri);
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpGet.setConfig(config);
    }
    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
        WxError error = WxError.fromJson(responseContent);
        if (error.getErrorCode() != 0) {
            throw new WxErrorException(error);
        }
        return responseContent;
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) WxError(me.chanjar.weixin.common.bean.result.WxError) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Aggregations

HttpGet (org.apache.http.client.methods.HttpGet)1143 HttpResponse (org.apache.http.HttpResponse)717 Test (org.junit.Test)504 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)242 TestHttpClient (io.undertow.testutils.TestHttpClient)239 IOException (java.io.IOException)200 HttpClient (org.apache.http.client.HttpClient)185 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)179 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)176 HttpEntity (org.apache.http.HttpEntity)152 Header (org.apache.http.Header)133 InputStream (java.io.InputStream)103 URI (java.net.URI)83 JsonNode (com.fasterxml.jackson.databind.JsonNode)68 ArrayList (java.util.ArrayList)60 MockResponse (com.google.mockwebserver.MockResponse)54 HttpPost (org.apache.http.client.methods.HttpPost)54 Deployment (org.activiti.engine.test.Deployment)49 ClientProtocolException (org.apache.http.client.ClientProtocolException)46 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)45