Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sparrow;
- import org.apache.http.HttpHost;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpUriRequest;
- import org.apache.http.client.protocol.ClientContext;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.protocol.BasicHttpContext;
- import org.apache.http.protocol.ExecutionContext;
- import org.apache.http.protocol.HttpContext;
- import org.apache.log4j.Logger;
- import java.io.IOException;
- /**
- * This class will attempt to follow a link completely until we get to the final URL
- * after all the 302 redirects. A bitly link may refer to a link that actually 302's to something
- * else so we need to follow the trail all the way.
- * @author Jim Plush
- *
- */
- public class FinalUrlGrabber {
- static Logger logger = Logger.getLogger(FinalUrlGrabber.class);
- public static String getURL(DefaultHttpClient httpClient, String startURL)
- {
- int cnt = 0;
- String finalURL = null;
- while(!startURL.equals(finalURL))
- {
- if(cnt > 1) {
- SparrowRunner.multiple302.incrementAndGet();
- }
- try {
- finalURL = followURL(httpClient, startURL);
- startURL = followURL(httpClient, finalURL);
- } catch(IOException e) {
- logger.info(e.toString());
- return null;
- }
- if(cnt > 20) {
- return null;
- }
- cnt++;
- //logger.info("START: "+startURL + " FINAL: "+finalURL);
- }
- return finalURL;
- }
- private static String followURL(DefaultHttpClient httpClient, String startURL) throws IOException
- {
- String finalURL = null;
- HttpGet httpget = null;
- HttpHost target = null;
- HttpUriRequest req = null;
- try {
- HttpContext localContext = new BasicHttpContext();
- localContext.setAttribute(ClientContext.COOKIE_STORE,HttpClientFetcher.emptyCookieStore);
- httpget = new HttpGet(startURL);
- httpget.setHeader("Range", "bytes=0-5000");
- HttpResponse response = httpClient.execute(httpget, localContext);
- HttpHost host = (HttpHost)localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
- HttpUriRequest finalRequest = (HttpUriRequest)localContext.getAttribute(ExecutionContext.HTTP_REQUEST);
- target = (HttpHost) localContext.getAttribute( ExecutionContext.HTTP_TARGET_HOST );
- req = (HttpUriRequest) localContext.getAttribute( ExecutionContext.HTTP_REQUEST );
- finalURL = target+""+req.getURI();
- return finalURL;
- } catch(Exception e) {
- throw new IOException("UNABLE TO GET LINK: "+startURL+ " "+e.toString());
- } finally {
- if(httpget != null) { try { httpget.abort(); } catch(Exception e) {}}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement