Revision 5291
Added by Matt Jones over 14 years ago
test/edu/ucsb/nceas/metacattest/restservice/MetacatRestClientTest.java | ||
---|---|---|
268 | 268 |
} |
269 | 269 |
|
270 | 270 |
/** |
271 |
* Test the get() function with a not existing document
|
|
271 |
* Test the get() function with a non existing document
|
|
272 | 272 |
*/ |
273 | 273 |
public void invalidGet() |
274 | 274 |
{ |
... | ... | |
366 | 366 |
m.login(username,password); |
367 | 367 |
|
368 | 368 |
debug("\nFirst insert the document..."); |
369 |
String response = m.putObject(docid + ".1",sr,true);
|
|
369 |
String response = m.create(docid + ".1",sr);
|
|
370 | 370 |
sr.close(); |
371 | 371 |
debug("response:\n"+response); |
372 | 372 |
assertTrue(response.indexOf("success") != -1); |
... | ... | |
375 | 375 |
|
376 | 376 |
sr = new StringReader(emldoc); |
377 | 377 |
debug("\nNow update the document..."); |
378 |
response = m.putObject(docid + ".2",sr,false);
|
|
378 |
response = m.update(docid + ".2",sr);
|
|
379 | 379 |
debug("response:\n"+response); |
380 | 380 |
assertTrue(response.indexOf("success") != -1); |
381 | 381 |
sr.close(); |
... | ... | |
540 | 540 |
String response; |
541 | 541 |
try { |
542 | 542 |
m.login(username,password); |
543 |
response = m.putObject(guid, sr, true);
|
|
543 |
response = m.create(guid, sr);
|
|
544 | 544 |
debug("response:\n"+response); |
545 | 545 |
assertTrue(response.indexOf("success") != -1); |
546 | 546 |
} catch (InsufficientKarmaException e) { |
src/edu/ucsb/nceas/metacat/client/rest/MetacatRestClient.java | ||
---|---|---|
247 | 247 |
} |
248 | 248 |
|
249 | 249 |
/** |
250 |
* Put (Insert/Update) an XML document into the repository. |
|
251 |
* |
|
252 |
* @param docid the docid to insert the document |
|
253 |
* @param xmlDocument a Reader for accessing the XML document to be inserted |
|
254 |
* @param isInsert whether the operation is update or insert |
|
255 |
* |
|
256 |
* |
|
257 |
* @return the metacat response message |
|
258 |
* @throws InsufficientKarmaException when the user has insufficent rights |
|
259 |
* for the operation |
|
260 |
* @throws MetacatInaccessibleException when the metacat server can not be |
|
261 |
* reached or does not respond |
|
262 |
* @throws MetacatException when the metacat server generates another error |
|
263 |
* @throws IOException when there is an error reading the xml document |
|
264 |
*/ |
|
265 |
public String putObject(String docid, Reader xmlDocument, boolean isInsert) |
|
266 |
throws InsufficientKarmaException, MetacatException, IOException, MetacatInaccessibleException{ |
|
267 |
String resource = RESOURCE_OBJECTS+"/"+docid; |
|
268 |
String urlParams = "sessionid="+sessionId+"&"; |
|
269 |
|
|
270 |
if (isInsert) |
|
271 |
urlParams += FUNCTION_KEYWORD+"="+FUNCTION_NAME_INSERT; |
|
272 |
else |
|
273 |
urlParams += FUNCTION_KEYWORD+"="+FUNCTION_NAME_UPDATE; |
|
274 |
|
|
275 |
String response = null; |
|
276 |
try{ |
|
277 |
response = sendData(resource, PUT, urlParams, null, "text/xml", xmlDocument, null); |
|
278 |
} catch (Exception e) { |
|
279 |
e.printStackTrace(); |
|
280 |
throw new MetacatInaccessibleException(e.getMessage()); |
|
281 |
} |
|
282 |
|
|
283 |
// Check for an error condition |
|
284 |
if (response.indexOf("<error>") != -1) { |
|
285 |
if (response.indexOf("does not have permission") != -1) { |
|
286 |
throw new InsufficientKarmaException(response); |
|
287 |
} else { |
|
288 |
throw new MetacatException(response); |
|
289 |
} |
|
290 |
} |
|
291 |
return response; |
|
292 |
} |
|
293 |
|
|
250 |
* Create an XML document in the repository. |
|
251 |
* |
|
252 |
* @param docid the docid to insert the document |
|
253 |
* @param xmlDocument a Reader for accessing the XML document to be inserted |
|
254 |
* @param isInsert whether the operation is update or insert |
|
255 |
* |
|
256 |
* |
|
257 |
* @return the metacat response message |
|
258 |
* @throws InsufficientKarmaException when the user has insufficent rights |
|
259 |
* for the operation |
|
260 |
* @throws MetacatInaccessibleException when the metacat server can not be |
|
261 |
* reached or does not respond |
|
262 |
* @throws MetacatException when the metacat server generates another error |
|
263 |
* @throws IOException when there is an error reading the xml document |
|
264 |
*/ |
|
265 |
public String create(String docid, Reader xmlDocument) |
|
266 |
throws InsufficientKarmaException, MetacatException, IOException, MetacatInaccessibleException { |
|
267 |
return putObject(docid, xmlDocument, true); |
|
268 |
} |
|
269 |
|
|
270 |
/** |
|
271 |
* Update an XML document in the repository, replacing an existing document. |
|
272 |
* |
|
273 |
* @param docid the docid to insert the document |
|
274 |
* @param xmlDocument a Reader for accessing the XML document to be inserted |
|
275 |
* @param isInsert whether the operation is update or insert |
|
276 |
* |
|
277 |
* |
|
278 |
* @return the metacat response message |
|
279 |
* @throws InsufficientKarmaException when the user has insufficent rights |
|
280 |
* for the operation |
|
281 |
* @throws MetacatInaccessibleException when the metacat server can not be |
|
282 |
* reached or does not respond |
|
283 |
* @throws MetacatException when the metacat server generates another error |
|
284 |
* @throws IOException when there is an error reading the xml document |
|
285 |
*/ |
|
286 |
public String update(String docid, Reader xmlDocument) |
|
287 |
throws InsufficientKarmaException, MetacatException, IOException, MetacatInaccessibleException { |
|
288 |
return putObject(docid, xmlDocument, false); |
|
289 |
} |
|
290 |
|
|
294 | 291 |
/** |
295 | 292 |
* Delete an XML document in the repository. |
296 | 293 |
* |
... | ... | |
506 | 503 |
} |
507 | 504 |
|
508 | 505 |
/** |
506 |
* Put (Insert/Update) an XML document into the repository. |
|
507 |
* |
|
508 |
* @param docid the docid to insert the document |
|
509 |
* @param xmlDocument a Reader for accessing the XML document to be inserted |
|
510 |
* @param isInsert whether the operation is update or insert |
|
511 |
* |
|
512 |
* @return the metacat response message |
|
513 |
* @throws InsufficientKarmaException when the user has insufficent rights |
|
514 |
* for the operation |
|
515 |
* @throws MetacatInaccessibleException when the metacat server can not be |
|
516 |
* reached or does not respond |
|
517 |
* @throws MetacatException when the metacat server generates another error |
|
518 |
* @throws IOException when there is an error reading the xml document |
|
519 |
*/ |
|
520 |
private String putObject(String docid, Reader xmlDocument, boolean isInsert) |
|
521 |
throws InsufficientKarmaException, MetacatException, IOException, MetacatInaccessibleException { |
|
522 |
String resource = RESOURCE_OBJECTS+"/"+docid; |
|
523 |
String urlParams = "sessionid="+sessionId+"&"; |
|
524 |
|
|
525 |
if (isInsert) |
|
526 |
urlParams += FUNCTION_KEYWORD+"="+FUNCTION_NAME_INSERT; |
|
527 |
else |
|
528 |
urlParams += FUNCTION_KEYWORD+"="+FUNCTION_NAME_UPDATE; |
|
529 |
|
|
530 |
String response = null; |
|
531 |
try{ |
|
532 |
response = sendData(resource, PUT, urlParams, null, "text/xml", xmlDocument, null); |
|
533 |
} catch (Exception e) { |
|
534 |
e.printStackTrace(); |
|
535 |
throw new MetacatInaccessibleException(e.getMessage()); |
|
536 |
} |
|
537 |
|
|
538 |
// Check for an error condition |
|
539 |
if (response.indexOf("<error>") != -1) { |
|
540 |
if (response.indexOf("does not have permission") != -1) { |
|
541 |
throw new InsufficientKarmaException(response); |
|
542 |
} else { |
|
543 |
throw new MetacatException(response); |
|
544 |
} |
|
545 |
} |
|
546 |
return response; |
|
547 |
} |
|
548 |
|
|
549 |
/** |
|
509 | 550 |
* Send request to metacat REST API. |
510 | 551 |
* @param resource Resource name to be accessed |
511 | 552 |
* @param method HTTP verb, shoudl be one of GET,POST,DELETE,PUT |
src/edu/ucsb/nceas/metacat/client/rest/MetacatRest.java | ||
---|---|---|
148 | 148 |
public Reader authenticatedQuery(Reader xmlQuery) throws MetacatInaccessibleException,IOException; |
149 | 149 |
|
150 | 150 |
/** |
151 |
* Insert / Update an XML document into the repository, making it available for
|
|
151 |
* Create an XML document into the repository, making it available for
|
|
152 | 152 |
* searching using the query() methods. |
153 | 153 |
* |
154 | 154 |
* @param docid the docid to insert the document |
155 | 155 |
* @param xmlDocument a Reader for accessing the XML document to be inserted |
156 |
* @param isInsert whether the operation is update or insert |
|
157 | 156 |
* |
158 | 157 |
* @return the metacat response message |
159 | 158 |
* @throws InsufficientKarmaException when the user has insufficent rights |
... | ... | |
163 | 162 |
* @throws MetacatException when the metacat server generates another error |
164 | 163 |
* @throws IOException when there is an error reading the xml document |
165 | 164 |
*/ |
166 |
public String putObject(String docid, Reader xmlDocument,boolean isInsert)
|
|
165 |
public String create(String docid, Reader xmlDocument)
|
|
167 | 166 |
throws InsufficientKarmaException, MetacatException, IOException, |
168 | 167 |
MetacatInaccessibleException; |
169 | 168 |
|
170 |
|
|
169 |
/** |
|
170 |
* Update an XML document into the repository, making it available for |
|
171 |
* searching using the query() methods. |
|
172 |
* |
|
173 |
* @param docid the docid to insert the document |
|
174 |
* @param xmlDocument a Reader for accessing the XML document to be inserted |
|
175 |
* |
|
176 |
* @return the metacat response message |
|
177 |
* @throws InsufficientKarmaException when the user has insufficent rights |
|
178 |
* for the operation |
|
179 |
* @throws MetacatInaccessibleException when the metacat server can not be |
|
180 |
* reached or does not respond |
|
181 |
* @throws MetacatException when the metacat server generates another error |
|
182 |
* @throws IOException when there is an error reading the xml document |
|
183 |
*/ |
|
184 |
public String update(String docid, Reader xmlDocument) |
|
185 |
throws InsufficientKarmaException, MetacatException, IOException, |
|
186 |
MetacatInaccessibleException; |
|
187 |
|
|
171 | 188 |
|
172 | 189 |
/** |
173 | 190 |
* Delete an XML document in the repository. |
Also available in: Unified diff
Refactored putObject method to separate the create() and update() portions in order to match the method signatures needed for DataONE.