OSDN Git Service

JSON入出力部をJovsonzプロジェクトに分離。
[jindolf/Jindolf.git] / src / main / java / jp / sourceforge / jindolf / ProxyInfo.java
1 /*
2  * proxy information
3  *
4  * License : The MIT License
5  * Copyright(c) 2009 olyutorskii
6  */
7
8 package jp.sourceforge.jindolf;
9
10 import java.net.InetSocketAddress;
11 import java.net.Proxy;
12 import java.net.SocketAddress;
13 import jp.sourceforge.jovsonz.JsNumber;
14 import jp.sourceforge.jovsonz.JsObject;
15 import jp.sourceforge.jovsonz.JsPair;
16 import jp.sourceforge.jovsonz.JsString;
17 import jp.sourceforge.jovsonz.JsValue;
18
19 /**
20  * プロクシ情報。
21  * SOCKSも範疇に入る。
22  * IP4,IP6以外のアドレスは未サポート。
23  */
24 public class ProxyInfo{
25
26     /**
27      * アドレス「0.0.0.0」、ポート番号0のソケット端点。
28      */
29     public static final InetSocketAddress IP4SOCKET_NOBODY =
30             InetSocketAddress.createUnresolved("0.0.0.0", 0);
31
32     /** デフォルトのプロクシ(直接接続)。 */
33     public static final ProxyInfo DEFAULT = new ProxyInfo();
34
35     private static final String HASH_TYPE = "type";
36     private static final String HASH_HOST = "host";
37     private static final String HASH_PORT = "port";
38
39
40     private final Proxy proxy;
41     private final InetSocketAddress inetAddr;
42
43
44     /**
45      * コンストラクタ。
46      * 直接接続プロクシが暗黙に指定される。
47      */
48     public ProxyInfo(){
49         this(Proxy.NO_PROXY);
50         return;
51     }
52
53     /**
54      * コンストラクタ。
55      * @param proxy プロクシ
56      */
57     public ProxyInfo(Proxy proxy){
58
59         this.proxy = proxy;
60
61         if(this.proxy.type() == Proxy.Type.DIRECT){
62             this.inetAddr = IP4SOCKET_NOBODY;
63         }else{
64             SocketAddress addr = this.proxy.address();
65             if( ! (addr instanceof InetSocketAddress) ){
66                 throw new IllegalArgumentException();
67             }
68             this.inetAddr = (InetSocketAddress) addr;
69         }
70
71         return;
72     }
73
74     /**
75      * コンストラクタ。
76      * @param type プロクシの種別
77      * @param hostName ホスト名
78      * @param port ポート番号
79      */
80     public ProxyInfo(Proxy.Type type, String hostName, int port){
81         this(type, InetSocketAddress.createUnresolved(hostName, port));
82         return;
83     }
84
85     /**
86      * コンストラクタ。
87      * @param type プロクシの種別
88      * @param inetAddr 端点
89      */
90     public ProxyInfo(Proxy.Type type, InetSocketAddress inetAddr){
91         super();
92
93         if(type == null || inetAddr == null){
94             throw new NullPointerException();
95         }
96
97         if(type == Proxy.Type.DIRECT){
98             this.proxy = Proxy.NO_PROXY;
99         }else{
100             this.proxy = new Proxy(type, inetAddr);
101         }
102
103         this.inetAddr = inetAddr;
104
105         return;
106     }
107
108
109     /**
110      * プロクシ設定をJSON形式にエンコードする。
111      * @param proxyInfo プロクシ設定
112      * @return JSON object
113      */
114     public static JsObject buildJson(ProxyInfo proxyInfo){
115         JsPair type = new JsPair(HASH_TYPE, proxyInfo.getType().name());
116         JsPair host = new JsPair(HASH_HOST, proxyInfo.getHostName());
117         JsPair port = new JsPair(HASH_PORT, proxyInfo.getPort());
118
119         JsObject result = new JsObject();
120         result.putPair(type);
121         result.putPair(host);
122         result.putPair(port);
123
124         return result;
125     }
126
127     /**
128      * JSONからのプロクシ設定復元。
129      * @param obj JSON object
130      * @return 復元されたプロクシ設定。
131      */
132     public static ProxyInfo decodeJson(JsObject obj){
133         JsValue value;
134
135         Proxy.Type type = Proxy.Type.DIRECT;
136         value = obj.getValue(HASH_TYPE);
137         if(value instanceof JsString){
138             JsString string = (JsString) value;
139             try{
140                 type = Enum.valueOf(Proxy.Type.class, string.toRawString());
141             }catch(IllegalArgumentException e){
142                 // NOTHING
143             }
144         }
145
146         String host = "0.0.0.0";
147         value = obj.getValue(HASH_HOST);
148         if(value instanceof JsString){
149             JsString string = (JsString) value;
150             host = string.toRawString();
151         }
152
153         int port = 0;
154         value = obj.getValue(HASH_PORT);
155         if(value instanceof JsNumber){
156             JsNumber number = (JsNumber) value;
157             port = number.intValue();
158         }
159
160         return new ProxyInfo(type, host, port);
161     }
162
163     /**
164      * {@inheritDoc}
165      * @param obj {@inheritDoc}
166      * @return {@inheritDoc}
167      */
168     @Override
169     public boolean equals(Object obj){
170         if(obj == null) return false;
171         if(getClass() != obj.getClass()) return false;
172         ProxyInfo target = (ProxyInfo) obj;
173
174         boolean result;
175
176         result = this.proxy.equals(target.proxy);
177         if(result){
178             result = this.inetAddr.equals(target.inetAddr);
179         }
180
181         return result;
182     }
183
184     /**
185      * {@inheritDoc}
186      * @return {@inheritDoc}
187      */
188     @Override
189     public int hashCode(){
190         return this.proxy.hashCode() ^ this.inetAddr.hashCode();
191     }
192
193     /**
194      * プロクシを返す。
195      * 直結プロクシだった場合、ホスト名とポート番号には何が入っているか不明。
196      * @return プロクシ
197      */
198     public Proxy getProxy(){
199         return this.proxy;
200     }
201
202     /**
203      * プロクシ種別を返す。
204      * @return プロクシ種別
205      */
206     public Proxy.Type getType(){
207         return this.proxy.type();
208     }
209
210     /**
211      * ソケット端点を返す。
212      * @return ソケット端点
213      */
214     public InetSocketAddress address(){
215         return this.inetAddr;
216     }
217
218     /**
219      * ホスト名を返す。
220      * @return ホスト名
221      */
222     public String getHostName(){
223         return this.inetAddr.getHostName();
224     }
225
226     /**
227      * ポート番号を返す。
228      * @return ポート番号
229      */
230     public int getPort(){
231         return this.inetAddr.getPort();
232     }
233
234     // TODO 認証情報のサポート
235 }