Project

General

Profile

« Previous | Next » 

Revision 8593

Added by Jing Tao about 10 years ago

Add the code to check if the docid contains the whitespaces in the handleInsertOrUpdate, handleUpload and handleInsertMultipartInsertAction methods.

View differences:

src/edu/ucsb/nceas/metacat/MetacatHandler.java
93 93
import edu.ucsb.nceas.metacat.common.query.EnabledQueryEngines;
94 94
import edu.ucsb.nceas.metacat.database.DBConnection;
95 95
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
96
import edu.ucsb.nceas.metacat.dataone.D1NodeService;
96 97
import edu.ucsb.nceas.metacat.dataone.SyncAccessPolicy;
97 98
import edu.ucsb.nceas.metacat.dataone.SystemMetadataFactory;
98 99
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
......
1787 1788
              String accNumber = docid[0];
1788 1789
              logMetacat.debug("MetacatHandler.handleInsertOrUpdateAction - " + 
1789 1790
                doAction + " " + accNumber + "...");
1791
              Identifier identifier = new Identifier();
1792
              identifier.setValue(accNumber);
1793
              if(!D1NodeService.isValidIdentifier(identifier)) {
1794
                  String error = "The docid "+accNumber +" is not valid since it is null or contians the white space(s).";
1795
                  logMetacat.warn("MetacatHandler.handleInsertOrUpdateAction - " +error);
1796
                  throw new Exception(error);
1797
              }
1790 1798
              
1791
              if (accNumber == null || accNumber.equals("")) {
1799
              /*if (accNumber == null || accNumber.equals("")) {
1792 1800
                  logMetacat.warn("MetacatHandler.handleInsertOrUpdateAction - " +
1793 1801
                		          "writing with null acnumber");
1794 1802
                  newdocid = documentWrapper.write(dbConn, doctext[0], pub, dtd,
1795 1803
                          doAction, null, user, groups);
1796 1804
                  EventLog.getInstance().log(ipAddress, userAgent, user, "", action[0]);
1797 1805
              
1798
              } else {
1799
                  newdocid = documentWrapper.write(dbConn, doctext[0], pub, dtd,
1806
              } else {*/
1807
              newdocid = documentWrapper.write(dbConn, doctext[0], pub, dtd,
1800 1808
                          doAction, accNumber, user, groups);
1801 1809
            
1802
                  EventLog.getInstance().log(ipAddress, userAgent, user, accNumber, action[0]);
1810
              EventLog.getInstance().log(ipAddress, userAgent, user, accNumber, action[0]);
1803 1811
              
1804
              }
1812
              //}
1805 1813
              
1806 1814
              // alert listeners of this event
1807 1815
              MetacatDocumentEvent mde = new MetacatDocumentEvent();
