| 1 |
package net.oni2; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.FileInputStream; |
| 5 |
import java.io.FileNotFoundException; |
| 6 |
import java.io.FileOutputStream; |
| 7 |
import java.io.IOException; |
| 8 |
import java.io.Serializable; |
| 9 |
import java.net.InetSocketAddress; |
| 10 |
import java.net.MalformedURLException; |
| 11 |
import java.net.Proxy; |
| 12 |
import java.net.Proxy.Type; |
| 13 |
import java.net.SocketTimeoutException; |
| 14 |
import java.net.URL; |
| 15 |
import java.net.URLConnection; |
| 16 |
|
| 17 |
import com.thoughtworks.xstream.XStream; |
| 18 |
import com.thoughtworks.xstream.io.xml.StaxDriver; |
| 19 |
|
| 20 |
/** |
| 21 |
* @author Christian Illy |
| 22 |
*/ |
| 23 |
public class ProxySettings implements Serializable { |
| 24 |
private static final long serialVersionUID = 7764207071836441954L; |
| 25 |
|
| 26 |
private static ProxySettings instance = new ProxySettings(); |
| 27 |
|
| 28 |
private boolean useProxy = false; |
| 29 |
private String hostOrIp = ""; |
| 30 |
private int port = 8080; |
| 31 |
|
| 32 |
private transient boolean validated = false; |
| 33 |
private transient boolean validatedOk = false; |
| 34 |
|
| 35 |
/** |
| 36 |
* Get the proxy for URL connections. Validates connection first if not |
| 37 |
* validated yet |
| 38 |
* |
| 39 |
* @return Proxy |
| 40 |
*/ |
| 41 |
public Proxy getProxy() { |
| 42 |
if (!useProxy) |
| 43 |
return Proxy.NO_PROXY; |
| 44 |
if (!validated) |
| 45 |
validate(); |
| 46 |
if (!validatedOk) |
| 47 |
return Proxy.NO_PROXY; |
| 48 |
|
| 49 |
return new Proxy(Type.HTTP, new InetSocketAddress(hostOrIp, port)); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Check if a connection to the given proxy can be established |
| 54 |
* |
| 55 |
* @return Data entered and connection Ok? |
| 56 |
*/ |
| 57 |
public boolean validate() { |
| 58 |
if (validated) { |
| 59 |
return validatedOk; |
| 60 |
} else { |
| 61 |
validated = true; |
| 62 |
if (isValid()) { |
| 63 |
try { |
| 64 |
URLConnection con = new URL(String.format("http://%s:%d", |
| 65 |
hostOrIp, port)).openConnection(); |
| 66 |
con.setConnectTimeout(1000); |
| 67 |
con.connect(); |
| 68 |
validatedOk = true; |
| 69 |
return true; |
| 70 |
} catch (MalformedURLException e) { |
| 71 |
} catch (SocketTimeoutException e) { |
| 72 |
} catch (IOException e) { |
| 73 |
} |
| 74 |
} |
| 75 |
validatedOk = false; |
| 76 |
return false; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @return Is data for both host and port entered? |
| 82 |
*/ |
| 83 |
public boolean isValid() { |
| 84 |
return (hostOrIp != null) && (port > 0); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @return Should a proxy be used for connections? |
| 89 |
*/ |
| 90 |
public boolean isUseProxy() { |
| 91 |
return useProxy; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @param use |
| 96 |
* Should a proxy be used for connections? |
| 97 |
*/ |
| 98 |
public void setUseProxy(boolean use) { |
| 99 |
this.useProxy = use; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @return Hostname or IP of host to use as proxy |
| 104 |
*/ |
| 105 |
public String getHostOrIp() { |
| 106 |
return hostOrIp; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @param hoi |
| 111 |
* Hostname or IP of host to use as proxy |
| 112 |
*/ |
| 113 |
public void setHostOrIp(String hoi) { |
| 114 |
validated = false; |
| 115 |
this.hostOrIp = hoi; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @return Port number proxy is listening on |
| 120 |
*/ |
| 121 |
public int getPort() { |
| 122 |
return port; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @param port |
| 127 |
* Port number proxy is listening on |
| 128 |
*/ |
| 129 |
public void setPort(int port) { |
| 130 |
validated = false; |
| 131 |
this.port = port; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Get the singleton instance |
| 136 |
* |
| 137 |
* @return Singleton instance |
| 138 |
*/ |
| 139 |
public static ProxySettings getInstance() { |
| 140 |
return instance; |
| 141 |
} |
| 142 |
|
| 143 |
private static XStream getXStream() { |
| 144 |
XStream xs = new XStream(new StaxDriver()); |
| 145 |
xs.alias("Proxy", ProxySettings.class); |
| 146 |
return xs; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Serializes the settings to disk |
| 151 |
* |
| 152 |
* @param settingsFile |
| 153 |
* File to write to |
| 154 |
*/ |
| 155 |
public void serializeToFile(File settingsFile) { |
| 156 |
try { |
| 157 |
FileOutputStream fos = new FileOutputStream(settingsFile); |
| 158 |
XStream xs = getXStream(); |
| 159 |
xs.toXML(this, fos); |
| 160 |
fos.close(); |
| 161 |
} catch (FileNotFoundException e) { |
| 162 |
e.printStackTrace(); |
| 163 |
} catch (IOException e) { |
| 164 |
e.printStackTrace(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Deserializes the settings from disk |
| 170 |
* |
| 171 |
* @param settingsFile |
| 172 |
* File to read from |
| 173 |
*/ |
| 174 |
public static void deserializeFromFile(File settingsFile) { |
| 175 |
try { |
| 176 |
FileInputStream fis = new FileInputStream(settingsFile); |
| 177 |
XStream xs = getXStream(); |
| 178 |
Object obj = xs.fromXML(fis); |
| 179 |
if (obj instanceof ProxySettings) |
| 180 |
instance = (ProxySettings) obj; |
| 181 |
fis.close(); |
| 182 |
} catch (FileNotFoundException e) { |
| 183 |
} catch (IOException e) { |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
} |