Revision 1783
Added by Matt Jones over 20 years ago
MetacatClient.java | ||
---|---|---|
25 | 25 |
package edu.ucsb.nceas.metacat.client; |
26 | 26 |
|
27 | 27 |
import java.io.InputStream; |
28 |
import java.io.InputStreamReader; |
|
29 |
import java.io.StringWriter; |
|
28 | 30 |
import java.io.Reader; |
29 | 31 |
import java.net.URL; |
30 | 32 |
import java.util.Properties; |
... | ... | |
63 | 65 |
public void login(String username, String password) |
64 | 66 |
throws MetacatAuthException, MetacatInaccessibleException |
65 | 67 |
{ |
68 |
Properties prop = new Properties(); |
|
69 |
prop.put("action", "login"); |
|
70 |
prop.put("qformat", "xml"); |
|
71 |
prop.put("username", username); |
|
72 |
prop.put("password", password); |
|
73 |
|
|
74 |
String response = null; |
|
75 |
try { |
|
76 |
response = sendDataForString(prop); |
|
77 |
} catch (Exception e) { |
|
78 |
throw new MetacatInaccessibleException(e.getMessage()); |
|
79 |
} |
|
80 |
|
|
81 |
if (response.indexOf("<login>") == -1) { |
|
82 |
HttpMessage.setCookie(null); |
|
83 |
throw new MetacatAuthException(response); |
|
84 |
} |
|
66 | 85 |
} |
67 | 86 |
|
68 | 87 |
/** |
... | ... | |
143 | 162 |
*/ |
144 | 163 |
public void setMetacatUrl(String metacatUrl) |
145 | 164 |
{ |
165 |
this.metacatUrl = metacatUrl; |
|
146 | 166 |
} |
147 | 167 |
|
148 | 168 |
/************************************************************************ |
149 | 169 |
* PRIVATE METHODS |
150 | 170 |
************************************************************************/ |
151 | 171 |
|
172 |
/** |
|
173 |
* Send a request to metacat. |
|
174 |
* |
|
175 |
* @param prop the properties to be URL encoded and sent |
|
176 |
*/ |
|
152 | 177 |
synchronized private InputStream sendDataOnce(Properties prop) |
153 | 178 |
throws Exception |
154 | 179 |
{ |
... | ... | |
194 | 219 |
} |
195 | 220 |
} |
196 | 221 |
} |
222 |
|
|
223 |
/** |
|
224 |
* Send a request to Metacat |
|
225 |
* |
|
226 |
* @param prop the properties to be sent to Metacat |
|
227 |
* @return a string as returned by Metacat |
|
228 |
*/ |
|
229 |
synchronized private String sendDataForString(Properties prop) |
|
230 |
throws Exception |
|
231 |
{ |
|
232 |
String response = null; |
|
233 |
|
|
234 |
try { |
|
235 |
InputStreamReader returnStream = |
|
236 |
new InputStreamReader(sendData(prop)); |
|
237 |
StringWriter sw = new StringWriter(); |
|
238 |
int len; |
|
239 |
char[] characters = new char[512]; |
|
240 |
while ((len = returnStream.read(characters, 0, 512)) != -1) { |
|
241 |
sw.write(characters, 0, len); |
|
242 |
} |
|
243 |
returnStream.close(); |
|
244 |
response = sw.toString(); |
|
245 |
sw.close(); |
|
246 |
} catch (Exception e) { |
|
247 |
throw e; |
|
248 |
} |
|
249 |
return response; |
|
250 |
} |
|
197 | 251 |
} |
Also available in: Unified diff
Wrote the login() function, the first of the series of API calls for
the metacat client to be implemented. Also wrote a test class to
test the API functionality.