Search in sources :

Example 1 with HttpRequestBase

use of org.apache.http.client.methods.HttpRequestBase 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)

Example 2 with HttpRequestBase

use of org.apache.http.client.methods.HttpRequestBase in project elasticsearch by elastic.

the class RestClient method performRequestAsync.

/**
     * Sends a request to the Elasticsearch cluster that the client points to. The request is executed asynchronously
     * and the provided {@link ResponseListener} gets notified upon request completion or failure.
     * Selects a host out of the provided ones in a round-robin fashion. Failing hosts are marked dead and retried after a certain
     * amount of time (minimum 1 minute, maximum 30 minutes), depending on how many times they previously failed (the more failures,
     * the later they will be retried). In case of failures all of the alive nodes (or dead nodes that deserve a retry) are retried
     * until one responds or none of them does, in which case an {@link IOException} will be thrown.
     *
     * @param method the http method
     * @param endpoint the path of the request (without host and port)
     * @param params the query_string parameters
     * @param entity the body of the request, null if not applicable
     * @param httpAsyncResponseConsumerFactory the {@link HttpAsyncResponseConsumerFactory} used to create one
     * {@link HttpAsyncResponseConsumer} callback per retry. Controls how the response body gets streamed from a non-blocking HTTP
     * connection on the client side.
     * @param responseListener the {@link ResponseListener} to notify when the request is completed or fails
     * @param headers the optional request headers
     */
public void performRequestAsync(String method, String endpoint, Map<String, String> params, HttpEntity entity, HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory, ResponseListener responseListener, Header... headers) {
    try {
        Objects.requireNonNull(params, "params must not be null");
        Map<String, String> requestParams = new HashMap<>(params);
        //ignore is a special parameter supported by the clients, shouldn't be sent to es
        String ignoreString = requestParams.remove("ignore");
        Set<Integer> ignoreErrorCodes;
        if (ignoreString == null) {
            if (HttpHead.METHOD_NAME.equals(method)) {
                //404 never causes error if returned for a HEAD request
                ignoreErrorCodes = Collections.singleton(404);
            } else {
                ignoreErrorCodes = Collections.emptySet();
            }
        } else {
            String[] ignoresArray = ignoreString.split(",");
            ignoreErrorCodes = new HashSet<>();
            if (HttpHead.METHOD_NAME.equals(method)) {
                //404 never causes error if returned for a HEAD request
                ignoreErrorCodes.add(404);
            }
            for (String ignoreCode : ignoresArray) {
                try {
                    ignoreErrorCodes.add(Integer.valueOf(ignoreCode));
                } catch (NumberFormatException e) {
                    throw new IllegalArgumentException("ignore value should be a number, found [" + ignoreString + "] instead", e);
                }
            }
        }
        URI uri = buildUri(pathPrefix, endpoint, requestParams);
        HttpRequestBase request = createHttpRequest(method, uri, entity);
        setHeaders(request, headers);
        FailureTrackingResponseListener failureTrackingResponseListener = new FailureTrackingResponseListener(responseListener);
        long startTime = System.nanoTime();
        performRequestAsync(startTime, nextHost(), request, ignoreErrorCodes, httpAsyncResponseConsumerFactory, failureTrackingResponseListener);
    } catch (Exception e) {
        responseListener.onFailure(e);
    }
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 3 with HttpRequestBase

use of org.apache.http.client.methods.HttpRequestBase in project DataX by alibaba.

the class HttpClientUtilTest method testExecuteAndGetWithRetry2.

/**
     * 和测试方法一样:testExecuteAndGetWithRetry(),只是换了一种写法,直接采用 Mockito 进行验证的。
     */
@Test
public void testExecuteAndGetWithRetry2() throws Exception {
    String url = "http://127.0.0.1/:8080";
    HttpRequestBase httpRequestBase = new HttpGet(url);
    HttpClientUtil httpClientUtil = Mockito.spy(HttpClientUtil.getHttpClientUtil());
    Mockito.doThrow(new Exception("one")).doThrow(new Exception("two")).doThrow(new Exception("three")).doReturn("成功").when(httpClientUtil).executeAndGet(httpRequestBase);
    String str = httpClientUtil.executeAndGetWithRetry(httpRequestBase, 4, 1000l);
    Assert.assertEquals(str, "成功");
    Mockito.reset(httpClientUtil);
    Mockito.doThrow(new Exception("one")).doThrow(new Exception("two")).doThrow(new Exception("three")).doReturn("成功").when(httpClientUtil).executeAndGet(httpRequestBase);
    try {
        httpClientUtil.executeAndGetWithRetry(httpRequestBase, 2, 1000l);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof DataXException);
        Assert.assertTrue(e.getMessage().contains("two"));
    }
    httpClientUtil.destroy();
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) DataXException(com.alibaba.datax.common.exception.DataXException) URISyntaxException(java.net.URISyntaxException) DataXException(com.alibaba.datax.common.exception.DataXException) Test(org.junit.Test)

