Browse Source

Store a URI in ServerInfo instead of a load of values

Fixes CLIENT-208

Change-Id: I958789f26b90f28129f22566addb63e5a5d23603
Reviewed-on: http://gerrit.dmdirc.com/1925
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.7rc1
Greg Holmes 13 years ago
parent
commit
28cd8696e7

+ 11
- 3
src/com/dmdirc/parser/irc/IRCParser.java View File

@@ -117,7 +117,7 @@ public class IRCParser implements SecureParser, EncodingParser, Runnable {
117 117
      */
118 118
     public MyInfo me = new MyInfo();
119 119
     /**    Server Info requested by user. */
120
-    public ServerInfo server = new ServerInfo();
120
+    public ServerInfo server;
121 121
 
122 122
     /** Should PINGs be sent to the server to check if its alive? */
123 123
     private boolean checkServerPing = true;
@@ -356,7 +356,12 @@ public class IRCParser implements SecureParser, EncodingParser, Runnable {
356 356
 
357 357
     /** {@inheritDoc} */
358 358
     @Override
359
-    public URI getURI() { return server.getURI(); }
359
+    public URI getURI() {
360
+        if (server == null) {
361
+            return null;
362
+        }
363
+        return server.getURI();
364
+    }
360 365
 
361 366
     /** {@inheritDoc} */
362 367
     @Override
@@ -752,6 +757,9 @@ public class IRCParser implements SecureParser, EncodingParser, Runnable {
752 757
      * @throws KeyManagementException if the trustManager is invalid
753 758
      */
754 759
     private void connect() throws UnknownHostException, IOException, NoSuchAlgorithmException, KeyManagementException {
760
+        if (server == null) {
761
+            throw new UnknownHostException("Unspecified host.");
762
+        }
755 763
         resetState();
756 764
         callDebugInfo(DEBUG_SOCKET, "Connecting to " + server.getHost() + ":" + server.getPort());
757 765
 
@@ -801,7 +809,7 @@ public class IRCParser implements SecureParser, EncodingParser, Runnable {
801 809
 
802 810
         rawSocket = socket;
803 811
 
804
-        if (server.getSSL()) {
812
+        if (server.isSSL()) {
805 813
             callDebugInfo(DEBUG_SOCKET, "Server is SSL.");
806 814
 
807 815
             if (myTrustManager == null) { myTrustManager = trustAllCerts; }

+ 29
- 107
src/com/dmdirc/parser/irc/ServerInfo.java View File

@@ -31,6 +31,7 @@ import java.net.URISyntaxException;
31 31
  * @see IRCParser
32 32
  */
33 33
 public class ServerInfo {
34
+
34 35
     /**
35 36
      * A version number for this class. It should be changed whenever the class
36 37
      * structure is changed (or anything else that would prevent serialized
@@ -38,14 +39,6 @@ public class ServerInfo {
38 39
      */
39 40
     private static final long serialVersionUID = 1;
40 41
 
41
-    /** Server to connect to (Default: "irc.quakenet.org"). */
42
-    private String host = "irc.quakenet.org";
43
-    /** Port server listens on for client connections (Default: 6667). */
44
-    private int port = 6667;
45
-    /** Optional password needed to connect to server (Default: ""). */
46
-    private String password = "";
47
-    /** Is this an ssl-enabled server (Default: false). */
48
-    private boolean isSSL = false;
49 42
     /** Are we using a socks proxy (Default: false). */
50 43
     private boolean useSocksProxy = false;
51 44
     /** Proxy server to connect to (Default: "127.0.0.1"). */
@@ -60,23 +53,25 @@ public class ServerInfo {
60 53
     private URI uri = null;
61 54
 
62 55
     /**
63
-     * Constructor using default values.
64
-     */
65
-    public ServerInfo() {
66
-        //Use default values
67
-    }
68
-
69
-    /**
70
-     * Constructor using specifed host, port and password, SSL/Proxy must be specifed separately.
56
+     * Constructor using specifed host, port and password, SSL/Proxy must be
57
+     * specifed separately.
71 58
      *
72 59
      * @param serverHost Host to use
73 60
      * @param serverPort Port to use
74 61
      * @param serverPass Password to use
75 62
      */
76
-    public ServerInfo(final String serverHost, final int serverPort, final String serverPass) {
77
-        host = serverHost;
78
-        port = serverPort;
79
-        password = serverPass;
63
+    public ServerInfo(final String serverHost, final int serverPort,
64
+            final String serverPass) {
65
+        try {
66
+            uri = new URI("irc", serverPass, serverHost, serverPort, null,
67
+                    null, null);
68
+        } catch (URISyntaxException ex) {
69
+            try {
70
+                uri = new URI("irc", null, "127.0.0.1", -1, null, null, null);
71
+            } catch (URISyntaxException ex1) {
72
+                //Won't happen
73
+            }
74
+        }
80 75
     }
81 76
 
82 77
     /**
@@ -87,7 +82,7 @@ public class ServerInfo {
87 82
      * @since 0.6.3
88 83
      */
89 84
     public ServerInfo(final URI uri) {
90
-        setURI(uri);
85
+        this.uri = uri;
91 86
     }
92 87
 
93 88
     /**
@@ -99,117 +94,44 @@ public class ServerInfo {
99 94
      * @return URI for this ServerInfo
100 95
      */
101 96
     public URI getURI() {
102
-        final StringBuilder uriString = new StringBuilder();
103
-
104
-        uriString.append(isSSL ? "ircs://" : "irc://");
105
-        if (!password.isEmpty()) {
106
-            uriString.append(password);
107
-            uriString.append("@");
108
-        }
109
-        uriString.append(host);
110
-        uriString.append(":");
111
-        uriString.append(port);
112
-        if (uri != null) {
113
-            if (!uri.getRawPath().isEmpty()) {
114
-                uriString.append(uri.getRawPath());
115
-            }
116
-            if (uri.getRawQuery() != null) {
117
-                uriString.append("?");
118
-                uriString.append(uri.getRawQuery());
119
-            }
120
-            if (uri.getRawFragment() != null) {
121
-                uriString.append("#");
122
-                uriString.append(uri.getRawFragment());
123
-            }
124
-        }
125
-        try {
126
-            return new URI(uriString.toString());
127
-        } catch (URISyntaxException ex) {
128
-            // Creating the new URI shouldn't fail unless the user passed
129
-            // stupid settings to setXXXX()
130
-            // In this case, try to return any given URI, else a blank one.
131
-            try {
132
-                return (uri == null) ? new URI("") : uri;
133
-            } catch (URISyntaxException ex2) {
134
-                /* This can't ever happen. */
135
-                return null;
136
-            }
137
-        }
138
-    }
139
-
140
-    /**
141
-     * Set the URI for this ServerInfo.
142
-     * This will overwrite host/port/password and isSSL.
143
-     *
144
-     * @param uri URI to use to configure this ServerInfo
145
-     */
146
-    public void setURI(final URI uri) {
147
-        this.uri = uri;
148
-        host = uri.getHost();
149
-        port = uri.getPort() > 0 ? uri.getPort() : 6667;
150
-
151
-        if ("ircs".equals(uri.getScheme())) {
152
-            setSSL(true);
153
-        }
154
-
155
-        password = uri.getUserInfo() == null ? "" : uri.getUserInfo();
97
+        return uri;
156 98
     }
157 99
 
158
-    /**
159
-     * Set the hostname.
160
-     *
161
-     * @param newValue Value to set to.
162
-     */
163
-    public void setHost(final String newValue) { host = newValue; }
164
-
165 100
     /**
166 101
      * Get the hostname.
167 102
      *
168 103
      * @return Current hostname
169 104
      */
170
-    public String getHost() { return host; }
171
-
172
-    /**
173
-     * Set the port.
174
-     *
175
-     * @param newValue Value to set to.
176
-     */
177
-    public void setPort(final int newValue) { port = newValue; }
105
+    public String getHost() {
106
+        return uri.getHost() == null ? "" : uri.getHost();
107
+    }
178 108
 
179 109
     /**
180 110
      * Get the port.
181 111
      *
182 112
      * @return Current port
183 113
      */
184
-    public int getPort() { return port; }
185
-
186
-    /**
187
-     * Set the password.
188
-     *
189
-     * @param newValue Value to set to.
190
-     */
191
-    public void setPassword(final String newValue) { password = newValue; }
114
+    public int getPort() {
115
+        return uri.getPort() == -1 ? 6667 : uri.getPort();
116
+    }
192 117
 
193 118
     /**
194 119
      * Get the password.
195 120
      *
196 121
      * @return Current Password
197 122
      */
198
-    public String getPassword() { return password; }
199
-
200
-    /**
201
-     * Set if the server uses ssl.
202
-     *
203
-     * @param newValue true if server uses ssl, else false
204
-     */
205
-    public void setSSL(final boolean newValue) { isSSL = newValue; }
123
+    public String getPassword() {
124
+        return uri.getUserInfo() == null ? "" : uri.getUserInfo();
125
+    }
206 126
 
207 127
     /**
208 128
      * Get if the server uses ssl.
209 129
      *
210 130
      * @return true if server uses ssl, else false
211 131
      */
212
-    public boolean getSSL() { return isSSL; }
132
+    public boolean isSSL() {
133
+        return "ircs".equals(uri.getScheme());
134
+    }
213 135
 
214 136
     /**
215 137
      * Set if we are connecting via a socks proxy.

+ 1
- 1
test/com/dmdirc/harness/parser/TestParser.java View File

@@ -40,7 +40,7 @@ public class TestParser extends IRCParser implements Parser {
40 40
     public String network = null;
41 41
 
42 42
     public TestParser() {
43
-        super();
43
+        super(new ServerInfo("", -1, ""));
44 44
         currentSocketState = SocketState.OPEN;
45 45
     }
46 46
 

+ 41
- 20
test/com/dmdirc/parser/irc/ServerInfoTest.java View File

@@ -22,41 +22,62 @@
22 22
 
23 23
 package com.dmdirc.parser.irc;
24 24
 
25
+import java.net.URI;
26
+import java.net.URISyntaxException;
25 27
 import org.junit.Test;
26 28
 import static org.junit.Assert.*;
27 29
 
28 30
 public class ServerInfoTest {
29 31
     
30 32
     @Test
31
-    public void testHost() {
32
-        final ServerInfo si = new ServerInfo("host0", 5, "");
33
+    public void testHost() throws URISyntaxException {
34
+        final ServerInfo si = new ServerInfo(new URI("ircs", "pass1", "host0", 
35
+                5, null, null, null));
33 36
         assertEquals("host0", si.getHost());
34
-        si.setHost("host1");
35
-        assertEquals("host1", si.getHost());
36 37
     }
37
-    
38
+
38 39
     @Test
39
-    public void testPort() {
40
-        final ServerInfo si = new ServerInfo("host0", 5, "");
40
+    public void testPort() throws URISyntaxException {
41
+        final ServerInfo si = new ServerInfo(new URI("ircs", "pass1", "host0",
42
+                5, null, null, null));
41 43
         assertEquals(5, si.getPort());
42
-        si.setPort(65530);
43
-        assertEquals(65530, si.getPort());
44 44
     }
45
-    
45
+
46 46
     @Test
47
-    public void testPassword() {
48
-        final ServerInfo si = new ServerInfo("host0", 5, "pass1");
47
+    public void testNoPort() throws URISyntaxException {
48
+        final ServerInfo si = new ServerInfo(new URI("ircs", "pass1", "host0",
49
+                -1, null, null, null));
50
+        assertEquals(6667, si.getPort());
51
+    }
52
+
53
+    @Test
54
+    public void testPassword() throws URISyntaxException {
55
+        final ServerInfo si = new ServerInfo(new URI("ircs", "pass1", "host0",
56
+                5, null, null, null));
49 57
         assertEquals("pass1", si.getPassword());
50
-        si.setPassword("pass2");
51
-        assertEquals("pass2", si.getPassword());
58
+
52 59
     }
53
-    
60
+
54 61
     @Test
55
-    public void testSSL() {
56
-        final ServerInfo si = new ServerInfo("host0", 5, "pass1");
57
-        assertFalse(si.getSSL());
58
-        si.setSSL(true);
59
-        assertTrue(si.getSSL());
62
+    public void testNoPassword() throws URISyntaxException {
63
+        final ServerInfo si = new ServerInfo(new URI("ircs", null, "host0",
64
+                5, null, null, null));
65
+        assertEquals("", si.getPassword());
66
+
67
+    }
68
+
69
+    @Test
70
+    public void testSSL() throws URISyntaxException {
71
+        final ServerInfo si = new ServerInfo(new URI("ircs", "pass1", "host0",
72
+                5, null, null, null));
73
+        assertTrue(si.isSSL());
74
+    }
75
+
76
+    @Test
77
+    public void testNonSSL() throws URISyntaxException {
78
+        final ServerInfo si = new ServerInfo(new URI("irc", "pass1", "host0",
79
+                5, null, null, null));
80
+        assertFalse(si.isSSL());
60 81
     }
61 82
     
62 83
     @Test

Loading…
Cancel
Save