Project

General

Profile

Revision 5319

Modifications to support the DataONE service API version 0.1.0. For DataONE, the get() and
create() services are partially complete. Several more functions and checks need to be added to
create() before it is viable. This DataONE support is not complete, and the current support breaks the MetacatRestClientTest for the time being (this client will eventually be removed).

View differences:

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

  
25 25
import java.io.BufferedReader;
26
import java.io.File;
27
import java.io.FileNotFoundException;
28
import java.io.FileOutputStream;
26 29
import java.io.IOException;
27 30
import java.io.InputStream;
31
import java.io.OutputStream;
28 32
import java.io.PrintWriter;
29 33
import java.util.Enumeration;
30 34
import java.util.Hashtable;
31 35
import java.util.Timer;
32 36

  
37
import javax.mail.BodyPart;
38
import javax.mail.MessagingException;
39
import javax.mail.internet.MimeMultipart;
33 40
import javax.servlet.ServletContext;
34 41
import javax.servlet.http.HttpServletRequest;
35 42
import javax.servlet.http.HttpServletResponse;
......
37 44
import org.apache.commons.io.IOUtils;
38 45
import org.apache.log4j.Logger;
39 46
import org.dataone.service.exceptions.BaseException;
47
import org.dataone.service.exceptions.IdentifierNotUnique;
48
import org.dataone.service.exceptions.InsufficientResources;
49
import org.dataone.service.exceptions.InvalidRequest;
50
import org.dataone.service.exceptions.InvalidSystemMetadata;
51
import org.dataone.service.exceptions.InvalidToken;
52
import org.dataone.service.exceptions.NotAuthorized;
53
import org.dataone.service.exceptions.NotImplemented;
40 54
import org.dataone.service.exceptions.ServiceFailure;
55
import org.dataone.service.exceptions.UnsupportedType;
41 56
import org.dataone.service.types.AuthToken;
42 57
import org.dataone.service.types.IdentifierType;
58
import org.dataone.service.types.SystemMetadata;
59
import org.exolab.castor.jdo.conf.DataSource;
43 60

  
44 61
import edu.ucsb.nceas.metacat.DBUtil;
45 62
import edu.ucsb.nceas.metacat.IdentifierManager;
......
280 297
                        status = true;
281 298
                    }
