Commit 393af65f authored by Sara Bertocco's avatar Sara Bertocco
Browse files

Firsst version running with TransferGenerator

parent fb50e994
Loading
Loading
Loading
Loading
+25 −5
Original line number Diff line number Diff line
/**
/**_____________________________________________________________________________
 *
 * @author bertocco
 */
 *                                 OATS - INAF
 *  Osservatorio Astronomico di Tireste - Istituto Nazionale di Astrofisica
 *  Astronomical Observatory of Trieste - National Institute for Astrophysics
 * ____________________________________________________________________________
 *
 * Copyright (C) 20016  Istituto Nazionale di Astrofisica
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * _____________________________________________________________________________
 **/
package it.inaf.oats.vospacebackend;

import org.restlet.Application;
@@ -17,8 +37,8 @@ public class VOSpaceBackendApplication extends Application {
        // Create a router Restlet that defines routes.
        Router router = new Router(getContext());
        // Defines a route for the resource "list of items"
        router.attach("/vospaceBackendSet", VOSpaceBackendResource.class );
        router.attach("/vospaceBackendSet/{fileToManage}", VOSpaceBackendResource.class );
        router.attach("/myResource", VOSpaceBackendResource.class );
        router.attach("/myResource/{security_token}/{unique_file_id_string}/{fileToManage}", VOSpaceBackendResource.class );
        //router.attach("/vospaceBackendGet", VOSpaceBackendResourceGet.class );
        
        return router;
+224 −94
Original line number Diff line number Diff line
/**_____________________________________________________________________________
 *
 *                                 OATS - INAF
 *  Osservatorio Astronomico di Tireste - Istituto Nazionale di Astrofisica
 *  Astronomical Observatory of Trieste - National Institute for Astrophysics
 * ____________________________________________________________________________
 *
 * Copyright (C) 20016  Istituto Nazionale di Astrofisica
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * _____________________________________________________________________________
 **/
package it.inaf.oats.vospacebackend;

import it.inaf.oats.vospacebackend.implementation.VOSpaceBackPosix;
import it.inaf.oats.vospacebackend.implementation.VOSpaceBackMetadata;

import ca.nrc.cadc.util.PropertiesReader;
import it.inaf.oats.vospacebackend.implementation.VOSpaceBackend;
import it.inaf.oats.vospacebackend.implementation.VOSpaceBackImplFactory;
import it.inaf.oats.vospacebackend.exceptions.ExceptionMessage;
@@ -13,22 +33,26 @@ import it.inaf.oats.vospacebackend.utils.ConfigReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.UUID;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils; 
import org.apache.commons.lang.exception.ExceptionUtils;

import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.Delete;
import org.restlet.Request;
import org.restlet.resource.ServerResource;
import org.restlet.representation.Variant;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.representation.FileRepresentation;
@@ -46,6 +70,49 @@ public class VOSpaceBackendResource extends ServerResource {
    
    protected Logger log = Logger.getLogger(VOSpaceBackendResource.class);
    
    @Put
    public Representation doPut(Representation entity, Variant variant) throws Exception {
        
        Representation result = null;
        
        log.info("Entering in PUT operation");
 
        String security_token = "";
        String unique_file_id_str = ""; 
        String fileToManage = "";       
        InputStream is;
        if (entity != null) {
            log.info("Received good entity");
            try {
                log.debug("Trying to read attributes");
                Request request = getRequest();
                log.debug("AAA");
                security_token = (String)getRequest().getAttributes().get("security_token");
                unique_file_id_str = (String)getRequest().getAttributes().get("unique_file_id_string");
                fileToManage = (String)getRequest().getAttributes().get("fileToManage");
                log.debug("\nsecurity token = " + security_token +
                          "\nunique_file_id_str = " + unique_file_id_str +
                          "\nfileToManage = " + fileToManage);
            } catch (Exception e) {
                log.debug("Exception reading string parameters");
            }   
                        
            try {
 
                is = entity.getStream();
                log.debug("Input stream get");
                result = readAndSaveFile(fileToManage, unique_file_id_str, is);
                
            } catch (Exception e) {
                result = this.printMessage("File NOT Uploaded! Something went wrong.");    
            }    
            
        }
        
        return result;
    }
    
        
    @Post
    public Representation doPost(Representation entity) throws Exception {
        
@@ -120,6 +187,7 @@ public class VOSpaceBackendResource extends ServerResource {
                
        Representation result = null;
         
        
        // 1/ Create a factory for disk-based file items
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(1000240);
@@ -129,46 +197,109 @@ public class VOSpaceBackendResource extends ServerResource {
        // generates FileItems.
        RestletFileUpload upload = new RestletFileUpload(factory);
        log.info("RestletFileUpload created");
        // 3/ Request is parsed by the handler which generates a
        // list of FileItems
        FileItemIterator fileIterator = null;
        
        List<FileItem> items;
        
        boolean fileFound = false; 
        String origFileName = null;           
        FileItem fileItemToStore = null;
        Map<String, String> parameters = new HashMap<String, String>();
               
        try {
            fileIterator = upload.getItemIterator(entity);
        } catch (Exception e) {
            log.info(e.toString());
        }
        log.info("Iterator created");

        // Process only the uploaded item called "fileToUpload"
        // and return back
        boolean found = false;            
        while (fileIterator.hasNext() && !found) {
            FileItemStream fi = fileIterator.next(); 
            if (fi.getFieldName().equals("fileToManage")) {
                found = true;
                // Read the original file name
                String origFileName = fi.getName();
                // Check if fileName is present in metadata DB
                VOSpaceBackMetadata metadataDB = new VOSpaceBackMetadata();        
                HashMap myMetadata;    
                myMetadata = metadataDB.getFile(origFileName);
                if ((boolean)myMetadata.get("ifSuccessful")) {
                    log.debug("File Name already present in the database");
                    result = this.printMessage("File Name already present in the database.");
            // 3/ Request is parsed by the handler which generates a list of FileItems
            items = upload.parseRequest(getRequest());
            
            for (final Iterator<FileItem> it = items.iterator(); it.hasNext(); ) {
                FileItem fi = it.next();
                String fileName = fi.getName();
                if (fileName == null) {
                    parameters.put(fi.getFieldName(), new String(fi.get(), "UTF-8"));
                } else {
                    // Read the file content
                    InputStream is = fi.openStream();
                    fileItemToStore = fi;
                    origFileName = fileName;
                    fileFound = true;
                }
            }
        } catch (Exception e) {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            result = this.printMessage("Unable to correctly parse request");
            return result;
        }
        
        if (!fileFound) {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            result = this.printMessage("Unable to find a file to download");
            return result;
        }
        
        String unique_file_id_str;
        String security_token;
        
        if (parameters.isEmpty()) {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            result = this.printMessage("Unable to find parameters in client request");
            return result;
        }
        
        if (origFileName == null) {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            result = this.printMessage("Unable to find original file name in client request");
            return result;
        }
        
        if (parameters.get("unique_file_id_string") == null) {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            result = this.printMessage("No parameter unique_file_id_string found in request");
            return result;
        }
        unique_file_id_str = parameters.get("unique_file_id_string");
        
    
        if (parameters.get("security_token") == null) {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            result = this.printMessage("No parameter security_token found in request");
            return result;
        }
        security_token = parameters.get("security_token");
        
        log.debug("unique_file_id_string = " + unique_file_id_str);
        
        log.debug("security_token = " + security_token);
        
        log.debug("fileName = " + origFileName);
        
        
        result = readAndSaveFile(origFileName, unique_file_id_str, fileItemToStore);
        
        return result;
        
    }
    
    private Representation readAndSaveFile(String origFileName, String unique_file_id_str, FileItem fi)
                           throws IOException, VOSpaceBackendException {
                
        Representation result;
        log.debug("Entering in readAndSaveFile");
        
        InputStream is = fi.getInputStream();
        log.debug("Input stream get");
        
        return readAndSaveFile(origFileName, unique_file_id_str, is);
        
    }
    
    
    private Representation readAndSaveFile(String origFileName, String unique_file_id_str, InputStream is)
                           throws IOException, VOSpaceBackendException {
        
        Representation result;
        
        byte[] data = IOUtils.toByteArray(is);
        log.debug("toByteArray OK");
        // Calculate the file MD5 checksum (so will not need to re-read 
        // the file a new time later
        String md5sum = new String(DigestUtils.md5Hex(data));
        log.debug("MD5Sum del file: " + md5sum);
                    // Calculate a unique file name to store the file                
                    String unique_file_id_str = UUID.randomUUID().toString();
                    log.debug("Unique file identifyer " + unique_file_id_str);
                
                    log.debug("FieldName = " + fi.getFieldName());
                    log.debug("Field " + fi.getFieldName() + " found");
        // Get temporary document root from configuration file
        String tmpStorageRoot = new String();
        try {
@@ -191,22 +322,21 @@ public class VOSpaceBackendResource extends ServerResource {
        FileOutputStream outStream = new FileOutputStream(savedUploadedFile);
        outStream.write(data);
        outStream.close();
        try {
            if (this.storeUploadedFile(unique_file_id_str, origFileName, md5sum)) {  
                setStatus(Status.SUCCESS_OK); 
                result = this.printMessage("File successfully uploaded");
            } else {                  
                result = this.printMessage("File NOT Uploaded! Something went wrong.");
            }
                   
                }
            
            }
        } catch (Exception e) {
            result = this.printMessage("File NOT Uploaded! Something went wrong.");    
        }
            
        return result;            
        
    }
    
    
    private boolean storeUploadedFile(String tmp_file_name, String orig_f_name, 
                                      String md5_sum) throws Exception {
        boolean stored = false;
+24 −3
Original line number Diff line number Diff line
/**
/**_____________________________________________________________________________
 *
 * @author bertocco
 */
 *                                 OATS - INAF
 *  Osservatorio Astronomico di Tireste - Istituto Nazionale di Astrofisica
 *  Astronomical Observatory of Trieste - National Institute for Astrophysics
 * ____________________________________________________________________________
 *
 * Copyright (C) 20016  Istituto Nazionale di Astrofisica
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * _____________________________________________________________________________
 **/

package it.inaf.oats.vospacebackend.exceptions;

import java.util.ResourceBundle;
+25 −3
Original line number Diff line number Diff line
/**
/**_____________________________________________________________________________
 *
 * @author bertocco
 */
 *                                 OATS - INAF
 *  Osservatorio Astronomico di Tireste - Istituto Nazionale di Astrofisica
 *  Astronomical Observatory of Trieste - National Institute for Astrophysics
 * ____________________________________________________________________________
 *
 * Copyright (C) 20016  Istituto Nazionale di Astrofisica
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * _____________________________________________________________________________
 **/


package it.inaf.oats.vospacebackend.exceptions;

import java.util.ResourceBundle;
+27 −1
Original line number Diff line number Diff line
/**_____________________________________________________________________________
 *
 *                                 OATS - INAF
 *  Osservatorio Astronomico di Tireste - Istituto Nazionale di Astrofisica
 *  Astronomical Observatory of Trieste - National Institute for Astrophysics
 * ____________________________________________________________________________
 *
 * Copyright (C) 20016  Istituto Nazionale di Astrofisica
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * _____________________________________________________________________________
 **/


# his properties file contains the messages related to error 
# cases specific to the vospace backend application.

Loading