Example 4 with HttpRequestBase

use of org.apache.http.client.methods.HttpRequestBase in project DataX by alibaba.

the class HttpClientUtilTest method testExecuteAndGetWithRetry.

@Test
public void testExecuteAndGetWithRetry() throws Exception {
    String url = "http://127.0.0.1/:8080";
    HttpRequestBase httpRequestBase = new HttpGet(url);
    HttpClientUtil httpClientUtil = PowerMockito.spy(HttpClientUtil.getHttpClientUtil());
    PowerMockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            System.out.println("失败第1次");
            return new Exception("失败第1次");
        }
    }).doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            System.out.println("失败第2次");
            return new Exception("失败第2次");
        }
    }).doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            System.out.println("失败第3次");
            return new Exception("失败第3次");
        }
    }).doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            System.out.println("失败第4次");
            return new Exception("失败第4次");
        }
    }).doReturn("成功").when(httpClientUtil).executeAndGet(any(HttpRequestBase.class));
    String str = httpClientUtil.executeAndGetWithRetry(httpRequestBase, 5, 1000l);
    Assert.assertEquals(str, "成功");
    try {
        httpClientUtil.executeAndGetWithRetry(httpRequestBase, 2, 1000l);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof DataXException);
    }
    httpClientUtil.destroy();
}
Also used : Answer(org.mockito.stubbing.Answer) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpGet(org.apache.http.client.methods.HttpGet) DataXException(com.alibaba.datax.common.exception.DataXException) URISyntaxException(java.net.URISyntaxException) DataXException(com.alibaba.datax.common.exception.DataXException) Test(org.junit.Test)

Example 5 with HttpRequestBase

use of org.apache.http.client.methods.HttpRequestBase in project gocd by gocd.

the class ServerBinaryDownloader method download.

protected synchronized boolean download(final DownloadableFile downloadableFile) throws Exception {
    File toDownload = downloadableFile.getLocalFile();
    LOG.info("Downloading " + toDownload);
    String url = downloadableFile.url(urlGenerator);
    final HttpRequestBase request = new HttpGet(url);
    request.setConfig(RequestConfig.custom().setConnectTimeout(HTTP_TIMEOUT_IN_MILLISECONDS).build());
    try (CloseableHttpClient httpClient = httpClientBuilder.build();
        CloseableHttpResponse response = httpClient.execute(request)) {
        LOG.info("Got server response");
        if (response.getEntity() == null) {
            LOG.error("Unable to read file from the server response");
            return false;
        }
        handleInvalidResponse(response, url);
        try (BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(downloadableFile.getLocalFile()))) {
            response.getEntity().writeTo(outStream);
            LOG.info("Piped the stream to " + downloadableFile);
        }
    }
    return true;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Aggregations

HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)49 HttpResponse (org.apache.http.HttpResponse)24 HttpGet (org.apache.http.client.methods.HttpGet)15 IOException (java.io.IOException)11 Header (org.apache.http.Header)11 HttpPost (org.apache.http.client.methods.HttpPost)10 HttpEntity (org.apache.http.HttpEntity)9 URI (java.net.URI)7 ArrayList (java.util.ArrayList)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 HttpHead (org.apache.http.client.methods.HttpHead)6 Test (org.junit.Test)6 HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)5 StringEntity (org.apache.http.entity.StringEntity)5 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 SimpleStringExtractorHandler (com.newsrob.util.SimpleStringExtractorHandler)4 Timing (com.newsrob.util.Timing)4 InputStream (java.io.InputStream)4 URISyntaxException (java.net.URISyntaxException)4 HttpPut (org.apache.http.client.methods.HttpPut)4