org.apache.http.client.methods.HttpRequestWrapper#wrap()源码实例Demo

下面列出了org.apache.http.client.methods.HttpRequestWrapper#wrap() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: streams   文件: TwitterSecurityTest.java
@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);
}
 
@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);
}
 
@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);
}
 
@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);
}
 
@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);
}
 
@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);
}
 
@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);
}
 
@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);
}
 
@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);
}
 
源代码10 项目: geoportal-server-harvester   文件: AgpClient.java
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");
  }
}