Project

General

Profile

« Previous | Next » 

Revision 5390

Added by berkley over 14 years ago

adding getlogrecords to the rest interface.

View differences:

test/edu/ucsb/nceas/metacat/dataone/CrudServiceTest.java
156 156
	        Date fromDate = new Date();
157 157
	        Identifier id = createDoc(token, getTestDoc());
158 158
	        Date toDate = new Date();
159
	        Log lrs = cs.getLogRecords(token, fromDate, toDate);
159
	        Log lrs = cs.getLogRecords(token, fromDate, toDate, null);
160 160
	        assertNotNull(lrs);
161 161
	        assertTrue(lrs.sizeLogEntryList() == 1);
162 162
	        LogEntry lrLogEvent = lrs.getLogEntry(0);
src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java
33 33
import javax.servlet.http.HttpServletResponse;
34 34
import java.text.DateFormat;
35 35

  
36
import org.apache.commons.httpclient.util.DateParser;
36 37
import org.apache.commons.io.IOUtils;
37 38
import org.apache.log4j.Logger;
38 39
import org.dataone.service.exceptions.BaseException;
......
190 191
    private static final String RESOURCE_META = "meta";
191 192
    private static final String RESOURCE_SESSION = "session";
192 193
    private static final String RESOURCE_IDENTIFIER = "identifier";
194
    private static final String RESOURCE_LOG = "log";
193 195

  
194 196
    /*
195 197
     * API Functions used as URL parameters
......
349 351
                        status = true;
350 352
                    }
351 353

  
354
                } else if (resource.equals(RESOURCE_LOG)) {
355
                    //handle log events
356
                    if(httpVerb == GET)
357
                    {
358
                        getLog();
359
                        status = true;
360
                    }
361
                    else
362
                    {
363
                        printError("POST, PUT, DELETE is not supported for logs.", response);
364
                        status = true;
365
                    }
366
                    
352 367
                }
368
                
353 369
                if (!status)
354 370
                    printError("Incorrect parameters!", response);
355 371
            } else {
......
362 378
    }
363 379
    
364 380
    /**
381
     * get the logs from the CrudService based on passed params.  Available 
382
     * params are token, fromDate, toDate, event.  See 
383
     * http://mule1.dataone.org/ArchitectureDocs/mn_api_crud.html#MN_crud.getLogRecords
384
     * for more info
385
     */
386
    private void getLog()
387
    {
388
        OutputStream out = null;
389
        try
390
        {
391
            out = response.getOutputStream();
392
            AuthToken token = new AuthToken(sessionId);
393
            String fromDateS = params.get("fromDate")[0];
394
            Date fromDate = null;
395
            String toDateS = params.get("toDate")[0];
396
            Date toDate = null;
397
            String eventS = params.get("event")[0];
398
            Event event = null;
399
            if(fromDateS != null)
400
            {
401
                fromDate = DateParser.parseDate(fromDateS);
402
            }
403
            if(toDateS != null)
404
            {
405
                toDate = DateParser.parseDate(toDateS);
406
            }
407
            if(eventS != null)
408
            {
409
                event = Event.convert(eventS);
410
            }
411

  
412
            Log log = CrudService.getInstance().getLogRecords(token, fromDate, toDate, event);
413
            serializeServiceType(Log.class, log, out);
414
        }
415
        catch(Exception e)
416
        {
417
            String msg = "Could not get logs from CrudService: " + e.getMessage();
418
            ServiceFailure sf = new ServiceFailure("1490", msg);
419
            logMetacat.error(msg);
420
            e.printStackTrace();
421
            serializeException(sf, out);
422
        }
423
    }
424
    
425
    /**
365 426
     *  copies request parameters to a hashtable which is given as argument to native metacathandler functions  
366 427
     */
