Revision 566
Added by Matt Jones over 23 years ago
MetacatURL.java | ||
---|---|---|
22 | 22 |
{ |
23 | 23 |
private String[][] params = new String[200][2]; |
24 | 24 |
private Hashtable paramsHash = new Hashtable(); |
25 |
private String urlType = null;
|
|
25 |
private String protocol = null;
|
|
26 | 26 |
private String host = null; |
27 | 27 |
private String url; |
28 | 28 |
|
29 | 29 |
/** |
30 | 30 |
* This constructor takes a string url and parses it according to the |
31 | 31 |
* following rules. |
32 |
* 1) The name of the url is the text before the "://" symbol.
|
|
32 |
* 1) The protocol of the url is the text before the "://" symbol.
|
|
33 | 33 |
* 2) Parameter names are written first and are terminated with the = symbol |
34 | 34 |
* 3) Parameter values come 2nd and are terminated with an & except for the |
35 | 35 |
* last value |
36 | 36 |
* The form of the url looks like: |
37 |
* urltype://server.domain.com/servlet/?name1=value1&name2=value2&nameN=valueN
|
|
37 |
* protocol://server.domain.com/servlet/?name1=val1&name2=val2&nameN=valN
|
|
38 | 38 |
* notice there is no & after the last param. If one is there it is ignored. |
39 | 39 |
* |
40 | 40 |
* @param url the string to parse |
... | ... | |
48 | 48 |
/** |
49 | 49 |
* This method takes a string url and parses it according to the following |
50 | 50 |
* rules. |
51 |
* 1) The name of the url is the text before the "://" symbol.
|
|
51 |
* 1) The protocol of the url is the text before the "://" symbol.
|
|
52 | 52 |
* 2) Parameter names are written first and are terminated with the = symbol |
53 | 53 |
* 3) Parameter values come 2nd and are terminated with an & except for the |
54 | 54 |
* last value |
55 | 55 |
* The form of the url looks like: |
56 |
* urltype://server.domain.com/servlet/?name1=value1&name2=value2&nameN=valueN
|
|
56 |
* protocol://server.domain.com/servlet/?name1=val1&name2=val2&nameN=valN
|
|
57 | 57 |
* notice there is no & after the last param. If one is there it is ignored. |
58 | 58 |
*/ |
59 | 59 |
private void parseURL(String url) throws MalformedURLException |
... | ... | |
65 | 65 |
boolean poundflag = false; |
66 | 66 |
int arrcount = 0; |
67 | 67 |
|
68 |
int urlTypeIndex = url.indexOf("://"); //anything before this is the urltype |
|
69 |
this.urlType = url.substring(0, urlTypeIndex); |
|
70 |
paramsHash.put("urlType", this.urlType); |
|
68 |
//anything before this is the protocol |
|
69 |
int protocolIndex = url.indexOf("://"); |
|
71 | 70 |
|
72 |
if(this.urlType.equals("http")) |
|
71 |
if (protocolIndex == -1) { |
|
72 |
// URL badly formed, no protocol |
|
73 |
throw new MalformedURLException("Invalid URL format: " + |
|
74 |
"no protocol provided."); |
|
75 |
} |
|
76 |
this.protocol = url.substring(0, protocolIndex); |
|
77 |
paramsHash.put("protocol", this.protocol); |
|
78 |
|
|
79 |
if(this.protocol.equals("http")) |
|
73 | 80 |
{//if this is an http url |
74 | 81 |
params[0][0] = "httpurl"; |
75 | 82 |
params[0][1] = url.substring(0, url.length()); |
... | ... | |
95 | 102 |
else |
96 | 103 |
{//other urls that meet the metacat type url structure. |
97 | 104 |
int hostIndex = url.indexOf("?"); |
98 |
this.host = url.substring(urlTypeIndex + 3, hostIndex);
|
|
105 |
this.host = url.substring(protocolIndex + 3, hostIndex);
|
|
99 | 106 |
paramsHash.put("host", this.host); |
100 | 107 |
for(int i=hostIndex + 1; i<url.length(); i++) |
101 | 108 |
{ //go throught the remainder of the url one character at a time. |
... | ... | |
152 | 159 |
* Returns the type of the url. This is defined by the text before the "://" |
153 | 160 |
* symbol in the url. |
154 | 161 |
*/ |
155 |
public String getURLType()
|
|
162 |
public String getProtocol()
|
|
156 | 163 |
{ |
157 |
return this.urlType;
|
|
164 |
return this.protocol;
|
|
158 | 165 |
} |
159 | 166 |
|
160 | 167 |
/** |
... | ... | |
210 | 217 |
public void printParams() |
211 | 218 |
{ |
212 | 219 |
String[][] p = null; |
213 |
System.out.println("url type: " + this.getURLType());
|
|
220 |
System.out.println("protocol: " + this.getProtocol());
|
|
214 | 221 |
System.out.println("parameters: "); |
215 | 222 |
p = this.getParams(); |
216 | 223 |
System.out.println("name value"); |
... | ... | |
249 | 256 |
{ |
250 | 257 |
String testurl = "metacat://dev.nceas.ucsb.edu?docid=NCEAS:10&username=chad&pasword=xyz"; |
251 | 258 |
String testurl2 = "http://dev.nceas.ucsb.edu/berkley/testdata.dat"; |
259 |
String testurl3 = "NCEAS.1287873498.32"; |
|
252 | 260 |
try |
253 | 261 |
{ |
262 |
System.out.println("*********************************************"); |
|
254 | 263 |
MetacatURL murl = new MetacatURL(testurl); |
264 |
//String[][] p = null; |
|
265 |
System.out.println("protocol: " + murl.getProtocol()); |
|
266 |
System.out.println("parameters: "); |
|
267 |
//p = murl.getParams(); |
|
268 |
//Hashtable h = murl.getHashParams(); |
|
269 |
murl.printParams(); |
|
270 |
murl.printHashParams(); |
|
271 |
System.out.println("*********************************************"); |
|
272 |
|
|
255 | 273 |
MetacatURL murl2 = new MetacatURL(testurl2); |
256 |
String[][] p = null; |
|
257 |
System.out.println("url type: " + murl.getURLType()); |
|
274 |
System.out.println("protocol: " + murl2.getProtocol()); |
|
258 | 275 |
System.out.println("parameters: "); |
259 |
p = murl.getParams(); |
|
260 |
Hashtable h = murl.getHashParams(); |
|
261 |
murl.printParams(); |
|
262 | 276 |
murl2.printParams(); |
263 |
System.out.println("hash params: " ); |
|
264 |
murl.printHashParams(); |
|
265 | 277 |
murl2.printHashParams(); |
278 |
System.out.println("*********************************************"); |
|
279 |
|
|
280 |
MetacatURL murl3 = new MetacatURL(testurl3); |
|
281 |
System.out.println("protocol: " + murl3.getProtocol()); |
|
282 |
System.out.println("parameters: "); |
|
283 |
murl3.printParams(); |
|
284 |
System.out.println("*********************************************"); |
|
266 | 285 |
} |
267 | 286 |
catch(MalformedURLException murle) |
268 | 287 |
{ |
Also available in: Unified diff
Modified stylesheets and server code to use a single "read" action instead
of the earlier "getdocument" and "getrelateddocument" actions. In the
process, developed and started to utilize a new suite of URL "protocol
handlers" that are in the package "edu.ucsb.nceas.protocols" and handle
the parsing of various forms of URLS (mainly metacat:// protocol URLS). New
protocols can be added by following the model for the metacat protocol handler.
Using this mechanism, the standard URL handling methods like "getProtocol()"
can be used, and I added the "parseQuery()" method to MetaCatUtil for
parsing query parameters into name/value pairs and returning a hashtable.
I've eliminated the use of the MetacatURL class from the main Metacat servlet
classes, but not yet from the replication server classes (because I don't
understand the implications of doing so yet).