1 11 package org.eclipse.help.internal.standalone; 12 13 import java.io.*; 14 import java.net.*; 15 import java.util.Properties ; 16 17 import javax.net.ssl.HostnameVerifier; 18 import javax.net.ssl.HttpsURLConnection; 19 20 24 public class EclipseConnection { 25 private String host; 27 private String port; 29 30 public EclipseConnection() { 31 } 32 33 public String getPort() { 34 return port; 35 } 36 37 public String getHost() { 38 return host; 39 } 40 41 public void reset() { 42 host = null; 43 port = null; 44 } 45 46 public boolean isValid() { 47 return (host != null && port != null); 48 } 49 50 public void connect(URL url) throws InterruptedException , Exception { 51 try { 52 HttpURLConnection connection = (HttpURLConnection) url 53 .openConnection(); 54 if (connection instanceof HttpsURLConnection) { 55 HttpsURLConnection secureConnection = (HttpsURLConnection) connection; 56 secureConnection.setHostnameVerifier(new HostnameVerifier() { 60 public boolean verify(String urlHostName, javax.net.ssl.SSLSession session) { 61 if (Options.isDebug()) { 62 System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost()); 65 } 66 return true; 67 } 68 }); 69 } 70 if (Options.isDebug()) { 71 System.out.println("Connection to control servlet created."); } 73 connection.connect(); 74 if (Options.isDebug()) { 75 System.out.println("Connection to control servlet connected."); } 77 int code = connection.getResponseCode(); 78 if (Options.isDebug()) { 79 System.out 80 .println("Response code from control servlet=" + code); } 82 connection.disconnect(); 83 if (code == HttpURLConnection.HTTP_MOVED_TEMP) { 84 String redirectLocation = connection.getHeaderField("location"); URL redirectURL = new URL(redirectLocation); 87 if (url.equals(redirectURL)) { 88 if (Options.isDebug()) { 89 System.out.println("Redirecting to the same URL! " + redirectLocation); 91 } 92 return; 93 } 94 if (Options.isDebug()) { 95 System.out.println("Follows redirect to " + redirectLocation); } 97 connect(redirectURL); 98 } 99 return; 100 } catch (IOException ioe) { 101 if (Options.isDebug()) { 102 ioe.printStackTrace(); 103 } 104 } 105 } 106 107 111 public void renew() throws Exception { 112 Properties p = new Properties (); 113 FileInputStream is = null; 114 try { 115 is = new FileInputStream(Options.getConnectionFile()); 116 p.load(is); 117 is.close(); 118 } catch (IOException ioe) { 119 throw ioe; 121 } finally { 122 if (is != null) { 123 try { 124 is.close(); 125 } catch (IOException ioe2) { 126 } 127 } 128 } 129 host = (String ) p.get("host"); port = (String ) p.get("port"); if (Options.isDebug()) { 132 System.out.println("Help server host=" + host); } 134 if (Options.isDebug()) { 135 System.out.println("Help server port=" + port); } 137 } 138 139 } 140 | Popular Tags |