1 16 package org.apache.commons.vfs.provider; 17 18 import org.apache.commons.vfs.FileName; 19 import org.apache.commons.vfs.FileSystemException; 20 import org.apache.commons.vfs.FileType; 21 22 30 public class HostFileNameParser extends AbstractFileNameParser 31 { 32 private final int defaultPort; 33 34 public HostFileNameParser(final int defaultPort) 35 { 36 this.defaultPort = defaultPort; 37 } 38 39 public int getDefaultPort() 40 { 41 return defaultPort; 42 } 43 44 public boolean encodeCharacter(char ch) 45 { 46 return super.encodeCharacter(ch); 47 } 48 49 public FileName parseUri(final VfsComponentContext context, FileName base, final String filename) throws FileSystemException 50 { 51 final StringBuffer name = new StringBuffer (); 53 54 final Authority auth = extractToPath(filename, name); 56 57 UriParser.canonicalizePath(name, 0, name.length(), this); 59 UriParser.fixSeparators(name); 60 FileType fileType = UriParser.normalisePath(name); 61 final String path = name.toString(); 62 63 return new GenericFileName( 64 auth.scheme, 65 auth.hostName, 66 auth.port, 67 defaultPort, 68 auth.userName, 69 auth.password, 70 path, 71 fileType); 72 } 73 74 81 protected Authority extractToPath(final String uri, 82 final StringBuffer name) 83 throws FileSystemException 84 { 85 final Authority auth = new Authority(); 86 87 auth.scheme = UriParser.extractScheme(uri, name); 89 90 if (name.length() < 2 || name.charAt(0) != '/' || name.charAt(1) != '/') 92 { 93 throw new FileSystemException("vfs.provider/missing-double-slashes.error", uri); 94 } 95 name.delete(0, 2); 96 97 final String userInfo = extractUserInfo(name); 99 final String userName; 100 final String password; 101 if (userInfo != null) 102 { 103 int idx = userInfo.indexOf(':'); 104 if (idx == -1) 105 { 106 userName = userInfo; 107 password = ""; 108 } 109 else 110 { 111 userName = userInfo.substring(0, idx); 112 password = userInfo.substring(idx + 1); 113 } 114 } 115 else 116 { 117 userName = ""; 118 password = ""; 119 } 120 auth.userName = UriParser.decode(userName); 121 auth.password = UriParser.decode(password); 122 123 final String hostName = extractHostName(name); 125 if (hostName == null) 126 { 127 throw new FileSystemException("vfs.provider/missing-hostname.error", uri); 128 } 129 auth.hostName = hostName; 131 auth.port = extractPort(name, uri); 133 134 if (name.length() > 0 && name.charAt(0) != '/') 136 { 137 throw new FileSystemException("vfs.provider/missing-hostname-path-sep.error", uri); 138 } 139 140 return auth; 141 } 142 143 147 protected String extractUserInfo(final StringBuffer name) 148 { 149 final int maxlen = name.length(); 150 for (int pos = 0; pos < maxlen; pos++) 151 { 152 final char ch = name.charAt(pos); 153 if (ch == '@') 154 { 155 String userInfo = name.substring(0, pos); 157 name.delete(0, pos + 1); 158 return userInfo; 159 } 160 if (ch == '/' || ch == '?') 161 { 162 break; 164 } 165 } 166 167 return null; 169 } 170 171 175 protected String extractHostName(final StringBuffer name) 176 { 177 final int maxlen = name.length(); 178 int pos = 0; 179 for (; pos < maxlen; pos++) 180 { 181 final char ch = name.charAt(pos); 182 if (ch == '/' || ch == ';' || ch == '?' || ch == ':' 183 || ch == '@' || ch == '&' || ch == '=' || ch == '+' 184 || ch == '$' || ch == ',') 185 { 186 break; 187 } 188 } 189 if (pos == 0) 190 { 191 return null; 192 } 193 194 final String hostname = name.substring(0, pos); 195 name.delete(0, pos); 196 return hostname; 197 } 198 199 205 protected int extractPort(final StringBuffer name, final String uri) throws FileSystemException 206 { 207 if (name.length() < 1 || name.charAt(0) != ':') 208 { 209 return -1; 210 } 211 212 final int maxlen = name.length(); 213 int pos = 1; 214 for (; pos < maxlen; pos++) 215 { 216 final char ch = name.charAt(pos); 217 if (ch < '0' || ch > '9') 218 { 219 break; 220 } 221 } 222 223 final String port = name.substring(1, pos); 224 name.delete(0, pos); 225 if (port.length() == 0) 226 { 227 throw new FileSystemException("vfs.provider/missing-port.error", uri); 228 } 229 230 return Integer.parseInt(port); 231 } 232 233 236 protected static class Authority 237 { 238 public String scheme; 239 public String hostName; 240 public String userName; 241 public String password; 242 public int port; 243 } 244 } 245 | Popular Tags |