Commit 739325fd authored by Sara Bertocco's avatar Sara Bertocco
Browse files

Original author Is Marco Molinaro. The class has been changed by Sara Bertocco in getDataSource.

parent 5d140905
Loading
Loading
Loading
Loading
+24 −4
Original line number Diff line number Diff line
@@ -46,13 +46,33 @@
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>ca.nrc.cadc.</groupId>
            <artifactId>cadcUtil</artifactId>
            <groupId>com.mysql.jdbc</groupId>
            <artifactId>mysql-connector</artifactId>
            <version>0.0</version>
        </dependency>
        <dependency>
            <groupId>com.mysql.jdbc</groupId>
            <artifactId>mysql-connector</artifactId>
            <groupId>ca.nrc.cadc.vos</groupId>
            <artifactId>cadcVOS</artifactId>
            <version>0.0</version>
        </dependency>
        <dependency>
            <groupId>ca.nrc.cadc.vos</groupId>
            <artifactId>cadcVOS-Server</artifactId>
            <version>0.0</version>
        </dependency>
        <dependency>
            <groupId>ca.nrc.cadc.uws</groupId>
            <artifactId>cadcUWS-Server</artifactId>
            <version>0.0</version>
        </dependency>
        <dependency>
            <groupId>ca.nrc.cadc.uws</groupId>
            <artifactId>cadcUWS</artifactId>
            <version>0.0</version>
        </dependency>
        <dependency>
            <groupId>ca.nrc.cadc</groupId>
            <artifactId>cadcUtil</artifactId>
            <version>0.0</version>
        </dependency>
        <dependency>
