193 |
193 |
* API Resources
|
194 |
194 |
*/
|
195 |
195 |
private static final String RESOURCE_OBJECTS = "object";
|
196 |
|
private static final String RESOURCE_META = "/meta";
|
|
196 |
private static final String RESOURCE_META = "meta";
|
197 |
197 |
private static final String RESOURCE_SESSION = "session";
|
198 |
198 |
private static final String RESOURCE_IDENTIFIER = "identifier";
|
199 |
199 |
|
... | ... | |
272 |
272 |
* @param httpVerb (GET, POST, PUT or DELETE)
|
273 |
273 |
*/
|
274 |
274 |
public void handle(byte httpVerb) {
|
275 |
|
|
276 |
275 |
logMetacat = Logger.getLogger(ResourceHandler.class);
|
277 |
276 |
try {
|
278 |
277 |
String resource = request.getServletPath();
|
279 |
|
|
|
278 |
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);
|
280 |
287 |
boolean status = false;
|
281 |
288 |
|
282 |
289 |
if (resource != null) {
|
... | ... | |
288 |
295 |
Timer timer = new Timer();
|
289 |
296 |
handler = new MetacatHandler(timer);
|
290 |
297 |
|
291 |
|
if (resource.equals(RESOURCE_SESSION) && httpVerb == POST
|
292 |
|
&& params.get(FUNCTION_KEYWORD) != null) {
|
293 |
|
if (params.get(FUNCTION_KEYWORD)[0]
|
294 |
|
.equals(FUNCTION_NAME_LOGIN)) {
|
295 |
|
login();
|
|
298 |
if (resource.equals(RESOURCE_SESSION) &&
|
|
299 |
httpVerb == POST &&
|
|
300 |
params.get(FUNCTION_KEYWORD) != null) {
|
|
301 |
|
|
302 |
if (params.get(FUNCTION_KEYWORD)[0]
|
|
303 |
.equals(FUNCTION_NAME_LOGIN)) {
|
|
304 |
login();
|
|
305 |
status = true;
|
|
306 |
} else if (params.get(FUNCTION_KEYWORD)[0]
|
|
307 |
.equals(FUNCTION_NAME_LOGOUT)) {
|
|
308 |
logout();
|
|
309 |
status = true;
|
|
310 |
}
|
|
311 |
} else if (resource.equals(RESOURCE_META)) {
|
|
312 |
loadSessionData();
|
|
313 |
String objectId = request.getPathInfo();
|
|
314 |
if (objectId != null && objectId.length() > 1)
|
|
315 |
{
|
|
316 |
objectId = request.getPathInfo().substring(1);
|
|
317 |
}
|
|
318 |
getSystemMetadataObject(objectId);
|
296 |
319 |
status = true;
|
297 |
|
} else if (params.get(FUNCTION_KEYWORD)[0]
|
298 |
|
.equals(FUNCTION_NAME_LOGOUT)) {
|
299 |
|
logout();
|
300 |
|
status = true;
|
301 |
|
}
|
302 |
|
} else if (resource.equals(RESOURCE_OBJECTS)) {
|
|
320 |
|
|
321 |
} else if (resource.equals(RESOURCE_OBJECTS)) {
|
303 |
322 |
logMetacat.debug("D1 Rest: Starting resource processing...");
|
304 |
323 |
loadSessionData();
|
305 |
324 |
|
306 |
|
boolean isSysmetaRequest = false;
|
307 |
325 |
String objectId = request.getPathInfo();
|
308 |
|
if (objectId != null && objectId.length() > 1) {
|
309 |
|
objectId = request.getPathInfo().substring(1); //trim the slash
|
310 |
|
|
311 |
|
// Check if this is a request for SystemMetadata
|
312 |
|
// TODO: Note that this REST uri means that GUIDs can not contain the RESOURCE_META string
|
313 |
|
int start = objectId.indexOf(RESOURCE_META);
|
314 |
|
if (start >= 0) {
|
315 |
|
logMetacat.debug("Pruning meta at start value: " + start);
|
316 |
|
objectId = objectId.substring(0, start);
|
317 |
|
logMetacat.debug("New objectId for meta is: " + objectId);
|
318 |
|
isSysmetaRequest = true;
|
319 |
|
}
|
|
326 |
if (objectId != null && objectId.length() > 1)
|
|
327 |
{
|
|
328 |
objectId = request.getPathInfo().substring(1);
|
320 |
329 |
}
|
321 |
|
logMetacat.debug("Processing objectId: " + objectId);
|
|
330 |
|
322 |
331 |
logMetacat.debug("verb:" + httpVerb);
|
323 |
332 |
|
324 |
|
logMetacat.debug("objectId:" + objectId);
|
325 |
|
|
326 |
333 |
if (httpVerb == GET) {
|
327 |
|
if (isSysmetaRequest) {
|
328 |
|
getSystemMetadataObject(objectId);
|
329 |
|
} else {
|
330 |
|
getObject(objectId);
|
331 |
|
}
|
|
334 |
getObject(objectId);
|
332 |
335 |
status = true;
|
333 |
336 |
} else if (httpVerb == POST) {
|
334 |
337 |
putObject(objectId, FUNCTION_NAME_INSERT);
|
... | ... | |
506 |
509 |
* @param guid ID of data object to be read
|
507 |
510 |
*/
|
508 |
511 |
private void getSystemMetadataObject(String guid) {
|
509 |
|
//CrudService cs = new CrudService(servletContext, request, response);
|
510 |
512 |
CrudService cs = CrudService.getInstance();
|
511 |
513 |
cs.setParamsFromRequest(request);
|
512 |
514 |
AuthToken token = null;
|
... | ... | |
516 |
518 |
Identifier id = new Identifier();
|
517 |
519 |
id.setValue(guid);
|
518 |
520 |
SystemMetadata sysmeta = cs.getSystemMetadata(token, id);
|
519 |
|
logMetacat.debug("Got sysmeta for: " + sysmeta.getIdentifier().getValue());
|
520 |
521 |
|
521 |
522 |
// Serialize and write it to the output stream
|
522 |
523 |
try {
|
... | ... | |
611 |
612 |
*/
|
612 |
613 |
private void putObject(String guid, String action) {
|
613 |
614 |
logMetacat.debug("Entering putObject: " + guid + "/" + action);
|
614 |
|
System.out.println("entering putObject. Action is " + action);
|
615 |
615 |
|
616 |
616 |
// TODO: This function lacks proper handling of authz and authn, so it
|
617 |
617 |
// seems that anyone can insert or update; interacts with
|
... | ... | |
635 |
635 |
logMetacat.debug("MMP created.");
|
636 |
636 |
mmp.writeTo(System.out);
|
637 |
637 |
for (int i = 0; i < mmp.getCount(); i++) {
|
638 |
|
logMetacat.debug("Looping over MMP parts: " + i);
|
639 |
638 |
BodyPart part = mmp.getBodyPart(i);
|
640 |
639 |
String name = part.getFileName();
|
641 |
640 |
logMetacat.debug("Part name is: " + name);
|
642 |
|
System.out.println("part name: " + name);
|
643 |
641 |
logMetacat.debug("Part has class name: " + part.getClass().getName());
|
644 |
642 |
if (name.equals("object")) {
|
645 |
643 |
object = part.getInputStream();
|
... | ... | |
663 |
661 |
// TODO: access control -- should be in CrudService et al. I think
|
664 |
662 |
//if (username != null && !username.equals("public")) {
|
665 |
663 |
if (username != null) {
|
666 |
|
|
667 |
664 |
logMetacat.debug("Commence creation...");
|
668 |
665 |
AuthToken token = null;
|
669 |
666 |
IBindingFactory bfact =
|
670 |
667 |
BindingDirectory.getFactory(SystemMetadata.class);
|
671 |
668 |
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
|
672 |
669 |
SystemMetadata m = (SystemMetadata) uctx.unmarshalDocument(sysmeta, null);
|
673 |
|
|
|
670 |
|
674 |
671 |
CrudService cs = CrudService.getInstance();
|
675 |
672 |
cs.setParamsFromRequest(request);
|
676 |
673 |
Identifier id = new Identifier();
|
... | ... | |
717 |
714 |
{
|
718 |
715 |
cs.setParamsFromRequest(request);
|
719 |
716 |
Identifier rId = cs.update(token, id, object, obsoletedGuid, m);
|
720 |
|
System.out.println("id returned from update " + rId.getValue());
|
721 |
717 |
}
|
722 |
718 |
catch(NotFound e)
|
723 |
719 |
{
|
added getSystemMetadata to ResourceHandler and changed the url format to be meta/guid. added a new servlet reponse handler in the web.xml file to handle the new urls and send them to the RestServlet.