Revision 5370
Added by berkley almost 14 years ago
ResourceHandler.java | ||
---|---|---|
22 | 22 |
*/ |
23 | 23 |
package edu.ucsb.nceas.metacat.restservice; |
24 | 24 |
|
25 |
import java.io.ByteArrayOutputStream; |
|
26 |
import java.io.IOException; |
|
27 |
import java.io.InputStream; |
|
28 |
import java.io.OutputStream; |
|
29 |
import java.io.PrintWriter; |
|
30 |
import java.util.Enumeration; |
|
31 |
import java.util.Hashtable; |
|
32 |
import java.util.Timer; |
|
25 |
import java.io.*; |
|
26 |
import java.util.*; |
|
33 | 27 |
|
34 | 28 |
import javax.mail.BodyPart; |
35 | 29 |
import javax.mail.MessagingException; |
... | ... | |
37 | 31 |
import javax.servlet.ServletContext; |
38 | 32 |
import javax.servlet.http.HttpServletRequest; |
39 | 33 |
import javax.servlet.http.HttpServletResponse; |
34 |
import java.text.DateFormat; |
|
40 | 35 |
|
41 | 36 |
import org.apache.commons.io.IOUtils; |
42 | 37 |
import org.apache.log4j.Logger; |
... | ... | |
51 | 46 |
import org.dataone.service.exceptions.ServiceFailure; |
52 | 47 |
import org.dataone.service.exceptions.UnsupportedType; |
53 | 48 |
import org.dataone.service.exceptions.NotFound; |
54 |
import org.dataone.service.types.AuthToken; |
|
55 |
import org.dataone.service.types.Identifier; |
|
56 |
import org.dataone.service.types.SystemMetadata; |
|
49 |
import org.dataone.service.types.*; |
|
57 | 50 |
import org.jibx.runtime.BindingDirectory; |
58 | 51 |
import org.jibx.runtime.IBindingFactory; |
59 | 52 |
import org.jibx.runtime.IMarshallingContext; |
... | ... | |
203 | 196 |
private static final String FUNCTION_KEYWORD = "op"; |
204 | 197 |
private static final String FUNCTION_NAME_LOGIN = "login"; |
205 | 198 |
private static final String FUNCTION_NAME_LOGOUT = "logout"; |
199 |
private static final String FUNCTION_NAME_SET_ACCESS = "setaccess"; |
|
206 | 200 |
private static final String FUNCTION_NAME_ISREGISTERED = "isregistered"; |
207 | 201 |
private static final String FUNCTION_NAME_GETALLDOCS = "getalldocids"; |
208 | 202 |
private static final String FUNCTION_NAME_GETNEXTREV = "getnextrevision"; |
... | ... | |
241 | 235 |
while (paramlist.hasMoreElements()) { |
242 | 236 |
name = (String) paramlist.nextElement(); |
243 | 237 |
value = request.getParameterValues(name); |
244 |
System.out.println("adding param: " + name + " = " + value); |
|
238 |
//System.out.println("adding param: " + name + " = " + value);
|
|
245 | 239 |
params.put(name, value); |
246 | 240 |
} |
247 | 241 |
|
... | ... | |
260 | 254 |
password = sessionData.getPassword(); |
261 | 255 |
groupNames = sessionData.getGroupNames(); |
262 | 256 |
sessionId = sessionData.getId(); |
263 |
|
|
257 |
//System.out.println("setting sessionid to " + sessionId); |
|
258 |
|
|
264 | 259 |
if (username == null) { |
265 | 260 |
username = "public"; |
266 | 261 |
} |
... | ... | |
276 | 271 |
try { |
277 | 272 |
String resource = request.getServletPath(); |
278 | 273 |
String verb = ""; |
279 |
switch (httpVerb) |
|
280 |
{ |
|
281 |
case GET: verb = "GET"; |
|
282 |
case POST: verb = "POST"; |
|
283 |
case PUT: verb = "PUT"; |
|
284 |
case DELETE: verb = "DELETE"; |
|
285 |
} |
|
286 |
System.out.println("handling " + verb + " request with resource " + resource); |
|
274 |
|
|
275 |
//System.out.println("handling verb " + httpVerb + " request with resource " + resource); |
|
287 | 276 |
boolean status = false; |
288 | 277 |
|
289 | 278 |
if (resource != null) { |
... | ... | |
298 | 287 |
if (resource.equals(RESOURCE_SESSION) && |
299 | 288 |
httpVerb == POST && |
300 | 289 |
params.get(FUNCTION_KEYWORD) != null) { |
301 |
|
|
302 |
if (params.get(FUNCTION_KEYWORD)[0]
|
|
303 |
.equals(FUNCTION_NAME_LOGIN)) {
|
|
290 |
//System.out.println("function_keyword: " + params.get(FUNCTION_KEYWORD)[0]); |
|
291 |
//System.out.println("function set access: " + FUNCTION_NAME_SET_ACCESS);
|
|
292 |
if (params.get(FUNCTION_KEYWORD)[0].equals(FUNCTION_NAME_LOGIN)) {
|
|
304 | 293 |
login(); |
305 | 294 |
status = true; |
306 |
} else if (params.get(FUNCTION_KEYWORD)[0] |
|
307 |
.equals(FUNCTION_NAME_LOGOUT)) { |
|
295 |
} else if (params.get(FUNCTION_KEYWORD)[0].equals(FUNCTION_NAME_LOGOUT)) { |
|
308 | 296 |
logout(); |
309 | 297 |
status = true; |
298 |
} else if (params.get(FUNCTION_KEYWORD)[0].equals(FUNCTION_NAME_SET_ACCESS)) { |
|
299 |
//System.out.println("setting access in resourceHandler"); |
|
300 |
setaccess(); |
|
301 |
status = true; |
|
302 |
//System.out.println("done setting access"); |
|
310 | 303 |
} |
311 | 304 |
} else if (resource.equals(RESOURCE_META)) { |
312 | 305 |
loadSessionData(); |
... | ... | |
327 | 320 |
{ |
328 | 321 |
objectId = request.getPathInfo().substring(1); |
329 | 322 |
} |
323 |
else |
|
324 |
{ |
|
325 |
objectId = null; |
|
326 |
} |
|
330 | 327 |
|
331 | 328 |
logMetacat.debug("verb:" + httpVerb); |
332 | 329 |
|
... | ... | |
364 | 361 |
} else if (op.equals(FUNCTION_NAME_GETNEXTOBJ)) { |
365 | 362 |
getNextObject(); |
366 | 363 |
status = true; |
367 |
} |
|
364 |
}
|
|
368 | 365 |
|
369 | 366 |
} else if (httpVerb == PUT) { |
370 | 367 |
//Earthgrid API > Identifier Service > addLSID Function |
... | ... | |
488 | 485 |
//CrudService cs = new CrudService(servletContext, request, response); |
489 | 486 |
CrudService cs = CrudService.getInstance(); |
490 | 487 |
cs.setParamsFromRequest(request); |
491 |
AuthToken token = null; |
|
488 |
//System.out.println("sessionId: " + sessionId); |
|
489 |
AuthToken token = new AuthToken(sessionId); |
|
492 | 490 |
OutputStream out = null; |
493 | 491 |
try { |
494 | 492 |
out = response.getOutputStream(); |
495 |
Identifier id = new Identifier(); |
|
496 |
id.setValue(guid); |
|
497 |
InputStream data = cs.get(token, id); |
|
498 |
IOUtils.copyLarge(data, response.getOutputStream()); |
|
493 |
if(guid != null) |
|
494 |
{ //get a specific document |
|
495 |
Identifier id = new Identifier(); |
|
496 |
id.setValue(guid); |
|
497 |
InputStream data = cs.get(token, id); |
|
498 |
IOUtils.copyLarge(data, response.getOutputStream()); |
|
499 |
} |
|
500 |
else |
|
501 |
{ //call listObjects with specified params |
|
502 |
Date startTime = null; |
|
503 |
Date endTime = null; |
|
504 |
ObjectFormat objectFormat = null; |
|
505 |
boolean replicaStatus = false; |
|
506 |
int start = 0; |
|
507 |
int count = 1000; |
|
508 |
Enumeration paramlist = request.getParameterNames(); |
|
509 |
while (paramlist.hasMoreElements()) |
|
510 |
{ //parse the params and make the crud call |
|
511 |
String name = (String) paramlist.nextElement(); |
|
512 |
String[] value = (String[])request.getParameterValues(name); |
|
513 |
if(name.equals("startTime")) |
|
514 |
{ |
|
515 |
try |
|
516 |
{ |
|
517 |
startTime = DateFormat.getDateTimeInstance().parse(value[0]); |
|
518 |
} |
|
519 |
catch(Exception e) |
|
520 |
{ //if we can't parse it, just don't use the startTime param |
|
521 |
System.out.println("Could not parse startTime: " + value[0]); |
|
522 |
startTime = null; |
|
523 |
} |
|
524 |
} |
|
525 |
else if(name.equals("endTime")) |
|
526 |
{ |
|
527 |
try |
|
528 |
{ |
|
529 |
endTime = DateFormat.getDateTimeInstance().parse(value[0]); |
|
530 |
} |
|
531 |
catch(Exception e) |
|
532 |
{ //if we can't parse it, just don't use the endTime param |
|
533 |
System.out.println("Could not parse endTime: " + value[0]); |
|
534 |
endTime = null; |
|
535 |
} |
|
536 |
} |
|
537 |
else if(name.equals("objectFormat")) |
|
538 |
{ |
|
539 |
objectFormat = ObjectFormat.convert(value[0]); |
|
540 |
} |
|
541 |
else if(name.equals("replicaStatus")) |
|
542 |
{ |
|
543 |
if(value[0].equals("true") || value[0].equals("TRUE") || value[0].equals("YES")) |
|
544 |
{ |
|
545 |
replicaStatus = true; |
|
546 |
} |
|
547 |
} |
|
548 |
else if(name.equals("start")) |
|
549 |
{ |
|
550 |
start = new Integer(value[0]).intValue(); |
|
551 |
} |
|
552 |
else if(name.equals("count")) |
|
553 |
{ |
|
554 |
count = new Integer(value[0]).intValue(); |
|
555 |
} |
|
556 |
} |
|
557 |
//make the crud call |
|
558 |
/*System.out.println("token: " + token + " startTime: " + startTime + |
|
559 |
" endtime: " + endTime + " objectFormat: " + |
|
560 |
objectFormat + " replicaStatus: " + replicaStatus + |
|
561 |
" start: " + start + " count: " + count); |
|
562 |
*/ |
|
563 |
ObjectList ol = cs.listObjects(token, startTime, endTime, |
|
564 |
objectFormat, replicaStatus, start, count); |
|
565 |
ol = cs.listObjects(token, null, null, null, false, 0, 1000); |
|
566 |
|
|
567 |
//StringReader sr = new StringReader(ol.toString()); |
|
568 |
//IOUtils.copy(sr, response.getOutputStream()); |
|
569 |
|
|
570 |
out = response.getOutputStream(); |
|
571 |
// Serialize and write it to the output stream |
|
572 |
try { |
|
573 |
IBindingFactory bfact = BindingDirectory.getFactory(ObjectList.class); |
|
574 |
IMarshallingContext mctx = bfact.createMarshallingContext(); |
|
575 |
mctx.marshalDocument(ol, "UTF-8", null, out); |
|
576 |
} catch (JiBXException e) { |
|
577 |
throw new ServiceFailure("1190", "Failed to serialize ObjectList: " + e.getMessage()); |
|
578 |
} |
|
579 |
} |
|
499 | 580 |
} catch (BaseException e) { |
500 | 581 |
serializeException(e, out); |
501 | 582 |
} catch (IOException e) { |
... | ... | |
777 | 858 |
groupNames); |
778 | 859 |
out.close(); |
779 | 860 |
} |
861 |
|
|
862 |
/** |
|
863 |
* set the access perms on a document |
|
864 |
* @throws IOException |
|
865 |
*/ |
|
866 |
private void setaccess() throws IOException |
|
867 |
{ |
|
868 |
try |
|
869 |
{ |
|
870 |
String guid = params.get("guid")[0]; |
|
871 |
Identifier id = new Identifier(); |
|
872 |
id.setValue(guid); |
|
873 |
AuthToken token = new AuthToken(sessionId); |
|
874 |
String principal = params.get("principal")[0]; |
|
875 |
String permission = params.get("permission")[0]; |
|
876 |
String permissionType = params.get("permissionType")[0]; |
|
877 |
String permissionOrder = params.get("permissionOrder")[0]; |
|
878 |
String setSystemMetadata = params.get("setsystemmetadata")[0]; |
|
879 |
CrudService cs = CrudService.getInstance(); |
|
880 |
cs.setAccess(token, id, principal, permission, permissionType, permissionOrder); |
|
881 |
if(setSystemMetadata.equals("true") || setSystemMetadata.equals("TRUE") || |
|
882 |
setSystemMetadata.equals("yes")) |
|
883 |
{ //set the same perms on the system metadata doc |
|
884 |
IdentifierManager im = IdentifierManager.getInstance(); |
|
885 |
String smidS = im.getSystemMetadataId(id.getValue()); |
|
886 |
Identifier smid = new Identifier(); |
|
887 |
smid.setValue(smidS); |
|
888 |
cs.setAccess(token, smid, "public", "read", "allow", "allowFirst"); |
|
889 |
} |
|
890 |
} |
|
891 |
catch(Exception e) |
|
892 |
{ |
|
893 |
printError("Error setting access in ResourceHandler: " + e.getMessage(), response); |
|
894 |
} |
|
895 |
} |
|
780 | 896 |
|
781 | 897 |
/** |
782 | 898 |
* Earthgrid API > Authentication Service > Login Function : calls MetacatHandler > handleLoginAction |
... | ... | |
818 | 934 |
e.printStackTrace(); |
819 | 935 |
} |
820 | 936 |
} |
821 |
|
|
937 |
|
|
822 | 938 |
private void serializeException(BaseException e, OutputStream out) { |
823 | 939 |
// TODO: Use content negotiation to determine which return format to use |
824 | 940 |
response.setContentType("text/xml"); |
Also available in: Unified diff
listObjects is now working for rest calls as well as crud calls