Example usage for org.apache.http.client.methods HttpUriRequest getURI

List of usage examples for org.apache.http.client.methods HttpUriRequest getURI

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest getURI.

Prototype

URI getURI();

Source Link

Document

Returns the URI this request uses, such as <code>http://example.org/path/to/file</code>.

Usage

From source file:org.apache.hadoop.gateway.hdfs.dispatch.WebHdfsHaDispatch.java

private void retryRequest(HttpUriRequest outboundRequest, HttpServletRequest inboundRequest,
        HttpServletResponse outboundResponse, HttpResponse inboundResponse, Exception exception)
        throws IOException {
    LOG.retryingRequest(outboundRequest.getURI().toString());
    AtomicInteger counter = (AtomicInteger) inboundRequest.getAttribute(RETRY_COUNTER_ATTRIBUTE);
    if (counter == null) {
        counter = new AtomicInteger(0);
    }/*  ww  w.j a v a 2  s  .  c  om*/
    inboundRequest.setAttribute(RETRY_COUNTER_ATTRIBUTE, counter);
    if (counter.incrementAndGet() <= maxRetryAttempts) {
        if (retrySleep > 0) {
            try {
                Thread.sleep(retrySleep);
            } catch (InterruptedException e) {
                LOG.retrySleepFailed(RESOURCE_ROLE, e);
            }
        }
        executeRequest(outboundRequest, inboundRequest, outboundResponse);
    } else {
        LOG.maxRetryAttemptsReached(maxRetryAttempts, RESOURCE_ROLE, outboundRequest.getURI().toString());
        if (inboundResponse != null) {
            writeOutboundResponse(outboundRequest, inboundRequest, outboundResponse, inboundResponse);
        } else {
            throw new IOException(exception);
        }
    }
}

From source file:org.apache.hadoop.gateway.hdfs.dispatch.WebHdfsHaHttpClientDispatch.java

private void failoverRequest(HttpUriRequest outboundRequest, HttpServletRequest inboundRequest,
        HttpServletResponse outboundResponse, HttpResponse inboundResponse, Exception exception)
        throws IOException {
    LOG.failingOverRequest(outboundRequest.getURI().toString());
    AtomicInteger counter = (AtomicInteger) inboundRequest.getAttribute(FAILOVER_COUNTER_ATTRIBUTE);
    if (counter == null) {
        counter = new AtomicInteger(0);
    }/*from  w w  w.  j a v a 2s .  com*/
    inboundRequest.setAttribute(FAILOVER_COUNTER_ATTRIBUTE, counter);
    if (counter.incrementAndGet() <= maxFailoverAttempts) {
        haProvider.markFailedURL(resourceRole, outboundRequest.getURI().toString());
        //null out target url so that rewriters run again
        inboundRequest.setAttribute(AbstractGatewayFilter.TARGET_REQUEST_URL_ATTRIBUTE_NAME, null);
        URI uri = getDispatchUrl(inboundRequest);
        ((HttpRequestBase) outboundRequest).setURI(uri);
        if (failoverSleep > 0) {
            try {
                Thread.sleep(failoverSleep);
            } catch (InterruptedException e) {
                LOG.failoverSleepFailed(resourceRole, e);
            }
        }
        executeRequest(outboundRequest, inboundRequest, outboundResponse);
    } else {
        LOG.maxFailoverAttemptsReached(maxFailoverAttempts, resourceRole);
        if (inboundResponse != null) {
            writeOutboundResponse(outboundRequest, inboundRequest, outboundResponse, inboundResponse);
        } else {
            throw new IOException(exception);
        }
    }
}

From source file:org.jasig.portlet.proxy.service.web.HttpContentServiceImpl.java

