Search in sources :

Example 6 with HttpGet

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

the class HttpComponentVerifier method verifyHttpConnectivity.

private void verifyHttpConnectivity(ResultBuilder builder, Map<String, Object> parameters) throws Exception {
    Optional<String> uri = getOption(parameters, "httpUri", String.class);
    CloseableHttpClient httpclient = createHttpClient(parameters);
    HttpUriRequest request = new HttpGet(uri.get());
    try (CloseableHttpResponse response = httpclient.execute(request)) {
        int code = response.getStatusLine().getStatusCode();
        String okCodes = getOption(parameters, "okStatusCodeRange", String.class).orElse("200-299");
        if (!HttpHelper.isStatusCodeOk(code, okCodes)) {
            if (code == 401) {
                // Unauthorized, add authUsername and authPassword to the list
                // of parameters in error
                builder.error(ResultErrorBuilder.withHttpCode(code).description(response.getStatusLine().getReasonPhrase()).parameter("authUsername").parameter("authPassword").build());
            } else if (code >= 300 && code < 400) {
                // redirect
                builder.error(ResultErrorBuilder.withHttpCode(code).description(response.getStatusLine().getReasonPhrase()).parameter("httpUri").attribute(ComponentVerifier.HTTP_REDIRECT, true).attribute(ComponentVerifier.HTTP_REDIRECT_LOCATION, () -> HttpUtil.responseHeaderValue(response, "location")).build());
            } else if (code >= 400) {
                // generic http error
                builder.error(ResultErrorBuilder.withHttpCode(code).description(response.getStatusLine().getReasonPhrase()).build());
            }
        }
    } catch (UnknownHostException e) {
        builder.error(ResultErrorBuilder.withException(e).parameter("httpUri").build());
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) UnknownHostException(java.net.UnknownHostException) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 7 with HttpGet

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

the class JobEndNotifier method httpNotification.

private static int httpNotification(String uri, int timeout) throws IOException, URISyntaxException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout).setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, (long) timeout);
    HttpGet httpGet = new HttpGet(new URI(uri));
    httpGet.setHeader("Accept", "*/*");
    return client.execute(httpGet).getStatusLine().getStatusCode();
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) URI(java.net.URI) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 8 with HttpGet

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

the class RemoteSASKeyGenerationResponse method makeRemoteRequest.

/**
   * Helper method to make a remote request.
   * @param uri - Uri to use for the remote request
   * @return RemoteSASKeyGenerationResponse
   */
private RemoteSASKeyGenerationResponse makeRemoteRequest(URI uri) throws SASKeyGenerationException {
    try {
        String responseBody = remoteCallHelper.makeRemoteGetRequest(new HttpGet(uri));
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(responseBody, RemoteSASKeyGenerationResponse.class);
    } catch (WasbRemoteCallException remoteCallEx) {
        throw new SASKeyGenerationException("Encountered RemoteCallException" + " while retrieving SAS key from remote service", remoteCallEx);
    } catch (JsonParseException jsonParserEx) {
        throw new SASKeyGenerationException("Encountered JsonParseException " + "while parsing the response from remote" + " service into RemoteSASKeyGenerationResponse object", jsonParserEx);
    } catch (JsonMappingException jsonMappingEx) {
        throw new SASKeyGenerationException("Encountered JsonMappingException" + " while mapping the response from remote service into " + "RemoteSASKeyGenerationResponse object", jsonMappingEx);
    } catch (IOException ioEx) {
        throw new SASKeyGenerationException("Encountered IOException while " + "accessing remote service to retrieve SAS Key", ioEx);
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 9 with HttpGet

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

the class Client method get.

/**
   * Send a GET request
   * @param c the cluster definition
   * @param path the path or URI
   * @param headers the HTTP headers to include in the request
   * @return a Response object with response detail
   * @throws IOException
   */
public Response get(Cluster c, String path, Header[] headers) throws IOException {
    if (httpGet != null) {
        httpGet.releaseConnection();
    }
    httpGet = new HttpGet(path);
    HttpResponse resp = execute(c, httpGet, headers, path);
    return new Response(resp.getStatusLine().getStatusCode(), resp.getAllHeaders(), resp, resp.getEntity() == null ? null : resp.getEntity().getContent());
}
Also used : HttpResponse(org.apache.http.HttpResponse) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse)

Example 10 with HttpGet

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

the class WebAppProxyServlet method proxyLink.

/**
   * Download link and have it be the response.
   * @param req the http request
   * @param resp the http response
   * @param link the link to download
   * @param c the cookie to set if any
   * @param proxyHost the proxy host
   * @param method the http method
   * @throws IOException on any error.
   */
private static void proxyLink(final HttpServletRequest req, final HttpServletResponse resp, final URI link, final Cookie c, final String proxyHost, final HTTP method) throws IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY).setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
    // Make sure we send the request from the proxy address in the config
    // since that is what the AM filter checks against. IP aliasing or
    // similar could cause issues otherwise.
    InetAddress localAddress = InetAddress.getByName(proxyHost);
    if (LOG.isDebugEnabled()) {
        LOG.debug("local InetAddress for proxy host: {}", localAddress);
    }
    client.getParams().setParameter(ConnRoutePNames.LOCAL_ADDRESS, localAddress);
    HttpRequestBase base = null;
    if (method.equals(HTTP.GET)) {
        base = new HttpGet(link);
    } else if (method.equals(HTTP.PUT)) {
        base = new HttpPut(link);
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream(), "UTF-8"));
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        ((HttpPut) base).setEntity(new StringEntity(sb.toString()));
    } else {
        resp.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }
    @SuppressWarnings("unchecked") Enumeration<String> names = req.getHeaderNames();
    while (names.hasMoreElements()) {
        String name = names.nextElement();
        if (PASS_THROUGH_HEADERS.contains(name)) {
            String value = req.getHeader(name);
            if (LOG.isDebugEnabled()) {
                LOG.debug("REQ HEADER: {} : {}", name, value);
            }
            base.setHeader(name, value);
        }
    }
    String user = req.getRemoteUser();
    if (user != null && !user.isEmpty()) {
        base.setHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII"));
    }
    OutputStream out = resp.getOutputStream();
    try {
        HttpResponse httpResp = client.execute(base);
        resp.setStatus(httpResp.getStatusLine().getStatusCode());
        for (Header header : httpResp.getAllHeaders()) {
            resp.setHeader(header.getName(), header.getValue());
        }
        if (c != null) {
            resp.addCookie(c);
        }
        InputStream in = httpResp.getEntity().getContent();
        if (in != null) {
            IOUtils.copyBytes(in, out, 4096, true);
        }
    } finally {
        base.releaseConnection();
    }
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) InputStreamReader(java.io.InputStreamReader) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) OutputStream(java.io.OutputStream) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) Header(org.apache.http.Header) BufferedReader(java.io.BufferedReader) InetAddress(java.net.InetAddress)

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