diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 8f8963a..fb2050d 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -121,6 +121,9 @@ uses + + + uses diff --git a/docker/logback.xml b/docker/logback.xml index fbd1ad6..906b20e 100644 --- a/docker/logback.xml +++ b/docker/logback.xml @@ -16,7 +16,7 @@ - + diff --git a/src/main/java/org/gcube/data/access/storagehub/handlers/items/Item2NodeConverter.java b/src/main/java/org/gcube/data/access/storagehub/handlers/items/Item2NodeConverter.java index 8118162..e8def90 100644 --- a/src/main/java/org/gcube/data/access/storagehub/handlers/items/Item2NodeConverter.java +++ b/src/main/java/org/gcube/data/access/storagehub/handlers/items/Item2NodeConverter.java @@ -114,6 +114,7 @@ public class Item2NodeConverter { @SuppressWarnings("rawtypes") Class returnType = field.getType(); Values values = getObjectValue(returnType, field.get(object)); + if (values == null) continue; if (values.isMulti()) newNode.setProperty(attribute.value(), values.getValues()); else newNode.setProperty(attribute.value(), values.getValue()); } catch (Exception e ) { @@ -165,7 +166,7 @@ public class Item2NodeConverter { @SuppressWarnings({ "rawtypes" }) public static Values getObjectValue(Class returnType, Object value) throws Exception{ - + if (value== null) return null; if (returnType.equals(String.class)) return new Values(new StringValue((String) value)); if (returnType.isEnum()) return new Values(new StringValue(((Enum) value).toString())); if (returnType.equals(Calendar.class)) return new Values(new DateValue((Calendar) value)); diff --git a/src/main/java/org/gcube/data/access/storagehub/handlers/items/ItemHandler.java b/src/main/java/org/gcube/data/access/storagehub/handlers/items/ItemHandler.java index 0c3c396..1457046 100644 --- a/src/main/java/org/gcube/data/access/storagehub/handlers/items/ItemHandler.java +++ b/src/main/java/org/gcube/data/access/storagehub/handlers/items/ItemHandler.java @@ -6,8 +6,6 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import javax.inject.Inject; import javax.inject.Singleton; @@ -59,8 +57,7 @@ import org.slf4j.LoggerFactory; @Singleton public class ItemHandler { - - @Inject + @Inject AccountingHandler accountingHandler; @Inject @@ -73,48 +70,48 @@ public class ItemHandler { VersionHandler versionHandler; @Inject - StorageBackendHandler storageBackendHandler; + StorageBackendHandler storageBackendHandler; - private static ExecutorService executor = Executors.newFixedThreadPool(100); + // private static ExecutorService executor = Executors.newFixedThreadPool(100); - @Inject Node2ItemConverter node2Item; - @Inject Item2NodeConverter item2Node; + @Inject + Node2ItemConverter node2Item; + @Inject + Item2NodeConverter item2Node; private static Logger log = LoggerFactory.getLogger(ItemHandler.class); - - //TODO: accounting - //provider URI host - //resourceOwner user - //consumer write - //in caso di versione - update con -delta - public String create(T parameters) throws Exception{ + // TODO: accounting + // provider URI host + // resourceOwner user + // consumer write + // in caso di versione - update con -delta + public String create(T parameters) throws Exception { Session ses = parameters.getSession(); Node destination; try { destination = ses.getNodeByIdentifier(parameters.getParentId()); - }catch(RepositoryException inf) { + } catch (RepositoryException inf) { throw new IdNotFoundException(parameters.getParentId()); } - if (!node2Item.checkNodeType(destination, FolderItem.class)) + if (!node2Item.checkNodeType(destination, FolderItem.class)) throw new InvalidItemException("the destination item is not a folder"); authChecker.checkWriteAuthorizationControl(ses, parameters.getUser(), destination.getIdentifier(), true); - try { Node newNode = null; switch (parameters.getMangedType()) { case FILE: - newNode = create((FileCreationParameters)parameters, destination); + newNode = create((FileCreationParameters) parameters, destination); break; case FOLDER: - newNode = create((FolderCreationParameters)parameters, destination); + newNode = create((FolderCreationParameters) parameters, destination); break; case ARCHIVE: - newNode = create((ArchiveStructureCreationParameter)parameters, destination); + newNode = create((ArchiveStructureCreationParameter) parameters, destination); break; case URL: newNode = create((URLCreationParameters) parameters, destination); @@ -125,7 +122,7 @@ public class ItemHandler { default: throw new InvalidCallParameters("Item not supported"); } - log.debug("item with id {} correctly created",newNode.getIdentifier()); + log.debug("item with id {} correctly created", newNode.getIdentifier()); return newNode.getIdentifier(); } finally { if (parameters.getSession().getWorkspace().getLockManager().isLocked(destination.getPath())) @@ -134,122 +131,132 @@ public class ItemHandler { } - - private Node create(FolderCreationParameters params, Node destination) throws Exception{ + private Node create(FolderCreationParameters params, Node destination) throws Exception { Utils.acquireLockWithWait(params.getSession(), destination.getPath(), false, params.getUser(), 10); Node newNode = Utils.createFolderInternally(params, accountingHandler, false); params.getSession().save(); return newNode; } - private Node create(FileCreationParameters params, Node destination) throws Exception{ - Node newNode = createFileItemInternally(params.getSession(), destination, params.getStream(), params.getName(), params.getDescription(), params.getFileDetails(), params.getUser(), true); + private Node create(FileCreationParameters params, Node destination) throws Exception { + Node newNode = createFileItemInternally(params.getSession(), destination, params.getStream(), params.getName(), + params.getDescription(), params.getFileDetails(), params.getUser(), true); params.getSession().save(); versionHandler.checkinContentNode(newNode); - log.info("file with id {} correctly created",newNode.getIdentifier()); - return newNode; + log.info("file with id {} correctly created", newNode.getIdentifier()); + return newNode; } - private Node create(URLCreationParameters params, Node destination) throws Exception{ + private Node create(URLCreationParameters params, Node destination) throws Exception { Utils.acquireLockWithWait(params.getSession(), destination.getPath(), false, params.getUser(), 10); - Node newNode = Utils.createURLInternally(params.getSession(), destination, params.getName(), params.getUrl(), params.getDescription(), params.getUser(), accountingHandler); + Node newNode = Utils.createURLInternally(params.getSession(), destination, params.getName(), params.getUrl(), + params.getDescription(), params.getUser(), accountingHandler); params.getSession().save(); return newNode; } - private Node create(ArchiveStructureCreationParameter params, Node destination) throws Exception{ + private Node create(ArchiveStructureCreationParameter params, Node destination) throws Exception { Utils.acquireLockWithWait(params.getSession(), destination.getPath(), false, params.getUser(), 10); - FolderCreationParameters folderParameters = FolderCreationParameters.builder().name(params.getParentFolderName()).author(params.getUser()).on(destination.getIdentifier()).with(params.getSession()).build(); + FolderCreationParameters folderParameters = FolderCreationParameters.builder() + .name(params.getParentFolderName()).author(params.getUser()).on(destination.getIdentifier()) + .with(params.getSession()).build(); Node parentDirectoryNode = Utils.createFolderInternally(folderParameters, accountingHandler, false); params.getSession().save(); try { if (params.getSession().getWorkspace().getLockManager().isLocked(destination.getPath())) params.getSession().getWorkspace().getLockManager().unlock(destination.getPath()); - } catch (Throwable t){ + } catch (Throwable t) { log.warn("error unlocking {}", destination.getPath(), t); } Set fileNodes = new HashSet<>(); + HashMap directoryNodeMap = new HashMap<>(); - HashMap directoryNodeMap = new HashMap<>(); - - try (ArchiveInputStream input = new ArchiveStreamFactory() - .createArchiveInputStream(new BufferedInputStream(params.getStream(), 1024*64))){ - ArchiveEntry entry; - while ((entry = input.getNextEntry()) != null) { - String entirePath = entry.getName(); - log.debug("reading new entry ------> ", entirePath); - if (entry.isDirectory()) { - log.debug("creating directory with entire path {} ", entirePath); - createPath(entirePath, directoryNodeMap, parentDirectoryNode, params.getSession(), params.getUser()); - continue; - } else { - try { - String name = entirePath.replaceAll("([^/]*/)*(.*)", "$2"); - String parentPath = entirePath.replaceAll("(([^/]*/)*)(.*)", "$1"); - log.debug("creating file with entire path {}, name {}, parentPath {} ", entirePath, name, parentPath); - Node fileNode = null; - long fileSize = entry.getSize(); - FormDataContentDisposition fileDetail = FormDataContentDisposition.name(name).size(fileSize).build(); - if (parentPath.isEmpty()) - fileNode = createFileItemInternally(params.getSession(), parentDirectoryNode, input, name, "", fileDetail, params.getUser(), false); - else { - Node parentNode = directoryNodeMap.get(parentPath); - if (parentNode ==null) - parentNode = createPath(parentPath, directoryNodeMap, parentDirectoryNode, params.getSession(), params.getUser()); - - fileNode = createFileItemInternally(params.getSession(), parentNode, input, name, "",fileDetail, params.getUser(), false); - } - fileNodes.add(fileNode); - }catch(Throwable e) { - log.warn("error getting file {}",entry.getName(),e); + ArchiveInputStream input = new ArchiveStreamFactory() + .createArchiveInputStream(new BufferedInputStream(params.getStream())); + + ArchiveEntry entry; + while ((entry = input.getNextEntry()) != null) { + String entirePath = entry.getName(); + log.debug("reading new entry ------> {} ", entirePath); + if (entry.isDirectory()) { + log.debug("creating directory with entire path {} ", entirePath); + createPath(entirePath, directoryNodeMap, parentDirectoryNode, params.getSession(), params.getUser()); + } else { + try { + String name = entirePath.replaceAll("([^/]*/)*(.*)", "$2"); + String parentPath = entirePath.replaceAll("(([^/]*/)*)(.*)", "$1"); + log.debug("creating file with entire path {}, name {}, parentPath {} ", entirePath, name, + parentPath); + Node fileNode = null; + long fileSize = entry.getSize(); + FormDataContentDisposition fileDetail = FormDataContentDisposition.name(name).size(fileSize) + .build(); + if (parentPath.isEmpty()) { + fileNode = createFileItemInternally(params.getSession(), parentDirectoryNode, input, name, "", + fileDetail, params.getUser(), false); + } else { + Node parentNode = directoryNodeMap.get(parentPath); + if (parentNode == null) + parentNode = createPath(parentPath, directoryNodeMap, parentDirectoryNode, + params.getSession(), params.getUser()); + fileNode = createFileItemInternally(params.getSession(), parentNode, input, name, "", + fileDetail, params.getUser(), false); } + fileNodes.add(fileNode); + } catch (Throwable e) { + log.warn("error getting file {}", entry.getName(), e); } } - } + log.info("archive {} uploading finished ", params.getParentFolderName()); params.getSession().save(); for (Node node : fileNodes) versionHandler.checkinContentNode(node); return parentDirectoryNode; } - private Node createPath(String parentPath, Map directoryNodeMap, Node rootNode, Session ses, String user) throws StorageHubException, RepositoryException{ + private Node createPath(String parentPath, Map directoryNodeMap, Node rootNode, Session ses, + String user) throws StorageHubException, RepositoryException { String[] parentPathSplit = parentPath.split("/"); - String name = parentPathSplit[parentPathSplit.length-1]; + String name = parentPathSplit[parentPathSplit.length - 1]; StringBuilder relParentPath = new StringBuilder(); - for (int i = 0 ; i<=parentPathSplit.length-2; i++) + for (int i = 0; i <= parentPathSplit.length - 2; i++) relParentPath.append(parentPathSplit[i]).append("/"); if (relParentPath.toString().isEmpty()) { - FolderCreationParameters folderParameters = FolderCreationParameters.builder().name(name).author(user).on(rootNode.getIdentifier()).with(ses).build(); - Node createdNode = Utils.createFolderInternally(folderParameters, accountingHandler, false); - directoryNodeMap.put(name+"/", createdNode); + FolderCreationParameters folderParameters = FolderCreationParameters.builder().name(name).author(user) + .on(rootNode.getIdentifier()).with(ses).build(); + Node createdNode = Utils.createFolderInternally(folderParameters, accountingHandler, false); + directoryNodeMap.put(name + "/", createdNode); return createdNode; - }else { + } else { Node relParentNode = directoryNodeMap.get(relParentPath.toString()); - if (relParentNode==null) { + if (relParentNode == null) { relParentNode = createPath(relParentPath.toString(), directoryNodeMap, rootNode, ses, user); } - FolderCreationParameters folderParameters = FolderCreationParameters.builder().name(name).author(user).on(relParentNode.getIdentifier()).with(ses).build(); + FolderCreationParameters folderParameters = FolderCreationParameters.builder().name(name).author(user) + .on(relParentNode.getIdentifier()).with(ses).build(); Node createdNode = Utils.createFolderInternally(folderParameters, accountingHandler, false); directoryNodeMap.put(relParentPath.append(name).append("/").toString(), createdNode); return createdNode; } } - - private Node create(GCubeItemCreationParameters params, Node destination) throws Exception{ + private Node create(GCubeItemCreationParameters params, Node destination) throws Exception { Utils.acquireLockWithWait(params.getSession(), destination.getPath(), false, params.getUser(), 10); - Node newNode = Utils.createGcubeItemInternally(params.getSession(), destination, params.getItem().getName(), params.getItem().getDescription(), params.getUser(), params.getItem(), accountingHandler); + Node newNode = Utils.createGcubeItemInternally(params.getSession(), destination, params.getItem().getName(), + params.getItem().getDescription(), params.getUser(), params.getItem(), accountingHandler); params.getSession().save(); return newNode; } - private Node createFileItemInternally(Session ses, Node destinationNode, InputStream stream, String name, String description, FormDataContentDisposition fileDetails, String login, boolean withLock) throws RepositoryException, StorageHubException{ + private Node createFileItemInternally(Session ses, Node destinationNode, InputStream stream, String name, + String description, FormDataContentDisposition fileDetails, String login, boolean withLock) + throws RepositoryException, StorageHubException { log.trace("UPLOAD: starting preparing file"); @@ -257,11 +264,11 @@ public class ItemHandler { FolderItem destinationItem = node2Item.getItem(destinationNode, Excludes.ALL); StorageBackendFactory sbf = storageBackendHandler.get(destinationItem.getBackend()); + StorageBackend sb = sbf.create(destinationItem.getBackend()); String relativePath = destinationNode.getPath(); - String newNodePath = Paths.append(Paths.getPath(destinationNode.getPath()), name).toPath(); log.info("new node path is {}", newNodePath); @@ -270,112 +277,121 @@ public class ItemHandler { newNode = ses.getNode(newNodePath); authChecker.checkWriteAuthorizationControl(ses, login, newNode.getIdentifier(), false); - AbstractFileItem item = fillItemWithContent(stream, sb, name, description, fileDetails, relativePath,login); + AbstractFileItem item = fillItemWithContent(stream, sb, name, description, fileDetails, relativePath, + login); if (withLock) { try { - ses.getWorkspace().getLockManager().lock(newNode.getPath(), true, true, 0,login); - }catch (LockException le) { + ses.getWorkspace().getLockManager().lock(newNode.getPath(), true, true, 0, login); + } catch (LockException le) { throw new ItemLockedException(le); } } try { versionHandler.checkoutContentNode(newNode); - log.trace("replacing content of class {}",item.getContent().getClass()); - item2Node.replaceContent(newNode,item, ItemAction.UPDATED); + log.trace("replacing content of class {}", item.getContent().getClass()); + item2Node.replaceContent(newNode, item, ItemAction.UPDATED); String versionName = null; try { Version version = versionHandler.getCurrentVersion(newNode); versionName = version.getName(); - }catch(RepositoryException e) { + } catch (RepositoryException e) { log.warn("current version of {} cannot be retreived", item.getId()); } accountingHandler.createFileUpdated(item.getTitle(), versionName, ses, newNode, login, false); ses.save(); - }finally { + } catch (Throwable t) { + log.error("error saving item", t); + } finally { if (withLock) { - if (ses!=null && ses.hasPendingChanges()) + if (ses != null && ses.hasPendingChanges()) ses.save(); ses.getWorkspace().getLockManager().unlock(newNode.getPath()); } } - } - else { + } else { authChecker.checkWriteAuthorizationControl(ses, login, destinationNode.getIdentifier(), true); - AbstractFileItem item = fillItemWithContent(stream, sb, name, description, fileDetails, relativePath, login); + AbstractFileItem item = fillItemWithContent(stream, sb, name, description, fileDetails, relativePath, + login); if (withLock) { try { log.debug("trying to acquire lock"); Utils.acquireLockWithWait(ses, destinationNode.getPath(), false, login, 10); - }catch (LockException le) { + } catch (LockException le) { throw new ItemLockedException(le); } } try { - newNode = item2Node.getNode(destinationNode, item); + newNode = item2Node.getNode(destinationNode, item); accountingHandler.createEntryCreate(item.getTitle(), ses, newNode, login, false); ses.save(); - }finally { - if (withLock) ses.getWorkspace().getLockManager().unlock(destinationNode.getPath()); + } catch (Throwable t) { + log.error("error saving item", t); + throw new BackendGenericError(t); + } finally { + if (withLock) + ses.getWorkspace().getLockManager().unlock(destinationNode.getPath()); } versionHandler.makeVersionableContent(newNode); - accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), item.getContent().getMimeType(), ses, login, destinationNode, false); + accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), item.getContent().getMimeType(), + ses, login, destinationNode, false); } - //TODO: Utils.updateParentSize() + // TODO: Utils.updateParentSize() return newNode; } - - - private AbstractFileItem fillItemWithContent(InputStream stream, StorageBackend storageBackend, String name, String description, FormDataContentDisposition fileDetails, String relPath, String login) throws BackendGenericError{ + private AbstractFileItem fillItemWithContent(InputStream stream, StorageBackend storageBackend, String name, + String description, FormDataContentDisposition fileDetails, String relPath, String login) + throws BackendGenericError { log.trace("UPLOAD: filling content"); - ContentHandler handler = getContentHandler(stream, storageBackend, name, fileDetails, relPath, login); - AbstractFileItem item =handler.buildItem(name, description, login); - return item ; + ContentHandler handler = getContentHandler(stream, storageBackend, name, fileDetails, relPath, login); + AbstractFileItem item = handler.buildItem(name, description, login); + return item; } - private ContentHandler getContentHandler(InputStream stream, StorageBackend storageBackend, String name, FormDataContentDisposition fileDetails, String relPath, String login) throws BackendGenericError { + private ContentHandler getContentHandler(InputStream stream, StorageBackend storageBackend, String name, + FormDataContentDisposition fileDetails, String relPath, String login) throws BackendGenericError { log.trace("UPLOAD: handling content"); long start = System.currentTimeMillis(); - log.trace("UPLOAD: writing the stream - start"); + log.trace("UPLOAD: writing the stream - start"); try { MetaInfo info = null; - try(InputStream is1 = stream){ - log.debug("UPLOAD: upload on {} - start",storageBackend.getClass()); - if (fileDetails !=null && fileDetails.getSize()>0) { - log.debug("UPLOAD: file size set is {} Byte",fileDetails.getSize()); - info = storageBackend.upload(is1, relPath, name, fileDetails.getSize(), login); - } else - info = storageBackend.upload(is1, relPath, name, login); + try { + log.debug("UPLOAD: upload on {} - start", storageBackend.getClass()); + if (fileDetails != null && fileDetails.getSize() > 0) { + log.debug("UPLOAD: file size set is {} Byte", fileDetails.getSize()); + info = storageBackend.upload(stream, relPath, name, fileDetails.getSize(), login); + } else + info = storageBackend.upload(stream, relPath, name, login); log.debug("UPLOAD: upload on storage - stop"); - }catch (Throwable e) { - log.error("error writing content",e ); + } catch (Throwable e) { + log.error("error writing content", e); throw e; } - - ContentHandler handler =null; + ContentHandler handler = null; String mimeType; log.debug("UPLOAD: reading the mimetype - start"); - try(InputStream is1 = new BufferedInputStream(storageBackend.download(info.getStorageId()))){ + try (InputStream is1 = new BufferedInputStream(storageBackend.download(info.getStorageId()))) { org.apache.tika.mime.MediaType mediaType = null; TikaConfig config = TikaConfig.getDefaultConfig(); Detector detector = config.getDetector(); TikaInputStream tikastream = TikaInputStream.get(is1); Metadata metadata = new Metadata(); - mediaType = detector.detect(tikastream, metadata); + mediaType = detector.detect(tikastream, metadata); mimeType = mediaType.getBaseType().toString(); handler = contenthandlerFactory.create(mimeType); - log.debug("UPLOAD: reading the mimetype {} - finished in {}",mimeType, System.currentTimeMillis()-start); + log.debug("UPLOAD: reading the mimetype {} - finished in {}", mimeType, + System.currentTimeMillis() - start); } catch (Throwable e) { - log.error("error retrieving mimeType",e); + log.error("error retrieving mimeType", e); throw new RuntimeException(e); - } + } if (handler.requiresInputStream()) try (InputStream is1 = new BufferedInputStream(storageBackend.download(info.getStorageId()))) { @@ -386,16 +402,19 @@ public class ItemHandler { log.debug("UPLOAD: the file type doesn't requires input stream"); handler.initiliseSpecificContent(name, mimeType); } - log.debug("UPLOAD: writing the stream - finished in {}",System.currentTimeMillis()-start); + log.debug("UPLOAD: writing the stream - finished in {}", System.currentTimeMillis() - start); + handler.getContent().setData(NodeConstants.CONTENT_NAME); handler.getContent().setStorageId(info.getStorageId()); handler.getContent().setSize(info.getSize()); handler.getContent().setRemotePath(info.getRemotePath()); handler.getContent().setPayloadBackend(info.getPayloadBackend()); - log.debug("UPLOAD: content payload seta as {} ", handler.getContent().getPayloadBackend()); + log.debug("UPLOAD: content payload set as {} ", handler.getContent().getPayloadBackend()); + return handler; - }catch (Throwable e) { + } catch (Throwable e) { + log.error("error writing file", e); throw new BackendGenericError(e); } diff --git a/src/main/java/org/gcube/data/access/storagehub/services/ItemsCreator.java b/src/main/java/org/gcube/data/access/storagehub/services/ItemsCreator.java index e4d08f8..2af3e94 100644 --- a/src/main/java/org/gcube/data/access/storagehub/services/ItemsCreator.java +++ b/src/main/java/org/gcube/data/access/storagehub/services/ItemsCreator.java @@ -1,5 +1,6 @@ package org.gcube.data.access.storagehub.services; +import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; @@ -239,7 +240,7 @@ public class ItemsCreator extends Impersonable{ Session ses = null; String toReturn = null; - try{ + try(InputStream is = new BufferedInputStream(stream)){ long size = fileDetail.getSize(); @@ -316,10 +317,10 @@ public class ItemsCreator extends Impersonable{ Session ses = null; String toReturn = null; - try{ + try(InputStream is = new BufferedInputStream(stream)){ ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context)); - ItemsParameterBuilder builder = ArchiveStructureCreationParameter.builder().parentName(parentFolderName).stream(stream).fileDetails(fileDetail) + ItemsParameterBuilder builder = ArchiveStructureCreationParameter.builder().parentName(parentFolderName).stream(is).fileDetails(fileDetail) .on(id).with(ses).author(currentUser); toReturn = itemHandler.create(builder.build()); diff --git a/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/.gitignore b/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/.gitignore new file mode 100644 index 0000000..1f37442 --- /dev/null +++ b/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/.gitignore @@ -0,0 +1,2 @@ +/MockStorage.java +/MockStorageFactory.java diff --git a/src/test/java/org/gcube/data/access/storages/mock/MockStorage.java b/src/test/java/org/gcube/data/access/storages/mock/MockStorage.java new file mode 100644 index 0000000..e438b51 --- /dev/null +++ b/src/test/java/org/gcube/data/access/storages/mock/MockStorage.java @@ -0,0 +1,112 @@ +package org.gcube.data.access.storages.mock; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; +import java.util.UUID; + +import org.apache.commons.io.IOUtils; +import org.gcube.common.storagehub.model.exceptions.StorageIdNotFoundException; +import org.gcube.common.storagehub.model.items.nodes.Content; +import org.gcube.common.storagehub.model.items.nodes.PayloadBackend; +import org.gcube.common.storagehub.model.storages.MetaInfo; +import org.gcube.common.storagehub.model.storages.StorageBackend; + +public class MockStorage extends StorageBackend { + + public MockStorage(PayloadBackend payloadConfiguration) { + super(payloadConfiguration); + } + + @Override + public MetaInfo onCopy(Content content, String newParentPath, String newName) { + // TODO Auto-generated method stub + return null; + } + + @Override + public MetaInfo onMove(Content content, String newParentPath) { + // TODO Auto-generated method stub + return null; + } + + @Override + public void delete(String id) { + // TODO Auto-generated method stub + + } + + @Override + public MetaInfo upload(InputStream stream, String relativePath, String name, String user) { + return this.upload(stream, relativePath, name, -1l, user); + } + + @Override + public MetaInfo upload(InputStream stream, String relativePath, String name, Long size, String user) { + + try { + InputStream is = new BufferedInputStream(stream); + String id = UUID.randomUUID().toString(); + File targetFile = new File("/tmp/" + id); + targetFile.createNewFile(); + OutputStream outStream = new FileOutputStream(targetFile); + + byte[] buffer = new byte[1024 * 64]; + int bytesRead; + while ((bytesRead = is.read(buffer, 0, 1024 * 64)) != -1) { + outStream.write(buffer, 0, bytesRead); + } + IOUtils.closeQuietly(outStream); + + return new MetaInfo(targetFile.length(), id, user, getPayloadConfiguration()); + } catch (Exception e) { + throw new RuntimeException(e); + } + + } + + @Override + public InputStream download(Content item) throws StorageIdNotFoundException { + return this.download(item.getStorageId()); + } + + @Override + public InputStream download(String id) throws StorageIdNotFoundException { + try { + return new FileInputStream(new File("/tmp/" + id)); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Override + public Map getFileMetadata(String id) { + // TODO Auto-generated method stub + return null; + } + + @Override + public MetaInfo upload(InputStream stream, String relativePath, String name, String storageId, Long size, + String user) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getTotalSizeStored() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getTotalItemsCount() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/test/java/org/gcube/data/access/storages/mock/MockStorageFactory.java b/src/test/java/org/gcube/data/access/storages/mock/MockStorageFactory.java new file mode 100644 index 0000000..c22d399 --- /dev/null +++ b/src/test/java/org/gcube/data/access/storages/mock/MockStorageFactory.java @@ -0,0 +1,23 @@ +package org.gcube.data.access.storages.mock; + +import javax.inject.Singleton; + +import org.gcube.common.storagehub.model.exceptions.InvalidCallParameters; +import org.gcube.common.storagehub.model.items.nodes.PayloadBackend; +import org.gcube.common.storagehub.model.storages.StorageBackend; +import org.gcube.common.storagehub.model.storages.StorageBackendFactory; + +@Singleton +public class MockStorageFactory implements StorageBackendFactory { + + @Override + public String getName() { + return "MockStorage"; + } + + @Override + public StorageBackend create(PayloadBackend payloadConfiguration) throws InvalidCallParameters { + return new MockStorage(payloadConfiguration); + } + +}