Java 类org.apache.http.impl.client.RedirectLocations 实例源码

项目:aerodrome-for-jet    文件:API.java   
/**
 * Retrieves the status and version number information from the response
 * @param response Response to pull data from
 */
private IAPIResponse createResponseObject( final HttpResponse response, final byte[] content, final String charset )
{
  final RedirectLocations locations = ((RedirectLocations)context.getAttribute( HttpClientContext.REDIRECT_LOCATIONS ));

  final List<URI> redirectLocations = new ArrayList<>();
  if ( locations != null )
  {
    redirectLocations.addAll( locations.getAll());
  }

  return new APIResponse(
    response.getProtocolVersion(),
    response.getStatusLine(),
    new ArrayList<>( Arrays.asList( response.getAllHeaders())),
    redirectLocations,
    content,
    charset
  );
}
项目:openimaj    文件:HttpUtilsTest.java   
/**
 * Test that the redirection handler is working
 *
 * @throws MalformedURLException
 * @throws IOException
 */
@Test
public void testRedirect() throws MalformedURLException, IOException {
    final String[][] links = new String[][] {
            // new String[] { "http://t.co/kp7qdeL6",
            // "http://www.openrightsgroup.org/press/releases/bruce-willis-right-to-challenge-apple#copyright"
            // },
            new String[] { "http://t.co/VJn1ISBl", "http://ow.ly/1mgxj1",
                    "http://www.electronicsweekly.com/Articles/2012/09/03/54467/raspberry-pi-goes-to-cambridge-to-get-free-os.htm" }
    };
    for (final String[] link : links) {
        final String[] expecting = Arrays.copyOfRange(link, 1, link.length);
        final String firstLink = link[0];
        HttpUtils.readURLAsByteArrayInputStream(new URL(firstLink), new HttpUtils.MetaRefreshRedirectStrategy() {
            int redirected = 0;

            @Override
            public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)
                    throws ProtocolException
            {
                final boolean isRedirect = super.isRedirected(request, response, context);
                if (redirected < expecting.length) {
                    assertTrue(isRedirect);
                    final HttpUriRequest redirect = this.getRedirect(request, response, context);
                    final RedirectLocations redirectLocations = (RedirectLocations) context
                            .getAttribute(REDIRECT_LOCATIONS);
                    if (redirectLocations != null)
                        redirectLocations.remove(redirect.getURI());
                    final String uriString = redirect.getURI().toString();
                    assertEquals(expecting[redirected++], uriString);
                }
                return isRedirect;
            }
        });
    }
}
项目:quality    文件:LinkExternalDataProviders.java   
private String getRedirection(String resource){
    HttpHead head = new HttpHead(resource);

    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(1000)
            .setConnectTimeout(1000)
            .build();

    CloseableHttpClient httpClient = HttpClientBuilder
                                .create()
                                .setDefaultRequestConfig(requestConfig)
                                .build();

       HttpContext context = new BasicHttpContext(); 

    try {
        httpClient.execute(head,context);
        RedirectLocations locations = (RedirectLocations) context.getAttribute(HttpClientContext.REDIRECT_LOCATIONS);
        if (locations.size() == 1) return locations.get(0).toString();
        for(URI loc : locations.getAll()){
            if ((loc.toString().contains("purl.org")) || (loc.toString().contains("w3id.org"))) continue;
            else return loc.toString();
        }
    } catch (Exception e) {
    }       
    return null;
}
项目:quality    文件:EstimatedLinkExternalDataProviders.java   
private String getRedirection(String resource){
    HttpHead head = new HttpHead(resource);

    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(1000)
            .setConnectTimeout(1000)
            .setRedirectsEnabled(true)
            .build();

    CloseableHttpClient httpClient = HttpClientBuilder
                                .create()
                                .setDefaultRequestConfig(requestConfig)
                                .build();

       HttpContext context = new BasicHttpContext(); 
       CloseableHttpResponse response = null;

    try {
        response = httpClient.execute(head,context);
        RedirectLocations locations = (RedirectLocations) context.getAttribute(HttpClientContext.REDIRECT_LOCATIONS);
        if (locations.size() == 1) return locations.get(0).toString();
        for(URI loc : locations.getAll()){
            if ((loc.toString().contains("purl.org")) || (loc.toString().contains("w3id.org"))) continue;
            else return loc.toString();
        }
    } catch (Exception e) {
        //e.printStackTrace();
    }       
    return null;
}