Project

General

Profile

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

    
25
package edu.ucsb.nceas.metacat;
26

    
27
import java.io.BufferedInputStream;
28
import java.io.File;
29
import java.io.FileInputStream;
30
import java.io.FileReader;
31
import java.io.IOException;
32
import java.io.OutputStream;
33
import java.io.OutputStreamWriter;
34
import java.io.PrintWriter;
35
import java.io.StringReader;
36
import java.io.ByteArrayOutputStream;
37
import java.io.Writer;
38
import java.net.MalformedURLException;
39
import java.net.URL;
40
import java.sql.PreparedStatement;
41
import java.sql.ResultSet;
42
import java.sql.SQLException;
43
import java.sql.Timestamp;
44
import java.text.ParseException;
45
import java.text.SimpleDateFormat;
46
import java.util.Enumeration;
47
import java.util.HashMap;
48
import java.util.Hashtable;
49
import java.util.Iterator;
50
import java.util.Map;
51
import java.util.Timer;
52
import java.util.Vector;
53
import java.util.zip.ZipEntry;
54
import java.util.zip.ZipOutputStream;
55

    
56
import javax.servlet.ServletContext;
57
import javax.servlet.ServletOutputStream;
58
import javax.servlet.http.HttpServletRequest;
59
import javax.servlet.http.HttpServletResponse;
60
import javax.servlet.http.HttpSession;
61
import javax.activation.MimetypesFileTypeMap;
62

    
63
import org.apache.log4j.Logger;
64
import org.ecoinformatics.eml.EMLParser;
65

    
66
import au.com.bytecode.opencsv.CSVWriter;
67

    
68
import com.oreilly.servlet.multipart.FilePart;
69
import com.oreilly.servlet.multipart.MultipartParser;
70
import com.oreilly.servlet.multipart.ParamPart;
71
import com.oreilly.servlet.multipart.Part;
72

    
73
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
74
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlForSingleFile;
75
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
76
import edu.ucsb.nceas.metacat.cart.CartManager;
77
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
78
import edu.ucsb.nceas.metacat.database.DBConnection;
79
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
80
import edu.ucsb.nceas.metacat.dataquery.DataQuery;
81
import edu.ucsb.nceas.metacat.properties.PropertyService;
82
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
83
import edu.ucsb.nceas.metacat.service.SessionService;
84
import edu.ucsb.nceas.metacat.service.XMLSchemaService;
85
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
86
import edu.ucsb.nceas.metacat.shared.ServiceException;
87
import edu.ucsb.nceas.metacat.spatial.SpatialHarvester;
88
import edu.ucsb.nceas.metacat.spatial.SpatialQuery;
89
import edu.ucsb.nceas.metacat.util.AuthUtil;
90
import edu.ucsb.nceas.metacat.util.DocumentUtil;
91
import edu.ucsb.nceas.metacat.util.MetacatUtil;
92
import edu.ucsb.nceas.metacat.util.RequestUtil;
93
import edu.ucsb.nceas.metacat.util.SystemUtil;
94
import edu.ucsb.nceas.utilities.FileUtil;
95
import edu.ucsb.nceas.utilities.LSIDUtil;
96
import edu.ucsb.nceas.utilities.ParseLSIDException;
97
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
98

    
99
/**
100
 * General entry point for the Metacat server which is called from various 
101
 * mechanisms such as the standard MetacatServlet class and the various web
102
 * service servlets such as RestServlet class.  All application logic should be
103
 * encapsulated in this class, and the calling classes should only contain
104
 * parameter marshaling and demarshaling code, delegating all else to this
105
 * MetacatHandler instance.
106
 * @author Matthew Jones
107
 */