367 428
    private void initParams() {
......
625 686
                out = response.getOutputStream();                
626 687
                // Serialize and write it to the output stream
627 688
                try {
628
                    IBindingFactory bfact = BindingDirectory.getFactory(ObjectList.class);
629
                    IMarshallingContext mctx = bfact.createMarshallingContext();
630
                    mctx.marshalDocument(ol, "UTF-8", null, out);
689
                    serializeServiceType(ObjectList.class, ol, out);
631 690
                } catch (JiBXException e) {
632 691
                    throw new ServiceFailure("1190", "Failed to serialize ObjectList: " + e.getMessage());
633 692
                }
......
657 716
            
658 717
            // Serialize and write it to the output stream
659 718
            try {
660
                IBindingFactory bfact = BindingDirectory.getFactory(SystemMetadata.class);
661
                IMarshallingContext mctx = bfact.createMarshallingContext();
662
                mctx.marshalDocument(sysmeta, "UTF-8", null, out);
719
                serializeServiceType(SystemMetadata.class, sysmeta, out);
663 720
            } catch (JiBXException e) {
664 721
                throw new ServiceFailure("1190", "Failed to serialize SystemMetadata: " + e.getMessage());
665 722
            }
......
674 731
    }
675 732
    
676 733
    /**
734
     * serialize an object of type to out
735
     * @param type the class of the object to serialize (i.e. SystemMetadata.class)
736
     * @param object the object to serialize
737
     * @param out the stream to serialize it to
738
     * @throws JiBXException
739
     */
740
    private void serializeServiceType(Class type, Object object, OutputStream out)
741
      throws JiBXException
742
    {
743
        IBindingFactory bfact = BindingDirectory.getFactory(type);
744
        IMarshallingContext mctx = bfact.createMarshallingContext();
745
        mctx.marshalDocument(object, "UTF-8", null, out);
746
    }
747
    
748
    /**
749
     * deserialize an object of type from is
750
     * @param type the class of the object to serialize (i.e. SystemMetadata.class)
751
     * @param is the stream to deserialize from
752
     * @throws JiBXException
753
     */
754
    private Object deserializeServiceType(Class type, InputStream is)
755
      throws JiBXException
756
    {
757
        IBindingFactory bfact = BindingDirectory.getFactory(type);
758
        IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
759
        Object o = (Object) uctx.unmarshalDocument(is, null);
760
        return o;
761
    }
762
    
763
    /**
677 764
     * Earthgrid API > Query Service > Query Function : translates ecogrid query document to metacat query 
678 765
     * then calls DBQuery > createResultDocument function and then again translate resultset to ecogrid resultset
679 766
     * 
src/edu/ucsb/nceas/metacat/dataone/CrudService.java
657 657
    /**
658 658
     * get log records.  
659 659
     */
660
    public Log getLogRecords(AuthToken token, Date fromDate, Date toDate)
660
    public Log getLogRecords(AuthToken token, Date fromDate, Date toDate, Event event)
661 661
            throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, 
662 662
            NotImplemented 
663 663
    {
......
686 686
            String ipAddress = getLogEntryField("ipAddress", entry);
687 687
            String principal = getLogEntryField("principal", entry);
688 688
            String docid = getLogEntryField("docid", entry);
689
            String event = getLogEntryField("event", entry);
689
            String eventS = getLogEntryField("event", entry);
690 690
            String dateLogged = getLogEntryField("dateLogged", entry);
691 691
            
692 692
            LogEntry le = new LogEntry();
693 693
            
694
            Event e = Event.convert(event);
694
            Event e = Event.convert(eventS);
695 695
            if(e == null)
696 696
            { //skip any events that are not Dataone Crud events
697 697
                continue;
......
727 727
            princ.setValue(principal);
728 728
            le.setPrincipal(princ);
729 729
            le.setUserAgent("metacat/RESTService");
730
            logs.add(le);
730
            
731
            if(event == null)
732
            {
733
                logs.add(le);
734
            }
735
            
736
            if(event != null &&
737
               e.toString().toLowerCase().trim().equals(event.toString().toLowerCase().trim()))
738
            {
739
              logs.add(le);
740
            }
731 741
        }
732 742
        
733 743
        log.setLogEntryList(logs);

Also available in: Unified diff