public GenericContentResponseImpl getContent(HttpContentRequestImpl proxyRequest, PortletRequest request,
        boolean runWrapperMethods) {
    try {/*from  ww w  .j  a v a  2  s.  com*/

        // get an HttpClient appropriate for this user and portlet instance
        // and set any basic auth credentials, if applicable
        final AbstractHttpClient httpclient = httpClientService.getHttpClient(request);

        // create the request
        final HttpUriRequest httpRequest = getHttpRequest(proxyRequest, request);
        if (log.isTraceEnabled()) {
            log.trace("Proxying " + httpRequest.getURI() + " via " + httpRequest.getMethod());
        }

        // execute the request
        final HttpContext context = new BasicHttpContext();
        final HttpResponse response = httpclient.execute(httpRequest, context);
        final HttpEntity entity = response.getEntity();

        // create the response object and set the content stream
        final HttpContentResponseImpl proxyResponse = new HttpContentResponseImpl(entity);
        proxyResponse.setContent(entity.getContent());

        // add each response header to our response object
        for (Header header : response.getAllHeaders()) {
            proxyResponse.getHeaders().put(header.getName(), header.getValue());
        }

        // set the final URL of the response in case it was redirected
        String finalUrl = (String) context.getAttribute(RedirectTrackingResponseInterceptor.FINAL_URL_KEY);
        if (finalUrl == null) {
            finalUrl = proxyRequest.getProxiedLocation();
        }
        proxyResponse.setProxiedLocation(finalUrl);

        return proxyResponse;

    } catch (ClientProtocolException e) {
        log.error("Exception retrieving remote content", e);
    } catch (IOException e) {
        log.error("Exception retrieving remote content", e);
    }

    return null;
}

From source file:org.onebusaway.siri.core.SiriClientTest.java

@Test
public void testHandleRequestWithResponseForCheckStatusRequestAndResponse()
        throws IllegalStateException, IOException, XpathException, SAXException {

    SiriClientRequest request = new SiriClientRequest();
    request.setTargetUrl("http://localhost/");
    request.setTargetVersion(ESiriVersion.V1_3);

    Siri payload = new Siri();
    request.setPayload(payload);/*w  w  w  . j av  a  2 s. com*/

    CheckStatusRequestStructure checkStatusRequest = new CheckStatusRequestStructure();
    payload.setCheckStatusRequest(checkStatusRequest);

    StringBuilder b = new StringBuilder();
    b.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
    b.append("<Siri xmlns=\"http://www.siri.org.uk/siri\" version=\"1.3\">");
    b.append("  <CheckStatusResponse>");
    b.append("  </CheckStatusResponse>");
    b.append("</Siri>");

    HttpResponse response = createResponse();
    response.setEntity(new StringEntity(b.toString()));

    Mockito.when(_httpClientService.executeHttpMethod(Mockito.any(HttpClient.class),
            Mockito.any(HttpUriRequest.class))).thenReturn(response);

    Siri siriResponse = _client.handleRequestWithResponse(request);

    /**
     * Verify that the http client was called and capture the request
     */
    ArgumentCaptor<HttpUriRequest> captor = ArgumentCaptor.forClass(HttpUriRequest.class);
    Mockito.verify(_httpClientService).executeHttpMethod(Mockito.any(HttpClient.class), captor.capture());

    /**
     * Verify that our CheckStatusResponse was properly passed on to the
     * subscription manager
     */
    Mockito.verify(_subscriptionManager)
            .handleCheckStatusNotification(Mockito.any(CheckStatusResponseStructure.class));

    /**
     * Verify the raw parameters of the request
     */
    HttpUriRequest uriRequest = captor.getValue();
    assertEquals("http://localhost/", uriRequest.getURI().toString());
    assertEquals("POST", uriRequest.getMethod());

    /**
     * Verify the request content
     */
    HttpPost post = (HttpPost) uriRequest;
    HttpEntity entity = post.getEntity();
    String content = getHttpEntityAsString(entity);
    Document doc = XMLUnit.buildControlDocument(content);

    assertXpathExists("/s:Siri", doc);
    assertXpathEvaluatesTo("1.3", "/s:Siri/@version", doc);
    assertXpathExists("/s:Siri/s:CheckStatusRequest", content);
    assertXpathExists("/s:Siri/s:CheckStatusRequest/s:RequestTimestamp", doc);
    assertXpathExists("/s:Siri/s:CheckStatusRequest/s:Address", doc);
    assertXpathEvaluatesTo("somebody", "/s:Siri/s:CheckStatusRequest/s:RequestorRef", doc);
    assertXpathExists("/s:Siri/s:CheckStatusRequest/s:MessageIdentifier", doc);

    // Verify that the message id is a UUID
    String messageId = evaluateXPath("/s:Siri/s:CheckStatusRequest/s:MessageIdentifier", doc);
    UUID.fromString(messageId);

    /**
     * Verify that the CheckStatusResponse was properly received and parsed
     */
    CheckStatusResponseStructure checkStatusResponse = siriResponse.getCheckStatusResponse();
    assertNotNull(checkStatusResponse);
}