282 299
                } else if (resource.equals(RESOURCE_OBJECTS)) {
283

  
300
                    logMetacat.debug("D1 Rest: Starting resource processing...");
284 301
                    loadSessionData();
285 302

  
286 303
                    String objectId = request.getPathInfo();
287 304
                    if (objectId != null && objectId.length() > 1)
288 305
                        objectId = request.getPathInfo().substring(1); //trim the slash
306
                    logMetacat.debug("Processing objectId: " + objectId);
307
                    logMetacat.debug("verb:" + httpVerb);
289 308

  
290
                    System.out.println("verb:" + httpVerb);
309
                    logMetacat.debug("objectId:" + objectId);
291 310

  
292
                    System.out.println("objectId:" + objectId);
293

  
294 311
                    if (httpVerb == GET) {
295 312
                        getObject(objectId);
296 313
                        status = true;
297 314
                    } else if (httpVerb == POST) {
298
                        query();
315
                        putObject(objectId, FUNCTION_NAME_INSERT);
299 316
                        status = true;
300 317
                    } else if (httpVerb == PUT) {
301
                        putObject(objectId);
318
                        putObject(objectId, FUNCTION_NAME_UPDATE);
302 319
                        status = true;
303 320
                    } else if (httpVerb == DELETE) {
304 321
                        deleteObject(objectId);
......
450 467
    private void getObject(String guid) {
451 468
        CrudService cs = new CrudService(servletContext, request, response);
452 469
        AuthToken token = null;
470
        OutputStream out = null;
453 471
        try {
472
            out = response.getOutputStream();
454 473
            InputStream data = cs.get(token, new IdentifierType(guid));
455 474
            IOUtils.copyLarge(data, response.getOutputStream());
456 475
        } catch (BaseException e) {
457
                response.setContentType("text/xml");
458
                response.setStatus(e.getCode());
459
                // TODO: Use content negotiation to determine which return format to use
460
                try {
461
                    IOUtils.write(e.serialize(BaseException.FMT_XML), 
462
                            response.getOutputStream());
463
                } catch (IOException e1) {
464
                    logMetacat.error("Error writing exception to stream. " 
465
                            + e1.getMessage());
466
                }
476
                serializeException(e, out);
467 477
        } catch (IOException e) {
468 478
            ServiceFailure sf = new ServiceFailure(1030, e.getMessage());
469
            response.setContentType("text/xml");
470
            response.setStatus(sf.getCode());
471
            try {
472
                IOUtils.write(sf.serialize(BaseException.FMT_XML), 
473
                        response.getOutputStream());
474
            } catch (IOException e1) {
475
                logMetacat.error("Error writing service exception to stream. " 
476
                        + e1.getMessage());
477
            }
479
            serializeException(sf, out);
478 480
        }
479 481
    }
480 482

  
......
544 546
        out.print("<error>Query operation not yet supported by Metacat.</error>");
545 547
        out.close();
546 548
    }
547

  
549
    
548 550
    /**
549 551
     * Earthgrid API > Put Service >Put Function : calls MetacatHandler > handleInsertOrUpdateAction 
550 552
     * 
551
     * @param objectId ID of data object to be inserted or updated
553
     * @param guid ID of data object to be inserted or updated
552 554
     * @throws IOException
553 555
     */
554
    private void putObject(String objectId) throws IOException {
555

  
556
    private void putObject(String guid, String action) {
557
        logMetacat.debug("Entering putObject: " + guid + "/" + action);
558
        
556 559
        // TODO: This function lacks proper handling of authz and authn, so it
557 560
        // seems that anyone can insert or update; interacts with 
558
        // loadSessinData(), which doesn't validate the session
561
        // loadSessionData(), which doesn't validate the session
559 562
        
560
        String action = request.getParameter(FUNCTION_KEYWORD);
563
        // Get an output stream for handling errors; this should really be passed in as
564
        // a parameter
565
        OutputStream out = null;
566
        try {
567
            out = response.getOutputStream();
568
        } catch (IOException e1) {
569
            logMetacat.error("Could not get the output stream for writing in putObject");
570
        }
571
        try {
572
            if (action.equals(FUNCTION_NAME_UPDATE)
573
                    || action.equals(FUNCTION_NAME_INSERT)) {
561 574

  
562
        if (action.equals(FUNCTION_NAME_UPDATE)
563
                || action.equals(FUNCTION_NAME_INSERT)) {
564
            
565
            // Check if the objectId exists
566
            IdentifierManager im = IdentifierManager.getInstance();
567
            if (im.identifierExists(objectId)) {
568
                // TODO: return IdentifierNotUnique exception
569
            }
570
            
571
            // TODO: For updates, need to check if the old id exists, and if not throw an exception
572
            
573
            // WARNING: This should not be a Reader if we are inserting data
574
            // Nor should it be read into a String
575
            // so this seems like a latent bug to me (MBJ; 16Mar2010)
576
            BufferedReader reader = request.getReader();
577
            StringBuffer buffer = new StringBuffer();
578
            String line = null;
579
            while ((line = reader.readLine()) != null) {
580
                buffer.append(line);
581
            }
575
                // Check if the objectId exists
576
                IdentifierManager im = IdentifierManager.getInstance();
577
                if (im.identifierExists(guid)) {
578
                    throw new IdentifierNotUnique(1000, "Identifier is already in use: " + guid);
579
                }
582 580

  
583
            String localId = im.generateLocalId(objectId, 1);
584
            params.put("docid", new String[] { localId });
585
            params.put("doctext", new String[] { buffer.toString().trim() });
586
            params.put("action", new String[] { action });
581
                // TODO: For updates, need to check if the old id exists, and if not throw an exception
587 582

  
588
            if (username != null && !username.equals("public")) {
589
                PrintWriter out = response.getWriter();
590
                handler.handleInsertOrUpdateAction(request, response, out,
591
                        params, username, groupNames);
592
                out.close();
583
                // TODO: Change this to read the MIME forms that are created
584
                logMetacat.debug("Disassembling MIME multipart form");
585
                InputStream object = null;
586
                InputStream sysmeta = null;
587
                MimeMultipart mmp = new MimeMultipart(new InputStreamDataSource("message", request.getInputStream()));
588
                logMetacat.debug("MMP created.");
589
                mmp.writeTo(System.out);
590
                for (int i = 0; i < mmp.getCount(); i++) {
591
                    logMetacat.debug("Looping over MMP parts: " + i);
592
                    BodyPart part = mmp.getBodyPart(i);
593
                    String name = part.getFileName();
594
                    logMetacat.debug("Part name is: " + name);
595
                    logMetacat.debug("Part has class name: " + part.getClass().getName());
596
                    if (name.equals("object")) {
597
                        object = part.getInputStream();
598
                        logMetacat.debug("Found object part, size is: " + part.getSize());
599
                    } else if (name.equals("systemmetadata")) {
600
                        sysmeta = part.getInputStream();
601
                        logMetacat.debug("Found sysmeta part, size is: " + part.getSize());
602
                    } else {
603
                        throw new InvalidRequest(1000, "Request had malformed MIME part with name: " + name);
604
                    }
605
                }
606

  
607
                //if (username != null && !username.equals("public")) {
608
                if (username != null) {
609

  
610
                    logMetacat.debug("Commence creation...");
611
                    AuthToken token = null;
612
                    SystemMetadata m = new SystemMetadata(sysmeta);
613
                    logMetacat.debug(m.serialize(SystemMetadata.FMT_XML));
614
                    //IOUtils.copy(object, System.out);
615
//                    byte[] b = new byte[2048];
616
//                    int len = object.read(b);
617
//                    logMetacat.debug("Read " + len + " bytes from object: " + new String(b));
618
                    CrudService cs = new CrudService(servletContext, request, response);
619
                    cs.create(token, new IdentifierType(guid), object, m);
620

  
621
                } else {
622
                    logMetacat.debug("Unauthorized to create.");
623
                    throw new NotAuthorized(1000, "Permission denied for user " + username);
624
                }
593 625
            } else {
594
                // TODO: throw exception to show lack of credentials
595
                printError("Permission denied for user " + username, response);
596

  
626
                throw new InvalidRequest(1000, "Operation must be create or update.");
597 627
            }
598
        } else {
599
            // TODO: throw the proper exception to indicate an invalid request
600
            printError("Specifiy the operation type.(update or insert)",
601
                    response);
628
        } catch (NotAuthorized e) {
629
            serializeException(e, out);
630
        } catch (InvalidToken e) {
631
            serializeException(e, out);
632
        } catch (ServiceFailure e) {
633
            serializeException(e, out);
634
        } catch (IdentifierNotUnique e) {
635
            serializeException(e, out);
636
        } catch (UnsupportedType e) {
637
            serializeException(e, out);
638
        } catch (InsufficientResources e) {
639
            serializeException(e, out);
640
        } catch (InvalidSystemMetadata e) {
641
            serializeException(e, out);
642
        } catch (NotImplemented e) {
643
            serializeException(e, out);
644
        } catch (InvalidRequest e) {
645
            serializeException(e, out);
646
        } catch (MessagingException e) {
647
            ServiceFailure sf = new ServiceFailure(1000, e.getMessage());
648
            serializeException(sf, out);
649
        } catch (IOException e) {
650
            ServiceFailure sf = new ServiceFailure(1000, e.getMessage());
651
            serializeException(sf, out);
602 652
        }
603

  
604 653
    }
605 654

  
606 655
    /**
......
668 717
        }
669 718
    }
670 719

  
720
    private void serializeException(BaseException e, OutputStream out) {
721
        // TODO: Use content negotiation to determine which return format to use
722
        response.setContentType("text/xml");
723
        response.setStatus(e.getCode());
724
        try {
725
            IOUtils.write(e.serialize(BaseException.FMT_XML), out);
726
        } catch (IOException e1) {
727
            logMetacat.error("Error writing exception to stream. " 
728
                    + e1.getMessage());
729
        }
730
    }
731

  
671 732
}
src/edu/ucsb/nceas/metacat/restservice/InputStreamDataSource.java
1
/**
2
 *  Copyright: 2010 Regents of the University of California and the
3
 *              National Center for Ecological Analysis and Synthesis
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package edu.ucsb.nceas.metacat.restservice;
20

  
21
import java.io.IOException;
22
import java.io.InputStream;
23
import java.io.OutputStream;
24

  
25
import javax.activation.DataSource;
26

  
27
/**
28
 * Encapsulate an InputStream within a DataSource interface so that it is 
29
 * accessible to MIME processors.
30
 * 
31
 * @author Matthew Jones
32
 */
33
public class InputStreamDataSource implements DataSource {
34
    private String name;
35
    private InputStream stream;
36
    private boolean readOnce;
37
    
38
    public InputStreamDataSource(String name, InputStream stream) {
39
        super();
40
        this.name = name;
41
        this.stream = stream;
42
        this.readOnce = false;
43
    }
44

  
45
    public String getContentType() {
46
        return "application/octet-stream";
47
    }
48

  
49
    public InputStream getInputStream() throws IOException {
50
        if (readOnce) {
51
            throw new IOException("Only call getInputStream() once.");
52
        }
53
        readOnce = true;
54
        
55
        return stream;
56
    }
57

  
58
    public String getName() {
59
        return this.name;
60
    }
61

  
62
    public OutputStream getOutputStream() throws IOException {
63
        throw new IOException("Can't get an OutputStream from an InputStreamDataSource.");
64
    }
65
}
0 66

  
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
874 874
			} else if (action.equals("insert") || action.equals("update")) {
875 875
				PrintWriter out = response.getWriter();
876 876
				if ((userName != null) && !userName.equals("public")) {
877
					handler.handleInsertOrUpdateAction(request, response, out, params, userName,
877
					handler.handleInsertOrUpdateAction(request.getRemoteAddr(), response, out, params, userName,
878 878
							groupNames);
879 879
				} else {
880 880
					response.setContentType("text/xml");
src/edu/ucsb/nceas/metacat/IndexingTimerTask.java
92 92
	    		}
93 93
	    		
94 94
	    		String xmlDocumentsCheck = 
95
	    			DatabaseService.getDBAdapter().getLeftJoinQuery("a.docid, a.rev", "xml_documents", 
95
	    			DatabaseService.getInstance().getDBAdapter().getLeftJoinQuery("a.docid, a.rev", "xml_documents", 
96 96
	    					"xml_index", "a.docid = b.docid", nonJoinCriteria);
97 97

  
98 98
	    		PreparedStatement xmlDocCheck = dbConn.prepareStatement(xmlDocumentsCheck);
src/edu/ucsb/nceas/metacat/DocumentImpl.java
485 485
      
486 486
      PreparedStatement pstmt = null;
487 487
      int revision = (new Integer(rev)).intValue();
488
      String sqlDateString = DatabaseService.getDBAdapter().getDateTimeFunction();
488
      String sqlDateString = DatabaseService.getInstance().getDBAdapter().getDateTimeFunction();
489 489
      if (createDate == null)
490 490
      {
491 491
          createDate = sqlDateString;
492 492
      }
493 493
      else
494 494
      {
495
          createDate = DatabaseService.getDBAdapter().toDate(createDate, "YYYY-MM-DD HH24:MI:SS");
495
          createDate = DatabaseService.getInstance().getDBAdapter().toDate(createDate, "YYYY-MM-DD HH24:MI:SS");
496 496
      }
497 497
      
498 498
      if (updateDate == null)
......
501 501
      }
502 502
      else
503 503
      {
504
          updateDate = DatabaseService.getDBAdapter().toDate(updateDate, "YYYY-MM-DD HH24:MI:SS");
504
          updateDate = DatabaseService.getInstance().getDBAdapter().toDate(updateDate, "YYYY-MM-DD HH24:MI:SS");
505 505
      }
506 506
      try
507 507
      {
......
549 549
    }
550 550
    catch(Exception e) 
551 551
    {
552
     
552
        logMetacat.debug("Caught a general exception: " + e.getMessage());
553 553
        throw e;
554 554
    }
555 555
    finally 
......
2273 2273
            String catalogid, int serverCode, String createDate, String updateDate) throws SQLException, Exception
2274 2274
    {
2275 2275
        //System.out.println("!!!!!!!!1write document to db  " +docid +"."+rev);
2276
        String sysdate = DatabaseService.getDBAdapter().getDateTimeFunction();
2276
        String sysdate = DatabaseService.getInstance().getDBAdapter().getDateTimeFunction();
2277 2277
        if (createDate == null)
2278 2278
        {
2279 2279
            createDate = sysdate;
2280 2280
        }
2281 2281
        else
2282 2282
        {
2283
            createDate = DatabaseService.getDBAdapter().toDate(createDate, "YYYY-MM-DD HH24:MI:SS");
2283
            createDate = DatabaseService.getInstance().getDBAdapter().toDate(createDate, "YYYY-MM-DD HH24:MI:SS");
2284 2284
        }
2285 2285
        if (updateDate == null)
2286 2286
        {
......
2288 2288
        }
2289 2289
        else
2290 2290
        {
2291
            updateDate = DatabaseService.getDBAdapter().toDate(updateDate, "YYYY-MM-DD HH24:MI:SS");
2291
            updateDate = DatabaseService.getInstance().getDBAdapter().toDate(updateDate, "YYYY-MM-DD HH24:MI:SS");
2292 2292
        }
2293 2293
        DocumentImpl thisdoc = null;
2294 2294

  
......
3551 3551
    private static void archiveDocAndNodesRevison(DBConnection dbconn, String docid, 
3552 3552
                                     String user, long rootNodeId) throws Exception
3553 3553
    {
3554
        String sysdate = DatabaseService.getDBAdapter().getDateTimeFunction();
3554
        String sysdate = DatabaseService.getInstance().getDBAdapter().getDateTimeFunction();
3555 3555
        //DBConnection conn = null;
3556 3556
        //int serialNumber = -1;
3557 3557
        PreparedStatement pstmt = null;
......
3637 3637
    /** Save a document entry in the xml_revisions table */
3638 3638
    private static void archiveDocRevision(String docid, String user, DBConnection conn) throws Exception
3639 3639
    {
3640
        String sysdate = DatabaseService.getDBAdapter().getDateTimeFunction();
3640
        String sysdate = DatabaseService.getInstance().getDBAdapter().getDateTimeFunction();
3641 3641
        PreparedStatement pstmt = null;
3642 3642

  
3643 3643
        // create a record in xml_revisions table
......
3882 3882
                        .prepareStatement("INSERT INTO xml_replication "
3883 3883
                                + "(server, last_checked, replicate, datareplicate, hub) "
3884 3884
                                + "VALUES ('" + server + "', "
3885
                                + DatabaseService.getDBAdapter().toDate("01/01/1980", "MM/DD/YYYY")
3885
                                + DatabaseService.getInstance().getDBAdapter().toDate("01/01/1980", "MM/DD/YYYY")
3886 3886
                                + ", '" + replicate + "', '" + dataReplicate
3887 3887
                                + "','" + hub + "')");
3888 3888

  
......
4078 4078
            
4079 4079
            if (createDate == null)
4080 4080
            {
4081
                createDate = DatabaseService.getDBAdapter().getDateTimeFunction();
4081
                createDate = DatabaseService.getInstance().getDBAdapter().getDateTimeFunction();
4082 4082
            }
4083 4083
            else
4084 4084
            {
4085
                createDate = DatabaseService.getDBAdapter().toDate(createDate, "YYYY-MM-DD HH24:MI:SS");
4085
                createDate = DatabaseService.getInstance().getDBAdapter().toDate(createDate, "YYYY-MM-DD HH24:MI:SS");
4086 4086
            }
4087 4087
            logMetacat.debug("DocumentImpl.writeDocumentToRevisionTable - the create date is "+createDate);
4088 4088
            if (updateDate == null)
4089 4089
            {
4090
                updateDate = DatabaseService.getDBAdapter().getDateTimeFunction();
4090
                updateDate = DatabaseService.getInstance().getDBAdapter().getDateTimeFunction();
4091 4091
            }
4092 4092
            else
4093 4093
            {
4094
                updateDate = DatabaseService.getDBAdapter().toDate(updateDate, "YYYY-MM-DD HH24:MI:SS");
4094
                updateDate = DatabaseService.getInstance().getDBAdapter().toDate(updateDate, "YYYY-MM-DD HH24:MI:SS");
4095 4095
            }
4096 4096
            logMetacat.debug("DocumentImpl.writeDocumentToRevisionTable - the update date is "+updateDate);
4097 4097
            PreparedStatement pstmt = null;
src/edu/ucsb/nceas/metacat/DBSAXNode.java
241 241
      pstmt.close();
242 242

  
243 243
      // get the generated unique id afterward
244
      nid = DatabaseService.getDBAdapter().getUniqueID(connection.getConnections(), "xml_nodes");
244
      nid = DatabaseService.getInstance().getDBAdapter().getUniqueID(connection.getConnections(), "xml_nodes");
245 245
      //should increase connection usage!!!!!!
246 246

  
247 247

  
......
370 370
      pstmt.close();
371 371

  
372 372
      // get the generated unique id afterward
373
      nid = DatabaseService.getDBAdapter().getUniqueID(connection.getConnections(), "xml_nodes");
373
      nid = DatabaseService.getInstance().getDBAdapter().getUniqueID(connection.getConnections(), "xml_nodes");
374 374
      //should incease connection usage!!!!!!
375 375

  
376 376
    } catch (SQLException e) {
src/edu/ucsb/nceas/metacat/MetacatHandler.java
1285 1285
     * Handle the database putdocument request and write an XML document to the
1286 1286
     * database connection
1287 1287
     */
1288
    public void handleInsertOrUpdateAction(HttpServletRequest request,
1288
    public void handleInsertOrUpdateAction(String ipAddress,
1289 1289
            HttpServletResponse response, PrintWriter out, Hashtable<String, String[]> params,
1290 1290
            String user, String[] groups) {
1291 1291
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
......
1421 1421
                        logMetacat.warn("MetaCatServlet.handleInsertOrUpdateAction - writing with null acnumber");
1422 1422
                        newdocid = documentWrapper.write(dbConn, doctext[0], pub, dtd,
1423 1423
                                doAction, null, user, groups);
1424
                        EventLog.getInstance().log(request.getRemoteAddr(),
1425
                                user, "", action[0]);
1426
                        } else {
1427
                            newdocid = documentWrapper.write(dbConn, doctext[0], pub, dtd,
1428
                                    doAction, accNumber, user, groups);
1429
                            
1430
                            EventLog.getInstance().log(request.getRemoteAddr(),
1431
                                    user, accNumber, action[0]);
1432
                        }
1424
                        EventLog.getInstance().log(ipAddress, user, "", action[0]);
1425
                    } else {
1426
                        newdocid = documentWrapper.write(dbConn, doctext[0], pub, dtd,
1427
                                doAction, accNumber, user, groups);
1428

  
1429
                        EventLog.getInstance().log(ipAddress, user, accNumber, action[0]);
1430
                    }
1433 1431
                } finally {
1434 1432
                    // Return db connection
1435 1433
                    DBConnectionPool.returnDBConnection(dbConn, serialNumber);
......
2345 2343
                doctextArr[0] = doctext;
2346 2344
                params.put("doctext", doctextArr);
2347 2345
                //call the insert routine
2348
                handleInsertOrUpdateAction(request, response, out, 
2346
                handleInsertOrUpdateAction(request.getRemoteAddr(), response, out, 
2349 2347
                          params, username, groupnames);
2350 2348
              }
2351 2349
              catch(Exception e)
......
2373 2371
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2374 2372
        //PrintWriter out = null;
2375 2373
        //Connection conn = null;
2376
//        String action = null;
2374
        //        String action = null;
2377 2375
        String docid = null;
2378 2376
        String qformat = null;
2379 2377
        String output = "";
2380
        
2378

  
2381 2379
        /*
2382 2380
         * response.setContentType("text/xml"); try { out =
2383 2381
         * response.getWriter(); } catch (IOException ioe2) {
2384 2382
         * System.err.println("Fatal Error: couldn't get response output
2385 2383
         * stream.");
2386 2384
         */
2387
        
2385

  
2388 2386
        if (params.containsKey("docid")) {
2389 2387
            docid = params.get("docid")[0];
2390 2388
        }
2391
        
2389

  
2392 2390
        if(params.containsKey("qformat")) {
2393 2391
            qformat = params.get("qformat")[0];
2394 2392
        }
2395
        
2393

  
2396 2394
        // Make sure we have a docid and datafile
2397 2395
        if (docid != null && fileList.containsKey("datafile")) {
2398 2396
            logMetacat.info("MetaCatServlet.handleUploadAction - Uploading data docid: " + docid);
......
2402 2400
            logMetacat.info("MetaCatServlet.handleUploadAction - Uploading filename: " + fileName);
2403 2401
            // Check if the right file existed in the uploaded data
2404 2402
            if (fileName != null) {
2405
                
2403

  
2406 2404
                try {
2407 2405
                    //logMetacat.info("Upload datafile " + docid
2408 2406
                    // +"...", 10);
......
2413 2411
                        File dataDirectory = new File(datafilepath);
2414 2412
                        dataDirectory.mkdirs();
2415 2413
                        File newFile = null;
2416
    //                    File tempFile = null;
2414
                        //                    File tempFile = null;
2417 2415
                        String tempFileName = fileList.get("name");
2418 2416
                        String newFileName = dataDirectory + File.separator + docid;
2419 2417
                        long size = 0;
2420 2418
                        boolean fileExists = false;
2421
                        
2419

  
2422 2420
                        try {
2423 2421
                            newFile = new File(newFileName);
2424 2422
                            fileExists = newFile.exists();
......
2436 2434
                                if (size == 0) {
2437 2435
                                    throw new IOException("Uploaded file is 0 bytes!");
2438 2436
                                }
2439
                            }
2437
                            } // Latent bug here if the file already exists, then the
2438
                              // conditional fails but the document is still registered.
2439
                              // maybe this never happens because we already requested a lock?
2440 2440
                            logMetacat.info("MetaCatServlet.handleUploadAction - Uploading the following to Metacat:" +
2441 2441
                                    fileName + ", " + docid + ", " +
2442 2442
                                    username + ", " + groupnames);
......
2447 2447
                            DocumentImpl.registerDocument(fileName, "BIN", docid,
2448 2448
                                    username, groupnames);
2449 2449
                        } catch (Exception ee) {
2450
                            //delete the file to create
2451
                            // if the revision already exists, don't delete
2452
                            // the file
2450
                            // If the file did not exist before this method was 
2451
                            // called and an exception is generated while 
2452
                            // creating or registering it, then we want to delete
2453
                            // the file from disk because the operation failed.
2454
                            // However, if the file already existed before the 
2455
                            // method was called, then the exception probably
2456
                            // occurs when registering the document, and so we
2457
                            // want to leave the old file in place.
2453 2458
                            if ( fileExists == false ) {
2454 2459
                                newFile.delete();
2455 2460
                            }
2461
                            
2456 2462
                            logMetacat.info("MetaCatServlet.handleUploadAction - in Exception: fileExists is " + fileExists);
2457 2463
                            logMetacat.error("MetaCatServlet.handleUploadAction - Upload Error: " + ee.getMessage());
2458 2464
                            throw ee;
2459 2465
                        }
2460
                        
2466

  
2461 2467
                        EventLog.getInstance().log(request.getRemoteAddr(),
2462 2468
                                username, docid, "upload");
2463 2469
                        // Force replication this data file
......
2470 2476
                        ForceReplicationHandler frh = new ForceReplicationHandler(
2471 2477
                                docid, "insert", false, null);
2472 2478
                        logMetacat.debug("MetaCatServlet.handleUploadAction - ForceReplicationHandler created: " + frh.toString());
2473
                        
2479

  
2474 2480
                        // set content type and other response header fields
2475 2481
                        // first
2476 2482
                        output += "<?xml version=\"1.0\"?>";
......
2479 2485
                        output += "<size>" + size + "</size>";
2480 2486
                        output += "</success>";
2481 2487
                    }
2482
                    
2488

  
2483 2489
                } catch (Exception e) {
2484
                    
2490

  
2485 2491
                    output += "<?xml version=\"1.0\"?>";
2486 2492
                    output += "<error>";
2487 2493
                    output += e.getMessage();
......
2499 2505
            output += "<?xml version=\"1.0\"?>";
2500 2506
            output += "<error>";
2501 2507
            output += "The uploaded data did not contain a valid docid "
2502
                    + "or valid file.";
2508
                + "or valid file.";
2503 2509
            output += "</error>";
2504 2510
        }
2505
        
2511

  
2506 2512
        if (qformat == null || qformat.equals("xml")) {
2507 2513
            response.setContentType("text/xml");
2508 2514
            out.println(output);
......
2514 2520
                        "message", "-//W3C//HTML//EN", qformat,
2515 2521
                        out, null, null);
2516 2522
            } catch (Exception e) {
2517
                
2523

  
2518 2524
                logMetacat.error("MetaCatServlet.handleUploadAction - General error: "
2519 2525
                        + e.getMessage());
2520 2526
                e.printStackTrace(System.out);
src/edu/ucsb/nceas/metacat/database/DBConnectionPool.java
224 224
    int random = 0; //random number
225 225
    int index = 0; //index
226 226
    int size = 0; //size of connection pool
227
    logMetacat.debug("DBConnectionPool.getDBConnection - Trying to check out connection...");
227
//    logMetacat.debug("DBConnectionPool.getDBConnection - Trying to check out connection...");
228 228
    size = connectionPool.size();
229
    logMetacat.debug("DBConnectionPool.getDBConnection - size of connection pool: " + size);
229
//    logMetacat.debug("DBConnectionPool.getDBConnection - size of connection pool: " + size);
230 230
    
231 231
     //try every DBConnection in the pool
232 232
    //every DBConnection will be try LIMITE times
......
239 239
      {
240 240
        index =(i+random)%size;
241 241
        db = (DBConnection) connectionPool.elementAt(index);
242
        logMetacat.debug("DBConnectionPool.getDBConnection - Index: " + index);
243
        logMetacat.debug("DBConnectionPool.getDBConnection - Tag: " + db.getTag());
244
        logMetacat.debug("DBConnectionPool.getDBConnection - Status: " + db.getStatus());
242
//        logMetacat.debug("DBConnectionPool.getDBConnection - Index: " + index);
243
//        logMetacat.debug("DBConnectionPool.getDBConnection - Tag: " + db.getTag());
244
//        logMetacat.debug("DBConnectionPool.getDBConnection - Status: " + db.getStatus());
245 245
        //check if the connection is free
246 246
        if (db.getStatus()==FREE)
247 247
        {
......
259 259
            db.setCheckOutMethodName(methodName);
260 260
            db.setAutoCommit(true);
261 261
            //debug message
262
            logMetacat.debug("DBConnectionPool.getDBConnection - The connection is checked out: " + db.getTag());
263
            logMetacat.debug("DBConnectionPool.getDBConnection - The method for checking is: " + db.getCheckOutMethodName());
264
            logMetacat.debug("DBConnectionPool.getDBConnection - The age is " + db.getAge());
265
            logMetacat.debug("DBConnectionPool.getDBConnection - The usage is " + db.getUsageCount());
266
            logMetacat.debug("DBConnectionPool.getDBConnection - The connection time is: " + db.getConnectionTime());
262
//            logMetacat.debug("DBConnectionPool.getDBConnection - The connection is checked out: " + db.getTag());
263
//            logMetacat.debug("DBConnectionPool.getDBConnection - The method for checking is: " + db.getCheckOutMethodName());
264
//            logMetacat.debug("DBConnectionPool.getDBConnection - The age is " + db.getAge());
265
//            logMetacat.debug("DBConnectionPool.getDBConnection - The usage is " + db.getUsageCount());
266
//            logMetacat.debug("DBConnectionPool.getDBConnection - The connection time is: " + db.getConnectionTime());
267 267
            //set check out time
268 268
            db.setCheckOutTime(System.currentTimeMillis());
269 269
            // set count of reach maximum 0 because it can check out
......
418 418
    index = getIndexOfPoolForConnection(conn);
419 419
    if ( index ==-1 )
420 420
    {
421
      logMetacat.info("DBConnectionPool.returnDBConnection - Couldn't find a DBConnection in the pool" + 
422
    		  " which have same tag to the returned DBConnetion object");
421
//      logMetacat.info("DBConnectionPool.returnDBConnection - Couldn't find a DBConnection in the pool" + 
422
//    		  " which have same tag to the returned DBConnetion object");
423 423
      return;
424 424
                                  
425 425
    }//if
......
429 429
      //if it is as same as the object's checkout serial number.
430 430
      //if it is same return it. If it is not same, maybe the connection already
431 431
      // was returned earlier.
432
      logMetacat.debug("DBConnectionPool.returnDBConnection - serial number in Connection: " +
433
              conn.getCheckOutSerialNumber());
434
      logMetacat.debug("DBConnectionPool.returnDBConnection - serial number in local: " + serialNumber);
432
//      logMetacat.debug("DBConnectionPool.returnDBConnection - serial number in Connection: " +
433
//              conn.getCheckOutSerialNumber());
434
//      logMetacat.debug("DBConnectionPool.returnDBConnection - serial number in local: " + serialNumber);
435 435
      if (conn.getCheckOutSerialNumber() == serialNumber)
436 436
      {
437 437
        dbConn = (DBConnection) connectionPool.elementAt(index);
......
444 444
        //set check out time to 0
445 445
        dbConn.setCheckOutTime(0);
446 446
        
447
        logMetacat.debug("DBConnectionPool.returnDBConnection - Connection: " + 
448
        		dbConn.getTag() + " checked in.");
449
        logMetacat.debug("DBConnectionPool.returnDBConnection - Connection: " +
450
        		dbConn.getTag() + "'s status: " + dbConn.getStatus());
447
//        logMetacat.debug("DBConnectionPool.returnDBConnection - Connection: " + 
448
//        		dbConn.getTag() + " checked in.");
449
//        logMetacat.debug("DBConnectionPool.returnDBConnection - Connection: " +
450
//        		dbConn.getTag() + "'s status: " + dbConn.getStatus());
451 451
                                                                       
452 452
      }//if
453 453
      else
454 454
      {
455
        logMetacat.info("DBConnectionPool.returnDBConnection - This DBConnection couldn't return" +
456
        		dbConn.getTag());
455
//        logMetacat.info("DBConnectionPool.returnDBConnection - This DBConnection couldn't return" +
456
//        		dbConn.getTag());
457 457
      }//else
458 458
    }//else
459 459
   
src/edu/ucsb/nceas/metacat/database/DatabaseService.java
102 102
	 * 
103 103
	 * @return AbstractDatabase object for this application's database adapter.
104 104
	 */
105
	public static AbstractDatabase getDBAdapter() {
105
	public AbstractDatabase getDBAdapter() {
106 106
		return dbAdapter;
107 107
	}
108 108

  
src/edu/ucsb/nceas/metacat/dataone/CrudService.java
22 22
 */
23 23
package edu.ucsb.nceas.metacat.dataone;
24 24

  
25
import java.io.File;
26
import java.io.FileNotFoundException;
27
import java.io.FileOutputStream;
25 28
import java.io.IOException;
26 29
import java.io.InputStream;
27 30
import java.io.OutputStream;
......
35 38
import javax.servlet.http.HttpServletRequest;
36 39
import javax.servlet.http.HttpServletResponse;
37 40

  
41
import org.apache.commons.io.IOUtils;
42
import org.apache.log4j.Logger;
38 43
import org.dataone.service.exceptions.IdentifierNotUnique;
39 44
import org.dataone.service.exceptions.InsufficientResources;
40 45
import org.dataone.service.exceptions.InvalidRequest;
......
55 60

  
56 61
import com.gc.iotools.stream.is.InputStreamFromOutputStream;
57 62

  
63
import edu.ucsb.nceas.metacat.AccessionNumberException;
64
import edu.ucsb.nceas.metacat.DocumentImpl;
65
import edu.ucsb.nceas.metacat.EventLog;
58 66
import edu.ucsb.nceas.metacat.IdentifierManager;
59 67
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
60 68
import edu.ucsb.nceas.metacat.McdbException;
61 69
import edu.ucsb.nceas.metacat.MetacatHandler;
62 70
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
71
import edu.ucsb.nceas.metacat.properties.PropertyService;
72
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
63 73
import edu.ucsb.nceas.metacat.service.SessionService;
64 74
import edu.ucsb.nceas.metacat.util.SessionData;
65 75
import edu.ucsb.nceas.utilities.ParseLSIDException;
......
84 94
//    private String sessionId;
85 95
//    private String[] groupNames;
86 96
    private Hashtable<String, String[]> params;
97
    Logger logMetacat = null;
87 98

  
88 99
    /**
89 100
     * Initializes new instance by setting servlet context,request and response.
......
95 106
        this.servletContext = servletContext;
96 107
        this.request = request;
97 108
        this.response = response;
109
        logMetacat = Logger.getLogger(CrudService.class);
98 110

  
99 111
        handler = new MetacatHandler(this.servletContext, new Timer());
100 112
        initParams();
......
165 177
            InputStream object, SystemMetadata sysmeta) throws InvalidToken, 
166 178
            ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, 
167 179
            InsufficientResources, InvalidSystemMetadata, NotImplemented {
168
        throw new NotImplemented(1000, "This method not yet implemented.");
180

  
181
        logMetacat.debug("Starting CrudService.create()...");
182
        
183
        // TODO: authenticate & get user info
184
        String username = "GARBAGETOBEREPLACED";
185
        String[] groups = null;
186

  
187
        // verify that guid == SystemMetadata.getIdentifier()
188
        logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getIdentifier() + "|" + sysmeta.getIdentifier().getIdentifier());
189
//        if (!guid.getIdentifier().equals(sysmeta.getIdentifier().getIdentifier())) {
190
//            throw new InvalidSystemMetadata(1180, 
191
//                "GUID in method call does not match GUID in system metadata.");
192
//        }
193

  
194
        logMetacat.debug("Checking if identifier exists...");
195
        // Check that the identifier does not already exist
196
        IdentifierManager im = IdentifierManager.getInstance();
197
        if (im.identifierExists(guid.getIdentifier())) {
198
            throw new IdentifierNotUnique(1120, 
199
                "GUID is already in use by an existing object.");
200
        }
201

  
202
        // generate guid/localId pair for object
203
        logMetacat.debug("Generating a guid/localId mapping");
204
        String localId = im.generateLocalId(guid.getIdentifier(), 1);
205

  
206
        // TODO: generate guid/localId pair for sysmeta
207
        // TODO: update system metadata fields
208
        // TODO: insert system metadata to metacat (probably at end)
209

  
210
        // TODO: Check if we are handling metadata or data
211
        
212
        // TODO: CASE METADATA:
213
        // Setup and call handleInsertOrUpdate()
214

  
215
        // TODO: DEFAULT CASE: DATA
216
        try {
217
            logMetacat.debug("Case DATA: starting to write to disk.");
218
            if (DocumentImpl.getDataFileLockGrant(localId)) {
219

  
220
                // Save the data file to disk using "localId" as the name
221
                try {
222
                    String datafilepath = PropertyService.getProperty("application.datafilepath");
223

  
224
                    File dataDirectory = new File(datafilepath);
225
                    dataDirectory.mkdirs();
226

  
227
                    File newFile = writeStreamToFile(dataDirectory, localId, object);
228

  
229
                    // TODO: Check that the file size matches SystemMetadata
230
                    //                        long size = newFile.length();
231
                    //                        if (size == 0) {
232
                    //                            throw new IOException("Uploaded file is 0 bytes!");
233
                    //                        }
234

  
235
                    //register the file in the database (which generates an exception
236
                    // if the docid is not acceptable or other untoward things happen
237
                    try {
238
                        logMetacat.debug("Registering document...");
239
                        DocumentImpl.registerDocument(localId, "BIN", localId,
240
                                username, groups);
241
                        logMetacat.debug("Registrtion step completed.");
242
                    } catch (SQLException e) {
243
                        //newFile.delete();
244
                        logMetacat.debug("SQLE: " + e.getMessage());
245
                        e.printStackTrace(System.out);
246
                        throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
247
                    } catch (AccessionNumberException e) {
248
                        //newFile.delete();
249
                        logMetacat.debug("ANE: " + e.getMessage());
250
                        e.printStackTrace(System.out);
251
                        throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
252
                    } catch (Exception e) {
253
                        //newFile.delete();
254
                        logMetacat.debug("Exception: " + e.getMessage());
255
                        e.printStackTrace(System.out);
256
                        throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
257
                    }
258

  
259
                    logMetacat.debug("Logging the creation event.");
260
                    EventLog.getInstance().log(request.getRemoteAddr(),
261
                            username, localId, "create");
262
                    
263
                    // Force replication this data file
264
                    // To data file, "insert" and update is same
265
                    // The fourth parameter is null. Because it is notification 
266
                    // server and this method is in MetaCatServerlet. It is
267
                    // original command, not get force replication info from 
268
                    // another metacat
269
                    // TODO: note that GUID mapping is not being replicated
270
                    logMetacat.debug("Scheduling replication.");
271
                    ForceReplicationHandler frh = new ForceReplicationHandler(
272
                            localId, "insert", false, null);
273

  
274
                } catch (PropertyNotFoundException e) {
275
                    throw new ServiceFailure(1190, "Could not lock file for writing:" + e.getMessage());
276
                }
277

  
278
            }
279
        } catch (Exception e) {
280
            // Could not get a lock on the document, so we can not update the file now
281
            throw new ServiceFailure(1190, "Failed to lock file: " + e.getMessage());
282
        }
283
        
284
        logMetacat.debug("Returning from CrudService.create()");
285
        return guid;
169 286
    }
170 287

  
171 288
    public IdentifierType delete(AuthToken token, IdentifierType guid)
......
296 413
    public SystemMetadata getSystemMetadata(AuthToken token, IdentifierType guid)
297 414
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
298 415
            InvalidRequest, NotImplemented {
416
        
417
        // Look up ID of system metadata based on guid
418
            // Initially from document, later from entry in table?
419
        
420
        // Read system metadata from disk and create SystemMetadata object
421
            // Follows same implementation plan as get()
422
        
423
        // return it
299 424
        throw new NotImplemented(1000, "This method not yet implemented.");
300 425
    }
301 426

  
......
307 432
        throw new NotImplemented(1000, "This method not yet implemented.");
308 433
    }
309 434

  
435
    private File writeStreamToFile(File dir, String fileName, InputStream data) 
436
        throws ServiceFailure {
437
//        logMetacat.debug("***********************************************");
438
//        logMetacat.debug("** FILE TEST FROM CS                         **");
439
//        logMetacat.debug("***********************************************");
440
        
441
        File newFile = new File(dir, fileName);
442
        logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
443

  
444
        try {
445
            if (newFile.createNewFile()) {
446
//                logMetacat.debug("File doesn't yet exist, so write to it.");
447
//                logMetacat.debug("Creating file stream...");
448
                
449
                // write data stream to desired file
450
                OutputStream os = new FileOutputStream(newFile);
451
//                logMetacat.debug("Copying file stream...");
452
                long length = IOUtils.copyLarge(data, os);
453
                os.flush();
454
//                logMetacat.debug("Closing file stream...");
455
                os.close();
456
//                logMetacat.debug("Done with file stream...");
457
            } else {
458
                logMetacat.debug("File creation failed, or file already exists.");
459
                throw new ServiceFailure(1190, "File already exists: " + fileName);
460
            }
461
        } catch (FileNotFoundException e) {
462
            logMetacat.debug("FNF: " + e.getMessage());
463
            throw new ServiceFailure(1190, "File not found: " + fileName + " " 
464
                    + e.getMessage());
465
        } catch (IOException e) {
466
            logMetacat.debug("IOE: " + e.getMessage());
467
            throw new ServiceFailure(1190, "File was not written: " + fileName 
468
                    + " " + e.getMessage());
469
        }
470

  
471
//        logMetacat.debug("***********************************************");
472
//        logMetacat.debug("** END FILE TEST                             **");
473
//        logMetacat.debug("***********************************************");
474
        
475
        return newFile;
476
    }   
310 477
}
src/edu/ucsb/nceas/metacat/replication/ReplicationHandler.java
671 671
         sql.append(datestr).append("', 'YY-MM-DD HH24:MI:SS') where ");
672 672
         sql.append("server like '").append(server).append("'");*/
673 673
         sql.append("update xml_replication set last_checked = ");
674
         sql.append(DatabaseService.getDBAdapter().toDate(datestr, "MM/DD/YY HH24:MI:SS"));
674
         sql.append(DatabaseService.getInstance().getDBAdapter().toDate(datestr, "MM/DD/YY HH24:MI:SS"));
675 675
         sql.append(" where server like '").append(server).append("'");
676 676
         pstmt = dbConn.prepareStatement(sql.toString());
677 677

  
src/edu/ucsb/nceas/metacat/replication/ReplicationService.java
279 279
				//Get hub value
280 280
				hub = ((String[]) params.get("hub"))[0];
281 281

  
282
				String toDateSql = DatabaseService.getDBAdapter().toDate("01/01/1980","MM/DD/YYYY");
282
				String toDateSql = DatabaseService.getInstance().getDBAdapter().toDate("01/01/1980","MM/DD/YYYY");
283 283
				String sql = "INSERT INTO xml_replication "
284 284
						+ "(server, last_checked, replicate, datareplicate, hub) "
285 285
						+ "VALUES (?," + toDateSql + ",?,?,?)";
......
1181 1181

  
1182 1182
			// Get correct docid that reside on this server according the requesting
1183 1183
			// server's replicate and data replicate value in xml_replication table
1184
			docsql.append(DatabaseService.getDBAdapter().getReplicationDocumentListSQL());
1184
			docsql.append(DatabaseService.getInstance().getDBAdapter().getReplicationDocumentListSQL());
1185 1185
			//docsql.append("select docid, rev, doctype from xml_documents where (docid not in (select a.docid from xml_documents a, xml_revisions b where a.docid=b.docid and a.rev<=b.rev)) ");
1186 1186
			revisionSql.append("select docid, rev, doctype from xml_revisions ");
1187 1187
			// If the localhost is not a hub to the remote server, only replicate
src/edu/ucsb/nceas/metacat/replication/ReplicationServerList.java
456 456
        pStmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
457 457
                      "(server, last_checked, replicate, datareplicate, hub) "+
458 458
                       "VALUES ('" + givenServerName + "', "+
459
                        DatabaseService.getDBAdapter().toDate("01/01/1980","MM/DD/YYYY") + ", '" +
459
                        DatabaseService.getInstance().getDBAdapter().toDate("01/01/1980","MM/DD/YYYY") + ", '" +
460 460
                       replicate +"', '"+dataReplicate+"','"+ hub + "')");
461 461
        pStmt.execute();
462 462
       

Also available in: Unified diff