Project

General

Profile

« Previous | Next » 

Revision 5332

Added initial implementation of getSystemMetadata and its associated REST service. Current implementation is returning a hardcoded system metadata document -- need to look up the real document for each guid and return that.

View differences:

src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java
22 22
 */
23 23
package edu.ucsb.nceas.metacat.restservice;
24 24

  
25
import java.io.ByteArrayOutputStream;
25 26
import java.io.IOException;
26 27
import java.io.InputStream;
27 28
import java.io.OutputStream;
......
54 55
import org.dataone.service.types.SystemMetadata;
55 56
import org.jibx.runtime.BindingDirectory;
56 57
import org.jibx.runtime.IBindingFactory;
58
import org.jibx.runtime.IMarshallingContext;
57 59
import org.jibx.runtime.IUnmarshallingContext;
58 60
import org.jibx.runtime.JiBXException;
59 61

  
......
190 192
     * API Resources
191 193
     */
192 194
    private static final String RESOURCE_OBJECTS = "object";
195
    private static final String RESOURCE_META = "/meta";
193 196
    private static final String RESOURCE_SESSION = "session";
194 197
    private static final String RESOURCE_IDENTIFIER = "identifier";
195 198

  
......
299 302
                    logMetacat.debug("D1 Rest: Starting resource processing...");
300 303
                    loadSessionData();
301 304

  
305
                    boolean isSysmetaRequest = false;
302 306
                    String objectId = request.getPathInfo();
303
                    if (objectId != null && objectId.length() > 1)
307
                    if (objectId != null && objectId.length() > 1) {
304 308
                        objectId = request.getPathInfo().substring(1); //trim the slash
309
                        
310
                        // Check if this is a request for SystemMetadata
311
                        // TODO: Note that this REST uri means that GUIDs can not contain the RESOURCE_META string
312
                        int start = objectId.indexOf(RESOURCE_META);
313
                        if (start >= 0) {
314
                            logMetacat.debug("Pruning meta at start value: " + start);
315
                            objectId = objectId.substring(0, start);
316
                            logMetacat.debug("New objectId for meta is: " + objectId);
317
                            isSysmetaRequest = true;
318
                        }
319
                    }
305 320
                    logMetacat.debug("Processing objectId: " + objectId);
306 321
                    logMetacat.debug("verb:" + httpVerb);
307 322

  
308 323
                    logMetacat.debug("objectId:" + objectId);
309 324

  
310 325
                    if (httpVerb == GET) {
311
                        getObject(objectId);
326
                        if (isSysmetaRequest) {
327
                            getSystemMetadataObject(objectId);
328
                        } else {
329
                            getObject(objectId);
330
                        }
312 331
                        status = true;
313 332
                    } else if (httpVerb == POST) {
314 333
                        putObject(objectId, FUNCTION_NAME_INSERT);
......
482 501
    }
483 502

  
484 503
    /**
504
     * Implements REST version of DataONE CRUD API --> getSystemMetadata
505
     * @param guid ID of data object to be read
506
     */
507
    private void getSystemMetadataObject(String guid) {
508
        CrudService cs = new CrudService(servletContext, request, response);
509
        AuthToken token = null;
510
        OutputStream out = null;
511
        try {
512
            out = response.getOutputStream();
513
            Identifier id = new Identifier();
514
            id.setValue(guid);
515
            SystemMetadata sysmeta = cs.getSystemMetadata(token, id);
516
            logMetacat.debug("Got sysmeta for: " + sysmeta.getIdentifier().getValue());
517
            
518
            // Serialize and write it to the output stream
519
            try {
520
                IBindingFactory bfact = BindingDirectory.getFactory(SystemMetadata.class);
521
                IMarshallingContext mctx = bfact.createMarshallingContext();
522
                mctx.marshalDocument(sysmeta, "UTF-8", null, out);
523
            } catch (JiBXException e) {
524
                throw new ServiceFailure(1190, "Failed to serialize SystemMetadata: " + e.getMessage());
525
            }
526
        } catch (BaseException e) {
527
                serializeException(e, out);
528
        } catch (IOException e) {
529
            ServiceFailure sf = new ServiceFailure(1030, e.getMessage());
530
            serializeException(sf, out);
531
        } finally {
532
            IOUtils.closeQuietly(out);
533
        }
534
    }
535
    