+1 −0
Original line number Diff line number Diff line
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDE+lo2eU+lvaqNfyE1K2PiEcVw7D6VoaVeFTQuEow9uGM4NyTLSYz4yD82jXNhheFzo399BzAk+EqdEWmx7P24OOSsCY1rzbohdeLgMDCURh+6wh4aeeKn+XptA9aXRs/Vlji337gG6BGS4zPICwCSWiHg8eGT0R3EIECLdI0+MBAwBxmOkkvrFMdDxcO4lRTu5lCWpR0gUvUde22kVUXfNoK+ZG92Ujxaw9zZapFaUEdDeCzFQCR9Bn4V4yWmSaJrVY+SsACzqz4A4vQ+FQh1+ayrNsoa0kgpPFCnv+Hv0lpPQ23yPpWO8mvdHs/OtJ5jT+jwdr/C3p87CFCkhGtP root@marmolada.oats.inaf.it
+2 −2
Original line number Diff line number Diff line
@@ -38,8 +38,8 @@ public class VOSpaceBackendApplication extends Application {
        Router router = new Router(getContext());
        // Defines a route for the resource "list of items"
        router.attach("/myResource", VOSpaceBackendResource.class );
        router.attach("/myResource/{security_token}/{unique_file_id_string}/{fileToManage}", VOSpaceBackendResource.class );
        //router.attach("/vospaceBackendGet", VOSpaceBackendResourceGet.class );
        //router.attach("/myResource/{security_token}/{unique_file_id_string}/{fileToManage}", VOSpaceBackendResource.class );
        router.attach("/myResource/{parameters}",  VOSpaceBackendResource.class );      
        
        return router;

+166 −34
Original line number Diff line number Diff line
@@ -34,17 +34,22 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Iterator;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
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.codec.binary.Hex;

import org.restlet.resource.Get;
import org.restlet.resource.Post;
@@ -62,6 +67,18 @@ import org.restlet.ext.fileupload.RestletFileUpload;
import org.restlet.data.MediaType;
import org.restlet.data.Status;

import java.security.InvalidKeyException;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;  
import java.io.OutputStream;
import java.util.UUID;
import javax.xml.bind.DatatypeConverter;

import ca.nrc.cadc.util.RsaSignatureGenerator;
import ca.nrc.cadc.util.RsaSignatureVerifier;
import java.security.MessageDigest;

/**
 *
 * @author bertocco
@@ -77,31 +94,28 @@ public class VOSpaceBackendResource extends ServerResource {
        
        log.info("Entering in PUT operation");
 
        String security_token = "";
        String unique_file_id_str = ""; 
        String fileToManage = "";       
        String vosuri = null;
        String encodedParameters;
        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);
                encodedParameters = (String)getRequest().getAttributes().get("parameters");
                log.debug("Received encoded parameters : " + encodedParameters);
                vosuri = manageParametersDecoding(encodedParameters);
                log.debug("Received parameters decoded = " + vosuri);
            } catch (Exception e) {
                log.debug("Exception reading string parameters");
                log.debug(e);
            }   
                       
            try {
 
                is = entity.getStream();
                log.debug("Input stream get");
                result = readAndSaveFile(fileToManage, unique_file_id_str, is);
                result = readAndSaveFile(vosuri, is);
                
            } catch (Exception e) {
                result = this.printMessage("File NOT Uploaded! Something went wrong.");    
@@ -112,7 +126,7 @@ public class VOSpaceBackendResource extends ServerResource {
        return result;
    }
    
        
/*        
    @Post
    public Representation doPost(Representation entity) throws Exception {
        
@@ -143,15 +157,16 @@ public class VOSpaceBackendResource extends ServerResource {
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            result = this.printMessage("POST request with no entity.");
        }
        
        return result;
    
    }
*/
    
    @Get
    public Representation doGet(){
        
        Representation result = null;
        
        log.info("Entering in GET operation");
        String fileName = (String)getRequestAttributes().get("fileToManage");
        log.debug("File to download is: " + fileName);
@@ -162,6 +177,7 @@ public class VOSpaceBackendResource extends ServerResource {
            result = this.printMessage("GET request: failed to download file.");  
        }  
        
        
        return result;
    }
    
@@ -172,6 +188,7 @@ public class VOSpaceBackendResource extends ServerResource {
        Representation result;
        
        log.info("Entering in DELETE operation");
        
        String fileName = (String)getRequestAttributes().get("fileToManage");
        log.debug("File to delete is: " + fileName);
        try {       
@@ -179,10 +196,10 @@ public class VOSpaceBackendResource extends ServerResource {
        } catch (Exception e) {         
            result = this.printMessage("DELETE request: failed to delete file " + fileName);  
        }   
         
        return result;                
    }
        
    }
/*    
    private Representation uploadFile(Representation entity) throws Exception {
                
        Representation result = null;
@@ -269,13 +286,14 @@ public class VOSpaceBackendResource extends ServerResource {
        log.debug("fileName = " + origFileName);
        
        
        result = readAndSaveFile(origFileName, unique_file_id_str, fileItemToStore);
        result = readAndSaveFile(vosuri, fileItemToStore);
        
        return result;
        
    }
    */
    
    private Representation readAndSaveFile(String origFileName, String unique_file_id_str, FileItem fi)
    private Representation readAndSaveFile(String vosuri, FileItem fi)
                           throws IOException, VOSpaceBackendException {
                
        Representation result;
@@ -284,22 +302,21 @@ public class VOSpaceBackendResource extends ServerResource {
        InputStream is = fi.getInputStream();
        log.debug("Input stream get");
        
        return readAndSaveFile(origFileName, unique_file_id_str, is);
        return readAndSaveFile(vosuri, is);
        
    }
    
    
    private Representation readAndSaveFile(String origFileName, String unique_file_id_str, InputStream is)
    private Representation readAndSaveFile(String vosuri, InputStream is)
                           throws IOException, VOSpaceBackendException {
        
        Representation result;
        String md5sum = null;
        
        // Generate the unique file identifier (storageFileID)
        String unique_file_id_str = UUID.randomUUID().toString();
        log.debug("Unique file identifyer " + unique_file_id_str);
        
        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);
        // Get temporary document root from configuration file
        String tmpStorageRoot = new String();
        try {
@@ -320,10 +337,14 @@ public class VOSpaceBackendResource extends ServerResource {
        // Seve the temporary file in temporary location with the new unique name
        File savedUploadedFile = new File(path + File.separator + unique_file_id_str);
        FileOutputStream outStream = new FileOutputStream(savedUploadedFile);
        outStream.write(data);
        outStream.close();
        try {
            if (this.storeUploadedFile(unique_file_id_str, origFileName, md5sum)) {  
            md5sum = readWriteAndGetChecksum(is, outStream);
        } catch (Exception e) {
            result = this.printMessage("File NOT Uploaded! Something went wrong." + e.getMessage());    
        }
        
        try {
            if (this.storeUploadedFile(vosuri, unique_file_id_str, md5sum)) {  
                setStatus(Status.SUCCESS_OK); 
                result = this.printMessage("File successfully uploaded");
            } else {                  
@@ -337,7 +358,7 @@ public class VOSpaceBackendResource extends ServerResource {
    }
    
    
    private boolean storeUploadedFile(String tmp_file_name, String orig_f_name, 
    private boolean storeUploadedFile(String vosuri, String tmp_file_name, 
                                      String md5_sum) throws Exception {
        boolean stored = false;
        log.debug("Entering in storeUploadedFile");
@@ -345,7 +366,7 @@ public class VOSpaceBackendResource extends ServerResource {
        log.debug("myVOSpaceFactory created");
        VOSpaceBackend myVOSpace = myVOSpaceFactory.getVOSpaceBackImpl();
        log.debug("myVOSpace get");
        stored = myVOSpace.createFile(orig_f_name, tmp_file_name, md5_sum);
        stored = myVOSpace.createFile(vosuri, tmp_file_name, md5_sum);
        log.debug("File stored: " + stored);
        return stored;
                
@@ -406,6 +427,118 @@ public class VOSpaceBackendResource extends ServerResource {
    }
    
    
    

    private String manageParametersDecoding(String toBeVerified) {
        
        String vosuri;
        int count = 0;
        
        log.debug("manageParametersEncoding BEGIN");
        String urlStr = new String(DatatypeConverter.parseBase64Binary(toBeVerified));
        log.debug("urlStr = " + urlStr);
        vosuri = urlStr.substring(0, urlStr.indexOf("|"));
        log.debug("vosuri = " + vosuri);
        String remaining = urlStr.substring(urlStr.indexOf("|")+1, urlStr.length());
        log.debug("Remaining after get vosuri = " + remaining);
        String signature = remaining.substring(remaining.indexOf("|")+1, remaining.length());
        log.debug("signature = \n" + signature);
        
        // Validation
        log.debug("I am going to create RsaSignatureVerifier");
        RsaSignatureVerifier su = new RsaSignatureVerifier();
        log.debug("Created");
        
        boolean valid = false;
        try {
        valid = su.verify(new ByteArrayInputStream(vosuri.getBytes()),
                    DatatypeConverter.parseBase64Binary(signature));
        } catch (IOException ioe) {
            log.debug("IOException");
            log.debug(ioe.getMessage());
        } catch (InvalidKeyException ike) {
            log.debug("InvalidKeyException");           
            log.debug(ike.getMessage());       
        }
                
        return vosuri;
        
    }
    
    public String readWriteAndGetChecksum(InputStream istream, FileOutputStream ostream) throws Exception {
        
        log.debug("Entering in readWriteAndGetChecksum(InputStream, FileOutputStream)");
        
        int DEFAULT_BUFFER_SIZE = 1024 * 4;
        String checksum = null;
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int bytesRead;
        
        MessageDigest msgDigest = MessageDigest.getInstance("MD5");
        
        // do an initial read to ensure there are bytes in the stream
        try {
            bytesRead = istream.read(buffer, 0, DEFAULT_BUFFER_SIZE);
            if (bytesRead <= 0) {
                // do not allow the creation of zero-length files
                log.debug("Cannot write a zero-length file.");
                throw new Exception("Cannot write a zero-length file.");
            } else
                log.debug("First bytes read OK");
        } catch (IOException ex) {
            String errorMsg = "Upstream exception while reading from "
                + istream.getClass().getName() + ": "
                + ex.getMessage();
            log.debug("IOException in the first byte reading of the incoming file");
            log.debug(errorMsg);
            //throw new TransferAbortedException(errorMsg);
            throw new Exception(errorMsg);
        }

        // Loop reading and writing data.
        msgDigest.update(buffer, 0, bytesRead);
        log.debug("First msgDigest.update OK");
        try {

            while (bytesRead >= 0) {
                ostream.write(buffer, 0, bytesRead);
                try {
                    bytesRead = istream.read(buffer, 0, DEFAULT_BUFFER_SIZE);
                    if(bytesRead > 0) {
                        msgDigest.update(buffer, 0, bytesRead);
                    }
                } catch (IOException ex) {
                    String errorMsg = "Upstream exception while reading from "
                        + istream.getClass().getName() + ": "
                        + ex.getMessage();
                    //throw new TransferAbortedException(errorMsg);                    
                    log.debug(errorMsg);                   
                    log.debug("A Exception in reading/writing file" + ex.getMessage());
                    throw new Exception(errorMsg);
                } 
            } 
            ostream.flush();
            ostream.close();
                
        } catch (IOException ex) {
            log.debug("B Exception in reading/writing file" + ex.getMessage());
        }
        
        //Get the hash's bytes
        byte[] bytes = null;
        try {
            bytes = msgDigest.digest();
        } catch (Exception ex) {
           log.debug("Exception doing msgDigest.digest()");
        }
        String md5_checksum = new String(Hex.encodeHex(bytes));
        log.debug("************************ MD5 checksum calculated: " + md5_checksum);
        
        return md5_checksum;
        
    }
    
    
    private Representation printMessage(String error) {
        
            StringBuilder sb = new StringBuilder("");
@@ -413,5 +546,4 @@ public class VOSpaceBackendResource extends ServerResource {
            sb.append("\n");
            return new StringRepresentation(sb.toString(), MediaType.TEXT_PLAIN);
    }

}
+41 −0
Original line number Diff line number Diff line
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.inaf.oats.vospacebackend.implementation;

import ca.nrc.cadc.uws.server.DatabaseJobPersistence;
import ca.nrc.cadc.uws.server.JobDAO;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
/**
 *
 * @author bertocco
 */
public class DatabaseJobPersistenceImpl extends DatabaseJobPersistence {
 
    

    protected JobDAO.JobSchema getJobSchema() {
        
        return new JobDAO.JobSchema("Job", "JobDetail", false);
        
    }
    
    /**
     * Providing a data source.
     */
    protected DataSource getDataSource() {
    
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setDatabaseName("cadctest");
        dataSource.setUser("oatsops");
        dataSource.setPassword("Peper0ne");
        dataSource.setServerName("localhost");
        
        return dataSource;
    }
    
}
Loading