Java Code Examples for org.apache.http.client.methods.HttpRequestWrapper#wrap()

The following examples show how to use org.apache.http.client.methods.HttpRequestWrapper#wrap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TwitterSecurityTest.java    From streams with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcess() throws Exception {
  URI testURI = new URIBuilder()
      .setPath("/1/statuses/update.json")
      .setParameter("include_entities", "true")
      .build();
  HttpPost testRequest = new HttpPost(testURI);
  testRequest.setEntity(new StringEntity("status="+security.encode("Hello Ladies + Gentlemen, a signed OAuth request!")));
  HttpHost host = new HttpHost("api.twitter.com", -1, "https");
  HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(testRequest, host);
  TwitterOAuthConfiguration testOauthConfiguration = new TwitterOAuthConfiguration()
      .withConsumerKey("xvz1evFS4wEEPTGEFPHBog")
      .withConsumerSecret("kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw")
      .withAccessToken("370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb")
      .withAccessTokenSecret("LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE");
  TwitterOAuthRequestInterceptor interceptor = Mockito.spy(new TwitterOAuthRequestInterceptor(testOauthConfiguration));
  Mockito.when(interceptor.generateNonce()).thenReturn("kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg");
  Mockito.when(interceptor.generateTimestamp()).thenReturn("1318622958");
  interceptor.process(wrapper, new HttpCoreContext());
  assertEquals(1, wrapper.getHeaders("Authorization").length);
  String actual = wrapper.getFirstHeader("Authorization").getValue();
  String expected = "OAuth oauth_consumer_key=\"xvz1evFS4wEEPTGEFPHBog\", oauth_nonce=\"kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg\", oauth_signature=\"tnnArxj06cWHq44gCs1OSKk%2FjLY%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1318622958\", oauth_token=\"370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb\", oauth_version=\"1.0\"";
  assertEquals(expected, actual);
}
 
Example 2
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableHttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap);
}
 
Example 3
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, context);
}
 
Example 4
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableHttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request, target);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap);
}
 
Example 5
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableHttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request, target);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, context);
}
 
Example 6
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, responseHandler);
}
 
Example 7
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, responseHandler, context);
}
 
Example 8
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request,target);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, responseHandler);
}
 
Example 9
Source File: BotsHttpClient.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException {
  HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request,target);
  adviseRobotsTxt(wrap.getURI());
  wrap.setURI(applyPHP(wrap.getURI()));
  return client.execute(wrap, responseHandler, context);
}
 
Example 10
Source File: AgpClient.java    From geoportal-server-harvester with Apache License 2.0 4 votes vote down vote up
private String execute(HttpUriRequest req, Integer redirectDepth) throws IOException {
  // Determine if we've reached the limit of redirection attempts
  if (redirectDepth > this.maxRedirects) {
    throw new HttpResponseException(HttpStatus.SC_GONE, "Too many redirects, aborting");
  }

  try (CloseableHttpResponse httpResponse = httpClient.execute(req); InputStream contentStream = httpResponse.getEntity().getContent();) {
    if (httpResponse.getStatusLine().getStatusCode()>=400) {
      throw new HttpResponseException(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase());
    } else if (httpResponse.getStatusLine().getStatusCode() >= 300) {
      // See if we can redirect the command
      Header locationHeader = httpResponse.getFirstHeader("Location");
      if (locationHeader != null) {
        try {
          HttpRequestWrapper newReq = HttpRequestWrapper.wrap(req);

          // Determine if this is a relataive redirection
          URI redirUrl = new URI(locationHeader.getValue());
          if (!redirUrl.isAbsolute()) {
            HttpHost target = URIUtils.extractHost(newReq.getURI());

            redirUrl = URI.create(
              String.format(
                "%s://%s%s",
                target.getSchemeName(),
                target.toHostString(),
                locationHeader.getValue()
              )
            );
          }
          
          newReq.setURI(redirUrl);

          return execute(newReq, ++redirectDepth);
        } catch (IOException | URISyntaxException e) {
          LOG.debug("Error executing request", e);
          throw new HttpResponseException(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase());
        }
      }
    }
    return IOUtils.toString(contentStream, "UTF-8");
  }
}