......
3006 3014
       * System.err.println("Fatal Error: couldn't get response output
3007 3015
       * stream.");
3008 3016
       */
3017
      if(params.containsKey("qformat")) 
3018
      {
3019
          qformat = params.get("qformat")[0];
3020
      }
3009 3021
      
3010 3022
      if (params.containsKey("docid")) 
3011 3023
      {
3012 3024
          docid = params.get("docid")[0];
3013 3025
      }
3014 3026
      
3015
      if(params.containsKey("qformat")) 
3016
      {
3017
          qformat = params.get("qformat")[0];
3027
      Identifier identifier = new Identifier();
3028
      identifier.setValue(docid);
3029
      if(!D1NodeService.isValidIdentifier(identifier)) {
3030
          output += this.PROLOG;
3031
          output += this.ERROR;
3032
          output += "The docid "+docid +" is not valid since it is null or contians the white space(s).";
3033
          output += this.ERRORCLOSE;
3034
          logMetacat.warn("MetacatHandler.handleInsertMultipartAction - " +
3035
                          "The docid "+docid +" is not valid since it is null or contians the white space(s).");
3036
          if (qformat == null || qformat.equals("xml")) {
3037
              response.setContentType("text/xml");
3038
              out.println(output);
3039
          } else {
3040
              try {
3041
                  DBTransform trans = new DBTransform();
3042
                  response.setContentType("text/html");
3043
                  trans.transformXMLDocument(output,
3044
                          "message", "-//W3C//HTML//EN", qformat,
3045
                          out, null, null);
3046
              } catch (Exception e) {
3047

  
3048
                  logMetacat.error("MetacatHandler.handleInsertMultipartAction - General error: "
3049
                          + e.getMessage());
3050
                  e.printStackTrace(System.out);
3051
              }
3052
          }
3053
          return;
3018 3054
      }
3019 3055
      
3056
      
3057
      
3020 3058
      // Make sure we have a docid and datafile
3021 3059
      if (docid != null && fileList.containsKey("datafile")) 
3022 3060
      {
......
3164 3202
        if (params.containsKey("docid")) {
3165 3203
            docid = params.get("docid")[0];
3166 3204
        }
3205
        
3206
        Identifier identifier = new Identifier();
3207
        identifier.setValue(docid);
3208
        if(!D1NodeService.isValidIdentifier(identifier)) {
3209
            output += this.PROLOG;
3210
            output += this.ERROR;
3211
            output += "The docid "+docid +" is not valid since it is null or contians the white space(s).";
3212
            output += this.ERRORCLOSE;
3213
            logMetacat.warn("MetacatHandler.handleUploadAction - " +
3214
                            "The docid "+docid +" is not valid since it is null or contians the white space(s).");
3215
        } else {
3216
         // Make sure we have a docid and datafile
3217
            if (docid != null && fileList.containsKey("datafile")) {
3218
                logMetacat.info("MetacatHandler.handleUploadAction - " +
3219
                                "Uploading data docid: " + docid);
3220
                // Get a reference to the file part of the form
3221
                //FilePart filePart = (FilePart) fileList.get("datafile");
3222
                String fileName = fileList.get("filename");
3223
                logMetacat.info("MetacatHandler.handleUploadAction - " +
3224
                                "Uploading filename: " + fileName);
3225
                // Check if the right file existed in the uploaded data
3226
                if (fileName != null) {
3167 3227

  
3168
        if(params.containsKey("qformat")) {
3169
            qformat = params.get("qformat")[0];
3170
        }
3228
                    try {
3229
                        //logMetacat.info("Upload datafile " + docid
3230
                        // +"...", 10);
3231
                        //If document get lock data file grant
3232
                        if (DocumentImpl.getDataFileLockGrant(docid)) {
3233
                            // Save the data file to disk using "docid" as the name
3234
                            String datafilepath = PropertyService.getProperty("application.datafilepath");
3235
                            File dataDirectory = new File(datafilepath);
3236
                            dataDirectory.mkdirs();
3237
                            File newFile = null;
3238
                            //                    File tempFile = null;
3239
                            String tempFileName = fileList.get("name");
3240
                            String newFileName = dataDirectory + File.separator + docid;
3241
                            long size = 0;
3242
                            boolean fileExists = false;
3171 3243

  
3172
        // Make sure we have a docid and datafile
3173
        if (docid != null && fileList.containsKey("datafile")) {
3174
            logMetacat.info("MetacatHandler.handleUploadAction - " +
3175
            		        "Uploading data docid: " + docid);
3176
            // Get a reference to the file part of the form
3177
            //FilePart filePart = (FilePart) fileList.get("datafile");
3178
            String fileName = fileList.get("filename");
3179
            logMetacat.info("MetacatHandler.handleUploadAction - " +
3180
            		        "Uploading filename: " + fileName);
3181
            // Check if the right file existed in the uploaded data
3182
            if (fileName != null) {
3244
                            try {
3245
                                newFile = new File(newFileName);
3246
                                fileExists = newFile.exists();
3247
                                logMetacat.info("MetacatHandler.handleUploadAction - " +
3248
                                                "new file status is: " + fileExists);
3249
                                if ( fileExists == false ) {
3250
                                    // copy file to desired output location
3251
                                    try {
3252
                                        MetacatUtil.copyFile(tempFileName, newFileName);
3253
                                    } catch (IOException ioe) {
3254
                                        logMetacat.error("IO Exception copying file: " +
3255
                                                ioe.getMessage());
3256
                                        ioe.printStackTrace(System.out);
3257
                                    }
3258
                                    size = newFile.length();
3259
                                    if (size == 0) {
3260
                                        throw new IOException("Uploaded file is 0 bytes!");
3261
                                    }
3262
                                } // Latent bug here if the file already exists, then the
3263
                                  // conditional fails but the document is still registered.
3264
                                  // maybe this never happens because we already requested a lock?
3265
                                logMetacat.info("MetacatHandler.handleUploadAction - " +
3266
                                                "Uploading the following to Metacat:" +
3267
                                                fileName + ", " + docid + ", " +
3268
                                                username + ", " + groupnames);
3269
                                //register the file in the database (which generates
3270
                                // an exception
3271
                                //if the docid is not acceptable or other untoward
3272
                                // things happen
3273
                                DocumentImpl.registerDocument(fileName, "BIN", docid,
3274
                                        username, groupnames);
3275
                                
3276
                                // generate system metadata about the doc
3277
                                SystemMetadata sm = SystemMetadataFactory.createSystemMetadata(docid, false, false);
3278
                                
3279
                                // manage it in the store
3280
                                HazelcastService.getInstance().getSystemMetadataMap().put(sm.getIdentifier(), sm);
3281
                                
3282
                                // submit for indexing
3283
                                MetacatSolrIndex.getInstance().submit(sm.getIdentifier(), sm, null);
3284
                                
3285
                            } catch (Exception ee) {
3286
                                // If the file did not exist before this method was 
3287
                                // called and an exception is generated while 
3288
                                // creating or registering it, then we want to delete
3289
                                // the file from disk because the operation failed.
3290
                                // However, if the file already existed before the 
3291
                                // method was called, then the exception probably
3292
                                // occurs when registering the document, and so we
3293
                                // want to leave the old file in place.
3294
                                if ( fileExists == false ) {
3295
                                    newFile.delete();
3296
                                }
3297
                                
3298
                                logMetacat.info("MetacatHandler.handleUploadAction - " +
3299
                                                "in Exception: fileExists is " + fileExists);
3300
                                logMetacat.error("MetacatHandler.handleUploadAction - " +
3301
                                                 "Upload Error: " + ee.getMessage());
3302
                                throw ee;
3303
                            }
3183 3304

  
3184
                try {
3185
                    //logMetacat.info("Upload datafile " + docid
3186
                    // +"...", 10);
3187
                    //If document get lock data file grant
3188
                    if (DocumentImpl.getDataFileLockGrant(docid)) {
3189
                        // Save the data file to disk using "docid" as the name
3190
                        String datafilepath = PropertyService.getProperty("application.datafilepath");
3191
                        File dataDirectory = new File(datafilepath);
3192
                        dataDirectory.mkdirs();
3193
                        File newFile = null;
3194
                        //                    File tempFile = null;
3195
                        String tempFileName = fileList.get("name");
3196
                        String newFileName = dataDirectory + File.separator + docid;
3197
                        long size = 0;
3198
                        boolean fileExists = false;
3305
                            EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"),
3306
                                    username, docid, "upload");
3307
                            // Force replication this data file
3308
                            // To data file, "insert" and update is same
3309
                            // The fourth parameter is null. Because it is
3310
                            // notification server
3311
                            // and this method is in MetaCatServerlet. It is
3312
                            // original command,
3313
                            // not get force replication info from another metacat
3314
                            ForceReplicationHandler frh = new ForceReplicationHandler(
3315
                                    docid, "insert", false, null);
3316
                            logMetacat.debug("MetacatHandler.handleUploadAction - " +
3317
                                             "ForceReplicationHandler created: " + 
3318
                                             frh.toString());
3199 3319

  
3200
                        try {
3201
                            newFile = new File(newFileName);
3202
                            fileExists = newFile.exists();
3203
                            logMetacat.info("MetacatHandler.handleUploadAction - " +
3204
                            		        "new file status is: " + fileExists);
3205
                            if ( fileExists == false ) {
3206
                                // copy file to desired output location
3207
                                try {
3208
                                    MetacatUtil.copyFile(tempFileName, newFileName);
3209
                                } catch (IOException ioe) {
3210
                                    logMetacat.error("IO Exception copying file: " +
3211
                                            ioe.getMessage());
3212
                                    ioe.printStackTrace(System.out);
3213
                                }
3214
                                size = newFile.length();
3215
                                if (size == 0) {
3216
                                    throw new IOException("Uploaded file is 0 bytes!");
3217
                                }
3218
                            } // Latent bug here if the file already exists, then the
3219
                              // conditional fails but the document is still registered.
3220
                              // maybe this never happens because we already requested a lock?
3221
                            logMetacat.info("MetacatHandler.handleUploadAction - " +
3222
                            		        "Uploading the following to Metacat:" +
3223
                                            fileName + ", " + docid + ", " +
3224
                                            username + ", " + groupnames);
3225
                            //register the file in the database (which generates
3226
                            // an exception
3227
                            //if the docid is not acceptable or other untoward
3228
                            // things happen
3229
                            DocumentImpl.registerDocument(fileName, "BIN", docid,
3230
                                    username, groupnames);
3231
                            
3232
                            // generate system metadata about the doc
3233
                            SystemMetadata sm = SystemMetadataFactory.createSystemMetadata(docid, false, false);
3234
							
3235
					        // manage it in the store
3236
                            HazelcastService.getInstance().getSystemMetadataMap().put(sm.getIdentifier(), sm);
3237
                            
3238
                            // submit for indexing
3239
                            MetacatSolrIndex.getInstance().submit(sm.getIdentifier(), sm, null);
3240
					        
3241
                        } catch (Exception ee) {
3242
                            // If the file did not exist before this method was 
3243
                            // called and an exception is generated while 
3244
                            // creating or registering it, then we want to delete
3245
                            // the file from disk because the operation failed.
3246
                            // However, if the file already existed before the 
3247
                            // method was called, then the exception probably
3248
                            // occurs when registering the document, and so we
3249
                            // want to leave the old file in place.
3250
                            if ( fileExists == false ) {
3251
                                newFile.delete();
3252
                            }
3253
                            
3254
                            logMetacat.info("MetacatHandler.handleUploadAction - " +
3255
                            		        "in Exception: fileExists is " + fileExists);
3256
                            logMetacat.error("MetacatHandler.handleUploadAction - " +
3257
                            		         "Upload Error: " + ee.getMessage());
3258
                            throw ee;
3320
                            // set content type and other response header fields
3321
                            // first
3322
                            output += "<?xml version=\"1.0\"?>";
3323
                            output += "<success>";
3324
                            output += "<docid>" + docid + "</docid>";
3325
                            output += "<size>" + size + "</size>";
3326
                            output += "</success>";
3259 3327
                        }
3260 3328

  
3261
                        EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"),
3262
                                username, docid, "upload");
3263
                        // Force replication this data file
3264
                        // To data file, "insert" and update is same
3265
                        // The fourth parameter is null. Because it is
3266
                        // notification server
3267
                        // and this method is in MetaCatServerlet. It is
3268
                        // original command,
3269
                        // not get force replication info from another metacat
3270
                        ForceReplicationHandler frh = new ForceReplicationHandler(
3271
                                docid, "insert", false, null);
3272
                        logMetacat.debug("MetacatHandler.handleUploadAction - " +
3273
                        		         "ForceReplicationHandler created: " + 
3274
                        		         frh.toString());
3329
                    } catch (Exception e) {
3275 3330

  
3276
                        // set content type and other response header fields
3277
                        // first
3278 3331
                        output += "<?xml version=\"1.0\"?>";
3279
                        output += "<success>";
3280
                        output += "<docid>" + docid + "</docid>";
3281
                        output += "<size>" + size + "</size>";
3282
                        output += "</success>";
3332
                        output += "<error>";
3333
                        output += e.getMessage();
3334
                        output += "</error>";
3283 3335
                    }
3284

  
3285
                } catch (Exception e) {
3286

  
3336
                } else {
3337
                    // the field did not contain a file
3287 3338
                    output += "<?xml version=\"1.0\"?>";
3288 3339
                    output += "<error>";
3289
                    output += e.getMessage();
3340
                    output += "The uploaded data did not contain a valid file.";
3290 3341
                    output += "</error>";
3291 3342
                }
3292 3343
            } else {
3293
                // the field did not contain a file
3344
                // Error bcse docid missing or file missing
3294 3345
                output += "<?xml version=\"1.0\"?>";
3295 3346
                output += "<error>";
3296
                output += "The uploaded data did not contain a valid file.";
3347
                output += "The uploaded data did not contain a valid docid "
3348
                    + "or valid file.";
3297 3349
                output += "</error>";
3298 3350
            }
3299
        } else {
3300
            // Error bcse docid missing or file missing
3301
            output += "<?xml version=\"1.0\"?>";
3302
            output += "<error>";
3303
            output += "The uploaded data did not contain a valid docid "
3304
                + "or valid file.";
3305
            output += "</error>";
3306 3351
        }
3307 3352

  
3353

  
3354
        if(params.containsKey("qformat")) {
3355
            qformat = params.get("qformat")[0];
3356
        }
3357
        
3308 3358
        if (qformat == null || qformat.equals("xml")) {
3309 3359
            response.setContentType("text/xml");
3310 3360
            out.println(output);

Also available in: Unified diff