diff --git a/src/main/java/it/inaf/oats/vospacebackend/VOSpaceBackendApplication.java b/src/main/java/it/inaf/oats/vospacebackend/VOSpaceBackendApplication.java index e0fd7a91126d3f09a6c642aff186177b0ba6ad44..4acd5f6c2163a3237c67c485399fadc9f2160710 100644 --- a/src/main/java/it/inaf/oats/vospacebackend/VOSpaceBackendApplication.java +++ b/src/main/java/it/inaf/oats/vospacebackend/VOSpaceBackendApplication.java @@ -1,7 +1,27 @@ -/** +/**_____________________________________________________________________________ * - * @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; diff --git a/src/main/java/it/inaf/oats/vospacebackend/VOSpaceBackendResource.java b/src/main/java/it/inaf/oats/vospacebackend/VOSpaceBackendResource.java index 5a3b324191d63646b7ebd0565900f6f36515dd51..6edb31a03030567d912c4e820699dbbcdbae97ab 100644 --- a/src/main/java/it/inaf/oats/vospacebackend/VOSpaceBackendResource.java +++ b/src/main/java/it/inaf/oats/vospacebackend/VOSpaceBackendResource.java @@ -1,9 +1,29 @@ +/**_____________________________________________________________________________ + * + * 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.apache.commons.io.IOUtils; 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; @@ -45,30 +69,73 @@ import org.restlet.data.Status; 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 { - Representation result = null; - log.info("Entering in POST operation"); + Representation result = null; + log.info("Entering in POST operation"); - if (entity != null) { + if (entity != null) { - if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) { - log.info("Correctly Using MULTIPART_FORM_DATA"); + if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) { + log.info("Correctly Using MULTIPART_FORM_DATA"); - try { - result = this.uploadFile(entity); - } catch (Exception e) { - result = this.printMessage(e.getMessage()); - } + try { + result = this.uploadFile(entity); + } catch (Exception e) { + result = this.printMessage(e.getMessage()); + } - } else { - // POST request with unexpected type. - setStatus(Status.CLIENT_ERROR_BAD_REQUEST); - result = this.printMessage("POST request with unexpected type."); - } + } else { + // POST request with unexpected type. + setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + result = this.printMessage("POST request with unexpected type."); + } } else { @@ -77,7 +144,7 @@ public class VOSpaceBackendResource extends ServerResource { result = this.printMessage("POST request with no entity."); } - return result; + return result; } @@ -119,6 +186,7 @@ public class VOSpaceBackendResource extends ServerResource { private Representation uploadFile(Representation entity) throws Exception { Representation result = null; + // 1/ Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); @@ -129,84 +197,146 @@ 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(); - byte[] data = IOUtils.toByteArray(is); - // 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 { - ConfigReader myConf = new ConfigReader("VOSpace.properties"); - tmpStorageRoot = myConf.getProperty("fs.posix.tmp.storage.root"); - } catch (Exception e) { - ExceptionMessage exMsg = new ExceptionMessage(); - log.debug(MessageFormat.format( - exMsg.getMessage("UNABLE_TO_READ_PROPERTIES"), "VOSpace.properties")); - throw new VOSpaceBackendException(MessageFormat.format( - exMsg.getMessage("PROPERTY_NOT_FOUND"), "fs.posix.tmp.storage.root", "VOSpace.properties")); - } - // Create the temporary directory, if needed - File path = new File(tmpStorageRoot); - if (!path.exists()) { - boolean status = path.mkdirs(); - } - // 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(); - 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."); - } - + 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); + // Get temporary document root from configuration file + String tmpStorageRoot = new String(); + try { + ConfigReader myConf = new ConfigReader("VOSpace.properties"); + tmpStorageRoot = myConf.getProperty("fs.posix.tmp.storage.root"); + } catch (Exception e) { + ExceptionMessage exMsg = new ExceptionMessage(); + log.debug(MessageFormat.format( + exMsg.getMessage("UNABLE_TO_READ_PROPERTIES"), "VOSpace.properties")); + throw new VOSpaceBackendException(MessageFormat.format( + exMsg.getMessage("PROPERTY_NOT_FOUND"), "fs.posix.tmp.storage.root", "VOSpace.properties")); + } + // Create the temporary directory, if needed + File path = new File(tmpStorageRoot); + if (!path.exists()) { + boolean status = path.mkdirs(); + } + // 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)) { + 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; diff --git a/src/main/java/it/inaf/oats/vospacebackend/exceptions/ExceptionMessage.java b/src/main/java/it/inaf/oats/vospacebackend/exceptions/ExceptionMessage.java index 8e6a82e8707897fb1f22d94891883d286b3e5759..44585e34471b96b6ea394b47d41cd03d09643c31 100644 --- a/src/main/java/it/inaf/oats/vospacebackend/exceptions/ExceptionMessage.java +++ b/src/main/java/it/inaf/oats/vospacebackend/exceptions/ExceptionMessage.java @@ -1,7 +1,28 @@ -/** +/**_____________________________________________________________________________ * - * @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; diff --git a/src/main/java/it/inaf/oats/vospacebackend/exceptions/VOSpaceBackendException.java b/src/main/java/it/inaf/oats/vospacebackend/exceptions/VOSpaceBackendException.java index 3089099c9a1caf8f6713750a4fa544b1a472e1aa..d909ebae0ec12625c71bc41b698188cf8ac27081 100644 --- a/src/main/java/it/inaf/oats/vospacebackend/exceptions/VOSpaceBackendException.java +++ b/src/main/java/it/inaf/oats/vospacebackend/exceptions/VOSpaceBackendException.java @@ -1,7 +1,29 @@ -/** +/**_____________________________________________________________________________ * - * @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; diff --git a/src/main/java/it/inaf/oats/vospacebackend/exceptions/exceptionMessages.properties b/src/main/java/it/inaf/oats/vospacebackend/exceptions/exceptionMessages.properties index 62540df84655a686d1508efef7cb22c29be891d9..b78f712f68c07feee6c2726a84db4e3e2020c528 100644 --- a/src/main/java/it/inaf/oats/vospacebackend/exceptions/exceptionMessages.properties +++ b/src/main/java/it/inaf/oats/vospacebackend/exceptions/exceptionMessages.properties @@ -1,3 +1,29 @@ +/**_____________________________________________________________________________ + * + * 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. @@ -10,4 +36,4 @@ UNABLE_TO_GET_DB_CONNECTION="Unable to get the database connection" ERROR_DISABLING_DB_AUTOCOMMIT="There was an error disabling the database autocommit" DB_OPERATION_NOT_RECOGNIZED="File metadata database operation not recognized. Admitted only SET, GET, DELETE" UNABLE_TO_READ_PROPERTIES="Unable to read properties file {0}" -PROPERTY_NOT_FOUND="Property {0} not found in file {1}" \ No newline at end of file +PROPERTY_NOT_FOUND="Property {0} not found in file {1}" diff --git a/src/main/java/it/inaf/oats/vospacebackend/implementation/DatabaseNodePersistenceExt.java b/src/main/java/it/inaf/oats/vospacebackend/implementation/DatabaseNodePersistenceExt.java new file mode 100644 index 0000000000000000000000000000000000000000..248934467f2a213c69ca7bb9abe17c9582e95f41 --- /dev/null +++ b/src/main/java/it/inaf/oats/vospacebackend/implementation/DatabaseNodePersistenceExt.java @@ -0,0 +1,77 @@ +/**_____________________________________________________________________________ + * + * 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.implementation; + +import ca.nrc.cadc.auth.IdentityManager; +import ca.nrc.cadc.vos.server.DatabaseNodePersistence; +import ca.nrc.cadc.vos.server.NodeDAO; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; + +import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; + +/** + * Database Persistence for VO-Space Nodes. + * Implementation built extending the openCADC implementation. + * @author Sara Bertocco (INAF-OATs) + */ +public class DatabaseNodePersistenceExt extends DatabaseNodePersistence { + + /** + * Constructor + */ + public DatabaseNodePersistenceExt() { + super(new NodeDAO.NodeSchema("node", "nodeproperty", false), "/deleted_nodes"); + } + + @Override + /** + * Get the IdentityManager to skip X509 auth using an HTTP one. + */ + protected IdentityManager getIdentityManager() { + IdentityManager IM = new IdentityManagerImpl(); + return IM; + } + + @Override + /** + * Providing test data source. + */ + protected DataSource getDataSource() { + + MysqlDataSource dataSource = new MysqlDataSource(); + dataSource.setDatabaseName("cadctest"); + dataSource.setUser("oatsops"); + dataSource.setPassword("Peper0ne"); + dataSource.setServerName("localhost"); + + return dataSource; + } + +} diff --git a/src/main/java/it/inaf/oats/vospacebackend/implementation/FileRecord.java b/src/main/java/it/inaf/oats/vospacebackend/implementation/FileRecord.java index a37316ccbaad4a40debeacf1607c29f9c5813f6f..4c11ede3d69b63b4a0a6978366c2a8c48acfb6f0 100644 --- a/src/main/java/it/inaf/oats/vospacebackend/implementation/FileRecord.java +++ b/src/main/java/it/inaf/oats/vospacebackend/implementation/FileRecord.java @@ -1,8 +1,27 @@ -/* - * 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. - */ +/**_____________________________________________________________________________ + * + * 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.implementation; import java.util.HashMap; @@ -13,7 +32,8 @@ public class FileRecord { private static final String original_file_name = "original_file_name"; private static final String stored_file_name = "stored_file_name"; private static final String md5_checksum = "md5_checksum"; - private static final String relative_path = "relative_path"; + private static final String relative_path = "relative_path"; + private static final String vosuri = "vosuri"; private HashMap content = new HashMap<String, String>(); @@ -23,6 +43,7 @@ public class FileRecord { content.put(stored_file_name, ""); content.put(md5_checksum, ""); content.put(relative_path, ""); + content.put(vosuri, ""); } @@ -35,6 +56,16 @@ public class FileRecord { } + public FileRecord(String fileName, String stored_f_name, String md5Checksum, String relativePath, String vosuriStr) { + + content.put(original_file_name, fileName); + content.put(stored_file_name, stored_f_name); + content.put(md5_checksum, md5Checksum); + content.put(relative_path, relativePath); + content.put(vosuri, vosuriStr); + + } + public HashMap getFileRecord() { return content; diff --git a/src/main/java/it/inaf/oats/vospacebackend/implementation/IdentityManagerImpl.java b/src/main/java/it/inaf/oats/vospacebackend/implementation/IdentityManagerImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..4731ae63eb770663b574fdbd1956101dd4f61b43 --- /dev/null +++ b/src/main/java/it/inaf/oats/vospacebackend/implementation/IdentityManagerImpl.java @@ -0,0 +1,150 @@ +/**_____________________________________________________________________________ + * + * 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.implementation; + +import ca.nrc.cadc.auth.HttpPrincipal; +import ca.nrc.cadc.auth.IdentityManager; +import java.security.Principal; +import java.sql.Types; +import java.util.Iterator; +import java.util.Set; +import javax.security.auth.Subject; + + +/** + * + * @author bertocco + */ +public class IdentityManagerImpl implements IdentityManager { + + @Override + /** + * Create a subject from the specified owner object. This is the reverse + * of toOwner(Subject). The returned subject must include at least one + * Principal but need not contain any credentials. + * + * @param owner + * @return + */ + /** + * Override + * Return subject containing the owner as an HttpPrincipal + */ + public Subject toSubject(Object owner) { + + Subject subject = new Subject(); + Principal httpPrincipal = new HttpPrincipal(owner.toString()); + Set<Principal> principals = subject.getPrincipals(); + principals.add(httpPrincipal); + + return subject; + } + + @Override + /** + * Convert the specified subject into an arbitrary object. This is the reverse + * of toSubject(owner). The persistable object must capture the identity (the + * principal from the subject) but generally does not capture the credentials. + * + * @param subject + * @return arbitary owner object to be persisted + */ + /** + * Overrride + * Return simple hardcoded owner name from subject's HttpPrincipal + */ + public Object toOwner(Subject subject) { + + String owner = null; + Set<Principal> principals = subject.getPrincipals(); + Iterator<Principal> principalsIterator = principals.iterator(); + while (principalsIterator.hasNext()) { + Principal next = principalsIterator.next(); + if (next instanceof HttpPrincipal) { + String HttpName = next.getName(); + if (HttpName.equals("Marco")) { + owner = HttpName; + } else { + owner = "whois:" + HttpName + "?"; + } + } + } + + return owner; + } + + @Override + /** + * Get the SQL TYPE for the column that stores the object + * returned by toOwner(Subject); + * + * @see java.sql.Types + * @see java.sql.PreparedStatement.setObject(int,Object,int) + * @return a valid SQL type for use with a PreparedStatement + */ + /** + * Override + * Hardcode VARCHAR sql Type for the ownerID column in Node table. + */ + public int getOwnerType() { + return Types.VARCHAR; + } + + @Override + /** + * Convert the specified subject to a suitable string representation of the + * owner. This should normally be an X509 distinguished name if IVOA + * single-sign-on has been implemented. + * + * @see VOS.PROPERTY_URI_CREATOR + * @param subject + * @return string representation of the owner (principal) + */ + /** + * Override + * Returns ownerID String, i.e. itself. + * NOTE: code pasted directly from the toOwner method. + */ + public String toOwnerString(Subject subject) { + + String owner = null; + Set<Principal> principals = subject.getPrincipals(); + Iterator<Principal> principalsIterator = principals.iterator(); + while (principalsIterator.hasNext()) { + Principal next = principalsIterator.next(); + if (next instanceof HttpPrincipal) { + String HttpName = next.getName(); + if (HttpName.equals("Marco")) { + owner = HttpName; + } else { + owner = "whois:" + HttpName + "?"; + } + } + } + + return owner; + } + +} diff --git a/src/main/java/it/inaf/oats/vospacebackend/implementation/TransferGeneratorImpl.java b/src/main/java/it/inaf/oats/vospacebackend/implementation/TransferGeneratorImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..c4460d1b80e9211445692c89ac6296d7d9e6ae5c --- /dev/null +++ b/src/main/java/it/inaf/oats/vospacebackend/implementation/TransferGeneratorImpl.java @@ -0,0 +1,275 @@ +/**_____________________________________________________________________________ + * + * 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.implementation; + +import java.io.FileNotFoundException; +import java.net.URL; +import java.net.MalformedURLException; +import java.util.List; +import java.util.HashMap; +import java.util.ArrayList; +import java.sql.*; + +import ca.nrc.cadc.net.TransientException; +import ca.nrc.cadc.uws.Job; +import ca.nrc.cadc.uws.Parameter; +import ca.nrc.cadc.vos.Protocol; +import ca.nrc.cadc.vos.VOSURI; +import ca.nrc.cadc.vos.View; +import it.inaf.oats.vospacebackend.exceptions.ExceptionMessage; +import it.inaf.oats.vospacebackend.exceptions.VOSpaceBackendException; +import java.sql.SQLException; +import java.util.UUID; +import java.util.logging.Level; +import javax.sql.DataSource; + +import org.apache.log4j.Logger; + +/** + * + * @author bertocco + */ +public class TransferGeneratorImpl { + + Logger log = Logger.getLogger(TransferGeneratorImpl.class); + + HashMap myResult = new <String, Object>HashMap(); + + public List<URL> getURLs(VOSURI target, + Protocol protocol, + View view, + Job job, + List<Parameter> additionalParams) + throws FileNotFoundException, TransientException { + + + + log.debug("VOSURI received: " + target.toString()); + + List<URL> result = new ArrayList(); + + // Get the node path, file name, vosuri + String path = target.getPath(); + String fileName = target.getName(); + String vosuri = target.toString(); + + + // Generate the unique file identifier (storageFileID) + String unique_file_id_str = UUID.randomUUID().toString(); + log.debug("Unique file identifyer " + unique_file_id_str); + + // Needs a couple of keys private and public + // The backend servlet should have access to the public key + + // the url must encapsulate a token/password generated here + // and which will be verified in the resource servlet + // the VOSURI which will be used by the beckend to do the new NODE(VOSURI) + // and to perform the node metadata staging + String security_token = "fake_token_jkshdcukckq"; + + + HashMap myMetadata; + try { + myMetadata = setPutRequest(unique_file_id_str, vosuri); + + if ((boolean)myMetadata.get("ifSuccessful")) { + log.debug("File Put Request correctly set"); + } else { + log.debug("File Put Request NOT set, return null"); + return null; + } + + result.add(new URL("http://localhost/VOSpaceBackend/service/myResource/" + + security_token + "/" + + unique_file_id_str + "/" + + fileName)); + + } catch (MalformedURLException e) { + log.debug("Error parsing target"); + return null; + } catch (VOSpaceBackendException ex) { + java.util.logging.Logger.getLogger(TransferGeneratorImpl.class.getName()).log(Level.SEVERE, null, ex); + } catch (SQLException ex) { + java.util.logging.Logger.getLogger(TransferGeneratorImpl.class.getName()).log(Level.SEVERE, null, ex); + } + + return result; + } + + + public HashMap getPutRequests(String stored_f_name) + throws VOSpaceBackendException, SQLException { + + String myOp = "GET_PUT_REQ"; + + String myQuery = "SELECT * FROM vosbackend.RequestedPuts " + + "WHERE storedFileID=?"; + + FileRecord myParams = new FileRecord("", stored_f_name, "", ""); + + myResult = excuteQuery(myOp, myQuery, myParams); + + return myResult; + + } + + + public HashMap setPutRequest(String stored_f_name, String vosuriStr) + throws VOSpaceBackendException, SQLException { + + String myOp = "SET_PUT_REQ"; + String myQuery = "INSERT INTO vosbackend.RequestedPuts " + + "(storedFileID, vosuri)" + + " VALUES (?, ?);"; + log.debug("Received params: " + stored_f_name + " " + vosuriStr ); + FileRecord myParams = new FileRecord("", stored_f_name, "", "", vosuriStr); + + myResult = excuteQuery(myOp, myQuery, myParams); + + return myResult; + + } + + private HashMap excuteQuery(String operation, String query, FileRecord fileToStore) + throws VOSpaceBackendException, SQLException { + PreparedStatement preparedStatementInsert = null; + + Connection dbConnection = null; + + try { + dbConnection = getDBConnection(); + log.debug("Database connection get"); + } catch (SQLException e) { + log.fatal(e); + ExceptionMessage exMsg = new ExceptionMessage(); + throw new VOSpaceBackendException(ExceptionMessage.getMessage("UNABLE_TO_GET_DB_CONNECTION")); + } + + try { + dbConnection.setAutoCommit(false); + log.debug("Autocommit set false"); + } catch (SQLException e) { + log.fatal(e); + ExceptionMessage exMsg = new ExceptionMessage(); + throw new VOSpaceBackendException(ExceptionMessage.getMessage("ERROR_DISABLING_DB_AUTOCOMMIT")); + } + + // Starts JDBC Transaction + PreparedStatement preparedQuery = null; + try { + + HashMap fileToStoreFields = fileToStore.getFileRecord(); + ResultSet rs = null; + int rowCounter = 0; + switch (operation) { + case "GET_PUT_REQ": + preparedQuery = dbConnection.prepareStatement(query); + preparedQuery.setString(1, (String)fileToStoreFields.get("stored_file_name")); + preparedQuery.setString(2, (String)fileToStoreFields.get("vosuri")); + rs = preparedQuery.executeQuery(); + rowCounter = 0; + while (rs.next()) { + rowCounter = rowCounter +1; + myResult.put("stored_file_name", rs.getString("storedFileID")); + myResult.put("vosuri", rs.getString("vosuri")); + } + if (rowCounter == 0) { + log.debug("GET_PUT_REQ: query successfully executed. File " + (String)fileToStoreFields.get("stored_file_name") + " not found"); + myResult.put("ifSuccessful", false); + } else { + log.debug("GET_PUT_REQ: query successfully executed. File " + (String)fileToStoreFields.get("stored_file_name") + " found"); + myResult.put("ifSuccessful", true); + } + break; + case "SET_PUT_REQ": + log.debug("Going to prepare query for SET_PUT_REQ operation"); + preparedQuery = dbConnection.prepareStatement(query); + log.debug("StoredFileName to set in db reqs " + (String)fileToStoreFields.get("stored_file_name")); + log.debug("vosuri to set in db reqs " + (String)fileToStoreFields.get("vosuri")); + preparedQuery.setString(1, (String)fileToStoreFields.get("stored_file_name")); + preparedQuery.setString(2, (String)fileToStoreFields.get("vosuri")); + log.debug("Going to execute query"); + preparedQuery.executeUpdate(); + log.debug("Query executed"); + dbConnection.commit(); + log.debug("Query committed"); + myResult.put("ifSuccessful", true); + break; + + default: + myResult.put("ifSuccessful", false); + ExceptionMessage exMsg = new ExceptionMessage(); + log.fatal(ExceptionMessage.getMessage("DB_OPERATION_NOT_RECOGNIZED")); + throw new VOSpaceBackendException(ExceptionMessage.getMessage("DB_OPERATION_NOT_RECOGNIZED")); + } + + dbConnection.setAutoCommit(true); + + dbConnection.close(); + + } catch (SQLException e) { + + log.error("SQLException exception executing SET file" + e.getMessage()); + dbConnection.rollback(); + + } finally { + + if (preparedQuery != null) { + preparedQuery.close(); + } + + if (dbConnection != null) { + dbConnection.close(); + } + + } + + return myResult; + + } + + + public Connection getDBConnection() throws SQLException { + + String url = "jdbc:mysql://localhost:3306/vosbackend"; + String username = "vosbackadmin"; + String password = "Peper0ne"; + + System.out.println("Connecting database..."); + + Connection connection; + try { + connection = DriverManager.getConnection(url, username, password); + System.out.println("Database connected!"); + } catch (SQLException e) { + throw new IllegalStateException("Cannot connect the database!", e); + + } + + return connection; + + } + +} diff --git a/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackImplFactory.java b/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackImplFactory.java index 5bf3225b0732137a73a5c405845db4d3941d3105..c015f90699cc554c7233dc8c70359913ef417e82 100644 --- a/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackImplFactory.java +++ b/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackImplFactory.java @@ -1,7 +1,28 @@ -/* +/**_____________________________________________________________________________ * - * @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.implementation; import java.util.List; diff --git a/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackMetadata.java b/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackMetadata.java index 325fc70590cf6471a8ead1578e267d50b5263925..ad819be0a3a26a019b468ca1bc1d6c1908eedcf8 100644 --- a/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackMetadata.java +++ b/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackMetadata.java @@ -1,3 +1,28 @@ +/**_____________________________________________________________________________ + * + * 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.implementation; import java.sql.Connection; @@ -78,6 +103,7 @@ public class VOSpaceBackMetadata { myResult = excuteQuery(myOp, myQuery, myParams); return myResult; + } public HashMap getFile(String orig_f_name) @@ -110,6 +136,40 @@ public class VOSpaceBackMetadata { } + + public HashMap getPutRequests(String stored_f_name) + throws VOSpaceBackendException, SQLException { + + String myOp = "GET_PUT_REQ"; + + String myQuery = "SELECT * FROM vosbackend.RequestedPuts " + + "WHERE storedFileID=?"; + + FileRecord myParams = new FileRecord("", stored_f_name, "", ""); + + myResult = excuteQuery(myOp, myQuery, myParams); + + return myResult; + + } + + + public HashMap setPutRequest(String stored_f_name, String vosuriStr) + throws VOSpaceBackendException, SQLException { + + String myOp = "SET_PUT_REQ"; + String myQuery = "INSERT INTO vosbackend.PutRequests " + + "(storedFileID, vosuri)" + + " VALUES (?, ?);"; + FileRecord myParams = new FileRecord("", stored_f_name, "", "", vosuriStr); + + + myResult = excuteQuery(myOp, myQuery, myParams); + + return myResult; + + } + private HashMap excuteQuery(String operation, String query, FileRecord fileToStore) throws VOSpaceBackendException, SQLException { PreparedStatement preparedStatementInsert = null; @@ -139,7 +199,8 @@ public class VOSpaceBackMetadata { try { HashMap fileToStoreFields = fileToStore.getFileRecord(); - + ResultSet rs = null; + int rowCounter = 0; switch (operation) { case "SET": log.debug("Going to prepare query for SET operation"); @@ -158,8 +219,8 @@ public class VOSpaceBackMetadata { case "GET": preparedQuery = dbConnection.prepareStatement(query); preparedQuery.setString(1, (String)fileToStoreFields.get("original_file_name")); - ResultSet rs = preparedQuery.executeQuery(); - int rowCounter = 0; + rs = preparedQuery.executeQuery(); + rowCounter = 0; while (rs.next()) { rowCounter = rowCounter +1; myResult.put("original_file_name", rs.getString("original_file_name")); @@ -182,6 +243,39 @@ public class VOSpaceBackMetadata { dbConnection.commit(); myResult.put("ifSuccessful", true); break; + case "GET_PUT_REQ": + preparedQuery = dbConnection.prepareStatement(query); + preparedQuery.setString(1, (String)fileToStoreFields.get("stored_file_name")); + preparedQuery.setString(1, (String)fileToStoreFields.get("vosuri")); + rs = preparedQuery.executeQuery(); + rowCounter = 0; + while (rs.next()) { + rowCounter = rowCounter +1; + myResult.put("stored_file_name", rs.getString("storedFileID")); + myResult.put("vosuri", rs.getString("vosuri")); + } + if (rowCounter == 0) { + log.debug("GET_PUT_REQ: query successfully executed. File " + (String)fileToStoreFields.get("stored_file_name") + " not found"); + myResult.put("ifSuccessful", false); + } else { + log.debug("GET_PUT_REQ: query successfully executed. File " + (String)fileToStoreFields.get("stored_file_name") + " found"); + myResult.put("ifSuccessful", true); + } + break; + case "SET_PUT_REQ": + log.debug("Going to prepare query for SET operation"); + preparedQuery = dbConnection.prepareStatement(query); + preparedQuery.setString(1, (String)fileToStoreFields.get("stored_file_name")); + preparedQuery.setString(3, (String)fileToStoreFields.get("md5_checksum")); + preparedQuery.setString(4, (String)fileToStoreFields.get("relative_path")); + log.debug("Going to execute query"); + preparedQuery.executeUpdate(); + log.debug("Query executed"); + dbConnection.commit(); + log.debug("Query committed"); + myResult.put("ifSuccessful", true); + break; + default: myResult.put("ifSuccessful", false); ExceptionMessage exMsg = new ExceptionMessage(); diff --git a/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackPosix.java b/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackPosix.java index 8f9884240cb396ac384a113d8cfbc6da412569f0..167ae7f696b63598d127094195a3a236f6b6ae7d 100644 --- a/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackPosix.java +++ b/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackPosix.java @@ -1,9 +1,38 @@ +/**_____________________________________________________________________________ + * + * 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.implementation; import it.inaf.oats.vospacebackend.exceptions.ExceptionMessage; import it.inaf.oats.vospacebackend.exceptions.VOSpaceBackendException; import it.inaf.oats.vospacebackend.utils.ConfigReader; +import ca.nrc.cadc.vos.Node; +import ca.nrc.cadc.vos.VOSURI; +import ca.nrc.cadc.vos.NodeNotFoundException; + import java.sql.SQLException; import java.io.File; import java.io.IOException; @@ -51,15 +80,44 @@ public class VOSpaceBackPosix implements VOSpaceBackend { VOSpaceBackMetadata metadata = new VOSpaceBackMetadata(); String relativePath = this.createPathFromString(md5_sum); + + // Gets metadata on frontend + HashMap putReqData = metadata.getPutRequests(tmp_file); + DatabaseNodePersistenceExt dbNodePers; + Node myNode; + boolean myresult = (boolean)putReqData.get("ifSuccessful"); + if(myresult) { + String vosuri = (String)putReqData.get("vosuri"); + dbNodePers = new DatabaseNodePersistenceExt(); + try { + myNode = dbNodePers.get(new VOSURI(vosuri)); + } catch (NodeNotFoundException e) { + log.debug("Node not found. Need to abort the operation"); + } catch (Exception e) { + log.debug("Node not found. Need to abort the operation"); + } + + } else + log.debug("File frontend metadata NOT retrieved. Need to abort the operation"); + // Needs syncronisation BEGIN + // Set busy state HOW ??? + + // Set metadata on frontend + // HERE SET NODE - HOW ??? + + + // sets backend metadata HashMap fileMetadata = metadata.setFile(orig_f_name, tmp_file, md5_sum, relativePath); + // Writes the file on the file system boolean result = (boolean)fileMetadata.get("ifSuccessful"); if (result) { log.debug("File metadata successfully saved. I'm going to store the file content"); this.fileFromTmpToFinalStorageArea(tmp_file, relativePath, "MOVE"); } else - log.debug("File metadata NOT saved. Need to abort the operation"); + log.debug("File backend metadata NOT saved. Need to abort the operation"); + // Needs syncronisation END return result; diff --git a/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackend.java b/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackend.java index 6db114d35152e4e8a06e4c0446e62f55ac6415be..dc7f32ae6bd1e397345b02392019a2d6ddd2344b 100644 --- a/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackend.java +++ b/src/main/java/it/inaf/oats/vospacebackend/implementation/VOSpaceBackend.java @@ -1,3 +1,27 @@ +/**_____________________________________________________________________________ + * + * 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.implementation; import it.inaf.oats.vospacebackend.exceptions.VOSpaceBackendException; diff --git a/src/main/java/it/inaf/oats/vospacebackend/utils/ConfigReader.java b/src/main/java/it/inaf/oats/vospacebackend/utils/ConfigReader.java index 3f1da4de60725c88a434cf2f12bca2433ca6757b..efd3bedbcbd9ddcffb0d5ae53e394675b9df9eaf 100644 --- a/src/main/java/it/inaf/oats/vospacebackend/utils/ConfigReader.java +++ b/src/main/java/it/inaf/oats/vospacebackend/utils/ConfigReader.java @@ -1,3 +1,27 @@ +/**_____________________________________________________________________________ + * + * 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.utils; import it.inaf.oats.vospacebackend.exceptions.VOSpaceBackendException; diff --git a/src/main/java/it/inaf/oats/vospacebackend/utils/VOSURIParser.java b/src/main/java/it/inaf/oats/vospacebackend/utils/VOSURIParser.java index 9cd4b0d7722241033a0d47246946e00089c4da2d..9815ec7936fd6046ba02d75c81c2e513bfc701f9 100644 --- a/src/main/java/it/inaf/oats/vospacebackend/utils/VOSURIParser.java +++ b/src/main/java/it/inaf/oats/vospacebackend/utils/VOSURIParser.java @@ -1,3 +1,27 @@ +/**_____________________________________________________________________________ + * + * 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.utils; import java.util.HashMap; diff --git a/src/main/java/resources/VOSpace.properties b/src/main/java/resources/VOSpace.properties index 23e4bb09e24b603e18310ee40a77bdce0718ef00..43a544bdaefa804c553c05fb074c19773cd15848 100644 --- a/src/main/java/resources/VOSpace.properties +++ b/src/main/java/resources/VOSpace.properties @@ -1,3 +1,28 @@ +/**_____________________________________________________________________________ + * + * 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. + * _____________________________________________________________________________ + **/ + # VOSpace Backend Implementation to be used it.inaf.oats.vospacebackendimplementation.VOSpaceBackendImpl = it.inaf.oats.vospacebackend.implementation.VOSpaceBackPosix diff --git a/src/main/java/resources/license.txt b/src/main/java/resources/license.txt new file mode 100644 index 0000000000000000000000000000000000000000..716b2c96556af5c5d541a569177b8d59c76172ea --- /dev/null +++ b/src/main/java/resources/license.txt @@ -0,0 +1,24 @@ +/**_____________________________________________________________________________ + * + * 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. + * _____________________________________________________________________________ + **/ \ No newline at end of file diff --git a/src/main/java/sql/create_vospace_backend.sql b/src/main/java/sql/create_vospace_backend.sql index 209112b0eb30bf6a382b57184c4f9d1a38e531a3..db455548f1e0da1ee2132060551f15832bca6aad 100644 --- a/src/main/java/sql/create_vospace_backend.sql +++ b/src/main/java/sql/create_vospace_backend.sql @@ -1,3 +1,28 @@ + +/**_____________________________________________________________________________ + * + * 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. + * _____________________________________________________________________________ + **/ CREATE DATABASE vosbackend; use vosbackend; @@ -16,3 +41,10 @@ CREATE TABLE StoredFiles ALTER TABLE StoredFiles ADD INDEX f_name (original_file_name); +CREATE TABLE RequestedPuts +( + storedFileID varchar(255) NOT NULL, + vosuri varchar(255) NOT NULL, + PRIMARY KEY (storedFileID) +) ENGINE=InnoDB; + diff --git a/src/main/java/test/ServiceTester.java b/src/main/java/test/ServiceTester.java new file mode 100644 index 0000000000000000000000000000000000000000..f826fd6ff087d7fea2a7054a8e19c1904d3f91d6 --- /dev/null +++ b/src/main/java/test/ServiceTester.java @@ -0,0 +1,109 @@ +/**_____________________________________________________________________________ + * + * 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 test; + +import ca.nrc.cadc.uws.Job; +import ca.nrc.cadc.uws.Parameter; +import ca.nrc.cadc.vos.Protocol; +import ca.nrc.cadc.vos.VOSURI; +import ca.nrc.cadc.vos.View; +import ca.nrc.cadc.vos.VOS; +import ca.nrc.cadc.net.HttpUpload; +import ca.nrc.cadc.net.HttpRequestProperty; + +import it.inaf.oats.vospacebackend.implementation.TransferGeneratorImpl; +import java.net.URL; +import java.util.List; +import java.util.ArrayList; +import java.io.File; +import java.io.IOException; + +/** + * + * @author bertocco + */ +public class ServiceTester { + + public static void main(String[] args) { + + TransferGeneratorImpl transGen = new TransferGeneratorImpl(); + + String vosuriStr = "vos://ia2.inaf.it!vospace/filepath/clean_build.tar.gz"; + try { + VOSURI target = new VOSURI(vosuriStr); + String URL1 = "http://example.com/someplace/123"; + Protocol protocol = new Protocol(VOS.PROTOCOL_HTTP_GET, URL1, null) ; + View view = null; + Job job = new Job(); + List<Parameter> additionalParams = null; + + List<URL> urlList = transGen.getURLs(target, protocol, view, job, additionalParams); + + + String fileToManage; + String urlStr = ""; + for (URL temp : urlList) { + urlStr = temp.toString(); + System.out.println(urlStr); + + //{security_token}/{unique_file_id_string}/{fileToManage} + + fileToManage = urlStr.substring(urlStr.lastIndexOf("/")+1); + } + + URL url = new URL(urlStr); + File localFile = new File ("/root/clean_build.tar.gz"); + List<HttpRequestProperty> httpRequestProperties = new ArrayList<HttpRequestProperty>(); + int maxRetries =3; + HttpUpload upload; + upload = new HttpUpload(localFile, url); + + System.out.println("calling HttpUpload.setRequestProperties with " + httpRequestProperties.size() + " props"); + upload.setRequestProperties(httpRequestProperties); + upload.setMaxRetries(maxRetries); + + upload.run(); + + if (upload.getThrowable() != null) { + // allow illegal arugment exceptions through + if (upload.getThrowable() instanceof IllegalArgumentException) { + throw (IllegalArgumentException) upload.getThrowable(); + } else { + try { + throw new IOException("failed to upload file", upload.getThrowable()); + } catch (NoSuchMethodError e) { + // Java5 does not have the above constructor. + throw new IOException("failed to upload file: " + upload.getThrowable().getMessage()); + } + } + } + + } catch (Exception e) { + System.out.println("Exception :" + e.getMessage()); + } // end of externals try/catch + + } // end of method + +} diff --git a/src/main/java/test/VOSURIParserTest.java b/src/main/java/test/VOSURIParserTest.java index 820f118e9af0d3dfdcf33262ff3985bf6010db63..e875d52feb6d48c55d2d42153d1a29080a47fbcf 100644 --- a/src/main/java/test/VOSURIParserTest.java +++ b/src/main/java/test/VOSURIParserTest.java @@ -1,3 +1,28 @@ +/**_____________________________________________________________________________ + * + * 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 test; import java.util.HashMap; diff --git a/src/main/webapp/META-INF/context.xml b/src/main/webapp/META-INF/context.xml index f13e3cbd7631a12579c70e4b691f82a8dddb6afa..63add891fa224dec619ec4b794f866c1ab43d52e 100644 --- a/src/main/webapp/META-INF/context.xml +++ b/src/main/webapp/META-INF/context.xml @@ -1,4 +1,30 @@ <?xml version="1.0" encoding="UTF-8"?> +<!-- +/**_____________________________________________________________________________ + * + * 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. + * _____________________________________________________________________________ + **/ +--> <Context path="/vospaceBackend" docBase="vospace" crossContext="true" reloadable="true" debug="1"> diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index f1716f446ebf38e920288dc83eb21e0424284c1d..38c50b955011099bf85ce708942a498c6c21d32d 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -1,4 +1,31 @@ <?xml version="1.0" encoding="UTF-8"?> +<!-- +/**_____________________________________________________________________________ + * + * 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. + * _____________________________________________________________________________ + **/ +--> + <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>VOSpace Backend</display-name> @@ -18,4 +45,4 @@ </servlet-mapping> </web-app> - \ No newline at end of file +