/** * 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 ); }
/** * 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; } }); } }
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; }
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; }