|
| 1 | +package tinystruct.examples; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStreamReader; |
| 7 | +import java.net.HttpURLConnection; |
| 8 | +import java.net.MalformedURLException; |
| 9 | +import java.net.URL; |
| 10 | +import java.util.Iterator; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Map.Entry; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +import org.tinystruct.AbstractApplication; |
| 17 | +import org.tinystruct.ApplicationException; |
| 18 | + |
| 19 | +public class redirectChecker extends AbstractApplication { |
| 20 | + |
| 21 | + @Override |
| 22 | + public void init() { |
| 23 | + // TODO Auto-generated method stub |
| 24 | + this.setAction("testing", "testing"); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + HTTP/1.1 301 Moved Permanently |
| 29 | + Server:[nginx] |
| 30 | + X-Request-ID:[v-34c8ab26-3140-11e6-8d1f-22000aab1671] |
| 31 | + X-Content-Type-Options:[nosniff] |
| 32 | + X-Age:[158593] |
| 33 | + Connection:[keep-alive] |
| 34 | + Date:[Wed, 15 Jun 2016 04:27:24 GMT] |
| 35 | + X-Varnish:[2128902194 2128190039] |
| 36 | + Cache-Control:[max-age=1209600] |
| 37 | + Content-Encoding:[gzip] |
| 38 | + Vary:[Accept-Encoding] |
| 39 | + Expires:[Mon, 27 Jun 2016 08:24:11 GMT] |
| 40 | + Content-Length:[217] |
| 41 | + X-Cache-Hits:[168] |
| 42 | + Location:[http://networks.*****.com/portfolio/industries/railways] |
| 43 | + Content-Type:[text/html; charset=iso-8859-1] |
| 44 | + * @throws ApplicationException |
| 45 | + */ |
| 46 | + public void testing() throws ApplicationException{ |
| 47 | + try |
| 48 | + { |
| 49 | + FileInputStream fileInput = new FileInputStream("url-rules.txt"); |
| 50 | + InputStreamReader reader = new InputStreamReader(fileInput); |
| 51 | + BufferedReader bufferReader = new BufferedReader(reader); |
| 52 | + |
| 53 | + String line, destinationURL, currentURL; |
| 54 | + String[] parts; |
| 55 | + URL from; |
| 56 | + while((line = bufferReader.readLine()) != null) |
| 57 | + { |
| 58 | + parts = line.split("\\t|\\s"); |
| 59 | + destinationURL = parts[1]; |
| 60 | + from = new URL(parts[0]); |
| 61 | + |
| 62 | + boolean follow = false, redirected = false; |
| 63 | + do { |
| 64 | + HttpURLConnection connection = (HttpURLConnection) from.openConnection(); |
| 65 | + connection.setInstanceFollowRedirects(follow); |
| 66 | + connection.connect(); |
| 67 | + currentURL = connection.getHeaderField("Location").trim(); |
| 68 | + |
| 69 | + int responseCode = connection.getResponseCode(); |
| 70 | + |
| 71 | + redirected = (responseCode == 302); |
| 72 | + |
| 73 | + if(!destinationURL.trim().equalsIgnoreCase(currentURL)) { |
| 74 | + Map<String, List<String>> list = connection.getHeaderFields(); |
| 75 | + Set<Entry<String, List<String>>> set = list.entrySet(); |
| 76 | + Iterator<Entry<String, List<String>>> iterator = set.iterator(); |
| 77 | + Entry<String, List<String>> entity; |
| 78 | + while(iterator.hasNext()) { |
| 79 | + entity = iterator.next(); |
| 80 | + System.out.println(entity.getKey()+":"+entity.getValue()); |
| 81 | + } |
| 82 | + } |
| 83 | + if(redirected) from = new URL(currentURL); |
| 84 | + System.out.println("Status:\t"+ connection.getHeaderField(0) +"\t"+ (destinationURL.trim().equalsIgnoreCase(currentURL)?"OK":"Failure")); |
| 85 | + System.out.println("From:\t"+from+"\r\nTo:\t"+currentURL+"\r\n"); |
| 86 | + } while (redirected); |
| 87 | + |
| 88 | + System.out.println(); |
| 89 | + } |
| 90 | + bufferReader.close(); |
| 91 | + reader.close(); |
| 92 | + fileInput.close(); |
| 93 | + } catch (MalformedURLException e) { |
| 94 | + throw new ApplicationException(e.getMessage(), e.getCause()); |
| 95 | + } catch (IOException e) { |
| 96 | + throw new ApplicationException(e.getMessage(), e.getCause()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public String version() { |
| 102 | + // TODO Auto-generated method stub |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments