KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > proxy > ConnectivitySettings


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.proxy;
21
22 import java.net.InetSocketAddress JavaDoc;
23
24 /**
25  * This data object encapsulates parameters that are needed to establish connection to an arbitrary remote IP address.
26  * There are basically 2 types of connectivity:
27  * <ul>
28  * <li>direct connection (without any firewall or proxy, or with a transparent proxy)
29  * <li>mediated connection that routes via a firewall or proxy
30  * </ul>
31  * If this object represents a direct connection type, no further parameters are required. If this object
32  * represents a proxy connection, it also hold the proxy type, address, and port. Other optional parameters
33  * include proxy username and password.
34  *
35  * @author Maros Sandor
36  */

37 public class ConnectivitySettings {
38     /**
39      * Connection type constant for a direct connection.
40      */

41     public static final int CONNECTION_DIRECT = 0;
42
43     /**
44      * Connection type constant for connection via SOCKS proxies.
45      */

46     public static final int CONNECTION_VIA_SOCKS = 1;
47
48     /**
49      * Connection type constant for connection via HTTP proxies.
50      */

51     public static final int CONNECTION_VIA_HTTPS = 2;
52
53     private static final int CONNECTION_TYPE_MIN = CONNECTION_DIRECT;
54     private static final int CONNECTION_TYPE_MAX = CONNECTION_VIA_HTTPS;
55
56     private int mConnectionType;
57     private String JavaDoc mProxyHost;
58     private int mProxyPort;
59     private String JavaDoc mProxyUsername;
60     private char[] mProxyPassword;
61     private int mKeepAliveIntervalSeconds;
62
63     public String JavaDoc toString() {
64         return "Type: " + mConnectionType + " Proxy: " + mProxyUsername + "@" + mProxyHost + ":" + mProxyPort;
65     }
66
67     /**
68      * Constructs connectivity settings with the default connection setting (direct connection).
69      */

70     public ConnectivitySettings() {
71         mConnectionType = CONNECTION_DIRECT;
72         mKeepAliveIntervalSeconds = 60;
73     }
74
75     /**
76      * Changes configuration of this connectivity settings.
77      *
78      * @param type one of the connection type constants
79      * @param host proxy hostname, must not be null for proxy configurations, is ignored for direct connectivity.
80      * @param port proxy port, must be in range 1-65535 for proxy configurations, is ignored for direct connectivity.
81      * @param username a username to supply to proxy when it request user authentication, may be null if the proxy
82      * does not require authentication or we use direct connection
83      * @param proxyPassword password to supply to proxy when it request user authentication, may be null if the proxy
84      * does not require authentication or we use direct connection
85      * @throws java.lang.IllegalArgumentException
86      * if the connection type constant is illegal, the proxy number is out of range or
87      * the proxy host is empty or null (for non-direct connections)
88      */

89     public void setProxy(int type, String JavaDoc host, int port, String JavaDoc username, char[] proxyPassword) {
90         if (type < CONNECTION_TYPE_MIN || type > CONNECTION_TYPE_MAX) throw new IllegalArgumentException JavaDoc("Illegal connection type");
91
92         if (type != CONNECTION_DIRECT) {
93             if (port < 1 || port > 65535) throw new IllegalArgumentException JavaDoc("Illegal proxy port number: " + port);
94             if (host == null || (host = host.trim()).length() == 0) throw new IllegalArgumentException JavaDoc("A proxy host must be specified");
95         }
96
97         mConnectionType = type;
98         mProxyHost = host;
99         mProxyPort = port;
100         mProxyUsername = username;
101         mProxyPassword = proxyPassword;
102     }
103
104     public int getKeepAliveIntervalSeconds() {
105         return mKeepAliveIntervalSeconds;
106     }
107
108     public void setKeepAliveIntervalSeconds(int keepAliveIntervalSeconds) {
109         mKeepAliveIntervalSeconds = keepAliveIntervalSeconds;
110     }
111
112     public int getConnectionType() {
113         return mConnectionType;
114     }
115
116     public void setConnectionType(int connectionType) {
117         mConnectionType = connectionType;
118     }
119
120     public String JavaDoc getProxyHost() {
121         return mProxyHost;
122     }
123
124     public void setProxyHost(String JavaDoc proxyHost) {
125         mProxyHost = proxyHost;
126     }
127
128     public int getProxyPort() {
129         return mProxyPort;
130     }
131
132     public void setProxyPort(int proxyPort) {
133         mProxyPort = proxyPort;
134     }
135
136     public String JavaDoc getProxyUsername() {
137         return mProxyUsername;
138     }
139
140     public void setProxyUsername(String JavaDoc proxyUsername) {
141         mProxyUsername = proxyUsername;
142     }
143
144     public char[] getProxyPassword() {
145         return mProxyPassword;
146     }
147
148     public void setProxyPassword(char[] proxyPassword) {
149         mProxyPassword = proxyPassword;
150     }
151 }
152
Popular Tags