1 45 package org.exolab.jms.net.uri; 46 47 import java.net.InetAddress ; 48 import java.net.UnknownHostException ; 49 50 import org.exolab.jms.common.security.BasicPrincipal; 51 52 53 60 public final class URIHelper { 61 62 65 private URIHelper() { 66 } 67 68 77 public static URI create(String scheme, String host, int port) 78 throws InvalidURIException { 79 return create(scheme, host, port, "/"); 80 } 81 82 92 public static URI create(String scheme, String host, int port, String path) 93 throws InvalidURIException { 94 URI result; 95 try { 96 result = new URI(scheme, null, host, port, path, null, null); 97 } catch (URI.MalformedURIException exception) { 98 throw new InvalidURIException(exception.getMessage()); 99 } 100 return result; 101 } 102 103 111 public static URI parse(String uri) throws InvalidURIException { 112 URI result; 113 try { 114 result = new URI(uri); 115 fixPath(result); 116 } catch (URI.MalformedURIException exception) { 117 throw new InvalidURIException(exception.getMessage()); 118 } 119 return result; 120 } 121 122 130 public static URI parse(String uri, String scheme) 131 throws InvalidURIException { 132 URI result = parse(uri); 133 if (!result.getScheme().equals(scheme)) { 134 throw new InvalidURIException( 135 "Invalid scheme: " + result.getScheme()); 136 } 137 return result; 138 } 139 140 149 public static URI parseHostPort(String uri, String scheme) 150 throws InvalidURIException { 151 URI result = parse(uri, scheme); 152 if (result.getHost() == null) { 153 throw new InvalidURIException("No host specified in URI: " + uri); 154 } 155 if (result.getPort() == -1) { 156 throw new InvalidURIException("No port specified in URI: " + uri); 157 } 158 if (result.getPath() != null && !result.getPath().equals("") 159 && !result.getPath().equals("/")) { 160 throw new InvalidURIException( 161 "URI must not specify a path: " + uri); 162 } 163 return result; 164 } 165 166 175 public static URI convertHostToAddress(URI uri) { 176 URI result = uri; 177 String host = uri.getHost(); 178 if (host != null && !host.equals("")) { 179 try { 180 InetAddress address = InetAddress.getByName(host); 181 result = new URI(uri.getScheme(), uri.getUserinfo(), 182 address.getHostAddress(), uri.getPort(), 183 uri.getPath(), uri.getQueryString(), 184 uri.getFragment()); 185 fixPath(result); 186 } catch (UnknownHostException ignore) { 187 } catch (URI.MalformedURIException exception) { 188 throw new IllegalArgumentException ("Failed to construct URI: " 190 + exception.getMessage()); 191 } 192 } 193 return result; 194 } 195 196 204 public static BasicPrincipal getPrincipal(URI uri) { 205 BasicPrincipal principal = null; 206 String userinfo = uri.getUserinfo(); 207 if (userinfo != null && userinfo.length() != 0) { 208 int index = userinfo.indexOf(":"); 209 String user; 210 String password = ""; 211 if (index != -1) { 212 user = userinfo.substring(0, index); 213 password = userinfo.substring(index + 1); 214 } else { 215 user = userinfo; 216 } 217 principal = new BasicPrincipal(user, password); 218 } 219 return principal; 220 } 221 222 230 private static void fixPath(URI uri) throws URI.MalformedURIException { 231 String path = uri.getPath(); 232 if (path == null || path.equals("")) { 233 uri.setPath("/"); 234 } 235 } 236 } 237 | Popular Tags |