From source file:com.jgoetsch.eventtrader.source.ApeStreamingMsgSource.java

private JSONArray executeRequest(HttpClient client, HttpUriRequest request) {
    try {/*from ww w  .  j av  a 2  s. c  o  m*/
        HttpResponse rsp = client.execute(request);
        HttpEntity entity = rsp.getEntity();
        if (rsp.getStatusLine().getStatusCode() >= 400 || entity == null) {
            log.error("HTTP request to " + request.getURI() + " failed with status " + rsp.getStatusLine());
            return null;
        } else {
            return (JSONArray) JSONValue.parse(new BufferedReader(new InputStreamReader(entity.getContent())));
        }
    } catch (Exception e) {
        log.warn("Exception reading or parsing message source", e);
        return null;
    } finally {
        request.abort();
    }
}

From source file:org.apache.hadoop.gateway.hdfs.dispatch.WebHdfsHaDispatch.java

private void failoverRequest(HttpUriRequest outboundRequest, HttpServletRequest inboundRequest,
        HttpServletResponse outboundResponse, HttpResponse inboundResponse, Exception exception)
        throws IOException {
    LOG.failingOverRequest(outboundRequest.getURI().toString());
    AtomicInteger counter = (AtomicInteger) inboundRequest.getAttribute(FAILOVER_COUNTER_ATTRIBUTE);
    if (counter == null) {
        counter = new AtomicInteger(0);
    }//from   www  .  j av a  2  s  . co  m
    inboundRequest.setAttribute(FAILOVER_COUNTER_ATTRIBUTE, counter);
    if (counter.incrementAndGet() <= maxFailoverAttempts) {
        haProvider.markFailedURL(RESOURCE_ROLE, outboundRequest.getURI().toString());
        //null out target url so that rewriters run again
        inboundRequest.setAttribute(AbstractGatewayFilter.TARGET_REQUEST_URL_ATTRIBUTE_NAME, null);
        URI uri = getDispatchUrl(inboundRequest);
        ((HttpRequestBase) outboundRequest).setURI(uri);
        if (failoverSleep > 0) {
            try {
                Thread.sleep(failoverSleep);
            } catch (InterruptedException e) {
                LOG.failoverSleepFailed(RESOURCE_ROLE, e);
            }
        }
        executeRequest(outboundRequest, inboundRequest, outboundResponse);
    } else {
        LOG.maxFailoverAttemptsReached(maxFailoverAttempts, RESOURCE_ROLE);
        if (inboundResponse != null) {
            writeOutboundResponse(outboundRequest, inboundRequest, outboundResponse, inboundResponse);
        } else {
            throw new IOException(exception);
        }
    }
}

From source file:com.gooddata.http.client.GoodDataHttpClient.java

@Override
public HttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException {
    final URI uri = request.getURI();
    final HttpHost httpHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    return execute(httpHost, request, context);
}

From source file:com.vmware.photon.controller.client.RestClientTest.java

@Test
public void testPerformGet() {
    ArgumentCaptor<HttpUriRequest> argumentCaptor = setup(RestClient.Method.GET, null);

    HttpUriRequest request = argumentCaptor.getValue();
    assertNotNull(request);//w  w  w  . ja  v  a2 s  . com
    assertTrue(request.getMethod().equalsIgnoreCase(RestClient.Method.GET.toString()));
    assertEquals(request.getURI().toString(), uri);
}

From source file:com.microsoft.windowsazure.messaging.Connection.java

/**
 * Adds the Authorization header to a request
 * @param request   The request to modify
 * @throws java.security.InvalidKeyException
 *//*w w  w .j av  a2  s  . co  m*/
private void addAuthorizationHeader(HttpUriRequest request) throws InvalidKeyException {
    String token = generateAuthToken(request.getURI().toString());

    request.addHeader(AUTHORIZATION_HEADER, token);
}

From source file:com.vmware.photon.controller.client.RestClientTest.java

@Test
public void testPerformDelete() {
    ArgumentCaptor<HttpUriRequest> argumentCaptor = setup(RestClient.Method.DELETE, null);

    HttpUriRequest request = argumentCaptor.getValue();
    assertNotNull(request);/* w  ww  .  j  a v  a  2  s . co m*/
    assertTrue(request.getMethod().equalsIgnoreCase(RestClient.Method.DELETE.toString()));
    assertEquals(request.getURI().toString(), uri);
}