Browse Source

getURI() now always returns a valid URI. Fixes issue 3209

setURI() now actually sets the URI rather than just parsing it and doing nothing with it. Fixes issue 3210
If the URI given to the parser has no port or an invalid port, default to 6667. Fixes issue 3208

Change-Id: I90ac6dbcc6544d82effee6dcd655d87d05126742
Reviewed-on: http://gerrit.dmdirc.com/126
Tested-by: Shane Mc Cormack <shane@dmdirc.com>
Reviewed-by: Gregory Holmes <greboid@dmdirc.com>
Tested-by: Gregory Holmes <greboid@dmdirc.com>
tags/0.6.3
Shane Mc Cormack 14 years ago
parent
commit
03dcb03936
1 changed files with 45 additions and 3 deletions
  1. 45
    3
      src/com/dmdirc/parser/irc/ServerInfo.java

+ 45
- 3
src/com/dmdirc/parser/irc/ServerInfo.java View File

@@ -23,6 +23,7 @@
23 23
 package com.dmdirc.parser.irc;
24 24
 
25 25
 import java.net.URI;
26
+import java.net.URISyntaxException;
26 27
 
27 28
 /**
28 29
  * Contains Server information.
@@ -88,11 +89,51 @@ public class ServerInfo {
88 89
     }
89 90
     
90 91
     /**
91
-     * Get the URI for this ServerInfo if created with one.
92
+     * Get the URI for this ServerInfo.
93
+     * This will return a new URI based on this ServerInfo.
94
+     * Protocol/Password/Host and Port are derived from the getXXXXX() methods
95
+     * the path, query and fragment from the
92 96
      *
93 97
      * @return URI for this ServerInfo
94 98
      */
95
-    public URI getURI() { return uri; }
99
+    public URI getURI() {
100
+        final StringBuilder uriString = new StringBuilder();
101
+
102
+        uriString.append(isSSL ? "irc://" : "ircs://");
103
+        if (!password.isEmpty()) {
104
+            uriString.append(password);
105
+            uriString.append("@");
106
+        }
107
+        uriString.append(host);
108
+        uriString.append(":");
109
+        uriString.append(port);
110
+        if (uri != null) {
111
+            if (!uri.getRawPath().isEmpty()) {
112
+                uriString.append(uri.getRawPath());
113
+            }
114
+            if (uri.getRawQuery() != null) {
115
+                uriString.append("?");
116
+                uriString.append(uri.getRawQuery());
117
+            }
118
+            if (uri.getRawFragment() != null) {
119
+                uriString.append("#");
120
+                uriString.append(uri.getRawFragment());
121
+            }
122
+        }
123
+        try {
124
+            return new URI(uriString.toString());
125
+        } catch (URISyntaxException ex) {
126
+            // Creating the new URI shouldn't fail unless the user passed
127
+            // stupid settings to setXXXX()
128
+            // In this case, try to return any given URI, else a blank one.
129
+            try {
130
+                return (uri != null) ? uri : new URI("");
131
+            } catch (URISyntaxException ex2) {
132
+                /* This can't ever happen. */
133
+                return null;
134
+            }
135
+        }
136
+    }
96 137
     
97 138
     /**
98 139
      * Set the URI for this ServerInfo.
@@ -101,8 +142,9 @@ public class ServerInfo {
101 142
      * @param uri URI to use to configure this ServerInfo
102 143
      */
103 144
     public void setURI(final URI uri) {
145
+        this.uri = uri;
104 146
         host = uri.getHost();
105
-        port = uri.getPort();
147
+        port = uri.getPort() > 0 ? uri.getPort() : 6667;
106 148
 
107 149
         if ("ircs".equals(uri.getScheme())) {
108 150
             setSSL(true);

Loading…
Cancel
Save