536
    /**
485 537
     * Earthgrid API > Query Service > Query Function : translates ecogrid query document to metacat query 
486 538
     * then calls DBQuery > createResultDocument function and then again translate resultset to ecogrid resultset
487 539
     * 
src/edu/ucsb/nceas/metacat/dataone/CrudService.java
62 62
import org.jibx.runtime.BindingDirectory;
63 63
import org.jibx.runtime.IBindingFactory;
64 64
import org.jibx.runtime.IMarshallingContext;
65
import org.jibx.runtime.IUnmarshallingContext;
65 66
import org.jibx.runtime.JiBXException;
66 67

  
67 68
import com.gc.iotools.stream.is.InputStreamFromOutputStream;
......
271 272
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
272 273
            InvalidRequest, NotImplemented {
273 274
        
274
        // TODO: Look up ID of system metadata based on guid
275
        logMetacat.debug("CrudService.getSystemMetadata - for guid: " + guid.getValue());
276
        
277
        // Retrieve the session information from the AuthToken
278
        // If the session is expired, then the user is 'public'
279
        final SessionData sessionData = getSessionData(token);
280

  
281
        // TODO: Check access control rules
282
                
283
        try {
284
            IdentifierManager im = IdentifierManager.getInstance();
285
            // This is a test id of an existing sysmeta document -- for temporary testing
286
            String testGuid = "autogen.20101192441566.1";
287
            // TODO: Look up ID of system metadata based on guid
275 288
            // TODO: Initially from document, later from entry in table?
276
        
277
        // TODO: Read system metadata from disk and create SystemMetadata object
278
            // TODO: Follows same implementation plan as get()
279
        
280
        // TODO: return it
281
        throw new NotImplemented(1000, "This method not yet implemented.");
289
//            String localId = im.getLocalId(guid.getValue());
290
            final String localId = im.getLocalId(testGuid);
291
            
292
            // Read system metadata from metacat's db
293
            final InputStreamFromOutputStream<String> objectStream = 
294
                new InputStreamFromOutputStream<String>() {
295
                
296
                @Override
297
                public String produce(final OutputStream dataSink) throws Exception {
298
                    try {
299
                        handler.readFromMetacat(request.getRemoteAddr(), null, 
300
                                dataSink, localId, "xml",
301
                                sessionData.getUserName(), 
302
                                sessionData.getGroupNames(), true, params);
303
                    } catch (PropertyNotFoundException e) {
304
                        throw new ServiceFailure(1030, e.getMessage());
305
                    } catch (ClassNotFoundException e) {
306
                        throw new ServiceFailure(1030, e.getMessage());
307
                    } catch (IOException e) {
308
                        throw new ServiceFailure(1030, e.getMessage());
309
                    } catch (SQLException e) {
310
                        throw new ServiceFailure(1030, e.getMessage());
311
                    } catch (McdbException e) {
312
                        throw new ServiceFailure(1030, e.getMessage());
313
                    } catch (ParseLSIDException e) {
314
                        throw new NotFound(1020, e.getMessage());
315
                    } catch (InsufficientKarmaException e) {
316
                        throw new NotAuthorized(1000, "Not authorized for get().");
317
                    }
318

  
319
                    return "Completed";
320
                }
321
            };
322
            
323
            // Deserialize the xml to create a SystemMetadata object
324
            SystemMetadata sysmeta = deserializeSystemMetadata(objectStream);
325
            return sysmeta;
326
            
327
        } catch (McdbDocNotFoundException e) {
328
            throw new NotFound(1000, e.getMessage());
329
        }                
282 330
    }
283 331

  
284 332
    public Identifier update(AuthToken token, Identifier guid, 
......
483 531
        handler.handleInsertOrUpdateAction(request.getRemoteAddr(), response, 
484 532
                pw, params, sessionData.getUserName(),
485 533
                sessionData.getGroupNames());
486
        logMetacat.debug(new String(output.toByteArray()));
534
        String outputS = new String(output.toByteArray());
535
        logMetacat.debug("CrudService.insertDocument - Metacat returned: " + outputS);
536
//        if (!(outputS.indexOf("<success>") > 0 && outputS.indexOf(localId) > 0)) {
537
//            throw new ServiceFailure(1190, outputS);
538
//        }
487 539
        logMetacat.debug("Finsished inserting xml document.");
488 540
    }
489 541
    
490
    private ByteArrayOutputStream serializeSystemMetadata(SystemMetadata sysmeta) 
542
    public static ByteArrayOutputStream serializeSystemMetadata(SystemMetadata sysmeta) 
491 543
        throws ServiceFailure {
492 544
        IBindingFactory bfact;
493 545
        ByteArrayOutputStream sysmetaOut = null;
......
502 554
        
503 555
        return sysmetaOut;
504 556
    }
557
    
558
    public static SystemMetadata deserializeSystemMetadata(InputStream xml) 
559
        throws ServiceFailure {
560
        try {
561
            IBindingFactory bfact = BindingDirectory.getFactory(SystemMetadata.class);
562
            IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
563
            SystemMetadata sysmeta = (SystemMetadata) uctx.unmarshalDocument(xml, null);
564
            return sysmeta;
565
        } catch (JiBXException e) {
566
            throw new ServiceFailure(1190, "Failed to serialize and insert SystemMetadata: " + e.getMessage());
567
        }    
568
    }
505 569
}

Also available in: Unified diff