108
public class MetacatHandler {
109

    
110
    private static boolean _sitemapScheduled = false;
111

    
112
    // Constants -- these should be final in a servlet    
113
    private static final String PROLOG = "<?xml version=\"1.0\"?>";
114
    private static final String SUCCESS = "<success>";
115
    private static final String SUCCESSCLOSE = "</success>";
116
    private static final String ERROR = "<error>";
117
    private static final String ERRORCLOSE = "</error>";
118
    
119
	private Timer timer;
120
	
121
    public MetacatHandler(Timer timer) {
122
    	this.timer = timer;
123
    }
124
    
125
    
126
    protected void handleDataquery(
127
            Hashtable<String, String[]> params,
128
            HttpServletResponse response,
129
            String sessionId) throws PropertyNotFoundException, IOException {
130
        
131
        DataQuery dq = null;
132
        if (sessionId != null) {
133
            dq = new DataQuery(sessionId);
134
        }
135
        else {
136
            dq = new DataQuery();
137
        }
138
        
139
        String dataqueryXML = (params.get("dataquery"))[0];
140

    
141
        ResultSet rs = null;
142
        try {
143
            rs = dq.executeQuery(dataqueryXML);
144
        } catch (Exception e) {
145
            //probably need to do something here
146
            e.printStackTrace();
147
            return;
148
        }
149
        
150
        //process the result set
151
        String qformat = "csv";
152
        String[] temp = params.get("qformat");
153
        if (temp != null && temp.length > 0) {
154
            qformat = temp[0];
155
        }
156
        String fileName = "query-results." + qformat;
157
        
158
        //get the results as csv file
159
        if (qformat != null && qformat.equalsIgnoreCase("csv")) {
160
            response.setContentType("text/csv");
161
            //response.setContentType("application/csv");
162
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
163
            
164
            Writer writer = new OutputStreamWriter(response.getOutputStream());
165
            CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
166
            try {
167
                
168
                csv.writeAll(rs, true);
169
                
170
                csv.flush();
171
                response.flushBuffer();
172
                 
173
                rs.close();
174
                
175
            } catch (SQLException e) {
176
                e.printStackTrace();
177
            }
178
            
179
            return;
180
        }
181
        
182
    }
183
    
184
    protected void handleEditCart(
185
            Hashtable<String, String[]> params,
186
            HttpServletResponse response,
187
            String sessionId) throws PropertyNotFoundException, IOException {
188
        
189
        CartManager cm = null;
190
        if (sessionId != null) {
191
            cm = new CartManager(sessionId);
192
        }
193
        else {
194
            cm = new CartManager();
195
        }
196
        
197
        String editOperation = (params.get("operation"))[0];
198
        
199
        String[] docids = params.get("docid");
200
        String[] field = params.get("field");
201
        String[] path = params.get("path");
202
        
203
        Map<String,String> fields = null;
204
        if (field != null && path != null) {
205
            fields = new HashMap<String,String>();
206
            fields.put(field[0], path[0]);
207
        }
208
        
209
        //TODO handle attribute map (metadata fields)
210
        cm.editCart(editOperation, docids, fields);
211
        
212
    }
213
    
214
    // ///////////////////////////// METACAT SPATIAL ///////////////////////////
215
    
216
    /**
217
     * handles all spatial queries -- these queries may include any of the
218
     * queries supported by the WFS / WMS standards
219
     * 
220
     * handleSQuery(out, params, response, username, groupnames, sess_id);
221
     */
222
    protected void handleSpatialQuery(PrintWriter out, Hashtable<String, String[]> params,
223
            HttpServletResponse response,
224
            String username, String[] groupnames,
225
            String sess_id) throws PropertyNotFoundException {
226
        
227
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
228
        
229
        if ( !PropertyService.getProperty("spatial.runSpatialOption").equals("true") ) {
230
            response.setContentType("text/html");
231
            out.println("<html> Metacat Spatial Option is turned off </html>");
232
            out.close();
233
            return ;
234
        }
235
        
236
        /*
237
         * Perform spatial query against spatial cache
238
         */
239
        float _xmax = Float.valueOf( (params.get("xmax"))[0] ).floatValue();
240
        float _ymax = Float.valueOf( (params.get("ymax"))[0] ).floatValue();
241
        float _xmin = Float.valueOf( (params.get("xmin"))[0] ).floatValue();
242
        float _ymin = Float.valueOf( (params.get("ymin"))[0] ).floatValue();
243
        SpatialQuery sq = new SpatialQuery();
244
        Vector<String> docids = sq.filterByBbox( _xmin, _ymin, _xmax, _ymax );
245
        // logMetacat.info(" --- Spatial Query completed. Passing on the SQuery
246
        // handler");
247
        // logMetacat.warn("\n\n ******* after spatial query, we've got " +
248
        // docids.size() + " docids \n\n");
249
        
250
        /*
251
         * Create an array matching docids
252
         */
253
        String [] docidArray = new String[docids.size()];
254
        docids.toArray(docidArray);
255
        
256
        /*
257
         * Create squery string
258
         */
259
        String squery = DocumentIdQuery.createDocidQuery( docidArray );
260
        // logMetacat.info("-----------\n" + squery + "\n------------------");
261
        String[] queryArray = new String[1];
262
        queryArray[0] = squery;
263
        params.put("query", queryArray);
264
        
265
        /*
266
         * Determine qformat
267
         */
268
        String[] qformatArray = new String[1];
269
        try {
270
            String _skin = (params.get("skin"))[0];
271
            qformatArray[0] = _skin;
272
        } catch (java.lang.NullPointerException e) {
273
            // should be "default" but keep this for backwards compatibility
274
            // with knp site
275
            logMetacat.warn("MetaCatServlet.handleSpatialQuery - No SKIN specified for metacat actions=spatial_query... defaulting to 'knp' skin !\n");
276
            qformatArray[0] = "knp";
277
        }
278
        params.put("qformat", qformatArray);
279
        
280
        // change the action
281
        String[] actionArray = new String[1];
282
        actionArray[0] = "squery";
283
        params.put("action", actionArray);
284
        
285
        /*
286
         * Pass the docids to the DBQuery contructor
287
         */
288
        // This is a hack to get the empty result set to show...
289
        // Otherwise dbquery sees no docidOverrides and does a full % percent
290
        // query
291
        if (docids.size() == 0)
292
            docids.add("");
293
        
294
        DBQuery queryobj = new DBQuery(docids);
295
        queryobj.findDocuments(response, out, params, username, groupnames, sess_id);
296
        
297
    }
298
    
299
    // LOGIN & LOGOUT SECTION
300
    /**
301
     * Handle the login request. Create a new session object. Do user
302
     * authentication through the session.
303
     */
304
    public void handleLoginAction(PrintWriter out, Hashtable<String, String[]> params,
305
            HttpServletRequest request, HttpServletResponse response) {
306
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
307
        AuthSession sess = null;
308
        
309
        if(params.get("username") == null){
310
            response.setContentType("text/xml");
311
            out.println("<?xml version=\"1.0\"?>");
312
            out.println("<error>");
313
            out.println("Username not specified");
314
            out.println("</error>");
315
            return;
316
        }
317
        
318
        // }
319
        
320
        if(params.get("password") == null){
321
            response.setContentType("text/xml");
322
            out.println("<?xml version=\"1.0\"?>");
323
            out.println("<error>");
324
            out.println("Password not specified");
325
            out.println("</error>");
326
            return;
327
        }
328
        
329
        String un = (params.get("username"))[0];
330
        logMetacat.info("MetaCatServlet.handleLoginAction - user " + un + " is trying to login");
331
        String pw = (params.get("password"))[0];
332
        
333
        String qformat = "xml";
334
        if (params.get("qformat") != null) {
335
            qformat = (params.get("qformat"))[0];
336
        }
337
        
338
        try {
339
            sess = new AuthSession();
340
        } catch (Exception e) {
341
            String errorMsg = "MetacatServlet.handleLoginAction - Problem in MetacatServlet.handleLoginAction() authenicating session: "
342
                + e.getMessage();
343
            logMetacat.error(errorMsg);
344
            out.println(errorMsg);
345
            e.printStackTrace(System.out);
346
            return;
347
        }
348
        boolean isValid = sess.authenticate(request, un, pw);
349
        
350
        //if it is authernticate is true, store the session
351
        if (isValid) {
352
            HttpSession session = sess.getSessions();
353
            String id = session.getId();
354
            
355
            logMetacat.debug("MetaCatServlet.handleLoginAction - Store session id " + id
356
                    + " which has username" + session.getAttribute("username")
357
                    + " into hash in login method");
358
            try {
359
                System.out.println("registering session with id " + id);
360
                System.out.println("username: " + (String) session.getAttribute("username"));
361
                SessionService.getInstance().registerSession(id, 
362
                        (String) session.getAttribute("username"), 
363
                        (String[]) session.getAttribute("groupnames"), 
364
                        (String) session.getAttribute("password"), 
365
                        (String) session.getAttribute("name"));
366
                
367
                    
368
            } catch (ServiceException se) {
369
                String errorMsg = "MetacatServlet.handleLoginAction - service problem registering session: "
370
                        + se.getMessage();
371
                logMetacat.error("MetaCatServlet.handleLoginAction - " + errorMsg);
372
                out.println(errorMsg);
373
                se.printStackTrace(System.out);
374
                return;
375
            }           
376
        }
377
                
378
        // format and transform the output
379
        if (qformat.equals("xml")) {
380
            response.setContentType("text/xml");
381
            out.println(sess.getMessage());
382
        } else {
383
            try {
384
                DBTransform trans = new DBTransform();
385
                response.setContentType("text/html");
386
                trans.transformXMLDocument(sess.getMessage(),
387
                        "-//NCEAS//login//EN", "-//W3C//HTML//EN", qformat,
388
                        out, null, null);
389
            } catch (Exception e) {               
390
                logMetacat.error("MetaCatServlet.handleLoginAction - General error"
391
                        + e.getMessage());
392
                e.printStackTrace(System.out);
393
            }
394
        }
395
    }
396
    
397
    /**
398
     * Handle the logout request. Close the connection.
399
     */
400
    public void handleLogoutAction(PrintWriter out, Hashtable<String, String[]> params,
401
            HttpServletRequest request, HttpServletResponse response) {
402
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
403
        String qformat = "xml";
404
        if(params.get("qformat") != null){
405
            qformat = params.get("qformat")[0];
406
        }
407
        
408
        // close the connection
409
        HttpSession sess = request.getSession(false);
410
        logMetacat.info("MetaCatServlet.handleLogoutAction - After get session in logout request");
411
        if (sess != null) {
412
            logMetacat.info("MetaCatServlet.handleLogoutAction - The session id " + sess.getId()
413
            + " will be invalidate in logout action");
414
            logMetacat.info("MetaCatServlet.handleLogoutAction - The session contains user "
415
                    + sess.getAttribute("username")
416
                    + " will be invalidate in logout action");
417
            sess.invalidate();
418
            SessionService.getInstance().unRegisterSession(sess.getId());
419
        }
420
        
421
        // produce output
422
        StringBuffer output = new StringBuffer();
423
        output.append("<?xml version=\"1.0\"?>");
424
        output.append("<logout>");
425
        output.append("User logged out");
426
        output.append("</logout>");
427
        
428
        //format and transform the output
429
        if (qformat.equals("xml")) {
430
            response.setContentType("text/xml");
431
            out.println(output.toString());
432
        } else {
433
            try {
434
                DBTransform trans = new DBTransform();
435
                response.setContentType("text/html");
436
                trans.transformXMLDocument(output.toString(),
437
                        "-//NCEAS//login//EN", "-//W3C//HTML//EN", qformat,
438
                        out, null, null);
439
            } catch (Exception e) {
440
                logMetacat.error(
441
                        "MetaCatServlet.handleLogoutAction - General error: "
442
                        + e.getMessage());
443
                e.printStackTrace(System.out);
444
            }
445
        }
446
    }
447
    
448
    // END OF LOGIN & LOGOUT SECTION
449
    
450
    // SQUERY & QUERY SECTION
451
    /**
452
     * Retreive the squery xml, execute it and display it
453
     *
454
     * @param out the output stream to the client
455
     * @param params the Hashtable of parameters that should be included in the
456
     *            squery.
457
     * @param response the response object linked to the client
458
     * @param conn the database connection
459
     */
460
    protected void handleSQuery(PrintWriter out, Hashtable<String, String[]> params,
461
            HttpServletResponse response, String user, String[] groups,
462
            String sessionid) throws PropertyNotFoundException {
463
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
464
        long squeryWarnLimit = Long.parseLong(PropertyService.getProperty("database.squeryTimeWarnLimit"));
465
        
466
        long startTime = System.currentTimeMillis();
467
        DBQuery queryobj = new DBQuery();
468
        queryobj.findDocuments(response, out, params, user, groups, sessionid);
469
        long outPutTime = System.currentTimeMillis();
470
        long runTime = outPutTime - startTime;
471

    
472
        if (runTime > squeryWarnLimit) {
473
            logMetacat.warn("MetaCatServlet.handleSQuery - Long running squery.  Total time: " + runTime + 
474
                    " ms, squery: " + ((String[])params.get("query"))[0]);
475
        }
476
        logMetacat.debug("MetaCatServlet.handleSQuery - squery: " + ((String[])params.get("query"))[0] + 
477
                " ran in " + runTime + " ms");
478
    }
479
    
480
    /**
481
     * Create the xml query, execute it and display the results.
482
     *
483
     * @param out the output stream to the client
484
     * @param params the Hashtable of parameters that should be included in the
485
     *            squery.
486
     * @param response the response object linked to the client
487
     */
488
    protected void handleQuery(PrintWriter out, Hashtable<String, String[]> params,
489
            HttpServletResponse response, String user, String[] groups,
490
            String sessionid) throws PropertyNotFoundException {
491
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
492
        long queryWarnLimit = Long.parseLong(PropertyService.getProperty("database.queryTimeWarnLimit"));
493
        
494
        //create the query and run it
495
        String xmlquery = DBQuery.createSQuery(params);
496
        String[] queryArray = new String[1];
497
        queryArray[0] = xmlquery;
498
        params.put("query", queryArray);
499
        long startTime = System.currentTimeMillis();
500
        DBQuery queryobj = new DBQuery();
501
        queryobj.findDocuments(response, out, params, user, groups, sessionid);
502
        long outPutTime = System.currentTimeMillis();
503
        long runTime = outPutTime -startTime;
504

    
505
        if (runTime > queryWarnLimit) {
506
            logMetacat.warn("MetaCatServlet.handleQuery - Long running squery.  Total time: " + runTime + 
507
                    " ms, squery: " + ((String[])params.get("query"))[0]);
508
        }
509
        logMetacat.debug("MetaCatServlet.handleQuery - query: " + ((String[])params.get("query"))[0] + 
510
                " ran in " + runTime + " ms");
511
    }
512
    
513
    // END OF SQUERY & QUERY SECTION
514
    
515
    //Export section
516
    /**
517
     * Handle the "export" request of data package from Metacat in zip format
518
     *
519
     * @param params the Hashtable of HTTP request parameters
520
     * @param response the HTTP response object linked to the client
521
     * @param user the username sent the request
522
     * @param groups the user's groupnames
523
     */
524
    protected void handleExportAction(Hashtable<String, String[]> params,
525
            HttpServletResponse response,
526
            String user, String[] groups, String passWord) {
527
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
528
        // Output stream
529
        ServletOutputStream out = null;
530
        // Zip output stream
531
        ZipOutputStream zOut = null;
532
        DBQuery queryObj = null;
533
        
534
        String[] docs = new String[10];
535
        String docId = "";
536
        
537
        try {
538
            // read the params
539
            if (params.containsKey("docid")) {
540
                docs = params.get("docid");
541
            }
542
            // Create a DBuery to handle export
543
            queryObj = new DBQuery();
544
            // Get the docid
545
            docId = docs[0];
546
            // Make sure the client specify docid
547
            if (docId == null || docId.equals("")) {
548
                response.setContentType("text/xml"); //MIME type
549
                // Get a printwriter
550
                PrintWriter pw = response.getWriter();
551
                // Send back message
552
                pw.println("<?xml version=\"1.0\"?>");
553
                pw.println("<error>");
554
                pw.println("You didn't specify requested docid");
555
                pw.println("</error>");
556
                // Close printwriter
557
                pw.close();
558
                return;
559
            }
560
            // Get output stream
561
            out = response.getOutputStream();
562
            response.setContentType("application/zip"); //MIME type
563
            response.setHeader("Content-Disposition",
564
                    "attachment; filename="
565
                    + docId + ".zip"); // Set the name of the zip file
566
            
567
            zOut = new ZipOutputStream(out);
568
            zOut = queryObj
569
                    .getZippedPackage(docId, out, user, groups, passWord);
570
            zOut.finish(); //terminate the zip file
571
            zOut.close(); //close the zip stream
572
            
573
        } catch (Exception e) {
574
            try {
575
                response.setContentType("text/xml"); //MIME type
576
                // Send error message back
577
                if (out != null) {
578
                    PrintWriter pw = new PrintWriter(out);
579
                    pw.println("<?xml version=\"1.0\"?>");
580
                    pw.println("<error>");
581
                    pw.println(e.getMessage());
582
                    pw.println("</error>");
583
                    // Close printwriter
584
                    pw.close();
585
                    // Close output stream
586
                    out.close();
587
                }
588
                // Close zip output stream
589
                if (zOut != null) {
590
                    zOut.close();
591
                }
592
            } catch (IOException ioe) {
593
                logMetacat.error("MetaCatServlet.handleExportAction - Problem with the servlet output: "
594
                        + ioe.getMessage());
595
                e.printStackTrace(System.out);
596
            }
597
            
598
            logMetacat.error("MetaCatServlet.handleExportAction - General error: "
599
                    + e.getMessage());
600
            e.printStackTrace(System.out);
601
            
602
        }
603
        
604
    }
605
    
606
    /**
607
     * In eml2 document, the xml can have inline data and data was stripped off
608
     * and store in file system. This action can be used to read inline data
609
     * only
610
     *
611
     * @param params the Hashtable of HTTP request parameters
612
     * @param response the HTTP response object linked to the client
613
     * @param user the username sent the request
614
     * @param groups the user's groupnames
615
     */
616
    protected void handleReadInlineDataAction(Hashtable<String, String[]> params,
617
            HttpServletRequest request, HttpServletResponse response,
618
            String user, String passWord, String[] groups) {
619
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
620
        String[] docs = new String[10];
621
        String inlineDataId = null;
622
        String docId = "";
623
        ServletOutputStream out = null;
624
        
625
        try {
626
            // read the params
627
            if (params.containsKey("inlinedataid")) {
628
                docs = params.get("inlinedataid");
629
            }
630
            // Get the docid
631
            inlineDataId = docs[0];
632
            // Make sure the client specify docid
633
            if (inlineDataId == null || inlineDataId.equals("")) {
634
                throw new Exception("You didn't specify requested inlinedataid"); }
635
            
636
            // check for permission
637
            docId = 
638
                DocumentUtil.getDocIdWithoutRevFromInlineDataID(inlineDataId);
639
            PermissionController controller = new PermissionController(docId);
640
            // check top level read permission
641
            if (!controller.hasPermission(user, groups,
642
                    AccessControlInterface.READSTRING)) {
643
                throw new Exception("User " + user
644
                        + " doesn't have permission " + " to read document "
645
                        + docId);
646
            } else {
647
                //check data access level
648
                try {
649
                    Hashtable<String,String> unReadableInlineDataList =
650
                            PermissionController.getUnReadableInlineDataIdList(docId,
651
                            user, groups, false);
652
                    String inlineDataIdWithoutRev = DocumentUtil.getInlineDataIdWithoutRev(inlineDataId);
653
                    if (unReadableInlineDataList.containsValue(inlineDataIdWithoutRev)) {
654
                        throw new Exception("User " + user
655
                                + " doesn't have permission " + " to read inlinedata "
656
                                + inlineDataId);
657
                        
658
                    }//if
659
                }//try
660
                catch (Exception e) {
661
                    throw e;
662
                }//catch
663
            }//else
664
            
665
            // Get output stream
666
            out = response.getOutputStream();
667
            // read the inline data from the file
668
            String inlinePath = PropertyService.getProperty("application.inlinedatafilepath");
669
            File lineData = new File(inlinePath, inlineDataId);
670
            FileInputStream input = new FileInputStream(lineData);
671
            byte[] buffer = new byte[4 * 1024];
672
            int bytes = input.read(buffer);
673
            while (bytes != -1) {
674
                out.write(buffer, 0, bytes);
675
                bytes = input.read(buffer);
676
            }
677
            out.close();
678
            
679
            EventLog.getInstance().log(request.getRemoteAddr(), user,
680
                    inlineDataId, "readinlinedata");
681
        } catch (Exception e) {
682
            try {
683
                PrintWriter pw = null;
684
                // Send error message back
685
                if (out != null) {
686
                    pw = new PrintWriter(out);
687
                } else {
688
                    pw = response.getWriter();
689
                }
690
                pw.println("<?xml version=\"1.0\"?>");
691
                pw.println("<error>");
692
                pw.println(e.getMessage());
693
                pw.println("</error>");
694
                // Close printwriter
695
                pw.close();
696
                // Close output stream if out is not null
697
                if (out != null) {
698
                    out.close();
699
                }
700
            } catch (IOException ioe) {
701
                logMetacat.error("MetaCatServlet.handleReadInlineDataAction - Problem with the servlet output: "
702
                        + ioe.getMessage());
703
                e.printStackTrace(System.out);
704
            }
705
            logMetacat.error("MetaCatServlet.handleReadInlineDataAction - General error: "
706
                    + e.getMessage());
707
            e.printStackTrace(System.out);
708
        }
709
    }
710
    
711
    /**
712
     * Handle the "read" request of metadata/data files from Metacat or any
713
     * files from Internet; transformed metadata XML document into HTML
714
     * presentation if requested; zip files when more than one were requested.
715
     *
716
     * @param params the Hashtable of HTTP request parameters
717
     * @param request the HTTP request object linked to the client
718
     * @param response the HTTP response object linked to the client
719
     * @param user the username sent the request
720
     * @param groups the user's groupnames
721
     */
722
    public void handleReadAction(Hashtable<String, String[]> params, HttpServletRequest request,
723
            HttpServletResponse response, String user, String passWord,
724
            String[] groups) {
725
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
726
        ServletOutputStream out = null;
727
        ZipOutputStream zout = null;
728
        PrintWriter pw = null;
729
        boolean zip = false;
730
        boolean withInlineData = true;
731
        
732
        try {
733
            String[] docs = new String[0];
734
            String docid = "";
735
            String qformat = "";
736
            
737
            // read the params
738
            if (params.containsKey("docid")) {
739
                docs = params.get("docid");
740
            }
741
            if (params.containsKey("qformat")) {
742
                qformat = params.get("qformat")[0];
743
            }
744
            // the param for only metadata (eml)
745
            // we don't support read a eml document without inline data now.
746
            /*if (params.containsKey("inlinedata")) {
747
             
748
                String inlineData = ((String[]) params.get("inlinedata"))[0];
749
                if (inlineData.equalsIgnoreCase("false")) {
750
                    withInlineData = false;
751
                }
752
            }*/
753
            if ((docs.length > 1) || qformat.equals("zip")) {
754
                zip = true;
755
                out = response.getOutputStream();
756
                response.setContentType("application/zip"); //MIME type
757
                zout = new ZipOutputStream(out);
758
            }
759
            // go through the list of docs to read
760
            for (int i = 0; i < docs.length; i++) {
761
                String providedFileName = null;
762
                if (params.containsKey(docs[i])) {
763
                    providedFileName = params.get(docs[i])[0];
764
                }
765
                try {
766
                    
767
                    URL murl = new URL(docs[i]);
768
                    Hashtable<String,String> murlQueryStr = MetacatUtil.parseQuery(
769
                            murl.getQuery());
770
                    // case docid="http://.../?docid=aaa"
771
                    // or docid="metacat://.../?docid=bbb"
772
                    if (murlQueryStr.containsKey("docid")) {
773
                        // get only docid, eliminate the rest
774
                        docid = murlQueryStr.get("docid");
775
                        if (zip) {
776
                            addDocToZip(request, docid, providedFileName, zout, user, groups);
777
                        } else {
778
                            readFromMetacat(request.getRemoteAddr(), response, response.getOutputStream(), docid, qformat,
779
                                    user, groups, withInlineData, params);
780
                        }
781
                        
782
                        // case docid="http://.../filename"
783
                    } else {
784
                        docid = docs[i];
785
                        if (zip) {
786
                            addDocToZip(request, docid, providedFileName, zout, user, groups);
787
                        } else {
788
                            readFromURLConnection(response, docid);
789
                        }
790
                    }
791
                    
792
                } catch (MalformedURLException mue) {
793
                    docid = docs[i];
794
                    if (zip) {
795
                        addDocToZip(request, docid, providedFileName, zout, user, groups);
796
                    } else {
797
                    	if (out == null) {
798
                    		out = response.getOutputStream();
799
                    	}
800
                        readFromMetacat(request.getRemoteAddr(), response, out, docid, qformat,
801
                                user, groups, withInlineData, params);
802
                    }
803
                }
804
            }
805
            
806
            if (zip) {
807
                zout.finish(); //terminate the zip file
808
                zout.close(); //close the zip stream
809
            }
810
            
811
        } catch (McdbDocNotFoundException notFoundE) {
812
            // To handle doc not found exception
813
            // the docid which didn't be found
814
            String notFoundDocId = notFoundE.getUnfoundDocId();
815
            String notFoundRevision = notFoundE.getUnfoundRevision();
816
            logMetacat.warn("MetaCatServlet.handleReadAction - Missed id: " + notFoundDocId);
817
            logMetacat.warn("MetaCatServlet.handleReadAction - Missed rev: " + notFoundRevision);
818
            try {
819
                // read docid from remote server
820
                readFromRemoteMetaCat(response, notFoundDocId,
821
                        notFoundRevision, user, passWord, out, zip, zout);
822
                // Close zout outputstream
823
                if (zout != null) {
824
                    zout.close();
825
                }
826
                // close output stream
827
                if (out != null) {
828
                    out.close();
829
                }
830
                
831
            } catch (Exception exc) {
832
                logMetacat.error("MetaCatServlet.handleReadAction - General error: "
833
                        + exc.getMessage());
834
                exc.printStackTrace(System.out);
835
                try {
836
                    if (out != null) {
837
                        response.setContentType("text/xml");
838
                        // Send back error message by printWriter
839
                        pw = new PrintWriter(out);
840
                        pw.println("<?xml version=\"1.0\"?>");
841
                        pw.println("<error>");
842
                        pw.println(notFoundE.getMessage());
843
                        pw.println("</error>");
844
                        pw.close();
845
                        out.close();
846
                        
847
                    } else {
848
                        response.setContentType("text/xml"); //MIME type
849
                        // Send back error message if out = null
850
                        if (pw == null) {
851
                            // If pw is null, open the respnose
852
                            pw = response.getWriter();
853
                        }
854
                        pw.println("<?xml version=\"1.0\"?>");
855
                        pw.println("<error>");
856
                        pw.println(notFoundE.getMessage());
857
                        pw.println("</error>");
858
                        pw.close();
859
                    }
860
                    // close zout
861
                    if (zout != null) {
862
                        zout.close();
863
                    }
864
                } catch (IOException ie) {
865
                    logMetacat.error("MetaCatServlet.handleReadAction - Problem with the servlet output: "
866
                            + ie.getMessage());
867
                    ie.printStackTrace(System.out);
868
                }
869
            }
870
        } catch (Exception e) {
871
            try {
872
                
873
                if (out != null) {
874
                    response.setContentType("text/xml"); //MIME type
875
                    pw = new PrintWriter(out);
876
                    pw.println("<?xml version=\"1.0\"?>");
877
                    pw.println("<error>");
878
                    pw.println(e.getMessage());
879
                    pw.println("</error>");
880
                    pw.close();
881
                    out.close();
882
                } else {
883
                    response.setContentType("text/xml"); //MIME type
884
                    // Send back error message if out = null
885
                    if (pw == null) {
886
                        pw = response.getWriter();
887
                    }
888
                    pw.println("<?xml version=\"1.0\"?>");
889
                    pw.println("<error>");
890
                    pw.println(e.getMessage());
891
                    pw.println("</error>");
892
                    pw.close();
893
                    
894
                }
895
                // Close zip output stream
896
                if (zout != null) {
897
                    zout.close();
898
                }
899
                
900
            } catch (Exception e2) {
901
                logMetacat.error("MetaCatServlet.handleReadAction - Problem with the servlet output: "
902
                        + e2.getMessage());
903
                e2.printStackTrace(System.out);
904
                
905
            }
906
            
907
            logMetacat.error("MetaCatServlet.handleReadAction - General error: "
908
                    + e.getMessage());
909
            e.printStackTrace(System.out);
910
        }
911
    }
912
    
913
    /**
914
     * 
915
     * @return
916
     */
917
    public MetacatResultSet query(String metacatUrl, Hashtable<String, String[]>params, 
918
            String username, String[] groups, String sessionid)
919
      throws Exception
920
    {
921
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
922
        PrintWriter out = new PrintWriter(baos);
923
        handleQuery(out, params, null, username, groups, sessionid);
924
        out.flush();
925
        baos.flush();
926
        //baos.close(); 
927
        //System.out.println("result from query: " + baos.toString());
928
        MetacatResultSet rs = new MetacatResultSet(baos.toString());
929
        return rs;
930
    }
931
    
932
    /**
933
     * set the access permissions on the document specified
934
     */
935
    public void setAccess(String metacatUrl, String username, String docid, String principal, 
936
            String permission, String permissionType, String permissionOrder)
937
      throws Exception
938
    {
939
        Hashtable<String,String[]> params = new Hashtable<String,String[]>();
940
        params.put("principal", new String[] {principal});
941
        params.put("permission", new String[] {permission});
942
        params.put("permType", new String[] {permissionType});
943
        params.put("permOrder", new String[] {permissionOrder});
944
        params.put("docid", new String[]{docid});
945
        
946
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
947
        PrintWriter out = new PrintWriter(baos);
948
        handleSetAccessAction(out, params, username, null, null);
949
        String resp = baos.toString();
950
        System.out.println("response from MetacatHandler.setAccess: " + resp);
951
    }
952
    
953
    /** read metadata or data from Metacat
954
     * @throws PropertyNotFoundException 
955
     * @throws ParseLSIDException 
956
     * @throws InsufficientKarmaException 
957
     */
958
    public void readFromMetacat(String ipAddress,
959
            HttpServletResponse response, OutputStream out, String docid, String qformat,
960
            String user, String[] groups, boolean withInlineData, 
961
            Hashtable<String, String[]> params) throws ClassNotFoundException, 
962
            IOException, SQLException, McdbException, PropertyNotFoundException, 
963
            ParseLSIDException, InsufficientKarmaException {
964
        
965
        Logger logMetacat = Logger.getLogger(MetacatHandler.class);
966
        try {
967
            
968
            if (docid.startsWith("urn:")) {
969
                docid = LSIDUtil.getDocId(docid, true);                 
970
            }
971
            
972
            // here is hack for handle docid=john.10(in order to tell mike.jim.10.1
973
            // mike.jim.10, we require to provide entire docid with rev). But
974
            // some old client they only provide docid without rev, so we need
975
            // to handle this suituation. First we will check how many
976
            // seperator here, if only one, we will append the rev in xml_documents
977
            // to the id.
978
            docid = appendRev(docid);
979
            
980
            DocumentImpl doc = new DocumentImpl(docid);
981
            
982
            //check the permission for read
983
            if (!DocumentImpl.hasReadPermission(user, groups, docid)) {
984
                InsufficientKarmaException e = new InsufficientKarmaException("User " + user
985
                        + " does not have permission"
986
                        + " to read the document with the docid " + docid);
987
                throw e;
988
            }
989
            
990
            if (doc.getRootNodeID() == 0) {
991
                // this is data file, so find the path on disk for the file
992
                String filepath = PropertyService.getProperty("application.datafilepath");
993
                if (!filepath.endsWith("/")) {
994
                    filepath += "/";
995
                }
996
                String filename = filepath + docid;
997
                FileInputStream fin = null;
998
                fin = new FileInputStream(filename);
999
                
1000
                if (response != null) {
1001
                    // MIME type
1002
                    //String contentType = servletContext.getMimeType(filename);
1003
                    String contentType = (new MimetypesFileTypeMap()).getContentType(filename);
1004
                    if (contentType == null) {
1005
                        ContentTypeProvider provider = new ContentTypeProvider(
1006
                                docid);
1007
                        contentType = provider.getContentType();
1008
                        logMetacat.info("MetaCatServlet.readFromMetacat - Final contenttype is: "
1009
                                + contentType);
1010
                    }
1011
                    response.setContentType(contentType);
1012

    
1013
                    // Set the output filename on the response
1014
                    String outputname = generateOutputName(docid, params, doc);                    
1015
                    response.setHeader("Content-Disposition",
1016
                            "attachment; filename=\"" + outputname + "\"");
1017
                }
1018
                
1019
                // Write the data to the output stream
1020
                try {
1021
                    byte[] buf = new byte[4 * 1024]; // 4K buffer
1022
                    int b = fin.read(buf);
1023
                    while (b != -1) {
1024
                        out.write(buf, 0, b);
1025
                        b = fin.read(buf);
1026
                    }
1027
                } finally {
1028
                    if (fin != null) fin.close();
1029
                }
1030
                
1031
            } else {
1032
                // this is metadata doc
1033
                if (qformat.equals("xml") || qformat.equals("")) {
1034
                    // if equals "", that means no qformat is specified. hence
1035
                    // by default the document should be returned in xml format
1036
                    // set content type first
1037
                    
1038
                    if (response != null) {
1039
                        response.setContentType("text/xml"); //MIME type
1040
                        response.setHeader("Content-Disposition",
1041
                                "attachment; filename=" + docid + ".xml");
1042
                    }
1043
                    
1044
                    // Try to get the metadata file from disk. If it isn't
1045
                    // found, create it from the db and write it to disk then.
1046
                    try {
1047
                        PrintWriter pw = new PrintWriter(out);
1048
                        doc.toXml(pw, user, groups, withInlineData);               
1049
                    } catch (McdbException e) {
1050
                        // any exceptions in reading the xml from disc, and we go back to the
1051
                        // old way of creating the xml directly.
1052
                        logMetacat.error("MetaCatServlet.readFromMetacat - could not read from document file " + docid 
1053
                                + ": " + e.getMessage());
1054
                        e.printStackTrace(System.out);
1055
                        PrintWriter pw = new PrintWriter(out);
1056
                        doc.toXmlFromDb(pw, user, groups, withInlineData);
1057
                    }
1058
                } else {
1059
                    // TODO MCD, this should read from disk as well?
1060
                    //*** This is a metadata doc, to be returned in a skin/custom format.
1061
                    //*** Add param to indicate if public has read access or not.
1062
                    logMetacat.debug("User: \n" + user);
1063
                    if (!user.equals("public")) {
1064
                        if (DocumentImpl.hasReadPermission("public", null, docid))
1065
                            params.put("publicRead", new String[] {"true"});
1066
                        else
1067
                            params.put("publicRead", new String[] {"false"});
1068
                    }
1069
                    
1070
                    if (response != null) {
1071
                        response.setContentType("text/html"); //MIME type
1072
                    }
1073
                    
1074
                    PrintWriter pw = new PrintWriter(out);
1075
                    
1076
                    // Look up the document type
1077
                    String doctype = doc.getDoctype();
1078
                    // Transform the document to the new doctype
1079
                    DBTransform dbt = new DBTransform();
1080
                    dbt.transformXMLDocument(doc.toString(user, groups,
1081
                            withInlineData), doctype, "-//W3C//HTML//EN",
1082
                            qformat, pw, params, null);
1083
                }
1084
                
1085
            }
1086
            EventLog.getInstance().log(ipAddress, user, docid, "read");
1087
        } catch (PropertyNotFoundException except) {
1088
            throw except;
1089
        }
1090
    }
1091

    
1092
    /**
1093
     * Create a filename to be used for naming a downloaded document
1094
     * @param docid the identifier of the document to be named
1095
     * @param params the parameters of the request
1096
     * @param doc the DocumentImpl of the document to be named
1097
     * @return String containing a name for the download
1098
     */
1099
    private String generateOutputName(String docid,
1100
            Hashtable<String, String[]> params, DocumentImpl doc) {
1101
        String outputname = null;
1102
        // check for the existence of a metadatadocid parameter,
1103
        // if this is sent, then send a filename which contains both
1104
        // the metadata docid and the data docid, so the link with
1105
        // metadata is explicitly encoded in the filename.
1106
        String metadatadocid = null;
1107
        Vector<String> nameparts = new Vector<String>();
1108

    
1109
        if(params.containsKey("metadatadocid")) {
1110
            metadatadocid = params.get("metadatadocid")[0];
1111
        }
1112
        if (metadatadocid != null && !metadatadocid.equals("")) {
1113
            nameparts.add(metadatadocid);
1114
        }
1115
        // we'll always have the docid, include it in the name
1116
        String doctype = doc.getDoctype();
1117
        // TODO: fix this to lookup the associated FGDC metadata document,
1118
        // and grab the doctype tag for it.  These should be set to something 
1119
        // consistent, not 'metadata' as it stands...
1120
        //if (!doctype.equals("metadata")) {
1121
        //    nameparts.add(docid);
1122
        //} 
1123
        nameparts.add(docid);
1124
        // Set the name of the data file to the entity name plus docid,
1125
        // or if that is unavailable, use the docid alone
1126
        String docname = doc.getDocname();
1127
        if (docname != null && !docname.equals("")) {
1128
            nameparts.add(docname); 
1129
        }
1130
        // combine the name elements with a dash, using a 'join' equivalent
1131
        String delimiter = "-";
1132
        Iterator<String> iter = nameparts.iterator();
1133
        StringBuffer buffer = new StringBuffer(iter.next());
1134
        while (iter.hasNext()) buffer.append(delimiter).append(iter.next());
1135
        outputname = buffer.toString();
1136
        return outputname;
1137
    }
1138
    
1139
    /**
1140
     * read data from URLConnection
1141
     */
1142
    private void readFromURLConnection(HttpServletResponse response,
1143
            String docid) throws IOException, MalformedURLException {
1144
        ServletOutputStream out = response.getOutputStream();
1145
        //String contentType = servletContext.getMimeType(docid); //MIME type
1146
        String contentType = (new MimetypesFileTypeMap()).getContentType(docid);
1147
        if (contentType == null) {
1148
            if (docid.endsWith(".xml")) {
1149
                contentType = "text/xml";
1150
            } else if (docid.endsWith(".css")) {
1151
                contentType = "text/css";
1152
            } else if (docid.endsWith(".dtd")) {
1153
                contentType = "text/plain";
1154
            } else if (docid.endsWith(".xsd")) {
1155
                contentType = "text/xml";
1156
            } else if (docid.endsWith("/")) {
1157
                contentType = "text/html";
1158
            } else {
1159
                File f = new File(docid);
1160
                if (f.isDirectory()) {
1161
                    contentType = "text/html";
1162
                } else {
1163
                    contentType = "application/octet-stream";
1164
                }
1165
            }
1166
        }
1167
        response.setContentType(contentType);
1168
        // if we decide to use "application/octet-stream" for all data returns
1169
        // response.setContentType("application/octet-stream");
1170
        
1171
        // this is http url
1172
        URL url = new URL(docid);
1173
        BufferedInputStream bis = null;
1174
        try {
1175
            bis = new BufferedInputStream(url.openStream());
1176
            byte[] buf = new byte[4 * 1024]; // 4K buffer
1177
            int b = bis.read(buf);
1178
            while (b != -1) {
1179
                out.write(buf, 0, b);
1180
                b = bis.read(buf);
1181
            }
1182
        } finally {
1183
            if (bis != null) bis.close();
1184
        }
1185
        
1186
    }
1187
    
1188
    /**
1189
     * read file/doc and write to ZipOutputStream
1190
     *
1191
     * @param docid
1192
     * @param zout
1193
     * @param user
1194
     * @param groups
1195
     * @throws ClassNotFoundException
1196
     * @throws IOException
1197
     * @throws SQLException
1198
     * @throws McdbException
1199
     * @throws Exception
1200
     */
1201
    private void addDocToZip(HttpServletRequest request, String docid, String providedFileName,
1202
            ZipOutputStream zout, String user, String[] groups) throws
1203
            ClassNotFoundException, IOException, SQLException, McdbException,
1204
            Exception {
1205
        byte[] bytestring = null;
1206
        ZipEntry zentry = null;
1207
        
1208
        try {
1209
            URL url = new URL(docid);
1210
            
1211
            // this http url; read from URLConnection; add to zip
1212
            //use provided file name if we have one
1213
            if (providedFileName != null && providedFileName.length() > 1) {
1214
                zentry = new ZipEntry(providedFileName);
1215
            }
1216
            else {
1217
                zentry = new ZipEntry(docid);
1218
            }
1219
            zout.putNextEntry(zentry);
1220
            BufferedInputStream bis = null;
1221
            try {
1222
                bis = new BufferedInputStream(url.openStream());
1223
                byte[] buf = new byte[4 * 1024]; // 4K buffer
1224
                int b = bis.read(buf);
1225
                while (b != -1) {
1226
                    zout.write(buf, 0, b);
1227
                    b = bis.read(buf);
1228
                }
1229
            } finally {
1230
                if (bis != null) bis.close();
1231
            }
1232
            zout.closeEntry();
1233
            
1234
        } catch (MalformedURLException mue) {
1235
            
1236
            // this is metacat doc (data file or metadata doc)
1237
            try {
1238
                DocumentImpl doc = new DocumentImpl(docid);
1239
                
1240
                //check the permission for read
1241
                if (!DocumentImpl.hasReadPermission(user, groups, docid)) {
1242
                    Exception e = new Exception("User " + user
1243
                            + " does not have "
1244
                            + "permission to read the document with the docid "
1245
                            + docid);
1246
                    throw e;
1247
                }
1248
                
1249
                if (doc.getRootNodeID() == 0) {
1250
                    // this is data file; add file to zip
1251
                    String filepath = PropertyService.getProperty("application.datafilepath");
1252
                    if (!filepath.endsWith("/")) {
1253
                        filepath += "/";
1254
                    }
1255
                    String filename = filepath + docid;
1256
                    FileInputStream fin = null;
1257
                    fin = new FileInputStream(filename);
1258
                    try {
1259
                        //use provided file name if we have one
1260
                        if (providedFileName != null && providedFileName.length() > 1) {
1261
                            zentry = new ZipEntry(providedFileName);
1262
                        }
1263
                        else {
1264
                            zentry = new ZipEntry(docid);
1265
                        }
1266
                        zout.putNextEntry(zentry);
1267
                        byte[] buf = new byte[4 * 1024]; // 4K buffer
1268
                        int b = fin.read(buf);
1269
                        while (b != -1) {
1270
                            zout.write(buf, 0, b);
1271
                            b = fin.read(buf);
1272
                        }
1273
                    } finally {
1274
                        if (fin != null) fin.close();
1275
                    }
1276
                    zout.closeEntry();
1277
                    
1278
                } else {
1279
                    // this is metadata doc; add doc to zip
1280
                    bytestring = doc.toString().getBytes();
1281
                    //use provided file name if given
1282
                    if (providedFileName != null && providedFileName.length() > 1) {
1283
                        zentry = new ZipEntry(providedFileName);
1284
                    }
1285
                    else {
1286
                        zentry = new ZipEntry(docid + ".xml");
1287
                    }
1288
                    zentry.setSize(bytestring.length);
1289
                    zout.putNextEntry(zentry);
1290
                    zout.write(bytestring, 0, bytestring.length);
1291
                    zout.closeEntry();
1292
                }
1293
                EventLog.getInstance().log(request.getRemoteAddr(), user,
1294
                        docid, "read");
1295
            } catch (Exception except) {
1296
                throw except;
1297
            }
1298
        }
1299
    }
1300
    
1301
    /**
1302
     * If metacat couldn't find a data file or document locally, it will read
1303
     * this docid from its home server. This is for the replication feature
1304
     */
1305
    private void readFromRemoteMetaCat(HttpServletResponse response,
1306
            String docid, String rev, String user, String password,
1307
            ServletOutputStream out, boolean zip, ZipOutputStream zout)
1308
            throws Exception {
1309
        // Create a object of RemoteDocument, "" is for zipEntryPath
1310
        RemoteDocument remoteDoc = new RemoteDocument(docid, rev, user,
1311
                password, "");
1312
        String docType = remoteDoc.getDocType();
1313
        // Only read data file
1314
        if (docType.equals("BIN")) {
1315
            // If it is zip format
1316
            if (zip) {
1317
                remoteDoc.readDocumentFromRemoteServerByZip(zout);
1318
            } else {
1319
                if (out == null) {
1320
                    out = response.getOutputStream();
1321
                }
1322
                response.setContentType("application/octet-stream");
1323
                remoteDoc.readDocumentFromRemoteServer(out);
1324
            }
1325
        } else {
1326
            throw new Exception("Docid: " + docid + "." + rev
1327
                    + " couldn't find");
1328
        }
1329
    }
1330
    
1331
    /**
1332
     * Handle the database putdocument request and write an XML document to the
1333
     * database connection
1334
     */
1335
    public String handleInsertOrUpdateAction(String ipAddress,
1336
            HttpServletResponse response, PrintWriter out, Hashtable<String, String[]> params,
1337
            String user, String[] groups) {
1338
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
1339
        DBConnection dbConn = null;
1340
        int serialNumber = -1;
1341
        String output = "";
1342
        String qformat = null;
1343
        if(params.containsKey("qformat"))
1344
        {
1345
          qformat = params.get("qformat")[0];
1346
        }
1347
        
1348
        if(params.get("docid") == null){
1349
            String msg = "<?xml version=\"1.0\"?>" +
1350
                "<error>" +
1351
                "Docid not specified" +
1352
                "</error>";
1353
            if(out != null)
1354
            {
1355
                out.println(msg);
1356
                logMetacat.error("MetaCatServlet.handleInsertOrUpdateAction - Docid not specified");
1357
            }
1358
            return msg; 
1359
        }
1360
        
1361
        try {
1362
            if (!AuthUtil.canInsertOrUpdate(user, groups)) {
1363
                String msg = "<?xml version=\"1.0\"?>" +
1364
                "<error>" +
1365
                "User '" + user + "' not allowed to insert and update" +
1366
                "</error>";
1367
                if(out != null)
1368
                {
1369
                  out.println(msg);
1370
                }
1371
                
1372
                logMetacat.error("MetaCatServlet.handleInsertOrUpdateAction - User '" + user + "' not allowed to insert and update");
1373
                return msg;
1374
            }
1375
        } catch (MetacatUtilException ue) {
1376
            logMetacat.error("MetaCatServlet.handleInsertOrUpdateAction - Could not determine if user could insert or update: "
1377
                    + ue.getMessage());
1378
            ue.printStackTrace(System.out);
1379
            // TODO: This is a bug, as it allows one to bypass the access check -- need to throw an exception
1380
        }
1381
        
1382
        try {
1383
            // Get the document indicated
1384
            logMetacat.debug("MetaCatServlet.handleInsertOrUpdateAction - params: " + params.toString());
1385
            
1386
            String[] doctext = params.get("doctext");
1387
            String pub = null;
1388
            if (params.containsKey("public")) {
1389
                pub = params.get("public")[0];
1390
            }
1391
            
1392
            StringReader dtd = null;
1393
            if (params.containsKey("dtdtext")) {
1394
                String[] dtdtext = params.get("dtdtext");
1395
                try {
1396
                    if (!dtdtext[0].equals("")) {
1397
                        dtd = new StringReader(dtdtext[0]);
1398
                    }
1399
                } catch (NullPointerException npe) {
1400
                }
1401
            }
1402
            
1403
            if(doctext == null){
1404
                String msg = "<?xml version=\"1.0\"?>" +
1405
                "<error>" +
1406
                "Document text not submitted." +
1407
                "</error>";
1408
                if(out != null)
1409
                {
1410
                  out.println(msg);
1411
                }
1412
                
1413
                // TODO: this should really throw an exception
1414
                return msg;
1415
            }
1416
            
1417
            logMetacat.debug("MetaCatServlet.handleInsertOrUpdateAction - the xml document in metacat servlet (before parsing):\n" + doctext[0]);
1418
            StringReader xmlReader = new StringReader(doctext[0]);
1419
            boolean validate = false;
1420
            DocumentImplWrapper documentWrapper = null;
1421
            try {
1422
                // look inside XML Document for <!DOCTYPE ... PUBLIC/SYSTEM ...
1423
                // >
1424
                // in order to decide whether to use validation parser
1425
                validate = needDTDValidation(xmlReader);
1426
                if (validate) {
1427
                    // set a dtd base validation parser
1428
                    String rule = DocumentImpl.DTD;
1429
                    documentWrapper = new DocumentImplWrapper(rule, validate);
1430
                } else {
1431
                    
1432
                    String namespace = XMLSchemaService.findDocumentNamespace(xmlReader);
1433
                    
1434
                    if (namespace != null) {
1435
                        if (namespace.compareTo(DocumentImpl.EML2_0_0NAMESPACE) == 0
1436
                                || namespace.compareTo(
1437
                                DocumentImpl.EML2_0_1NAMESPACE) == 0) {
1438
                            // set eml2 base     validation parser
1439
                            String rule = DocumentImpl.EML200;
1440
                            // using emlparser to check id validation
1441
                            @SuppressWarnings("unused")
1442
                            EMLParser parser = new EMLParser(doctext[0]);
1443
                            documentWrapper = new DocumentImplWrapper(rule, true);
1444
                        } else if (namespace.compareTo(
1445
                                DocumentImpl.EML2_1_0NAMESPACE) == 0) {
1446
                            // set eml2 base validation parser
1447
                            String rule = DocumentImpl.EML210;
1448
                            // using emlparser to check id validation
1449
                            @SuppressWarnings("unused")
1450
                            EMLParser parser = new EMLParser(doctext[0]);
1451
                            documentWrapper = new DocumentImplWrapper(rule, true);
1452
                        } else {
1453
                            // set schema base validation parser
1454
                            String rule = DocumentImpl.SCHEMA;
1455
                            documentWrapper = new DocumentImplWrapper(rule, true);
1456
                        }
1457
                    } else {
1458
                        documentWrapper = new DocumentImplWrapper("", false);
1459
                    }
1460
                }
1461
                
1462
                String[] action = params.get("action");
1463
                String[] docid = params.get("docid");
1464
                String newdocid = null;
1465
                
1466
                String doAction = null;
1467
                if (action[0].equals("insert") || action[0].equals("insertmultipart")) {
1468
                    doAction = "INSERT";
1469
                } else if (action[0].equals("update")) {
1470
                    doAction = "UPDATE";
1471
                }
1472
                
1473
                try {
1474
                    // get a connection from the pool
1475
                    dbConn = DBConnectionPool
1476
                            .getDBConnection("MetaCatServlet.handleInsertOrUpdateAction");
1477
                    serialNumber = dbConn.getCheckOutSerialNumber();
1478
                    
1479
                    // write the document to the database and disk
1480
                    String accNumber = docid[0];
1481
                    logMetacat.debug("MetaCatServlet.handleInsertOrUpdateAction - " + doAction + " "
1482
                            + accNumber + "...");
1483
                    if (accNumber == null || accNumber.equals("")) {
1484
                        logMetacat.warn("MetaCatServlet.handleInsertOrUpdateAction - writing with null acnumber");
1485
                        newdocid = documentWrapper.write(dbConn, doctext[0], pub, dtd,
1486
                                doAction, null, user, groups);
1487
                        EventLog.getInstance().log(ipAddress, user, "", action[0]);
1488
                    } else {
1489
                        newdocid = documentWrapper.write(dbConn, doctext[0], pub, dtd,
1490
                                doAction, accNumber, user, groups);
1491

    
1492
                        EventLog.getInstance().log(ipAddress, user, accNumber, action[0]);
1493
                    }
1494
                } finally {
1495
                    // Return db connection
1496
                    DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1497
                }
1498
                
1499
                // set content type and other response header fields first
1500
                //response.setContentType("text/xml");
1501
                output += "<?xml version=\"1.0\"?>";
1502
                output += "<success>";
1503
                output += "<docid>" + newdocid + "</docid>";
1504
                output += "</success>";
1505
                
1506
            } catch (NullPointerException npe) {
1507
                //response.setContentType("text/xml");
1508
                output += "<?xml version=\"1.0\"?>";
1509
                output += "<error>";
1510
                output += npe.getMessage();
1511
                output += "</error>";
1512
                logMetacat.warn("MetaCatServlet.handleInsertOrUpdateAction - Null pointer error when writing eml document to the database: " + npe.getMessage());
1513
                npe.printStackTrace();
1514
            }
1515
        } catch (Exception e) {
1516
            //response.setContentType("text/xml");
1517
            output += "<?xml version=\"1.0\"?>";
1518
            output += "<error>";
1519
            output += e.getMessage();
1520
            output += "</error>";
1521
            logMetacat.warn("MetaCatServlet.handleInsertOrUpdateAction - General error when writing eml document to the database: " + e.getMessage());
1522
            e.printStackTrace();
1523
        }
1524
        
1525
        if (qformat == null || qformat.equals("xml")) {
1526
            if(response != null)
1527
            {
1528
              response.setContentType("text/xml");
1529
              out.println(output);
1530
            }
1531
            return output;
1532
        } else {
1533
            try {
1534
                DBTransform trans = new DBTransform();
1535
                response.setContentType("text/html");
1536
                trans.transformXMLDocument(output,
1537
                        "message", "-//W3C//HTML//EN", qformat,
1538
                        out, null, null);
1539
            } catch (Exception e) {
1540
                
1541
                logMetacat.error("MetaCatServlet.handleInsertOrUpdateAction - General error: "
1542
                        + e.getMessage());
1543
                e.printStackTrace(System.out);
1544
            }
1545
        }
1546
        return null;
1547
    }
1548
    
1549
    /**
1550
     * Parse XML Document to look for <!DOCTYPE ... PUBLIC/SYSTEM ... > in
1551
     * order to decide whether to use validation parser
1552
     */
1553
    private static boolean needDTDValidation(StringReader xmlreader)
1554
    throws IOException {
1555
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
1556
        StringBuffer cbuff = new StringBuffer();
1557
        java.util.Stack<String> st = new java.util.Stack<String>();
1558
        boolean validate = false;
1559
        boolean commented = false;
1560
        int c;
1561
        int inx;
1562
        
1563
        // read from the stream until find the keywords
1564
        while ((st.empty() || st.size() < 4) && ((c = xmlreader.read()) != -1)) {
1565
            cbuff.append((char) c);
1566
            
1567
            if ((inx = cbuff.toString().indexOf("<!--")) != -1) {
1568
                commented = true;
1569
            }
1570
            
1571
            // "<!DOCTYPE" keyword is found; put it in the stack
1572
            if ((inx = cbuff.toString().indexOf("<!DOCTYPE")) != -1) {
1573
                cbuff = new StringBuffer();
1574
                st.push("<!DOCTYPE");
1575
            }
1576
            // "PUBLIC" keyword is found; put it in the stack
1577
            if ((inx = cbuff.toString().indexOf("PUBLIC")) != -1) {
1578
                cbuff = new StringBuffer();
1579
                st.push("PUBLIC");
1580
            }
1581
            // "SYSTEM" keyword is found; put it in the stack
1582
            if ((inx = cbuff.toString().indexOf("SYSTEM")) != -1) {
1583
                cbuff = new StringBuffer();
1584
                st.push("SYSTEM");
1585
            }
1586
            // ">" character is found; put it in the stack
1587
            // ">" is found twice: fisrt from <?xml ...?>
1588
            // and second from <!DOCTYPE ... >
1589
            if ((inx = cbuff.toString().indexOf(">")) != -1) {
1590
                cbuff = new StringBuffer();
1591
                st.push(">");
1592
            }
1593
        }
1594
        
1595
        // close the stream
1596
        xmlreader.reset();
1597
        
1598
        // check the stack whether it contains the keywords:
1599
        // "<!DOCTYPE", "PUBLIC" or "SYSTEM", and ">" in this order
1600
        if (st.size() == 4) {
1601
            if ((st.pop()).equals(">")
1602
            && ((st.peek()).equals("PUBLIC") | (st.pop()).equals("SYSTEM"))
1603
                    && (st.pop()).equals("<!DOCTYPE")) {
1604
                validate = true && !commented;
1605
            }
1606
        }
1607
        
1608
        logMetacat.info("MetaCatServlet.needDTDValidation - Validation for dtd is " + validate);
1609
        return validate;
1610
    }
1611
    
1612
    // END OF INSERT/UPDATE SECTION
1613
    
1614
    /**
1615
     * Handle the database delete request and delete an XML document from the
1616
     * database connection
1617
     */
1618
    public void handleDeleteAction(PrintWriter out, Hashtable<String, String[]> params,
1619
            HttpServletRequest request, HttpServletResponse response,
1620
            String user, String[] groups) {
1621
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
1622
        String[] docid = params.get("docid");
1623
        
1624
        if(docid == null){
1625
            response.setContentType("text/xml");
1626
            out.println("<?xml version=\"1.0\"?>");
1627
            out.println("<error>");
1628
            out.println("Docid not specified.");
1629
            out.println("</error>");
1630
            logMetacat.error("MetaCatServlet.handleDeleteAction - Docid not specified for the document to be deleted.");
1631
        } else {
1632
            
1633
            // delete the document from the database
1634
            try {
1635
                
1636
                try {
1637
                    // null means notify server is null
1638
                    DocumentImpl.delete(docid[0], user, groups, null);
1639
                    EventLog.getInstance().log(request.getRemoteAddr(),
1640
                            user, docid[0], "delete");
1641
                    response.setContentType("text/xml");
1642
                    out.println("<?xml version=\"1.0\"?>");
1643
                    out.println("<success>");
1644
                    out.println("Document deleted.");
1645
                    out.println("</success>");
1646
                    logMetacat.info("MetaCatServlet.handleDeleteAction - Document deleted.");
1647
                    
1648
                    // Delete from spatial cache if runningSpatialOption
1649
                    if ( PropertyService.getProperty("spatial.runSpatialOption").equals("true") ) {
1650
                        SpatialHarvester sh = new SpatialHarvester();
1651
                        sh.addToDeleteQue( DocumentUtil.getSmartDocId( docid[0] ) );
1652
                        sh.destroy();
1653
                    }
1654
                    
1655
                } catch (AccessionNumberException ane) {
1656
                    response.setContentType("text/xml");
1657
                    out.println("<?xml version=\"1.0\"?>");
1658
                    out.println("<error>");
1659
                    //out.println("Error deleting document!!!");
1660
                    out.println(ane.getMessage());
1661
                    out.println("</error>");
1662
                    logMetacat.error("MetaCatServlet.handleDeleteAction - Document could not be deleted: "
1663
                            + ane.getMessage());
1664
                    ane.printStackTrace(System.out);
1665
                }
1666
            } catch (Exception e) {
1667
                response.setContentType("text/xml");
1668
                out.println("<?xml version=\"1.0\"?>");
1669
                out.println("<error>");
1670
                out.println(e.getMessage());
1671
                out.println("</error>");
1672
                logMetacat.error("MetaCatServlet.handleDeleteAction - Document could not be deleted: "
1673
                        + e.getMessage());
1674
                e.printStackTrace(System.out);
1675
            }
1676
        }
1677
    }
1678
    
1679
    /**
1680
     * Handle the validation request and return the results to the requestor
1681
     */
1682
    protected void handleValidateAction(PrintWriter out, Hashtable<String, String[]> params) {
1683
        
1684
        // Get the document indicated
1685
        String valtext = null;
1686
        DBConnection dbConn = null;
1687
        int serialNumber = -1;
1688
        
1689
        try {
1690
            valtext = params.get("valtext")[0];
1691
        } catch (Exception nullpe) {
1692
            
1693
            String docid = null;
1694
            try {
1695
                // Find the document id number
1696
                docid = params.get("docid")[0];
1697
                
1698
                // Get the document indicated from the db
1699
                DocumentImpl xmldoc = new DocumentImpl(docid);
1700
                valtext = xmldoc.toString();
1701
                
1702
            } catch (NullPointerException npe) {
1703
                
1704
                out.println("<error>Error getting document ID: " + docid
1705
                        + "</error>");
1706
                //if ( conn != null ) { util.returnConnection(conn); }
1707
                return;
1708
            } catch (Exception e) {
1709
                
1710
                out.println(e.getMessage());
1711
            }
1712
        }
1713
        
1714
        try {
1715
            // get a connection from the pool
1716
            dbConn = DBConnectionPool
1717
                    .getDBConnection("MetaCatServlet.handleValidateAction");
1718
            serialNumber = dbConn.getCheckOutSerialNumber();
1719
            DBValidate valobj = new DBValidate(dbConn);
1720
//            boolean valid = valobj.validateString(valtext);
1721
            
1722
            // set content type and other response header fields first
1723
            
1724
            out.println(valobj.returnErrors());
1725
            
1726
        } catch (NullPointerException npe2) {
1727
            // set content type and other response header fields first
1728
            
1729
            out.println("<error>Error validating document.</error>");
1730
        } catch (Exception e) {
1731
            
1732
            out.println(e.getMessage());
1733
        } finally {
1734
            // Return db connection
1735
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1736
        }
1737
    }
1738
    
1739
    /**
1740
     * Handle "getrevsionanddoctype" action Given a docid, return it's current
1741
     * revision and doctype from data base The output is String look like
1742
     * "rev;doctype"
1743
     */
1744
    protected void handleGetRevisionAndDocTypeAction(PrintWriter out,
1745
            Hashtable<String, String[]> params) {
1746
        // To store doc parameter
1747
        String[] docs = new String[10];
1748
        // Store a single doc id
1749
        String givenDocId = null;
1750
        // Get docid from parameters
1751
        if (params.containsKey("docid")) {
1752
            docs = params.get("docid");
1753
        }
1754
        // Get first docid form string array
1755
        givenDocId = docs[0];
1756
        
1757
        try {
1758
            // Make sure there is a docid
1759
            if (givenDocId == null || givenDocId.equals("")) { throw new Exception(
1760
                    "User didn't specify docid!"); }//if
1761
            
1762
            // Create a DBUtil object
1763
            DBUtil dbutil = new DBUtil();
1764
            // Get a rev and doctype
1765
            String revAndDocType = dbutil
1766
                    .getCurrentRevisionAndDocTypeForGivenDocument(givenDocId);
1767
            out.println(revAndDocType);
1768
            
1769
        } catch (Exception e) {
1770
            // Handle exception
1771
            out.println("<?xml version=\"1.0\"?>");
1772
            out.println("<error>");
1773
            out.println(e.getMessage());
1774
            out.println("</error>");
1775
        }
1776
        
1777
    }
1778
    
1779
    /**
1780
     * Handle "getaccesscontrol" action. Read Access Control List from db
1781
     * connection in XML format
1782
     */
1783
    protected void handleGetAccessControlAction(PrintWriter out,
1784
            Hashtable<String,String[]> params, HttpServletResponse response, String username,
1785
            String[] groupnames) {
1786
        
1787
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
1788

    
1789
        String docid = params.get("docid")[0];
1790
        if (docid.startsWith("urn:")) {
1791
            try {
1792
                String actualDocId = LSIDUtil.getDocId(docid, false);
1793
                docid = actualDocId;
1794
            } catch (ParseLSIDException ple) {
1795
                logMetacat.error("MetaCatServlet.handleGetAccessControlAction - MetaCatServlet.handleGetAccessControlAction - " +
1796
                        "could not parse lsid: " + docid + " : " + ple.getMessage());  
1797
                ple.printStackTrace(System.out);
1798
            }
1799
        }
1800
        
1801
        String qformat = "xml";
1802
        if (params.get("qformat") != null) {
1803
            qformat = (params.get("qformat"))[0];
1804
        }
1805
        
1806
        try {
1807
            AccessControlForSingleFile acfsf = new AccessControlForSingleFile(docid);
1808
            String acltext = acfsf.getACL(username, groupnames);
1809
            if (qformat.equals("xml")) {
1810
                response.setContentType("text/xml");
1811
                out.println(acltext);
1812
            } else {
1813
                DBTransform trans = new DBTransform();
1814
                response.setContentType("text/html");
1815
                trans.transformXMLDocument(acltext,"-//NCEAS//getaccesscontrol//EN", 
1816
                    "-//W3C//HTML//EN", qformat, out, params, null);              
1817
            }            
1818
        } catch (Exception e) {
1819
            out.println("<?xml version=\"1.0\"?>");
1820
            out.println("<error>");
1821
            out.println(e.getMessage());
1822
            out.println("</error>");
1823
        } 
1824
//        finally {
1825
//            // Retrun db connection to pool
1826
//            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1827
//        }
1828
    }
1829
    
1830
    /**
1831
     * Handle the "getprincipals" action. Read all principals from
1832
     * authentication scheme in XML format
1833
     */
1834
    protected void handleGetPrincipalsAction(PrintWriter out, String user,
1835
            String password) {
1836
        try {
1837
            AuthSession auth = new AuthSession();
1838
            String principals = auth.getPrincipals(user, password);
1839
            out.println(principals);
1840
            
1841
        } catch (Exception e) {
1842
            out.println("<?xml version=\"1.0\"?>");
1843
            out.println("<error>");
1844
            out.println(e.getMessage());
1845
            out.println("</error>");
1846
        }
1847
    }
1848
    
1849
    /**
1850
     * Handle "getdoctypes" action. Read all doctypes from db connection in XML
1851
     * format
1852
     */
1853
    protected void handleGetDoctypesAction(PrintWriter out, Hashtable<String, String[]> params,
1854
            HttpServletResponse response) {
1855
        try {
1856
            DBUtil dbutil = new DBUtil();
1857
            String doctypes = dbutil.readDoctypes();
1858
            out.println(doctypes);
1859
        } catch (Exception e) {
1860
            out.println("<?xml version=\"1.0\"?>");
1861
            out.println("<error>");
1862
            out.println(e.getMessage());
1863
            out.println("</error>");
1864
        }
1865
    }
1866
    
1867
    /**
1868
     * Handle the "getdtdschema" action. Read DTD or Schema file for a given
1869
     * doctype from Metacat catalog system
1870
     */
1871
    protected void handleGetDTDSchemaAction(PrintWriter out, Hashtable<String, String[]> params,
1872
            HttpServletResponse response) {
1873
        
1874
        String doctype = null;
1875
        String[] doctypeArr = params.get("doctype");
1876
        
1877
        // get only the first doctype specified in the list of doctypes
1878
        // it could be done for all doctypes in that list
1879
        if (doctypeArr != null) {
1880
            doctype = params.get("doctype")[0];
1881
        }
1882
        
1883
        try {
1884
            DBUtil dbutil = new DBUtil();
1885
            String dtdschema = dbutil.readDTDSchema(doctype);
1886
            out.println(dtdschema);
1887
            
1888
        } catch (Exception e) {
1889
            out.println("<?xml version=\"1.0\"?>");
1890
            out.println("<error>");
1891
            out.println(e.getMessage());
1892
            out.println("</error>");
1893
        }
1894
        
1895
    }
1896
    
1897
    /**
1898
     * Check if the document is registered in either the xml_documents or xml_revisions table
1899
     * @param out the writer to write the xml results to
1900
     * @param params request parameters
1901
     * @param response the http servlet response
1902
     */
1903
    public void handleIdIsRegisteredAction(PrintWriter out, Hashtable<String, String[]> params,
1904
            HttpServletResponse response) {
1905
        String id = null;
1906
        boolean exists = false;
1907
        if(params.get("docid") != null) {
1908
            id = params.get("docid")[0];
1909
        }
1910
        
1911
        try {
1912
            DBUtil dbutil = new DBUtil();
1913
            exists = dbutil.idExists(id);
1914
        } catch (Exception e) {
1915
            out.println("<?xml version=\"1.0\"?>");
1916
            out.println("<error>");
1917
            out.println(e.getMessage());
1918
            out.println("</error>");
1919
        }
1920
        
1921
        out.println("<?xml version=\"1.0\"?>");
1922
        out.println("<isRegistered>");
1923
        out.println("<docid>" + id + "</docid>");
1924
        out.println("<exists>" + exists + "</exists>");
1925
        out.println("</isRegistered>");
1926
    }
1927
    
1928
    /**
1929
     * Handle the "getalldocids" action. return a list of all docids registered
1930
     * in the system
1931
     */
1932
    public void handleGetAllDocidsAction(PrintWriter out, Hashtable<String, String[]> params,
1933
            HttpServletResponse response) {
1934
        String scope = null;
1935
        if(params.get("scope") != null) {
1936
            scope = params.get("scope")[0];
1937
        }
1938
        
1939
        try {
1940
            DBUtil dbutil = new DBUtil();
1941
            Vector<String> docids = dbutil.getAllDocids(scope);
1942
            out.println("<?xml version=\"1.0\"?>");
1943
            out.println("<idList>");
1944
            out.println("  <scope>" + scope + "</scope>");
1945
            for(int i=0; i<docids.size(); i++) {
1946
                String docid = docids.elementAt(i);
1947
                out.println("  <docid>" + docid + "</docid>");
1948
            }
1949
            out.println("</idList>");
1950
            
1951
        } catch (Exception e) {
1952
            out.println("<?xml version=\"1.0\"?>");
1953
            out.println("<error>");
1954
            out.println(e.getMessage());
1955
            out.println("</error>");
1956
        }
1957
    }
1958
    
1959
    /**
1960
     * Handle the "getlastdocid" action. Get the latest docid with rev number
1961
     * from db connection in XML format
1962
     */
1963
    public void handleGetMaxDocidAction(PrintWriter out, Hashtable<String, String[]> params,
1964
            HttpServletResponse response) {
1965
        
1966
        String scope = params.get("scope")[0];
1967
        if (scope == null) {
1968
            scope = params.get("username")[0];
1969
        }
1970
        
1971
        try {
1972
            
1973
            DBUtil dbutil = new DBUtil();
1974
            String lastDocid = dbutil.getMaxDocid(scope);
1975
            out.println("<?xml version=\"1.0\"?>");
1976
            out.println("<lastDocid>");
1977
            out.println("  <scope>" + scope + "</scope>");
1978
            out.println("  <docid>" + lastDocid + "</docid>");
1979
            out.println("</lastDocid>");
1980
            
1981
        } catch (Exception e) {
1982
            out.println("<?xml version=\"1.0\"?>");
1983
            out.println("<error>");
1984
            out.println(e.getMessage());
1985
            out.println("</error>");
1986
        }
1987
    }
1988
    
1989
    /**
1990
     * Print a report from the event log based on filter parameters passed in
1991
     * from the web.
1992
     *
1993
     * @param params the parameters from the web request
1994
     * @param request the http request object for getting request details
1995
     * @param response the http response object for writing output
1996
     */
1997
    protected void handleGetLogAction(Hashtable<String, String[]> params, HttpServletRequest request,
1998
            HttpServletResponse response, String username, String[] groups) {
1999
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2000
        try {
2001
            response.setContentType("text/xml");
2002
            PrintWriter out = response.getWriter();
2003
            
2004
            // Check that the user is authenticated as an administrator account
2005
            if (!AuthUtil.isAdministrator(username, groups)) {
2006
                out.print("<error>");
2007
                out.print("The user \"" + username +
2008
                        "\" is not authorized for this action.");
2009
                out.print("</error>");
2010
                return;
2011
            }
2012
            
2013
            // Get all of the parameters in the correct formats
2014
            String[] ipAddress = params.get("ipaddress");
2015
            String[] principal = params.get("principal");
2016
            String[] docid = params.get("docid");
2017
            String[] event = params.get("event");
2018
            String[] startArray = params.get("start");
2019
            String[] endArray = params.get("end");
2020
            String start = null;
2021
            String end = null;
2022
            if (startArray != null) {
2023
                start = startArray[0];
2024
            }
2025
            if (endArray != null) {
2026
                end = endArray[0];
2027
            }
2028
            Timestamp startDate = null;
2029
            Timestamp endDate = null;
2030
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
2031
            try {
2032
                if (start != null) {
2033
                    startDate = new Timestamp((format.parse(start)).getTime());
2034
                }
2035
                if (end != null) {
2036
                    endDate = new Timestamp((format.parse(end)).getTime());
2037
                }
2038
            } catch (ParseException e) {
2039
                logMetacat.error("MetaCatServlet.handleGetLogAction - Failed to created Timestamp from input.");
2040
                e.printStackTrace(System.out);
2041
            }
2042
            
2043
            // Request the report by passing the filter parameters
2044
            out.println(EventLog.getInstance().getReport(ipAddress, principal,
2045
                    docid, event, startDate, endDate));
2046
            out.close();
2047
        } catch (IOException e) {
2048
            logMetacat.error("MetaCatServlet.handleGetLogAction - Could not open http response for writing: "
2049
                    + e.getMessage());
2050
            e.printStackTrace(System.out);
2051
        } catch (MetacatUtilException ue) {
2052
            logMetacat.error("MetaCatServlet.handleGetLogAction - Could not determine if user is administrator: "
2053
                    + ue.getMessage());
2054
            ue.printStackTrace(System.out);
2055
        }
2056
    }
2057
    
2058
    /**
2059
     * Rebuild the index for one or more documents. If the docid parameter is
2060
     * provided, rebuild for just that one document or list of documents. If
2061
     * not, then rebuild the index for all documents in the xml_documents table.
2062
     * 
2063
     * @param params
2064
     *            the parameters from the web request
2065
     * @param request
2066
     *            the http request object for getting request details
2067
     * @param response
2068
     *            the http response object for writing output
2069
     * @param username
2070
     *            the username of the authenticated user
2071
     */
2072
    protected void handleBuildIndexAction(Hashtable<String, String[]> params,
2073
            HttpServletRequest request, HttpServletResponse response,
2074
            String username, String[] groups) {
2075
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2076
        
2077
        // Get all of the parameters in the correct formats
2078
        String[] docid = params.get("docid");
2079
        
2080
        // Rebuild the indices for appropriate documents
2081
        try {
2082
            response.setContentType("text/xml");
2083
            PrintWriter out = response.getWriter();
2084
            
2085
            // Check that the user is authenticated as an administrator account
2086
            if (!AuthUtil.isAdministrator(username, groups)) {
2087
                out.print("<error>");
2088
                out.print("The user \"" + username +
2089
                        "\" is not authorized for this action.");
2090
                out.print("</error>");
2091
                return;
2092
            }
2093
            
2094
            // Process the documents
2095
            out.println("<success>");
2096
            if (docid == null || docid.length == 0) {
2097
                // Process all of the documents
2098
                try {
2099
                    Vector<String> documents = getDocumentList();
2100
                    Iterator<String> it = documents.iterator();
2101
                    while (it.hasNext()) {
2102
                        String id = it.next();
2103
                        buildDocumentIndex(id, out);
2104
                    }
2105
                } catch (SQLException se) {
2106
                    out.print("<error>");
2107
                    out.print(se.getMessage());
2108
                    out.println("</error>");
2109
                }
2110
            } else {
2111
                // Only process the requested documents
2112
                for (int i = 0; i < docid.length; i++) {
2113
                    buildDocumentIndex(docid[i], out);
2114
                }
2115
            }
2116
            out.println("</success>");
2117
            out.close();
2118
        } catch (IOException e) {
2119
            logMetacat.error("MetaCatServlet.handleBuildIndexAction - Could not open http response for writing: "
2120
                    + e.getMessage());
2121
            e.printStackTrace(System.out);
2122
        } catch (MetacatUtilException ue) {
2123
            logMetacat.error("MetaCatServlet.handleBuildIndexAction - Could not determine if user is administrator: "
2124
                    + ue.getMessage());
2125
            ue.printStackTrace(System.out);
2126
        }
2127
    }
2128
    
2129
    /**
2130
     * Build the index for one document by reading the document and
2131
     * calling its buildIndex() method.
2132
     *
2133
     * @param docid the document (with revision) to rebuild
2134
     * @param out the PrintWriter to which output is printed
2135
     */
2136
    private void buildDocumentIndex(String docid, PrintWriter out) {
2137
        try {
2138
            DocumentImpl doc = new DocumentImpl(docid, false);
2139
            doc.buildIndex();
2140
            out.print("<docid>" + docid);
2141
            out.println("</docid>");
2142
        } catch (McdbException me) {
2143
            out.print("<error>");
2144
            out.print(me.getMessage());
2145
            out.println("</error>");
2146
        }
2147
    }
2148
    
2149
    /**
2150
     * Handle documents passed to metacat that are encoded using the
2151
     * "multipart/form-data" mime type. This is typically used for uploading
2152
     * data files which may be binary and large.
2153
     */
2154
    protected void handleMultipartForm(HttpServletRequest request,
2155
            HttpServletResponse response) {
2156
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2157
        PrintWriter out = null;
2158
        String action = null;
2159
        
2160
        // Parse the multipart form, and save the parameters in a Hashtable and
2161
        // save the FileParts in a hashtable
2162
        
2163
        Hashtable<String,String[]> params = new Hashtable<String,String[]>();
2164
        Hashtable<String,String> fileList = new Hashtable<String,String>();
2165
        int sizeLimit = 1000;
2166
        try {
2167
            sizeLimit = 
2168
                (new Integer(PropertyService.getProperty("replication.datafilesizelimit"))).intValue();
2169
        } catch (PropertyNotFoundException pnfe) {
2170
            logMetacat.error("MetaCatServlet.handleMultipartForm - Could not determine data file size limit.  Using 1000. " 
2171
                    + pnfe.getMessage());
2172
            pnfe.printStackTrace(System.out);
2173
        }
2174
        logMetacat.debug("MetaCatServlet.handleMultipartForm - The size limit of uploaded data files is: " + sizeLimit);
2175
        
2176
        try {
2177
            MultipartParser mp = new MultipartParser(request,
2178
                    sizeLimit * 1024 * 1024);
2179
            Part part;
2180
            
2181
            while ((part = mp.readNextPart()) != null) {
2182
                String name = part.getName();
2183
                
2184
                if (part.isParam()) {
2185
                    // it's a parameter part
2186
                    ParamPart paramPart = (ParamPart) part;
2187
                    String[] values = new String[1];
2188
                    values[0] = paramPart.getStringValue();
2189
                    params.put(name, values);
2190
                    if (name.equals("action")) {
2191
                        action = values[0];
2192
                    }
2193
                } else if (part.isFile()) {
2194
                    // it's a file part
2195
                    FilePart filePart = (FilePart) part;
2196
                    String fileName = filePart.getFileName();
2197
                    String fileTempLocation;
2198
                    
2199
                    // the filePart will be clobbered on the next loop, save to disk
2200
                    fileTempLocation = MetacatUtil.writeTempUploadFile(filePart, fileName);
2201
                    fileList.put(name, fileTempLocation);
2202
                    fileList.put("filename", fileName);
2203
                    fileList.put("name", fileTempLocation);
2204
                } else {
2205
                    logMetacat.info("MetaCatServlet.handleMultipartForm - Upload name '" + name + "' was empty.");
2206
                }
2207
            }
2208
        } catch (IOException ioe) {
2209
            try {
2210
                out = response.getWriter();
2211
            } catch (IOException ioe2) {
2212
                logMetacat.fatal("MetaCatServlet.handleMultipartForm - Fatal Error: couldn't get response output stream.");
2213
            }
2214
            out.println("<?xml version=\"1.0\"?>");
2215
            out.println("<error>");
2216
            out.println("Error: problem reading multipart data: " + ioe.getMessage());
2217
            out.println("</error>");
2218
            out.close();
2219
            return;
2220
        }
2221
        
2222
        // Get the session information
2223
        String username = null;
2224
        String password = null;
2225
        String[] groupnames = null;
2226
        String sess_id = null;
2227
        
2228
        // be aware of session expiration on every request
2229
        HttpSession sess = request.getSession(true);
2230
        if (sess.isNew()) {
2231
            // session expired or has not been stored b/w user requests
2232
            username = "public";
2233
            sess.setAttribute("username", username);
2234
        } else {
2235
            username = (String) sess.getAttribute("username");
2236
            password = (String) sess.getAttribute("password");
2237
            groupnames = (String[]) sess.getAttribute("groupnames");
2238
            try {
2239
                sess_id = (String) sess.getId();
2240
            } catch (IllegalStateException ise) {
2241
                System.out
2242
                        .println("error in  handleMultipartForm: this shouldn't "
2243
                        + "happen: the session should be valid: "
2244
                        + ise.getMessage());
2245
            }
2246
        }
2247
        
2248
        // Get the out stream
2249
        try {
2250
            out = response.getWriter();
2251
        } catch (IOException ioe2) {
2252
            logMetacat.error("MetaCatServlet.handleMultipartForm - Fatal Error: couldn't get response "
2253
                    + "output stream.");
2254
            ioe2.printStackTrace(System.out);
2255
        }
2256
        
2257
        if (action.equals("upload")) {
2258
            if (username != null && !username.equals("public")) {
2259
                handleUploadAction(request, out, params, fileList, username,
2260
                        groupnames, response);
2261
            } else {                
2262
                out.println("<?xml version=\"1.0\"?>");
2263
                out.println("<error>");
2264
                out.println("Permission denied for " + action);
2265
                out.println("</error>");
2266
            }
2267
        } else if(action.equals("insertmultipart")) {
2268
          if (username != null && !username.equals("public")) {
2269
            logMetacat.debug("MetaCatServlet.handleMultipartForm - handling multipart insert");
2270
              handleInsertMultipartAction(request, response,
2271
                            out, params, fileList, username, groupnames);
2272
          } else {
2273
              out.println("<?xml version=\"1.0\"?>");
2274
              out.println("<error>");
2275
              out.println("Permission denied for " + action);
2276
              out.println("</error>");
2277
          }
2278
        } else {
2279
            /*
2280
             * try { out = response.getWriter(); } catch (IOException ioe2) {
2281
             * System.err.println("Fatal Error: couldn't get response output
2282
             * stream.");
2283
             */
2284
            out.println("<?xml version=\"1.0\"?>");
2285
            out.println("<error>");
2286
            out.println("Error: action not registered.  Please report this error.");
2287
            out.println("</error>");
2288
        }
2289
        out.close();
2290
    }
2291
    
2292
    /**
2293
     * Handle the upload action by saving the attached file to disk and
2294
     * registering it in the Metacat db
2295
     */
2296
    private void handleInsertMultipartAction(HttpServletRequest request, 
2297
            HttpServletResponse response,
2298
            PrintWriter out, Hashtable<String,String[]> params, Hashtable<String,String> fileList,
2299
            String username, String[] groupnames)
2300
    {
2301
      Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2302
      String action = null;
2303
      String docid = null;
2304
      String qformat = null;
2305
      String output = "";
2306
      
2307
      /*
2308
       * response.setContentType("text/xml"); try { out =
2309
       * response.getWriter(); } catch (IOException ioe2) {
2310
       * System.err.println("Fatal Error: couldn't get response output
2311
       * stream.");
2312
       */
2313
      
2314
      if (params.containsKey("docid")) 
2315
      {
2316
          docid = params.get("docid")[0];
2317
      }
2318
      
2319
      if(params.containsKey("qformat")) 
2320
      {
2321
          qformat = params.get("qformat")[0];
2322
      }
2323
      
2324
      // Make sure we have a docid and datafile
2325
      if (docid != null && fileList.containsKey("datafile")) 
2326
      {
2327
        logMetacat.info("MetaCatServlet.handleInsertMultipartAction - Uploading data docid: " + docid);
2328
        // Get a reference to the file part of the form
2329
        //FilePart filePart = (FilePart) fileList.get("datafile");
2330
        String fileName = fileList.get("filename");
2331
        logMetacat.debug("MetaCatServlet.handleInsertMultipartAction - Uploading filename: " + fileName);
2332
        // Check if the right file existed in the uploaded data
2333
        if (fileName != null) 
2334
        {
2335
              
2336
          try 
2337
          {
2338
              //logMetacat.info("Upload datafile " + docid
2339
              // +"...", 10);
2340
              //If document get lock data file grant
2341
            if (DocumentImpl.getDataFileLockGrant(docid)) 
2342
            {
2343
              // Save the data file to disk using "docid" as the name
2344
              String datafilepath = PropertyService.getProperty("application.datafilepath");
2345
              File dataDirectory = new File(datafilepath);
2346
              dataDirectory.mkdirs();
2347
              File newFile = null;
2348
    //          File tempFile = null;
2349
              String tempFileName = fileList.get("name");
2350
              String newFileName = dataDirectory + File.separator + docid;
2351
              long size = 0;
2352
              boolean fileExists = false;
2353
                      
2354
              try 
2355
              {
2356
                newFile = new File(newFileName);
2357
                fileExists = newFile.exists();
2358
                logMetacat.info("MetaCatServlet.handleInsertMultipartAction - new file status is: " + fileExists);
2359
                if(fileExists)
2360
                {
2361
                  newFile.delete();
2362
                  newFile.createNewFile();
2363
                  fileExists = false;
2364
                }
2365
                
2366
                if ( fileExists == false ) 
2367
                {
2368
                    // copy file to desired output location
2369
                    try 
2370
                    {
2371
                        MetacatUtil.copyFile(tempFileName, newFileName);
2372
                    } 
2373
                    catch (IOException ioe) 
2374
                    {
2375
                        logMetacat.error("MetaCatServlet.handleInsertMultipartAction - IO Exception copying file: " +
2376
                                ioe.getMessage());
2377
                        ioe.printStackTrace(System.out);
2378
                    }
2379
                    size = newFile.length();
2380
                    if (size == 0) 
2381
                    {
2382
                        throw new IOException("MetaCatServlet.handleInsertMultipartAction - Uploaded file is 0 bytes!");
2383
                    }
2384
                }
2385
                logMetacat.info("MetaCatServlet.handleInsertMultipartAction - Uploading the following to Metacat:" +
2386
                        fileName + ", " + docid + ", " +
2387
                        username + ", " + groupnames);
2388
                FileReader fr = new FileReader(newFile);
2389
                
2390
                char[] c = new char[1024];
2391
                int numread = fr.read(c, 0, 1024);
2392
                StringBuffer sb = new StringBuffer();
2393
                while(numread != -1)
2394
                {
2395
                  sb.append(c, 0, numread);
2396
                  numread = fr.read(c, 0, 1024);
2397
                }
2398
                
2399
                Enumeration<String> keys = params.keys();
2400
                while(keys.hasMoreElements())
2401
                { //convert the params to arrays
2402
                  String key = keys.nextElement();
2403
                  String[] paramValue = params.get(key);
2404
                  String[] s = new String[1];
2405
                  s[0] = paramValue[0];
2406
                  params.put(key, s);
2407
                }
2408
                //add the doctext to the params
2409
                String doctext = sb.toString();
2410
                String[] doctextArr = new String[1];
2411
                doctextArr[0] = doctext;
2412
                params.put("doctext", doctextArr);
2413
                //call the insert routine
2414
                handleInsertOrUpdateAction(request.getRemoteAddr(), response, out, 
2415
                          params, username, groupnames);
2416
              }
2417
              catch(Exception e)
2418
              {
2419
                throw e;
2420
              }
2421
            }
2422
          }
2423
          catch(Exception e)
2424
          {
2425
              logMetacat.error("MetaCatServlet.handleInsertMultipartAction - error uploading text file via multipart: " + e.getMessage());
2426
              e.printStackTrace(System.out);;
2427
          }
2428
        }
2429
      }
2430
    }
2431
    
2432
    /**
2433
     * Handle the upload action by saving the attached file to disk and
2434
     * registering it in the Metacat db
2435
     */
2436
    private void handleUploadAction(HttpServletRequest request,
2437
            PrintWriter out, Hashtable<String, String[]> params, Hashtable<String,String> fileList,
2438
            String username, String[] groupnames, HttpServletResponse response) {
2439
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2440
        //PrintWriter out = null;
2441
        //Connection conn = null;
2442
        //        String action = null;
2443
        String docid = null;
2444
        String qformat = null;
2445
        String output = "";
2446

    
2447
        /*
2448
         * response.setContentType("text/xml"); try { out =
2449
         * response.getWriter(); } catch (IOException ioe2) {
2450
         * System.err.println("Fatal Error: couldn't get response output
2451
         * stream.");
2452
         */
2453

    
2454
        if (params.containsKey("docid")) {
2455
            docid = params.get("docid")[0];
2456
        }
2457

    
2458
        if(params.containsKey("qformat")) {
2459
            qformat = params.get("qformat")[0];
2460
        }
2461

    
2462
        // Make sure we have a docid and datafile
2463
        if (docid != null && fileList.containsKey("datafile")) {
2464
            logMetacat.info("MetaCatServlet.handleUploadAction - Uploading data docid: " + docid);
2465
            // Get a reference to the file part of the form
2466
            //FilePart filePart = (FilePart) fileList.get("datafile");
2467
            String fileName = fileList.get("filename");
2468
            logMetacat.info("MetaCatServlet.handleUploadAction - Uploading filename: " + fileName);
2469
            // Check if the right file existed in the uploaded data
2470
            if (fileName != null) {
2471

    
2472
                try {
2473
                    //logMetacat.info("Upload datafile " + docid
2474
                    // +"...", 10);
2475
                    //If document get lock data file grant
2476
                    if (DocumentImpl.getDataFileLockGrant(docid)) {
2477
                        // Save the data file to disk using "docid" as the name
2478
                        String datafilepath = PropertyService.getProperty("application.datafilepath");
2479
                        File dataDirectory = new File(datafilepath);
2480
                        dataDirectory.mkdirs();
2481
                        File newFile = null;
2482
                        //                    File tempFile = null;
2483
                        String tempFileName = fileList.get("name");
2484
                        String newFileName = dataDirectory + File.separator + docid;
2485
                        long size = 0;
2486
                        boolean fileExists = false;
2487

    
2488
                        try {
2489
                            newFile = new File(newFileName);
2490
                            fileExists = newFile.exists();
2491
                            logMetacat.info("MetaCatServlet.handleUploadAction - new file status is: " + fileExists);
2492
                            if ( fileExists == false ) {
2493
                                // copy file to desired output location
2494
                                try {
2495
                                    MetacatUtil.copyFile(tempFileName, newFileName);
2496
                                } catch (IOException ioe) {
2497
                                    logMetacat.error("IO Exception copying file: " +
2498
                                            ioe.getMessage());
2499
                                    ioe.printStackTrace(System.out);
2500
                                }
2501
                                size = newFile.length();
2502
                                if (size == 0) {
2503
                                    throw new IOException("Uploaded file is 0 bytes!");
2504
                                }
2505
                            } // Latent bug here if the file already exists, then the
2506
                              // conditional fails but the document is still registered.
2507
                              // maybe this never happens because we already requested a lock?
2508
                            logMetacat.info("MetaCatServlet.handleUploadAction - Uploading the following to Metacat:" +
2509
                                    fileName + ", " + docid + ", " +
2510
                                    username + ", " + groupnames);
2511
                            //register the file in the database (which generates
2512
                            // an exception
2513
                            //if the docid is not acceptable or other untoward
2514
                            // things happen
2515
                            DocumentImpl.registerDocument(fileName, "BIN", docid,
2516
                                    username, groupnames);
2517
                        } catch (Exception ee) {
2518
                            // If the file did not exist before this method was 
2519
                            // called and an exception is generated while 
2520
                            // creating or registering it, then we want to delete
2521
                            // the file from disk because the operation failed.
2522
                            // However, if the file already existed before the 
2523
                            // method was called, then the exception probably
2524
                            // occurs when registering the document, and so we
2525
                            // want to leave the old file in place.
2526
                            if ( fileExists == false ) {
2527
                                newFile.delete();
2528
                            }
2529
                            
2530
                            logMetacat.info("MetaCatServlet.handleUploadAction - in Exception: fileExists is " + fileExists);
2531
                            logMetacat.error("MetaCatServlet.handleUploadAction - Upload Error: " + ee.getMessage());
2532
                            throw ee;
2533
                        }
2534

    
2535
                        EventLog.getInstance().log(request.getRemoteAddr(),
2536
                                username, docid, "upload");
2537
                        // Force replication this data file
2538
                        // To data file, "insert" and update is same
2539
                        // The fourth parameter is null. Because it is
2540
                        // notification server
2541
                        // and this method is in MetaCatServerlet. It is
2542
                        // original command,
2543
                        // not get force replication info from another metacat
2544
                        ForceReplicationHandler frh = new ForceReplicationHandler(
2545
                                docid, "insert", false, null);
2546
                        logMetacat.debug("MetaCatServlet.handleUploadAction - ForceReplicationHandler created: " + frh.toString());
2547

    
2548
                        // set content type and other response header fields
2549
                        // first
2550
                        output += "<?xml version=\"1.0\"?>";
2551
                        output += "<success>";
2552
                        output += "<docid>" + docid + "</docid>";
2553
                        output += "<size>" + size + "</size>";
2554
                        output += "</success>";
2555
                    }
2556

    
2557
                } catch (Exception e) {
2558

    
2559
                    output += "<?xml version=\"1.0\"?>";
2560
                    output += "<error>";
2561
                    output += e.getMessage();
2562
                    output += "</error>";
2563
                }
2564
            } else {
2565
                // the field did not contain a file
2566
                output += "<?xml version=\"1.0\"?>";
2567
                output += "<error>";
2568
                output += "The uploaded data did not contain a valid file.";
2569
                output += "</error>";
2570
            }
2571
        } else {
2572
            // Error bcse docid missing or file missing
2573
            output += "<?xml version=\"1.0\"?>";
2574
            output += "<error>";
2575
            output += "The uploaded data did not contain a valid docid "
2576
                + "or valid file.";
2577
            output += "</error>";
2578
        }
2579

    
2580
        if (qformat == null || qformat.equals("xml")) {
2581
            response.setContentType("text/xml");
2582
            out.println(output);
2583
        } else {
2584
            try {
2585
                DBTransform trans = new DBTransform();
2586
                response.setContentType("text/html");
2587
                trans.transformXMLDocument(output,
2588
                        "message", "-//W3C//HTML//EN", qformat,
2589
                        out, null, null);
2590
            } catch (Exception e) {
2591

    
2592
                logMetacat.error("MetaCatServlet.handleUploadAction - General error: "
2593
                        + e.getMessage());
2594
                e.printStackTrace(System.out);
2595
            }
2596
        }
2597
    }
2598
    
2599
    /*
2600
     * A method to handle set access action
2601
     */
2602
    protected void handleSetAccessAction(PrintWriter out, Hashtable<String, String[]> params,
2603
            String username, HttpServletRequest request, HttpServletResponse response) {
2604
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2605
        String permission = null;
2606
        String permType = null;
2607
        String permOrder = null;
2608
        Vector<String> errorList = new Vector<String>();
2609
        String error = null;
2610
        Vector<String> successList = new Vector<String>();
2611
        String success = null;
2612
        boolean isEmlPkgMember = false;
2613
        
2614
        String[] docList = params.get("docid");
2615
        String[] principalList = params.get("principal");
2616
        String[] permissionList = params.get("permission");
2617
        String[] permTypeList = params.get("permType");
2618
        String[] permOrderList = params.get("permOrder");
2619
        String[] qformatList = params.get("qformat");
2620
        String[] accessBlock = params.get("accessBlock");
2621
        if(accessBlock != null) {
2622
            if (docList == null) {
2623
                errorList.addElement("MetaCatServlet.handleSetAccessAction - Doc id missing.  Please check your parameter list, it should look like: "
2624
                    + "?action=setaccess&docid=<doc_id>&accessBlock=<access_section>");
2625
                outputResponse(successList, errorList, out);
2626
                return;
2627
            }
2628
            try {
2629
                AccessControlForSingleFile accessControl = 
2630
                    new AccessControlForSingleFile(docList[0]);
2631
                accessControl.insertPermissions(accessBlock[0]);
2632
                successList.addElement("MetaCatServlet.handleSetAccessAction - successfully replaced access block for doc id: " + docList[0]);
2633
            } catch(AccessControlException ace) {
2634
                errorList.addElement("MetaCatServlet.handleSetAccessAction - access control error when setting " + 
2635
                    "access block: " + ace.getMessage());
2636
            }
2637
            outputResponse(successList, errorList, out);
2638
            return;
2639
        }
2640
        
2641
        // Make sure the parameter is not null
2642
        if (docList == null || principalList == null || permTypeList == null
2643
                || permissionList == null) {
2644
            error = "MetaCatServlet.handleSetAccessAction - Please check your parameter list, it should look like: "
2645
                    + "?action=setaccess&docid=pipeline.1.1&principal=public"
2646
                    + "&permission=read&permType=allow&permOrder=allowFirst";
2647
            errorList.addElement(error);
2648
            outputResponse(successList, errorList, out);
2649
            return;
2650
        }
2651
        
2652
        // Only select first element for permission, type and order
2653
        permission = permissionList[0];
2654
        permType = permTypeList[0];
2655
        if (permOrderList != null) {
2656
            permOrder = permOrderList[0];
2657
        }
2658
        
2659
        // Get package doctype set
2660
        Vector<String> packageSet = null;
2661
        try {
2662
            packageSet = 
2663
                MetacatUtil.getOptionList(PropertyService.getProperty("xml.packagedoctypeset"));
2664
        } catch (PropertyNotFoundException pnfe) {
2665
            logMetacat.error("MetaCatServlet.handleSetAccessAction - Could not find package doctype set.  Setting to null: " 
2666
                    + pnfe.getMessage());
2667
        }
2668
        //debug
2669
        if (packageSet != null) {
2670
            for (int i = 0; i < packageSet.size(); i++) {
2671
                logMetacat.debug("MetaCatServlet.handleSetAccessAction - doctype in package set: "
2672
                        + packageSet.elementAt(i));
2673
            }
2674
        }
2675
        
2676
        // handle every accessionNumber
2677
        for (int i = 0; i < docList.length; i++) {
2678
            String docid = docList[i];
2679
            if (docid.startsWith("urn:")) {
2680
                try {
2681
                    String actualDocId = LSIDUtil.getDocId(docid, false);
2682
                    docid = actualDocId;
2683
                } catch (ParseLSIDException ple) {
2684
                    logMetacat.error("MetaCatServlet.handleSetAccessAction - " +
2685
                            "could not parse lsid: " + docid + " : " + ple.getMessage()); 
2686
                    ple.printStackTrace(System.out);
2687
                }
2688
            }
2689
            String accessionNumber = docid;
2690
            String owner = null;
2691
            String publicId = null;
2692
            // Get document owner and public id
2693
            try {
2694
                owner = getFieldValueForDoc(accessionNumber, "user_owner");
2695
                publicId = getFieldValueForDoc(accessionNumber, "doctype");
2696
            } catch (Exception e) {
2697
                logMetacat.error("MetaCatServlet.handleSetAccessAction - Error in handleSetAccessAction: "
2698
                        + e.getMessage());
2699
                e.printStackTrace(System.out);
2700
                error = "Error in set access control for document - " + accessionNumber + e.getMessage();
2701
                errorList.addElement(error);
2702
                continue;
2703
            }
2704
            //check if user is the owner. Only owner can do owner
2705
            if (username == null || owner == null || !username.equals(owner)) {
2706
                error = "User - " + username + " does not have permission to set "
2707
                        + "access control for docid - " + accessionNumber;
2708
                errorList.addElement(error);
2709
                System.out.println("user " + username + " does not have permission to set " + 
2710
                        "access control for docid " + accessionNumber);
2711
                continue;
2712
            }
2713
            
2714
            //*** Find out if the document is a Member of an EML package.
2715
            //*** (for efficiency, only check if it isn't already true
2716
            //*** for the case of multiple files).
2717
            if (isEmlPkgMember == false)
2718
                isEmlPkgMember = (DBUtil.findDataSetDocIdForGivenDocument(accessionNumber) != null);
2719
            
2720
            // If docid publicid is BIN data file or other beta4, 6 package document
2721
            // we could not do set access control. Because we don't want inconsistent
2722
            // to its access docuemnt
2723
            if (publicId != null && packageSet != null
2724
                    && packageSet.contains(publicId) && isEmlPkgMember) {
2725
                error = "Could not set access control to document " + accessionNumber
2726
                        + "because it is in a pakcage and it has a access file for it";
2727
                errorList.addElement(error);
2728
                System.out.println("this is a beta4 or 6 document so we can't set the access element.");
2729
                continue;
2730
            }
2731
            // for every principle
2732
            for (int j = 0; j < principalList.length; j++) {
2733
                String principal = principalList[j];
2734
                try {
2735
                    //insert permission
2736
                    AccessControlForSingleFile accessControl = 
2737
                        new AccessControlForSingleFile(accessionNumber);
2738
                    accessControl.insertPermissions(principal, Long.valueOf(permission), permType, permOrder);
2739
                } catch (Exception ee) {
2740
                    logMetacat.error("MetaCatServlet.handleSetAccessAction - Error inserting permission: "
2741
                            + ee.getMessage());
2742
                    ee.printStackTrace(System.out);
2743
                    error = "Failed to set access control for document "
2744
                            + accessionNumber + " because " + ee.getMessage();
2745
                    errorList.addElement(error);
2746
                    continue;
2747
                }
2748
            }
2749
            //force replication when this action is called
2750
            boolean isXml = true;
2751
            if (publicId.equalsIgnoreCase("BIN")) {
2752
                isXml = false;
2753
            }
2754
            ForceReplicationHandler frh = 
2755
                new ForceReplicationHandler(accessionNumber, isXml, null);
2756
            logMetacat.debug("MetaCatServlet.handleSetAccessAction - ForceReplicationHandler created: " + frh.toString());
2757
            
2758
        }
2759
        successList.addElement("MetaCatServlet.handleSetAccessAction - successfully added individual access for doc id: " + docList[0]);
2760
        
2761
        if (params.get("forwardto")  != null) {
2762
            try {
2763
                RequestUtil.forwardRequest(request, response, params);
2764
            } catch (MetacatUtilException mue) {
2765
                logMetacat.error("metaCatServlet.handleSetAccessAction - could not forward " +
2766
                        "request. Sending output to response writer");
2767
                mue.printStackTrace(System.out);
2768
                outputResponse(successList, errorList, out);
2769
            }               
2770
        } else {
2771
            outputResponse(successList, errorList, out);
2772
        }
2773
    }
2774
    
2775
    /*
2776
     * A method try to determin a docid's public id, if couldn't find null will
2777
     * be returned.
2778
     */
2779
    private String getFieldValueForDoc(String accessionNumber, String fieldName)
2780
    throws Exception {
2781
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2782
        if (accessionNumber == null || accessionNumber.equals("")
2783
        || fieldName == null || fieldName.equals("")) { throw new Exception(
2784
                "Docid or field name was not specified"); }
2785
        
2786
        PreparedStatement pstmt = null;
2787
        ResultSet rs = null;
2788
        String fieldValue = null;
2789
        String docId = null;
2790
        DBConnection conn = null;
2791
        int serialNumber = -1;
2792
        
2793
        // get rid of revision if access number has
2794
        docId = DocumentUtil.getDocIdFromString(accessionNumber);
2795
        try {
2796
            //check out DBConnection
2797
            conn = DBConnectionPool
2798
                    .getDBConnection("MetaCatServlet.getPublicIdForDoc");
2799
            serialNumber = conn.getCheckOutSerialNumber();
2800
            pstmt = conn.prepareStatement("SELECT " + fieldName
2801
                    + " FROM xml_documents " + "WHERE docid = ? ");
2802
            
2803
            pstmt.setString(1, docId);
2804
            pstmt.execute();
2805
            rs = pstmt.getResultSet();
2806
            boolean hasRow = rs.next();
2807
        //    int perm = 0;
2808
            if (hasRow) {
2809
                fieldValue = rs.getString(1);
2810
            } else {
2811
                throw new Exception("Could not find document: "
2812
                        + accessionNumber);
2813
            }
2814
        } catch (Exception e) {
2815
            logMetacat.error("MetaCatServlet.getFieldValueForDoc - General error: "
2816
                    + e.getMessage());
2817
            throw e;
2818
        } finally {
2819
            try {
2820
                rs.close();
2821
                pstmt.close();
2822
                
2823
            } finally {
2824
                DBConnectionPool.returnDBConnection(conn, serialNumber);
2825
            }
2826
        }
2827
        return fieldValue;
2828
    }
2829
    
2830
    /*
2831
     * Get the list of documents from the database and return the list in an
2832
     * Vector of identifiers.
2833
     *
2834
     * @ returns the array of identifiers
2835
     */
2836
    private Vector<String> getDocumentList() throws SQLException {
2837
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2838
        Vector<String> docList = new Vector<String>();
2839
        PreparedStatement pstmt = null;
2840
        ResultSet rs = null;
2841
        DBConnection conn = null;
2842
        int serialNumber = -1;
2843
        
2844
        try {
2845
            //check out DBConnection
2846
            conn = DBConnectionPool
2847
                    .getDBConnection("MetaCatServlet.getDocumentList");
2848
            serialNumber = conn.getCheckOutSerialNumber();
2849
            pstmt = conn.prepareStatement("SELECT docid, rev"
2850
                    + " FROM xml_documents ");
2851
            pstmt.execute();
2852
            rs = pstmt.getResultSet();
2853
            while (rs.next()) {
2854
                String docid = rs.getString(1);
2855
                String rev = rs.getString(2);
2856
                docList.add(docid + "." + rev);
2857
            }
2858
        } catch (SQLException e) {
2859
            logMetacat.error("MetaCatServlet.getDocumentList - General exception: "
2860
                    + e.getMessage());
2861
            throw e;
2862
        } finally {
2863
            try {
2864
                rs.close();
2865
                pstmt.close();
2866
                
2867
            } catch (SQLException se) {
2868
                logMetacat.error("MetaCatServlet.getDocumentList - General exception: "
2869
                        + se.getMessage());
2870
                throw se;
2871
            } finally {
2872
                DBConnectionPool.returnDBConnection(conn, serialNumber);
2873
            }
2874
        }
2875
        return docList;
2876
    }
2877
    
2878
    /*
2879
     * A method to output setAccess action result
2880
     */
2881
    private void outputResponse(Vector<String> successList, Vector<String> errorList,
2882
            PrintWriter out) {
2883
        boolean error = false;
2884
        boolean success = false;
2885
        // Output prolog
2886
        out.println(PROLOG);
2887
        // output success message
2888
        if (successList != null) {
2889
            for (int i = 0; i < successList.size(); i++) {
2890
                out.println(SUCCESS);
2891
                out.println(successList.elementAt(i));
2892
                out.println(SUCCESSCLOSE);
2893
                success = true;
2894
            }
2895
        }
2896
        // output error message
2897
        if (errorList != null) {
2898
            for (int i = 0; i < errorList.size(); i++) {
2899
                out.println(ERROR);
2900
                out.println(errorList.elementAt(i));
2901
                out.println(ERRORCLOSE);
2902
                error = true;
2903
            }
2904
        }
2905
        
2906
        // if no error and no success info, send a error that nothing happened
2907
        if (!error && !success) {
2908
            out.println(ERROR);
2909
            out.println("Nothing happend for setaccess action");
2910
            out.println(ERRORCLOSE);
2911
        }
2912
    }
2913
    
2914
    /*
2915
     * If the given docid only have one seperter, we need
2916
     * append rev for it. The rev come from xml_documents
2917
     */
2918
    private static String appendRev(String docid) throws PropertyNotFoundException, SQLException, McdbDocNotFoundException {
2919
//        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2920
        String newAccNum = null;
2921
        String separator = PropertyService.getProperty("document.accNumSeparator");
2922
        int firstIndex = docid.indexOf(separator);
2923
        int lastIndex = docid.lastIndexOf(separator);
2924
        if (firstIndex == lastIndex) {
2925
            
2926
            //only one seperater
2927
            int rev = DBUtil.getLatestRevisionInDocumentTable(docid);
2928
            if (rev == -1) {
2929
                throw new McdbDocNotFoundException("the requested docid '"
2930
                        + docid+ "' does not exist");
2931
            } else {
2932
                newAccNum = docid+ separator+ rev;
2933
            }
2934
        } else {
2935
            // in other suituation we don't change the docid
2936
            newAccNum = docid;
2937
        }
2938
        //logMetacat.debug("The docid will be read is "+newAccNum);
2939
        return newAccNum;
2940
    }
2941
    
2942
    /**
2943
     * Schedule the sitemap generator to run periodically and update all
2944
     * of the sitemap files for search indexing engines.
2945
     *
2946
     * @param request a servlet request, from which we determine the context
2947
     */
2948
    protected void scheduleSitemapGeneration(HttpServletRequest request) {
2949
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2950
        if (!_sitemapScheduled) {
2951
            String directoryName = null;
2952
            String skin = null;
2953
            long sitemapInterval = 0;
2954
            
2955
            try {
2956
                directoryName = SystemUtil.getContextDir() + FileUtil.getFS() + "sitemaps";
2957
                skin = PropertyService.getProperty("application.default-style");
2958
                sitemapInterval = 
2959
                    Integer.parseInt(PropertyService.getProperty("sitemap.interval"));
2960
            } catch (PropertyNotFoundException pnfe) {
2961
                logMetacat.error("MetaCatServlet.scheduleSitemapGeneration - Could not run site map generation because property " 
2962
                        + "could not be found: " + pnfe.getMessage());
2963
            }
2964
            
2965
            File directory = new File(directoryName);
2966
            directory.mkdirs();
2967
            String urlRoot = request.getRequestURL().toString();
2968
            Sitemap smap = new Sitemap(directory, urlRoot, skin);
2969
            long firstDelay = 60*1000;   // 60 seconds delay
2970
            timer.schedule(smap, firstDelay, sitemapInterval);
2971
            _sitemapScheduled = true;
2972
        }
2973
    }
2974

    
2975
    /**
2976
     * @param sitemapScheduled toggle the _sitemapScheduled flag
2977
     */
2978
    public void set_sitemapScheduled(boolean sitemapScheduled) {
2979
        _sitemapScheduled = sitemapScheduled;
2980
    }
2981

    
2982
}
(43-43/63)