From a8f40a31ed62a92656c2ae34605539d940d54974 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:48:24 +0200 Subject: [PATCH 01/19] Added prettier maven plugin --- pom.xml | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/pom.xml b/pom.xml index 9607b7ad..47d6086d 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,7 @@ UTF-8 https://repo.icatproject.org/repo github + write @@ -20,6 +21,10 @@ ICAT Repo https://repo.icatproject.org/repo + + com.hubspot.maven.plugins Repo + https://mvnrepository.com/artifact/com.hubspot.maven.plugins/ + @@ -129,6 +134,12 @@ test + + com.hubspot.maven.plugins + prettier-maven-plugin + 0.22 + + @@ -415,10 +426,53 @@ + + com.hubspot.maven.plugins + prettier-maven-plugin + 0.22 + + 2.0.0 + 80 + 4 + false + true + true + + + + src/main/java/**/*.java + src/test/java/**/*.java + + + + + validate + + ${plugin.prettier.goal} + + + + + + + + travis + + + env.TRAVIS + + + + + check + + + + From 6d6461e7d12954c9709aa59b0d1ffd3f45448698 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Thu, 6 Jun 2024 11:32:38 +0200 Subject: [PATCH 02/19] prettier config file and linting check while build. --- README.md | 6 ++++++ pom.xml | 28 ++-------------------------- src/.prettierrc.yaml | 4 ++++ 3 files changed, 12 insertions(+), 26 deletions(-) create mode 100644 src/.prettierrc.yaml diff --git a/README.md b/README.md index 93da61c5..8066012f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ # ids.server: Server part of ICAT Data Server (IDS) +## Linting +The Prettier Maven Plugin is used for linting. it is configured for "check" while build. That's why a build will fail when the files haven't the right format. +execute `mvn prettier:write` before. + +TODO: execute `mvn prettier:wite` while local build but only the `check` while CI build. + [![Build Status](https://github.com/icatproject/ids.server/workflows/CI%20Build/badge.svg?branch=master)](https://github.com/icatproject/ids.server/actions?query=workflow%3A%22CI+Build%22) General installation instructions are at http://www.icatproject.org/installation/component diff --git a/pom.xml b/pom.xml index 47d6086d..c35d3629 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ UTF-8 https://repo.icatproject.org/repo github - write + check @@ -432,17 +432,8 @@ 0.22 2.0.0 - 80 - 4 - false - true + false true - - - - src/main/java/**/*.java - src/test/java/**/*.java - @@ -458,21 +449,6 @@ - - - travis - - - env.TRAVIS - - - - - check - - - - diff --git a/src/.prettierrc.yaml b/src/.prettierrc.yaml new file mode 100644 index 00000000..314ad51b --- /dev/null +++ b/src/.prettierrc.yaml @@ -0,0 +1,4 @@ +useTabs: false +tabWidth: 4 +printWidth: 80 +ignoreEditorConfig: true \ No newline at end of file From bf8565dfb1e7042eb19ac5af5072a9242f99d324 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Thu, 6 Jun 2024 12:59:17 +0200 Subject: [PATCH 03/19] WIP: only lint check at CI build --- README.md | 2 +- pom.xml | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8066012f..5c5debcb 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ The Prettier Maven Plugin is used for linting. it is configured for "check" while build. That's why a build will fail when the files haven't the right format. execute `mvn prettier:write` before. -TODO: execute `mvn prettier:wite` while local build but only the `check` while CI build. +TODO: execute `mvn prettier:write` while local build but only the `check` while CI build. [![Build Status](https://github.com/icatproject/ids.server/workflows/CI%20Build/badge.svg?branch=master)](https://github.com/icatproject/ids.server/actions?query=workflow%3A%22CI+Build%22) diff --git a/pom.xml b/pom.xml index c35d3629..a9264b82 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ UTF-8 https://repo.icatproject.org/repo github - check + write @@ -449,6 +449,21 @@ + + + cibuild + + + env.build_and_test + + + + + check + + + + From 9ca3c4bb0b7719366d6a6edca6fcfad7473fd871 Mon Sep 17 00:00:00 2001 From: MLewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Thu, 6 Jun 2024 13:19:20 +0200 Subject: [PATCH 04/19] added env param to ci-build.yml --- .github/workflows/ci-build.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci-build.yml b/.github/workflows/ci-build.yml index 48345241..41d75b2a 100644 --- a/.github/workflows/ci-build.yml +++ b/.github/workflows/ci-build.yml @@ -1,4 +1,8 @@ name: CI Build + +env: + CIBUILD: true + on: workflow_dispatch: pull_request: From a43120ee9f0fda31d534381f5530338cd6c6c40a Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Thu, 6 Jun 2024 13:23:26 +0200 Subject: [PATCH 05/19] ci env param dependend prettier check --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a9264b82..0a1b2bd2 100644 --- a/pom.xml +++ b/pom.xml @@ -454,7 +454,7 @@ cibuild - env.build_and_test + env.CIBUILD From 1c2aa8de0650703222641a465f8a60f02e03e210 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Thu, 6 Jun 2024 13:42:04 +0200 Subject: [PATCH 06/19] linted code to test ci check --- .../java/org/icatproject/ids/IdsService.java | 631 ++++++++++------ src/main/java/org/icatproject/ids/Tidier.java | 281 +++++-- .../org/icatproject/ids/enums/CallType.java | 6 +- .../org/icatproject/ids/enums/DeferredOp.java | 5 +- .../icatproject/ids/enums/RequestIdNames.java | 2 +- .../icatproject/ids/enums/RequestType.java | 24 +- .../org/icatproject/ids/enums/Status.java | 4 +- .../icatproject/ids/enums/StorageUnit.java | 3 +- .../ids/enums/ValueContainerType.java | 9 +- .../exceptions/DataNotOnlineException.java | 1 - .../ids/exceptions/IdsException.java | 2 +- .../ids/exceptions/IdsExceptionMapper.java | 14 +- .../InsufficientStorageException.java | 1 + .../exceptions/NotFoundExceptionMapper.java | 24 +- .../exceptions/NotImplementedException.java | 1 - .../exceptions/RuntimeExceptionMapper.java | 30 +- .../FiniteStateMachine.java | 100 ++- ...niteStateMachineForSingleLevelStorage.java | 22 +- ...iteStateMachineForStorageUnitDatafile.java | 337 +++++++-- ...niteStateMachineForStorageUnitDataset.java | 229 ++++-- .../ids/helpers/CORSResponseFilter.java | 10 +- .../helpers/CheckedWithSizeInputStream.java | 1 + .../icatproject/ids/helpers/Constants.java | 8 +- .../ids/helpers/LocationHelper.java | 20 +- .../ids/helpers/RangeOutputStream.java | 22 +- .../java/org/icatproject/ids/helpers/SO.java | 91 ++- .../ids/helpers/ValueContainer.java | 67 +- .../icatproject/ids/models/DataInfoBase.java | 3 +- .../icatproject/ids/models/DataSelection.java | 29 +- .../icatproject/ids/models/DatafileInfo.java | 30 +- .../icatproject/ids/models/DatasetInfo.java | 68 +- .../org/icatproject/ids/models/Prepared.java | 132 +++- .../ids/requestHandlers/ArchiveHandler.java | 24 +- .../ids/requestHandlers/DeleteHandler.java | 56 +- .../GetDataFileIdsHandler.java | 45 +- .../ids/requestHandlers/GetDataHandler.java | 217 ++++-- .../requestHandlers/GetIcatUrlHandler.java | 10 +- .../GetServiceStatusHandler.java | 27 +- .../ids/requestHandlers/GetStatusHandler.java | 36 +- .../requestHandlers/IsPreparedHandler.java | 36 +- .../requestHandlers/IsReadOnlyHandler.java | 7 +- .../requestHandlers/IsTwoLevelHandler.java | 4 +- .../requestHandlers/PrepareDataHandler.java | 61 +- .../ids/requestHandlers/PutHandler.java | 371 ++++++--- .../ids/requestHandlers/ResetHandler.java | 35 +- .../ids/requestHandlers/RestoreHandler.java | 27 +- .../ids/requestHandlers/WriteHandler.java | 47 +- .../base/DataControllerBase.java | 42 +- .../base/DataRequestHandler.java | 78 +- .../base/PreparedDataController.java | 41 +- .../base/RequestHandlerBase.java | 76 +- .../base/UnpreparedDataController.java | 76 +- .../getSizeHandlers/GetSizeHandler.java | 58 +- .../GetSizeHandlerForFastProcessing.java | 115 ++- .../icatproject/ids/services/ICATGetter.java | 30 +- .../icatproject/ids/services/IcatReader.java | 26 +- .../icatproject/ids/services/LockManager.java | 41 +- .../ids/services/PropertyHandler.java | 215 ++++-- .../ids/services/ServiceProvider.java | 55 +- .../icatproject/ids/services/Transmitter.java | 37 +- .../ids/services/UnfinishedWorkService.java | 102 ++- .../DataSelectionService.java | 200 +++-- .../DataSelectionServiceFactory.java | 698 +++++++++++++---- ...SelectionServiceForSingleLevelStorage.java | 62 +- ...electionServiceForStorageUnitDatafile.java | 52 +- ...SelectionServiceForStorageUnitDataset.java | 62 +- .../icatproject/ids/thread/DfArchiver.java | 47 +- .../org/icatproject/ids/thread/DfDeleter.java | 27 +- .../icatproject/ids/thread/DfRestorer.java | 36 +- .../org/icatproject/ids/thread/DfWriter.java | 39 +- .../icatproject/ids/thread/DsArchiver.java | 34 +- .../icatproject/ids/thread/DsRestorer.java | 105 ++- .../org/icatproject/ids/thread/DsWriter.java | 86 ++- .../icatproject/ids/DataSelectionDevTest.java | 79 +- .../java/org/icatproject/ids/DigestTest.java | 16 +- .../icatproject/ids/PreparePackingTest.java | 73 +- .../ids/RangeOutputStreamTest.java | 12 +- .../org/icatproject/ids/TestICATGetter.java | 17 +- .../java/org/icatproject/ids/TestUtils.java | 5 +- .../icatproject/ids/TestValueContainer.java | 19 +- .../java/org/icatproject/ids/TestZipping.java | 24 +- .../java/org/icatproject/ids/TidierTest.java | 36 +- .../org/icatproject/ids/TransmittingTest.java | 289 +++++-- .../org/icatproject/ids/ValidaterTest.java | 5 +- .../icatproject/ids/integration/BaseTest.java | 617 +++++++++++---- .../ids/integration/one/BaseTests.java | 6 +- .../integration/one/BogusDatafileTest.java | 96 ++- .../ids/integration/one/DeleteTest.java | 33 +- .../integration/one/GetDataExplicitTest.java | 258 +++++-- .../one/GetDataForPreparedIdTest.java | 111 ++- .../one/GetStatusExplicitTest.java | 47 +- .../one/GetStatusForPreparedIdTest.java | 49 +- .../ids/integration/one/MiscTest.java | 42 +- .../ids/integration/one/PerformanceTest.java | 64 +- .../ids/integration/one/PrepareDataTest.java | 99 ++- .../ids/integration/one/PutTest.java | 113 ++- .../ids/integration/one/WriteTest.java | 12 +- .../ids/integration/two/ArchiveTest.java | 40 +- .../ids/integration/two/BaseTests.java | 6 +- .../integration/two/BogusDatafileTest.java | 143 ++-- .../ids/integration/two/DeleteTest.java | 38 +- .../integration/two/GetDataExplicitTest.java | 161 +++- .../two/GetDataForPreparedIdTest.java | 100 ++- .../two/GetStatusExplicitTest.java | 63 +- .../two/GetStatusForPreparedIdTest.java | 49 +- .../ids/integration/two/MiscTest.java | 39 +- .../ids/integration/two/PrepareDataTest.java | 149 +++- .../ids/integration/two/PutTest.java | 81 +- .../integration/two/RestoreErrorsTest.java | 28 +- .../ids/integration/two/RestoreTest.java | 42 +- .../ids/integration/two/WriteTest.java | 41 +- .../ids/integration/twodf/ArchiveTest.java | 40 +- .../ids/integration/twodf/BaseTests.java | 6 +- .../integration/twodf/BogusDatafileTest.java | 143 ++-- .../ids/integration/twodf/DeleteTest.java | 38 +- .../twodf/GetDataExplicitTest.java | 162 +++- .../twodf/GetDataForPreparedIdTest.java | 100 ++- .../twodf/GetStatusExplicitTest.java | 66 +- .../twodf/GetStatusForPreparedIdTest.java | 49 +- .../ids/integration/twodf/MiscTest.java | 30 +- .../integration/twodf/PrepareDataTest.java | 149 +++- .../ids/integration/twodf/PutTest.java | 80 +- .../ids/integration/twodf/QueueTest.java | 22 +- .../ids/integration/twodf/RestoreTest.java | 40 +- .../ids/integration/twodf/WriteTest.java | 41 +- .../ids/integration/util/Setup.java | 41 +- .../util/client/BadRequestException.java | 1 - .../util/client/DataNotOnlineException.java | 1 - .../util/client/HttpInputStream.java | 8 +- .../integration/util/client/IdsException.java | 1 - .../InsufficientPrivilegesException.java | 1 - .../client/InsufficientStorageException.java | 1 - .../util/client/InternalException.java | 1 - .../util/client/NotFoundException.java | 1 - .../util/client/NotImplementedException.java | 1 - .../util/client/TestingClient.java | 712 ++++++++++++------ 136 files changed, 7478 insertions(+), 3009 deletions(-) diff --git a/src/main/java/org/icatproject/ids/IdsService.java b/src/main/java/org/icatproject/ids/IdsService.java index 0d517fa9..218306cb 100644 --- a/src/main/java/org/icatproject/ids/IdsService.java +++ b/src/main/java/org/icatproject/ids/IdsService.java @@ -1,12 +1,5 @@ package org.icatproject.ids; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; - - import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import jakarta.ejb.EJB; @@ -23,15 +16,17 @@ import jakarta.ws.rs.HeaderParam; import jakarta.ws.rs.POST; import jakarta.ws.rs.PUT; +import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; -import jakarta.ws.rs.Path; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.RequestIdNames; import org.icatproject.ids.exceptions.BadRequestException; @@ -66,12 +61,16 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.Transmitter; import org.icatproject.ids.services.UnfinishedWorkService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @Path("/") @Stateless public class IdsService { - private final static Logger logger = LoggerFactory.getLogger(IdsService.class); + private static final Logger logger = LoggerFactory.getLogger( + IdsService.class + ); @EJB Transmitter transmitter; @@ -89,24 +88,34 @@ public class IdsService { private UnfinishedWorkService unfinishedWorkService; - @PostConstruct private void init() { - try { synchronized (lock) { - logger.info("creating IdsService"); - FiniteStateMachine.createInstance(reader, lockManager, PropertyHandler.getInstance().getStorageUnit()); + FiniteStateMachine.createInstance( + reader, + lockManager, + PropertyHandler.getInstance().getStorageUnit() + ); this.fsm = FiniteStateMachine.getInstance(); this.fsm.init(); - ServiceProvider.createInstance(transmitter, fsm, lockManager, reader); - - var propertyHandler = ServiceProvider.getInstance().getPropertyHandler(); + ServiceProvider.createInstance( + transmitter, + fsm, + lockManager, + reader + ); + + var propertyHandler = ServiceProvider + .getInstance() + .getPropertyHandler(); var archiveStorage = propertyHandler.getArchiveStorage(); var twoLevel = archiveStorage != null; - var preparedDir = propertyHandler.getCacheDir().resolve("prepared"); + var preparedDir = propertyHandler + .getCacheDir() + .resolve("prepared"); Files.createDirectories(preparedDir); this.unfinishedWorkService = new UnfinishedWorkService(); @@ -120,13 +129,21 @@ private void init() { java.nio.file.Path datasetDir; if (twoLevel) { - datasetDir = propertyHandler.getCacheDir().resolve("dataset"); - var markerDir = propertyHandler.getCacheDir().resolve("marker"); + datasetDir = + propertyHandler.getCacheDir().resolve("dataset"); + var markerDir = propertyHandler + .getCacheDir() + .resolve("marker"); if (!inited) { Files.createDirectories(datasetDir); Files.createDirectories(markerDir); - this.unfinishedWorkService.restartUnfinishedWork(markerDir, key); - this.unfinishedWorkService.cleanDatasetCache(datasetDir); + this.unfinishedWorkService.restartUnfinishedWork( + markerDir, + key + ); + this.unfinishedWorkService.cleanDatasetCache( + datasetDir + ); } } @@ -136,18 +153,18 @@ private void init() { } } catch (Throwable e) { logger.error("Won't start ", e); - throw new RuntimeException("IdsService reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException( + "IdsService reports " + e.getClass() + " " + e.getMessage() + ); } } - @PreDestroy private void exit() { this.fsm.exit(); logger.info("destroyed IdsService"); } - /** * Archive data specified by the investigationIds, datasetIds and * datafileIds specified along with a sessionId. If two level storage is not @@ -164,19 +181,27 @@ private void exit() { * @throws InsufficientPrivilegesException * @throws InternalException * @throws NotFoundException - * @throws DataNotOnlineException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("archive") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void archive(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, - NotFoundException, DataNotOnlineException { - - var handler = new ArchiveHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + public void archive( + @Context HttpServletRequest request, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds + ) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { + var handler = new ArchiveHandler( + request.getRemoteAddr(), + sessionId, + investigationIds, + datasetIds, + datafileIds + ); handler.handle(); } @@ -200,16 +225,24 @@ public void archive(@Context HttpServletRequest request, @FormParam(RequestIdNam */ @DELETE @Path("delete") - public void delete(@Context HttpServletRequest request, @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds) throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { - - var handler = new DeleteHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + public void delete( + @Context HttpServletRequest request, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds + ) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { + var handler = new DeleteHandler( + request.getRemoteAddr(), + sessionId, + investigationIds, + datasetIds, + datafileIds + ); handler.handle(); } - /** * Return the version of the server * @@ -222,7 +255,10 @@ public void delete(@Context HttpServletRequest request, @QueryParam(RequestIdNam public String getVersion() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen.writeStartObject().write("version", Constants.API_VERSION).writeEnd(); + gen + .writeStartObject() + .write("version", Constants.API_VERSION) + .writeEnd(); gen.close(); return baos.toString(); } @@ -257,20 +293,37 @@ public String getVersion() { * @throws InternalException * @throws InsufficientPrivilegesException * @throws DataNotOnlineException - * @throws NotImplementedException + * @throws NotImplementedException * @statuscode 200 To indicate success */ @GET @Path("getData") @Produces(MediaType.APPLICATION_OCTET_STREAM) - public Response getData(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds, - @QueryParam("compress") boolean compress, @QueryParam("zip") boolean zip, - @QueryParam("outname") String outname, @HeaderParam("Range") String range) throws BadRequestException, - NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - - var handler = new GetDataHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds, compress, zip, outname, range); + public Response getData( + @Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds, + @QueryParam("compress") boolean compress, + @QueryParam("zip") boolean zip, + @QueryParam("outname") String outname, + @HeaderParam("Range") String range + ) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + var handler = new GetDataHandler( + request.getRemoteAddr(), + preparedId, + sessionId, + investigationIds, + datasetIds, + datafileIds, + compress, + zip, + outname, + range + ); return handler.handle().getResponse(); } @@ -291,19 +344,30 @@ public Response getData(@Context HttpServletRequest request, @QueryParam(Request * @throws InternalException * @throws NotFoundException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getDatafileIds") @Produces(MediaType.APPLICATION_JSON) - public String getDatafileIds(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - - var handler = new GetDataFileIdsHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public String getDatafileIds( + @Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds + ) + throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + var handler = new GetDataFileIdsHandler( + request.getRemoteAddr(), + preparedId, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); return handler.handle().getString(); } @@ -313,19 +377,19 @@ public String getDatafileIds(@Context HttpServletRequest request, @QueryParam(Re * obtained. * * @return the url of the icat server - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("getIcatUrl") @Produces(MediaType.TEXT_PLAIN) - public String getIcatUrl(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - + public String getIcatUrl(@Context HttpServletRequest request) + throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var handler = new GetIcatUrlHandler(request.getRemoteAddr()); return handler.handle().getString(); } @@ -340,19 +404,24 @@ public String getIcatUrl(@Context HttpServletRequest request) throws InternalExc * @return a json string. * @throws InternalException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws BadRequestException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws BadRequestException * @statuscode 200 To indicate success */ @GET @Path("getServiceStatus") @Produces(MediaType.APPLICATION_JSON) - public String getServiceStatus(@Context HttpServletRequest request, @QueryParam(RequestIdNames.sessionId) String sessionId) - throws InternalException, InsufficientPrivilegesException, BadRequestException, NotFoundException, DataNotOnlineException, NotImplementedException { - - var handler = new GetServiceStatusHandler(request.getRemoteAddr(), sessionId); + public String getServiceStatus( + @Context HttpServletRequest request, + @QueryParam(RequestIdNames.sessionId) String sessionId + ) + throws InternalException, InsufficientPrivilegesException, BadRequestException, NotFoundException, DataNotOnlineException, NotImplementedException { + var handler = new GetServiceStatusHandler( + request.getRemoteAddr(), + sessionId + ); return handler.handle().getString(); } @@ -372,30 +441,46 @@ public String getServiceStatus(@Context HttpServletRequest request, @QueryParam( * @throws NotFoundException * @throws InsufficientPrivilegesException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getSize") @Produces(MediaType.TEXT_PLAIN) - public long getSize(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { - - + public long getSize( + @Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds + ) + throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { var result = ValueContainer.getInvalid(); // trying fast computation - if(sessionId != null) { - var fastHandler = new GetSizeHandlerForFastProcessing(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + if (sessionId != null) { + var fastHandler = new GetSizeHandlerForFastProcessing( + request.getRemoteAddr(), + sessionId, + investigationIds, + datasetIds, + datafileIds + ); result = fastHandler.handle(); } // otherwise normal computation - if(result.isInvalid()) { - var handler = new GetSizeHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); + if (result.isInvalid()) { + var handler = new GetSizeHandler( + request.getRemoteAddr(), + preparedId, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); result = handler.handle(); } @@ -424,32 +509,48 @@ public long getSize(@Context HttpServletRequest request, @QueryParam(RequestIdNa * @throws NotFoundException * @throws InsufficientPrivilegesException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getStatus") @Produces(MediaType.TEXT_PLAIN) - public String getStatus(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { - + public String getStatus( + @Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds + ) + throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { // special case for getStatus request: getting status is possible without authentification if (sessionId == null && preparedId == null) { try { - sessionId = ServiceProvider.getInstance().getIcatReader().getSessionId(); + sessionId = + ServiceProvider + .getInstance() + .getIcatReader() + .getSessionId(); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage() + ); } } - var handler = new GetStatusHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new GetStatusHandler( + request.getRemoteAddr(), + preparedId, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); return handler.handle().getString(); } - /** * Returns true if all the data files are ready to be downloaded. As a side * effect, if any data files are archived and no restoration has been @@ -467,18 +568,23 @@ public String getStatus(@Context HttpServletRequest request, @QueryParam(Request * @throws BadRequestException * @throws NotFoundException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws InsufficientPrivilegesException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws InsufficientPrivilegesException * @statuscode 200 To indicate success */ @GET @Path("isPrepared") @Produces(MediaType.TEXT_PLAIN) - public boolean isPrepared(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - - var handler = new IsPreparedHandler(request.getRemoteAddr(), preparedId); + public boolean isPrepared( + @Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId + ) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + var handler = new IsPreparedHandler( + request.getRemoteAddr(), + preparedId + ); return handler.handle().getBool(); } @@ -488,19 +594,19 @@ public boolean isPrepared(@Context HttpServletRequest request, @QueryParam(Reque * * @title isReadOnly * @return true if readonly, else false - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("isReadOnly") @Produces(MediaType.TEXT_PLAIN) - public boolean isReadOnly(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - + public boolean isReadOnly(@Context HttpServletRequest request) + throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var handler = new IsReadOnlyHandler(request.getRemoteAddr()); return handler.handle().getBool(); } @@ -511,19 +617,19 @@ public boolean isReadOnly(@Context HttpServletRequest request) throws InternalEx * * @title isTwoLevel * @return true if twoLevel, else false - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("isTwoLevel") @Produces(MediaType.TEXT_PLAIN) - public boolean isTwoLevel(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - + public boolean isTwoLevel(@Context HttpServletRequest request) + throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var handler = new IsTwoLevelHandler(request.getRemoteAddr()); return handler.handle().getBool(); } @@ -567,21 +673,33 @@ public String ping() { * @throws InsufficientPrivilegesException * @throws NotFoundException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("prepareData") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.TEXT_PLAIN) - public String prepareData(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds, @FormParam("compress") boolean compress, - @FormParam("zip") boolean zip) - throws BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, NotImplementedException, DataNotOnlineException { - - var handler = new PrepareDataHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds, compress, zip); + public String prepareData( + @Context HttpServletRequest request, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds, + @FormParam("compress") boolean compress, + @FormParam("zip") boolean zip + ) + throws BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, NotImplementedException, DataNotOnlineException { + var handler = new PrepareDataHandler( + request.getRemoteAddr(), + sessionId, + investigationIds, + datasetIds, + datafileIds, + compress, + zip + ); return handler.handle().getString(); } @@ -613,16 +731,33 @@ public String prepareData(@Context HttpServletRequest request, @FormParam(Reques @Path("put") @Consumes(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.APPLICATION_JSON) - public Response put(@Context HttpServletRequest request, InputStream body, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("name") String name, - @QueryParam("datafileFormatId") String datafileFormatId, @QueryParam("datasetId") String datasetId, - @QueryParam("description") String description, @QueryParam("doi") String doi, - @QueryParam("datafileCreateTime") String datafileCreateTime, - @QueryParam("datafileModTime") String datafileModTime) throws BadRequestException, NotFoundException, - InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { - - var handler = new PutHandler(request.getRemoteAddr(), sessionId, body, name, datafileFormatId, datasetId, - description, doi, datafileCreateTime, datafileModTime, false, false); + public Response put( + @Context HttpServletRequest request, + InputStream body, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("name") String name, + @QueryParam("datafileFormatId") String datafileFormatId, + @QueryParam("datasetId") String datasetId, + @QueryParam("description") String description, + @QueryParam("doi") String doi, + @QueryParam("datafileCreateTime") String datafileCreateTime, + @QueryParam("datafileModTime") String datafileModTime + ) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { + var handler = new PutHandler( + request.getRemoteAddr(), + sessionId, + body, + name, + datafileFormatId, + datasetId, + description, + doi, + datafileCreateTime, + datafileModTime, + false, + false + ); return handler.handle().getResponse(); } @@ -649,65 +784,90 @@ public Response put(@Context HttpServletRequest request, InputStream body, @Path("put") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) - public Response putAsPost(@Context HttpServletRequest request) throws BadRequestException, NotFoundException, - InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { - try { - String sessionId = null; - String name = null; - String datafileFormatId = null; - String datasetId = null; - String description = null; - String doi = null; - String datafileCreateTime = null; - String datafileModTime = null; - Response result = null; - boolean wrap = false; - boolean padding = false; - - // Parse the request - for (Part part : request.getParts()) { - String fieldName = part.getName(); - InputStream stream = part.getInputStream(); - if (part.getSubmittedFileName() == null) { - String value = new String(stream.readAllBytes(), StandardCharsets.UTF_8); - if (fieldName.equals("sessionId")) { - sessionId = value; - } else if (fieldName.equals("name")) { - name = value; - } else if (fieldName.equals("datafileFormatId")) { - datafileFormatId = value; - } else if (fieldName.equals("datasetId")) { - datasetId = value; - } else if (fieldName.equals("description")) { - description = value; - } else if (fieldName.equals("doi")) { - doi = value; - } else if (fieldName.equals("datafileCreateTime")) { - datafileCreateTime = value; - } else if (fieldName.equals("datafileModTime")) { - datafileModTime = value; - } else if (fieldName.equals("wrap")) { - wrap = (value != null && value.toUpperCase().equals("TRUE")); - } else if (fieldName.equals("padding")) { - padding = (value != null && value.toUpperCase().equals("TRUE")); - } else { - throw new BadRequestException("Form field " + fieldName + "is not recognised"); - } - } else { - if (name == null) { - name = part.getSubmittedFileName(); - } - var handler = new PutHandler(request.getRemoteAddr(), sessionId, stream, name, datafileFormatId, datasetId, description, - doi, datafileCreateTime, datafileModTime, wrap, padding); - result = handler.handle().getResponse(); - } + public Response putAsPost(@Context HttpServletRequest request) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { + try { + String sessionId = null; + String name = null; + String datafileFormatId = null; + String datasetId = null; + String description = null; + String doi = null; + String datafileCreateTime = null; + String datafileModTime = null; + Response result = null; + boolean wrap = false; + boolean padding = false; + + // Parse the request + for (Part part : request.getParts()) { + String fieldName = part.getName(); + InputStream stream = part.getInputStream(); + if (part.getSubmittedFileName() == null) { + String value = new String( + stream.readAllBytes(), + StandardCharsets.UTF_8 + ); + if (fieldName.equals("sessionId")) { + sessionId = value; + } else if (fieldName.equals("name")) { + name = value; + } else if (fieldName.equals("datafileFormatId")) { + datafileFormatId = value; + } else if (fieldName.equals("datasetId")) { + datasetId = value; + } else if (fieldName.equals("description")) { + description = value; + } else if (fieldName.equals("doi")) { + doi = value; + } else if (fieldName.equals("datafileCreateTime")) { + datafileCreateTime = value; + } else if (fieldName.equals("datafileModTime")) { + datafileModTime = value; + } else if (fieldName.equals("wrap")) { + wrap = + ( + value != null && + value.toUpperCase().equals("TRUE") + ); + } else if (fieldName.equals("padding")) { + padding = + ( + value != null && + value.toUpperCase().equals("TRUE") + ); + } else { + throw new BadRequestException( + "Form field " + fieldName + "is not recognised" + ); + } + } else { + if (name == null) { + name = part.getSubmittedFileName(); } - return result; - } catch (IOException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); - } catch (ServletException e) { - throw new BadRequestException("Multipart content expected"); + var handler = new PutHandler( + request.getRemoteAddr(), + sessionId, + stream, + name, + datafileFormatId, + datasetId, + description, + doi, + datafileCreateTime, + datafileModTime, + wrap, + padding + ); + result = handler.handle().getResponse(); } + } + return result; + } catch (IOException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (ServletException e) { + throw new BadRequestException("Multipart content expected"); + } } /** @@ -732,20 +892,30 @@ public Response putAsPost(@Context HttpServletRequest request) throws BadRequest * @throws NotFoundException * @throws InternalException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("reset") - public void reset(@Context HttpServletRequest request, @FormParam(RequestIdNames.preparedId) String preparedId, - @FormParam(RequestIdNames.sessionId) String sessionId, @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, @FormParam("datafileIds") String datafileIds) - throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - - var handler = new ResetHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public void reset( + @Context HttpServletRequest request, + @FormParam(RequestIdNames.preparedId) String preparedId, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds + ) + throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + var handler = new ResetHandler( + request.getRemoteAddr(), + preparedId, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); handler.handle(); - } /** @@ -764,23 +934,30 @@ public void reset(@Context HttpServletRequest request, @FormParam(RequestIdNames * @throws InsufficientPrivilegesException * @throws InternalException * @throws NotFoundException - * @throws DataNotOnlineException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("restore") @Consumes("application/x-www-form-urlencoded") - public void restore(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, - NotFoundException, DataNotOnlineException { - - var handler = new RestoreHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + public void restore( + @Context HttpServletRequest request, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds + ) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { + var handler = new RestoreHandler( + request.getRemoteAddr(), + sessionId, + investigationIds, + datasetIds, + datafileIds + ); handler.handle(); } - /** * Write data specified by the investigationIds, datasetIds * and datafileIds specified along with a sessionId to archive @@ -803,13 +980,21 @@ public void restore(@Context HttpServletRequest request, @FormParam(RequestIdNam @POST @Path("write") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void write(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, - NotFoundException, DataNotOnlineException { - - var handler = new WriteHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + public void write( + @Context HttpServletRequest request, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds + ) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { + var handler = new WriteHandler( + request.getRemoteAddr(), + sessionId, + investigationIds, + datasetIds, + datafileIds + ); handler.handle(); } } diff --git a/src/main/java/org/icatproject/ids/Tidier.java b/src/main/java/org/icatproject/ids/Tidier.java index 9d647de0..43ab0552 100644 --- a/src/main/java/org/icatproject/ids/Tidier.java +++ b/src/main/java/org/icatproject/ids/Tidier.java @@ -1,5 +1,10 @@ package org.icatproject.ids; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.ejb.EJB; +import jakarta.ejb.Singleton; +import jakarta.ejb.Startup; import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -11,16 +16,6 @@ import java.util.Map; import java.util.Timer; import java.util.TimerTask; - -import jakarta.annotation.PostConstruct; -import jakarta.annotation.PreDestroy; -import jakarta.ejb.EJB; -import jakarta.ejb.Singleton; -import jakarta.ejb.Startup; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.IcatException_Exception; @@ -38,6 +33,8 @@ import org.icatproject.ids.services.IcatReader; import org.icatproject.ids.services.LockManager; import org.icatproject.ids.services.PropertyHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @Singleton @Startup @@ -55,57 +52,153 @@ public void run() { if (twoLevel) { if (storageUnit == StorageUnit.DATASET) { - List dsInfos = mainStorage.getDatasetsToArchive(stopArchivingLevel, - startArchivingLevel); + List dsInfos = mainStorage.getDatasetsToArchive( + stopArchivingLevel, + startArchivingLevel + ); for (DsInfo dsInfo : dsInfos) { StringBuilder sb = new StringBuilder( - "SELECT ds FROM Dataset ds, ds.investigation inv, inv.facility fac WHERE"); + "SELECT ds FROM Dataset ds, ds.investigation inv, inv.facility fac WHERE" + ); boolean andNeeded = false; - andNeeded = addNumericConstraint(sb, "ds.id", dsInfo.getDsId(), andNeeded); - andNeeded = addStringConstraint(sb, "ds.location", dsInfo.getDsLocation(), andNeeded); - andNeeded = addStringConstraint(sb, "ds.name", dsInfo.getDsName(), andNeeded); - - andNeeded = addNumericConstraint(sb, "inv.id", dsInfo.getInvId(), andNeeded); - andNeeded = addStringConstraint(sb, "inv.name", dsInfo.getInvName(), andNeeded); - andNeeded = addStringConstraint(sb, "inv.visitId", dsInfo.getVisitId(), andNeeded); - - andNeeded = addStringConstraint(sb, "fac.name", dsInfo.getFacilityName(), andNeeded); - andNeeded = addNumericConstraint(sb, "fac.id", dsInfo.getFacilityId(), andNeeded); + andNeeded = + addNumericConstraint( + sb, + "ds.id", + dsInfo.getDsId(), + andNeeded + ); + andNeeded = + addStringConstraint( + sb, + "ds.location", + dsInfo.getDsLocation(), + andNeeded + ); + andNeeded = + addStringConstraint( + sb, + "ds.name", + dsInfo.getDsName(), + andNeeded + ); + + andNeeded = + addNumericConstraint( + sb, + "inv.id", + dsInfo.getInvId(), + andNeeded + ); + andNeeded = + addStringConstraint( + sb, + "inv.name", + dsInfo.getInvName(), + andNeeded + ); + andNeeded = + addStringConstraint( + sb, + "inv.visitId", + dsInfo.getVisitId(), + andNeeded + ); + + andNeeded = + addStringConstraint( + sb, + "fac.name", + dsInfo.getFacilityName(), + andNeeded + ); + andNeeded = + addNumericConstraint( + sb, + "fac.id", + dsInfo.getFacilityId(), + andNeeded + ); sb.append(" INCLUDE ds.investigation.facility"); try { int low = 0; while (true) { - String query = sb.toString() + " LIMIT " + low + "," + tidyBlockSize; + String query = + sb.toString() + + " LIMIT " + + low + + "," + + tidyBlockSize; List os = reader.search(query); - logger.debug(query + " returns " + os.size() + " datasets"); + logger.debug( + query + + " returns " + + os.size() + + " datasets" + ); for (Object o : os) { - DatasetInfo dsInfoImpl = new DatasetInfo((Dataset) o); + DatasetInfo dsInfoImpl = new DatasetInfo( + (Dataset) o + ); logger.debug( - "Requesting archive of " + dsInfoImpl + " to recover main storage"); - fsm.queue(dsInfoImpl, DeferredOp.ARCHIVE); + "Requesting archive of " + + dsInfoImpl + + " to recover main storage" + ); + fsm.queue( + dsInfoImpl, + DeferredOp.ARCHIVE + ); } if (os.size() < tidyBlockSize) { break; } low += tidyBlockSize; } - } catch (InternalException | IcatException_Exception | InsufficientPrivilegesException e) { + } catch ( + InternalException + | IcatException_Exception + | InsufficientPrivilegesException e + ) { // Log it and carry on - logger.error(e.getClass() + " " + e.getMessage()); + logger.error( + e.getClass() + " " + e.getMessage() + ); } } - } else if (storageUnit == StorageUnit.DATAFILE) { - List dfInfos = mainStorage.getDatafilesToArchive(stopArchivingLevel, - startArchivingLevel); + List dfInfos = mainStorage.getDatafilesToArchive( + stopArchivingLevel, + startArchivingLevel + ); for (DfInfo dfInfo : dfInfos) { - StringBuilder sb = new StringBuilder("SELECT df FROM Datafile df WHERE"); + StringBuilder sb = new StringBuilder( + "SELECT df FROM Datafile df WHERE" + ); boolean andNeeded = false; - andNeeded = addNumericConstraint(sb, "df.id", dfInfo.getDfId(), andNeeded); - andNeeded = addStringConstraint(sb, "df.createId", dfInfo.getCreateId(), andNeeded); - andNeeded = addStringConstraint(sb, "df.modId", dfInfo.getModId(), andNeeded); + andNeeded = + addNumericConstraint( + sb, + "df.id", + dfInfo.getDfId(), + andNeeded + ); + andNeeded = + addStringConstraint( + sb, + "df.createId", + dfInfo.getCreateId(), + andNeeded + ); + andNeeded = + addStringConstraint( + sb, + "df.modId", + dfInfo.getModId(), + andNeeded + ); if (key != null) { if (dfInfo.getDfLocation() != null) { if (andNeeded) { @@ -114,42 +207,85 @@ public void run() { sb.append(" "); andNeeded = true; } - sb.append("df.location" + " LIKE '" + dfInfo.getDfLocation() + " %'"); + sb.append( + "df.location" + + " LIKE '" + + dfInfo.getDfLocation() + + " %'" + ); } } else { - andNeeded = addStringConstraint(sb, "df.location", - - dfInfo.getDfLocation(), andNeeded); + andNeeded = + addStringConstraint( + sb, + "df.location", + dfInfo.getDfLocation(), + andNeeded + ); } - andNeeded = addStringConstraint(sb, "df.name", dfInfo.getDfName(), andNeeded); + andNeeded = + addStringConstraint( + sb, + "df.name", + dfInfo.getDfName(), + andNeeded + ); sb.append(" INCLUDE df.dataset"); try { int low = 0; while (true) { - String query = sb.toString() + " LIMIT " + low + "," + tidyBlockSize; + String query = + sb.toString() + + " LIMIT " + + low + + "," + + tidyBlockSize; List os = reader.search(query); - logger.debug(query + " returns " + os.size() + " datafiles"); + logger.debug( + query + + " returns " + + os.size() + + " datafiles" + ); for (Object o : os) { Datafile df = (Datafile) o; - DatafileInfo dfInfoImpl = new DatafileInfo(df.getId(), df.getName(), - - LocationHelper.getLocation(df.getId(), df.getLocation()), df.getCreateId(), - df.getModId(), df.getDataset().getId()); - + DatafileInfo dfInfoImpl = new DatafileInfo( + df.getId(), + df.getName(), + LocationHelper.getLocation( + df.getId(), + df.getLocation() + ), + df.getCreateId(), + df.getModId(), + df.getDataset().getId() + ); logger.debug( - "Requesting archive of " + dfInfoImpl + " to recover main storage"); - fsm.queue(dfInfoImpl, DeferredOp.ARCHIVE); + "Requesting archive of " + + dfInfoImpl + + " to recover main storage" + ); + fsm.queue( + dfInfoImpl, + DeferredOp.ARCHIVE + ); } if (os.size() < tidyBlockSize) { break; } low += tidyBlockSize; } - } catch (InternalException | IcatException_Exception | InsufficientPrivilegesException e) { + } catch ( + InternalException + | IcatException_Exception + | InsufficientPrivilegesException e + ) { // Log it and carry on - logger.error(e.getClass() + " " + e.getMessage()); + logger.error( + e.getClass() + " " + e.getMessage() + ); } } } @@ -160,12 +296,16 @@ public void run() { timer.schedule(new Action(), sizeCheckIntervalMillis); } } - } - private final static Logger logger = LoggerFactory.getLogger(Tidier.class); + private static final Logger logger = LoggerFactory.getLogger(Tidier.class); - static boolean addStringConstraint(StringBuilder sb, String var, String value, boolean andNeeded) { + static boolean addStringConstraint( + StringBuilder sb, + String var, + String value, + boolean andNeeded + ) { if (value != null) { if (andNeeded) { sb.append(" AND "); @@ -178,7 +318,12 @@ static boolean addStringConstraint(StringBuilder sb, String var, String value, b return andNeeded; } - static boolean addNumericConstraint(StringBuilder sb, String var, Long value, boolean andNeeded) { + static boolean addNumericConstraint( + StringBuilder sb, + String var, + Long value, + boolean andNeeded + ) { if (value != null) { if (andNeeded) { sb.append(" AND "); @@ -191,8 +336,8 @@ static boolean addNumericConstraint(StringBuilder sb, String var, Long value, bo return andNeeded; } - static void cleanPreparedDir(Path preparedDir, int preparedCount) throws IOException { - + static void cleanPreparedDir(Path preparedDir, int preparedCount) + throws IOException { Map dateMap = new HashMap<>(); File[] files = preparedDir.toFile().listFiles(); int ndel = files.length - preparedCount; @@ -249,13 +394,17 @@ public void exit() { @PostConstruct public void init() { try { - PropertyHandler propertyHandler = PropertyHandler.getInstance(); - FiniteStateMachine.createInstance(reader, lockManager, propertyHandler.getStorageUnit()); + FiniteStateMachine.createInstance( + reader, + lockManager, + propertyHandler.getStorageUnit() + ); this.fsm = FiniteStateMachine.getInstance(); this.fsm.init(); //if not yet initialized by IdsService do it now - - sizeCheckIntervalMillis = propertyHandler.getSizeCheckIntervalMillis(); + + sizeCheckIntervalMillis = + propertyHandler.getSizeCheckIntervalMillis(); preparedCount = propertyHandler.getPreparedCount(); preparedDir = propertyHandler.getCacheDir().resolve("prepared"); Files.createDirectories(preparedDir); @@ -273,9 +422,9 @@ public void init() { logger.info("Tidier started"); } catch (IOException e) { - throw new RuntimeException("Tidier reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException( + "Tidier reports " + e.getClass() + " " + e.getMessage() + ); } - } - } diff --git a/src/main/java/org/icatproject/ids/enums/CallType.java b/src/main/java/org/icatproject/ids/enums/CallType.java index 835e9956..2f3edbd8 100644 --- a/src/main/java/org/icatproject/ids/enums/CallType.java +++ b/src/main/java/org/icatproject/ids/enums/CallType.java @@ -1,5 +1,9 @@ package org.icatproject.ids.enums; public enum CallType { - INFO, PREPARE, READ, WRITE, MIGRATE + INFO, + PREPARE, + READ, + WRITE, + MIGRATE, } diff --git a/src/main/java/org/icatproject/ids/enums/DeferredOp.java b/src/main/java/org/icatproject/ids/enums/DeferredOp.java index 3cda8707..ef159a29 100644 --- a/src/main/java/org/icatproject/ids/enums/DeferredOp.java +++ b/src/main/java/org/icatproject/ids/enums/DeferredOp.java @@ -6,5 +6,8 @@ * decides what real action should be performed on data entities. */ public enum DeferredOp { - ARCHIVE, RESTORE, WRITE, DELETE + ARCHIVE, + RESTORE, + WRITE, + DELETE, } diff --git a/src/main/java/org/icatproject/ids/enums/RequestIdNames.java b/src/main/java/org/icatproject/ids/enums/RequestIdNames.java index 8bfdb689..3432a218 100644 --- a/src/main/java/org/icatproject/ids/enums/RequestIdNames.java +++ b/src/main/java/org/icatproject/ids/enums/RequestIdNames.java @@ -1,7 +1,7 @@ package org.icatproject.ids.enums; public class RequestIdNames { + public static final String sessionId = "sessionId"; public static final String preparedId = "preparedId"; - } diff --git a/src/main/java/org/icatproject/ids/enums/RequestType.java b/src/main/java/org/icatproject/ids/enums/RequestType.java index a79f83e5..9b154e8b 100644 --- a/src/main/java/org/icatproject/ids/enums/RequestType.java +++ b/src/main/java/org/icatproject/ids/enums/RequestType.java @@ -4,20 +4,20 @@ * This enum contains all defined types of requests to this server */ public enum RequestType { - GETDATA, - ARCHIVE, - GETICATURL, - GETDATAFILEIDS, - GETSERVICESTATUS, - GETSIZE, - GETSTATUS, - ISPREPARED, - ISREADONLY, - ISTWOLEVEL, - PREPAREDATA, + GETDATA, + ARCHIVE, + GETICATURL, + GETDATAFILEIDS, + GETSERVICESTATUS, + GETSIZE, + GETSTATUS, + ISPREPARED, + ISREADONLY, + ISTWOLEVEL, + PREPAREDATA, PUT, RESET, RESTORE, WRITE, - DELETE + DELETE, } diff --git a/src/main/java/org/icatproject/ids/enums/Status.java b/src/main/java/org/icatproject/ids/enums/Status.java index abc187aa..8b5392f9 100644 --- a/src/main/java/org/icatproject/ids/enums/Status.java +++ b/src/main/java/org/icatproject/ids/enums/Status.java @@ -5,5 +5,7 @@ * They are only used when returning status to the client, nowhere else. */ public enum Status { - ONLINE, RESTORING, ARCHIVED + ONLINE, + RESTORING, + ARCHIVED, } diff --git a/src/main/java/org/icatproject/ids/enums/StorageUnit.java b/src/main/java/org/icatproject/ids/enums/StorageUnit.java index bd5779a3..43aab195 100644 --- a/src/main/java/org/icatproject/ids/enums/StorageUnit.java +++ b/src/main/java/org/icatproject/ids/enums/StorageUnit.java @@ -1,5 +1,6 @@ package org.icatproject.ids.enums; public enum StorageUnit { - DATASET, DATAFILE + DATASET, + DATAFILE, } diff --git a/src/main/java/org/icatproject/ids/enums/ValueContainerType.java b/src/main/java/org/icatproject/ids/enums/ValueContainerType.java index cf7501ac..dd4cf64f 100644 --- a/src/main/java/org/icatproject/ids/enums/ValueContainerType.java +++ b/src/main/java/org/icatproject/ids/enums/ValueContainerType.java @@ -4,5 +4,12 @@ * This enum provides all possible values of a ValueContainer */ public enum ValueContainerType { - INVALID, VOID, INT, LONG, BOOL, STRING, RESPONSE, INPUTSTREAM + INVALID, + VOID, + INT, + LONG, + BOOL, + STRING, + RESPONSE, + INPUTSTREAM, } diff --git a/src/main/java/org/icatproject/ids/exceptions/DataNotOnlineException.java b/src/main/java/org/icatproject/ids/exceptions/DataNotOnlineException.java index 567f1257..7afd5771 100644 --- a/src/main/java/org/icatproject/ids/exceptions/DataNotOnlineException.java +++ b/src/main/java/org/icatproject/ids/exceptions/DataNotOnlineException.java @@ -8,5 +8,4 @@ public class DataNotOnlineException extends IdsException { public DataNotOnlineException(String msg) { super(HttpURLConnection.HTTP_UNAVAILABLE, msg); } - } diff --git a/src/main/java/org/icatproject/ids/exceptions/IdsException.java b/src/main/java/org/icatproject/ids/exceptions/IdsException.java index 007baf4d..bfc13258 100644 --- a/src/main/java/org/icatproject/ids/exceptions/IdsException.java +++ b/src/main/java/org/icatproject/ids/exceptions/IdsException.java @@ -7,6 +7,7 @@ */ @SuppressWarnings("serial") public class IdsException extends Exception { + private int httpStatusCode; private String message; @@ -26,5 +27,4 @@ public int getHttpStatusCode() { public String getMessage() { return "(" + httpStatusCode + ") : " + message; } - } diff --git a/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java index 77920041..be9d1f25 100644 --- a/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java @@ -1,12 +1,11 @@ package org.icatproject.ids.exceptions; -import java.io.ByteArrayOutputStream; - import jakarta.json.Json; import jakarta.json.stream.JsonGenerator; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; +import java.io.ByteArrayOutputStream; /** * Capture any {@link org.icatproject.ids.exceptions.IdsException WebServiceException} thrown from @@ -20,9 +19,14 @@ public class IdsExceptionMapper implements ExceptionMapper { public Response toResponse(IdsException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen.writeStartObject().write("code", e.getClass().getSimpleName()) - .write("message", e.getShortMessage()); + gen + .writeStartObject() + .write("code", e.getClass().getSimpleName()) + .write("message", e.getShortMessage()); gen.writeEnd().close(); - return Response.status(e.getHttpStatusCode()).entity(baos.toString()).build(); + return Response + .status(e.getHttpStatusCode()) + .entity(baos.toString()) + .build(); } } diff --git a/src/main/java/org/icatproject/ids/exceptions/InsufficientStorageException.java b/src/main/java/org/icatproject/ids/exceptions/InsufficientStorageException.java index 360eae14..01b60e9c 100644 --- a/src/main/java/org/icatproject/ids/exceptions/InsufficientStorageException.java +++ b/src/main/java/org/icatproject/ids/exceptions/InsufficientStorageException.java @@ -3,6 +3,7 @@ import java.net.HttpURLConnection; public class InsufficientStorageException extends IdsException { + private static final long serialVersionUID = 1L; public InsufficientStorageException(String message) { diff --git a/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java index f9157c92..7fb6ecfc 100644 --- a/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java @@ -1,29 +1,37 @@ package org.icatproject.ids.exceptions; -import java.io.ByteArrayOutputStream; - import jakarta.json.Json; import jakarta.json.stream.JsonGenerator; import jakarta.ws.rs.NotFoundException; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; - +import java.io.ByteArrayOutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Provider -public class NotFoundExceptionMapper implements ExceptionMapper { +public class NotFoundExceptionMapper + implements ExceptionMapper { - private static Logger logger = LoggerFactory.getLogger(NotFoundExceptionMapper.class); + private static Logger logger = LoggerFactory.getLogger( + NotFoundExceptionMapper.class + ); @Override public Response toResponse(NotFoundException e) { logger.info("Processing: " + e.getClass() + " " + e.getMessage(), e); ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen.writeStartObject().write("code", "NOT_IMPLEMENTED") - .write("message", "Operation not implemented by this IDS server.").writeEnd().close(); - return Response.status(Response.Status.NOT_IMPLEMENTED).entity(baos.toString()).build(); + gen + .writeStartObject() + .write("code", "NOT_IMPLEMENTED") + .write("message", "Operation not implemented by this IDS server.") + .writeEnd() + .close(); + return Response + .status(Response.Status.NOT_IMPLEMENTED) + .entity(baos.toString()) + .build(); } } diff --git a/src/main/java/org/icatproject/ids/exceptions/NotImplementedException.java b/src/main/java/org/icatproject/ids/exceptions/NotImplementedException.java index 802d4fe6..aef12495 100644 --- a/src/main/java/org/icatproject/ids/exceptions/NotImplementedException.java +++ b/src/main/java/org/icatproject/ids/exceptions/NotImplementedException.java @@ -8,5 +8,4 @@ public class NotImplementedException extends IdsException { public NotImplementedException(String message) { super(HttpURLConnection.HTTP_NOT_IMPLEMENTED, message); } - } diff --git a/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java index aa38ba0a..f3c9c6ce 100644 --- a/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java @@ -1,34 +1,40 @@ package org.icatproject.ids.exceptions; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.net.HttpURLConnection; - import jakarta.json.Json; import jakarta.json.stream.JsonGenerator; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; - +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.net.HttpURLConnection; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Provider -public class RuntimeExceptionMapper implements ExceptionMapper { +public class RuntimeExceptionMapper + implements ExceptionMapper { - private final static Logger logger = LoggerFactory.getLogger(RuntimeExceptionMapper.class); + private static final Logger logger = LoggerFactory.getLogger( + RuntimeExceptionMapper.class + ); @Override public Response toResponse(RuntimeException e) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); logger.error("Processing: " + baos.toString()); baos.reset(); JsonGenerator gen = Json.createGenerator(baos); - gen.writeStartObject().write("code", "InternalException") - .write("message", e.getClass() + " " + e.getMessage()).writeEnd().close(); - return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR).entity(baos.toString()) - .build(); + gen + .writeStartObject() + .write("code", "InternalException") + .write("message", e.getClass() + " " + e.getMessage()) + .writeEnd() + .close(); + return Response + .status(HttpURLConnection.HTTP_INTERNAL_ERROR) + .entity(baos.toString()) + .build(); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java index f115c8da..2cebb277 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java @@ -1,5 +1,7 @@ package org.icatproject.ids.finiteStateMachine; +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.Files; @@ -12,24 +14,19 @@ import java.util.Set; import java.util.Timer; import java.util.concurrent.ConcurrentHashMap; - -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.IcatReader; import org.icatproject.ids.services.LockManager; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.LockInfo; +import org.icatproject.ids.services.PropertyHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class FiniteStateMachine { - private static FiniteStateMachine instance; private static Boolean inited = false; @@ -39,36 +36,57 @@ protected FiniteStateMachine(IcatReader reader, LockManager lockManager) { this.lockManager = lockManager; } - public static void createInstance(IcatReader reader, LockManager lockManager, StorageUnit storageUnit) { - - if(instance == null) { - if(storageUnit == StorageUnit.DATAFILE) - instance = new FiniteStateMachineForStorageUnitDatafile(reader, lockManager); - else if(storageUnit == StorageUnit.DATASET) - instance = new FiniteStateMachineForStorageUnitDataset(reader, lockManager); - else - instance = new FiniteStateMachineForSingleLevelStorage(reader, lockManager); + public static void createInstance( + IcatReader reader, + LockManager lockManager, + StorageUnit storageUnit + ) { + if (instance == null) { + if (storageUnit == StorageUnit.DATAFILE) instance = + new FiniteStateMachineForStorageUnitDatafile( + reader, + lockManager + ); else if (storageUnit == StorageUnit.DATASET) instance = + new FiniteStateMachineForStorageUnitDataset( + reader, + lockManager + ); else instance = + new FiniteStateMachineForSingleLevelStorage( + reader, + lockManager + ); } } public static FiniteStateMachine getInstance() { - if(instance != null) { + if (instance != null) { return instance; } // If this assert was executed: Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once. - throw new RuntimeException("Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once."); + throw new RuntimeException( + "Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once." + ); } - public abstract void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException; + public abstract void queue(DataInfoBase dataInfo, DeferredOp deferredOp) + throws InternalException; + protected abstract void scheduleTimer(); + protected abstract void addDataInfoJson(JsonGenerator gen); public enum RequestedState { - ARCHIVE_REQUESTED, DELETE_REQUESTED, RESTORE_REQUESTED, WRITE_REQUESTED, WRITE_THEN_ARCHIVE_REQUESTED + ARCHIVE_REQUESTED, + DELETE_REQUESTED, + RESTORE_REQUESTED, + WRITE_REQUESTED, + WRITE_THEN_ARCHIVE_REQUESTED, } - protected static Logger logger = LoggerFactory.getLogger(FiniteStateMachine.class); + protected static Logger logger = LoggerFactory.getLogger( + FiniteStateMachine.class + ); /* * Note that the veriable processOpsDelayMillis is used to either delay all deferred @@ -145,23 +163,32 @@ protected void addDataInfoJsonFromDeferredOpsQueue(JsonGenerator gen) { gen.writeStartArray("opsQueue"); for (Entry entry : union.entrySet()) { DataInfoBase item = entry.getKey(); - gen.writeStartObject().write("data", item.toString()).write("request", entry.getValue().name()) - .writeEnd(); + gen + .writeStartObject() + .write("data", item.toString()) + .write("request", entry.getValue().name()) + .writeEnd(); } gen.writeEnd(); // end Array("opsQueue") } public String getServiceStatus() throws InternalException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { - + try ( + JsonGenerator gen = Json.createGenerator(baos).writeStartObject() + ) { this.addDataInfoJson(gen); Collection lockInfo = lockManager.getLockInfo(); gen.write("lockCount", lockInfo.size()); gen.writeStartArray("locks"); for (LockInfo li : lockInfo) { - gen.writeStartObject().write("id", li.id).write("type", li.type.name()).write("count", li.count).writeEnd(); + gen + .writeStartObject() + .write("id", li.id) + .write("type", li.type.name()) + .write("count", li.count) + .writeEnd(); } gen.writeEnd(); // end Array("locks") @@ -176,26 +203,30 @@ public String getServiceStatus() throws InternalException { return baos.toString(); } - public void init() { - try { synchronized (inited) { - if(!inited) { + if (!inited) { propertyHandler = PropertyHandler.getInstance(); - processQueueIntervalMillis = propertyHandler.getProcessQueueIntervalSeconds() * 1000L; + processQueueIntervalMillis = + propertyHandler.getProcessQueueIntervalSeconds() * + 1000L; this.scheduleTimer(); - + markerDir = propertyHandler.getCacheDir().resolve("marker"); Files.createDirectories(markerDir); inited = true; } } } catch (IOException e) { - throw new RuntimeException("FiniteStateMachine reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException( + "FiniteStateMachine reports " + + e.getClass() + + " " + + e.getMessage() + ); } - } public void removeFromChanging(DataInfoBase dfInfo) { @@ -221,5 +252,4 @@ public void checkFailure(Long id) throws InternalException { throw new InternalException("Restore failed"); } } - } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java index 7ffb349d..081d8a4e 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java @@ -1,35 +1,37 @@ package org.icatproject.ids.finiteStateMachine; +import jakarta.json.stream.JsonGenerator; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.IcatReader; import org.icatproject.ids.services.LockManager; -import jakarta.json.stream.JsonGenerator; - -public class FiniteStateMachineForSingleLevelStorage extends FiniteStateMachine { +public class FiniteStateMachineForSingleLevelStorage + extends FiniteStateMachine { - protected FiniteStateMachineForSingleLevelStorage(IcatReader reader, LockManager lockManager) { + protected FiniteStateMachineForSingleLevelStorage( + IcatReader reader, + LockManager lockManager + ) { super(reader, lockManager); } - @Override - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { - throw new InternalException("### Operation is not permitted for single level storage"); + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) + throws InternalException { + throw new InternalException( + "### Operation is not permitted for single level storage" + ); } - @Override protected void scheduleTimer() { //nothing to do here for single level storage } - @Override protected void addDataInfoJson(JsonGenerator gen) { gen.writeStartArray("opsQueue").writeEnd(); } - } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java index 88543bb1..f949e284 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java @@ -1,5 +1,6 @@ package org.icatproject.ids.finiteStateMachine; +import jakarta.json.stream.JsonGenerator; import java.io.IOException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; @@ -11,14 +12,12 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; - import java.util.TimerTask; - import org.icatproject.Dataset; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.exceptions.InternalException; -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.services.IcatReader; @@ -30,40 +29,46 @@ import org.icatproject.ids.thread.DfRestorer; import org.icatproject.ids.thread.DfWriter; -import jakarta.json.stream.JsonGenerator; - -public class FiniteStateMachineForStorageUnitDatafile extends FiniteStateMachine { +public class FiniteStateMachineForStorageUnitDatafile + extends FiniteStateMachine { - protected FiniteStateMachineForStorageUnitDatafile(IcatReader icatReader, LockManager lockManager) { + protected FiniteStateMachineForStorageUnitDatafile( + IcatReader icatReader, + LockManager lockManager + ) { super(icatReader, lockManager); } - @Override protected void scheduleTimer() { - processOpsDelayMillis = propertyHandler.getDelayDatafileOperations() * 1000L; + processOpsDelayMillis = + propertyHandler.getDelayDatafileOperations() * 1000L; timer.schedule(new DfProcessQueue(), processQueueIntervalMillis); - logger.info("DfProcessQueue scheduled to run in " + processQueueIntervalMillis + " milliseconds"); + logger.info( + "DfProcessQueue scheduled to run in " + + processQueueIntervalMillis + + " milliseconds" + ); } - @Override protected void addDataInfoJson(JsonGenerator gen) { this.addDataInfoJsonFromDeferredOpsQueue(gen); } - - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { - + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) + throws InternalException { var dfInfo = (DatafileInfo) dataInfo; - if(dfInfo == null) throw new InternalException("DataInfoBase object could not be casted into a DataFileInfo. Did you handed over a DataSetInfo instead?"); + if (dfInfo == null) throw new InternalException( + "DataInfoBase object could not be casted into a DataFileInfo. Did you handed over a DataSetInfo instead?" + ); logger.info("Requesting " + deferredOp + " of datafile " + dfInfo); synchronized (deferredOpsQueue) { - if (processOpsTime == null) { - processOpsTime = System.currentTimeMillis() + processOpsDelayMillis; + processOpsTime = + System.currentTimeMillis() + processOpsDelayMillis; final Date d = new Date(processOpsTime); logger.debug("Requesting delay operations till " + d); } @@ -72,62 +77,100 @@ public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalE if (state == null) { if (deferredOp == DeferredOp.WRITE) { try { - Path marker = markerDir.resolve(Long.toString(dfInfo.getId())); + Path marker = markerDir.resolve( + Long.toString(dfInfo.getId()) + ); Files.createFile(marker); logger.debug("Created marker " + marker); } catch (FileAlreadyExistsException e) { // Pass will ignore this } catch (IOException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } - deferredOpsQueue.put(dfInfo, RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put( + dfInfo, + RequestedState.WRITE_REQUESTED + ); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put( + dfInfo, + RequestedState.ARCHIVE_REQUESTED + ); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dfInfo, RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put( + dfInfo, + RequestedState.RESTORE_REQUESTED + ); } else if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put( + dfInfo, + RequestedState.DELETE_REQUESTED + ); } } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.RESTORE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put( + dfInfo, + RequestedState.DELETE_REQUESTED + ); } } else if (state == RequestedState.DELETE_REQUESTED) { // No way out } else if (state == RequestedState.RESTORE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put( + dfInfo, + RequestedState.DELETE_REQUESTED + ); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put( + dfInfo, + RequestedState.ARCHIVE_REQUESTED + ); } } else if (state == RequestedState.WRITE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put( + dfInfo, + RequestedState.WRITE_THEN_ARCHIVE_REQUESTED + ); } } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dfInfo, RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put( + dfInfo, + RequestedState.WRITE_REQUESTED + ); } } } } - private class DfProcessQueue extends TimerTask { @Override public void run() { try { synchronized (deferredOpsQueue) { - if (processOpsTime != null && System.currentTimeMillis() > processOpsTime && !deferredOpsQueue.isEmpty()) { + if ( + processOpsTime != null && + System.currentTimeMillis() > processOpsTime && + !deferredOpsQueue.isEmpty() + ) { processOpsTime = null; - logger.debug("deferredDfOpsQueue has " + deferredOpsQueue.size() + " entries"); + logger.debug( + "deferredDfOpsQueue has " + + deferredOpsQueue.size() + + " entries" + ); List writes = new ArrayList<>(); List archives = new ArrayList<>(); List restores = new ArrayList<>(); @@ -138,20 +181,31 @@ public void run() { Map deleteLocks = new HashMap<>(); Map newOps = new HashMap<>(); - final Iterator> it = deferredOpsQueue.entrySet().iterator(); + final Iterator> it = deferredOpsQueue + .entrySet() + .iterator(); while (it.hasNext()) { Entry opEntry = it.next(); var dfInfo = (DatafileInfo) opEntry.getKey(); - if(dfInfo == null) throw new RuntimeException("Could not cast DataInfoBase to DataFileInfo. Did you handed over another sub type?"); + if (dfInfo == null) throw new RuntimeException( + "Could not cast DataInfoBase to DataFileInfo. Did you handed over another sub type?" + ); Long dsId = dfInfo.getDsId(); DatasetInfo dsInfo; try { - Dataset ds = (Dataset) reader.get("Dataset ds INCLUDE ds.investigation.facility", dsId); + Dataset ds = (Dataset) reader.get( + "Dataset ds INCLUDE ds.investigation.facility", + dsId + ); dsInfo = new DatasetInfo(ds); } catch (Exception e) { - logger.error("Could not get dsInfo {}: {}.", dsId, e.getMessage()); + logger.error( + "Could not get dsInfo {}: {}.", + dsId, + e.getMessage() + ); continue; } if (!dataInfoChanging.containsKey(dfInfo)) { @@ -160,73 +214,168 @@ public void run() { if (state == RequestedState.WRITE_REQUESTED) { if (!writeLocks.containsKey(dsId)) { try { - writeLocks.put(dsId, lockManager.lock(dsInfo, LockType.SHARED)); + writeLocks.put( + dsId, + lockManager.lock( + dsInfo, + LockType.SHARED + ) + ); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state + ); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error( + "I/O exception " + + e.getMessage() + + " locking " + + dsId + ); continue; } } it.remove(); dataInfoChanging.put(dfInfo, state); writes.add(dfInfo); - } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { + } else if ( + state == + RequestedState.WRITE_THEN_ARCHIVE_REQUESTED + ) { if (!writeLocks.containsKey(dsId)) { try { - writeLocks.put(dsId, lockManager.lock(dsInfo, LockType.SHARED)); + writeLocks.put( + dsId, + lockManager.lock( + dsInfo, + LockType.SHARED + ) + ); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state + ); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error( + "I/O exception " + + e.getMessage() + + " locking " + + dsId + ); continue; } } it.remove(); - dataInfoChanging.put(dfInfo, RequestedState.WRITE_REQUESTED); + dataInfoChanging.put( + dfInfo, + RequestedState.WRITE_REQUESTED + ); writes.add(dfInfo); - newOps.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); - } else if (state == RequestedState.ARCHIVE_REQUESTED) { + newOps.put( + dfInfo, + RequestedState.ARCHIVE_REQUESTED + ); + } else if ( + state == RequestedState.ARCHIVE_REQUESTED + ) { if (!archiveLocks.containsKey(dsId)) { try { - archiveLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); + archiveLocks.put( + dsId, + lockManager.lock( + dsInfo, + LockType.EXCLUSIVE + ) + ); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state + ); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error( + "I/O exception " + + e.getMessage() + + " locking " + + dsId + ); continue; } } it.remove(); dataInfoChanging.put(dfInfo, state); archives.add(dfInfo); - } else if (state == RequestedState.RESTORE_REQUESTED) { + } else if ( + state == RequestedState.RESTORE_REQUESTED + ) { if (!restoreLocks.containsKey(dsId)) { try { - restoreLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); + restoreLocks.put( + dsId, + lockManager.lock( + dsInfo, + LockType.EXCLUSIVE + ) + ); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state + ); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error( + "I/O exception " + + e.getMessage() + + " locking " + + dsId + ); continue; } } it.remove(); dataInfoChanging.put(dfInfo, state); restores.add(dfInfo); - } else if (state == RequestedState.DELETE_REQUESTED) { + } else if ( + state == RequestedState.DELETE_REQUESTED + ) { if (!deleteLocks.containsKey(dsId)) { try { - deleteLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); + deleteLocks.put( + dsId, + lockManager.lock( + dsInfo, + LockType.EXCLUSIVE + ) + ); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state + ); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error( + "I/O exception " + + e.getMessage() + + " locking " + + dsId + ); continue; } } @@ -234,44 +383,94 @@ public void run() { dataInfoChanging.put(dfInfo, state); deletes.add(dfInfo); } else { - throw new AssertionError("Impossible state"); + throw new AssertionError( + "Impossible state" + ); } } } if (!newOps.isEmpty()) { deferredOpsQueue.putAll(newOps); - logger.debug("Adding {} operations to be scheduled next time round", newOps.size()); + logger.debug( + "Adding {} operations to be scheduled next time round", + newOps.size() + ); } if (!deferredOpsQueue.isEmpty()) { processOpsTime = 0L; } if (!writes.isEmpty()) { - logger.debug("Launch thread to process " + writes.size() + " writes"); - Thread w = new Thread(new DfWriter(writes, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, writeLocks.values())); + logger.debug( + "Launch thread to process " + + writes.size() + + " writes" + ); + Thread w = new Thread( + new DfWriter( + writes, + propertyHandler, + FiniteStateMachineForStorageUnitDatafile.this, + writeLocks.values() + ) + ); w.start(); } if (!archives.isEmpty()) { - logger.debug("Launch thread to process " + archives.size() + " archives"); - Thread w = new Thread(new DfArchiver(archives, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, archiveLocks.values())); + logger.debug( + "Launch thread to process " + + archives.size() + + " archives" + ); + Thread w = new Thread( + new DfArchiver( + archives, + propertyHandler, + FiniteStateMachineForStorageUnitDatafile.this, + archiveLocks.values() + ) + ); w.start(); } if (!restores.isEmpty()) { - logger.debug("Launch thread to process " + restores.size() + " restores"); - Thread w = new Thread(new DfRestorer(restores, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, restoreLocks.values())); + logger.debug( + "Launch thread to process " + + restores.size() + + " restores" + ); + Thread w = new Thread( + new DfRestorer( + restores, + propertyHandler, + FiniteStateMachineForStorageUnitDatafile.this, + restoreLocks.values() + ) + ); w.start(); } if (!deletes.isEmpty()) { - logger.debug("Launch thread to process " + deletes.size() + " deletes"); - Thread w = new Thread(new DfDeleter(deletes, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, deleteLocks.values())); + logger.debug( + "Launch thread to process " + + deletes.size() + + " deletes" + ); + Thread w = new Thread( + new DfDeleter( + deletes, + propertyHandler, + FiniteStateMachineForStorageUnitDatafile.this, + deleteLocks.values() + ) + ); w.start(); } } } } finally { - timer.schedule(new DfProcessQueue(), processQueueIntervalMillis); + timer.schedule( + new DfProcessQueue(), + processQueueIntervalMillis + ); } - } - } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java index cbe0a5cd..10846beb 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java @@ -1,5 +1,6 @@ package org.icatproject.ids.finiteStateMachine; +import jakarta.json.stream.JsonGenerator; import java.io.IOException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; @@ -10,7 +11,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.TimerTask; - import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; @@ -24,22 +24,27 @@ import org.icatproject.ids.thread.DsRestorer; import org.icatproject.ids.thread.DsWriter; -import jakarta.json.stream.JsonGenerator; - -public class FiniteStateMachineForStorageUnitDataset extends FiniteStateMachine { +public class FiniteStateMachineForStorageUnitDataset + extends FiniteStateMachine { protected Map writeTimes = new HashMap<>(); - protected FiniteStateMachineForStorageUnitDataset(IcatReader reader, LockManager lockManager) { + protected FiniteStateMachineForStorageUnitDataset( + IcatReader reader, + LockManager lockManager + ) { super(reader, lockManager); } - @Override protected void scheduleTimer() { processOpsDelayMillis = propertyHandler.getDelayDatasetWrites() * 1000L; timer.schedule(new DsProcessQueue(), processQueueIntervalMillis); - logger.info("DsProcessQueue scheduled to run in " + processQueueIntervalMillis + " milliseconds"); + logger.info( + "DsProcessQueue scheduled to run in " + + processQueueIntervalMillis + + " milliseconds" + ); } @Override @@ -47,54 +52,70 @@ protected void addDataInfoJson(JsonGenerator gen) { this.addDataInfoJsonFromDeferredOpsQueue(gen); } - - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { - - + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) + throws InternalException { logger.info("Requesting " + deferredOp + " of dataset " + dataInfo); synchronized (deferredOpsQueue) { - final RequestedState state = this.deferredOpsQueue.get(dataInfo); if (state == null) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put( + dataInfo, + RequestedState.ARCHIVE_REQUESTED + ); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put( + dataInfo, + RequestedState.RESTORE_REQUESTED + ); } } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); - deferredOpsQueue.put(dataInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put( + dataInfo, + RequestedState.WRITE_THEN_ARCHIVE_REQUESTED + ); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put( + dataInfo, + RequestedState.RESTORE_REQUESTED + ); } } else if (state == RequestedState.RESTORE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put( + dataInfo, + RequestedState.ARCHIVE_REQUESTED + ); } } else if (state == RequestedState.WRITE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { setDelay(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put( + dataInfo, + RequestedState.WRITE_THEN_ARCHIVE_REQUESTED + ); } } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { setDelay(dataInfo); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put( + dataInfo, + RequestedState.WRITE_REQUESTED + ); } } } - } - private void requestWrite(DataInfoBase dsInfo) throws InternalException { try { Path marker = markerDir.resolve(Long.toString(dsInfo.getId())); @@ -109,16 +130,22 @@ private void requestWrite(DataInfoBase dsInfo) throws InternalException { setDelay(dsInfo); } - private void setDelay(DataInfoBase dsInfo) { - writeTimes.put(dsInfo, System.currentTimeMillis() + processOpsDelayMillis); + writeTimes.put( + dsInfo, + System.currentTimeMillis() + processOpsDelayMillis + ); if (logger.isDebugEnabled()) { final Date d = new Date(writeTimes.get(dsInfo)); - logger.debug("Requesting delay of writing of dataset " + dsInfo + " till " + d); + logger.debug( + "Requesting delay of writing of dataset " + + dsInfo + + " till " + + d + ); } } - private class DsProcessQueue extends TimerTask { @Override @@ -127,77 +154,169 @@ public void run() { synchronized (deferredOpsQueue) { final long now = System.currentTimeMillis(); Map newOps = new HashMap<>(); - final Iterator> it = deferredOpsQueue.entrySet().iterator(); + final Iterator> it = deferredOpsQueue + .entrySet() + .iterator(); while (it.hasNext()) { final Entry opEntry = it.next(); final var dsInfo = (DatasetInfo) opEntry.getKey(); - - if(dsInfo == null) throw new RuntimeException("Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); + if (dsInfo == null) throw new RuntimeException( + "Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?" + ); if (!dataInfoChanging.containsKey(dsInfo)) { final RequestedState state = opEntry.getValue(); - if (state == RequestedState.WRITE_REQUESTED - || state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { + if ( + state == RequestedState.WRITE_REQUESTED || + state == + RequestedState.WRITE_THEN_ARCHIVE_REQUESTED + ) { if (now > writeTimes.get(dsInfo)) { try { - Lock lock = lockManager.lock(dsInfo, LockType.SHARED); - logger.debug("Will process " + dsInfo + " with " + state); + Lock lock = lockManager.lock( + dsInfo, + LockType.SHARED + ); + logger.debug( + "Will process " + + dsInfo + + " with " + + state + ); writeTimes.remove(dsInfo); - dataInfoChanging.put(dsInfo, RequestedState.WRITE_REQUESTED); + dataInfoChanging.put( + dsInfo, + RequestedState.WRITE_REQUESTED + ); it.remove(); final Thread w = new Thread( - new DsWriter(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, reader, lock)); + new DsWriter( + dsInfo, + propertyHandler, + FiniteStateMachineForStorageUnitDataset.this, + reader, + lock + ) + ); w.start(); - if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { - newOps.put(dsInfo, RequestedState.ARCHIVE_REQUESTED); + if ( + state == + RequestedState.WRITE_THEN_ARCHIVE_REQUESTED + ) { + newOps.put( + dsInfo, + RequestedState.ARCHIVE_REQUESTED + ); } } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); + logger.debug( + "Could not acquire lock on " + + dsInfo + + ", hold back process with " + + state + ); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); + logger.error( + "I/O exception " + + e.getMessage() + + " locking " + + dsInfo + ); } } - } else if (state == RequestedState.ARCHIVE_REQUESTED) { + } else if ( + state == RequestedState.ARCHIVE_REQUESTED + ) { try { - Lock lock = lockManager.lock(dsInfo, LockType.EXCLUSIVE); + Lock lock = lockManager.lock( + dsInfo, + LockType.EXCLUSIVE + ); it.remove(); - logger.debug("Will process " + dsInfo + " with " + state); + logger.debug( + "Will process " + + dsInfo + + " with " + + state + ); dataInfoChanging.put(dsInfo, state); final Thread w = new Thread( - new DsArchiver(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, lock)); + new DsArchiver( + dsInfo, + propertyHandler, + FiniteStateMachineForStorageUnitDataset.this, + lock + ) + ); w.start(); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); + logger.debug( + "Could not acquire lock on " + + dsInfo + + ", hold back process with " + + state + ); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); + logger.error( + "I/O exception " + + e.getMessage() + + " locking " + + dsInfo + ); } - } else if (state == RequestedState.RESTORE_REQUESTED) { + } else if ( + state == RequestedState.RESTORE_REQUESTED + ) { try { - Lock lock = lockManager.lock(dsInfo, LockType.EXCLUSIVE); - logger.debug("Will process " + dsInfo + " with " + state); + Lock lock = lockManager.lock( + dsInfo, + LockType.EXCLUSIVE + ); + logger.debug( + "Will process " + + dsInfo + + " with " + + state + ); dataInfoChanging.put(dsInfo, state); it.remove(); final Thread w = new Thread( - new DsRestorer(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, reader, lock)); + new DsRestorer( + dsInfo, + propertyHandler, + FiniteStateMachineForStorageUnitDataset.this, + reader, + lock + ) + ); w.start(); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); + logger.debug( + "Could not acquire lock on " + + dsInfo + + ", hold back process with " + + state + ); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); + logger.error( + "I/O exception " + + e.getMessage() + + " locking " + + dsInfo + ); } } } } deferredOpsQueue.putAll(newOps); } - } finally { - timer.schedule(new DsProcessQueue(), processQueueIntervalMillis); + timer.schedule( + new DsProcessQueue(), + processQueueIntervalMillis + ); } - } - } - } diff --git a/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java b/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java index 8c813be8..e95e3165 100644 --- a/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java +++ b/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java @@ -1,19 +1,19 @@ package org.icatproject.ids.helpers; -import java.io.IOException; - import jakarta.ws.rs.container.ContainerRequestContext; import jakarta.ws.rs.container.ContainerResponseContext; import jakarta.ws.rs.container.ContainerResponseFilter; import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.ext.Provider; +import java.io.IOException; @Provider public class CORSResponseFilter implements ContainerResponseFilter { - public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) - throws IOException { - + public void filter( + ContainerRequestContext requestContext, + ContainerResponseContext responseContext + ) throws IOException { MultivaluedMap headers = responseContext.getHeaders(); headers.add("Access-Control-Allow-Origin", "*"); headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); diff --git a/src/main/java/org/icatproject/ids/helpers/CheckedWithSizeInputStream.java b/src/main/java/org/icatproject/ids/helpers/CheckedWithSizeInputStream.java index d3d409e4..f626bde8 100644 --- a/src/main/java/org/icatproject/ids/helpers/CheckedWithSizeInputStream.java +++ b/src/main/java/org/icatproject/ids/helpers/CheckedWithSizeInputStream.java @@ -6,6 +6,7 @@ import java.util.zip.Checksum; public class CheckedWithSizeInputStream extends FilterInputStream { + private Checksum cksum; private long size; diff --git a/src/main/java/org/icatproject/ids/helpers/Constants.java b/src/main/java/org/icatproject/ids/helpers/Constants.java index bc7d4aaa..85011272 100644 --- a/src/main/java/org/icatproject/ids/helpers/Constants.java +++ b/src/main/java/org/icatproject/ids/helpers/Constants.java @@ -5,12 +5,13 @@ import java.util.Properties; public class Constants { + public static String API_VERSION; static { - - InputStream inputStream = Constants.class.getClassLoader().getResourceAsStream( - "app.properties"); + InputStream inputStream = + Constants.class.getClassLoader() + .getResourceAsStream("app.properties"); Properties p = new Properties(); try { @@ -19,6 +20,5 @@ public class Constants { } catch (IOException e) { // API Version will be null } - } } diff --git a/src/main/java/org/icatproject/ids/helpers/LocationHelper.java b/src/main/java/org/icatproject/ids/helpers/LocationHelper.java index b3c1e8b5..052e1c00 100644 --- a/src/main/java/org/icatproject/ids/helpers/LocationHelper.java +++ b/src/main/java/org/icatproject/ids/helpers/LocationHelper.java @@ -1,7 +1,6 @@ package org.icatproject.ids.helpers; import java.security.NoSuchAlgorithmException; - import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.services.ServiceProvider; @@ -10,7 +9,7 @@ public class LocationHelper { public static String getLocation(long dfid, String location) - throws InsufficientPrivilegesException, InternalException { + throws InsufficientPrivilegesException, InternalException { if (location == null) { throw new InternalException("location is null"); } @@ -23,20 +22,27 @@ public static String getLocation(long dfid, String location) } } - - public static String getLocationFromDigest(long id, String locationWithHash, String key) - throws InternalException, InsufficientPrivilegesException { + public static String getLocationFromDigest( + long id, + String locationWithHash, + String key + ) throws InternalException, InsufficientPrivilegesException { int i = locationWithHash.lastIndexOf(' '); try { String location = locationWithHash.substring(0, i); String hash = locationWithHash.substring(i + 1); if (!hash.equals(IcatSecurity.digest(id, location, key))) { throw new InsufficientPrivilegesException( - "Location \"" + locationWithHash + "\" does not contain a valid hash."); + "Location \"" + + locationWithHash + + "\" does not contain a valid hash." + ); } return location; } catch (IndexOutOfBoundsException e) { - throw new InsufficientPrivilegesException("Location \"" + locationWithHash + "\" does not contain hash."); + throw new InsufficientPrivilegesException( + "Location \"" + locationWithHash + "\" does not contain hash." + ); } catch (NoSuchAlgorithmException e) { throw new InternalException(e.getMessage()); } diff --git a/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java b/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java index ffaa34be..58a3374b 100644 --- a/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java +++ b/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java @@ -31,12 +31,12 @@ public RangeOutputStream(OutputStream os, long min, Long count) { super(os); this.min = min; this.max = count == null ? Long.MAX_VALUE : min + count; - if (min < 0) - throw new IllegalArgumentException( - "min must be a non-negative long"); - if (count != null && count < 0) - throw new IllegalArgumentException( - "count must be a non-negative long"); + if (min < 0) throw new IllegalArgumentException( + "min must be a non-negative long" + ); + if (count != null && count < 0) throw new IllegalArgumentException( + "count must be a non-negative long" + ); } /* @@ -61,8 +61,13 @@ public void write(int b) throws IOException { public void write(byte[] b, int off, final int len) throws IOException { if (b == null) { throw new NullPointerException(); - } else if ((off < 0) || (off > b.length) || (len < 0) - || ((off + len) > b.length) || ((off + len) < 0)) { + } else if ( + (off < 0) || + (off > b.length) || + (len < 0) || + ((off + len) > b.length) || + ((off + len) < 0) + ) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; @@ -80,5 +85,4 @@ public void write(byte[] b, int off, final int len) throws IOException { } pos += len; } - } diff --git a/src/main/java/org/icatproject/ids/helpers/SO.java b/src/main/java/org/icatproject/ids/helpers/SO.java index 58e1ee4a..176d2ba5 100644 --- a/src/main/java/org/icatproject/ids/helpers/SO.java +++ b/src/main/java/org/icatproject/ids/helpers/SO.java @@ -1,5 +1,8 @@ package org.icatproject.ids.helpers; +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; +import jakarta.ws.rs.core.StreamingOutput; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -9,19 +12,14 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipOutputStream; - -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DatasetInfo; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.ServiceProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; -import jakarta.ws.rs.core.StreamingOutput; - public class SO implements StreamingOutput { private long offset; @@ -36,10 +34,20 @@ public class SO implements StreamingOutput { private ServiceProvider serviceProvider; private static final int BUFSIZ = 2048; - private final static Logger logger = LoggerFactory.getLogger(SO.class); + private static final Logger logger = LoggerFactory.getLogger(SO.class); - public SO(Map dsInfos, Map dfInfos, long offset, boolean zip, boolean compress, - Lock lock, Long transferId, String ip, long start, ServiceProvider serviceProvider) { + public SO( + Map dsInfos, + Map dfInfos, + long offset, + boolean zip, + boolean compress, + Lock lock, + Long transferId, + String ip, + long start, + ServiceProvider serviceProvider + ) { this.offset = offset; this.zip = zip; this.dsInfos = dsInfos; @@ -54,7 +62,6 @@ public SO(Map dsInfos, Map dfInfos, long @Override public void write(OutputStream output) throws IOException { - Object transfer = "??"; try { if (offset != 0) { // Wrap the stream if needed @@ -62,21 +69,35 @@ public void write(OutputStream output) throws IOException { } byte[] bytes = new byte[BUFSIZ]; if (zip) { - ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(output)); + ZipOutputStream zos = new ZipOutputStream( + new BufferedOutputStream(output) + ); if (!compress) { zos.setLevel(0); // Otherwise use default compression } - for ( DataInfoBase dataInfo : dfInfos.values()) { + for (DataInfoBase dataInfo : dfInfos.values()) { var dfInfo = (DatafileInfo) dataInfo; logger.debug("Adding " + dfInfo + " to zip"); transfer = dfInfo; - DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId() ); - String entryName = this.serviceProvider.getPropertyHandler().getZipMapper().getFullEntryName((DatasetInfo)dsInfo, (DatafileInfo)dfInfo); + DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId()); + String entryName = + this.serviceProvider.getPropertyHandler() + .getZipMapper() + .getFullEntryName( + (DatasetInfo) dsInfo, + (DatafileInfo) dfInfo + ); InputStream stream = null; try { zos.putNextEntry(new ZipEntry(entryName)); - stream = this.serviceProvider.getMainStorage().get(dfInfo.getLocation(), dfInfo.getCreateId(), dfInfo.getModId()); + stream = + this.serviceProvider.getMainStorage() + .get( + dfInfo.getLocation(), + dfInfo.getCreateId(), + dfInfo.getModId() + ); int length; while ((length = stream.read(bytes)) >= 0) { zos.write(bytes, 0, length); @@ -91,10 +112,18 @@ public void write(OutputStream output) throws IOException { } zos.close(); } else { - DatafileInfo dfInfo = (DatafileInfo) dfInfos.values().iterator().next(); + DatafileInfo dfInfo = (DatafileInfo) dfInfos + .values() + .iterator() + .next(); transfer = dfInfo; - InputStream stream = this.serviceProvider.getMainStorage().get(dfInfo.getDfLocation(), dfInfo.getCreateId(), - dfInfo.getModId()); + InputStream stream = + this.serviceProvider.getMainStorage() + .get( + dfInfo.getDfLocation(), + dfInfo.getCreateId(), + dfInfo.getModId() + ); int length; while ((length = stream.read(bytes)) >= 0) { output.write(bytes, 0, length); @@ -105,27 +134,37 @@ public void write(OutputStream output) throws IOException { if (transferId != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + try ( + JsonGenerator gen = Json + .createGenerator(baos) + .writeStartObject() + ) { gen.write("transferId", transferId); gen.writeEnd(); } - this.serviceProvider.getTransmitter().processMessage("getData", ip, baos.toString(), start); + this.serviceProvider.getTransmitter() + .processMessage("getData", ip, baos.toString(), start); } - } catch (IOException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + try ( + JsonGenerator gen = Json + .createGenerator(baos) + .writeStartObject() + ) { gen.write("transferId", transferId); gen.write("exceptionClass", e.getClass().toString()); gen.write("exceptionMessage", e.getMessage()); gen.writeEnd(); } - this.serviceProvider.getTransmitter().processMessage("getData", ip, baos.toString(), start); - logger.error("Failed to stream " + transfer + " due to " + e.getMessage()); + this.serviceProvider.getTransmitter() + .processMessage("getData", ip, baos.toString(), start); + logger.error( + "Failed to stream " + transfer + " due to " + e.getMessage() + ); throw e; } finally { lock.release(); } } - } diff --git a/src/main/java/org/icatproject/ids/helpers/ValueContainer.java b/src/main/java/org/icatproject/ids/helpers/ValueContainer.java index bb99efba..52577eca 100644 --- a/src/main/java/org/icatproject/ids/helpers/ValueContainer.java +++ b/src/main/java/org/icatproject/ids/helpers/ValueContainer.java @@ -1,12 +1,10 @@ package org.icatproject.ids.helpers; +import jakarta.ws.rs.core.Response; import java.io.InputStream; - import org.icatproject.ids.enums.ValueContainerType; import org.icatproject.ids.exceptions.InternalException; -import jakarta.ws.rs.core.Response; - /** * This class provides a container vor carrying different types of values */ @@ -25,8 +23,15 @@ private ValueContainer(Object value, ValueContainerType type) { * @param typeToCheck the type to be checked if the contained value is of * @throws InternalException if the types don't match an exception is thrown */ - private void checkType(ValueContainerType typeToCheck) throws InternalException { - if(this.type != typeToCheck) throw new InternalException("This ValueContainer ist not of the needed type " + typeToCheck + " its type is " + this.type + "."); + private void checkType(ValueContainerType typeToCheck) + throws InternalException { + if (this.type != typeToCheck) throw new InternalException( + "This ValueContainer ist not of the needed type " + + typeToCheck + + " its type is " + + this.type + + "." + ); } public static ValueContainer getInvalid() { @@ -83,8 +88,8 @@ public ValueContainer(boolean value) { */ public ValueContainer(Response value) { this(value, ValueContainerType.RESPONSE); - } - + } + /** * Creates a ValueContainer of type InputStream * @param value the value contained by the container @@ -163,27 +168,41 @@ public InputStream getInputStream() throws InternalException { @Override public String toString() { - switch(this.type) { - case INVALID: return ""+ValueContainerType.INVALID; - case VOID: return ""+ValueContainerType.VOID; - case INT: return ""+this.value; - case LONG: return ""+this.value; - case BOOL: return ((boolean)this.value ? "true" : "false"); - case STRING: return (String)this.value; - case RESPONSE: return "Response " + ((Response) this.value).toString(); - case INPUTSTREAM: return "An InputStream which will be printed here to prevent it from closing (and maybe it is too long)."; + switch (this.type) { + case INVALID: + return "" + ValueContainerType.INVALID; + case VOID: + return "" + ValueContainerType.VOID; + case INT: + return "" + this.value; + case LONG: + return "" + this.value; + case BOOL: + return ((boolean) this.value ? "true" : "false"); + case STRING: + return (String) this.value; + case RESPONSE: + return "Response " + ((Response) this.value).toString(); + case INPUTSTREAM: + return "An InputStream which will be printed here to prevent it from closing (and maybe it is too long)."; default: - throw new RuntimeException("Doesn't know how to make a String vom ValueContainer of type " + this.type + ". Please implement a new case is ValueContainer.toString()."); + throw new RuntimeException( + "Doesn't know how to make a String vom ValueContainer of type " + + this.type + + ". Please implement a new case is ValueContainer.toString()." + ); } } - public boolean isNull() { return this.isInvalid() && this.value == null; } - - public boolean isInvalid() { return this.type == ValueContainerType.INVALID; } - - public boolean isVoid() { return this.type == ValueContainerType.VOID; } - - + public boolean isNull() { + return this.isInvalid() && this.value == null; + } + public boolean isInvalid() { + return this.type == ValueContainerType.INVALID; + } + public boolean isVoid() { + return this.type == ValueContainerType.VOID; + } } diff --git a/src/main/java/org/icatproject/ids/models/DataInfoBase.java b/src/main/java/org/icatproject/ids/models/DataInfoBase.java index 603a3e7e..b5268d87 100644 --- a/src/main/java/org/icatproject/ids/models/DataInfoBase.java +++ b/src/main/java/org/icatproject/ids/models/DataInfoBase.java @@ -9,7 +9,7 @@ public abstract class DataInfoBase { protected String name; protected String location; - protected DataInfoBase(long id, String name, String location){ + protected DataInfoBase(long id, String name, String location) { this.name = name; this.id = id; this.location = location; @@ -42,5 +42,4 @@ public boolean equals(Object obj) { } return this.id == ((DataInfoBase) obj).getId(); } - } diff --git a/src/main/java/org/icatproject/ids/models/DataSelection.java b/src/main/java/org/icatproject/ids/models/DataSelection.java index 1bc59344..0ad85b9b 100644 --- a/src/main/java/org/icatproject/ids/models/DataSelection.java +++ b/src/main/java/org/icatproject/ids/models/DataSelection.java @@ -5,6 +5,7 @@ import java.util.SortedMap; public class DataSelection { + protected SortedMap dsInfos; protected SortedMap dfInfos; protected Set emptyDatasets; @@ -15,11 +16,17 @@ public class DataSelection { protected Boolean compress; private long length; - - - public DataSelection(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress) { - + public DataSelection( + SortedMap dsInfos, + SortedMap dfInfos, + Set emptyDatasets, + List invids2, + List dsids, + List dfids, + long length, + Boolean zip, + Boolean compress + ) { this.dsInfos = dsInfos; this.dfInfos = dfInfos; this.emptyDatasets = emptyDatasets; @@ -31,12 +38,10 @@ public DataSelection(SortedMap dsInfos, SortedMap getDsInfo() { return dsInfos; } - public SortedMap getDfInfo() { return dfInfos; } @@ -53,11 +58,14 @@ public long getLength() { return this.length; } - public boolean mustZip() { // if(this.zip == null) { - return dfids.size() > 1L || !dsids.isEmpty() || !invids.isEmpty() - || (dfids.isEmpty() && dsids.isEmpty() && invids.isEmpty()); + return ( + dfids.size() > 1L || + !dsids.isEmpty() || + !invids.isEmpty() || + (dfids.isEmpty() && dsids.isEmpty() && invids.isEmpty()) + ); // } // return this.zip; @@ -67,7 +75,6 @@ public boolean isSingleDataset() { return dfids.isEmpty() && dsids.size() == 1 && invids.isEmpty(); } - public Set getEmptyDatasets() { return emptyDatasets; } diff --git a/src/main/java/org/icatproject/ids/models/DatafileInfo.java b/src/main/java/org/icatproject/ids/models/DatafileInfo.java index 789cfa79..efc92846 100644 --- a/src/main/java/org/icatproject/ids/models/DatafileInfo.java +++ b/src/main/java/org/icatproject/ids/models/DatafileInfo.java @@ -6,15 +6,23 @@ * Contains information about a Datafile. Replaces DsInfo in v3 * May should implement DfInfo interface */ -public class DatafileInfo extends DataInfoBase implements Comparable, DfInfo { +public class DatafileInfo + extends DataInfoBase + implements Comparable, DfInfo { protected String createId; - protected String modId; + protected String modId; protected long datasId; - public DatafileInfo(Long id, String name, String location, String createId, String modId, Long datasId) { + public DatafileInfo( + Long id, + String name, + String location, + String createId, + String modId, + Long datasId + ) { super(id, name, location); - this.createId = createId; this.modId = modId; this.datasId = datasId; @@ -50,9 +58,17 @@ public int compareTo(DatafileInfo o) { // implementing DfInfo @Override - public Long getDfId() { return this.getId(); } + public Long getDfId() { + return this.getId(); + } + @Override - public String getDfLocation() { return this.getLocation(); } + public String getDfLocation() { + return this.getLocation(); + } + @Override - public String getDfName() { return this.getName(); } + public String getDfName() { + return this.getName(); + } } diff --git a/src/main/java/org/icatproject/ids/models/DatasetInfo.java b/src/main/java/org/icatproject/ids/models/DatasetInfo.java index be4d9a77..55723028 100644 --- a/src/main/java/org/icatproject/ids/models/DatasetInfo.java +++ b/src/main/java/org/icatproject/ids/models/DatasetInfo.java @@ -18,20 +18,21 @@ public class DatasetInfo extends DataInfoBase implements DsInfo { protected String investigationName; protected String visitId; - public DatasetInfo(Dataset ds) throws InsufficientPrivilegesException { - super(ds.getId(), ds.getName(), ds.getLocation()); - + super(ds.getId(), ds.getName(), ds.getLocation()); Investigation investigation = ds.getInvestigation(); if (investigation == null) { throw new InsufficientPrivilegesException( - "Probably not able to read Investigation for dataset id " + ds.getId()); + "Probably not able to read Investigation for dataset id " + + ds.getId() + ); } Facility facility = investigation.getFacility(); if (facility == null) { throw new InsufficientPrivilegesException( - "Probably not able to read Facility for investigation id " - + investigation.getId()); + "Probably not able to read Facility for investigation id " + + investigation.getId() + ); } this.investigationId = investigation.getId(); @@ -41,10 +42,17 @@ public DatasetInfo(Dataset ds) throws InsufficientPrivilegesException { this.facilityName = facility.getName(); } - - public DatasetInfo(Long id, String name, String location, Long investigationId, String investigationName, String visitId, Long facilityId, String facilityName) { + public DatasetInfo( + Long id, + String name, + String location, + Long investigationId, + String investigationName, + String visitId, + Long facilityId, + String facilityName + ) { super(id, name, location); - this.facilityId = facilityId; this.facilityName = facilityName; this.investigationId = investigationId; @@ -54,8 +62,20 @@ public DatasetInfo(Long id, String name, String location, Long investigationId, @Override public String toString() { - return this.investigationId + "/" + this.id + " (" + this.facilityName + "/" + this.investigationName + "/" + this.visitId + "/" - + this.name + ")"; + return ( + this.investigationId + + "/" + + this.id + + " (" + + this.facilityName + + "/" + + this.investigationName + + "/" + + this.visitId + + "/" + + this.name + + ")" + ); } public Long getFacilityId() { @@ -78,18 +98,30 @@ public String getVisitId() { return visitId; } - // implementing DsInfo @Override - public Long getDsId() { return this.getId(); } - @Override - public String getDsName() { return this.getName(); } + public Long getDsId() { + return this.getId(); + } + @Override - public String getDsLocation() { return this.getLocation(); } + public String getDsName() { + return this.getName(); + } + @Override - public Long getInvId() { return this.getInvestigationId(); } + public String getDsLocation() { + return this.getLocation(); + } + @Override - public String getInvName() { return this.getInvestigationName(); } + public Long getInvId() { + return this.getInvestigationId(); + } + @Override + public String getInvName() { + return this.getInvestigationName(); + } } diff --git a/src/main/java/org/icatproject/ids/models/Prepared.java b/src/main/java/org/icatproject/ids/models/Prepared.java index 5e04d830..48524c63 100644 --- a/src/main/java/org/icatproject/ids/models/Prepared.java +++ b/src/main/java/org/icatproject/ids/models/Prepared.java @@ -1,5 +1,11 @@ package org.icatproject.ids.models; +import jakarta.json.Json; +import jakarta.json.JsonNumber; +import jakarta.json.JsonObject; +import jakarta.json.JsonReader; +import jakarta.json.JsonValue; +import jakarta.json.stream.JsonGenerator; import java.io.InputStream; import java.io.OutputStream; import java.util.HashSet; @@ -7,25 +13,18 @@ import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; - import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import jakarta.json.Json; -import jakarta.json.JsonNumber; -import jakarta.json.JsonObject; -import jakarta.json.JsonReader; -import jakarta.json.JsonValue; -import jakarta.json.stream.JsonGenerator; - /* This is a POJO with only package access so don't make data private */ public class Prepared { - protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); + protected static final Logger logger = LoggerFactory.getLogger( + RequestHandlerBase.class + ); - public SortedMap dsInfos; public SortedMap dfInfos; public Set emptyDatasets; @@ -33,7 +32,12 @@ public class Prepared { public boolean zip; public boolean compress; - public Prepared(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength) { + public Prepared( + SortedMap dsInfos, + SortedMap dfInfos, + Set emptyDatasets, + long fileLength + ) { this.dsInfos = dsInfos; this.dfInfos = dfInfos; this.emptyDatasets = emptyDatasets; @@ -42,15 +46,28 @@ public Prepared(SortedMap dsInfos, SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength, Boolean zip, Boolean compress) { + public Prepared( + SortedMap dsInfos, + SortedMap dfInfos, + Set emptyDatasets, + long fileLength, + Boolean zip, + Boolean compress + ) { this(dsInfos, dfInfos, emptyDatasets, fileLength); this.zip = zip; this.compress = compress; } - - public static void pack(OutputStream stream, boolean zip, boolean compress, Map dsInfos, - Map dfInfos, Set emptyDatasets, long fileLength) { + public static void pack( + OutputStream stream, + boolean zip, + boolean compress, + Map dsInfos, + Map dfInfos, + Set emptyDatasets, + long fileLength + ) { JsonGenerator gen = Json.createGenerator(stream); gen.writeStartObject(); gen.write("zip", zip); @@ -59,13 +76,17 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, Map< gen.writeStartArray("dsInfo"); for (DataInfoBase dataInfo : dsInfos.values()) { - var dsInfo = (DatasetInfo)dataInfo; + var dsInfo = (DatasetInfo) dataInfo; logger.debug("dsInfo " + dsInfo); - gen.writeStartObject().write("dsId", dsInfo.getId()) - - .write("dsName", dsInfo.getDsName()).write("facilityId", dsInfo.getFacilityId()) - .write("facilityName", dsInfo.getFacilityName()).write("invId", dsInfo.getInvId()) - .write("invName", dsInfo.getInvName()).write("visitId", dsInfo.getVisitId()); + gen + .writeStartObject() + .write("dsId", dsInfo.getId()) + .write("dsName", dsInfo.getDsName()) + .write("facilityId", dsInfo.getFacilityId()) + .write("facilityName", dsInfo.getFacilityName()) + .write("invId", dsInfo.getInvId()) + .write("invName", dsInfo.getInvName()) + .write("visitId", dsInfo.getVisitId()); if (dsInfo.getDsLocation() != null) { gen.write("dsLocation", dsInfo.getDsLocation()); } else { @@ -77,18 +98,21 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, Map< gen.writeStartArray("dfInfo"); for (DataInfoBase dataInfo : dfInfos.values()) { - var dfInfo = (DatafileInfo)dataInfo; + var dfInfo = (DatafileInfo) dataInfo; DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId()); - gen.writeStartObject().write("dsId", dsInfo.getId()).write("dfId", dfInfo.getId()) - .write("dfName", dfInfo.getDfName()).write("createId", dfInfo.getCreateId()) - .write("modId", dfInfo.getModId()); + gen + .writeStartObject() + .write("dsId", dsInfo.getId()) + .write("dfId", dfInfo.getId()) + .write("dfName", dfInfo.getDfName()) + .write("createId", dfInfo.getCreateId()) + .write("modId", dfInfo.getModId()); if (dfInfo.getDfLocation() != null) { gen.write("dfLocation", dfInfo.getDfLocation()); } else { gen.writeNull("dfLocation"); } gen.writeEnd(); - } gen.writeEnd(); @@ -99,47 +123,73 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, Map< gen.writeEnd(); gen.writeEnd().close(); - } - public static Prepared unpack(InputStream stream) throws InternalException { - JsonObject pd; try (JsonReader jsonReader = Json.createReader(stream)) { pd = jsonReader.readObject(); } var zip = pd.getBoolean("zip"); var compress = pd.getBoolean("compress"); - var fileLength = pd.containsKey("fileLength") ? pd.getInt("fileLength") : 0; + var fileLength = pd.containsKey("fileLength") + ? pd.getInt("fileLength") + : 0; SortedMap dsInfos = new TreeMap<>(); SortedMap dfInfos = new TreeMap<>(); Set emptyDatasets = new HashSet<>(); for (JsonValue itemV : pd.getJsonArray("dfInfo")) { JsonObject item = (JsonObject) itemV; - String dfLocation = item.isNull("dfLocation") ? null : item.getString("dfLocation"); + String dfLocation = item.isNull("dfLocation") + ? null + : item.getString("dfLocation"); long dfid = item.getJsonNumber("dfId").longValueExact(); - dfInfos.put(dfid, new DatafileInfo(dfid, item.getString("dfName"), - dfLocation, item.getString("createId"), item.getString("modId"), - item.getJsonNumber("dsId").longValueExact())); - + dfInfos.put( + dfid, + new DatafileInfo( + dfid, + item.getString("dfName"), + dfLocation, + item.getString("createId"), + item.getString("modId"), + item.getJsonNumber("dsId").longValueExact() + ) + ); } for (JsonValue itemV : pd.getJsonArray("dsInfo")) { JsonObject item = (JsonObject) itemV; long dsId = item.getJsonNumber("dsId").longValueExact(); - String dsLocation = item.isNull("dsLocation") ? null : item.getString("dsLocation"); - dsInfos.put(dsId, new DatasetInfo(dsId, item.getString("dsName"), dsLocation, - item.getJsonNumber("invId").longValueExact(), item.getString("invName"), item.getString("visitId"), - item.getJsonNumber("facilityId").longValueExact(), item.getString("facilityName"))); + String dsLocation = item.isNull("dsLocation") + ? null + : item.getString("dsLocation"); + dsInfos.put( + dsId, + new DatasetInfo( + dsId, + item.getString("dsName"), + dsLocation, + item.getJsonNumber("invId").longValueExact(), + item.getString("invName"), + item.getString("visitId"), + item.getJsonNumber("facilityId").longValueExact(), + item.getString("facilityName") + ) + ); } for (JsonValue itemV : pd.getJsonArray("emptyDs")) { emptyDatasets.add(((JsonNumber) itemV).longValueExact()); } - return new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength, zip, compress); + return new Prepared( + dsInfos, + dfInfos, + emptyDatasets, + fileLength, + zip, + compress + ); } - } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java index 032e8437..94fddc68 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java @@ -1,6 +1,5 @@ package org.icatproject.ids.requestHandlers; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; @@ -12,13 +11,27 @@ public class ArchiveHandler extends DataRequestHandler { - public ArchiveHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.ARCHIVE, ip, sessionId, investigationIds, datasetIds, datafileIds); + public ArchiveHandler( + String ip, + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds + ) { + super( + RequestType.ARCHIVE, + ip, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws NotImplementedException, InternalException { - + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService + ) throws NotImplementedException, InternalException { dataSelectionService.scheduleTasks(DeferredOp.ARCHIVE); return ValueContainer.getVoid(); } @@ -27,5 +40,4 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic public CallType getCallType() { return CallType.MIGRATE; } - } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java index a4cb44a9..704d1652 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java @@ -3,7 +3,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; - import org.icatproject.Datafile; import org.icatproject.EntityBaseBean; import org.icatproject.IcatExceptionType; @@ -21,48 +20,76 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataRequestHandler; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; - public class DeleteHandler extends DataRequestHandler { - public DeleteHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.DELETE, ip, sessionId, investigationIds, datasetIds, datafileIds); + public DeleteHandler( + String ip, + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds + ) { + super( + RequestType.DELETE, + ip, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService + ) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { if (readOnly) { - throw new NotImplementedException("This operation has been configured to be unavailable"); + throw new NotImplementedException( + "This operation has been configured to be unavailable" + ); } var serviceProvider = ServiceProvider.getInstance(); // Do it - try (Lock lock = serviceProvider.getLockManager().lock(dataSelectionService.getDsInfo().values(), LockType.EXCLUSIVE)) { + try ( + Lock lock = serviceProvider + .getLockManager() + .lock( + dataSelectionService.getDsInfo().values(), + LockType.EXCLUSIVE + ) + ) { if (storageUnit == StorageUnit.DATASET) { dataSelectionService.checkOnline(); } /* Now delete from ICAT */ List dfs = new ArrayList<>(); - for (DataInfoBase dfInfo : dataSelectionService.getDfInfo().values()) { + for (DataInfoBase dfInfo : dataSelectionService + .getDfInfo() + .values()) { Datafile df = new Datafile(); df.setId(dfInfo.getId()); dfs.add(df); } try { - serviceProvider.getIcat().deleteMany(this.dataController.getOperationId(), dfs); + serviceProvider + .getIcat() + .deleteMany(this.dataController.getOperationId(), dfs); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if ( + type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || + type == IcatExceptionType.SESSION + ) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -72,7 +99,6 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic } dataSelectionService.delete(); - } catch (AlreadyLockedException e) { logger.debug("Could not acquire lock, delete failed"); throw new DataNotOnlineException("Data is busy"); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java index 7c22c38f..d62b7077 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java @@ -1,7 +1,8 @@ package org.icatproject.ids.requestHandlers; +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; import java.io.ByteArrayOutputStream; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -16,30 +17,45 @@ import org.icatproject.ids.requestHandlers.base.PreparedDataController; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; - public class GetDataFileIdsHandler extends DataRequestHandler { - public GetDataFileIdsHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETDATAFILEIDS, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public GetDataFileIdsHandler( + String ip, + String preparedId, + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds + ) { + super( + RequestType.GETDATAFILEIDS, + ip, + preparedId, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService + ) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { - - if(this.dataController instanceof PreparedDataController) { + try ( + JsonGenerator gen = Json.createGenerator(baos).writeStartObject() + ) { + if (this.dataController instanceof PreparedDataController) { gen.write("zip", dataSelectionService.getZip()); gen.write("compress", dataSelectionService.getCompress()); } gen.writeStartArray("ids"); - for (DataInfoBase dfInfo : dataSelectionService.getDfInfo().values()) { + for (DataInfoBase dfInfo : dataSelectionService + .getDfInfo() + .values()) { gen.write(dfInfo.getId()); } gen.writeEnd().writeEnd().close(); @@ -53,5 +69,4 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic public CallType getCallType() { return CallType.INFO; } - } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java index 8cdb64ce..50f03f93 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java @@ -1,5 +1,9 @@ package org.icatproject.ids.requestHandlers; +import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH; + +import jakarta.json.stream.JsonGenerator; +import jakarta.ws.rs.core.Response; import java.io.IOException; import java.net.HttpURLConnection; import java.text.SimpleDateFormat; @@ -9,7 +13,6 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.regex.Matcher; import java.util.regex.Pattern; - import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; @@ -25,15 +28,11 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataRequestHandler; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; -import jakarta.json.stream.JsonGenerator; -import jakarta.ws.rs.core.Response; -import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH; - public class GetDataHandler extends DataRequestHandler { private Pattern rangeRe; @@ -45,40 +44,71 @@ public class GetDataHandler extends DataRequestHandler { String range; Long transferId; - public GetDataHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds, Boolean compress, Boolean zip, String outname, String range) { - super(RequestType.GETDATA, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds ); - - this.initializeAdditionallParameters(sessionId, compress, zip, outname, range); + public GetDataHandler( + String ip, + String preparedId, + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds, + Boolean compress, + Boolean zip, + String outname, + String range + ) { + super( + RequestType.GETDATA, + ip, + preparedId, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); + this.initializeAdditionallParameters( + sessionId, + compress, + zip, + outname, + range + ); } - private void initializeAdditionallParameters(String sessionId, Boolean compress, Boolean zip, String outname, String range) { - + private void initializeAdditionallParameters( + String sessionId, + Boolean compress, + Boolean zip, + String outname, + String range + ) { this.outname = outname; this.range = range; this.transferId = null; this.rangeRe = Pattern.compile("bytes=(\\d+)-"); - if(sessionId != null) { + if (sessionId != null) { this.compress = compress; this.zip = zip; } } - @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws InternalException, NotImplementedException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException { - - if(this.compress == null || this.zip == null) { + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService + ) + throws InternalException, NotImplementedException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException { + if (this.compress == null || this.zip == null) { this.zip = dataSelectionService.getZip(); this.compress = dataSelectionService.getCompress(); } long offset = 0; - if ( range != null) { - + if (range != null) { Matcher m = rangeRe.matcher(range); if (!m.matches()) { - throw new BadRequestException("The range must match " + rangeRe.pattern()); + throw new BadRequestException( + "The range must match " + rangeRe.pattern() + ); } offset = Long.parseLong(m.group(1)); logger.debug("Range " + range + " -> offset " + offset); @@ -87,20 +117,30 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic return new ValueContainer(this.getData(dataSelectionService, offset)); } - - private Response getData(DataSelectionService dataSelectionService, final long offset) throws BadRequestException, - NotFoundException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - + private Response getData( + DataSelectionService dataSelectionService, + final long offset + ) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { long start = System.currentTimeMillis(); - - var length = this.zip ? OptionalLong.empty() : dataSelectionService.getFileLength(); - final boolean finalZip = this.dataController.mustZip(zip, dataSelectionService); + var length = this.zip + ? OptionalLong.empty() + : dataSelectionService.getFileLength(); + + final boolean finalZip = + this.dataController.mustZip(zip, dataSelectionService); Lock lock = null; try { var serviceProvider = ServiceProvider.getInstance(); - lock = serviceProvider.getLockManager().lock(dataSelectionService.getDsInfo().values(), LockType.SHARED); + lock = + serviceProvider + .getLockManager() + .lock( + dataSelectionService.getDsInfo().values(), + LockType.SHARED + ); if (twoLevel) { dataSelectionService.checkOnline(); @@ -111,13 +151,25 @@ private Response getData(DataSelectionService dataSelectionService, final long o String name; if (outname == null) { if (finalZip) { - name = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()) + ".zip"; + name = + new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss") + .format(new Date()) + + ".zip"; } else { - name = dataSelectionService.getDfInfo().values().iterator().next().getName(); + name = + dataSelectionService + .getDfInfo() + .values() + .iterator() + .next() + .getName(); } } else { if (finalZip) { - String ext = outname.substring(outname.lastIndexOf(".") + 1, outname.length()); + String ext = outname.substring( + outname.lastIndexOf(".") + 1, + outname.length() + ); if ("zip".equals(ext)) { name = outname; } else { @@ -128,19 +180,47 @@ private Response getData(DataSelectionService dataSelectionService, final long o } } - if (ServiceProvider.getInstance().getPropertyHandler().getLogSet().contains(this.getCallType())) { + if ( + ServiceProvider + .getInstance() + .getPropertyHandler() + .getLogSet() + .contains(this.getCallType()) + ) { transferId = transferIdCounter.getAndIncrement(); } - var response = Response.status(offset == 0 ? HttpURLConnection.HTTP_OK : HttpURLConnection.HTTP_PARTIAL) - .entity(new SO(dataSelectionService.getDsInfo(), dataSelectionService.getDfInfo(), offset, finalZip, compress, lock, transferId, ip, start, serviceProvider)) - .header("Content-Disposition", "attachment; filename=\"" + name + "\"").header("Accept-Ranges", "bytes"); - length.stream() - .map(l -> Math.max(0L, l - offset)) - .forEach(l -> response.header(CONTENT_LENGTH, l)); - - return response.build(); + var response = Response + .status( + offset == 0 + ? HttpURLConnection.HTTP_OK + : HttpURLConnection.HTTP_PARTIAL + ) + .entity( + new SO( + dataSelectionService.getDsInfo(), + dataSelectionService.getDfInfo(), + offset, + finalZip, + compress, + lock, + transferId, + ip, + start, + serviceProvider + ) + ) + .header( + "Content-Disposition", + "attachment; filename=\"" + name + "\"" + ) + .header("Accept-Ranges", "bytes"); + length + .stream() + .map(l -> Math.max(0L, l - offset)) + .forEach(l -> response.header(CONTENT_LENGTH, l)); + return response.build(); } catch (AlreadyLockedException e) { logger.debug("Could not acquire lock, getData failed"); throw new DataNotOnlineException("Data is busy"); @@ -156,41 +236,69 @@ private Response getData(DataSelectionService dataSelectionService, final long o } } - private void checkDatafilesPresent(Collection dfInfos) - throws NotFoundException, InternalException { - + throws NotFoundException, InternalException { var serviceProvider = ServiceProvider.getInstance(); /* Check that datafiles have not been deleted before locking */ int n = 0; - StringBuffer sb = new StringBuffer("SELECT COUNT(df) from Datafile df WHERE (df.id in ("); + StringBuffer sb = new StringBuffer( + "SELECT COUNT(df) from Datafile df WHERE (df.id in (" + ); for (DataInfoBase dfInfo : dfInfos) { if (n != 0) { sb.append(','); } sb.append(dfInfo.getId()); - if (++n == serviceProvider.getPropertyHandler().getMaxIdsInQuery()) { + if ( + ++n == serviceProvider.getPropertyHandler().getMaxIdsInQuery() + ) { try { - if (((Long) serviceProvider.getIcatReader().search(sb.append("))").toString()).get(0)).intValue() != n) { - throw new NotFoundException("One of the data files requested has been deleted"); + if ( + ( + (Long) serviceProvider + .getIcatReader() + .search(sb.append("))").toString()) + .get(0) + ).intValue() != + n + ) { + throw new NotFoundException( + "One of the data files requested has been deleted" + ); } n = 0; - sb = new StringBuffer("SELECT COUNT(df) from Datafile df WHERE (df.id in ("); + sb = + new StringBuffer( + "SELECT COUNT(df) from Datafile df WHERE (df.id in (" + ); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage() + ); } } } if (n != 0) { try { - if (((Long) serviceProvider.getIcatReader().search(sb.append("))").toString()).get(0)).intValue() != n) { - throw new NotFoundException("One of the datafiles requested has been deleted"); + if ( + ( + (Long) serviceProvider + .getIcatReader() + .search(sb.append("))").toString()) + .get(0) + ).intValue() != + n + ) { + throw new NotFoundException( + "One of the datafiles requested has been deleted" + ); } } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage() + ); } } - } @Override @@ -200,8 +308,7 @@ public CallType getCallType() { @Override public void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - if(transferId == null) transferId = -1L; + if (transferId == null) transferId = -1L; gen.write("transferId", transferId); } - } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java index 94a78919..e711f0c3 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java @@ -12,7 +12,6 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; import org.icatproject.ids.services.ServiceProvider; - public class GetIcatUrlHandler extends RequestHandlerBase { public GetIcatUrlHandler(String ip) { @@ -21,10 +20,10 @@ public GetIcatUrlHandler(String ip) { @Override public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - - var propertyHandler = ServiceProvider.getInstance().getPropertyHandler(); + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + var propertyHandler = ServiceProvider + .getInstance() + .getPropertyHandler(); return new ValueContainer(propertyHandler.getIcatUrl()); } @@ -32,5 +31,4 @@ public ValueContainer handleRequest() public CallType getCallType() { return CallType.INFO; } - } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java index 713c3603..28ee0d3d 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java @@ -1,7 +1,7 @@ package org.icatproject.ids.requestHandlers; +import jakarta.json.stream.JsonGenerator; import java.util.Set; - import org.icatproject.IcatExceptionType; import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.CallType; @@ -16,8 +16,6 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; import org.icatproject.ids.services.ServiceProvider; -import jakarta.json.stream.JsonGenerator; - public class GetServiceStatusHandler extends RequestHandlerBase { private Set rootUserNames; @@ -26,26 +24,31 @@ public class GetServiceStatusHandler extends RequestHandlerBase { public GetServiceStatusHandler(String ip, String sessionId) { super(RequestType.GETSERVICESTATUS, ip); - this.sessionId = sessionId; - rootUserNames = ServiceProvider.getInstance().getPropertyHandler().getRootUserNames(); + rootUserNames = + ServiceProvider + .getInstance() + .getPropertyHandler() + .getRootUserNames(); this.serviceProvider = ServiceProvider.getInstance(); } @Override public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { try { String uname = serviceProvider.getIcat().getUserName(sessionId); if (!rootUserNames.contains(uname)) { - throw new InsufficientPrivilegesException(uname + " is not included in the ids rootUserNames set."); + throw new InsufficientPrivilegesException( + uname + " is not included in the ids rootUserNames set." + ); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); if (type == IcatExceptionType.SESSION) { - throw new InsufficientPrivilegesException(e.getClass() + " " + e.getMessage()); + throw new InsufficientPrivilegesException( + e.getClass() + " " + e.getMessage() + ); } throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -54,7 +57,8 @@ public ValueContainer handleRequest() } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { gen.write("userName", serviceProvider.getIcat().getUserName(sessionId)); } @@ -62,5 +66,4 @@ public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatExcepti public CallType getCallType() { return CallType.INFO; } - } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java index 9da9787c..1695c6ae 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java @@ -1,7 +1,6 @@ package org.icatproject.ids.requestHandlers; import java.util.Set; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.enums.Status; @@ -19,21 +18,40 @@ public class GetStatusHandler extends DataRequestHandler { - public GetStatusHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETSTATUS, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public GetStatusHandler( + String ip, + String preparedId, + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds + ) { + super( + RequestType.GETSTATUS, + ip, + preparedId, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService + ) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { Status status = Status.ONLINE; var serviceProvider = ServiceProvider.getInstance(); Set restoring = serviceProvider.getFsm().getRestoring(); - Set maybeOffline = serviceProvider.getFsm().getMaybeOffline(); - for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { + Set maybeOffline = serviceProvider + .getFsm() + .getMaybeOffline(); + for (DataInfoBase dataInfo : dataSelectionService + .getPrimaryDataInfos() + .values()) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (restoring.contains(dataInfo)) { status = Status.RESTORING; diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java index 6f2deba7..1306c1ec 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java @@ -5,7 +5,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.locks.ReentrantLock; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -25,26 +24,32 @@ public IsPreparedHandler(String ip, String preparedId) { } class PreparedStatus { + public ReentrantLock lock = new ReentrantLock(); public Long fromElement; public Future future; - } private Map preparedStatusMap = new ConcurrentHashMap<>(); @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService + ) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { // Do it boolean prepared = true; - PreparedStatus status = preparedStatusMap.computeIfAbsent(this.dataController.getOperationId(), k -> new PreparedStatus()); + PreparedStatus status = preparedStatusMap.computeIfAbsent( + this.dataController.getOperationId(), + k -> new PreparedStatus() + ); if (!status.lock.tryLock()) { - logger.debug("Lock held for evaluation of isPrepared for preparedId {}", this.dataController.getOperationId()); + logger.debug( + "Lock held for evaluation of isPrepared for preparedId {}", + this.dataController.getOperationId() + ); return new ValueContainer(false); } try { @@ -54,22 +59,29 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic try { future.get(); } catch (ExecutionException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } catch (InterruptedException e) { // Ignore } finally { status.future = null; } } else { - logger.debug("Background process still running for preparedId {}", this.dataController.getOperationId()); + logger.debug( + "Background process still running for preparedId {}", + this.dataController.getOperationId() + ); return new ValueContainer(false); } } - prepared = dataSelectionService.isPrepared(this.dataController.getOperationId()); + prepared = + dataSelectionService.isPrepared( + this.dataController.getOperationId() + ); return new ValueContainer(prepared); - } finally { status.lock.unlock(); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java index a7acf20e..d12466a3 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java @@ -12,17 +12,14 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; public class IsReadOnlyHandler extends RequestHandlerBase { - + public IsReadOnlyHandler(String ip) { super(RequestType.ISREADONLY, ip); - } @Override public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { return new ValueContainer(this.readOnly); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java index f9cc3be1..4348674a 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java @@ -19,9 +19,7 @@ public IsTwoLevelHandler(String ip) { @Override public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { return new ValueContainer(twoLevel); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java index 625c4ac2..8e00f75e 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java @@ -1,11 +1,11 @@ package org.icatproject.ids.requestHandlers; +import jakarta.json.stream.JsonGenerator; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.util.UUID; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestIdNames; import org.icatproject.ids.enums.RequestType; @@ -20,28 +20,40 @@ import org.icatproject.ids.requestHandlers.base.DataRequestHandler; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; -import jakarta.json.stream.JsonGenerator; - public class PrepareDataHandler extends DataRequestHandler { Boolean compress; Boolean zip; String preparedId; - public PrepareDataHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds, Boolean compress, Boolean zip) { - super(RequestType.PREPAREDATA,ip, sessionId, investigationIds, datasetIds, datafileIds); - + public PrepareDataHandler( + String ip, + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds, + Boolean compress, + Boolean zip + ) { + super( + RequestType.PREPAREDATA, + ip, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); this.zip = zip; this.compress = compress; } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService + ) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { preparedId = UUID.randomUUID().toString(); - + dataSelectionService.restoreDataInfos(); if (dataSelectionService.mustZip()) { @@ -49,14 +61,21 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic } logger.debug("Writing to " + preparedDir.resolve(preparedId)); - try (OutputStream stream = new BufferedOutputStream(Files.newOutputStream(preparedDir.resolve(preparedId)))) { - Prepared.pack( stream, - zip, - compress, - dataSelectionService.getDsInfo(), - dataSelectionService.getDfInfo(), - dataSelectionService.getEmptyDatasets(), - dataSelectionService.getFileLength().isEmpty() ? 0 : dataSelectionService.getFileLength().getAsLong() + try ( + OutputStream stream = new BufferedOutputStream( + Files.newOutputStream(preparedDir.resolve(preparedId)) + ) + ) { + Prepared.pack( + stream, + zip, + compress, + dataSelectionService.getDsInfo(), + dataSelectionService.getDfInfo(), + dataSelectionService.getEmptyDatasets(), + dataSelectionService.getFileLength().isEmpty() + ? 0 + : dataSelectionService.getFileLength().getAsLong() ); } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -68,13 +87,13 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic } @Override - public String addCustomParametersToLogString() { + public String addCustomParametersToLogString() { return "zip='" + this.zip + "'' compress='" + compress + "'"; } @Override public void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - if(preparedId == null) preparedId = ""; + if (preparedId == null) preparedId = ""; gen.write(RequestIdNames.preparedId, preparedId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java index 3e9bfbd9..c147a306 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java @@ -1,5 +1,8 @@ package org.icatproject.ids.requestHandlers; +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; +import jakarta.ws.rs.core.Response; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -12,7 +15,6 @@ import java.util.TreeMap; import java.util.zip.CRC32; import javax.xml.datatype.DatatypeFactory; - import org.icatproject.Datafile; import org.icatproject.DatafileFormat; import org.icatproject.Dataset; @@ -33,23 +35,19 @@ import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.helpers.CheckedWithSizeInputStream; import org.icatproject.ids.helpers.ValueContainer; -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataControllerBase; import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; import org.icatproject.ids.services.dataSelectionService.DataSelectionServiceFactory; import org.icatproject.utils.IcatSecurity; -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; -import jakarta.ws.rs.core.Response; - public class PutHandler extends RequestHandlerBase { String sessionId; @@ -68,7 +66,8 @@ public class PutHandler extends RequestHandlerBase { private ServiceProvider serviceProvider; private DatatypeFactory datatypeFactory; private static String paddedPrefix; - private static final String prefix = ""; static { @@ -79,11 +78,21 @@ public class PutHandler extends RequestHandlerBase { paddedPrefix += "*/window.name='"; } - public PutHandler( String ip, String sessionId, InputStream body, String name, String datafileFormatIdString, - String datasetIdString, String description, String doi, String datafileCreateTimeString, - String datafileModTimeString, boolean wrap, boolean padding) { + public PutHandler( + String ip, + String sessionId, + InputStream body, + String name, + String datafileFormatIdString, + String datasetIdString, + String description, + String doi, + String datafileCreateTimeString, + String datafileModTimeString, + boolean wrap, + boolean padding + ) { super(RequestType.PUT, ip); - this.sessionId = sessionId; this.body = body; this.name = name; @@ -100,57 +109,70 @@ public PutHandler( String ip, String sessionId, InputStream body, String name, this.serviceProvider = ServiceProvider.getInstance(); try { - this.datatypeFactory = DatatypeFactory.newInstance(); - } catch (Throwable e) { logger.error("Won't start ", e); - throw new RuntimeException("PutHandler reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException( + "PutHandler reports " + e.getClass() + " " + e.getMessage() + ); } } @Override public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { try { - if (readOnly) { - throw new NotImplementedException("This operation has been configured to be unavailable"); + throw new NotImplementedException( + "This operation has been configured to be unavailable" + ); } - DataControllerBase.validateUUID(RequestIdNames.sessionId, sessionId); + DataControllerBase.validateUUID( + RequestIdNames.sessionId, + sessionId + ); if (name == null) { throw new BadRequestException("The name parameter must be set"); } if (datafileFormatIdString == null) { - throw new BadRequestException("The datafileFormatId parameter must be set"); + throw new BadRequestException( + "The datafileFormatId parameter must be set" + ); } long datafileFormatId; try { datafileFormatId = Long.parseLong(datafileFormatIdString); } catch (NumberFormatException e) { - throw new BadRequestException("The datafileFormatId parameter must be numeric"); + throw new BadRequestException( + "The datafileFormatId parameter must be numeric" + ); } if (datasetIdString == null) { - throw new BadRequestException("The datasetId parameter must be set"); + throw new BadRequestException( + "The datasetId parameter must be set" + ); } long datasetId; try { datasetId = Long.parseLong(datasetIdString); } catch (NumberFormatException e) { - throw new BadRequestException("The datasetId parameter must be numeric"); + throw new BadRequestException( + "The datasetId parameter must be numeric" + ); } Long datafileCreateTime = null; if (datafileCreateTimeString != null) { try { - datafileCreateTime = Long.parseLong(datafileCreateTimeString); + datafileCreateTime = + Long.parseLong(datafileCreateTimeString); } catch (NumberFormatException e) { - throw new BadRequestException("The datafileCreateTime parameter must be numeric"); + throw new BadRequestException( + "The datafileCreateTime parameter must be numeric" + ); } } @@ -159,17 +181,29 @@ public ValueContainer handleRequest() try { datafileModTime = Long.parseLong(datafileModTimeString); } catch (NumberFormatException e) { - throw new BadRequestException("The datafileModTime parameter must be numeric"); + throw new BadRequestException( + "The datafileModTime parameter must be numeric" + ); } } // Do it Dataset ds; try { - ds = (Dataset) serviceProvider.getIcat().get(sessionId, "Dataset INCLUDE Investigation, Facility", datasetId); + ds = + (Dataset) serviceProvider + .getIcat() + .get( + sessionId, + "Dataset INCLUDE Investigation, Facility", + datasetId + ); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if ( + type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || + type == IcatExceptionType.SESSION + ) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -179,59 +213,117 @@ public ValueContainer handleRequest() } DatasetInfo dsInfo = new DatasetInfo(ds); - try (Lock lock = serviceProvider.getLockManager().lock(dsInfo, LockType.SHARED)) { + try ( + Lock lock = serviceProvider + .getLockManager() + .lock(dsInfo, LockType.SHARED) + ) { if (storageUnit == StorageUnit.DATASET) { var dfInfos = new TreeMap(); Set emptyDatasets = new HashSet<>(); try { - List counts = serviceProvider.getIcat().search(sessionId, - "COUNT(Datafile) <-> Dataset [id=" + dsInfo.getId() + "]"); + List counts = serviceProvider + .getIcat() + .search( + sessionId, + "COUNT(Datafile) <-> Dataset [id=" + + dsInfo.getId() + + "]" + ); if ((Long) counts.get(0) == 0) { emptyDatasets.add(dsInfo.getId()); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { - throw new InsufficientPrivilegesException(e.getMessage()); + if ( + type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || + type == IcatExceptionType.SESSION + ) { + throw new InsufficientPrivilegesException( + e.getMessage() + ); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } - throw new InternalException(type + " " + e.getMessage()); + throw new InternalException( + type + " " + e.getMessage() + ); } var dsInfos = new TreeMap(); dsInfos.put(dsInfo.getId(), dsInfo); - DataSelectionService dataSelectionService = DataSelectionServiceFactory.getService(dsInfos, dfInfos, emptyDatasets, 0, this.getRequestType()); + DataSelectionService dataSelectionService = DataSelectionServiceFactory.getService( + dsInfos, + dfInfos, + emptyDatasets, + 0, + this.getRequestType() + ); dataSelectionService.checkOnline(); } CRC32 crc = new CRC32(); - CheckedWithSizeInputStream is = new CheckedWithSizeInputStream(body, crc); + CheckedWithSizeInputStream is = new CheckedWithSizeInputStream( + body, + crc + ); String location; try { - location = serviceProvider.getMainStorage().put(dsInfo, name, is); + location = + serviceProvider.getMainStorage().put(dsInfo, name, is); } catch (IllegalArgumentException e) { - throw new BadRequestException("Illegal filename or dataset: " + e.getMessage()); + throw new BadRequestException( + "Illegal filename or dataset: " + e.getMessage() + ); } is.close(); long checksum = crc.getValue(); long size = is.getSize(); - + try { - dfId = registerDatafile(sessionId, name, datafileFormatId, location, checksum, size, ds, - description, doi, datafileCreateTime, datafileModTime); - } catch (InsufficientPrivilegesException | NotFoundException | InternalException - | BadRequestException e) { - logger.debug("Problem with registration " + e.getClass() + " " + e.getMessage() - + " datafile will now be deleted"); + dfId = + registerDatafile( + sessionId, + name, + datafileFormatId, + location, + checksum, + size, + ds, + description, + doi, + datafileCreateTime, + datafileModTime + ); + } catch ( + InsufficientPrivilegesException + | NotFoundException + | InternalException + | BadRequestException e + ) { + logger.debug( + "Problem with registration " + + e.getClass() + + " " + + e.getMessage() + + " datafile will now be deleted" + ); String userId = null; try { - userId = serviceProvider.getIcat().getUserName(sessionId); + userId = + serviceProvider.getIcat().getUserName(sessionId); } catch (IcatException_Exception e1) { - logger.error("Unable to get user name for session " + sessionId + " so mainStorage.delete of " - + location + " may fail"); + logger.error( + "Unable to get user name for session " + + sessionId + + " so mainStorage.delete of " + + location + + " may fail" + ); } - serviceProvider.getMainStorage().delete(location, userId, userId); + serviceProvider + .getMainStorage() + .delete(location, userId, userId); throw e; } @@ -240,62 +332,130 @@ public ValueContainer handleRequest() } else if (storageUnit == StorageUnit.DATAFILE) { Datafile df; try { - df = (Datafile) serviceProvider.getIcatReader().get("Datafile", dfId); + df = + (Datafile) serviceProvider + .getIcatReader() + .get("Datafile", dfId); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage() + ); } - serviceProvider.getFsm().queue(new DatafileInfo(dfId, name, location, df.getCreateId(), df.getModId(), dsInfo.getId()), - DeferredOp.WRITE); + serviceProvider + .getFsm() + .queue( + new DatafileInfo( + dfId, + name, + location, + df.getCreateId(), + df.getModId(), + dsInfo.getId() + ), + DeferredOp.WRITE + ); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Json.createGenerator(baos).writeStartObject().write("id", dfId).write("checksum", checksum) - .write("location", location.replace("\\", "\\\\").replace("'", "\\'")).write("size", size) - .writeEnd().close(); - String resp = wrap ? prefix + baos.toString() + suffix : baos.toString(); - - return new ValueContainer(Response.status(HttpURLConnection.HTTP_CREATED).entity(resp).build()); - + Json + .createGenerator(baos) + .writeStartObject() + .write("id", dfId) + .write("checksum", checksum) + .write( + "location", + location.replace("\\", "\\\\").replace("'", "\\'") + ) + .write("size", size) + .writeEnd() + .close(); + String resp = wrap + ? prefix + baos.toString() + suffix + : baos.toString(); + + return new ValueContainer( + Response + .status(HttpURLConnection.HTTP_CREATED) + .entity(resp) + .build() + ); } catch (AlreadyLockedException e) { logger.debug("Could not acquire lock, put failed"); throw new DataNotOnlineException("Data is busy"); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " putting " + name + " to Dataset with id " - + datasetIdString); - throw new InternalException(e.getClass() + " " + e.getMessage()); + logger.error( + "I/O exception " + + e.getMessage() + + " putting " + + name + + " to Dataset with id " + + datasetIdString + ); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IdsException e) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen.writeStartObject().write("code", e.getClass().getSimpleName()).write("message", e.getShortMessage()); + gen + .writeStartObject() + .write("code", e.getClass().getSimpleName()) + .write("message", e.getShortMessage()); gen.writeEnd().close(); if (wrap) { String pre = padding ? paddedPrefix : prefix; - return new ValueContainer(Response.status(e.getHttpStatusCode()).entity(pre + baos.toString().replace("'", "\\'") + suffix) - .build()); + return new ValueContainer( + Response + .status(e.getHttpStatusCode()) + .entity( + pre + baos.toString().replace("'", "\\'") + suffix + ) + .build() + ); } else { - return new ValueContainer(Response.status(e.getHttpStatusCode()).entity(baos.toString()).build()); + return new ValueContainer( + Response + .status(e.getHttpStatusCode()) + .entity(baos.toString()) + .build() + ); } } } - - private Long registerDatafile(String sessionId, String name, long datafileFormatId, String location, long checksum, - long size, Dataset dataset, String description, String doi, Long datafileCreateTime, Long datafileModTime) - throws InsufficientPrivilegesException, NotFoundException, InternalException, BadRequestException { - - + private Long registerDatafile( + String sessionId, + String name, + long datafileFormatId, + String location, + long checksum, + long size, + Dataset dataset, + String description, + String doi, + Long datafileCreateTime, + Long datafileModTime + ) + throws InsufficientPrivilegesException, NotFoundException, InternalException, BadRequestException { var serviceProvider = ServiceProvider.getInstance(); ICAT icat = serviceProvider.getIcat(); - + final Datafile df = new Datafile(); DatafileFormat format; try { - format = (DatafileFormat) icat.get(sessionId, "DatafileFormat", datafileFormatId); + format = + (DatafileFormat) icat.get( + sessionId, + "DatafileFormat", + datafileFormatId + ); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if ( + type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || + type == IcatExceptionType.SESSION + ) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -315,12 +475,16 @@ private Long registerDatafile(String sessionId, String name, long datafileFormat if (datafileCreateTime != null) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTimeInMillis(datafileCreateTime); - df.setDatafileCreateTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); + df.setDatafileCreateTime( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar) + ); } if (datafileModTime != null) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTimeInMillis(datafileModTime); - df.setDatafileModTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); + df.setDatafileModTime( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar) + ); } try { long dfId = icat.create(sessionId, df); @@ -328,15 +492,24 @@ private Long registerDatafile(String sessionId, String name, long datafileFormat String key = serviceProvider.getPropertyHandler().getKey(); if (key != null) { - df.setLocation(location + " " + IcatSecurity.digest(dfId, location, key)); + df.setLocation( + location + " " + IcatSecurity.digest(dfId, location, key) + ); icat.update(sessionId, df); } - logger.debug("Registered datafile for dataset {} for {}", dataset.getId(), name + " at " + location); + logger.debug( + "Registered datafile for dataset {} for {}", + dataset.getId(), + name + " at " + location + ); return dfId; } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if ( + type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || + type == IcatExceptionType.SESSION + ) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.VALIDATION) { @@ -349,13 +522,33 @@ private Long registerDatafile(String sessionId, String name, long datafileFormat } public String addParametersToLogString() { - return "name='" + name + "' " + "datafileFormatId='" - + datafileFormatIdString + "' " + "datasetId='" + datasetIdString + "' " + "description='" - + description + "' " + "doi='" + doi + "' " + "datafileCreateTime='" + datafileCreateTimeString - + "' " + "datafileModTime='" + datafileModTimeString + "'"; + return ( + "name='" + + name + + "' " + + "datafileFormatId='" + + datafileFormatIdString + + "' " + + "datasetId='" + + datasetIdString + + "' " + + "description='" + + description + + "' " + + "doi='" + + doi + + "' " + + "datafileCreateTime='" + + datafileCreateTimeString + + "' " + + "datafileModTime='" + + datafileModTimeString + + "'" + ); } - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { gen.write("userName", serviceProvider.getIcat().getUserName(sessionId)); gen.write("datafileId", dfId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java index 2fff2bb3..902b848c 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java @@ -15,20 +15,36 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; - public class ResetHandler extends DataRequestHandler { - public ResetHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.RESET, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public ResetHandler( + String ip, + String preparedId, + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds + ) { + super( + RequestType.RESET, + ip, + preparedId, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService + ) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { FiniteStateMachine fsm = ServiceProvider.getInstance().getFsm(); - for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { + for (DataInfoBase dataInfo : dataSelectionService + .getPrimaryDataInfos() + .values()) { fsm.recordSuccess(dataInfo.getId()); } @@ -39,7 +55,4 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic public CallType getCallType() { return CallType.MIGRATE; } - - - } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java index 77e0c7e5..259545fc 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java @@ -15,16 +15,29 @@ public class RestoreHandler extends DataRequestHandler { - public RestoreHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.RESTORE, ip, sessionId, investigationIds, datasetIds, datafileIds); + public RestoreHandler( + String ip, + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds + ) { + super( + RequestType.RESTORE, + ip, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - - dataSelectionService.scheduleTasks(DeferredOp.RESTORE); + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService + ) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + dataSelectionService.scheduleTasks(DeferredOp.RESTORE); return ValueContainer.getVoid(); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java index c6f994f4..5965b281 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java @@ -2,7 +2,6 @@ import java.io.IOException; import java.util.Map; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; @@ -16,45 +15,67 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataRequestHandler; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; public class WriteHandler extends DataRequestHandler { - public WriteHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.WRITE, ip, sessionId, investigationIds, datasetIds, datafileIds); + public WriteHandler( + String ip, + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds + ) { + super( + RequestType.WRITE, + ip, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService + ) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var serviceProvider = ServiceProvider.getInstance(); if (!serviceProvider.getPropertyHandler().getEnableWrite()) { - throw new NotImplementedException("This operation has been configured to be unavailable"); + throw new NotImplementedException( + "This operation has been configured to be unavailable" + ); } Map dsInfos = dataSelectionService.getDsInfo(); - try (Lock lock = serviceProvider.getLockManager().lock(dsInfos.values(), LockType.SHARED)) { + try ( + Lock lock = serviceProvider + .getLockManager() + .lock(dsInfos.values(), LockType.SHARED) + ) { if (twoLevel) { boolean maybeOffline = false; - for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { + for (DataInfoBase dataInfo : dataSelectionService + .getPrimaryDataInfos() + .values()) { if (!dataSelectionService.existsInMainStorage(dataInfo)) { maybeOffline = true; } } if (maybeOffline) { - throw new DataNotOnlineException("Requested data is not online, write request refused"); + throw new DataNotOnlineException( + "Requested data is not online, write request refused" + ); } } dataSelectionService.scheduleTasks(DeferredOp.WRITE); - } catch (AlreadyLockedException e) { logger.debug("Could not acquire lock, write failed"); throw new DataNotOnlineException("Data is busy"); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java b/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java index 1291e98e..956a8210 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java @@ -1,7 +1,7 @@ package org.icatproject.ids.requestHandlers.base; +import jakarta.json.stream.JsonGenerator; import java.util.regex.Pattern; - import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -12,32 +12,38 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; -import jakarta.json.stream.JsonGenerator; - public abstract class DataControllerBase { /** * matches standard UUID format of 8-4-4-4-12 hexadecimal digits */ - public static final Pattern uuidRegExp = Pattern - .compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); + public static final Pattern uuidRegExp = Pattern.compile( + "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$" + ); - public abstract DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException; + public abstract DataSelectionService provideDataSelectionService( + RequestType requestType + ) + throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException; public abstract void validateUUID() throws BadRequestException; public abstract String addParametersToLogString(); - public abstract void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException; + public abstract void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException; - public abstract boolean mustZip(boolean zip, DataSelectionService dataSelectionService); + public abstract boolean mustZip( + boolean zip, + DataSelectionService dataSelectionService + ); public abstract String getOperationId(); /** * returns the current sessionId. If it's null or not defined it should be created * @return - * @throws InternalException + * @throws InternalException */ public abstract String forceGetSessionId() throws InternalException; @@ -47,19 +53,25 @@ public abstract class DataControllerBase { * @param id The String which has to be checked if it is a valid UUID * @throws BadRequestException */ - public static void validateUUID(String thing, String id) throws BadRequestException { - if (id == null || !uuidRegExp.matcher(id).matches()) - throw new BadRequestException("The " + thing + " parameter '" + id + "' is not a valid UUID"); + public static void validateUUID(String thing, String id) + throws BadRequestException { + if ( + id == null || !uuidRegExp.matcher(id).matches() + ) throw new BadRequestException( + "The " + thing + " parameter '" + id + "' is not a valid UUID" + ); } protected String createSessionId() throws InternalException { String sessionId; try { - sessionId = ServiceProvider.getInstance().getIcatReader().getSessionId(); + sessionId = + ServiceProvider.getInstance().getIcatReader().getSessionId(); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage() + ); } return sessionId; } - } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java index 72576fa5..f604cd3b 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java @@ -1,5 +1,6 @@ package org.icatproject.ids.requestHandlers.base; +import jakarta.json.stream.JsonGenerator; import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -11,60 +12,91 @@ import org.icatproject.ids.helpers.ValueContainer; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; -import jakarta.json.stream.JsonGenerator; - - public abstract class DataRequestHandler extends RequestHandlerBase { protected DataControllerBase dataController; - - protected DataRequestHandler(RequestType requestType, String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + protected DataRequestHandler( + RequestType requestType, + String ip, + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds + ) { super(requestType, ip); - - this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); + this.dataController = + new UnpreparedDataController( + sessionId, + investigationIds, + datasetIds, + datafileIds + ); } - protected DataRequestHandler(RequestType requestType, String ip, String preparedId) { + protected DataRequestHandler( + RequestType requestType, + String ip, + String preparedId + ) { super(requestType, ip); - this.dataController = new PreparedDataController(preparedId); } - protected DataRequestHandler(RequestType requestType, String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + protected DataRequestHandler( + RequestType requestType, + String ip, + String preparedId, + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds + ) { super(requestType, ip); - - if(sessionId != null) { - this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); + if (sessionId != null) { + this.dataController = + new UnpreparedDataController( + sessionId, + investigationIds, + datasetIds, + datafileIds + ); } else { this.dataController = new PreparedDataController(preparedId); } - } @Override - protected ValueContainer handleRequest() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - + protected ValueContainer handleRequest() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { this.dataController.validateUUID(); - DataSelectionService dataSelectionService = this.dataController.provideDataSelectionService(this.requestType); + DataSelectionService dataSelectionService = + this.dataController.provideDataSelectionService(this.requestType); ValueContainer result = this.handleDataRequest(dataSelectionService); return result; - } @Override - protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + protected void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { this.dataController.addParametersToTransmitterJSON(gen); this.addCustomParametersToTransmitterJSON(gen); } - protected abstract ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws NotImplementedException, InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException; + protected abstract ValueContainer handleDataRequest( + DataSelectionService dataSelectionService + ) + throws NotImplementedException, InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException; @Override protected String addParametersToLogString() { - return this.dataController.addParametersToLogString() + " " + this.addCustomParametersToLogString(); + return ( + this.dataController.addParametersToLogString() + + " " + + this.addCustomParametersToLogString() + ); } /** @@ -76,5 +108,7 @@ protected void addCustomParametersToTransmitterJSON(JsonGenerator gen) {} /** * Override this method in your concrete DataRequestHandler to add custom parameters to the log output. */ - protected String addCustomParametersToLogString() { return "";} + protected String addCustomParametersToLogString() { + return ""; + } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java b/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java index f9fe6519..b9a006dc 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java @@ -1,10 +1,10 @@ package org.icatproject.ids.requestHandlers.base; +import jakarta.json.stream.JsonGenerator; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.NoSuchFileException; - import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.RequestIdNames; import org.icatproject.ids.enums.RequestType; @@ -20,30 +20,40 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import jakarta.json.stream.JsonGenerator; - public class PreparedDataController extends DataControllerBase { - protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); + protected static final Logger logger = LoggerFactory.getLogger( + RequestHandlerBase.class + ); String preparedId; public PreparedDataController(String preparedId) { - this.preparedId = preparedId; } @Override - public DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, - BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - - var preparedDir = ServiceProvider.getInstance().getPropertyHandler().getCacheDir().resolve("prepared"); + public DataSelectionService provideDataSelectionService( + RequestType requestType + ) + throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + var preparedDir = ServiceProvider + .getInstance() + .getPropertyHandler() + .getCacheDir() + .resolve("prepared"); Prepared prepared; - try (InputStream stream = Files.newInputStream(preparedDir.resolve(preparedId))) { + try ( + InputStream stream = Files.newInputStream( + preparedDir.resolve(preparedId) + ) + ) { prepared = Prepared.unpack(stream); } catch (NoSuchFileException e) { - throw new NotFoundException("The preparedId " + preparedId + " is not known"); + throw new NotFoundException( + "The preparedId " + preparedId + " is not known" + ); } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -62,12 +72,16 @@ public String addParametersToLogString() { } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { gen.write(RequestIdNames.preparedId, this.preparedId); } @Override - public boolean mustZip(boolean zip, DataSelectionService dataSelectionService) { + public boolean mustZip( + boolean zip, + DataSelectionService dataSelectionService + ) { return zip; } @@ -80,5 +94,4 @@ public String getOperationId() { public String forceGetSessionId() throws InternalException { return this.createSessionId(); } - } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java b/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java index 9524b7a0..5281c60b 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java @@ -1,15 +1,10 @@ package org.icatproject.ids.requestHandlers.base; +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; import java.io.ByteArrayOutputStream; import java.nio.file.Path; import java.util.regex.Pattern; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; - import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; @@ -22,7 +17,8 @@ import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.helpers.ValueContainer; import org.icatproject.ids.services.ServiceProvider; - +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This base class represents all common properties and methods which are needed by each request handler. @@ -30,7 +26,9 @@ */ public abstract class RequestHandlerBase { - protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); + protected static final Logger logger = LoggerFactory.getLogger( + RequestHandlerBase.class + ); protected Path preparedDir; protected boolean twoLevel; protected StorageUnit storageUnit; @@ -41,11 +39,11 @@ public abstract class RequestHandlerBase { /** * matches standard UUID format of 8-4-4-4-12 hexadecimal digits */ - public static final Pattern uuidRegExp = Pattern - .compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); - + public static final Pattern uuidRegExp = Pattern.compile( + "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$" + ); - protected RequestHandlerBase(RequestType requestType, String ip ) { + protected RequestHandlerBase(RequestType requestType, String ip) { this.requestType = requestType; this.ip = ip; @@ -58,7 +56,6 @@ protected RequestHandlerBase(RequestType requestType, String ip ) { this.readOnly = propertyHandler.getReadOnly(); } - /** * Informs about the RequestType the handler ist providing a handling for. * @return @@ -77,11 +74,16 @@ public RequestType getRequestType() { * @throws DataNotOnlineException * @throws NotImplementedException */ - public ValueContainer handle() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - + public ValueContainer handle() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { // some preprocessing long start = System.currentTimeMillis(); - logger.info("New webservice request: " + this.requestType.toString().toLowerCase() + " " + this.addParametersToLogString()); + logger.info( + "New webservice request: " + + this.requestType.toString().toLowerCase() + + " " + + this.addParametersToLogString() + ); // Do it ValueContainer result = this.handleRequest(); @@ -90,36 +92,51 @@ public ValueContainer handle() throws BadRequestException, InternalException, In this.transmit(start); return result; - } - private void transmit(long start) throws BadRequestException { - if (ServiceProvider.getInstance().getLogSet().contains(this.getCallType())) { + if ( + ServiceProvider + .getInstance() + .getLogSet() + .contains(this.getCallType()) + ) { try { String body = this.provideTransmissionBody(); - ServiceProvider.getInstance().getTransmitter().processMessage("archive", ip, body, start); + ServiceProvider + .getInstance() + .getTransmitter() + .processMessage("archive", ip, body, start); } catch (IcatException_Exception e) { - logger.error("Failed to prepare jms message " + e.getClass() + " " + e.getMessage()); + logger.error( + "Failed to prepare jms message " + + e.getClass() + + " " + + e.getMessage() + ); } } } - public String provideTransmissionBody() throws BadRequestException, IcatException_Exception { + public String provideTransmissionBody() + throws BadRequestException, IcatException_Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + try ( + JsonGenerator gen = Json.createGenerator(baos).writeStartObject() + ) { this.addParametersToTransmitterJSON(gen); gen.writeEnd(); } return baos.toString(); } - /** * Override to add additional parameters to the log output for the current request * @return */ - protected String addParametersToLogString() { return ""; } + protected String addParametersToLogString() { + return ""; + } /** * Override to add additional parameters to the transmitter JSON @@ -127,7 +144,8 @@ public String provideTransmissionBody() throws BadRequestException, IcatExceptio * @throws IcatException_Exception * @throws BadRequestException */ - protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException {} + protected void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException {} /** * The core method of each request handler. It has to be overwritten in the concrete implementation to provide an individual request handling @@ -139,12 +157,12 @@ protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatExce * @throws DataNotOnlineException * @throws NotImplementedException */ - protected abstract ValueContainer handleRequest() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException; + protected abstract ValueContainer handleRequest() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException; /** * each handler should provide its own CallType which is needed to create the Transmitter message * @return the Calltype of the request */ protected abstract CallType getCallType(); - } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java b/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java index 57668b02..fc737ad2 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java @@ -1,5 +1,6 @@ package org.icatproject.ids.requestHandlers.base; +import jakarta.json.stream.JsonGenerator; import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -11,8 +12,6 @@ import org.icatproject.ids.services.dataSelectionService.DataSelectionService; import org.icatproject.ids.services.dataSelectionService.DataSelectionServiceFactory; -import jakarta.json.stream.JsonGenerator; - public class UnpreparedDataController extends DataControllerBase { public String sessionId; @@ -20,7 +19,12 @@ public class UnpreparedDataController extends DataControllerBase { public String datasetIds; public String datafileIds; - public UnpreparedDataController(String sessionId, String investigationIds, String datasetIds, String datafileIds) { + public UnpreparedDataController( + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds + ) { this.sessionId = sessionId; this.investigationIds = investigationIds; this.datasetIds = datasetIds; @@ -34,42 +38,75 @@ public void validateUUID() throws BadRequestException { @Override public String addParametersToLogString() { - return "investigationIds='" + investigationIds + "' " + "datasetIds='" - + datasetIds + "' " + "datafileIds='" + datafileIds + "'"; + return ( + "investigationIds='" + + investigationIds + + "' " + + "datasetIds='" + + datasetIds + + "' " + + "datafileIds='" + + datafileIds + + "'" + ); } @Override - public DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - return DataSelectionServiceFactory.getService(sessionId, investigationIds, datasetIds, datafileIds, requestType); + public DataSelectionService provideDataSelectionService( + RequestType requestType + ) + throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + return DataSelectionServiceFactory.getService( + sessionId, + investigationIds, + datasetIds, + datafileIds, + requestType + ); } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { - gen.write("userName", ServiceProvider.getInstance().getIcat().getUserName(sessionId)); + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { + gen.write( + "userName", + ServiceProvider.getInstance().getIcat().getUserName(sessionId) + ); addIds(gen, investigationIds, datasetIds, datafileIds); } - - - protected void addIds(JsonGenerator gen, String investigationIds, String datasetIds, String datafileIds) - throws BadRequestException { + protected void addIds( + JsonGenerator gen, + String investigationIds, + String datasetIds, + String datafileIds + ) throws BadRequestException { if (investigationIds != null) { gen.writeStartArray("investigationIds"); - for (long invid : DataSelectionService.getValidIds("investigationIds", investigationIds)) { + for (long invid : DataSelectionService.getValidIds( + "investigationIds", + investigationIds + )) { gen.write(invid); } gen.writeEnd(); } if (datasetIds != null) { gen.writeStartArray("datasetIds"); - for (long invid : DataSelectionService.getValidIds("datasetIds", datasetIds)) { + for (long invid : DataSelectionService.getValidIds( + "datasetIds", + datasetIds + )) { gen.write(invid); } gen.writeEnd(); } if (datafileIds != null) { gen.writeStartArray("datafileIds"); - for (long invid : DataSelectionService.getValidIds("datafileIds", datafileIds)) { + for (long invid : DataSelectionService.getValidIds( + "datafileIds", + datafileIds + )) { gen.write(invid); } gen.writeEnd(); @@ -77,7 +114,10 @@ protected void addIds(JsonGenerator gen, String investigationIds, String dataset } @Override - public boolean mustZip(boolean zip, DataSelectionService dataSelectionService) { + public boolean mustZip( + boolean zip, + DataSelectionService dataSelectionService + ) { return zip ? true : dataSelectionService.mustZip(); } @@ -91,7 +131,7 @@ public String forceGetSessionId() throws InternalException { if (this.sessionId == null) { this.sessionId = this.createSessionId(); } - + return this.sessionId; } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java index 0b4aa315..46253d9f 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java @@ -1,7 +1,6 @@ package org.icatproject.ids.requestHandlers.getSizeHandlers; import java.util.Map; - import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; @@ -19,25 +18,48 @@ public class GetSizeHandler extends DataRequestHandler { - public GetSizeHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETSIZE, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public GetSizeHandler( + String ip, + String preparedId, + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds + ) { + super( + RequestType.GETSIZE, + ip, + preparedId, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService + ) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { logger.debug("Slow computation for normal case"); long size = 0; - size = this.updateSizeFromDataInfoIds(size, dataSelectionService.getDfInfo(), this.dataController.forceGetSessionId()); + size = + this.updateSizeFromDataInfoIds( + size, + dataSelectionService.getDfInfo(), + this.dataController.forceGetSessionId() + ); return new ValueContainer(size); } - - protected long updateSizeFromDataInfoIds(long size, Map dataInfos, String sessionId) throws InternalException { + protected long updateSizeFromDataInfoIds( + long size, + Map dataInfos, + String sessionId + ) throws InternalException { StringBuilder sb = new StringBuilder(); int n = 0; for (DataInfoBase dataInfo : dataInfos.values()) { @@ -58,11 +80,18 @@ protected long updateSizeFromDataInfoIds(long size, Map data return size; } - - private long getSizeFor(String sessionId, StringBuilder sb) throws InternalException { - String query = "SELECT SUM(df.fileSize) from Datafile df WHERE df.id IN (" + sb.toString() + ") AND df.location IS NOT NULL"; + private long getSizeFor(String sessionId, StringBuilder sb) + throws InternalException { + String query = + "SELECT SUM(df.fileSize) from Datafile df WHERE df.id IN (" + + sb.toString() + + ") AND df.location IS NOT NULL"; try { - return (Long) ServiceProvider.getInstance().getIcat().search(sessionId, query).get(0); + return (Long) ServiceProvider + .getInstance() + .getIcat() + .search(sessionId, query) + .get(0); } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (IndexOutOfBoundsException e) { @@ -74,5 +103,4 @@ private long getSizeFor(String sessionId, StringBuilder sb) throws InternalExcep public CallType getCallType() { return CallType.INFO; } - } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java index 6b57cd9b..d65eb51e 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java @@ -1,7 +1,7 @@ package org.icatproject.ids.requestHandlers.getSizeHandlers; +import jakarta.json.stream.JsonGenerator; import java.util.List; - import org.icatproject.Datafile; import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.CallType; @@ -18,50 +18,92 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; -import jakarta.json.stream.JsonGenerator; - public class GetSizeHandlerForFastProcessing extends RequestHandlerBase { UnpreparedDataController dataController; - public GetSizeHandlerForFastProcessing(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + public GetSizeHandlerForFastProcessing( + String ip, + String sessionId, + String investigationIds, + String datasetIds, + String datafileIds + ) { super(RequestType.GETSIZE, ip); - - this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); + this.dataController = + new UnpreparedDataController( + sessionId, + investigationIds, + datasetIds, + datafileIds + ); } @Override - public ValueContainer handleRequest() throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - + public ValueContainer handleRequest() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { this.dataController.validateUUID(); var serviceProvider = ServiceProvider.getInstance(); - List dfids = DataSelectionService.getValidIds("datafileIds", dataController.datafileIds); - List dsids = DataSelectionService.getValidIds("datasetIds", dataController.datasetIds); - List invids = DataSelectionService.getValidIds("investigationIds", dataController.investigationIds); + List dfids = DataSelectionService.getValidIds( + "datafileIds", + dataController.datafileIds + ); + List dsids = DataSelectionService.getValidIds( + "datasetIds", + dataController.datasetIds + ); + List invids = DataSelectionService.getValidIds( + "investigationIds", + dataController.investigationIds + ); - if (dfids.size() + dsids.size() + invids.size() == 1) { - long size = 0; - size = getSizeFor(dataController.getOperationId(), invids, "df.dataset.investigation.id") - + getSizeFor(dataController.getOperationId(), dsids, "df.dataset.id") - + getSizeFor(dataController.getOperationId(), dfids, "df.id"); + size = + getSizeFor( + dataController.getOperationId(), + invids, + "df.dataset.investigation.id" + ) + + getSizeFor( + dataController.getOperationId(), + dsids, + "df.dataset.id" + ) + + getSizeFor(dataController.getOperationId(), dfids, "df.id"); logger.debug("Fast computation for simple case"); if (size == 0) { try { if (dfids.size() != 0) { - Datafile datafile = (Datafile) serviceProvider.getIcat().get(dataController.getOperationId(), "Datafile", dfids.get(0)); + Datafile datafile = (Datafile) serviceProvider + .getIcat() + .get( + dataController.getOperationId(), + "Datafile", + dfids.get(0) + ); if (datafile.getLocation() == null) { throw new NotFoundException("Datafile not found"); } } if (dsids.size() != 0) { - serviceProvider.getIcat().get(dataController.getOperationId(), "Dataset", dsids.get(0)); + serviceProvider + .getIcat() + .get( + dataController.getOperationId(), + "Dataset", + dsids.get(0) + ); } if (invids.size() != 0) { - serviceProvider.getIcat().get(dataController.getOperationId(), "Investigation", invids.get(0)); + serviceProvider + .getIcat() + .get( + dataController.getOperationId(), + "Investigation", + invids.get(0) + ); } } catch (IcatException_Exception e) { throw new NotFoundException(e.getMessage()); @@ -70,10 +112,8 @@ public ValueContainer handleRequest() throws BadRequestException, InternalExcept return new ValueContainer(size); } - - return ValueContainer.getInvalid(); //is case of fast computation is not the right way. - + return ValueContainer.getInvalid(); //is case of fast computation is not the right way. } @Override @@ -81,17 +121,19 @@ public CallType getCallType() { return CallType.INFO; } - public String addParametersToLogString() { return this.dataController.addParametersToLogString(); } + public String addParametersToLogString() { + return this.dataController.addParametersToLogString(); + } - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { this.dataController.addParametersToTransmitterJSON(gen); } - private long getSizeFor(String sessionId, List ids, String where) throws InternalException { - + private long getSizeFor(String sessionId, List ids, String where) + throws InternalException { long size = 0; if (ids != null) { - StringBuilder sb = new StringBuilder(); int n = 0; for (Long id : ids) { @@ -112,16 +154,25 @@ private long getSizeFor(String sessionId, List ids, String where) throws I return size; } - private long evalSizeFor(String sessionId, String where, StringBuilder sb) throws InternalException { - String query = "SELECT SUM(df.fileSize) from Datafile df WHERE " + where + " IN (" + sb.toString() + ") AND df.location IS NOT NULL"; + private long evalSizeFor(String sessionId, String where, StringBuilder sb) + throws InternalException { + String query = + "SELECT SUM(df.fileSize) from Datafile df WHERE " + + where + + " IN (" + + sb.toString() + + ") AND df.location IS NOT NULL"; logger.debug("icat query for size: {}", query); try { - return (Long) ServiceProvider.getInstance().getIcat().search(sessionId, query).get(0); + return (Long) ServiceProvider + .getInstance() + .getIcat() + .search(sessionId, query) + .get(0); } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (IndexOutOfBoundsException e) { return 0L; } } - } diff --git a/src/main/java/org/icatproject/ids/services/ICATGetter.java b/src/main/java/org/icatproject/ids/services/ICATGetter.java index 4073c3ce..669f86d4 100644 --- a/src/main/java/org/icatproject/ids/services/ICATGetter.java +++ b/src/main/java/org/icatproject/ids/services/ICATGetter.java @@ -1,10 +1,8 @@ package org.icatproject.ids.services; +import jakarta.xml.ws.WebServiceException; import java.net.MalformedURLException; import java.net.URL; - -import jakarta.xml.ws.WebServiceException; - import org.icatproject.ICAT; import org.icatproject.ICATService; import org.icatproject.IcatException; @@ -17,7 +15,10 @@ */ public class ICATGetter { - private static String[] suffices = new String[]{"ICATService/ICAT?wsdl", "icat/ICAT?wsdl"}; + private static String[] suffices = new String[] { + "ICATService/ICAT?wsdl", + "icat/ICAT?wsdl", + }; /** * Provide access to an ICAT SOAP web service with the basic URL string @@ -29,8 +30,8 @@ public class ICATGetter { * @return an ICAT * @throws IcatException_Exception if something is wrong */ - public static ICAT getService(String urlString) throws IcatException_Exception { - + public static ICAT getService(String urlString) + throws IcatException_Exception { if (urlString == null) { throwSessionException("Argument to constructor must not be null"); } @@ -47,14 +48,17 @@ public static ICAT getService(String urlString) throws IcatException_Exception { for (String suffix : suffices) { String icatUrlWsdl = urlString + "/" + suffix; try { - icatService = new ICATService(new URL(icatUrlWsdl)).getICATPort(); + icatService = + new ICATService(new URL(icatUrlWsdl)).getICATPort(); icatService.getApiVersion(); return icatService; } catch (MalformedURLException e) { throwSessionException("Invalid URL"); } catch (WebServiceException e) { Throwable cause = e.getCause(); - if (cause != null && cause.getMessage().contains("security")) { + if ( + cause != null && cause.getMessage().contains("security") + ) { throwSessionException(cause.getMessage()); } } catch (Exception e) { @@ -76,7 +80,6 @@ public static ICAT getService(String urlString) throws IcatException_Exception { } catch (Exception e) { throwSessionException(e.getMessage()); } - } throwSessionException("Unable to connect to: " + urlString); return null; // To please the compiler @@ -86,7 +89,10 @@ public static String getCleanUrl(String urlString) { for (String suffix : suffices) { suffix = "/" + suffix; if (urlString.endsWith(suffix)) { - return urlString.substring(0, urlString.length() - suffix.length()); + return urlString.substring( + 0, + urlString.length() - suffix.length() + ); } } if (urlString.endsWith("/")) { @@ -95,11 +101,11 @@ public static String getCleanUrl(String urlString) { return urlString; } - private static void throwSessionException(String msg) throws IcatException_Exception { + private static void throwSessionException(String msg) + throws IcatException_Exception { IcatException except = new IcatException(); except.setMessage(msg); except.setType(IcatExceptionType.SESSION); throw new IcatException_Exception(msg, new IcatException()); } - } diff --git a/src/main/java/org/icatproject/ids/services/IcatReader.java b/src/main/java/org/icatproject/ids/services/IcatReader.java index 9f5adbb8..a5ec4dc7 100644 --- a/src/main/java/org/icatproject/ids/services/IcatReader.java +++ b/src/main/java/org/icatproject/ids/services/IcatReader.java @@ -1,19 +1,16 @@ package org.icatproject.ids.services; -import java.util.List; - import jakarta.annotation.PostConstruct; import jakarta.ejb.Singleton; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - +import java.util.List; import org.icatproject.EntityBaseBean; import org.icatproject.ICAT; import org.icatproject.IcatExceptionType; import org.icatproject.IcatException_Exception; import org.icatproject.Login.Credentials; import org.icatproject.Login.Credentials.Entry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @Singleton public class IcatReader { @@ -22,14 +19,14 @@ public class IcatReader { private String sessionId; - private final static Logger logger = LoggerFactory.getLogger(IcatReader.class); + private static final Logger logger = LoggerFactory.getLogger( + IcatReader.class + ); private PropertyHandler propertyHandler; // required for injection when other constructors exist - public IcatReader() { - - } + public IcatReader() {} // primarily required for testing to make it possible to use a mocked PropertyHandler public IcatReader(PropertyHandler propertyHandler) { @@ -43,7 +40,9 @@ private void init() { login(); logger.info("Reader started"); } catch (Exception e) { - throw new RuntimeException("Reader reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException( + "Reader reports " + e.getClass() + " " + e.getMessage() + ); } } @@ -54,7 +53,6 @@ private void login() throws IcatException_Exception { icat = propertyHandler.getIcatService(); List creds = propertyHandler.getReader(); if (creds != null) { - Credentials credentials = new Credentials(); List entries = credentials.getEntry(); for (int i = 1; i < creds.size(); i += 2) { @@ -67,7 +65,8 @@ private void login() throws IcatException_Exception { } } - public EntityBaseBean get(String query, long id) throws IcatException_Exception { + public EntityBaseBean get(String query, long id) + throws IcatException_Exception { try { return icat.get(sessionId, query, id); } catch (IcatException_Exception e) { @@ -106,5 +105,4 @@ public String getSessionId() throws IcatException_Exception { } } } - } diff --git a/src/main/java/org/icatproject/ids/services/LockManager.java b/src/main/java/org/icatproject/ids/services/LockManager.java index 49beba05..e87c44aa 100644 --- a/src/main/java/org/icatproject/ids/services/LockManager.java +++ b/src/main/java/org/icatproject/ids/services/LockManager.java @@ -1,30 +1,30 @@ package org.icatproject.ids.services; +import jakarta.annotation.PostConstruct; +import jakarta.ejb.Singleton; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; - -import jakarta.annotation.PostConstruct; -import jakarta.ejb.Singleton; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.plugin.MainStorageInterface; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @Singleton public class LockManager { public enum LockType { - SHARED, EXCLUSIVE + SHARED, + EXCLUSIVE, } public class LockInfo { + public final Long id; public final LockType type; public final int count; @@ -37,6 +37,7 @@ public class LockInfo { } private class LockEntry { + final Long id; final LockType type; int count; @@ -65,6 +66,7 @@ void dec() { * Define the common interface of SingleLock and LockCollection */ public abstract class Lock implements AutoCloseable { + public abstract void release(); public void close() { @@ -73,6 +75,7 @@ public void close() { } private class SingleLock extends Lock { + private final Long id; private boolean isValid; private AutoCloseable storageLock; @@ -92,7 +95,11 @@ public void release() { try { storageLock.close(); } catch (Exception e) { - logger.error("Error while closing lock on {} in the storage plugin: {}.", id, e.getMessage()); + logger.error( + "Error while closing lock on {} in the storage plugin: {}.", + id, + e.getMessage() + ); } } logger.debug("Released a lock on {}.", id); @@ -102,6 +109,7 @@ public void release() { } private class LockCollection extends Lock { + private ArrayList locks; LockCollection() { @@ -131,7 +139,8 @@ private void init() { logger.debug("LockManager initialized."); } - public Lock lock(DatasetInfo ds, LockType type) throws AlreadyLockedException, IOException { + public Lock lock(DatasetInfo ds, LockType type) + throws AlreadyLockedException, IOException { Long id = ds.getDsId(); assert id != null; synchronized (lockMap) { @@ -139,7 +148,9 @@ public Lock lock(DatasetInfo ds, LockType type) throws AlreadyLockedException, I if (le == null) { le = new LockEntry(id, type); } else { - if (type == LockType.EXCLUSIVE || le.type == LockType.EXCLUSIVE) { + if ( + type == LockType.EXCLUSIVE || le.type == LockType.EXCLUSIVE + ) { throw new AlreadyLockedException(); } } @@ -156,12 +167,17 @@ public Lock lock(DatasetInfo ds, LockType type) throws AlreadyLockedException, I } } - public Lock lock(Collection datasets, LockType type) throws AlreadyLockedException, IOException, InternalException { + public Lock lock(Collection datasets, LockType type) + throws AlreadyLockedException, IOException, InternalException { LockCollection locks = new LockCollection(); try { for (DataInfoBase dataInfo : datasets) { DatasetInfo ds = (DatasetInfo) dataInfo; - if(ds == null) throw new InternalException("Could not cast " + dataInfo.getClass() + " to DataSetInfo. Did you handed over another sub type of DataInfoBase? "); + if (ds == null) throw new InternalException( + "Could not cast " + + dataInfo.getClass() + + " to DataSetInfo. Did you handed over another sub type of DataInfoBase? " + ); locks.add(lock(ds, type)); } } catch (AlreadyLockedException | IOException e) { @@ -180,5 +196,4 @@ public Collection getLockInfo() { return lockInfo; } } - } diff --git a/src/main/java/org/icatproject/ids/services/PropertyHandler.java b/src/main/java/org/icatproject/ids/services/PropertyHandler.java index e3cbe354..7ea1dc50 100644 --- a/src/main/java/org/icatproject/ids/services/PropertyHandler.java +++ b/src/main/java/org/icatproject/ids/services/PropertyHandler.java @@ -1,5 +1,7 @@ package org.icatproject.ids.services; +import jakarta.json.Json; +import jakarta.json.JsonReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -13,13 +15,6 @@ import java.util.List; import java.util.Properties; import java.util.Set; - -import jakarta.json.Json; -import jakarta.json.JsonReader; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.CallType; @@ -29,6 +24,8 @@ import org.icatproject.ids.plugin.ZipMapperInterface; import org.icatproject.utils.CheckedProperties; import org.icatproject.utils.CheckedProperties.CheckedPropertyException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /* * Load the properties specified in the properties file ids.properties. @@ -36,9 +33,11 @@ public class PropertyHandler { private static PropertyHandler instance = null; - private static final Logger logger = LoggerFactory.getLogger(PropertyHandler.class); + private static final Logger logger = LoggerFactory.getLogger( + PropertyHandler.class + ); - public synchronized static PropertyHandler getInstance() { + public static synchronized PropertyHandler getInstance() { if (instance == null) { instance = new PropertyHandler(); } @@ -81,7 +80,6 @@ public static Logger getLogger() { @SuppressWarnings("unchecked") private PropertyHandler() { - CheckedProperties props = new CheckedProperties(); try { @@ -96,12 +94,21 @@ private PropertyHandler() { } preparedCount = props.getPositiveInt("preparedCount"); - processQueueIntervalSeconds = props.getPositiveLong("processQueueIntervalSeconds"); - rootUserNames = new HashSet<>(Arrays.asList(props.getString("rootUserNames").trim().split("\\s+"))); - - reader = Arrays.asList(props.getString("reader").trim().split("\\s+")); + processQueueIntervalSeconds = + props.getPositiveLong("processQueueIntervalSeconds"); + rootUserNames = + new HashSet<>( + Arrays.asList( + props.getString("rootUserNames").trim().split("\\s+") + ) + ); + + reader = + Arrays.asList(props.getString("reader").trim().split("\\s+")); if (reader.size() % 2 != 1) { - throw new IllegalStateException("reader must have an odd number of words"); + throw new IllegalStateException( + "reader must have an odd number of words" + ); } readOnly = props.getBoolean("readOnly", false); @@ -111,82 +118,131 @@ private PropertyHandler() { enableWrite = !readOnly; } - sizeCheckIntervalMillis = props.getPositiveInt("sizeCheckIntervalSeconds") * 1000L; + sizeCheckIntervalMillis = + props.getPositiveInt("sizeCheckIntervalSeconds") * 1000L; if (props.has("key")) { key = props.getString("key"); } try { - Class klass = (Class) Class - .forName(props.getString("plugin.zipMapper.class")); + Class klass = (Class) Class.forName( + props.getString("plugin.zipMapper.class") + ); zipMapper = klass.getConstructor().newInstance(); logger.debug("ZipMapper initialised"); - - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException | SecurityException e) { + } catch ( + ClassNotFoundException + | InstantiationException + | IllegalAccessException + | IllegalArgumentException + | InvocationTargetException + | NoSuchMethodException + | SecurityException e + ) { abort(e.getClass() + " " + e.getMessage()); } // Now get simple properties to pass to the plugins Properties simpleProps = new Properties(); - try (InputStream is = getClass().getClassLoader().getResourceAsStream("run.properties")) { + try ( + InputStream is = getClass() + .getClassLoader() + .getResourceAsStream("run.properties") + ) { simpleProps.load(is); } catch (IOException e) { abort(e.getClass() + " " + e.getMessage()); } try { - Class klass = (Class) Class - .forName(props.getString("plugin.main.class")); - mainStorage = klass.getConstructor(Properties.class).newInstance(simpleProps); + Class klass = (Class) Class.forName( + props.getString("plugin.main.class") + ); + mainStorage = + klass + .getConstructor(Properties.class) + .newInstance(simpleProps); logger.debug("mainStorage initialised"); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException | SecurityException e) { + } catch ( + ClassNotFoundException + | InstantiationException + | IllegalAccessException + | IllegalArgumentException + | InvocationTargetException + | NoSuchMethodException + | SecurityException e + ) { logger.error("Plugin failed...", e); abort(e.getClass() + " " + e.getMessage()); } if (!props.has("plugin.archive.class")) { - logger.info("Property plugin.archive.class not set, single storage enabled."); + logger.info( + "Property plugin.archive.class not set, single storage enabled." + ); } else { try { - Class klass = (Class) Class - .forName(props.getString("plugin.archive.class")); - archiveStorage = klass.getConstructor(Properties.class).newInstance(simpleProps); + Class klass = (Class) Class.forName( + props.getString("plugin.archive.class") + ); + archiveStorage = + klass + .getConstructor(Properties.class) + .newInstance(simpleProps); logger.debug("archiveStorage initialised"); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException - | IllegalArgumentException | InvocationTargetException | NoSuchMethodException - | SecurityException e) { + } catch ( + ClassNotFoundException + | InstantiationException + | IllegalAccessException + | IllegalArgumentException + | InvocationTargetException + | NoSuchMethodException + | SecurityException e + ) { logger.error("Plugin failed...", e); abort(e.getClass() + " " + e.getMessage()); } - startArchivingLevel = props.getPositiveLong("startArchivingLevel1024bytes") * 1024; - stopArchivingLevel = props.getPositiveLong("stopArchivingLevel1024bytes") * 1024; + startArchivingLevel = + props.getPositiveLong("startArchivingLevel1024bytes") * + 1024; + stopArchivingLevel = + props.getPositiveLong("stopArchivingLevel1024bytes") * 1024; if (stopArchivingLevel >= startArchivingLevel) { - abort("startArchivingLevel1024bytes must be greater than stopArchivingLevel1024bytes"); + abort( + "startArchivingLevel1024bytes must be greater than stopArchivingLevel1024bytes" + ); } try { String storageUnitName = props.getString("storageUnit"); - storageUnit = StorageUnit.valueOf(storageUnitName.toUpperCase()); + storageUnit = + StorageUnit.valueOf(storageUnitName.toUpperCase()); } catch (IllegalArgumentException e) { List vs = new ArrayList<>(); for (StorageUnit s : StorageUnit.values()) { vs.add(s.name()); } - abort("storageUnit value " + props.getString("storageUnit") + " must be taken from " + vs); + abort( + "storageUnit value " + + props.getString("storageUnit") + + " must be taken from " + + vs + ); } if (storageUnit == StorageUnit.DATASET) { - delayDatasetWrites = props.getPositiveLong("delayDatasetWritesSeconds"); + delayDatasetWrites = + props.getPositiveLong("delayDatasetWritesSeconds"); } else if (storageUnit == StorageUnit.DATAFILE) { - delayDatafileOperations = props.getPositiveLong("delayDatafileOperationsSeconds"); + delayDatafileOperations = + props.getPositiveLong("delayDatafileOperationsSeconds"); } tidyBlockSize = props.getPositiveInt("tidyBlockSize"); } try { - cacheDir = props.getFile("cache.dir").getCanonicalFile().toPath(); + cacheDir = + props.getFile("cache.dir").getCanonicalFile().toPath(); } catch (IOException e) { abort("IOException " + e.getMessage()); } @@ -197,26 +253,39 @@ private PropertyHandler() { maxIdsInQuery = props.getPositiveInt("maxIdsInQuery"); /* JMS stuff */ - jmsTopicConnectionFactory = props.getString("jms.topicConnectionFactory", - "java:comp/DefaultJMSConnectionFactory"); + jmsTopicConnectionFactory = + props.getString( + "jms.topicConnectionFactory", + "java:comp/DefaultJMSConnectionFactory" + ); /* Call logging categories */ if (props.has("log.list")) { - for (String callTypeString : props.getString("log.list").split("\\s+")) { + for (String callTypeString : props + .getString("log.list") + .split("\\s+")) { try { - logSet.add(CallType.valueOf(callTypeString.toUpperCase())); + logSet.add( + CallType.valueOf(callTypeString.toUpperCase()) + ); } catch (IllegalArgumentException e) { - abort("Value " + callTypeString + " in log.list must be chosen from " - + Arrays.asList(CallType.values())); + abort( + "Value " + + callTypeString + + " in log.list must be chosen from " + + Arrays.asList(CallType.values()) + ); } } logger.info("log.list: " + logSet); } else { - logger.info("'log.list' entry not present so no JMS call logging will be performed"); + logger.info( + "'log.list' entry not present so no JMS call logging will be performed" + ); } - useReaderForPerformance = props.getBoolean("useReaderForPerformance", false); - + useReaderForPerformance = + props.getBoolean("useReaderForPerformance", false); } catch (CheckedPropertyException e) { abort(e.getMessage()); } @@ -247,8 +316,13 @@ public synchronized ICAT getIcatService() { try { icatService = ICATGetter.getService(icatUrl); } catch (IcatException_Exception e) { - String msg = "Problem finding ICAT API version at " + icatUrl + ": " + e.getFaultInfo().getType() + " " - + e.getMessage(); + String msg = + "Problem finding ICAT API version at " + + icatUrl + + ": " + + e.getFaultInfo().getType() + + " " + + e.getMessage(); logger.error(msg); try { Thread.sleep(10000); @@ -285,15 +359,30 @@ public synchronized int getMaxEntities() { // will produce an error message. while (maxEntities == null) { try { - org.icatproject.icat.client.ICAT ricat = new org.icatproject.icat.client.ICAT(icatUrl); - - try (JsonReader parser = Json - .createReader(new ByteArrayInputStream(ricat.getProperties().getBytes()))) { + org.icatproject.icat.client.ICAT ricat = new org.icatproject.icat.client.ICAT( + icatUrl + ); + + try ( + JsonReader parser = Json.createReader( + new ByteArrayInputStream( + ricat.getProperties().getBytes() + ) + ) + ) { maxEntities = parser.readObject().getInt("maxEntities"); - logger.info("maxEntities from the ICAT.server {} version {} is {}", icatUrl, ricat.getVersion(), - maxEntities); + logger.info( + "maxEntities from the ICAT.server {} version {} is {}", + icatUrl, + ricat.getVersion(), + maxEntities + ); } catch (Exception e) { - String msg = "Problem finding 1 ICAT API version " + e.getClass() + " " + e.getMessage(); + String msg = + "Problem finding 1 ICAT API version " + + e.getClass() + + " " + + e.getMessage(); logger.error(msg); try { Thread.sleep(10000); @@ -302,7 +391,11 @@ public synchronized int getMaxEntities() { } } } catch (URISyntaxException e) { - String msg = "Problem finding 2 ICAT API version " + e.getClass() + " " + e.getMessage(); + String msg = + "Problem finding 2 ICAT API version " + + e.getClass() + + " " + + e.getMessage(); logger.error(msg); throw new IllegalStateException(msg); } diff --git a/src/main/java/org/icatproject/ids/services/ServiceProvider.java b/src/main/java/org/icatproject/ids/services/ServiceProvider.java index dc179ad5..dace322c 100644 --- a/src/main/java/org/icatproject/ids/services/ServiceProvider.java +++ b/src/main/java/org/icatproject/ids/services/ServiceProvider.java @@ -1,7 +1,6 @@ package org.icatproject.ids.services; import java.util.Set; - import org.icatproject.ICAT; import org.icatproject.ids.enums.CallType; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; @@ -21,7 +20,13 @@ public class ServiceProvider { private IcatReader icatReader; private PropertyHandler propertyHandler; - private ServiceProvider(PropertyHandler propertyHandler, Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { + private ServiceProvider( + PropertyHandler propertyHandler, + Transmitter transmitter, + FiniteStateMachine fsm, + LockManager lockManager, + IcatReader reader + ) { this.transmitter = transmitter; this.fsm = fsm; this.lockManager = lockManager; @@ -36,21 +41,45 @@ private ServiceProvider(PropertyHandler propertyHandler, Transmitter transmitter * @param lockManager * @param reader */ - public static void createInstance(Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { - - createInstance(PropertyHandler.getInstance(), transmitter, fsm, lockManager, reader); + public static void createInstance( + Transmitter transmitter, + FiniteStateMachine fsm, + LockManager lockManager, + IcatReader reader + ) { + createInstance( + PropertyHandler.getInstance(), + transmitter, + fsm, + lockManager, + reader + ); } - public static void createInstance(PropertyHandler propertyHandler, Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { - - if(instance != null) return; - - instance = new ServiceProvider(propertyHandler, transmitter, fsm, lockManager, reader); + public static void createInstance( + PropertyHandler propertyHandler, + Transmitter transmitter, + FiniteStateMachine fsm, + LockManager lockManager, + IcatReader reader + ) { + if (instance != null) return; + + instance = + new ServiceProvider( + propertyHandler, + transmitter, + fsm, + lockManager, + reader + ); } public static ServiceProvider getInstance() { - if(instance == null) { - throw new RuntimeException("ServiceProvider is not yet instantiated, please call createInstance at first."); + if (instance == null) { + throw new RuntimeException( + "ServiceProvider is not yet instantiated, please call createInstance at first." + ); } return instance; } @@ -86,6 +115,4 @@ public ICAT getIcat() { public Set getLogSet() { return PropertyHandler.getInstance().getLogSet(); } - - } diff --git a/src/main/java/org/icatproject/ids/services/Transmitter.java b/src/main/java/org/icatproject/ids/services/Transmitter.java index 996522cb..011b3d19 100644 --- a/src/main/java/org/icatproject/ids/services/Transmitter.java +++ b/src/main/java/org/icatproject/ids/services/Transmitter.java @@ -1,8 +1,5 @@ package org.icatproject.ids.services; -import javax.naming.InitialContext; -import javax.naming.NamingException; - import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import jakarta.ejb.Singleton; @@ -13,7 +10,8 @@ import jakarta.jms.Topic; import jakarta.jms.TopicConnection; import jakarta.jms.TopicConnectionFactory; - +import javax.naming.InitialContext; +import javax.naming.NamingException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.Marker; @@ -23,7 +21,7 @@ public class Transmitter { private static Logger logger = LoggerFactory.getLogger(Transmitter.class); - private final static Marker fatal = MarkerFactory.getMarker("FATAL"); + private static final Marker fatal = MarkerFactory.getMarker("FATAL"); private Topic topic; @@ -31,12 +29,12 @@ public class Transmitter { @PostConstruct private void init() { - try { PropertyHandler propertyHandler = PropertyHandler.getInstance(); InitialContext ic = new InitialContext(); - TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ic - .lookup(propertyHandler.getJmsTopicConnectionFactory()); + TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ic.lookup( + propertyHandler.getJmsTopicConnectionFactory() + ); topicConnection = topicConnectionFactory.createTopicConnection(); topic = (Topic) ic.lookup("jms/IDS/log"); logger.info("Notification Transmitter created"); @@ -44,10 +42,9 @@ private void init() { logger.error(fatal, "Problem with JMS " + e); throw new IllegalStateException(e.getMessage()); } - } - @PreDestroy() + @PreDestroy private void exit() { try { if (topicConnection != null) { @@ -59,12 +56,25 @@ private void exit() { } } - public void processMessage(String operation, String ip, String body, long startMillis) { - try (Session jmsSession = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE)) { + public void processMessage( + String operation, + String ip, + String body, + long startMillis + ) { + try ( + Session jmsSession = topicConnection.createSession( + false, + Session.AUTO_ACKNOWLEDGE + ) + ) { TextMessage jmsg = jmsSession.createTextMessage(body); jmsg.setStringProperty("operation", operation); jmsg.setStringProperty("ip", ip); - jmsg.setLongProperty("millis", System.currentTimeMillis() - startMillis); + jmsg.setLongProperty( + "millis", + System.currentTimeMillis() - startMillis + ); jmsg.setLongProperty("start", startMillis); MessageProducer jmsProducer = jmsSession.createProducer(topic); jmsProducer.send(jmsg); @@ -73,5 +83,4 @@ public void processMessage(String operation, String ip, String body, long startM logger.error("Failed to send jms message " + operation + " " + ip); } } - } diff --git a/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java b/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java index 5f9b372b..84bb07a9 100644 --- a/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java +++ b/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java @@ -4,7 +4,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; - import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.IcatExceptionType; @@ -14,8 +13,8 @@ import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.helpers.LocationHelper; -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DatasetInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,33 +25,49 @@ */ public class UnfinishedWorkService { + protected static final Logger logger = LoggerFactory.getLogger( + UnfinishedWorkService.class + ); - protected final static Logger logger = LoggerFactory.getLogger(UnfinishedWorkService.class); - - public UnfinishedWorkService() { - - } - - public void restartUnfinishedWork(Path markerDir, String key) throws InternalException { + public UnfinishedWorkService() {} + public void restartUnfinishedWork(Path markerDir, String key) + throws InternalException { try { var serviceProvider = ServiceProvider.getInstance(); - StorageUnit storageUnit = serviceProvider.getPropertyHandler().getStorageUnit(); + StorageUnit storageUnit = serviceProvider + .getPropertyHandler() + .getStorageUnit(); for (File file : markerDir.toFile().listFiles()) { + if (storageUnit == null) break; - if(storageUnit == null) break; - - long id = Long.parseLong(file.toPath().getFileName().toString()); + long id = Long.parseLong( + file.toPath().getFileName().toString() + ); try { DataInfoBase dataInfo = this.loadDataInfo(storageUnit, id); serviceProvider.getFsm().queue(dataInfo, DeferredOp.WRITE); - logger.info("Queued " + storageUnit.toString().toLowerCase() + " with id " + id + " " + dataInfo - + " to be written as it was not written out previously by IDS"); + logger.info( + "Queued " + + storageUnit.toString().toLowerCase() + + " with id " + + id + + " " + + dataInfo + + " to be written as it was not written out previously by IDS" + ); } catch (IcatException_Exception e) { - if (e.getFaultInfo().getType() == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { - logger.warn( storageUnit.toString().toLowerCase() + " with id " + id - + " was not written out by IDS and now no longer known to ICAT"); + if ( + e.getFaultInfo().getType() == + IcatExceptionType.NO_SUCH_OBJECT_FOUND + ) { + logger.warn( + storageUnit.toString().toLowerCase() + + " with id " + + id + + " was not written out by IDS and now no longer known to ICAT" + ); Files.delete(file.toPath()); } else { throw e; @@ -64,7 +79,6 @@ public void restartUnfinishedWork(Path markerDir, String key) throws InternalExc } } - public void cleanPreparedDir(Path preparedDir) { for (File file : preparedDir.toFile().listFiles()) { Path path = file.toPath(); @@ -80,7 +94,9 @@ public void cleanPreparedDir(Path preparedDir) { } thisSize += Files.size(path); Files.delete(path); - logger.debug("Deleted " + path + " to reclaim " + thisSize + " bytes"); + logger.debug( + "Deleted " + path + " to reclaim " + thisSize + " bytes" + ); } catch (IOException e) { logger.debug("Failed to delete " + path + e.getMessage()); } @@ -88,31 +104,55 @@ public void cleanPreparedDir(Path preparedDir) { } } - public void cleanDatasetCache(Path datasetDir) { for (File dsFile : datasetDir.toFile().listFiles()) { Path path = dsFile.toPath(); try { long thisSize = Files.size(path); Files.delete(path); - logger.debug("Deleted " + path + " to reclaim " + thisSize + " bytes"); + logger.debug( + "Deleted " + path + " to reclaim " + thisSize + " bytes" + ); } catch (IOException e) { - logger.debug("Failed to delete " + path + " " + e.getClass() + " " + e.getMessage()); + logger.debug( + "Failed to delete " + + path + + " " + + e.getClass() + + " " + + e.getMessage() + ); } } } - - private DataInfoBase loadDataInfo(StorageUnit storageUnit, long id) throws IcatException_Exception, InsufficientPrivilegesException, InternalException { - if(storageUnit == StorageUnit.DATASET) { - Dataset ds = (Dataset) ServiceProvider.getInstance().getIcatReader().get("Dataset ds INCLUDE ds.investigation.facility", id); + private DataInfoBase loadDataInfo(StorageUnit storageUnit, long id) + throws IcatException_Exception, InsufficientPrivilegesException, InternalException { + if (storageUnit == StorageUnit.DATASET) { + Dataset ds = (Dataset) ServiceProvider + .getInstance() + .getIcatReader() + .get("Dataset ds INCLUDE ds.investigation.facility", id); return new DatasetInfo(ds); } - if(storageUnit == StorageUnit.DATAFILE) { - Datafile df = (Datafile) ServiceProvider.getInstance().getIcatReader().get("Datafile ds INCLUDE ds.dataset", id); - String location = LocationHelper.getLocation(df.getId(), df.getLocation()); - return new DatafileInfo(id, df.getName(), location, df.getCreateId(), df.getModId(), df.getDataset().getId()); + if (storageUnit == StorageUnit.DATAFILE) { + Datafile df = (Datafile) ServiceProvider + .getInstance() + .getIcatReader() + .get("Datafile ds INCLUDE ds.dataset", id); + String location = LocationHelper.getLocation( + df.getId(), + df.getLocation() + ); + return new DatafileInfo( + id, + df.getName(), + location, + df.getCreateId(), + df.getModId(), + df.getDataset().getId() + ); } return null; diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java index 650e410f..63de4261 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java @@ -15,7 +15,6 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.locks.ReentrantLock; - import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; @@ -23,42 +22,67 @@ import org.icatproject.ids.exceptions.DataNotOnlineException; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.exceptions.NotImplementedException; -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DataSelection; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.services.ServiceProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public abstract class DataSelectionService { - protected final static Logger logger = LoggerFactory.getLogger(DataSelectionService.class); + protected static final Logger logger = LoggerFactory.getLogger( + DataSelectionService.class + ); protected DataSelection dataSelection; protected RequestType requestType; - protected static ExecutorService threadPool; - static { threadPool = Executors.newCachedThreadPool(); } + + static { + threadPool = Executors.newCachedThreadPool(); + } private Map preparedStatusMap = new ConcurrentHashMap<>(); class PreparedStatus { + public ReentrantLock lock = new ReentrantLock(); public Long fromElement; public Future future; - } - protected DataSelectionService(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { - - this.dataSelection = new DataSelection(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress); + protected DataSelectionService( + SortedMap dsInfos, + SortedMap dfInfos, + Set emptyDatasets, + List invids2, + List dsids, + List dfids, + long length, + Boolean zip, + Boolean compress, + RequestType requestType + ) { + this.dataSelection = + new DataSelection( + dsInfos, + dfInfos, + emptyDatasets, + invids2, + dsids, + dfids, + length, + zip, + compress + ); this.requestType = requestType; } - public abstract boolean isPrepared(String preparedId) throws InternalException; + public abstract boolean isPrepared(String preparedId) + throws InternalException; /** * To get the DataInfos that is currently worked with, depending on StorageUnit @@ -66,11 +90,14 @@ protected DataSelectionService(SortedMap dsInfos, SortedMap< */ public abstract SortedMap getPrimaryDataInfos(); - public abstract boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException; + public abstract boolean existsInMainStorage(DataInfoBase dataInfo) + throws InternalException; - public abstract void queueDelete() throws NotImplementedException, InternalException; + public abstract void queueDelete() + throws NotImplementedException, InternalException; - public abstract void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException; + public abstract void scheduleTasks(DeferredOp operation) + throws NotImplementedException, InternalException; //TODO: maybe implementing this method here whould be the better way, than making it abstract. But we will miss the NotImplementedException for SingleLevelStorage in that case. // should we change the tests and the behavior of the ids server here @@ -84,7 +111,6 @@ public SortedMap getDsInfo() { return this.dataSelection.getDsInfo(); } - public SortedMap getDfInfo() { return this.dataSelection.getDfInfo(); } @@ -101,7 +127,6 @@ public long getLength() { return this.dataSelection.getLength(); } - public boolean mustZip() { return this.dataSelection.mustZip(); } @@ -110,12 +135,10 @@ public boolean isSingleDataset() { return this.dataSelection.isSingleDataset(); } - public Set getEmptyDatasets() { return this.dataSelection.getEmptyDatasets(); } - /** * tries to extract a list of ids from a comma separated id string. No spaces or leading 0's. Also * accepts null. @@ -124,8 +147,8 @@ public Set getEmptyDatasets() { * @return list of long numbers - the extracted id values * @throws BadRequestException */ - public static List getValidIds(String thing, String idList) throws BadRequestException { - + public static List getValidIds(String thing, String idList) + throws BadRequestException { List result; if (idList == null) { result = Collections.emptyList(); @@ -136,16 +159,22 @@ public static List getValidIds(String thing, String idList) throws BadRequ try { result.add(Long.parseLong(id)); } catch (NumberFormatException e) { - throw new BadRequestException("The " + thing + " parameter '" + idList + "' is not a valid " - + "string representation of a comma separated list of longs"); + throw new BadRequestException( + "The " + + thing + + " parameter '" + + idList + + "' is not a valid " + + "string representation of a comma separated list of longs" + ); } } } return result; } - - private boolean restoreIfOffline(DataInfoBase dataInfo) throws InternalException { + private boolean restoreIfOffline(DataInfoBase dataInfo) + throws InternalException { boolean maybeOffline = false; var serviceProvider = ServiceProvider.getInstance(); if (serviceProvider.getFsm().getMaybeOffline().contains(dataInfo)) { @@ -157,9 +186,7 @@ private boolean restoreIfOffline(DataInfoBase dataInfo) throws InternalException return maybeOffline; } - - public void checkOnline()throws InternalException, DataNotOnlineException { - + public void checkOnline() throws InternalException, DataNotOnlineException { boolean maybeOffline = false; for (DataInfoBase dfInfo : this.getPrimaryDataInfos().values()) { if (this.restoreIfOffline(dfInfo)) { @@ -168,55 +195,79 @@ public void checkOnline()throws InternalException, DataNotOnlineException { } if (maybeOffline) { throw new DataNotOnlineException( - "Before getting, putting, etc. a datafile or dataset, it must be restored, restoration requested automatically"); + "Before getting, putting, etc. a datafile or dataset, it must be restored, restoration requested automatically" + ); } } - public void restoreDataInfos() { - var dataInfos = this.getPrimaryDataInfos().values(); - if(!dataInfos.isEmpty()) { + if (!dataInfos.isEmpty()) { for (DataInfoBase dataInfo : dataInfos) { - ServiceProvider.getInstance().getFsm().recordSuccess(dataInfo.getId()); + ServiceProvider + .getInstance() + .getFsm() + .recordSuccess(dataInfo.getId()); } threadPool.submit(new RestoreDataInfoTask(dataInfos, this, false)); } } - public OptionalLong getFileLength() { - if (this.dataSelection.getDfInfo().isEmpty() || this.dataSelection.mustZip()) { + if ( + this.dataSelection.getDfInfo().isEmpty() || + this.dataSelection.mustZip() + ) { return OptionalLong.empty(); } return OptionalLong.of(this.dataSelection.getLength()); } - - protected boolean areDataInfosPrepared(String preparedId) throws InternalException { + protected boolean areDataInfosPrepared(String preparedId) + throws InternalException { boolean prepared = true; var serviceProvider = ServiceProvider.getInstance(); - PreparedStatus status = preparedStatusMap.computeIfAbsent(preparedId, k -> new PreparedStatus()); - - Collection toCheck = status.fromElement == null ? this.getPrimaryDataInfos().values() - : this.getPrimaryDataInfos().tailMap(status.fromElement).values(); + PreparedStatus status = preparedStatusMap.computeIfAbsent( + preparedId, + k -> new PreparedStatus() + ); + + Collection toCheck = status.fromElement == null + ? this.getPrimaryDataInfos().values() + : this.getPrimaryDataInfos().tailMap(status.fromElement).values(); logger.debug("Will check online status of {} entries", toCheck.size()); for (DataInfoBase dataInfo : toCheck) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (this.restoreIfOffline(dataInfo)) { prepared = false; status.fromElement = dataInfo.getId(); - toCheck = this.getPrimaryDataInfos().tailMap(status.fromElement).values(); - logger.debug("Will check in background status of {} entries", toCheck.size()); - status.future = threadPool.submit(new RestoreDataInfoTask(toCheck, this, true)); + toCheck = + this.getPrimaryDataInfos() + .tailMap(status.fromElement) + .values(); + logger.debug( + "Will check in background status of {} entries", + toCheck.size() + ); + status.future = + threadPool.submit( + new RestoreDataInfoTask(toCheck, this, true) + ); break; } } if (prepared) { - toCheck = status.fromElement == null ? Collections.emptySet() - : this.getPrimaryDataInfos().headMap(status.fromElement).values(); - logger.debug("Will check finally online status of {} entries", toCheck.size()); + toCheck = + status.fromElement == null + ? Collections.emptySet() + : this.getPrimaryDataInfos() + .headMap(status.fromElement) + .values(); + logger.debug( + "Will check finally online status of {} entries", + toCheck.size() + ); for (DataInfoBase dataInfo : toCheck) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (this.restoreIfOffline(dataInfo)) { @@ -228,9 +279,7 @@ protected boolean areDataInfosPrepared(String preparedId) throws InternalExcepti return prepared; } - public void delete() throws InternalException, NotImplementedException { - var serviceProvider = ServiceProvider.getInstance(); /* @@ -242,33 +291,60 @@ public void delete() throws InternalException, NotImplementedException { var dfInfo = (DatafileInfo) dataInfo; String location = dataInfo.getLocation(); try { - if ((long) serviceProvider.getIcatReader() - .search("SELECT COUNT(df) FROM Datafile df WHERE df.location LIKE '" + location.replaceAll("'", "''") + "%'") - .get(0) == 0) { + if ( + (long) serviceProvider + .getIcatReader() + .search( + "SELECT COUNT(df) FROM Datafile df WHERE df.location LIKE '" + + location.replaceAll("'", "''") + + "%'" + ) + .get(0) == + 0 + ) { if (serviceProvider.getMainStorage().exists(location)) { - logger.debug("Delete physical file " + location + " from main storage"); - serviceProvider.getMainStorage().delete(location, dfInfo.getCreateId(), dfInfo.getModId()); + logger.debug( + "Delete physical file " + + location + + " from main storage" + ); + serviceProvider + .getMainStorage() + .delete( + location, + dfInfo.getCreateId(), + dfInfo.getModId() + ); } } } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage() + ); } catch (IOException e) { - logger.error("I/O error " + e.getMessage() + " deleting " + dfInfo); - throw new InternalException(e.getClass() + " " + e.getMessage()); + logger.error( + "I/O error " + e.getMessage() + " deleting " + dfInfo + ); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } this.queueDelete(); } - private class RestoreDataInfoTask implements Callable { private Collection dataInfos; private DataSelectionService dataselection; private boolean checkFailure; - public RestoreDataInfoTask(Collection dataInfos, DataSelectionService dataSelection, boolean checkFailure) { + public RestoreDataInfoTask( + Collection dataInfos, + DataSelectionService dataSelection, + boolean checkFailure + ) { this.dataInfos = dataInfos; this.dataselection = dataSelection; this.checkFailure = checkFailure; @@ -277,13 +353,13 @@ public RestoreDataInfoTask(Collection dataInfos, DataSelectionServ @Override public Void call() throws Exception { for (DataInfoBase dataInfo : dataInfos) { - if(checkFailure) - ServiceProvider.getInstance().getFsm().checkFailure(dataInfo.getId()); + if (checkFailure) ServiceProvider + .getInstance() + .getFsm() + .checkFailure(dataInfo.getId()); dataselection.restoreIfOffline(dataInfo); } return null; } - } - } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java index 25ff3ccb..7f52b5c1 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java @@ -1,5 +1,8 @@ package org.icatproject.ids.services.dataSelectionService; +import jakarta.json.Json; +import jakarta.json.JsonArray; +import jakarta.json.JsonValue; import java.io.ByteArrayInputStream; import java.util.ArrayList; import java.util.Collections; @@ -10,7 +13,6 @@ import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; - import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ICAT; @@ -26,8 +28,8 @@ import org.icatproject.ids.exceptions.NotFoundException; import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.helpers.LocationHelper; -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.models.Prepared; import org.icatproject.ids.services.IcatReader; @@ -36,13 +38,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import jakarta.json.Json; -import jakarta.json.JsonArray; -import jakarta.json.JsonValue; - public class DataSelectionServiceFactory { - private final static Logger logger = LoggerFactory.getLogger(DataSelectionServiceFactory.class); + private static final Logger logger = LoggerFactory.getLogger( + DataSelectionServiceFactory.class + ); private static DataSelectionServiceFactory instance = null; @@ -54,31 +54,53 @@ public class DataSelectionServiceFactory { private HashMap requestTypeToReturnsMapping; public enum Returns { - DATASETS, DATASETS_AND_DATAFILES, DATAFILES + DATASETS, + DATASETS_AND_DATAFILES, + DATAFILES, } - public static DataSelectionServiceFactory getInstance() throws InternalException { + public static DataSelectionServiceFactory getInstance() + throws InternalException { if (instance == null) { var serviceProvider = ServiceProvider.getInstance(); - instance = new DataSelectionServiceFactory(serviceProvider.getPropertyHandler(), serviceProvider.getIcatReader()); + instance = + new DataSelectionServiceFactory( + serviceProvider.getPropertyHandler(), + serviceProvider.getIcatReader() + ); } return instance; } - public static DataSelectionServiceFactory getInstanceOnlyForTesting(PropertyHandler propertyHandler, IcatReader reader) throws InternalException { + public static DataSelectionServiceFactory getInstanceOnlyForTesting( + PropertyHandler propertyHandler, + IcatReader reader + ) throws InternalException { if (instance == null) { instance = new DataSelectionServiceFactory(propertyHandler, reader); } return instance; } - public static DataSelectionService getService(String userSessionId, String investigationIds, String datasetIds, String datafileIds, RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - - return DataSelectionServiceFactory.getInstance().getSelectionService(userSessionId, investigationIds, datasetIds, datafileIds, requestType); + public static DataSelectionService getService( + String userSessionId, + String investigationIds, + String datasetIds, + String datafileIds, + RequestType requestType + ) + throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + return DataSelectionServiceFactory + .getInstance() + .getSelectionService( + userSessionId, + investigationIds, + datasetIds, + datafileIds, + requestType + ); } - /** * created a DataSelectionBase object from already created data infos. That's why no infos must be loaded from ICAT. * @param dsInfos @@ -89,33 +111,61 @@ public static DataSelectionService getService(String userSessionId, String inves * @return * @throws InternalException */ - public static DataSelectionService getService(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength, RequestType requestType) throws InternalException { - - var prepared = new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength); + public static DataSelectionService getService( + SortedMap dsInfos, + SortedMap dfInfos, + Set emptyDatasets, + long fileLength, + RequestType requestType + ) throws InternalException { + var prepared = new Prepared( + dsInfos, + dfInfos, + emptyDatasets, + fileLength + ); return DataSelectionServiceFactory.getService(prepared, requestType); } - public static DataSelectionService getService(Prepared prepared, RequestType requestType) throws InternalException { + public static DataSelectionService getService( + Prepared prepared, + RequestType requestType + ) throws InternalException { List dsids = new ArrayList(prepared.dsInfos.keySet()); List dfids = new ArrayList(); List invIds = new ArrayList(); var dataFileInfos = new HashMap(); - for(DataInfoBase dfInfo: prepared.dfInfos.values()) { + for (DataInfoBase dfInfo : prepared.dfInfos.values()) { dfids.add(dfInfo.getId()); dataFileInfos.put(dfInfo.getId(), dfInfo); dfids.add(dfInfo.getId()); } - for(DataInfoBase dsInfo : prepared.dsInfos.values()) { + for (DataInfoBase dsInfo : prepared.dsInfos.values()) { dsids.add(dsInfo.getId()); - invIds.add( ((DatasetInfo)dsInfo).getInvId() ); + invIds.add(((DatasetInfo) dsInfo).getInvId()); } - return DataSelectionServiceFactory.getInstance().createSelectionService(prepared.dsInfos, prepared.dfInfos, prepared.emptyDatasets, invIds, dsids, dfids, prepared.fileLength, prepared.zip, prepared.compress, requestType); + return DataSelectionServiceFactory + .getInstance() + .createSelectionService( + prepared.dsInfos, + prepared.dfInfos, + prepared.emptyDatasets, + invIds, + dsids, + dfids, + prepared.fileLength, + prepared.zip, + prepared.compress, + requestType + ); } - private DataSelectionServiceFactory(PropertyHandler propertyHandler, IcatReader reader) throws InternalException - { + private DataSelectionServiceFactory( + PropertyHandler propertyHandler, + IcatReader reader + ) throws InternalException { logger.info("### Constructing..."); this.propertyHandler = propertyHandler; this.icat = propertyHandler.getIcatService(); @@ -128,47 +178,100 @@ private DataSelectionServiceFactory(PropertyHandler propertyHandler, IcatReader logger.info("### Constructing finished"); } - private DataSelectionService createSelectionService(SortedMap dsInfos, SortedMap dfInfos, - Set emptyDatasets, List invids, List dsids, - List dfids, long length, Boolean zip, Boolean compress, - RequestType requestType) throws InternalException { - + private DataSelectionService createSelectionService( + SortedMap dsInfos, + SortedMap dfInfos, + Set emptyDatasets, + List invids, + List dsids, + List dfids, + long length, + Boolean zip, + Boolean compress, + RequestType requestType + ) throws InternalException { StorageUnit storageUnit = this.propertyHandler.getStorageUnit(); - if(storageUnit == null ) - return new DataSelectionServiceForSingleLevelStorage(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); - - else if (storageUnit == StorageUnit.DATAFILE) - return new DataSelectionServiceForStorageUnitDatafile(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); - - else if(storageUnit == StorageUnit.DATASET) - return new DataSelectionServiceForStorageUnitDataset(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); - - else throw new InternalException("StorageUnit " + storageUnit + " unknown. Maybe you forgot to handle a new StorageUnit here?"); - + if ( + storageUnit == null + ) return new DataSelectionServiceForSingleLevelStorage( + dsInfos, + dfInfos, + emptyDatasets, + invids, + dsids, + dfids, + length, + zip, + compress, + requestType + ); else if ( + storageUnit == StorageUnit.DATAFILE + ) return new DataSelectionServiceForStorageUnitDatafile( + dsInfos, + dfInfos, + emptyDatasets, + invids, + dsids, + dfids, + length, + zip, + compress, + requestType + ); else if ( + storageUnit == StorageUnit.DATASET + ) return new DataSelectionServiceForStorageUnitDataset( + dsInfos, + dfInfos, + emptyDatasets, + invids, + dsids, + dfids, + length, + zip, + compress, + requestType + ); else throw new InternalException( + "StorageUnit " + + storageUnit + + " unknown. Maybe you forgot to handle a new StorageUnit here?" + ); } - public DataSelectionService getSelectionService( String userSessionId, String investigationIds, String datasetIds, String datafileIds, RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - + public DataSelectionService getSelectionService( + String userSessionId, + String investigationIds, + String datasetIds, + String datafileIds, + RequestType requestType + ) + throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { List dfids = getValidIds("datafileIds", datafileIds); List dsids = getValidIds("datasetIds", datasetIds); List invids = getValidIds("investigationIds", investigationIds); Returns returns = this.getReturns(requestType); - boolean dfWanted = returns == Returns.DATASETS_AND_DATAFILES || returns == Returns.DATAFILES; - boolean dsWanted = returns == Returns.DATASETS_AND_DATAFILES || returns == Returns.DATASETS; + boolean dfWanted = + returns == Returns.DATASETS_AND_DATAFILES || + returns == Returns.DATAFILES; + boolean dsWanted = + returns == Returns.DATASETS_AND_DATAFILES || + returns == Returns.DATASETS; Session userRestSession = restIcat.getSession(userSessionId); // by default use the user's REST ICAT session Session restSessionToUse = userRestSession; - + try { - logger.debug("useReaderForPerformance = {}", propertyHandler.getUseReaderForPerformance()); + logger.debug( + "useReaderForPerformance = {}", + propertyHandler.getUseReaderForPerformance() + ); if (propertyHandler.getUseReaderForPerformance()) { // if this is set, use a REST session for the reader account where possible // to improve performance due to the final database queries being simpler - restSessionToUse = restIcat.getSession(this.icatReader.getSessionId()); + restSessionToUse = + restIcat.getSession(this.icatReader.getSessionId()); } } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -176,13 +279,31 @@ public DataSelectionService getSelectionService( String userSessionId, String in logger.debug("dfids: {} dsids: {} invids: {}", dfids, dsids, invids); - return prepareFromIds(dfWanted, dsWanted, dfids, dsids, invids, userSessionId, restSessionToUse, userRestSession, requestType); + return prepareFromIds( + dfWanted, + dsWanted, + dfids, + dsids, + invids, + userSessionId, + restSessionToUse, + userRestSession, + requestType + ); } - - - private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, List dfids, List dsids, List invids, String userSessionId, Session restSessionToUse, Session userRestSession, RequestType requestType) - throws NotFoundException, InsufficientPrivilegesException, InternalException, BadRequestException { + private DataSelectionService prepareFromIds( + boolean dfWanted, + boolean dsWanted, + List dfids, + List dsids, + List invids, + String userSessionId, + Session restSessionToUse, + Session userRestSession, + RequestType requestType + ) + throws NotFoundException, InsufficientPrivilegesException, InternalException, BadRequestException { var dsInfos = new TreeMap(); var emptyDatasets = new HashSet(); var dfInfos = new TreeMap(); @@ -194,21 +315,39 @@ private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, } try { - for (Long dfid : dfids) { - List dss = icat.search(userSessionId, - "SELECT ds FROM Dataset ds JOIN ds.datafiles df WHERE df.id = " + dfid - + " AND df.location IS NOT NULL INCLUDE ds.investigation.facility"); + List dss = icat.search( + userSessionId, + "SELECT ds FROM Dataset ds JOIN ds.datafiles df WHERE df.id = " + + dfid + + " AND df.location IS NOT NULL INCLUDE ds.investigation.facility" + ); if (dss.size() == 1) { Dataset ds = (Dataset) dss.get(0); long dsid = ds.getId(); dsInfos.put(dsid, new DatasetInfo(ds)); if (dfWanted) { - Datafile df = (Datafile) icat.get(userSessionId, "Datafile", dfid); + Datafile df = (Datafile) icat.get( + userSessionId, + "Datafile", + dfid + ); length += df.getFileSize(); - String location = LocationHelper.getLocation(dfid, df.getLocation()); - dfInfos.put( df.getId(), - new DatafileInfo(dfid, df.getName(), location, df.getCreateId(), df.getModId(), dsid)); + String location = LocationHelper.getLocation( + dfid, + df.getLocation() + ); + dfInfos.put( + df.getId(), + new DatafileInfo( + dfid, + df.getName(), + location, + df.getCreateId(), + df.getModId(), + dsid + ) + ); } } else { // Next line may reveal a permissions problem @@ -218,14 +357,26 @@ private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, } for (Long dsid : dsids) { - Dataset ds = (Dataset) icat.get(userSessionId, "Dataset ds INCLUDE ds.investigation.facility", dsid); + Dataset ds = (Dataset) icat.get( + userSessionId, + "Dataset ds INCLUDE ds.investigation.facility", + dsid + ); dsInfos.put(dsid, new DatasetInfo(ds)); // dataset access for the user has been checked so the REST session for the // reader account can be used if the IDS setting to allow this is enabled - String query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " - + dsid + " AND df.location IS NOT NULL"; - JsonArray result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())) - .readArray().getJsonArray(0); + String query = + "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + + dsid + + " AND df.location IS NOT NULL"; + JsonArray result = Json + .createReader( + new ByteArrayInputStream( + restSessionToUse.search(query).getBytes() + ) + ) + .readArray() + .getJsonArray(0); if (result.getJsonNumber(2).longValueExact() == 0) { // Count 0 emptyDatasets.add(dsid); } else if (dfWanted) { @@ -234,33 +385,60 @@ private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, } for (Long invid : invids) { - String query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " - + invid; - JsonArray result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())) - .readArray().getJsonArray(0); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUse, result); - + String query = + "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + + invid; + JsonArray result = Json + .createReader( + new ByteArrayInputStream( + userRestSession.search(query).getBytes() + ) + ) + .readArray() + .getJsonArray(0); + manyDss( + dsInfos, + emptyDatasets, + dfInfos, + invid, + dfWanted, + userRestSession, + restSessionToUse, + result + ); } - } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if ( + type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || + type == IcatExceptionType.SESSION + ) { throw new InsufficientPrivilegesException(e.getMessage()); } else if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } else { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } - } catch (IcatException e) { org.icatproject.icat.client.IcatException.IcatExceptionType type = e.getType(); - if (type == org.icatproject.icat.client.IcatException.IcatExceptionType.INSUFFICIENT_PRIVILEGES - || type == org.icatproject.icat.client.IcatException.IcatExceptionType.SESSION) { + if ( + type == + org.icatproject.icat.client.IcatException.IcatExceptionType.INSUFFICIENT_PRIVILEGES || + type == + org.icatproject.icat.client.IcatException.IcatExceptionType.SESSION + ) { throw new InsufficientPrivilegesException(e.getMessage()); - } else if (type == org.icatproject.icat.client.IcatException.IcatExceptionType.NO_SUCH_OBJECT_FOUND) { + } else if ( + type == + org.icatproject.icat.client.IcatException.IcatExceptionType.NO_SUCH_OBJECT_FOUND + ) { throw new NotFoundException(e.getMessage()); } else { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } /* @@ -272,17 +450,27 @@ private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, emptyDatasets = null; } - return this.createSelectionService(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, false, false, requestType); + return this.createSelectionService( + dsInfos, + dfInfos, + emptyDatasets, + invids, + dsids, + dfids, + length, + false, + false, + requestType + ); } - /** * Checks to see if the investigation, dataset or datafile id list is a * valid comma separated list of longs. No spaces or leading 0's. Also * accepts null. */ - public static List getValidIds(String thing, String idList) throws BadRequestException { - + public static List getValidIds(String thing, String idList) + throws BadRequestException { List result; if (idList == null) { result = Collections.emptyList(); @@ -293,16 +481,26 @@ public static List getValidIds(String thing, String idList) throws BadRequ try { result.add(Long.parseLong(id)); } catch (NumberFormatException e) { - throw new BadRequestException("The " + thing + " parameter '" + idList + "' is not a valid " - + "string representation of a comma separated list of longs"); + throw new BadRequestException( + "The " + + thing + + " parameter '" + + idList + + "' is not a valid " + + "string representation of a comma separated list of longs" + ); } } } return result; } - private void manyDfs(Map dfInfos, long dsid, Session restSessionToUse, JsonArray result) - throws IcatException, InsufficientPrivilegesException, InternalException { + private void manyDfs( + Map dfInfos, + long dsid, + Session restSessionToUse, + JsonArray result + ) throws IcatException, InsufficientPrivilegesException, InternalException { // dataset access for the user has been checked so the REST session for the // reader account can be used if the IDS setting to allow this is enabled long min = result.getJsonNumber(0).longValueExact(); @@ -311,43 +509,107 @@ private void manyDfs(Map dfInfos, long dsid, Session restSes logger.debug("manyDfs min: {} max: {} count: {}", min, max, count); if (count != 0) { if (count <= maxEntities) { - String query = "SELECT df.id, df.name, df.location, df.createId, df.modId FROM Datafile df WHERE df.dataset.id = " - + dsid + " AND df.location IS NOT NULL AND df.id BETWEEN " + min + " AND " + max; - result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray(); + String query = + "SELECT df.id, df.name, df.location, df.createId, df.modId FROM Datafile df WHERE df.dataset.id = " + + dsid + + " AND df.location IS NOT NULL AND df.id BETWEEN " + + min + + " AND " + + max; + result = + Json + .createReader( + new ByteArrayInputStream( + restSessionToUse.search(query).getBytes() + ) + ) + .readArray(); for (JsonValue tupV : result) { JsonArray tup = (JsonArray) tupV; long dfid = tup.getJsonNumber(0).longValueExact(); - String location = LocationHelper.getLocation(dfid, tup.getString(2, null)); - dfInfos.put(dfid, - new DatafileInfo(dfid, tup.getString(1), location, tup.getString(3), tup.getString(4), dsid)); + String location = LocationHelper.getLocation( + dfid, + tup.getString(2, null) + ); + dfInfos.put( + dfid, + new DatafileInfo( + dfid, + tup.getString(1), + location, + tup.getString(3), + tup.getString(4), + dsid + ) + ); } } else { long half = (min + max) / 2; - String query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " - + dsid + " AND df.location IS NOT NULL AND df.id BETWEEN " + min + " AND " + half; - result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray() + String query = + "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + + dsid + + " AND df.location IS NOT NULL AND df.id BETWEEN " + + min + + " AND " + + half; + result = + Json + .createReader( + new ByteArrayInputStream( + restSessionToUse.search(query).getBytes() + ) + ) + .readArray() .getJsonArray(0); manyDfs(dfInfos, dsid, restSessionToUse, result); - query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid - + " AND df.location IS NOT NULL AND df.id BETWEEN " + (half + 1) + " AND " + max; - result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray() + query = + "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + + dsid + + " AND df.location IS NOT NULL AND df.id BETWEEN " + + (half + 1) + + " AND " + + max; + result = + Json + .createReader( + new ByteArrayInputStream( + restSessionToUse.search(query).getBytes() + ) + ) + .readArray() .getJsonArray(0); manyDfs(dfInfos, dsid, restSessionToUse, result); } } } - private void manyDss(Map dsInfos, HashSet emptyDatasets, Map dfInfos, Long invid, boolean dfWanted, Session userRestSession, Session restSessionToUseForDfs, JsonArray result) - throws IcatException, InsufficientPrivilegesException, InternalException { + private void manyDss( + Map dsInfos, + HashSet emptyDatasets, + Map dfInfos, + Long invid, + boolean dfWanted, + Session userRestSession, + Session restSessionToUseForDfs, + JsonArray result + ) throws IcatException, InsufficientPrivilegesException, InternalException { long min = result.getJsonNumber(0).longValueExact(); long max = result.getJsonNumber(1).longValueExact(); long count = result.getJsonNumber(2).longValueExact(); logger.debug("manyDss min: {} max: {} count: {}", min, max, count); if (count != 0) { if (count <= maxEntities) { - String query = "SELECT inv.name, inv.visitId, inv.facility.id, inv.facility.name FROM Investigation inv WHERE inv.id = " - + invid; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); + String query = + "SELECT inv.name, inv.visitId, inv.facility.id, inv.facility.name FROM Investigation inv WHERE inv.id = " + + invid; + result = + Json + .createReader( + new ByteArrayInputStream( + userRestSession.search(query).getBytes() + ) + ) + .readArray(); if (result.size() == 0) { return; } @@ -357,83 +619,201 @@ private void manyDss(Map dsInfos, HashSet emptyDataset long facilityId = result.getJsonNumber(2).longValueExact(); String facilityName = result.getString(3); - query = "SELECT ds.id, ds.name, ds.location FROM Dataset ds WHERE ds.investigation.id = " + invid - + " AND ds.id BETWEEN " + min + " AND " + max; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); + query = + "SELECT ds.id, ds.name, ds.location FROM Dataset ds WHERE ds.investigation.id = " + + invid + + " AND ds.id BETWEEN " + + min + + " AND " + + max; + result = + Json + .createReader( + new ByteArrayInputStream( + userRestSession.search(query).getBytes() + ) + ) + .readArray(); for (JsonValue tupV : result) { JsonArray tup = (JsonArray) tupV; long dsid = tup.getJsonNumber(0).longValueExact(); - dsInfos.put(dsid, new DatasetInfo(dsid, tup.getString(1), tup.getString(2, null), invid, invName, - visitId, facilityId, facilityName)); - - query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " - + dsid + " AND df.location IS NOT NULL"; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())) - .readArray().getJsonArray(0); + dsInfos.put( + dsid, + new DatasetInfo( + dsid, + tup.getString(1), + tup.getString(2, null), + invid, + invName, + visitId, + facilityId, + facilityName + ) + ); + + query = + "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + + dsid + + " AND df.location IS NOT NULL"; + result = + Json + .createReader( + new ByteArrayInputStream( + userRestSession.search(query).getBytes() + ) + ) + .readArray() + .getJsonArray(0); if (result.getJsonNumber(2).longValueExact() == 0) { emptyDatasets.add(dsid); } else if (dfWanted) { manyDfs(dfInfos, dsid, restSessionToUseForDfs, result); } - } } else { long half = (min + max) / 2; - String query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " - + invid + " AND ds.id BETWEEN " + min + " AND " + half; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUseForDfs, result); - query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " - + invid + " AND ds.id BETWEEN " + half + 1 + " AND " + max; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray() + String query = + "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + + invid + + " AND ds.id BETWEEN " + + min + + " AND " + + half; + result = + Json + .createReader( + new ByteArrayInputStream( + userRestSession.search(query).getBytes() + ) + ) + .readArray(); + manyDss( + dsInfos, + emptyDatasets, + dfInfos, + invid, + dfWanted, + userRestSession, + restSessionToUseForDfs, + result + ); + query = + "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + + invid + + " AND ds.id BETWEEN " + + half + + 1 + + " AND " + + max; + result = + Json + .createReader( + new ByteArrayInputStream( + userRestSession.search(query).getBytes() + ) + ) + .readArray() .getJsonArray(0); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUseForDfs, result); + manyDss( + dsInfos, + emptyDatasets, + dfInfos, + invid, + dfWanted, + userRestSession, + restSessionToUseForDfs, + result + ); } } - } private void createRequestTypeToReturnsMapping() throws InternalException { - this.requestTypeToReturnsMapping = new HashMap(); StorageUnit storageUnit = this.propertyHandler.getStorageUnit(); - this.requestTypeToReturnsMapping.put(RequestType.DELETE, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETDATAFILEIDS, Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETSIZE, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.PREPAREDATA, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.RESET, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.WRITE, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETDATA, Returns.DATASETS_AND_DATAFILES); - - if(storageUnit == null ) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATASETS); - } - else if (storageUnit == StorageUnit.DATAFILE) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.RESTORE, Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, Returns.DATAFILES); - } - else if(storageUnit == StorageUnit.DATASET) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATASETS); - this.requestTypeToReturnsMapping.put(RequestType.RESTORE, Returns.DATASETS); - this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, Returns.DATASETS); - } - - - else throw new InternalException("StorageUnit " + storageUnit + " unknown. Maybe you forgot to handle a new StorageUnit here?"); + this.requestTypeToReturnsMapping.put( + RequestType.DELETE, + Returns.DATASETS_AND_DATAFILES + ); + this.requestTypeToReturnsMapping.put( + RequestType.GETDATAFILEIDS, + Returns.DATAFILES + ); + this.requestTypeToReturnsMapping.put( + RequestType.GETSIZE, + Returns.DATASETS_AND_DATAFILES + ); + this.requestTypeToReturnsMapping.put( + RequestType.PREPAREDATA, + Returns.DATASETS_AND_DATAFILES + ); + this.requestTypeToReturnsMapping.put( + RequestType.RESET, + Returns.DATASETS_AND_DATAFILES + ); + this.requestTypeToReturnsMapping.put( + RequestType.WRITE, + Returns.DATASETS_AND_DATAFILES + ); + this.requestTypeToReturnsMapping.put( + RequestType.GETDATA, + Returns.DATASETS_AND_DATAFILES + ); + + if (storageUnit == null) { + this.requestTypeToReturnsMapping.put( + RequestType.GETSTATUS, + Returns.DATASETS + ); + } else if (storageUnit == StorageUnit.DATAFILE) { + this.requestTypeToReturnsMapping.put( + RequestType.GETSTATUS, + Returns.DATAFILES + ); + this.requestTypeToReturnsMapping.put( + RequestType.RESTORE, + Returns.DATAFILES + ); + this.requestTypeToReturnsMapping.put( + RequestType.ARCHIVE, + Returns.DATAFILES + ); + } else if (storageUnit == StorageUnit.DATASET) { + this.requestTypeToReturnsMapping.put( + RequestType.GETSTATUS, + Returns.DATASETS + ); + this.requestTypeToReturnsMapping.put( + RequestType.RESTORE, + Returns.DATASETS + ); + this.requestTypeToReturnsMapping.put( + RequestType.ARCHIVE, + Returns.DATASETS + ); + } else throw new InternalException( + "StorageUnit " + + storageUnit + + " unknown. Maybe you forgot to handle a new StorageUnit here?" + ); } - private Returns getReturns(RequestType requestType) throws NotImplementedException { + private Returns getReturns(RequestType requestType) + throws NotImplementedException { + if ( + this.requestTypeToReturnsMapping.containsKey(requestType) + ) return this.requestTypeToReturnsMapping.get(requestType); - if(this.requestTypeToReturnsMapping.containsKey(requestType)) - return this.requestTypeToReturnsMapping.get(requestType); - - // is this needed here? //if(this.propertyHandler.getStorageUnit() == null) throw new NotImplementedException("This operation is unavailable for single level storage"); - throw new NotImplementedException("There is to mapping for RequestType." + requestType + " and StorageUnit." + this.propertyHandler.getStorageUnit() + " defined. Did you forgot to register it in createRequestTypeToReturnsMapping()?"); + throw new NotImplementedException( + "There is to mapping for RequestType." + + requestType + + " and StorageUnit." + + this.propertyHandler.getStorageUnit() + + " defined. Did you forgot to register it in createRequestTypeToReturnsMapping()?" + ); } - } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java index b7915daa..6e92dcd8 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java @@ -4,54 +4,70 @@ import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; - import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.models.DataInfoBase; -public class DataSelectionServiceForSingleLevelStorage extends DataSelectionService { - - protected DataSelectionServiceForSingleLevelStorage(SortedMap dsInfos, SortedMap dfInfos, - Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { - - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); +public class DataSelectionServiceForSingleLevelStorage + extends DataSelectionService { + + protected DataSelectionServiceForSingleLevelStorage( + SortedMap dsInfos, + SortedMap dfInfos, + Set emptyDatasets, + List invids2, + List dsids, + List dfids, + long length, + Boolean zip, + Boolean compress, + RequestType requestType + ) { + super( + dsInfos, + dfInfos, + emptyDatasets, + invids2, + dsids, + dfids, + length, + zip, + compress, + requestType + ); } - @Override public SortedMap getPrimaryDataInfos() { return new TreeMap(); } - @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { - - throw new InternalException("This operation is unavailable for single level storage"); + public boolean existsInMainStorage(DataInfoBase dataInfo) + throws InternalException { + throw new InternalException( + "This operation is unavailable for single level storage" + ); } - @Override public boolean isPrepared(String preparedId) throws InternalException { return true; } - @Override - public void queueDelete() throws NotImplementedException, InternalException { + public void queueDelete() + throws NotImplementedException, InternalException { //nothing todo for single level storage } - @Override - public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { - - throw new NotImplementedException("This operation is unavailable for single level storage"); + public void scheduleTasks(DeferredOp operation) + throws NotImplementedException, InternalException { + throw new NotImplementedException( + "This operation is unavailable for single level storage" + ); } - - - - } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java index f744fa8f..4afce3b6 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java @@ -3,7 +3,6 @@ import java.util.List; import java.util.Set; import java.util.SortedMap; - import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.InternalException; @@ -11,44 +10,65 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.ServiceProvider; -public class DataSelectionServiceForStorageUnitDatafile extends DataSelectionService { - - protected DataSelectionServiceForStorageUnitDatafile(SortedMap dsInfos, SortedMap dfInfos, - Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { +public class DataSelectionServiceForStorageUnitDatafile + extends DataSelectionService { - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); + protected DataSelectionServiceForStorageUnitDatafile( + SortedMap dsInfos, + SortedMap dfInfos, + Set emptyDatasets, + List invids2, + List dsids, + List dfids, + long length, + Boolean zip, + Boolean compress, + RequestType requestType + ) { + super( + dsInfos, + dfInfos, + emptyDatasets, + invids2, + dsids, + dfids, + length, + zip, + compress, + requestType + ); } - @Override public SortedMap getPrimaryDataInfos() { return this.dataSelection.getDfInfo(); } @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { - return ServiceProvider.getInstance().getMainStorage().exists(dataInfo.getLocation()); + public boolean existsInMainStorage(DataInfoBase dataInfo) + throws InternalException { + return ServiceProvider + .getInstance() + .getMainStorage() + .exists(dataInfo.getLocation()); } - @Override public boolean isPrepared(String preparedId) throws InternalException { return areDataInfosPrepared(preparedId); } - @Override - public void queueDelete() throws NotImplementedException, InternalException { + public void queueDelete() + throws NotImplementedException, InternalException { this.scheduleTasks(DeferredOp.DELETE); } - @Override - public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { - + public void scheduleTasks(DeferredOp operation) + throws NotImplementedException, InternalException { for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); } } - } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java index ff1b5c65..f2066e36 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java @@ -3,7 +3,6 @@ import java.util.List; import java.util.Set; import java.util.SortedMap; - import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.InternalException; @@ -12,49 +11,70 @@ import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.services.ServiceProvider; -public class DataSelectionServiceForStorageUnitDataset extends DataSelectionService { - - protected DataSelectionServiceForStorageUnitDataset(SortedMap dsInfos, SortedMap dfInfos, - Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { - - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); +public class DataSelectionServiceForStorageUnitDataset + extends DataSelectionService { + + protected DataSelectionServiceForStorageUnitDataset( + SortedMap dsInfos, + SortedMap dfInfos, + Set emptyDatasets, + List invids2, + List dsids, + List dfids, + long length, + Boolean zip, + Boolean compress, + RequestType requestType + ) { + super( + dsInfos, + dfInfos, + emptyDatasets, + invids2, + dsids, + dfids, + length, + zip, + compress, + requestType + ); } - @Override public SortedMap getPrimaryDataInfos() { return this.dataSelection.getDsInfo(); } @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { - + public boolean existsInMainStorage(DataInfoBase dataInfo) + throws InternalException { var dsInfo = (DatasetInfo) dataInfo; - if(dsInfo == null) throw new InternalException("Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); - - return this.dataSelection.getEmptyDatasets().contains(dataInfo.getId()) || ServiceProvider.getInstance().getMainStorage().exists(dsInfo); + if (dsInfo == null) throw new InternalException( + "Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?" + ); + + return ( + this.dataSelection.getEmptyDatasets().contains(dataInfo.getId()) || + ServiceProvider.getInstance().getMainStorage().exists(dsInfo) + ); } - @Override public boolean isPrepared(String preparedId) throws InternalException { return this.areDataInfosPrepared(preparedId); } - @Override - public void queueDelete() throws NotImplementedException, InternalException { + public void queueDelete() + throws NotImplementedException, InternalException { this.scheduleTasks(DeferredOp.WRITE); } - @Override - public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { - + public void scheduleTasks(DeferredOp operation) + throws NotImplementedException, InternalException { for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); } } - - } diff --git a/src/main/java/org/icatproject/ids/thread/DfArchiver.java b/src/main/java/org/icatproject/ids/thread/DfArchiver.java index 15df979f..5f8c5963 100644 --- a/src/main/java/org/icatproject/ids/thread/DfArchiver.java +++ b/src/main/java/org/icatproject/ids/thread/DfArchiver.java @@ -4,20 +4,22 @@ import java.nio.file.Path; import java.util.Collection; import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /* * Removes datafiles from the fast storage (doesn't write them to archive storage) */ public class DfArchiver implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DfArchiver.class); + + private static final Logger logger = LoggerFactory.getLogger( + DfArchiver.class + ); private MainStorageInterface mainStorageInterface; private FiniteStateMachine fsm; @@ -25,7 +27,12 @@ public class DfArchiver implements Runnable { private Path markerDir; private Collection locks; - public DfArchiver(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { + public DfArchiver( + List dfInfos, + PropertyHandler propertyHandler, + FiniteStateMachine fsm, + Collection locks + ) { this.dfInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -38,16 +45,34 @@ public void run() { try { for (DatafileInfo dfInfo : dfInfos) { try { - if (Files.exists(markerDir.resolve(Long.toString(dfInfo.getDfId())))) { - logger.error("Archive of " + dfInfo - + " not carried out because a write to secondary storage operation failed previously"); + if ( + Files.exists( + markerDir.resolve(Long.toString(dfInfo.getDfId())) + ) + ) { + logger.error( + "Archive of " + + dfInfo + + " not carried out because a write to secondary storage operation failed previously" + ); } else { String dfLocation = dfInfo.getDfLocation(); - mainStorageInterface.delete(dfLocation, dfInfo.getCreateId(), dfInfo.getModId()); + mainStorageInterface.delete( + dfLocation, + dfInfo.getCreateId(), + dfInfo.getModId() + ); logger.debug("Archive of " + dfInfo + " completed"); } } catch (Exception e) { - logger.error("Archive of " + dfInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error( + "Archive of " + + dfInfo + + " failed due to " + + e.getClass() + + " " + + e.getMessage() + ); } finally { fsm.removeFromChanging(dfInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DfDeleter.java b/src/main/java/org/icatproject/ids/thread/DfDeleter.java index e0b4dab6..d69a4306 100644 --- a/src/main/java/org/icatproject/ids/thread/DfDeleter.java +++ b/src/main/java/org/icatproject/ids/thread/DfDeleter.java @@ -2,28 +2,34 @@ import java.util.Collection; import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.ArchiveStorageInterface; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Delete datafiles from archive */ public class DfDeleter implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DfDeleter.class); + private static final Logger logger = LoggerFactory.getLogger( + DfDeleter.class + ); private FiniteStateMachine fsm; private ArchiveStorageInterface archiveStorageInterface; private List dfInfos; private Collection locks; - public DfDeleter(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { + public DfDeleter( + List dfInfos, + PropertyHandler propertyHandler, + FiniteStateMachine fsm, + Collection locks + ) { this.dfInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -39,7 +45,14 @@ public void run() { archiveStorageInterface.delete(dfLocation); logger.debug("Delete of " + dfInfo + " completed"); } catch (Exception e) { - logger.error("Delete of " + dfInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error( + "Delete of " + + dfInfo + + " failed due to " + + e.getClass() + + " " + + e.getMessage() + ); } finally { fsm.removeFromChanging(dfInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DfRestorer.java b/src/main/java/org/icatproject/ids/thread/DfRestorer.java index a97327b5..a8795ed2 100644 --- a/src/main/java/org/icatproject/ids/thread/DfRestorer.java +++ b/src/main/java/org/icatproject/ids/thread/DfRestorer.java @@ -5,23 +5,24 @@ import java.util.Iterator; import java.util.List; import java.util.Set; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.ArchiveStorageInterface; import org.icatproject.ids.plugin.DfInfo; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /* * Restores datafiles from the slow to the fast storage. */ public class DfRestorer implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DfRestorer.class); + private static final Logger logger = LoggerFactory.getLogger( + DfRestorer.class + ); private MainStorageInterface mainStorageInterface; private ArchiveStorageInterface archiveStorageInterface; @@ -29,14 +30,18 @@ public class DfRestorer implements Runnable { private List dataFileInfos; private Collection locks; - public DfRestorer(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { + public DfRestorer( + List dfInfos, + PropertyHandler propertyHandler, + FiniteStateMachine fsm, + Collection locks + ) { this.dataFileInfos = dfInfos; this.fsm = fsm; this.locks = locks; mainStorageInterface = propertyHandler.getMainStorage(); archiveStorageInterface = propertyHandler.getArchiveStorage(); - } /* @@ -63,12 +68,14 @@ public void run() { //TODO: This is additional conversion caused by the redesign :-( List dfInfos = new ArrayList<>(); - for(DfInfo dfInfo : this.dataFileInfos) { + for (DfInfo dfInfo : this.dataFileInfos) { dfInfos.add(dfInfo); } - - Set failures = archiveStorageInterface.restore(mainStorageInterface, dfInfos); + Set failures = archiveStorageInterface.restore( + mainStorageInterface, + dfInfos + ); for (DatafileInfo dfInfo : dataFileInfos) { if (failures.contains(dfInfo)) { fsm.recordFailure(dfInfo.getDfId()); @@ -81,7 +88,14 @@ public void run() { } } catch (Exception e) { for (DatafileInfo dfInfo : dataFileInfos) { - logger.error("Restore of " + dfInfo + " failed " + e.getClass() + " " + e.getMessage()); + logger.error( + "Restore of " + + dfInfo + + " failed " + + e.getClass() + + " " + + e.getMessage() + ); fsm.removeFromChanging(dfInfo); } } finally { diff --git a/src/main/java/org/icatproject/ids/thread/DfWriter.java b/src/main/java/org/icatproject/ids/thread/DfWriter.java index 09881acc..b02ef46e 100644 --- a/src/main/java/org/icatproject/ids/thread/DfWriter.java +++ b/src/main/java/org/icatproject/ids/thread/DfWriter.java @@ -5,22 +5,23 @@ import java.nio.file.Path; import java.util.Collection; import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.ArchiveStorageInterface; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Copies datafiles from main to archive */ public class DfWriter implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DfWriter.class); + private static final Logger logger = LoggerFactory.getLogger( + DfWriter.class + ); private FiniteStateMachine fsm; private MainStorageInterface mainStorageInterface; @@ -29,7 +30,12 @@ public class DfWriter implements Runnable { private List dataFileInfos; private Collection locks; - public DfWriter(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { + public DfWriter( + List dfInfos, + PropertyHandler propertyHandler, + FiniteStateMachine fsm, + Collection locks + ) { this.dataFileInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -43,14 +49,29 @@ public void run() { try { for (DatafileInfo dataFileInfo : dataFileInfos) { String dfLocation = dataFileInfo.getDfLocation(); - try (InputStream is = mainStorageInterface.get(dfLocation, dataFileInfo.getCreateId(), dataFileInfo.getModId())) { + try ( + InputStream is = mainStorageInterface.get( + dfLocation, + dataFileInfo.getCreateId(), + dataFileInfo.getModId() + ) + ) { archiveStorageInterface.put(is, dfLocation); - Path marker = markerDir.resolve(Long.toString(dataFileInfo.getDfId())); + Path marker = markerDir.resolve( + Long.toString(dataFileInfo.getDfId()) + ); Files.deleteIfExists(marker); logger.debug("Removed marker " + marker); logger.debug("Write of " + dataFileInfo + " completed"); } catch (Exception e) { - logger.error("Write of " + dataFileInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error( + "Write of " + + dataFileInfo + + " failed due to " + + e.getClass() + + " " + + e.getMessage() + ); } finally { fsm.removeFromChanging(dataFileInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DsArchiver.java b/src/main/java/org/icatproject/ids/thread/DsArchiver.java index 81c2c664..4d633a34 100644 --- a/src/main/java/org/icatproject/ids/thread/DsArchiver.java +++ b/src/main/java/org/icatproject/ids/thread/DsArchiver.java @@ -2,20 +2,22 @@ import java.nio.file.Files; import java.nio.file.Path; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /* * Removes datasets from the fast storage (doesn't write them to slow storage) */ public class DsArchiver implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DsArchiver.class); + + private static final Logger logger = LoggerFactory.getLogger( + DsArchiver.class + ); private DatasetInfo dsInfo; private MainStorageInterface mainStorageInterface; @@ -23,7 +25,12 @@ public class DsArchiver implements Runnable { private Path markerDir; private Lock lock; - public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, Lock lock) { + public DsArchiver( + DatasetInfo dsInfo, + PropertyHandler propertyHandler, + FiniteStateMachine fsm, + Lock lock + ) { this.dsInfo = dsInfo; this.fsm = fsm; mainStorageInterface = propertyHandler.getMainStorage(); @@ -34,15 +41,22 @@ public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteSta @Override public void run() { try { - if (Files.exists(markerDir.resolve(Long.toString(dsInfo.getDsId())))) { - logger.error("Archive of " + dsInfo - + " not carried out because a write to secondary storage operation failed previously"); + if ( + Files.exists(markerDir.resolve(Long.toString(dsInfo.getDsId()))) + ) { + logger.error( + "Archive of " + + dsInfo + + " not carried out because a write to secondary storage operation failed previously" + ); } else { mainStorageInterface.delete(dsInfo); logger.debug("Archive of " + dsInfo + " completed"); } } catch (Exception e) { - logger.error("Archive of " + dsInfo + " failed due to " + e.getMessage()); + logger.error( + "Archive of " + dsInfo + " failed due to " + e.getMessage() + ); } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/main/java/org/icatproject/ids/thread/DsRestorer.java b/src/main/java/org/icatproject/ids/thread/DsRestorer.java index 20c7b49a..c901f834 100644 --- a/src/main/java/org/icatproject/ids/thread/DsRestorer.java +++ b/src/main/java/org/icatproject/ids/thread/DsRestorer.java @@ -10,10 +10,6 @@ import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; @@ -23,15 +19,19 @@ import org.icatproject.ids.plugin.MainStorageInterface; import org.icatproject.ids.plugin.ZipMapperInterface; import org.icatproject.ids.services.IcatReader; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /* * Restores datafiles from the slow to the fast storage. */ public class DsRestorer implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DsRestorer.class); + private static final Logger logger = LoggerFactory.getLogger( + DsRestorer.class + ); private DatasetInfo dsInfo; @@ -46,7 +46,13 @@ public class DsRestorer implements Runnable { private ZipMapperInterface zipMapper; private Lock lock; - public DsRestorer(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, IcatReader reader, Lock lock) { + public DsRestorer( + DatasetInfo dsInfo, + PropertyHandler propertyHandler, + FiniteStateMachine fsm, + IcatReader reader, + Lock lock + ) { this.dsInfo = dsInfo; this.fsm = fsm; zipMapper = propertyHandler.getZipMapper(); @@ -71,39 +77,84 @@ public void run() { long size = 0; int n = 0; - List datafiles = ((Dataset) reader.get("Dataset INCLUDE Datafile", dsInfo.getDsId())) - .getDatafiles(); - Map nameToLocalMap = new HashMap<>(datafiles.size()); + List datafiles = + ( + (Dataset) reader.get( + "Dataset INCLUDE Datafile", + dsInfo.getDsId() + ) + ).getDatafiles(); + Map nameToLocalMap = new HashMap<>( + datafiles.size() + ); for (Datafile datafile : datafiles) { if (datafile.getLocation() == null) { continue; } - nameToLocalMap.put(datafile.getName(), LocationHelper.getLocation(datafile.getId(), datafile.getLocation())); + nameToLocalMap.put( + datafile.getName(), + LocationHelper.getLocation( + datafile.getId(), + datafile.getLocation() + ) + ); size += datafile.getFileSize(); n++; } - logger.debug("Restoring dataset " + dsInfo.getInvId() + "/" + dsInfo.getDsId() + " with " + n - + " files of total size " + size); + logger.debug( + "Restoring dataset " + + dsInfo.getInvId() + + "/" + + dsInfo.getDsId() + + " with " + + n + + " files of total size " + + size + ); // Get the file into the dataset cache - Path datasetCachePath = Files.createTempFile(datasetCache, null, null); + Path datasetCachePath = Files.createTempFile( + datasetCache, + null, + null + ); archiveStorageInterface.get(dsInfo, datasetCachePath); // Now split file and store it locally - logger.debug("Unpacking dataset " + dsInfo.getInvId() + "/" + dsInfo.getDsId() + " with " + n - + " files of total size " + size); - ZipInputStream zis = new ZipInputStream(Files.newInputStream(datasetCachePath)); + logger.debug( + "Unpacking dataset " + + dsInfo.getInvId() + + "/" + + dsInfo.getDsId() + + " with " + + n + + " files of total size " + + size + ); + ZipInputStream zis = new ZipInputStream( + Files.newInputStream(datasetCachePath) + ); ZipEntry ze = zis.getNextEntry(); Set seen = new HashSet<>(); while (ze != null) { String dfName = zipMapper.getFileName(ze.getName()); if (seen.contains(dfName)) { - throw new RuntimeException("Corrupt archive for " + dsInfo + ": duplicate entry " + dfName); + throw new RuntimeException( + "Corrupt archive for " + + dsInfo + + ": duplicate entry " + + dfName + ); } String location = nameToLocalMap.get(dfName); if (location == null) { - throw new RuntimeException("Corrupt archive for " + dsInfo + ": spurious entry " + dfName); + throw new RuntimeException( + "Corrupt archive for " + + dsInfo + + ": spurious entry " + + dfName + ); } mainStorageInterface.put(zis, location); ze = zis.getNextEntry(); @@ -111,18 +162,26 @@ public void run() { } zis.close(); if (!seen.equals(nameToLocalMap.keySet())) { - throw new RuntimeException("Corrupt archive for " + dsInfo + ": missing entries"); + throw new RuntimeException( + "Corrupt archive for " + dsInfo + ": missing entries" + ); } Files.delete(datasetCachePath); fsm.recordSuccess(dsInfo.getDsId()); logger.debug("Restore of " + dsInfo + " completed"); } catch (Exception e) { fsm.recordFailure(dsInfo.getDsId()); - logger.error("Restore of " + dsInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error( + "Restore of " + + dsInfo + + " failed due to " + + e.getClass() + + " " + + e.getMessage() + ); try { mainStorageInterface.delete(dsInfo); - } catch (IOException e2) { - } + } catch (IOException e2) {} } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/main/java/org/icatproject/ids/thread/DsWriter.java b/src/main/java/org/icatproject/ids/thread/DsWriter.java index e5c1843b..4f312ac4 100644 --- a/src/main/java/org/icatproject/ids/thread/DsWriter.java +++ b/src/main/java/org/icatproject/ids/thread/DsWriter.java @@ -8,10 +8,6 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipOutputStream; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; @@ -22,15 +18,19 @@ import org.icatproject.ids.plugin.MainStorageInterface; import org.icatproject.ids.plugin.ZipMapperInterface; import org.icatproject.ids.services.IcatReader; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Copies dataset from main to archive */ public class DsWriter implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DsWriter.class); + private static final Logger logger = LoggerFactory.getLogger( + DsWriter.class + ); private static final int BUFSIZ = 1024; private DatasetInfo dsInfo; @@ -43,7 +43,13 @@ public class DsWriter implements Runnable { private ZipMapperInterface zipMapper; private Lock lock; - public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, IcatReader reader, Lock lock) { + public DsWriter( + DatasetInfo dsInfo, + PropertyHandler propertyHandler, + FiniteStateMachine fsm, + IcatReader reader, + Lock lock + ) { this.dsInfo = dsInfo; this.fsm = fsm; this.zipMapper = propertyHandler.getZipMapper(); @@ -59,27 +65,64 @@ public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteState public void run() { try { if (!mainStorageInterface.exists(dsInfo)) { - logger.info("No files present in main storage for " + dsInfo + " - will delete archive"); + logger.info( + "No files present in main storage for " + + dsInfo + + " - will delete archive" + ); archiveStorageInterface.delete(dsInfo); } else { - Path datasetCachePath = Files.createTempFile(datasetCache, null, null); + Path datasetCachePath = Files.createTempFile( + datasetCache, + null, + null + ); logger.debug("Creating " + datasetCachePath); - List datafiles = ((Dataset) reader.get("Dataset INCLUDE Datafile", dsInfo.getDsId())) - .getDatafiles(); + List datafiles = + ( + (Dataset) reader.get( + "Dataset INCLUDE Datafile", + dsInfo.getDsId() + ) + ).getDatafiles(); ZipOutputStream zos = new ZipOutputStream( - Files.newOutputStream(datasetCachePath, StandardOpenOption.CREATE)); + Files.newOutputStream( + datasetCachePath, + StandardOpenOption.CREATE + ) + ); for (Datafile datafile : datafiles) { if (datafile.getLocation() == null) { continue; } - String location = LocationHelper.getLocation(datafile.getId(), datafile.getLocation()); + String location = LocationHelper.getLocation( + datafile.getId(), + datafile.getLocation() + ); InputStream is = null; try { - zos.putNextEntry(new ZipEntry( - zipMapper.getFullEntryName(dsInfo, new DatafileInfo(datafile.getId(), datafile.getName(), - location, datafile.getCreateId(), datafile.getModId(), 0L)))); - is = mainStorageInterface.get(location, datafile.getCreateId(), datafile.getModId()); + zos.putNextEntry( + new ZipEntry( + zipMapper.getFullEntryName( + dsInfo, + new DatafileInfo( + datafile.getId(), + datafile.getName(), + location, + datafile.getCreateId(), + datafile.getModId(), + 0L + ) + ) + ) + ); + is = + mainStorageInterface.get( + location, + datafile.getCreateId(), + datafile.getModId() + ); int bytesRead = 0; byte[] buffer = new byte[BUFSIZ]; while ((bytesRead = is.read(buffer)) > 0) { @@ -105,7 +148,14 @@ public void run() { logger.debug("Removed marker " + marker); logger.debug("Write of " + dsInfo + " completed"); } catch (Exception e) { - logger.error("Write of " + dsInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error( + "Write of " + + dsInfo + + " failed due to " + + e.getClass() + + " " + + e.getMessage() + ); } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/test/java/org/icatproject/ids/DataSelectionDevTest.java b/src/test/java/org/icatproject/ids/DataSelectionDevTest.java index f0ba796e..db4e62b9 100644 --- a/src/test/java/org/icatproject/ids/DataSelectionDevTest.java +++ b/src/test/java/org/icatproject/ids/DataSelectionDevTest.java @@ -1,24 +1,16 @@ package org.icatproject.ids; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import jakarta.json.Json; +import jakarta.json.JsonReader; import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.net.URISyntaxException; import java.util.Arrays; import java.util.List; import java.util.Properties; - -import jakarta.json.Json; -import jakarta.json.JsonReader; - -import static org.junit.Assert.assertTrue; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import static org.mockito.Mockito.when; -import org.mockito.junit.MockitoJUnitRunner; - import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; import org.icatproject.icat.client.IcatException; @@ -28,6 +20,12 @@ import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; import org.icatproject.ids.services.dataSelectionService.DataSelectionServiceFactory; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; /** * This test was created to fix issue #115 and was run against the Diamond @@ -74,7 +72,11 @@ public class DataSelectionDevTest { @Before public void setup() throws Exception { testProps = new Properties(); - testProps.load(new FileInputStream("src/test/resources/DataSelectionDevTest.properties")); + testProps.load( + new FileInputStream( + "src/test/resources/DataSelectionDevTest.properties" + ) + ); // set up the SOAP and REST ICAT clients icatUrl = ICATGetter.getCleanUrl(testProps.getProperty("icat.url")); icatService = ICATGetter.getService(icatUrl); @@ -83,37 +85,64 @@ public void setup() throws Exception { String userCredsString = testProps.getProperty("login.user"); userSessionId = TestUtils.login(icatService, userCredsString); System.out.println("userSessionId = " + userSessionId); - List readerCreds = Arrays.asList(testProps.getProperty("login.reader").trim().split("\\s+")); + List readerCreds = Arrays.asList( + testProps.getProperty("login.reader").trim().split("\\s+") + ); this.readerCreds = readerCreds; investigationIds = testProps.getProperty("investigation.ids"); datasetIds = testProps.getProperty("dataset.ids"); datafileIds = testProps.getProperty("datafile.ids"); - useReaderForPerformance = testProps.getProperty("useReaderForPerformance").equalsIgnoreCase("true"); + useReaderForPerformance = + testProps + .getProperty("useReaderForPerformance") + .equalsIgnoreCase("true"); // set up a mocked version of the PropertyHandler setupPropertyHandler(); icatReader = new IcatReader(mockedPropertyHandler); } private void setupPropertyHandler() - throws URISyntaxException, IcatException_Exception, IcatException { - JsonReader parser = Json.createReader(new ByteArrayInputStream(restIcat.getProperties().getBytes())); + throws URISyntaxException, IcatException_Exception, IcatException { + JsonReader parser = Json.createReader( + new ByteArrayInputStream(restIcat.getProperties().getBytes()) + ); maxEntities = parser.readObject().getInt("maxEntities"); when(mockedPropertyHandler.getMaxEntities()).thenReturn(maxEntities); when(mockedPropertyHandler.getIcatService()).thenReturn(icatService); when(mockedPropertyHandler.getRestIcat()).thenReturn(restIcat); when(mockedPropertyHandler.getReader()).thenReturn(readerCreds); - when(mockedPropertyHandler.getUseReaderForPerformance()).thenReturn(useReaderForPerformance); + when(mockedPropertyHandler.getUseReaderForPerformance()) + .thenReturn(useReaderForPerformance); } - @Ignore("Test requires a specific ICAT setup to produce meaningful results. See class javadoc comment.") + @Ignore( + "Test requires a specific ICAT setup to produce meaningful results. See class javadoc comment." + ) @Test public void testCreateDataSelection() throws Exception { long startMs = System.currentTimeMillis(); - var dataSelectionFactory = DataSelectionServiceFactory.getInstanceOnlyForTesting(mockedPropertyHandler, icatReader); - DataSelectionService dataSelectionService = dataSelectionFactory.getSelectionService(userSessionId,investigationIds, datasetIds, datafileIds, RequestType.GETSIZE); - System.out.println("Creating DataSelection took " + (System.currentTimeMillis() - startMs) + " ms"); - System.out.println("DsInfo size: " + dataSelectionService.getDsInfo().size()); - System.out.println("DfInfo size: " + dataSelectionService.getDfInfo().size()); + var dataSelectionFactory = DataSelectionServiceFactory.getInstanceOnlyForTesting( + mockedPropertyHandler, + icatReader + ); + DataSelectionService dataSelectionService = dataSelectionFactory.getSelectionService( + userSessionId, + investigationIds, + datasetIds, + datafileIds, + RequestType.GETSIZE + ); + System.out.println( + "Creating DataSelection took " + + (System.currentTimeMillis() - startMs) + + " ms" + ); + System.out.println( + "DsInfo size: " + dataSelectionService.getDsInfo().size() + ); + System.out.println( + "DfInfo size: " + dataSelectionService.getDfInfo().size() + ); // there must be at least one Datafile in the DataSelection assertTrue("message", dataSelectionService.getDfInfo().size() > 0); } diff --git a/src/test/java/org/icatproject/ids/DigestTest.java b/src/test/java/org/icatproject/ids/DigestTest.java index 735e6195..897d11e9 100644 --- a/src/test/java/org/icatproject/ids/DigestTest.java +++ b/src/test/java/org/icatproject/ids/DigestTest.java @@ -3,11 +3,11 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import org.junit.Test; import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.helpers.LocationHelper; import org.icatproject.utils.IcatSecurity; +import org.junit.Test; public class DigestTest { @@ -16,15 +16,21 @@ public class DigestTest { @Test public void testCheck() throws Exception { - String a = location + " " + IcatSecurity.digest(1234567L, location, key); + String a = + location + " " + IcatSecurity.digest(1234567L, location, key); - assertEquals(location, LocationHelper.getLocationFromDigest(1234567L, a, key)); + assertEquals( + location, + LocationHelper.getLocationFromDigest(1234567L, a, key) + ); try { LocationHelper.getLocationFromDigest(1234568L, a, key); fail(); } catch (InsufficientPrivilegesException e) { - assertTrue(e.getMessage().contains("does not contain a valid hash")); + assertTrue( + e.getMessage().contains("does not contain a valid hash") + ); } try { @@ -33,7 +39,5 @@ public void testCheck() throws Exception { } catch (InsufficientPrivilegesException e) { assertTrue(e.getMessage().contains("does not contain hash")); } - } - } diff --git a/src/test/java/org/icatproject/ids/PreparePackingTest.java b/src/test/java/org/icatproject/ids/PreparePackingTest.java index 6b2a857e..8ef93b5f 100644 --- a/src/test/java/org/icatproject/ids/PreparePackingTest.java +++ b/src/test/java/org/icatproject/ids/PreparePackingTest.java @@ -1,5 +1,11 @@ package org.icatproject.ids; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -10,15 +16,8 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.models.Prepared; import org.junit.Test; @@ -37,21 +36,64 @@ public void packAndUnpack() throws Exception { long dsid2 = 18L; long invId = 15L; long facilityId = 45L; - dfInfos.put(5L, new DatafileInfo(5L, "dfName", "dfLocation", "createId", "modId", dsid1)); + dfInfos.put( + 5L, + new DatafileInfo( + 5L, + "dfName", + "dfLocation", + "createId", + "modId", + dsid1 + ) + ); - dfInfos.put(51L, new DatafileInfo(51L, "dfName2", null, "createId", "modId", dsid1)); + dfInfos.put( + 51L, + new DatafileInfo(51L, "dfName2", null, "createId", "modId", dsid1) + ); - dsInfos.put(dsid1, new DatasetInfo(dsid1, "dsName", "dsLocation", invId, "invName", - "visitId", facilityId, "facilityName")); + dsInfos.put( + dsid1, + new DatasetInfo( + dsid1, + "dsName", + "dsLocation", + invId, + "invName", + "visitId", + facilityId, + "facilityName" + ) + ); - dsInfos.put(dsid2, new DatasetInfo(dsid2, "dsName2", null, invId, "invName", "visitId", - facilityId, "facilityName")); + dsInfos.put( + dsid2, + new DatasetInfo( + dsid2, + "dsName2", + null, + invId, + "invName", + "visitId", + facilityId, + "facilityName" + ) + ); emptyDatasets.add(dsid2); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (OutputStream stream = new BufferedOutputStream(baos)) { - Prepared.pack(stream, zip, compress, dsInfos, dfInfos, emptyDatasets, 51); + Prepared.pack( + stream, + zip, + compress, + dsInfos, + dfInfos, + emptyDatasets, + 51 + ); } System.out.println(baos.toString()); InputStream stream = new ByteArrayInputStream(baos.toByteArray()); @@ -96,7 +138,6 @@ public void packAndUnpack() throws Exception { assertEquals("visitId", dsInfo.getVisitId()); assertEquals((Long) facilityId, dsInfo.getFacilityId()); assertEquals("facilityName", dsInfo.getFacilityName()); - } assertEquals(1, prepared.emptyDatasets.size()); assertEquals((Long) dsid2, prepared.emptyDatasets.iterator().next()); diff --git a/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java b/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java index e6960866..79cc009e 100644 --- a/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java +++ b/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java @@ -1,10 +1,9 @@ package org.icatproject.ids; -import java.io.ByteArrayOutputStream; -import java.io.OutputStream; - import static org.junit.Assert.assertEquals; +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; import org.icatproject.ids.helpers.RangeOutputStream; import org.junit.Test; @@ -49,7 +48,11 @@ public void t3() throws Exception { os.flush(); } os.close(); - assertEquals("m is " + m, "ABCDEFGHIJKLMONPQRSTUVWXYZ", baos.toString()); + assertEquals( + "m is " + m, + "ABCDEFGHIJKLMONPQRSTUVWXYZ", + baos.toString() + ); } } @@ -68,7 +71,6 @@ public void t4() throws Exception { } } - @Test public void t5() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); diff --git a/src/test/java/org/icatproject/ids/TestICATGetter.java b/src/test/java/org/icatproject/ids/TestICATGetter.java index 8bfa99c6..e8e488a6 100644 --- a/src/test/java/org/icatproject/ids/TestICATGetter.java +++ b/src/test/java/org/icatproject/ids/TestICATGetter.java @@ -1,26 +1,35 @@ package org.icatproject.ids; import static org.junit.Assert.assertEquals; -import org.junit.Test; import org.icatproject.ICAT; import org.icatproject.ids.services.ICATGetter; +import org.junit.Test; public class TestICATGetter { @Test public void testGFUrl() throws Exception { - assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost/ICATService/ICAT?wsdl")); + assertEquals( + "http://localhost", + ICATGetter.getCleanUrl("http://localhost/ICATService/ICAT?wsdl") + ); } @Test public void testSlash() throws Exception { - assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost/")); + assertEquals( + "http://localhost", + ICATGetter.getCleanUrl("http://localhost/") + ); } @Test public void testClean() throws Exception { - assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost")); + assertEquals( + "http://localhost", + ICATGetter.getCleanUrl("http://localhost") + ); } @Test diff --git a/src/test/java/org/icatproject/ids/TestUtils.java b/src/test/java/org/icatproject/ids/TestUtils.java index cbd62530..054856fc 100644 --- a/src/test/java/org/icatproject/ids/TestUtils.java +++ b/src/test/java/org/icatproject/ids/TestUtils.java @@ -2,7 +2,6 @@ import java.util.Arrays; import java.util.List; - import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; import org.icatproject.Login.Credentials; @@ -20,7 +19,8 @@ public class TestUtils { * @return an ICAT session ID * @throws IcatException_Exception if the login fails */ - public static String login(ICAT icatService, String credsString) throws IcatException_Exception { + public static String login(ICAT icatService, String credsString) + throws IcatException_Exception { List creds = Arrays.asList(credsString.trim().split("\\s+")); Credentials credentials = new Credentials(); List entries = credentials.getEntry(); @@ -32,5 +32,4 @@ public static String login(ICAT icatService, String credsString) throws IcatExce } return icatService.login(creds.get(0), credentials); } - } diff --git a/src/test/java/org/icatproject/ids/TestValueContainer.java b/src/test/java/org/icatproject/ids/TestValueContainer.java index bc3daf98..41183f33 100644 --- a/src/test/java/org/icatproject/ids/TestValueContainer.java +++ b/src/test/java/org/icatproject/ids/TestValueContainer.java @@ -9,18 +9,15 @@ import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.stream.Collectors; - import org.icatproject.ids.enums.ValueContainerType; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.helpers.ValueContainer; import org.junit.Test; - public class TestValueContainer { @Test public void testInvalidValueContainer() throws Exception { - var vc = ValueContainer.getInvalid(); assertFalse(vc.isVoid()); assertTrue(vc.getType() == ValueContainerType.INVALID); @@ -28,7 +25,6 @@ public void testInvalidValueContainer() throws Exception { @Test public void testVoidValueContainer() throws Exception { - var vc = ValueContainer.getVoid(); assertTrue(vc.isVoid()); assertTrue(vc.getType() == ValueContainerType.VOID); @@ -37,7 +33,6 @@ public void testVoidValueContainer() throws Exception { @Test(expected = InternalException.class) public void testIntValueContainer() throws Exception { - var vc = new ValueContainer(1); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -50,7 +45,6 @@ public void testIntValueContainer() throws Exception { @Test(expected = InternalException.class) public void testLongValueContainer() throws Exception { - var vc = new ValueContainer(1L); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -63,7 +57,6 @@ public void testLongValueContainer() throws Exception { @Test(expected = InternalException.class) public void testBoolValueContainer() throws Exception { - var vc = new ValueContainer(true); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -76,7 +69,6 @@ public void testBoolValueContainer() throws Exception { @Test(expected = InternalException.class) public void testStringValueContainer() throws Exception { - var vc = new ValueContainer("test"); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -89,7 +81,6 @@ public void testStringValueContainer() throws Exception { @Test(expected = InternalException.class) public void testInputStreamValueContainer() throws Exception { - String s = "test InputStream"; var inputStream = new ByteArrayInputStream(s.getBytes()); var vc = new ValueContainer(inputStream); @@ -98,8 +89,11 @@ public void testInputStreamValueContainer() throws Exception { assertFalse(vc.isNull()); assertEquals(vc.getInputStream(), inputStream); - - var ISReader = new InputStreamReader(vc.getInputStream(), StandardCharsets.UTF_8); + + var ISReader = new InputStreamReader( + vc.getInputStream(), + StandardCharsets.UTF_8 + ); var BReader = new BufferedReader(ISReader); String textFromStream = BReader.lines().collect(Collectors.joining()); BReader.close(); @@ -107,5 +101,4 @@ public void testInputStreamValueContainer() throws Exception { vc.getString(); } - -} \ No newline at end of file +} diff --git a/src/test/java/org/icatproject/ids/TestZipping.java b/src/test/java/org/icatproject/ids/TestZipping.java index 56dce770..2f728467 100644 --- a/src/test/java/org/icatproject/ids/TestZipping.java +++ b/src/test/java/org/icatproject/ids/TestZipping.java @@ -1,5 +1,7 @@ package org.icatproject.ids; +import static org.junit.Assert.assertEquals; + import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -10,8 +12,6 @@ import java.util.zip.ZipException; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; - -import static org.junit.Assert.assertEquals; import org.junit.Test; public class TestZipping { @@ -19,13 +19,21 @@ public class TestZipping { @Test public void testDuplicates() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos)); + ZipOutputStream zos = new ZipOutputStream( + new BufferedOutputStream(baos) + ); // zos.setLevel(0); List in = new ArrayList<>(); - for (String entryName : Arrays.asList("abcd/qa", "abcd/qw", "abcd/qw", "abcd/qb", "abcd/qc", "abcd/qw")) { - + for (String entryName : Arrays.asList( + "abcd/qa", + "abcd/qw", + "abcd/qw", + "abcd/qb", + "abcd/qc", + "abcd/qw" + )) { try { zos.putNextEntry(new ZipEntry(entryName)); byte[] bytes = entryName.getBytes(); @@ -42,7 +50,9 @@ public void testDuplicates() throws Exception { assertEquals(4, in.size()); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream( + baos.toByteArray() + ); ZipInputStream zis = new ZipInputStream(bais); int off = 0; @@ -61,7 +71,5 @@ public void testDuplicates() throws Exception { ze = zis.getNextEntry(); } zis.close(); - } - } diff --git a/src/test/java/org/icatproject/ids/TidierTest.java b/src/test/java/org/icatproject/ids/TidierTest.java index 8f2762c6..58e75957 100644 --- a/src/test/java/org/icatproject/ids/TidierTest.java +++ b/src/test/java/org/icatproject/ids/TidierTest.java @@ -1,13 +1,13 @@ package org.icatproject.ids; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.io.ByteArrayInputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -16,10 +16,13 @@ public class TidierTest { private static Path file; @BeforeClass - static public void beforeClass() throws Exception { + public static void beforeClass() throws Exception { file = Files.createTempFile(null, null); - Files.copy(new ByteArrayInputStream(new byte[2000]), file, - StandardCopyOption.REPLACE_EXISTING); + Files.copy( + new ByteArrayInputStream(new byte[2000]), + file, + StandardCopyOption.REPLACE_EXISTING + ); } @Test @@ -85,13 +88,26 @@ public void testCleanPreparedDir() throws Exception { @Test public void testAddStringConstraint() throws Exception { StringBuilder sb1 = new StringBuilder(); - Tidier.addStringConstraint(sb1, "df.location", "/path/to/normal/file", false); + Tidier.addStringConstraint( + sb1, + "df.location", + "/path/to/normal/file", + false + ); assertEquals(" df.location = '/path/to/normal/file'", sb1.toString()); /* Fix error where a file path contains an apostrophe */ StringBuilder sb2 = new StringBuilder(); - Tidier.addStringConstraint(sb2, "df.location", "/path/to/Person's Files/myscript.py", false); - assertEquals(" df.location = '/path/to/Person''s Files/myscript.py'", sb2.toString()); + Tidier.addStringConstraint( + sb2, + "df.location", + "/path/to/Person's Files/myscript.py", + false + ); + assertEquals( + " df.location = '/path/to/Person''s Files/myscript.py'", + sb2.toString() + ); } @Test diff --git a/src/test/java/org/icatproject/ids/TransmittingTest.java b/src/test/java/org/icatproject/ids/TransmittingTest.java index 7e2e986c..f73e71f6 100644 --- a/src/test/java/org/icatproject/ids/TransmittingTest.java +++ b/src/test/java/org/icatproject/ids/TransmittingTest.java @@ -6,7 +6,6 @@ import java.io.ByteArrayInputStream; import java.net.URISyntaxException; import java.nio.file.Paths; - import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; import org.icatproject.icat.client.IcatException; @@ -40,9 +39,8 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; - /** - * This test was created when the building of the transmission body was made more comon and generic. + * This test was created when the building of the transmission body was made more comon and generic. * It should ensure the right structure of the containing json. */ @RunWith(MockitoJUnitRunner.class) @@ -50,15 +48,22 @@ public class TransmittingTest { @Mock private PropertyHandler mockedPropertyHandler; + @Mock private Transmitter mockedTransmitter; + @Mock private FiniteStateMachine mockedFsm; + @Mock private LockManager mockedLockManager; + @Mock private IcatReader mockedReader; - @Mock ArchiveStorageInterface mockedArchiveStorage; + + @Mock + ArchiveStorageInterface mockedArchiveStorage; + @Mock private ICAT mockedIcat; @@ -68,98 +73,184 @@ public class TransmittingTest { private String investigationIds = "1, 2, 3"; private String datasetIds = "4, 5, 6"; private String datafileIds = "7, 8, 9"; - private String defaultTransmissionBodyForPreparedId = "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}"; - private String defaultTransmissionBodyForSessionId = "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9]}"; + private String defaultTransmissionBodyForPreparedId = + "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}"; + private String defaultTransmissionBodyForSessionId = + "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9]}"; private void setup() - throws URISyntaxException, IcatException_Exception, IcatException { - + throws URISyntaxException, IcatException_Exception, IcatException { when(mockedPropertyHandler.getIcatService()).thenReturn(mockedIcat); when(mockedPropertyHandler.getCacheDir()).thenReturn(Paths.get("")); - when(mockedPropertyHandler.getStorageUnit()).thenReturn(StorageUnit.DATASET); - when(mockedPropertyHandler.getArchiveStorage()).thenReturn(mockedArchiveStorage); + when(mockedPropertyHandler.getStorageUnit()) + .thenReturn(StorageUnit.DATASET); + when(mockedPropertyHandler.getArchiveStorage()) + .thenReturn(mockedArchiveStorage); when(mockedPropertyHandler.getReadOnly()).thenReturn(false); - when(mockedIcat.getUserName("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")).thenReturn("TestUser"); - - ServiceProvider.createInstance(mockedPropertyHandler, mockedTransmitter, mockedFsm, mockedLockManager, mockedReader); + when(mockedIcat.getUserName("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")) + .thenReturn("TestUser"); + + ServiceProvider.createInstance( + mockedPropertyHandler, + mockedTransmitter, + mockedFsm, + mockedLockManager, + mockedReader + ); } @Test - public void testTransmitterBodyForArchiveRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForArchiveRequest_shouldBeOk() + throws Exception { setup(); - var handler = new ArchiveHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new ArchiveHandler( + ip, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetSizeRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetSizeRequest_shouldBeOk() + throws Exception { setup(); - var handler = new GetSizeHandler(ip, preparedId, null, null, null, null); + var handler = new GetSizeHandler( + ip, + preparedId, + null, + null, + null, + null + ); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetSizeHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); + handler = + new GetSizeHandler( + ip, + null, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetSizeFastRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetSizeFastRequest_shouldBeOk() + throws Exception { setup(); - var handler = new GetSizeHandlerForFastProcessing(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new GetSizeHandlerForFastProcessing( + ip, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); - } @Test - public void testTransmitterBodyForDeleteRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForDeleteRequest_shouldBeOk() + throws Exception { setup(); - var handler = new DeleteHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new DeleteHandler( + ip, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetDataFileIdsRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetDataFileIdsRequest_shouldBeOk() + throws Exception { setup(); - var handler = new GetDataFileIdsHandler(ip, preparedId, null, null, null, null); + var handler = new GetDataFileIdsHandler( + ip, + preparedId, + null, + null, + null, + null + ); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetDataFileIdsHandler("192.168.17.1", null, sessionId, investigationIds, datasetIds, datafileIds); + handler = + new GetDataFileIdsHandler( + "192.168.17.1", + null, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetDataRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetDataRequest_shouldBeOk() + throws Exception { setup(); - var handler = new GetDataHandler(ip, preparedId, null, null, null, null, false, false, "", ""); + var handler = new GetDataHandler( + ip, + preparedId, + null, + null, + null, + null, + false, + false, + "", + "" + ); String body = handler.provideTransmissionBody(); - assertEquals("{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"transferId\":-1}", body); - - handler = new GetDataHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds, false, false, "", ""); + assertEquals( + "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"transferId\":-1}", + body + ); + + handler = + new GetDataHandler( + ip, + null, + sessionId, + investigationIds, + datasetIds, + datafileIds, + false, + false, + "", + "" + ); body = handler.provideTransmissionBody(); - assertEquals("{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"transferId\":-1}", body); + assertEquals( + "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"transferId\":-1}", + body + ); } @Test - public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() + throws Exception { setup(); var handler = new GetIcatUrlHandler(ip); @@ -168,8 +259,8 @@ public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() throws Exceptio } @Test - public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() + throws Exception { setup(); var handler = new GetServiceStatusHandler(ip, sessionId); @@ -178,32 +269,50 @@ public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() throws Ex } @Test - public void testTransmitterBodyForGetStatusRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetStatusRequest_shouldBeOk() + throws Exception { setup(); - var handler = new GetStatusHandler(ip, preparedId, null, null, null, null); + var handler = new GetStatusHandler( + ip, + preparedId, + null, + null, + null, + null + ); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetStatusHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); + handler = + new GetStatusHandler( + ip, + null, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForIsPreparedRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForIsPreparedRequest_shouldBeOk() + throws Exception { setup(); var handler = new IsPreparedHandler(ip, preparedId); String body = handler.provideTransmissionBody(); - assertEquals("{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}", body); + assertEquals( + "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}", + body + ); } @Test - public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() + throws Exception { setup(); var handler = new IsReadOnlyHandler(ip); @@ -212,8 +321,8 @@ public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() throws Exceptio } @Test - public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() + throws Exception { setup(); var handler = new IsTwoLevelHandler(ip); @@ -222,56 +331,98 @@ public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() throws Exceptio } @Test - public void testTransmitterBodyForPrepareDataRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForPrepareDataRequest_shouldBeOk() + throws Exception { setup(); - var handler = new PrepareDataHandler(ip, sessionId, investigationIds, datasetIds, datafileIds, false, false); + var handler = new PrepareDataHandler( + ip, + sessionId, + investigationIds, + datasetIds, + datafileIds, + false, + false + ); String body = handler.provideTransmissionBody(); - assertEquals("{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"preparedId\":\"\"}", body); - + assertEquals( + "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"preparedId\":\"\"}", + body + ); } @Test public void testTransmitterBodyForPutRequest_shouldBeOk() throws Exception { - setup(); - var handler = new PutHandler(ip, sessionId, new ByteArrayInputStream( "".getBytes() ), "someName", datafileIds , datasetIds, "someDescription", "someDOI", "simeCreateTimeString", "someModTimeString", false, false ); + var handler = new PutHandler( + ip, + sessionId, + new ByteArrayInputStream("".getBytes()), + "someName", + datafileIds, + datasetIds, + "someDescription", + "someDOI", + "simeCreateTimeString", + "someModTimeString", + false, + false + ); String body = handler.provideTransmissionBody(); assertEquals("{\"userName\":\"TestUser\",\"datafileId\":-1}", body); } @Test - public void testTransmitterBodyForResetRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForResetRequest_shouldBeOk() + throws Exception { setup(); var handler = new ResetHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new ResetHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); + handler = + new ResetHandler( + ip, + null, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForRestoreRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForRestoreRequest_shouldBeOk() + throws Exception { setup(); - var handler = new RestoreHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new RestoreHandler( + ip, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForWriteRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForWriteRequest_shouldBeOk() + throws Exception { setup(); - var handler = new WriteHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new WriteHandler( + ip, + sessionId, + investigationIds, + datasetIds, + datafileIds + ); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } diff --git a/src/test/java/org/icatproject/ids/ValidaterTest.java b/src/test/java/org/icatproject/ids/ValidaterTest.java index 6dda09ef..f66928cb 100644 --- a/src/test/java/org/icatproject/ids/ValidaterTest.java +++ b/src/test/java/org/icatproject/ids/ValidaterTest.java @@ -2,10 +2,10 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import org.junit.Test; import org.icatproject.ids.exceptions.BadRequestException; import org.icatproject.ids.requestHandlers.base.DataControllerBase; +import org.junit.Test; public class ValidaterTest { @@ -28,8 +28,5 @@ private void testValidUUID(boolean b, String id) { System.out.println(e.getMessage()); assertFalse(b); } - } - } - diff --git a/src/test/java/org/icatproject/ids/integration/BaseTest.java b/src/test/java/org/icatproject/ids/integration/BaseTest.java index 4f0a23d4..45022576 100644 --- a/src/test/java/org/icatproject/ids/integration/BaseTest.java +++ b/src/test/java/org/icatproject/ids/integration/BaseTest.java @@ -1,5 +1,12 @@ package org.icatproject.ids.integration; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import jakarta.json.Json; +import jakarta.json.JsonReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -26,16 +33,6 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; - -import jakarta.json.Json; -import jakarta.json.JsonReader; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import org.junit.Before; - import org.icatproject.Datafile; import org.icatproject.DatafileFormat; import org.icatproject.Dataset; @@ -54,19 +51,22 @@ import org.icatproject.ids.integration.util.client.TestingClient.ServiceStatus; import org.icatproject.ids.integration.util.client.TestingClient.Status; import org.icatproject.ids.services.ICATGetter; +import org.junit.Before; public class BaseTest { public class DeleteVisitor extends SimpleFileVisitor { @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) + throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override - public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { + public FileVisitResult postVisitDirectory(Path dir, IOException e) + throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; @@ -75,21 +75,34 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOExce throw e; } } - } private static long timestamp = System.currentTimeMillis(); - protected Path getDirOnFastStorage(Long dsId) throws IcatException_Exception { - Dataset icatDs = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Investigation", dsId); - return setup.getStorageDir().resolve(Long.toString(icatDs.getInvestigation().getId())) - .resolve(Long.toString(icatDs.getId())); + protected Path getDirOnFastStorage(Long dsId) + throws IcatException_Exception { + Dataset icatDs = (Dataset) icatWS.get( + sessionId, + "Dataset INCLUDE Investigation", + dsId + ); + return setup + .getStorageDir() + .resolve(Long.toString(icatDs.getInvestigation().getId())) + .resolve(Long.toString(icatDs.getId())); } - protected Path getFileOnArchiveStorage(Long dsId) throws IcatException_Exception { - Dataset icatDs = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Investigation", dsId); - return setup.getStorageArchiveDir().resolve(Long.toString(icatDs.getInvestigation().getId())) - .resolve(Long.toString(icatDs.getId())); + protected Path getFileOnArchiveStorage(Long dsId) + throws IcatException_Exception { + Dataset icatDs = (Dataset) icatWS.get( + sessionId, + "Dataset INCLUDE Investigation", + dsId + ); + return setup + .getStorageArchiveDir() + .resolve(Long.toString(icatDs.getInvestigation().getId())) + .resolve(Long.toString(icatDs.getId())); } protected static org.icatproject.ICAT icatWS; @@ -117,7 +130,11 @@ public void before() throws Exception { testingClient = new TestingClient(setup.getIdsUrl()); sessionId = setup.getRootSessionId(); waitForIds(); - populateStorage(setup.isTwoLevel(), setup.getStorageUnit(), setup.getKey()); + populateStorage( + setup.isTwoLevel(), + setup.getStorageUnit(), + setup.getKey() + ); } protected void checkAbsent(Path file) { @@ -158,7 +175,9 @@ protected void logTime(String msg) { long now = System.currentTimeMillis(); if (msg != null) { if (time != 0) { - System.out.println(msg + " took " + (now - time) / 1000. + "s."); + System.out.println( + msg + " took " + (now - time) / 1000. + "s." + ); } else { System.out.println(msg); } @@ -166,7 +185,8 @@ protected void logTime(String msg) { time = now; } - protected void checkZipFile(Path file, List ids, int compressedSize) throws IOException { + protected void checkZipFile(Path file, List ids, int compressedSize) + throws IOException { checkZipStream(Files.newInputStream(file), ids, compressedSize, 0); } @@ -205,13 +225,15 @@ protected byte[] getOutput(InputStream stream) throws IOException { } } - private void populateStorage(boolean twoLevel, String storageUnit, String key) - throws IOException, IcatException_Exception, NoSuchAlgorithmException { + private void populateStorage( + boolean twoLevel, + String storageUnit, + String key + ) throws IOException, IcatException_Exception, NoSuchAlgorithmException { clearStorage(); long timestamp = System.currentTimeMillis(); try { - List objects = icatWS.search(sessionId, "Facility"); List facilities = new ArrayList<>(); for (Object o : objects) { @@ -233,7 +255,9 @@ private void populateStorage(boolean twoLevel, String storageUnit, String key) supportedDatafileFormat.setFacility(fac); supportedDatafileFormat.setName("test_format"); supportedDatafileFormat.setVersion("42.0.0"); - supportedDatafileFormat.setId(icatWS.create(sessionId, supportedDatafileFormat)); + supportedDatafileFormat.setId( + icatWS.create(sessionId, supportedDatafileFormat) + ); InvestigationType invType = new InvestigationType(); invType.setName("Not null"); @@ -277,25 +301,41 @@ private void populateStorage(boolean twoLevel, String storageUnit, String key) df1.setName("a/df1_" + timestamp); df1.setLocation(ds1Loc + UUID.randomUUID()); df1.setDataset(ds1); - writeToFile(df1, "df1 test content very compressible very compressible", key); + writeToFile( + df1, + "df1 test content very compressible very compressible", + key + ); Datafile df2 = new Datafile(); df2.setName("df2_" + timestamp); df2.setLocation(ds1Loc + UUID.randomUUID()); df2.setDataset(ds1); - writeToFile(df2, "df2 test content very compressible very compressible", key); + writeToFile( + df2, + "df2 test content very compressible very compressible", + key + ); Datafile df3 = new Datafile(); df3.setName("df3_" + timestamp); df3.setLocation(ds2Loc + UUID.randomUUID()); df3.setDataset(ds2); - writeToFile(df3, "df3 test content very compressible very compressible", key); + writeToFile( + df3, + "df3 test content very compressible very compressible", + key + ); Datafile df4 = new Datafile(); df4.setName("df4_" + timestamp); df4.setLocation(ds2Loc + "Person's file"); df4.setDataset(ds2); - writeToFile(df4, "df4 test content very compressible very compressible", key); + writeToFile( + df4, + "df4 test content very compressible very compressible", + key + ); datasetIds.add(ds1.getId()); datasetIds.add(ds2.getId()); @@ -311,18 +351,20 @@ private void populateStorage(boolean twoLevel, String storageUnit, String key) moveDatasetToArchive(storageUnit, ds2, ds2Loc, fac, inv, key); } - newFileLocation = setup.getUpdownDir().resolve("new_file_" + timestamp); + newFileLocation = + setup.getUpdownDir().resolve("new_file_" + timestamp); Files.createDirectories(newFileLocation.getParent()); byte[] bytes = "new_file test content".getBytes(); OutputStream out = Files.newOutputStream(newFileLocation); out.write(bytes); out.close(); } catch (IllegalAccessError e) { - System.err.println("Could not prepare ICAT db for testing: " + e.getMessage()); + System.err.println( + "Could not prepare ICAT db for testing: " + e.getMessage() + ); e.printStackTrace(); throw e; } - } protected Map crcs = new HashMap<>(); @@ -331,8 +373,12 @@ private void populateStorage(boolean twoLevel, String storageUnit, String key) protected Map ids = new HashMap<>(); protected Map paths = new HashMap<>(); - protected void checkZipStream(InputStream stream, List datafileIdsIn, long compressedSize, int numLeft) - throws IOException { + protected void checkZipStream( + InputStream stream, + List datafileIdsIn, + long compressedSize, + int numLeft + ) throws IOException { ZipInputStream zis = new ZipInputStream(stream); ZipEntry ze = zis.getNextEntry(); List idsNeeded = new ArrayList<>(datafileIdsIn); @@ -361,7 +407,10 @@ protected void checkStream(InputStream stream, long id) throws IOException { boolean found = false; for (Entry e : ids.entrySet()) { if (id == e.getValue()) { - assertEquals(contents.get(e.getKey()), new String(getOutput(stream))); + assertEquals( + contents.get(e.getKey()), + new String(getOutput(stream)) + ); found = true; break; } @@ -370,7 +419,7 @@ protected void checkStream(InputStream stream, long id) throws IOException { } protected void writeToFile(Datafile df, String content, String key) - throws IOException, IcatException_Exception, NoSuchAlgorithmException { + throws IOException, IcatException_Exception, NoSuchAlgorithmException { Path path = setup.getStorageDir().resolve(df.getLocation()); Files.createDirectories(path.getParent()); byte[] bytes = content.getBytes(); @@ -396,16 +445,42 @@ protected void writeToFile(Datafile df, String content, String key) Dataset ds = df.getDataset(); Investigation inv = ds.getInvestigation(); Facility fac = inv.getFacility(); - paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + URLEncoder.encode(inv.getVisitId(), "UTF-8") - + "/" + ds.getName() + "/" + df.getName(), df.getLocation()); + paths.put( + "ids/" + + fac.getName() + + "/" + + inv.getName() + + "/" + + URLEncoder.encode(inv.getVisitId(), "UTF-8") + + "/" + + ds.getName() + + "/" + + df.getName(), + df.getLocation() + ); } private static final char[] HEX_CHARS = { - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', }; - private String digest(Long id, String location, String key) throws NoSuchAlgorithmException { + private String digest(Long id, String location, String key) + throws NoSuchAlgorithmException { byte[] pattern = (id + location + key).getBytes(); MessageDigest digest = null; digest = MessageDigest.getInstance("SHA-256"); @@ -420,16 +495,29 @@ private String digest(Long id, String location, String key) throws NoSuchAlgorit return new String(hexChars); } - private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, Facility fac, Investigation inv, - String key) throws IOException, IcatException_Exception { - ds = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Datafile", ds.getId()); + private void moveDatasetToArchive( + String storageUnit, + Dataset ds, + String dsLoc, + Facility fac, + Investigation inv, + String key + ) throws IOException, IcatException_Exception { + ds = + (Dataset) icatWS.get( + sessionId, + "Dataset INCLUDE Datafile", + ds.getId() + ); Path top = setup.getStorageDir(); if (storageUnit.equals("DATASET")) { Path zipFile = setup.getStorageArchiveDir().resolve(dsLoc); Files.createDirectories(zipFile.getParent()); - ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile)); + ZipOutputStream zos = new ZipOutputStream( + Files.newOutputStream(zipFile) + ); zos.setLevel(0); for (Datafile df : ds.getDatafiles()) { @@ -437,12 +525,27 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, if (key == null) { file = top.resolve(df.getLocation()); } else { - file = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); + file = + top.resolve( + getLocationFromDigest(df.getId(), df.getLocation()) + ); } InputStream fis = Files.newInputStream(file); - zos.putNextEntry(new ZipEntry("ids/" + fac.getName() + "/" + inv.getName() + "/" - + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" + ds.getName() + "/" + df.getName())); + zos.putNextEntry( + new ZipEntry( + "ids/" + + fac.getName() + + "/" + + inv.getName() + + "/" + + URLEncoder.encode(inv.getVisitId(), "UTF-8") + + "/" + + ds.getName() + + "/" + + df.getName() + ) + ); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { @@ -450,7 +553,6 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, } zos.closeEntry(); fis.close(); - } zos.close(); } else if (storageUnit.equals("DATAFILE")) { @@ -461,9 +563,16 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, if (key == null) { p = top.resolve(df.getLocation()); } else { - p = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); + p = + top.resolve( + getLocationFromDigest(df.getId(), df.getLocation()) + ); } - Files.move(p, archive.resolve(p.getFileName()), StandardCopyOption.REPLACE_EXISTING); + Files.move( + p, + archive.resolve(p.getFileName()), + StandardCopyOption.REPLACE_EXISTING + ); } } for (Datafile df : ds.getDatafiles()) { @@ -471,7 +580,10 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, if (key == null) { file = top.resolve(df.getLocation()); } else { - file = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); + file = + top.resolve( + getLocationFromDigest(df.getId(), df.getLocation()) + ); } Files.deleteIfExists(file); Path parent = file.getParent(); @@ -484,7 +596,6 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, } } } - } protected String getLocationFromDigest(Long id, String locationWithHash) { @@ -508,8 +619,12 @@ protected void raceTest() throws Exception { } logTime("File created"); - Investigation inv = (Investigation) icatWS.search(sessionId, "Investigation").get(0); - DatasetType dst = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); + Investigation inv = (Investigation) icatWS + .search(sessionId, "Investigation") + .get(0); + DatasetType dst = (DatasetType) icatWS + .search(sessionId, "DatasetType") + .get(0); Dataset ds = new Dataset(); ds.setName("Big ds"); @@ -519,17 +634,39 @@ protected void raceTest() throws Exception { ds.setId(dsid); for (int i = 0; i < 30; i++) { - testingClient.put(sessionId, Files.newInputStream(path), "uploaded_file" + i, dsid, - supportedDatafileFormat.getId(), "A rather splendid datafile", 201); + testingClient.put( + sessionId, + Files.newInputStream(path), + "uploaded_file" + i, + dsid, + supportedDatafileFormat.getId(), + "A rather splendid datafile", + 201 + ); } - testingClient.archive(sessionId, new DataSelection().addDataset(dsid), 204); + testingClient.archive( + sessionId, + new DataSelection().addDataset(dsid), + 204 + ); logTime("Put and archive calls"); waitForIds(300); logTime("Archive complete"); - testingClient.restore(sessionId, new DataSelection().addDataset(dsid), 204); - while (testingClient.getStatus(sessionId, new DataSelection().addDataset(dsid), 200) != Status.ONLINE) { + testingClient.restore( + sessionId, + new DataSelection().addDataset(dsid), + 204 + ); + while ( + testingClient.getStatus( + sessionId, + new DataSelection().addDataset(dsid), + 200 + ) != + Status.ONLINE + ) { Thread.sleep(1000); } logTime("Marked online"); @@ -538,57 +675,95 @@ protected void raceTest() throws Exception { assertTrue(stat.getOpItems().toString(), stat.getOpItems().isEmpty()); - testingClient.delete(sessionId, new DataSelection().addDataset(dsid), 204); + testingClient.delete( + sessionId, + new DataSelection().addDataset(dsid), + 204 + ); icatWS.delete(sessionId, ds); waitForIds(300); logTime("Deleted"); Files.delete(path); - } public void getDatafileIdsTest() throws Exception { - - List ids = testingClient.getDatafileIds(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafile(datafileIds.get(0)), 200); + List ids = testingClient.getDatafileIds( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), + 200 + ); assertEquals(2, ids.size()); assertTrue(ids.contains(datafileIds.get(0))); assertTrue(ids.contains(datafileIds.get(1))); - ids = testingClient.getDatafileIds(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 200); + ids = + testingClient.getDatafileIds( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + 200 + ); assertEquals(1, ids.size()); assertTrue(ids.contains(datafileIds.get(0))); - ids = testingClient.getDatafileIds(sessionId, new DataSelection().addInvestigation(investigationId), 200); + ids = + testingClient.getDatafileIds( + sessionId, + new DataSelection().addInvestigation(investigationId), + 200 + ); assertEquals(4, ids.size()); for (Long id : datafileIds) { assertTrue(ids.contains(id)); } - } public void bigDataSelectionTest() throws Exception { String icatUrl = testingClient.getIcatUrl(200).toExternalForm(); ICAT restIcat = new org.icatproject.icat.client.ICAT(icatUrl); - try (JsonReader parser = Json.createReader(new ByteArrayInputStream(restIcat.getProperties().getBytes()))) { - assertEquals("maxEntities must have a fixed value in the icat.server for test to be useful", 20, - parser.readObject().getInt("maxEntities")); + try ( + JsonReader parser = Json.createReader( + new ByteArrayInputStream(restIcat.getProperties().getBytes()) + ) + ) { + assertEquals( + "maxEntities must have a fixed value in the icat.server for test to be useful", + 20, + parser.readObject().getInt("maxEntities") + ); } - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); List idList = new ArrayList<>(); for (int i = 0; i < 45; i++) { - Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp + i, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfid = testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp + i, + datasetIds.get(0), + supportedDatafileFormat.getId(), + "A rather splendid datafile", + 201 + ); idList.add(dfid); } waitForIds(); - List idList2 = testingClient.getDatafileIds(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafile(datafileIds.get(0)), 200); + List idList2 = testingClient.getDatafileIds( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), + 200 + ); assertEquals(47, idList2.size()); for (Long id : idList) { assertTrue(idList2.contains(id)); @@ -605,69 +780,184 @@ public void bigDataSelectionTest() throws Exception { assertTrue(idList2.contains(idList.get(i))); } - idList2 = testingClient.getDatafileIds(sessionId, new DataSelection().addInvestigation(investigationId), 200); + idList2 = + testingClient.getDatafileIds( + sessionId, + new DataSelection().addInvestigation(investigationId), + 200 + ); assertEquals(49, idList2.size()); } public void cloningTest() throws Exception { - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); - assertEquals(104, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 200)); + assertEquals( + 104, + testingClient.getSize( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 200 + ) + ); Session s = icat.getSession(sessionId); Map m = new HashMap<>(); m.put("name", "newOne"); long dfid = datafileIds.get(0); long ndfid = s.cloneEntity("Datafile", dfid, m); - Datafile df = (Datafile) icatWS.get(sessionId, "Datafile df INCLUDE df.dataset.investigation.facility", ndfid); + Datafile df = (Datafile) icatWS.get( + sessionId, + "Datafile df INCLUDE df.dataset.investigation.facility", + ndfid + ); Dataset ds = df.getDataset(); Investigation inv = ds.getInvestigation(); Facility fac = inv.getFacility(); - paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + URLEncoder.encode(inv.getVisitId(), "UTF-8") - + "/" + ds.getName() + "/" + df.getName(), df.getLocation()); + paths.put( + "ids/" + + fac.getName() + + "/" + + inv.getName() + + "/" + + URLEncoder.encode(inv.getVisitId(), "UTF-8") + + "/" + + ds.getName() + + "/" + + df.getName(), + df.getLocation() + ); crcs.put(df.getLocation(), df.getChecksum()); fsizes.put(df.getLocation(), df.getFileSize()); - contents.put(df.getLocation(), "df1 test content very compressible very compressible"); + contents.put( + df.getLocation(), + "df1 test content very compressible very compressible" + ); ids.put(df.getLocation(), df.getId()); - assertEquals(156, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 200)); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, 200)) { + assertEquals( + 156, + testingClient.getSize( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 200 + ) + ); + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 0, + 200 + ) + ) { List dfids = new ArrayList<>(datafileIds.subList(0, 2)); dfids.add(ndfid); checkZipStream(stream, dfids, 57, 0); } long dsid = datasetIds.get(0); assertEquals(2, getDirOnFastStorage(dsid).toFile().list().length); - assertEquals("[3]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); - assertEquals(2, getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); - assertEquals("[2]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(1)), 204); - assertEquals(1, getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); - assertEquals("[1]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); - testingClient.delete(sessionId, new DataSelection().addDatafile(ndfid), 204); + assertEquals( + "[3]", + s.search( + "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + + dsid + ) + ); + testingClient.delete( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + 204 + ); + assertEquals( + 2, + getDirOnFastStorage(datasetIds.get(0)).toFile().list().length + ); + assertEquals( + "[2]", + s.search( + "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + + dsid + ) + ); + testingClient.delete( + sessionId, + new DataSelection().addDatafile(datafileIds.get(1)), + 204 + ); + assertEquals( + 1, + getDirOnFastStorage(datasetIds.get(0)).toFile().list().length + ); + assertEquals( + "[1]", + s.search( + "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + + dsid + ) + ); + testingClient.delete( + sessionId, + new DataSelection().addDatafile(ndfid), + 204 + ); assertFalse(getDirOnFastStorage(datasetIds.get(0)).toFile().exists()); - assertEquals("[0]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); + assertEquals( + "[0]", + s.search( + "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + + dsid + ) + ); } public void reliabilityTest() throws Exception { - - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); + testingClient.restore( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + 204 + ); waitForIds(); - Long dfid1 = testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file2_" + timestamp, - datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", 201); - - testingClient.archive(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); + Long dfid1 = testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + "A rather splendid datafile", + 201 + ); + + testingClient.archive( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + 204 + ); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures().isEmpty()); + assertTrue( + testingClient + .getServiceStatus(sessionId, 200) + .getFailures() + .isEmpty() + ); setup.setReliability(0.); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(dfid1), Flag.NONE, - 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(dfid1), + Flag.NONE, + 200 + ); try { while (!testingClient.isPrepared(preparedId, null)) { @@ -676,38 +966,79 @@ public void reliabilityTest() throws Exception { } catch (Exception e) { System.out.println(e); assertEquals("Restore failed", e.getMessage()); - Set failures = testingClient.getServiceStatus(sessionId, 200).getFailures(); + Set failures = testingClient + .getServiceStatus(sessionId, 200) + .getFailures(); assertEquals(1, failures.size()); setup.setReliability(1.); testingClient.reset(preparedId, 204); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures().isEmpty()); + assertTrue( + testingClient + .getServiceStatus(sessionId, 200) + .getFailures() + .isEmpty() + ); while (!testingClient.isPrepared(preparedId, null)) { Thread.sleep(1000); } } setup.setReliability(0.); - Long dfid2 = testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file3_" + timestamp, - datasetIds.get(0), supportedDatafileFormat.getId(), "An even better datafile", "7.1.3", - new Date(420000), new Date(42000), 201); + Long dfid2 = testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + "An even better datafile", + "7.1.3", + new Date(420000), + new Date(42000), + 201 + ); waitForIds(); - System.out.println(testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 200)); + System.out.println( + testingClient.getStatus( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDatafile(dfid2), + 200 + ) + ); setup.setReliability(1.); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 204); + testingClient.restore( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDatafile(dfid2), + 204 + ); waitForIds(); - System.out.println(testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 200)); - + System.out.println( + testingClient.getStatus( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDatafile(dfid2), + 200 + ) + ); } protected void reliabilityTest2() throws Exception { - DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(0)); + DataSelection dsel = new DataSelection() + .addDatafile(datafileIds.get(0)); testingClient.archive(sessionId, dsel, 204); waitForIds(); setup.setReliability(0.); - String preparedId = testingClient.prepareData(sessionId, dsel, Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + dsel, + Flag.NONE, + 200 + ); try { while (!testingClient.isPrepared(preparedId, null)) { @@ -728,11 +1059,11 @@ protected void reliabilityTest2() throws Exception { while (!testingClient.isPrepared(preparedId, null)) { Thread.sleep(1000); } - } protected void reliabilityTest3() throws Exception { - DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(0)); + DataSelection dsel = new DataSelection() + .addDatafile(datafileIds.get(0)); testingClient.archive(sessionId, dsel, 204); waitForIds(); @@ -740,7 +1071,9 @@ protected void reliabilityTest3() throws Exception { testingClient.restore(sessionId, dsel, 204); try { - while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { + while ( + testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE + ) { Thread.sleep(1000); } fail("Should throw an error"); @@ -751,7 +1084,9 @@ protected void reliabilityTest3() throws Exception { setup.setReliability(1.); testingClient.restore(sessionId, dsel, 204); try { - while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { + while ( + testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE + ) { Thread.sleep(1000); } } catch (Exception e) { @@ -760,19 +1095,24 @@ protected void reliabilityTest3() throws Exception { testingClient.reset(sessionId, dsel, 204); testingClient.restore(sessionId, dsel, 204); - while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { + while ( + testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE + ) { Thread.sleep(1000); } } protected void isPreparedTest() throws Exception { - int numDs = 30; int numDf = 19; - Investigation inv = (Investigation) icatWS.search(sessionId, "Investigation INCLUDE Facility").get(0); + Investigation inv = (Investigation) icatWS + .search(sessionId, "Investigation INCLUDE Facility") + .get(0); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS + .search(sessionId, "DatasetType") + .get(0); Facility fac = inv.getFacility(); String key = setup.getKey(); @@ -794,7 +1134,6 @@ protected void isPreparedTest() throws Exception { String dsLoc = invLoc + dsid + "/"; ds.setId(dsid); for (int j = 0; j < numDf; j++) { - Datafile df = new Datafile(); df.setName("a/df1_" + j); df.setLocation(dsLoc + UUID.randomUUID()); @@ -811,7 +1150,12 @@ protected void isPreparedTest() throws Exception { logTime("Put calls"); - String preparedId = testingClient.prepareData(sessionId, dsel, Flag.ZIP, 200); + String preparedId = testingClient.prepareData( + sessionId, + dsel, + Flag.ZIP, + 200 + ); logTime("Data prepared"); while (!testingClient.isPrepared(preparedId, 200)) { @@ -829,7 +1173,11 @@ protected void isPreparedTest() throws Exception { logTime("Read " + l + " bytes"); } - testingClient.archive(sessionId, new DataSelection().addDataset(dsid), 204); + testingClient.archive( + sessionId, + new DataSelection().addDataset(dsid), + 204 + ); waitForIds(300); logTime(null); @@ -854,5 +1202,4 @@ protected void isPreparedTest() throws Exception { logTime("Read " + l + " bytes"); } } - } diff --git a/src/test/java/org/icatproject/ids/integration/one/BaseTests.java b/src/test/java/org/icatproject/ids/integration/one/BaseTests.java index b86f6faa..54663ed2 100644 --- a/src/test/java/org/icatproject/ids/integration/one/BaseTests.java +++ b/src/test/java/org/icatproject/ids/integration/one/BaseTests.java @@ -1,10 +1,9 @@ package org.icatproject.ids.integration.one; -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; +import org.junit.BeforeClass; +import org.junit.Test; public class BaseTests extends BaseTest { @@ -38,5 +37,4 @@ public void bigDataSelectionTest() throws Exception { public void cloningTest() throws Exception { super.cloningTest(); } - } diff --git a/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java index d6cc867a..70213c1d 100644 --- a/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java @@ -1,13 +1,9 @@ package org.icatproject.ids.integration.one; -import java.io.InputStream; -import java.util.Collections; - import static org.junit.Assert.assertEquals; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import java.io.InputStream; +import java.util.Collections; import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ids.integration.BaseTest; @@ -15,6 +11,9 @@ import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; /* * Issue #63: Internal error is raised trying to restore a dataset @@ -38,21 +37,33 @@ public static void setup() throws Exception { public void createBogusFiles() throws Exception { long timestamp = System.currentTimeMillis(); - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get( + sessionId, + "Dataset", + datasetIds.get(0) + ); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get( + sessionId, + "Dataset", + datasetIds.get(1) + ); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get( + sessionId, + "Dataset", + datasetIds.get(2) + ); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -66,59 +77,76 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); - - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); + + try ( + InputStream stream = testingClient.getData( + sessionId, + selection, + Flag.NONE, + 0, + null + ) + ) { checkZipStream(stream, Collections.emptyList(), 57, 0); } - } @Test public void getSizeEmptyDataset() throws Exception { - - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); - } @Test public void getNonEmptyDataset() throws Exception { - - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); - - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); + + try ( + InputStream stream = testingClient.getData( + sessionId, + selection, + Flag.NONE, + 0, + null + ) + ) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } - } @Test public void getSizeNonEmptyDataset() throws Exception { - - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); - } @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); - - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); + + try ( + InputStream stream = testingClient.getData( + sessionId, + selection, + Flag.NONE, + 0, + 404 + ) + ) { checkZipStream(stream, Collections.emptyList(), 57, 0); } - } @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); - } - } diff --git a/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java index 5f5b4881..93aa25ba 100644 --- a/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java @@ -1,20 +1,19 @@ package org.icatproject.ids.integration.one; -import java.nio.file.Files; -import java.nio.file.Path; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import org.junit.BeforeClass; -import org.junit.Test; +import java.nio.file.Files; +import java.nio.file.Path; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Status; +import org.junit.BeforeClass; +import org.junit.Test; public class DeleteTest extends BaseTest { @@ -26,9 +25,17 @@ public static void setup() throws Exception { @Test public void deleteDatafileTest() throws Exception { - DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(3)); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); + DataSelection dsel = new DataSelection() + .addDatafile(datafileIds.get(3)); + assertEquals( + Status.ONLINE, + testingClient.getStatus(sessionId, dsel, 200) + ); + testingClient.delete( + sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), + 204 + ); try { testingClient.getStatus(sessionId, dsel, 404); fail(); @@ -44,10 +51,16 @@ public void deleteDatafileTest() throws Exception { public void deleteDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); DataSelection dsel = new DataSelection().addDataset(datasetIds.get(1)); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); + assertEquals( + Status.ONLINE, + testingClient.getStatus(sessionId, dsel, 200) + ); assertTrue(Files.exists(dirOnFastStorage)); testingClient.delete(sessionId, dsel, 204); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); + assertEquals( + Status.ONLINE, + testingClient.getStatus(sessionId, dsel, 200) + ); assertFalse(Files.exists(dirOnFastStorage)); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java index 16026ef5..0be497b7 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java @@ -1,12 +1,9 @@ package org.icatproject.ids.integration.one; -import java.io.InputStream; -import java.util.Arrays; - import static org.junit.Assert.assertEquals; -import org.junit.BeforeClass; -import org.junit.Test; +import java.io.InputStream; +import java.util.Arrays; import org.icatproject.Datafile; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; @@ -15,6 +12,8 @@ import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class GetDataExplicitTest extends BaseTest { @@ -29,11 +28,23 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = + (Datafile) icatWS.get( + sessionId, + "Datafile INCLUDE 1", + datafileIds.get(0) + ); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals( + 209L, + testingClient.getSize( + sessionId, + new DataSelection().addDatafiles(datafileIds), + 200 + ) + ); } finally { if (df != null) { df.setFileSize(size); @@ -47,12 +58,23 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = + (Datafile) icatWS.get( + sessionId, + "Datafile INCLUDE 1", + datafileIds.get(0) + ); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection().addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafiles(datafileIds); + String preparedId = testingClient.prepareData( + sessionId, + selection, + Flag.NONE, + 200 + ); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -64,114 +86,252 @@ public void getSizePreparedId() throws Exception { @Test public void getSizes1() throws Exception { - assertEquals(208L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); - assertEquals(208L, testingClient.getSize(sessionId, new DataSelection().addDatasets(datasetIds), 200)); - assertEquals(208L, - testingClient.getSize(sessionId, new DataSelection().addInvestigation(investigationId), 200)); - assertEquals(208L, testingClient.getSize(sessionId, - new DataSelection().addInvestigation(investigationId).addDatafiles(datafileIds), 200)); - assertEquals(0L, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(2)), 200)); + assertEquals( + 208L, + testingClient.getSize( + sessionId, + new DataSelection().addDatafiles(datafileIds), + 200 + ) + ); + assertEquals( + 208L, + testingClient.getSize( + sessionId, + new DataSelection().addDatasets(datasetIds), + 200 + ) + ); + assertEquals( + 208L, + testingClient.getSize( + sessionId, + new DataSelection().addInvestigation(investigationId), + 200 + ) + ); + assertEquals( + 208L, + testingClient.getSize( + sessionId, + new DataSelection() + .addInvestigation(investigationId) + .addDatafiles(datafileIds), + 200 + ) + ); + assertEquals( + 0L, + testingClient.getSize( + sessionId, + new DataSelection().addDataset(datasetIds.get(2)), + 200 + ) + ); } @Test(expected = NotFoundException.class) public void getSizesBad() throws Exception { - testingClient.getSize(sessionId, new DataSelection().addDatafile(563L), 404); + testingClient.getSize( + sessionId, + new DataSelection().addDatafile(563L), + 404 + ); } @Test(expected = NotFoundException.class) public void getSizesBad2() throws Exception { - testingClient.getSize(sessionId, new DataSelection().addDatafile(563L).addDatafile(564L), 404); + testingClient.getSize( + sessionId, + new DataSelection().addDatafile(563L).addDatafile(564L), + 404 + ); } @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0, 400)) { - } + try ( + InputStream z = testingClient.getData( + "bad preparedId format", + 0, + 400 + ) + ) {} } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { - } + try ( + InputStream z = testingClient.getData( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 0, + 403 + ) + ) {} } @Test public void correctBehaviourTestNone() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 0, + 200 + ) + ) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 0, + 200 + ) + ) { checkStream(stream, datafileIds.get(0)); } } @Test public void correctBehaviourTestCompress() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.COMPRESS, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.COMPRESS, + 0, + 200 + ) + ) { checkZipStream(stream, datafileIds, 36L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.COMPRESS, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.COMPRESS, + 0, + 200 + ) + ) { checkStream(stream, datafileIds.get(0)); } } @Test public void correctBehaviourTestZip() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.ZIP, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.ZIP, + 0, + 200 + ) + ) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP, + 0, + 200 + ) + ) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } @Test public void correctBehaviourTestZipAndCompress() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.ZIP_AND_COMPRESS, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.ZIP_AND_COMPRESS, + 0, + 200 + ) + ) { checkZipStream(stream, datafileIds, 36L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP_AND_COMPRESS, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP_AND_COMPRESS, + 0, + 200 + ) + ) { checkZipStream(stream, datafileIds.subList(0, 1), 36L, 0); } } @Test public void correctBehaviourInvestigation() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addInvestigation(investigationId), Flag.NONE, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addInvestigation(investigationId), + Flag.NONE, + 0, + 200 + ) + ) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP, + 0, + 200 + ) + ) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } @Test public void correctBehaviourInvestigations() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addInvestigations(Arrays.asList(investigationId)), Flag.NONE, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection() + .addInvestigations(Arrays.asList(investigationId)), + Flag.NONE, + 0, + 200 + ) + ) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP, + 0, + 200 + ) + ) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } - } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java index 9d844536..294f580b 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java @@ -1,17 +1,16 @@ package org.icatproject.ids.integration.one; -import java.io.InputStream; - import static org.junit.Assert.assertEquals; -import org.junit.BeforeClass; -import org.junit.Test; +import java.io.InputStream; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class GetDataForPreparedIdTest extends BaseTest { @@ -25,43 +24,59 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { - } + try ( + InputStream z = testingClient.getData( + "bad preparedId format", + 0L, + 400 + ) + ) {} } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); do { Thread.sleep(500); } while (!testingClient.isPrepared(preparedId, 200)); - try (InputStream z = testingClient.getData(preparedId, -10L, 400)) { - } + try (InputStream z = testingClient.getData(preparedId, -10L, 400)) {} } @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { - } + try ( + InputStream z = testingClient.getData( + "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + 0L, + 404 + ) + ) {} } @Test public void correctBehaviourNoOffsetTest() throws Exception { - for (Flag flag : Flag.values()) { - - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), flag, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + flag, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { + try ( + InputStream stream = testingClient.getData(preparedId, 0, 200) + ) { if (flag == Flag.NONE || flag == Flag.COMPRESS) { checkStream(stream, datafileIds.get(0)); } else if (flag == Flag.ZIP) { @@ -74,9 +89,14 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() + throws Exception { + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -89,9 +109,12 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -102,10 +125,16 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), Flag.NONE, 200); + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() + throws Exception { + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -118,11 +147,13 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exceptio @Test public void correctBehaviourWithOffsetTest() throws Exception { - for (Flag flag : Flag.values()) { - - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), flag, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + flag, + 200 + ); do { Thread.sleep(1000); @@ -130,16 +161,22 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the file twice, with and without an offset byte[] out = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] outOffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); + byte[] outOffset = getOutput( + testingClient.getData(preparedId, goodOffset, 206) + ); // compare the two zip files byte by byte taking into account the // offset - System.out.println(flag + ": " + out.length + " " + outOffset.length); + System.out.println( + flag + ": " + out.length + " " + outOffset.length + ); for (int i = 0; i < outOffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) outOffset[i], (byte) out[i + goodOffset]); + assertEquals( + "Byte offset: " + i, + (byte) outOffset[i], + (byte) out[i + goodOffset] + ); } } - } - } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java index d5db81b7..9cfa31e5 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java @@ -1,11 +1,8 @@ package org.icatproject.ids.integration.one; -import java.util.Arrays; - import static org.junit.Assert.assertEquals; -import org.junit.BeforeClass; -import org.junit.Test; +import java.util.Arrays; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -14,6 +11,8 @@ import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; import org.icatproject.ids.integration.util.client.TestingClient.Status; +import org.junit.BeforeClass; +import org.junit.Test; public class GetStatusExplicitTest extends BaseTest { @@ -40,39 +39,61 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus( + sessionId, + new DataSelection() + .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), + 404 + ); } @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTestAnon() throws Exception { - testingClient.getStatus(null, new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus( + null, + new DataSelection() + .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), + 404 + ); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.getStatus(setup.getForbiddenSessionId(), new DataSelection().addDatafiles(datafileIds), 403); + testingClient.getStatus( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), + 403 + ); } @Test public void correctBehaviourTest() throws Exception { - Status status; do { Thread.sleep(1000); - status = testingClient.getStatus(sessionId, new DataSelection().addDatafiles(datafileIds), 200); + status = + testingClient.getStatus( + sessionId, + new DataSelection().addDatafiles(datafileIds), + 200 + ); System.out.println("*" + status + "*"); } while (status != Status.ONLINE); - } @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData( + sessionId, + selection, + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } assertEquals(testingClient.getStatus(preparedId, 200), Status.ONLINE); } - } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java index b870eba0..d5d742bc 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java @@ -1,10 +1,6 @@ package org.icatproject.ids.integration.one; import java.util.Arrays; - -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -12,6 +8,8 @@ import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class GetStatusForPreparedIdTest extends BaseTest { @@ -33,38 +31,55 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient - .prepareData(sessionId, - new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), - Flag.NONE, 404); + testingClient.prepareData( + sessionId, + new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), + Flag.NONE, + 404 + ); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, - 404); + testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(99999L), + Flag.NONE, + 404 + ); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient - .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient.prepareData( + sessionId, + new DataSelection().addDataset(99999L), + Flag.NONE, + 404 + ); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.prepareData(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 403); + testingClient.prepareData( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 403 + ); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } } - } diff --git a/src/test/java/org/icatproject/ids/integration/one/MiscTest.java b/src/test/java/org/icatproject/ids/integration/one/MiscTest.java index 3baf012c..0dc25b5a 100644 --- a/src/test/java/org/icatproject/ids/integration/one/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/MiscTest.java @@ -1,18 +1,17 @@ package org.icatproject.ids.integration.one; -import java.io.InputStream; - import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import org.junit.BeforeClass; -import org.junit.Test; +import java.io.InputStream; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.NotImplementedException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class MiscTest extends BaseTest { @@ -41,17 +40,31 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue( + testingClient + .getServiceStatus(sessionId, 200) + .getOpItems() + .isEmpty() + ); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue( + testingClient + .getServiceStatus(sessionId, 200) + .getOpItems() + .isEmpty() + ); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); @@ -60,14 +73,21 @@ public void correctBehaviourNoOffsetTest() throws Exception { @Test(expected = NotImplementedException.class) public void restoreNotAvailableTest() throws Exception { - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 501 + ); waitForIds(); } @Test(expected = NotImplementedException.class) public void archiveNotAvailableTest() throws Exception { - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.archive( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 501 + ); waitForIds(); } - } diff --git a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java index 1943ae01..2c30f38b 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java @@ -5,15 +5,13 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; public class PerformanceTest extends BaseTest { @@ -29,7 +27,12 @@ public static void beforeClass() throws Exception { int n = 100000; start = System.currentTimeMillis(); - try (OutputStream f = Files.newOutputStream(infile, StandardOpenOption.CREATE)) { + try ( + OutputStream f = Files.newOutputStream( + infile, + StandardOpenOption.CREATE + ) + ) { for (int i = 0; i < n; i++) { f.write(junk); } @@ -50,12 +53,26 @@ public static void afterClass() throws Exception { public void putOneFileTest() throws Exception { byte[] junk = new byte[1000]; start = System.currentTimeMillis(); - Long dfid = testingClient.put(sessionId, Files.newInputStream(infile), "big_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "A big datafile", 201); + Long dfid = testingClient.put( + sessionId, + Files.newInputStream(infile), + "big_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + "A big datafile", + 201 + ); ts("store file (put)"); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(dfid), Flag.NONE, 0, - null)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafile(dfid), + Flag.NONE, + 0, + null + ) + ) { boolean first = true; while (stream.read(junk) > 0) { if (first) { @@ -77,14 +94,31 @@ private static void ts(String msg) { public void putAsPostOneFileTest() throws Exception { byte[] junk = new byte[1000]; start = System.currentTimeMillis(); - Long dfid = testingClient.putAsPost(sessionId, Files.newInputStream(infile), "big2_" + timestamp, - datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", null, null, null, - true, 201); + Long dfid = testingClient.putAsPost( + sessionId, + Files.newInputStream(infile), + "big2_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + "A rather splendid datafile", + null, + null, + null, + true, + 201 + ); ts("store file (post)"); int ntot = 0; - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(dfid), Flag.NONE, 0, - null)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafile(dfid), + Flag.NONE, + 0, + null + ) + ) { boolean first = true; int n; diff --git a/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java index acdf7be9..62d50701 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java @@ -1,20 +1,19 @@ package org.icatproject.ids.integration.one; -import java.nio.file.Files; -import java.util.List; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import org.junit.BeforeClass; -import org.junit.Test; +import java.nio.file.Files; +import java.util.List; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class PrepareDataTest extends BaseTest { @@ -26,36 +25,54 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 400); + testingClient.prepareData( + "bad sessionId format", + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 400 + ); } @Test(expected = BadRequestException.class) public void noIdsTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection(), Flag.NONE, 400); - + testingClient.prepareData( + "bad sessionId format", + new DataSelection(), + Flag.NONE, + 400 + ); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); - + testingClient.prepareData( + "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 403 + ); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 200 + ); System.out.println(preparedId); assertNotNull(preparedId); } @Test public void prepareDataset() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 200 + ); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -66,14 +83,21 @@ public void prepareDataset() throws Exception { Thread.sleep(1000); } - assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue( + Files.exists(setup.getPreparedCacheDir().resolve(preparedId)) + ); } @Test public void prepareTwoDatasets() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, + 200 + ); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -85,14 +109,19 @@ public void prepareTwoDatasets() throws Exception { Thread.sleep(1000); } - assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue( + Files.exists(setup.getPreparedCacheDir().resolve(preparedId)) + ); } @Test public void prepareDatafile() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -102,14 +131,21 @@ public void prepareDatafile() throws Exception { Thread.sleep(1000); } - assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue( + Files.exists(setup.getPreparedCacheDir().resolve(preparedId)) + ); } @Test public void prepareDatafileAndItsDataset() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection() + .addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), + Flag.NONE, + 200 + ); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -120,7 +156,8 @@ public void prepareDatafileAndItsDataset() throws Exception { Thread.sleep(1000); } - assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue( + Files.exists(setup.getPreparedCacheDir().resolve(preparedId)) + ); } - } diff --git a/src/test/java/org/icatproject/ids/integration/one/PutTest.java b/src/test/java/org/icatproject/ids/integration/one/PutTest.java index 0a6dec0a..df2ea8cc 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PutTest.java @@ -1,21 +1,20 @@ package org.icatproject.ids.integration.one; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + import java.nio.file.Files; import java.nio.file.Path; import java.util.Date; import java.util.GregorianCalendar; import javax.xml.datatype.DatatypeFactory; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.Datafile; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; +import org.junit.BeforeClass; +import org.junit.Test; public class PutTest extends BaseTest { @@ -29,18 +28,30 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void putDirectoryTest() throws Exception { - testingClient.put(sessionId, null, "junk", datasetIds.get(0), - supportedDatafileFormat.getId(), "", 201); + testingClient.put( + sessionId, + null, + "junk", + datasetIds.get(0), + supportedDatafileFormat.getId(), + "", + 201 + ); } @Test public void putOneFileTest() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfid = testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + "A rather splendid datafile", + 201 + ); Datafile df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("A rather splendid datafile", df.getDescription()); @@ -49,9 +60,19 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileModTime()); assertTrue(Files.exists(dirOnFastStorage)); - dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); + dfid = + testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + "An even better datafile", + "7.1.3", + new Date(420000), + new Date(42000), + 201 + ); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -59,22 +80,34 @@ public void putOneFileTest() throws Exception { DatatypeFactory datatypeFactory = DatatypeFactory.newInstance(); GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTime(new Date(420000)); - assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileCreateTime()); + assertEquals( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileCreateTime() + ); gregorianCalendar.setTime(new Date(42000)); - assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileModTime()); - + assertEquals( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileModTime() + ); } @Test public void putAsPostOneFileTest() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - Long dfid = testingClient.putAsPost(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", null, null, null, true, 201); + Long dfid = testingClient.putAsPost( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + "A rather splendid datafile", + null, + null, + null, + true, + 201 + ); Datafile df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("A rather splendid datafile", df.getDescription()); @@ -83,9 +116,20 @@ public void putAsPostOneFileTest() throws Exception { assertNull(df.getDatafileModTime()); assertTrue(Files.exists(dirOnFastStorage)); - dfid = testingClient.putAsPost(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "An even better datafile", "7.1.3", new Date(420000), new Date(42000), false, 201); + dfid = + testingClient.putAsPost( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + "An even better datafile", + "7.1.3", + new Date(420000), + new Date(42000), + false, + 201 + ); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -93,11 +137,14 @@ public void putAsPostOneFileTest() throws Exception { DatatypeFactory datatypeFactory = DatatypeFactory.newInstance(); GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTime(new Date(420000)); - assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileCreateTime()); + assertEquals( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileCreateTime() + ); gregorianCalendar.setTime(new Date(42000)); - assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileModTime()); - + assertEquals( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileModTime() + ); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/WriteTest.java b/src/test/java/org/icatproject/ids/integration/one/WriteTest.java index 9a7550a3..5d06360e 100644 --- a/src/test/java/org/icatproject/ids/integration/one/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/WriteTest.java @@ -1,12 +1,11 @@ package org.icatproject.ids.integration.one; -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.NotImplementedException; +import org.junit.BeforeClass; +import org.junit.Test; public class WriteTest extends BaseTest { @@ -18,7 +17,10 @@ public static void setup() throws Exception { @Test(expected = NotImplementedException.class) public void writeNotAvailableTest() throws Exception { - testingClient.write(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.write( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 501 + ); } - } diff --git a/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java b/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java index d5bf557e..59cac582 100644 --- a/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java @@ -1,19 +1,18 @@ package org.icatproject.ids.integration.two; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Arrays; - import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import org.junit.BeforeClass; -import org.junit.Test; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; +import org.junit.BeforeClass; +import org.junit.Test; public class ArchiveTest extends BaseTest { @@ -25,28 +24,37 @@ public static void setup() throws Exception { @Test public void restoreThenArchiveDataset() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); - } @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.archive("bad sessionId format", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 400); + testingClient.archive( + "bad sessionId format", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), + 400 + ); } @Test @@ -56,8 +64,10 @@ public void noIdsTest() throws Exception { @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.archive("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 403); + testingClient.archive( + "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), + 403 + ); } - } diff --git a/src/test/java/org/icatproject/ids/integration/two/BaseTests.java b/src/test/java/org/icatproject/ids/integration/two/BaseTests.java index 5207604f..aabf59f9 100644 --- a/src/test/java/org/icatproject/ids/integration/two/BaseTests.java +++ b/src/test/java/org/icatproject/ids/integration/two/BaseTests.java @@ -1,12 +1,11 @@ package org.icatproject.ids.integration.two; +import org.icatproject.ids.integration.BaseTest; +import org.icatproject.ids.integration.util.Setup; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; -import org.icatproject.ids.integration.BaseTest; -import org.icatproject.ids.integration.util.Setup; - public class BaseTests extends BaseTest { @BeforeClass @@ -60,5 +59,4 @@ public void cloningTest() throws Exception { public void isPreparedTest() throws Exception { super.isPreparedTest(); } - } diff --git a/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java index 3ff9adf1..2bbd70cb 100644 --- a/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java @@ -1,5 +1,9 @@ package org.icatproject.ids.integration.two; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.io.File; import java.io.InputStream; import java.nio.file.Files; @@ -7,14 +11,6 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ids.integration.BaseTest; @@ -23,6 +19,9 @@ import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; import org.icatproject.ids.integration.util.client.TestingClient.Status; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; /* * Issue #63: Internal error is raised trying to restore a dataset @@ -46,22 +45,33 @@ public static void setup() throws Exception { @Before public void createBogusFiles() throws Exception { - - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get( + sessionId, + "Dataset", + datasetIds.get(0) + ); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get( + sessionId, + "Dataset", + datasetIds.get(1) + ); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get( + sessionId, + "Dataset", + datasetIds.get(2) + ); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -71,74 +81,97 @@ public void createBogusFiles() throws Exception { datafileIds.add(dfb1.getId()); datafileIds.add(dfb2.getId()); datafileIds.add(dfb3.getId()); - } @Test public void getEmptyDataset() throws Exception { - - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); - - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); + + try ( + InputStream stream = testingClient.getData( + sessionId, + selection, + Flag.NONE, + 0, + null + ) + ) { checkZipStream(stream, Collections.emptyList(), 57, 0); } - } @Test public void getSizeEmptyDataset() throws Exception { - - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); - } @Test public void getNonEmptyDataset() throws Exception { - - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); testingClient.restore(sessionId, selection, 204); waitForIds(); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, null)); - - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + assertEquals( + Status.ONLINE, + testingClient.getStatus(sessionId, selection, null) + ); + + try ( + InputStream stream = testingClient.getData( + sessionId, + selection, + Flag.NONE, + 0, + null + ) + ) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } - } @Test public void getSizeNonEmptyDataset() throws Exception { - - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); - } @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.restore(sessionId, selection, 404); waitForIds(); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, 404)); - - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { + assertEquals( + Status.ONLINE, + testingClient.getStatus(sessionId, selection, 404) + ); + + try ( + InputStream stream = testingClient.getData( + sessionId, + selection, + Flag.NONE, + 0, + 404 + ) + ) { checkZipStream(stream, Collections.emptyList(), 57, 0); } - } @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); - } /* @@ -148,7 +181,6 @@ public void getSizeBogusFile() throws Exception { */ @Test public void putWriteArchiveRestore() throws Exception { - Long dsId = datasetIds.get(0); DataSelection selection = new DataSelection().addDataset(dsId); Path dirOnFastStorage = getDirOnFastStorage(dsId); @@ -157,9 +189,15 @@ public void putWriteArchiveRestore() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfulId = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfulId = testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, + dsId, + supportedDatafileFormat.getId(), + "A rather splendid datafile", + 201 + ); Datafile dful = (Datafile) icatWS.get(sessionId, "Datafile", dfulId); testingClient.archive(sessionId, selection, 204); waitForIds(); @@ -172,16 +210,25 @@ public void putWriteArchiveRestore() throws Exception { File[] filesList = dirOnFastStorage.toFile().listFiles(); assertEquals(3, filesList.length); Set locations = new HashSet<>(); - Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(0)); + Datafile df1 = (Datafile) icatWS.get( + sessionId, + "Datafile", + datafileIds.get(0) + ); locations.add(getLocationFromDigest(df1.getId(), df1.getLocation())); - Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(1)); + Datafile df2 = (Datafile) icatWS.get( + sessionId, + "Datafile", + datafileIds.get(1) + ); locations.add(getLocationFromDigest(df2.getId(), df2.getLocation())); locations.add(getLocationFromDigest(dful.getId(), dful.getLocation())); for (File file : filesList) { - String location = setup.getStorageDir().relativize(file.toPath()).toString(); + String location = setup + .getStorageDir() + .relativize(file.toPath()) + .toString(); assertTrue(locations.contains(location)); } - } - } diff --git a/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java index d8179493..cb86f3b1 100644 --- a/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java @@ -1,14 +1,12 @@ package org.icatproject.ids.integration.two; import java.nio.file.Path; - -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataNotOnlineException; import org.icatproject.ids.integration.util.client.DataSelection; +import org.junit.BeforeClass; +import org.junit.Test; public class DeleteTest extends BaseTest { @@ -20,20 +18,31 @@ public static void setup() throws Exception { @Test(expected = DataNotOnlineException.class) public void deleteFromUnrestoredDataset() throws Exception { - testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 503); + testingClient.delete( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 503 + ); } @Test public void deleteDatafileFromRestoredDatasetTest() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(1)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); + testingClient.delete( + sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), + 204 + ); waitForIds(); checkZipFile(fileOnArchiveStorage, datafileIds.subList(2, 3), 36); @@ -44,15 +53,22 @@ public void deleteRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(1)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); checkPresent(fileOnArchiveStorage); - testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.delete( + sessionId, + new DataSelection().addDataset(datasetIds.get(1)), + 204 + ); waitForIds(); checkAbsent(dirOnFastStorage); checkAbsent(fileOnArchiveStorage); } - } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java index 1a3970a0..4e658722 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java @@ -1,12 +1,9 @@ package org.icatproject.ids.integration.two; -import java.io.InputStream; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -import org.junit.BeforeClass; -import org.junit.Test; +import java.io.InputStream; import org.icatproject.Datafile; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; @@ -16,6 +13,8 @@ import org.icatproject.ids.integration.util.client.IdsException; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class GetDataExplicitTest extends BaseTest { @@ -31,15 +30,26 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { - } + try ( + InputStream z = testingClient.getData( + "bad preparedId format", + 0L, + 400 + ) + ) {} } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { - } + try ( + InputStream z = testingClient.getData( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 0, + 403 + ) + ) {} } @Test @@ -47,11 +57,23 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = + (Datafile) icatWS.get( + sessionId, + "Datafile INCLUDE 1", + datafileIds.get(0) + ); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals( + 209L, + testingClient.getSize( + sessionId, + new DataSelection().addDatafiles(datafileIds), + 200 + ) + ); } finally { if (df != null) { df.setFileSize(size); @@ -65,12 +87,23 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = + (Datafile) icatWS.get( + sessionId, + "Datafile INCLUDE 1", + datafileIds.get(0) + ); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection().addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafiles(datafileIds); + String preparedId = testingClient.prepareData( + sessionId, + selection, + Flag.NONE, + 200 + ); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -82,17 +115,30 @@ public void getSizePreparedId() throws Exception { @Test public void correctBehaviourTest() throws Exception { - - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.NONE, - 0, 503)) { + try ( + InputStream z = testingClient.getData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 0, + 503 + ) + ) { fail("Should have thrown exception"); } catch (IdsException e) { assertEquals(DataNotOnlineException.class, e.getClass()); } while (true) { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 0, null)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 0, + null + ) + ) { checkZipStream(stream, datafileIds, 57, 0); break; } catch (IdsException e) { @@ -104,27 +150,45 @@ public void correctBehaviourTest() throws Exception { @Test public void gettingDatafileRestoresItsDatasetTest() throws Exception { - - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)), - Flag.NONE, 0, null)) { + try ( + InputStream z = testingClient.getData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(2)), + Flag.NONE, + 0, + null + ) + ) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), - Flag.NONE, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), + Flag.NONE, + 0, + 200 + ) + ) { checkStream(stream, datafileIds.get(3)); } - } @Test public void gettingDatasetUsesCacheTest() throws Exception { - - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, null)) { + try ( + InputStream z = testingClient.getData( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 0, + null + ) + ) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -132,28 +196,49 @@ public void gettingDatasetUsesCacheTest() throws Exception { waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 0, + 200 + ) + ) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } - } @Test - public void gettingDatafileAndDatasetShouldRestoreBothDatasetsTest() throws Exception { - - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)) - .addDataset(datasetIds.get(0)), Flag.NONE, 0, 503)) { + public void gettingDatafileAndDatasetShouldRestoreBothDatasetsTest() + throws Exception { + try ( + InputStream z = testingClient.getData( + sessionId, + new DataSelection() + .addDatafile(datafileIds.get(2)) + .addDataset(datasetIds.get(0)), + Flag.NONE, + 0, + 503 + ) + ) { fail("Should throw exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatasets(datasetIds), - Flag.NONE, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatasets(datasetIds), + Flag.NONE, + 0, + 200 + ) + ) { checkZipStream(stream, datafileIds, 57, 0); } } - } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java index 97f66d5a..93ea1695 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java @@ -1,17 +1,16 @@ package org.icatproject.ids.integration.two; -import java.io.InputStream; - import static org.junit.Assert.assertEquals; -import org.junit.BeforeClass; -import org.junit.Test; +import java.io.InputStream; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class GetDataForPreparedIdTest extends BaseTest { @@ -25,34 +24,50 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { - } + try ( + InputStream z = testingClient.getData( + "bad preparedId format", + 0L, + 400 + ) + ) {} } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - try (InputStream z = testingClient.getData(preparedId, -10L, 400)) { - } + try (InputStream z = testingClient.getData(preparedId, -10L, 400)) {} } @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { - } + try ( + InputStream z = testingClient.getData( + "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + 0L, + 404 + ) + ) {} } @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -64,9 +79,14 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() + throws Exception { + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -75,14 +95,16 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkZipStream(stream, datafileIds, 57, 0); } - } @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -94,10 +116,16 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafiles(datafileIds), Flag.NONE, 200); + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() + throws Exception { + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -110,9 +138,12 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exceptio @Test public void correctBehaviourWithOffsetTest() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -120,13 +151,18 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the zip file twice, with and without an offset byte[] zip = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] zipoffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); + byte[] zipoffset = getOutput( + testingClient.getData(preparedId, goodOffset, 206) + ); // compare the two zip files byte by byte taking into account the offset System.out.println(zip.length + " " + zipoffset.length); for (int i = 0; i < zipoffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) zipoffset[i], (byte) zip[i + goodOffset]); + assertEquals( + "Byte offset: " + i, + (byte) zipoffset[i], + (byte) zip[i + goodOffset] + ); } } - } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java index 8ecd4429..8cab8c9f 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java @@ -1,11 +1,8 @@ package org.icatproject.ids.integration.two; -import java.util.Arrays; - import static org.junit.Assert.assertEquals; -import org.junit.BeforeClass; -import org.junit.Test; +import java.util.Arrays; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; @@ -13,6 +10,8 @@ import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; import org.icatproject.ids.integration.util.client.TestingClient.Status; +import org.junit.BeforeClass; +import org.junit.Test; public class GetStatusExplicitTest extends BaseTest { @@ -34,20 +33,30 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, - new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus( + sessionId, + new DataSelection() + .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), + 404 + ); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.getStatus(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), 403); + testingClient.getStatus( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), + 403 + ); } @Test public void correctBehaviourTest() throws Exception { - Status status = testingClient.getStatus(sessionId, - new DataSelection().addDatafiles(datafileIds), 200); + Status status = testingClient.getStatus( + sessionId, + new DataSelection().addDatafiles(datafileIds), + 200 + ); assertEquals(status, Status.ARCHIVED); waitForIds(); assertEquals(status, Status.ARCHIVED); @@ -55,8 +64,14 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData( + sessionId, + selection, + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } @@ -71,19 +86,29 @@ public void getStatusPreparedIdTest() throws Exception { @Test public void restoringDatafileRestoresItsDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - Status status = testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 200); + Status status = testingClient.getStatus( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 200 + ); assertEquals(Status.ONLINE, status); - status = testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 200); + status = + testingClient.getStatus( + sessionId, + new DataSelection().addDataset(datasetIds.get(1)), + 200 + ); assertEquals(Status.ARCHIVED, status); } - } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java index c7daebea..409e8cfa 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java @@ -1,10 +1,6 @@ package org.icatproject.ids.integration.two; import java.util.Arrays; - -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -12,6 +8,8 @@ import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class GetStatusForPreparedIdTest extends BaseTest { @@ -33,37 +31,54 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient - .prepareData(sessionId, - new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), - Flag.NONE, 404); + testingClient.prepareData( + sessionId, + new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), + Flag.NONE, + 404 + ); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, - 404); + testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(99999L), + Flag.NONE, + 404 + ); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient - .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient.prepareData( + sessionId, + new DataSelection().addDataset(99999L), + Flag.NONE, + 404 + ); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.prepareData(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 403); + testingClient.prepareData( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 403 + ); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } } - } diff --git a/src/test/java/org/icatproject/ids/integration/two/MiscTest.java b/src/test/java/org/icatproject/ids/integration/two/MiscTest.java index fa5751d8..a186f0db 100644 --- a/src/test/java/org/icatproject/ids/integration/two/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/MiscTest.java @@ -1,17 +1,16 @@ package org.icatproject.ids.integration.two; -import java.io.InputStream; - import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import org.junit.BeforeClass; -import org.junit.Test; +import java.io.InputStream; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class MiscTest extends BaseTest { @@ -40,23 +39,41 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); - - assertFalse(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); + + assertFalse( + testingClient + .getServiceStatus(sessionId, 200) + .getOpItems() + .isEmpty() + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue( + testingClient + .getServiceStatus(sessionId, 200) + .getOpItems() + .isEmpty() + ); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue( + testingClient + .getServiceStatus(sessionId, 200) + .getOpItems() + .isEmpty() + ); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); } - } } diff --git a/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java index 891da989..a660f5cc 100644 --- a/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java @@ -1,20 +1,19 @@ package org.icatproject.ids.integration.two; -import java.nio.file.Path; -import java.util.List; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import org.junit.BeforeClass; -import org.junit.Test; +import java.nio.file.Path; +import java.util.List; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class PrepareDataTest extends BaseTest { @@ -28,8 +27,12 @@ public static void setup() throws Exception { public void prepareArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 200 + ); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -45,12 +48,17 @@ public void prepareArchivedDataset() throws Exception { @Test public void prepareTwoArchivedDatasets() throws Exception { - Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, + 200 + ); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -70,8 +78,12 @@ public void prepareTwoArchivedDatasets() throws Exception { public void prepareArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -88,8 +100,14 @@ public void prepareArchivedDatafile() throws Exception { public void prepareArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -105,26 +123,42 @@ public void prepareArchivedDatafileAndItsDataset() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 400); + testingClient.prepareData( + "bad sessionId format", + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 400 + ); } @Test public void noIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, 200); + testingClient.prepareData( + sessionId, + new DataSelection(), + Flag.NONE, + 200 + ); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); - + testingClient.prepareData( + "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 403 + ); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 200 + ); assertNotNull(preparedId); } @@ -132,14 +166,22 @@ public void correctBehaviourTest() throws Exception { public void prepareRestoredDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -151,20 +193,30 @@ public void prepareRestoredDataset() throws Exception { @Test public void prepareTwoRestoredDatasets() throws Exception { - Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), 204); + testingClient.restore( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage1); checkPresent(dirOnFastStorage2); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -176,17 +228,24 @@ public void prepareTwoRestoredDatasets() throws Exception { @Test public void prepareRestoredDatafile() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -198,23 +257,31 @@ public void prepareRestoredDatafile() throws Exception { @Test public void prepareRestoredDatafileAndItsDataset() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection() + .addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection() + .addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } checkPresent(setup.getPreparedCacheDir().resolve(preparedId)); - } - } diff --git a/src/test/java/org/icatproject/ids/integration/two/PutTest.java b/src/test/java/org/icatproject/ids/integration/two/PutTest.java index 3ca3f8dd..43712d0c 100644 --- a/src/test/java/org/icatproject/ids/integration/two/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/PutTest.java @@ -1,23 +1,22 @@ package org.icatproject.ids.integration.two; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Date; -import java.util.GregorianCalendar; -import javax.xml.datatype.DatatypeFactory; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import org.junit.BeforeClass; -import org.junit.Test; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Date; +import java.util.GregorianCalendar; +import javax.xml.datatype.DatatypeFactory; import org.icatproject.Datafile; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataNotOnlineException; import org.icatproject.ids.integration.util.client.DataSelection; +import org.junit.BeforeClass; +import org.junit.Test; public class PutTest extends BaseTest { @@ -31,9 +30,15 @@ public static void setup() throws Exception { @Test(expected = DataNotOnlineException.class) public void putToUnrestoredDataset() throws Exception { - - testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file1_" - + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), null, 503); + testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file1_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + null, + 503 + ); } @Test @@ -41,15 +46,25 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfid = testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + "A rather splendid datafile", + 201 + ); waitForIds(); @@ -59,9 +74,19 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileCreateTime()); assertNull(df.getDatafileModTime()); - dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); + dfid = + testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + "An even better datafile", + "7.1.3", + new Date(420000), + new Date(42000), + 201 + ); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -69,22 +94,28 @@ public void putOneFileTest() throws Exception { DatatypeFactory datatypeFactory = DatatypeFactory.newInstance(); GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTime(new Date(420000)); - assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileCreateTime()); + assertEquals( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileCreateTime() + ); gregorianCalendar.setTime(new Date(42000)); - assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileModTime()); + assertEquals( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileModTime() + ); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); - } - } diff --git a/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java b/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java index 5122232e..f921996b 100644 --- a/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java @@ -1,5 +1,8 @@ package org.icatproject.ids.integration.two; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + /* * Test various error conditions in the DsRestorer caused by ZIP files * in archive storage having unexpected content. @@ -11,17 +14,13 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InternalException; import org.icatproject.ids.integration.util.client.TestingClient.Status; +import org.junit.BeforeClass; +import org.junit.Test; public class RestoreErrorsTest extends BaseTest { @@ -37,14 +36,25 @@ public static void setup() throws Exception { * occur. */ private enum Defect { - NONE, MISSING_ENTRY, SPURIOUS_ENTRY, DUPLICATE_ENTRY + NONE, + MISSING_ENTRY, + SPURIOUS_ENTRY, + DUPLICATE_ENTRY, } private void cloneZip(Path archivepath, Defect defect) throws IOException { Path savepath = archivepath.getParent().resolve(".sav"); Files.move(archivepath, savepath); - try (ZipOutputStream zipout = new ZipOutputStream(Files.newOutputStream(archivepath))) { - try (ZipInputStream zipin = new ZipInputStream(Files.newInputStream(savepath))) { + try ( + ZipOutputStream zipout = new ZipOutputStream( + Files.newOutputStream(archivepath) + ) + ) { + try ( + ZipInputStream zipin = new ZipInputStream( + Files.newInputStream(savepath) + ) + ) { ZipEntry entry = zipin.getNextEntry(); boolean first = true; String entryName = ""; diff --git a/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java b/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java index 2755fc9e..45709a01 100644 --- a/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java @@ -1,13 +1,11 @@ package org.icatproject.ids.integration.two; import java.nio.file.Path; - -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; +import org.junit.BeforeClass; +import org.junit.Test; public class RestoreTest extends BaseTest { @@ -19,23 +17,30 @@ public static void setup() throws Exception { @Test public void restoreArchivedDataset() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); - } @Test public void restoreTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), 204); + testingClient.restore( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage1); @@ -44,27 +49,32 @@ public void restoreTwoArchivedDatasets() throws Exception { @Test public void restoreArchivedDatafile() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); - } @Test public void restoreArchivedDatafileAndItsDataset() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection() + .addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); - } } diff --git a/src/test/java/org/icatproject/ids/integration/two/WriteTest.java b/src/test/java/org/icatproject/ids/integration/two/WriteTest.java index c9367a43..9bc5f6e0 100644 --- a/src/test/java/org/icatproject/ids/integration/two/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/WriteTest.java @@ -4,10 +4,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.UUID; - -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.DatasetType; @@ -18,6 +14,8 @@ import org.icatproject.ids.integration.util.client.DataNotOnlineException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; +import org.junit.BeforeClass; +import org.junit.Test; public class WriteTest extends BaseTest { @@ -33,7 +31,6 @@ public static void setup() throws Exception { */ @Test public void restoreThenWriteDataset() throws Exception { - Long dsId = datasetIds.get(0); Path dirOnFastStorage = getDirOnFastStorage(dsId); DataSelection selection = new DataSelection().addDataset(dsId); @@ -43,7 +40,6 @@ public void restoreThenWriteDataset() throws Exception { checkPresent(dirOnFastStorage); testingClient.write(sessionId, selection, 204); - } /** @@ -53,12 +49,17 @@ public void restoreThenWriteDataset() throws Exception { */ @Test public void storeThenWrite() throws Exception { - long timestamp = System.currentTimeMillis(); - Investigation inv = (Investigation) icatWS.get(sessionId, "Investigation INCLUDE Facility", investigationId); + Investigation inv = (Investigation) icatWS.get( + sessionId, + "Investigation INCLUDE Facility", + investigationId + ); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS + .search(sessionId, "DatasetType") + .get(0); Dataset ds = new Dataset(); ds.setName("dsWrite_" + timestamp); @@ -72,7 +73,11 @@ public void storeThenWrite() throws Exception { df.setName("dfWrite_" + timestamp); df.setLocation(dsLoc + UUID.randomUUID()); df.setDataset(ds); - writeToFile(df, "some really boring datafile test content", setup.getKey()); + writeToFile( + df, + "some really boring datafile test content", + setup.getKey() + ); Long dsId = ds.getId(); Path dirOnFastStorage = getDirOnFastStorage(dsId); @@ -87,7 +92,6 @@ public void storeThenWrite() throws Exception { ArrayList list = new ArrayList(); list.add(df.getId()); checkZipFile(fileOnArchiveStorage, list, 42); - } /** @@ -106,8 +110,11 @@ public void notOnlineTest() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.write("bad sessionId format", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 400); + testingClient.write( + "bad sessionId format", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), + 400 + ); } @Test @@ -117,8 +124,10 @@ public void noIdsTest() throws Exception { @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.write("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 403); + testingClient.write( + "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), + 403 + ); } - } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java b/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java index 75557414..6cd575f0 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java @@ -1,19 +1,18 @@ package org.icatproject.ids.integration.twodf; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Arrays; - import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import org.junit.BeforeClass; -import org.junit.Test; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; +import org.junit.BeforeClass; +import org.junit.Test; public class ArchiveTest extends BaseTest { @@ -25,28 +24,37 @@ public static void setup() throws Exception { @Test public void restoreThenArchiveDataset() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); - } @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.archive("bad sessionId format", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 400); + testingClient.archive( + "bad sessionId format", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), + 400 + ); } @Test @@ -56,8 +64,10 @@ public void noIdsTest() throws Exception { @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.archive("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 403); + testingClient.archive( + "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), + 403 + ); } - } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/BaseTests.java b/src/test/java/org/icatproject/ids/integration/twodf/BaseTests.java index aa9d2157..c47c7ba3 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/BaseTests.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/BaseTests.java @@ -1,12 +1,11 @@ package org.icatproject.ids.integration.twodf; +import org.icatproject.ids.integration.BaseTest; +import org.icatproject.ids.integration.util.Setup; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; -import org.icatproject.ids.integration.BaseTest; -import org.icatproject.ids.integration.util.Setup; - public class BaseTests extends BaseTest { @BeforeClass @@ -60,5 +59,4 @@ public void cloningTest() throws Exception { public void isPreparedTest() throws Exception { super.isPreparedTest(); } - } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java index 670a071a..983aaa36 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java @@ -1,5 +1,9 @@ package org.icatproject.ids.integration.twodf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.io.File; import java.io.InputStream; import java.nio.file.Files; @@ -7,14 +11,6 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ids.integration.BaseTest; @@ -23,6 +19,9 @@ import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; import org.icatproject.ids.integration.util.client.TestingClient.Status; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; /* * Issue #63: Internal error is raised trying to restore a dataset @@ -46,22 +45,33 @@ public static void setup() throws Exception { @Before public void createBogusFiles() throws Exception { - - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get( + sessionId, + "Dataset", + datasetIds.get(0) + ); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get( + sessionId, + "Dataset", + datasetIds.get(1) + ); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get( + sessionId, + "Dataset", + datasetIds.get(2) + ); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -71,74 +81,97 @@ public void createBogusFiles() throws Exception { datafileIds.add(dfb1.getId()); datafileIds.add(dfb2.getId()); datafileIds.add(dfb3.getId()); - } @Test public void getEmptyDataset() throws Exception { - - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); - - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); + + try ( + InputStream stream = testingClient.getData( + sessionId, + selection, + Flag.NONE, + 0, + null + ) + ) { checkZipStream(stream, Collections.emptyList(), 57, 0); } - } @Test public void getSizeEmptyDataset() throws Exception { - - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); - } @Test public void getNonEmptyDataset() throws Exception { - - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); testingClient.restore(sessionId, selection, 204); waitForIds(); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, null)); - - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + assertEquals( + Status.ONLINE, + testingClient.getStatus(sessionId, selection, null) + ); + + try ( + InputStream stream = testingClient.getData( + sessionId, + selection, + Flag.NONE, + 0, + null + ) + ) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } - } @Test public void getSizeNonEmptyDataset() throws Exception { - - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); - } @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.restore(sessionId, selection, 404); waitForIds(); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, 404)); - - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { + assertEquals( + Status.ONLINE, + testingClient.getStatus(sessionId, selection, 404) + ); + + try ( + InputStream stream = testingClient.getData( + sessionId, + selection, + Flag.NONE, + 0, + 404 + ) + ) { checkZipStream(stream, Collections.emptyList(), 57, 0); } - } @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); - } /* @@ -148,7 +181,6 @@ public void getSizeBogusFile() throws Exception { */ @Test public void putWriteArchiveRestore() throws Exception { - Long dsId = datasetIds.get(0); DataSelection selection = new DataSelection().addDataset(dsId); Path dirOnFastStorage = getDirOnFastStorage(dsId); @@ -157,9 +189,15 @@ public void putWriteArchiveRestore() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfulId = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfulId = testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, + dsId, + supportedDatafileFormat.getId(), + "A rather splendid datafile", + 201 + ); Datafile dful = (Datafile) icatWS.get(sessionId, "Datafile", dfulId); testingClient.archive(sessionId, selection, 204); waitForIds(); @@ -172,16 +210,25 @@ public void putWriteArchiveRestore() throws Exception { File[] filesList = dirOnFastStorage.toFile().listFiles(); assertEquals(3, filesList.length); Set locations = new HashSet<>(); - Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(0)); + Datafile df1 = (Datafile) icatWS.get( + sessionId, + "Datafile", + datafileIds.get(0) + ); locations.add(getLocationFromDigest(df1.getId(), df1.getLocation())); - Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(1)); + Datafile df2 = (Datafile) icatWS.get( + sessionId, + "Datafile", + datafileIds.get(1) + ); locations.add(getLocationFromDigest(df2.getId(), df2.getLocation())); locations.add(getLocationFromDigest(dful.getId(), dful.getLocation())); for (File file : filesList) { - String location = setup.getStorageDir().relativize(file.toPath()).toString(); + String location = setup + .getStorageDir() + .relativize(file.toPath()) + .toString(); assertTrue(locations.contains(location)); } - } - } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java index 063102fb..a5474857 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java @@ -1,13 +1,11 @@ package org.icatproject.ids.integration.twodf; import java.nio.file.Path; - -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; +import org.junit.BeforeClass; +import org.junit.Test; public class DeleteTest extends BaseTest { @@ -19,19 +17,30 @@ public static void setup() throws Exception { @Test public void deleteFromUnrestoredDataset() throws Exception { - testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.delete( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); } @Test public void deleteDatafileFromRestoredDatasetTest() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(1)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); + testingClient.delete( + sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), + 204 + ); waitForIds(); } @@ -40,15 +49,22 @@ public void deleteRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(1)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); checkPresent(fileOnArchiveStorage); - testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.delete( + sessionId, + new DataSelection().addDataset(datasetIds.get(1)), + 204 + ); waitForIds(); checkAbsent(dirOnFastStorage); checkAbsent(fileOnArchiveStorage); } - } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java index b6d9e772..7a85bfaf 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java @@ -1,12 +1,9 @@ package org.icatproject.ids.integration.twodf; -import java.io.InputStream; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -import org.junit.BeforeClass; -import org.junit.Test; +import java.io.InputStream; import org.icatproject.Datafile; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; @@ -16,6 +13,8 @@ import org.icatproject.ids.integration.util.client.IdsException; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class GetDataExplicitTest extends BaseTest { @@ -31,15 +30,26 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { - } + try ( + InputStream z = testingClient.getData( + "bad preparedId format", + 0L, + 400 + ) + ) {} } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { - } + try ( + InputStream z = testingClient.getData( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 0, + 403 + ) + ) {} } @Test @@ -47,11 +57,23 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = + (Datafile) icatWS.get( + sessionId, + "Datafile INCLUDE 1", + datafileIds.get(0) + ); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals( + 209L, + testingClient.getSize( + sessionId, + new DataSelection().addDatafiles(datafileIds), + 200 + ) + ); } finally { if (df != null) { df.setFileSize(size); @@ -65,12 +87,23 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = + (Datafile) icatWS.get( + sessionId, + "Datafile INCLUDE 1", + datafileIds.get(0) + ); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection().addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafiles(datafileIds); + String preparedId = testingClient.prepareData( + sessionId, + selection, + Flag.NONE, + 200 + ); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -82,18 +115,30 @@ public void getSizePreparedId() throws Exception { @Test public void correctBehaviourTest() throws Exception { - - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.NONE, - 0, 503)) { - + try ( + InputStream z = testingClient.getData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 0, + 503 + ) + ) { fail("Should have thrown exception"); } catch (IdsException e) { assertEquals(DataNotOnlineException.class, e.getClass()); } while (true) { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 0, null)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 0, + null + ) + ) { checkZipStream(stream, datafileIds, 57, 0); break; } catch (IdsException e) { @@ -105,29 +150,47 @@ public void correctBehaviourTest() throws Exception { @Test public void gettingDatafileDoesNotRestoreItsDatasetTest() throws Exception { - - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)), - Flag.NONE, 0, 503)) { + try ( + InputStream z = testingClient.getData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(2)), + Flag.NONE, + 0, + 503 + ) + ) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), - Flag.NONE, 0, 503)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), + Flag.NONE, + 0, + 503 + ) + ) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } - } @Test public void gettingDatasetUsesCacheTest() throws Exception { - - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, 503)) { + try ( + InputStream z = testingClient.getData( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 0, + 503 + ) + ) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -135,30 +198,51 @@ public void gettingDatasetUsesCacheTest() throws Exception { waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, 200)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 0, + 200 + ) + ) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } - } @Test - public void gettingDatafileAndDatasetShouldNotRestoreBothDatasetsTest() throws Exception { - - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)) - .addDataset(datasetIds.get(0)), Flag.NONE, 0, 503)) { + public void gettingDatafileAndDatasetShouldNotRestoreBothDatasetsTest() + throws Exception { + try ( + InputStream z = testingClient.getData( + sessionId, + new DataSelection() + .addDatafile(datafileIds.get(2)) + .addDataset(datasetIds.get(0)), + Flag.NONE, + 0, + 503 + ) + ) { fail("Should throw exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatasets(datasetIds), - Flag.NONE, 0, 503)) { + try ( + InputStream stream = testingClient.getData( + sessionId, + new DataSelection().addDatasets(datasetIds), + Flag.NONE, + 0, + 503 + ) + ) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } } - } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java index c0fa6eef..9c054e27 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java @@ -1,17 +1,16 @@ package org.icatproject.ids.integration.twodf; -import java.io.InputStream; - import static org.junit.Assert.assertEquals; -import org.junit.BeforeClass; -import org.junit.Test; +import java.io.InputStream; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class GetDataForPreparedIdTest extends BaseTest { @@ -25,34 +24,50 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { - } + try ( + InputStream z = testingClient.getData( + "bad preparedId format", + 0L, + 400 + ) + ) {} } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - try (InputStream z = testingClient.getData(preparedId, -10L, 400)) { - } + try (InputStream z = testingClient.getData(preparedId, -10L, 400)) {} } @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { - } + try ( + InputStream z = testingClient.getData( + "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + 0L, + 404 + ) + ) {} } @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -64,9 +79,14 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() + throws Exception { + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -75,14 +95,16 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkZipStream(stream, datafileIds, 57, 0); } - } @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -94,10 +116,16 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), Flag.NONE, 200); + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() + throws Exception { + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -110,9 +138,12 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exceptio @Test public void correctBehaviourWithOffsetTest() throws Exception { - - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -120,13 +151,18 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the zip file twice, with and without an offset byte[] zip = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] zipoffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); + byte[] zipoffset = getOutput( + testingClient.getData(preparedId, goodOffset, 206) + ); // compare the two zip files byte by byte taking into account the offset System.out.println(zip.length + " " + zipoffset.length); for (int i = 0; i < zipoffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) zipoffset[i], (byte) zip[i + goodOffset]); + assertEquals( + "Byte offset: " + i, + (byte) zipoffset[i], + (byte) zip[i + goodOffset] + ); } } - } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java index ed6bed60..07cd73fb 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java @@ -1,11 +1,8 @@ package org.icatproject.ids.integration.twodf; -import java.util.Arrays; - import static org.junit.Assert.assertEquals; -import org.junit.BeforeClass; -import org.junit.Test; +import java.util.Arrays; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; @@ -13,6 +10,8 @@ import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; import org.icatproject.ids.integration.util.client.TestingClient.Status; +import org.junit.BeforeClass; +import org.junit.Test; public class GetStatusExplicitTest extends BaseTest { @@ -34,20 +33,30 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, - new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus( + sessionId, + new DataSelection() + .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), + 404 + ); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.getStatus(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), 403); + testingClient.getStatus( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), + 403 + ); } @Test public void correctBehaviourTest() throws Exception { - Status status = testingClient.getStatus(sessionId, - new DataSelection().addDatafiles(datafileIds), 200); + Status status = testingClient.getStatus( + sessionId, + new DataSelection().addDatafiles(datafileIds), + 200 + ); assertEquals(status, Status.ARCHIVED); waitForIds(); assertEquals(status, Status.ARCHIVED); @@ -55,8 +64,14 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData( + sessionId, + selection, + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } @@ -70,20 +85,31 @@ public void getStatusPreparedIdTest() throws Exception { } @Test - public void restoringDatafileDoesNotRestoresItsDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, 200); + public void restoringDatafileDoesNotRestoresItsDatasetTest() + throws Exception { + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - Status status = testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 200); + Status status = testingClient.getStatus( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 200 + ); assertEquals(Status.ARCHIVED, status); - status = testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 200); + status = + testingClient.getStatus( + sessionId, + new DataSelection().addDataset(datasetIds.get(1)), + 200 + ); assertEquals(Status.ARCHIVED, status); } - } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java index 7889492e..041b9002 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java @@ -1,10 +1,6 @@ package org.icatproject.ids.integration.twodf; import java.util.Arrays; - -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -12,6 +8,8 @@ import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class GetStatusForPreparedIdTest extends BaseTest { @@ -33,37 +31,54 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient - .prepareData(sessionId, - new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), - Flag.NONE, 404); + testingClient.prepareData( + sessionId, + new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), + Flag.NONE, + 404 + ); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, - 404); + testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(99999L), + Flag.NONE, + 404 + ); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient - .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient.prepareData( + sessionId, + new DataSelection().addDataset(99999L), + Flag.NONE, + 404 + ); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.prepareData(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 403); + testingClient.prepareData( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 403 + ); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } } - } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java b/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java index 011de0ad..530d1527 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java @@ -1,17 +1,16 @@ package org.icatproject.ids.integration.twodf; -import java.io.InputStream; - import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import org.junit.BeforeClass; -import org.junit.Test; +import java.io.InputStream; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class MiscTest extends BaseTest { @@ -40,8 +39,12 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); var status = testingClient.getServiceStatus(sessionId, 200); //System.out.println("### twodf.MiscTest.correctBehaviourNoOffsetTest - status: " + status.toString()); @@ -52,14 +55,23 @@ public void correctBehaviourNoOffsetTest() throws Exception { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue( + testingClient + .getServiceStatus(sessionId, 200) + .getOpItems() + .isEmpty() + ); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue( + testingClient + .getServiceStatus(sessionId, 200) + .getOpItems() + .isEmpty() + ); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); } - } } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java index 386a7fff..72df6190 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java @@ -1,20 +1,19 @@ package org.icatproject.ids.integration.twodf; -import java.nio.file.Path; -import java.util.List; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import org.junit.BeforeClass; -import org.junit.Test; +import java.nio.file.Path; +import java.util.List; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; +import org.junit.BeforeClass; +import org.junit.Test; public class PrepareDataTest extends BaseTest { @@ -28,8 +27,12 @@ public static void setup() throws Exception { public void prepareArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 200 + ); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -45,12 +48,17 @@ public void prepareArchivedDataset() throws Exception { @Test public void prepareTwoArchivedDatasets() throws Exception { - Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, + 200 + ); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -70,8 +78,12 @@ public void prepareTwoArchivedDatasets() throws Exception { public void prepareArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -88,8 +100,14 @@ public void prepareArchivedDatafile() throws Exception { public void prepareArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -105,26 +123,42 @@ public void prepareArchivedDatafileAndItsDataset() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 400); + testingClient.prepareData( + "bad sessionId format", + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 400 + ); } @Test public void noIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, 200); + testingClient.prepareData( + sessionId, + new DataSelection(), + Flag.NONE, + 200 + ); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); - + testingClient.prepareData( + "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 403 + ); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafiles(datafileIds), + Flag.NONE, + 200 + ); assertNotNull(preparedId); } @@ -132,14 +166,22 @@ public void correctBehaviourTest() throws Exception { public void prepareRestoredDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -151,20 +193,30 @@ public void prepareRestoredDataset() throws Exception { @Test public void prepareTwoRestoredDatasets() throws Exception { - Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), 204); + testingClient.restore( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage1); checkPresent(dirOnFastStorage2); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -176,17 +228,24 @@ public void prepareTwoRestoredDatasets() throws Exception { @Test public void prepareRestoredDatafile() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -198,23 +257,31 @@ public void prepareRestoredDatafile() throws Exception { @Test public void prepareRestoredDatafileAndItsDataset() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection() + .addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient.prepareData( + sessionId, + new DataSelection() + .addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), + Flag.NONE, + 200 + ); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } checkPresent(setup.getPreparedCacheDir().resolve(preparedId)); - } - } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java b/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java index b960cd6e..b4b71d28 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java @@ -1,22 +1,21 @@ package org.icatproject.ids.integration.twodf; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Date; -import java.util.GregorianCalendar; -import javax.xml.datatype.DatatypeFactory; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import org.junit.BeforeClass; -import org.junit.Test; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Date; +import java.util.GregorianCalendar; +import javax.xml.datatype.DatatypeFactory; import org.icatproject.Datafile; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; +import org.junit.BeforeClass; +import org.junit.Test; public class PutTest extends BaseTest { @@ -31,8 +30,15 @@ public static void setup() throws Exception { @Test // Works fine for datafile storage public void putToUnrestoredDataset() throws Exception { - testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file1_" - + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), null, 201); + testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file1_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + null, + 201 + ); } @Test @@ -40,15 +46,25 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfid = testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + "A rather splendid datafile", + 201 + ); waitForIds(); @@ -58,9 +74,19 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileCreateTime()); assertNull(df.getDatafileModTime()); - dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); + dfid = + testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, + datasetIds.get(0), + supportedDatafileFormat.getId(), + "An even better datafile", + "7.1.3", + new Date(420000), + new Date(42000), + 201 + ); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -68,22 +94,28 @@ public void putOneFileTest() throws Exception { DatatypeFactory datatypeFactory = DatatypeFactory.newInstance(); GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTime(new Date(420000)); - assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileCreateTime()); + assertEquals( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileCreateTime() + ); gregorianCalendar.setTime(new Date(42000)); - assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileModTime()); + assertEquals( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileModTime() + ); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); - } - } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java b/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java index baf7be8d..47450757 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java @@ -1,15 +1,14 @@ package org.icatproject.ids.integration.twodf; -import java.nio.file.Files; -import java.nio.file.Path; - import static org.junit.Assert.assertTrue; -import org.junit.BeforeClass; -import org.junit.Test; +import java.nio.file.Files; +import java.nio.file.Path; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; +import org.junit.BeforeClass; +import org.junit.Test; public class QueueTest extends BaseTest { @@ -36,11 +35,16 @@ public void multiOperationTest() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + testingClient.put( + sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, + dsId, + supportedDatafileFormat.getId(), + "A rather splendid datafile", + 201 + ); testingClient.archive(sessionId, selection, 204); waitForIds(); } - } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java b/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java index 7125b9a4..0bef830a 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java @@ -1,13 +1,11 @@ package org.icatproject.ids.integration.twodf; import java.nio.file.Path; - -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; +import org.junit.BeforeClass; +import org.junit.Test; public class RestoreTest extends BaseTest { @@ -20,7 +18,11 @@ public static void setup() throws Exception { @Test public void restoreArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); } @@ -29,8 +31,13 @@ public void restoreArchivedDataset() throws Exception { public void restoreTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); + testingClient.restore( + sessionId, + new DataSelection() + .addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage1); @@ -39,27 +46,32 @@ public void restoreTwoArchivedDatasets() throws Exception { @Test public void restoreArchivedDatafile() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); - } @Test public void restoreArchivedDatafileAndItsDataset() throws Exception { - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); + testingClient.restore( + sessionId, + new DataSelection() + .addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), + 204 + ); waitForIds(); checkPresent(dirOnFastStorage); - } } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java b/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java index 876af417..ca55e365 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java @@ -3,10 +3,6 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.UUID; - -import org.junit.BeforeClass; -import org.junit.Test; - import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.DatasetType; @@ -17,6 +13,8 @@ import org.icatproject.ids.integration.util.client.DataNotOnlineException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; +import org.junit.BeforeClass; +import org.junit.Test; public class WriteTest extends BaseTest { @@ -32,7 +30,6 @@ public static void setup() throws Exception { */ @Test public void restoreThenWriteDataset() throws Exception { - Long dsId = datasetIds.get(0); Path dirOnFastStorage = getDirOnFastStorage(dsId); DataSelection selection = new DataSelection().addDataset(dsId); @@ -42,7 +39,6 @@ public void restoreThenWriteDataset() throws Exception { checkPresent(dirOnFastStorage); testingClient.write(sessionId, selection, 204); - } /** @@ -52,12 +48,17 @@ public void restoreThenWriteDataset() throws Exception { */ @Test public void storeThenWrite() throws Exception { - long timestamp = System.currentTimeMillis(); - Investigation inv = (Investigation) icatWS.get(sessionId, "Investigation INCLUDE Facility", investigationId); + Investigation inv = (Investigation) icatWS.get( + sessionId, + "Investigation INCLUDE Facility", + investigationId + ); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS + .search(sessionId, "DatasetType") + .get(0); Dataset ds = new Dataset(); ds.setName("dsWrite_" + timestamp); @@ -71,7 +72,11 @@ public void storeThenWrite() throws Exception { df.setName("dfWrite_" + timestamp); df.setLocation(dsLoc + UUID.randomUUID()); df.setDataset(ds); - writeToFile(df, "some really boring datafile test content", setup.getKey()); + writeToFile( + df, + "some really boring datafile test content", + setup.getKey() + ); Long dsId = ds.getId(); Path dirOnFastStorage = getDirOnFastStorage(dsId); @@ -83,7 +88,6 @@ public void storeThenWrite() throws Exception { testingClient.write(sessionId, selection, 204); waitForIds(); checkPresent(dirOnArchiveStorage); - } /** @@ -102,8 +106,11 @@ public void notOnlineTest() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.write("bad sessionId format", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 400); + testingClient.write( + "bad sessionId format", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), + 400 + ); } @Test @@ -113,8 +120,10 @@ public void noIdsTest() throws Exception { @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.write("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 403); + testingClient.write( + "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), + 403 + ); } - } diff --git a/src/test/java/org/icatproject/ids/integration/util/Setup.java b/src/test/java/org/icatproject/ids/integration/util/Setup.java index 2392805d..da06d235 100644 --- a/src/test/java/org/icatproject/ids/integration/util/Setup.java +++ b/src/test/java/org/icatproject/ids/integration/util/Setup.java @@ -8,7 +8,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Properties; - import org.icatproject.ICAT; import org.icatproject.ids.TestUtils; import org.icatproject.ids.services.ICATGetter; @@ -47,7 +46,6 @@ public String getStorageUnit() { private String key; public Setup(String runPropertyFile) throws Exception { - // Test home directory String testHome = System.getProperty("testHome"); if (testHome == null) { @@ -57,11 +55,17 @@ public Setup(String runPropertyFile) throws Exception { // Start by reading the test properties Properties testProps = new Properties(); - InputStream is = Setup.class.getClassLoader().getResourceAsStream("test.properties"); + InputStream is = + Setup.class.getClassLoader().getResourceAsStream("test.properties"); try { testProps.load(is); } catch (Exception e) { - System.err.println("Problem loading test.properties: " + e.getClass() + " " + e.getMessage()); + System.err.println( + "Problem loading test.properties: " + + e.getClass() + + " " + + e.getMessage() + ); } setReliability(1.); @@ -72,16 +76,29 @@ public Setup(String runPropertyFile) throws Exception { String containerHome = System.getProperty("containerHome"); if (containerHome == null) { - System.err.println("containerHome is not defined as a system property"); + System.err.println( + "containerHome is not defined as a system property" + ); } long time = System.currentTimeMillis(); - ShellCommand sc = new ShellCommand("src/test/scripts/prepare_test.py", "src/test/resources/" + runPropertyFile, - home.toString(), containerHome, serverUrl); + ShellCommand sc = new ShellCommand( + "src/test/scripts/prepare_test.py", + "src/test/resources/" + runPropertyFile, + home.toString(), + containerHome, + serverUrl + ); System.out.println(sc.getStdout() + " " + sc.getStderr()); System.out.println( - "Setting up " + runPropertyFile + " took " + (System.currentTimeMillis() - time) / 1000. + "seconds"); + "Setting up " + + runPropertyFile + + " took " + + (System.currentTimeMillis() - time) / + 1000. + + "seconds" + ); // Having set up the ids.properties file read it find other things CheckedProperties runProperties = new CheckedProperties(); @@ -92,8 +109,10 @@ public Setup(String runPropertyFile) throws Exception { updownDir = home.resolve(testProps.getProperty("updownDir")); icatUrl = runProperties.getURL("icat.url"); ICAT icat = ICATGetter.getService(icatUrl.toString()); - rootSessionId = TestUtils.login(icat, testProps.getProperty("login.root")); - forbiddenSessionId = TestUtils.login(icat, testProps.getProperty("login.unauthorized")); + rootSessionId = + TestUtils.login(icat, testProps.getProperty("login.root")); + forbiddenSessionId = + TestUtils.login(icat, testProps.getProperty("login.unauthorized")); storageDir = runProperties.getPath("plugin.main.dir"); @@ -111,7 +130,6 @@ public Setup(String runPropertyFile) throws Exception { } storageUnit = storageUnitString.toUpperCase(); } - } public void setReliability(double d) throws IOException { @@ -156,5 +174,4 @@ public URL getIcatUrl() { public String getKey() { return key; } - } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/BadRequestException.java b/src/test/java/org/icatproject/ids/integration/util/client/BadRequestException.java index 5f592906..a5bf70d2 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/BadRequestException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/BadRequestException.java @@ -6,5 +6,4 @@ public class BadRequestException extends IdsException { public BadRequestException(String msg) { super(msg); } - } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/DataNotOnlineException.java b/src/test/java/org/icatproject/ids/integration/util/client/DataNotOnlineException.java index 37cb084e..d928792c 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/DataNotOnlineException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/DataNotOnlineException.java @@ -6,5 +6,4 @@ public class DataNotOnlineException extends IdsException { public DataNotOnlineException(String msg) { super(msg); } - } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java b/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java index 6527c191..97c31eac 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java @@ -2,7 +2,6 @@ import java.io.FilterInputStream; import java.io.IOException; - import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.impl.client.CloseableHttpClient; @@ -17,8 +16,10 @@ public class HttpInputStream extends FilterInputStream { * @param httpclient the CloseableHttpClient to close * @param response the response to use and ultimately close */ - public HttpInputStream(CloseableHttpClient httpclient, CloseableHttpResponse response) - throws IllegalStateException, IOException { + public HttpInputStream( + CloseableHttpClient httpclient, + CloseableHttpResponse response + ) throws IllegalStateException, IOException { super(response.getEntity().getContent()); this.response = response; this.httpclient = httpclient; @@ -53,5 +54,4 @@ public void close() throws IOException { throw exception; } } - } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/IdsException.java b/src/test/java/org/icatproject/ids/integration/util/client/IdsException.java index 0169a56e..34b1add2 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/IdsException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/IdsException.java @@ -6,5 +6,4 @@ public class IdsException extends Exception { public IdsException(String msg) { super(msg); } - } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/InsufficientPrivilegesException.java b/src/test/java/org/icatproject/ids/integration/util/client/InsufficientPrivilegesException.java index be6dcdd7..5b4a4108 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/InsufficientPrivilegesException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/InsufficientPrivilegesException.java @@ -6,5 +6,4 @@ public class InsufficientPrivilegesException extends IdsException { public InsufficientPrivilegesException(String msg) { super(msg); } - } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/InsufficientStorageException.java b/src/test/java/org/icatproject/ids/integration/util/client/InsufficientStorageException.java index c09bfba1..db6b2a20 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/InsufficientStorageException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/InsufficientStorageException.java @@ -6,5 +6,4 @@ public class InsufficientStorageException extends IdsException { public InsufficientStorageException(String msg) { super(msg); } - } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/InternalException.java b/src/test/java/org/icatproject/ids/integration/util/client/InternalException.java index fcc71b3e..74485718 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/InternalException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/InternalException.java @@ -6,5 +6,4 @@ public class InternalException extends IdsException { public InternalException(String msg) { super(msg); } - } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/NotFoundException.java b/src/test/java/org/icatproject/ids/integration/util/client/NotFoundException.java index 521baf31..7f84f619 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/NotFoundException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/NotFoundException.java @@ -6,5 +6,4 @@ public class NotFoundException extends IdsException { public NotFoundException(String msg) { super(msg); } - } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/NotImplementedException.java b/src/test/java/org/icatproject/ids/integration/util/client/NotImplementedException.java index 7e12007d..3f335584 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/NotImplementedException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/NotImplementedException.java @@ -6,5 +6,4 @@ public class NotImplementedException extends IdsException { public NotImplementedException(String msg) { super(msg); } - } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java b/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java index 7bb22543..2b257cd3 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java @@ -1,5 +1,13 @@ package org.icatproject.ids.integration.util.client; +import static org.junit.Assert.fail; + +import jakarta.json.Json; +import jakarta.json.JsonException; +import jakarta.json.JsonNumber; +import jakarta.json.JsonObject; +import jakarta.json.JsonReader; +import jakarta.json.JsonValue; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; @@ -17,14 +25,6 @@ import java.util.Set; import java.util.zip.CRC32; import java.util.zip.CheckedInputStream; - -import jakarta.json.Json; -import jakarta.json.JsonException; -import jakarta.json.JsonNumber; -import jakarta.json.JsonObject; -import jakarta.json.JsonReader; -import jakarta.json.JsonValue; - import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; @@ -48,20 +48,25 @@ import org.apache.http.util.EntityUtils; import org.icatproject.ids.enums.RequestIdNames; -import static org.junit.Assert.fail; - public class TestingClient { public enum Flag { - COMPRESS, NONE, ZIP, ZIP_AND_COMPRESS + COMPRESS, + NONE, + ZIP, + ZIP_AND_COMPRESS, } public enum Method { - DELETE, GET, POST, PUT + DELETE, + GET, + POST, + PUT, } public enum ParmPos { - BODY, URL + BODY, + URL, } public class ServiceStatus { @@ -107,52 +112,68 @@ void storeFailure(Long id) { public String toString() { String result = "lockCount: " + this.lockCount + ", "; result += "lockedIDs: " + lockedDs.size() + "\n"; - for(long lockedId : lockedDs) result += "\t" + lockedId + "\n"; + for (long lockedId : lockedDs) result += "\t" + lockedId + "\n"; result += "opItems: " + opItems.size() + "\n"; - for(String key : opItems.keySet()) result += "\t" + key + ": " + opItems.get(key) + "\n"; + for (String key : opItems.keySet()) result += + "\t" + key + ": " + opItems.get(key) + "\n"; result += "failures: " + failures.size() + "\n"; - for(long fail : failures) result += " " + fail; + for (long fail : failures) result += " " + fail; result += "\n"; return result; } - } public enum Status { - ARCHIVED, ONLINE, RESTORING + ARCHIVED, + ONLINE, + RESTORING, } private String basePath; private URI idsUri; public TestingClient(URL idsUrl) { - try { - idsUri = new URI(idsUrl.getProtocol(), null, idsUrl.getHost(), idsUrl.getPort(), null, null, null); + idsUri = + new URI( + idsUrl.getProtocol(), + null, + idsUrl.getHost(), + idsUrl.getPort(), + null, + null, + null + ); basePath = idsUrl.getPath(); } catch (URISyntaxException e) { throw new RuntimeException(e); } - } - public void archive(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, - BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { - + public void archive(String sessionId, DataSelection data, Integer sc) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { URI uri = getUri(getUriBuilder("archive")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId) + ); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue()) + ); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(formparams)); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try ( + CloseableHttpResponse response = httpclient.execute(httpPost) + ) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -160,8 +181,7 @@ public void archive(String sessionId, DataSelection data, Integer sc) throws Not } private void checkStatus(HttpResponse response, Integer sc) - throws InternalException, BadRequestException, DataNotOnlineException, ParseException, IOException, - InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException { + throws InternalException, BadRequestException, DataNotOnlineException, ParseException, IOException, InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException { StatusLine status = response.getStatusLine(); if (status == null) { throw new InternalException("Status line returned is empty"); @@ -175,8 +195,7 @@ private void checkStatus(HttpResponse response, Integer sc) } else { fail("rc was " + rc + " " + EntityUtils.toString(entity)); } - } else - fail("Expected " + sc.intValue() + " but was " + rc); + } else fail("Expected " + sc.intValue() + " but was " + rc); } if (rc / 100 != 2) { HttpEntity entity = response.getEntity(); @@ -188,7 +207,11 @@ private void checkStatus(HttpResponse response, Integer sc) } String code; String message; - try (JsonReader jsonReader = Json.createReader(new StringReader(error))) { + try ( + JsonReader jsonReader = Json.createReader( + new StringReader(error) + ) + ) { JsonObject json = jsonReader.readObject(); code = json.getString("code"); message = json.getString("message"); @@ -223,13 +246,10 @@ private void checkStatus(HttpResponse response, Integer sc) throw new NotImplementedException(message); } } - } public void delete(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, - NotFoundException, DataNotOnlineException { - + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("delete"); uriBuilder.addParameter(RequestIdNames.sessionId, sessionId); for (Entry entry : data.getParameters().entrySet()) { @@ -239,48 +259,66 @@ public void delete(String sessionId, DataSelection data, Integer sc) try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpDelete httpDelete = new HttpDelete(uri); - try (CloseableHttpResponse response = httpclient.execute(httpDelete)) { + try ( + CloseableHttpResponse response = httpclient.execute(httpDelete) + ) { expectNothing(response, sc); } catch (InsufficientStorageException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - private void expectNothing(CloseableHttpResponse response, Integer sc) throws InternalException, - BadRequestException, DataNotOnlineException, ParseException, InsufficientPrivilegesException, - NotImplementedException, InsufficientStorageException, NotFoundException, IOException { + private void expectNothing(CloseableHttpResponse response, Integer sc) + throws InternalException, BadRequestException, DataNotOnlineException, ParseException, InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException, IOException { checkStatus(response, sc); HttpEntity entity = response.getEntity(); if (entity != null) { if (!EntityUtils.toString(entity).isEmpty()) { - throw new InternalException("No http entity expected in response"); + throw new InternalException( + "No http entity expected in response" + ); } } } - public URL getIcatUrl(int sc) throws InternalException, ParseException, NotImplementedException { + public URL getIcatUrl(int sc) + throws InternalException, ParseException, NotImplementedException { URI uri = getUri(getUriBuilder("getIcatUrl")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return new URL(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch ( + IOException + | InsufficientStorageException + | DataNotOnlineException + | BadRequestException + | InsufficientPrivilegesException + | NotFoundException e + ) { + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public InputStream getData(String sessionId, DataSelection data, Flag flags, long offset, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, - InternalException, DataNotOnlineException { - + public InputStream getData( + String sessionId, + DataSelection data, + Flag flags, + long offset, + Integer sc + ) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("getData"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); for (Entry entry : data.getParameters().entrySet()) { @@ -330,9 +368,7 @@ public InputStream getData(String sessionId, DataSelection data, Flag flags, lon } public InputStream getData(String preparedId, long offset, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, - InternalException, DataNotOnlineException { - + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("getData"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); URI uri = getUri(uriBuilder); @@ -372,8 +408,7 @@ public InputStream getData(String preparedId, long offset, Integer sc) } public ServiceStatus getServiceStatus(String sessionId, Integer sc) - throws InternalException, InsufficientPrivilegesException, NotImplementedException { - + throws InternalException, InsufficientPrivilegesException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getServiceStatus"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); URI uri = getUri(uriBuilder); @@ -384,7 +419,11 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); String result = getString(response, sc); - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + try ( + JsonReader jsonReader = Json.createReader( + new StringReader(result) + ) + ) { ServiceStatus serviceStatus = new ServiceStatus(); JsonObject rootNode = jsonReader.readObject(); for (JsonValue on : rootNode.getJsonArray("opsQueue")) { @@ -394,7 +433,9 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) } serviceStatus.setLockedCount(rootNode.getInt("lockCount")); for (JsonValue lock : rootNode.getJsonArray("locks")) { - Long dsId = ((JsonObject) lock).getJsonNumber("id").longValueExact(); + Long dsId = + ((JsonObject) lock).getJsonNumber("id") + .longValueExact(); serviceStatus.storeLockedDs(dsId); } for (JsonValue num : rootNode.getJsonArray("failures")) { @@ -404,21 +445,32 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) return serviceStatus; } catch (JsonException e) { throw new InternalException( - "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); + "TestingClient " + + e.getClass() + + " " + + e.getMessage() + + " from " + + result + ); } - - } catch (InsufficientStorageException | DataNotOnlineException | InternalException | BadRequestException - | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch ( + InsufficientStorageException + | DataNotOnlineException + | InternalException + | BadRequestException + | NotFoundException e + ) { + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public long getSize(String sessionId, DataSelection data, int sc) throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, NotImplementedException { - + public long getSize(String sessionId, DataSelection data, int sc) + throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getSize"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); for (Entry entry : data.getParameters().entrySet()) { @@ -431,17 +483,22 @@ public long getSize(String sessionId, DataSelection data, int sc) throws BadRequ try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Long.parseLong(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch ( + IOException + | InsufficientStorageException + | DataNotOnlineException e + ) { + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public long getSize(String preparedId, int sc) throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, NotImplementedException { - + public long getSize(String preparedId, int sc) + throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getSize"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); URI uri = getUri(uriBuilder); @@ -450,17 +507,22 @@ public long getSize(String preparedId, int sc) throws BadRequestException, NotFo HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { return Long.parseLong(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch ( + IOException + | InsufficientStorageException + | DataNotOnlineException e + ) { + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public Status getStatus(String sessionId, DataSelection data, Integer sc) throws BadRequestException, - NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { - + public Status getStatus(String sessionId, DataSelection data, Integer sc) + throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getStatus"); if (sessionId != null) { uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -477,17 +539,17 @@ public Status getStatus(String sessionId, DataSelection data, Integer sc) throws checkResponseConformity(response); return Status.valueOf(getString(response, sc)); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } - } - public Status getStatus(String preparedId, Integer sc) throws BadRequestException, - NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { - + public Status getStatus(String preparedId, Integer sc) + throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getStatus"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); URI uri = getUri(uriBuilder); @@ -498,17 +560,17 @@ public Status getStatus(String preparedId, Integer sc) throws BadRequestExceptio try (CloseableHttpResponse response = httpclient.execute(httpGet)) { return Status.valueOf(getString(response, sc)); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } - } - private String getString(CloseableHttpResponse response, Integer sc) throws InternalException, BadRequestException, - DataNotOnlineException, ParseException, InsufficientPrivilegesException, NotImplementedException, - InsufficientStorageException, NotFoundException, IOException { + private String getString(CloseableHttpResponse response, Integer sc) + throws InternalException, BadRequestException, DataNotOnlineException, ParseException, InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException, IOException { checkStatus(response, sc); HttpEntity entity = response.getEntity(); if (entity == null) { @@ -530,8 +592,7 @@ private URIBuilder getUriBuilder(String path) { } public boolean isPrepared(String preparedId, Integer sc) - throws BadRequestException, NotFoundException, InternalException, NotImplementedException { - + throws BadRequestException, NotFoundException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("isPrepared"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); URI uri = getUri(uriBuilder); @@ -542,48 +603,72 @@ public boolean isPrepared(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch ( + InsufficientStorageException + | DataNotOnlineException + | InsufficientPrivilegesException e + ) { + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public boolean isReadOnly(int sc) throws InternalException, NotImplementedException { + public boolean isReadOnly(int sc) + throws InternalException, NotImplementedException { URI uri = getUri(getUriBuilder("isReadOnly")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch ( + IOException + | InsufficientStorageException + | DataNotOnlineException + | BadRequestException + | InsufficientPrivilegesException + | NotFoundException e + ) { + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public boolean isTwoLevel(int sc) throws InternalException, NotImplementedException { + public boolean isTwoLevel(int sc) + throws InternalException, NotImplementedException { URI uri = getUri(getUriBuilder("isTwoLevel")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch ( + IOException + | InsufficientStorageException + | DataNotOnlineException + | BadRequestException + | InsufficientPrivilegesException + | NotFoundException e + ) { + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void ping(Integer sc) throws InternalException, NotFoundException, NotImplementedException { - + public void ping(Integer sc) + throws InternalException, NotFoundException, NotImplementedException { URI uri = getUri(getUriBuilder("ping")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); @@ -591,26 +676,43 @@ public void ping(Integer sc) throws InternalException, NotFoundException, NotImp checkResponseConformity(response); String result = getString(response, sc); if (!result.equals("IdsOK")) { - throw new NotFoundException("Server gave invalid response: " + result); + throw new NotFoundException( + "Server gave invalid response: " + result + ); } - } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch ( + IOException + | InsufficientStorageException + | DataNotOnlineException + | BadRequestException + | InsufficientPrivilegesException + | NotFoundException e + ) { + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public String prepareData(String sessionId, DataSelection data, Flag flags, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, - InternalException { - + public String prepareData( + String sessionId, + DataSelection data, + Flag flags, + Integer sc + ) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException { URI uri = getUri(getUriBuilder("prepareData")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId) + ); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue()) + ); } if (flags == Flag.ZIP || flags == Flag.ZIP_AND_COMPRESS) { formparams.add(new BasicNameValuePair("zip", "true")); @@ -622,39 +724,69 @@ public String prepareData(String sessionId, DataSelection data, Flag flags, Inte HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try ( + CloseableHttpResponse response = httpclient.execute(httpPost) + ) { checkResponseConformity(response); return getString(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } - } - public Long put(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, - String description, Integer sc) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, - NotImplementedException, DataNotOnlineException, InsufficientStorageException { - return put(sessionId, inputStream, name, datasetId, datafileFormatId, description, null, null, null, sc); + public Long put( + String sessionId, + InputStream inputStream, + String name, + long datasetId, + long datafileFormatId, + String description, + Integer sc + ) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException, InsufficientStorageException { + return put( + sessionId, + inputStream, + name, + datasetId, + datafileFormatId, + description, + null, + null, + null, + sc + ); } - public Long put(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, - String description, String doi, Date datafileCreateTime, Date datafileModTime, Integer sc) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, - NotImplementedException, DataNotOnlineException, InsufficientStorageException { - + public Long put( + String sessionId, + InputStream inputStream, + String name, + long datasetId, + long datafileFormatId, + String description, + String doi, + Date datafileCreateTime, + Date datafileModTime, + Integer sc + ) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException, InsufficientStorageException { if (inputStream == null) { throw new BadRequestException("Input stream is null"); } CRC32 crc = new CRC32(); inputStream = new CheckedInputStream(inputStream, crc); URIBuilder uriBuilder = getUriBuilder("put"); - uriBuilder.setParameter(RequestIdNames.sessionId, sessionId).setParameter("name", name) - .setParameter("datafileFormatId", Long.toString(datafileFormatId)) - .setParameter("datasetId", Long.toString(datasetId)); + uriBuilder + .setParameter(RequestIdNames.sessionId, sessionId) + .setParameter("name", name) + .setParameter("datafileFormatId", Long.toString(datafileFormatId)) + .setParameter("datasetId", Long.toString(datasetId)); if (description != null) { uriBuilder.setParameter("description", description); } @@ -662,40 +794,70 @@ public Long put(String sessionId, InputStream inputStream, String name, long dat uriBuilder.setParameter("doi", doi); } if (datafileCreateTime != null) { - uriBuilder.setParameter("datafileCreateTime", Long.toString(datafileCreateTime.getTime())); + uriBuilder.setParameter( + "datafileCreateTime", + Long.toString(datafileCreateTime.getTime()) + ); } if (datafileModTime != null) { - uriBuilder.setParameter("datafileModTime", Long.toString(datafileModTime.getTime())); + uriBuilder.setParameter( + "datafileModTime", + Long.toString(datafileModTime.getTime()) + ); } URI uri = getUri(uriBuilder); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPut httpPut = new HttpPut(uri); - httpPut.setEntity(new InputStreamEntity(inputStream, ContentType.APPLICATION_OCTET_STREAM)); + httpPut.setEntity( + new InputStreamEntity( + inputStream, + ContentType.APPLICATION_OCTET_STREAM + ) + ); try (CloseableHttpResponse response = httpclient.execute(httpPut)) { String result = getString(response, sc); checkResponseConformity(response); - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + try ( + JsonReader jsonReader = Json.createReader( + new StringReader(result) + ) + ) { JsonObject rootNode = jsonReader.readObject(); - if (rootNode.getJsonNumber("checksum").longValueExact() != crc.getValue()) { - throw new InternalException("Error uploading - the checksum was not as expected"); + if ( + rootNode.getJsonNumber("checksum").longValueExact() != + crc.getValue() + ) { + throw new InternalException( + "Error uploading - the checksum was not as expected" + ); } return rootNode.getJsonNumber("id").longValueExact(); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (NumberFormatException e) { - throw new InternalException("Web service call did not return a valid Long value"); + throw new InternalException( + "Web service call did not return a valid Long value" + ); } - } - public Long putAsPost(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, - String description, String doi, Date datafileCreateTime, Date datafileModTime, boolean wrap, Integer sc) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, - NotImplementedException, DataNotOnlineException, InsufficientStorageException { - + public Long putAsPost( + String sessionId, + InputStream inputStream, + String name, + long datasetId, + long datafileFormatId, + String description, + String doi, + Date datafileCreateTime, + Date datafileModTime, + boolean wrap, + Integer sc + ) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException, InsufficientStorageException { if (inputStream == null) { throw new BadRequestException("Input stream is null"); } @@ -703,30 +865,65 @@ public Long putAsPost(String sessionId, InputStream inputStream, String name, lo inputStream = new CheckedInputStream(inputStream, crc); URI uri = getUri(getUriBuilder("put")); - MultipartEntityBuilder reqEntityBuilder = MultipartEntityBuilder.create() - .addPart(RequestIdNames.sessionId, new StringBody(sessionId, ContentType.TEXT_PLAIN)) - .addPart("datafileFormatId", new StringBody(Long.toString(datafileFormatId), ContentType.TEXT_PLAIN)) - .addPart("name", new StringBody(name, ContentType.TEXT_PLAIN)) - .addPart("datasetId", new StringBody(Long.toString(datasetId), ContentType.TEXT_PLAIN)); + MultipartEntityBuilder reqEntityBuilder = MultipartEntityBuilder + .create() + .addPart( + RequestIdNames.sessionId, + new StringBody(sessionId, ContentType.TEXT_PLAIN) + ) + .addPart( + "datafileFormatId", + new StringBody( + Long.toString(datafileFormatId), + ContentType.TEXT_PLAIN + ) + ) + .addPart("name", new StringBody(name, ContentType.TEXT_PLAIN)) + .addPart( + "datasetId", + new StringBody(Long.toString(datasetId), ContentType.TEXT_PLAIN) + ); if (description != null) { - reqEntityBuilder.addPart("description", new StringBody(description, ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart( + "description", + new StringBody(description, ContentType.TEXT_PLAIN) + ); } if (doi != null) { - reqEntityBuilder.addPart("doi", new StringBody(doi, ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart( + "doi", + new StringBody(doi, ContentType.TEXT_PLAIN) + ); } if (datafileCreateTime != null) { - reqEntityBuilder.addPart("datafileCreateTime", - new StringBody(Long.toString(datafileCreateTime.getTime()), ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart( + "datafileCreateTime", + new StringBody( + Long.toString(datafileCreateTime.getTime()), + ContentType.TEXT_PLAIN + ) + ); } if (datafileModTime != null) { - reqEntityBuilder.addPart("datafileModTime", - new StringBody(Long.toString(datafileModTime.getTime()), ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart( + "datafileModTime", + new StringBody( + Long.toString(datafileModTime.getTime()), + ContentType.TEXT_PLAIN + ) + ); } if (wrap) { - reqEntityBuilder.addPart("wrap", new StringBody("true", ContentType.TEXT_PLAIN)); - } - InputStreamBody body = new InputStreamBody(new BufferedInputStream(inputStream), - ContentType.APPLICATION_OCTET_STREAM, "unreliable"); + reqEntityBuilder.addPart( + "wrap", + new StringBody("true", ContentType.TEXT_PLAIN) + ); + } + InputStreamBody body = new InputStreamBody( + new BufferedInputStream(inputStream), + ContentType.APPLICATION_OCTET_STREAM, + "unreliable" + ); HttpEntity entity = reqEntityBuilder.addPart("file", body).build(); CloseableHttpClient httpclient = HttpClients.createDefault(); @@ -736,43 +933,65 @@ public Long putAsPost(String sessionId, InputStream inputStream, String name, lo try (CloseableHttpResponse response = httpclient.execute(httpPost)) { checkResponseConformity(response); String result = getString(response, sc); - String prefix = ""; if (result.startsWith(prefix)) { - result = result.substring(prefix.length(), result.length() - suffix.length()); + result = + result.substring( + prefix.length(), + result.length() - suffix.length() + ); } - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + try ( + JsonReader jsonReader = Json.createReader( + new StringReader(result) + ) + ) { JsonObject rootNode = jsonReader.readObject(); - if (rootNode.getJsonNumber("checksum").longValueExact() != crc.getValue()) { - throw new InternalException("Error uploading - the checksum was not as expected"); + if ( + rootNode.getJsonNumber("checksum").longValueExact() != + crc.getValue() + ) { + throw new InternalException( + "Error uploading - the checksum was not as expected" + ); } return rootNode.getJsonNumber("id").longValueExact(); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (NumberFormatException e) { - throw new InternalException("Web service call did not return a valid Long value"); + throw new InternalException( + "Web service call did not return a valid Long value" + ); } - } - public void restore(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, - BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { - + public void restore(String sessionId, DataSelection data, Integer sc) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { URI uri = getUri(getUriBuilder("restore")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId) + ); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue()) + ); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try ( + CloseableHttpResponse response = httpclient.execute(httpPost) + ) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -780,8 +999,7 @@ public void restore(String sessionId, DataSelection data, Integer sc) throws Not } public List getDatafileIds(String preparedId, Integer sc) - throws InternalException, BadRequestException, NotFoundException { - + throws InternalException, BadRequestException, NotFoundException { URIBuilder uriBuilder = getUriBuilder("getDatafileIds"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); URI uri = getUri(uriBuilder); @@ -792,8 +1010,11 @@ public List getDatafileIds(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { String result = getString(response, sc); checkResponseConformity(response); - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { - + try ( + JsonReader jsonReader = Json.createReader( + new StringReader(result) + ) + ) { JsonObject rootNode = jsonReader.readObject(); List ids = new ArrayList<>(); for (JsonValue num : rootNode.getJsonArray("ids")) { @@ -803,20 +1024,35 @@ public List getDatafileIds(String preparedId, Integer sc) return ids; } catch (JsonException e) { throw new InternalException( - "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); + "TestingClient " + + e.getClass() + + " " + + e.getMessage() + + " from " + + result + ); } - - } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException - | NotImplementedException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch ( + InsufficientStorageException + | DataNotOnlineException + | InsufficientPrivilegesException + | NotImplementedException e + ) { + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public List getDatafileIds(String sessionId, DataSelection data, Integer sc) - throws InternalException, BadRequestException, ParseException, NotFoundException { + public List getDatafileIds( + String sessionId, + DataSelection data, + Integer sc + ) + throws InternalException, BadRequestException, ParseException, NotFoundException { URIBuilder uriBuilder = getUriBuilder("getDatafileIds"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); for (Entry entry : data.getParameters().entrySet()) { @@ -830,8 +1066,11 @@ public List getDatafileIds(String sessionId, DataSelection data, Integer s try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); String result = getString(response, sc); - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { - + try ( + JsonReader jsonReader = Json.createReader( + new StringReader(result) + ) + ) { JsonObject rootNode = jsonReader.readObject(); List ids = new ArrayList<>(); for (JsonValue num : rootNode.getJsonArray("ids")) { @@ -841,98 +1080,139 @@ public List getDatafileIds(String sessionId, DataSelection data, Integer s return ids; } catch (JsonException e) { throw new InternalException( - "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); + "TestingClient " + + e.getClass() + + " " + + e.getMessage() + + " from " + + result + ); } - - } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException - | NotImplementedException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch ( + InsufficientStorageException + | DataNotOnlineException + | InsufficientPrivilegesException + | NotImplementedException e + ) { + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void reset(String preparedId, Integer sc) throws InternalException, BadRequestException, ParseException, - InsufficientPrivilegesException, NotImplementedException, NotFoundException { + public void reset(String preparedId, Integer sc) + throws InternalException, BadRequestException, ParseException, InsufficientPrivilegesException, NotImplementedException, NotFoundException { URI uri = getUri(getUriBuilder("reset")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.preparedId, preparedId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.preparedId, preparedId) + ); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try ( + CloseableHttpResponse response = httpclient.execute(httpPost) + ) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void reset(String sessionId, DataSelection data, int sc) throws InternalException, BadRequestException, - ParseException, InsufficientPrivilegesException, NotImplementedException, NotFoundException { + public void reset(String sessionId, DataSelection data, int sc) + throws InternalException, BadRequestException, ParseException, InsufficientPrivilegesException, NotImplementedException, NotFoundException { URI uri = getUri(getUriBuilder("reset")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId) + ); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue()) + ); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try ( + CloseableHttpResponse response = httpclient.execute(httpPost) + ) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void write(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, - BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, - DataNotOnlineException { - + public void write(String sessionId, DataSelection data, Integer sc) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { URI uri = getUri(getUriBuilder("write")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId) + ); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue()) + ); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(formparams)); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try ( + CloseableHttpResponse response = httpclient.execute(httpPost) + ) { expectNothing(response, sc); } catch (InsufficientStorageException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage() + ); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - private void checkResponseConformity(HttpResponse response) throws InternalException { - + private void checkResponseConformity(HttpResponse response) + throws InternalException { StatusLine status = response.getStatusLine(); var statusCode = status.getStatusCode(); - if(statusCode == 200 && response.getEntity() != null) { + if (statusCode == 200 && response.getEntity() != null) { //we have a status of 200 and a payload. Check for header conformity - Boolean chunked = response.getFirstHeader("Transfer-Encoding") != null ? response.getFirstHeader("Transfer-Encoding").getValue().contains("chunked") - : false; - - if ( (response.getFirstHeader("Content-Length") == null && !chunked) - || (response.getFirstHeader("Content-Length") != null && chunked ) ) { - - throw new InternalException("Responses with payload should either contain the Content-Length header or the 'Transfer-Encoding: chunked' header. It needs exactly one of them."); + Boolean chunked = response.getFirstHeader("Transfer-Encoding") != + null + ? response + .getFirstHeader("Transfer-Encoding") + .getValue() + .contains("chunked") + : false; + + if ( + ( + response.getFirstHeader("Content-Length") == null && + !chunked + ) || + (response.getFirstHeader("Content-Length") != null && chunked) + ) { + throw new InternalException( + "Responses with payload should either contain the Content-Length header or the 'Transfer-Encoding: chunked' header. It needs exactly one of them." + ); } } } - } From d76498c0cbc394a92b04d60e5523d54de102eb50 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Thu, 6 Jun 2024 14:23:43 +0200 Subject: [PATCH 07/19] Revert "linted code to test ci check" This reverts commit 1c2aa8de0650703222641a465f8a60f02e03e210. --- .../java/org/icatproject/ids/IdsService.java | 631 ++++++---------- src/main/java/org/icatproject/ids/Tidier.java | 281 ++----- .../org/icatproject/ids/enums/CallType.java | 6 +- .../org/icatproject/ids/enums/DeferredOp.java | 5 +- .../icatproject/ids/enums/RequestIdNames.java | 2 +- .../icatproject/ids/enums/RequestType.java | 24 +- .../org/icatproject/ids/enums/Status.java | 4 +- .../icatproject/ids/enums/StorageUnit.java | 3 +- .../ids/enums/ValueContainerType.java | 9 +- .../exceptions/DataNotOnlineException.java | 1 + .../ids/exceptions/IdsException.java | 2 +- .../ids/exceptions/IdsExceptionMapper.java | 14 +- .../InsufficientStorageException.java | 1 - .../exceptions/NotFoundExceptionMapper.java | 24 +- .../exceptions/NotImplementedException.java | 1 + .../exceptions/RuntimeExceptionMapper.java | 30 +- .../FiniteStateMachine.java | 100 +-- ...niteStateMachineForSingleLevelStorage.java | 22 +- ...iteStateMachineForStorageUnitDatafile.java | 337 ++------- ...niteStateMachineForStorageUnitDataset.java | 229 ++---- .../ids/helpers/CORSResponseFilter.java | 10 +- .../helpers/CheckedWithSizeInputStream.java | 1 - .../icatproject/ids/helpers/Constants.java | 8 +- .../ids/helpers/LocationHelper.java | 20 +- .../ids/helpers/RangeOutputStream.java | 22 +- .../java/org/icatproject/ids/helpers/SO.java | 91 +-- .../ids/helpers/ValueContainer.java | 67 +- .../icatproject/ids/models/DataInfoBase.java | 3 +- .../icatproject/ids/models/DataSelection.java | 29 +- .../icatproject/ids/models/DatafileInfo.java | 30 +- .../icatproject/ids/models/DatasetInfo.java | 68 +- .../org/icatproject/ids/models/Prepared.java | 132 +--- .../ids/requestHandlers/ArchiveHandler.java | 24 +- .../ids/requestHandlers/DeleteHandler.java | 56 +- .../GetDataFileIdsHandler.java | 45 +- .../ids/requestHandlers/GetDataHandler.java | 217 ++---- .../requestHandlers/GetIcatUrlHandler.java | 10 +- .../GetServiceStatusHandler.java | 27 +- .../ids/requestHandlers/GetStatusHandler.java | 36 +- .../requestHandlers/IsPreparedHandler.java | 36 +- .../requestHandlers/IsReadOnlyHandler.java | 7 +- .../requestHandlers/IsTwoLevelHandler.java | 4 +- .../requestHandlers/PrepareDataHandler.java | 61 +- .../ids/requestHandlers/PutHandler.java | 371 +++------ .../ids/requestHandlers/ResetHandler.java | 35 +- .../ids/requestHandlers/RestoreHandler.java | 27 +- .../ids/requestHandlers/WriteHandler.java | 47 +- .../base/DataControllerBase.java | 42 +- .../base/DataRequestHandler.java | 78 +- .../base/PreparedDataController.java | 41 +- .../base/RequestHandlerBase.java | 76 +- .../base/UnpreparedDataController.java | 76 +- .../getSizeHandlers/GetSizeHandler.java | 58 +- .../GetSizeHandlerForFastProcessing.java | 115 +-- .../icatproject/ids/services/ICATGetter.java | 30 +- .../icatproject/ids/services/IcatReader.java | 26 +- .../icatproject/ids/services/LockManager.java | 41 +- .../ids/services/PropertyHandler.java | 215 ++---- .../ids/services/ServiceProvider.java | 55 +- .../icatproject/ids/services/Transmitter.java | 37 +- .../ids/services/UnfinishedWorkService.java | 102 +-- .../DataSelectionService.java | 200 ++--- .../DataSelectionServiceFactory.java | 698 ++++------------- ...SelectionServiceForSingleLevelStorage.java | 62 +- ...electionServiceForStorageUnitDatafile.java | 52 +- ...SelectionServiceForStorageUnitDataset.java | 62 +- .../icatproject/ids/thread/DfArchiver.java | 47 +- .../org/icatproject/ids/thread/DfDeleter.java | 27 +- .../icatproject/ids/thread/DfRestorer.java | 36 +- .../org/icatproject/ids/thread/DfWriter.java | 39 +- .../icatproject/ids/thread/DsArchiver.java | 34 +- .../icatproject/ids/thread/DsRestorer.java | 105 +-- .../org/icatproject/ids/thread/DsWriter.java | 86 +-- .../icatproject/ids/DataSelectionDevTest.java | 79 +- .../java/org/icatproject/ids/DigestTest.java | 16 +- .../icatproject/ids/PreparePackingTest.java | 73 +- .../ids/RangeOutputStreamTest.java | 12 +- .../org/icatproject/ids/TestICATGetter.java | 17 +- .../java/org/icatproject/ids/TestUtils.java | 5 +- .../icatproject/ids/TestValueContainer.java | 19 +- .../java/org/icatproject/ids/TestZipping.java | 24 +- .../java/org/icatproject/ids/TidierTest.java | 36 +- .../org/icatproject/ids/TransmittingTest.java | 289 ++----- .../org/icatproject/ids/ValidaterTest.java | 5 +- .../icatproject/ids/integration/BaseTest.java | 617 ++++----------- .../ids/integration/one/BaseTests.java | 6 +- .../integration/one/BogusDatafileTest.java | 96 +-- .../ids/integration/one/DeleteTest.java | 33 +- .../integration/one/GetDataExplicitTest.java | 258 ++----- .../one/GetDataForPreparedIdTest.java | 111 +-- .../one/GetStatusExplicitTest.java | 47 +- .../one/GetStatusForPreparedIdTest.java | 49 +- .../ids/integration/one/MiscTest.java | 42 +- .../ids/integration/one/PerformanceTest.java | 64 +- .../ids/integration/one/PrepareDataTest.java | 99 +-- .../ids/integration/one/PutTest.java | 113 +-- .../ids/integration/one/WriteTest.java | 12 +- .../ids/integration/two/ArchiveTest.java | 40 +- .../ids/integration/two/BaseTests.java | 6 +- .../integration/two/BogusDatafileTest.java | 143 ++-- .../ids/integration/two/DeleteTest.java | 38 +- .../integration/two/GetDataExplicitTest.java | 161 +--- .../two/GetDataForPreparedIdTest.java | 100 +-- .../two/GetStatusExplicitTest.java | 63 +- .../two/GetStatusForPreparedIdTest.java | 49 +- .../ids/integration/two/MiscTest.java | 39 +- .../ids/integration/two/PrepareDataTest.java | 149 +--- .../ids/integration/two/PutTest.java | 81 +- .../integration/two/RestoreErrorsTest.java | 28 +- .../ids/integration/two/RestoreTest.java | 42 +- .../ids/integration/two/WriteTest.java | 41 +- .../ids/integration/twodf/ArchiveTest.java | 40 +- .../ids/integration/twodf/BaseTests.java | 6 +- .../integration/twodf/BogusDatafileTest.java | 143 ++-- .../ids/integration/twodf/DeleteTest.java | 38 +- .../twodf/GetDataExplicitTest.java | 162 +--- .../twodf/GetDataForPreparedIdTest.java | 100 +-- .../twodf/GetStatusExplicitTest.java | 66 +- .../twodf/GetStatusForPreparedIdTest.java | 49 +- .../ids/integration/twodf/MiscTest.java | 30 +- .../integration/twodf/PrepareDataTest.java | 149 +--- .../ids/integration/twodf/PutTest.java | 80 +- .../ids/integration/twodf/QueueTest.java | 22 +- .../ids/integration/twodf/RestoreTest.java | 40 +- .../ids/integration/twodf/WriteTest.java | 41 +- .../ids/integration/util/Setup.java | 41 +- .../util/client/BadRequestException.java | 1 + .../util/client/DataNotOnlineException.java | 1 + .../util/client/HttpInputStream.java | 8 +- .../integration/util/client/IdsException.java | 1 + .../InsufficientPrivilegesException.java | 1 + .../client/InsufficientStorageException.java | 1 + .../util/client/InternalException.java | 1 + .../util/client/NotFoundException.java | 1 + .../util/client/NotImplementedException.java | 1 + .../util/client/TestingClient.java | 712 ++++++------------ 136 files changed, 3009 insertions(+), 7478 deletions(-) diff --git a/src/main/java/org/icatproject/ids/IdsService.java b/src/main/java/org/icatproject/ids/IdsService.java index 218306cb..0d517fa9 100644 --- a/src/main/java/org/icatproject/ids/IdsService.java +++ b/src/main/java/org/icatproject/ids/IdsService.java @@ -1,5 +1,12 @@ package org.icatproject.ids; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; + + import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import jakarta.ejb.EJB; @@ -16,17 +23,15 @@ import jakarta.ws.rs.HeaderParam; import jakarta.ws.rs.POST; import jakarta.ws.rs.PUT; -import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; +import jakarta.ws.rs.Path; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.RequestIdNames; import org.icatproject.ids.exceptions.BadRequestException; @@ -61,16 +66,12 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.Transmitter; import org.icatproject.ids.services.UnfinishedWorkService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; @Path("/") @Stateless public class IdsService { - private static final Logger logger = LoggerFactory.getLogger( - IdsService.class - ); + private final static Logger logger = LoggerFactory.getLogger(IdsService.class); @EJB Transmitter transmitter; @@ -88,34 +89,24 @@ public class IdsService { private UnfinishedWorkService unfinishedWorkService; + @PostConstruct private void init() { + try { synchronized (lock) { + logger.info("creating IdsService"); - FiniteStateMachine.createInstance( - reader, - lockManager, - PropertyHandler.getInstance().getStorageUnit() - ); + FiniteStateMachine.createInstance(reader, lockManager, PropertyHandler.getInstance().getStorageUnit()); this.fsm = FiniteStateMachine.getInstance(); this.fsm.init(); - ServiceProvider.createInstance( - transmitter, - fsm, - lockManager, - reader - ); - - var propertyHandler = ServiceProvider - .getInstance() - .getPropertyHandler(); + ServiceProvider.createInstance(transmitter, fsm, lockManager, reader); + + var propertyHandler = ServiceProvider.getInstance().getPropertyHandler(); var archiveStorage = propertyHandler.getArchiveStorage(); var twoLevel = archiveStorage != null; - var preparedDir = propertyHandler - .getCacheDir() - .resolve("prepared"); + var preparedDir = propertyHandler.getCacheDir().resolve("prepared"); Files.createDirectories(preparedDir); this.unfinishedWorkService = new UnfinishedWorkService(); @@ -129,21 +120,13 @@ private void init() { java.nio.file.Path datasetDir; if (twoLevel) { - datasetDir = - propertyHandler.getCacheDir().resolve("dataset"); - var markerDir = propertyHandler - .getCacheDir() - .resolve("marker"); + datasetDir = propertyHandler.getCacheDir().resolve("dataset"); + var markerDir = propertyHandler.getCacheDir().resolve("marker"); if (!inited) { Files.createDirectories(datasetDir); Files.createDirectories(markerDir); - this.unfinishedWorkService.restartUnfinishedWork( - markerDir, - key - ); - this.unfinishedWorkService.cleanDatasetCache( - datasetDir - ); + this.unfinishedWorkService.restartUnfinishedWork(markerDir, key); + this.unfinishedWorkService.cleanDatasetCache(datasetDir); } } @@ -153,18 +136,18 @@ private void init() { } } catch (Throwable e) { logger.error("Won't start ", e); - throw new RuntimeException( - "IdsService reports " + e.getClass() + " " + e.getMessage() - ); + throw new RuntimeException("IdsService reports " + e.getClass() + " " + e.getMessage()); } } + @PreDestroy private void exit() { this.fsm.exit(); logger.info("destroyed IdsService"); } + /** * Archive data specified by the investigationIds, datasetIds and * datafileIds specified along with a sessionId. If two level storage is not @@ -181,27 +164,19 @@ private void exit() { * @throws InsufficientPrivilegesException * @throws InternalException * @throws NotFoundException - * @throws DataNotOnlineException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("archive") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void archive( - @Context HttpServletRequest request, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds - ) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new ArchiveHandler( - request.getRemoteAddr(), - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public void archive(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, + NotFoundException, DataNotOnlineException { + + var handler = new ArchiveHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } @@ -225,24 +200,16 @@ public void archive( */ @DELETE @Path("delete") - public void delete( - @Context HttpServletRequest request, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds - ) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { - var handler = new DeleteHandler( - request.getRemoteAddr(), - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public void delete(@Context HttpServletRequest request, @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds) throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { + + var handler = new DeleteHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } + /** * Return the version of the server * @@ -255,10 +222,7 @@ public void delete( public String getVersion() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen - .writeStartObject() - .write("version", Constants.API_VERSION) - .writeEnd(); + gen.writeStartObject().write("version", Constants.API_VERSION).writeEnd(); gen.close(); return baos.toString(); } @@ -293,37 +257,20 @@ public String getVersion() { * @throws InternalException * @throws InsufficientPrivilegesException * @throws DataNotOnlineException - * @throws NotImplementedException + * @throws NotImplementedException * @statuscode 200 To indicate success */ @GET @Path("getData") @Produces(MediaType.APPLICATION_OCTET_STREAM) - public Response getData( - @Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds, - @QueryParam("compress") boolean compress, - @QueryParam("zip") boolean zip, - @QueryParam("outname") String outname, - @HeaderParam("Range") String range - ) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - var handler = new GetDataHandler( - request.getRemoteAddr(), - preparedId, - sessionId, - investigationIds, - datasetIds, - datafileIds, - compress, - zip, - outname, - range - ); + public Response getData(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds, + @QueryParam("compress") boolean compress, @QueryParam("zip") boolean zip, + @QueryParam("outname") String outname, @HeaderParam("Range") String range) throws BadRequestException, + NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + + var handler = new GetDataHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds, compress, zip, outname, range); return handler.handle().getResponse(); } @@ -344,30 +291,19 @@ public Response getData( * @throws InternalException * @throws NotFoundException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getDatafileIds") @Produces(MediaType.APPLICATION_JSON) - public String getDatafileIds( - @Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds - ) - throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - var handler = new GetDataFileIdsHandler( - request.getRemoteAddr(), - preparedId, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public String getDatafileIds(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + + var handler = new GetDataFileIdsHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); return handler.handle().getString(); } @@ -377,19 +313,19 @@ public String getDatafileIds( * obtained. * * @return the url of the icat server - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("getIcatUrl") @Produces(MediaType.TEXT_PLAIN) - public String getIcatUrl(@Context HttpServletRequest request) - throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public String getIcatUrl(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + var handler = new GetIcatUrlHandler(request.getRemoteAddr()); return handler.handle().getString(); } @@ -404,24 +340,19 @@ public String getIcatUrl(@Context HttpServletRequest request) * @return a json string. * @throws InternalException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws BadRequestException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws BadRequestException * @statuscode 200 To indicate success */ @GET @Path("getServiceStatus") @Produces(MediaType.APPLICATION_JSON) - public String getServiceStatus( - @Context HttpServletRequest request, - @QueryParam(RequestIdNames.sessionId) String sessionId - ) - throws InternalException, InsufficientPrivilegesException, BadRequestException, NotFoundException, DataNotOnlineException, NotImplementedException { - var handler = new GetServiceStatusHandler( - request.getRemoteAddr(), - sessionId - ); + public String getServiceStatus(@Context HttpServletRequest request, @QueryParam(RequestIdNames.sessionId) String sessionId) + throws InternalException, InsufficientPrivilegesException, BadRequestException, NotFoundException, DataNotOnlineException, NotImplementedException { + + var handler = new GetServiceStatusHandler(request.getRemoteAddr(), sessionId); return handler.handle().getString(); } @@ -441,46 +372,30 @@ public String getServiceStatus( * @throws NotFoundException * @throws InsufficientPrivilegesException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getSize") @Produces(MediaType.TEXT_PLAIN) - public long getSize( - @Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds - ) - throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { + public long getSize(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { + + var result = ValueContainer.getInvalid(); // trying fast computation - if (sessionId != null) { - var fastHandler = new GetSizeHandlerForFastProcessing( - request.getRemoteAddr(), - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + if(sessionId != null) { + var fastHandler = new GetSizeHandlerForFastProcessing(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); result = fastHandler.handle(); } // otherwise normal computation - if (result.isInvalid()) { - var handler = new GetSizeHandler( - request.getRemoteAddr(), - preparedId, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + if(result.isInvalid()) { + var handler = new GetSizeHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); result = handler.handle(); } @@ -509,48 +424,32 @@ public long getSize( * @throws NotFoundException * @throws InsufficientPrivilegesException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getStatus") @Produces(MediaType.TEXT_PLAIN) - public String getStatus( - @Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds - ) - throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { + public String getStatus(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { + // special case for getStatus request: getting status is possible without authentification if (sessionId == null && preparedId == null) { try { - sessionId = - ServiceProvider - .getInstance() - .getIcatReader() - .getSessionId(); + sessionId = ServiceProvider.getInstance().getIcatReader().getSessionId(); } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage() - ); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } } - var handler = new GetStatusHandler( - request.getRemoteAddr(), - preparedId, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + var handler = new GetStatusHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); return handler.handle().getString(); } + /** * Returns true if all the data files are ready to be downloaded. As a side * effect, if any data files are archived and no restoration has been @@ -568,23 +467,18 @@ public String getStatus( * @throws BadRequestException * @throws NotFoundException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws InsufficientPrivilegesException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws InsufficientPrivilegesException * @statuscode 200 To indicate success */ @GET @Path("isPrepared") @Produces(MediaType.TEXT_PLAIN) - public boolean isPrepared( - @Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId - ) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - var handler = new IsPreparedHandler( - request.getRemoteAddr(), - preparedId - ); + public boolean isPrepared(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + + var handler = new IsPreparedHandler(request.getRemoteAddr(), preparedId); return handler.handle().getBool(); } @@ -594,19 +488,19 @@ public boolean isPrepared( * * @title isReadOnly * @return true if readonly, else false - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("isReadOnly") @Produces(MediaType.TEXT_PLAIN) - public boolean isReadOnly(@Context HttpServletRequest request) - throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public boolean isReadOnly(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + var handler = new IsReadOnlyHandler(request.getRemoteAddr()); return handler.handle().getBool(); } @@ -617,19 +511,19 @@ public boolean isReadOnly(@Context HttpServletRequest request) * * @title isTwoLevel * @return true if twoLevel, else false - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("isTwoLevel") @Produces(MediaType.TEXT_PLAIN) - public boolean isTwoLevel(@Context HttpServletRequest request) - throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public boolean isTwoLevel(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + var handler = new IsTwoLevelHandler(request.getRemoteAddr()); return handler.handle().getBool(); } @@ -673,33 +567,21 @@ public String ping() { * @throws InsufficientPrivilegesException * @throws NotFoundException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("prepareData") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.TEXT_PLAIN) - public String prepareData( - @Context HttpServletRequest request, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds, - @FormParam("compress") boolean compress, - @FormParam("zip") boolean zip - ) - throws BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, NotImplementedException, DataNotOnlineException { - var handler = new PrepareDataHandler( - request.getRemoteAddr(), - sessionId, - investigationIds, - datasetIds, - datafileIds, - compress, - zip - ); + public String prepareData(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds, @FormParam("compress") boolean compress, + @FormParam("zip") boolean zip) + throws BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, NotImplementedException, DataNotOnlineException { + + var handler = new PrepareDataHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds, compress, zip); return handler.handle().getString(); } @@ -731,33 +613,16 @@ public String prepareData( @Path("put") @Consumes(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.APPLICATION_JSON) - public Response put( - @Context HttpServletRequest request, - InputStream body, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("name") String name, - @QueryParam("datafileFormatId") String datafileFormatId, - @QueryParam("datasetId") String datasetId, - @QueryParam("description") String description, - @QueryParam("doi") String doi, - @QueryParam("datafileCreateTime") String datafileCreateTime, - @QueryParam("datafileModTime") String datafileModTime - ) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { - var handler = new PutHandler( - request.getRemoteAddr(), - sessionId, - body, - name, - datafileFormatId, - datasetId, - description, - doi, - datafileCreateTime, - datafileModTime, - false, - false - ); + public Response put(@Context HttpServletRequest request, InputStream body, + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("name") String name, + @QueryParam("datafileFormatId") String datafileFormatId, @QueryParam("datasetId") String datasetId, + @QueryParam("description") String description, @QueryParam("doi") String doi, + @QueryParam("datafileCreateTime") String datafileCreateTime, + @QueryParam("datafileModTime") String datafileModTime) throws BadRequestException, NotFoundException, + InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { + + var handler = new PutHandler(request.getRemoteAddr(), sessionId, body, name, datafileFormatId, datasetId, + description, doi, datafileCreateTime, datafileModTime, false, false); return handler.handle().getResponse(); } @@ -784,90 +649,65 @@ public Response put( @Path("put") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) - public Response putAsPost(@Context HttpServletRequest request) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { - try { - String sessionId = null; - String name = null; - String datafileFormatId = null; - String datasetId = null; - String description = null; - String doi = null; - String datafileCreateTime = null; - String datafileModTime = null; - Response result = null; - boolean wrap = false; - boolean padding = false; - - // Parse the request - for (Part part : request.getParts()) { - String fieldName = part.getName(); - InputStream stream = part.getInputStream(); - if (part.getSubmittedFileName() == null) { - String value = new String( - stream.readAllBytes(), - StandardCharsets.UTF_8 - ); - if (fieldName.equals("sessionId")) { - sessionId = value; - } else if (fieldName.equals("name")) { - name = value; - } else if (fieldName.equals("datafileFormatId")) { - datafileFormatId = value; - } else if (fieldName.equals("datasetId")) { - datasetId = value; - } else if (fieldName.equals("description")) { - description = value; - } else if (fieldName.equals("doi")) { - doi = value; - } else if (fieldName.equals("datafileCreateTime")) { - datafileCreateTime = value; - } else if (fieldName.equals("datafileModTime")) { - datafileModTime = value; - } else if (fieldName.equals("wrap")) { - wrap = - ( - value != null && - value.toUpperCase().equals("TRUE") - ); - } else if (fieldName.equals("padding")) { - padding = - ( - value != null && - value.toUpperCase().equals("TRUE") - ); - } else { - throw new BadRequestException( - "Form field " + fieldName + "is not recognised" - ); - } - } else { - if (name == null) { - name = part.getSubmittedFileName(); + public Response putAsPost(@Context HttpServletRequest request) throws BadRequestException, NotFoundException, + InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { + try { + String sessionId = null; + String name = null; + String datafileFormatId = null; + String datasetId = null; + String description = null; + String doi = null; + String datafileCreateTime = null; + String datafileModTime = null; + Response result = null; + boolean wrap = false; + boolean padding = false; + + // Parse the request + for (Part part : request.getParts()) { + String fieldName = part.getName(); + InputStream stream = part.getInputStream(); + if (part.getSubmittedFileName() == null) { + String value = new String(stream.readAllBytes(), StandardCharsets.UTF_8); + if (fieldName.equals("sessionId")) { + sessionId = value; + } else if (fieldName.equals("name")) { + name = value; + } else if (fieldName.equals("datafileFormatId")) { + datafileFormatId = value; + } else if (fieldName.equals("datasetId")) { + datasetId = value; + } else if (fieldName.equals("description")) { + description = value; + } else if (fieldName.equals("doi")) { + doi = value; + } else if (fieldName.equals("datafileCreateTime")) { + datafileCreateTime = value; + } else if (fieldName.equals("datafileModTime")) { + datafileModTime = value; + } else if (fieldName.equals("wrap")) { + wrap = (value != null && value.toUpperCase().equals("TRUE")); + } else if (fieldName.equals("padding")) { + padding = (value != null && value.toUpperCase().equals("TRUE")); + } else { + throw new BadRequestException("Form field " + fieldName + "is not recognised"); + } + } else { + if (name == null) { + name = part.getSubmittedFileName(); + } + var handler = new PutHandler(request.getRemoteAddr(), sessionId, stream, name, datafileFormatId, datasetId, description, + doi, datafileCreateTime, datafileModTime, wrap, padding); + result = handler.handle().getResponse(); + } } - var handler = new PutHandler( - request.getRemoteAddr(), - sessionId, - stream, - name, - datafileFormatId, - datasetId, - description, - doi, - datafileCreateTime, - datafileModTime, - wrap, - padding - ); - result = handler.handle().getResponse(); + return result; + } catch (IOException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (ServletException e) { + throw new BadRequestException("Multipart content expected"); } - } - return result; - } catch (IOException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); - } catch (ServletException e) { - throw new BadRequestException("Multipart content expected"); - } } /** @@ -892,30 +732,20 @@ public Response putAsPost(@Context HttpServletRequest request) * @throws NotFoundException * @throws InternalException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("reset") - public void reset( - @Context HttpServletRequest request, - @FormParam(RequestIdNames.preparedId) String preparedId, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds - ) - throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - var handler = new ResetHandler( - request.getRemoteAddr(), - preparedId, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public void reset(@Context HttpServletRequest request, @FormParam(RequestIdNames.preparedId) String preparedId, + @FormParam(RequestIdNames.sessionId) String sessionId, @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, @FormParam("datafileIds") String datafileIds) + throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + + var handler = new ResetHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); + } /** @@ -934,30 +764,23 @@ public void reset( * @throws InsufficientPrivilegesException * @throws InternalException * @throws NotFoundException - * @throws DataNotOnlineException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("restore") @Consumes("application/x-www-form-urlencoded") - public void restore( - @Context HttpServletRequest request, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds - ) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new RestoreHandler( - request.getRemoteAddr(), - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public void restore(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, + NotFoundException, DataNotOnlineException { + + var handler = new RestoreHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } + /** * Write data specified by the investigationIds, datasetIds * and datafileIds specified along with a sessionId to archive @@ -980,21 +803,13 @@ public void restore( @POST @Path("write") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void write( - @Context HttpServletRequest request, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds - ) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new WriteHandler( - request.getRemoteAddr(), - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public void write(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, + NotFoundException, DataNotOnlineException { + + var handler = new WriteHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } } diff --git a/src/main/java/org/icatproject/ids/Tidier.java b/src/main/java/org/icatproject/ids/Tidier.java index 43ab0552..9d647de0 100644 --- a/src/main/java/org/icatproject/ids/Tidier.java +++ b/src/main/java/org/icatproject/ids/Tidier.java @@ -1,10 +1,5 @@ package org.icatproject.ids; -import jakarta.annotation.PostConstruct; -import jakarta.annotation.PreDestroy; -import jakarta.ejb.EJB; -import jakarta.ejb.Singleton; -import jakarta.ejb.Startup; import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -16,6 +11,16 @@ import java.util.Map; import java.util.Timer; import java.util.TimerTask; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.ejb.EJB; +import jakarta.ejb.Singleton; +import jakarta.ejb.Startup; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.IcatException_Exception; @@ -33,8 +38,6 @@ import org.icatproject.ids.services.IcatReader; import org.icatproject.ids.services.LockManager; import org.icatproject.ids.services.PropertyHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; @Singleton @Startup @@ -52,153 +55,57 @@ public void run() { if (twoLevel) { if (storageUnit == StorageUnit.DATASET) { - List dsInfos = mainStorage.getDatasetsToArchive( - stopArchivingLevel, - startArchivingLevel - ); + List dsInfos = mainStorage.getDatasetsToArchive(stopArchivingLevel, + startArchivingLevel); for (DsInfo dsInfo : dsInfos) { StringBuilder sb = new StringBuilder( - "SELECT ds FROM Dataset ds, ds.investigation inv, inv.facility fac WHERE" - ); + "SELECT ds FROM Dataset ds, ds.investigation inv, inv.facility fac WHERE"); boolean andNeeded = false; - andNeeded = - addNumericConstraint( - sb, - "ds.id", - dsInfo.getDsId(), - andNeeded - ); - andNeeded = - addStringConstraint( - sb, - "ds.location", - dsInfo.getDsLocation(), - andNeeded - ); - andNeeded = - addStringConstraint( - sb, - "ds.name", - dsInfo.getDsName(), - andNeeded - ); - - andNeeded = - addNumericConstraint( - sb, - "inv.id", - dsInfo.getInvId(), - andNeeded - ); - andNeeded = - addStringConstraint( - sb, - "inv.name", - dsInfo.getInvName(), - andNeeded - ); - andNeeded = - addStringConstraint( - sb, - "inv.visitId", - dsInfo.getVisitId(), - andNeeded - ); - - andNeeded = - addStringConstraint( - sb, - "fac.name", - dsInfo.getFacilityName(), - andNeeded - ); - andNeeded = - addNumericConstraint( - sb, - "fac.id", - dsInfo.getFacilityId(), - andNeeded - ); + andNeeded = addNumericConstraint(sb, "ds.id", dsInfo.getDsId(), andNeeded); + andNeeded = addStringConstraint(sb, "ds.location", dsInfo.getDsLocation(), andNeeded); + andNeeded = addStringConstraint(sb, "ds.name", dsInfo.getDsName(), andNeeded); + + andNeeded = addNumericConstraint(sb, "inv.id", dsInfo.getInvId(), andNeeded); + andNeeded = addStringConstraint(sb, "inv.name", dsInfo.getInvName(), andNeeded); + andNeeded = addStringConstraint(sb, "inv.visitId", dsInfo.getVisitId(), andNeeded); + + andNeeded = addStringConstraint(sb, "fac.name", dsInfo.getFacilityName(), andNeeded); + andNeeded = addNumericConstraint(sb, "fac.id", dsInfo.getFacilityId(), andNeeded); sb.append(" INCLUDE ds.investigation.facility"); try { int low = 0; while (true) { - String query = - sb.toString() + - " LIMIT " + - low + - "," + - tidyBlockSize; + String query = sb.toString() + " LIMIT " + low + "," + tidyBlockSize; List os = reader.search(query); - logger.debug( - query + - " returns " + - os.size() + - " datasets" - ); + logger.debug(query + " returns " + os.size() + " datasets"); for (Object o : os) { - DatasetInfo dsInfoImpl = new DatasetInfo( - (Dataset) o - ); + DatasetInfo dsInfoImpl = new DatasetInfo((Dataset) o); logger.debug( - "Requesting archive of " + - dsInfoImpl + - " to recover main storage" - ); - fsm.queue( - dsInfoImpl, - DeferredOp.ARCHIVE - ); + "Requesting archive of " + dsInfoImpl + " to recover main storage"); + fsm.queue(dsInfoImpl, DeferredOp.ARCHIVE); } if (os.size() < tidyBlockSize) { break; } low += tidyBlockSize; } - } catch ( - InternalException - | IcatException_Exception - | InsufficientPrivilegesException e - ) { + } catch (InternalException | IcatException_Exception | InsufficientPrivilegesException e) { // Log it and carry on - logger.error( - e.getClass() + " " + e.getMessage() - ); + logger.error(e.getClass() + " " + e.getMessage()); } } + } else if (storageUnit == StorageUnit.DATAFILE) { - List dfInfos = mainStorage.getDatafilesToArchive( - stopArchivingLevel, - startArchivingLevel - ); + List dfInfos = mainStorage.getDatafilesToArchive(stopArchivingLevel, + startArchivingLevel); for (DfInfo dfInfo : dfInfos) { - StringBuilder sb = new StringBuilder( - "SELECT df FROM Datafile df WHERE" - ); + StringBuilder sb = new StringBuilder("SELECT df FROM Datafile df WHERE"); boolean andNeeded = false; - andNeeded = - addNumericConstraint( - sb, - "df.id", - dfInfo.getDfId(), - andNeeded - ); - andNeeded = - addStringConstraint( - sb, - "df.createId", - dfInfo.getCreateId(), - andNeeded - ); - andNeeded = - addStringConstraint( - sb, - "df.modId", - dfInfo.getModId(), - andNeeded - ); + andNeeded = addNumericConstraint(sb, "df.id", dfInfo.getDfId(), andNeeded); + andNeeded = addStringConstraint(sb, "df.createId", dfInfo.getCreateId(), andNeeded); + andNeeded = addStringConstraint(sb, "df.modId", dfInfo.getModId(), andNeeded); if (key != null) { if (dfInfo.getDfLocation() != null) { if (andNeeded) { @@ -207,85 +114,42 @@ public void run() { sb.append(" "); andNeeded = true; } - sb.append( - "df.location" + - " LIKE '" + - dfInfo.getDfLocation() + - " %'" - ); + sb.append("df.location" + " LIKE '" + dfInfo.getDfLocation() + " %'"); } } else { - andNeeded = - addStringConstraint( - sb, - "df.location", - dfInfo.getDfLocation(), - andNeeded - ); + andNeeded = addStringConstraint(sb, "df.location", + + dfInfo.getDfLocation(), andNeeded); } - andNeeded = - addStringConstraint( - sb, - "df.name", - dfInfo.getDfName(), - andNeeded - ); + andNeeded = addStringConstraint(sb, "df.name", dfInfo.getDfName(), andNeeded); sb.append(" INCLUDE df.dataset"); try { int low = 0; while (true) { - String query = - sb.toString() + - " LIMIT " + - low + - "," + - tidyBlockSize; + String query = sb.toString() + " LIMIT " + low + "," + tidyBlockSize; List os = reader.search(query); - logger.debug( - query + - " returns " + - os.size() + - " datafiles" - ); + logger.debug(query + " returns " + os.size() + " datafiles"); for (Object o : os) { Datafile df = (Datafile) o; - DatafileInfo dfInfoImpl = new DatafileInfo( - df.getId(), - df.getName(), - LocationHelper.getLocation( - df.getId(), - df.getLocation() - ), - df.getCreateId(), - df.getModId(), - df.getDataset().getId() - ); + DatafileInfo dfInfoImpl = new DatafileInfo(df.getId(), df.getName(), + + LocationHelper.getLocation(df.getId(), df.getLocation()), df.getCreateId(), + df.getModId(), df.getDataset().getId()); + logger.debug( - "Requesting archive of " + - dfInfoImpl + - " to recover main storage" - ); - fsm.queue( - dfInfoImpl, - DeferredOp.ARCHIVE - ); + "Requesting archive of " + dfInfoImpl + " to recover main storage"); + fsm.queue(dfInfoImpl, DeferredOp.ARCHIVE); } if (os.size() < tidyBlockSize) { break; } low += tidyBlockSize; } - } catch ( - InternalException - | IcatException_Exception - | InsufficientPrivilegesException e - ) { + } catch (InternalException | IcatException_Exception | InsufficientPrivilegesException e) { // Log it and carry on - logger.error( - e.getClass() + " " + e.getMessage() - ); + logger.error(e.getClass() + " " + e.getMessage()); } } } @@ -296,16 +160,12 @@ public void run() { timer.schedule(new Action(), sizeCheckIntervalMillis); } } + } - private static final Logger logger = LoggerFactory.getLogger(Tidier.class); + private final static Logger logger = LoggerFactory.getLogger(Tidier.class); - static boolean addStringConstraint( - StringBuilder sb, - String var, - String value, - boolean andNeeded - ) { + static boolean addStringConstraint(StringBuilder sb, String var, String value, boolean andNeeded) { if (value != null) { if (andNeeded) { sb.append(" AND "); @@ -318,12 +178,7 @@ static boolean addStringConstraint( return andNeeded; } - static boolean addNumericConstraint( - StringBuilder sb, - String var, - Long value, - boolean andNeeded - ) { + static boolean addNumericConstraint(StringBuilder sb, String var, Long value, boolean andNeeded) { if (value != null) { if (andNeeded) { sb.append(" AND "); @@ -336,8 +191,8 @@ static boolean addNumericConstraint( return andNeeded; } - static void cleanPreparedDir(Path preparedDir, int preparedCount) - throws IOException { + static void cleanPreparedDir(Path preparedDir, int preparedCount) throws IOException { + Map dateMap = new HashMap<>(); File[] files = preparedDir.toFile().listFiles(); int ndel = files.length - preparedCount; @@ -394,17 +249,13 @@ public void exit() { @PostConstruct public void init() { try { + PropertyHandler propertyHandler = PropertyHandler.getInstance(); - FiniteStateMachine.createInstance( - reader, - lockManager, - propertyHandler.getStorageUnit() - ); + FiniteStateMachine.createInstance(reader, lockManager, propertyHandler.getStorageUnit()); this.fsm = FiniteStateMachine.getInstance(); this.fsm.init(); //if not yet initialized by IdsService do it now - - sizeCheckIntervalMillis = - propertyHandler.getSizeCheckIntervalMillis(); + + sizeCheckIntervalMillis = propertyHandler.getSizeCheckIntervalMillis(); preparedCount = propertyHandler.getPreparedCount(); preparedDir = propertyHandler.getCacheDir().resolve("prepared"); Files.createDirectories(preparedDir); @@ -422,9 +273,9 @@ public void init() { logger.info("Tidier started"); } catch (IOException e) { - throw new RuntimeException( - "Tidier reports " + e.getClass() + " " + e.getMessage() - ); + throw new RuntimeException("Tidier reports " + e.getClass() + " " + e.getMessage()); } + } + } diff --git a/src/main/java/org/icatproject/ids/enums/CallType.java b/src/main/java/org/icatproject/ids/enums/CallType.java index 2f3edbd8..835e9956 100644 --- a/src/main/java/org/icatproject/ids/enums/CallType.java +++ b/src/main/java/org/icatproject/ids/enums/CallType.java @@ -1,9 +1,5 @@ package org.icatproject.ids.enums; public enum CallType { - INFO, - PREPARE, - READ, - WRITE, - MIGRATE, + INFO, PREPARE, READ, WRITE, MIGRATE } diff --git a/src/main/java/org/icatproject/ids/enums/DeferredOp.java b/src/main/java/org/icatproject/ids/enums/DeferredOp.java index ef159a29..3cda8707 100644 --- a/src/main/java/org/icatproject/ids/enums/DeferredOp.java +++ b/src/main/java/org/icatproject/ids/enums/DeferredOp.java @@ -6,8 +6,5 @@ * decides what real action should be performed on data entities. */ public enum DeferredOp { - ARCHIVE, - RESTORE, - WRITE, - DELETE, + ARCHIVE, RESTORE, WRITE, DELETE } diff --git a/src/main/java/org/icatproject/ids/enums/RequestIdNames.java b/src/main/java/org/icatproject/ids/enums/RequestIdNames.java index 3432a218..8bfdb689 100644 --- a/src/main/java/org/icatproject/ids/enums/RequestIdNames.java +++ b/src/main/java/org/icatproject/ids/enums/RequestIdNames.java @@ -1,7 +1,7 @@ package org.icatproject.ids.enums; public class RequestIdNames { - public static final String sessionId = "sessionId"; public static final String preparedId = "preparedId"; + } diff --git a/src/main/java/org/icatproject/ids/enums/RequestType.java b/src/main/java/org/icatproject/ids/enums/RequestType.java index 9b154e8b..a79f83e5 100644 --- a/src/main/java/org/icatproject/ids/enums/RequestType.java +++ b/src/main/java/org/icatproject/ids/enums/RequestType.java @@ -4,20 +4,20 @@ * This enum contains all defined types of requests to this server */ public enum RequestType { - GETDATA, - ARCHIVE, - GETICATURL, - GETDATAFILEIDS, - GETSERVICESTATUS, - GETSIZE, - GETSTATUS, - ISPREPARED, - ISREADONLY, - ISTWOLEVEL, - PREPAREDATA, + GETDATA, + ARCHIVE, + GETICATURL, + GETDATAFILEIDS, + GETSERVICESTATUS, + GETSIZE, + GETSTATUS, + ISPREPARED, + ISREADONLY, + ISTWOLEVEL, + PREPAREDATA, PUT, RESET, RESTORE, WRITE, - DELETE, + DELETE } diff --git a/src/main/java/org/icatproject/ids/enums/Status.java b/src/main/java/org/icatproject/ids/enums/Status.java index 8b5392f9..abc187aa 100644 --- a/src/main/java/org/icatproject/ids/enums/Status.java +++ b/src/main/java/org/icatproject/ids/enums/Status.java @@ -5,7 +5,5 @@ * They are only used when returning status to the client, nowhere else. */ public enum Status { - ONLINE, - RESTORING, - ARCHIVED, + ONLINE, RESTORING, ARCHIVED } diff --git a/src/main/java/org/icatproject/ids/enums/StorageUnit.java b/src/main/java/org/icatproject/ids/enums/StorageUnit.java index 43aab195..bd5779a3 100644 --- a/src/main/java/org/icatproject/ids/enums/StorageUnit.java +++ b/src/main/java/org/icatproject/ids/enums/StorageUnit.java @@ -1,6 +1,5 @@ package org.icatproject.ids.enums; public enum StorageUnit { - DATASET, - DATAFILE, + DATASET, DATAFILE } diff --git a/src/main/java/org/icatproject/ids/enums/ValueContainerType.java b/src/main/java/org/icatproject/ids/enums/ValueContainerType.java index dd4cf64f..cf7501ac 100644 --- a/src/main/java/org/icatproject/ids/enums/ValueContainerType.java +++ b/src/main/java/org/icatproject/ids/enums/ValueContainerType.java @@ -4,12 +4,5 @@ * This enum provides all possible values of a ValueContainer */ public enum ValueContainerType { - INVALID, - VOID, - INT, - LONG, - BOOL, - STRING, - RESPONSE, - INPUTSTREAM, + INVALID, VOID, INT, LONG, BOOL, STRING, RESPONSE, INPUTSTREAM } diff --git a/src/main/java/org/icatproject/ids/exceptions/DataNotOnlineException.java b/src/main/java/org/icatproject/ids/exceptions/DataNotOnlineException.java index 7afd5771..567f1257 100644 --- a/src/main/java/org/icatproject/ids/exceptions/DataNotOnlineException.java +++ b/src/main/java/org/icatproject/ids/exceptions/DataNotOnlineException.java @@ -8,4 +8,5 @@ public class DataNotOnlineException extends IdsException { public DataNotOnlineException(String msg) { super(HttpURLConnection.HTTP_UNAVAILABLE, msg); } + } diff --git a/src/main/java/org/icatproject/ids/exceptions/IdsException.java b/src/main/java/org/icatproject/ids/exceptions/IdsException.java index bfc13258..007baf4d 100644 --- a/src/main/java/org/icatproject/ids/exceptions/IdsException.java +++ b/src/main/java/org/icatproject/ids/exceptions/IdsException.java @@ -7,7 +7,6 @@ */ @SuppressWarnings("serial") public class IdsException extends Exception { - private int httpStatusCode; private String message; @@ -27,4 +26,5 @@ public int getHttpStatusCode() { public String getMessage() { return "(" + httpStatusCode + ") : " + message; } + } diff --git a/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java index be9d1f25..77920041 100644 --- a/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java @@ -1,11 +1,12 @@ package org.icatproject.ids.exceptions; +import java.io.ByteArrayOutputStream; + import jakarta.json.Json; import jakarta.json.stream.JsonGenerator; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; -import java.io.ByteArrayOutputStream; /** * Capture any {@link org.icatproject.ids.exceptions.IdsException WebServiceException} thrown from @@ -19,14 +20,9 @@ public class IdsExceptionMapper implements ExceptionMapper { public Response toResponse(IdsException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen - .writeStartObject() - .write("code", e.getClass().getSimpleName()) - .write("message", e.getShortMessage()); + gen.writeStartObject().write("code", e.getClass().getSimpleName()) + .write("message", e.getShortMessage()); gen.writeEnd().close(); - return Response - .status(e.getHttpStatusCode()) - .entity(baos.toString()) - .build(); + return Response.status(e.getHttpStatusCode()).entity(baos.toString()).build(); } } diff --git a/src/main/java/org/icatproject/ids/exceptions/InsufficientStorageException.java b/src/main/java/org/icatproject/ids/exceptions/InsufficientStorageException.java index 01b60e9c..360eae14 100644 --- a/src/main/java/org/icatproject/ids/exceptions/InsufficientStorageException.java +++ b/src/main/java/org/icatproject/ids/exceptions/InsufficientStorageException.java @@ -3,7 +3,6 @@ import java.net.HttpURLConnection; public class InsufficientStorageException extends IdsException { - private static final long serialVersionUID = 1L; public InsufficientStorageException(String message) { diff --git a/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java index 7fb6ecfc..f9157c92 100644 --- a/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java @@ -1,37 +1,29 @@ package org.icatproject.ids.exceptions; +import java.io.ByteArrayOutputStream; + import jakarta.json.Json; import jakarta.json.stream.JsonGenerator; import jakarta.ws.rs.NotFoundException; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; -import java.io.ByteArrayOutputStream; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Provider -public class NotFoundExceptionMapper - implements ExceptionMapper { +public class NotFoundExceptionMapper implements ExceptionMapper { - private static Logger logger = LoggerFactory.getLogger( - NotFoundExceptionMapper.class - ); + private static Logger logger = LoggerFactory.getLogger(NotFoundExceptionMapper.class); @Override public Response toResponse(NotFoundException e) { logger.info("Processing: " + e.getClass() + " " + e.getMessage(), e); ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen - .writeStartObject() - .write("code", "NOT_IMPLEMENTED") - .write("message", "Operation not implemented by this IDS server.") - .writeEnd() - .close(); - return Response - .status(Response.Status.NOT_IMPLEMENTED) - .entity(baos.toString()) - .build(); + gen.writeStartObject().write("code", "NOT_IMPLEMENTED") + .write("message", "Operation not implemented by this IDS server.").writeEnd().close(); + return Response.status(Response.Status.NOT_IMPLEMENTED).entity(baos.toString()).build(); } } diff --git a/src/main/java/org/icatproject/ids/exceptions/NotImplementedException.java b/src/main/java/org/icatproject/ids/exceptions/NotImplementedException.java index aef12495..802d4fe6 100644 --- a/src/main/java/org/icatproject/ids/exceptions/NotImplementedException.java +++ b/src/main/java/org/icatproject/ids/exceptions/NotImplementedException.java @@ -8,4 +8,5 @@ public class NotImplementedException extends IdsException { public NotImplementedException(String message) { super(HttpURLConnection.HTTP_NOT_IMPLEMENTED, message); } + } diff --git a/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java index f3c9c6ce..aa38ba0a 100644 --- a/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java @@ -1,40 +1,34 @@ package org.icatproject.ids.exceptions; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.net.HttpURLConnection; + import jakarta.json.Json; import jakarta.json.stream.JsonGenerator; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.net.HttpURLConnection; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Provider -public class RuntimeExceptionMapper - implements ExceptionMapper { +public class RuntimeExceptionMapper implements ExceptionMapper { - private static final Logger logger = LoggerFactory.getLogger( - RuntimeExceptionMapper.class - ); + private final static Logger logger = LoggerFactory.getLogger(RuntimeExceptionMapper.class); @Override public Response toResponse(RuntimeException e) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(baos)); logger.error("Processing: " + baos.toString()); baos.reset(); JsonGenerator gen = Json.createGenerator(baos); - gen - .writeStartObject() - .write("code", "InternalException") - .write("message", e.getClass() + " " + e.getMessage()) - .writeEnd() - .close(); - return Response - .status(HttpURLConnection.HTTP_INTERNAL_ERROR) - .entity(baos.toString()) - .build(); + gen.writeStartObject().write("code", "InternalException") + .write("message", e.getClass() + " " + e.getMessage()).writeEnd().close(); + return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR).entity(baos.toString()) + .build(); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java index 2cebb277..f115c8da 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java @@ -1,7 +1,5 @@ package org.icatproject.ids.finiteStateMachine; -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.Files; @@ -14,19 +12,24 @@ import java.util.Set; import java.util.Timer; import java.util.concurrent.ConcurrentHashMap; + +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.IcatReader; import org.icatproject.ids.services.LockManager; -import org.icatproject.ids.services.LockManager.LockInfo; import org.icatproject.ids.services.PropertyHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.icatproject.ids.services.LockManager.LockInfo; public abstract class FiniteStateMachine { + private static FiniteStateMachine instance; private static Boolean inited = false; @@ -36,57 +39,36 @@ protected FiniteStateMachine(IcatReader reader, LockManager lockManager) { this.lockManager = lockManager; } - public static void createInstance( - IcatReader reader, - LockManager lockManager, - StorageUnit storageUnit - ) { - if (instance == null) { - if (storageUnit == StorageUnit.DATAFILE) instance = - new FiniteStateMachineForStorageUnitDatafile( - reader, - lockManager - ); else if (storageUnit == StorageUnit.DATASET) instance = - new FiniteStateMachineForStorageUnitDataset( - reader, - lockManager - ); else instance = - new FiniteStateMachineForSingleLevelStorage( - reader, - lockManager - ); + public static void createInstance(IcatReader reader, LockManager lockManager, StorageUnit storageUnit) { + + if(instance == null) { + if(storageUnit == StorageUnit.DATAFILE) + instance = new FiniteStateMachineForStorageUnitDatafile(reader, lockManager); + else if(storageUnit == StorageUnit.DATASET) + instance = new FiniteStateMachineForStorageUnitDataset(reader, lockManager); + else + instance = new FiniteStateMachineForSingleLevelStorage(reader, lockManager); } } public static FiniteStateMachine getInstance() { - if (instance != null) { + if(instance != null) { return instance; } // If this assert was executed: Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once. - throw new RuntimeException( - "Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once." - ); + throw new RuntimeException("Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once."); } - public abstract void queue(DataInfoBase dataInfo, DeferredOp deferredOp) - throws InternalException; - + public abstract void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException; protected abstract void scheduleTimer(); - protected abstract void addDataInfoJson(JsonGenerator gen); public enum RequestedState { - ARCHIVE_REQUESTED, - DELETE_REQUESTED, - RESTORE_REQUESTED, - WRITE_REQUESTED, - WRITE_THEN_ARCHIVE_REQUESTED, + ARCHIVE_REQUESTED, DELETE_REQUESTED, RESTORE_REQUESTED, WRITE_REQUESTED, WRITE_THEN_ARCHIVE_REQUESTED } - protected static Logger logger = LoggerFactory.getLogger( - FiniteStateMachine.class - ); + protected static Logger logger = LoggerFactory.getLogger(FiniteStateMachine.class); /* * Note that the veriable processOpsDelayMillis is used to either delay all deferred @@ -163,32 +145,23 @@ protected void addDataInfoJsonFromDeferredOpsQueue(JsonGenerator gen) { gen.writeStartArray("opsQueue"); for (Entry entry : union.entrySet()) { DataInfoBase item = entry.getKey(); - gen - .writeStartObject() - .write("data", item.toString()) - .write("request", entry.getValue().name()) - .writeEnd(); + gen.writeStartObject().write("data", item.toString()).write("request", entry.getValue().name()) + .writeEnd(); } gen.writeEnd(); // end Array("opsQueue") } public String getServiceStatus() throws InternalException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try ( - JsonGenerator gen = Json.createGenerator(baos).writeStartObject() - ) { + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + this.addDataInfoJson(gen); Collection lockInfo = lockManager.getLockInfo(); gen.write("lockCount", lockInfo.size()); gen.writeStartArray("locks"); for (LockInfo li : lockInfo) { - gen - .writeStartObject() - .write("id", li.id) - .write("type", li.type.name()) - .write("count", li.count) - .writeEnd(); + gen.writeStartObject().write("id", li.id).write("type", li.type.name()).write("count", li.count).writeEnd(); } gen.writeEnd(); // end Array("locks") @@ -203,30 +176,26 @@ public String getServiceStatus() throws InternalException { return baos.toString(); } + public void init() { + try { synchronized (inited) { - if (!inited) { + if(!inited) { propertyHandler = PropertyHandler.getInstance(); - processQueueIntervalMillis = - propertyHandler.getProcessQueueIntervalSeconds() * - 1000L; + processQueueIntervalMillis = propertyHandler.getProcessQueueIntervalSeconds() * 1000L; this.scheduleTimer(); - + markerDir = propertyHandler.getCacheDir().resolve("marker"); Files.createDirectories(markerDir); inited = true; } } } catch (IOException e) { - throw new RuntimeException( - "FiniteStateMachine reports " + - e.getClass() + - " " + - e.getMessage() - ); + throw new RuntimeException("FiniteStateMachine reports " + e.getClass() + " " + e.getMessage()); } + } public void removeFromChanging(DataInfoBase dfInfo) { @@ -252,4 +221,5 @@ public void checkFailure(Long id) throws InternalException { throw new InternalException("Restore failed"); } } + } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java index 081d8a4e..7ffb349d 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java @@ -1,37 +1,35 @@ package org.icatproject.ids.finiteStateMachine; -import jakarta.json.stream.JsonGenerator; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.IcatReader; import org.icatproject.ids.services.LockManager; -public class FiniteStateMachineForSingleLevelStorage - extends FiniteStateMachine { +import jakarta.json.stream.JsonGenerator; + +public class FiniteStateMachineForSingleLevelStorage extends FiniteStateMachine { - protected FiniteStateMachineForSingleLevelStorage( - IcatReader reader, - LockManager lockManager - ) { + protected FiniteStateMachineForSingleLevelStorage(IcatReader reader, LockManager lockManager) { super(reader, lockManager); } + @Override - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) - throws InternalException { - throw new InternalException( - "### Operation is not permitted for single level storage" - ); + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { + throw new InternalException("### Operation is not permitted for single level storage"); } + @Override protected void scheduleTimer() { //nothing to do here for single level storage } + @Override protected void addDataInfoJson(JsonGenerator gen) { gen.writeStartArray("opsQueue").writeEnd(); } + } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java index f949e284..88543bb1 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java @@ -1,6 +1,5 @@ package org.icatproject.ids.finiteStateMachine; -import jakarta.json.stream.JsonGenerator; import java.io.IOException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; @@ -12,12 +11,14 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; + import java.util.TimerTask; + import org.icatproject.Dataset; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.exceptions.InternalException; -import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatafileInfo; +import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.services.IcatReader; @@ -29,46 +30,40 @@ import org.icatproject.ids.thread.DfRestorer; import org.icatproject.ids.thread.DfWriter; -public class FiniteStateMachineForStorageUnitDatafile - extends FiniteStateMachine { +import jakarta.json.stream.JsonGenerator; + +public class FiniteStateMachineForStorageUnitDatafile extends FiniteStateMachine { - protected FiniteStateMachineForStorageUnitDatafile( - IcatReader icatReader, - LockManager lockManager - ) { + protected FiniteStateMachineForStorageUnitDatafile(IcatReader icatReader, LockManager lockManager) { super(icatReader, lockManager); } + @Override protected void scheduleTimer() { - processOpsDelayMillis = - propertyHandler.getDelayDatafileOperations() * 1000L; + processOpsDelayMillis = propertyHandler.getDelayDatafileOperations() * 1000L; timer.schedule(new DfProcessQueue(), processQueueIntervalMillis); - logger.info( - "DfProcessQueue scheduled to run in " + - processQueueIntervalMillis + - " milliseconds" - ); + logger.info("DfProcessQueue scheduled to run in " + processQueueIntervalMillis + " milliseconds"); } + @Override protected void addDataInfoJson(JsonGenerator gen) { this.addDataInfoJsonFromDeferredOpsQueue(gen); } - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) - throws InternalException { + + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { + var dfInfo = (DatafileInfo) dataInfo; - if (dfInfo == null) throw new InternalException( - "DataInfoBase object could not be casted into a DataFileInfo. Did you handed over a DataSetInfo instead?" - ); + if(dfInfo == null) throw new InternalException("DataInfoBase object could not be casted into a DataFileInfo. Did you handed over a DataSetInfo instead?"); logger.info("Requesting " + deferredOp + " of datafile " + dfInfo); synchronized (deferredOpsQueue) { + if (processOpsTime == null) { - processOpsTime = - System.currentTimeMillis() + processOpsDelayMillis; + processOpsTime = System.currentTimeMillis() + processOpsDelayMillis; final Date d = new Date(processOpsTime); logger.debug("Requesting delay operations till " + d); } @@ -77,100 +72,62 @@ public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) if (state == null) { if (deferredOp == DeferredOp.WRITE) { try { - Path marker = markerDir.resolve( - Long.toString(dfInfo.getId()) - ); + Path marker = markerDir.resolve(Long.toString(dfInfo.getId())); Files.createFile(marker); logger.debug("Created marker " + marker); } catch (FileAlreadyExistsException e) { // Pass will ignore this } catch (IOException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + throw new InternalException(e.getClass() + " " + e.getMessage()); } - deferredOpsQueue.put( - dfInfo, - RequestedState.WRITE_REQUESTED - ); + deferredOpsQueue.put(dfInfo, RequestedState.WRITE_REQUESTED); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put( - dfInfo, - RequestedState.ARCHIVE_REQUESTED - ); + deferredOpsQueue.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put( - dfInfo, - RequestedState.RESTORE_REQUESTED - ); + deferredOpsQueue.put(dfInfo, RequestedState.RESTORE_REQUESTED); } else if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put( - dfInfo, - RequestedState.DELETE_REQUESTED - ); + deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); } } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.RESTORE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put( - dfInfo, - RequestedState.DELETE_REQUESTED - ); + deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); } } else if (state == RequestedState.DELETE_REQUESTED) { // No way out } else if (state == RequestedState.RESTORE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put( - dfInfo, - RequestedState.DELETE_REQUESTED - ); + deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put( - dfInfo, - RequestedState.ARCHIVE_REQUESTED - ); + deferredOpsQueue.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put( - dfInfo, - RequestedState.WRITE_THEN_ARCHIVE_REQUESTED - ); + deferredOpsQueue.put(dfInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put( - dfInfo, - RequestedState.WRITE_REQUESTED - ); + deferredOpsQueue.put(dfInfo, RequestedState.WRITE_REQUESTED); } } } } + private class DfProcessQueue extends TimerTask { @Override public void run() { try { synchronized (deferredOpsQueue) { - if ( - processOpsTime != null && - System.currentTimeMillis() > processOpsTime && - !deferredOpsQueue.isEmpty() - ) { + if (processOpsTime != null && System.currentTimeMillis() > processOpsTime && !deferredOpsQueue.isEmpty()) { processOpsTime = null; - logger.debug( - "deferredDfOpsQueue has " + - deferredOpsQueue.size() + - " entries" - ); + logger.debug("deferredDfOpsQueue has " + deferredOpsQueue.size() + " entries"); List writes = new ArrayList<>(); List archives = new ArrayList<>(); List restores = new ArrayList<>(); @@ -181,31 +138,20 @@ public void run() { Map deleteLocks = new HashMap<>(); Map newOps = new HashMap<>(); - final Iterator> it = deferredOpsQueue - .entrySet() - .iterator(); + final Iterator> it = deferredOpsQueue.entrySet().iterator(); while (it.hasNext()) { Entry opEntry = it.next(); var dfInfo = (DatafileInfo) opEntry.getKey(); - if (dfInfo == null) throw new RuntimeException( - "Could not cast DataInfoBase to DataFileInfo. Did you handed over another sub type?" - ); + if(dfInfo == null) throw new RuntimeException("Could not cast DataInfoBase to DataFileInfo. Did you handed over another sub type?"); Long dsId = dfInfo.getDsId(); DatasetInfo dsInfo; try { - Dataset ds = (Dataset) reader.get( - "Dataset ds INCLUDE ds.investigation.facility", - dsId - ); + Dataset ds = (Dataset) reader.get("Dataset ds INCLUDE ds.investigation.facility", dsId); dsInfo = new DatasetInfo(ds); } catch (Exception e) { - logger.error( - "Could not get dsInfo {}: {}.", - dsId, - e.getMessage() - ); + logger.error("Could not get dsInfo {}: {}.", dsId, e.getMessage()); continue; } if (!dataInfoChanging.containsKey(dfInfo)) { @@ -214,168 +160,73 @@ public void run() { if (state == RequestedState.WRITE_REQUESTED) { if (!writeLocks.containsKey(dsId)) { try { - writeLocks.put( - dsId, - lockManager.lock( - dsInfo, - LockType.SHARED - ) - ); + writeLocks.put(dsId, lockManager.lock(dsInfo, LockType.SHARED)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " + - dsId + - ", hold back " + - state - ); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error( - "I/O exception " + - e.getMessage() + - " locking " + - dsId - ); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } it.remove(); dataInfoChanging.put(dfInfo, state); writes.add(dfInfo); - } else if ( - state == - RequestedState.WRITE_THEN_ARCHIVE_REQUESTED - ) { + } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (!writeLocks.containsKey(dsId)) { try { - writeLocks.put( - dsId, - lockManager.lock( - dsInfo, - LockType.SHARED - ) - ); + writeLocks.put(dsId, lockManager.lock(dsInfo, LockType.SHARED)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " + - dsId + - ", hold back " + - state - ); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error( - "I/O exception " + - e.getMessage() + - " locking " + - dsId - ); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } it.remove(); - dataInfoChanging.put( - dfInfo, - RequestedState.WRITE_REQUESTED - ); + dataInfoChanging.put(dfInfo, RequestedState.WRITE_REQUESTED); writes.add(dfInfo); - newOps.put( - dfInfo, - RequestedState.ARCHIVE_REQUESTED - ); - } else if ( - state == RequestedState.ARCHIVE_REQUESTED - ) { + newOps.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); + } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (!archiveLocks.containsKey(dsId)) { try { - archiveLocks.put( - dsId, - lockManager.lock( - dsInfo, - LockType.EXCLUSIVE - ) - ); + archiveLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " + - dsId + - ", hold back " + - state - ); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error( - "I/O exception " + - e.getMessage() + - " locking " + - dsId - ); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } it.remove(); dataInfoChanging.put(dfInfo, state); archives.add(dfInfo); - } else if ( - state == RequestedState.RESTORE_REQUESTED - ) { + } else if (state == RequestedState.RESTORE_REQUESTED) { if (!restoreLocks.containsKey(dsId)) { try { - restoreLocks.put( - dsId, - lockManager.lock( - dsInfo, - LockType.EXCLUSIVE - ) - ); + restoreLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " + - dsId + - ", hold back " + - state - ); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error( - "I/O exception " + - e.getMessage() + - " locking " + - dsId - ); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } it.remove(); dataInfoChanging.put(dfInfo, state); restores.add(dfInfo); - } else if ( - state == RequestedState.DELETE_REQUESTED - ) { + } else if (state == RequestedState.DELETE_REQUESTED) { if (!deleteLocks.containsKey(dsId)) { try { - deleteLocks.put( - dsId, - lockManager.lock( - dsInfo, - LockType.EXCLUSIVE - ) - ); + deleteLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " + - dsId + - ", hold back " + - state - ); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error( - "I/O exception " + - e.getMessage() + - " locking " + - dsId - ); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } @@ -383,94 +234,44 @@ public void run() { dataInfoChanging.put(dfInfo, state); deletes.add(dfInfo); } else { - throw new AssertionError( - "Impossible state" - ); + throw new AssertionError("Impossible state"); } } } if (!newOps.isEmpty()) { deferredOpsQueue.putAll(newOps); - logger.debug( - "Adding {} operations to be scheduled next time round", - newOps.size() - ); + logger.debug("Adding {} operations to be scheduled next time round", newOps.size()); } if (!deferredOpsQueue.isEmpty()) { processOpsTime = 0L; } if (!writes.isEmpty()) { - logger.debug( - "Launch thread to process " + - writes.size() + - " writes" - ); - Thread w = new Thread( - new DfWriter( - writes, - propertyHandler, - FiniteStateMachineForStorageUnitDatafile.this, - writeLocks.values() - ) - ); + logger.debug("Launch thread to process " + writes.size() + " writes"); + Thread w = new Thread(new DfWriter(writes, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, writeLocks.values())); w.start(); } if (!archives.isEmpty()) { - logger.debug( - "Launch thread to process " + - archives.size() + - " archives" - ); - Thread w = new Thread( - new DfArchiver( - archives, - propertyHandler, - FiniteStateMachineForStorageUnitDatafile.this, - archiveLocks.values() - ) - ); + logger.debug("Launch thread to process " + archives.size() + " archives"); + Thread w = new Thread(new DfArchiver(archives, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, archiveLocks.values())); w.start(); } if (!restores.isEmpty()) { - logger.debug( - "Launch thread to process " + - restores.size() + - " restores" - ); - Thread w = new Thread( - new DfRestorer( - restores, - propertyHandler, - FiniteStateMachineForStorageUnitDatafile.this, - restoreLocks.values() - ) - ); + logger.debug("Launch thread to process " + restores.size() + " restores"); + Thread w = new Thread(new DfRestorer(restores, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, restoreLocks.values())); w.start(); } if (!deletes.isEmpty()) { - logger.debug( - "Launch thread to process " + - deletes.size() + - " deletes" - ); - Thread w = new Thread( - new DfDeleter( - deletes, - propertyHandler, - FiniteStateMachineForStorageUnitDatafile.this, - deleteLocks.values() - ) - ); + logger.debug("Launch thread to process " + deletes.size() + " deletes"); + Thread w = new Thread(new DfDeleter(deletes, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, deleteLocks.values())); w.start(); } } } } finally { - timer.schedule( - new DfProcessQueue(), - processQueueIntervalMillis - ); + timer.schedule(new DfProcessQueue(), processQueueIntervalMillis); } + } + } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java index 10846beb..cbe0a5cd 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java @@ -1,6 +1,5 @@ package org.icatproject.ids.finiteStateMachine; -import jakarta.json.stream.JsonGenerator; import java.io.IOException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; @@ -11,6 +10,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.TimerTask; + import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; @@ -24,27 +24,22 @@ import org.icatproject.ids.thread.DsRestorer; import org.icatproject.ids.thread.DsWriter; -public class FiniteStateMachineForStorageUnitDataset - extends FiniteStateMachine { +import jakarta.json.stream.JsonGenerator; + +public class FiniteStateMachineForStorageUnitDataset extends FiniteStateMachine { protected Map writeTimes = new HashMap<>(); - protected FiniteStateMachineForStorageUnitDataset( - IcatReader reader, - LockManager lockManager - ) { + protected FiniteStateMachineForStorageUnitDataset(IcatReader reader, LockManager lockManager) { super(reader, lockManager); } + @Override protected void scheduleTimer() { processOpsDelayMillis = propertyHandler.getDelayDatasetWrites() * 1000L; timer.schedule(new DsProcessQueue(), processQueueIntervalMillis); - logger.info( - "DsProcessQueue scheduled to run in " + - processQueueIntervalMillis + - " milliseconds" - ); + logger.info("DsProcessQueue scheduled to run in " + processQueueIntervalMillis + " milliseconds"); } @Override @@ -52,70 +47,54 @@ protected void addDataInfoJson(JsonGenerator gen) { this.addDataInfoJsonFromDeferredOpsQueue(gen); } - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) - throws InternalException { + + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { + + logger.info("Requesting " + deferredOp + " of dataset " + dataInfo); synchronized (deferredOpsQueue) { + final RequestedState state = this.deferredOpsQueue.get(dataInfo); if (state == null) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put( - dataInfo, - RequestedState.ARCHIVE_REQUESTED - ); + deferredOpsQueue.put(dataInfo, RequestedState.ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put( - dataInfo, - RequestedState.RESTORE_REQUESTED - ); + deferredOpsQueue.put(dataInfo, RequestedState.RESTORE_REQUESTED); } } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); - deferredOpsQueue.put( - dataInfo, - RequestedState.WRITE_THEN_ARCHIVE_REQUESTED - ); + deferredOpsQueue.put(dataInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put( - dataInfo, - RequestedState.RESTORE_REQUESTED - ); + deferredOpsQueue.put(dataInfo, RequestedState.RESTORE_REQUESTED); } } else if (state == RequestedState.RESTORE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put( - dataInfo, - RequestedState.ARCHIVE_REQUESTED - ); + deferredOpsQueue.put(dataInfo, RequestedState.ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { setDelay(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put( - dataInfo, - RequestedState.WRITE_THEN_ARCHIVE_REQUESTED - ); + deferredOpsQueue.put(dataInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { setDelay(dataInfo); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put( - dataInfo, - RequestedState.WRITE_REQUESTED - ); + deferredOpsQueue.put(dataInfo, RequestedState.WRITE_REQUESTED); } } } + } + private void requestWrite(DataInfoBase dsInfo) throws InternalException { try { Path marker = markerDir.resolve(Long.toString(dsInfo.getId())); @@ -130,22 +109,16 @@ private void requestWrite(DataInfoBase dsInfo) throws InternalException { setDelay(dsInfo); } + private void setDelay(DataInfoBase dsInfo) { - writeTimes.put( - dsInfo, - System.currentTimeMillis() + processOpsDelayMillis - ); + writeTimes.put(dsInfo, System.currentTimeMillis() + processOpsDelayMillis); if (logger.isDebugEnabled()) { final Date d = new Date(writeTimes.get(dsInfo)); - logger.debug( - "Requesting delay of writing of dataset " + - dsInfo + - " till " + - d - ); + logger.debug("Requesting delay of writing of dataset " + dsInfo + " till " + d); } } + private class DsProcessQueue extends TimerTask { @Override @@ -154,169 +127,77 @@ public void run() { synchronized (deferredOpsQueue) { final long now = System.currentTimeMillis(); Map newOps = new HashMap<>(); - final Iterator> it = deferredOpsQueue - .entrySet() - .iterator(); + final Iterator> it = deferredOpsQueue.entrySet().iterator(); while (it.hasNext()) { final Entry opEntry = it.next(); final var dsInfo = (DatasetInfo) opEntry.getKey(); - if (dsInfo == null) throw new RuntimeException( - "Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?" - ); + + if(dsInfo == null) throw new RuntimeException("Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); if (!dataInfoChanging.containsKey(dsInfo)) { final RequestedState state = opEntry.getValue(); - if ( - state == RequestedState.WRITE_REQUESTED || - state == - RequestedState.WRITE_THEN_ARCHIVE_REQUESTED - ) { + if (state == RequestedState.WRITE_REQUESTED + || state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (now > writeTimes.get(dsInfo)) { try { - Lock lock = lockManager.lock( - dsInfo, - LockType.SHARED - ); - logger.debug( - "Will process " + - dsInfo + - " with " + - state - ); + Lock lock = lockManager.lock(dsInfo, LockType.SHARED); + logger.debug("Will process " + dsInfo + " with " + state); writeTimes.remove(dsInfo); - dataInfoChanging.put( - dsInfo, - RequestedState.WRITE_REQUESTED - ); + dataInfoChanging.put(dsInfo, RequestedState.WRITE_REQUESTED); it.remove(); final Thread w = new Thread( - new DsWriter( - dsInfo, - propertyHandler, - FiniteStateMachineForStorageUnitDataset.this, - reader, - lock - ) - ); + new DsWriter(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, reader, lock)); w.start(); - if ( - state == - RequestedState.WRITE_THEN_ARCHIVE_REQUESTED - ) { - newOps.put( - dsInfo, - RequestedState.ARCHIVE_REQUESTED - ); + if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { + newOps.put(dsInfo, RequestedState.ARCHIVE_REQUESTED); } } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " + - dsInfo + - ", hold back process with " + - state - ); + logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); } catch (IOException e) { - logger.error( - "I/O exception " + - e.getMessage() + - " locking " + - dsInfo - ); + logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); } } - } else if ( - state == RequestedState.ARCHIVE_REQUESTED - ) { + } else if (state == RequestedState.ARCHIVE_REQUESTED) { try { - Lock lock = lockManager.lock( - dsInfo, - LockType.EXCLUSIVE - ); + Lock lock = lockManager.lock(dsInfo, LockType.EXCLUSIVE); it.remove(); - logger.debug( - "Will process " + - dsInfo + - " with " + - state - ); + logger.debug("Will process " + dsInfo + " with " + state); dataInfoChanging.put(dsInfo, state); final Thread w = new Thread( - new DsArchiver( - dsInfo, - propertyHandler, - FiniteStateMachineForStorageUnitDataset.this, - lock - ) - ); + new DsArchiver(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, lock)); w.start(); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " + - dsInfo + - ", hold back process with " + - state - ); + logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); } catch (IOException e) { - logger.error( - "I/O exception " + - e.getMessage() + - " locking " + - dsInfo - ); + logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); } - } else if ( - state == RequestedState.RESTORE_REQUESTED - ) { + } else if (state == RequestedState.RESTORE_REQUESTED) { try { - Lock lock = lockManager.lock( - dsInfo, - LockType.EXCLUSIVE - ); - logger.debug( - "Will process " + - dsInfo + - " with " + - state - ); + Lock lock = lockManager.lock(dsInfo, LockType.EXCLUSIVE); + logger.debug("Will process " + dsInfo + " with " + state); dataInfoChanging.put(dsInfo, state); it.remove(); final Thread w = new Thread( - new DsRestorer( - dsInfo, - propertyHandler, - FiniteStateMachineForStorageUnitDataset.this, - reader, - lock - ) - ); + new DsRestorer(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, reader, lock)); w.start(); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " + - dsInfo + - ", hold back process with " + - state - ); + logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); } catch (IOException e) { - logger.error( - "I/O exception " + - e.getMessage() + - " locking " + - dsInfo - ); + logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); } } } } deferredOpsQueue.putAll(newOps); } + } finally { - timer.schedule( - new DsProcessQueue(), - processQueueIntervalMillis - ); + timer.schedule(new DsProcessQueue(), processQueueIntervalMillis); } + } + } + } diff --git a/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java b/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java index e95e3165..8c813be8 100644 --- a/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java +++ b/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java @@ -1,19 +1,19 @@ package org.icatproject.ids.helpers; +import java.io.IOException; + import jakarta.ws.rs.container.ContainerRequestContext; import jakarta.ws.rs.container.ContainerResponseContext; import jakarta.ws.rs.container.ContainerResponseFilter; import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.ext.Provider; -import java.io.IOException; @Provider public class CORSResponseFilter implements ContainerResponseFilter { - public void filter( - ContainerRequestContext requestContext, - ContainerResponseContext responseContext - ) throws IOException { + public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) + throws IOException { + MultivaluedMap headers = responseContext.getHeaders(); headers.add("Access-Control-Allow-Origin", "*"); headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); diff --git a/src/main/java/org/icatproject/ids/helpers/CheckedWithSizeInputStream.java b/src/main/java/org/icatproject/ids/helpers/CheckedWithSizeInputStream.java index f626bde8..d3d409e4 100644 --- a/src/main/java/org/icatproject/ids/helpers/CheckedWithSizeInputStream.java +++ b/src/main/java/org/icatproject/ids/helpers/CheckedWithSizeInputStream.java @@ -6,7 +6,6 @@ import java.util.zip.Checksum; public class CheckedWithSizeInputStream extends FilterInputStream { - private Checksum cksum; private long size; diff --git a/src/main/java/org/icatproject/ids/helpers/Constants.java b/src/main/java/org/icatproject/ids/helpers/Constants.java index 85011272..bc7d4aaa 100644 --- a/src/main/java/org/icatproject/ids/helpers/Constants.java +++ b/src/main/java/org/icatproject/ids/helpers/Constants.java @@ -5,13 +5,12 @@ import java.util.Properties; public class Constants { - public static String API_VERSION; static { - InputStream inputStream = - Constants.class.getClassLoader() - .getResourceAsStream("app.properties"); + + InputStream inputStream = Constants.class.getClassLoader().getResourceAsStream( + "app.properties"); Properties p = new Properties(); try { @@ -20,5 +19,6 @@ public class Constants { } catch (IOException e) { // API Version will be null } + } } diff --git a/src/main/java/org/icatproject/ids/helpers/LocationHelper.java b/src/main/java/org/icatproject/ids/helpers/LocationHelper.java index 052e1c00..b3c1e8b5 100644 --- a/src/main/java/org/icatproject/ids/helpers/LocationHelper.java +++ b/src/main/java/org/icatproject/ids/helpers/LocationHelper.java @@ -1,6 +1,7 @@ package org.icatproject.ids.helpers; import java.security.NoSuchAlgorithmException; + import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.services.ServiceProvider; @@ -9,7 +10,7 @@ public class LocationHelper { public static String getLocation(long dfid, String location) - throws InsufficientPrivilegesException, InternalException { + throws InsufficientPrivilegesException, InternalException { if (location == null) { throw new InternalException("location is null"); } @@ -22,27 +23,20 @@ public static String getLocation(long dfid, String location) } } - public static String getLocationFromDigest( - long id, - String locationWithHash, - String key - ) throws InternalException, InsufficientPrivilegesException { + + public static String getLocationFromDigest(long id, String locationWithHash, String key) + throws InternalException, InsufficientPrivilegesException { int i = locationWithHash.lastIndexOf(' '); try { String location = locationWithHash.substring(0, i); String hash = locationWithHash.substring(i + 1); if (!hash.equals(IcatSecurity.digest(id, location, key))) { throw new InsufficientPrivilegesException( - "Location \"" + - locationWithHash + - "\" does not contain a valid hash." - ); + "Location \"" + locationWithHash + "\" does not contain a valid hash."); } return location; } catch (IndexOutOfBoundsException e) { - throw new InsufficientPrivilegesException( - "Location \"" + locationWithHash + "\" does not contain hash." - ); + throw new InsufficientPrivilegesException("Location \"" + locationWithHash + "\" does not contain hash."); } catch (NoSuchAlgorithmException e) { throw new InternalException(e.getMessage()); } diff --git a/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java b/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java index 58a3374b..ffaa34be 100644 --- a/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java +++ b/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java @@ -31,12 +31,12 @@ public RangeOutputStream(OutputStream os, long min, Long count) { super(os); this.min = min; this.max = count == null ? Long.MAX_VALUE : min + count; - if (min < 0) throw new IllegalArgumentException( - "min must be a non-negative long" - ); - if (count != null && count < 0) throw new IllegalArgumentException( - "count must be a non-negative long" - ); + if (min < 0) + throw new IllegalArgumentException( + "min must be a non-negative long"); + if (count != null && count < 0) + throw new IllegalArgumentException( + "count must be a non-negative long"); } /* @@ -61,13 +61,8 @@ public void write(int b) throws IOException { public void write(byte[] b, int off, final int len) throws IOException { if (b == null) { throw new NullPointerException(); - } else if ( - (off < 0) || - (off > b.length) || - (len < 0) || - ((off + len) > b.length) || - ((off + len) < 0) - ) { + } else if ((off < 0) || (off > b.length) || (len < 0) + || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; @@ -85,4 +80,5 @@ public void write(byte[] b, int off, final int len) throws IOException { } pos += len; } + } diff --git a/src/main/java/org/icatproject/ids/helpers/SO.java b/src/main/java/org/icatproject/ids/helpers/SO.java index 176d2ba5..58e1ee4a 100644 --- a/src/main/java/org/icatproject/ids/helpers/SO.java +++ b/src/main/java/org/icatproject/ids/helpers/SO.java @@ -1,8 +1,5 @@ package org.icatproject.ids.helpers; -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; -import jakarta.ws.rs.core.StreamingOutput; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -12,14 +9,19 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipOutputStream; -import org.icatproject.ids.models.DataInfoBase; + import org.icatproject.ids.models.DatafileInfo; +import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.ServiceProvider; +import org.icatproject.ids.services.LockManager.Lock; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; +import jakarta.ws.rs.core.StreamingOutput; + public class SO implements StreamingOutput { private long offset; @@ -34,20 +36,10 @@ public class SO implements StreamingOutput { private ServiceProvider serviceProvider; private static final int BUFSIZ = 2048; - private static final Logger logger = LoggerFactory.getLogger(SO.class); + private final static Logger logger = LoggerFactory.getLogger(SO.class); - public SO( - Map dsInfos, - Map dfInfos, - long offset, - boolean zip, - boolean compress, - Lock lock, - Long transferId, - String ip, - long start, - ServiceProvider serviceProvider - ) { + public SO(Map dsInfos, Map dfInfos, long offset, boolean zip, boolean compress, + Lock lock, Long transferId, String ip, long start, ServiceProvider serviceProvider) { this.offset = offset; this.zip = zip; this.dsInfos = dsInfos; @@ -62,6 +54,7 @@ public SO( @Override public void write(OutputStream output) throws IOException { + Object transfer = "??"; try { if (offset != 0) { // Wrap the stream if needed @@ -69,35 +62,21 @@ public void write(OutputStream output) throws IOException { } byte[] bytes = new byte[BUFSIZ]; if (zip) { - ZipOutputStream zos = new ZipOutputStream( - new BufferedOutputStream(output) - ); + ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(output)); if (!compress) { zos.setLevel(0); // Otherwise use default compression } - for (DataInfoBase dataInfo : dfInfos.values()) { + for ( DataInfoBase dataInfo : dfInfos.values()) { var dfInfo = (DatafileInfo) dataInfo; logger.debug("Adding " + dfInfo + " to zip"); transfer = dfInfo; - DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId()); - String entryName = - this.serviceProvider.getPropertyHandler() - .getZipMapper() - .getFullEntryName( - (DatasetInfo) dsInfo, - (DatafileInfo) dfInfo - ); + DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId() ); + String entryName = this.serviceProvider.getPropertyHandler().getZipMapper().getFullEntryName((DatasetInfo)dsInfo, (DatafileInfo)dfInfo); InputStream stream = null; try { zos.putNextEntry(new ZipEntry(entryName)); - stream = - this.serviceProvider.getMainStorage() - .get( - dfInfo.getLocation(), - dfInfo.getCreateId(), - dfInfo.getModId() - ); + stream = this.serviceProvider.getMainStorage().get(dfInfo.getLocation(), dfInfo.getCreateId(), dfInfo.getModId()); int length; while ((length = stream.read(bytes)) >= 0) { zos.write(bytes, 0, length); @@ -112,18 +91,10 @@ public void write(OutputStream output) throws IOException { } zos.close(); } else { - DatafileInfo dfInfo = (DatafileInfo) dfInfos - .values() - .iterator() - .next(); + DatafileInfo dfInfo = (DatafileInfo) dfInfos.values().iterator().next(); transfer = dfInfo; - InputStream stream = - this.serviceProvider.getMainStorage() - .get( - dfInfo.getDfLocation(), - dfInfo.getCreateId(), - dfInfo.getModId() - ); + InputStream stream = this.serviceProvider.getMainStorage().get(dfInfo.getDfLocation(), dfInfo.getCreateId(), + dfInfo.getModId()); int length; while ((length = stream.read(bytes)) >= 0) { output.write(bytes, 0, length); @@ -134,37 +105,27 @@ public void write(OutputStream output) throws IOException { if (transferId != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try ( - JsonGenerator gen = Json - .createGenerator(baos) - .writeStartObject() - ) { + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { gen.write("transferId", transferId); gen.writeEnd(); } - this.serviceProvider.getTransmitter() - .processMessage("getData", ip, baos.toString(), start); + this.serviceProvider.getTransmitter().processMessage("getData", ip, baos.toString(), start); } + } catch (IOException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try ( - JsonGenerator gen = Json - .createGenerator(baos) - .writeStartObject() - ) { + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { gen.write("transferId", transferId); gen.write("exceptionClass", e.getClass().toString()); gen.write("exceptionMessage", e.getMessage()); gen.writeEnd(); } - this.serviceProvider.getTransmitter() - .processMessage("getData", ip, baos.toString(), start); - logger.error( - "Failed to stream " + transfer + " due to " + e.getMessage() - ); + this.serviceProvider.getTransmitter().processMessage("getData", ip, baos.toString(), start); + logger.error("Failed to stream " + transfer + " due to " + e.getMessage()); throw e; } finally { lock.release(); } } + } diff --git a/src/main/java/org/icatproject/ids/helpers/ValueContainer.java b/src/main/java/org/icatproject/ids/helpers/ValueContainer.java index 52577eca..bb99efba 100644 --- a/src/main/java/org/icatproject/ids/helpers/ValueContainer.java +++ b/src/main/java/org/icatproject/ids/helpers/ValueContainer.java @@ -1,10 +1,12 @@ package org.icatproject.ids.helpers; -import jakarta.ws.rs.core.Response; import java.io.InputStream; + import org.icatproject.ids.enums.ValueContainerType; import org.icatproject.ids.exceptions.InternalException; +import jakarta.ws.rs.core.Response; + /** * This class provides a container vor carrying different types of values */ @@ -23,15 +25,8 @@ private ValueContainer(Object value, ValueContainerType type) { * @param typeToCheck the type to be checked if the contained value is of * @throws InternalException if the types don't match an exception is thrown */ - private void checkType(ValueContainerType typeToCheck) - throws InternalException { - if (this.type != typeToCheck) throw new InternalException( - "This ValueContainer ist not of the needed type " + - typeToCheck + - " its type is " + - this.type + - "." - ); + private void checkType(ValueContainerType typeToCheck) throws InternalException { + if(this.type != typeToCheck) throw new InternalException("This ValueContainer ist not of the needed type " + typeToCheck + " its type is " + this.type + "."); } public static ValueContainer getInvalid() { @@ -88,8 +83,8 @@ public ValueContainer(boolean value) { */ public ValueContainer(Response value) { this(value, ValueContainerType.RESPONSE); - } - + } + /** * Creates a ValueContainer of type InputStream * @param value the value contained by the container @@ -168,41 +163,27 @@ public InputStream getInputStream() throws InternalException { @Override public String toString() { - switch (this.type) { - case INVALID: - return "" + ValueContainerType.INVALID; - case VOID: - return "" + ValueContainerType.VOID; - case INT: - return "" + this.value; - case LONG: - return "" + this.value; - case BOOL: - return ((boolean) this.value ? "true" : "false"); - case STRING: - return (String) this.value; - case RESPONSE: - return "Response " + ((Response) this.value).toString(); - case INPUTSTREAM: - return "An InputStream which will be printed here to prevent it from closing (and maybe it is too long)."; + switch(this.type) { + case INVALID: return ""+ValueContainerType.INVALID; + case VOID: return ""+ValueContainerType.VOID; + case INT: return ""+this.value; + case LONG: return ""+this.value; + case BOOL: return ((boolean)this.value ? "true" : "false"); + case STRING: return (String)this.value; + case RESPONSE: return "Response " + ((Response) this.value).toString(); + case INPUTSTREAM: return "An InputStream which will be printed here to prevent it from closing (and maybe it is too long)."; default: - throw new RuntimeException( - "Doesn't know how to make a String vom ValueContainer of type " + - this.type + - ". Please implement a new case is ValueContainer.toString()." - ); + throw new RuntimeException("Doesn't know how to make a String vom ValueContainer of type " + this.type + ". Please implement a new case is ValueContainer.toString()."); } } - public boolean isNull() { - return this.isInvalid() && this.value == null; - } + public boolean isNull() { return this.isInvalid() && this.value == null; } + + public boolean isInvalid() { return this.type == ValueContainerType.INVALID; } + + public boolean isVoid() { return this.type == ValueContainerType.VOID; } + + - public boolean isInvalid() { - return this.type == ValueContainerType.INVALID; - } - public boolean isVoid() { - return this.type == ValueContainerType.VOID; - } } diff --git a/src/main/java/org/icatproject/ids/models/DataInfoBase.java b/src/main/java/org/icatproject/ids/models/DataInfoBase.java index b5268d87..603a3e7e 100644 --- a/src/main/java/org/icatproject/ids/models/DataInfoBase.java +++ b/src/main/java/org/icatproject/ids/models/DataInfoBase.java @@ -9,7 +9,7 @@ public abstract class DataInfoBase { protected String name; protected String location; - protected DataInfoBase(long id, String name, String location) { + protected DataInfoBase(long id, String name, String location){ this.name = name; this.id = id; this.location = location; @@ -42,4 +42,5 @@ public boolean equals(Object obj) { } return this.id == ((DataInfoBase) obj).getId(); } + } diff --git a/src/main/java/org/icatproject/ids/models/DataSelection.java b/src/main/java/org/icatproject/ids/models/DataSelection.java index 0ad85b9b..1bc59344 100644 --- a/src/main/java/org/icatproject/ids/models/DataSelection.java +++ b/src/main/java/org/icatproject/ids/models/DataSelection.java @@ -5,7 +5,6 @@ import java.util.SortedMap; public class DataSelection { - protected SortedMap dsInfos; protected SortedMap dfInfos; protected Set emptyDatasets; @@ -16,17 +15,11 @@ public class DataSelection { protected Boolean compress; private long length; - public DataSelection( - SortedMap dsInfos, - SortedMap dfInfos, - Set emptyDatasets, - List invids2, - List dsids, - List dfids, - long length, - Boolean zip, - Boolean compress - ) { + + + public DataSelection(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress) { + this.dsInfos = dsInfos; this.dfInfos = dfInfos; this.emptyDatasets = emptyDatasets; @@ -38,10 +31,12 @@ public DataSelection( this.compress = compress; } + public SortedMap getDsInfo() { return dsInfos; } + public SortedMap getDfInfo() { return dfInfos; } @@ -58,14 +53,11 @@ public long getLength() { return this.length; } + public boolean mustZip() { // if(this.zip == null) { - return ( - dfids.size() > 1L || - !dsids.isEmpty() || - !invids.isEmpty() || - (dfids.isEmpty() && dsids.isEmpty() && invids.isEmpty()) - ); + return dfids.size() > 1L || !dsids.isEmpty() || !invids.isEmpty() + || (dfids.isEmpty() && dsids.isEmpty() && invids.isEmpty()); // } // return this.zip; @@ -75,6 +67,7 @@ public boolean isSingleDataset() { return dfids.isEmpty() && dsids.size() == 1 && invids.isEmpty(); } + public Set getEmptyDatasets() { return emptyDatasets; } diff --git a/src/main/java/org/icatproject/ids/models/DatafileInfo.java b/src/main/java/org/icatproject/ids/models/DatafileInfo.java index efc92846..789cfa79 100644 --- a/src/main/java/org/icatproject/ids/models/DatafileInfo.java +++ b/src/main/java/org/icatproject/ids/models/DatafileInfo.java @@ -6,23 +6,15 @@ * Contains information about a Datafile. Replaces DsInfo in v3 * May should implement DfInfo interface */ -public class DatafileInfo - extends DataInfoBase - implements Comparable, DfInfo { +public class DatafileInfo extends DataInfoBase implements Comparable, DfInfo { protected String createId; - protected String modId; + protected String modId; protected long datasId; - public DatafileInfo( - Long id, - String name, - String location, - String createId, - String modId, - Long datasId - ) { + public DatafileInfo(Long id, String name, String location, String createId, String modId, Long datasId) { super(id, name, location); + this.createId = createId; this.modId = modId; this.datasId = datasId; @@ -58,17 +50,9 @@ public int compareTo(DatafileInfo o) { // implementing DfInfo @Override - public Long getDfId() { - return this.getId(); - } - + public Long getDfId() { return this.getId(); } @Override - public String getDfLocation() { - return this.getLocation(); - } - + public String getDfLocation() { return this.getLocation(); } @Override - public String getDfName() { - return this.getName(); - } + public String getDfName() { return this.getName(); } } diff --git a/src/main/java/org/icatproject/ids/models/DatasetInfo.java b/src/main/java/org/icatproject/ids/models/DatasetInfo.java index 55723028..be4d9a77 100644 --- a/src/main/java/org/icatproject/ids/models/DatasetInfo.java +++ b/src/main/java/org/icatproject/ids/models/DatasetInfo.java @@ -18,21 +18,20 @@ public class DatasetInfo extends DataInfoBase implements DsInfo { protected String investigationName; protected String visitId; + public DatasetInfo(Dataset ds) throws InsufficientPrivilegesException { - super(ds.getId(), ds.getName(), ds.getLocation()); + super(ds.getId(), ds.getName(), ds.getLocation()); + Investigation investigation = ds.getInvestigation(); if (investigation == null) { throw new InsufficientPrivilegesException( - "Probably not able to read Investigation for dataset id " + - ds.getId() - ); + "Probably not able to read Investigation for dataset id " + ds.getId()); } Facility facility = investigation.getFacility(); if (facility == null) { throw new InsufficientPrivilegesException( - "Probably not able to read Facility for investigation id " + - investigation.getId() - ); + "Probably not able to read Facility for investigation id " + + investigation.getId()); } this.investigationId = investigation.getId(); @@ -42,17 +41,10 @@ public DatasetInfo(Dataset ds) throws InsufficientPrivilegesException { this.facilityName = facility.getName(); } - public DatasetInfo( - Long id, - String name, - String location, - Long investigationId, - String investigationName, - String visitId, - Long facilityId, - String facilityName - ) { + + public DatasetInfo(Long id, String name, String location, Long investigationId, String investigationName, String visitId, Long facilityId, String facilityName) { super(id, name, location); + this.facilityId = facilityId; this.facilityName = facilityName; this.investigationId = investigationId; @@ -62,20 +54,8 @@ public DatasetInfo( @Override public String toString() { - return ( - this.investigationId + - "/" + - this.id + - " (" + - this.facilityName + - "/" + - this.investigationName + - "/" + - this.visitId + - "/" + - this.name + - ")" - ); + return this.investigationId + "/" + this.id + " (" + this.facilityName + "/" + this.investigationName + "/" + this.visitId + "/" + + this.name + ")"; } public Long getFacilityId() { @@ -98,30 +78,18 @@ public String getVisitId() { return visitId; } + // implementing DsInfo @Override - public Long getDsId() { - return this.getId(); - } - + public Long getDsId() { return this.getId(); } @Override - public String getDsName() { - return this.getName(); - } - + public String getDsName() { return this.getName(); } @Override - public String getDsLocation() { - return this.getLocation(); - } - + public String getDsLocation() { return this.getLocation(); } @Override - public Long getInvId() { - return this.getInvestigationId(); - } - + public Long getInvId() { return this.getInvestigationId(); } @Override - public String getInvName() { - return this.getInvestigationName(); - } + public String getInvName() { return this.getInvestigationName(); } + } diff --git a/src/main/java/org/icatproject/ids/models/Prepared.java b/src/main/java/org/icatproject/ids/models/Prepared.java index 48524c63..5e04d830 100644 --- a/src/main/java/org/icatproject/ids/models/Prepared.java +++ b/src/main/java/org/icatproject/ids/models/Prepared.java @@ -1,11 +1,5 @@ package org.icatproject.ids.models; -import jakarta.json.Json; -import jakarta.json.JsonNumber; -import jakarta.json.JsonObject; -import jakarta.json.JsonReader; -import jakarta.json.JsonValue; -import jakarta.json.stream.JsonGenerator; import java.io.InputStream; import java.io.OutputStream; import java.util.HashSet; @@ -13,18 +7,25 @@ import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; + import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import jakarta.json.Json; +import jakarta.json.JsonNumber; +import jakarta.json.JsonObject; +import jakarta.json.JsonReader; +import jakarta.json.JsonValue; +import jakarta.json.stream.JsonGenerator; + /* This is a POJO with only package access so don't make data private */ public class Prepared { - protected static final Logger logger = LoggerFactory.getLogger( - RequestHandlerBase.class - ); + protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); + public SortedMap dsInfos; public SortedMap dfInfos; public Set emptyDatasets; @@ -32,12 +33,7 @@ public class Prepared { public boolean zip; public boolean compress; - public Prepared( - SortedMap dsInfos, - SortedMap dfInfos, - Set emptyDatasets, - long fileLength - ) { + public Prepared(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength) { this.dsInfos = dsInfos; this.dfInfos = dfInfos; this.emptyDatasets = emptyDatasets; @@ -46,28 +42,15 @@ public Prepared( this.compress = false; } - public Prepared( - SortedMap dsInfos, - SortedMap dfInfos, - Set emptyDatasets, - long fileLength, - Boolean zip, - Boolean compress - ) { + public Prepared(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength, Boolean zip, Boolean compress) { this(dsInfos, dfInfos, emptyDatasets, fileLength); this.zip = zip; this.compress = compress; } - public static void pack( - OutputStream stream, - boolean zip, - boolean compress, - Map dsInfos, - Map dfInfos, - Set emptyDatasets, - long fileLength - ) { + + public static void pack(OutputStream stream, boolean zip, boolean compress, Map dsInfos, + Map dfInfos, Set emptyDatasets, long fileLength) { JsonGenerator gen = Json.createGenerator(stream); gen.writeStartObject(); gen.write("zip", zip); @@ -76,17 +59,13 @@ public static void pack( gen.writeStartArray("dsInfo"); for (DataInfoBase dataInfo : dsInfos.values()) { - var dsInfo = (DatasetInfo) dataInfo; + var dsInfo = (DatasetInfo)dataInfo; logger.debug("dsInfo " + dsInfo); - gen - .writeStartObject() - .write("dsId", dsInfo.getId()) - .write("dsName", dsInfo.getDsName()) - .write("facilityId", dsInfo.getFacilityId()) - .write("facilityName", dsInfo.getFacilityName()) - .write("invId", dsInfo.getInvId()) - .write("invName", dsInfo.getInvName()) - .write("visitId", dsInfo.getVisitId()); + gen.writeStartObject().write("dsId", dsInfo.getId()) + + .write("dsName", dsInfo.getDsName()).write("facilityId", dsInfo.getFacilityId()) + .write("facilityName", dsInfo.getFacilityName()).write("invId", dsInfo.getInvId()) + .write("invName", dsInfo.getInvName()).write("visitId", dsInfo.getVisitId()); if (dsInfo.getDsLocation() != null) { gen.write("dsLocation", dsInfo.getDsLocation()); } else { @@ -98,21 +77,18 @@ public static void pack( gen.writeStartArray("dfInfo"); for (DataInfoBase dataInfo : dfInfos.values()) { - var dfInfo = (DatafileInfo) dataInfo; + var dfInfo = (DatafileInfo)dataInfo; DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId()); - gen - .writeStartObject() - .write("dsId", dsInfo.getId()) - .write("dfId", dfInfo.getId()) - .write("dfName", dfInfo.getDfName()) - .write("createId", dfInfo.getCreateId()) - .write("modId", dfInfo.getModId()); + gen.writeStartObject().write("dsId", dsInfo.getId()).write("dfId", dfInfo.getId()) + .write("dfName", dfInfo.getDfName()).write("createId", dfInfo.getCreateId()) + .write("modId", dfInfo.getModId()); if (dfInfo.getDfLocation() != null) { gen.write("dfLocation", dfInfo.getDfLocation()); } else { gen.writeNull("dfLocation"); } gen.writeEnd(); + } gen.writeEnd(); @@ -123,73 +99,47 @@ public static void pack( gen.writeEnd(); gen.writeEnd().close(); + } + public static Prepared unpack(InputStream stream) throws InternalException { + JsonObject pd; try (JsonReader jsonReader = Json.createReader(stream)) { pd = jsonReader.readObject(); } var zip = pd.getBoolean("zip"); var compress = pd.getBoolean("compress"); - var fileLength = pd.containsKey("fileLength") - ? pd.getInt("fileLength") - : 0; + var fileLength = pd.containsKey("fileLength") ? pd.getInt("fileLength") : 0; SortedMap dsInfos = new TreeMap<>(); SortedMap dfInfos = new TreeMap<>(); Set emptyDatasets = new HashSet<>(); for (JsonValue itemV : pd.getJsonArray("dfInfo")) { JsonObject item = (JsonObject) itemV; - String dfLocation = item.isNull("dfLocation") - ? null - : item.getString("dfLocation"); + String dfLocation = item.isNull("dfLocation") ? null : item.getString("dfLocation"); long dfid = item.getJsonNumber("dfId").longValueExact(); - dfInfos.put( - dfid, - new DatafileInfo( - dfid, - item.getString("dfName"), - dfLocation, - item.getString("createId"), - item.getString("modId"), - item.getJsonNumber("dsId").longValueExact() - ) - ); + dfInfos.put(dfid, new DatafileInfo(dfid, item.getString("dfName"), + dfLocation, item.getString("createId"), item.getString("modId"), + item.getJsonNumber("dsId").longValueExact())); + } for (JsonValue itemV : pd.getJsonArray("dsInfo")) { JsonObject item = (JsonObject) itemV; long dsId = item.getJsonNumber("dsId").longValueExact(); - String dsLocation = item.isNull("dsLocation") - ? null - : item.getString("dsLocation"); - dsInfos.put( - dsId, - new DatasetInfo( - dsId, - item.getString("dsName"), - dsLocation, - item.getJsonNumber("invId").longValueExact(), - item.getString("invName"), - item.getString("visitId"), - item.getJsonNumber("facilityId").longValueExact(), - item.getString("facilityName") - ) - ); + String dsLocation = item.isNull("dsLocation") ? null : item.getString("dsLocation"); + dsInfos.put(dsId, new DatasetInfo(dsId, item.getString("dsName"), dsLocation, + item.getJsonNumber("invId").longValueExact(), item.getString("invName"), item.getString("visitId"), + item.getJsonNumber("facilityId").longValueExact(), item.getString("facilityName"))); } for (JsonValue itemV : pd.getJsonArray("emptyDs")) { emptyDatasets.add(((JsonNumber) itemV).longValueExact()); } - return new Prepared( - dsInfos, - dfInfos, - emptyDatasets, - fileLength, - zip, - compress - ); + return new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength, zip, compress); } + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java index 94fddc68..032e8437 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java @@ -1,5 +1,6 @@ package org.icatproject.ids.requestHandlers; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; @@ -11,27 +12,13 @@ public class ArchiveHandler extends DataRequestHandler { - public ArchiveHandler( - String ip, - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds - ) { - super( - RequestType.ARCHIVE, - ip, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public ArchiveHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.ARCHIVE, ip, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService - ) throws NotImplementedException, InternalException { + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws NotImplementedException, InternalException { + dataSelectionService.scheduleTasks(DeferredOp.ARCHIVE); return ValueContainer.getVoid(); } @@ -40,4 +27,5 @@ public ValueContainer handleDataRequest( public CallType getCallType() { return CallType.MIGRATE; } + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java index 704d1652..a4cb44a9 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; + import org.icatproject.Datafile; import org.icatproject.EntityBaseBean; import org.icatproject.IcatExceptionType; @@ -20,76 +21,48 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataRequestHandler; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; + public class DeleteHandler extends DataRequestHandler { - public DeleteHandler( - String ip, - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds - ) { - super( - RequestType.DELETE, - ip, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public DeleteHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.DELETE, ip, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService - ) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + if (readOnly) { - throw new NotImplementedException( - "This operation has been configured to be unavailable" - ); + throw new NotImplementedException("This operation has been configured to be unavailable"); } var serviceProvider = ServiceProvider.getInstance(); // Do it - try ( - Lock lock = serviceProvider - .getLockManager() - .lock( - dataSelectionService.getDsInfo().values(), - LockType.EXCLUSIVE - ) - ) { + try (Lock lock = serviceProvider.getLockManager().lock(dataSelectionService.getDsInfo().values(), LockType.EXCLUSIVE)) { if (storageUnit == StorageUnit.DATASET) { dataSelectionService.checkOnline(); } /* Now delete from ICAT */ List dfs = new ArrayList<>(); - for (DataInfoBase dfInfo : dataSelectionService - .getDfInfo() - .values()) { + for (DataInfoBase dfInfo : dataSelectionService.getDfInfo().values()) { Datafile df = new Datafile(); df.setId(dfInfo.getId()); dfs.add(df); } try { - serviceProvider - .getIcat() - .deleteMany(this.dataController.getOperationId(), dfs); + serviceProvider.getIcat().deleteMany(this.dataController.getOperationId(), dfs); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if ( - type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || - type == IcatExceptionType.SESSION - ) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -99,6 +72,7 @@ public ValueContainer handleDataRequest( } dataSelectionService.delete(); + } catch (AlreadyLockedException e) { logger.debug("Could not acquire lock, delete failed"); throw new DataNotOnlineException("Data is busy"); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java index d62b7077..7c22c38f 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java @@ -1,8 +1,7 @@ package org.icatproject.ids.requestHandlers; -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; import java.io.ByteArrayOutputStream; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -17,45 +16,30 @@ import org.icatproject.ids.requestHandlers.base.PreparedDataController; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; + public class GetDataFileIdsHandler extends DataRequestHandler { - public GetDataFileIdsHandler( - String ip, - String preparedId, - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds - ) { - super( - RequestType.GETDATAFILEIDS, - ip, - preparedId, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public GetDataFileIdsHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETDATAFILEIDS, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService - ) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try ( - JsonGenerator gen = Json.createGenerator(baos).writeStartObject() - ) { - if (this.dataController instanceof PreparedDataController) { + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + + if(this.dataController instanceof PreparedDataController) { gen.write("zip", dataSelectionService.getZip()); gen.write("compress", dataSelectionService.getCompress()); } gen.writeStartArray("ids"); - for (DataInfoBase dfInfo : dataSelectionService - .getDfInfo() - .values()) { + for (DataInfoBase dfInfo : dataSelectionService.getDfInfo().values()) { gen.write(dfInfo.getId()); } gen.writeEnd().writeEnd().close(); @@ -69,4 +53,5 @@ public ValueContainer handleDataRequest( public CallType getCallType() { return CallType.INFO; } + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java index 50f03f93..8cdb64ce 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java @@ -1,9 +1,5 @@ package org.icatproject.ids.requestHandlers; -import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH; - -import jakarta.json.stream.JsonGenerator; -import jakarta.ws.rs.core.Response; import java.io.IOException; import java.net.HttpURLConnection; import java.text.SimpleDateFormat; @@ -13,6 +9,7 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.regex.Matcher; import java.util.regex.Pattern; + import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; @@ -28,11 +25,15 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataRequestHandler; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; +import jakarta.json.stream.JsonGenerator; +import jakarta.ws.rs.core.Response; +import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH; + public class GetDataHandler extends DataRequestHandler { private Pattern rangeRe; @@ -44,71 +45,40 @@ public class GetDataHandler extends DataRequestHandler { String range; Long transferId; - public GetDataHandler( - String ip, - String preparedId, - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds, - Boolean compress, - Boolean zip, - String outname, - String range - ) { - super( - RequestType.GETDATA, - ip, - preparedId, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); - this.initializeAdditionallParameters( - sessionId, - compress, - zip, - outname, - range - ); + public GetDataHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds, Boolean compress, Boolean zip, String outname, String range) { + super(RequestType.GETDATA, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds ); + + this.initializeAdditionallParameters(sessionId, compress, zip, outname, range); } - private void initializeAdditionallParameters( - String sessionId, - Boolean compress, - Boolean zip, - String outname, - String range - ) { + private void initializeAdditionallParameters(String sessionId, Boolean compress, Boolean zip, String outname, String range) { + this.outname = outname; this.range = range; this.transferId = null; this.rangeRe = Pattern.compile("bytes=(\\d+)-"); - if (sessionId != null) { + if(sessionId != null) { this.compress = compress; this.zip = zip; } } + @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService - ) - throws InternalException, NotImplementedException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException { - if (this.compress == null || this.zip == null) { + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws InternalException, NotImplementedException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException { + + if(this.compress == null || this.zip == null) { this.zip = dataSelectionService.getZip(); this.compress = dataSelectionService.getCompress(); } long offset = 0; - if (range != null) { + if ( range != null) { + Matcher m = rangeRe.matcher(range); if (!m.matches()) { - throw new BadRequestException( - "The range must match " + rangeRe.pattern() - ); + throw new BadRequestException("The range must match " + rangeRe.pattern()); } offset = Long.parseLong(m.group(1)); logger.debug("Range " + range + " -> offset " + offset); @@ -117,30 +87,20 @@ public ValueContainer handleDataRequest( return new ValueContainer(this.getData(dataSelectionService, offset)); } - private Response getData( - DataSelectionService dataSelectionService, - final long offset - ) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - long start = System.currentTimeMillis(); - var length = this.zip - ? OptionalLong.empty() - : dataSelectionService.getFileLength(); + private Response getData(DataSelectionService dataSelectionService, final long offset) throws BadRequestException, + NotFoundException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + + long start = System.currentTimeMillis(); + + var length = this.zip ? OptionalLong.empty() : dataSelectionService.getFileLength(); - final boolean finalZip = - this.dataController.mustZip(zip, dataSelectionService); + final boolean finalZip = this.dataController.mustZip(zip, dataSelectionService); Lock lock = null; try { var serviceProvider = ServiceProvider.getInstance(); - lock = - serviceProvider - .getLockManager() - .lock( - dataSelectionService.getDsInfo().values(), - LockType.SHARED - ); + lock = serviceProvider.getLockManager().lock(dataSelectionService.getDsInfo().values(), LockType.SHARED); if (twoLevel) { dataSelectionService.checkOnline(); @@ -151,25 +111,13 @@ private Response getData( String name; if (outname == null) { if (finalZip) { - name = - new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss") - .format(new Date()) + - ".zip"; + name = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()) + ".zip"; } else { - name = - dataSelectionService - .getDfInfo() - .values() - .iterator() - .next() - .getName(); + name = dataSelectionService.getDfInfo().values().iterator().next().getName(); } } else { if (finalZip) { - String ext = outname.substring( - outname.lastIndexOf(".") + 1, - outname.length() - ); + String ext = outname.substring(outname.lastIndexOf(".") + 1, outname.length()); if ("zip".equals(ext)) { name = outname; } else { @@ -180,47 +128,19 @@ private Response getData( } } - if ( - ServiceProvider - .getInstance() - .getPropertyHandler() - .getLogSet() - .contains(this.getCallType()) - ) { + if (ServiceProvider.getInstance().getPropertyHandler().getLogSet().contains(this.getCallType())) { transferId = transferIdCounter.getAndIncrement(); } - var response = Response - .status( - offset == 0 - ? HttpURLConnection.HTTP_OK - : HttpURLConnection.HTTP_PARTIAL - ) - .entity( - new SO( - dataSelectionService.getDsInfo(), - dataSelectionService.getDfInfo(), - offset, - finalZip, - compress, - lock, - transferId, - ip, - start, - serviceProvider - ) - ) - .header( - "Content-Disposition", - "attachment; filename=\"" + name + "\"" - ) - .header("Accept-Ranges", "bytes"); - length - .stream() - .map(l -> Math.max(0L, l - offset)) - .forEach(l -> response.header(CONTENT_LENGTH, l)); - + var response = Response.status(offset == 0 ? HttpURLConnection.HTTP_OK : HttpURLConnection.HTTP_PARTIAL) + .entity(new SO(dataSelectionService.getDsInfo(), dataSelectionService.getDfInfo(), offset, finalZip, compress, lock, transferId, ip, start, serviceProvider)) + .header("Content-Disposition", "attachment; filename=\"" + name + "\"").header("Accept-Ranges", "bytes"); + length.stream() + .map(l -> Math.max(0L, l - offset)) + .forEach(l -> response.header(CONTENT_LENGTH, l)); + return response.build(); + } catch (AlreadyLockedException e) { logger.debug("Could not acquire lock, getData failed"); throw new DataNotOnlineException("Data is busy"); @@ -236,69 +156,41 @@ private Response getData( } } + private void checkDatafilesPresent(Collection dfInfos) - throws NotFoundException, InternalException { + throws NotFoundException, InternalException { + var serviceProvider = ServiceProvider.getInstance(); /* Check that datafiles have not been deleted before locking */ int n = 0; - StringBuffer sb = new StringBuffer( - "SELECT COUNT(df) from Datafile df WHERE (df.id in (" - ); + StringBuffer sb = new StringBuffer("SELECT COUNT(df) from Datafile df WHERE (df.id in ("); for (DataInfoBase dfInfo : dfInfos) { if (n != 0) { sb.append(','); } sb.append(dfInfo.getId()); - if ( - ++n == serviceProvider.getPropertyHandler().getMaxIdsInQuery() - ) { + if (++n == serviceProvider.getPropertyHandler().getMaxIdsInQuery()) { try { - if ( - ( - (Long) serviceProvider - .getIcatReader() - .search(sb.append("))").toString()) - .get(0) - ).intValue() != - n - ) { - throw new NotFoundException( - "One of the data files requested has been deleted" - ); + if (((Long) serviceProvider.getIcatReader().search(sb.append("))").toString()).get(0)).intValue() != n) { + throw new NotFoundException("One of the data files requested has been deleted"); } n = 0; - sb = - new StringBuffer( - "SELECT COUNT(df) from Datafile df WHERE (df.id in (" - ); + sb = new StringBuffer("SELECT COUNT(df) from Datafile df WHERE (df.id in ("); } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage() - ); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } } } if (n != 0) { try { - if ( - ( - (Long) serviceProvider - .getIcatReader() - .search(sb.append("))").toString()) - .get(0) - ).intValue() != - n - ) { - throw new NotFoundException( - "One of the datafiles requested has been deleted" - ); + if (((Long) serviceProvider.getIcatReader().search(sb.append("))").toString()).get(0)).intValue() != n) { + throw new NotFoundException("One of the datafiles requested has been deleted"); } } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage() - ); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } } + } @Override @@ -308,7 +200,8 @@ public CallType getCallType() { @Override public void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - if (transferId == null) transferId = -1L; + if(transferId == null) transferId = -1L; gen.write("transferId", transferId); } + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java index e711f0c3..94a78919 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java @@ -12,6 +12,7 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; import org.icatproject.ids.services.ServiceProvider; + public class GetIcatUrlHandler extends RequestHandlerBase { public GetIcatUrlHandler(String ip) { @@ -20,10 +21,10 @@ public GetIcatUrlHandler(String ip) { @Override public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - var propertyHandler = ServiceProvider - .getInstance() - .getPropertyHandler(); + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + + var propertyHandler = ServiceProvider.getInstance().getPropertyHandler(); return new ValueContainer(propertyHandler.getIcatUrl()); } @@ -31,4 +32,5 @@ public ValueContainer handleRequest() public CallType getCallType() { return CallType.INFO; } + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java index 28ee0d3d..713c3603 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java @@ -1,7 +1,7 @@ package org.icatproject.ids.requestHandlers; -import jakarta.json.stream.JsonGenerator; import java.util.Set; + import org.icatproject.IcatExceptionType; import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.CallType; @@ -16,6 +16,8 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; import org.icatproject.ids.services.ServiceProvider; +import jakarta.json.stream.JsonGenerator; + public class GetServiceStatusHandler extends RequestHandlerBase { private Set rootUserNames; @@ -24,31 +26,26 @@ public class GetServiceStatusHandler extends RequestHandlerBase { public GetServiceStatusHandler(String ip, String sessionId) { super(RequestType.GETSERVICESTATUS, ip); + this.sessionId = sessionId; - rootUserNames = - ServiceProvider - .getInstance() - .getPropertyHandler() - .getRootUserNames(); + rootUserNames = ServiceProvider.getInstance().getPropertyHandler().getRootUserNames(); this.serviceProvider = ServiceProvider.getInstance(); } @Override public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + try { String uname = serviceProvider.getIcat().getUserName(sessionId); if (!rootUserNames.contains(uname)) { - throw new InsufficientPrivilegesException( - uname + " is not included in the ids rootUserNames set." - ); + throw new InsufficientPrivilegesException(uname + " is not included in the ids rootUserNames set."); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); if (type == IcatExceptionType.SESSION) { - throw new InsufficientPrivilegesException( - e.getClass() + " " + e.getMessage() - ); + throw new InsufficientPrivilegesException(e.getClass() + " " + e.getMessage()); } throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -57,8 +54,7 @@ public ValueContainer handleRequest() } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { gen.write("userName", serviceProvider.getIcat().getUserName(sessionId)); } @@ -66,4 +62,5 @@ public void addParametersToTransmitterJSON(JsonGenerator gen) public CallType getCallType() { return CallType.INFO; } + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java index 1695c6ae..9da9787c 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java @@ -1,6 +1,7 @@ package org.icatproject.ids.requestHandlers; import java.util.Set; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.enums.Status; @@ -18,40 +19,21 @@ public class GetStatusHandler extends DataRequestHandler { - public GetStatusHandler( - String ip, - String preparedId, - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds - ) { - super( - RequestType.GETSTATUS, - ip, - preparedId, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public GetStatusHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETSTATUS, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService - ) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + Status status = Status.ONLINE; var serviceProvider = ServiceProvider.getInstance(); Set restoring = serviceProvider.getFsm().getRestoring(); - Set maybeOffline = serviceProvider - .getFsm() - .getMaybeOffline(); - for (DataInfoBase dataInfo : dataSelectionService - .getPrimaryDataInfos() - .values()) { + Set maybeOffline = serviceProvider.getFsm().getMaybeOffline(); + for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (restoring.contains(dataInfo)) { status = Status.RESTORING; diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java index 1306c1ec..6f2deba7 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java @@ -5,6 +5,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.locks.ReentrantLock; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -24,32 +25,26 @@ public IsPreparedHandler(String ip, String preparedId) { } class PreparedStatus { - public ReentrantLock lock = new ReentrantLock(); public Long fromElement; public Future future; + } private Map preparedStatusMap = new ConcurrentHashMap<>(); @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService - ) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + // Do it boolean prepared = true; - PreparedStatus status = preparedStatusMap.computeIfAbsent( - this.dataController.getOperationId(), - k -> new PreparedStatus() - ); + PreparedStatus status = preparedStatusMap.computeIfAbsent(this.dataController.getOperationId(), k -> new PreparedStatus()); if (!status.lock.tryLock()) { - logger.debug( - "Lock held for evaluation of isPrepared for preparedId {}", - this.dataController.getOperationId() - ); + logger.debug("Lock held for evaluation of isPrepared for preparedId {}", this.dataController.getOperationId()); return new ValueContainer(false); } try { @@ -59,29 +54,22 @@ public ValueContainer handleDataRequest( try { future.get(); } catch (ExecutionException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (InterruptedException e) { // Ignore } finally { status.future = null; } } else { - logger.debug( - "Background process still running for preparedId {}", - this.dataController.getOperationId() - ); + logger.debug("Background process still running for preparedId {}", this.dataController.getOperationId()); return new ValueContainer(false); } } - prepared = - dataSelectionService.isPrepared( - this.dataController.getOperationId() - ); + prepared = dataSelectionService.isPrepared(this.dataController.getOperationId()); return new ValueContainer(prepared); + } finally { status.lock.unlock(); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java index d12466a3..a7acf20e 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java @@ -12,14 +12,17 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; public class IsReadOnlyHandler extends RequestHandlerBase { - + public IsReadOnlyHandler(String ip) { super(RequestType.ISREADONLY, ip); + } @Override public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + return new ValueContainer(this.readOnly); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java index 4348674a..f9cc3be1 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java @@ -19,7 +19,9 @@ public IsTwoLevelHandler(String ip) { @Override public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + return new ValueContainer(twoLevel); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java index 8e00f75e..625c4ac2 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java @@ -1,11 +1,11 @@ package org.icatproject.ids.requestHandlers; -import jakarta.json.stream.JsonGenerator; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.util.UUID; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestIdNames; import org.icatproject.ids.enums.RequestType; @@ -20,40 +20,28 @@ import org.icatproject.ids.requestHandlers.base.DataRequestHandler; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; +import jakarta.json.stream.JsonGenerator; + public class PrepareDataHandler extends DataRequestHandler { Boolean compress; Boolean zip; String preparedId; - public PrepareDataHandler( - String ip, - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds, - Boolean compress, - Boolean zip - ) { - super( - RequestType.PREPAREDATA, - ip, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public PrepareDataHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds, Boolean compress, Boolean zip) { + super(RequestType.PREPAREDATA,ip, sessionId, investigationIds, datasetIds, datafileIds); + this.zip = zip; this.compress = compress; } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService - ) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - preparedId = UUID.randomUUID().toString(); + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + preparedId = UUID.randomUUID().toString(); + dataSelectionService.restoreDataInfos(); if (dataSelectionService.mustZip()) { @@ -61,21 +49,14 @@ public ValueContainer handleDataRequest( } logger.debug("Writing to " + preparedDir.resolve(preparedId)); - try ( - OutputStream stream = new BufferedOutputStream( - Files.newOutputStream(preparedDir.resolve(preparedId)) - ) - ) { - Prepared.pack( - stream, - zip, - compress, - dataSelectionService.getDsInfo(), - dataSelectionService.getDfInfo(), - dataSelectionService.getEmptyDatasets(), - dataSelectionService.getFileLength().isEmpty() - ? 0 - : dataSelectionService.getFileLength().getAsLong() + try (OutputStream stream = new BufferedOutputStream(Files.newOutputStream(preparedDir.resolve(preparedId)))) { + Prepared.pack( stream, + zip, + compress, + dataSelectionService.getDsInfo(), + dataSelectionService.getDfInfo(), + dataSelectionService.getEmptyDatasets(), + dataSelectionService.getFileLength().isEmpty() ? 0 : dataSelectionService.getFileLength().getAsLong() ); } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -87,13 +68,13 @@ public ValueContainer handleDataRequest( } @Override - public String addCustomParametersToLogString() { + public String addCustomParametersToLogString() { return "zip='" + this.zip + "'' compress='" + compress + "'"; } @Override public void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - if (preparedId == null) preparedId = ""; + if(preparedId == null) preparedId = ""; gen.write(RequestIdNames.preparedId, preparedId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java index c147a306..3e9bfbd9 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java @@ -1,8 +1,5 @@ package org.icatproject.ids.requestHandlers; -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; -import jakarta.ws.rs.core.Response; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -15,6 +12,7 @@ import java.util.TreeMap; import java.util.zip.CRC32; import javax.xml.datatype.DatatypeFactory; + import org.icatproject.Datafile; import org.icatproject.DatafileFormat; import org.icatproject.Dataset; @@ -35,19 +33,23 @@ import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.helpers.CheckedWithSizeInputStream; import org.icatproject.ids.helpers.ValueContainer; -import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatafileInfo; +import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataControllerBase; import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; import org.icatproject.ids.services.dataSelectionService.DataSelectionServiceFactory; import org.icatproject.utils.IcatSecurity; +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; +import jakarta.ws.rs.core.Response; + public class PutHandler extends RequestHandlerBase { String sessionId; @@ -66,8 +68,7 @@ public class PutHandler extends RequestHandlerBase { private ServiceProvider serviceProvider; private DatatypeFactory datatypeFactory; private static String paddedPrefix; - private static final String prefix = - ""; static { @@ -78,21 +79,11 @@ public class PutHandler extends RequestHandlerBase { paddedPrefix += "*/window.name='"; } - public PutHandler( - String ip, - String sessionId, - InputStream body, - String name, - String datafileFormatIdString, - String datasetIdString, - String description, - String doi, - String datafileCreateTimeString, - String datafileModTimeString, - boolean wrap, - boolean padding - ) { + public PutHandler( String ip, String sessionId, InputStream body, String name, String datafileFormatIdString, + String datasetIdString, String description, String doi, String datafileCreateTimeString, + String datafileModTimeString, boolean wrap, boolean padding) { super(RequestType.PUT, ip); + this.sessionId = sessionId; this.body = body; this.name = name; @@ -109,70 +100,57 @@ public PutHandler( this.serviceProvider = ServiceProvider.getInstance(); try { + this.datatypeFactory = DatatypeFactory.newInstance(); + } catch (Throwable e) { logger.error("Won't start ", e); - throw new RuntimeException( - "PutHandler reports " + e.getClass() + " " + e.getMessage() - ); + throw new RuntimeException("PutHandler reports " + e.getClass() + " " + e.getMessage()); } } @Override public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + try { + if (readOnly) { - throw new NotImplementedException( - "This operation has been configured to be unavailable" - ); + throw new NotImplementedException("This operation has been configured to be unavailable"); } - DataControllerBase.validateUUID( - RequestIdNames.sessionId, - sessionId - ); + DataControllerBase.validateUUID(RequestIdNames.sessionId, sessionId); if (name == null) { throw new BadRequestException("The name parameter must be set"); } if (datafileFormatIdString == null) { - throw new BadRequestException( - "The datafileFormatId parameter must be set" - ); + throw new BadRequestException("The datafileFormatId parameter must be set"); } long datafileFormatId; try { datafileFormatId = Long.parseLong(datafileFormatIdString); } catch (NumberFormatException e) { - throw new BadRequestException( - "The datafileFormatId parameter must be numeric" - ); + throw new BadRequestException("The datafileFormatId parameter must be numeric"); } if (datasetIdString == null) { - throw new BadRequestException( - "The datasetId parameter must be set" - ); + throw new BadRequestException("The datasetId parameter must be set"); } long datasetId; try { datasetId = Long.parseLong(datasetIdString); } catch (NumberFormatException e) { - throw new BadRequestException( - "The datasetId parameter must be numeric" - ); + throw new BadRequestException("The datasetId parameter must be numeric"); } Long datafileCreateTime = null; if (datafileCreateTimeString != null) { try { - datafileCreateTime = - Long.parseLong(datafileCreateTimeString); + datafileCreateTime = Long.parseLong(datafileCreateTimeString); } catch (NumberFormatException e) { - throw new BadRequestException( - "The datafileCreateTime parameter must be numeric" - ); + throw new BadRequestException("The datafileCreateTime parameter must be numeric"); } } @@ -181,29 +159,17 @@ public ValueContainer handleRequest() try { datafileModTime = Long.parseLong(datafileModTimeString); } catch (NumberFormatException e) { - throw new BadRequestException( - "The datafileModTime parameter must be numeric" - ); + throw new BadRequestException("The datafileModTime parameter must be numeric"); } } // Do it Dataset ds; try { - ds = - (Dataset) serviceProvider - .getIcat() - .get( - sessionId, - "Dataset INCLUDE Investigation, Facility", - datasetId - ); + ds = (Dataset) serviceProvider.getIcat().get(sessionId, "Dataset INCLUDE Investigation, Facility", datasetId); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if ( - type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || - type == IcatExceptionType.SESSION - ) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -213,117 +179,59 @@ public ValueContainer handleRequest() } DatasetInfo dsInfo = new DatasetInfo(ds); - try ( - Lock lock = serviceProvider - .getLockManager() - .lock(dsInfo, LockType.SHARED) - ) { + try (Lock lock = serviceProvider.getLockManager().lock(dsInfo, LockType.SHARED)) { if (storageUnit == StorageUnit.DATASET) { var dfInfos = new TreeMap(); Set emptyDatasets = new HashSet<>(); try { - List counts = serviceProvider - .getIcat() - .search( - sessionId, - "COUNT(Datafile) <-> Dataset [id=" + - dsInfo.getId() + - "]" - ); + List counts = serviceProvider.getIcat().search(sessionId, + "COUNT(Datafile) <-> Dataset [id=" + dsInfo.getId() + "]"); if ((Long) counts.get(0) == 0) { emptyDatasets.add(dsInfo.getId()); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if ( - type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || - type == IcatExceptionType.SESSION - ) { - throw new InsufficientPrivilegesException( - e.getMessage() - ); + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } - throw new InternalException( - type + " " + e.getMessage() - ); + throw new InternalException(type + " " + e.getMessage()); } var dsInfos = new TreeMap(); dsInfos.put(dsInfo.getId(), dsInfo); - DataSelectionService dataSelectionService = DataSelectionServiceFactory.getService( - dsInfos, - dfInfos, - emptyDatasets, - 0, - this.getRequestType() - ); + DataSelectionService dataSelectionService = DataSelectionServiceFactory.getService(dsInfos, dfInfos, emptyDatasets, 0, this.getRequestType()); dataSelectionService.checkOnline(); } CRC32 crc = new CRC32(); - CheckedWithSizeInputStream is = new CheckedWithSizeInputStream( - body, - crc - ); + CheckedWithSizeInputStream is = new CheckedWithSizeInputStream(body, crc); String location; try { - location = - serviceProvider.getMainStorage().put(dsInfo, name, is); + location = serviceProvider.getMainStorage().put(dsInfo, name, is); } catch (IllegalArgumentException e) { - throw new BadRequestException( - "Illegal filename or dataset: " + e.getMessage() - ); + throw new BadRequestException("Illegal filename or dataset: " + e.getMessage()); } is.close(); long checksum = crc.getValue(); long size = is.getSize(); - + try { - dfId = - registerDatafile( - sessionId, - name, - datafileFormatId, - location, - checksum, - size, - ds, - description, - doi, - datafileCreateTime, - datafileModTime - ); - } catch ( - InsufficientPrivilegesException - | NotFoundException - | InternalException - | BadRequestException e - ) { - logger.debug( - "Problem with registration " + - e.getClass() + - " " + - e.getMessage() + - " datafile will now be deleted" - ); + dfId = registerDatafile(sessionId, name, datafileFormatId, location, checksum, size, ds, + description, doi, datafileCreateTime, datafileModTime); + } catch (InsufficientPrivilegesException | NotFoundException | InternalException + | BadRequestException e) { + logger.debug("Problem with registration " + e.getClass() + " " + e.getMessage() + + " datafile will now be deleted"); String userId = null; try { - userId = - serviceProvider.getIcat().getUserName(sessionId); + userId = serviceProvider.getIcat().getUserName(sessionId); } catch (IcatException_Exception e1) { - logger.error( - "Unable to get user name for session " + - sessionId + - " so mainStorage.delete of " + - location + - " may fail" - ); + logger.error("Unable to get user name for session " + sessionId + " so mainStorage.delete of " + + location + " may fail"); } - serviceProvider - .getMainStorage() - .delete(location, userId, userId); + serviceProvider.getMainStorage().delete(location, userId, userId); throw e; } @@ -332,130 +240,62 @@ public ValueContainer handleRequest() } else if (storageUnit == StorageUnit.DATAFILE) { Datafile df; try { - df = - (Datafile) serviceProvider - .getIcatReader() - .get("Datafile", dfId); + df = (Datafile) serviceProvider.getIcatReader().get("Datafile", dfId); } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage() - ); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } - serviceProvider - .getFsm() - .queue( - new DatafileInfo( - dfId, - name, - location, - df.getCreateId(), - df.getModId(), - dsInfo.getId() - ), - DeferredOp.WRITE - ); + serviceProvider.getFsm().queue(new DatafileInfo(dfId, name, location, df.getCreateId(), df.getModId(), dsInfo.getId()), + DeferredOp.WRITE); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Json - .createGenerator(baos) - .writeStartObject() - .write("id", dfId) - .write("checksum", checksum) - .write( - "location", - location.replace("\\", "\\\\").replace("'", "\\'") - ) - .write("size", size) - .writeEnd() - .close(); - String resp = wrap - ? prefix + baos.toString() + suffix - : baos.toString(); - - return new ValueContainer( - Response - .status(HttpURLConnection.HTTP_CREATED) - .entity(resp) - .build() - ); + Json.createGenerator(baos).writeStartObject().write("id", dfId).write("checksum", checksum) + .write("location", location.replace("\\", "\\\\").replace("'", "\\'")).write("size", size) + .writeEnd().close(); + String resp = wrap ? prefix + baos.toString() + suffix : baos.toString(); + + return new ValueContainer(Response.status(HttpURLConnection.HTTP_CREATED).entity(resp).build()); + } catch (AlreadyLockedException e) { logger.debug("Could not acquire lock, put failed"); throw new DataNotOnlineException("Data is busy"); } catch (IOException e) { - logger.error( - "I/O exception " + - e.getMessage() + - " putting " + - name + - " to Dataset with id " + - datasetIdString - ); - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + logger.error("I/O exception " + e.getMessage() + " putting " + name + " to Dataset with id " + + datasetIdString); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IdsException e) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen - .writeStartObject() - .write("code", e.getClass().getSimpleName()) - .write("message", e.getShortMessage()); + gen.writeStartObject().write("code", e.getClass().getSimpleName()).write("message", e.getShortMessage()); gen.writeEnd().close(); if (wrap) { String pre = padding ? paddedPrefix : prefix; - return new ValueContainer( - Response - .status(e.getHttpStatusCode()) - .entity( - pre + baos.toString().replace("'", "\\'") + suffix - ) - .build() - ); + return new ValueContainer(Response.status(e.getHttpStatusCode()).entity(pre + baos.toString().replace("'", "\\'") + suffix) + .build()); } else { - return new ValueContainer( - Response - .status(e.getHttpStatusCode()) - .entity(baos.toString()) - .build() - ); + return new ValueContainer(Response.status(e.getHttpStatusCode()).entity(baos.toString()).build()); } } } - private Long registerDatafile( - String sessionId, - String name, - long datafileFormatId, - String location, - long checksum, - long size, - Dataset dataset, - String description, - String doi, - Long datafileCreateTime, - Long datafileModTime - ) - throws InsufficientPrivilegesException, NotFoundException, InternalException, BadRequestException { + + private Long registerDatafile(String sessionId, String name, long datafileFormatId, String location, long checksum, + long size, Dataset dataset, String description, String doi, Long datafileCreateTime, Long datafileModTime) + throws InsufficientPrivilegesException, NotFoundException, InternalException, BadRequestException { + + var serviceProvider = ServiceProvider.getInstance(); ICAT icat = serviceProvider.getIcat(); - + final Datafile df = new Datafile(); DatafileFormat format; try { - format = - (DatafileFormat) icat.get( - sessionId, - "DatafileFormat", - datafileFormatId - ); + format = (DatafileFormat) icat.get(sessionId, "DatafileFormat", datafileFormatId); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if ( - type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || - type == IcatExceptionType.SESSION - ) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -475,16 +315,12 @@ private Long registerDatafile( if (datafileCreateTime != null) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTimeInMillis(datafileCreateTime); - df.setDatafileCreateTime( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar) - ); + df.setDatafileCreateTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); } if (datafileModTime != null) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTimeInMillis(datafileModTime); - df.setDatafileModTime( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar) - ); + df.setDatafileModTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); } try { long dfId = icat.create(sessionId, df); @@ -492,24 +328,15 @@ private Long registerDatafile( String key = serviceProvider.getPropertyHandler().getKey(); if (key != null) { - df.setLocation( - location + " " + IcatSecurity.digest(dfId, location, key) - ); + df.setLocation(location + " " + IcatSecurity.digest(dfId, location, key)); icat.update(sessionId, df); } - logger.debug( - "Registered datafile for dataset {} for {}", - dataset.getId(), - name + " at " + location - ); + logger.debug("Registered datafile for dataset {} for {}", dataset.getId(), name + " at " + location); return dfId; } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if ( - type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || - type == IcatExceptionType.SESSION - ) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.VALIDATION) { @@ -522,33 +349,13 @@ private Long registerDatafile( } public String addParametersToLogString() { - return ( - "name='" + - name + - "' " + - "datafileFormatId='" + - datafileFormatIdString + - "' " + - "datasetId='" + - datasetIdString + - "' " + - "description='" + - description + - "' " + - "doi='" + - doi + - "' " + - "datafileCreateTime='" + - datafileCreateTimeString + - "' " + - "datafileModTime='" + - datafileModTimeString + - "'" - ); + return "name='" + name + "' " + "datafileFormatId='" + + datafileFormatIdString + "' " + "datasetId='" + datasetIdString + "' " + "description='" + + description + "' " + "doi='" + doi + "' " + "datafileCreateTime='" + datafileCreateTimeString + + "' " + "datafileModTime='" + datafileModTimeString + "'"; } - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { gen.write("userName", serviceProvider.getIcat().getUserName(sessionId)); gen.write("datafileId", dfId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java index 902b848c..2fff2bb3 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java @@ -15,36 +15,20 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; + public class ResetHandler extends DataRequestHandler { - public ResetHandler( - String ip, - String preparedId, - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds - ) { - super( - RequestType.RESET, - ip, - preparedId, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public ResetHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.RESET, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService - ) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + FiniteStateMachine fsm = ServiceProvider.getInstance().getFsm(); - for (DataInfoBase dataInfo : dataSelectionService - .getPrimaryDataInfos() - .values()) { + for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { fsm.recordSuccess(dataInfo.getId()); } @@ -55,4 +39,7 @@ public ValueContainer handleDataRequest( public CallType getCallType() { return CallType.MIGRATE; } + + + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java index 259545fc..77e0c7e5 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java @@ -15,29 +15,16 @@ public class RestoreHandler extends DataRequestHandler { - public RestoreHandler( - String ip, - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds - ) { - super( - RequestType.RESTORE, - ip, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public RestoreHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.RESTORE, ip, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService - ) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - dataSelectionService.scheduleTasks(DeferredOp.RESTORE); + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + + dataSelectionService.scheduleTasks(DeferredOp.RESTORE); return ValueContainer.getVoid(); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java index 5965b281..c6f994f4 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.util.Map; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; @@ -15,67 +16,45 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataRequestHandler; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; public class WriteHandler extends DataRequestHandler { - public WriteHandler( - String ip, - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds - ) { - super( - RequestType.WRITE, - ip, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public WriteHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.WRITE, ip, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService - ) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + var serviceProvider = ServiceProvider.getInstance(); if (!serviceProvider.getPropertyHandler().getEnableWrite()) { - throw new NotImplementedException( - "This operation has been configured to be unavailable" - ); + throw new NotImplementedException("This operation has been configured to be unavailable"); } Map dsInfos = dataSelectionService.getDsInfo(); - try ( - Lock lock = serviceProvider - .getLockManager() - .lock(dsInfos.values(), LockType.SHARED) - ) { + try (Lock lock = serviceProvider.getLockManager().lock(dsInfos.values(), LockType.SHARED)) { if (twoLevel) { boolean maybeOffline = false; - for (DataInfoBase dataInfo : dataSelectionService - .getPrimaryDataInfos() - .values()) { + for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { if (!dataSelectionService.existsInMainStorage(dataInfo)) { maybeOffline = true; } } if (maybeOffline) { - throw new DataNotOnlineException( - "Requested data is not online, write request refused" - ); + throw new DataNotOnlineException("Requested data is not online, write request refused"); } } dataSelectionService.scheduleTasks(DeferredOp.WRITE); + } catch (AlreadyLockedException e) { logger.debug("Could not acquire lock, write failed"); throw new DataNotOnlineException("Data is busy"); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java b/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java index 956a8210..1291e98e 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java @@ -1,7 +1,7 @@ package org.icatproject.ids.requestHandlers.base; -import jakarta.json.stream.JsonGenerator; import java.util.regex.Pattern; + import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -12,38 +12,32 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; +import jakarta.json.stream.JsonGenerator; + public abstract class DataControllerBase { /** * matches standard UUID format of 8-4-4-4-12 hexadecimal digits */ - public static final Pattern uuidRegExp = Pattern.compile( - "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$" - ); + public static final Pattern uuidRegExp = Pattern + .compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); - public abstract DataSelectionService provideDataSelectionService( - RequestType requestType - ) - throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException; + public abstract DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException; public abstract void validateUUID() throws BadRequestException; public abstract String addParametersToLogString(); - public abstract void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException; + public abstract void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException; - public abstract boolean mustZip( - boolean zip, - DataSelectionService dataSelectionService - ); + public abstract boolean mustZip(boolean zip, DataSelectionService dataSelectionService); public abstract String getOperationId(); /** * returns the current sessionId. If it's null or not defined it should be created * @return - * @throws InternalException + * @throws InternalException */ public abstract String forceGetSessionId() throws InternalException; @@ -53,25 +47,19 @@ public abstract boolean mustZip( * @param id The String which has to be checked if it is a valid UUID * @throws BadRequestException */ - public static void validateUUID(String thing, String id) - throws BadRequestException { - if ( - id == null || !uuidRegExp.matcher(id).matches() - ) throw new BadRequestException( - "The " + thing + " parameter '" + id + "' is not a valid UUID" - ); + public static void validateUUID(String thing, String id) throws BadRequestException { + if (id == null || !uuidRegExp.matcher(id).matches()) + throw new BadRequestException("The " + thing + " parameter '" + id + "' is not a valid UUID"); } protected String createSessionId() throws InternalException { String sessionId; try { - sessionId = - ServiceProvider.getInstance().getIcatReader().getSessionId(); + sessionId = ServiceProvider.getInstance().getIcatReader().getSessionId(); } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage() - ); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } return sessionId; } + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java index f604cd3b..72576fa5 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java @@ -1,6 +1,5 @@ package org.icatproject.ids.requestHandlers.base; -import jakarta.json.stream.JsonGenerator; import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -12,91 +11,60 @@ import org.icatproject.ids.helpers.ValueContainer; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; +import jakarta.json.stream.JsonGenerator; + + public abstract class DataRequestHandler extends RequestHandlerBase { protected DataControllerBase dataController; - protected DataRequestHandler( - RequestType requestType, - String ip, - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds - ) { + + protected DataRequestHandler(RequestType requestType, String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { super(requestType, ip); - this.dataController = - new UnpreparedDataController( - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + + this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); } - protected DataRequestHandler( - RequestType requestType, - String ip, - String preparedId - ) { + protected DataRequestHandler(RequestType requestType, String ip, String preparedId) { super(requestType, ip); + this.dataController = new PreparedDataController(preparedId); } - protected DataRequestHandler( - RequestType requestType, - String ip, - String preparedId, - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds - ) { + protected DataRequestHandler(RequestType requestType, String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { super(requestType, ip); - if (sessionId != null) { - this.dataController = - new UnpreparedDataController( - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + + if(sessionId != null) { + this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); } else { this.dataController = new PreparedDataController(preparedId); } + } @Override - protected ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + protected ValueContainer handleRequest() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + this.dataController.validateUUID(); - DataSelectionService dataSelectionService = - this.dataController.provideDataSelectionService(this.requestType); + DataSelectionService dataSelectionService = this.dataController.provideDataSelectionService(this.requestType); ValueContainer result = this.handleDataRequest(dataSelectionService); return result; + } @Override - protected void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { this.dataController.addParametersToTransmitterJSON(gen); this.addCustomParametersToTransmitterJSON(gen); } - protected abstract ValueContainer handleDataRequest( - DataSelectionService dataSelectionService - ) - throws NotImplementedException, InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException; + protected abstract ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws NotImplementedException, InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException; @Override protected String addParametersToLogString() { - return ( - this.dataController.addParametersToLogString() + - " " + - this.addCustomParametersToLogString() - ); + return this.dataController.addParametersToLogString() + " " + this.addCustomParametersToLogString(); } /** @@ -108,7 +76,5 @@ protected void addCustomParametersToTransmitterJSON(JsonGenerator gen) {} /** * Override this method in your concrete DataRequestHandler to add custom parameters to the log output. */ - protected String addCustomParametersToLogString() { - return ""; - } + protected String addCustomParametersToLogString() { return "";} } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java b/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java index b9a006dc..f9fe6519 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java @@ -1,10 +1,10 @@ package org.icatproject.ids.requestHandlers.base; -import jakarta.json.stream.JsonGenerator; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.NoSuchFileException; + import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.RequestIdNames; import org.icatproject.ids.enums.RequestType; @@ -20,40 +20,30 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import jakarta.json.stream.JsonGenerator; + public class PreparedDataController extends DataControllerBase { - protected static final Logger logger = LoggerFactory.getLogger( - RequestHandlerBase.class - ); + protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); String preparedId; public PreparedDataController(String preparedId) { + this.preparedId = preparedId; } @Override - public DataSelectionService provideDataSelectionService( - RequestType requestType - ) - throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - var preparedDir = ServiceProvider - .getInstance() - .getPropertyHandler() - .getCacheDir() - .resolve("prepared"); + public DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, + BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + + var preparedDir = ServiceProvider.getInstance().getPropertyHandler().getCacheDir().resolve("prepared"); Prepared prepared; - try ( - InputStream stream = Files.newInputStream( - preparedDir.resolve(preparedId) - ) - ) { + try (InputStream stream = Files.newInputStream(preparedDir.resolve(preparedId))) { prepared = Prepared.unpack(stream); } catch (NoSuchFileException e) { - throw new NotFoundException( - "The preparedId " + preparedId + " is not known" - ); + throw new NotFoundException("The preparedId " + preparedId + " is not known"); } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -72,16 +62,12 @@ public String addParametersToLogString() { } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { gen.write(RequestIdNames.preparedId, this.preparedId); } @Override - public boolean mustZip( - boolean zip, - DataSelectionService dataSelectionService - ) { + public boolean mustZip(boolean zip, DataSelectionService dataSelectionService) { return zip; } @@ -94,4 +80,5 @@ public String getOperationId() { public String forceGetSessionId() throws InternalException { return this.createSessionId(); } + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java b/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java index 5281c60b..9524b7a0 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java @@ -1,10 +1,15 @@ package org.icatproject.ids.requestHandlers.base; -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; import java.io.ByteArrayOutputStream; import java.nio.file.Path; import java.util.regex.Pattern; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; + import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; @@ -17,8 +22,7 @@ import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.helpers.ValueContainer; import org.icatproject.ids.services.ServiceProvider; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + /** * This base class represents all common properties and methods which are needed by each request handler. @@ -26,9 +30,7 @@ */ public abstract class RequestHandlerBase { - protected static final Logger logger = LoggerFactory.getLogger( - RequestHandlerBase.class - ); + protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); protected Path preparedDir; protected boolean twoLevel; protected StorageUnit storageUnit; @@ -39,11 +41,11 @@ public abstract class RequestHandlerBase { /** * matches standard UUID format of 8-4-4-4-12 hexadecimal digits */ - public static final Pattern uuidRegExp = Pattern.compile( - "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$" - ); + public static final Pattern uuidRegExp = Pattern + .compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); + - protected RequestHandlerBase(RequestType requestType, String ip) { + protected RequestHandlerBase(RequestType requestType, String ip ) { this.requestType = requestType; this.ip = ip; @@ -56,6 +58,7 @@ protected RequestHandlerBase(RequestType requestType, String ip) { this.readOnly = propertyHandler.getReadOnly(); } + /** * Informs about the RequestType the handler ist providing a handling for. * @return @@ -74,16 +77,11 @@ public RequestType getRequestType() { * @throws DataNotOnlineException * @throws NotImplementedException */ - public ValueContainer handle() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handle() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + // some preprocessing long start = System.currentTimeMillis(); - logger.info( - "New webservice request: " + - this.requestType.toString().toLowerCase() + - " " + - this.addParametersToLogString() - ); + logger.info("New webservice request: " + this.requestType.toString().toLowerCase() + " " + this.addParametersToLogString()); // Do it ValueContainer result = this.handleRequest(); @@ -92,51 +90,36 @@ public ValueContainer handle() this.transmit(start); return result; + } + private void transmit(long start) throws BadRequestException { - if ( - ServiceProvider - .getInstance() - .getLogSet() - .contains(this.getCallType()) - ) { + if (ServiceProvider.getInstance().getLogSet().contains(this.getCallType())) { try { String body = this.provideTransmissionBody(); - ServiceProvider - .getInstance() - .getTransmitter() - .processMessage("archive", ip, body, start); + ServiceProvider.getInstance().getTransmitter().processMessage("archive", ip, body, start); } catch (IcatException_Exception e) { - logger.error( - "Failed to prepare jms message " + - e.getClass() + - " " + - e.getMessage() - ); + logger.error("Failed to prepare jms message " + e.getClass() + " " + e.getMessage()); } } } - public String provideTransmissionBody() - throws BadRequestException, IcatException_Exception { + public String provideTransmissionBody() throws BadRequestException, IcatException_Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try ( - JsonGenerator gen = Json.createGenerator(baos).writeStartObject() - ) { + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { this.addParametersToTransmitterJSON(gen); gen.writeEnd(); } return baos.toString(); } + /** * Override to add additional parameters to the log output for the current request * @return */ - protected String addParametersToLogString() { - return ""; - } + protected String addParametersToLogString() { return ""; } /** * Override to add additional parameters to the transmitter JSON @@ -144,8 +127,7 @@ protected String addParametersToLogString() { * @throws IcatException_Exception * @throws BadRequestException */ - protected void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException {} + protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException {} /** * The core method of each request handler. It has to be overwritten in the concrete implementation to provide an individual request handling @@ -157,12 +139,12 @@ protected void addParametersToTransmitterJSON(JsonGenerator gen) * @throws DataNotOnlineException * @throws NotImplementedException */ - protected abstract ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException; + protected abstract ValueContainer handleRequest() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException; /** * each handler should provide its own CallType which is needed to create the Transmitter message * @return the Calltype of the request */ protected abstract CallType getCallType(); + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java b/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java index fc737ad2..57668b02 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java @@ -1,6 +1,5 @@ package org.icatproject.ids.requestHandlers.base; -import jakarta.json.stream.JsonGenerator; import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -12,6 +11,8 @@ import org.icatproject.ids.services.dataSelectionService.DataSelectionService; import org.icatproject.ids.services.dataSelectionService.DataSelectionServiceFactory; +import jakarta.json.stream.JsonGenerator; + public class UnpreparedDataController extends DataControllerBase { public String sessionId; @@ -19,12 +20,7 @@ public class UnpreparedDataController extends DataControllerBase { public String datasetIds; public String datafileIds; - public UnpreparedDataController( - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds - ) { + public UnpreparedDataController(String sessionId, String investigationIds, String datasetIds, String datafileIds) { this.sessionId = sessionId; this.investigationIds = investigationIds; this.datasetIds = datasetIds; @@ -38,75 +34,42 @@ public void validateUUID() throws BadRequestException { @Override public String addParametersToLogString() { - return ( - "investigationIds='" + - investigationIds + - "' " + - "datasetIds='" + - datasetIds + - "' " + - "datafileIds='" + - datafileIds + - "'" - ); + return "investigationIds='" + investigationIds + "' " + "datasetIds='" + + datasetIds + "' " + "datafileIds='" + datafileIds + "'"; } @Override - public DataSelectionService provideDataSelectionService( - RequestType requestType - ) - throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - return DataSelectionServiceFactory.getService( - sessionId, - investigationIds, - datasetIds, - datafileIds, - requestType - ); + public DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + return DataSelectionServiceFactory.getService(sessionId, investigationIds, datasetIds, datafileIds, requestType); } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { - gen.write( - "userName", - ServiceProvider.getInstance().getIcat().getUserName(sessionId) - ); + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + gen.write("userName", ServiceProvider.getInstance().getIcat().getUserName(sessionId)); addIds(gen, investigationIds, datasetIds, datafileIds); } - protected void addIds( - JsonGenerator gen, - String investigationIds, - String datasetIds, - String datafileIds - ) throws BadRequestException { + + + protected void addIds(JsonGenerator gen, String investigationIds, String datasetIds, String datafileIds) + throws BadRequestException { if (investigationIds != null) { gen.writeStartArray("investigationIds"); - for (long invid : DataSelectionService.getValidIds( - "investigationIds", - investigationIds - )) { + for (long invid : DataSelectionService.getValidIds("investigationIds", investigationIds)) { gen.write(invid); } gen.writeEnd(); } if (datasetIds != null) { gen.writeStartArray("datasetIds"); - for (long invid : DataSelectionService.getValidIds( - "datasetIds", - datasetIds - )) { + for (long invid : DataSelectionService.getValidIds("datasetIds", datasetIds)) { gen.write(invid); } gen.writeEnd(); } if (datafileIds != null) { gen.writeStartArray("datafileIds"); - for (long invid : DataSelectionService.getValidIds( - "datafileIds", - datafileIds - )) { + for (long invid : DataSelectionService.getValidIds("datafileIds", datafileIds)) { gen.write(invid); } gen.writeEnd(); @@ -114,10 +77,7 @@ protected void addIds( } @Override - public boolean mustZip( - boolean zip, - DataSelectionService dataSelectionService - ) { + public boolean mustZip(boolean zip, DataSelectionService dataSelectionService) { return zip ? true : dataSelectionService.mustZip(); } @@ -131,7 +91,7 @@ public String forceGetSessionId() throws InternalException { if (this.sessionId == null) { this.sessionId = this.createSessionId(); } - + return this.sessionId; } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java index 46253d9f..0b4aa315 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java @@ -1,6 +1,7 @@ package org.icatproject.ids.requestHandlers.getSizeHandlers; import java.util.Map; + import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; @@ -18,48 +19,25 @@ public class GetSizeHandler extends DataRequestHandler { - public GetSizeHandler( - String ip, - String preparedId, - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds - ) { - super( - RequestType.GETSIZE, - ip, - preparedId, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + public GetSizeHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETSIZE, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService - ) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + logger.debug("Slow computation for normal case"); long size = 0; - size = - this.updateSizeFromDataInfoIds( - size, - dataSelectionService.getDfInfo(), - this.dataController.forceGetSessionId() - ); + size = this.updateSizeFromDataInfoIds(size, dataSelectionService.getDfInfo(), this.dataController.forceGetSessionId()); return new ValueContainer(size); } - protected long updateSizeFromDataInfoIds( - long size, - Map dataInfos, - String sessionId - ) throws InternalException { + + protected long updateSizeFromDataInfoIds(long size, Map dataInfos, String sessionId) throws InternalException { StringBuilder sb = new StringBuilder(); int n = 0; for (DataInfoBase dataInfo : dataInfos.values()) { @@ -80,18 +58,11 @@ protected long updateSizeFromDataInfoIds( return size; } - private long getSizeFor(String sessionId, StringBuilder sb) - throws InternalException { - String query = - "SELECT SUM(df.fileSize) from Datafile df WHERE df.id IN (" + - sb.toString() + - ") AND df.location IS NOT NULL"; + + private long getSizeFor(String sessionId, StringBuilder sb) throws InternalException { + String query = "SELECT SUM(df.fileSize) from Datafile df WHERE df.id IN (" + sb.toString() + ") AND df.location IS NOT NULL"; try { - return (Long) ServiceProvider - .getInstance() - .getIcat() - .search(sessionId, query) - .get(0); + return (Long) ServiceProvider.getInstance().getIcat().search(sessionId, query).get(0); } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (IndexOutOfBoundsException e) { @@ -103,4 +74,5 @@ private long getSizeFor(String sessionId, StringBuilder sb) public CallType getCallType() { return CallType.INFO; } + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java index d65eb51e..6b57cd9b 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java @@ -1,7 +1,7 @@ package org.icatproject.ids.requestHandlers.getSizeHandlers; -import jakarta.json.stream.JsonGenerator; import java.util.List; + import org.icatproject.Datafile; import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.CallType; @@ -18,92 +18,50 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; +import jakarta.json.stream.JsonGenerator; + public class GetSizeHandlerForFastProcessing extends RequestHandlerBase { UnpreparedDataController dataController; - public GetSizeHandlerForFastProcessing( - String ip, - String sessionId, - String investigationIds, - String datasetIds, - String datafileIds - ) { + public GetSizeHandlerForFastProcessing(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { super(RequestType.GETSIZE, ip); - this.dataController = - new UnpreparedDataController( - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + + this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + this.dataController.validateUUID(); var serviceProvider = ServiceProvider.getInstance(); - List dfids = DataSelectionService.getValidIds( - "datafileIds", - dataController.datafileIds - ); - List dsids = DataSelectionService.getValidIds( - "datasetIds", - dataController.datasetIds - ); - List invids = DataSelectionService.getValidIds( - "investigationIds", - dataController.investigationIds - ); + List dfids = DataSelectionService.getValidIds("datafileIds", dataController.datafileIds); + List dsids = DataSelectionService.getValidIds("datasetIds", dataController.datasetIds); + List invids = DataSelectionService.getValidIds("investigationIds", dataController.investigationIds); + if (dfids.size() + dsids.size() + invids.size() == 1) { + long size = 0; - size = - getSizeFor( - dataController.getOperationId(), - invids, - "df.dataset.investigation.id" - ) + - getSizeFor( - dataController.getOperationId(), - dsids, - "df.dataset.id" - ) + - getSizeFor(dataController.getOperationId(), dfids, "df.id"); + size = getSizeFor(dataController.getOperationId(), invids, "df.dataset.investigation.id") + + getSizeFor(dataController.getOperationId(), dsids, "df.dataset.id") + + getSizeFor(dataController.getOperationId(), dfids, "df.id"); logger.debug("Fast computation for simple case"); if (size == 0) { try { if (dfids.size() != 0) { - Datafile datafile = (Datafile) serviceProvider - .getIcat() - .get( - dataController.getOperationId(), - "Datafile", - dfids.get(0) - ); + Datafile datafile = (Datafile) serviceProvider.getIcat().get(dataController.getOperationId(), "Datafile", dfids.get(0)); if (datafile.getLocation() == null) { throw new NotFoundException("Datafile not found"); } } if (dsids.size() != 0) { - serviceProvider - .getIcat() - .get( - dataController.getOperationId(), - "Dataset", - dsids.get(0) - ); + serviceProvider.getIcat().get(dataController.getOperationId(), "Dataset", dsids.get(0)); } if (invids.size() != 0) { - serviceProvider - .getIcat() - .get( - dataController.getOperationId(), - "Investigation", - invids.get(0) - ); + serviceProvider.getIcat().get(dataController.getOperationId(), "Investigation", invids.get(0)); } } catch (IcatException_Exception e) { throw new NotFoundException(e.getMessage()); @@ -112,8 +70,10 @@ public ValueContainer handleRequest() return new ValueContainer(size); } - + return ValueContainer.getInvalid(); //is case of fast computation is not the right way. + + } @Override @@ -121,19 +81,17 @@ public CallType getCallType() { return CallType.INFO; } - public String addParametersToLogString() { - return this.dataController.addParametersToLogString(); - } + public String addParametersToLogString() { return this.dataController.addParametersToLogString(); } - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { this.dataController.addParametersToTransmitterJSON(gen); } - private long getSizeFor(String sessionId, List ids, String where) - throws InternalException { + private long getSizeFor(String sessionId, List ids, String where) throws InternalException { + long size = 0; if (ids != null) { + StringBuilder sb = new StringBuilder(); int n = 0; for (Long id : ids) { @@ -154,25 +112,16 @@ private long getSizeFor(String sessionId, List ids, String where) return size; } - private long evalSizeFor(String sessionId, String where, StringBuilder sb) - throws InternalException { - String query = - "SELECT SUM(df.fileSize) from Datafile df WHERE " + - where + - " IN (" + - sb.toString() + - ") AND df.location IS NOT NULL"; + private long evalSizeFor(String sessionId, String where, StringBuilder sb) throws InternalException { + String query = "SELECT SUM(df.fileSize) from Datafile df WHERE " + where + " IN (" + sb.toString() + ") AND df.location IS NOT NULL"; logger.debug("icat query for size: {}", query); try { - return (Long) ServiceProvider - .getInstance() - .getIcat() - .search(sessionId, query) - .get(0); + return (Long) ServiceProvider.getInstance().getIcat().search(sessionId, query).get(0); } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (IndexOutOfBoundsException e) { return 0L; } } + } diff --git a/src/main/java/org/icatproject/ids/services/ICATGetter.java b/src/main/java/org/icatproject/ids/services/ICATGetter.java index 669f86d4..4073c3ce 100644 --- a/src/main/java/org/icatproject/ids/services/ICATGetter.java +++ b/src/main/java/org/icatproject/ids/services/ICATGetter.java @@ -1,8 +1,10 @@ package org.icatproject.ids.services; -import jakarta.xml.ws.WebServiceException; import java.net.MalformedURLException; import java.net.URL; + +import jakarta.xml.ws.WebServiceException; + import org.icatproject.ICAT; import org.icatproject.ICATService; import org.icatproject.IcatException; @@ -15,10 +17,7 @@ */ public class ICATGetter { - private static String[] suffices = new String[] { - "ICATService/ICAT?wsdl", - "icat/ICAT?wsdl", - }; + private static String[] suffices = new String[]{"ICATService/ICAT?wsdl", "icat/ICAT?wsdl"}; /** * Provide access to an ICAT SOAP web service with the basic URL string @@ -30,8 +29,8 @@ public class ICATGetter { * @return an ICAT * @throws IcatException_Exception if something is wrong */ - public static ICAT getService(String urlString) - throws IcatException_Exception { + public static ICAT getService(String urlString) throws IcatException_Exception { + if (urlString == null) { throwSessionException("Argument to constructor must not be null"); } @@ -48,17 +47,14 @@ public static ICAT getService(String urlString) for (String suffix : suffices) { String icatUrlWsdl = urlString + "/" + suffix; try { - icatService = - new ICATService(new URL(icatUrlWsdl)).getICATPort(); + icatService = new ICATService(new URL(icatUrlWsdl)).getICATPort(); icatService.getApiVersion(); return icatService; } catch (MalformedURLException e) { throwSessionException("Invalid URL"); } catch (WebServiceException e) { Throwable cause = e.getCause(); - if ( - cause != null && cause.getMessage().contains("security") - ) { + if (cause != null && cause.getMessage().contains("security")) { throwSessionException(cause.getMessage()); } } catch (Exception e) { @@ -80,6 +76,7 @@ public static ICAT getService(String urlString) } catch (Exception e) { throwSessionException(e.getMessage()); } + } throwSessionException("Unable to connect to: " + urlString); return null; // To please the compiler @@ -89,10 +86,7 @@ public static String getCleanUrl(String urlString) { for (String suffix : suffices) { suffix = "/" + suffix; if (urlString.endsWith(suffix)) { - return urlString.substring( - 0, - urlString.length() - suffix.length() - ); + return urlString.substring(0, urlString.length() - suffix.length()); } } if (urlString.endsWith("/")) { @@ -101,11 +95,11 @@ public static String getCleanUrl(String urlString) { return urlString; } - private static void throwSessionException(String msg) - throws IcatException_Exception { + private static void throwSessionException(String msg) throws IcatException_Exception { IcatException except = new IcatException(); except.setMessage(msg); except.setType(IcatExceptionType.SESSION); throw new IcatException_Exception(msg, new IcatException()); } + } diff --git a/src/main/java/org/icatproject/ids/services/IcatReader.java b/src/main/java/org/icatproject/ids/services/IcatReader.java index a5ec4dc7..9f5adbb8 100644 --- a/src/main/java/org/icatproject/ids/services/IcatReader.java +++ b/src/main/java/org/icatproject/ids/services/IcatReader.java @@ -1,16 +1,19 @@ package org.icatproject.ids.services; +import java.util.List; + import jakarta.annotation.PostConstruct; import jakarta.ejb.Singleton; -import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.icatproject.EntityBaseBean; import org.icatproject.ICAT; import org.icatproject.IcatExceptionType; import org.icatproject.IcatException_Exception; import org.icatproject.Login.Credentials; import org.icatproject.Login.Credentials.Entry; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; @Singleton public class IcatReader { @@ -19,14 +22,14 @@ public class IcatReader { private String sessionId; - private static final Logger logger = LoggerFactory.getLogger( - IcatReader.class - ); + private final static Logger logger = LoggerFactory.getLogger(IcatReader.class); private PropertyHandler propertyHandler; // required for injection when other constructors exist - public IcatReader() {} + public IcatReader() { + + } // primarily required for testing to make it possible to use a mocked PropertyHandler public IcatReader(PropertyHandler propertyHandler) { @@ -40,9 +43,7 @@ private void init() { login(); logger.info("Reader started"); } catch (Exception e) { - throw new RuntimeException( - "Reader reports " + e.getClass() + " " + e.getMessage() - ); + throw new RuntimeException("Reader reports " + e.getClass() + " " + e.getMessage()); } } @@ -53,6 +54,7 @@ private void login() throws IcatException_Exception { icat = propertyHandler.getIcatService(); List creds = propertyHandler.getReader(); if (creds != null) { + Credentials credentials = new Credentials(); List entries = credentials.getEntry(); for (int i = 1; i < creds.size(); i += 2) { @@ -65,8 +67,7 @@ private void login() throws IcatException_Exception { } } - public EntityBaseBean get(String query, long id) - throws IcatException_Exception { + public EntityBaseBean get(String query, long id) throws IcatException_Exception { try { return icat.get(sessionId, query, id); } catch (IcatException_Exception e) { @@ -105,4 +106,5 @@ public String getSessionId() throws IcatException_Exception { } } } + } diff --git a/src/main/java/org/icatproject/ids/services/LockManager.java b/src/main/java/org/icatproject/ids/services/LockManager.java index e87c44aa..49beba05 100644 --- a/src/main/java/org/icatproject/ids/services/LockManager.java +++ b/src/main/java/org/icatproject/ids/services/LockManager.java @@ -1,30 +1,30 @@ package org.icatproject.ids.services; -import jakarta.annotation.PostConstruct; -import jakarta.ejb.Singleton; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; + +import jakarta.annotation.PostConstruct; +import jakarta.ejb.Singleton; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.plugin.MainStorageInterface; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; @Singleton public class LockManager { public enum LockType { - SHARED, - EXCLUSIVE, + SHARED, EXCLUSIVE } public class LockInfo { - public final Long id; public final LockType type; public final int count; @@ -37,7 +37,6 @@ public class LockInfo { } private class LockEntry { - final Long id; final LockType type; int count; @@ -66,7 +65,6 @@ void dec() { * Define the common interface of SingleLock and LockCollection */ public abstract class Lock implements AutoCloseable { - public abstract void release(); public void close() { @@ -75,7 +73,6 @@ public void close() { } private class SingleLock extends Lock { - private final Long id; private boolean isValid; private AutoCloseable storageLock; @@ -95,11 +92,7 @@ public void release() { try { storageLock.close(); } catch (Exception e) { - logger.error( - "Error while closing lock on {} in the storage plugin: {}.", - id, - e.getMessage() - ); + logger.error("Error while closing lock on {} in the storage plugin: {}.", id, e.getMessage()); } } logger.debug("Released a lock on {}.", id); @@ -109,7 +102,6 @@ public void release() { } private class LockCollection extends Lock { - private ArrayList locks; LockCollection() { @@ -139,8 +131,7 @@ private void init() { logger.debug("LockManager initialized."); } - public Lock lock(DatasetInfo ds, LockType type) - throws AlreadyLockedException, IOException { + public Lock lock(DatasetInfo ds, LockType type) throws AlreadyLockedException, IOException { Long id = ds.getDsId(); assert id != null; synchronized (lockMap) { @@ -148,9 +139,7 @@ public Lock lock(DatasetInfo ds, LockType type) if (le == null) { le = new LockEntry(id, type); } else { - if ( - type == LockType.EXCLUSIVE || le.type == LockType.EXCLUSIVE - ) { + if (type == LockType.EXCLUSIVE || le.type == LockType.EXCLUSIVE) { throw new AlreadyLockedException(); } } @@ -167,17 +156,12 @@ public Lock lock(DatasetInfo ds, LockType type) } } - public Lock lock(Collection datasets, LockType type) - throws AlreadyLockedException, IOException, InternalException { + public Lock lock(Collection datasets, LockType type) throws AlreadyLockedException, IOException, InternalException { LockCollection locks = new LockCollection(); try { for (DataInfoBase dataInfo : datasets) { DatasetInfo ds = (DatasetInfo) dataInfo; - if (ds == null) throw new InternalException( - "Could not cast " + - dataInfo.getClass() + - " to DataSetInfo. Did you handed over another sub type of DataInfoBase? " - ); + if(ds == null) throw new InternalException("Could not cast " + dataInfo.getClass() + " to DataSetInfo. Did you handed over another sub type of DataInfoBase? "); locks.add(lock(ds, type)); } } catch (AlreadyLockedException | IOException e) { @@ -196,4 +180,5 @@ public Collection getLockInfo() { return lockInfo; } } + } diff --git a/src/main/java/org/icatproject/ids/services/PropertyHandler.java b/src/main/java/org/icatproject/ids/services/PropertyHandler.java index 7ea1dc50..e3cbe354 100644 --- a/src/main/java/org/icatproject/ids/services/PropertyHandler.java +++ b/src/main/java/org/icatproject/ids/services/PropertyHandler.java @@ -1,7 +1,5 @@ package org.icatproject.ids.services; -import jakarta.json.Json; -import jakarta.json.JsonReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -15,6 +13,13 @@ import java.util.List; import java.util.Properties; import java.util.Set; + +import jakarta.json.Json; +import jakarta.json.JsonReader; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.CallType; @@ -24,8 +29,6 @@ import org.icatproject.ids.plugin.ZipMapperInterface; import org.icatproject.utils.CheckedProperties; import org.icatproject.utils.CheckedProperties.CheckedPropertyException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /* * Load the properties specified in the properties file ids.properties. @@ -33,11 +36,9 @@ public class PropertyHandler { private static PropertyHandler instance = null; - private static final Logger logger = LoggerFactory.getLogger( - PropertyHandler.class - ); + private static final Logger logger = LoggerFactory.getLogger(PropertyHandler.class); - public static synchronized PropertyHandler getInstance() { + public synchronized static PropertyHandler getInstance() { if (instance == null) { instance = new PropertyHandler(); } @@ -80,6 +81,7 @@ public static Logger getLogger() { @SuppressWarnings("unchecked") private PropertyHandler() { + CheckedProperties props = new CheckedProperties(); try { @@ -94,21 +96,12 @@ private PropertyHandler() { } preparedCount = props.getPositiveInt("preparedCount"); - processQueueIntervalSeconds = - props.getPositiveLong("processQueueIntervalSeconds"); - rootUserNames = - new HashSet<>( - Arrays.asList( - props.getString("rootUserNames").trim().split("\\s+") - ) - ); - - reader = - Arrays.asList(props.getString("reader").trim().split("\\s+")); + processQueueIntervalSeconds = props.getPositiveLong("processQueueIntervalSeconds"); + rootUserNames = new HashSet<>(Arrays.asList(props.getString("rootUserNames").trim().split("\\s+"))); + + reader = Arrays.asList(props.getString("reader").trim().split("\\s+")); if (reader.size() % 2 != 1) { - throw new IllegalStateException( - "reader must have an odd number of words" - ); + throw new IllegalStateException("reader must have an odd number of words"); } readOnly = props.getBoolean("readOnly", false); @@ -118,131 +111,82 @@ private PropertyHandler() { enableWrite = !readOnly; } - sizeCheckIntervalMillis = - props.getPositiveInt("sizeCheckIntervalSeconds") * 1000L; + sizeCheckIntervalMillis = props.getPositiveInt("sizeCheckIntervalSeconds") * 1000L; if (props.has("key")) { key = props.getString("key"); } try { - Class klass = (Class) Class.forName( - props.getString("plugin.zipMapper.class") - ); + Class klass = (Class) Class + .forName(props.getString("plugin.zipMapper.class")); zipMapper = klass.getConstructor().newInstance(); logger.debug("ZipMapper initialised"); - } catch ( - ClassNotFoundException - | InstantiationException - | IllegalAccessException - | IllegalArgumentException - | InvocationTargetException - | NoSuchMethodException - | SecurityException e - ) { + + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException | SecurityException e) { abort(e.getClass() + " " + e.getMessage()); } // Now get simple properties to pass to the plugins Properties simpleProps = new Properties(); - try ( - InputStream is = getClass() - .getClassLoader() - .getResourceAsStream("run.properties") - ) { + try (InputStream is = getClass().getClassLoader().getResourceAsStream("run.properties")) { simpleProps.load(is); } catch (IOException e) { abort(e.getClass() + " " + e.getMessage()); } try { - Class klass = (Class) Class.forName( - props.getString("plugin.main.class") - ); - mainStorage = - klass - .getConstructor(Properties.class) - .newInstance(simpleProps); + Class klass = (Class) Class + .forName(props.getString("plugin.main.class")); + mainStorage = klass.getConstructor(Properties.class).newInstance(simpleProps); logger.debug("mainStorage initialised"); - } catch ( - ClassNotFoundException - | InstantiationException - | IllegalAccessException - | IllegalArgumentException - | InvocationTargetException - | NoSuchMethodException - | SecurityException e - ) { + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException | SecurityException e) { logger.error("Plugin failed...", e); abort(e.getClass() + " " + e.getMessage()); } if (!props.has("plugin.archive.class")) { - logger.info( - "Property plugin.archive.class not set, single storage enabled." - ); + logger.info("Property plugin.archive.class not set, single storage enabled."); } else { try { - Class klass = (Class) Class.forName( - props.getString("plugin.archive.class") - ); - archiveStorage = - klass - .getConstructor(Properties.class) - .newInstance(simpleProps); + Class klass = (Class) Class + .forName(props.getString("plugin.archive.class")); + archiveStorage = klass.getConstructor(Properties.class).newInstance(simpleProps); logger.debug("archiveStorage initialised"); - } catch ( - ClassNotFoundException - | InstantiationException - | IllegalAccessException - | IllegalArgumentException - | InvocationTargetException - | NoSuchMethodException - | SecurityException e - ) { + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException + | IllegalArgumentException | InvocationTargetException | NoSuchMethodException + | SecurityException e) { logger.error("Plugin failed...", e); abort(e.getClass() + " " + e.getMessage()); } - startArchivingLevel = - props.getPositiveLong("startArchivingLevel1024bytes") * - 1024; - stopArchivingLevel = - props.getPositiveLong("stopArchivingLevel1024bytes") * 1024; + startArchivingLevel = props.getPositiveLong("startArchivingLevel1024bytes") * 1024; + stopArchivingLevel = props.getPositiveLong("stopArchivingLevel1024bytes") * 1024; if (stopArchivingLevel >= startArchivingLevel) { - abort( - "startArchivingLevel1024bytes must be greater than stopArchivingLevel1024bytes" - ); + abort("startArchivingLevel1024bytes must be greater than stopArchivingLevel1024bytes"); } try { String storageUnitName = props.getString("storageUnit"); - storageUnit = - StorageUnit.valueOf(storageUnitName.toUpperCase()); + storageUnit = StorageUnit.valueOf(storageUnitName.toUpperCase()); } catch (IllegalArgumentException e) { List vs = new ArrayList<>(); for (StorageUnit s : StorageUnit.values()) { vs.add(s.name()); } - abort( - "storageUnit value " + - props.getString("storageUnit") + - " must be taken from " + - vs - ); + abort("storageUnit value " + props.getString("storageUnit") + " must be taken from " + vs); } if (storageUnit == StorageUnit.DATASET) { - delayDatasetWrites = - props.getPositiveLong("delayDatasetWritesSeconds"); + delayDatasetWrites = props.getPositiveLong("delayDatasetWritesSeconds"); } else if (storageUnit == StorageUnit.DATAFILE) { - delayDatafileOperations = - props.getPositiveLong("delayDatafileOperationsSeconds"); + delayDatafileOperations = props.getPositiveLong("delayDatafileOperationsSeconds"); } tidyBlockSize = props.getPositiveInt("tidyBlockSize"); } try { - cacheDir = - props.getFile("cache.dir").getCanonicalFile().toPath(); + cacheDir = props.getFile("cache.dir").getCanonicalFile().toPath(); } catch (IOException e) { abort("IOException " + e.getMessage()); } @@ -253,39 +197,26 @@ private PropertyHandler() { maxIdsInQuery = props.getPositiveInt("maxIdsInQuery"); /* JMS stuff */ - jmsTopicConnectionFactory = - props.getString( - "jms.topicConnectionFactory", - "java:comp/DefaultJMSConnectionFactory" - ); + jmsTopicConnectionFactory = props.getString("jms.topicConnectionFactory", + "java:comp/DefaultJMSConnectionFactory"); /* Call logging categories */ if (props.has("log.list")) { - for (String callTypeString : props - .getString("log.list") - .split("\\s+")) { + for (String callTypeString : props.getString("log.list").split("\\s+")) { try { - logSet.add( - CallType.valueOf(callTypeString.toUpperCase()) - ); + logSet.add(CallType.valueOf(callTypeString.toUpperCase())); } catch (IllegalArgumentException e) { - abort( - "Value " + - callTypeString + - " in log.list must be chosen from " + - Arrays.asList(CallType.values()) - ); + abort("Value " + callTypeString + " in log.list must be chosen from " + + Arrays.asList(CallType.values())); } } logger.info("log.list: " + logSet); } else { - logger.info( - "'log.list' entry not present so no JMS call logging will be performed" - ); + logger.info("'log.list' entry not present so no JMS call logging will be performed"); } - useReaderForPerformance = - props.getBoolean("useReaderForPerformance", false); + useReaderForPerformance = props.getBoolean("useReaderForPerformance", false); + } catch (CheckedPropertyException e) { abort(e.getMessage()); } @@ -316,13 +247,8 @@ public synchronized ICAT getIcatService() { try { icatService = ICATGetter.getService(icatUrl); } catch (IcatException_Exception e) { - String msg = - "Problem finding ICAT API version at " + - icatUrl + - ": " + - e.getFaultInfo().getType() + - " " + - e.getMessage(); + String msg = "Problem finding ICAT API version at " + icatUrl + ": " + e.getFaultInfo().getType() + " " + + e.getMessage(); logger.error(msg); try { Thread.sleep(10000); @@ -359,30 +285,15 @@ public synchronized int getMaxEntities() { // will produce an error message. while (maxEntities == null) { try { - org.icatproject.icat.client.ICAT ricat = new org.icatproject.icat.client.ICAT( - icatUrl - ); - - try ( - JsonReader parser = Json.createReader( - new ByteArrayInputStream( - ricat.getProperties().getBytes() - ) - ) - ) { + org.icatproject.icat.client.ICAT ricat = new org.icatproject.icat.client.ICAT(icatUrl); + + try (JsonReader parser = Json + .createReader(new ByteArrayInputStream(ricat.getProperties().getBytes()))) { maxEntities = parser.readObject().getInt("maxEntities"); - logger.info( - "maxEntities from the ICAT.server {} version {} is {}", - icatUrl, - ricat.getVersion(), - maxEntities - ); + logger.info("maxEntities from the ICAT.server {} version {} is {}", icatUrl, ricat.getVersion(), + maxEntities); } catch (Exception e) { - String msg = - "Problem finding 1 ICAT API version " + - e.getClass() + - " " + - e.getMessage(); + String msg = "Problem finding 1 ICAT API version " + e.getClass() + " " + e.getMessage(); logger.error(msg); try { Thread.sleep(10000); @@ -391,11 +302,7 @@ public synchronized int getMaxEntities() { } } } catch (URISyntaxException e) { - String msg = - "Problem finding 2 ICAT API version " + - e.getClass() + - " " + - e.getMessage(); + String msg = "Problem finding 2 ICAT API version " + e.getClass() + " " + e.getMessage(); logger.error(msg); throw new IllegalStateException(msg); } diff --git a/src/main/java/org/icatproject/ids/services/ServiceProvider.java b/src/main/java/org/icatproject/ids/services/ServiceProvider.java index dace322c..dc179ad5 100644 --- a/src/main/java/org/icatproject/ids/services/ServiceProvider.java +++ b/src/main/java/org/icatproject/ids/services/ServiceProvider.java @@ -1,6 +1,7 @@ package org.icatproject.ids.services; import java.util.Set; + import org.icatproject.ICAT; import org.icatproject.ids.enums.CallType; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; @@ -20,13 +21,7 @@ public class ServiceProvider { private IcatReader icatReader; private PropertyHandler propertyHandler; - private ServiceProvider( - PropertyHandler propertyHandler, - Transmitter transmitter, - FiniteStateMachine fsm, - LockManager lockManager, - IcatReader reader - ) { + private ServiceProvider(PropertyHandler propertyHandler, Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { this.transmitter = transmitter; this.fsm = fsm; this.lockManager = lockManager; @@ -41,45 +36,21 @@ private ServiceProvider( * @param lockManager * @param reader */ - public static void createInstance( - Transmitter transmitter, - FiniteStateMachine fsm, - LockManager lockManager, - IcatReader reader - ) { - createInstance( - PropertyHandler.getInstance(), - transmitter, - fsm, - lockManager, - reader - ); + public static void createInstance(Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { + + createInstance(PropertyHandler.getInstance(), transmitter, fsm, lockManager, reader); } - public static void createInstance( - PropertyHandler propertyHandler, - Transmitter transmitter, - FiniteStateMachine fsm, - LockManager lockManager, - IcatReader reader - ) { - if (instance != null) return; - - instance = - new ServiceProvider( - propertyHandler, - transmitter, - fsm, - lockManager, - reader - ); + public static void createInstance(PropertyHandler propertyHandler, Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { + + if(instance != null) return; + + instance = new ServiceProvider(propertyHandler, transmitter, fsm, lockManager, reader); } public static ServiceProvider getInstance() { - if (instance == null) { - throw new RuntimeException( - "ServiceProvider is not yet instantiated, please call createInstance at first." - ); + if(instance == null) { + throw new RuntimeException("ServiceProvider is not yet instantiated, please call createInstance at first."); } return instance; } @@ -115,4 +86,6 @@ public ICAT getIcat() { public Set getLogSet() { return PropertyHandler.getInstance().getLogSet(); } + + } diff --git a/src/main/java/org/icatproject/ids/services/Transmitter.java b/src/main/java/org/icatproject/ids/services/Transmitter.java index 011b3d19..996522cb 100644 --- a/src/main/java/org/icatproject/ids/services/Transmitter.java +++ b/src/main/java/org/icatproject/ids/services/Transmitter.java @@ -1,5 +1,8 @@ package org.icatproject.ids.services; +import javax.naming.InitialContext; +import javax.naming.NamingException; + import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import jakarta.ejb.Singleton; @@ -10,8 +13,7 @@ import jakarta.jms.Topic; import jakarta.jms.TopicConnection; import jakarta.jms.TopicConnectionFactory; -import javax.naming.InitialContext; -import javax.naming.NamingException; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.Marker; @@ -21,7 +23,7 @@ public class Transmitter { private static Logger logger = LoggerFactory.getLogger(Transmitter.class); - private static final Marker fatal = MarkerFactory.getMarker("FATAL"); + private final static Marker fatal = MarkerFactory.getMarker("FATAL"); private Topic topic; @@ -29,12 +31,12 @@ public class Transmitter { @PostConstruct private void init() { + try { PropertyHandler propertyHandler = PropertyHandler.getInstance(); InitialContext ic = new InitialContext(); - TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ic.lookup( - propertyHandler.getJmsTopicConnectionFactory() - ); + TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ic + .lookup(propertyHandler.getJmsTopicConnectionFactory()); topicConnection = topicConnectionFactory.createTopicConnection(); topic = (Topic) ic.lookup("jms/IDS/log"); logger.info("Notification Transmitter created"); @@ -42,9 +44,10 @@ private void init() { logger.error(fatal, "Problem with JMS " + e); throw new IllegalStateException(e.getMessage()); } + } - @PreDestroy + @PreDestroy() private void exit() { try { if (topicConnection != null) { @@ -56,25 +59,12 @@ private void exit() { } } - public void processMessage( - String operation, - String ip, - String body, - long startMillis - ) { - try ( - Session jmsSession = topicConnection.createSession( - false, - Session.AUTO_ACKNOWLEDGE - ) - ) { + public void processMessage(String operation, String ip, String body, long startMillis) { + try (Session jmsSession = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE)) { TextMessage jmsg = jmsSession.createTextMessage(body); jmsg.setStringProperty("operation", operation); jmsg.setStringProperty("ip", ip); - jmsg.setLongProperty( - "millis", - System.currentTimeMillis() - startMillis - ); + jmsg.setLongProperty("millis", System.currentTimeMillis() - startMillis); jmsg.setLongProperty("start", startMillis); MessageProducer jmsProducer = jmsSession.createProducer(topic); jmsProducer.send(jmsg); @@ -83,4 +73,5 @@ public void processMessage( logger.error("Failed to send jms message " + operation + " " + ip); } } + } diff --git a/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java b/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java index 84bb07a9..5f9b372b 100644 --- a/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java +++ b/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; + import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.IcatExceptionType; @@ -13,8 +14,8 @@ import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.helpers.LocationHelper; -import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatafileInfo; +import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,49 +26,33 @@ */ public class UnfinishedWorkService { - protected static final Logger logger = LoggerFactory.getLogger( - UnfinishedWorkService.class - ); - public UnfinishedWorkService() {} + protected final static Logger logger = LoggerFactory.getLogger(UnfinishedWorkService.class); + + public UnfinishedWorkService() { + + } + + public void restartUnfinishedWork(Path markerDir, String key) throws InternalException { - public void restartUnfinishedWork(Path markerDir, String key) - throws InternalException { try { var serviceProvider = ServiceProvider.getInstance(); - StorageUnit storageUnit = serviceProvider - .getPropertyHandler() - .getStorageUnit(); + StorageUnit storageUnit = serviceProvider.getPropertyHandler().getStorageUnit(); for (File file : markerDir.toFile().listFiles()) { - if (storageUnit == null) break; - long id = Long.parseLong( - file.toPath().getFileName().toString() - ); + if(storageUnit == null) break; + + long id = Long.parseLong(file.toPath().getFileName().toString()); try { DataInfoBase dataInfo = this.loadDataInfo(storageUnit, id); serviceProvider.getFsm().queue(dataInfo, DeferredOp.WRITE); - logger.info( - "Queued " + - storageUnit.toString().toLowerCase() + - " with id " + - id + - " " + - dataInfo + - " to be written as it was not written out previously by IDS" - ); + logger.info("Queued " + storageUnit.toString().toLowerCase() + " with id " + id + " " + dataInfo + + " to be written as it was not written out previously by IDS"); } catch (IcatException_Exception e) { - if ( - e.getFaultInfo().getType() == - IcatExceptionType.NO_SUCH_OBJECT_FOUND - ) { - logger.warn( - storageUnit.toString().toLowerCase() + - " with id " + - id + - " was not written out by IDS and now no longer known to ICAT" - ); + if (e.getFaultInfo().getType() == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { + logger.warn( storageUnit.toString().toLowerCase() + " with id " + id + + " was not written out by IDS and now no longer known to ICAT"); Files.delete(file.toPath()); } else { throw e; @@ -79,6 +64,7 @@ public void restartUnfinishedWork(Path markerDir, String key) } } + public void cleanPreparedDir(Path preparedDir) { for (File file : preparedDir.toFile().listFiles()) { Path path = file.toPath(); @@ -94,9 +80,7 @@ public void cleanPreparedDir(Path preparedDir) { } thisSize += Files.size(path); Files.delete(path); - logger.debug( - "Deleted " + path + " to reclaim " + thisSize + " bytes" - ); + logger.debug("Deleted " + path + " to reclaim " + thisSize + " bytes"); } catch (IOException e) { logger.debug("Failed to delete " + path + e.getMessage()); } @@ -104,55 +88,31 @@ public void cleanPreparedDir(Path preparedDir) { } } + public void cleanDatasetCache(Path datasetDir) { for (File dsFile : datasetDir.toFile().listFiles()) { Path path = dsFile.toPath(); try { long thisSize = Files.size(path); Files.delete(path); - logger.debug( - "Deleted " + path + " to reclaim " + thisSize + " bytes" - ); + logger.debug("Deleted " + path + " to reclaim " + thisSize + " bytes"); } catch (IOException e) { - logger.debug( - "Failed to delete " + - path + - " " + - e.getClass() + - " " + - e.getMessage() - ); + logger.debug("Failed to delete " + path + " " + e.getClass() + " " + e.getMessage()); } } } - private DataInfoBase loadDataInfo(StorageUnit storageUnit, long id) - throws IcatException_Exception, InsufficientPrivilegesException, InternalException { - if (storageUnit == StorageUnit.DATASET) { - Dataset ds = (Dataset) ServiceProvider - .getInstance() - .getIcatReader() - .get("Dataset ds INCLUDE ds.investigation.facility", id); + + private DataInfoBase loadDataInfo(StorageUnit storageUnit, long id) throws IcatException_Exception, InsufficientPrivilegesException, InternalException { + if(storageUnit == StorageUnit.DATASET) { + Dataset ds = (Dataset) ServiceProvider.getInstance().getIcatReader().get("Dataset ds INCLUDE ds.investigation.facility", id); return new DatasetInfo(ds); } - if (storageUnit == StorageUnit.DATAFILE) { - Datafile df = (Datafile) ServiceProvider - .getInstance() - .getIcatReader() - .get("Datafile ds INCLUDE ds.dataset", id); - String location = LocationHelper.getLocation( - df.getId(), - df.getLocation() - ); - return new DatafileInfo( - id, - df.getName(), - location, - df.getCreateId(), - df.getModId(), - df.getDataset().getId() - ); + if(storageUnit == StorageUnit.DATAFILE) { + Datafile df = (Datafile) ServiceProvider.getInstance().getIcatReader().get("Datafile ds INCLUDE ds.dataset", id); + String location = LocationHelper.getLocation(df.getId(), df.getLocation()); + return new DatafileInfo(id, df.getName(), location, df.getCreateId(), df.getModId(), df.getDataset().getId()); } return null; diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java index 63de4261..650e410f 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java @@ -15,6 +15,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.locks.ReentrantLock; + import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; @@ -22,67 +23,42 @@ import org.icatproject.ids.exceptions.DataNotOnlineException; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.exceptions.NotImplementedException; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DataSelection; -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.services.ServiceProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public abstract class DataSelectionService { - protected static final Logger logger = LoggerFactory.getLogger( - DataSelectionService.class - ); + protected final static Logger logger = LoggerFactory.getLogger(DataSelectionService.class); protected DataSelection dataSelection; protected RequestType requestType; - protected static ExecutorService threadPool; - static { - threadPool = Executors.newCachedThreadPool(); - } + protected static ExecutorService threadPool; + static { threadPool = Executors.newCachedThreadPool(); } private Map preparedStatusMap = new ConcurrentHashMap<>(); class PreparedStatus { - public ReentrantLock lock = new ReentrantLock(); public Long fromElement; public Future future; + } - protected DataSelectionService( - SortedMap dsInfos, - SortedMap dfInfos, - Set emptyDatasets, - List invids2, - List dsids, - List dfids, - long length, - Boolean zip, - Boolean compress, - RequestType requestType - ) { - this.dataSelection = - new DataSelection( - dsInfos, - dfInfos, - emptyDatasets, - invids2, - dsids, - dfids, - length, - zip, - compress - ); + protected DataSelectionService(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { + + this.dataSelection = new DataSelection(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress); this.requestType = requestType; } - public abstract boolean isPrepared(String preparedId) - throws InternalException; + public abstract boolean isPrepared(String preparedId) throws InternalException; /** * To get the DataInfos that is currently worked with, depending on StorageUnit @@ -90,14 +66,11 @@ public abstract boolean isPrepared(String preparedId) */ public abstract SortedMap getPrimaryDataInfos(); - public abstract boolean existsInMainStorage(DataInfoBase dataInfo) - throws InternalException; + public abstract boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException; - public abstract void queueDelete() - throws NotImplementedException, InternalException; + public abstract void queueDelete() throws NotImplementedException, InternalException; - public abstract void scheduleTasks(DeferredOp operation) - throws NotImplementedException, InternalException; + public abstract void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException; //TODO: maybe implementing this method here whould be the better way, than making it abstract. But we will miss the NotImplementedException for SingleLevelStorage in that case. // should we change the tests and the behavior of the ids server here @@ -111,6 +84,7 @@ public SortedMap getDsInfo() { return this.dataSelection.getDsInfo(); } + public SortedMap getDfInfo() { return this.dataSelection.getDfInfo(); } @@ -127,6 +101,7 @@ public long getLength() { return this.dataSelection.getLength(); } + public boolean mustZip() { return this.dataSelection.mustZip(); } @@ -135,10 +110,12 @@ public boolean isSingleDataset() { return this.dataSelection.isSingleDataset(); } + public Set getEmptyDatasets() { return this.dataSelection.getEmptyDatasets(); } + /** * tries to extract a list of ids from a comma separated id string. No spaces or leading 0's. Also * accepts null. @@ -147,8 +124,8 @@ public Set getEmptyDatasets() { * @return list of long numbers - the extracted id values * @throws BadRequestException */ - public static List getValidIds(String thing, String idList) - throws BadRequestException { + public static List getValidIds(String thing, String idList) throws BadRequestException { + List result; if (idList == null) { result = Collections.emptyList(); @@ -159,22 +136,16 @@ public static List getValidIds(String thing, String idList) try { result.add(Long.parseLong(id)); } catch (NumberFormatException e) { - throw new BadRequestException( - "The " + - thing + - " parameter '" + - idList + - "' is not a valid " + - "string representation of a comma separated list of longs" - ); + throw new BadRequestException("The " + thing + " parameter '" + idList + "' is not a valid " + + "string representation of a comma separated list of longs"); } } } return result; } - private boolean restoreIfOffline(DataInfoBase dataInfo) - throws InternalException { + + private boolean restoreIfOffline(DataInfoBase dataInfo) throws InternalException { boolean maybeOffline = false; var serviceProvider = ServiceProvider.getInstance(); if (serviceProvider.getFsm().getMaybeOffline().contains(dataInfo)) { @@ -186,7 +157,9 @@ private boolean restoreIfOffline(DataInfoBase dataInfo) return maybeOffline; } - public void checkOnline() throws InternalException, DataNotOnlineException { + + public void checkOnline()throws InternalException, DataNotOnlineException { + boolean maybeOffline = false; for (DataInfoBase dfInfo : this.getPrimaryDataInfos().values()) { if (this.restoreIfOffline(dfInfo)) { @@ -195,79 +168,55 @@ public void checkOnline() throws InternalException, DataNotOnlineException { } if (maybeOffline) { throw new DataNotOnlineException( - "Before getting, putting, etc. a datafile or dataset, it must be restored, restoration requested automatically" - ); + "Before getting, putting, etc. a datafile or dataset, it must be restored, restoration requested automatically"); } } + public void restoreDataInfos() { + var dataInfos = this.getPrimaryDataInfos().values(); - if (!dataInfos.isEmpty()) { + if(!dataInfos.isEmpty()) { for (DataInfoBase dataInfo : dataInfos) { - ServiceProvider - .getInstance() - .getFsm() - .recordSuccess(dataInfo.getId()); + ServiceProvider.getInstance().getFsm().recordSuccess(dataInfo.getId()); } threadPool.submit(new RestoreDataInfoTask(dataInfos, this, false)); } } + public OptionalLong getFileLength() { - if ( - this.dataSelection.getDfInfo().isEmpty() || - this.dataSelection.mustZip() - ) { + if (this.dataSelection.getDfInfo().isEmpty() || this.dataSelection.mustZip()) { return OptionalLong.empty(); } return OptionalLong.of(this.dataSelection.getLength()); } - protected boolean areDataInfosPrepared(String preparedId) - throws InternalException { + + protected boolean areDataInfosPrepared(String preparedId) throws InternalException { boolean prepared = true; var serviceProvider = ServiceProvider.getInstance(); - PreparedStatus status = preparedStatusMap.computeIfAbsent( - preparedId, - k -> new PreparedStatus() - ); - - Collection toCheck = status.fromElement == null - ? this.getPrimaryDataInfos().values() - : this.getPrimaryDataInfos().tailMap(status.fromElement).values(); + PreparedStatus status = preparedStatusMap.computeIfAbsent(preparedId, k -> new PreparedStatus()); + + Collection toCheck = status.fromElement == null ? this.getPrimaryDataInfos().values() + : this.getPrimaryDataInfos().tailMap(status.fromElement).values(); logger.debug("Will check online status of {} entries", toCheck.size()); for (DataInfoBase dataInfo : toCheck) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (this.restoreIfOffline(dataInfo)) { prepared = false; status.fromElement = dataInfo.getId(); - toCheck = - this.getPrimaryDataInfos() - .tailMap(status.fromElement) - .values(); - logger.debug( - "Will check in background status of {} entries", - toCheck.size() - ); - status.future = - threadPool.submit( - new RestoreDataInfoTask(toCheck, this, true) - ); + toCheck = this.getPrimaryDataInfos().tailMap(status.fromElement).values(); + logger.debug("Will check in background status of {} entries", toCheck.size()); + status.future = threadPool.submit(new RestoreDataInfoTask(toCheck, this, true)); break; } } if (prepared) { - toCheck = - status.fromElement == null - ? Collections.emptySet() - : this.getPrimaryDataInfos() - .headMap(status.fromElement) - .values(); - logger.debug( - "Will check finally online status of {} entries", - toCheck.size() - ); + toCheck = status.fromElement == null ? Collections.emptySet() + : this.getPrimaryDataInfos().headMap(status.fromElement).values(); + logger.debug("Will check finally online status of {} entries", toCheck.size()); for (DataInfoBase dataInfo : toCheck) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (this.restoreIfOffline(dataInfo)) { @@ -279,7 +228,9 @@ protected boolean areDataInfosPrepared(String preparedId) return prepared; } + public void delete() throws InternalException, NotImplementedException { + var serviceProvider = ServiceProvider.getInstance(); /* @@ -291,60 +242,33 @@ public void delete() throws InternalException, NotImplementedException { var dfInfo = (DatafileInfo) dataInfo; String location = dataInfo.getLocation(); try { - if ( - (long) serviceProvider - .getIcatReader() - .search( - "SELECT COUNT(df) FROM Datafile df WHERE df.location LIKE '" + - location.replaceAll("'", "''") + - "%'" - ) - .get(0) == - 0 - ) { + if ((long) serviceProvider.getIcatReader() + .search("SELECT COUNT(df) FROM Datafile df WHERE df.location LIKE '" + location.replaceAll("'", "''") + "%'") + .get(0) == 0) { if (serviceProvider.getMainStorage().exists(location)) { - logger.debug( - "Delete physical file " + - location + - " from main storage" - ); - serviceProvider - .getMainStorage() - .delete( - location, - dfInfo.getCreateId(), - dfInfo.getModId() - ); + logger.debug("Delete physical file " + location + " from main storage"); + serviceProvider.getMainStorage().delete(location, dfInfo.getCreateId(), dfInfo.getModId()); } } } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage() - ); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } catch (IOException e) { - logger.error( - "I/O error " + e.getMessage() + " deleting " + dfInfo - ); - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + logger.error("I/O error " + e.getMessage() + " deleting " + dfInfo); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } this.queueDelete(); } + private class RestoreDataInfoTask implements Callable { private Collection dataInfos; private DataSelectionService dataselection; private boolean checkFailure; - public RestoreDataInfoTask( - Collection dataInfos, - DataSelectionService dataSelection, - boolean checkFailure - ) { + public RestoreDataInfoTask(Collection dataInfos, DataSelectionService dataSelection, boolean checkFailure) { this.dataInfos = dataInfos; this.dataselection = dataSelection; this.checkFailure = checkFailure; @@ -353,13 +277,13 @@ public RestoreDataInfoTask( @Override public Void call() throws Exception { for (DataInfoBase dataInfo : dataInfos) { - if (checkFailure) ServiceProvider - .getInstance() - .getFsm() - .checkFailure(dataInfo.getId()); + if(checkFailure) + ServiceProvider.getInstance().getFsm().checkFailure(dataInfo.getId()); dataselection.restoreIfOffline(dataInfo); } return null; } + } + } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java index 7f52b5c1..25ff3ccb 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java @@ -1,8 +1,5 @@ package org.icatproject.ids.services.dataSelectionService; -import jakarta.json.Json; -import jakarta.json.JsonArray; -import jakarta.json.JsonValue; import java.io.ByteArrayInputStream; import java.util.ArrayList; import java.util.Collections; @@ -13,6 +10,7 @@ import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; + import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ICAT; @@ -28,8 +26,8 @@ import org.icatproject.ids.exceptions.NotFoundException; import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.helpers.LocationHelper; -import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatafileInfo; +import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.models.Prepared; import org.icatproject.ids.services.IcatReader; @@ -38,11 +36,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import jakarta.json.Json; +import jakarta.json.JsonArray; +import jakarta.json.JsonValue; + public class DataSelectionServiceFactory { - private static final Logger logger = LoggerFactory.getLogger( - DataSelectionServiceFactory.class - ); + private final static Logger logger = LoggerFactory.getLogger(DataSelectionServiceFactory.class); private static DataSelectionServiceFactory instance = null; @@ -54,53 +54,31 @@ public class DataSelectionServiceFactory { private HashMap requestTypeToReturnsMapping; public enum Returns { - DATASETS, - DATASETS_AND_DATAFILES, - DATAFILES, + DATASETS, DATASETS_AND_DATAFILES, DATAFILES } - public static DataSelectionServiceFactory getInstance() - throws InternalException { + public static DataSelectionServiceFactory getInstance() throws InternalException { if (instance == null) { var serviceProvider = ServiceProvider.getInstance(); - instance = - new DataSelectionServiceFactory( - serviceProvider.getPropertyHandler(), - serviceProvider.getIcatReader() - ); + instance = new DataSelectionServiceFactory(serviceProvider.getPropertyHandler(), serviceProvider.getIcatReader()); } return instance; } - public static DataSelectionServiceFactory getInstanceOnlyForTesting( - PropertyHandler propertyHandler, - IcatReader reader - ) throws InternalException { + public static DataSelectionServiceFactory getInstanceOnlyForTesting(PropertyHandler propertyHandler, IcatReader reader) throws InternalException { if (instance == null) { instance = new DataSelectionServiceFactory(propertyHandler, reader); } return instance; } - public static DataSelectionService getService( - String userSessionId, - String investigationIds, - String datasetIds, - String datafileIds, - RequestType requestType - ) - throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - return DataSelectionServiceFactory - .getInstance() - .getSelectionService( - userSessionId, - investigationIds, - datasetIds, - datafileIds, - requestType - ); + public static DataSelectionService getService(String userSessionId, String investigationIds, String datasetIds, String datafileIds, RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + + return DataSelectionServiceFactory.getInstance().getSelectionService(userSessionId, investigationIds, datasetIds, datafileIds, requestType); } + /** * created a DataSelectionBase object from already created data infos. That's why no infos must be loaded from ICAT. * @param dsInfos @@ -111,61 +89,33 @@ public static DataSelectionService getService( * @return * @throws InternalException */ - public static DataSelectionService getService( - SortedMap dsInfos, - SortedMap dfInfos, - Set emptyDatasets, - long fileLength, - RequestType requestType - ) throws InternalException { - var prepared = new Prepared( - dsInfos, - dfInfos, - emptyDatasets, - fileLength - ); + public static DataSelectionService getService(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength, RequestType requestType) throws InternalException { + + var prepared = new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength); return DataSelectionServiceFactory.getService(prepared, requestType); } - public static DataSelectionService getService( - Prepared prepared, - RequestType requestType - ) throws InternalException { + public static DataSelectionService getService(Prepared prepared, RequestType requestType) throws InternalException { List dsids = new ArrayList(prepared.dsInfos.keySet()); List dfids = new ArrayList(); List invIds = new ArrayList(); var dataFileInfos = new HashMap(); - for (DataInfoBase dfInfo : prepared.dfInfos.values()) { + for(DataInfoBase dfInfo: prepared.dfInfos.values()) { dfids.add(dfInfo.getId()); dataFileInfos.put(dfInfo.getId(), dfInfo); dfids.add(dfInfo.getId()); } - for (DataInfoBase dsInfo : prepared.dsInfos.values()) { + for(DataInfoBase dsInfo : prepared.dsInfos.values()) { dsids.add(dsInfo.getId()); - invIds.add(((DatasetInfo) dsInfo).getInvId()); + invIds.add( ((DatasetInfo)dsInfo).getInvId() ); } - return DataSelectionServiceFactory - .getInstance() - .createSelectionService( - prepared.dsInfos, - prepared.dfInfos, - prepared.emptyDatasets, - invIds, - dsids, - dfids, - prepared.fileLength, - prepared.zip, - prepared.compress, - requestType - ); + return DataSelectionServiceFactory.getInstance().createSelectionService(prepared.dsInfos, prepared.dfInfos, prepared.emptyDatasets, invIds, dsids, dfids, prepared.fileLength, prepared.zip, prepared.compress, requestType); } - private DataSelectionServiceFactory( - PropertyHandler propertyHandler, - IcatReader reader - ) throws InternalException { + private DataSelectionServiceFactory(PropertyHandler propertyHandler, IcatReader reader) throws InternalException + { logger.info("### Constructing..."); this.propertyHandler = propertyHandler; this.icat = propertyHandler.getIcatService(); @@ -178,100 +128,47 @@ private DataSelectionServiceFactory( logger.info("### Constructing finished"); } - private DataSelectionService createSelectionService( - SortedMap dsInfos, - SortedMap dfInfos, - Set emptyDatasets, - List invids, - List dsids, - List dfids, - long length, - Boolean zip, - Boolean compress, - RequestType requestType - ) throws InternalException { + private DataSelectionService createSelectionService(SortedMap dsInfos, SortedMap dfInfos, + Set emptyDatasets, List invids, List dsids, + List dfids, long length, Boolean zip, Boolean compress, + RequestType requestType) throws InternalException { + StorageUnit storageUnit = this.propertyHandler.getStorageUnit(); - if ( - storageUnit == null - ) return new DataSelectionServiceForSingleLevelStorage( - dsInfos, - dfInfos, - emptyDatasets, - invids, - dsids, - dfids, - length, - zip, - compress, - requestType - ); else if ( - storageUnit == StorageUnit.DATAFILE - ) return new DataSelectionServiceForStorageUnitDatafile( - dsInfos, - dfInfos, - emptyDatasets, - invids, - dsids, - dfids, - length, - zip, - compress, - requestType - ); else if ( - storageUnit == StorageUnit.DATASET - ) return new DataSelectionServiceForStorageUnitDataset( - dsInfos, - dfInfos, - emptyDatasets, - invids, - dsids, - dfids, - length, - zip, - compress, - requestType - ); else throw new InternalException( - "StorageUnit " + - storageUnit + - " unknown. Maybe you forgot to handle a new StorageUnit here?" - ); + if(storageUnit == null ) + return new DataSelectionServiceForSingleLevelStorage(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); + + else if (storageUnit == StorageUnit.DATAFILE) + return new DataSelectionServiceForStorageUnitDatafile(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); + + else if(storageUnit == StorageUnit.DATASET) + return new DataSelectionServiceForStorageUnitDataset(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); + + else throw new InternalException("StorageUnit " + storageUnit + " unknown. Maybe you forgot to handle a new StorageUnit here?"); + } - public DataSelectionService getSelectionService( - String userSessionId, - String investigationIds, - String datasetIds, - String datafileIds, - RequestType requestType - ) - throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + public DataSelectionService getSelectionService( String userSessionId, String investigationIds, String datasetIds, String datafileIds, RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + List dfids = getValidIds("datafileIds", datafileIds); List dsids = getValidIds("datasetIds", datasetIds); List invids = getValidIds("investigationIds", investigationIds); Returns returns = this.getReturns(requestType); - boolean dfWanted = - returns == Returns.DATASETS_AND_DATAFILES || - returns == Returns.DATAFILES; - boolean dsWanted = - returns == Returns.DATASETS_AND_DATAFILES || - returns == Returns.DATASETS; + boolean dfWanted = returns == Returns.DATASETS_AND_DATAFILES || returns == Returns.DATAFILES; + boolean dsWanted = returns == Returns.DATASETS_AND_DATAFILES || returns == Returns.DATASETS; Session userRestSession = restIcat.getSession(userSessionId); // by default use the user's REST ICAT session Session restSessionToUse = userRestSession; - + try { - logger.debug( - "useReaderForPerformance = {}", - propertyHandler.getUseReaderForPerformance() - ); + logger.debug("useReaderForPerformance = {}", propertyHandler.getUseReaderForPerformance()); if (propertyHandler.getUseReaderForPerformance()) { // if this is set, use a REST session for the reader account where possible // to improve performance due to the final database queries being simpler - restSessionToUse = - restIcat.getSession(this.icatReader.getSessionId()); + restSessionToUse = restIcat.getSession(this.icatReader.getSessionId()); } } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -279,31 +176,13 @@ public DataSelectionService getSelectionService( logger.debug("dfids: {} dsids: {} invids: {}", dfids, dsids, invids); - return prepareFromIds( - dfWanted, - dsWanted, - dfids, - dsids, - invids, - userSessionId, - restSessionToUse, - userRestSession, - requestType - ); + return prepareFromIds(dfWanted, dsWanted, dfids, dsids, invids, userSessionId, restSessionToUse, userRestSession, requestType); } - private DataSelectionService prepareFromIds( - boolean dfWanted, - boolean dsWanted, - List dfids, - List dsids, - List invids, - String userSessionId, - Session restSessionToUse, - Session userRestSession, - RequestType requestType - ) - throws NotFoundException, InsufficientPrivilegesException, InternalException, BadRequestException { + + + private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, List dfids, List dsids, List invids, String userSessionId, Session restSessionToUse, Session userRestSession, RequestType requestType) + throws NotFoundException, InsufficientPrivilegesException, InternalException, BadRequestException { var dsInfos = new TreeMap(); var emptyDatasets = new HashSet(); var dfInfos = new TreeMap(); @@ -315,39 +194,21 @@ private DataSelectionService prepareFromIds( } try { + for (Long dfid : dfids) { - List dss = icat.search( - userSessionId, - "SELECT ds FROM Dataset ds JOIN ds.datafiles df WHERE df.id = " + - dfid + - " AND df.location IS NOT NULL INCLUDE ds.investigation.facility" - ); + List dss = icat.search(userSessionId, + "SELECT ds FROM Dataset ds JOIN ds.datafiles df WHERE df.id = " + dfid + + " AND df.location IS NOT NULL INCLUDE ds.investigation.facility"); if (dss.size() == 1) { Dataset ds = (Dataset) dss.get(0); long dsid = ds.getId(); dsInfos.put(dsid, new DatasetInfo(ds)); if (dfWanted) { - Datafile df = (Datafile) icat.get( - userSessionId, - "Datafile", - dfid - ); + Datafile df = (Datafile) icat.get(userSessionId, "Datafile", dfid); length += df.getFileSize(); - String location = LocationHelper.getLocation( - dfid, - df.getLocation() - ); - dfInfos.put( - df.getId(), - new DatafileInfo( - dfid, - df.getName(), - location, - df.getCreateId(), - df.getModId(), - dsid - ) - ); + String location = LocationHelper.getLocation(dfid, df.getLocation()); + dfInfos.put( df.getId(), + new DatafileInfo(dfid, df.getName(), location, df.getCreateId(), df.getModId(), dsid)); } } else { // Next line may reveal a permissions problem @@ -357,26 +218,14 @@ private DataSelectionService prepareFromIds( } for (Long dsid : dsids) { - Dataset ds = (Dataset) icat.get( - userSessionId, - "Dataset ds INCLUDE ds.investigation.facility", - dsid - ); + Dataset ds = (Dataset) icat.get(userSessionId, "Dataset ds INCLUDE ds.investigation.facility", dsid); dsInfos.put(dsid, new DatasetInfo(ds)); // dataset access for the user has been checked so the REST session for the // reader account can be used if the IDS setting to allow this is enabled - String query = - "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + - dsid + - " AND df.location IS NOT NULL"; - JsonArray result = Json - .createReader( - new ByteArrayInputStream( - restSessionToUse.search(query).getBytes() - ) - ) - .readArray() - .getJsonArray(0); + String query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + + dsid + " AND df.location IS NOT NULL"; + JsonArray result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())) + .readArray().getJsonArray(0); if (result.getJsonNumber(2).longValueExact() == 0) { // Count 0 emptyDatasets.add(dsid); } else if (dfWanted) { @@ -385,60 +234,33 @@ private DataSelectionService prepareFromIds( } for (Long invid : invids) { - String query = - "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + - invid; - JsonArray result = Json - .createReader( - new ByteArrayInputStream( - userRestSession.search(query).getBytes() - ) - ) - .readArray() - .getJsonArray(0); - manyDss( - dsInfos, - emptyDatasets, - dfInfos, - invid, - dfWanted, - userRestSession, - restSessionToUse, - result - ); + String query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + + invid; + JsonArray result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())) + .readArray().getJsonArray(0); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUse, result); + } + } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if ( - type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || - type == IcatExceptionType.SESSION - ) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } else if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } else { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + throw new InternalException(e.getClass() + " " + e.getMessage()); } + } catch (IcatException e) { org.icatproject.icat.client.IcatException.IcatExceptionType type = e.getType(); - if ( - type == - org.icatproject.icat.client.IcatException.IcatExceptionType.INSUFFICIENT_PRIVILEGES || - type == - org.icatproject.icat.client.IcatException.IcatExceptionType.SESSION - ) { + if (type == org.icatproject.icat.client.IcatException.IcatExceptionType.INSUFFICIENT_PRIVILEGES + || type == org.icatproject.icat.client.IcatException.IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); - } else if ( - type == - org.icatproject.icat.client.IcatException.IcatExceptionType.NO_SUCH_OBJECT_FOUND - ) { + } else if (type == org.icatproject.icat.client.IcatException.IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } else { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } /* @@ -450,27 +272,17 @@ private DataSelectionService prepareFromIds( emptyDatasets = null; } - return this.createSelectionService( - dsInfos, - dfInfos, - emptyDatasets, - invids, - dsids, - dfids, - length, - false, - false, - requestType - ); + return this.createSelectionService(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, false, false, requestType); } + /** * Checks to see if the investigation, dataset or datafile id list is a * valid comma separated list of longs. No spaces or leading 0's. Also * accepts null. */ - public static List getValidIds(String thing, String idList) - throws BadRequestException { + public static List getValidIds(String thing, String idList) throws BadRequestException { + List result; if (idList == null) { result = Collections.emptyList(); @@ -481,26 +293,16 @@ public static List getValidIds(String thing, String idList) try { result.add(Long.parseLong(id)); } catch (NumberFormatException e) { - throw new BadRequestException( - "The " + - thing + - " parameter '" + - idList + - "' is not a valid " + - "string representation of a comma separated list of longs" - ); + throw new BadRequestException("The " + thing + " parameter '" + idList + "' is not a valid " + + "string representation of a comma separated list of longs"); } } } return result; } - private void manyDfs( - Map dfInfos, - long dsid, - Session restSessionToUse, - JsonArray result - ) throws IcatException, InsufficientPrivilegesException, InternalException { + private void manyDfs(Map dfInfos, long dsid, Session restSessionToUse, JsonArray result) + throws IcatException, InsufficientPrivilegesException, InternalException { // dataset access for the user has been checked so the REST session for the // reader account can be used if the IDS setting to allow this is enabled long min = result.getJsonNumber(0).longValueExact(); @@ -509,107 +311,43 @@ private void manyDfs( logger.debug("manyDfs min: {} max: {} count: {}", min, max, count); if (count != 0) { if (count <= maxEntities) { - String query = - "SELECT df.id, df.name, df.location, df.createId, df.modId FROM Datafile df WHERE df.dataset.id = " + - dsid + - " AND df.location IS NOT NULL AND df.id BETWEEN " + - min + - " AND " + - max; - result = - Json - .createReader( - new ByteArrayInputStream( - restSessionToUse.search(query).getBytes() - ) - ) - .readArray(); + String query = "SELECT df.id, df.name, df.location, df.createId, df.modId FROM Datafile df WHERE df.dataset.id = " + + dsid + " AND df.location IS NOT NULL AND df.id BETWEEN " + min + " AND " + max; + result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray(); for (JsonValue tupV : result) { JsonArray tup = (JsonArray) tupV; long dfid = tup.getJsonNumber(0).longValueExact(); - String location = LocationHelper.getLocation( - dfid, - tup.getString(2, null) - ); - dfInfos.put( - dfid, - new DatafileInfo( - dfid, - tup.getString(1), - location, - tup.getString(3), - tup.getString(4), - dsid - ) - ); + String location = LocationHelper.getLocation(dfid, tup.getString(2, null)); + dfInfos.put(dfid, + new DatafileInfo(dfid, tup.getString(1), location, tup.getString(3), tup.getString(4), dsid)); } } else { long half = (min + max) / 2; - String query = - "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + - dsid + - " AND df.location IS NOT NULL AND df.id BETWEEN " + - min + - " AND " + - half; - result = - Json - .createReader( - new ByteArrayInputStream( - restSessionToUse.search(query).getBytes() - ) - ) - .readArray() + String query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + + dsid + " AND df.location IS NOT NULL AND df.id BETWEEN " + min + " AND " + half; + result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray() .getJsonArray(0); manyDfs(dfInfos, dsid, restSessionToUse, result); - query = - "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + - dsid + - " AND df.location IS NOT NULL AND df.id BETWEEN " + - (half + 1) + - " AND " + - max; - result = - Json - .createReader( - new ByteArrayInputStream( - restSessionToUse.search(query).getBytes() - ) - ) - .readArray() + query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid + + " AND df.location IS NOT NULL AND df.id BETWEEN " + (half + 1) + " AND " + max; + result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray() .getJsonArray(0); manyDfs(dfInfos, dsid, restSessionToUse, result); } } } - private void manyDss( - Map dsInfos, - HashSet emptyDatasets, - Map dfInfos, - Long invid, - boolean dfWanted, - Session userRestSession, - Session restSessionToUseForDfs, - JsonArray result - ) throws IcatException, InsufficientPrivilegesException, InternalException { + private void manyDss(Map dsInfos, HashSet emptyDatasets, Map dfInfos, Long invid, boolean dfWanted, Session userRestSession, Session restSessionToUseForDfs, JsonArray result) + throws IcatException, InsufficientPrivilegesException, InternalException { long min = result.getJsonNumber(0).longValueExact(); long max = result.getJsonNumber(1).longValueExact(); long count = result.getJsonNumber(2).longValueExact(); logger.debug("manyDss min: {} max: {} count: {}", min, max, count); if (count != 0) { if (count <= maxEntities) { - String query = - "SELECT inv.name, inv.visitId, inv.facility.id, inv.facility.name FROM Investigation inv WHERE inv.id = " + - invid; - result = - Json - .createReader( - new ByteArrayInputStream( - userRestSession.search(query).getBytes() - ) - ) - .readArray(); + String query = "SELECT inv.name, inv.visitId, inv.facility.id, inv.facility.name FROM Investigation inv WHERE inv.id = " + + invid; + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); if (result.size() == 0) { return; } @@ -619,201 +357,83 @@ private void manyDss( long facilityId = result.getJsonNumber(2).longValueExact(); String facilityName = result.getString(3); - query = - "SELECT ds.id, ds.name, ds.location FROM Dataset ds WHERE ds.investigation.id = " + - invid + - " AND ds.id BETWEEN " + - min + - " AND " + - max; - result = - Json - .createReader( - new ByteArrayInputStream( - userRestSession.search(query).getBytes() - ) - ) - .readArray(); + query = "SELECT ds.id, ds.name, ds.location FROM Dataset ds WHERE ds.investigation.id = " + invid + + " AND ds.id BETWEEN " + min + " AND " + max; + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); for (JsonValue tupV : result) { JsonArray tup = (JsonArray) tupV; long dsid = tup.getJsonNumber(0).longValueExact(); - dsInfos.put( - dsid, - new DatasetInfo( - dsid, - tup.getString(1), - tup.getString(2, null), - invid, - invName, - visitId, - facilityId, - facilityName - ) - ); - - query = - "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + - dsid + - " AND df.location IS NOT NULL"; - result = - Json - .createReader( - new ByteArrayInputStream( - userRestSession.search(query).getBytes() - ) - ) - .readArray() - .getJsonArray(0); + dsInfos.put(dsid, new DatasetInfo(dsid, tup.getString(1), tup.getString(2, null), invid, invName, + visitId, facilityId, facilityName)); + + query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + + dsid + " AND df.location IS NOT NULL"; + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())) + .readArray().getJsonArray(0); if (result.getJsonNumber(2).longValueExact() == 0) { emptyDatasets.add(dsid); } else if (dfWanted) { manyDfs(dfInfos, dsid, restSessionToUseForDfs, result); } + } } else { long half = (min + max) / 2; - String query = - "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + - invid + - " AND ds.id BETWEEN " + - min + - " AND " + - half; - result = - Json - .createReader( - new ByteArrayInputStream( - userRestSession.search(query).getBytes() - ) - ) - .readArray(); - manyDss( - dsInfos, - emptyDatasets, - dfInfos, - invid, - dfWanted, - userRestSession, - restSessionToUseForDfs, - result - ); - query = - "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + - invid + - " AND ds.id BETWEEN " + - half + - 1 + - " AND " + - max; - result = - Json - .createReader( - new ByteArrayInputStream( - userRestSession.search(query).getBytes() - ) - ) - .readArray() + String query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + + invid + " AND ds.id BETWEEN " + min + " AND " + half; + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUseForDfs, result); + query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + + invid + " AND ds.id BETWEEN " + half + 1 + " AND " + max; + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray() .getJsonArray(0); - manyDss( - dsInfos, - emptyDatasets, - dfInfos, - invid, - dfWanted, - userRestSession, - restSessionToUseForDfs, - result - ); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUseForDfs, result); } } + } private void createRequestTypeToReturnsMapping() throws InternalException { + this.requestTypeToReturnsMapping = new HashMap(); StorageUnit storageUnit = this.propertyHandler.getStorageUnit(); - this.requestTypeToReturnsMapping.put( - RequestType.DELETE, - Returns.DATASETS_AND_DATAFILES - ); - this.requestTypeToReturnsMapping.put( - RequestType.GETDATAFILEIDS, - Returns.DATAFILES - ); - this.requestTypeToReturnsMapping.put( - RequestType.GETSIZE, - Returns.DATASETS_AND_DATAFILES - ); - this.requestTypeToReturnsMapping.put( - RequestType.PREPAREDATA, - Returns.DATASETS_AND_DATAFILES - ); - this.requestTypeToReturnsMapping.put( - RequestType.RESET, - Returns.DATASETS_AND_DATAFILES - ); - this.requestTypeToReturnsMapping.put( - RequestType.WRITE, - Returns.DATASETS_AND_DATAFILES - ); - this.requestTypeToReturnsMapping.put( - RequestType.GETDATA, - Returns.DATASETS_AND_DATAFILES - ); - - if (storageUnit == null) { - this.requestTypeToReturnsMapping.put( - RequestType.GETSTATUS, - Returns.DATASETS - ); - } else if (storageUnit == StorageUnit.DATAFILE) { - this.requestTypeToReturnsMapping.put( - RequestType.GETSTATUS, - Returns.DATAFILES - ); - this.requestTypeToReturnsMapping.put( - RequestType.RESTORE, - Returns.DATAFILES - ); - this.requestTypeToReturnsMapping.put( - RequestType.ARCHIVE, - Returns.DATAFILES - ); - } else if (storageUnit == StorageUnit.DATASET) { - this.requestTypeToReturnsMapping.put( - RequestType.GETSTATUS, - Returns.DATASETS - ); - this.requestTypeToReturnsMapping.put( - RequestType.RESTORE, - Returns.DATASETS - ); - this.requestTypeToReturnsMapping.put( - RequestType.ARCHIVE, - Returns.DATASETS - ); - } else throw new InternalException( - "StorageUnit " + - storageUnit + - " unknown. Maybe you forgot to handle a new StorageUnit here?" - ); + this.requestTypeToReturnsMapping.put(RequestType.DELETE, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETDATAFILEIDS, Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETSIZE, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.PREPAREDATA, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.RESET, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.WRITE, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETDATA, Returns.DATASETS_AND_DATAFILES); + + if(storageUnit == null ) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATASETS); + } + else if (storageUnit == StorageUnit.DATAFILE) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.RESTORE, Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, Returns.DATAFILES); + } + else if(storageUnit == StorageUnit.DATASET) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATASETS); + this.requestTypeToReturnsMapping.put(RequestType.RESTORE, Returns.DATASETS); + this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, Returns.DATASETS); + } + + + else throw new InternalException("StorageUnit " + storageUnit + " unknown. Maybe you forgot to handle a new StorageUnit here?"); } - private Returns getReturns(RequestType requestType) - throws NotImplementedException { - if ( - this.requestTypeToReturnsMapping.containsKey(requestType) - ) return this.requestTypeToReturnsMapping.get(requestType); + private Returns getReturns(RequestType requestType) throws NotImplementedException { + if(this.requestTypeToReturnsMapping.containsKey(requestType)) + return this.requestTypeToReturnsMapping.get(requestType); + + // is this needed here? //if(this.propertyHandler.getStorageUnit() == null) throw new NotImplementedException("This operation is unavailable for single level storage"); - throw new NotImplementedException( - "There is to mapping for RequestType." + - requestType + - " and StorageUnit." + - this.propertyHandler.getStorageUnit() + - " defined. Did you forgot to register it in createRequestTypeToReturnsMapping()?" - ); + throw new NotImplementedException("There is to mapping for RequestType." + requestType + " and StorageUnit." + this.propertyHandler.getStorageUnit() + " defined. Did you forgot to register it in createRequestTypeToReturnsMapping()?"); } + } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java index 6e92dcd8..b7915daa 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java @@ -4,70 +4,54 @@ import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; + import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.models.DataInfoBase; -public class DataSelectionServiceForSingleLevelStorage - extends DataSelectionService { - - protected DataSelectionServiceForSingleLevelStorage( - SortedMap dsInfos, - SortedMap dfInfos, - Set emptyDatasets, - List invids2, - List dsids, - List dfids, - long length, - Boolean zip, - Boolean compress, - RequestType requestType - ) { - super( - dsInfos, - dfInfos, - emptyDatasets, - invids2, - dsids, - dfids, - length, - zip, - compress, - requestType - ); +public class DataSelectionServiceForSingleLevelStorage extends DataSelectionService { + + protected DataSelectionServiceForSingleLevelStorage(SortedMap dsInfos, SortedMap dfInfos, + Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { + + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); } + @Override public SortedMap getPrimaryDataInfos() { return new TreeMap(); } + @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) - throws InternalException { - throw new InternalException( - "This operation is unavailable for single level storage" - ); + public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { + + throw new InternalException("This operation is unavailable for single level storage"); } + @Override public boolean isPrepared(String preparedId) throws InternalException { return true; } + @Override - public void queueDelete() - throws NotImplementedException, InternalException { + public void queueDelete() throws NotImplementedException, InternalException { //nothing todo for single level storage } + @Override - public void scheduleTasks(DeferredOp operation) - throws NotImplementedException, InternalException { - throw new NotImplementedException( - "This operation is unavailable for single level storage" - ); + public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { + + throw new NotImplementedException("This operation is unavailable for single level storage"); } + + + + } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java index 4afce3b6..f744fa8f 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java @@ -3,6 +3,7 @@ import java.util.List; import java.util.Set; import java.util.SortedMap; + import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.InternalException; @@ -10,65 +11,44 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.ServiceProvider; -public class DataSelectionServiceForStorageUnitDatafile - extends DataSelectionService { +public class DataSelectionServiceForStorageUnitDatafile extends DataSelectionService { + + protected DataSelectionServiceForStorageUnitDatafile(SortedMap dsInfos, SortedMap dfInfos, + Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { - protected DataSelectionServiceForStorageUnitDatafile( - SortedMap dsInfos, - SortedMap dfInfos, - Set emptyDatasets, - List invids2, - List dsids, - List dfids, - long length, - Boolean zip, - Boolean compress, - RequestType requestType - ) { - super( - dsInfos, - dfInfos, - emptyDatasets, - invids2, - dsids, - dfids, - length, - zip, - compress, - requestType - ); + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); } + @Override public SortedMap getPrimaryDataInfos() { return this.dataSelection.getDfInfo(); } @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) - throws InternalException { - return ServiceProvider - .getInstance() - .getMainStorage() - .exists(dataInfo.getLocation()); + public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { + return ServiceProvider.getInstance().getMainStorage().exists(dataInfo.getLocation()); } + @Override public boolean isPrepared(String preparedId) throws InternalException { return areDataInfosPrepared(preparedId); } + @Override - public void queueDelete() - throws NotImplementedException, InternalException { + public void queueDelete() throws NotImplementedException, InternalException { this.scheduleTasks(DeferredOp.DELETE); } + @Override - public void scheduleTasks(DeferredOp operation) - throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { + for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); } } + } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java index f2066e36..ff1b5c65 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java @@ -3,6 +3,7 @@ import java.util.List; import java.util.Set; import java.util.SortedMap; + import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.InternalException; @@ -11,70 +12,49 @@ import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.services.ServiceProvider; -public class DataSelectionServiceForStorageUnitDataset - extends DataSelectionService { - - protected DataSelectionServiceForStorageUnitDataset( - SortedMap dsInfos, - SortedMap dfInfos, - Set emptyDatasets, - List invids2, - List dsids, - List dfids, - long length, - Boolean zip, - Boolean compress, - RequestType requestType - ) { - super( - dsInfos, - dfInfos, - emptyDatasets, - invids2, - dsids, - dfids, - length, - zip, - compress, - requestType - ); +public class DataSelectionServiceForStorageUnitDataset extends DataSelectionService { + + protected DataSelectionServiceForStorageUnitDataset(SortedMap dsInfos, SortedMap dfInfos, + Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { + + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); } + @Override public SortedMap getPrimaryDataInfos() { return this.dataSelection.getDsInfo(); } @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) - throws InternalException { + public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { + var dsInfo = (DatasetInfo) dataInfo; - if (dsInfo == null) throw new InternalException( - "Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?" - ); - - return ( - this.dataSelection.getEmptyDatasets().contains(dataInfo.getId()) || - ServiceProvider.getInstance().getMainStorage().exists(dsInfo) - ); + if(dsInfo == null) throw new InternalException("Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); + + return this.dataSelection.getEmptyDatasets().contains(dataInfo.getId()) || ServiceProvider.getInstance().getMainStorage().exists(dsInfo); } + @Override public boolean isPrepared(String preparedId) throws InternalException { return this.areDataInfosPrepared(preparedId); } + @Override - public void queueDelete() - throws NotImplementedException, InternalException { + public void queueDelete() throws NotImplementedException, InternalException { this.scheduleTasks(DeferredOp.WRITE); } + @Override - public void scheduleTasks(DeferredOp operation) - throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { + for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); } } + + } diff --git a/src/main/java/org/icatproject/ids/thread/DfArchiver.java b/src/main/java/org/icatproject/ids/thread/DfArchiver.java index 5f8c5963..15df979f 100644 --- a/src/main/java/org/icatproject/ids/thread/DfArchiver.java +++ b/src/main/java/org/icatproject/ids/thread/DfArchiver.java @@ -4,22 +4,20 @@ import java.nio.file.Path; import java.util.Collection; import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.icatproject.ids.services.LockManager.Lock; /* * Removes datafiles from the fast storage (doesn't write them to archive storage) */ public class DfArchiver implements Runnable { - - private static final Logger logger = LoggerFactory.getLogger( - DfArchiver.class - ); + private final static Logger logger = LoggerFactory.getLogger(DfArchiver.class); private MainStorageInterface mainStorageInterface; private FiniteStateMachine fsm; @@ -27,12 +25,7 @@ public class DfArchiver implements Runnable { private Path markerDir; private Collection locks; - public DfArchiver( - List dfInfos, - PropertyHandler propertyHandler, - FiniteStateMachine fsm, - Collection locks - ) { + public DfArchiver(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { this.dfInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -45,34 +38,16 @@ public void run() { try { for (DatafileInfo dfInfo : dfInfos) { try { - if ( - Files.exists( - markerDir.resolve(Long.toString(dfInfo.getDfId())) - ) - ) { - logger.error( - "Archive of " + - dfInfo + - " not carried out because a write to secondary storage operation failed previously" - ); + if (Files.exists(markerDir.resolve(Long.toString(dfInfo.getDfId())))) { + logger.error("Archive of " + dfInfo + + " not carried out because a write to secondary storage operation failed previously"); } else { String dfLocation = dfInfo.getDfLocation(); - mainStorageInterface.delete( - dfLocation, - dfInfo.getCreateId(), - dfInfo.getModId() - ); + mainStorageInterface.delete(dfLocation, dfInfo.getCreateId(), dfInfo.getModId()); logger.debug("Archive of " + dfInfo + " completed"); } } catch (Exception e) { - logger.error( - "Archive of " + - dfInfo + - " failed due to " + - e.getClass() + - " " + - e.getMessage() - ); + logger.error("Archive of " + dfInfo + " failed due to " + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dfInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DfDeleter.java b/src/main/java/org/icatproject/ids/thread/DfDeleter.java index d69a4306..e0b4dab6 100644 --- a/src/main/java/org/icatproject/ids/thread/DfDeleter.java +++ b/src/main/java/org/icatproject/ids/thread/DfDeleter.java @@ -2,34 +2,28 @@ import java.util.Collection; import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.ArchiveStorageInterface; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.icatproject.ids.services.LockManager.Lock; /** * Delete datafiles from archive */ public class DfDeleter implements Runnable { - private static final Logger logger = LoggerFactory.getLogger( - DfDeleter.class - ); + private final static Logger logger = LoggerFactory.getLogger(DfDeleter.class); private FiniteStateMachine fsm; private ArchiveStorageInterface archiveStorageInterface; private List dfInfos; private Collection locks; - public DfDeleter( - List dfInfos, - PropertyHandler propertyHandler, - FiniteStateMachine fsm, - Collection locks - ) { + public DfDeleter(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { this.dfInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -45,14 +39,7 @@ public void run() { archiveStorageInterface.delete(dfLocation); logger.debug("Delete of " + dfInfo + " completed"); } catch (Exception e) { - logger.error( - "Delete of " + - dfInfo + - " failed due to " + - e.getClass() + - " " + - e.getMessage() - ); + logger.error("Delete of " + dfInfo + " failed due to " + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dfInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DfRestorer.java b/src/main/java/org/icatproject/ids/thread/DfRestorer.java index a8795ed2..a97327b5 100644 --- a/src/main/java/org/icatproject/ids/thread/DfRestorer.java +++ b/src/main/java/org/icatproject/ids/thread/DfRestorer.java @@ -5,24 +5,23 @@ import java.util.Iterator; import java.util.List; import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.ArchiveStorageInterface; import org.icatproject.ids.plugin.DfInfo; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.icatproject.ids.services.LockManager.Lock; /* * Restores datafiles from the slow to the fast storage. */ public class DfRestorer implements Runnable { - private static final Logger logger = LoggerFactory.getLogger( - DfRestorer.class - ); + private final static Logger logger = LoggerFactory.getLogger(DfRestorer.class); private MainStorageInterface mainStorageInterface; private ArchiveStorageInterface archiveStorageInterface; @@ -30,18 +29,14 @@ public class DfRestorer implements Runnable { private List dataFileInfos; private Collection locks; - public DfRestorer( - List dfInfos, - PropertyHandler propertyHandler, - FiniteStateMachine fsm, - Collection locks - ) { + public DfRestorer(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { this.dataFileInfos = dfInfos; this.fsm = fsm; this.locks = locks; mainStorageInterface = propertyHandler.getMainStorage(); archiveStorageInterface = propertyHandler.getArchiveStorage(); + } /* @@ -68,14 +63,12 @@ public void run() { //TODO: This is additional conversion caused by the redesign :-( List dfInfos = new ArrayList<>(); - for (DfInfo dfInfo : this.dataFileInfos) { + for(DfInfo dfInfo : this.dataFileInfos) { dfInfos.add(dfInfo); } - Set failures = archiveStorageInterface.restore( - mainStorageInterface, - dfInfos - ); + + Set failures = archiveStorageInterface.restore(mainStorageInterface, dfInfos); for (DatafileInfo dfInfo : dataFileInfos) { if (failures.contains(dfInfo)) { fsm.recordFailure(dfInfo.getDfId()); @@ -88,14 +81,7 @@ public void run() { } } catch (Exception e) { for (DatafileInfo dfInfo : dataFileInfos) { - logger.error( - "Restore of " + - dfInfo + - " failed " + - e.getClass() + - " " + - e.getMessage() - ); + logger.error("Restore of " + dfInfo + " failed " + e.getClass() + " " + e.getMessage()); fsm.removeFromChanging(dfInfo); } } finally { diff --git a/src/main/java/org/icatproject/ids/thread/DfWriter.java b/src/main/java/org/icatproject/ids/thread/DfWriter.java index b02ef46e..09881acc 100644 --- a/src/main/java/org/icatproject/ids/thread/DfWriter.java +++ b/src/main/java/org/icatproject/ids/thread/DfWriter.java @@ -5,23 +5,22 @@ import java.nio.file.Path; import java.util.Collection; import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.ArchiveStorageInterface; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.icatproject.ids.services.LockManager.Lock; /** * Copies datafiles from main to archive */ public class DfWriter implements Runnable { - private static final Logger logger = LoggerFactory.getLogger( - DfWriter.class - ); + private final static Logger logger = LoggerFactory.getLogger(DfWriter.class); private FiniteStateMachine fsm; private MainStorageInterface mainStorageInterface; @@ -30,12 +29,7 @@ public class DfWriter implements Runnable { private List dataFileInfos; private Collection locks; - public DfWriter( - List dfInfos, - PropertyHandler propertyHandler, - FiniteStateMachine fsm, - Collection locks - ) { + public DfWriter(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { this.dataFileInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -49,29 +43,14 @@ public void run() { try { for (DatafileInfo dataFileInfo : dataFileInfos) { String dfLocation = dataFileInfo.getDfLocation(); - try ( - InputStream is = mainStorageInterface.get( - dfLocation, - dataFileInfo.getCreateId(), - dataFileInfo.getModId() - ) - ) { + try (InputStream is = mainStorageInterface.get(dfLocation, dataFileInfo.getCreateId(), dataFileInfo.getModId())) { archiveStorageInterface.put(is, dfLocation); - Path marker = markerDir.resolve( - Long.toString(dataFileInfo.getDfId()) - ); + Path marker = markerDir.resolve(Long.toString(dataFileInfo.getDfId())); Files.deleteIfExists(marker); logger.debug("Removed marker " + marker); logger.debug("Write of " + dataFileInfo + " completed"); } catch (Exception e) { - logger.error( - "Write of " + - dataFileInfo + - " failed due to " + - e.getClass() + - " " + - e.getMessage() - ); + logger.error("Write of " + dataFileInfo + " failed due to " + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dataFileInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DsArchiver.java b/src/main/java/org/icatproject/ids/thread/DsArchiver.java index 4d633a34..81c2c664 100644 --- a/src/main/java/org/icatproject/ids/thread/DsArchiver.java +++ b/src/main/java/org/icatproject/ids/thread/DsArchiver.java @@ -2,22 +2,20 @@ import java.nio.file.Files; import java.nio.file.Path; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.icatproject.ids.services.LockManager.Lock; /* * Removes datasets from the fast storage (doesn't write them to slow storage) */ public class DsArchiver implements Runnable { - - private static final Logger logger = LoggerFactory.getLogger( - DsArchiver.class - ); + private final static Logger logger = LoggerFactory.getLogger(DsArchiver.class); private DatasetInfo dsInfo; private MainStorageInterface mainStorageInterface; @@ -25,12 +23,7 @@ public class DsArchiver implements Runnable { private Path markerDir; private Lock lock; - public DsArchiver( - DatasetInfo dsInfo, - PropertyHandler propertyHandler, - FiniteStateMachine fsm, - Lock lock - ) { + public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; mainStorageInterface = propertyHandler.getMainStorage(); @@ -41,22 +34,15 @@ public DsArchiver( @Override public void run() { try { - if ( - Files.exists(markerDir.resolve(Long.toString(dsInfo.getDsId()))) - ) { - logger.error( - "Archive of " + - dsInfo + - " not carried out because a write to secondary storage operation failed previously" - ); + if (Files.exists(markerDir.resolve(Long.toString(dsInfo.getDsId())))) { + logger.error("Archive of " + dsInfo + + " not carried out because a write to secondary storage operation failed previously"); } else { mainStorageInterface.delete(dsInfo); logger.debug("Archive of " + dsInfo + " completed"); } } catch (Exception e) { - logger.error( - "Archive of " + dsInfo + " failed due to " + e.getMessage() - ); + logger.error("Archive of " + dsInfo + " failed due to " + e.getMessage()); } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/main/java/org/icatproject/ids/thread/DsRestorer.java b/src/main/java/org/icatproject/ids/thread/DsRestorer.java index c901f834..20c7b49a 100644 --- a/src/main/java/org/icatproject/ids/thread/DsRestorer.java +++ b/src/main/java/org/icatproject/ids/thread/DsRestorer.java @@ -10,6 +10,10 @@ import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; @@ -19,19 +23,15 @@ import org.icatproject.ids.plugin.MainStorageInterface; import org.icatproject.ids.plugin.ZipMapperInterface; import org.icatproject.ids.services.IcatReader; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.icatproject.ids.services.LockManager.Lock; /* * Restores datafiles from the slow to the fast storage. */ public class DsRestorer implements Runnable { - private static final Logger logger = LoggerFactory.getLogger( - DsRestorer.class - ); + private final static Logger logger = LoggerFactory.getLogger(DsRestorer.class); private DatasetInfo dsInfo; @@ -46,13 +46,7 @@ public class DsRestorer implements Runnable { private ZipMapperInterface zipMapper; private Lock lock; - public DsRestorer( - DatasetInfo dsInfo, - PropertyHandler propertyHandler, - FiniteStateMachine fsm, - IcatReader reader, - Lock lock - ) { + public DsRestorer(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, IcatReader reader, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; zipMapper = propertyHandler.getZipMapper(); @@ -77,84 +71,39 @@ public void run() { long size = 0; int n = 0; - List datafiles = - ( - (Dataset) reader.get( - "Dataset INCLUDE Datafile", - dsInfo.getDsId() - ) - ).getDatafiles(); - Map nameToLocalMap = new HashMap<>( - datafiles.size() - ); + List datafiles = ((Dataset) reader.get("Dataset INCLUDE Datafile", dsInfo.getDsId())) + .getDatafiles(); + Map nameToLocalMap = new HashMap<>(datafiles.size()); for (Datafile datafile : datafiles) { if (datafile.getLocation() == null) { continue; } - nameToLocalMap.put( - datafile.getName(), - LocationHelper.getLocation( - datafile.getId(), - datafile.getLocation() - ) - ); + nameToLocalMap.put(datafile.getName(), LocationHelper.getLocation(datafile.getId(), datafile.getLocation())); size += datafile.getFileSize(); n++; } - logger.debug( - "Restoring dataset " + - dsInfo.getInvId() + - "/" + - dsInfo.getDsId() + - " with " + - n + - " files of total size " + - size - ); + logger.debug("Restoring dataset " + dsInfo.getInvId() + "/" + dsInfo.getDsId() + " with " + n + + " files of total size " + size); // Get the file into the dataset cache - Path datasetCachePath = Files.createTempFile( - datasetCache, - null, - null - ); + Path datasetCachePath = Files.createTempFile(datasetCache, null, null); archiveStorageInterface.get(dsInfo, datasetCachePath); // Now split file and store it locally - logger.debug( - "Unpacking dataset " + - dsInfo.getInvId() + - "/" + - dsInfo.getDsId() + - " with " + - n + - " files of total size " + - size - ); - ZipInputStream zis = new ZipInputStream( - Files.newInputStream(datasetCachePath) - ); + logger.debug("Unpacking dataset " + dsInfo.getInvId() + "/" + dsInfo.getDsId() + " with " + n + + " files of total size " + size); + ZipInputStream zis = new ZipInputStream(Files.newInputStream(datasetCachePath)); ZipEntry ze = zis.getNextEntry(); Set seen = new HashSet<>(); while (ze != null) { String dfName = zipMapper.getFileName(ze.getName()); if (seen.contains(dfName)) { - throw new RuntimeException( - "Corrupt archive for " + - dsInfo + - ": duplicate entry " + - dfName - ); + throw new RuntimeException("Corrupt archive for " + dsInfo + ": duplicate entry " + dfName); } String location = nameToLocalMap.get(dfName); if (location == null) { - throw new RuntimeException( - "Corrupt archive for " + - dsInfo + - ": spurious entry " + - dfName - ); + throw new RuntimeException("Corrupt archive for " + dsInfo + ": spurious entry " + dfName); } mainStorageInterface.put(zis, location); ze = zis.getNextEntry(); @@ -162,26 +111,18 @@ public void run() { } zis.close(); if (!seen.equals(nameToLocalMap.keySet())) { - throw new RuntimeException( - "Corrupt archive for " + dsInfo + ": missing entries" - ); + throw new RuntimeException("Corrupt archive for " + dsInfo + ": missing entries"); } Files.delete(datasetCachePath); fsm.recordSuccess(dsInfo.getDsId()); logger.debug("Restore of " + dsInfo + " completed"); } catch (Exception e) { fsm.recordFailure(dsInfo.getDsId()); - logger.error( - "Restore of " + - dsInfo + - " failed due to " + - e.getClass() + - " " + - e.getMessage() - ); + logger.error("Restore of " + dsInfo + " failed due to " + e.getClass() + " " + e.getMessage()); try { mainStorageInterface.delete(dsInfo); - } catch (IOException e2) {} + } catch (IOException e2) { + } } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/main/java/org/icatproject/ids/thread/DsWriter.java b/src/main/java/org/icatproject/ids/thread/DsWriter.java index 4f312ac4..e5c1843b 100644 --- a/src/main/java/org/icatproject/ids/thread/DsWriter.java +++ b/src/main/java/org/icatproject/ids/thread/DsWriter.java @@ -8,6 +8,10 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipOutputStream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; @@ -18,19 +22,15 @@ import org.icatproject.ids.plugin.MainStorageInterface; import org.icatproject.ids.plugin.ZipMapperInterface; import org.icatproject.ids.services.IcatReader; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.icatproject.ids.services.LockManager.Lock; /** * Copies dataset from main to archive */ public class DsWriter implements Runnable { - private static final Logger logger = LoggerFactory.getLogger( - DsWriter.class - ); + private final static Logger logger = LoggerFactory.getLogger(DsWriter.class); private static final int BUFSIZ = 1024; private DatasetInfo dsInfo; @@ -43,13 +43,7 @@ public class DsWriter implements Runnable { private ZipMapperInterface zipMapper; private Lock lock; - public DsWriter( - DatasetInfo dsInfo, - PropertyHandler propertyHandler, - FiniteStateMachine fsm, - IcatReader reader, - Lock lock - ) { + public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, IcatReader reader, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; this.zipMapper = propertyHandler.getZipMapper(); @@ -65,64 +59,27 @@ public DsWriter( public void run() { try { if (!mainStorageInterface.exists(dsInfo)) { - logger.info( - "No files present in main storage for " + - dsInfo + - " - will delete archive" - ); + logger.info("No files present in main storage for " + dsInfo + " - will delete archive"); archiveStorageInterface.delete(dsInfo); } else { - Path datasetCachePath = Files.createTempFile( - datasetCache, - null, - null - ); + Path datasetCachePath = Files.createTempFile(datasetCache, null, null); logger.debug("Creating " + datasetCachePath); - List datafiles = - ( - (Dataset) reader.get( - "Dataset INCLUDE Datafile", - dsInfo.getDsId() - ) - ).getDatafiles(); + List datafiles = ((Dataset) reader.get("Dataset INCLUDE Datafile", dsInfo.getDsId())) + .getDatafiles(); ZipOutputStream zos = new ZipOutputStream( - Files.newOutputStream( - datasetCachePath, - StandardOpenOption.CREATE - ) - ); + Files.newOutputStream(datasetCachePath, StandardOpenOption.CREATE)); for (Datafile datafile : datafiles) { if (datafile.getLocation() == null) { continue; } - String location = LocationHelper.getLocation( - datafile.getId(), - datafile.getLocation() - ); + String location = LocationHelper.getLocation(datafile.getId(), datafile.getLocation()); InputStream is = null; try { - zos.putNextEntry( - new ZipEntry( - zipMapper.getFullEntryName( - dsInfo, - new DatafileInfo( - datafile.getId(), - datafile.getName(), - location, - datafile.getCreateId(), - datafile.getModId(), - 0L - ) - ) - ) - ); - is = - mainStorageInterface.get( - location, - datafile.getCreateId(), - datafile.getModId() - ); + zos.putNextEntry(new ZipEntry( + zipMapper.getFullEntryName(dsInfo, new DatafileInfo(datafile.getId(), datafile.getName(), + location, datafile.getCreateId(), datafile.getModId(), 0L)))); + is = mainStorageInterface.get(location, datafile.getCreateId(), datafile.getModId()); int bytesRead = 0; byte[] buffer = new byte[BUFSIZ]; while ((bytesRead = is.read(buffer)) > 0) { @@ -148,14 +105,7 @@ public void run() { logger.debug("Removed marker " + marker); logger.debug("Write of " + dsInfo + " completed"); } catch (Exception e) { - logger.error( - "Write of " + - dsInfo + - " failed due to " + - e.getClass() + - " " + - e.getMessage() - ); + logger.error("Write of " + dsInfo + " failed due to " + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/test/java/org/icatproject/ids/DataSelectionDevTest.java b/src/test/java/org/icatproject/ids/DataSelectionDevTest.java index db4e62b9..f0ba796e 100644 --- a/src/test/java/org/icatproject/ids/DataSelectionDevTest.java +++ b/src/test/java/org/icatproject/ids/DataSelectionDevTest.java @@ -1,16 +1,24 @@ package org.icatproject.ids; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; - -import jakarta.json.Json; -import jakarta.json.JsonReader; import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.net.URISyntaxException; import java.util.Arrays; import java.util.List; import java.util.Properties; + +import jakarta.json.Json; +import jakarta.json.JsonReader; + +import static org.junit.Assert.assertTrue; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import static org.mockito.Mockito.when; +import org.mockito.junit.MockitoJUnitRunner; + import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; import org.icatproject.icat.client.IcatException; @@ -20,12 +28,6 @@ import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; import org.icatproject.ids.services.dataSelectionService.DataSelectionServiceFactory; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; /** * This test was created to fix issue #115 and was run against the Diamond @@ -72,11 +74,7 @@ public class DataSelectionDevTest { @Before public void setup() throws Exception { testProps = new Properties(); - testProps.load( - new FileInputStream( - "src/test/resources/DataSelectionDevTest.properties" - ) - ); + testProps.load(new FileInputStream("src/test/resources/DataSelectionDevTest.properties")); // set up the SOAP and REST ICAT clients icatUrl = ICATGetter.getCleanUrl(testProps.getProperty("icat.url")); icatService = ICATGetter.getService(icatUrl); @@ -85,64 +83,37 @@ public void setup() throws Exception { String userCredsString = testProps.getProperty("login.user"); userSessionId = TestUtils.login(icatService, userCredsString); System.out.println("userSessionId = " + userSessionId); - List readerCreds = Arrays.asList( - testProps.getProperty("login.reader").trim().split("\\s+") - ); + List readerCreds = Arrays.asList(testProps.getProperty("login.reader").trim().split("\\s+")); this.readerCreds = readerCreds; investigationIds = testProps.getProperty("investigation.ids"); datasetIds = testProps.getProperty("dataset.ids"); datafileIds = testProps.getProperty("datafile.ids"); - useReaderForPerformance = - testProps - .getProperty("useReaderForPerformance") - .equalsIgnoreCase("true"); + useReaderForPerformance = testProps.getProperty("useReaderForPerformance").equalsIgnoreCase("true"); // set up a mocked version of the PropertyHandler setupPropertyHandler(); icatReader = new IcatReader(mockedPropertyHandler); } private void setupPropertyHandler() - throws URISyntaxException, IcatException_Exception, IcatException { - JsonReader parser = Json.createReader( - new ByteArrayInputStream(restIcat.getProperties().getBytes()) - ); + throws URISyntaxException, IcatException_Exception, IcatException { + JsonReader parser = Json.createReader(new ByteArrayInputStream(restIcat.getProperties().getBytes())); maxEntities = parser.readObject().getInt("maxEntities"); when(mockedPropertyHandler.getMaxEntities()).thenReturn(maxEntities); when(mockedPropertyHandler.getIcatService()).thenReturn(icatService); when(mockedPropertyHandler.getRestIcat()).thenReturn(restIcat); when(mockedPropertyHandler.getReader()).thenReturn(readerCreds); - when(mockedPropertyHandler.getUseReaderForPerformance()) - .thenReturn(useReaderForPerformance); + when(mockedPropertyHandler.getUseReaderForPerformance()).thenReturn(useReaderForPerformance); } - @Ignore( - "Test requires a specific ICAT setup to produce meaningful results. See class javadoc comment." - ) + @Ignore("Test requires a specific ICAT setup to produce meaningful results. See class javadoc comment.") @Test public void testCreateDataSelection() throws Exception { long startMs = System.currentTimeMillis(); - var dataSelectionFactory = DataSelectionServiceFactory.getInstanceOnlyForTesting( - mockedPropertyHandler, - icatReader - ); - DataSelectionService dataSelectionService = dataSelectionFactory.getSelectionService( - userSessionId, - investigationIds, - datasetIds, - datafileIds, - RequestType.GETSIZE - ); - System.out.println( - "Creating DataSelection took " + - (System.currentTimeMillis() - startMs) + - " ms" - ); - System.out.println( - "DsInfo size: " + dataSelectionService.getDsInfo().size() - ); - System.out.println( - "DfInfo size: " + dataSelectionService.getDfInfo().size() - ); + var dataSelectionFactory = DataSelectionServiceFactory.getInstanceOnlyForTesting(mockedPropertyHandler, icatReader); + DataSelectionService dataSelectionService = dataSelectionFactory.getSelectionService(userSessionId,investigationIds, datasetIds, datafileIds, RequestType.GETSIZE); + System.out.println("Creating DataSelection took " + (System.currentTimeMillis() - startMs) + " ms"); + System.out.println("DsInfo size: " + dataSelectionService.getDsInfo().size()); + System.out.println("DfInfo size: " + dataSelectionService.getDfInfo().size()); // there must be at least one Datafile in the DataSelection assertTrue("message", dataSelectionService.getDfInfo().size() > 0); } diff --git a/src/test/java/org/icatproject/ids/DigestTest.java b/src/test/java/org/icatproject/ids/DigestTest.java index 897d11e9..735e6195 100644 --- a/src/test/java/org/icatproject/ids/DigestTest.java +++ b/src/test/java/org/icatproject/ids/DigestTest.java @@ -3,11 +3,11 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import org.junit.Test; import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.helpers.LocationHelper; import org.icatproject.utils.IcatSecurity; -import org.junit.Test; public class DigestTest { @@ -16,21 +16,15 @@ public class DigestTest { @Test public void testCheck() throws Exception { - String a = - location + " " + IcatSecurity.digest(1234567L, location, key); + String a = location + " " + IcatSecurity.digest(1234567L, location, key); - assertEquals( - location, - LocationHelper.getLocationFromDigest(1234567L, a, key) - ); + assertEquals(location, LocationHelper.getLocationFromDigest(1234567L, a, key)); try { LocationHelper.getLocationFromDigest(1234568L, a, key); fail(); } catch (InsufficientPrivilegesException e) { - assertTrue( - e.getMessage().contains("does not contain a valid hash") - ); + assertTrue(e.getMessage().contains("does not contain a valid hash")); } try { @@ -39,5 +33,7 @@ public void testCheck() throws Exception { } catch (InsufficientPrivilegesException e) { assertTrue(e.getMessage().contains("does not contain hash")); } + } + } diff --git a/src/test/java/org/icatproject/ids/PreparePackingTest.java b/src/test/java/org/icatproject/ids/PreparePackingTest.java index 8ef93b5f..6b2a857e 100644 --- a/src/test/java/org/icatproject/ids/PreparePackingTest.java +++ b/src/test/java/org/icatproject/ids/PreparePackingTest.java @@ -1,11 +1,5 @@ package org.icatproject.ids; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -16,8 +10,15 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import org.icatproject.ids.models.DataInfoBase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import org.icatproject.ids.models.DatafileInfo; +import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.models.Prepared; import org.junit.Test; @@ -36,64 +37,21 @@ public void packAndUnpack() throws Exception { long dsid2 = 18L; long invId = 15L; long facilityId = 45L; - dfInfos.put( - 5L, - new DatafileInfo( - 5L, - "dfName", - "dfLocation", - "createId", - "modId", - dsid1 - ) - ); + dfInfos.put(5L, new DatafileInfo(5L, "dfName", "dfLocation", "createId", "modId", dsid1)); - dfInfos.put( - 51L, - new DatafileInfo(51L, "dfName2", null, "createId", "modId", dsid1) - ); + dfInfos.put(51L, new DatafileInfo(51L, "dfName2", null, "createId", "modId", dsid1)); - dsInfos.put( - dsid1, - new DatasetInfo( - dsid1, - "dsName", - "dsLocation", - invId, - "invName", - "visitId", - facilityId, - "facilityName" - ) - ); + dsInfos.put(dsid1, new DatasetInfo(dsid1, "dsName", "dsLocation", invId, "invName", + "visitId", facilityId, "facilityName")); - dsInfos.put( - dsid2, - new DatasetInfo( - dsid2, - "dsName2", - null, - invId, - "invName", - "visitId", - facilityId, - "facilityName" - ) - ); + dsInfos.put(dsid2, new DatasetInfo(dsid2, "dsName2", null, invId, "invName", "visitId", + facilityId, "facilityName")); emptyDatasets.add(dsid2); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (OutputStream stream = new BufferedOutputStream(baos)) { - Prepared.pack( - stream, - zip, - compress, - dsInfos, - dfInfos, - emptyDatasets, - 51 - ); + Prepared.pack(stream, zip, compress, dsInfos, dfInfos, emptyDatasets, 51); } System.out.println(baos.toString()); InputStream stream = new ByteArrayInputStream(baos.toByteArray()); @@ -138,6 +96,7 @@ public void packAndUnpack() throws Exception { assertEquals("visitId", dsInfo.getVisitId()); assertEquals((Long) facilityId, dsInfo.getFacilityId()); assertEquals("facilityName", dsInfo.getFacilityName()); + } assertEquals(1, prepared.emptyDatasets.size()); assertEquals((Long) dsid2, prepared.emptyDatasets.iterator().next()); diff --git a/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java b/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java index 79cc009e..e6960866 100644 --- a/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java +++ b/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java @@ -1,9 +1,10 @@ package org.icatproject.ids; -import static org.junit.Assert.assertEquals; - import java.io.ByteArrayOutputStream; import java.io.OutputStream; + +import static org.junit.Assert.assertEquals; + import org.icatproject.ids.helpers.RangeOutputStream; import org.junit.Test; @@ -48,11 +49,7 @@ public void t3() throws Exception { os.flush(); } os.close(); - assertEquals( - "m is " + m, - "ABCDEFGHIJKLMONPQRSTUVWXYZ", - baos.toString() - ); + assertEquals("m is " + m, "ABCDEFGHIJKLMONPQRSTUVWXYZ", baos.toString()); } } @@ -71,6 +68,7 @@ public void t4() throws Exception { } } + @Test public void t5() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); diff --git a/src/test/java/org/icatproject/ids/TestICATGetter.java b/src/test/java/org/icatproject/ids/TestICATGetter.java index e8e488a6..8bfa99c6 100644 --- a/src/test/java/org/icatproject/ids/TestICATGetter.java +++ b/src/test/java/org/icatproject/ids/TestICATGetter.java @@ -1,35 +1,26 @@ package org.icatproject.ids; import static org.junit.Assert.assertEquals; +import org.junit.Test; import org.icatproject.ICAT; import org.icatproject.ids.services.ICATGetter; -import org.junit.Test; public class TestICATGetter { @Test public void testGFUrl() throws Exception { - assertEquals( - "http://localhost", - ICATGetter.getCleanUrl("http://localhost/ICATService/ICAT?wsdl") - ); + assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost/ICATService/ICAT?wsdl")); } @Test public void testSlash() throws Exception { - assertEquals( - "http://localhost", - ICATGetter.getCleanUrl("http://localhost/") - ); + assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost/")); } @Test public void testClean() throws Exception { - assertEquals( - "http://localhost", - ICATGetter.getCleanUrl("http://localhost") - ); + assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost")); } @Test diff --git a/src/test/java/org/icatproject/ids/TestUtils.java b/src/test/java/org/icatproject/ids/TestUtils.java index 054856fc..cbd62530 100644 --- a/src/test/java/org/icatproject/ids/TestUtils.java +++ b/src/test/java/org/icatproject/ids/TestUtils.java @@ -2,6 +2,7 @@ import java.util.Arrays; import java.util.List; + import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; import org.icatproject.Login.Credentials; @@ -19,8 +20,7 @@ public class TestUtils { * @return an ICAT session ID * @throws IcatException_Exception if the login fails */ - public static String login(ICAT icatService, String credsString) - throws IcatException_Exception { + public static String login(ICAT icatService, String credsString) throws IcatException_Exception { List creds = Arrays.asList(credsString.trim().split("\\s+")); Credentials credentials = new Credentials(); List entries = credentials.getEntry(); @@ -32,4 +32,5 @@ public static String login(ICAT icatService, String credsString) } return icatService.login(creds.get(0), credentials); } + } diff --git a/src/test/java/org/icatproject/ids/TestValueContainer.java b/src/test/java/org/icatproject/ids/TestValueContainer.java index 41183f33..bc3daf98 100644 --- a/src/test/java/org/icatproject/ids/TestValueContainer.java +++ b/src/test/java/org/icatproject/ids/TestValueContainer.java @@ -9,15 +9,18 @@ import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.stream.Collectors; + import org.icatproject.ids.enums.ValueContainerType; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.helpers.ValueContainer; import org.junit.Test; + public class TestValueContainer { @Test public void testInvalidValueContainer() throws Exception { + var vc = ValueContainer.getInvalid(); assertFalse(vc.isVoid()); assertTrue(vc.getType() == ValueContainerType.INVALID); @@ -25,6 +28,7 @@ public void testInvalidValueContainer() throws Exception { @Test public void testVoidValueContainer() throws Exception { + var vc = ValueContainer.getVoid(); assertTrue(vc.isVoid()); assertTrue(vc.getType() == ValueContainerType.VOID); @@ -33,6 +37,7 @@ public void testVoidValueContainer() throws Exception { @Test(expected = InternalException.class) public void testIntValueContainer() throws Exception { + var vc = new ValueContainer(1); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -45,6 +50,7 @@ public void testIntValueContainer() throws Exception { @Test(expected = InternalException.class) public void testLongValueContainer() throws Exception { + var vc = new ValueContainer(1L); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -57,6 +63,7 @@ public void testLongValueContainer() throws Exception { @Test(expected = InternalException.class) public void testBoolValueContainer() throws Exception { + var vc = new ValueContainer(true); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -69,6 +76,7 @@ public void testBoolValueContainer() throws Exception { @Test(expected = InternalException.class) public void testStringValueContainer() throws Exception { + var vc = new ValueContainer("test"); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -81,6 +89,7 @@ public void testStringValueContainer() throws Exception { @Test(expected = InternalException.class) public void testInputStreamValueContainer() throws Exception { + String s = "test InputStream"; var inputStream = new ByteArrayInputStream(s.getBytes()); var vc = new ValueContainer(inputStream); @@ -89,11 +98,8 @@ public void testInputStreamValueContainer() throws Exception { assertFalse(vc.isNull()); assertEquals(vc.getInputStream(), inputStream); - - var ISReader = new InputStreamReader( - vc.getInputStream(), - StandardCharsets.UTF_8 - ); + + var ISReader = new InputStreamReader(vc.getInputStream(), StandardCharsets.UTF_8); var BReader = new BufferedReader(ISReader); String textFromStream = BReader.lines().collect(Collectors.joining()); BReader.close(); @@ -101,4 +107,5 @@ public void testInputStreamValueContainer() throws Exception { vc.getString(); } -} + +} \ No newline at end of file diff --git a/src/test/java/org/icatproject/ids/TestZipping.java b/src/test/java/org/icatproject/ids/TestZipping.java index 2f728467..56dce770 100644 --- a/src/test/java/org/icatproject/ids/TestZipping.java +++ b/src/test/java/org/icatproject/ids/TestZipping.java @@ -1,7 +1,5 @@ package org.icatproject.ids; -import static org.junit.Assert.assertEquals; - import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -12,6 +10,8 @@ import java.util.zip.ZipException; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; + +import static org.junit.Assert.assertEquals; import org.junit.Test; public class TestZipping { @@ -19,21 +19,13 @@ public class TestZipping { @Test public void testDuplicates() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ZipOutputStream zos = new ZipOutputStream( - new BufferedOutputStream(baos) - ); + ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos)); // zos.setLevel(0); List in = new ArrayList<>(); - for (String entryName : Arrays.asList( - "abcd/qa", - "abcd/qw", - "abcd/qw", - "abcd/qb", - "abcd/qc", - "abcd/qw" - )) { + for (String entryName : Arrays.asList("abcd/qa", "abcd/qw", "abcd/qw", "abcd/qb", "abcd/qc", "abcd/qw")) { + try { zos.putNextEntry(new ZipEntry(entryName)); byte[] bytes = entryName.getBytes(); @@ -50,9 +42,7 @@ public void testDuplicates() throws Exception { assertEquals(4, in.size()); - ByteArrayInputStream bais = new ByteArrayInputStream( - baos.toByteArray() - ); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ZipInputStream zis = new ZipInputStream(bais); int off = 0; @@ -71,5 +61,7 @@ public void testDuplicates() throws Exception { ze = zis.getNextEntry(); } zis.close(); + } + } diff --git a/src/test/java/org/icatproject/ids/TidierTest.java b/src/test/java/org/icatproject/ids/TidierTest.java index 58e75957..8f2762c6 100644 --- a/src/test/java/org/icatproject/ids/TidierTest.java +++ b/src/test/java/org/icatproject/ids/TidierTest.java @@ -1,13 +1,13 @@ package org.icatproject.ids; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.io.ByteArrayInputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -16,13 +16,10 @@ public class TidierTest { private static Path file; @BeforeClass - public static void beforeClass() throws Exception { + static public void beforeClass() throws Exception { file = Files.createTempFile(null, null); - Files.copy( - new ByteArrayInputStream(new byte[2000]), - file, - StandardCopyOption.REPLACE_EXISTING - ); + Files.copy(new ByteArrayInputStream(new byte[2000]), file, + StandardCopyOption.REPLACE_EXISTING); } @Test @@ -88,26 +85,13 @@ public void testCleanPreparedDir() throws Exception { @Test public void testAddStringConstraint() throws Exception { StringBuilder sb1 = new StringBuilder(); - Tidier.addStringConstraint( - sb1, - "df.location", - "/path/to/normal/file", - false - ); + Tidier.addStringConstraint(sb1, "df.location", "/path/to/normal/file", false); assertEquals(" df.location = '/path/to/normal/file'", sb1.toString()); /* Fix error where a file path contains an apostrophe */ StringBuilder sb2 = new StringBuilder(); - Tidier.addStringConstraint( - sb2, - "df.location", - "/path/to/Person's Files/myscript.py", - false - ); - assertEquals( - " df.location = '/path/to/Person''s Files/myscript.py'", - sb2.toString() - ); + Tidier.addStringConstraint(sb2, "df.location", "/path/to/Person's Files/myscript.py", false); + assertEquals(" df.location = '/path/to/Person''s Files/myscript.py'", sb2.toString()); } @Test diff --git a/src/test/java/org/icatproject/ids/TransmittingTest.java b/src/test/java/org/icatproject/ids/TransmittingTest.java index f73e71f6..7e2e986c 100644 --- a/src/test/java/org/icatproject/ids/TransmittingTest.java +++ b/src/test/java/org/icatproject/ids/TransmittingTest.java @@ -6,6 +6,7 @@ import java.io.ByteArrayInputStream; import java.net.URISyntaxException; import java.nio.file.Paths; + import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; import org.icatproject.icat.client.IcatException; @@ -39,8 +40,9 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; + /** - * This test was created when the building of the transmission body was made more comon and generic. + * This test was created when the building of the transmission body was made more comon and generic. * It should ensure the right structure of the containing json. */ @RunWith(MockitoJUnitRunner.class) @@ -48,22 +50,15 @@ public class TransmittingTest { @Mock private PropertyHandler mockedPropertyHandler; - @Mock private Transmitter mockedTransmitter; - @Mock private FiniteStateMachine mockedFsm; - @Mock private LockManager mockedLockManager; - @Mock private IcatReader mockedReader; - - @Mock - ArchiveStorageInterface mockedArchiveStorage; - + @Mock ArchiveStorageInterface mockedArchiveStorage; @Mock private ICAT mockedIcat; @@ -73,184 +68,98 @@ public class TransmittingTest { private String investigationIds = "1, 2, 3"; private String datasetIds = "4, 5, 6"; private String datafileIds = "7, 8, 9"; - private String defaultTransmissionBodyForPreparedId = - "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}"; - private String defaultTransmissionBodyForSessionId = - "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9]}"; + private String defaultTransmissionBodyForPreparedId = "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}"; + private String defaultTransmissionBodyForSessionId = "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9]}"; private void setup() - throws URISyntaxException, IcatException_Exception, IcatException { + throws URISyntaxException, IcatException_Exception, IcatException { + when(mockedPropertyHandler.getIcatService()).thenReturn(mockedIcat); when(mockedPropertyHandler.getCacheDir()).thenReturn(Paths.get("")); - when(mockedPropertyHandler.getStorageUnit()) - .thenReturn(StorageUnit.DATASET); - when(mockedPropertyHandler.getArchiveStorage()) - .thenReturn(mockedArchiveStorage); + when(mockedPropertyHandler.getStorageUnit()).thenReturn(StorageUnit.DATASET); + when(mockedPropertyHandler.getArchiveStorage()).thenReturn(mockedArchiveStorage); when(mockedPropertyHandler.getReadOnly()).thenReturn(false); - when(mockedIcat.getUserName("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")) - .thenReturn("TestUser"); - - ServiceProvider.createInstance( - mockedPropertyHandler, - mockedTransmitter, - mockedFsm, - mockedLockManager, - mockedReader - ); + when(mockedIcat.getUserName("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")).thenReturn("TestUser"); + + ServiceProvider.createInstance(mockedPropertyHandler, mockedTransmitter, mockedFsm, mockedLockManager, mockedReader); } @Test - public void testTransmitterBodyForArchiveRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForArchiveRequest_shouldBeOk() throws Exception { + setup(); - var handler = new ArchiveHandler( - ip, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + var handler = new ArchiveHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetSizeRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForGetSizeRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetSizeHandler( - ip, - preparedId, - null, - null, - null, - null - ); + var handler = new GetSizeHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = - new GetSizeHandler( - ip, - null, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + handler = new GetSizeHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetSizeFastRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForGetSizeFastRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetSizeHandlerForFastProcessing( - ip, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + var handler = new GetSizeHandlerForFastProcessing(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); + } @Test - public void testTransmitterBodyForDeleteRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForDeleteRequest_shouldBeOk() throws Exception { + setup(); - var handler = new DeleteHandler( - ip, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + var handler = new DeleteHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetDataFileIdsRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForGetDataFileIdsRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetDataFileIdsHandler( - ip, - preparedId, - null, - null, - null, - null - ); + var handler = new GetDataFileIdsHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = - new GetDataFileIdsHandler( - "192.168.17.1", - null, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + handler = new GetDataFileIdsHandler("192.168.17.1", null, sessionId, investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetDataRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForGetDataRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetDataHandler( - ip, - preparedId, - null, - null, - null, - null, - false, - false, - "", - "" - ); + var handler = new GetDataHandler(ip, preparedId, null, null, null, null, false, false, "", ""); String body = handler.provideTransmissionBody(); - assertEquals( - "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"transferId\":-1}", - body - ); - - handler = - new GetDataHandler( - ip, - null, - sessionId, - investigationIds, - datasetIds, - datafileIds, - false, - false, - "", - "" - ); + assertEquals("{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"transferId\":-1}", body); + + handler = new GetDataHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds, false, false, "", ""); body = handler.provideTransmissionBody(); - assertEquals( - "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"transferId\":-1}", - body - ); + assertEquals("{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"transferId\":-1}", body); } @Test - public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() throws Exception { + setup(); var handler = new GetIcatUrlHandler(ip); @@ -259,8 +168,8 @@ public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() } @Test - public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() throws Exception { + setup(); var handler = new GetServiceStatusHandler(ip, sessionId); @@ -269,50 +178,32 @@ public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() } @Test - public void testTransmitterBodyForGetStatusRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForGetStatusRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetStatusHandler( - ip, - preparedId, - null, - null, - null, - null - ); + var handler = new GetStatusHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = - new GetStatusHandler( - ip, - null, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + handler = new GetStatusHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForIsPreparedRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForIsPreparedRequest_shouldBeOk() throws Exception { + setup(); var handler = new IsPreparedHandler(ip, preparedId); String body = handler.provideTransmissionBody(); - assertEquals( - "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}", - body - ); + assertEquals("{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}", body); } @Test - public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() throws Exception { + setup(); var handler = new IsReadOnlyHandler(ip); @@ -321,8 +212,8 @@ public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() } @Test - public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() throws Exception { + setup(); var handler = new IsTwoLevelHandler(ip); @@ -331,98 +222,56 @@ public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() } @Test - public void testTransmitterBodyForPrepareDataRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForPrepareDataRequest_shouldBeOk() throws Exception { + setup(); - var handler = new PrepareDataHandler( - ip, - sessionId, - investigationIds, - datasetIds, - datafileIds, - false, - false - ); + var handler = new PrepareDataHandler(ip, sessionId, investigationIds, datasetIds, datafileIds, false, false); String body = handler.provideTransmissionBody(); - assertEquals( - "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"preparedId\":\"\"}", - body - ); + assertEquals("{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"preparedId\":\"\"}", body); + } @Test public void testTransmitterBodyForPutRequest_shouldBeOk() throws Exception { + setup(); - var handler = new PutHandler( - ip, - sessionId, - new ByteArrayInputStream("".getBytes()), - "someName", - datafileIds, - datasetIds, - "someDescription", - "someDOI", - "simeCreateTimeString", - "someModTimeString", - false, - false - ); + var handler = new PutHandler(ip, sessionId, new ByteArrayInputStream( "".getBytes() ), "someName", datafileIds , datasetIds, "someDescription", "someDOI", "simeCreateTimeString", "someModTimeString", false, false ); String body = handler.provideTransmissionBody(); assertEquals("{\"userName\":\"TestUser\",\"datafileId\":-1}", body); } @Test - public void testTransmitterBodyForResetRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForResetRequest_shouldBeOk() throws Exception { + setup(); var handler = new ResetHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = - new ResetHandler( - ip, - null, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + handler = new ResetHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForRestoreRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForRestoreRequest_shouldBeOk() throws Exception { + setup(); - var handler = new RestoreHandler( - ip, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + var handler = new RestoreHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForWriteRequest_shouldBeOk() - throws Exception { + public void testTransmitterBodyForWriteRequest_shouldBeOk() throws Exception { + setup(); - var handler = new WriteHandler( - ip, - sessionId, - investigationIds, - datasetIds, - datafileIds - ); + var handler = new WriteHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } diff --git a/src/test/java/org/icatproject/ids/ValidaterTest.java b/src/test/java/org/icatproject/ids/ValidaterTest.java index f66928cb..6dda09ef 100644 --- a/src/test/java/org/icatproject/ids/ValidaterTest.java +++ b/src/test/java/org/icatproject/ids/ValidaterTest.java @@ -2,10 +2,10 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import org.junit.Test; import org.icatproject.ids.exceptions.BadRequestException; import org.icatproject.ids.requestHandlers.base.DataControllerBase; -import org.junit.Test; public class ValidaterTest { @@ -28,5 +28,8 @@ private void testValidUUID(boolean b, String id) { System.out.println(e.getMessage()); assertFalse(b); } + } + } + diff --git a/src/test/java/org/icatproject/ids/integration/BaseTest.java b/src/test/java/org/icatproject/ids/integration/BaseTest.java index 45022576..4f0a23d4 100644 --- a/src/test/java/org/icatproject/ids/integration/BaseTest.java +++ b/src/test/java/org/icatproject/ids/integration/BaseTest.java @@ -1,12 +1,5 @@ package org.icatproject.ids.integration; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import jakarta.json.Json; -import jakarta.json.JsonReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -33,6 +26,16 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; + +import jakarta.json.Json; +import jakarta.json.JsonReader; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import org.junit.Before; + import org.icatproject.Datafile; import org.icatproject.DatafileFormat; import org.icatproject.Dataset; @@ -51,22 +54,19 @@ import org.icatproject.ids.integration.util.client.TestingClient.ServiceStatus; import org.icatproject.ids.integration.util.client.TestingClient.Status; import org.icatproject.ids.services.ICATGetter; -import org.junit.Before; public class BaseTest { public class DeleteVisitor extends SimpleFileVisitor { @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) - throws IOException { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override - public FileVisitResult postVisitDirectory(Path dir, IOException e) - throws IOException { + public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; @@ -75,34 +75,21 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) throw e; } } + } private static long timestamp = System.currentTimeMillis(); - protected Path getDirOnFastStorage(Long dsId) - throws IcatException_Exception { - Dataset icatDs = (Dataset) icatWS.get( - sessionId, - "Dataset INCLUDE Investigation", - dsId - ); - return setup - .getStorageDir() - .resolve(Long.toString(icatDs.getInvestigation().getId())) - .resolve(Long.toString(icatDs.getId())); + protected Path getDirOnFastStorage(Long dsId) throws IcatException_Exception { + Dataset icatDs = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Investigation", dsId); + return setup.getStorageDir().resolve(Long.toString(icatDs.getInvestigation().getId())) + .resolve(Long.toString(icatDs.getId())); } - protected Path getFileOnArchiveStorage(Long dsId) - throws IcatException_Exception { - Dataset icatDs = (Dataset) icatWS.get( - sessionId, - "Dataset INCLUDE Investigation", - dsId - ); - return setup - .getStorageArchiveDir() - .resolve(Long.toString(icatDs.getInvestigation().getId())) - .resolve(Long.toString(icatDs.getId())); + protected Path getFileOnArchiveStorage(Long dsId) throws IcatException_Exception { + Dataset icatDs = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Investigation", dsId); + return setup.getStorageArchiveDir().resolve(Long.toString(icatDs.getInvestigation().getId())) + .resolve(Long.toString(icatDs.getId())); } protected static org.icatproject.ICAT icatWS; @@ -130,11 +117,7 @@ public void before() throws Exception { testingClient = new TestingClient(setup.getIdsUrl()); sessionId = setup.getRootSessionId(); waitForIds(); - populateStorage( - setup.isTwoLevel(), - setup.getStorageUnit(), - setup.getKey() - ); + populateStorage(setup.isTwoLevel(), setup.getStorageUnit(), setup.getKey()); } protected void checkAbsent(Path file) { @@ -175,9 +158,7 @@ protected void logTime(String msg) { long now = System.currentTimeMillis(); if (msg != null) { if (time != 0) { - System.out.println( - msg + " took " + (now - time) / 1000. + "s." - ); + System.out.println(msg + " took " + (now - time) / 1000. + "s."); } else { System.out.println(msg); } @@ -185,8 +166,7 @@ protected void logTime(String msg) { time = now; } - protected void checkZipFile(Path file, List ids, int compressedSize) - throws IOException { + protected void checkZipFile(Path file, List ids, int compressedSize) throws IOException { checkZipStream(Files.newInputStream(file), ids, compressedSize, 0); } @@ -225,15 +205,13 @@ protected byte[] getOutput(InputStream stream) throws IOException { } } - private void populateStorage( - boolean twoLevel, - String storageUnit, - String key - ) throws IOException, IcatException_Exception, NoSuchAlgorithmException { + private void populateStorage(boolean twoLevel, String storageUnit, String key) + throws IOException, IcatException_Exception, NoSuchAlgorithmException { clearStorage(); long timestamp = System.currentTimeMillis(); try { + List objects = icatWS.search(sessionId, "Facility"); List facilities = new ArrayList<>(); for (Object o : objects) { @@ -255,9 +233,7 @@ private void populateStorage( supportedDatafileFormat.setFacility(fac); supportedDatafileFormat.setName("test_format"); supportedDatafileFormat.setVersion("42.0.0"); - supportedDatafileFormat.setId( - icatWS.create(sessionId, supportedDatafileFormat) - ); + supportedDatafileFormat.setId(icatWS.create(sessionId, supportedDatafileFormat)); InvestigationType invType = new InvestigationType(); invType.setName("Not null"); @@ -301,41 +277,25 @@ private void populateStorage( df1.setName("a/df1_" + timestamp); df1.setLocation(ds1Loc + UUID.randomUUID()); df1.setDataset(ds1); - writeToFile( - df1, - "df1 test content very compressible very compressible", - key - ); + writeToFile(df1, "df1 test content very compressible very compressible", key); Datafile df2 = new Datafile(); df2.setName("df2_" + timestamp); df2.setLocation(ds1Loc + UUID.randomUUID()); df2.setDataset(ds1); - writeToFile( - df2, - "df2 test content very compressible very compressible", - key - ); + writeToFile(df2, "df2 test content very compressible very compressible", key); Datafile df3 = new Datafile(); df3.setName("df3_" + timestamp); df3.setLocation(ds2Loc + UUID.randomUUID()); df3.setDataset(ds2); - writeToFile( - df3, - "df3 test content very compressible very compressible", - key - ); + writeToFile(df3, "df3 test content very compressible very compressible", key); Datafile df4 = new Datafile(); df4.setName("df4_" + timestamp); df4.setLocation(ds2Loc + "Person's file"); df4.setDataset(ds2); - writeToFile( - df4, - "df4 test content very compressible very compressible", - key - ); + writeToFile(df4, "df4 test content very compressible very compressible", key); datasetIds.add(ds1.getId()); datasetIds.add(ds2.getId()); @@ -351,20 +311,18 @@ private void populateStorage( moveDatasetToArchive(storageUnit, ds2, ds2Loc, fac, inv, key); } - newFileLocation = - setup.getUpdownDir().resolve("new_file_" + timestamp); + newFileLocation = setup.getUpdownDir().resolve("new_file_" + timestamp); Files.createDirectories(newFileLocation.getParent()); byte[] bytes = "new_file test content".getBytes(); OutputStream out = Files.newOutputStream(newFileLocation); out.write(bytes); out.close(); } catch (IllegalAccessError e) { - System.err.println( - "Could not prepare ICAT db for testing: " + e.getMessage() - ); + System.err.println("Could not prepare ICAT db for testing: " + e.getMessage()); e.printStackTrace(); throw e; } + } protected Map crcs = new HashMap<>(); @@ -373,12 +331,8 @@ private void populateStorage( protected Map ids = new HashMap<>(); protected Map paths = new HashMap<>(); - protected void checkZipStream( - InputStream stream, - List datafileIdsIn, - long compressedSize, - int numLeft - ) throws IOException { + protected void checkZipStream(InputStream stream, List datafileIdsIn, long compressedSize, int numLeft) + throws IOException { ZipInputStream zis = new ZipInputStream(stream); ZipEntry ze = zis.getNextEntry(); List idsNeeded = new ArrayList<>(datafileIdsIn); @@ -407,10 +361,7 @@ protected void checkStream(InputStream stream, long id) throws IOException { boolean found = false; for (Entry e : ids.entrySet()) { if (id == e.getValue()) { - assertEquals( - contents.get(e.getKey()), - new String(getOutput(stream)) - ); + assertEquals(contents.get(e.getKey()), new String(getOutput(stream))); found = true; break; } @@ -419,7 +370,7 @@ protected void checkStream(InputStream stream, long id) throws IOException { } protected void writeToFile(Datafile df, String content, String key) - throws IOException, IcatException_Exception, NoSuchAlgorithmException { + throws IOException, IcatException_Exception, NoSuchAlgorithmException { Path path = setup.getStorageDir().resolve(df.getLocation()); Files.createDirectories(path.getParent()); byte[] bytes = content.getBytes(); @@ -445,42 +396,16 @@ protected void writeToFile(Datafile df, String content, String key) Dataset ds = df.getDataset(); Investigation inv = ds.getInvestigation(); Facility fac = inv.getFacility(); - paths.put( - "ids/" + - fac.getName() + - "/" + - inv.getName() + - "/" + - URLEncoder.encode(inv.getVisitId(), "UTF-8") + - "/" + - ds.getName() + - "/" + - df.getName(), - df.getLocation() - ); + paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + URLEncoder.encode(inv.getVisitId(), "UTF-8") + + "/" + ds.getName() + "/" + df.getName(), df.getLocation()); } private static final char[] HEX_CHARS = { - '0', - '1', - '2', - '3', - '4', - '5', - '6', - '7', - '8', - '9', - 'A', - 'B', - 'C', - 'D', - 'E', - 'F', + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; - private String digest(Long id, String location, String key) - throws NoSuchAlgorithmException { + private String digest(Long id, String location, String key) throws NoSuchAlgorithmException { byte[] pattern = (id + location + key).getBytes(); MessageDigest digest = null; digest = MessageDigest.getInstance("SHA-256"); @@ -495,29 +420,16 @@ private String digest(Long id, String location, String key) return new String(hexChars); } - private void moveDatasetToArchive( - String storageUnit, - Dataset ds, - String dsLoc, - Facility fac, - Investigation inv, - String key - ) throws IOException, IcatException_Exception { - ds = - (Dataset) icatWS.get( - sessionId, - "Dataset INCLUDE Datafile", - ds.getId() - ); + private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, Facility fac, Investigation inv, + String key) throws IOException, IcatException_Exception { + ds = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Datafile", ds.getId()); Path top = setup.getStorageDir(); if (storageUnit.equals("DATASET")) { Path zipFile = setup.getStorageArchiveDir().resolve(dsLoc); Files.createDirectories(zipFile.getParent()); - ZipOutputStream zos = new ZipOutputStream( - Files.newOutputStream(zipFile) - ); + ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile)); zos.setLevel(0); for (Datafile df : ds.getDatafiles()) { @@ -525,27 +437,12 @@ private void moveDatasetToArchive( if (key == null) { file = top.resolve(df.getLocation()); } else { - file = - top.resolve( - getLocationFromDigest(df.getId(), df.getLocation()) - ); + file = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); } InputStream fis = Files.newInputStream(file); - zos.putNextEntry( - new ZipEntry( - "ids/" + - fac.getName() + - "/" + - inv.getName() + - "/" + - URLEncoder.encode(inv.getVisitId(), "UTF-8") + - "/" + - ds.getName() + - "/" + - df.getName() - ) - ); + zos.putNextEntry(new ZipEntry("ids/" + fac.getName() + "/" + inv.getName() + "/" + + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" + ds.getName() + "/" + df.getName())); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { @@ -553,6 +450,7 @@ private void moveDatasetToArchive( } zos.closeEntry(); fis.close(); + } zos.close(); } else if (storageUnit.equals("DATAFILE")) { @@ -563,16 +461,9 @@ private void moveDatasetToArchive( if (key == null) { p = top.resolve(df.getLocation()); } else { - p = - top.resolve( - getLocationFromDigest(df.getId(), df.getLocation()) - ); + p = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); } - Files.move( - p, - archive.resolve(p.getFileName()), - StandardCopyOption.REPLACE_EXISTING - ); + Files.move(p, archive.resolve(p.getFileName()), StandardCopyOption.REPLACE_EXISTING); } } for (Datafile df : ds.getDatafiles()) { @@ -580,10 +471,7 @@ private void moveDatasetToArchive( if (key == null) { file = top.resolve(df.getLocation()); } else { - file = - top.resolve( - getLocationFromDigest(df.getId(), df.getLocation()) - ); + file = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); } Files.deleteIfExists(file); Path parent = file.getParent(); @@ -596,6 +484,7 @@ private void moveDatasetToArchive( } } } + } protected String getLocationFromDigest(Long id, String locationWithHash) { @@ -619,12 +508,8 @@ protected void raceTest() throws Exception { } logTime("File created"); - Investigation inv = (Investigation) icatWS - .search(sessionId, "Investigation") - .get(0); - DatasetType dst = (DatasetType) icatWS - .search(sessionId, "DatasetType") - .get(0); + Investigation inv = (Investigation) icatWS.search(sessionId, "Investigation").get(0); + DatasetType dst = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); Dataset ds = new Dataset(); ds.setName("Big ds"); @@ -634,39 +519,17 @@ protected void raceTest() throws Exception { ds.setId(dsid); for (int i = 0; i < 30; i++) { - testingClient.put( - sessionId, - Files.newInputStream(path), - "uploaded_file" + i, - dsid, - supportedDatafileFormat.getId(), - "A rather splendid datafile", - 201 - ); + testingClient.put(sessionId, Files.newInputStream(path), "uploaded_file" + i, dsid, + supportedDatafileFormat.getId(), "A rather splendid datafile", 201); } - testingClient.archive( - sessionId, - new DataSelection().addDataset(dsid), - 204 - ); + testingClient.archive(sessionId, new DataSelection().addDataset(dsid), 204); logTime("Put and archive calls"); waitForIds(300); logTime("Archive complete"); - testingClient.restore( - sessionId, - new DataSelection().addDataset(dsid), - 204 - ); - while ( - testingClient.getStatus( - sessionId, - new DataSelection().addDataset(dsid), - 200 - ) != - Status.ONLINE - ) { + testingClient.restore(sessionId, new DataSelection().addDataset(dsid), 204); + while (testingClient.getStatus(sessionId, new DataSelection().addDataset(dsid), 200) != Status.ONLINE) { Thread.sleep(1000); } logTime("Marked online"); @@ -675,95 +538,57 @@ protected void raceTest() throws Exception { assertTrue(stat.getOpItems().toString(), stat.getOpItems().isEmpty()); - testingClient.delete( - sessionId, - new DataSelection().addDataset(dsid), - 204 - ); + testingClient.delete(sessionId, new DataSelection().addDataset(dsid), 204); icatWS.delete(sessionId, ds); waitForIds(300); logTime("Deleted"); Files.delete(path); + } public void getDatafileIdsTest() throws Exception { - List ids = testingClient.getDatafileIds( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), - 200 - ); + + List ids = testingClient.getDatafileIds(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDatafile(datafileIds.get(0)), 200); assertEquals(2, ids.size()); assertTrue(ids.contains(datafileIds.get(0))); assertTrue(ids.contains(datafileIds.get(1))); - ids = - testingClient.getDatafileIds( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - 200 - ); + ids = testingClient.getDatafileIds(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 200); assertEquals(1, ids.size()); assertTrue(ids.contains(datafileIds.get(0))); - ids = - testingClient.getDatafileIds( - sessionId, - new DataSelection().addInvestigation(investigationId), - 200 - ); + ids = testingClient.getDatafileIds(sessionId, new DataSelection().addInvestigation(investigationId), 200); assertEquals(4, ids.size()); for (Long id : datafileIds) { assertTrue(ids.contains(id)); } + } public void bigDataSelectionTest() throws Exception { String icatUrl = testingClient.getIcatUrl(200).toExternalForm(); ICAT restIcat = new org.icatproject.icat.client.ICAT(icatUrl); - try ( - JsonReader parser = Json.createReader( - new ByteArrayInputStream(restIcat.getProperties().getBytes()) - ) - ) { - assertEquals( - "maxEntities must have a fixed value in the icat.server for test to be useful", - 20, - parser.readObject().getInt("maxEntities") - ); + try (JsonReader parser = Json.createReader(new ByteArrayInputStream(restIcat.getProperties().getBytes()))) { + assertEquals("maxEntities must have a fixed value in the icat.server for test to be useful", 20, + parser.readObject().getInt("maxEntities")); } - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); List idList = new ArrayList<>(); for (int i = 0; i < 45; i++) { - Long dfid = testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp + i, - datasetIds.get(0), - supportedDatafileFormat.getId(), - "A rather splendid datafile", - 201 - ); + Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp + i, datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); idList.add(dfid); } waitForIds(); - List idList2 = testingClient.getDatafileIds( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), - 200 - ); + List idList2 = testingClient.getDatafileIds(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDatafile(datafileIds.get(0)), 200); assertEquals(47, idList2.size()); for (Long id : idList) { assertTrue(idList2.contains(id)); @@ -780,184 +605,69 @@ public void bigDataSelectionTest() throws Exception { assertTrue(idList2.contains(idList.get(i))); } - idList2 = - testingClient.getDatafileIds( - sessionId, - new DataSelection().addInvestigation(investigationId), - 200 - ); + idList2 = testingClient.getDatafileIds(sessionId, new DataSelection().addInvestigation(investigationId), 200); assertEquals(49, idList2.size()); } public void cloningTest() throws Exception { - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); - assertEquals( - 104, - testingClient.getSize( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 200 - ) - ); + assertEquals(104, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 200)); Session s = icat.getSession(sessionId); Map m = new HashMap<>(); m.put("name", "newOne"); long dfid = datafileIds.get(0); long ndfid = s.cloneEntity("Datafile", dfid, m); - Datafile df = (Datafile) icatWS.get( - sessionId, - "Datafile df INCLUDE df.dataset.investigation.facility", - ndfid - ); + Datafile df = (Datafile) icatWS.get(sessionId, "Datafile df INCLUDE df.dataset.investigation.facility", ndfid); Dataset ds = df.getDataset(); Investigation inv = ds.getInvestigation(); Facility fac = inv.getFacility(); - paths.put( - "ids/" + - fac.getName() + - "/" + - inv.getName() + - "/" + - URLEncoder.encode(inv.getVisitId(), "UTF-8") + - "/" + - ds.getName() + - "/" + - df.getName(), - df.getLocation() - ); + paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + URLEncoder.encode(inv.getVisitId(), "UTF-8") + + "/" + ds.getName() + "/" + df.getName(), df.getLocation()); crcs.put(df.getLocation(), df.getChecksum()); fsizes.put(df.getLocation(), df.getFileSize()); - contents.put( - df.getLocation(), - "df1 test content very compressible very compressible" - ); + contents.put(df.getLocation(), "df1 test content very compressible very compressible"); ids.put(df.getLocation(), df.getId()); - assertEquals( - 156, - testingClient.getSize( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 200 - ) - ); - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 0, - 200 - ) - ) { + assertEquals(156, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 200)); + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, 200)) { List dfids = new ArrayList<>(datafileIds.subList(0, 2)); dfids.add(ndfid); checkZipStream(stream, dfids, 57, 0); } long dsid = datasetIds.get(0); assertEquals(2, getDirOnFastStorage(dsid).toFile().list().length); - assertEquals( - "[3]", - s.search( - "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + - dsid - ) - ); - testingClient.delete( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - 204 - ); - assertEquals( - 2, - getDirOnFastStorage(datasetIds.get(0)).toFile().list().length - ); - assertEquals( - "[2]", - s.search( - "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + - dsid - ) - ); - testingClient.delete( - sessionId, - new DataSelection().addDatafile(datafileIds.get(1)), - 204 - ); - assertEquals( - 1, - getDirOnFastStorage(datasetIds.get(0)).toFile().list().length - ); - assertEquals( - "[1]", - s.search( - "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + - dsid - ) - ); - testingClient.delete( - sessionId, - new DataSelection().addDatafile(ndfid), - 204 - ); + assertEquals("[3]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + assertEquals(2, getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); + assertEquals("[2]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(1)), 204); + assertEquals(1, getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); + assertEquals("[1]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); + testingClient.delete(sessionId, new DataSelection().addDatafile(ndfid), 204); assertFalse(getDirOnFastStorage(datasetIds.get(0)).toFile().exists()); - assertEquals( - "[0]", - s.search( - "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + - dsid - ) - ); + assertEquals("[0]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); } public void reliabilityTest() throws Exception { - testingClient.restore( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - 204 - ); + + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); waitForIds(); - Long dfid1 = testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - "A rather splendid datafile", - 201 - ); - - testingClient.archive( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - 204 - ); + Long dfid1 = testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file2_" + timestamp, + datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", 201); + + testingClient.archive(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); waitForIds(); - assertTrue( - testingClient - .getServiceStatus(sessionId, 200) - .getFailures() - .isEmpty() - ); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures().isEmpty()); setup.setReliability(0.); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(dfid1), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(dfid1), Flag.NONE, + 200); try { while (!testingClient.isPrepared(preparedId, null)) { @@ -966,79 +676,38 @@ public void reliabilityTest() throws Exception { } catch (Exception e) { System.out.println(e); assertEquals("Restore failed", e.getMessage()); - Set failures = testingClient - .getServiceStatus(sessionId, 200) - .getFailures(); + Set failures = testingClient.getServiceStatus(sessionId, 200).getFailures(); assertEquals(1, failures.size()); setup.setReliability(1.); testingClient.reset(preparedId, 204); - assertTrue( - testingClient - .getServiceStatus(sessionId, 200) - .getFailures() - .isEmpty() - ); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures().isEmpty()); while (!testingClient.isPrepared(preparedId, null)) { Thread.sleep(1000); } } setup.setReliability(0.); - Long dfid2 = testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - "An even better datafile", - "7.1.3", - new Date(420000), - new Date(42000), - 201 - ); + Long dfid2 = testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file3_" + timestamp, + datasetIds.get(0), supportedDatafileFormat.getId(), "An even better datafile", "7.1.3", + new Date(420000), new Date(42000), 201); waitForIds(); - System.out.println( - testingClient.getStatus( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDatafile(dfid2), - 200 - ) - ); + System.out.println(testingClient.getStatus(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 200)); setup.setReliability(1.); - testingClient.restore( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDatafile(dfid2), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 204); waitForIds(); - System.out.println( - testingClient.getStatus( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDatafile(dfid2), - 200 - ) - ); + System.out.println(testingClient.getStatus(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 200)); + } protected void reliabilityTest2() throws Exception { - DataSelection dsel = new DataSelection() - .addDatafile(datafileIds.get(0)); + DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(0)); testingClient.archive(sessionId, dsel, 204); waitForIds(); setup.setReliability(0.); - String preparedId = testingClient.prepareData( - sessionId, - dsel, - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, dsel, Flag.NONE, 200); try { while (!testingClient.isPrepared(preparedId, null)) { @@ -1059,11 +728,11 @@ protected void reliabilityTest2() throws Exception { while (!testingClient.isPrepared(preparedId, null)) { Thread.sleep(1000); } + } protected void reliabilityTest3() throws Exception { - DataSelection dsel = new DataSelection() - .addDatafile(datafileIds.get(0)); + DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(0)); testingClient.archive(sessionId, dsel, 204); waitForIds(); @@ -1071,9 +740,7 @@ protected void reliabilityTest3() throws Exception { testingClient.restore(sessionId, dsel, 204); try { - while ( - testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE - ) { + while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { Thread.sleep(1000); } fail("Should throw an error"); @@ -1084,9 +751,7 @@ protected void reliabilityTest3() throws Exception { setup.setReliability(1.); testingClient.restore(sessionId, dsel, 204); try { - while ( - testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE - ) { + while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { Thread.sleep(1000); } } catch (Exception e) { @@ -1095,24 +760,19 @@ protected void reliabilityTest3() throws Exception { testingClient.reset(sessionId, dsel, 204); testingClient.restore(sessionId, dsel, 204); - while ( - testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE - ) { + while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { Thread.sleep(1000); } } protected void isPreparedTest() throws Exception { + int numDs = 30; int numDf = 19; - Investigation inv = (Investigation) icatWS - .search(sessionId, "Investigation INCLUDE Facility") - .get(0); + Investigation inv = (Investigation) icatWS.search(sessionId, "Investigation INCLUDE Facility").get(0); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS - .search(sessionId, "DatasetType") - .get(0); + DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); Facility fac = inv.getFacility(); String key = setup.getKey(); @@ -1134,6 +794,7 @@ protected void isPreparedTest() throws Exception { String dsLoc = invLoc + dsid + "/"; ds.setId(dsid); for (int j = 0; j < numDf; j++) { + Datafile df = new Datafile(); df.setName("a/df1_" + j); df.setLocation(dsLoc + UUID.randomUUID()); @@ -1150,12 +811,7 @@ protected void isPreparedTest() throws Exception { logTime("Put calls"); - String preparedId = testingClient.prepareData( - sessionId, - dsel, - Flag.ZIP, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, dsel, Flag.ZIP, 200); logTime("Data prepared"); while (!testingClient.isPrepared(preparedId, 200)) { @@ -1173,11 +829,7 @@ protected void isPreparedTest() throws Exception { logTime("Read " + l + " bytes"); } - testingClient.archive( - sessionId, - new DataSelection().addDataset(dsid), - 204 - ); + testingClient.archive(sessionId, new DataSelection().addDataset(dsid), 204); waitForIds(300); logTime(null); @@ -1202,4 +854,5 @@ protected void isPreparedTest() throws Exception { logTime("Read " + l + " bytes"); } } + } diff --git a/src/test/java/org/icatproject/ids/integration/one/BaseTests.java b/src/test/java/org/icatproject/ids/integration/one/BaseTests.java index 54663ed2..b86f6faa 100644 --- a/src/test/java/org/icatproject/ids/integration/one/BaseTests.java +++ b/src/test/java/org/icatproject/ids/integration/one/BaseTests.java @@ -1,10 +1,11 @@ package org.icatproject.ids.integration.one; -import org.icatproject.ids.integration.BaseTest; -import org.icatproject.ids.integration.util.Setup; import org.junit.BeforeClass; import org.junit.Test; +import org.icatproject.ids.integration.BaseTest; +import org.icatproject.ids.integration.util.Setup; + public class BaseTests extends BaseTest { @BeforeClass @@ -37,4 +38,5 @@ public void bigDataSelectionTest() throws Exception { public void cloningTest() throws Exception { super.cloningTest(); } + } diff --git a/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java index 70213c1d..d6cc867a 100644 --- a/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java @@ -1,9 +1,13 @@ package org.icatproject.ids.integration.one; -import static org.junit.Assert.assertEquals; - import java.io.InputStream; import java.util.Collections; + +import static org.junit.Assert.assertEquals; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ids.integration.BaseTest; @@ -11,9 +15,6 @@ import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; /* * Issue #63: Internal error is raised trying to restore a dataset @@ -37,33 +38,21 @@ public static void setup() throws Exception { public void createBogusFiles() throws Exception { long timestamp = System.currentTimeMillis(); - Dataset ds1 = (Dataset) icatWS.get( - sessionId, - "Dataset", - datasetIds.get(0) - ); + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get( - sessionId, - "Dataset", - datasetIds.get(1) - ); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get( - sessionId, - "Dataset", - datasetIds.get(2) - ); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -77,76 +66,59 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); - - try ( - InputStream stream = testingClient.getData( - sessionId, - selection, - Flag.NONE, - 0, - null - ) - ) { + + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } + } @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); + } @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); - - try ( - InputStream stream = testingClient.getData( - sessionId, - selection, - Flag.NONE, - 0, - null - ) - ) { + + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } + } @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); + } @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); - - try ( - InputStream stream = testingClient.getData( - sessionId, - selection, - Flag.NONE, - 0, - 404 - ) - ) { + + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } + } @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); + } + } diff --git a/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java index 93aa25ba..5f5b4881 100644 --- a/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java @@ -1,19 +1,20 @@ package org.icatproject.ids.integration.one; +import java.nio.file.Files; +import java.nio.file.Path; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import org.junit.BeforeClass; +import org.junit.Test; -import java.nio.file.Files; -import java.nio.file.Path; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Status; -import org.junit.BeforeClass; -import org.junit.Test; public class DeleteTest extends BaseTest { @@ -25,17 +26,9 @@ public static void setup() throws Exception { @Test public void deleteDatafileTest() throws Exception { - DataSelection dsel = new DataSelection() - .addDatafile(datafileIds.get(3)); - assertEquals( - Status.ONLINE, - testingClient.getStatus(sessionId, dsel, 200) - ); - testingClient.delete( - sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), - 204 - ); + DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(3)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); try { testingClient.getStatus(sessionId, dsel, 404); fail(); @@ -51,16 +44,10 @@ public void deleteDatafileTest() throws Exception { public void deleteDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); DataSelection dsel = new DataSelection().addDataset(datasetIds.get(1)); - assertEquals( - Status.ONLINE, - testingClient.getStatus(sessionId, dsel, 200) - ); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); assertTrue(Files.exists(dirOnFastStorage)); testingClient.delete(sessionId, dsel, 204); - assertEquals( - Status.ONLINE, - testingClient.getStatus(sessionId, dsel, 200) - ); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); assertFalse(Files.exists(dirOnFastStorage)); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java index 0be497b7..16026ef5 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java @@ -1,9 +1,12 @@ package org.icatproject.ids.integration.one; -import static org.junit.Assert.assertEquals; - import java.io.InputStream; import java.util.Arrays; + +import static org.junit.Assert.assertEquals; +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.Datafile; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; @@ -12,8 +15,6 @@ import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class GetDataExplicitTest extends BaseTest { @@ -28,23 +29,11 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = - (Datafile) icatWS.get( - sessionId, - "Datafile INCLUDE 1", - datafileIds.get(0) - ); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals( - 209L, - testingClient.getSize( - sessionId, - new DataSelection().addDatafiles(datafileIds), - 200 - ) - ); + assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -58,23 +47,12 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = - (Datafile) icatWS.get( - sessionId, - "Datafile INCLUDE 1", - datafileIds.get(0) - ); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection() - .addDatafiles(datafileIds); - String preparedId = testingClient.prepareData( - sessionId, - selection, - Flag.NONE, - 200 - ); + DataSelection selection = new DataSelection().addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -86,252 +64,114 @@ public void getSizePreparedId() throws Exception { @Test public void getSizes1() throws Exception { - assertEquals( - 208L, - testingClient.getSize( - sessionId, - new DataSelection().addDatafiles(datafileIds), - 200 - ) - ); - assertEquals( - 208L, - testingClient.getSize( - sessionId, - new DataSelection().addDatasets(datasetIds), - 200 - ) - ); - assertEquals( - 208L, - testingClient.getSize( - sessionId, - new DataSelection().addInvestigation(investigationId), - 200 - ) - ); - assertEquals( - 208L, - testingClient.getSize( - sessionId, - new DataSelection() - .addInvestigation(investigationId) - .addDatafiles(datafileIds), - 200 - ) - ); - assertEquals( - 0L, - testingClient.getSize( - sessionId, - new DataSelection().addDataset(datasetIds.get(2)), - 200 - ) - ); + assertEquals(208L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(208L, testingClient.getSize(sessionId, new DataSelection().addDatasets(datasetIds), 200)); + assertEquals(208L, + testingClient.getSize(sessionId, new DataSelection().addInvestigation(investigationId), 200)); + assertEquals(208L, testingClient.getSize(sessionId, + new DataSelection().addInvestigation(investigationId).addDatafiles(datafileIds), 200)); + assertEquals(0L, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(2)), 200)); } @Test(expected = NotFoundException.class) public void getSizesBad() throws Exception { - testingClient.getSize( - sessionId, - new DataSelection().addDatafile(563L), - 404 - ); + testingClient.getSize(sessionId, new DataSelection().addDatafile(563L), 404); } @Test(expected = NotFoundException.class) public void getSizesBad2() throws Exception { - testingClient.getSize( - sessionId, - new DataSelection().addDatafile(563L).addDatafile(564L), - 404 - ); + testingClient.getSize(sessionId, new DataSelection().addDatafile(563L).addDatafile(564L), 404); } @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try ( - InputStream z = testingClient.getData( - "bad preparedId format", - 0, - 400 - ) - ) {} + try (InputStream z = testingClient.getData("bad preparedId format", 0, 400)) { + } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try ( - InputStream z = testingClient.getData( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 0, - 403 - ) - ) {} + try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { + } } @Test public void correctBehaviourTestNone() throws Exception { - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 0, 200)) { checkStream(stream, datafileIds.get(0)); } } @Test public void correctBehaviourTestCompress() throws Exception { - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.COMPRESS, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.COMPRESS, 0, 200)) { checkZipStream(stream, datafileIds, 36L, 0); } - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.COMPRESS, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.COMPRESS, 0, 200)) { checkStream(stream, datafileIds.get(0)); } } @Test public void correctBehaviourTestZip() throws Exception { - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.ZIP, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.ZIP, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } @Test public void correctBehaviourTestZipAndCompress() throws Exception { - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.ZIP_AND_COMPRESS, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.ZIP_AND_COMPRESS, 0, 200)) { checkZipStream(stream, datafileIds, 36L, 0); } - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP_AND_COMPRESS, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP_AND_COMPRESS, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 36L, 0); } } @Test public void correctBehaviourInvestigation() throws Exception { - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addInvestigation(investigationId), - Flag.NONE, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addInvestigation(investigationId), Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } @Test public void correctBehaviourInvestigations() throws Exception { - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection() - .addInvestigations(Arrays.asList(investigationId)), - Flag.NONE, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addInvestigations(Arrays.asList(investigationId)), Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } + } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java index 294f580b..9d844536 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java @@ -1,16 +1,17 @@ package org.icatproject.ids.integration.one; +import java.io.InputStream; + import static org.junit.Assert.assertEquals; +import org.junit.BeforeClass; +import org.junit.Test; -import java.io.InputStream; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class GetDataForPreparedIdTest extends BaseTest { @@ -24,59 +25,43 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try ( - InputStream z = testingClient.getData( - "bad preparedId format", - 0L, - 400 - ) - ) {} + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); do { Thread.sleep(500); } while (!testingClient.isPrepared(preparedId, 200)); - try (InputStream z = testingClient.getData(preparedId, -10L, 400)) {} + try (InputStream z = testingClient.getData(preparedId, -10L, 400)) { + } } @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try ( - InputStream z = testingClient.getData( - "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - 0L, - 404 - ) - ) {} + try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + } } @Test public void correctBehaviourNoOffsetTest() throws Exception { + for (Flag flag : Flag.values()) { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - flag, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), flag, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - try ( - InputStream stream = testingClient.getData(preparedId, 0, 200) - ) { + try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { if (flag == Flag.NONE || flag == Flag.COMPRESS) { checkStream(stream, datafileIds.get(0)); } else if (flag == Flag.ZIP) { @@ -89,14 +74,9 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() - throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 200 - ); + public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -109,12 +89,9 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -125,16 +102,10 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() - throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), - Flag.NONE, - 200 - ); + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -147,13 +118,11 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() @Test public void correctBehaviourWithOffsetTest() throws Exception { + for (Flag flag : Flag.values()) { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - flag, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), flag, 200); do { Thread.sleep(1000); @@ -161,22 +130,16 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the file twice, with and without an offset byte[] out = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] outOffset = getOutput( - testingClient.getData(preparedId, goodOffset, 206) - ); + byte[] outOffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the // offset - System.out.println( - flag + ": " + out.length + " " + outOffset.length - ); + System.out.println(flag + ": " + out.length + " " + outOffset.length); for (int i = 0; i < outOffset.length; i++) { - assertEquals( - "Byte offset: " + i, - (byte) outOffset[i], - (byte) out[i + goodOffset] - ); + assertEquals("Byte offset: " + i, (byte) outOffset[i], (byte) out[i + goodOffset]); } } + } + } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java index 9cfa31e5..d5db81b7 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java @@ -1,8 +1,11 @@ package org.icatproject.ids.integration.one; +import java.util.Arrays; + import static org.junit.Assert.assertEquals; +import org.junit.BeforeClass; +import org.junit.Test; -import java.util.Arrays; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -11,8 +14,6 @@ import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; import org.icatproject.ids.integration.util.client.TestingClient.Status; -import org.junit.BeforeClass; -import org.junit.Test; public class GetStatusExplicitTest extends BaseTest { @@ -39,61 +40,39 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus( - sessionId, - new DataSelection() - .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), - 404 - ); + testingClient.getStatus(sessionId, new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTestAnon() throws Exception { - testingClient.getStatus( - null, - new DataSelection() - .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), - 404 - ); + testingClient.getStatus(null, new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.getStatus( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), - 403 - ); + testingClient.getStatus(setup.getForbiddenSessionId(), new DataSelection().addDatafiles(datafileIds), 403); } @Test public void correctBehaviourTest() throws Exception { + Status status; do { Thread.sleep(1000); - status = - testingClient.getStatus( - sessionId, - new DataSelection().addDatafiles(datafileIds), - 200 - ); + status = testingClient.getStatus(sessionId, new DataSelection().addDatafiles(datafileIds), 200); System.out.println("*" + status + "*"); } while (status != Status.ONLINE); + } @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData( - sessionId, - selection, - Flag.NONE, - 200 - ); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } assertEquals(testingClient.getStatus(preparedId, 200), Status.ONLINE); } + } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java index d5d742bc..b870eba0 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java @@ -1,6 +1,10 @@ package org.icatproject.ids.integration.one; import java.util.Arrays; + +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -8,8 +12,6 @@ import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class GetStatusForPreparedIdTest extends BaseTest { @@ -31,55 +33,38 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient.prepareData( - sessionId, - new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), - Flag.NONE, - 404 - ); + testingClient + .prepareData(sessionId, + new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), + Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(99999L), - Flag.NONE, - 404 - ); + testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, + 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient.prepareData( - sessionId, - new DataSelection().addDataset(99999L), - Flag.NONE, - 404 - ); + testingClient + .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.prepareData( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 403 - ); + testingClient.prepareData(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } } + } diff --git a/src/test/java/org/icatproject/ids/integration/one/MiscTest.java b/src/test/java/org/icatproject/ids/integration/one/MiscTest.java index 0dc25b5a..3baf012c 100644 --- a/src/test/java/org/icatproject/ids/integration/one/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/MiscTest.java @@ -1,17 +1,18 @@ package org.icatproject.ids.integration.one; +import java.io.InputStream; + import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import org.junit.BeforeClass; +import org.junit.Test; -import java.io.InputStream; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.NotImplementedException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class MiscTest extends BaseTest { @@ -40,31 +41,17 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - assertTrue( - testingClient - .getServiceStatus(sessionId, 200) - .getOpItems() - .isEmpty() - ); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); waitForIds(); - assertTrue( - testingClient - .getServiceStatus(sessionId, 200) - .getOpItems() - .isEmpty() - ); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); @@ -73,21 +60,14 @@ public void correctBehaviourNoOffsetTest() throws Exception { @Test(expected = NotImplementedException.class) public void restoreNotAvailableTest() throws Exception { - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 501 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); waitForIds(); } @Test(expected = NotImplementedException.class) public void archiveNotAvailableTest() throws Exception { - testingClient.archive( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 501 - ); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); waitForIds(); } + } diff --git a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java index 2c30f38b..1943ae01 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java @@ -5,13 +5,15 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; public class PerformanceTest extends BaseTest { @@ -27,12 +29,7 @@ public static void beforeClass() throws Exception { int n = 100000; start = System.currentTimeMillis(); - try ( - OutputStream f = Files.newOutputStream( - infile, - StandardOpenOption.CREATE - ) - ) { + try (OutputStream f = Files.newOutputStream(infile, StandardOpenOption.CREATE)) { for (int i = 0; i < n; i++) { f.write(junk); } @@ -53,26 +50,12 @@ public static void afterClass() throws Exception { public void putOneFileTest() throws Exception { byte[] junk = new byte[1000]; start = System.currentTimeMillis(); - Long dfid = testingClient.put( - sessionId, - Files.newInputStream(infile), - "big_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - "A big datafile", - 201 - ); + Long dfid = testingClient.put(sessionId, Files.newInputStream(infile), "big_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "A big datafile", 201); ts("store file (put)"); - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafile(dfid), - Flag.NONE, - 0, - null - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(dfid), Flag.NONE, 0, + null)) { boolean first = true; while (stream.read(junk) > 0) { if (first) { @@ -94,31 +77,14 @@ private static void ts(String msg) { public void putAsPostOneFileTest() throws Exception { byte[] junk = new byte[1000]; start = System.currentTimeMillis(); - Long dfid = testingClient.putAsPost( - sessionId, - Files.newInputStream(infile), - "big2_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - "A rather splendid datafile", - null, - null, - null, - true, - 201 - ); + Long dfid = testingClient.putAsPost(sessionId, Files.newInputStream(infile), "big2_" + timestamp, + datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", null, null, null, + true, 201); ts("store file (post)"); int ntot = 0; - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafile(dfid), - Flag.NONE, - 0, - null - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(dfid), Flag.NONE, 0, + null)) { boolean first = true; int n; diff --git a/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java index 62d50701..acdf7be9 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java @@ -1,19 +1,20 @@ package org.icatproject.ids.integration.one; +import java.nio.file.Files; +import java.util.List; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import org.junit.BeforeClass; +import org.junit.Test; -import java.nio.file.Files; -import java.util.List; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class PrepareDataTest extends BaseTest { @@ -25,54 +26,36 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData( - "bad sessionId format", - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 400 - ); + testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 400); } @Test(expected = BadRequestException.class) public void noIdsTest() throws Exception { - testingClient.prepareData( - "bad sessionId format", - new DataSelection(), - Flag.NONE, - 400 - ); + testingClient.prepareData("bad sessionId format", new DataSelection(), Flag.NONE, 400); + } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.prepareData( - "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 403 - ); + testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); + } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); System.out.println(preparedId); assertNotNull(preparedId); } @Test public void prepareDataset() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -83,21 +66,14 @@ public void prepareDataset() throws Exception { Thread.sleep(1000); } - assertTrue( - Files.exists(setup.getPreparedCacheDir().resolve(preparedId)) - ); + assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareTwoDatasets() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -109,19 +85,14 @@ public void prepareTwoDatasets() throws Exception { Thread.sleep(1000); } - assertTrue( - Files.exists(setup.getPreparedCacheDir().resolve(preparedId)) - ); + assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareDatafile() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -131,21 +102,14 @@ public void prepareDatafile() throws Exception { Thread.sleep(1000); } - assertTrue( - Files.exists(setup.getPreparedCacheDir().resolve(preparedId)) - ); + assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareDatafileAndItsDataset() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection() - .addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), - Flag.NONE, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -156,8 +120,7 @@ public void prepareDatafileAndItsDataset() throws Exception { Thread.sleep(1000); } - assertTrue( - Files.exists(setup.getPreparedCacheDir().resolve(preparedId)) - ); + assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } + } diff --git a/src/test/java/org/icatproject/ids/integration/one/PutTest.java b/src/test/java/org/icatproject/ids/integration/one/PutTest.java index df2ea8cc..0a6dec0a 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PutTest.java @@ -1,20 +1,21 @@ package org.icatproject.ids.integration.one; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - import java.nio.file.Files; import java.nio.file.Path; import java.util.Date; import java.util.GregorianCalendar; import javax.xml.datatype.DatatypeFactory; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.Datafile; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; -import org.junit.BeforeClass; -import org.junit.Test; public class PutTest extends BaseTest { @@ -28,30 +29,18 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void putDirectoryTest() throws Exception { - testingClient.put( - sessionId, - null, - "junk", - datasetIds.get(0), - supportedDatafileFormat.getId(), - "", - 201 - ); + testingClient.put(sessionId, null, "junk", datasetIds.get(0), + supportedDatafileFormat.getId(), "", 201); } @Test public void putOneFileTest() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - Long dfid = testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - "A rather splendid datafile", - 201 - ); + Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); Datafile df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("A rather splendid datafile", df.getDescription()); @@ -60,19 +49,9 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileModTime()); assertTrue(Files.exists(dirOnFastStorage)); - dfid = - testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - "An even better datafile", - "7.1.3", - new Date(420000), - new Date(42000), - 201 - ); + dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -80,34 +59,22 @@ public void putOneFileTest() throws Exception { DatatypeFactory datatypeFactory = DatatypeFactory.newInstance(); GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTime(new Date(420000)); - assertEquals( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileCreateTime() - ); + assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileCreateTime()); gregorianCalendar.setTime(new Date(42000)); - assertEquals( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileModTime() - ); + assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileModTime()); + } @Test public void putAsPostOneFileTest() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - Long dfid = testingClient.putAsPost( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - "A rather splendid datafile", - null, - null, - null, - true, - 201 - ); + Long dfid = testingClient.putAsPost(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", null, null, null, true, 201); Datafile df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("A rather splendid datafile", df.getDescription()); @@ -116,20 +83,9 @@ public void putAsPostOneFileTest() throws Exception { assertNull(df.getDatafileModTime()); assertTrue(Files.exists(dirOnFastStorage)); - dfid = - testingClient.putAsPost( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - "An even better datafile", - "7.1.3", - new Date(420000), - new Date(42000), - false, - 201 - ); + dfid = testingClient.putAsPost(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "An even better datafile", "7.1.3", new Date(420000), new Date(42000), false, 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -137,14 +93,11 @@ public void putAsPostOneFileTest() throws Exception { DatatypeFactory datatypeFactory = DatatypeFactory.newInstance(); GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTime(new Date(420000)); - assertEquals( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileCreateTime() - ); + assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileCreateTime()); gregorianCalendar.setTime(new Date(42000)); - assertEquals( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileModTime() - ); + assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileModTime()); + } } diff --git a/src/test/java/org/icatproject/ids/integration/one/WriteTest.java b/src/test/java/org/icatproject/ids/integration/one/WriteTest.java index 5d06360e..9a7550a3 100644 --- a/src/test/java/org/icatproject/ids/integration/one/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/WriteTest.java @@ -1,11 +1,12 @@ package org.icatproject.ids.integration.one; +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.NotImplementedException; -import org.junit.BeforeClass; -import org.junit.Test; public class WriteTest extends BaseTest { @@ -17,10 +18,7 @@ public static void setup() throws Exception { @Test(expected = NotImplementedException.class) public void writeNotAvailableTest() throws Exception { - testingClient.write( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 501 - ); + testingClient.write(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); } + } diff --git a/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java b/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java index 59cac582..d5bf557e 100644 --- a/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java @@ -1,18 +1,19 @@ package org.icatproject.ids.integration.two; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; -import org.junit.BeforeClass; -import org.junit.Test; public class ArchiveTest extends BaseTest { @@ -24,37 +25,28 @@ public static void setup() throws Exception { @Test public void restoreThenArchiveDataset() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); + } @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.archive( - "bad sessionId format", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), - 400 - ); + testingClient.archive("bad sessionId format", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 400); } @Test @@ -64,10 +56,8 @@ public void noIdsTest() throws Exception { @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.archive( - "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), - 403 - ); + testingClient.archive("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 403); } + } diff --git a/src/test/java/org/icatproject/ids/integration/two/BaseTests.java b/src/test/java/org/icatproject/ids/integration/two/BaseTests.java index aabf59f9..5207604f 100644 --- a/src/test/java/org/icatproject/ids/integration/two/BaseTests.java +++ b/src/test/java/org/icatproject/ids/integration/two/BaseTests.java @@ -1,11 +1,12 @@ package org.icatproject.ids.integration.two; -import org.icatproject.ids.integration.BaseTest; -import org.icatproject.ids.integration.util.Setup; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; +import org.icatproject.ids.integration.BaseTest; +import org.icatproject.ids.integration.util.Setup; + public class BaseTests extends BaseTest { @BeforeClass @@ -59,4 +60,5 @@ public void cloningTest() throws Exception { public void isPreparedTest() throws Exception { super.isPreparedTest(); } + } diff --git a/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java index 2bbd70cb..3ff9adf1 100644 --- a/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java @@ -1,9 +1,5 @@ package org.icatproject.ids.integration.two; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.io.File; import java.io.InputStream; import java.nio.file.Files; @@ -11,6 +7,14 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ids.integration.BaseTest; @@ -19,9 +23,6 @@ import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; import org.icatproject.ids.integration.util.client.TestingClient.Status; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; /* * Issue #63: Internal error is raised trying to restore a dataset @@ -45,33 +46,22 @@ public static void setup() throws Exception { @Before public void createBogusFiles() throws Exception { - Dataset ds1 = (Dataset) icatWS.get( - sessionId, - "Dataset", - datasetIds.get(0) - ); + + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get( - sessionId, - "Dataset", - datasetIds.get(1) - ); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get( - sessionId, - "Dataset", - datasetIds.get(2) - ); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -81,97 +71,74 @@ public void createBogusFiles() throws Exception { datafileIds.add(dfb1.getId()); datafileIds.add(dfb2.getId()); datafileIds.add(dfb3.getId()); + } @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); - - try ( - InputStream stream = testingClient.getData( - sessionId, - selection, - Flag.NONE, - 0, - null - ) - ) { + + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } + } @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); + } @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); testingClient.restore(sessionId, selection, 204); waitForIds(); - assertEquals( - Status.ONLINE, - testingClient.getStatus(sessionId, selection, null) - ); - - try ( - InputStream stream = testingClient.getData( - sessionId, - selection, - Flag.NONE, - 0, - null - ) - ) { + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, null)); + + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } + } @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); + } @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.restore(sessionId, selection, 404); waitForIds(); - assertEquals( - Status.ONLINE, - testingClient.getStatus(sessionId, selection, 404) - ); - - try ( - InputStream stream = testingClient.getData( - sessionId, - selection, - Flag.NONE, - 0, - 404 - ) - ) { + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, 404)); + + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } + } @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); + } /* @@ -181,6 +148,7 @@ public void getSizeBogusFile() throws Exception { */ @Test public void putWriteArchiveRestore() throws Exception { + Long dsId = datasetIds.get(0); DataSelection selection = new DataSelection().addDataset(dsId); Path dirOnFastStorage = getDirOnFastStorage(dsId); @@ -189,15 +157,9 @@ public void putWriteArchiveRestore() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfulId = testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, - dsId, - supportedDatafileFormat.getId(), - "A rather splendid datafile", - 201 - ); + Long dfulId = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); Datafile dful = (Datafile) icatWS.get(sessionId, "Datafile", dfulId); testingClient.archive(sessionId, selection, 204); waitForIds(); @@ -210,25 +172,16 @@ public void putWriteArchiveRestore() throws Exception { File[] filesList = dirOnFastStorage.toFile().listFiles(); assertEquals(3, filesList.length); Set locations = new HashSet<>(); - Datafile df1 = (Datafile) icatWS.get( - sessionId, - "Datafile", - datafileIds.get(0) - ); + Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(0)); locations.add(getLocationFromDigest(df1.getId(), df1.getLocation())); - Datafile df2 = (Datafile) icatWS.get( - sessionId, - "Datafile", - datafileIds.get(1) - ); + Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(1)); locations.add(getLocationFromDigest(df2.getId(), df2.getLocation())); locations.add(getLocationFromDigest(dful.getId(), dful.getLocation())); for (File file : filesList) { - String location = setup - .getStorageDir() - .relativize(file.toPath()) - .toString(); + String location = setup.getStorageDir().relativize(file.toPath()).toString(); assertTrue(locations.contains(location)); } + } + } diff --git a/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java index cb86f3b1..d8179493 100644 --- a/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java @@ -1,12 +1,14 @@ package org.icatproject.ids.integration.two; import java.nio.file.Path; + +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataNotOnlineException; import org.icatproject.ids.integration.util.client.DataSelection; -import org.junit.BeforeClass; -import org.junit.Test; public class DeleteTest extends BaseTest { @@ -18,31 +20,20 @@ public static void setup() throws Exception { @Test(expected = DataNotOnlineException.class) public void deleteFromUnrestoredDataset() throws Exception { - testingClient.delete( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 503 - ); + testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 503); } @Test public void deleteDatafileFromRestoredDatasetTest() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(1)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); - testingClient.delete( - sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), - 204 - ); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); waitForIds(); checkZipFile(fileOnArchiveStorage, datafileIds.subList(2, 3), 36); @@ -53,22 +44,15 @@ public void deleteRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(1)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); checkPresent(fileOnArchiveStorage); - testingClient.delete( - sessionId, - new DataSelection().addDataset(datasetIds.get(1)), - 204 - ); + testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkAbsent(dirOnFastStorage); checkAbsent(fileOnArchiveStorage); } + } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java index 4e658722..1a3970a0 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java @@ -1,9 +1,12 @@ package org.icatproject.ids.integration.two; +import java.io.InputStream; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import org.junit.BeforeClass; +import org.junit.Test; -import java.io.InputStream; import org.icatproject.Datafile; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; @@ -13,8 +16,6 @@ import org.icatproject.ids.integration.util.client.IdsException; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class GetDataExplicitTest extends BaseTest { @@ -30,26 +31,15 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try ( - InputStream z = testingClient.getData( - "bad preparedId format", - 0L, - 400 - ) - ) {} + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try ( - InputStream z = testingClient.getData( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 0, - 403 - ) - ) {} + try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { + } } @Test @@ -57,23 +47,11 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = - (Datafile) icatWS.get( - sessionId, - "Datafile INCLUDE 1", - datafileIds.get(0) - ); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals( - 209L, - testingClient.getSize( - sessionId, - new DataSelection().addDatafiles(datafileIds), - 200 - ) - ); + assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -87,23 +65,12 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = - (Datafile) icatWS.get( - sessionId, - "Datafile INCLUDE 1", - datafileIds.get(0) - ); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection() - .addDatafiles(datafileIds); - String preparedId = testingClient.prepareData( - sessionId, - selection, - Flag.NONE, - 200 - ); + DataSelection selection = new DataSelection().addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -115,30 +82,17 @@ public void getSizePreparedId() throws Exception { @Test public void correctBehaviourTest() throws Exception { - try ( - InputStream z = testingClient.getData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 0, - 503 - ) - ) { + + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.NONE, + 0, 503)) { fail("Should have thrown exception"); } catch (IdsException e) { assertEquals(DataNotOnlineException.class, e.getClass()); } while (true) { - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 0, - null - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds, 57, 0); break; } catch (IdsException e) { @@ -150,45 +104,27 @@ public void correctBehaviourTest() throws Exception { @Test public void gettingDatafileRestoresItsDatasetTest() throws Exception { - try ( - InputStream z = testingClient.getData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(2)), - Flag.NONE, - 0, - null - ) - ) { + + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)), + Flag.NONE, 0, null)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), - Flag.NONE, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), + Flag.NONE, 0, 200)) { checkStream(stream, datafileIds.get(3)); } + } @Test public void gettingDatasetUsesCacheTest() throws Exception { - try ( - InputStream z = testingClient.getData( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 0, - null - ) - ) { + + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, null)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -196,49 +132,28 @@ public void gettingDatasetUsesCacheTest() throws Exception { waitForIds(); - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } + } @Test - public void gettingDatafileAndDatasetShouldRestoreBothDatasetsTest() - throws Exception { - try ( - InputStream z = testingClient.getData( - sessionId, - new DataSelection() - .addDatafile(datafileIds.get(2)) - .addDataset(datasetIds.get(0)), - Flag.NONE, - 0, - 503 - ) - ) { + public void gettingDatafileAndDatasetShouldRestoreBothDatasetsTest() throws Exception { + + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)) + .addDataset(datasetIds.get(0)), Flag.NONE, 0, 503)) { fail("Should throw exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatasets(datasetIds), - Flag.NONE, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatasets(datasetIds), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57, 0); } } + } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java index 93ea1695..97f66d5a 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java @@ -1,16 +1,17 @@ package org.icatproject.ids.integration.two; +import java.io.InputStream; + import static org.junit.Assert.assertEquals; +import org.junit.BeforeClass; +import org.junit.Test; -import java.io.InputStream; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class GetDataForPreparedIdTest extends BaseTest { @@ -24,50 +25,34 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try ( - InputStream z = testingClient.getData( - "bad preparedId format", - 0L, - 400 - ) - ) {} + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - try (InputStream z = testingClient.getData(preparedId, -10L, 400)) {} + try (InputStream z = testingClient.getData(preparedId, -10L, 400)) { + } } @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try ( - InputStream z = testingClient.getData( - "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - 0L, - 404 - ) - ) {} + try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + } } @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -79,14 +64,9 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() - throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 200 - ); + public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -95,16 +75,14 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkZipStream(stream, datafileIds, 57, 0); } + } @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -116,16 +94,10 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() - throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), - Flag.NONE, - 200 - ); + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { + + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -138,12 +110,9 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() @Test public void correctBehaviourWithOffsetTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -151,18 +120,13 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the zip file twice, with and without an offset byte[] zip = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] zipoffset = getOutput( - testingClient.getData(preparedId, goodOffset, 206) - ); + byte[] zipoffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the offset System.out.println(zip.length + " " + zipoffset.length); for (int i = 0; i < zipoffset.length; i++) { - assertEquals( - "Byte offset: " + i, - (byte) zipoffset[i], - (byte) zip[i + goodOffset] - ); + assertEquals("Byte offset: " + i, (byte) zipoffset[i], (byte) zip[i + goodOffset]); } } + } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java index 8cab8c9f..8ecd4429 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java @@ -1,8 +1,11 @@ package org.icatproject.ids.integration.two; +import java.util.Arrays; + import static org.junit.Assert.assertEquals; +import org.junit.BeforeClass; +import org.junit.Test; -import java.util.Arrays; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; @@ -10,8 +13,6 @@ import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; import org.icatproject.ids.integration.util.client.TestingClient.Status; -import org.junit.BeforeClass; -import org.junit.Test; public class GetStatusExplicitTest extends BaseTest { @@ -33,30 +34,20 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus( - sessionId, - new DataSelection() - .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), - 404 - ); + testingClient.getStatus(sessionId, + new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.getStatus( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), - 403 - ); + testingClient.getStatus(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), 403); } @Test public void correctBehaviourTest() throws Exception { - Status status = testingClient.getStatus( - sessionId, - new DataSelection().addDatafiles(datafileIds), - 200 - ); + Status status = testingClient.getStatus(sessionId, + new DataSelection().addDatafiles(datafileIds), 200); assertEquals(status, Status.ARCHIVED); waitForIds(); assertEquals(status, Status.ARCHIVED); @@ -64,14 +55,8 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData( - sessionId, - selection, - Flag.NONE, - 200 - ); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } @@ -86,29 +71,19 @@ public void getStatusPreparedIdTest() throws Exception { @Test public void restoringDatafileRestoresItsDatasetTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - Status status = testingClient.getStatus( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 200 - ); + Status status = testingClient.getStatus(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 200); assertEquals(Status.ONLINE, status); - status = - testingClient.getStatus( - sessionId, - new DataSelection().addDataset(datasetIds.get(1)), - 200 - ); + status = testingClient.getStatus(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 200); assertEquals(Status.ARCHIVED, status); } + } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java index 409e8cfa..c7daebea 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java @@ -1,6 +1,10 @@ package org.icatproject.ids.integration.two; import java.util.Arrays; + +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -8,8 +12,6 @@ import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class GetStatusForPreparedIdTest extends BaseTest { @@ -31,54 +33,37 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient.prepareData( - sessionId, - new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), - Flag.NONE, - 404 - ); + testingClient + .prepareData(sessionId, + new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), + Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(99999L), - Flag.NONE, - 404 - ); + testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, + 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient.prepareData( - sessionId, - new DataSelection().addDataset(99999L), - Flag.NONE, - 404 - ); + testingClient + .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.prepareData( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 403 - ); + testingClient.prepareData(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } } + } diff --git a/src/test/java/org/icatproject/ids/integration/two/MiscTest.java b/src/test/java/org/icatproject/ids/integration/two/MiscTest.java index a186f0db..fa5751d8 100644 --- a/src/test/java/org/icatproject/ids/integration/two/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/MiscTest.java @@ -1,16 +1,17 @@ package org.icatproject.ids.integration.two; +import java.io.InputStream; + import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import org.junit.BeforeClass; +import org.junit.Test; -import java.io.InputStream; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class MiscTest extends BaseTest { @@ -39,41 +40,23 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); - - assertFalse( - testingClient - .getServiceStatus(sessionId, 200) - .getOpItems() - .isEmpty() - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); + + assertFalse(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - assertTrue( - testingClient - .getServiceStatus(sessionId, 200) - .getOpItems() - .isEmpty() - ); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); waitForIds(); - assertTrue( - testingClient - .getServiceStatus(sessionId, 200) - .getOpItems() - .isEmpty() - ); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); } + } } diff --git a/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java index a660f5cc..891da989 100644 --- a/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java @@ -1,19 +1,20 @@ package org.icatproject.ids.integration.two; +import java.nio.file.Path; +import java.util.List; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import org.junit.BeforeClass; +import org.junit.Test; -import java.nio.file.Path; -import java.util.List; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class PrepareDataTest extends BaseTest { @@ -27,12 +28,8 @@ public static void setup() throws Exception { public void prepareArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -48,17 +45,12 @@ public void prepareArchivedDataset() throws Exception { @Test public void prepareTwoArchivedDatasets() throws Exception { + Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -78,12 +70,8 @@ public void prepareTwoArchivedDatasets() throws Exception { public void prepareArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -100,14 +88,8 @@ public void prepareArchivedDatafile() throws Exception { public void prepareArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -123,42 +105,26 @@ public void prepareArchivedDatafileAndItsDataset() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData( - "bad sessionId format", - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 400 - ); + testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 400); } @Test public void noIdsTest() throws Exception { - testingClient.prepareData( - sessionId, - new DataSelection(), - Flag.NONE, - 200 - ); + testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, 200); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.prepareData( - "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 403 - ); + testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); + } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); assertNotNull(preparedId); } @@ -166,22 +132,14 @@ public void correctBehaviourTest() throws Exception { public void prepareRestoredDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -193,30 +151,20 @@ public void prepareRestoredDataset() throws Exception { @Test public void prepareTwoRestoredDatasets() throws Exception { + Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage1); checkPresent(dirOnFastStorage2); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -228,24 +176,17 @@ public void prepareTwoRestoredDatasets() throws Exception { @Test public void prepareRestoredDatafile() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -257,31 +198,23 @@ public void prepareRestoredDatafile() throws Exception { @Test public void prepareRestoredDatafileAndItsDataset() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore( - sessionId, - new DataSelection() - .addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection() - .addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } checkPresent(setup.getPreparedCacheDir().resolve(preparedId)); + } + } diff --git a/src/test/java/org/icatproject/ids/integration/two/PutTest.java b/src/test/java/org/icatproject/ids/integration/two/PutTest.java index 43712d0c..3ca3f8dd 100644 --- a/src/test/java/org/icatproject/ids/integration/two/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/PutTest.java @@ -1,22 +1,23 @@ package org.icatproject.ids.integration.two; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - import java.nio.file.Files; import java.nio.file.Path; import java.util.Date; import java.util.GregorianCalendar; import javax.xml.datatype.DatatypeFactory; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.Datafile; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataNotOnlineException; import org.icatproject.ids.integration.util.client.DataSelection; -import org.junit.BeforeClass; -import org.junit.Test; public class PutTest extends BaseTest { @@ -30,15 +31,9 @@ public static void setup() throws Exception { @Test(expected = DataNotOnlineException.class) public void putToUnrestoredDataset() throws Exception { - testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file1_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - null, - 503 - ); + + testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file1_" + + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), null, 503); } @Test @@ -46,25 +41,15 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfid = testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - "A rather splendid datafile", - 201 - ); + Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); waitForIds(); @@ -74,19 +59,9 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileCreateTime()); assertNull(df.getDatafileModTime()); - dfid = - testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - "An even better datafile", - "7.1.3", - new Date(420000), - new Date(42000), - 201 - ); + dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -94,28 +69,22 @@ public void putOneFileTest() throws Exception { DatatypeFactory datatypeFactory = DatatypeFactory.newInstance(); GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTime(new Date(420000)); - assertEquals( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileCreateTime() - ); + assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileCreateTime()); gregorianCalendar.setTime(new Date(42000)); - assertEquals( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileModTime() - ); + assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileModTime()); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); + } + } diff --git a/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java b/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java index f921996b..5122232e 100644 --- a/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java @@ -1,8 +1,5 @@ package org.icatproject.ids.integration.two; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - /* * Test various error conditions in the DsRestorer caused by ZIP files * in archive storage having unexpected content. @@ -14,13 +11,17 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InternalException; import org.icatproject.ids.integration.util.client.TestingClient.Status; -import org.junit.BeforeClass; -import org.junit.Test; public class RestoreErrorsTest extends BaseTest { @@ -36,25 +37,14 @@ public static void setup() throws Exception { * occur. */ private enum Defect { - NONE, - MISSING_ENTRY, - SPURIOUS_ENTRY, - DUPLICATE_ENTRY, + NONE, MISSING_ENTRY, SPURIOUS_ENTRY, DUPLICATE_ENTRY } private void cloneZip(Path archivepath, Defect defect) throws IOException { Path savepath = archivepath.getParent().resolve(".sav"); Files.move(archivepath, savepath); - try ( - ZipOutputStream zipout = new ZipOutputStream( - Files.newOutputStream(archivepath) - ) - ) { - try ( - ZipInputStream zipin = new ZipInputStream( - Files.newInputStream(savepath) - ) - ) { + try (ZipOutputStream zipout = new ZipOutputStream(Files.newOutputStream(archivepath))) { + try (ZipInputStream zipin = new ZipInputStream(Files.newInputStream(savepath))) { ZipEntry entry = zipin.getNextEntry(); boolean first = true; String entryName = ""; diff --git a/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java b/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java index 45709a01..2755fc9e 100644 --- a/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java @@ -1,11 +1,13 @@ package org.icatproject.ids.integration.two; import java.nio.file.Path; + +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; -import org.junit.BeforeClass; -import org.junit.Test; public class RestoreTest extends BaseTest { @@ -17,30 +19,23 @@ public static void setup() throws Exception { @Test public void restoreArchivedDataset() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); + } @Test public void restoreTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage1); @@ -49,32 +44,27 @@ public void restoreTwoArchivedDatasets() throws Exception { @Test public void restoreArchivedDatafile() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); + } @Test public void restoreArchivedDatafileAndItsDataset() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore( - sessionId, - new DataSelection() - .addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); + } } diff --git a/src/test/java/org/icatproject/ids/integration/two/WriteTest.java b/src/test/java/org/icatproject/ids/integration/two/WriteTest.java index 9bc5f6e0..c9367a43 100644 --- a/src/test/java/org/icatproject/ids/integration/two/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/WriteTest.java @@ -4,6 +4,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.UUID; + +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.DatasetType; @@ -14,8 +18,6 @@ import org.icatproject.ids.integration.util.client.DataNotOnlineException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; -import org.junit.BeforeClass; -import org.junit.Test; public class WriteTest extends BaseTest { @@ -31,6 +33,7 @@ public static void setup() throws Exception { */ @Test public void restoreThenWriteDataset() throws Exception { + Long dsId = datasetIds.get(0); Path dirOnFastStorage = getDirOnFastStorage(dsId); DataSelection selection = new DataSelection().addDataset(dsId); @@ -40,6 +43,7 @@ public void restoreThenWriteDataset() throws Exception { checkPresent(dirOnFastStorage); testingClient.write(sessionId, selection, 204); + } /** @@ -49,17 +53,12 @@ public void restoreThenWriteDataset() throws Exception { */ @Test public void storeThenWrite() throws Exception { + long timestamp = System.currentTimeMillis(); - Investigation inv = (Investigation) icatWS.get( - sessionId, - "Investigation INCLUDE Facility", - investigationId - ); + Investigation inv = (Investigation) icatWS.get(sessionId, "Investigation INCLUDE Facility", investigationId); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS - .search(sessionId, "DatasetType") - .get(0); + DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); Dataset ds = new Dataset(); ds.setName("dsWrite_" + timestamp); @@ -73,11 +72,7 @@ public void storeThenWrite() throws Exception { df.setName("dfWrite_" + timestamp); df.setLocation(dsLoc + UUID.randomUUID()); df.setDataset(ds); - writeToFile( - df, - "some really boring datafile test content", - setup.getKey() - ); + writeToFile(df, "some really boring datafile test content", setup.getKey()); Long dsId = ds.getId(); Path dirOnFastStorage = getDirOnFastStorage(dsId); @@ -92,6 +87,7 @@ public void storeThenWrite() throws Exception { ArrayList list = new ArrayList(); list.add(df.getId()); checkZipFile(fileOnArchiveStorage, list, 42); + } /** @@ -110,11 +106,8 @@ public void notOnlineTest() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.write( - "bad sessionId format", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), - 400 - ); + testingClient.write("bad sessionId format", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 400); } @Test @@ -124,10 +117,8 @@ public void noIdsTest() throws Exception { @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.write( - "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), - 403 - ); + testingClient.write("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 403); } + } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java b/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java index 6cd575f0..75557414 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java @@ -1,18 +1,19 @@ package org.icatproject.ids.integration.twodf; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; -import org.junit.BeforeClass; -import org.junit.Test; public class ArchiveTest extends BaseTest { @@ -24,37 +25,28 @@ public static void setup() throws Exception { @Test public void restoreThenArchiveDataset() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); + } @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.archive( - "bad sessionId format", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), - 400 - ); + testingClient.archive("bad sessionId format", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 400); } @Test @@ -64,10 +56,8 @@ public void noIdsTest() throws Exception { @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.archive( - "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), - 403 - ); + testingClient.archive("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 403); } + } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/BaseTests.java b/src/test/java/org/icatproject/ids/integration/twodf/BaseTests.java index c47c7ba3..aa9d2157 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/BaseTests.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/BaseTests.java @@ -1,11 +1,12 @@ package org.icatproject.ids.integration.twodf; -import org.icatproject.ids.integration.BaseTest; -import org.icatproject.ids.integration.util.Setup; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; +import org.icatproject.ids.integration.BaseTest; +import org.icatproject.ids.integration.util.Setup; + public class BaseTests extends BaseTest { @BeforeClass @@ -59,4 +60,5 @@ public void cloningTest() throws Exception { public void isPreparedTest() throws Exception { super.isPreparedTest(); } + } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java index 983aaa36..670a071a 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java @@ -1,9 +1,5 @@ package org.icatproject.ids.integration.twodf; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.io.File; import java.io.InputStream; import java.nio.file.Files; @@ -11,6 +7,14 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ids.integration.BaseTest; @@ -19,9 +23,6 @@ import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; import org.icatproject.ids.integration.util.client.TestingClient.Status; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; /* * Issue #63: Internal error is raised trying to restore a dataset @@ -45,33 +46,22 @@ public static void setup() throws Exception { @Before public void createBogusFiles() throws Exception { - Dataset ds1 = (Dataset) icatWS.get( - sessionId, - "Dataset", - datasetIds.get(0) - ); + + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get( - sessionId, - "Dataset", - datasetIds.get(1) - ); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get( - sessionId, - "Dataset", - datasetIds.get(2) - ); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -81,97 +71,74 @@ public void createBogusFiles() throws Exception { datafileIds.add(dfb1.getId()); datafileIds.add(dfb2.getId()); datafileIds.add(dfb3.getId()); + } @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); - - try ( - InputStream stream = testingClient.getData( - sessionId, - selection, - Flag.NONE, - 0, - null - ) - ) { + + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } + } @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); + } @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); testingClient.restore(sessionId, selection, 204); waitForIds(); - assertEquals( - Status.ONLINE, - testingClient.getStatus(sessionId, selection, null) - ); - - try ( - InputStream stream = testingClient.getData( - sessionId, - selection, - Flag.NONE, - 0, - null - ) - ) { + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, null)); + + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } + } @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); + } @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.restore(sessionId, selection, 404); waitForIds(); - assertEquals( - Status.ONLINE, - testingClient.getStatus(sessionId, selection, 404) - ); - - try ( - InputStream stream = testingClient.getData( - sessionId, - selection, - Flag.NONE, - 0, - 404 - ) - ) { + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, 404)); + + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } + } @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); + } /* @@ -181,6 +148,7 @@ public void getSizeBogusFile() throws Exception { */ @Test public void putWriteArchiveRestore() throws Exception { + Long dsId = datasetIds.get(0); DataSelection selection = new DataSelection().addDataset(dsId); Path dirOnFastStorage = getDirOnFastStorage(dsId); @@ -189,15 +157,9 @@ public void putWriteArchiveRestore() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfulId = testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, - dsId, - supportedDatafileFormat.getId(), - "A rather splendid datafile", - 201 - ); + Long dfulId = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); Datafile dful = (Datafile) icatWS.get(sessionId, "Datafile", dfulId); testingClient.archive(sessionId, selection, 204); waitForIds(); @@ -210,25 +172,16 @@ public void putWriteArchiveRestore() throws Exception { File[] filesList = dirOnFastStorage.toFile().listFiles(); assertEquals(3, filesList.length); Set locations = new HashSet<>(); - Datafile df1 = (Datafile) icatWS.get( - sessionId, - "Datafile", - datafileIds.get(0) - ); + Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(0)); locations.add(getLocationFromDigest(df1.getId(), df1.getLocation())); - Datafile df2 = (Datafile) icatWS.get( - sessionId, - "Datafile", - datafileIds.get(1) - ); + Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(1)); locations.add(getLocationFromDigest(df2.getId(), df2.getLocation())); locations.add(getLocationFromDigest(dful.getId(), dful.getLocation())); for (File file : filesList) { - String location = setup - .getStorageDir() - .relativize(file.toPath()) - .toString(); + String location = setup.getStorageDir().relativize(file.toPath()).toString(); assertTrue(locations.contains(location)); } + } + } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java index a5474857..063102fb 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java @@ -1,11 +1,13 @@ package org.icatproject.ids.integration.twodf; import java.nio.file.Path; + +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; -import org.junit.BeforeClass; -import org.junit.Test; public class DeleteTest extends BaseTest { @@ -17,30 +19,19 @@ public static void setup() throws Exception { @Test public void deleteFromUnrestoredDataset() throws Exception { - testingClient.delete( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); } @Test public void deleteDatafileFromRestoredDatasetTest() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(1)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); - testingClient.delete( - sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), - 204 - ); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); waitForIds(); } @@ -49,22 +40,15 @@ public void deleteRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(1)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); checkPresent(fileOnArchiveStorage); - testingClient.delete( - sessionId, - new DataSelection().addDataset(datasetIds.get(1)), - 204 - ); + testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkAbsent(dirOnFastStorage); checkAbsent(fileOnArchiveStorage); } + } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java index 7a85bfaf..b6d9e772 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java @@ -1,9 +1,12 @@ package org.icatproject.ids.integration.twodf; +import java.io.InputStream; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import org.junit.BeforeClass; +import org.junit.Test; -import java.io.InputStream; import org.icatproject.Datafile; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; @@ -13,8 +16,6 @@ import org.icatproject.ids.integration.util.client.IdsException; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class GetDataExplicitTest extends BaseTest { @@ -30,26 +31,15 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try ( - InputStream z = testingClient.getData( - "bad preparedId format", - 0L, - 400 - ) - ) {} + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try ( - InputStream z = testingClient.getData( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 0, - 403 - ) - ) {} + try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { + } } @Test @@ -57,23 +47,11 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = - (Datafile) icatWS.get( - sessionId, - "Datafile INCLUDE 1", - datafileIds.get(0) - ); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals( - 209L, - testingClient.getSize( - sessionId, - new DataSelection().addDatafiles(datafileIds), - 200 - ) - ); + assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -87,23 +65,12 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = - (Datafile) icatWS.get( - sessionId, - "Datafile INCLUDE 1", - datafileIds.get(0) - ); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection() - .addDatafiles(datafileIds); - String preparedId = testingClient.prepareData( - sessionId, - selection, - Flag.NONE, - 200 - ); + DataSelection selection = new DataSelection().addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -115,30 +82,18 @@ public void getSizePreparedId() throws Exception { @Test public void correctBehaviourTest() throws Exception { - try ( - InputStream z = testingClient.getData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 0, - 503 - ) - ) { + + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.NONE, + 0, 503)) { + fail("Should have thrown exception"); } catch (IdsException e) { assertEquals(DataNotOnlineException.class, e.getClass()); } while (true) { - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 0, - null - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds, 57, 0); break; } catch (IdsException e) { @@ -150,47 +105,29 @@ public void correctBehaviourTest() throws Exception { @Test public void gettingDatafileDoesNotRestoreItsDatasetTest() throws Exception { - try ( - InputStream z = testingClient.getData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(2)), - Flag.NONE, - 0, - 503 - ) - ) { + + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)), + Flag.NONE, 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), - Flag.NONE, - 0, - 503 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), + Flag.NONE, 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } + } @Test public void gettingDatasetUsesCacheTest() throws Exception { - try ( - InputStream z = testingClient.getData( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 0, - 503 - ) - ) { + + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -198,51 +135,30 @@ public void gettingDatasetUsesCacheTest() throws Exception { waitForIds(); - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 0, - 200 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } + } @Test - public void gettingDatafileAndDatasetShouldNotRestoreBothDatasetsTest() - throws Exception { - try ( - InputStream z = testingClient.getData( - sessionId, - new DataSelection() - .addDatafile(datafileIds.get(2)) - .addDataset(datasetIds.get(0)), - Flag.NONE, - 0, - 503 - ) - ) { + public void gettingDatafileAndDatasetShouldNotRestoreBothDatasetsTest() throws Exception { + + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)) + .addDataset(datasetIds.get(0)), Flag.NONE, 0, 503)) { fail("Should throw exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try ( - InputStream stream = testingClient.getData( - sessionId, - new DataSelection().addDatasets(datasetIds), - Flag.NONE, - 0, - 503 - ) - ) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatasets(datasetIds), + Flag.NONE, 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } } + } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java index 9c054e27..c0fa6eef 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java @@ -1,16 +1,17 @@ package org.icatproject.ids.integration.twodf; +import java.io.InputStream; + import static org.junit.Assert.assertEquals; +import org.junit.BeforeClass; +import org.junit.Test; -import java.io.InputStream; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class GetDataForPreparedIdTest extends BaseTest { @@ -24,50 +25,34 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try ( - InputStream z = testingClient.getData( - "bad preparedId format", - 0L, - 400 - ) - ) {} + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - try (InputStream z = testingClient.getData(preparedId, -10L, 400)) {} + try (InputStream z = testingClient.getData(preparedId, -10L, 400)) { + } } @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try ( - InputStream z = testingClient.getData( - "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - 0L, - 404 - ) - ) {} + try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + } } @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -79,14 +64,9 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() - throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 200 - ); + public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -95,16 +75,14 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkZipStream(stream, datafileIds, 57, 0); } + } @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -116,16 +94,10 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() - throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), - Flag.NONE, - 200 - ); + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -138,12 +110,9 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() @Test public void correctBehaviourWithOffsetTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -151,18 +120,13 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the zip file twice, with and without an offset byte[] zip = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] zipoffset = getOutput( - testingClient.getData(preparedId, goodOffset, 206) - ); + byte[] zipoffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the offset System.out.println(zip.length + " " + zipoffset.length); for (int i = 0; i < zipoffset.length; i++) { - assertEquals( - "Byte offset: " + i, - (byte) zipoffset[i], - (byte) zip[i + goodOffset] - ); + assertEquals("Byte offset: " + i, (byte) zipoffset[i], (byte) zip[i + goodOffset]); } } + } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java index 07cd73fb..ed6bed60 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java @@ -1,8 +1,11 @@ package org.icatproject.ids.integration.twodf; +import java.util.Arrays; + import static org.junit.Assert.assertEquals; +import org.junit.BeforeClass; +import org.junit.Test; -import java.util.Arrays; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; @@ -10,8 +13,6 @@ import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; import org.icatproject.ids.integration.util.client.TestingClient.Status; -import org.junit.BeforeClass; -import org.junit.Test; public class GetStatusExplicitTest extends BaseTest { @@ -33,30 +34,20 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus( - sessionId, - new DataSelection() - .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), - 404 - ); + testingClient.getStatus(sessionId, + new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.getStatus( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), - 403 - ); + testingClient.getStatus(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), 403); } @Test public void correctBehaviourTest() throws Exception { - Status status = testingClient.getStatus( - sessionId, - new DataSelection().addDatafiles(datafileIds), - 200 - ); + Status status = testingClient.getStatus(sessionId, + new DataSelection().addDatafiles(datafileIds), 200); assertEquals(status, Status.ARCHIVED); waitForIds(); assertEquals(status, Status.ARCHIVED); @@ -64,14 +55,8 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData( - sessionId, - selection, - Flag.NONE, - 200 - ); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } @@ -85,31 +70,20 @@ public void getStatusPreparedIdTest() throws Exception { } @Test - public void restoringDatafileDoesNotRestoresItsDatasetTest() - throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + public void restoringDatafileDoesNotRestoresItsDatasetTest() throws Exception { + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - Status status = testingClient.getStatus( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 200 - ); + Status status = testingClient.getStatus(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 200); assertEquals(Status.ARCHIVED, status); - status = - testingClient.getStatus( - sessionId, - new DataSelection().addDataset(datasetIds.get(1)), - 200 - ); + status = testingClient.getStatus(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 200); assertEquals(Status.ARCHIVED, status); } + } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java index 041b9002..7889492e 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java @@ -1,6 +1,10 @@ package org.icatproject.ids.integration.twodf; import java.util.Arrays; + +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -8,8 +12,6 @@ import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.NotFoundException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class GetStatusForPreparedIdTest extends BaseTest { @@ -31,54 +33,37 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient.prepareData( - sessionId, - new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), - Flag.NONE, - 404 - ); + testingClient + .prepareData(sessionId, + new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), + Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(99999L), - Flag.NONE, - 404 - ); + testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, + 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient.prepareData( - sessionId, - new DataSelection().addDataset(99999L), - Flag.NONE, - 404 - ); + testingClient + .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.prepareData( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 403 - ); + testingClient.prepareData(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } } + } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java b/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java index 530d1527..011de0ad 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java @@ -1,16 +1,17 @@ package org.icatproject.ids.integration.twodf; +import java.io.InputStream; + import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import org.junit.BeforeClass; +import org.junit.Test; -import java.io.InputStream; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class MiscTest extends BaseTest { @@ -39,12 +40,8 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); var status = testingClient.getServiceStatus(sessionId, 200); //System.out.println("### twodf.MiscTest.correctBehaviourNoOffsetTest - status: " + status.toString()); @@ -55,23 +52,14 @@ public void correctBehaviourNoOffsetTest() throws Exception { Thread.sleep(1000); } - assertTrue( - testingClient - .getServiceStatus(sessionId, 200) - .getOpItems() - .isEmpty() - ); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); waitForIds(); - assertTrue( - testingClient - .getServiceStatus(sessionId, 200) - .getOpItems() - .isEmpty() - ); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); } + } } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java index 72df6190..386a7fff 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java @@ -1,19 +1,20 @@ package org.icatproject.ids.integration.twodf; +import java.nio.file.Path; +import java.util.List; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import org.junit.BeforeClass; +import org.junit.Test; -import java.nio.file.Path; -import java.util.List; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; import org.icatproject.ids.integration.util.client.TestingClient.Flag; -import org.junit.BeforeClass; -import org.junit.Test; public class PrepareDataTest extends BaseTest { @@ -27,12 +28,8 @@ public static void setup() throws Exception { public void prepareArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -48,17 +45,12 @@ public void prepareArchivedDataset() throws Exception { @Test public void prepareTwoArchivedDatasets() throws Exception { + Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -78,12 +70,8 @@ public void prepareTwoArchivedDatasets() throws Exception { public void prepareArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -100,14 +88,8 @@ public void prepareArchivedDatafile() throws Exception { public void prepareArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -123,42 +105,26 @@ public void prepareArchivedDatafileAndItsDataset() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData( - "bad sessionId format", - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 400 - ); + testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 400); } @Test public void noIdsTest() throws Exception { - testingClient.prepareData( - sessionId, - new DataSelection(), - Flag.NONE, - 200 - ); + testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, 200); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.prepareData( - "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 403 - ); + testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); + } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafiles(datafileIds), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); assertNotNull(preparedId); } @@ -166,22 +132,14 @@ public void correctBehaviourTest() throws Exception { public void prepareRestoredDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -193,30 +151,20 @@ public void prepareRestoredDataset() throws Exception { @Test public void prepareTwoRestoredDatasets() throws Exception { + Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage1); checkPresent(dirOnFastStorage2); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -228,24 +176,17 @@ public void prepareTwoRestoredDatasets() throws Exception { @Test public void prepareRestoredDatafile() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -257,31 +198,23 @@ public void prepareRestoredDatafile() throws Exception { @Test public void prepareRestoredDatafileAndItsDataset() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore( - sessionId, - new DataSelection() - .addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData( - sessionId, - new DataSelection() - .addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), - Flag.NONE, - 200 - ); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } checkPresent(setup.getPreparedCacheDir().resolve(preparedId)); + } + } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java b/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java index b4b71d28..b960cd6e 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java @@ -1,21 +1,22 @@ package org.icatproject.ids.integration.twodf; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - import java.nio.file.Files; import java.nio.file.Path; import java.util.Date; import java.util.GregorianCalendar; import javax.xml.datatype.DatatypeFactory; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.Datafile; import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; -import org.junit.BeforeClass; -import org.junit.Test; public class PutTest extends BaseTest { @@ -30,15 +31,8 @@ public static void setup() throws Exception { @Test // Works fine for datafile storage public void putToUnrestoredDataset() throws Exception { - testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file1_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - null, - 201 - ); + testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file1_" + + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), null, 201); } @Test @@ -46,25 +40,15 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfid = testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - "A rather splendid datafile", - 201 - ); + Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); waitForIds(); @@ -74,19 +58,9 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileCreateTime()); assertNull(df.getDatafileModTime()); - dfid = - testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, - datasetIds.get(0), - supportedDatafileFormat.getId(), - "An even better datafile", - "7.1.3", - new Date(420000), - new Date(42000), - 201 - ); + dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -94,28 +68,22 @@ public void putOneFileTest() throws Exception { DatatypeFactory datatypeFactory = DatatypeFactory.newInstance(); GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTime(new Date(420000)); - assertEquals( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileCreateTime() - ); + assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileCreateTime()); gregorianCalendar.setTime(new Date(42000)); - assertEquals( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileModTime() - ); + assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), + df.getDatafileModTime()); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); + } + } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java b/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java index 47450757..baf7be8d 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java @@ -1,14 +1,15 @@ package org.icatproject.ids.integration.twodf; -import static org.junit.Assert.assertTrue; - import java.nio.file.Files; import java.nio.file.Path; + +import static org.junit.Assert.assertTrue; +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; -import org.junit.BeforeClass; -import org.junit.Test; public class QueueTest extends BaseTest { @@ -35,16 +36,11 @@ public void multiOperationTest() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.put( - sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, - dsId, - supportedDatafileFormat.getId(), - "A rather splendid datafile", - 201 - ); + testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); testingClient.archive(sessionId, selection, 204); waitForIds(); } + } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java b/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java index 0bef830a..7125b9a4 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java @@ -1,11 +1,13 @@ package org.icatproject.ids.integration.twodf; import java.nio.file.Path; + +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; -import org.junit.BeforeClass; -import org.junit.Test; public class RestoreTest extends BaseTest { @@ -18,11 +20,7 @@ public static void setup() throws Exception { @Test public void restoreArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore( - sessionId, - new DataSelection().addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); } @@ -31,13 +29,8 @@ public void restoreArchivedDataset() throws Exception { public void restoreTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore( - sessionId, - new DataSelection() - .addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - 204 - ); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage1); @@ -46,32 +39,27 @@ public void restoreTwoArchivedDatasets() throws Exception { @Test public void restoreArchivedDatafile() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore( - sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), - 204 - ); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); + } @Test public void restoreArchivedDatafileAndItsDataset() throws Exception { + Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore( - sessionId, - new DataSelection() - .addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), - 204 - ); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); + } } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java b/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java index ca55e365..876af417 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java @@ -3,6 +3,10 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.UUID; + +import org.junit.BeforeClass; +import org.junit.Test; + import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.DatasetType; @@ -13,8 +17,6 @@ import org.icatproject.ids.integration.util.client.DataNotOnlineException; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.InsufficientPrivilegesException; -import org.junit.BeforeClass; -import org.junit.Test; public class WriteTest extends BaseTest { @@ -30,6 +32,7 @@ public static void setup() throws Exception { */ @Test public void restoreThenWriteDataset() throws Exception { + Long dsId = datasetIds.get(0); Path dirOnFastStorage = getDirOnFastStorage(dsId); DataSelection selection = new DataSelection().addDataset(dsId); @@ -39,6 +42,7 @@ public void restoreThenWriteDataset() throws Exception { checkPresent(dirOnFastStorage); testingClient.write(sessionId, selection, 204); + } /** @@ -48,17 +52,12 @@ public void restoreThenWriteDataset() throws Exception { */ @Test public void storeThenWrite() throws Exception { + long timestamp = System.currentTimeMillis(); - Investigation inv = (Investigation) icatWS.get( - sessionId, - "Investigation INCLUDE Facility", - investigationId - ); + Investigation inv = (Investigation) icatWS.get(sessionId, "Investigation INCLUDE Facility", investigationId); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS - .search(sessionId, "DatasetType") - .get(0); + DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); Dataset ds = new Dataset(); ds.setName("dsWrite_" + timestamp); @@ -72,11 +71,7 @@ public void storeThenWrite() throws Exception { df.setName("dfWrite_" + timestamp); df.setLocation(dsLoc + UUID.randomUUID()); df.setDataset(ds); - writeToFile( - df, - "some really boring datafile test content", - setup.getKey() - ); + writeToFile(df, "some really boring datafile test content", setup.getKey()); Long dsId = ds.getId(); Path dirOnFastStorage = getDirOnFastStorage(dsId); @@ -88,6 +83,7 @@ public void storeThenWrite() throws Exception { testingClient.write(sessionId, selection, 204); waitForIds(); checkPresent(dirOnArchiveStorage); + } /** @@ -106,11 +102,8 @@ public void notOnlineTest() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.write( - "bad sessionId format", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), - 400 - ); + testingClient.write("bad sessionId format", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 400); } @Test @@ -120,10 +113,8 @@ public void noIdsTest() throws Exception { @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { - testingClient.write( - "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), - 403 - ); + testingClient.write("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + new DataSelection().addDatafiles(Arrays.asList(1L, 2L)), 403); } + } diff --git a/src/test/java/org/icatproject/ids/integration/util/Setup.java b/src/test/java/org/icatproject/ids/integration/util/Setup.java index da06d235..2392805d 100644 --- a/src/test/java/org/icatproject/ids/integration/util/Setup.java +++ b/src/test/java/org/icatproject/ids/integration/util/Setup.java @@ -8,6 +8,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Properties; + import org.icatproject.ICAT; import org.icatproject.ids.TestUtils; import org.icatproject.ids.services.ICATGetter; @@ -46,6 +47,7 @@ public String getStorageUnit() { private String key; public Setup(String runPropertyFile) throws Exception { + // Test home directory String testHome = System.getProperty("testHome"); if (testHome == null) { @@ -55,17 +57,11 @@ public Setup(String runPropertyFile) throws Exception { // Start by reading the test properties Properties testProps = new Properties(); - InputStream is = - Setup.class.getClassLoader().getResourceAsStream("test.properties"); + InputStream is = Setup.class.getClassLoader().getResourceAsStream("test.properties"); try { testProps.load(is); } catch (Exception e) { - System.err.println( - "Problem loading test.properties: " + - e.getClass() + - " " + - e.getMessage() - ); + System.err.println("Problem loading test.properties: " + e.getClass() + " " + e.getMessage()); } setReliability(1.); @@ -76,29 +72,16 @@ public Setup(String runPropertyFile) throws Exception { String containerHome = System.getProperty("containerHome"); if (containerHome == null) { - System.err.println( - "containerHome is not defined as a system property" - ); + System.err.println("containerHome is not defined as a system property"); } long time = System.currentTimeMillis(); - ShellCommand sc = new ShellCommand( - "src/test/scripts/prepare_test.py", - "src/test/resources/" + runPropertyFile, - home.toString(), - containerHome, - serverUrl - ); + ShellCommand sc = new ShellCommand("src/test/scripts/prepare_test.py", "src/test/resources/" + runPropertyFile, + home.toString(), containerHome, serverUrl); System.out.println(sc.getStdout() + " " + sc.getStderr()); System.out.println( - "Setting up " + - runPropertyFile + - " took " + - (System.currentTimeMillis() - time) / - 1000. + - "seconds" - ); + "Setting up " + runPropertyFile + " took " + (System.currentTimeMillis() - time) / 1000. + "seconds"); // Having set up the ids.properties file read it find other things CheckedProperties runProperties = new CheckedProperties(); @@ -109,10 +92,8 @@ public Setup(String runPropertyFile) throws Exception { updownDir = home.resolve(testProps.getProperty("updownDir")); icatUrl = runProperties.getURL("icat.url"); ICAT icat = ICATGetter.getService(icatUrl.toString()); - rootSessionId = - TestUtils.login(icat, testProps.getProperty("login.root")); - forbiddenSessionId = - TestUtils.login(icat, testProps.getProperty("login.unauthorized")); + rootSessionId = TestUtils.login(icat, testProps.getProperty("login.root")); + forbiddenSessionId = TestUtils.login(icat, testProps.getProperty("login.unauthorized")); storageDir = runProperties.getPath("plugin.main.dir"); @@ -130,6 +111,7 @@ public Setup(String runPropertyFile) throws Exception { } storageUnit = storageUnitString.toUpperCase(); } + } public void setReliability(double d) throws IOException { @@ -174,4 +156,5 @@ public URL getIcatUrl() { public String getKey() { return key; } + } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/BadRequestException.java b/src/test/java/org/icatproject/ids/integration/util/client/BadRequestException.java index a5bf70d2..5f592906 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/BadRequestException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/BadRequestException.java @@ -6,4 +6,5 @@ public class BadRequestException extends IdsException { public BadRequestException(String msg) { super(msg); } + } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/DataNotOnlineException.java b/src/test/java/org/icatproject/ids/integration/util/client/DataNotOnlineException.java index d928792c..37cb084e 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/DataNotOnlineException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/DataNotOnlineException.java @@ -6,4 +6,5 @@ public class DataNotOnlineException extends IdsException { public DataNotOnlineException(String msg) { super(msg); } + } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java b/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java index 97c31eac..6527c191 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java @@ -2,6 +2,7 @@ import java.io.FilterInputStream; import java.io.IOException; + import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.impl.client.CloseableHttpClient; @@ -16,10 +17,8 @@ public class HttpInputStream extends FilterInputStream { * @param httpclient the CloseableHttpClient to close * @param response the response to use and ultimately close */ - public HttpInputStream( - CloseableHttpClient httpclient, - CloseableHttpResponse response - ) throws IllegalStateException, IOException { + public HttpInputStream(CloseableHttpClient httpclient, CloseableHttpResponse response) + throws IllegalStateException, IOException { super(response.getEntity().getContent()); this.response = response; this.httpclient = httpclient; @@ -54,4 +53,5 @@ public void close() throws IOException { throw exception; } } + } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/IdsException.java b/src/test/java/org/icatproject/ids/integration/util/client/IdsException.java index 34b1add2..0169a56e 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/IdsException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/IdsException.java @@ -6,4 +6,5 @@ public class IdsException extends Exception { public IdsException(String msg) { super(msg); } + } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/InsufficientPrivilegesException.java b/src/test/java/org/icatproject/ids/integration/util/client/InsufficientPrivilegesException.java index 5b4a4108..be6dcdd7 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/InsufficientPrivilegesException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/InsufficientPrivilegesException.java @@ -6,4 +6,5 @@ public class InsufficientPrivilegesException extends IdsException { public InsufficientPrivilegesException(String msg) { super(msg); } + } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/InsufficientStorageException.java b/src/test/java/org/icatproject/ids/integration/util/client/InsufficientStorageException.java index db6b2a20..c09bfba1 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/InsufficientStorageException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/InsufficientStorageException.java @@ -6,4 +6,5 @@ public class InsufficientStorageException extends IdsException { public InsufficientStorageException(String msg) { super(msg); } + } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/InternalException.java b/src/test/java/org/icatproject/ids/integration/util/client/InternalException.java index 74485718..fcc71b3e 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/InternalException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/InternalException.java @@ -6,4 +6,5 @@ public class InternalException extends IdsException { public InternalException(String msg) { super(msg); } + } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/NotFoundException.java b/src/test/java/org/icatproject/ids/integration/util/client/NotFoundException.java index 7f84f619..521baf31 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/NotFoundException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/NotFoundException.java @@ -6,4 +6,5 @@ public class NotFoundException extends IdsException { public NotFoundException(String msg) { super(msg); } + } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/NotImplementedException.java b/src/test/java/org/icatproject/ids/integration/util/client/NotImplementedException.java index 3f335584..7e12007d 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/NotImplementedException.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/NotImplementedException.java @@ -6,4 +6,5 @@ public class NotImplementedException extends IdsException { public NotImplementedException(String msg) { super(msg); } + } diff --git a/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java b/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java index 2b257cd3..7bb22543 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java @@ -1,13 +1,5 @@ package org.icatproject.ids.integration.util.client; -import static org.junit.Assert.fail; - -import jakarta.json.Json; -import jakarta.json.JsonException; -import jakarta.json.JsonNumber; -import jakarta.json.JsonObject; -import jakarta.json.JsonReader; -import jakarta.json.JsonValue; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; @@ -25,6 +17,14 @@ import java.util.Set; import java.util.zip.CRC32; import java.util.zip.CheckedInputStream; + +import jakarta.json.Json; +import jakarta.json.JsonException; +import jakarta.json.JsonNumber; +import jakarta.json.JsonObject; +import jakarta.json.JsonReader; +import jakarta.json.JsonValue; + import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; @@ -48,25 +48,20 @@ import org.apache.http.util.EntityUtils; import org.icatproject.ids.enums.RequestIdNames; +import static org.junit.Assert.fail; + public class TestingClient { public enum Flag { - COMPRESS, - NONE, - ZIP, - ZIP_AND_COMPRESS, + COMPRESS, NONE, ZIP, ZIP_AND_COMPRESS } public enum Method { - DELETE, - GET, - POST, - PUT, + DELETE, GET, POST, PUT } public enum ParmPos { - BODY, - URL, + BODY, URL } public class ServiceStatus { @@ -112,68 +107,52 @@ void storeFailure(Long id) { public String toString() { String result = "lockCount: " + this.lockCount + ", "; result += "lockedIDs: " + lockedDs.size() + "\n"; - for (long lockedId : lockedDs) result += "\t" + lockedId + "\n"; + for(long lockedId : lockedDs) result += "\t" + lockedId + "\n"; result += "opItems: " + opItems.size() + "\n"; - for (String key : opItems.keySet()) result += - "\t" + key + ": " + opItems.get(key) + "\n"; + for(String key : opItems.keySet()) result += "\t" + key + ": " + opItems.get(key) + "\n"; result += "failures: " + failures.size() + "\n"; - for (long fail : failures) result += " " + fail; + for(long fail : failures) result += " " + fail; result += "\n"; return result; } + } public enum Status { - ARCHIVED, - ONLINE, - RESTORING, + ARCHIVED, ONLINE, RESTORING } private String basePath; private URI idsUri; public TestingClient(URL idsUrl) { + try { - idsUri = - new URI( - idsUrl.getProtocol(), - null, - idsUrl.getHost(), - idsUrl.getPort(), - null, - null, - null - ); + idsUri = new URI(idsUrl.getProtocol(), null, idsUrl.getHost(), idsUrl.getPort(), null, null, null); basePath = idsUrl.getPath(); } catch (URISyntaxException e) { throw new RuntimeException(e); } + } - public void archive(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { + public void archive(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, + BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { + URI uri = getUri(getUriBuilder("archive")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId) - ); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue()) - ); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(formparams)); - try ( - CloseableHttpResponse response = httpclient.execute(httpPost) - ) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -181,7 +160,8 @@ public void archive(String sessionId, DataSelection data, Integer sc) } private void checkStatus(HttpResponse response, Integer sc) - throws InternalException, BadRequestException, DataNotOnlineException, ParseException, IOException, InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException { + throws InternalException, BadRequestException, DataNotOnlineException, ParseException, IOException, + InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException { StatusLine status = response.getStatusLine(); if (status == null) { throw new InternalException("Status line returned is empty"); @@ -195,7 +175,8 @@ private void checkStatus(HttpResponse response, Integer sc) } else { fail("rc was " + rc + " " + EntityUtils.toString(entity)); } - } else fail("Expected " + sc.intValue() + " but was " + rc); + } else + fail("Expected " + sc.intValue() + " but was " + rc); } if (rc / 100 != 2) { HttpEntity entity = response.getEntity(); @@ -207,11 +188,7 @@ private void checkStatus(HttpResponse response, Integer sc) } String code; String message; - try ( - JsonReader jsonReader = Json.createReader( - new StringReader(error) - ) - ) { + try (JsonReader jsonReader = Json.createReader(new StringReader(error))) { JsonObject json = jsonReader.readObject(); code = json.getString("code"); message = json.getString("message"); @@ -246,10 +223,13 @@ private void checkStatus(HttpResponse response, Integer sc) throw new NotImplementedException(message); } } + } public void delete(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, + NotFoundException, DataNotOnlineException { + URIBuilder uriBuilder = getUriBuilder("delete"); uriBuilder.addParameter(RequestIdNames.sessionId, sessionId); for (Entry entry : data.getParameters().entrySet()) { @@ -259,66 +239,48 @@ public void delete(String sessionId, DataSelection data, Integer sc) try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpDelete httpDelete = new HttpDelete(uri); - try ( - CloseableHttpResponse response = httpclient.execute(httpDelete) - ) { + try (CloseableHttpResponse response = httpclient.execute(httpDelete)) { expectNothing(response, sc); } catch (InsufficientStorageException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - private void expectNothing(CloseableHttpResponse response, Integer sc) - throws InternalException, BadRequestException, DataNotOnlineException, ParseException, InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException, IOException { + private void expectNothing(CloseableHttpResponse response, Integer sc) throws InternalException, + BadRequestException, DataNotOnlineException, ParseException, InsufficientPrivilegesException, + NotImplementedException, InsufficientStorageException, NotFoundException, IOException { checkStatus(response, sc); HttpEntity entity = response.getEntity(); if (entity != null) { if (!EntityUtils.toString(entity).isEmpty()) { - throw new InternalException( - "No http entity expected in response" - ); + throw new InternalException("No http entity expected in response"); } } } - public URL getIcatUrl(int sc) - throws InternalException, ParseException, NotImplementedException { + public URL getIcatUrl(int sc) throws InternalException, ParseException, NotImplementedException { URI uri = getUri(getUriBuilder("getIcatUrl")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return new URL(getString(response, sc)); - } catch ( - IOException - | InsufficientStorageException - | DataNotOnlineException - | BadRequestException - | InsufficientPrivilegesException - | NotFoundException e - ) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public InputStream getData( - String sessionId, - DataSelection data, - Flag flags, - long offset, - Integer sc - ) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { + public InputStream getData(String sessionId, DataSelection data, Flag flags, long offset, Integer sc) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, + InternalException, DataNotOnlineException { + URIBuilder uriBuilder = getUriBuilder("getData"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); for (Entry entry : data.getParameters().entrySet()) { @@ -368,7 +330,9 @@ public InputStream getData( } public InputStream getData(String preparedId, long offset, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, + InternalException, DataNotOnlineException { + URIBuilder uriBuilder = getUriBuilder("getData"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); URI uri = getUri(uriBuilder); @@ -408,7 +372,8 @@ public InputStream getData(String preparedId, long offset, Integer sc) } public ServiceStatus getServiceStatus(String sessionId, Integer sc) - throws InternalException, InsufficientPrivilegesException, NotImplementedException { + throws InternalException, InsufficientPrivilegesException, NotImplementedException { + URIBuilder uriBuilder = getUriBuilder("getServiceStatus"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); URI uri = getUri(uriBuilder); @@ -419,11 +384,7 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); String result = getString(response, sc); - try ( - JsonReader jsonReader = Json.createReader( - new StringReader(result) - ) - ) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { ServiceStatus serviceStatus = new ServiceStatus(); JsonObject rootNode = jsonReader.readObject(); for (JsonValue on : rootNode.getJsonArray("opsQueue")) { @@ -433,9 +394,7 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) } serviceStatus.setLockedCount(rootNode.getInt("lockCount")); for (JsonValue lock : rootNode.getJsonArray("locks")) { - Long dsId = - ((JsonObject) lock).getJsonNumber("id") - .longValueExact(); + Long dsId = ((JsonObject) lock).getJsonNumber("id").longValueExact(); serviceStatus.storeLockedDs(dsId); } for (JsonValue num : rootNode.getJsonArray("failures")) { @@ -445,32 +404,21 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) return serviceStatus; } catch (JsonException e) { throw new InternalException( - "TestingClient " + - e.getClass() + - " " + - e.getMessage() + - " from " + - result - ); + "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); } - } catch ( - InsufficientStorageException - | DataNotOnlineException - | InternalException - | BadRequestException - | NotFoundException e - ) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + + } catch (InsufficientStorageException | DataNotOnlineException | InternalException | BadRequestException + | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public long getSize(String sessionId, DataSelection data, int sc) - throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { + public long getSize(String sessionId, DataSelection data, int sc) throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, NotImplementedException { + URIBuilder uriBuilder = getUriBuilder("getSize"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); for (Entry entry : data.getParameters().entrySet()) { @@ -483,22 +431,17 @@ public long getSize(String sessionId, DataSelection data, int sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Long.parseLong(getString(response, sc)); - } catch ( - IOException - | InsufficientStorageException - | DataNotOnlineException e - ) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + } catch (IOException | InsufficientStorageException | DataNotOnlineException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public long getSize(String preparedId, int sc) - throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { + public long getSize(String preparedId, int sc) throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, NotImplementedException { + URIBuilder uriBuilder = getUriBuilder("getSize"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); URI uri = getUri(uriBuilder); @@ -507,22 +450,17 @@ public long getSize(String preparedId, int sc) HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { return Long.parseLong(getString(response, sc)); - } catch ( - IOException - | InsufficientStorageException - | DataNotOnlineException e - ) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + } catch (IOException | InsufficientStorageException | DataNotOnlineException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public Status getStatus(String sessionId, DataSelection data, Integer sc) - throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { + public Status getStatus(String sessionId, DataSelection data, Integer sc) throws BadRequestException, + NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { + URIBuilder uriBuilder = getUriBuilder("getStatus"); if (sessionId != null) { uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -539,17 +477,17 @@ public Status getStatus(String sessionId, DataSelection data, Integer sc) checkResponseConformity(response); return Status.valueOf(getString(response, sc)); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } + } - public Status getStatus(String preparedId, Integer sc) - throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { + public Status getStatus(String preparedId, Integer sc) throws BadRequestException, + NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { + URIBuilder uriBuilder = getUriBuilder("getStatus"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); URI uri = getUri(uriBuilder); @@ -560,17 +498,17 @@ public Status getStatus(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { return Status.valueOf(getString(response, sc)); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } + } - private String getString(CloseableHttpResponse response, Integer sc) - throws InternalException, BadRequestException, DataNotOnlineException, ParseException, InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException, IOException { + private String getString(CloseableHttpResponse response, Integer sc) throws InternalException, BadRequestException, + DataNotOnlineException, ParseException, InsufficientPrivilegesException, NotImplementedException, + InsufficientStorageException, NotFoundException, IOException { checkStatus(response, sc); HttpEntity entity = response.getEntity(); if (entity == null) { @@ -592,7 +530,8 @@ private URIBuilder getUriBuilder(String path) { } public boolean isPrepared(String preparedId, Integer sc) - throws BadRequestException, NotFoundException, InternalException, NotImplementedException { + throws BadRequestException, NotFoundException, InternalException, NotImplementedException { + URIBuilder uriBuilder = getUriBuilder("isPrepared"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); URI uri = getUri(uriBuilder); @@ -603,72 +542,48 @@ public boolean isPrepared(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch ( - InsufficientStorageException - | DataNotOnlineException - | InsufficientPrivilegesException e - ) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public boolean isReadOnly(int sc) - throws InternalException, NotImplementedException { + public boolean isReadOnly(int sc) throws InternalException, NotImplementedException { URI uri = getUri(getUriBuilder("isReadOnly")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch ( - IOException - | InsufficientStorageException - | DataNotOnlineException - | BadRequestException - | InsufficientPrivilegesException - | NotFoundException e - ) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public boolean isTwoLevel(int sc) - throws InternalException, NotImplementedException { + public boolean isTwoLevel(int sc) throws InternalException, NotImplementedException { URI uri = getUri(getUriBuilder("isTwoLevel")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch ( - IOException - | InsufficientStorageException - | DataNotOnlineException - | BadRequestException - | InsufficientPrivilegesException - | NotFoundException e - ) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void ping(Integer sc) - throws InternalException, NotFoundException, NotImplementedException { + public void ping(Integer sc) throws InternalException, NotFoundException, NotImplementedException { + URI uri = getUri(getUriBuilder("ping")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); @@ -676,43 +591,26 @@ public void ping(Integer sc) checkResponseConformity(response); String result = getString(response, sc); if (!result.equals("IdsOK")) { - throw new NotFoundException( - "Server gave invalid response: " + result - ); + throw new NotFoundException("Server gave invalid response: " + result); } - } catch ( - IOException - | InsufficientStorageException - | DataNotOnlineException - | BadRequestException - | InsufficientPrivilegesException - | NotFoundException e - ) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public String prepareData( - String sessionId, - DataSelection data, - Flag flags, - Integer sc - ) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException { + public String prepareData(String sessionId, DataSelection data, Flag flags, Integer sc) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, + InternalException { + URI uri = getUri(getUriBuilder("prepareData")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId) - ); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue()) - ); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } if (flags == Flag.ZIP || flags == Flag.ZIP_AND_COMPRESS) { formparams.add(new BasicNameValuePair("zip", "true")); @@ -724,69 +622,39 @@ public String prepareData( HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try ( - CloseableHttpResponse response = httpclient.execute(httpPost) - ) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { checkResponseConformity(response); return getString(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } + } - public Long put( - String sessionId, - InputStream inputStream, - String name, - long datasetId, - long datafileFormatId, - String description, - Integer sc - ) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException, InsufficientStorageException { - return put( - sessionId, - inputStream, - name, - datasetId, - datafileFormatId, - description, - null, - null, - null, - sc - ); + public Long put(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, + String description, Integer sc) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, + NotImplementedException, DataNotOnlineException, InsufficientStorageException { + return put(sessionId, inputStream, name, datasetId, datafileFormatId, description, null, null, null, sc); } - public Long put( - String sessionId, - InputStream inputStream, - String name, - long datasetId, - long datafileFormatId, - String description, - String doi, - Date datafileCreateTime, - Date datafileModTime, - Integer sc - ) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException, InsufficientStorageException { + public Long put(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, + String description, String doi, Date datafileCreateTime, Date datafileModTime, Integer sc) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, + NotImplementedException, DataNotOnlineException, InsufficientStorageException { + if (inputStream == null) { throw new BadRequestException("Input stream is null"); } CRC32 crc = new CRC32(); inputStream = new CheckedInputStream(inputStream, crc); URIBuilder uriBuilder = getUriBuilder("put"); - uriBuilder - .setParameter(RequestIdNames.sessionId, sessionId) - .setParameter("name", name) - .setParameter("datafileFormatId", Long.toString(datafileFormatId)) - .setParameter("datasetId", Long.toString(datasetId)); + uriBuilder.setParameter(RequestIdNames.sessionId, sessionId).setParameter("name", name) + .setParameter("datafileFormatId", Long.toString(datafileFormatId)) + .setParameter("datasetId", Long.toString(datasetId)); if (description != null) { uriBuilder.setParameter("description", description); } @@ -794,70 +662,40 @@ public Long put( uriBuilder.setParameter("doi", doi); } if (datafileCreateTime != null) { - uriBuilder.setParameter( - "datafileCreateTime", - Long.toString(datafileCreateTime.getTime()) - ); + uriBuilder.setParameter("datafileCreateTime", Long.toString(datafileCreateTime.getTime())); } if (datafileModTime != null) { - uriBuilder.setParameter( - "datafileModTime", - Long.toString(datafileModTime.getTime()) - ); + uriBuilder.setParameter("datafileModTime", Long.toString(datafileModTime.getTime())); } URI uri = getUri(uriBuilder); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPut httpPut = new HttpPut(uri); - httpPut.setEntity( - new InputStreamEntity( - inputStream, - ContentType.APPLICATION_OCTET_STREAM - ) - ); + httpPut.setEntity(new InputStreamEntity(inputStream, ContentType.APPLICATION_OCTET_STREAM)); try (CloseableHttpResponse response = httpclient.execute(httpPut)) { String result = getString(response, sc); checkResponseConformity(response); - try ( - JsonReader jsonReader = Json.createReader( - new StringReader(result) - ) - ) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); - if ( - rootNode.getJsonNumber("checksum").longValueExact() != - crc.getValue() - ) { - throw new InternalException( - "Error uploading - the checksum was not as expected" - ); + if (rootNode.getJsonNumber("checksum").longValueExact() != crc.getValue()) { + throw new InternalException("Error uploading - the checksum was not as expected"); } return rootNode.getJsonNumber("id").longValueExact(); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (NumberFormatException e) { - throw new InternalException( - "Web service call did not return a valid Long value" - ); + throw new InternalException("Web service call did not return a valid Long value"); } + } - public Long putAsPost( - String sessionId, - InputStream inputStream, - String name, - long datasetId, - long datafileFormatId, - String description, - String doi, - Date datafileCreateTime, - Date datafileModTime, - boolean wrap, - Integer sc - ) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException, InsufficientStorageException { + public Long putAsPost(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, + String description, String doi, Date datafileCreateTime, Date datafileModTime, boolean wrap, Integer sc) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, + NotImplementedException, DataNotOnlineException, InsufficientStorageException { + if (inputStream == null) { throw new BadRequestException("Input stream is null"); } @@ -865,65 +703,30 @@ public Long putAsPost( inputStream = new CheckedInputStream(inputStream, crc); URI uri = getUri(getUriBuilder("put")); - MultipartEntityBuilder reqEntityBuilder = MultipartEntityBuilder - .create() - .addPart( - RequestIdNames.sessionId, - new StringBody(sessionId, ContentType.TEXT_PLAIN) - ) - .addPart( - "datafileFormatId", - new StringBody( - Long.toString(datafileFormatId), - ContentType.TEXT_PLAIN - ) - ) - .addPart("name", new StringBody(name, ContentType.TEXT_PLAIN)) - .addPart( - "datasetId", - new StringBody(Long.toString(datasetId), ContentType.TEXT_PLAIN) - ); + MultipartEntityBuilder reqEntityBuilder = MultipartEntityBuilder.create() + .addPart(RequestIdNames.sessionId, new StringBody(sessionId, ContentType.TEXT_PLAIN)) + .addPart("datafileFormatId", new StringBody(Long.toString(datafileFormatId), ContentType.TEXT_PLAIN)) + .addPart("name", new StringBody(name, ContentType.TEXT_PLAIN)) + .addPart("datasetId", new StringBody(Long.toString(datasetId), ContentType.TEXT_PLAIN)); if (description != null) { - reqEntityBuilder.addPart( - "description", - new StringBody(description, ContentType.TEXT_PLAIN) - ); + reqEntityBuilder.addPart("description", new StringBody(description, ContentType.TEXT_PLAIN)); } if (doi != null) { - reqEntityBuilder.addPart( - "doi", - new StringBody(doi, ContentType.TEXT_PLAIN) - ); + reqEntityBuilder.addPart("doi", new StringBody(doi, ContentType.TEXT_PLAIN)); } if (datafileCreateTime != null) { - reqEntityBuilder.addPart( - "datafileCreateTime", - new StringBody( - Long.toString(datafileCreateTime.getTime()), - ContentType.TEXT_PLAIN - ) - ); + reqEntityBuilder.addPart("datafileCreateTime", + new StringBody(Long.toString(datafileCreateTime.getTime()), ContentType.TEXT_PLAIN)); } if (datafileModTime != null) { - reqEntityBuilder.addPart( - "datafileModTime", - new StringBody( - Long.toString(datafileModTime.getTime()), - ContentType.TEXT_PLAIN - ) - ); + reqEntityBuilder.addPart("datafileModTime", + new StringBody(Long.toString(datafileModTime.getTime()), ContentType.TEXT_PLAIN)); } if (wrap) { - reqEntityBuilder.addPart( - "wrap", - new StringBody("true", ContentType.TEXT_PLAIN) - ); - } - InputStreamBody body = new InputStreamBody( - new BufferedInputStream(inputStream), - ContentType.APPLICATION_OCTET_STREAM, - "unreliable" - ); + reqEntityBuilder.addPart("wrap", new StringBody("true", ContentType.TEXT_PLAIN)); + } + InputStreamBody body = new InputStreamBody(new BufferedInputStream(inputStream), + ContentType.APPLICATION_OCTET_STREAM, "unreliable"); HttpEntity entity = reqEntityBuilder.addPart("file", body).build(); CloseableHttpClient httpclient = HttpClients.createDefault(); @@ -933,65 +736,43 @@ public Long putAsPost( try (CloseableHttpResponse response = httpclient.execute(httpPost)) { checkResponseConformity(response); String result = getString(response, sc); - String prefix = - ""; if (result.startsWith(prefix)) { - result = - result.substring( - prefix.length(), - result.length() - suffix.length() - ); + result = result.substring(prefix.length(), result.length() - suffix.length()); } - try ( - JsonReader jsonReader = Json.createReader( - new StringReader(result) - ) - ) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); - if ( - rootNode.getJsonNumber("checksum").longValueExact() != - crc.getValue() - ) { - throw new InternalException( - "Error uploading - the checksum was not as expected" - ); + if (rootNode.getJsonNumber("checksum").longValueExact() != crc.getValue()) { + throw new InternalException("Error uploading - the checksum was not as expected"); } return rootNode.getJsonNumber("id").longValueExact(); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (NumberFormatException e) { - throw new InternalException( - "Web service call did not return a valid Long value" - ); + throw new InternalException("Web service call did not return a valid Long value"); } + } - public void restore(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { + public void restore(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, + BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { + URI uri = getUri(getUriBuilder("restore")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId) - ); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue()) - ); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try ( - CloseableHttpResponse response = httpclient.execute(httpPost) - ) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -999,7 +780,8 @@ public void restore(String sessionId, DataSelection data, Integer sc) } public List getDatafileIds(String preparedId, Integer sc) - throws InternalException, BadRequestException, NotFoundException { + throws InternalException, BadRequestException, NotFoundException { + URIBuilder uriBuilder = getUriBuilder("getDatafileIds"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); URI uri = getUri(uriBuilder); @@ -1010,11 +792,8 @@ public List getDatafileIds(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { String result = getString(response, sc); checkResponseConformity(response); - try ( - JsonReader jsonReader = Json.createReader( - new StringReader(result) - ) - ) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + JsonObject rootNode = jsonReader.readObject(); List ids = new ArrayList<>(); for (JsonValue num : rootNode.getJsonArray("ids")) { @@ -1024,35 +803,20 @@ public List getDatafileIds(String preparedId, Integer sc) return ids; } catch (JsonException e) { throw new InternalException( - "TestingClient " + - e.getClass() + - " " + - e.getMessage() + - " from " + - result - ); + "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); } - } catch ( - InsufficientStorageException - | DataNotOnlineException - | InsufficientPrivilegesException - | NotImplementedException e - ) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + + } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException + | NotImplementedException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public List getDatafileIds( - String sessionId, - DataSelection data, - Integer sc - ) - throws InternalException, BadRequestException, ParseException, NotFoundException { + public List getDatafileIds(String sessionId, DataSelection data, Integer sc) + throws InternalException, BadRequestException, ParseException, NotFoundException { URIBuilder uriBuilder = getUriBuilder("getDatafileIds"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); for (Entry entry : data.getParameters().entrySet()) { @@ -1066,11 +830,8 @@ public List getDatafileIds( try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); String result = getString(response, sc); - try ( - JsonReader jsonReader = Json.createReader( - new StringReader(result) - ) - ) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + JsonObject rootNode = jsonReader.readObject(); List ids = new ArrayList<>(); for (JsonValue num : rootNode.getJsonArray("ids")) { @@ -1080,139 +841,98 @@ public List getDatafileIds( return ids; } catch (JsonException e) { throw new InternalException( - "TestingClient " + - e.getClass() + - " " + - e.getMessage() + - " from " + - result - ); + "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); } - } catch ( - InsufficientStorageException - | DataNotOnlineException - | InsufficientPrivilegesException - | NotImplementedException e - ) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + + } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException + | NotImplementedException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void reset(String preparedId, Integer sc) - throws InternalException, BadRequestException, ParseException, InsufficientPrivilegesException, NotImplementedException, NotFoundException { + public void reset(String preparedId, Integer sc) throws InternalException, BadRequestException, ParseException, + InsufficientPrivilegesException, NotImplementedException, NotFoundException { URI uri = getUri(getUriBuilder("reset")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.preparedId, preparedId) - ); + formparams.add(new BasicNameValuePair(RequestIdNames.preparedId, preparedId)); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try ( - CloseableHttpResponse response = httpclient.execute(httpPost) - ) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void reset(String sessionId, DataSelection data, int sc) - throws InternalException, BadRequestException, ParseException, InsufficientPrivilegesException, NotImplementedException, NotFoundException { + public void reset(String sessionId, DataSelection data, int sc) throws InternalException, BadRequestException, + ParseException, InsufficientPrivilegesException, NotImplementedException, NotFoundException { URI uri = getUri(getUriBuilder("reset")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId) - ); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue()) - ); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try ( - CloseableHttpResponse response = httpclient.execute(httpPost) - ) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void write(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { + public void write(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, + BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, + DataNotOnlineException { + URI uri = getUri(getUriBuilder("write")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId) - ); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue()) - ); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(formparams)); - try ( - CloseableHttpResponse response = httpclient.execute(httpPost) - ) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage() - ); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - private void checkResponseConformity(HttpResponse response) - throws InternalException { + private void checkResponseConformity(HttpResponse response) throws InternalException { + StatusLine status = response.getStatusLine(); var statusCode = status.getStatusCode(); - if (statusCode == 200 && response.getEntity() != null) { + if(statusCode == 200 && response.getEntity() != null) { //we have a status of 200 and a payload. Check for header conformity - Boolean chunked = response.getFirstHeader("Transfer-Encoding") != - null - ? response - .getFirstHeader("Transfer-Encoding") - .getValue() - .contains("chunked") - : false; - - if ( - ( - response.getFirstHeader("Content-Length") == null && - !chunked - ) || - (response.getFirstHeader("Content-Length") != null && chunked) - ) { - throw new InternalException( - "Responses with payload should either contain the Content-Length header or the 'Transfer-Encoding: chunked' header. It needs exactly one of them." - ); + Boolean chunked = response.getFirstHeader("Transfer-Encoding") != null ? response.getFirstHeader("Transfer-Encoding").getValue().contains("chunked") + : false; + + if ( (response.getFirstHeader("Content-Length") == null && !chunked) + || (response.getFirstHeader("Content-Length") != null && chunked ) ) { + + throw new InternalException("Responses with payload should either contain the Content-Length header or the 'Transfer-Encoding: chunked' header. It needs exactly one of them."); } } } + } From 79d8eef1522b3d4485c4f43614e428e41b1ef3e3 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Tue, 11 Jun 2024 13:26:03 +0200 Subject: [PATCH 08/19] Revomed prettier because of the lack of an import order feature --- pom.xml | 40 ---------------------------------------- src/.prettierrc.yaml | 4 ---- 2 files changed, 44 deletions(-) delete mode 100644 src/.prettierrc.yaml diff --git a/pom.xml b/pom.xml index 0a1b2bd2..840907ec 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,6 @@ UTF-8 https://repo.icatproject.org/repo github - write @@ -21,10 +20,6 @@ ICAT Repo https://repo.icatproject.org/repo - - com.hubspot.maven.plugins Repo - https://mvnrepository.com/artifact/com.hubspot.maven.plugins/ - @@ -425,45 +420,10 @@ -Dmaven.javadoc.skip=true - - - com.hubspot.maven.plugins - prettier-maven-plugin - 0.22 - - 2.0.0 - false - true - - - - validate - - ${plugin.prettier.goal} - - - - - - - - cibuild - - - env.CIBUILD - - - - - check - - - - diff --git a/src/.prettierrc.yaml b/src/.prettierrc.yaml deleted file mode 100644 index 314ad51b..00000000 --- a/src/.prettierrc.yaml +++ /dev/null @@ -1,4 +0,0 @@ -useTabs: false -tabWidth: 4 -printWidth: 80 -ignoreEditorConfig: true \ No newline at end of file From 72d2bb736a761ec97dc16e85ad69c6fc16fe75d3 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:08:11 +0200 Subject: [PATCH 09/19] WIP: eclipse formatter for maven --- eclipse-formatter-config.xml | 404 +++++++++++++++++++++++++++++++++++ pom.xml | 28 ++- 2 files changed, 429 insertions(+), 3 deletions(-) create mode 100644 eclipse-formatter-config.xml diff --git a/eclipse-formatter-config.xml b/eclipse-formatter-config.xml new file mode 100644 index 00000000..8c3a01e3 --- /dev/null +++ b/eclipse-formatter-config.xml @@ -0,0 +1,404 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 840907ec..596bd688 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,11 @@ MireDot Releases https://secure-nexus.miredot.com/content/repositories/miredot + + net.revelc.code.formatter + formatter-maven-plugin Repo + https://mvnrepository.com/artifact/net.revelc.code.formatter/ + @@ -129,10 +134,11 @@ test + - com.hubspot.maven.plugins - prettier-maven-plugin - 0.22 + net.revelc.code.formatter + formatter-maven-plugin + 2.24.1 @@ -420,6 +426,22 @@ -Dmaven.javadoc.skip=true + + + net.revelc.code.formatter + formatter-maven-plugin + 2.23.0 + + ${project.basedir}/eclipse-formatter-config.xml + + + + + format + + + + From d23ccb987dda57f503eac30b37c406b522c3032b Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:24:16 +0200 Subject: [PATCH 10/19] let formatter only validate while ci build --- pom.xml | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 596bd688..39f071f5 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,7 @@ UTF-8 https://repo.icatproject.org/repo github + format @@ -437,7 +438,7 @@ - format + ${formatter.goal} @@ -446,6 +447,21 @@ + + + cibuild + + + env.CIBUILD + + + + + validate + + + + From 4ee63301bcc547c63d242090984e1b4f7d954bcf Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:31:41 +0200 Subject: [PATCH 11/19] updated README --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5c5debcb..4aa195e1 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,15 @@ # ids.server: Server part of ICAT Data Server (IDS) ## Linting -The Prettier Maven Plugin is used for linting. it is configured for "check" while build. That's why a build will fail when the files haven't the right format. -execute `mvn prettier:write` before. +The [formatter-maven-plugin](https://code.revelc.net/formatter-maven-plugin/) is used here for linting. It uses the same configuration like the Eclipse code formatting feature. +An automatically code formatting is triggered while local build. In CI builds the code is only validated. +You also can execute the commands manually: +* `mvn formatter:validate` for validation +* `mvn formatter:format` to update the source code like it is defined in the formatters configuration. -TODO: execute `mvn prettier:write` while local build but only the `check` while CI build. +The rules for the formatter are defined in `eclipse-formatter-config.xml`. +## Further Information [![Build Status](https://github.com/icatproject/ids.server/workflows/CI%20Build/badge.svg?branch=master)](https://github.com/icatproject/ids.server/actions?query=workflow%3A%22CI+Build%22) General installation instructions are at http://www.icatproject.org/installation/component From 55ec85320f5274d512454fc833ad9c98678f78f9 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Wed, 12 Jun 2024 08:02:51 +0200 Subject: [PATCH 12/19] linted code to test ci check --- .../java/org/icatproject/ids/IdsService.java | 700 +++++++++++------- src/main/java/org/icatproject/ids/Tidier.java | 138 ++-- .../icatproject/ids/enums/RequestType.java | 19 +- .../ids/exceptions/IdsExceptionMapper.java | 9 +- .../exceptions/NotFoundExceptionMapper.java | 13 +- .../exceptions/RuntimeExceptionMapper.java | 13 +- .../FiniteStateMachine.java | 70 +- ...niteStateMachineForSingleLevelStorage.java | 17 +- ...iteStateMachineForStorageUnitDatafile.java | 201 +++-- ...niteStateMachineForStorageUnitDataset.java | 128 ++-- .../ids/helpers/CORSResponseFilter.java | 4 +- .../icatproject/ids/helpers/Constants.java | 4 +- .../ids/helpers/LocationHelper.java | 10 +- .../ids/helpers/RangeOutputStream.java | 8 +- .../java/org/icatproject/ids/helpers/SO.java | 43 +- .../ids/helpers/ValueContainer.java | 92 ++- .../icatproject/ids/models/DataInfoBase.java | 4 +- .../icatproject/ids/models/DataSelection.java | 16 +- .../icatproject/ids/models/DatafileInfo.java | 26 +- .../icatproject/ids/models/DatasetInfo.java | 43 +- .../org/icatproject/ids/models/Prepared.java | 65 +- .../ids/requestHandlers/ArchiveHandler.java | 13 +- .../ids/requestHandlers/DeleteHandler.java | 29 +- .../GetDataFileIdsHandler.java | 20 +- .../ids/requestHandlers/GetDataHandler.java | 115 ++- .../requestHandlers/GetIcatUrlHandler.java | 10 +- .../GetServiceStatusHandler.java | 18 +- .../ids/requestHandlers/GetStatusHandler.java | 18 +- .../requestHandlers/IsPreparedHandler.java | 24 +- .../requestHandlers/IsReadOnlyHandler.java | 8 +- .../requestHandlers/IsTwoLevelHandler.java | 8 +- .../requestHandlers/PrepareDataHandler.java | 39 +- .../ids/requestHandlers/PutHandler.java | 194 +++-- .../ids/requestHandlers/ResetHandler.java | 18 +- .../ids/requestHandlers/RestoreHandler.java | 14 +- .../ids/requestHandlers/WriteHandler.java | 24 +- .../base/DataControllerBase.java | 40 +- .../base/DataRequestHandler.java | 54 +- .../base/PreparedDataController.java | 26 +- .../base/RequestHandlerBase.java | 73 +- .../base/UnpreparedDataController.java | 39 +- .../getSizeHandlers/GetSizeHandler.java | 35 +- .../GetSizeHandlerForFastProcessing.java | 71 +- .../icatproject/ids/services/ICATGetter.java | 22 +- .../icatproject/ids/services/IcatReader.java | 12 +- .../icatproject/ids/services/LockManager.java | 18 +- .../ids/services/PropertyHandler.java | 108 ++- .../ids/services/ServiceProvider.java | 36 +- .../icatproject/ids/services/Transmitter.java | 9 +- .../ids/services/UnfinishedWorkService.java | 66 +- .../DataSelectionService.java | 151 ++-- .../DataSelectionServiceFactory.java | 360 ++++++--- ...SelectionServiceForSingleLevelStorage.java | 42 +- ...electionServiceForStorageUnitDatafile.java | 31 +- ...SelectionServiceForStorageUnitDataset.java | 37 +- .../icatproject/ids/thread/DfArchiver.java | 16 +- .../org/icatproject/ids/thread/DfDeleter.java | 10 +- .../icatproject/ids/thread/DfRestorer.java | 18 +- .../org/icatproject/ids/thread/DfWriter.java | 15 +- .../icatproject/ids/thread/DsArchiver.java | 12 +- .../icatproject/ids/thread/DsRestorer.java | 43 +- .../org/icatproject/ids/thread/DsWriter.java | 37 +- .../icatproject/ids/DataSelectionDevTest.java | 39 +- .../java/org/icatproject/ids/DigestTest.java | 9 +- .../icatproject/ids/PreparePackingTest.java | 17 +- .../ids/RangeOutputStreamTest.java | 4 +- .../org/icatproject/ids/TestICATGetter.java | 9 +- .../java/org/icatproject/ids/TestUtils.java | 7 +- .../icatproject/ids/TestValueContainer.java | 20 +- .../java/org/icatproject/ids/TestZipping.java | 9 +- .../java/org/icatproject/ids/TidierTest.java | 9 +- .../org/icatproject/ids/TransmittingTest.java | 157 ++-- .../org/icatproject/ids/ValidaterTest.java | 1 - .../icatproject/ids/integration/BaseTest.java | 308 +++++--- .../integration/one/BogusDatafileTest.java | 36 +- .../ids/integration/one/DeleteTest.java | 15 +- .../integration/one/GetDataExplicitTest.java | 102 ++- .../one/GetDataForPreparedIdTest.java | 50 +- .../one/GetStatusExplicitTest.java | 18 +- .../one/GetStatusForPreparedIdTest.java | 14 +- .../ids/integration/one/MiscTest.java | 17 +- .../ids/integration/one/PerformanceTest.java | 21 +- .../ids/integration/one/PrepareDataTest.java | 47 +- .../ids/integration/one/PutTest.java | 32 +- .../ids/integration/one/WriteTest.java | 3 +- .../ids/integration/two/ArchiveTest.java | 6 +- .../integration/two/BogusDatafileTest.java | 66 +- .../ids/integration/two/DeleteTest.java | 15 +- .../integration/two/GetDataExplicitTest.java | 71 +- .../two/GetDataForPreparedIdTest.java | 46 +- .../two/GetStatusExplicitTest.java | 16 +- .../two/GetStatusForPreparedIdTest.java | 14 +- .../ids/integration/two/MiscTest.java | 14 +- .../ids/integration/two/PrepareDataTest.java | 74 +- .../ids/integration/two/PutTest.java | 27 +- .../integration/two/RestoreErrorsTest.java | 12 +- .../ids/integration/two/RestoreTest.java | 16 +- .../ids/integration/two/WriteTest.java | 18 +- .../ids/integration/twodf/ArchiveTest.java | 6 +- .../integration/twodf/BogusDatafileTest.java | 66 +- .../ids/integration/twodf/DeleteTest.java | 15 +- .../twodf/GetDataExplicitTest.java | 71 +- .../twodf/GetDataForPreparedIdTest.java | 48 +- .../twodf/GetStatusExplicitTest.java | 19 +- .../twodf/GetStatusForPreparedIdTest.java | 14 +- .../ids/integration/twodf/MiscTest.java | 14 +- .../integration/twodf/PrepareDataTest.java | 74 +- .../ids/integration/twodf/PutTest.java | 27 +- .../ids/integration/twodf/QueueTest.java | 10 +- .../ids/integration/twodf/RestoreTest.java | 16 +- .../ids/integration/twodf/WriteTest.java | 18 +- .../ids/integration/util/Setup.java | 24 +- .../util/client/HttpInputStream.java | 3 +- .../util/client/TestingClient.java | 450 +++++++---- 114 files changed, 3657 insertions(+), 2113 deletions(-) diff --git a/src/main/java/org/icatproject/ids/IdsService.java b/src/main/java/org/icatproject/ids/IdsService.java index 0d517fa9..f5f6a7be 100644 --- a/src/main/java/org/icatproject/ids/IdsService.java +++ b/src/main/java/org/icatproject/ids/IdsService.java @@ -6,7 +6,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; - import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import jakarta.ejb.EJB; @@ -71,7 +70,8 @@ @Stateless public class IdsService { - private final static Logger logger = LoggerFactory.getLogger(IdsService.class); + private final static Logger logger = LoggerFactory + .getLogger(IdsService.class); @EJB Transmitter transmitter; @@ -89,7 +89,6 @@ public class IdsService { private UnfinishedWorkService unfinishedWorkService; - @PostConstruct private void init() { @@ -98,15 +97,19 @@ private void init() { logger.info("creating IdsService"); - FiniteStateMachine.createInstance(reader, lockManager, PropertyHandler.getInstance().getStorageUnit()); + FiniteStateMachine.createInstance(reader, lockManager, + PropertyHandler.getInstance().getStorageUnit()); this.fsm = FiniteStateMachine.getInstance(); this.fsm.init(); - ServiceProvider.createInstance(transmitter, fsm, lockManager, reader); + ServiceProvider.createInstance(transmitter, fsm, lockManager, + reader); - var propertyHandler = ServiceProvider.getInstance().getPropertyHandler(); + var propertyHandler = ServiceProvider.getInstance() + .getPropertyHandler(); var archiveStorage = propertyHandler.getArchiveStorage(); var twoLevel = archiveStorage != null; - var preparedDir = propertyHandler.getCacheDir().resolve("prepared"); + var preparedDir = propertyHandler.getCacheDir() + .resolve("prepared"); Files.createDirectories(preparedDir); this.unfinishedWorkService = new UnfinishedWorkService(); @@ -120,13 +123,17 @@ private void init() { java.nio.file.Path datasetDir; if (twoLevel) { - datasetDir = propertyHandler.getCacheDir().resolve("dataset"); - var markerDir = propertyHandler.getCacheDir().resolve("marker"); + datasetDir = propertyHandler.getCacheDir() + .resolve("dataset"); + var markerDir = propertyHandler.getCacheDir() + .resolve("marker"); if (!inited) { Files.createDirectories(datasetDir); Files.createDirectories(markerDir); - this.unfinishedWorkService.restartUnfinishedWork(markerDir, key); - this.unfinishedWorkService.cleanDatasetCache(datasetDir); + this.unfinishedWorkService + .restartUnfinishedWork(markerDir, key); + this.unfinishedWorkService + .cleanDatasetCache(datasetDir); } } @@ -136,47 +143,53 @@ private void init() { } } catch (Throwable e) { logger.error("Won't start ", e); - throw new RuntimeException("IdsService reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException("IdsService reports " + e.getClass() + + " " + e.getMessage()); } } - @PreDestroy private void exit() { this.fsm.exit(); logger.info("destroyed IdsService"); } - /** * Archive data specified by the investigationIds, datasetIds and * datafileIds specified along with a sessionId. If two level storage is not * in use this has no effect. * * @title archive - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds If present, a comma separated list of investigation id values - * @param datasetIds If present, a comma separated list of data set id values or - * null - * @param datafileIds If present, a comma separated list of datafile id values. + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds If present, a comma separated list of + * investigation id values + * @param datasetIds If present, a comma separated list of data set id + * values or null + * @param datafileIds If present, a comma separated list of datafile id + * values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException * @throws InternalException * @throws NotFoundException - * @throws DataNotOnlineException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("archive") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void archive(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, + public void archive(@Context HttpServletRequest request, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new ArchiveHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + var handler = new ArchiveHandler(request.getRemoteAddr(), sessionId, + investigationIds, datasetIds, datafileIds); handler.handle(); } @@ -185,11 +198,14 @@ public void archive(@Context HttpServletRequest request, @FormParam(RequestIdNam * specified along with a sessionId. * * @title delete - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds If present, a comma separated list of investigation id values - * @param datasetIds If present, a comma separated list of data set id values or - * null - * @param datafileIds If present, a comma separated list of datafile id values. + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds If present, a comma separated list of + * investigation id values + * @param datasetIds If present, a comma separated list of data set id + * values or null + * @param datafileIds If present, a comma separated list of datafile id + * values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException @@ -200,16 +216,20 @@ public void archive(@Context HttpServletRequest request, @FormParam(RequestIdNam */ @DELETE @Path("delete") - public void delete(@Context HttpServletRequest request, @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds) throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { - - var handler = new DeleteHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + public void delete(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, + InternalException, DataNotOnlineException { + + var handler = new DeleteHandler(request.getRemoteAddr(), sessionId, + investigationIds, datasetIds, datafileIds); handler.handle(); } - /** * Return the version of the server * @@ -222,7 +242,8 @@ public void delete(@Context HttpServletRequest request, @QueryParam(RequestIdNam public String getVersion() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen.writeStartObject().write("version", Constants.API_VERSION).writeEnd(); + gen.writeStartObject().write("version", Constants.API_VERSION) + .writeEnd(); gen.close(); return baos.toString(); } @@ -234,43 +255,58 @@ public String getVersion() { * set. If preparedId is set the compress and zip arguments are not used. * * @title getData - * @param preparedId A valid preparedId returned by a call to prepareData - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds A comma separated list of investigation id values. + * @param preparedId A valid preparedId returned by a call to + * prepareData + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds A comma separated list of investigation id + * values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of data file id values. - * @param compress If true use default compression otherwise no compression. This - * only applies if preparedId is not set and if the results are - * being zipped. - * @param zip If true the data should be zipped. If multiple files are - * requested (or could be because a datasetId or investigationId - * has been specified) the data are zipped regardless of the + * @param compress If true use default compression otherwise no + * compression. This only applies if preparedId is + * not set and if the results are being zipped. + * @param zip If true the data should be zipped. If multiple + * files are requested (or could be because a + * datasetId or investigationId has been specified) + * the data are zipped regardless of the * specification of this flag. * @param outname The file name to put in the returned header - * "ContentDisposition". If it does not end in .zip but it is a - * zip file then a ".zip" will be appended. - * @param range A range header which must match "bytes=(\\d+)-" to specify an - * offset i.e. to skip a number of bytes. + * "ContentDisposition". If it does not end in .zip + * but it is a zip file then a ".zip" will be + * appended. + * @param range A range header which must match "bytes=(\\d+)-" + * to specify an offset i.e. to skip a number of + * bytes. * @return a stream of json data. * @throws BadRequestException * @throws NotFoundException * @throws InternalException * @throws InsufficientPrivilegesException * @throws DataNotOnlineException - * @throws NotImplementedException + * @throws NotImplementedException * @statuscode 200 To indicate success */ @GET @Path("getData") @Produces(MediaType.APPLICATION_OCTET_STREAM) - public Response getData(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds, - @QueryParam("compress") boolean compress, @QueryParam("zip") boolean zip, - @QueryParam("outname") String outname, @HeaderParam("Range") String range) throws BadRequestException, - NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - - var handler = new GetDataHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds, compress, zip, outname, range); + public Response getData(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds, + @QueryParam("compress") boolean compress, + @QueryParam("zip") boolean zip, + @QueryParam("outname") String outname, + @HeaderParam("Range") String range) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, DataNotOnlineException, + NotImplementedException { + + var handler = new GetDataHandler(request.getRemoteAddr(), preparedId, + sessionId, investigationIds, datasetIds, datafileIds, compress, + zip, outname, range); return handler.handle().getResponse(); } @@ -281,9 +317,12 @@ public Response getData(@Context HttpServletRequest request, @QueryParam(Request * set. * * @title getDatafileIds - * @param preparedId A valid preparedId returned by a call to prepareData - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds A comma separated list of investigation id values. + * @param preparedId A valid preparedId returned by a call to + * prepareData + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds A comma separated list of investigation id + * values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of datafile id values. * @return a list of id values @@ -291,19 +330,26 @@ public Response getData(@Context HttpServletRequest request, @QueryParam(Request * @throws InternalException * @throws NotFoundException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getDatafileIds") @Produces(MediaType.APPLICATION_JSON) - public String getDatafileIds(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - - var handler = new GetDataFileIdsHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public String getDatafileIds(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, InternalException, NotFoundException, + InsufficientPrivilegesException, DataNotOnlineException, + NotImplementedException { + + var handler = new GetDataFileIdsHandler(request.getRemoteAddr(), + preparedId, sessionId, investigationIds, datasetIds, + datafileIds); return handler.handle().getString(); } @@ -313,18 +359,21 @@ public String getDatafileIds(@Context HttpServletRequest request, @QueryParam(Re * obtained. * * @return the url of the icat server - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("getIcatUrl") @Produces(MediaType.TEXT_PLAIN) - public String getIcatUrl(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public String getIcatUrl(@Context HttpServletRequest request) + throws InternalException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { var handler = new GetIcatUrlHandler(request.getRemoteAddr()); return handler.handle().getString(); @@ -335,24 +384,28 @@ public String getIcatUrl(@Context HttpServletRequest request) throws InternalExc * privileged to use this call. * * @title getServiceStatus - * @param sessionId A valid ICAT session ID of a user in the IDS rootUserNames - * set. + * @param sessionId A valid ICAT session ID of a user in the IDS + * rootUserNames set. * @return a json string. * @throws InternalException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws BadRequestException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws BadRequestException * @statuscode 200 To indicate success */ @GET @Path("getServiceStatus") @Produces(MediaType.APPLICATION_JSON) - public String getServiceStatus(@Context HttpServletRequest request, @QueryParam(RequestIdNames.sessionId) String sessionId) - throws InternalException, InsufficientPrivilegesException, BadRequestException, NotFoundException, DataNotOnlineException, NotImplementedException { - - var handler = new GetServiceStatusHandler(request.getRemoteAddr(), sessionId); + public String getServiceStatus(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.sessionId) String sessionId) + throws InternalException, InsufficientPrivilegesException, + BadRequestException, NotFoundException, DataNotOnlineException, + NotImplementedException { + + var handler = new GetServiceStatusHandler(request.getRemoteAddr(), + sessionId); return handler.handle().getString(); } @@ -361,41 +414,53 @@ public String getServiceStatus(@Context HttpServletRequest request, @QueryParam( * investigationIds, datasetIds and datafileIds along with a sessionId. * * @title getSize - * @param preparedId A valid preparedId returned by a call to prepareData - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds If present, a comma separated list of investigation id values - * @param datasetIds If present, a comma separated list of data set id values or - * null - * @param datafileIds If present, a comma separated list of data file id values. + * @param preparedId A valid preparedId returned by a call to + * prepareData + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds If present, a comma separated list of + * investigation id values + * @param datasetIds If present, a comma separated list of data set id + * values or null + * @param datafileIds If present, a comma separated list of data file + * id values. * @return the size in bytes * @throws BadRequestException * @throws NotFoundException * @throws InsufficientPrivilegesException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getSize") @Produces(MediaType.TEXT_PLAIN) - public long getSize(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { - + public long getSize(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, + DataNotOnlineException, NotImplementedException { var result = ValueContainer.getInvalid(); // trying fast computation - if(sessionId != null) { - var fastHandler = new GetSizeHandlerForFastProcessing(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + if (sessionId != null) { + var fastHandler = new GetSizeHandlerForFastProcessing( + request.getRemoteAddr(), sessionId, investigationIds, + datasetIds, datafileIds); result = fastHandler.handle(); } // otherwise normal computation - if(result.isInvalid()) { - var handler = new GetSizeHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); + if (result.isInvalid()) { + var handler = new GetSizeHandler(request.getRemoteAddr(), + preparedId, sessionId, investigationIds, datasetIds, + datafileIds); result = handler.handle(); } @@ -407,49 +472,61 @@ public long getSize(@Context HttpServletRequest request, @QueryParam(RequestIdNa * investigationIds, datasetIds and datafileIds along with a sessionId. * * @title getStatus - * @param preparedId A valid preparedId returned by a call to prepareData - * @param sessionId A sessionId returned by a call to the icat server. If the - * sessionId is omitted or null the ids reader account will be - * used which has read access to all data. - * @param investigationIds If present, a comma separated list of investigation id values - * @param datasetIds If present, a comma separated list of data set id values or - * null - * @param datafileIds If present, a comma separated list of data file id values. + * @param preparedId A valid preparedId returned by a call to + * prepareData + * @param sessionId A sessionId returned by a call to the icat + * server. If the sessionId is omitted or null the + * ids reader account will be used which has read + * access to all data. + * @param investigationIds If present, a comma separated list of + * investigation id values + * @param datasetIds If present, a comma separated list of data set id + * values or null + * @param datafileIds If present, a comma separated list of data file + * id values. * @return a string with "ONLINE" if all data are online, "RESTORING" if one - * or more files are in the process of being restored but none are - * archived and no restoration has been requested or "ARCHIVED" if - * one or more files are archived and and no restoration has been - * requested. + * or more files are in the process of being restored but none are + * archived and no restoration has been requested or "ARCHIVED" if + * one or more files are archived and and no restoration has been + * requested. * @throws BadRequestException * @throws NotFoundException * @throws InsufficientPrivilegesException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getStatus") @Produces(MediaType.TEXT_PLAIN) - public String getStatus(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { - - // special case for getStatus request: getting status is possible without authentification + public String getStatus(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, + DataNotOnlineException, NotImplementedException { + + // special case for getStatus request: getting status is possible + // without authentification if (sessionId == null && preparedId == null) { try { - sessionId = ServiceProvider.getInstance().getIcatReader().getSessionId(); + sessionId = ServiceProvider.getInstance().getIcatReader() + .getSessionId(); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage()); } } - var handler = new GetStatusHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new GetStatusHandler(request.getRemoteAddr(), preparedId, + sessionId, investigationIds, datasetIds, datafileIds); return handler.handle().getString(); } - /** * Returns true if all the data files are ready to be downloaded. As a side * effect, if any data files are archived and no restoration has been @@ -467,39 +544,46 @@ public String getStatus(@Context HttpServletRequest request, @QueryParam(Request * @throws BadRequestException * @throws NotFoundException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws InsufficientPrivilegesException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws InsufficientPrivilegesException * @statuscode 200 To indicate success */ @GET @Path("isPrepared") @Produces(MediaType.TEXT_PLAIN) - public boolean isPrepared(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - - var handler = new IsPreparedHandler(request.getRemoteAddr(), preparedId); + public boolean isPrepared(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, DataNotOnlineException, + NotImplementedException { + + var handler = new IsPreparedHandler(request.getRemoteAddr(), + preparedId); return handler.handle().getBool(); } /** - * An ids server can be configured to be read only. This returns thenew DfProcessQueue() - * readOnly status of the server. + * An ids server can be configured to be read only. This returns thenew + * DfProcessQueue() readOnly status of the server. * * @title isReadOnly * @return true if readonly, else false - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("isReadOnly") @Produces(MediaType.TEXT_PLAIN) - public boolean isReadOnly(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public boolean isReadOnly(@Context HttpServletRequest request) + throws InternalException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { var handler = new IsReadOnlyHandler(request.getRemoteAddr()); return handler.handle().getBool(); @@ -511,18 +595,21 @@ public boolean isReadOnly(@Context HttpServletRequest request) throws InternalEx * * @title isTwoLevel * @return true if twoLevel, else false - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("isTwoLevel") @Produces(MediaType.TEXT_PLAIN) - public boolean isTwoLevel(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public boolean isTwoLevel(@Context HttpServletRequest request) + throws InternalException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { var handler = new IsTwoLevelHandler(request.getRemoteAddr()); return handler.handle().getBool(); @@ -551,37 +638,45 @@ public String ping() { * omitted, along with a sessionId. * * @title prepareData - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds A comma separated list of investigation id values. + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds A comma separated list of investigation id + * values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of datafile id values. - * @param compress If true use default compression otherwise no compression. This - * only applies if preparedId is not set and if the results are - * being zipped. - * @param zip If true the data should be zipped. If multiple files are - * requested (or could be because a datasetId or investigationId - * has been specified) the data are zipped regardless of the + * @param compress If true use default compression otherwise no + * compression. This only applies if preparedId is + * not set and if the results are being zipped. + * @param zip If true the data should be zipped. If multiple + * files are requested (or could be because a + * datasetId or investigationId has been specified) + * the data are zipped regardless of the * specification of this flag. * @return a string with the preparedId * @throws BadRequestException * @throws InsufficientPrivilegesException * @throws NotFoundException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("prepareData") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.TEXT_PLAIN) - public String prepareData(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds, @FormParam("compress") boolean compress, - @FormParam("zip") boolean zip) - throws BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, NotImplementedException, DataNotOnlineException { - - var handler = new PrepareDataHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds, compress, zip); + public String prepareData(@Context HttpServletRequest request, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds, + @FormParam("compress") boolean compress, + @FormParam("zip") boolean zip) throws BadRequestException, + InsufficientPrivilegesException, NotFoundException, + InternalException, NotImplementedException, DataNotOnlineException { + + var handler = new PrepareDataHandler(request.getRemoteAddr(), sessionId, + investigationIds, datasetIds, datafileIds, compress, zip); return handler.handle().getString(); } @@ -590,17 +685,23 @@ public String prepareData(@Context HttpServletRequest request, @FormParam(Reques * * @title put * @param body The contents of the file to be stored - * @param sessionId A sessionId returned by a call to the icat server. + * @param sessionId A sessionId returned by a call to the icat + * server. * @param name A name to assign to the data file - * @param datafileFormatId The id of the data file format to associate with the data file - * @param datasetId The id of the data set to which the data file should be - * associated. - * @param description An optional description to associate with the data file - * @param doi An optional description to associate with the data file - * @param datafileCreateTime An optional datafileCreateTime to associate with the data file - * @param datafileModTime An optional datafileModTime to associate with the data file + * @param datafileFormatId The id of the data file format to associate + * with the data file + * @param datasetId The id of the data set to which the data file + * should be associated. + * @param description An optional description to associate with the + * data file + * @param doi An optional description to associate with the + * data file + * @param datafileCreateTime An optional datafileCreateTime to associate + * with the data file + * @param datafileModTime An optional datafileModTime to associate with + * the data file * @return a json object with attributes of "id", "checksum", "location" and - * "size"; + * "size"; * @throws BadRequestException * @throws NotFoundException * @throws InternalException @@ -614,15 +715,21 @@ public String prepareData(@Context HttpServletRequest request, @FormParam(Reques @Consumes(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.APPLICATION_JSON) public Response put(@Context HttpServletRequest request, InputStream body, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("name") String name, - @QueryParam("datafileFormatId") String datafileFormatId, @QueryParam("datasetId") String datasetId, - @QueryParam("description") String description, @QueryParam("doi") String doi, - @QueryParam("datafileCreateTime") String datafileCreateTime, - @QueryParam("datafileModTime") String datafileModTime) throws BadRequestException, NotFoundException, - InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { - - var handler = new PutHandler(request.getRemoteAddr(), sessionId, body, name, datafileFormatId, datasetId, - description, doi, datafileCreateTime, datafileModTime, false, false); + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("name") String name, + @QueryParam("datafileFormatId") String datafileFormatId, + @QueryParam("datasetId") String datasetId, + @QueryParam("description") String description, + @QueryParam("doi") String doi, + @QueryParam("datafileCreateTime") String datafileCreateTime, + @QueryParam("datafileModTime") String datafileModTime) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, NotImplementedException, + DataNotOnlineException { + + var handler = new PutHandler(request.getRemoteAddr(), sessionId, body, + name, datafileFormatId, datasetId, description, doi, + datafileCreateTime, datafileModTime, false, false); return handler.handle().getResponse(); } @@ -636,7 +743,7 @@ public Response put(@Context HttpServletRequest request, InputStream body, * @title putAsPost * @param request * @return a json object with attributes of "id", "checksum", "location" and - * "size"; + * "size"; * @throws BadRequestException * @throws NotFoundException * @throws InternalException @@ -649,65 +756,73 @@ public Response put(@Context HttpServletRequest request, InputStream body, @Path("put") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) - public Response putAsPost(@Context HttpServletRequest request) throws BadRequestException, NotFoundException, - InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { - try { - String sessionId = null; - String name = null; - String datafileFormatId = null; - String datasetId = null; - String description = null; - String doi = null; - String datafileCreateTime = null; - String datafileModTime = null; - Response result = null; - boolean wrap = false; - boolean padding = false; - - // Parse the request - for (Part part : request.getParts()) { - String fieldName = part.getName(); - InputStream stream = part.getInputStream(); - if (part.getSubmittedFileName() == null) { - String value = new String(stream.readAllBytes(), StandardCharsets.UTF_8); - if (fieldName.equals("sessionId")) { - sessionId = value; - } else if (fieldName.equals("name")) { - name = value; - } else if (fieldName.equals("datafileFormatId")) { - datafileFormatId = value; - } else if (fieldName.equals("datasetId")) { - datasetId = value; - } else if (fieldName.equals("description")) { - description = value; - } else if (fieldName.equals("doi")) { - doi = value; - } else if (fieldName.equals("datafileCreateTime")) { - datafileCreateTime = value; - } else if (fieldName.equals("datafileModTime")) { - datafileModTime = value; - } else if (fieldName.equals("wrap")) { - wrap = (value != null && value.toUpperCase().equals("TRUE")); - } else if (fieldName.equals("padding")) { - padding = (value != null && value.toUpperCase().equals("TRUE")); - } else { - throw new BadRequestException("Form field " + fieldName + "is not recognised"); - } - } else { - if (name == null) { - name = part.getSubmittedFileName(); - } - var handler = new PutHandler(request.getRemoteAddr(), sessionId, stream, name, datafileFormatId, datasetId, description, - doi, datafileCreateTime, datafileModTime, wrap, padding); - result = handler.handle().getResponse(); - } + public Response putAsPost(@Context HttpServletRequest request) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, NotImplementedException, + DataNotOnlineException { + try { + String sessionId = null; + String name = null; + String datafileFormatId = null; + String datasetId = null; + String description = null; + String doi = null; + String datafileCreateTime = null; + String datafileModTime = null; + Response result = null; + boolean wrap = false; + boolean padding = false; + + // Parse the request + for (Part part : request.getParts()) { + String fieldName = part.getName(); + InputStream stream = part.getInputStream(); + if (part.getSubmittedFileName() == null) { + String value = new String(stream.readAllBytes(), + StandardCharsets.UTF_8); + if (fieldName.equals("sessionId")) { + sessionId = value; + } else if (fieldName.equals("name")) { + name = value; + } else if (fieldName.equals("datafileFormatId")) { + datafileFormatId = value; + } else if (fieldName.equals("datasetId")) { + datasetId = value; + } else if (fieldName.equals("description")) { + description = value; + } else if (fieldName.equals("doi")) { + doi = value; + } else if (fieldName.equals("datafileCreateTime")) { + datafileCreateTime = value; + } else if (fieldName.equals("datafileModTime")) { + datafileModTime = value; + } else if (fieldName.equals("wrap")) { + wrap = (value != null + && value.toUpperCase().equals("TRUE")); + } else if (fieldName.equals("padding")) { + padding = (value != null + && value.toUpperCase().equals("TRUE")); + } else { + throw new BadRequestException("Form field " + fieldName + + "is not recognised"); + } + } else { + if (name == null) { + name = part.getSubmittedFileName(); } - return result; - } catch (IOException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); - } catch (ServletException e) { - throw new BadRequestException("Multipart content expected"); + var handler = new PutHandler(request.getRemoteAddr(), + sessionId, stream, name, datafileFormatId, + datasetId, description, doi, datafileCreateTime, + datafileModTime, wrap, padding); + result = handler.handle().getResponse(); } + } + return result; + } catch (IOException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (ServletException e) { + throw new BadRequestException("Multipart content expected"); + } } /** @@ -722,9 +837,12 @@ public Response putAsPost(@Context HttpServletRequest request) throws BadRequest * specified then investigationIds, datasetIds and datafileIds are not used. * * @title reset - * @param preparedId A valid preparedId returned by a call to prepareData - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds A comma separated list of investigation id values. + * @param preparedId A valid preparedId returned by a call to + * prepareData + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds A comma separated list of investigation id + * values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of data file id values. * @return a stream of json data. @@ -732,18 +850,24 @@ public Response putAsPost(@Context HttpServletRequest request) throws BadRequest * @throws NotFoundException * @throws InternalException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("reset") - public void reset(@Context HttpServletRequest request, @FormParam(RequestIdNames.preparedId) String preparedId, - @FormParam(RequestIdNames.sessionId) String sessionId, @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, @FormParam("datafileIds") String datafileIds) - throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - - var handler = new ResetHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public void reset(@Context HttpServletRequest request, + @FormParam(RequestIdNames.preparedId) String preparedId, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws BadRequestException, InternalException, NotFoundException, + InsufficientPrivilegesException, DataNotOnlineException, + NotImplementedException { + + var handler = new ResetHandler(request.getRemoteAddr(), preparedId, + sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } @@ -754,45 +878,53 @@ public void reset(@Context HttpServletRequest request, @FormParam(RequestIdNames * in use this has no effect. * * @title restore - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds If present, a comma separated list of investigation id values - * @param datasetIds If present, a comma separated list of data set id values or - * null - * @param datafileIds If present, a comma separated list of datafile id values. + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds If present, a comma separated list of + * investigation id values + * @param datasetIds If present, a comma separated list of data set id + * values or null + * @param datafileIds If present, a comma separated list of datafile id + * values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException * @throws InternalException * @throws NotFoundException - * @throws DataNotOnlineException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("restore") @Consumes("application/x-www-form-urlencoded") - public void restore(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, + public void restore(@Context HttpServletRequest request, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new RestoreHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + var handler = new RestoreHandler(request.getRemoteAddr(), sessionId, + investigationIds, datasetIds, datafileIds); handler.handle(); } - /** - * Write data specified by the investigationIds, datasetIds - * and datafileIds specified along with a sessionId to archive - * storage. If two level storage is not in use this has no - * effect. + * Write data specified by the investigationIds, datasetIds and datafileIds + * specified along with a sessionId to archive storage. If two level storage + * is not in use this has no effect. * * @title write - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds If present, a comma separated list of investigation id values - * @param datasetIds If present, a comma separated list of data set id values or - * null - * @param datafileIds If present, a comma separated list of datafile id values. + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds If present, a comma separated list of + * investigation id values + * @param datasetIds If present, a comma separated list of data set id + * values or null + * @param datafileIds If present, a comma separated list of datafile id + * values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException @@ -803,13 +935,17 @@ public void restore(@Context HttpServletRequest request, @FormParam(RequestIdNam @POST @Path("write") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void write(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, + public void write(@Context HttpServletRequest request, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new WriteHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + var handler = new WriteHandler(request.getRemoteAddr(), sessionId, + investigationIds, datasetIds, datafileIds); handler.handle(); } } diff --git a/src/main/java/org/icatproject/ids/Tidier.java b/src/main/java/org/icatproject/ids/Tidier.java index 9d647de0..7f75328f 100644 --- a/src/main/java/org/icatproject/ids/Tidier.java +++ b/src/main/java/org/icatproject/ids/Tidier.java @@ -55,57 +55,77 @@ public void run() { if (twoLevel) { if (storageUnit == StorageUnit.DATASET) { - List dsInfos = mainStorage.getDatasetsToArchive(stopArchivingLevel, - startArchivingLevel); + List dsInfos = mainStorage.getDatasetsToArchive( + stopArchivingLevel, startArchivingLevel); for (DsInfo dsInfo : dsInfos) { StringBuilder sb = new StringBuilder( "SELECT ds FROM Dataset ds, ds.investigation inv, inv.facility fac WHERE"); boolean andNeeded = false; - andNeeded = addNumericConstraint(sb, "ds.id", dsInfo.getDsId(), andNeeded); - andNeeded = addStringConstraint(sb, "ds.location", dsInfo.getDsLocation(), andNeeded); - andNeeded = addStringConstraint(sb, "ds.name", dsInfo.getDsName(), andNeeded); - - andNeeded = addNumericConstraint(sb, "inv.id", dsInfo.getInvId(), andNeeded); - andNeeded = addStringConstraint(sb, "inv.name", dsInfo.getInvName(), andNeeded); - andNeeded = addStringConstraint(sb, "inv.visitId", dsInfo.getVisitId(), andNeeded); - - andNeeded = addStringConstraint(sb, "fac.name", dsInfo.getFacilityName(), andNeeded); - andNeeded = addNumericConstraint(sb, "fac.id", dsInfo.getFacilityId(), andNeeded); + andNeeded = addNumericConstraint(sb, "ds.id", + dsInfo.getDsId(), andNeeded); + andNeeded = addStringConstraint(sb, "ds.location", + dsInfo.getDsLocation(), andNeeded); + andNeeded = addStringConstraint(sb, "ds.name", + dsInfo.getDsName(), andNeeded); + + andNeeded = addNumericConstraint(sb, "inv.id", + dsInfo.getInvId(), andNeeded); + andNeeded = addStringConstraint(sb, "inv.name", + dsInfo.getInvName(), andNeeded); + andNeeded = addStringConstraint(sb, "inv.visitId", + dsInfo.getVisitId(), andNeeded); + + andNeeded = addStringConstraint(sb, "fac.name", + dsInfo.getFacilityName(), andNeeded); + andNeeded = addNumericConstraint(sb, "fac.id", + dsInfo.getFacilityId(), andNeeded); sb.append(" INCLUDE ds.investigation.facility"); try { int low = 0; while (true) { - String query = sb.toString() + " LIMIT " + low + "," + tidyBlockSize; + String query = sb.toString() + " LIMIT " + + low + "," + tidyBlockSize; List os = reader.search(query); - logger.debug(query + " returns " + os.size() + " datasets"); + logger.debug(query + " returns " + os.size() + + " datasets"); for (Object o : os) { - DatasetInfo dsInfoImpl = new DatasetInfo((Dataset) o); - logger.debug( - "Requesting archive of " + dsInfoImpl + " to recover main storage"); - fsm.queue(dsInfoImpl, DeferredOp.ARCHIVE); + DatasetInfo dsInfoImpl = new DatasetInfo( + (Dataset) o); + logger.debug("Requesting archive of " + + dsInfoImpl + + " to recover main storage"); + fsm.queue(dsInfoImpl, + DeferredOp.ARCHIVE); } if (os.size() < tidyBlockSize) { break; } low += tidyBlockSize; } - } catch (InternalException | IcatException_Exception | InsufficientPrivilegesException e) { + } catch (InternalException | IcatException_Exception + | InsufficientPrivilegesException e) { // Log it and carry on - logger.error(e.getClass() + " " + e.getMessage()); + logger.error( + e.getClass() + " " + e.getMessage()); } } } else if (storageUnit == StorageUnit.DATAFILE) { - List dfInfos = mainStorage.getDatafilesToArchive(stopArchivingLevel, - startArchivingLevel); + List dfInfos = mainStorage + .getDatafilesToArchive(stopArchivingLevel, + startArchivingLevel); for (DfInfo dfInfo : dfInfos) { - StringBuilder sb = new StringBuilder("SELECT df FROM Datafile df WHERE"); + StringBuilder sb = new StringBuilder( + "SELECT df FROM Datafile df WHERE"); boolean andNeeded = false; - andNeeded = addNumericConstraint(sb, "df.id", dfInfo.getDfId(), andNeeded); - andNeeded = addStringConstraint(sb, "df.createId", dfInfo.getCreateId(), andNeeded); - andNeeded = addStringConstraint(sb, "df.modId", dfInfo.getModId(), andNeeded); + andNeeded = addNumericConstraint(sb, "df.id", + dfInfo.getDfId(), andNeeded); + andNeeded = addStringConstraint(sb, "df.createId", + dfInfo.getCreateId(), andNeeded); + andNeeded = addStringConstraint(sb, "df.modId", + dfInfo.getModId(), andNeeded); if (key != null) { if (dfInfo.getDfLocation() != null) { if (andNeeded) { @@ -114,42 +134,54 @@ public void run() { sb.append(" "); andNeeded = true; } - sb.append("df.location" + " LIKE '" + dfInfo.getDfLocation() + " %'"); + sb.append("df.location" + " LIKE '" + + dfInfo.getDfLocation() + " %'"); } } else { - andNeeded = addStringConstraint(sb, "df.location", + andNeeded = addStringConstraint(sb, + "df.location", dfInfo.getDfLocation(), andNeeded); } - andNeeded = addStringConstraint(sb, "df.name", dfInfo.getDfName(), andNeeded); + andNeeded = addStringConstraint(sb, "df.name", + dfInfo.getDfName(), andNeeded); sb.append(" INCLUDE df.dataset"); try { int low = 0; while (true) { - String query = sb.toString() + " LIMIT " + low + "," + tidyBlockSize; + String query = sb.toString() + " LIMIT " + + low + "," + tidyBlockSize; List os = reader.search(query); - logger.debug(query + " returns " + os.size() + " datafiles"); + logger.debug(query + " returns " + os.size() + + " datafiles"); for (Object o : os) { Datafile df = (Datafile) o; - DatafileInfo dfInfoImpl = new DatafileInfo(df.getId(), df.getName(), - - LocationHelper.getLocation(df.getId(), df.getLocation()), df.getCreateId(), - df.getModId(), df.getDataset().getId()); - - - logger.debug( - "Requesting archive of " + dfInfoImpl + " to recover main storage"); - fsm.queue(dfInfoImpl, DeferredOp.ARCHIVE); + DatafileInfo dfInfoImpl = new DatafileInfo( + df.getId(), df.getName(), + + LocationHelper.getLocation( + df.getId(), + df.getLocation()), + df.getCreateId(), df.getModId(), + df.getDataset().getId()); + + logger.debug("Requesting archive of " + + dfInfoImpl + + " to recover main storage"); + fsm.queue(dfInfoImpl, + DeferredOp.ARCHIVE); } if (os.size() < tidyBlockSize) { break; } low += tidyBlockSize; } - } catch (InternalException | IcatException_Exception | InsufficientPrivilegesException e) { + } catch (InternalException | IcatException_Exception + | InsufficientPrivilegesException e) { // Log it and carry on - logger.error(e.getClass() + " " + e.getMessage()); + logger.error( + e.getClass() + " " + e.getMessage()); } } } @@ -165,7 +197,8 @@ public void run() { private final static Logger logger = LoggerFactory.getLogger(Tidier.class); - static boolean addStringConstraint(StringBuilder sb, String var, String value, boolean andNeeded) { + static boolean addStringConstraint(StringBuilder sb, String var, + String value, boolean andNeeded) { if (value != null) { if (andNeeded) { sb.append(" AND "); @@ -178,7 +211,8 @@ static boolean addStringConstraint(StringBuilder sb, String var, String value, b return andNeeded; } - static boolean addNumericConstraint(StringBuilder sb, String var, Long value, boolean andNeeded) { + static boolean addNumericConstraint(StringBuilder sb, String var, + Long value, boolean andNeeded) { if (value != null) { if (andNeeded) { sb.append(" AND "); @@ -191,7 +225,8 @@ static boolean addNumericConstraint(StringBuilder sb, String var, Long value, bo return andNeeded; } - static void cleanPreparedDir(Path preparedDir, int preparedCount) throws IOException { + static void cleanPreparedDir(Path preparedDir, int preparedCount) + throws IOException { Map dateMap = new HashMap<>(); File[] files = preparedDir.toFile().listFiles(); @@ -251,11 +286,13 @@ public void init() { try { PropertyHandler propertyHandler = PropertyHandler.getInstance(); - FiniteStateMachine.createInstance(reader, lockManager, propertyHandler.getStorageUnit()); + FiniteStateMachine.createInstance(reader, lockManager, + propertyHandler.getStorageUnit()); this.fsm = FiniteStateMachine.getInstance(); - this.fsm.init(); //if not yet initialized by IdsService do it now - - sizeCheckIntervalMillis = propertyHandler.getSizeCheckIntervalMillis(); + this.fsm.init(); // if not yet initialized by IdsService do it now + + sizeCheckIntervalMillis = propertyHandler + .getSizeCheckIntervalMillis(); preparedCount = propertyHandler.getPreparedCount(); preparedDir = propertyHandler.getCacheDir().resolve("prepared"); Files.createDirectories(preparedDir); @@ -273,7 +310,8 @@ public void init() { logger.info("Tidier started"); } catch (IOException e) { - throw new RuntimeException("Tidier reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException( + "Tidier reports " + e.getClass() + " " + e.getMessage()); } } diff --git a/src/main/java/org/icatproject/ids/enums/RequestType.java b/src/main/java/org/icatproject/ids/enums/RequestType.java index a79f83e5..1f4c0b77 100644 --- a/src/main/java/org/icatproject/ids/enums/RequestType.java +++ b/src/main/java/org/icatproject/ids/enums/RequestType.java @@ -4,20 +4,7 @@ * This enum contains all defined types of requests to this server */ public enum RequestType { - GETDATA, - ARCHIVE, - GETICATURL, - GETDATAFILEIDS, - GETSERVICESTATUS, - GETSIZE, - GETSTATUS, - ISPREPARED, - ISREADONLY, - ISTWOLEVEL, - PREPAREDATA, - PUT, - RESET, - RESTORE, - WRITE, - DELETE + GETDATA, ARCHIVE, GETICATURL, GETDATAFILEIDS, GETSERVICESTATUS, GETSIZE, + GETSTATUS, ISPREPARED, ISREADONLY, ISTWOLEVEL, PREPAREDATA, PUT, RESET, + RESTORE, WRITE, DELETE } diff --git a/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java index 77920041..0c9567db 100644 --- a/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java @@ -9,9 +9,9 @@ import jakarta.ws.rs.ext.Provider; /** - * Capture any {@link org.icatproject.ids.exceptions.IdsException WebServiceException} thrown from - * {@link org.icatproject.ids.IdsService WebService} and generate the appropriate response code and - * message. + * Capture any {@link org.icatproject.ids.exceptions.IdsException + * WebServiceException} thrown from {@link org.icatproject.ids.IdsService + * WebService} and generate the appropriate response code and message. */ @Provider public class IdsExceptionMapper implements ExceptionMapper { @@ -23,6 +23,7 @@ public Response toResponse(IdsException e) { gen.writeStartObject().write("code", e.getClass().getSimpleName()) .write("message", e.getShortMessage()); gen.writeEnd().close(); - return Response.status(e.getHttpStatusCode()).entity(baos.toString()).build(); + return Response.status(e.getHttpStatusCode()).entity(baos.toString()) + .build(); } } diff --git a/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java index f9157c92..266b2e8a 100644 --- a/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java @@ -13,9 +13,11 @@ import org.slf4j.LoggerFactory; @Provider -public class NotFoundExceptionMapper implements ExceptionMapper { +public class NotFoundExceptionMapper + implements ExceptionMapper { - private static Logger logger = LoggerFactory.getLogger(NotFoundExceptionMapper.class); + private static Logger logger = LoggerFactory + .getLogger(NotFoundExceptionMapper.class); @Override public Response toResponse(NotFoundException e) { @@ -23,7 +25,10 @@ public Response toResponse(NotFoundException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); gen.writeStartObject().write("code", "NOT_IMPLEMENTED") - .write("message", "Operation not implemented by this IDS server.").writeEnd().close(); - return Response.status(Response.Status.NOT_IMPLEMENTED).entity(baos.toString()).build(); + .write("message", + "Operation not implemented by this IDS server.") + .writeEnd().close(); + return Response.status(Response.Status.NOT_IMPLEMENTED) + .entity(baos.toString()).build(); } } diff --git a/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java index aa38ba0a..55249b31 100644 --- a/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java @@ -14,9 +14,11 @@ import org.slf4j.LoggerFactory; @Provider -public class RuntimeExceptionMapper implements ExceptionMapper { +public class RuntimeExceptionMapper + implements ExceptionMapper { - private final static Logger logger = LoggerFactory.getLogger(RuntimeExceptionMapper.class); + private final static Logger logger = LoggerFactory + .getLogger(RuntimeExceptionMapper.class); @Override public Response toResponse(RuntimeException e) { @@ -27,8 +29,9 @@ public Response toResponse(RuntimeException e) { baos.reset(); JsonGenerator gen = Json.createGenerator(baos); gen.writeStartObject().write("code", "InternalException") - .write("message", e.getClass() + " " + e.getMessage()).writeEnd().close(); - return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR).entity(baos.toString()) - .build(); + .write("message", e.getClass() + " " + e.getMessage()) + .writeEnd().close(); + return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR) + .entity(baos.toString()).build(); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java index f115c8da..4e9d4c20 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java @@ -29,7 +29,6 @@ public abstract class FiniteStateMachine { - private static FiniteStateMachine instance; private static Boolean inited = false; @@ -39,40 +38,52 @@ protected FiniteStateMachine(IcatReader reader, LockManager lockManager) { this.lockManager = lockManager; } - public static void createInstance(IcatReader reader, LockManager lockManager, StorageUnit storageUnit) { + public static void createInstance(IcatReader reader, + LockManager lockManager, StorageUnit storageUnit) { - if(instance == null) { - if(storageUnit == StorageUnit.DATAFILE) - instance = new FiniteStateMachineForStorageUnitDatafile(reader, lockManager); - else if(storageUnit == StorageUnit.DATASET) - instance = new FiniteStateMachineForStorageUnitDataset(reader, lockManager); + if (instance == null) { + if (storageUnit == StorageUnit.DATAFILE) + instance = new FiniteStateMachineForStorageUnitDatafile(reader, + lockManager); + else if (storageUnit == StorageUnit.DATASET) + instance = new FiniteStateMachineForStorageUnitDataset(reader, + lockManager); else - instance = new FiniteStateMachineForSingleLevelStorage(reader, lockManager); + instance = new FiniteStateMachineForSingleLevelStorage(reader, + lockManager); } } public static FiniteStateMachine getInstance() { - if(instance != null) { + if (instance != null) { return instance; } - // If this assert was executed: Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once. - throw new RuntimeException("Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once."); + // If this assert was executed: Instance of FiniteStateMachine is not + // created. At First createInstance() has to be called at least once. + throw new RuntimeException( + "Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once."); } - public abstract void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException; + public abstract void queue(DataInfoBase dataInfo, DeferredOp deferredOp) + throws InternalException; + protected abstract void scheduleTimer(); + protected abstract void addDataInfoJson(JsonGenerator gen); public enum RequestedState { - ARCHIVE_REQUESTED, DELETE_REQUESTED, RESTORE_REQUESTED, WRITE_REQUESTED, WRITE_THEN_ARCHIVE_REQUESTED + ARCHIVE_REQUESTED, DELETE_REQUESTED, RESTORE_REQUESTED, WRITE_REQUESTED, + WRITE_THEN_ARCHIVE_REQUESTED } - protected static Logger logger = LoggerFactory.getLogger(FiniteStateMachine.class); + protected static Logger logger = LoggerFactory + .getLogger(FiniteStateMachine.class); /* - * Note that the veriable processOpsDelayMillis is used to either delay all deferred - * datafile operations or to delay dataset writes, depending on the setting of storageUnit. + * Note that the veriable processOpsDelayMillis is used to either delay all + * deferred datafile operations or to delay dataset writes, depending on the + * setting of storageUnit. */ protected long processOpsDelayMillis; @@ -119,7 +130,8 @@ public Set getMaybeOffline() { } /** - * Find any DataFileInfo which are being restored or are queued for restoration + * Find any DataFileInfo which are being restored or are queued for + * restoration */ public Set getRestoring() { Map union; @@ -145,23 +157,26 @@ protected void addDataInfoJsonFromDeferredOpsQueue(JsonGenerator gen) { gen.writeStartArray("opsQueue"); for (Entry entry : union.entrySet()) { DataInfoBase item = entry.getKey(); - gen.writeStartObject().write("data", item.toString()).write("request", entry.getValue().name()) - .writeEnd(); + gen.writeStartObject().write("data", item.toString()) + .write("request", entry.getValue().name()).writeEnd(); } gen.writeEnd(); // end Array("opsQueue") } public String getServiceStatus() throws InternalException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { - + try (JsonGenerator gen = Json.createGenerator(baos) + .writeStartObject()) { + this.addDataInfoJson(gen); Collection lockInfo = lockManager.getLockInfo(); gen.write("lockCount", lockInfo.size()); gen.writeStartArray("locks"); for (LockInfo li : lockInfo) { - gen.writeStartObject().write("id", li.id).write("type", li.type.name()).write("count", li.count).writeEnd(); + gen.writeStartObject().write("id", li.id) + .write("type", li.type.name()).write("count", li.count) + .writeEnd(); } gen.writeEnd(); // end Array("locks") @@ -176,24 +191,25 @@ public String getServiceStatus() throws InternalException { return baos.toString(); } - public void init() { try { synchronized (inited) { - if(!inited) { + if (!inited) { propertyHandler = PropertyHandler.getInstance(); - processQueueIntervalMillis = propertyHandler.getProcessQueueIntervalSeconds() * 1000L; + processQueueIntervalMillis = propertyHandler + .getProcessQueueIntervalSeconds() * 1000L; this.scheduleTimer(); - + markerDir = propertyHandler.getCacheDir().resolve("marker"); Files.createDirectories(markerDir); inited = true; } } } catch (IOException e) { - throw new RuntimeException("FiniteStateMachine reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException("FiniteStateMachine reports " + + e.getClass() + " " + e.getMessage()); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java index 7ffb349d..9a96776a 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java @@ -8,25 +8,26 @@ import jakarta.json.stream.JsonGenerator; -public class FiniteStateMachineForSingleLevelStorage extends FiniteStateMachine { +public class FiniteStateMachineForSingleLevelStorage + extends FiniteStateMachine { - protected FiniteStateMachineForSingleLevelStorage(IcatReader reader, LockManager lockManager) { + protected FiniteStateMachineForSingleLevelStorage(IcatReader reader, + LockManager lockManager) { super(reader, lockManager); } - @Override - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { - throw new InternalException("### Operation is not permitted for single level storage"); + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) + throws InternalException { + throw new InternalException( + "### Operation is not permitted for single level storage"); } - @Override protected void scheduleTimer() { - //nothing to do here for single level storage + // nothing to do here for single level storage } - @Override protected void addDataInfoJson(JsonGenerator gen) { gen.writeStartArray("opsQueue").writeEnd(); diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java index 88543bb1..b634d39c 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java @@ -32,38 +32,43 @@ import jakarta.json.stream.JsonGenerator; -public class FiniteStateMachineForStorageUnitDatafile extends FiniteStateMachine { +public class FiniteStateMachineForStorageUnitDatafile + extends FiniteStateMachine { - protected FiniteStateMachineForStorageUnitDatafile(IcatReader icatReader, LockManager lockManager) { + protected FiniteStateMachineForStorageUnitDatafile(IcatReader icatReader, + LockManager lockManager) { super(icatReader, lockManager); } - @Override protected void scheduleTimer() { - processOpsDelayMillis = propertyHandler.getDelayDatafileOperations() * 1000L; + processOpsDelayMillis = propertyHandler.getDelayDatafileOperations() + * 1000L; timer.schedule(new DfProcessQueue(), processQueueIntervalMillis); - logger.info("DfProcessQueue scheduled to run in " + processQueueIntervalMillis + " milliseconds"); + logger.info("DfProcessQueue scheduled to run in " + + processQueueIntervalMillis + " milliseconds"); } - @Override protected void addDataInfoJson(JsonGenerator gen) { this.addDataInfoJsonFromDeferredOpsQueue(gen); } - - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) + throws InternalException { var dfInfo = (DatafileInfo) dataInfo; - if(dfInfo == null) throw new InternalException("DataInfoBase object could not be casted into a DataFileInfo. Did you handed over a DataSetInfo instead?"); + if (dfInfo == null) + throw new InternalException( + "DataInfoBase object could not be casted into a DataFileInfo. Did you handed over a DataSetInfo instead?"); logger.info("Requesting " + deferredOp + " of datafile " + dfInfo); synchronized (deferredOpsQueue) { if (processOpsTime == null) { - processOpsTime = System.currentTimeMillis() + processOpsDelayMillis; + processOpsTime = System.currentTimeMillis() + + processOpsDelayMillis; final Date d = new Date(processOpsTime); logger.debug("Requesting delay operations till " + d); } @@ -72,62 +77,75 @@ public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalE if (state == null) { if (deferredOp == DeferredOp.WRITE) { try { - Path marker = markerDir.resolve(Long.toString(dfInfo.getId())); + Path marker = markerDir + .resolve(Long.toString(dfInfo.getId())); Files.createFile(marker); logger.debug("Created marker " + marker); } catch (FileAlreadyExistsException e) { // Pass will ignore this } catch (IOException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } - deferredOpsQueue.put(dfInfo, RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.WRITE_REQUESTED); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dfInfo, RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.RESTORE_REQUESTED); } else if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.DELETE_REQUESTED); } } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.RESTORE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.DELETE_REQUESTED); } } else if (state == RequestedState.DELETE_REQUESTED) { // No way out } else if (state == RequestedState.RESTORE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.DELETE_REQUESTED); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dfInfo, RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.WRITE_REQUESTED); } } } } - private class DfProcessQueue extends TimerTask { @Override public void run() { try { synchronized (deferredOpsQueue) { - if (processOpsTime != null && System.currentTimeMillis() > processOpsTime && !deferredOpsQueue.isEmpty()) { + if (processOpsTime != null + && System.currentTimeMillis() > processOpsTime + && !deferredOpsQueue.isEmpty()) { processOpsTime = null; - logger.debug("deferredDfOpsQueue has " + deferredOpsQueue.size() + " entries"); + logger.debug("deferredDfOpsQueue has " + + deferredOpsQueue.size() + " entries"); List writes = new ArrayList<>(); List archives = new ArrayList<>(); List restores = new ArrayList<>(); @@ -138,20 +156,27 @@ public void run() { Map deleteLocks = new HashMap<>(); Map newOps = new HashMap<>(); - final Iterator> it = deferredOpsQueue.entrySet().iterator(); + final Iterator> it = deferredOpsQueue + .entrySet().iterator(); while (it.hasNext()) { - Entry opEntry = it.next(); + Entry opEntry = it + .next(); var dfInfo = (DatafileInfo) opEntry.getKey(); - if(dfInfo == null) throw new RuntimeException("Could not cast DataInfoBase to DataFileInfo. Did you handed over another sub type?"); + if (dfInfo == null) + throw new RuntimeException( + "Could not cast DataInfoBase to DataFileInfo. Did you handed over another sub type?"); Long dsId = dfInfo.getDsId(); DatasetInfo dsInfo; try { - Dataset ds = (Dataset) reader.get("Dataset ds INCLUDE ds.investigation.facility", dsId); + Dataset ds = (Dataset) reader.get( + "Dataset ds INCLUDE ds.investigation.facility", + dsId); dsInfo = new DatasetInfo(ds); } catch (Exception e) { - logger.error("Could not get dsInfo {}: {}.", dsId, e.getMessage()); + logger.error("Could not get dsInfo {}: {}.", + dsId, e.getMessage()); continue; } if (!dataInfoChanging.containsKey(dfInfo)) { @@ -160,12 +185,20 @@ public void run() { if (state == RequestedState.WRITE_REQUESTED) { if (!writeLocks.containsKey(dsId)) { try { - writeLocks.put(dsId, lockManager.lock(dsInfo, LockType.SHARED)); + writeLocks.put(dsId, + lockManager.lock(dsInfo, + LockType.SHARED)); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error("I/O exception " + + e.getMessage() + + " locking " + dsId); continue; } } @@ -175,28 +208,46 @@ public void run() { } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (!writeLocks.containsKey(dsId)) { try { - writeLocks.put(dsId, lockManager.lock(dsInfo, LockType.SHARED)); + writeLocks.put(dsId, + lockManager.lock(dsInfo, + LockType.SHARED)); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error("I/O exception " + + e.getMessage() + + " locking " + dsId); continue; } } it.remove(); - dataInfoChanging.put(dfInfo, RequestedState.WRITE_REQUESTED); + dataInfoChanging.put(dfInfo, + RequestedState.WRITE_REQUESTED); writes.add(dfInfo); - newOps.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); + newOps.put(dfInfo, + RequestedState.ARCHIVE_REQUESTED); } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (!archiveLocks.containsKey(dsId)) { try { - archiveLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); + archiveLocks.put(dsId, + lockManager.lock(dsInfo, + LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error("I/O exception " + + e.getMessage() + + " locking " + dsId); continue; } } @@ -206,12 +257,20 @@ public void run() { } else if (state == RequestedState.RESTORE_REQUESTED) { if (!restoreLocks.containsKey(dsId)) { try { - restoreLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); + restoreLocks.put(dsId, + lockManager.lock(dsInfo, + LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error("I/O exception " + + e.getMessage() + + " locking " + dsId); continue; } } @@ -221,12 +280,20 @@ public void run() { } else if (state == RequestedState.DELETE_REQUESTED) { if (!deleteLocks.containsKey(dsId)) { try { - deleteLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); + deleteLocks.put(dsId, + lockManager.lock(dsInfo, + LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error("I/O exception " + + e.getMessage() + + " locking " + dsId); continue; } } @@ -234,41 +301,61 @@ public void run() { dataInfoChanging.put(dfInfo, state); deletes.add(dfInfo); } else { - throw new AssertionError("Impossible state"); + throw new AssertionError( + "Impossible state"); } } } if (!newOps.isEmpty()) { deferredOpsQueue.putAll(newOps); - logger.debug("Adding {} operations to be scheduled next time round", newOps.size()); + logger.debug( + "Adding {} operations to be scheduled next time round", + newOps.size()); } if (!deferredOpsQueue.isEmpty()) { processOpsTime = 0L; } if (!writes.isEmpty()) { - logger.debug("Launch thread to process " + writes.size() + " writes"); - Thread w = new Thread(new DfWriter(writes, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, writeLocks.values())); + logger.debug("Launch thread to process " + + writes.size() + " writes"); + Thread w = new Thread(new DfWriter(writes, + propertyHandler, + FiniteStateMachineForStorageUnitDatafile.this, + writeLocks.values())); w.start(); } if (!archives.isEmpty()) { - logger.debug("Launch thread to process " + archives.size() + " archives"); - Thread w = new Thread(new DfArchiver(archives, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, archiveLocks.values())); + logger.debug("Launch thread to process " + + archives.size() + " archives"); + Thread w = new Thread(new DfArchiver(archives, + propertyHandler, + FiniteStateMachineForStorageUnitDatafile.this, + archiveLocks.values())); w.start(); } if (!restores.isEmpty()) { - logger.debug("Launch thread to process " + restores.size() + " restores"); - Thread w = new Thread(new DfRestorer(restores, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, restoreLocks.values())); + logger.debug("Launch thread to process " + + restores.size() + " restores"); + Thread w = new Thread(new DfRestorer(restores, + propertyHandler, + FiniteStateMachineForStorageUnitDatafile.this, + restoreLocks.values())); w.start(); } if (!deletes.isEmpty()) { - logger.debug("Launch thread to process " + deletes.size() + " deletes"); - Thread w = new Thread(new DfDeleter(deletes, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, deleteLocks.values())); + logger.debug("Launch thread to process " + + deletes.size() + " deletes"); + Thread w = new Thread(new DfDeleter(deletes, + propertyHandler, + FiniteStateMachineForStorageUnitDatafile.this, + deleteLocks.values())); w.start(); } } } } finally { - timer.schedule(new DfProcessQueue(), processQueueIntervalMillis); + timer.schedule(new DfProcessQueue(), + processQueueIntervalMillis); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java index cbe0a5cd..9120dee5 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java @@ -26,20 +26,22 @@ import jakarta.json.stream.JsonGenerator; -public class FiniteStateMachineForStorageUnitDataset extends FiniteStateMachine { +public class FiniteStateMachineForStorageUnitDataset + extends FiniteStateMachine { protected Map writeTimes = new HashMap<>(); - protected FiniteStateMachineForStorageUnitDataset(IcatReader reader, LockManager lockManager) { + protected FiniteStateMachineForStorageUnitDataset(IcatReader reader, + LockManager lockManager) { super(reader, lockManager); } - @Override protected void scheduleTimer() { processOpsDelayMillis = propertyHandler.getDelayDatasetWrites() * 1000L; timer.schedule(new DsProcessQueue(), processQueueIntervalMillis); - logger.info("DsProcessQueue scheduled to run in " + processQueueIntervalMillis + " milliseconds"); + logger.info("DsProcessQueue scheduled to run in " + + processQueueIntervalMillis + " milliseconds"); } @Override @@ -47,9 +49,8 @@ protected void addDataInfoJson(JsonGenerator gen) { this.addDataInfoJsonFromDeferredOpsQueue(gen); } - - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { - + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) + throws InternalException { logger.info("Requesting " + deferredOp + " of dataset " + dataInfo); @@ -60,41 +61,47 @@ public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalE if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.RESTORE_REQUESTED); } } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); - deferredOpsQueue.put(dataInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.RESTORE_REQUESTED); } } else if (state == RequestedState.RESTORE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { setDelay(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { setDelay(dataInfo); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.WRITE_REQUESTED); } } } } - private void requestWrite(DataInfoBase dsInfo) throws InternalException { try { Path marker = markerDir.resolve(Long.toString(dsInfo.getId())); @@ -109,16 +116,16 @@ private void requestWrite(DataInfoBase dsInfo) throws InternalException { setDelay(dsInfo); } - private void setDelay(DataInfoBase dsInfo) { - writeTimes.put(dsInfo, System.currentTimeMillis() + processOpsDelayMillis); + writeTimes.put(dsInfo, + System.currentTimeMillis() + processOpsDelayMillis); if (logger.isDebugEnabled()) { final Date d = new Date(writeTimes.get(dsInfo)); - logger.debug("Requesting delay of writing of dataset " + dsInfo + " till " + d); + logger.debug("Requesting delay of writing of dataset " + dsInfo + + " till " + d); } } - private class DsProcessQueue extends TimerTask { @Override @@ -127,13 +134,16 @@ public void run() { synchronized (deferredOpsQueue) { final long now = System.currentTimeMillis(); Map newOps = new HashMap<>(); - final Iterator> it = deferredOpsQueue.entrySet().iterator(); + final Iterator> it = deferredOpsQueue + .entrySet().iterator(); while (it.hasNext()) { - final Entry opEntry = it.next(); + final Entry opEntry = it + .next(); final var dsInfo = (DatasetInfo) opEntry.getKey(); - - if(dsInfo == null) throw new RuntimeException("Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); + if (dsInfo == null) + throw new RuntimeException( + "Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); if (!dataInfoChanging.containsKey(dsInfo)) { final RequestedState state = opEntry.getValue(); @@ -141,50 +151,81 @@ public void run() { || state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (now > writeTimes.get(dsInfo)) { try { - Lock lock = lockManager.lock(dsInfo, LockType.SHARED); - logger.debug("Will process " + dsInfo + " with " + state); + Lock lock = lockManager.lock(dsInfo, + LockType.SHARED); + logger.debug("Will process " + dsInfo + + " with " + state); writeTimes.remove(dsInfo); - dataInfoChanging.put(dsInfo, RequestedState.WRITE_REQUESTED); + dataInfoChanging.put(dsInfo, + RequestedState.WRITE_REQUESTED); it.remove(); final Thread w = new Thread( - new DsWriter(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, reader, lock)); + new DsWriter(dsInfo, + propertyHandler, + FiniteStateMachineForStorageUnitDataset.this, + reader, lock)); w.start(); if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { - newOps.put(dsInfo, RequestedState.ARCHIVE_REQUESTED); + newOps.put(dsInfo, + RequestedState.ARCHIVE_REQUESTED); } } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); + logger.debug( + "Could not acquire lock on " + + dsInfo + + ", hold back process with " + + state); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); + logger.error("I/O exception " + + e.getMessage() + " locking " + + dsInfo); } } } else if (state == RequestedState.ARCHIVE_REQUESTED) { try { - Lock lock = lockManager.lock(dsInfo, LockType.EXCLUSIVE); + Lock lock = lockManager.lock(dsInfo, + LockType.EXCLUSIVE); it.remove(); - logger.debug("Will process " + dsInfo + " with " + state); + logger.debug("Will process " + dsInfo + + " with " + state); dataInfoChanging.put(dsInfo, state); - final Thread w = new Thread( - new DsArchiver(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, lock)); + final Thread w = new Thread(new DsArchiver( + dsInfo, propertyHandler, + FiniteStateMachineForStorageUnitDataset.this, + lock)); w.start(); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); + logger.debug("Could not acquire lock on " + + dsInfo + + ", hold back process with " + + state); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); + logger.error( + "I/O exception " + e.getMessage() + + " locking " + dsInfo); } } else if (state == RequestedState.RESTORE_REQUESTED) { try { - Lock lock = lockManager.lock(dsInfo, LockType.EXCLUSIVE); - logger.debug("Will process " + dsInfo + " with " + state); + Lock lock = lockManager.lock(dsInfo, + LockType.EXCLUSIVE); + logger.debug("Will process " + dsInfo + + " with " + state); dataInfoChanging.put(dsInfo, state); it.remove(); - final Thread w = new Thread( - new DsRestorer(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, reader, lock)); + final Thread w = new Thread(new DsRestorer( + dsInfo, propertyHandler, + FiniteStateMachineForStorageUnitDataset.this, + reader, lock)); w.start(); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); + logger.debug("Could not acquire lock on " + + dsInfo + + ", hold back process with " + + state); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); + logger.error( + "I/O exception " + e.getMessage() + + " locking " + dsInfo); } } } @@ -193,7 +234,8 @@ public void run() { } } finally { - timer.schedule(new DsProcessQueue(), processQueueIntervalMillis); + timer.schedule(new DsProcessQueue(), + processQueueIntervalMillis); } } diff --git a/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java b/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java index 8c813be8..8a29aec2 100644 --- a/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java +++ b/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java @@ -11,8 +11,8 @@ @Provider public class CORSResponseFilter implements ContainerResponseFilter { - public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) - throws IOException { + public void filter(ContainerRequestContext requestContext, + ContainerResponseContext responseContext) throws IOException { MultivaluedMap headers = responseContext.getHeaders(); headers.add("Access-Control-Allow-Origin", "*"); diff --git a/src/main/java/org/icatproject/ids/helpers/Constants.java b/src/main/java/org/icatproject/ids/helpers/Constants.java index bc7d4aaa..298205bf 100644 --- a/src/main/java/org/icatproject/ids/helpers/Constants.java +++ b/src/main/java/org/icatproject/ids/helpers/Constants.java @@ -9,8 +9,8 @@ public class Constants { static { - InputStream inputStream = Constants.class.getClassLoader().getResourceAsStream( - "app.properties"); + InputStream inputStream = Constants.class.getClassLoader() + .getResourceAsStream("app.properties"); Properties p = new Properties(); try { diff --git a/src/main/java/org/icatproject/ids/helpers/LocationHelper.java b/src/main/java/org/icatproject/ids/helpers/LocationHelper.java index b3c1e8b5..8954cc89 100644 --- a/src/main/java/org/icatproject/ids/helpers/LocationHelper.java +++ b/src/main/java/org/icatproject/ids/helpers/LocationHelper.java @@ -23,8 +23,8 @@ public static String getLocation(long dfid, String location) } } - - public static String getLocationFromDigest(long id, String locationWithHash, String key) + public static String getLocationFromDigest(long id, String locationWithHash, + String key) throws InternalException, InsufficientPrivilegesException { int i = locationWithHash.lastIndexOf(' '); try { @@ -32,11 +32,13 @@ public static String getLocationFromDigest(long id, String locationWithHash, Str String hash = locationWithHash.substring(i + 1); if (!hash.equals(IcatSecurity.digest(id, location, key))) { throw new InsufficientPrivilegesException( - "Location \"" + locationWithHash + "\" does not contain a valid hash."); + "Location \"" + locationWithHash + + "\" does not contain a valid hash."); } return location; } catch (IndexOutOfBoundsException e) { - throw new InsufficientPrivilegesException("Location \"" + locationWithHash + "\" does not contain hash."); + throw new InsufficientPrivilegesException("Location \"" + + locationWithHash + "\" does not contain hash."); } catch (NoSuchAlgorithmException e) { throw new InternalException(e.getMessage()); } diff --git a/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java b/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java index ffaa34be..208d39a5 100644 --- a/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java +++ b/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java @@ -21,10 +21,10 @@ public class RangeOutputStream extends FilterOutputStream { * fixed number of bytes following some offset. * * @param os the stream to wrap - * @param min the number of bytes to skip. It may larger than the number of - * bytes in the stream. - * @param count restrict to transmit only this number of bytes. The value null - * indicates that all bytes after the offset should be + * @param min the number of bytes to skip. It may larger than the number + * of bytes in the stream. + * @param count restrict to transmit only this number of bytes. The value + * null indicates that all bytes after the offset should be * transmitted. */ public RangeOutputStream(OutputStream os, long min, Long count) { diff --git a/src/main/java/org/icatproject/ids/helpers/SO.java b/src/main/java/org/icatproject/ids/helpers/SO.java index 58e1ee4a..254a4e08 100644 --- a/src/main/java/org/icatproject/ids/helpers/SO.java +++ b/src/main/java/org/icatproject/ids/helpers/SO.java @@ -38,8 +38,10 @@ public class SO implements StreamingOutput { private static final int BUFSIZ = 2048; private final static Logger logger = LoggerFactory.getLogger(SO.class); - public SO(Map dsInfos, Map dfInfos, long offset, boolean zip, boolean compress, - Lock lock, Long transferId, String ip, long start, ServiceProvider serviceProvider) { + public SO(Map dsInfos, Map dfInfos, + long offset, boolean zip, boolean compress, Lock lock, + Long transferId, String ip, long start, + ServiceProvider serviceProvider) { this.offset = offset; this.zip = zip; this.dsInfos = dsInfos; @@ -62,21 +64,27 @@ public void write(OutputStream output) throws IOException { } byte[] bytes = new byte[BUFSIZ]; if (zip) { - ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(output)); + ZipOutputStream zos = new ZipOutputStream( + new BufferedOutputStream(output)); if (!compress) { zos.setLevel(0); // Otherwise use default compression } - for ( DataInfoBase dataInfo : dfInfos.values()) { + for (DataInfoBase dataInfo : dfInfos.values()) { var dfInfo = (DatafileInfo) dataInfo; logger.debug("Adding " + dfInfo + " to zip"); transfer = dfInfo; - DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId() ); - String entryName = this.serviceProvider.getPropertyHandler().getZipMapper().getFullEntryName((DatasetInfo)dsInfo, (DatafileInfo)dfInfo); + DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId()); + String entryName = this.serviceProvider.getPropertyHandler() + .getZipMapper() + .getFullEntryName((DatasetInfo) dsInfo, + (DatafileInfo) dfInfo); InputStream stream = null; try { zos.putNextEntry(new ZipEntry(entryName)); - stream = this.serviceProvider.getMainStorage().get(dfInfo.getLocation(), dfInfo.getCreateId(), dfInfo.getModId()); + stream = this.serviceProvider.getMainStorage().get( + dfInfo.getLocation(), dfInfo.getCreateId(), + dfInfo.getModId()); int length; while ((length = stream.read(bytes)) >= 0) { zos.write(bytes, 0, length); @@ -91,9 +99,11 @@ public void write(OutputStream output) throws IOException { } zos.close(); } else { - DatafileInfo dfInfo = (DatafileInfo) dfInfos.values().iterator().next(); + DatafileInfo dfInfo = (DatafileInfo) dfInfos.values().iterator() + .next(); transfer = dfInfo; - InputStream stream = this.serviceProvider.getMainStorage().get(dfInfo.getDfLocation(), dfInfo.getCreateId(), + InputStream stream = this.serviceProvider.getMainStorage().get( + dfInfo.getDfLocation(), dfInfo.getCreateId(), dfInfo.getModId()); int length; while ((length = stream.read(bytes)) >= 0) { @@ -105,23 +115,28 @@ public void write(OutputStream output) throws IOException { if (transferId != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos) + .writeStartObject()) { gen.write("transferId", transferId); gen.writeEnd(); } - this.serviceProvider.getTransmitter().processMessage("getData", ip, baos.toString(), start); + this.serviceProvider.getTransmitter().processMessage("getData", + ip, baos.toString(), start); } } catch (IOException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos) + .writeStartObject()) { gen.write("transferId", transferId); gen.write("exceptionClass", e.getClass().toString()); gen.write("exceptionMessage", e.getMessage()); gen.writeEnd(); } - this.serviceProvider.getTransmitter().processMessage("getData", ip, baos.toString(), start); - logger.error("Failed to stream " + transfer + " due to " + e.getMessage()); + this.serviceProvider.getTransmitter().processMessage("getData", ip, + baos.toString(), start); + logger.error("Failed to stream " + transfer + " due to " + + e.getMessage()); throw e; } finally { lock.release(); diff --git a/src/main/java/org/icatproject/ids/helpers/ValueContainer.java b/src/main/java/org/icatproject/ids/helpers/ValueContainer.java index bb99efba..9638ca1f 100644 --- a/src/main/java/org/icatproject/ids/helpers/ValueContainer.java +++ b/src/main/java/org/icatproject/ids/helpers/ValueContainer.java @@ -22,11 +22,16 @@ private ValueContainer(Object value, ValueContainerType type) { /** * checks if the type of the contained value is the same as the typeToCheck + * * @param typeToCheck the type to be checked if the contained value is of * @throws InternalException if the types don't match an exception is thrown */ - private void checkType(ValueContainerType typeToCheck) throws InternalException { - if(this.type != typeToCheck) throw new InternalException("This ValueContainer ist not of the needed type " + typeToCheck + " its type is " + this.type + "."); + private void checkType(ValueContainerType typeToCheck) + throws InternalException { + if (this.type != typeToCheck) + throw new InternalException( + "This ValueContainer ist not of the needed type " + + typeToCheck + " its type is " + this.type + "."); } public static ValueContainer getInvalid() { @@ -47,6 +52,7 @@ private ValueContainer(Void value) { /** * Creates a ValueContainer of type int + * * @param value the value contained by the container */ public ValueContainer(int value) { @@ -55,6 +61,7 @@ public ValueContainer(int value) { /** * Creates a ValueContainer of type long + * * @param value the value contained by the container */ public ValueContainer(long value) { @@ -63,6 +70,7 @@ public ValueContainer(long value) { /** * Creates a ValueContainer of type String + * * @param value the value contained by the container */ public ValueContainer(String value) { @@ -71,6 +79,7 @@ public ValueContainer(String value) { /** * Creates a ValueContainer of type boolean + * * @param value the value contained by the container */ public ValueContainer(boolean value) { @@ -79,14 +88,16 @@ public ValueContainer(boolean value) { /** * Creates a ValueContainer of type Response + * * @param value the value contained by the container */ public ValueContainer(Response value) { this(value, ValueContainerType.RESPONSE); - } - + } + /** * Creates a ValueContainer of type InputStream + * * @param value the value contained by the container */ public ValueContainer(InputStream value) { @@ -95,6 +106,7 @@ public ValueContainer(InputStream value) { /** * Informs about the type of the contained value + * * @return */ public ValueContainerType getType() { @@ -103,8 +115,10 @@ public ValueContainerType getType() { /** * Tries to return the value of the type int. + * * @return - * @throws InternalException if the container has another type an exception will be thrown + * @throws InternalException if the container has another type an exception + * will be thrown */ public int getInt() throws InternalException { this.checkType(ValueContainerType.INT); @@ -113,8 +127,10 @@ public int getInt() throws InternalException { /** * Tries to return the value of the type long. + * * @return - * @throws InternalException if the container has another type an exception will be thrown + * @throws InternalException if the container has another type an exception + * will be thrown */ public long getLong() throws InternalException { this.checkType(ValueContainerType.LONG); @@ -123,8 +139,10 @@ public long getLong() throws InternalException { /** * Tries to return the value of the type boolean. + * * @return - * @throws InternalException if the container has another type an exception will be thrown + * @throws InternalException if the container has another type an exception + * will be thrown */ public boolean getBool() throws InternalException { this.checkType(ValueContainerType.BOOL); @@ -133,8 +151,10 @@ public boolean getBool() throws InternalException { /** * Tries to return the value of the type String. + * * @return - * @throws InternalException if the container has another type an exception will be thrown + * @throws InternalException if the container has another type an exception + * will be thrown */ public String getString() throws InternalException { this.checkType(ValueContainerType.STRING); @@ -143,8 +163,10 @@ public String getString() throws InternalException { /** * Tries to return the value of the type Response. + * * @return - * @throws InternalException if the container has another type an exception will be thrown + * @throws InternalException if the container has another type an exception + * will be thrown */ public Response getResponse() throws InternalException { this.checkType(ValueContainerType.RESPONSE); @@ -153,8 +175,10 @@ public Response getResponse() throws InternalException { /** * Tries to return the value of the type InputStream. + * * @return - * @throws InternalException if the container has another type an exception will be thrown + * @throws InternalException if the container has another type an exception + * will be thrown */ public InputStream getInputStream() throws InternalException { this.checkType(ValueContainerType.INPUTSTREAM); @@ -163,27 +187,41 @@ public InputStream getInputStream() throws InternalException { @Override public String toString() { - switch(this.type) { - case INVALID: return ""+ValueContainerType.INVALID; - case VOID: return ""+ValueContainerType.VOID; - case INT: return ""+this.value; - case LONG: return ""+this.value; - case BOOL: return ((boolean)this.value ? "true" : "false"); - case STRING: return (String)this.value; - case RESPONSE: return "Response " + ((Response) this.value).toString(); - case INPUTSTREAM: return "An InputStream which will be printed here to prevent it from closing (and maybe it is too long)."; - default: - throw new RuntimeException("Doesn't know how to make a String vom ValueContainer of type " + this.type + ". Please implement a new case is ValueContainer.toString()."); + switch (this.type) { + case INVALID: + return "" + ValueContainerType.INVALID; + case VOID: + return "" + ValueContainerType.VOID; + case INT: + return "" + this.value; + case LONG: + return "" + this.value; + case BOOL: + return ((boolean) this.value ? "true" : "false"); + case STRING: + return (String) this.value; + case RESPONSE: + return "Response " + ((Response) this.value).toString(); + case INPUTSTREAM: + return "An InputStream which will be printed here to prevent it from closing (and maybe it is too long)."; + default: + throw new RuntimeException( + "Doesn't know how to make a String vom ValueContainer of type " + + this.type + + ". Please implement a new case is ValueContainer.toString()."); } } - public boolean isNull() { return this.isInvalid() && this.value == null; } - - public boolean isInvalid() { return this.type == ValueContainerType.INVALID; } - - public boolean isVoid() { return this.type == ValueContainerType.VOID; } + public boolean isNull() { + return this.isInvalid() && this.value == null; + } - + public boolean isInvalid() { + return this.type == ValueContainerType.INVALID; + } + public boolean isVoid() { + return this.type == ValueContainerType.VOID; + } } diff --git a/src/main/java/org/icatproject/ids/models/DataInfoBase.java b/src/main/java/org/icatproject/ids/models/DataInfoBase.java index 603a3e7e..57adf664 100644 --- a/src/main/java/org/icatproject/ids/models/DataInfoBase.java +++ b/src/main/java/org/icatproject/ids/models/DataInfoBase.java @@ -9,7 +9,7 @@ public abstract class DataInfoBase { protected String name; protected String location; - protected DataInfoBase(long id, String name, String location){ + protected DataInfoBase(long id, String name, String location) { this.name = name; this.id = id; this.location = location; @@ -42,5 +42,5 @@ public boolean equals(Object obj) { } return this.id == ((DataInfoBase) obj).getId(); } - + } diff --git a/src/main/java/org/icatproject/ids/models/DataSelection.java b/src/main/java/org/icatproject/ids/models/DataSelection.java index 1bc59344..ffb8aa77 100644 --- a/src/main/java/org/icatproject/ids/models/DataSelection.java +++ b/src/main/java/org/icatproject/ids/models/DataSelection.java @@ -15,10 +15,10 @@ public class DataSelection { protected Boolean compress; private long length; - - - public DataSelection(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress) { + public DataSelection(SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, + Boolean zip, Boolean compress) { this.dsInfos = dsInfos; this.dfInfos = dfInfos; @@ -31,12 +31,10 @@ public DataSelection(SortedMap dsInfos, SortedMap getDsInfo() { return dsInfos; } - public SortedMap getDfInfo() { return dfInfos; } @@ -53,11 +51,10 @@ public long getLength() { return this.length; } - public boolean mustZip() { // if(this.zip == null) { - return dfids.size() > 1L || !dsids.isEmpty() || !invids.isEmpty() - || (dfids.isEmpty() && dsids.isEmpty() && invids.isEmpty()); + return dfids.size() > 1L || !dsids.isEmpty() || !invids.isEmpty() + || (dfids.isEmpty() && dsids.isEmpty() && invids.isEmpty()); // } // return this.zip; @@ -67,7 +64,6 @@ public boolean isSingleDataset() { return dfids.isEmpty() && dsids.size() == 1 && invids.isEmpty(); } - public Set getEmptyDatasets() { return emptyDatasets; } diff --git a/src/main/java/org/icatproject/ids/models/DatafileInfo.java b/src/main/java/org/icatproject/ids/models/DatafileInfo.java index 789cfa79..4fa20cfd 100644 --- a/src/main/java/org/icatproject/ids/models/DatafileInfo.java +++ b/src/main/java/org/icatproject/ids/models/DatafileInfo.java @@ -3,16 +3,18 @@ import org.icatproject.ids.plugin.DfInfo; /** - * Contains information about a Datafile. Replaces DsInfo in v3 - * May should implement DfInfo interface + * Contains information about a Datafile. Replaces DsInfo in v3 May should + * implement DfInfo interface */ -public class DatafileInfo extends DataInfoBase implements Comparable, DfInfo { +public class DatafileInfo extends DataInfoBase + implements Comparable, DfInfo { protected String createId; - protected String modId; + protected String modId; protected long datasId; - public DatafileInfo(Long id, String name, String location, String createId, String modId, Long datasId) { + public DatafileInfo(Long id, String name, String location, String createId, + String modId, Long datasId) { super(id, name, location); this.createId = createId; @@ -50,9 +52,17 @@ public int compareTo(DatafileInfo o) { // implementing DfInfo @Override - public Long getDfId() { return this.getId(); } + public Long getDfId() { + return this.getId(); + } + @Override - public String getDfLocation() { return this.getLocation(); } + public String getDfLocation() { + return this.getLocation(); + } + @Override - public String getDfName() { return this.getName(); } + public String getDfName() { + return this.getName(); + } } diff --git a/src/main/java/org/icatproject/ids/models/DatasetInfo.java b/src/main/java/org/icatproject/ids/models/DatasetInfo.java index be4d9a77..a7ad9de2 100644 --- a/src/main/java/org/icatproject/ids/models/DatasetInfo.java +++ b/src/main/java/org/icatproject/ids/models/DatasetInfo.java @@ -7,8 +7,8 @@ import org.icatproject.ids.plugin.DsInfo; /** - * Contains information about a Dataset. Replaces DsInfo in v3. - * May should implement DsInfo interface + * Contains information about a Dataset. Replaces DsInfo in v3. May should + * implement DsInfo interface */ public class DatasetInfo extends DataInfoBase implements DsInfo { @@ -18,14 +18,14 @@ public class DatasetInfo extends DataInfoBase implements DsInfo { protected String investigationName; protected String visitId; - public DatasetInfo(Dataset ds) throws InsufficientPrivilegesException { - super(ds.getId(), ds.getName(), ds.getLocation()); + super(ds.getId(), ds.getName(), ds.getLocation()); Investigation investigation = ds.getInvestigation(); if (investigation == null) { throw new InsufficientPrivilegesException( - "Probably not able to read Investigation for dataset id " + ds.getId()); + "Probably not able to read Investigation for dataset id " + + ds.getId()); } Facility facility = investigation.getFacility(); if (facility == null) { @@ -41,8 +41,9 @@ public DatasetInfo(Dataset ds) throws InsufficientPrivilegesException { this.facilityName = facility.getName(); } - - public DatasetInfo(Long id, String name, String location, Long investigationId, String investigationName, String visitId, Long facilityId, String facilityName) { + public DatasetInfo(Long id, String name, String location, + Long investigationId, String investigationName, String visitId, + Long facilityId, String facilityName) { super(id, name, location); this.facilityId = facilityId; @@ -54,7 +55,8 @@ public DatasetInfo(Long id, String name, String location, Long investigationId, @Override public String toString() { - return this.investigationId + "/" + this.id + " (" + this.facilityName + "/" + this.investigationName + "/" + this.visitId + "/" + return this.investigationId + "/" + this.id + " (" + this.facilityName + + "/" + this.investigationName + "/" + this.visitId + "/" + this.name + ")"; } @@ -78,18 +80,31 @@ public String getVisitId() { return visitId; } - // implementing DsInfo @Override - public Long getDsId() { return this.getId(); } + public Long getDsId() { + return this.getId(); + } + @Override - public String getDsName() { return this.getName(); } + public String getDsName() { + return this.getName(); + } + @Override - public String getDsLocation() { return this.getLocation(); } + public String getDsLocation() { + return this.getLocation(); + } + @Override - public Long getInvId() { return this.getInvestigationId(); } + public Long getInvId() { + return this.getInvestigationId(); + } + @Override - public String getInvName() { return this.getInvestigationName(); } + public String getInvName() { + return this.getInvestigationName(); + } } diff --git a/src/main/java/org/icatproject/ids/models/Prepared.java b/src/main/java/org/icatproject/ids/models/Prepared.java index 5e04d830..2e557916 100644 --- a/src/main/java/org/icatproject/ids/models/Prepared.java +++ b/src/main/java/org/icatproject/ids/models/Prepared.java @@ -23,9 +23,9 @@ /* This is a POJO with only package access so don't make data private */ public class Prepared { - protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); + protected final static Logger logger = LoggerFactory + .getLogger(RequestHandlerBase.class); - public SortedMap dsInfos; public SortedMap dfInfos; public Set emptyDatasets; @@ -33,7 +33,9 @@ public class Prepared { public boolean zip; public boolean compress; - public Prepared(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength) { + public Prepared(SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + long fileLength) { this.dsInfos = dsInfos; this.dfInfos = dfInfos; this.emptyDatasets = emptyDatasets; @@ -42,15 +44,17 @@ public Prepared(SortedMap dsInfos, SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength, Boolean zip, Boolean compress) { + public Prepared(SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + long fileLength, Boolean zip, Boolean compress) { this(dsInfos, dfInfos, emptyDatasets, fileLength); this.zip = zip; this.compress = compress; } - - public static void pack(OutputStream stream, boolean zip, boolean compress, Map dsInfos, - Map dfInfos, Set emptyDatasets, long fileLength) { + public static void pack(OutputStream stream, boolean zip, boolean compress, + Map dsInfos, Map dfInfos, + Set emptyDatasets, long fileLength) { JsonGenerator gen = Json.createGenerator(stream); gen.writeStartObject(); gen.write("zip", zip); @@ -59,13 +63,16 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, Map< gen.writeStartArray("dsInfo"); for (DataInfoBase dataInfo : dsInfos.values()) { - var dsInfo = (DatasetInfo)dataInfo; + var dsInfo = (DatasetInfo) dataInfo; logger.debug("dsInfo " + dsInfo); gen.writeStartObject().write("dsId", dsInfo.getId()) - .write("dsName", dsInfo.getDsName()).write("facilityId", dsInfo.getFacilityId()) - .write("facilityName", dsInfo.getFacilityName()).write("invId", dsInfo.getInvId()) - .write("invName", dsInfo.getInvName()).write("visitId", dsInfo.getVisitId()); + .write("dsName", dsInfo.getDsName()) + .write("facilityId", dsInfo.getFacilityId()) + .write("facilityName", dsInfo.getFacilityName()) + .write("invId", dsInfo.getInvId()) + .write("invName", dsInfo.getInvName()) + .write("visitId", dsInfo.getVisitId()); if (dsInfo.getDsLocation() != null) { gen.write("dsLocation", dsInfo.getDsLocation()); } else { @@ -77,10 +84,12 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, Map< gen.writeStartArray("dfInfo"); for (DataInfoBase dataInfo : dfInfos.values()) { - var dfInfo = (DatafileInfo)dataInfo; + var dfInfo = (DatafileInfo) dataInfo; DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId()); - gen.writeStartObject().write("dsId", dsInfo.getId()).write("dfId", dfInfo.getId()) - .write("dfName", dfInfo.getDfName()).write("createId", dfInfo.getCreateId()) + gen.writeStartObject().write("dsId", dsInfo.getId()) + .write("dfId", dfInfo.getId()) + .write("dfName", dfInfo.getDfName()) + .write("createId", dfInfo.getCreateId()) .write("modId", dfInfo.getModId()); if (dfInfo.getDfLocation() != null) { gen.write("dfLocation", dfInfo.getDfLocation()); @@ -102,7 +111,6 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, Map< } - public static Prepared unpack(InputStream stream) throws InternalException { JsonObject pd; @@ -111,35 +119,42 @@ public static Prepared unpack(InputStream stream) throws InternalException { } var zip = pd.getBoolean("zip"); var compress = pd.getBoolean("compress"); - var fileLength = pd.containsKey("fileLength") ? pd.getInt("fileLength") : 0; + var fileLength = pd.containsKey("fileLength") ? pd.getInt("fileLength") + : 0; SortedMap dsInfos = new TreeMap<>(); SortedMap dfInfos = new TreeMap<>(); Set emptyDatasets = new HashSet<>(); for (JsonValue itemV : pd.getJsonArray("dfInfo")) { JsonObject item = (JsonObject) itemV; - String dfLocation = item.isNull("dfLocation") ? null : item.getString("dfLocation"); + String dfLocation = item.isNull("dfLocation") ? null + : item.getString("dfLocation"); long dfid = item.getJsonNumber("dfId").longValueExact(); - dfInfos.put(dfid, new DatafileInfo(dfid, item.getString("dfName"), - dfLocation, item.getString("createId"), item.getString("modId"), - item.getJsonNumber("dsId").longValueExact())); + dfInfos.put(dfid, + new DatafileInfo(dfid, item.getString("dfName"), dfLocation, + item.getString("createId"), item.getString("modId"), + item.getJsonNumber("dsId").longValueExact())); } for (JsonValue itemV : pd.getJsonArray("dsInfo")) { JsonObject item = (JsonObject) itemV; long dsId = item.getJsonNumber("dsId").longValueExact(); - String dsLocation = item.isNull("dsLocation") ? null : item.getString("dsLocation"); - dsInfos.put(dsId, new DatasetInfo(dsId, item.getString("dsName"), dsLocation, - item.getJsonNumber("invId").longValueExact(), item.getString("invName"), item.getString("visitId"), - item.getJsonNumber("facilityId").longValueExact(), item.getString("facilityName"))); + String dsLocation = item.isNull("dsLocation") ? null + : item.getString("dsLocation"); + dsInfos.put(dsId, new DatasetInfo(dsId, item.getString("dsName"), + dsLocation, item.getJsonNumber("invId").longValueExact(), + item.getString("invName"), item.getString("visitId"), + item.getJsonNumber("facilityId").longValueExact(), + item.getString("facilityName"))); } for (JsonValue itemV : pd.getJsonArray("emptyDs")) { emptyDatasets.add(((JsonNumber) itemV).longValueExact()); } - return new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength, zip, compress); + return new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength, zip, + compress); } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java index 032e8437..df19f640 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java @@ -1,6 +1,5 @@ package org.icatproject.ids.requestHandlers; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; @@ -12,13 +11,17 @@ public class ArchiveHandler extends DataRequestHandler { - public ArchiveHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.ARCHIVE, ip, sessionId, investigationIds, datasetIds, datafileIds); + public ArchiveHandler(String ip, String sessionId, String investigationIds, + String datasetIds, String datafileIds) { + super(RequestType.ARCHIVE, ip, sessionId, investigationIds, datasetIds, + datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws NotImplementedException, InternalException { - + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws NotImplementedException, InternalException { + dataSelectionService.scheduleTasks(DeferredOp.ARCHIVE); return ValueContainer.getVoid(); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java index a4cb44a9..e7ea350b 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java @@ -26,43 +26,52 @@ import org.icatproject.ids.services.LockManager.LockType; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; - public class DeleteHandler extends DataRequestHandler { - public DeleteHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.DELETE, ip, sessionId, investigationIds, datasetIds, datafileIds); + public DeleteHandler(String ip, String sessionId, String investigationIds, + String datasetIds, String datafileIds) { + super(RequestType.DELETE, ip, sessionId, investigationIds, datasetIds, + datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { if (readOnly) { - throw new NotImplementedException("This operation has been configured to be unavailable"); + throw new NotImplementedException( + "This operation has been configured to be unavailable"); } var serviceProvider = ServiceProvider.getInstance(); // Do it - try (Lock lock = serviceProvider.getLockManager().lock(dataSelectionService.getDsInfo().values(), LockType.EXCLUSIVE)) { + try (Lock lock = serviceProvider.getLockManager().lock( + dataSelectionService.getDsInfo().values(), + LockType.EXCLUSIVE)) { if (storageUnit == StorageUnit.DATASET) { dataSelectionService.checkOnline(); } /* Now delete from ICAT */ List dfs = new ArrayList<>(); - for (DataInfoBase dfInfo : dataSelectionService.getDfInfo().values()) { + for (DataInfoBase dfInfo : dataSelectionService.getDfInfo() + .values()) { Datafile df = new Datafile(); df.setId(dfInfo.getId()); dfs.add(df); } try { - serviceProvider.getIcat().deleteMany(this.dataController.getOperationId(), dfs); + serviceProvider.getIcat() + .deleteMany(this.dataController.getOperationId(), dfs); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES + || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java index 7c22c38f..c265418c 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java @@ -21,25 +21,31 @@ public class GetDataFileIdsHandler extends DataRequestHandler { - public GetDataFileIdsHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETDATAFILEIDS, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public GetDataFileIdsHandler(String ip, String preparedId, String sessionId, + String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETDATAFILEIDS, ip, preparedId, sessionId, + investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos) + .writeStartObject()) { - if(this.dataController instanceof PreparedDataController) { + if (this.dataController instanceof PreparedDataController) { gen.write("zip", dataSelectionService.getZip()); gen.write("compress", dataSelectionService.getCompress()); } gen.writeStartArray("ids"); - for (DataInfoBase dfInfo : dataSelectionService.getDfInfo().values()) { + for (DataInfoBase dfInfo : dataSelectionService.getDfInfo() + .values()) { gen.write(dfInfo.getId()); } gen.writeEnd().writeEnd().close(); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java index 8cdb64ce..a3ef818b 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java @@ -45,40 +45,48 @@ public class GetDataHandler extends DataRequestHandler { String range; Long transferId; - public GetDataHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds, Boolean compress, Boolean zip, String outname, String range) { - super(RequestType.GETDATA, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds ); - - this.initializeAdditionallParameters(sessionId, compress, zip, outname, range); + public GetDataHandler(String ip, String preparedId, String sessionId, + String investigationIds, String datasetIds, String datafileIds, + Boolean compress, Boolean zip, String outname, String range) { + super(RequestType.GETDATA, ip, preparedId, sessionId, investigationIds, + datasetIds, datafileIds); + + this.initializeAdditionallParameters(sessionId, compress, zip, outname, + range); } - private void initializeAdditionallParameters(String sessionId, Boolean compress, Boolean zip, String outname, String range) { - + private void initializeAdditionallParameters(String sessionId, + Boolean compress, Boolean zip, String outname, String range) { + this.outname = outname; this.range = range; this.transferId = null; this.rangeRe = Pattern.compile("bytes=(\\d+)-"); - if(sessionId != null) { + if (sessionId != null) { this.compress = compress; this.zip = zip; } } - @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws InternalException, NotImplementedException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException { + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) throws InternalException, + NotImplementedException, BadRequestException, NotFoundException, + InsufficientPrivilegesException, DataNotOnlineException { - if(this.compress == null || this.zip == null) { + if (this.compress == null || this.zip == null) { this.zip = dataSelectionService.getZip(); this.compress = dataSelectionService.getCompress(); } long offset = 0; - if ( range != null) { + if (range != null) { Matcher m = rangeRe.matcher(range); if (!m.matches()) { - throw new BadRequestException("The range must match " + rangeRe.pattern()); + throw new BadRequestException( + "The range must match " + rangeRe.pattern()); } offset = Long.parseLong(m.group(1)); logger.debug("Range " + range + " -> offset " + offset); @@ -87,20 +95,24 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic return new ValueContainer(this.getData(dataSelectionService, offset)); } - - private Response getData(DataSelectionService dataSelectionService, final long offset) throws BadRequestException, - NotFoundException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + private Response getData(DataSelectionService dataSelectionService, + final long offset) throws BadRequestException, NotFoundException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { long start = System.currentTimeMillis(); - - var length = this.zip ? OptionalLong.empty() : dataSelectionService.getFileLength(); - final boolean finalZip = this.dataController.mustZip(zip, dataSelectionService); + var length = this.zip ? OptionalLong.empty() + : dataSelectionService.getFileLength(); + + final boolean finalZip = this.dataController.mustZip(zip, + dataSelectionService); Lock lock = null; try { var serviceProvider = ServiceProvider.getInstance(); - lock = serviceProvider.getLockManager().lock(dataSelectionService.getDsInfo().values(), LockType.SHARED); + lock = serviceProvider.getLockManager().lock( + dataSelectionService.getDsInfo().values(), LockType.SHARED); if (twoLevel) { dataSelectionService.checkOnline(); @@ -111,13 +123,16 @@ private Response getData(DataSelectionService dataSelectionService, final long o String name; if (outname == null) { if (finalZip) { - name = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()) + ".zip"; + name = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss") + .format(new Date()) + ".zip"; } else { - name = dataSelectionService.getDfInfo().values().iterator().next().getName(); + name = dataSelectionService.getDfInfo().values().iterator() + .next().getName(); } } else { if (finalZip) { - String ext = outname.substring(outname.lastIndexOf(".") + 1, outname.length()); + String ext = outname.substring(outname.lastIndexOf(".") + 1, + outname.length()); if ("zip".equals(ext)) { name = outname; } else { @@ -128,17 +143,24 @@ private Response getData(DataSelectionService dataSelectionService, final long o } } - if (ServiceProvider.getInstance().getPropertyHandler().getLogSet().contains(this.getCallType())) { + if (ServiceProvider.getInstance().getPropertyHandler().getLogSet() + .contains(this.getCallType())) { transferId = transferIdCounter.getAndIncrement(); } - var response = Response.status(offset == 0 ? HttpURLConnection.HTTP_OK : HttpURLConnection.HTTP_PARTIAL) - .entity(new SO(dataSelectionService.getDsInfo(), dataSelectionService.getDfInfo(), offset, finalZip, compress, lock, transferId, ip, start, serviceProvider)) - .header("Content-Disposition", "attachment; filename=\"" + name + "\"").header("Accept-Ranges", "bytes"); - length.stream() - .map(l -> Math.max(0L, l - offset)) + var response = Response + .status(offset == 0 ? HttpURLConnection.HTTP_OK + : HttpURLConnection.HTTP_PARTIAL) + .entity(new SO(dataSelectionService.getDsInfo(), + dataSelectionService.getDfInfo(), offset, finalZip, + compress, lock, transferId, ip, start, + serviceProvider)) + .header("Content-Disposition", + "attachment; filename=\"" + name + "\"") + .header("Accept-Ranges", "bytes"); + length.stream().map(l -> Math.max(0L, l - offset)) .forEach(l -> response.header(CONTENT_LENGTH, l)); - + return response.build(); } catch (AlreadyLockedException e) { @@ -146,7 +168,7 @@ private Response getData(DataSelectionService dataSelectionService, final long o throw new DataNotOnlineException("Data is busy"); } catch (IOException e) { // if (lock != null) { - // lock.release(); + // lock.release(); // } logger.error("I/O error " + e.getMessage()); throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -156,38 +178,48 @@ private Response getData(DataSelectionService dataSelectionService, final long o } } - private void checkDatafilesPresent(Collection dfInfos) throws NotFoundException, InternalException { var serviceProvider = ServiceProvider.getInstance(); /* Check that datafiles have not been deleted before locking */ int n = 0; - StringBuffer sb = new StringBuffer("SELECT COUNT(df) from Datafile df WHERE (df.id in ("); + StringBuffer sb = new StringBuffer( + "SELECT COUNT(df) from Datafile df WHERE (df.id in ("); for (DataInfoBase dfInfo : dfInfos) { if (n != 0) { sb.append(','); } sb.append(dfInfo.getId()); - if (++n == serviceProvider.getPropertyHandler().getMaxIdsInQuery()) { + if (++n == serviceProvider.getPropertyHandler() + .getMaxIdsInQuery()) { try { - if (((Long) serviceProvider.getIcatReader().search(sb.append("))").toString()).get(0)).intValue() != n) { - throw new NotFoundException("One of the data files requested has been deleted"); + if (((Long) serviceProvider.getIcatReader() + .search(sb.append("))").toString()).get(0)) + .intValue() != n) { + throw new NotFoundException( + "One of the data files requested has been deleted"); } n = 0; - sb = new StringBuffer("SELECT COUNT(df) from Datafile df WHERE (df.id in ("); + sb = new StringBuffer( + "SELECT COUNT(df) from Datafile df WHERE (df.id in ("); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage()); } } } if (n != 0) { try { - if (((Long) serviceProvider.getIcatReader().search(sb.append("))").toString()).get(0)).intValue() != n) { - throw new NotFoundException("One of the datafiles requested has been deleted"); + if (((Long) serviceProvider.getIcatReader() + .search(sb.append("))").toString()).get(0)) + .intValue() != n) { + throw new NotFoundException( + "One of the datafiles requested has been deleted"); } } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage()); } } @@ -200,7 +232,8 @@ public CallType getCallType() { @Override public void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - if(transferId == null) transferId = -1L; + if (transferId == null) + transferId = -1L; gen.write("transferId", transferId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java index 94a78919..70942624 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java @@ -12,7 +12,6 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; import org.icatproject.ids.services.ServiceProvider; - public class GetIcatUrlHandler extends RequestHandlerBase { public GetIcatUrlHandler(String ip) { @@ -20,11 +19,12 @@ public GetIcatUrlHandler(String ip) { } @Override - public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { - var propertyHandler = ServiceProvider.getInstance().getPropertyHandler(); + var propertyHandler = ServiceProvider.getInstance() + .getPropertyHandler(); return new ValueContainer(propertyHandler.getIcatUrl()); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java index 713c3603..e2c1b106 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java @@ -28,24 +28,27 @@ public GetServiceStatusHandler(String ip, String sessionId) { super(RequestType.GETSERVICESTATUS, ip); this.sessionId = sessionId; - rootUserNames = ServiceProvider.getInstance().getPropertyHandler().getRootUserNames(); + rootUserNames = ServiceProvider.getInstance().getPropertyHandler() + .getRootUserNames(); this.serviceProvider = ServiceProvider.getInstance(); } @Override - public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { try { String uname = serviceProvider.getIcat().getUserName(sessionId); if (!rootUserNames.contains(uname)) { - throw new InsufficientPrivilegesException(uname + " is not included in the ids rootUserNames set."); + throw new InsufficientPrivilegesException(uname + + " is not included in the ids rootUserNames set."); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); if (type == IcatExceptionType.SESSION) { - throw new InsufficientPrivilegesException(e.getClass() + " " + e.getMessage()); + throw new InsufficientPrivilegesException( + e.getClass() + " " + e.getMessage()); } throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -54,7 +57,8 @@ public ValueContainer handleRequest() } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { gen.write("userName", serviceProvider.getIcat().getUserName(sessionId)); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java index 9da9787c..8f4c145f 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java @@ -19,21 +19,27 @@ public class GetStatusHandler extends DataRequestHandler { - public GetStatusHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETSTATUS, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public GetStatusHandler(String ip, String preparedId, String sessionId, + String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETSTATUS, ip, preparedId, sessionId, + investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { Status status = Status.ONLINE; var serviceProvider = ServiceProvider.getInstance(); Set restoring = serviceProvider.getFsm().getRestoring(); - Set maybeOffline = serviceProvider.getFsm().getMaybeOffline(); - for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { + Set maybeOffline = serviceProvider.getFsm() + .getMaybeOffline(); + for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos() + .values()) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (restoring.contains(dataInfo)) { status = Status.RESTORING; diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java index 6f2deba7..53660e58 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java @@ -34,17 +34,23 @@ class PreparedStatus { private Map preparedStatusMap = new ConcurrentHashMap<>(); @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { // Do it boolean prepared = true; - PreparedStatus status = preparedStatusMap.computeIfAbsent(this.dataController.getOperationId(), k -> new PreparedStatus()); + PreparedStatus status = preparedStatusMap.computeIfAbsent( + this.dataController.getOperationId(), + k -> new PreparedStatus()); if (!status.lock.tryLock()) { - logger.debug("Lock held for evaluation of isPrepared for preparedId {}", this.dataController.getOperationId()); + logger.debug( + "Lock held for evaluation of isPrepared for preparedId {}", + this.dataController.getOperationId()); return new ValueContainer(false); } try { @@ -54,19 +60,23 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic try { future.get(); } catch (ExecutionException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } catch (InterruptedException e) { // Ignore } finally { status.future = null; } } else { - logger.debug("Background process still running for preparedId {}", this.dataController.getOperationId()); + logger.debug( + "Background process still running for preparedId {}", + this.dataController.getOperationId()); return new ValueContainer(false); } } - prepared = dataSelectionService.isPrepared(this.dataController.getOperationId()); + prepared = dataSelectionService + .isPrepared(this.dataController.getOperationId()); return new ValueContainer(prepared); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java index a7acf20e..15c93a26 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java @@ -12,16 +12,16 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; public class IsReadOnlyHandler extends RequestHandlerBase { - + public IsReadOnlyHandler(String ip) { super(RequestType.ISREADONLY, ip); } @Override - public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { return new ValueContainer(this.readOnly); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java index f9cc3be1..1ffd2326 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java @@ -18,10 +18,10 @@ public IsTwoLevelHandler(String ip) { } @Override - public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + public ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { + return new ValueContainer(twoLevel); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java index 625c4ac2..8d2acde0 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java @@ -28,20 +28,25 @@ public class PrepareDataHandler extends DataRequestHandler { Boolean zip; String preparedId; - public PrepareDataHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds, Boolean compress, Boolean zip) { - super(RequestType.PREPAREDATA,ip, sessionId, investigationIds, datasetIds, datafileIds); + public PrepareDataHandler(String ip, String sessionId, + String investigationIds, String datasetIds, String datafileIds, + Boolean compress, Boolean zip) { + super(RequestType.PREPAREDATA, ip, sessionId, investigationIds, + datasetIds, datafileIds); this.zip = zip; this.compress = compress; } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { preparedId = UUID.randomUUID().toString(); - + dataSelectionService.restoreDataInfos(); if (dataSelectionService.mustZip()) { @@ -49,15 +54,14 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic } logger.debug("Writing to " + preparedDir.resolve(preparedId)); - try (OutputStream stream = new BufferedOutputStream(Files.newOutputStream(preparedDir.resolve(preparedId)))) { - Prepared.pack( stream, - zip, - compress, - dataSelectionService.getDsInfo(), - dataSelectionService.getDfInfo(), - dataSelectionService.getEmptyDatasets(), - dataSelectionService.getFileLength().isEmpty() ? 0 : dataSelectionService.getFileLength().getAsLong() - ); + try (OutputStream stream = new BufferedOutputStream( + Files.newOutputStream(preparedDir.resolve(preparedId)))) { + Prepared.pack(stream, zip, compress, + dataSelectionService.getDsInfo(), + dataSelectionService.getDfInfo(), + dataSelectionService.getEmptyDatasets(), + dataSelectionService.getFileLength().isEmpty() ? 0 + : dataSelectionService.getFileLength().getAsLong()); } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -68,13 +72,14 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic } @Override - public String addCustomParametersToLogString() { + public String addCustomParametersToLogString() { return "zip='" + this.zip + "'' compress='" + compress + "'"; } @Override public void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - if(preparedId == null) preparedId = ""; + if (preparedId == null) + preparedId = ""; gen.write(RequestIdNames.preparedId, preparedId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java index 3e9bfbd9..91778039 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java @@ -79,9 +79,10 @@ public class PutHandler extends RequestHandlerBase { paddedPrefix += "*/window.name='"; } - public PutHandler( String ip, String sessionId, InputStream body, String name, String datafileFormatIdString, - String datasetIdString, String description, String doi, String datafileCreateTimeString, - String datafileModTimeString, boolean wrap, boolean padding) { + public PutHandler(String ip, String sessionId, InputStream body, + String name, String datafileFormatIdString, String datasetIdString, + String description, String doi, String datafileCreateTimeString, + String datafileModTimeString, boolean wrap, boolean padding) { super(RequestType.PUT, ip); this.sessionId = sessionId; @@ -105,52 +106,61 @@ public PutHandler( String ip, String sessionId, InputStream body, String name, } catch (Throwable e) { logger.error("Won't start ", e); - throw new RuntimeException("PutHandler reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException("PutHandler reports " + e.getClass() + + " " + e.getMessage()); } } @Override - public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { try { if (readOnly) { - throw new NotImplementedException("This operation has been configured to be unavailable"); + throw new NotImplementedException( + "This operation has been configured to be unavailable"); } - DataControllerBase.validateUUID(RequestIdNames.sessionId, sessionId); + DataControllerBase.validateUUID(RequestIdNames.sessionId, + sessionId); if (name == null) { throw new BadRequestException("The name parameter must be set"); } if (datafileFormatIdString == null) { - throw new BadRequestException("The datafileFormatId parameter must be set"); + throw new BadRequestException( + "The datafileFormatId parameter must be set"); } long datafileFormatId; try { datafileFormatId = Long.parseLong(datafileFormatIdString); } catch (NumberFormatException e) { - throw new BadRequestException("The datafileFormatId parameter must be numeric"); + throw new BadRequestException( + "The datafileFormatId parameter must be numeric"); } if (datasetIdString == null) { - throw new BadRequestException("The datasetId parameter must be set"); + throw new BadRequestException( + "The datasetId parameter must be set"); } long datasetId; try { datasetId = Long.parseLong(datasetIdString); } catch (NumberFormatException e) { - throw new BadRequestException("The datasetId parameter must be numeric"); + throw new BadRequestException( + "The datasetId parameter must be numeric"); } Long datafileCreateTime = null; if (datafileCreateTimeString != null) { try { - datafileCreateTime = Long.parseLong(datafileCreateTimeString); + datafileCreateTime = Long + .parseLong(datafileCreateTimeString); } catch (NumberFormatException e) { - throw new BadRequestException("The datafileCreateTime parameter must be numeric"); + throw new BadRequestException( + "The datafileCreateTime parameter must be numeric"); } } @@ -159,17 +169,20 @@ public ValueContainer handleRequest() try { datafileModTime = Long.parseLong(datafileModTimeString); } catch (NumberFormatException e) { - throw new BadRequestException("The datafileModTime parameter must be numeric"); + throw new BadRequestException( + "The datafileModTime parameter must be numeric"); } } // Do it Dataset ds; try { - ds = (Dataset) serviceProvider.getIcat().get(sessionId, "Dataset INCLUDE Investigation, Facility", datasetId); + ds = (Dataset) serviceProvider.getIcat().get(sessionId, + "Dataset INCLUDE Investigation, Facility", datasetId); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES + || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -179,59 +192,74 @@ public ValueContainer handleRequest() } DatasetInfo dsInfo = new DatasetInfo(ds); - try (Lock lock = serviceProvider.getLockManager().lock(dsInfo, LockType.SHARED)) { + try (Lock lock = serviceProvider.getLockManager().lock(dsInfo, + LockType.SHARED)) { if (storageUnit == StorageUnit.DATASET) { var dfInfos = new TreeMap(); Set emptyDatasets = new HashSet<>(); try { - List counts = serviceProvider.getIcat().search(sessionId, - "COUNT(Datafile) <-> Dataset [id=" + dsInfo.getId() + "]"); + List counts = serviceProvider.getIcat().search( + sessionId, "COUNT(Datafile) <-> Dataset [id=" + + dsInfo.getId() + "]"); if ((Long) counts.get(0) == 0) { emptyDatasets.add(dsInfo.getId()); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { - throw new InsufficientPrivilegesException(e.getMessage()); + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES + || type == IcatExceptionType.SESSION) { + throw new InsufficientPrivilegesException( + e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } - throw new InternalException(type + " " + e.getMessage()); + throw new InternalException( + type + " " + e.getMessage()); } var dsInfos = new TreeMap(); dsInfos.put(dsInfo.getId(), dsInfo); - DataSelectionService dataSelectionService = DataSelectionServiceFactory.getService(dsInfos, dfInfos, emptyDatasets, 0, this.getRequestType()); + DataSelectionService dataSelectionService = DataSelectionServiceFactory + .getService(dsInfos, dfInfos, emptyDatasets, 0, + this.getRequestType()); dataSelectionService.checkOnline(); } CRC32 crc = new CRC32(); - CheckedWithSizeInputStream is = new CheckedWithSizeInputStream(body, crc); + CheckedWithSizeInputStream is = new CheckedWithSizeInputStream( + body, crc); String location; try { - location = serviceProvider.getMainStorage().put(dsInfo, name, is); + location = serviceProvider.getMainStorage().put(dsInfo, + name, is); } catch (IllegalArgumentException e) { - throw new BadRequestException("Illegal filename or dataset: " + e.getMessage()); + throw new BadRequestException( + "Illegal filename or dataset: " + e.getMessage()); } is.close(); long checksum = crc.getValue(); long size = is.getSize(); - + try { - dfId = registerDatafile(sessionId, name, datafileFormatId, location, checksum, size, ds, - description, doi, datafileCreateTime, datafileModTime); - } catch (InsufficientPrivilegesException | NotFoundException | InternalException - | BadRequestException e) { - logger.debug("Problem with registration " + e.getClass() + " " + e.getMessage() + dfId = registerDatafile(sessionId, name, datafileFormatId, + location, checksum, size, ds, description, doi, + datafileCreateTime, datafileModTime); + } catch (InsufficientPrivilegesException | NotFoundException + | InternalException | BadRequestException e) { + logger.debug("Problem with registration " + e.getClass() + + " " + e.getMessage() + " datafile will now be deleted"); String userId = null; try { - userId = serviceProvider.getIcat().getUserName(sessionId); + userId = serviceProvider.getIcat() + .getUserName(sessionId); } catch (IcatException_Exception e1) { - logger.error("Unable to get user name for session " + sessionId + " so mainStorage.delete of " + logger.error("Unable to get user name for session " + + sessionId + " so mainStorage.delete of " + location + " may fail"); } - serviceProvider.getMainStorage().delete(location, userId, userId); + serviceProvider.getMainStorage().delete(location, userId, + userId); throw e; } @@ -240,62 +268,80 @@ public ValueContainer handleRequest() } else if (storageUnit == StorageUnit.DATAFILE) { Datafile df; try { - df = (Datafile) serviceProvider.getIcatReader().get("Datafile", dfId); + df = (Datafile) serviceProvider.getIcatReader() + .get("Datafile", dfId); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + + " " + e.getMessage()); } - serviceProvider.getFsm().queue(new DatafileInfo(dfId, name, location, df.getCreateId(), df.getModId(), dsInfo.getId()), - DeferredOp.WRITE); + serviceProvider.getFsm() + .queue(new DatafileInfo(dfId, name, location, + df.getCreateId(), df.getModId(), + dsInfo.getId()), DeferredOp.WRITE); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Json.createGenerator(baos).writeStartObject().write("id", dfId).write("checksum", checksum) - .write("location", location.replace("\\", "\\\\").replace("'", "\\'")).write("size", size) - .writeEnd().close(); - String resp = wrap ? prefix + baos.toString() + suffix : baos.toString(); - - return new ValueContainer(Response.status(HttpURLConnection.HTTP_CREATED).entity(resp).build()); + Json.createGenerator(baos).writeStartObject().write("id", dfId) + .write("checksum", checksum) + .write("location", + location.replace("\\", "\\\\").replace("'", + "\\'")) + .write("size", size).writeEnd().close(); + String resp = wrap ? prefix + baos.toString() + suffix + : baos.toString(); + + return new ValueContainer( + Response.status(HttpURLConnection.HTTP_CREATED) + .entity(resp).build()); } catch (AlreadyLockedException e) { logger.debug("Could not acquire lock, put failed"); throw new DataNotOnlineException("Data is busy"); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " putting " + name + " to Dataset with id " - + datasetIdString); - throw new InternalException(e.getClass() + " " + e.getMessage()); + logger.error("I/O exception " + e.getMessage() + " putting " + + name + " to Dataset with id " + datasetIdString); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IdsException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen.writeStartObject().write("code", e.getClass().getSimpleName()).write("message", e.getShortMessage()); + gen.writeStartObject().write("code", e.getClass().getSimpleName()) + .write("message", e.getShortMessage()); gen.writeEnd().close(); if (wrap) { String pre = padding ? paddedPrefix : prefix; - return new ValueContainer(Response.status(e.getHttpStatusCode()).entity(pre + baos.toString().replace("'", "\\'") + suffix) + return new ValueContainer(Response + .status(e.getHttpStatusCode()).entity(pre + + baos.toString().replace("'", "\\'") + suffix) .build()); } else { - return new ValueContainer(Response.status(e.getHttpStatusCode()).entity(baos.toString()).build()); + return new ValueContainer(Response.status(e.getHttpStatusCode()) + .entity(baos.toString()).build()); } } } + private Long registerDatafile(String sessionId, String name, + long datafileFormatId, String location, long checksum, long size, + Dataset dataset, String description, String doi, + Long datafileCreateTime, Long datafileModTime) + throws InsufficientPrivilegesException, NotFoundException, + InternalException, BadRequestException { - private Long registerDatafile(String sessionId, String name, long datafileFormatId, String location, long checksum, - long size, Dataset dataset, String description, String doi, Long datafileCreateTime, Long datafileModTime) - throws InsufficientPrivilegesException, NotFoundException, InternalException, BadRequestException { - - var serviceProvider = ServiceProvider.getInstance(); ICAT icat = serviceProvider.getIcat(); - + final Datafile df = new Datafile(); DatafileFormat format; try { - format = (DatafileFormat) icat.get(sessionId, "DatafileFormat", datafileFormatId); + format = (DatafileFormat) icat.get(sessionId, "DatafileFormat", + datafileFormatId); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES + || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -315,12 +361,14 @@ private Long registerDatafile(String sessionId, String name, long datafileFormat if (datafileCreateTime != null) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTimeInMillis(datafileCreateTime); - df.setDatafileCreateTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); + df.setDatafileCreateTime( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); } if (datafileModTime != null) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTimeInMillis(datafileModTime); - df.setDatafileModTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); + df.setDatafileModTime( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); } try { long dfId = icat.create(sessionId, df); @@ -328,15 +376,18 @@ private Long registerDatafile(String sessionId, String name, long datafileFormat String key = serviceProvider.getPropertyHandler().getKey(); if (key != null) { - df.setLocation(location + " " + IcatSecurity.digest(dfId, location, key)); + df.setLocation(location + " " + + IcatSecurity.digest(dfId, location, key)); icat.update(sessionId, df); } - logger.debug("Registered datafile for dataset {} for {}", dataset.getId(), name + " at " + location); + logger.debug("Registered datafile for dataset {} for {}", + dataset.getId(), name + " at " + location); return dfId; } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES + || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.VALIDATION) { @@ -350,12 +401,15 @@ private Long registerDatafile(String sessionId, String name, long datafileFormat public String addParametersToLogString() { return "name='" + name + "' " + "datafileFormatId='" - + datafileFormatIdString + "' " + "datasetId='" + datasetIdString + "' " + "description='" - + description + "' " + "doi='" + doi + "' " + "datafileCreateTime='" + datafileCreateTimeString - + "' " + "datafileModTime='" + datafileModTimeString + "'"; + + datafileFormatIdString + "' " + "datasetId='" + + datasetIdString + "' " + "description='" + description + "' " + + "doi='" + doi + "' " + "datafileCreateTime='" + + datafileCreateTimeString + "' " + "datafileModTime='" + + datafileModTimeString + "'"; } - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { gen.write("userName", serviceProvider.getIcat().getUserName(sessionId)); gen.write("datafileId", dfId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java index 2fff2bb3..b8f6e667 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java @@ -15,20 +15,24 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; - public class ResetHandler extends DataRequestHandler { - public ResetHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.RESET, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public ResetHandler(String ip, String preparedId, String sessionId, + String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.RESET, ip, preparedId, sessionId, investigationIds, + datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { FiniteStateMachine fsm = ServiceProvider.getInstance().getFsm(); - for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { + for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos() + .values()) { fsm.recordSuccess(dataInfo.getId()); } @@ -39,7 +43,5 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic public CallType getCallType() { return CallType.MIGRATE; } - - } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java index 77e0c7e5..3f3c1e81 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java @@ -15,16 +15,20 @@ public class RestoreHandler extends DataRequestHandler { - public RestoreHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.RESTORE, ip, sessionId, investigationIds, datasetIds, datafileIds); + public RestoreHandler(String ip, String sessionId, String investigationIds, + String datasetIds, String datafileIds) { + super(RequestType.RESTORE, ip, sessionId, investigationIds, datasetIds, + datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - dataSelectionService.scheduleTasks(DeferredOp.RESTORE); + dataSelectionService.scheduleTasks(DeferredOp.RESTORE); return ValueContainer.getVoid(); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java index c6f994f4..31b53ca9 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java @@ -23,33 +23,41 @@ public class WriteHandler extends DataRequestHandler { - public WriteHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.WRITE, ip, sessionId, investigationIds, datasetIds, datafileIds); + public WriteHandler(String ip, String sessionId, String investigationIds, + String datasetIds, String datafileIds) { + super(RequestType.WRITE, ip, sessionId, investigationIds, datasetIds, + datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var serviceProvider = ServiceProvider.getInstance(); if (!serviceProvider.getPropertyHandler().getEnableWrite()) { - throw new NotImplementedException("This operation has been configured to be unavailable"); + throw new NotImplementedException( + "This operation has been configured to be unavailable"); } Map dsInfos = dataSelectionService.getDsInfo(); - try (Lock lock = serviceProvider.getLockManager().lock(dsInfos.values(), LockType.SHARED)) { + try (Lock lock = serviceProvider.getLockManager().lock(dsInfos.values(), + LockType.SHARED)) { if (twoLevel) { boolean maybeOffline = false; - for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { + for (DataInfoBase dataInfo : dataSelectionService + .getPrimaryDataInfos().values()) { if (!dataSelectionService.existsInMainStorage(dataInfo)) { maybeOffline = true; } } if (maybeOffline) { - throw new DataNotOnlineException("Requested data is not online, write request refused"); + throw new DataNotOnlineException( + "Requested data is not online, write request refused"); } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java b/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java index 1291e98e..fefc5a49 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java @@ -19,45 +19,59 @@ public abstract class DataControllerBase { /** * matches standard UUID format of 8-4-4-4-12 hexadecimal digits */ - public static final Pattern uuidRegExp = Pattern - .compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); + public static final Pattern uuidRegExp = Pattern.compile( + "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); - public abstract DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException; + public abstract DataSelectionService provideDataSelectionService( + RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, + InsufficientPrivilegesException, NotImplementedException; public abstract void validateUUID() throws BadRequestException; public abstract String addParametersToLogString(); - public abstract void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException; + public abstract void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException; - public abstract boolean mustZip(boolean zip, DataSelectionService dataSelectionService); + public abstract boolean mustZip(boolean zip, + DataSelectionService dataSelectionService); public abstract String getOperationId(); /** - * returns the current sessionId. If it's null or not defined it should be created + * returns the current sessionId. If it's null or not defined it should be + * created + * * @return - * @throws InternalException + * @throws InternalException */ public abstract String forceGetSessionId() throws InternalException; /** * Provides a validity check for UUIDs - * @param thing You can give here a name of the prameter or whatever has been checked here (to provide a qualified error message if needed). - * @param id The String which has to be checked if it is a valid UUID + * + * @param thing You can give here a name of the prameter or whatever has + * been checked here (to provide a qualified error message if + * needed). + * @param id The String which has to be checked if it is a valid UUID * @throws BadRequestException */ - public static void validateUUID(String thing, String id) throws BadRequestException { + public static void validateUUID(String thing, String id) + throws BadRequestException { if (id == null || !uuidRegExp.matcher(id).matches()) - throw new BadRequestException("The " + thing + " parameter '" + id + "' is not a valid UUID"); + throw new BadRequestException("The " + thing + " parameter '" + id + + "' is not a valid UUID"); } protected String createSessionId() throws InternalException { String sessionId; try { - sessionId = ServiceProvider.getInstance().getIcatReader().getSessionId(); + sessionId = ServiceProvider.getInstance().getIcatReader() + .getSessionId(); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage()); } return sessionId; } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java index 72576fa5..0a730d33 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java @@ -13,29 +13,34 @@ import jakarta.json.stream.JsonGenerator; - public abstract class DataRequestHandler extends RequestHandlerBase { protected DataControllerBase dataController; - - protected DataRequestHandler(RequestType requestType, String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + protected DataRequestHandler(RequestType requestType, String ip, + String sessionId, String investigationIds, String datasetIds, + String datafileIds) { super(requestType, ip); - this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); + this.dataController = new UnpreparedDataController(sessionId, + investigationIds, datasetIds, datafileIds); } - protected DataRequestHandler(RequestType requestType, String ip, String preparedId) { + protected DataRequestHandler(RequestType requestType, String ip, + String preparedId) { super(requestType, ip); this.dataController = new PreparedDataController(preparedId); } - protected DataRequestHandler(RequestType requestType, String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + protected DataRequestHandler(RequestType requestType, String ip, + String preparedId, String sessionId, String investigationIds, + String datasetIds, String datafileIds) { super(requestType, ip); - if(sessionId != null) { - this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); + if (sessionId != null) { + this.dataController = new UnpreparedDataController(sessionId, + investigationIds, datasetIds, datafileIds); } else { this.dataController = new PreparedDataController(preparedId); } @@ -43,11 +48,14 @@ protected DataRequestHandler(RequestType requestType, String ip, String prepared } @Override - protected ValueContainer handleRequest() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + protected ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { this.dataController.validateUUID(); - DataSelectionService dataSelectionService = this.dataController.provideDataSelectionService(this.requestType); + DataSelectionService dataSelectionService = this.dataController + .provideDataSelectionService(this.requestType); ValueContainer result = this.handleDataRequest(dataSelectionService); return result; @@ -55,26 +63,38 @@ protected ValueContainer handleRequest() throws BadRequestException, InternalExc } @Override - protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + protected void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { this.dataController.addParametersToTransmitterJSON(gen); this.addCustomParametersToTransmitterJSON(gen); } - protected abstract ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws NotImplementedException, InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException; + protected abstract ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws NotImplementedException, InternalException, + BadRequestException, NotFoundException, + InsufficientPrivilegesException, DataNotOnlineException; @Override protected String addParametersToLogString() { - return this.dataController.addParametersToLogString() + " " + this.addCustomParametersToLogString(); + return this.dataController.addParametersToLogString() + " " + + this.addCustomParametersToLogString(); } /** - * Override this method in your concrete DataRequestHandler to add custom parameters to the JSON which will be transmitted. + * Override this method in your concrete DataRequestHandler to add custom + * parameters to the JSON which will be transmitted. + * * @param gen */ - protected void addCustomParametersToTransmitterJSON(JsonGenerator gen) {} + protected void addCustomParametersToTransmitterJSON(JsonGenerator gen) { + } /** - * Override this method in your concrete DataRequestHandler to add custom parameters to the log output. + * Override this method in your concrete DataRequestHandler to add custom + * parameters to the log output. */ - protected String addCustomParametersToLogString() { return "";} + protected String addCustomParametersToLogString() { + return ""; + } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java b/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java index f9fe6519..c15203ab 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java @@ -24,7 +24,8 @@ public class PreparedDataController extends DataControllerBase { - protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); + protected final static Logger logger = LoggerFactory + .getLogger(RequestHandlerBase.class); String preparedId; @@ -34,16 +35,21 @@ public PreparedDataController(String preparedId) { } @Override - public DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, - BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - - var preparedDir = ServiceProvider.getInstance().getPropertyHandler().getCacheDir().resolve("prepared"); + public DataSelectionService provideDataSelectionService( + RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, + InsufficientPrivilegesException, NotImplementedException { + + var preparedDir = ServiceProvider.getInstance().getPropertyHandler() + .getCacheDir().resolve("prepared"); Prepared prepared; - try (InputStream stream = Files.newInputStream(preparedDir.resolve(preparedId))) { + try (InputStream stream = Files + .newInputStream(preparedDir.resolve(preparedId))) { prepared = Prepared.unpack(stream); } catch (NoSuchFileException e) { - throw new NotFoundException("The preparedId " + preparedId + " is not known"); + throw new NotFoundException( + "The preparedId " + preparedId + " is not known"); } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -62,12 +68,14 @@ public String addParametersToLogString() { } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { gen.write(RequestIdNames.preparedId, this.preparedId); } @Override - public boolean mustZip(boolean zip, DataSelectionService dataSelectionService) { + public boolean mustZip(boolean zip, + DataSelectionService dataSelectionService) { return zip; } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java b/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java index 9524b7a0..79d741f5 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java @@ -23,14 +23,15 @@ import org.icatproject.ids.helpers.ValueContainer; import org.icatproject.ids.services.ServiceProvider; - /** - * This base class represents all common properties and methods which are needed by each request handler. - * Request handlers schould be added to the internal request handler list in RequestHandlerService, to be able to be called. + * This base class represents all common properties and methods which are needed + * by each request handler. Request handlers schould be added to the internal + * request handler list in RequestHandlerService, to be able to be called. */ public abstract class RequestHandlerBase { - protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); + protected final static Logger logger = LoggerFactory + .getLogger(RequestHandlerBase.class); protected Path preparedDir; protected boolean twoLevel; protected StorageUnit storageUnit; @@ -41,11 +42,10 @@ public abstract class RequestHandlerBase { /** * matches standard UUID format of 8-4-4-4-12 hexadecimal digits */ - public static final Pattern uuidRegExp = Pattern - .compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); - + public static final Pattern uuidRegExp = Pattern.compile( + "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); - protected RequestHandlerBase(RequestType requestType, String ip ) { + protected RequestHandlerBase(RequestType requestType, String ip) { this.requestType = requestType; this.ip = ip; @@ -58,9 +58,9 @@ protected RequestHandlerBase(RequestType requestType, String ip ) { this.readOnly = propertyHandler.getReadOnly(); } - /** * Informs about the RequestType the handler ist providing a handling for. + * * @return */ public RequestType getRequestType() { @@ -68,7 +68,9 @@ public RequestType getRequestType() { } /** - * The core method of each request handler. It has to be overwritten in the concrete implementation to provide an individual request handling + * The core method of each request handler. It has to be overwritten in the + * concrete implementation to provide an individual request handling + * * @return A ValueContainer with an indiviadual result type. * @throws BadRequestException * @throws InternalException @@ -77,11 +79,15 @@ public RequestType getRequestType() { * @throws DataNotOnlineException * @throws NotImplementedException */ - public ValueContainer handle() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handle() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { // some preprocessing long start = System.currentTimeMillis(); - logger.info("New webservice request: " + this.requestType.toString().toLowerCase() + " " + this.addParametersToLogString()); + logger.info("New webservice request: " + + this.requestType.toString().toLowerCase() + " " + + this.addParametersToLogString()); // Do it ValueContainer result = this.handleRequest(); @@ -93,44 +99,56 @@ public ValueContainer handle() throws BadRequestException, InternalException, In } - private void transmit(long start) throws BadRequestException { - if (ServiceProvider.getInstance().getLogSet().contains(this.getCallType())) { + if (ServiceProvider.getInstance().getLogSet() + .contains(this.getCallType())) { try { String body = this.provideTransmissionBody(); - ServiceProvider.getInstance().getTransmitter().processMessage("archive", ip, body, start); + ServiceProvider.getInstance().getTransmitter() + .processMessage("archive", ip, body, start); } catch (IcatException_Exception e) { - logger.error("Failed to prepare jms message " + e.getClass() + " " + e.getMessage()); + logger.error("Failed to prepare jms message " + e.getClass() + + " " + e.getMessage()); } } } - public String provideTransmissionBody() throws BadRequestException, IcatException_Exception { + public String provideTransmissionBody() + throws BadRequestException, IcatException_Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos) + .writeStartObject()) { this.addParametersToTransmitterJSON(gen); gen.writeEnd(); } return baos.toString(); } - /** - * Override to add additional parameters to the log output for the current request + * Override to add additional parameters to the log output for the current + * request + * * @return */ - protected String addParametersToLogString() { return ""; } + protected String addParametersToLogString() { + return ""; + } /** * Override to add additional parameters to the transmitter JSON + * * @param gen * @throws IcatException_Exception * @throws BadRequestException */ - protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException {} + protected void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { + } /** - * The core method of each request handler. It has to be overwritten in the concrete implementation to provide an individual request handling + * The core method of each request handler. It has to be overwritten in the + * concrete implementation to provide an individual request handling + * * @return A ValueContainer with an indiviadual result type. * @throws BadRequestException * @throws InternalException @@ -139,10 +157,15 @@ protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatExce * @throws DataNotOnlineException * @throws NotImplementedException */ - protected abstract ValueContainer handleRequest() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException; + protected abstract ValueContainer handleRequest() + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException; /** - * each handler should provide its own CallType which is needed to create the Transmitter message + * each handler should provide its own CallType which is needed to create + * the Transmitter message + * * @return the Calltype of the request */ protected abstract CallType getCallType(); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java b/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java index 57668b02..fbc66012 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java @@ -20,7 +20,8 @@ public class UnpreparedDataController extends DataControllerBase { public String datasetIds; public String datafileIds; - public UnpreparedDataController(String sessionId, String investigationIds, String datasetIds, String datafileIds) { + public UnpreparedDataController(String sessionId, String investigationIds, + String datasetIds, String datafileIds) { this.sessionId = sessionId; this.investigationIds = investigationIds; this.datasetIds = datasetIds; @@ -35,41 +36,48 @@ public void validateUUID() throws BadRequestException { @Override public String addParametersToLogString() { return "investigationIds='" + investigationIds + "' " + "datasetIds='" - + datasetIds + "' " + "datafileIds='" + datafileIds + "'"; + + datasetIds + "' " + "datafileIds='" + datafileIds + "'"; } @Override - public DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - return DataSelectionServiceFactory.getService(sessionId, investigationIds, datasetIds, datafileIds, requestType); + public DataSelectionService provideDataSelectionService( + RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, + InsufficientPrivilegesException, NotImplementedException { + return DataSelectionServiceFactory.getService(sessionId, + investigationIds, datasetIds, datafileIds, requestType); } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { - gen.write("userName", ServiceProvider.getInstance().getIcat().getUserName(sessionId)); + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { + gen.write("userName", + ServiceProvider.getInstance().getIcat().getUserName(sessionId)); addIds(gen, investigationIds, datasetIds, datafileIds); } - - - protected void addIds(JsonGenerator gen, String investigationIds, String datasetIds, String datafileIds) - throws BadRequestException { + protected void addIds(JsonGenerator gen, String investigationIds, + String datasetIds, String datafileIds) throws BadRequestException { if (investigationIds != null) { gen.writeStartArray("investigationIds"); - for (long invid : DataSelectionService.getValidIds("investigationIds", investigationIds)) { + for (long invid : DataSelectionService + .getValidIds("investigationIds", investigationIds)) { gen.write(invid); } gen.writeEnd(); } if (datasetIds != null) { gen.writeStartArray("datasetIds"); - for (long invid : DataSelectionService.getValidIds("datasetIds", datasetIds)) { + for (long invid : DataSelectionService.getValidIds("datasetIds", + datasetIds)) { gen.write(invid); } gen.writeEnd(); } if (datafileIds != null) { gen.writeStartArray("datafileIds"); - for (long invid : DataSelectionService.getValidIds("datafileIds", datafileIds)) { + for (long invid : DataSelectionService.getValidIds("datafileIds", + datafileIds)) { gen.write(invid); } gen.writeEnd(); @@ -77,7 +85,8 @@ protected void addIds(JsonGenerator gen, String investigationIds, String dataset } @Override - public boolean mustZip(boolean zip, DataSelectionService dataSelectionService) { + public boolean mustZip(boolean zip, + DataSelectionService dataSelectionService) { return zip ? true : dataSelectionService.mustZip(); } @@ -91,7 +100,7 @@ public String forceGetSessionId() throws InternalException { if (this.sessionId == null) { this.sessionId = this.createSessionId(); } - + return this.sessionId; } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java index 0b4aa315..29e1130d 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java @@ -19,25 +19,32 @@ public class GetSizeHandler extends DataRequestHandler { - public GetSizeHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETSIZE, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public GetSizeHandler(String ip, String preparedId, String sessionId, + String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETSIZE, ip, preparedId, sessionId, investigationIds, + datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - + logger.debug("Slow computation for normal case"); long size = 0; - size = this.updateSizeFromDataInfoIds(size, dataSelectionService.getDfInfo(), this.dataController.forceGetSessionId()); + size = this.updateSizeFromDataInfoIds(size, + dataSelectionService.getDfInfo(), + this.dataController.forceGetSessionId()); return new ValueContainer(size); } - - protected long updateSizeFromDataInfoIds(long size, Map dataInfos, String sessionId) throws InternalException { + protected long updateSizeFromDataInfoIds(long size, + Map dataInfos, String sessionId) + throws InternalException { StringBuilder sb = new StringBuilder(); int n = 0; for (DataInfoBase dataInfo : dataInfos.values()) { @@ -58,11 +65,13 @@ protected long updateSizeFromDataInfoIds(long size, Map data return size; } - - private long getSizeFor(String sessionId, StringBuilder sb) throws InternalException { - String query = "SELECT SUM(df.fileSize) from Datafile df WHERE df.id IN (" + sb.toString() + ") AND df.location IS NOT NULL"; + private long getSizeFor(String sessionId, StringBuilder sb) + throws InternalException { + String query = "SELECT SUM(df.fileSize) from Datafile df WHERE df.id IN (" + + sb.toString() + ") AND df.location IS NOT NULL"; try { - return (Long) ServiceProvider.getInstance().getIcat().search(sessionId, query).get(0); + return (Long) ServiceProvider.getInstance().getIcat() + .search(sessionId, query).get(0); } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (IndexOutOfBoundsException e) { @@ -74,5 +83,5 @@ private long getSizeFor(String sessionId, StringBuilder sb) throws InternalExcep public CallType getCallType() { return CallType.INFO; } - + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java index 6b57cd9b..a3abf50d 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java @@ -24,44 +24,58 @@ public class GetSizeHandlerForFastProcessing extends RequestHandlerBase { UnpreparedDataController dataController; - public GetSizeHandlerForFastProcessing(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + public GetSizeHandlerForFastProcessing(String ip, String sessionId, + String investigationIds, String datasetIds, String datafileIds) { super(RequestType.GETSIZE, ip); - this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); + this.dataController = new UnpreparedDataController(sessionId, + investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleRequest() throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - + public ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { + this.dataController.validateUUID(); var serviceProvider = ServiceProvider.getInstance(); - List dfids = DataSelectionService.getValidIds("datafileIds", dataController.datafileIds); - List dsids = DataSelectionService.getValidIds("datasetIds", dataController.datasetIds); - List invids = DataSelectionService.getValidIds("investigationIds", dataController.investigationIds); + List dfids = DataSelectionService.getValidIds("datafileIds", + dataController.datafileIds); + List dsids = DataSelectionService.getValidIds("datasetIds", + dataController.datasetIds); + List invids = DataSelectionService.getValidIds("investigationIds", + dataController.investigationIds); - if (dfids.size() + dsids.size() + invids.size() == 1) { long size = 0; - size = getSizeFor(dataController.getOperationId(), invids, "df.dataset.investigation.id") - + getSizeFor(dataController.getOperationId(), dsids, "df.dataset.id") - + getSizeFor(dataController.getOperationId(), dfids, "df.id"); + size = getSizeFor(dataController.getOperationId(), invids, + "df.dataset.investigation.id") + + getSizeFor(dataController.getOperationId(), dsids, + "df.dataset.id") + + getSizeFor(dataController.getOperationId(), dfids, + "df.id"); logger.debug("Fast computation for simple case"); if (size == 0) { try { if (dfids.size() != 0) { - Datafile datafile = (Datafile) serviceProvider.getIcat().get(dataController.getOperationId(), "Datafile", dfids.get(0)); + Datafile datafile = (Datafile) serviceProvider.getIcat() + .get(dataController.getOperationId(), + "Datafile", dfids.get(0)); if (datafile.getLocation() == null) { throw new NotFoundException("Datafile not found"); } } if (dsids.size() != 0) { - serviceProvider.getIcat().get(dataController.getOperationId(), "Dataset", dsids.get(0)); + serviceProvider.getIcat().get( + dataController.getOperationId(), "Dataset", + dsids.get(0)); } if (invids.size() != 0) { - serviceProvider.getIcat().get(dataController.getOperationId(), "Investigation", invids.get(0)); + serviceProvider.getIcat().get( + dataController.getOperationId(), + "Investigation", invids.get(0)); } } catch (IcatException_Exception e) { throw new NotFoundException(e.getMessage()); @@ -70,10 +84,10 @@ public ValueContainer handleRequest() throws BadRequestException, InternalExcept return new ValueContainer(size); } - - return ValueContainer.getInvalid(); //is case of fast computation is not the right way. - + return ValueContainer.getInvalid(); // is case of fast computation is + // not the right way. + } @Override @@ -81,13 +95,17 @@ public CallType getCallType() { return CallType.INFO; } - public String addParametersToLogString() { return this.dataController.addParametersToLogString(); } + public String addParametersToLogString() { + return this.dataController.addParametersToLogString(); + } - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { this.dataController.addParametersToTransmitterJSON(gen); } - private long getSizeFor(String sessionId, List ids, String where) throws InternalException { + private long getSizeFor(String sessionId, List ids, String where) + throws InternalException { long size = 0; if (ids != null) { @@ -112,16 +130,19 @@ private long getSizeFor(String sessionId, List ids, String where) throws I return size; } - private long evalSizeFor(String sessionId, String where, StringBuilder sb) throws InternalException { - String query = "SELECT SUM(df.fileSize) from Datafile df WHERE " + where + " IN (" + sb.toString() + ") AND df.location IS NOT NULL"; + private long evalSizeFor(String sessionId, String where, StringBuilder sb) + throws InternalException { + String query = "SELECT SUM(df.fileSize) from Datafile df WHERE " + where + + " IN (" + sb.toString() + ") AND df.location IS NOT NULL"; logger.debug("icat query for size: {}", query); try { - return (Long) ServiceProvider.getInstance().getIcat().search(sessionId, query).get(0); + return (Long) ServiceProvider.getInstance().getIcat() + .search(sessionId, query).get(0); } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (IndexOutOfBoundsException e) { return 0L; } } - + } diff --git a/src/main/java/org/icatproject/ids/services/ICATGetter.java b/src/main/java/org/icatproject/ids/services/ICATGetter.java index 4073c3ce..d62bd126 100644 --- a/src/main/java/org/icatproject/ids/services/ICATGetter.java +++ b/src/main/java/org/icatproject/ids/services/ICATGetter.java @@ -17,19 +17,21 @@ */ public class ICATGetter { - private static String[] suffices = new String[]{"ICATService/ICAT?wsdl", "icat/ICAT?wsdl"}; + private static String[] suffices = new String[] { "ICATService/ICAT?wsdl", + "icat/ICAT?wsdl" }; /** * Provide access to an ICAT SOAP web service with the basic URL string * provided. This exists to hide the differences between containers. * * @param urlString the url of the machine to be contacted. If the url has a - * non-empty file part it is used unchanged, otherwise suffices - * are tried suitable for Glassfish and WildFly. + * non-empty file part it is used unchanged, otherwise + * suffices are tried suitable for Glassfish and WildFly. * @return an ICAT * @throws IcatException_Exception if something is wrong */ - public static ICAT getService(String urlString) throws IcatException_Exception { + public static ICAT getService(String urlString) + throws IcatException_Exception { if (urlString == null) { throwSessionException("Argument to constructor must not be null"); @@ -47,14 +49,16 @@ public static ICAT getService(String urlString) throws IcatException_Exception { for (String suffix : suffices) { String icatUrlWsdl = urlString + "/" + suffix; try { - icatService = new ICATService(new URL(icatUrlWsdl)).getICATPort(); + icatService = new ICATService(new URL(icatUrlWsdl)) + .getICATPort(); icatService.getApiVersion(); return icatService; } catch (MalformedURLException e) { throwSessionException("Invalid URL"); } catch (WebServiceException e) { Throwable cause = e.getCause(); - if (cause != null && cause.getMessage().contains("security")) { + if (cause != null + && cause.getMessage().contains("security")) { throwSessionException(cause.getMessage()); } } catch (Exception e) { @@ -86,7 +90,8 @@ public static String getCleanUrl(String urlString) { for (String suffix : suffices) { suffix = "/" + suffix; if (urlString.endsWith(suffix)) { - return urlString.substring(0, urlString.length() - suffix.length()); + return urlString.substring(0, + urlString.length() - suffix.length()); } } if (urlString.endsWith("/")) { @@ -95,7 +100,8 @@ public static String getCleanUrl(String urlString) { return urlString; } - private static void throwSessionException(String msg) throws IcatException_Exception { + private static void throwSessionException(String msg) + throws IcatException_Exception { IcatException except = new IcatException(); except.setMessage(msg); except.setType(IcatExceptionType.SESSION); diff --git a/src/main/java/org/icatproject/ids/services/IcatReader.java b/src/main/java/org/icatproject/ids/services/IcatReader.java index 9f5adbb8..ab184adf 100644 --- a/src/main/java/org/icatproject/ids/services/IcatReader.java +++ b/src/main/java/org/icatproject/ids/services/IcatReader.java @@ -22,7 +22,8 @@ public class IcatReader { private String sessionId; - private final static Logger logger = LoggerFactory.getLogger(IcatReader.class); + private final static Logger logger = LoggerFactory + .getLogger(IcatReader.class); private PropertyHandler propertyHandler; @@ -31,7 +32,8 @@ public IcatReader() { } - // primarily required for testing to make it possible to use a mocked PropertyHandler + // primarily required for testing to make it possible to use a mocked + // PropertyHandler public IcatReader(PropertyHandler propertyHandler) { this.propertyHandler = propertyHandler; init(); @@ -43,7 +45,8 @@ private void init() { login(); logger.info("Reader started"); } catch (Exception e) { - throw new RuntimeException("Reader reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException( + "Reader reports " + e.getClass() + " " + e.getMessage()); } } @@ -67,7 +70,8 @@ private void login() throws IcatException_Exception { } } - public EntityBaseBean get(String query, long id) throws IcatException_Exception { + public EntityBaseBean get(String query, long id) + throws IcatException_Exception { try { return icat.get(sessionId, query, id); } catch (IcatException_Exception e) { diff --git a/src/main/java/org/icatproject/ids/services/LockManager.java b/src/main/java/org/icatproject/ids/services/LockManager.java index 49beba05..0e42bf25 100644 --- a/src/main/java/org/icatproject/ids/services/LockManager.java +++ b/src/main/java/org/icatproject/ids/services/LockManager.java @@ -92,7 +92,9 @@ public void release() { try { storageLock.close(); } catch (Exception e) { - logger.error("Error while closing lock on {} in the storage plugin: {}.", id, e.getMessage()); + logger.error( + "Error while closing lock on {} in the storage plugin: {}.", + id, e.getMessage()); } } logger.debug("Released a lock on {}.", id); @@ -131,7 +133,8 @@ private void init() { logger.debug("LockManager initialized."); } - public Lock lock(DatasetInfo ds, LockType type) throws AlreadyLockedException, IOException { + public Lock lock(DatasetInfo ds, LockType type) + throws AlreadyLockedException, IOException { Long id = ds.getDsId(); assert id != null; synchronized (lockMap) { @@ -139,7 +142,8 @@ public Lock lock(DatasetInfo ds, LockType type) throws AlreadyLockedException, I if (le == null) { le = new LockEntry(id, type); } else { - if (type == LockType.EXCLUSIVE || le.type == LockType.EXCLUSIVE) { + if (type == LockType.EXCLUSIVE + || le.type == LockType.EXCLUSIVE) { throw new AlreadyLockedException(); } } @@ -156,12 +160,16 @@ public Lock lock(DatasetInfo ds, LockType type) throws AlreadyLockedException, I } } - public Lock lock(Collection datasets, LockType type) throws AlreadyLockedException, IOException, InternalException { + public Lock lock(Collection datasets, LockType type) + throws AlreadyLockedException, IOException, InternalException { LockCollection locks = new LockCollection(); try { for (DataInfoBase dataInfo : datasets) { DatasetInfo ds = (DatasetInfo) dataInfo; - if(ds == null) throw new InternalException("Could not cast " + dataInfo.getClass() + " to DataSetInfo. Did you handed over another sub type of DataInfoBase? "); + if (ds == null) + throw new InternalException("Could not cast " + + dataInfo.getClass() + + " to DataSetInfo. Did you handed over another sub type of DataInfoBase? "); locks.add(lock(ds, type)); } } catch (AlreadyLockedException | IOException e) { diff --git a/src/main/java/org/icatproject/ids/services/PropertyHandler.java b/src/main/java/org/icatproject/ids/services/PropertyHandler.java index e3cbe354..ff66afad 100644 --- a/src/main/java/org/icatproject/ids/services/PropertyHandler.java +++ b/src/main/java/org/icatproject/ids/services/PropertyHandler.java @@ -36,7 +36,8 @@ public class PropertyHandler { private static PropertyHandler instance = null; - private static final Logger logger = LoggerFactory.getLogger(PropertyHandler.class); + private static final Logger logger = LoggerFactory + .getLogger(PropertyHandler.class); public synchronized static PropertyHandler getInstance() { if (instance == null) { @@ -96,12 +97,16 @@ private PropertyHandler() { } preparedCount = props.getPositiveInt("preparedCount"); - processQueueIntervalSeconds = props.getPositiveLong("processQueueIntervalSeconds"); - rootUserNames = new HashSet<>(Arrays.asList(props.getString("rootUserNames").trim().split("\\s+"))); + processQueueIntervalSeconds = props + .getPositiveLong("processQueueIntervalSeconds"); + rootUserNames = new HashSet<>(Arrays.asList( + props.getString("rootUserNames").trim().split("\\s+"))); - reader = Arrays.asList(props.getString("reader").trim().split("\\s+")); + reader = Arrays + .asList(props.getString("reader").trim().split("\\s+")); if (reader.size() % 2 != 1) { - throw new IllegalStateException("reader must have an odd number of words"); + throw new IllegalStateException( + "reader must have an odd number of words"); } readOnly = props.getBoolean("readOnly", false); @@ -111,7 +116,8 @@ private PropertyHandler() { enableWrite = !readOnly; } - sizeCheckIntervalMillis = props.getPositiveInt("sizeCheckIntervalSeconds") * 1000L; + sizeCheckIntervalMillis = props + .getPositiveInt("sizeCheckIntervalSeconds") * 1000L; if (props.has("key")) { key = props.getString("key"); @@ -123,14 +129,17 @@ private PropertyHandler() { zipMapper = klass.getConstructor().newInstance(); logger.debug("ZipMapper initialised"); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException | SecurityException e) { + } catch (ClassNotFoundException | InstantiationException + | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException + | SecurityException e) { abort(e.getClass() + " " + e.getMessage()); } // Now get simple properties to pass to the plugins Properties simpleProps = new Properties(); - try (InputStream is = getClass().getClassLoader().getResourceAsStream("run.properties")) { + try (InputStream is = getClass().getClassLoader() + .getResourceAsStream("run.properties")) { simpleProps.load(is); } catch (IOException e) { abort(e.getClass() + " " + e.getMessage()); @@ -139,54 +148,67 @@ private PropertyHandler() { try { Class klass = (Class) Class .forName(props.getString("plugin.main.class")); - mainStorage = klass.getConstructor(Properties.class).newInstance(simpleProps); + mainStorage = klass.getConstructor(Properties.class) + .newInstance(simpleProps); logger.debug("mainStorage initialised"); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException | SecurityException e) { + } catch (ClassNotFoundException | InstantiationException + | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException + | SecurityException e) { logger.error("Plugin failed...", e); abort(e.getClass() + " " + e.getMessage()); } if (!props.has("plugin.archive.class")) { - logger.info("Property plugin.archive.class not set, single storage enabled."); + logger.info( + "Property plugin.archive.class not set, single storage enabled."); } else { try { Class klass = (Class) Class .forName(props.getString("plugin.archive.class")); - archiveStorage = klass.getConstructor(Properties.class).newInstance(simpleProps); + archiveStorage = klass.getConstructor(Properties.class) + .newInstance(simpleProps); logger.debug("archiveStorage initialised"); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException - | IllegalArgumentException | InvocationTargetException | NoSuchMethodException - | SecurityException e) { + } catch (ClassNotFoundException | InstantiationException + | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException + | SecurityException e) { logger.error("Plugin failed...", e); abort(e.getClass() + " " + e.getMessage()); } - startArchivingLevel = props.getPositiveLong("startArchivingLevel1024bytes") * 1024; - stopArchivingLevel = props.getPositiveLong("stopArchivingLevel1024bytes") * 1024; + startArchivingLevel = props + .getPositiveLong("startArchivingLevel1024bytes") * 1024; + stopArchivingLevel = props + .getPositiveLong("stopArchivingLevel1024bytes") * 1024; if (stopArchivingLevel >= startArchivingLevel) { abort("startArchivingLevel1024bytes must be greater than stopArchivingLevel1024bytes"); } try { String storageUnitName = props.getString("storageUnit"); - storageUnit = StorageUnit.valueOf(storageUnitName.toUpperCase()); + storageUnit = StorageUnit + .valueOf(storageUnitName.toUpperCase()); } catch (IllegalArgumentException e) { List vs = new ArrayList<>(); for (StorageUnit s : StorageUnit.values()) { vs.add(s.name()); } - abort("storageUnit value " + props.getString("storageUnit") + " must be taken from " + vs); + abort("storageUnit value " + props.getString("storageUnit") + + " must be taken from " + vs); } if (storageUnit == StorageUnit.DATASET) { - delayDatasetWrites = props.getPositiveLong("delayDatasetWritesSeconds"); + delayDatasetWrites = props + .getPositiveLong("delayDatasetWritesSeconds"); } else if (storageUnit == StorageUnit.DATAFILE) { - delayDatafileOperations = props.getPositiveLong("delayDatafileOperationsSeconds"); + delayDatafileOperations = props + .getPositiveLong("delayDatafileOperationsSeconds"); } tidyBlockSize = props.getPositiveInt("tidyBlockSize"); } try { - cacheDir = props.getFile("cache.dir").getCanonicalFile().toPath(); + cacheDir = props.getFile("cache.dir").getCanonicalFile() + .toPath(); } catch (IOException e) { abort("IOException " + e.getMessage()); } @@ -197,25 +219,31 @@ private PropertyHandler() { maxIdsInQuery = props.getPositiveInt("maxIdsInQuery"); /* JMS stuff */ - jmsTopicConnectionFactory = props.getString("jms.topicConnectionFactory", + jmsTopicConnectionFactory = props.getString( + "jms.topicConnectionFactory", "java:comp/DefaultJMSConnectionFactory"); /* Call logging categories */ if (props.has("log.list")) { - for (String callTypeString : props.getString("log.list").split("\\s+")) { + for (String callTypeString : props.getString("log.list") + .split("\\s+")) { try { - logSet.add(CallType.valueOf(callTypeString.toUpperCase())); + logSet.add( + CallType.valueOf(callTypeString.toUpperCase())); } catch (IllegalArgumentException e) { - abort("Value " + callTypeString + " in log.list must be chosen from " + abort("Value " + callTypeString + + " in log.list must be chosen from " + Arrays.asList(CallType.values())); } } logger.info("log.list: " + logSet); } else { - logger.info("'log.list' entry not present so no JMS call logging will be performed"); + logger.info( + "'log.list' entry not present so no JMS call logging will be performed"); } - useReaderForPerformance = props.getBoolean("useReaderForPerformance", false); + useReaderForPerformance = props + .getBoolean("useReaderForPerformance", false); } catch (CheckedPropertyException e) { abort(e.getMessage()); @@ -247,7 +275,8 @@ public synchronized ICAT getIcatService() { try { icatService = ICATGetter.getService(icatUrl); } catch (IcatException_Exception e) { - String msg = "Problem finding ICAT API version at " + icatUrl + ": " + e.getFaultInfo().getType() + " " + String msg = "Problem finding ICAT API version at " + icatUrl + + ": " + e.getFaultInfo().getType() + " " + e.getMessage(); logger.error(msg); try { @@ -285,15 +314,19 @@ public synchronized int getMaxEntities() { // will produce an error message. while (maxEntities == null) { try { - org.icatproject.icat.client.ICAT ricat = new org.icatproject.icat.client.ICAT(icatUrl); + org.icatproject.icat.client.ICAT ricat = new org.icatproject.icat.client.ICAT( + icatUrl); try (JsonReader parser = Json - .createReader(new ByteArrayInputStream(ricat.getProperties().getBytes()))) { + .createReader(new ByteArrayInputStream( + ricat.getProperties().getBytes()))) { maxEntities = parser.readObject().getInt("maxEntities"); - logger.info("maxEntities from the ICAT.server {} version {} is {}", icatUrl, ricat.getVersion(), - maxEntities); + logger.info( + "maxEntities from the ICAT.server {} version {} is {}", + icatUrl, ricat.getVersion(), maxEntities); } catch (Exception e) { - String msg = "Problem finding 1 ICAT API version " + e.getClass() + " " + e.getMessage(); + String msg = "Problem finding 1 ICAT API version " + + e.getClass() + " " + e.getMessage(); logger.error(msg); try { Thread.sleep(10000); @@ -302,7 +335,8 @@ public synchronized int getMaxEntities() { } } } catch (URISyntaxException e) { - String msg = "Problem finding 2 ICAT API version " + e.getClass() + " " + e.getMessage(); + String msg = "Problem finding 2 ICAT API version " + + e.getClass() + " " + e.getMessage(); logger.error(msg); throw new IllegalStateException(msg); } diff --git a/src/main/java/org/icatproject/ids/services/ServiceProvider.java b/src/main/java/org/icatproject/ids/services/ServiceProvider.java index dc179ad5..e06194ea 100644 --- a/src/main/java/org/icatproject/ids/services/ServiceProvider.java +++ b/src/main/java/org/icatproject/ids/services/ServiceProvider.java @@ -8,8 +8,9 @@ import org.icatproject.ids.plugin.MainStorageInterface; /** - * This class provides multiple services to the developer - * Maybe it is just for the redesign for version 3 and will later be replaced with dependency injection, when it will be more clear where which service is used. + * This class provides multiple services to the developer Maybe it is just for + * the redesign for version 3 and will later be replaced with dependency + * injection, when it will be more clear where which service is used. */ public class ServiceProvider { @@ -21,7 +22,9 @@ public class ServiceProvider { private IcatReader icatReader; private PropertyHandler propertyHandler; - private ServiceProvider(PropertyHandler propertyHandler, Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { + private ServiceProvider(PropertyHandler propertyHandler, + Transmitter transmitter, FiniteStateMachine fsm, + LockManager lockManager, IcatReader reader) { this.transmitter = transmitter; this.fsm = fsm; this.lockManager = lockManager; @@ -30,27 +33,37 @@ private ServiceProvider(PropertyHandler propertyHandler, Transmitter transmitter } /** - * At first, the ServiceProvider has to be created. Do not call getInstance() before you have called createInstande() + * At first, the ServiceProvider has to be created. Do not call + * getInstance() before you have called createInstande() + * * @param transmitter * @param fsm * @param lockManager * @param reader */ - public static void createInstance(Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { + public static void createInstance(Transmitter transmitter, + FiniteStateMachine fsm, LockManager lockManager, + IcatReader reader) { - createInstance(PropertyHandler.getInstance(), transmitter, fsm, lockManager, reader); + createInstance(PropertyHandler.getInstance(), transmitter, fsm, + lockManager, reader); } - public static void createInstance(PropertyHandler propertyHandler, Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { + public static void createInstance(PropertyHandler propertyHandler, + Transmitter transmitter, FiniteStateMachine fsm, + LockManager lockManager, IcatReader reader) { - if(instance != null) return; + if (instance != null) + return; - instance = new ServiceProvider(propertyHandler, transmitter, fsm, lockManager, reader); + instance = new ServiceProvider(propertyHandler, transmitter, fsm, + lockManager, reader); } public static ServiceProvider getInstance() { - if(instance == null) { - throw new RuntimeException("ServiceProvider is not yet instantiated, please call createInstance at first."); + if (instance == null) { + throw new RuntimeException( + "ServiceProvider is not yet instantiated, please call createInstance at first."); } return instance; } @@ -87,5 +100,4 @@ public Set getLogSet() { return PropertyHandler.getInstance().getLogSet(); } - } diff --git a/src/main/java/org/icatproject/ids/services/Transmitter.java b/src/main/java/org/icatproject/ids/services/Transmitter.java index 996522cb..267e4940 100644 --- a/src/main/java/org/icatproject/ids/services/Transmitter.java +++ b/src/main/java/org/icatproject/ids/services/Transmitter.java @@ -59,12 +59,15 @@ private void exit() { } } - public void processMessage(String operation, String ip, String body, long startMillis) { - try (Session jmsSession = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE)) { + public void processMessage(String operation, String ip, String body, + long startMillis) { + try (Session jmsSession = topicConnection.createSession(false, + Session.AUTO_ACKNOWLEDGE)) { TextMessage jmsg = jmsSession.createTextMessage(body); jmsg.setStringProperty("operation", operation); jmsg.setStringProperty("ip", ip); - jmsg.setLongProperty("millis", System.currentTimeMillis() - startMillis); + jmsg.setLongProperty("millis", + System.currentTimeMillis() - startMillis); jmsg.setLongProperty("start", startMillis); MessageProducer jmsProducer = jmsSession.createProducer(topic); jmsProducer.send(jmsg); diff --git a/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java b/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java index 5f9b372b..42a5d949 100644 --- a/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java +++ b/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java @@ -21,37 +21,45 @@ import org.slf4j.LoggerFactory; /** - * This class is to handle not finished work. - * It could be redesigned with sub classes depending on StorageUnit, but it isn't worth it i guess, just for one abstract method (loadDataInfo()). + * This class is to handle not finished work. It could be redesigned with sub + * classes depending on StorageUnit, but it isn't worth it i guess, just for one + * abstract method (loadDataInfo()). */ public class UnfinishedWorkService { - - protected final static Logger logger = LoggerFactory.getLogger(UnfinishedWorkService.class); + protected final static Logger logger = LoggerFactory + .getLogger(UnfinishedWorkService.class); public UnfinishedWorkService() { - + } - public void restartUnfinishedWork(Path markerDir, String key) throws InternalException { + public void restartUnfinishedWork(Path markerDir, String key) + throws InternalException { try { var serviceProvider = ServiceProvider.getInstance(); - StorageUnit storageUnit = serviceProvider.getPropertyHandler().getStorageUnit(); + StorageUnit storageUnit = serviceProvider.getPropertyHandler() + .getStorageUnit(); for (File file : markerDir.toFile().listFiles()) { - if(storageUnit == null) break; - - long id = Long.parseLong(file.toPath().getFileName().toString()); + if (storageUnit == null) + break; + + long id = Long + .parseLong(file.toPath().getFileName().toString()); try { DataInfoBase dataInfo = this.loadDataInfo(storageUnit, id); serviceProvider.getFsm().queue(dataInfo, DeferredOp.WRITE); - logger.info("Queued " + storageUnit.toString().toLowerCase() + " with id " + id + " " + dataInfo + logger.info("Queued " + storageUnit.toString().toLowerCase() + + " with id " + id + " " + dataInfo + " to be written as it was not written out previously by IDS"); } catch (IcatException_Exception e) { - if (e.getFaultInfo().getType() == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { - logger.warn( storageUnit.toString().toLowerCase() + " with id " + id + if (e.getFaultInfo() + .getType() == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { + logger.warn(storageUnit.toString().toLowerCase() + + " with id " + id + " was not written out by IDS and now no longer known to ICAT"); Files.delete(file.toPath()); } else { @@ -64,7 +72,6 @@ public void restartUnfinishedWork(Path markerDir, String key) throws InternalExc } } - public void cleanPreparedDir(Path preparedDir) { for (File file : preparedDir.toFile().listFiles()) { Path path = file.toPath(); @@ -80,7 +87,8 @@ public void cleanPreparedDir(Path preparedDir) { } thisSize += Files.size(path); Files.delete(path); - logger.debug("Deleted " + path + " to reclaim " + thisSize + " bytes"); + logger.debug("Deleted " + path + " to reclaim " + thisSize + + " bytes"); } catch (IOException e) { logger.debug("Failed to delete " + path + e.getMessage()); } @@ -88,31 +96,37 @@ public void cleanPreparedDir(Path preparedDir) { } } - public void cleanDatasetCache(Path datasetDir) { for (File dsFile : datasetDir.toFile().listFiles()) { Path path = dsFile.toPath(); try { long thisSize = Files.size(path); Files.delete(path); - logger.debug("Deleted " + path + " to reclaim " + thisSize + " bytes"); + logger.debug("Deleted " + path + " to reclaim " + thisSize + + " bytes"); } catch (IOException e) { - logger.debug("Failed to delete " + path + " " + e.getClass() + " " + e.getMessage()); + logger.debug("Failed to delete " + path + " " + e.getClass() + + " " + e.getMessage()); } } } - - private DataInfoBase loadDataInfo(StorageUnit storageUnit, long id) throws IcatException_Exception, InsufficientPrivilegesException, InternalException { - if(storageUnit == StorageUnit.DATASET) { - Dataset ds = (Dataset) ServiceProvider.getInstance().getIcatReader().get("Dataset ds INCLUDE ds.investigation.facility", id); + private DataInfoBase loadDataInfo(StorageUnit storageUnit, long id) + throws IcatException_Exception, InsufficientPrivilegesException, + InternalException { + if (storageUnit == StorageUnit.DATASET) { + Dataset ds = (Dataset) ServiceProvider.getInstance().getIcatReader() + .get("Dataset ds INCLUDE ds.investigation.facility", id); return new DatasetInfo(ds); } - if(storageUnit == StorageUnit.DATAFILE) { - Datafile df = (Datafile) ServiceProvider.getInstance().getIcatReader().get("Datafile ds INCLUDE ds.dataset", id); - String location = LocationHelper.getLocation(df.getId(), df.getLocation()); - return new DatafileInfo(id, df.getName(), location, df.getCreateId(), df.getModId(), df.getDataset().getId()); + if (storageUnit == StorageUnit.DATAFILE) { + Datafile df = (Datafile) ServiceProvider.getInstance() + .getIcatReader().get("Datafile ds INCLUDE ds.dataset", id); + String location = LocationHelper.getLocation(df.getId(), + df.getLocation()); + return new DatafileInfo(id, df.getName(), location, + df.getCreateId(), df.getModId(), df.getDataset().getId()); } return null; diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java index 650e410f..d5042bec 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java @@ -32,14 +32,16 @@ public abstract class DataSelectionService { - protected final static Logger logger = LoggerFactory.getLogger(DataSelectionService.class); + protected final static Logger logger = LoggerFactory + .getLogger(DataSelectionService.class); protected DataSelection dataSelection; protected RequestType requestType; - protected static ExecutorService threadPool; - static { threadPool = Executors.newCachedThreadPool(); } + static { + threadPool = Executors.newCachedThreadPool(); + } private Map preparedStatusMap = new ConcurrentHashMap<>(); @@ -50,41 +52,54 @@ class PreparedStatus { } - protected DataSelectionService(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { + protected DataSelectionService(SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, + Boolean zip, Boolean compress, RequestType requestType) { - this.dataSelection = new DataSelection(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress); + this.dataSelection = new DataSelection(dsInfos, dfInfos, emptyDatasets, + invids2, dsids, dfids, length, zip, compress); this.requestType = requestType; } - public abstract boolean isPrepared(String preparedId) throws InternalException; + public abstract boolean isPrepared(String preparedId) + throws InternalException; /** - * To get the DataInfos that is currently worked with, depending on StorageUnit + * To get the DataInfos that is currently worked with, depending on + * StorageUnit + * * @return */ public abstract SortedMap getPrimaryDataInfos(); - public abstract boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException; + public abstract boolean existsInMainStorage(DataInfoBase dataInfo) + throws InternalException; - public abstract void queueDelete() throws NotImplementedException, InternalException; + public abstract void queueDelete() + throws NotImplementedException, InternalException; - public abstract void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException; + public abstract void scheduleTasks(DeferredOp operation) + throws NotImplementedException, InternalException; - //TODO: maybe implementing this method here whould be the better way, than making it abstract. But we will miss the NotImplementedException for SingleLevelStorage in that case. + // TODO: maybe implementing this method here whould be the better way, than + // making it abstract. But we will miss the NotImplementedException for + // SingleLevelStorage in that case. // should we change the tests and the behavior of the ids server here - // public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { - // for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { // in case of SingleLevelStorage an empty map is returned and nothing will happen (even not an exception). - // ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); - // } + // public void scheduleTasks(DeferredOp operation) throws + // NotImplementedException, InternalException { + // for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { // in + // case of SingleLevelStorage an empty map is returned and nothing will + // happen (even not an exception). + // ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); + // } // } public SortedMap getDsInfo() { return this.dataSelection.getDsInfo(); } - public SortedMap getDfInfo() { return this.dataSelection.getDfInfo(); } @@ -101,7 +116,6 @@ public long getLength() { return this.dataSelection.getLength(); } - public boolean mustZip() { return this.dataSelection.mustZip(); } @@ -110,21 +124,22 @@ public boolean isSingleDataset() { return this.dataSelection.isSingleDataset(); } - public Set getEmptyDatasets() { return this.dataSelection.getEmptyDatasets(); } - /** - * tries to extract a list of ids from a comma separated id string. No spaces or leading 0's. Also - * accepts null. - * @param thing the name of the id list - for better error message - * @param idList a String which shoald contain long numbers seperated by commas + * tries to extract a list of ids from a comma separated id string. No + * spaces or leading 0's. Also accepts null. + * + * @param thing the name of the id list - for better error message + * @param idList a String which shoald contain long numbers seperated by + * commas * @return list of long numbers - the extracted id values * @throws BadRequestException */ - public static List getValidIds(String thing, String idList) throws BadRequestException { + public static List getValidIds(String thing, String idList) + throws BadRequestException { List result; if (idList == null) { @@ -136,7 +151,8 @@ public static List getValidIds(String thing, String idList) throws BadRequ try { result.add(Long.parseLong(id)); } catch (NumberFormatException e) { - throw new BadRequestException("The " + thing + " parameter '" + idList + "' is not a valid " + throw new BadRequestException("The " + thing + + " parameter '" + idList + "' is not a valid " + "string representation of a comma separated list of longs"); } } @@ -144,8 +160,8 @@ public static List getValidIds(String thing, String idList) throws BadRequ return result; } - - private boolean restoreIfOffline(DataInfoBase dataInfo) throws InternalException { + private boolean restoreIfOffline(DataInfoBase dataInfo) + throws InternalException { boolean maybeOffline = false; var serviceProvider = ServiceProvider.getInstance(); if (serviceProvider.getFsm().getMaybeOffline().contains(dataInfo)) { @@ -157,8 +173,7 @@ private boolean restoreIfOffline(DataInfoBase dataInfo) throws InternalException return maybeOffline; } - - public void checkOnline()throws InternalException, DataNotOnlineException { + public void checkOnline() throws InternalException, DataNotOnlineException { boolean maybeOffline = false; for (DataInfoBase dfInfo : this.getPrimaryDataInfos().values()) { @@ -172,51 +187,59 @@ public void checkOnline()throws InternalException, DataNotOnlineException { } } - public void restoreDataInfos() { var dataInfos = this.getPrimaryDataInfos().values(); - if(!dataInfos.isEmpty()) { + if (!dataInfos.isEmpty()) { for (DataInfoBase dataInfo : dataInfos) { - ServiceProvider.getInstance().getFsm().recordSuccess(dataInfo.getId()); + ServiceProvider.getInstance().getFsm() + .recordSuccess(dataInfo.getId()); } threadPool.submit(new RestoreDataInfoTask(dataInfos, this, false)); } } - public OptionalLong getFileLength() { - if (this.dataSelection.getDfInfo().isEmpty() || this.dataSelection.mustZip()) { + if (this.dataSelection.getDfInfo().isEmpty() + || this.dataSelection.mustZip()) { return OptionalLong.empty(); } return OptionalLong.of(this.dataSelection.getLength()); } - - protected boolean areDataInfosPrepared(String preparedId) throws InternalException { + protected boolean areDataInfosPrepared(String preparedId) + throws InternalException { boolean prepared = true; var serviceProvider = ServiceProvider.getInstance(); - PreparedStatus status = preparedStatusMap.computeIfAbsent(preparedId, k -> new PreparedStatus()); + PreparedStatus status = preparedStatusMap.computeIfAbsent(preparedId, + k -> new PreparedStatus()); - Collection toCheck = status.fromElement == null ? this.getPrimaryDataInfos().values() - : this.getPrimaryDataInfos().tailMap(status.fromElement).values(); + Collection toCheck = status.fromElement == null + ? this.getPrimaryDataInfos().values() + : this.getPrimaryDataInfos().tailMap(status.fromElement) + .values(); logger.debug("Will check online status of {} entries", toCheck.size()); for (DataInfoBase dataInfo : toCheck) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (this.restoreIfOffline(dataInfo)) { prepared = false; status.fromElement = dataInfo.getId(); - toCheck = this.getPrimaryDataInfos().tailMap(status.fromElement).values(); - logger.debug("Will check in background status of {} entries", toCheck.size()); - status.future = threadPool.submit(new RestoreDataInfoTask(toCheck, this, true)); + toCheck = this.getPrimaryDataInfos().tailMap(status.fromElement) + .values(); + logger.debug("Will check in background status of {} entries", + toCheck.size()); + status.future = threadPool + .submit(new RestoreDataInfoTask(toCheck, this, true)); break; } } if (prepared) { toCheck = status.fromElement == null ? Collections.emptySet() - : this.getPrimaryDataInfos().headMap(status.fromElement).values(); - logger.debug("Will check finally online status of {} entries", toCheck.size()); + : this.getPrimaryDataInfos().headMap(status.fromElement) + .values(); + logger.debug("Will check finally online status of {} entries", + toCheck.size()); for (DataInfoBase dataInfo : toCheck) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (this.restoreIfOffline(dataInfo)) { @@ -228,47 +251,52 @@ protected boolean areDataInfosPrepared(String preparedId) throws InternalExcepti return prepared; } - public void delete() throws InternalException, NotImplementedException { var serviceProvider = ServiceProvider.getInstance(); /* - * Delete the local copy directly rather than queueing it as it has - * been removed from ICAT so will not be accessible to any - * subsequent IDS calls. + * Delete the local copy directly rather than queueing it as it has been + * removed from ICAT so will not be accessible to any subsequent IDS + * calls. */ for (DataInfoBase dataInfo : this.dataSelection.getDfInfo().values()) { var dfInfo = (DatafileInfo) dataInfo; String location = dataInfo.getLocation(); try { - if ((long) serviceProvider.getIcatReader() - .search("SELECT COUNT(df) FROM Datafile df WHERE df.location LIKE '" + location.replaceAll("'", "''") + "%'") + if ((long) serviceProvider.getIcatReader().search( + "SELECT COUNT(df) FROM Datafile df WHERE df.location LIKE '" + + location.replaceAll("'", "''") + "%'") .get(0) == 0) { if (serviceProvider.getMainStorage().exists(location)) { - logger.debug("Delete physical file " + location + " from main storage"); - serviceProvider.getMainStorage().delete(location, dfInfo.getCreateId(), dfInfo.getModId()); + logger.debug("Delete physical file " + location + + " from main storage"); + serviceProvider.getMainStorage().delete(location, + dfInfo.getCreateId(), dfInfo.getModId()); } } } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage()); } catch (IOException e) { - logger.error("I/O error " + e.getMessage() + " deleting " + dfInfo); - throw new InternalException(e.getClass() + " " + e.getMessage()); + logger.error( + "I/O error " + e.getMessage() + " deleting " + dfInfo); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } this.queueDelete(); } - private class RestoreDataInfoTask implements Callable { private Collection dataInfos; private DataSelectionService dataselection; private boolean checkFailure; - public RestoreDataInfoTask(Collection dataInfos, DataSelectionService dataSelection, boolean checkFailure) { + public RestoreDataInfoTask(Collection dataInfos, + DataSelectionService dataSelection, boolean checkFailure) { this.dataInfos = dataInfos; this.dataselection = dataSelection; this.checkFailure = checkFailure; @@ -277,13 +305,14 @@ public RestoreDataInfoTask(Collection dataInfos, DataSelectionServ @Override public Void call() throws Exception { for (DataInfoBase dataInfo : dataInfos) { - if(checkFailure) - ServiceProvider.getInstance().getFsm().checkFailure(dataInfo.getId()); + if (checkFailure) + ServiceProvider.getInstance().getFsm() + .checkFailure(dataInfo.getId()); dataselection.restoreIfOffline(dataInfo); } return null; } - + } } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java index 25ff3ccb..44e622a2 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java @@ -42,7 +42,8 @@ public class DataSelectionServiceFactory { - private final static Logger logger = LoggerFactory.getLogger(DataSelectionServiceFactory.class); + private final static Logger logger = LoggerFactory + .getLogger(DataSelectionServiceFactory.class); private static DataSelectionServiceFactory instance = null; @@ -57,30 +58,41 @@ public enum Returns { DATASETS, DATASETS_AND_DATAFILES, DATAFILES } - public static DataSelectionServiceFactory getInstance() throws InternalException { + public static DataSelectionServiceFactory getInstance() + throws InternalException { if (instance == null) { var serviceProvider = ServiceProvider.getInstance(); - instance = new DataSelectionServiceFactory(serviceProvider.getPropertyHandler(), serviceProvider.getIcatReader()); + instance = new DataSelectionServiceFactory( + serviceProvider.getPropertyHandler(), + serviceProvider.getIcatReader()); } return instance; } - public static DataSelectionServiceFactory getInstanceOnlyForTesting(PropertyHandler propertyHandler, IcatReader reader) throws InternalException { + public static DataSelectionServiceFactory getInstanceOnlyForTesting( + PropertyHandler propertyHandler, IcatReader reader) + throws InternalException { if (instance == null) { instance = new DataSelectionServiceFactory(propertyHandler, reader); } return instance; } - public static DataSelectionService getService(String userSessionId, String investigationIds, String datasetIds, String datafileIds, RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + public static DataSelectionService getService(String userSessionId, + String investigationIds, String datasetIds, String datafileIds, + RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, + InsufficientPrivilegesException, NotImplementedException { - return DataSelectionServiceFactory.getInstance().getSelectionService(userSessionId, investigationIds, datasetIds, datafileIds, requestType); + return DataSelectionServiceFactory.getInstance().getSelectionService( + userSessionId, investigationIds, datasetIds, datafileIds, + requestType); } - /** - * created a DataSelectionBase object from already created data infos. That's why no infos must be loaded from ICAT. + * created a DataSelectionBase object from already created data infos. + * That's why no infos must be loaded from ICAT. + * * @param dsInfos * @param dfInfos * @param emptyDatasets @@ -89,33 +101,41 @@ public static DataSelectionService getService(String userSessionId, String inves * @return * @throws InternalException */ - public static DataSelectionService getService(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength, RequestType requestType) throws InternalException { + public static DataSelectionService getService( + SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + long fileLength, RequestType requestType) throws InternalException { - var prepared = new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength); + var prepared = new Prepared(dsInfos, dfInfos, emptyDatasets, + fileLength); return DataSelectionServiceFactory.getService(prepared, requestType); } - public static DataSelectionService getService(Prepared prepared, RequestType requestType) throws InternalException { + public static DataSelectionService getService(Prepared prepared, + RequestType requestType) throws InternalException { List dsids = new ArrayList(prepared.dsInfos.keySet()); List dfids = new ArrayList(); List invIds = new ArrayList(); var dataFileInfos = new HashMap(); - for(DataInfoBase dfInfo: prepared.dfInfos.values()) { + for (DataInfoBase dfInfo : prepared.dfInfos.values()) { dfids.add(dfInfo.getId()); dataFileInfos.put(dfInfo.getId(), dfInfo); dfids.add(dfInfo.getId()); } - for(DataInfoBase dsInfo : prepared.dsInfos.values()) { + for (DataInfoBase dsInfo : prepared.dsInfos.values()) { dsids.add(dsInfo.getId()); - invIds.add( ((DatasetInfo)dsInfo).getInvId() ); + invIds.add(((DatasetInfo) dsInfo).getInvId()); } - return DataSelectionServiceFactory.getInstance().createSelectionService(prepared.dsInfos, prepared.dfInfos, prepared.emptyDatasets, invIds, dsids, dfids, prepared.fileLength, prepared.zip, prepared.compress, requestType); + return DataSelectionServiceFactory.getInstance().createSelectionService( + prepared.dsInfos, prepared.dfInfos, prepared.emptyDatasets, + invIds, dsids, dfids, prepared.fileLength, prepared.zip, + prepared.compress, requestType); } - private DataSelectionServiceFactory(PropertyHandler propertyHandler, IcatReader reader) throws InternalException - { + private DataSelectionServiceFactory(PropertyHandler propertyHandler, + IcatReader reader) throws InternalException { logger.info("### Constructing..."); this.propertyHandler = propertyHandler; this.icat = propertyHandler.getIcatService(); @@ -128,47 +148,66 @@ private DataSelectionServiceFactory(PropertyHandler propertyHandler, IcatReader logger.info("### Constructing finished"); } - private DataSelectionService createSelectionService(SortedMap dsInfos, SortedMap dfInfos, - Set emptyDatasets, List invids, List dsids, - List dfids, long length, Boolean zip, Boolean compress, - RequestType requestType) throws InternalException { + private DataSelectionService createSelectionService( + SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + List invids, List dsids, List dfids, long length, + Boolean zip, Boolean compress, RequestType requestType) + throws InternalException { StorageUnit storageUnit = this.propertyHandler.getStorageUnit(); - if(storageUnit == null ) - return new DataSelectionServiceForSingleLevelStorage(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); + if (storageUnit == null) + return new DataSelectionServiceForSingleLevelStorage(dsInfos, + dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, + compress, requestType); else if (storageUnit == StorageUnit.DATAFILE) - return new DataSelectionServiceForStorageUnitDatafile(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); + return new DataSelectionServiceForStorageUnitDatafile(dsInfos, + dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, + compress, requestType); - else if(storageUnit == StorageUnit.DATASET) - return new DataSelectionServiceForStorageUnitDataset(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); + else if (storageUnit == StorageUnit.DATASET) + return new DataSelectionServiceForStorageUnitDataset(dsInfos, + dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, + compress, requestType); - else throw new InternalException("StorageUnit " + storageUnit + " unknown. Maybe you forgot to handle a new StorageUnit here?"); + else + throw new InternalException("StorageUnit " + storageUnit + + " unknown. Maybe you forgot to handle a new StorageUnit here?"); } - public DataSelectionService getSelectionService( String userSessionId, String investigationIds, String datasetIds, String datafileIds, RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - + public DataSelectionService getSelectionService(String userSessionId, + String investigationIds, String datasetIds, String datafileIds, + RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, + InsufficientPrivilegesException, NotImplementedException { + List dfids = getValidIds("datafileIds", datafileIds); List dsids = getValidIds("datasetIds", datasetIds); List invids = getValidIds("investigationIds", investigationIds); Returns returns = this.getReturns(requestType); - boolean dfWanted = returns == Returns.DATASETS_AND_DATAFILES || returns == Returns.DATAFILES; - boolean dsWanted = returns == Returns.DATASETS_AND_DATAFILES || returns == Returns.DATASETS; + boolean dfWanted = returns == Returns.DATASETS_AND_DATAFILES + || returns == Returns.DATAFILES; + boolean dsWanted = returns == Returns.DATASETS_AND_DATAFILES + || returns == Returns.DATASETS; Session userRestSession = restIcat.getSession(userSessionId); // by default use the user's REST ICAT session Session restSessionToUse = userRestSession; - + try { - logger.debug("useReaderForPerformance = {}", propertyHandler.getUseReaderForPerformance()); + logger.debug("useReaderForPerformance = {}", + propertyHandler.getUseReaderForPerformance()); if (propertyHandler.getUseReaderForPerformance()) { - // if this is set, use a REST session for the reader account where possible - // to improve performance due to the final database queries being simpler - restSessionToUse = restIcat.getSession(this.icatReader.getSessionId()); + // if this is set, use a REST session for the reader account + // where possible + // to improve performance due to the final database queries + // being simpler + restSessionToUse = restIcat + .getSession(this.icatReader.getSessionId()); } } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -176,20 +215,23 @@ public DataSelectionService getSelectionService( String userSessionId, String in logger.debug("dfids: {} dsids: {} invids: {}", dfids, dsids, invids); - return prepareFromIds(dfWanted, dsWanted, dfids, dsids, invids, userSessionId, restSessionToUse, userRestSession, requestType); + return prepareFromIds(dfWanted, dsWanted, dfids, dsids, invids, + userSessionId, restSessionToUse, userRestSession, requestType); } - - - private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, List dfids, List dsids, List invids, String userSessionId, Session restSessionToUse, Session userRestSession, RequestType requestType) - throws NotFoundException, InsufficientPrivilegesException, InternalException, BadRequestException { + private DataSelectionService prepareFromIds(boolean dfWanted, + boolean dsWanted, List dfids, List dsids, + List invids, String userSessionId, Session restSessionToUse, + Session userRestSession, RequestType requestType) + throws NotFoundException, InsufficientPrivilegesException, + InternalException, BadRequestException { var dsInfos = new TreeMap(); var emptyDatasets = new HashSet(); var dfInfos = new TreeMap(); long length = 0; - if (dfWanted) { //redundant ? + if (dfWanted) { // redundant ? dfInfos = new TreeMap(); } @@ -197,18 +239,22 @@ private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, for (Long dfid : dfids) { List dss = icat.search(userSessionId, - "SELECT ds FROM Dataset ds JOIN ds.datafiles df WHERE df.id = " + dfid + "SELECT ds FROM Dataset ds JOIN ds.datafiles df WHERE df.id = " + + dfid + " AND df.location IS NOT NULL INCLUDE ds.investigation.facility"); if (dss.size() == 1) { Dataset ds = (Dataset) dss.get(0); long dsid = ds.getId(); dsInfos.put(dsid, new DatasetInfo(ds)); if (dfWanted) { - Datafile df = (Datafile) icat.get(userSessionId, "Datafile", dfid); + Datafile df = (Datafile) icat.get(userSessionId, + "Datafile", dfid); length += df.getFileSize(); - String location = LocationHelper.getLocation(dfid, df.getLocation()); - dfInfos.put( df.getId(), - new DatafileInfo(dfid, df.getName(), location, df.getCreateId(), df.getModId(), dsid)); + String location = LocationHelper.getLocation(dfid, + df.getLocation()); + dfInfos.put(df.getId(), + new DatafileInfo(dfid, df.getName(), location, + df.getCreateId(), df.getModId(), dsid)); } } else { // Next line may reveal a permissions problem @@ -218,13 +264,18 @@ private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, } for (Long dsid : dsids) { - Dataset ds = (Dataset) icat.get(userSessionId, "Dataset ds INCLUDE ds.investigation.facility", dsid); + Dataset ds = (Dataset) icat.get(userSessionId, + "Dataset ds INCLUDE ds.investigation.facility", dsid); dsInfos.put(dsid, new DatasetInfo(ds)); - // dataset access for the user has been checked so the REST session for the - // reader account can be used if the IDS setting to allow this is enabled + // dataset access for the user has been checked so the REST + // session for the + // reader account can be used if the IDS setting to allow this + // is enabled String query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid + " AND df.location IS NOT NULL"; - JsonArray result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())) + JsonArray result = Json + .createReader(new ByteArrayInputStream( + restSessionToUse.search(query).getBytes())) .readArray().getJsonArray(0); if (result.getJsonNumber(2).longValueExact() == 0) { // Count 0 emptyDatasets.add(dsid); @@ -236,31 +287,38 @@ private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, for (Long invid : invids) { String query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + invid; - JsonArray result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())) + JsonArray result = Json + .createReader(new ByteArrayInputStream( + userRestSession.search(query).getBytes())) .readArray().getJsonArray(0); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUse, result); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, + userRestSession, restSessionToUse, result); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES + || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } else if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } else { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IcatException e) { - org.icatproject.icat.client.IcatException.IcatExceptionType type = e.getType(); + org.icatproject.icat.client.IcatException.IcatExceptionType type = e + .getType(); if (type == org.icatproject.icat.client.IcatException.IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == org.icatproject.icat.client.IcatException.IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } else if (type == org.icatproject.icat.client.IcatException.IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } else { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } /* @@ -272,16 +330,17 @@ private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, emptyDatasets = null; } - return this.createSelectionService(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, false, false, requestType); + return this.createSelectionService(dsInfos, dfInfos, emptyDatasets, + invids, dsids, dfids, length, false, false, requestType); } - /** * Checks to see if the investigation, dataset or datafile id list is a * valid comma separated list of longs. No spaces or leading 0's. Also * accepts null. */ - public static List getValidIds(String thing, String idList) throws BadRequestException { + public static List getValidIds(String thing, String idList) + throws BadRequestException { List result; if (idList == null) { @@ -293,7 +352,8 @@ public static List getValidIds(String thing, String idList) throws BadRequ try { result.add(Long.parseLong(id)); } catch (NumberFormatException e) { - throw new BadRequestException("The " + thing + " parameter '" + idList + "' is not a valid " + throw new BadRequestException("The " + thing + + " parameter '" + idList + "' is not a valid " + "string representation of a comma separated list of longs"); } } @@ -301,10 +361,13 @@ public static List getValidIds(String thing, String idList) throws BadRequ return result; } - private void manyDfs(Map dfInfos, long dsid, Session restSessionToUse, JsonArray result) - throws IcatException, InsufficientPrivilegesException, InternalException { - // dataset access for the user has been checked so the REST session for the - // reader account can be used if the IDS setting to allow this is enabled + private void manyDfs(Map dfInfos, long dsid, + Session restSessionToUse, JsonArray result) throws IcatException, + InsufficientPrivilegesException, InternalException { + // dataset access for the user has been checked so the REST session for + // the + // reader account can be used if the IDS setting to allow this is + // enabled long min = result.getJsonNumber(0).longValueExact(); long max = result.getJsonNumber(1).longValueExact(); long count = result.getJsonNumber(2).longValueExact(); @@ -312,33 +375,52 @@ private void manyDfs(Map dfInfos, long dsid, Session restSes if (count != 0) { if (count <= maxEntities) { String query = "SELECT df.id, df.name, df.location, df.createId, df.modId FROM Datafile df WHERE df.dataset.id = " - + dsid + " AND df.location IS NOT NULL AND df.id BETWEEN " + min + " AND " + max; - result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray(); + + dsid + + " AND df.location IS NOT NULL AND df.id BETWEEN " + + min + " AND " + max; + result = Json + .createReader(new ByteArrayInputStream( + restSessionToUse.search(query).getBytes())) + .readArray(); for (JsonValue tupV : result) { JsonArray tup = (JsonArray) tupV; long dfid = tup.getJsonNumber(0).longValueExact(); - String location = LocationHelper.getLocation(dfid, tup.getString(2, null)); + String location = LocationHelper.getLocation(dfid, + tup.getString(2, null)); dfInfos.put(dfid, - new DatafileInfo(dfid, tup.getString(1), location, tup.getString(3), tup.getString(4), dsid)); + new DatafileInfo(dfid, tup.getString(1), location, + tup.getString(3), tup.getString(4), dsid)); } } else { long half = (min + max) / 2; String query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " - + dsid + " AND df.location IS NOT NULL AND df.id BETWEEN " + min + " AND " + half; - result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray() - .getJsonArray(0); + + dsid + + " AND df.location IS NOT NULL AND df.id BETWEEN " + + min + " AND " + half; + result = Json + .createReader(new ByteArrayInputStream( + restSessionToUse.search(query).getBytes())) + .readArray().getJsonArray(0); manyDfs(dfInfos, dsid, restSessionToUse, result); - query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid - + " AND df.location IS NOT NULL AND df.id BETWEEN " + (half + 1) + " AND " + max; - result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray() - .getJsonArray(0); + query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + + dsid + + " AND df.location IS NOT NULL AND df.id BETWEEN " + + (half + 1) + " AND " + max; + result = Json + .createReader(new ByteArrayInputStream( + restSessionToUse.search(query).getBytes())) + .readArray().getJsonArray(0); manyDfs(dfInfos, dsid, restSessionToUse, result); } } } - private void manyDss(Map dsInfos, HashSet emptyDatasets, Map dfInfos, Long invid, boolean dfWanted, Session userRestSession, Session restSessionToUseForDfs, JsonArray result) - throws IcatException, InsufficientPrivilegesException, InternalException { + private void manyDss(Map dsInfos, + HashSet emptyDatasets, Map dfInfos, + Long invid, boolean dfWanted, Session userRestSession, + Session restSessionToUseForDfs, JsonArray result) + throws IcatException, InsufficientPrivilegesException, + InternalException { long min = result.getJsonNumber(0).longValueExact(); long max = result.getJsonNumber(1).longValueExact(); long count = result.getJsonNumber(2).longValueExact(); @@ -347,7 +429,10 @@ private void manyDss(Map dsInfos, HashSet emptyDataset if (count <= maxEntities) { String query = "SELECT inv.name, inv.visitId, inv.facility.id, inv.facility.name FROM Investigation inv WHERE inv.id = " + invid; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); + result = Json + .createReader(new ByteArrayInputStream( + userRestSession.search(query).getBytes())) + .readArray(); if (result.size() == 0) { return; } @@ -357,18 +442,25 @@ private void manyDss(Map dsInfos, HashSet emptyDataset long facilityId = result.getJsonNumber(2).longValueExact(); String facilityName = result.getString(3); - query = "SELECT ds.id, ds.name, ds.location FROM Dataset ds WHERE ds.investigation.id = " + invid - + " AND ds.id BETWEEN " + min + " AND " + max; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); + query = "SELECT ds.id, ds.name, ds.location FROM Dataset ds WHERE ds.investigation.id = " + + invid + " AND ds.id BETWEEN " + min + " AND " + max; + result = Json + .createReader(new ByteArrayInputStream( + userRestSession.search(query).getBytes())) + .readArray(); for (JsonValue tupV : result) { JsonArray tup = (JsonArray) tupV; long dsid = tup.getJsonNumber(0).longValueExact(); - dsInfos.put(dsid, new DatasetInfo(dsid, tup.getString(1), tup.getString(2, null), invid, invName, - visitId, facilityId, facilityName)); + dsInfos.put(dsid, + new DatasetInfo(dsid, tup.getString(1), + tup.getString(2, null), invid, invName, + visitId, facilityId, facilityName)); query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid + " AND df.location IS NOT NULL"; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())) + result = Json + .createReader(new ByteArrayInputStream( + userRestSession.search(query).getBytes())) .readArray().getJsonArray(0); if (result.getJsonNumber(2).longValueExact() == 0) { emptyDatasets.add(dsid); @@ -381,13 +473,21 @@ private void manyDss(Map dsInfos, HashSet emptyDataset long half = (min + max) / 2; String query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + invid + " AND ds.id BETWEEN " + min + " AND " + half; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUseForDfs, result); + result = Json + .createReader(new ByteArrayInputStream( + userRestSession.search(query).getBytes())) + .readArray(); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, + userRestSession, restSessionToUseForDfs, result); query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " - + invid + " AND ds.id BETWEEN " + half + 1 + " AND " + max; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray() - .getJsonArray(0); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUseForDfs, result); + + invid + " AND ds.id BETWEEN " + half + 1 + " AND " + + max; + result = Json + .createReader(new ByteArrayInputStream( + userRestSession.search(query).getBytes())) + .readArray().getJsonArray(0); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, + userRestSession, restSessionToUseForDfs, result); } } @@ -398,42 +498,60 @@ private void createRequestTypeToReturnsMapping() throws InternalException { this.requestTypeToReturnsMapping = new HashMap(); StorageUnit storageUnit = this.propertyHandler.getStorageUnit(); - this.requestTypeToReturnsMapping.put(RequestType.DELETE, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETDATAFILEIDS, Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETSIZE, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.PREPAREDATA, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.RESET, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.WRITE, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETDATA, Returns.DATASETS_AND_DATAFILES); - - if(storageUnit == null ) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATASETS); - } - else if (storageUnit == StorageUnit.DATAFILE) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.RESTORE, Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.DELETE, + Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETDATAFILEIDS, + Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETSIZE, + Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.PREPAREDATA, + Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.RESET, + Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.WRITE, + Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETDATA, + Returns.DATASETS_AND_DATAFILES); + + if (storageUnit == null) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, + Returns.DATASETS); + } else if (storageUnit == StorageUnit.DATAFILE) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, + Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.RESTORE, + Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, + Returns.DATAFILES); + } else if (storageUnit == StorageUnit.DATASET) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, + Returns.DATASETS); + this.requestTypeToReturnsMapping.put(RequestType.RESTORE, + Returns.DATASETS); + this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, + Returns.DATASETS); } - else if(storageUnit == StorageUnit.DATASET) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATASETS); - this.requestTypeToReturnsMapping.put(RequestType.RESTORE, Returns.DATASETS); - this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, Returns.DATASETS); - } - - else throw new InternalException("StorageUnit " + storageUnit + " unknown. Maybe you forgot to handle a new StorageUnit here?"); + else + throw new InternalException("StorageUnit " + storageUnit + + " unknown. Maybe you forgot to handle a new StorageUnit here?"); } - private Returns getReturns(RequestType requestType) throws NotImplementedException { + private Returns getReturns(RequestType requestType) + throws NotImplementedException { - if(this.requestTypeToReturnsMapping.containsKey(requestType)) + if (this.requestTypeToReturnsMapping.containsKey(requestType)) return this.requestTypeToReturnsMapping.get(requestType); - // is this needed here? - //if(this.propertyHandler.getStorageUnit() == null) throw new NotImplementedException("This operation is unavailable for single level storage"); - - throw new NotImplementedException("There is to mapping for RequestType." + requestType + " and StorageUnit." + this.propertyHandler.getStorageUnit() + " defined. Did you forgot to register it in createRequestTypeToReturnsMapping()?"); + // if(this.propertyHandler.getStorageUnit() == null) throw new + // NotImplementedException("This operation is unavailable for single + // level storage"); + + throw new NotImplementedException("There is to mapping for RequestType." + + requestType + " and StorageUnit." + + this.propertyHandler.getStorageUnit() + + " defined. Did you forgot to register it in createRequestTypeToReturnsMapping()?"); } } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java index b7915daa..c1a6a8a5 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java @@ -11,47 +11,49 @@ import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.models.DataInfoBase; -public class DataSelectionServiceForSingleLevelStorage extends DataSelectionService { +public class DataSelectionServiceForSingleLevelStorage + extends DataSelectionService { - protected DataSelectionServiceForSingleLevelStorage(SortedMap dsInfos, SortedMap dfInfos, - Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { - - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); - } + protected DataSelectionServiceForSingleLevelStorage( + SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, + Boolean zip, Boolean compress, RequestType requestType) { + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, + zip, compress, requestType); + } @Override public SortedMap getPrimaryDataInfos() { return new TreeMap(); } - @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { - - throw new InternalException("This operation is unavailable for single level storage"); - } + public boolean existsInMainStorage(DataInfoBase dataInfo) + throws InternalException { + throw new InternalException( + "This operation is unavailable for single level storage"); + } @Override public boolean isPrepared(String preparedId) throws InternalException { return true; } - @Override - public void queueDelete() throws NotImplementedException, InternalException { - //nothing todo for single level storage + public void queueDelete() + throws NotImplementedException, InternalException { + // nothing todo for single level storage } - @Override - public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) + throws NotImplementedException, InternalException { - throw new NotImplementedException("This operation is unavailable for single level storage"); + throw new NotImplementedException( + "This operation is unavailable for single level storage"); } - - - } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java index f744fa8f..18da82fb 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java @@ -11,44 +11,49 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.ServiceProvider; -public class DataSelectionServiceForStorageUnitDatafile extends DataSelectionService { +public class DataSelectionServiceForStorageUnitDatafile + extends DataSelectionService { - protected DataSelectionServiceForStorageUnitDatafile(SortedMap dsInfos, SortedMap dfInfos, - Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { + protected DataSelectionServiceForStorageUnitDatafile( + SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, + Boolean zip, Boolean compress, RequestType requestType) { - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, + zip, compress, requestType); } - @Override public SortedMap getPrimaryDataInfos() { return this.dataSelection.getDfInfo(); } @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { - return ServiceProvider.getInstance().getMainStorage().exists(dataInfo.getLocation()); + public boolean existsInMainStorage(DataInfoBase dataInfo) + throws InternalException { + return ServiceProvider.getInstance().getMainStorage() + .exists(dataInfo.getLocation()); } - @Override public boolean isPrepared(String preparedId) throws InternalException { return areDataInfosPrepared(preparedId); } - @Override - public void queueDelete() throws NotImplementedException, InternalException { + public void queueDelete() + throws NotImplementedException, InternalException { this.scheduleTasks(DeferredOp.DELETE); } - @Override - public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) + throws NotImplementedException, InternalException { for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); } } - + } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java index ff1b5c65..d75a4920 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java @@ -12,49 +12,56 @@ import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.services.ServiceProvider; -public class DataSelectionServiceForStorageUnitDataset extends DataSelectionService { +public class DataSelectionServiceForStorageUnitDataset + extends DataSelectionService { - protected DataSelectionServiceForStorageUnitDataset(SortedMap dsInfos, SortedMap dfInfos, - Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { + protected DataSelectionServiceForStorageUnitDataset( + SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, + Boolean zip, Boolean compress, RequestType requestType) { - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, + zip, compress, requestType); } - @Override public SortedMap getPrimaryDataInfos() { return this.dataSelection.getDsInfo(); } @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { + public boolean existsInMainStorage(DataInfoBase dataInfo) + throws InternalException { var dsInfo = (DatasetInfo) dataInfo; - if(dsInfo == null) throw new InternalException("Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); - - return this.dataSelection.getEmptyDatasets().contains(dataInfo.getId()) || ServiceProvider.getInstance().getMainStorage().exists(dsInfo); - } + if (dsInfo == null) + throw new InternalException( + "Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); + return this.dataSelection.getEmptyDatasets().contains(dataInfo.getId()) + || ServiceProvider.getInstance().getMainStorage() + .exists(dsInfo); + } @Override public boolean isPrepared(String preparedId) throws InternalException { return this.areDataInfosPrepared(preparedId); } - @Override - public void queueDelete() throws NotImplementedException, InternalException { + public void queueDelete() + throws NotImplementedException, InternalException { this.scheduleTasks(DeferredOp.WRITE); } - @Override - public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) + throws NotImplementedException, InternalException { for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); } } - } diff --git a/src/main/java/org/icatproject/ids/thread/DfArchiver.java b/src/main/java/org/icatproject/ids/thread/DfArchiver.java index 15df979f..7dc6b74a 100644 --- a/src/main/java/org/icatproject/ids/thread/DfArchiver.java +++ b/src/main/java/org/icatproject/ids/thread/DfArchiver.java @@ -17,7 +17,8 @@ * Removes datafiles from the fast storage (doesn't write them to archive storage) */ public class DfArchiver implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DfArchiver.class); + private final static Logger logger = LoggerFactory + .getLogger(DfArchiver.class); private MainStorageInterface mainStorageInterface; private FiniteStateMachine fsm; @@ -25,7 +26,9 @@ public class DfArchiver implements Runnable { private Path markerDir; private Collection locks; - public DfArchiver(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { + public DfArchiver(List dfInfos, + PropertyHandler propertyHandler, FiniteStateMachine fsm, + Collection locks) { this.dfInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -38,16 +41,19 @@ public void run() { try { for (DatafileInfo dfInfo : dfInfos) { try { - if (Files.exists(markerDir.resolve(Long.toString(dfInfo.getDfId())))) { + if (Files.exists(markerDir + .resolve(Long.toString(dfInfo.getDfId())))) { logger.error("Archive of " + dfInfo + " not carried out because a write to secondary storage operation failed previously"); } else { String dfLocation = dfInfo.getDfLocation(); - mainStorageInterface.delete(dfLocation, dfInfo.getCreateId(), dfInfo.getModId()); + mainStorageInterface.delete(dfLocation, + dfInfo.getCreateId(), dfInfo.getModId()); logger.debug("Archive of " + dfInfo + " completed"); } } catch (Exception e) { - logger.error("Archive of " + dfInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error("Archive of " + dfInfo + " failed due to " + + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dfInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DfDeleter.java b/src/main/java/org/icatproject/ids/thread/DfDeleter.java index e0b4dab6..dd4afc0c 100644 --- a/src/main/java/org/icatproject/ids/thread/DfDeleter.java +++ b/src/main/java/org/icatproject/ids/thread/DfDeleter.java @@ -16,14 +16,17 @@ */ public class DfDeleter implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DfDeleter.class); + private final static Logger logger = LoggerFactory + .getLogger(DfDeleter.class); private FiniteStateMachine fsm; private ArchiveStorageInterface archiveStorageInterface; private List dfInfos; private Collection locks; - public DfDeleter(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { + public DfDeleter(List dfInfos, + PropertyHandler propertyHandler, FiniteStateMachine fsm, + Collection locks) { this.dfInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -39,7 +42,8 @@ public void run() { archiveStorageInterface.delete(dfLocation); logger.debug("Delete of " + dfInfo + " completed"); } catch (Exception e) { - logger.error("Delete of " + dfInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error("Delete of " + dfInfo + " failed due to " + + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dfInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DfRestorer.java b/src/main/java/org/icatproject/ids/thread/DfRestorer.java index a97327b5..5c75d189 100644 --- a/src/main/java/org/icatproject/ids/thread/DfRestorer.java +++ b/src/main/java/org/icatproject/ids/thread/DfRestorer.java @@ -21,7 +21,8 @@ */ public class DfRestorer implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DfRestorer.class); + private final static Logger logger = LoggerFactory + .getLogger(DfRestorer.class); private MainStorageInterface mainStorageInterface; private ArchiveStorageInterface archiveStorageInterface; @@ -29,7 +30,9 @@ public class DfRestorer implements Runnable { private List dataFileInfos; private Collection locks; - public DfRestorer(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { + public DfRestorer(List dfInfos, + PropertyHandler propertyHandler, FiniteStateMachine fsm, + Collection locks) { this.dataFileInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -61,14 +64,14 @@ public void run() { } } - //TODO: This is additional conversion caused by the redesign :-( + // TODO: This is additional conversion caused by the redesign :-( List dfInfos = new ArrayList<>(); - for(DfInfo dfInfo : this.dataFileInfos) { + for (DfInfo dfInfo : this.dataFileInfos) { dfInfos.add(dfInfo); } - - Set failures = archiveStorageInterface.restore(mainStorageInterface, dfInfos); + Set failures = archiveStorageInterface + .restore(mainStorageInterface, dfInfos); for (DatafileInfo dfInfo : dataFileInfos) { if (failures.contains(dfInfo)) { fsm.recordFailure(dfInfo.getDfId()); @@ -81,7 +84,8 @@ public void run() { } } catch (Exception e) { for (DatafileInfo dfInfo : dataFileInfos) { - logger.error("Restore of " + dfInfo + " failed " + e.getClass() + " " + e.getMessage()); + logger.error("Restore of " + dfInfo + " failed " + e.getClass() + + " " + e.getMessage()); fsm.removeFromChanging(dfInfo); } } finally { diff --git a/src/main/java/org/icatproject/ids/thread/DfWriter.java b/src/main/java/org/icatproject/ids/thread/DfWriter.java index 09881acc..05faa301 100644 --- a/src/main/java/org/icatproject/ids/thread/DfWriter.java +++ b/src/main/java/org/icatproject/ids/thread/DfWriter.java @@ -20,7 +20,8 @@ */ public class DfWriter implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DfWriter.class); + private final static Logger logger = LoggerFactory + .getLogger(DfWriter.class); private FiniteStateMachine fsm; private MainStorageInterface mainStorageInterface; @@ -29,7 +30,8 @@ public class DfWriter implements Runnable { private List dataFileInfos; private Collection locks; - public DfWriter(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { + public DfWriter(List dfInfos, PropertyHandler propertyHandler, + FiniteStateMachine fsm, Collection locks) { this.dataFileInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -43,14 +45,17 @@ public void run() { try { for (DatafileInfo dataFileInfo : dataFileInfos) { String dfLocation = dataFileInfo.getDfLocation(); - try (InputStream is = mainStorageInterface.get(dfLocation, dataFileInfo.getCreateId(), dataFileInfo.getModId())) { + try (InputStream is = mainStorageInterface.get(dfLocation, + dataFileInfo.getCreateId(), dataFileInfo.getModId())) { archiveStorageInterface.put(is, dfLocation); - Path marker = markerDir.resolve(Long.toString(dataFileInfo.getDfId())); + Path marker = markerDir + .resolve(Long.toString(dataFileInfo.getDfId())); Files.deleteIfExists(marker); logger.debug("Removed marker " + marker); logger.debug("Write of " + dataFileInfo + " completed"); } catch (Exception e) { - logger.error("Write of " + dataFileInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error("Write of " + dataFileInfo + " failed due to " + + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dataFileInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DsArchiver.java b/src/main/java/org/icatproject/ids/thread/DsArchiver.java index 81c2c664..a017b182 100644 --- a/src/main/java/org/icatproject/ids/thread/DsArchiver.java +++ b/src/main/java/org/icatproject/ids/thread/DsArchiver.java @@ -15,7 +15,8 @@ * Removes datasets from the fast storage (doesn't write them to slow storage) */ public class DsArchiver implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DsArchiver.class); + private final static Logger logger = LoggerFactory + .getLogger(DsArchiver.class); private DatasetInfo dsInfo; private MainStorageInterface mainStorageInterface; @@ -23,7 +24,8 @@ public class DsArchiver implements Runnable { private Path markerDir; private Lock lock; - public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, Lock lock) { + public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, + FiniteStateMachine fsm, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; mainStorageInterface = propertyHandler.getMainStorage(); @@ -34,7 +36,8 @@ public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteSta @Override public void run() { try { - if (Files.exists(markerDir.resolve(Long.toString(dsInfo.getDsId())))) { + if (Files.exists( + markerDir.resolve(Long.toString(dsInfo.getDsId())))) { logger.error("Archive of " + dsInfo + " not carried out because a write to secondary storage operation failed previously"); } else { @@ -42,7 +45,8 @@ public void run() { logger.debug("Archive of " + dsInfo + " completed"); } } catch (Exception e) { - logger.error("Archive of " + dsInfo + " failed due to " + e.getMessage()); + logger.error("Archive of " + dsInfo + " failed due to " + + e.getMessage()); } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/main/java/org/icatproject/ids/thread/DsRestorer.java b/src/main/java/org/icatproject/ids/thread/DsRestorer.java index 20c7b49a..9e4e73d6 100644 --- a/src/main/java/org/icatproject/ids/thread/DsRestorer.java +++ b/src/main/java/org/icatproject/ids/thread/DsRestorer.java @@ -31,7 +31,8 @@ */ public class DsRestorer implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DsRestorer.class); + private final static Logger logger = LoggerFactory + .getLogger(DsRestorer.class); private DatasetInfo dsInfo; @@ -46,7 +47,8 @@ public class DsRestorer implements Runnable { private ZipMapperInterface zipMapper; private Lock lock; - public DsRestorer(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, IcatReader reader, Lock lock) { + public DsRestorer(DatasetInfo dsInfo, PropertyHandler propertyHandler, + FiniteStateMachine fsm, IcatReader reader, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; zipMapper = propertyHandler.getZipMapper(); @@ -71,39 +73,48 @@ public void run() { long size = 0; int n = 0; - List datafiles = ((Dataset) reader.get("Dataset INCLUDE Datafile", dsInfo.getDsId())) + List datafiles = ((Dataset) reader + .get("Dataset INCLUDE Datafile", dsInfo.getDsId())) .getDatafiles(); - Map nameToLocalMap = new HashMap<>(datafiles.size()); + Map nameToLocalMap = new HashMap<>( + datafiles.size()); for (Datafile datafile : datafiles) { if (datafile.getLocation() == null) { continue; } - nameToLocalMap.put(datafile.getName(), LocationHelper.getLocation(datafile.getId(), datafile.getLocation())); + nameToLocalMap.put(datafile.getName(), LocationHelper + .getLocation(datafile.getId(), datafile.getLocation())); size += datafile.getFileSize(); n++; } - logger.debug("Restoring dataset " + dsInfo.getInvId() + "/" + dsInfo.getDsId() + " with " + n - + " files of total size " + size); + logger.debug("Restoring dataset " + dsInfo.getInvId() + "/" + + dsInfo.getDsId() + " with " + n + " files of total size " + + size); // Get the file into the dataset cache - Path datasetCachePath = Files.createTempFile(datasetCache, null, null); + Path datasetCachePath = Files.createTempFile(datasetCache, null, + null); archiveStorageInterface.get(dsInfo, datasetCachePath); // Now split file and store it locally - logger.debug("Unpacking dataset " + dsInfo.getInvId() + "/" + dsInfo.getDsId() + " with " + n - + " files of total size " + size); - ZipInputStream zis = new ZipInputStream(Files.newInputStream(datasetCachePath)); + logger.debug("Unpacking dataset " + dsInfo.getInvId() + "/" + + dsInfo.getDsId() + " with " + n + " files of total size " + + size); + ZipInputStream zis = new ZipInputStream( + Files.newInputStream(datasetCachePath)); ZipEntry ze = zis.getNextEntry(); Set seen = new HashSet<>(); while (ze != null) { String dfName = zipMapper.getFileName(ze.getName()); if (seen.contains(dfName)) { - throw new RuntimeException("Corrupt archive for " + dsInfo + ": duplicate entry " + dfName); + throw new RuntimeException("Corrupt archive for " + dsInfo + + ": duplicate entry " + dfName); } String location = nameToLocalMap.get(dfName); if (location == null) { - throw new RuntimeException("Corrupt archive for " + dsInfo + ": spurious entry " + dfName); + throw new RuntimeException("Corrupt archive for " + dsInfo + + ": spurious entry " + dfName); } mainStorageInterface.put(zis, location); ze = zis.getNextEntry(); @@ -111,14 +122,16 @@ public void run() { } zis.close(); if (!seen.equals(nameToLocalMap.keySet())) { - throw new RuntimeException("Corrupt archive for " + dsInfo + ": missing entries"); + throw new RuntimeException( + "Corrupt archive for " + dsInfo + ": missing entries"); } Files.delete(datasetCachePath); fsm.recordSuccess(dsInfo.getDsId()); logger.debug("Restore of " + dsInfo + " completed"); } catch (Exception e) { fsm.recordFailure(dsInfo.getDsId()); - logger.error("Restore of " + dsInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error("Restore of " + dsInfo + " failed due to " + + e.getClass() + " " + e.getMessage()); try { mainStorageInterface.delete(dsInfo); } catch (IOException e2) { diff --git a/src/main/java/org/icatproject/ids/thread/DsWriter.java b/src/main/java/org/icatproject/ids/thread/DsWriter.java index e5c1843b..55e65f21 100644 --- a/src/main/java/org/icatproject/ids/thread/DsWriter.java +++ b/src/main/java/org/icatproject/ids/thread/DsWriter.java @@ -30,7 +30,8 @@ */ public class DsWriter implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DsWriter.class); + private final static Logger logger = LoggerFactory + .getLogger(DsWriter.class); private static final int BUFSIZ = 1024; private DatasetInfo dsInfo; @@ -43,7 +44,8 @@ public class DsWriter implements Runnable { private ZipMapperInterface zipMapper; private Lock lock; - public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, IcatReader reader, Lock lock) { + public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, + FiniteStateMachine fsm, IcatReader reader, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; this.zipMapper = propertyHandler.getZipMapper(); @@ -59,27 +61,35 @@ public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteState public void run() { try { if (!mainStorageInterface.exists(dsInfo)) { - logger.info("No files present in main storage for " + dsInfo + " - will delete archive"); + logger.info("No files present in main storage for " + dsInfo + + " - will delete archive"); archiveStorageInterface.delete(dsInfo); } else { - Path datasetCachePath = Files.createTempFile(datasetCache, null, null); + Path datasetCachePath = Files.createTempFile(datasetCache, null, + null); logger.debug("Creating " + datasetCachePath); - List datafiles = ((Dataset) reader.get("Dataset INCLUDE Datafile", dsInfo.getDsId())) + List datafiles = ((Dataset) reader + .get("Dataset INCLUDE Datafile", dsInfo.getDsId())) .getDatafiles(); - ZipOutputStream zos = new ZipOutputStream( - Files.newOutputStream(datasetCachePath, StandardOpenOption.CREATE)); + ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream( + datasetCachePath, StandardOpenOption.CREATE)); for (Datafile datafile : datafiles) { if (datafile.getLocation() == null) { continue; } - String location = LocationHelper.getLocation(datafile.getId(), datafile.getLocation()); + String location = LocationHelper.getLocation( + datafile.getId(), datafile.getLocation()); InputStream is = null; try { - zos.putNextEntry(new ZipEntry( - zipMapper.getFullEntryName(dsInfo, new DatafileInfo(datafile.getId(), datafile.getName(), - location, datafile.getCreateId(), datafile.getModId(), 0L)))); - is = mainStorageInterface.get(location, datafile.getCreateId(), datafile.getModId()); + zos.putNextEntry( + new ZipEntry(zipMapper.getFullEntryName(dsInfo, + new DatafileInfo(datafile.getId(), + datafile.getName(), location, + datafile.getCreateId(), + datafile.getModId(), 0L)))); + is = mainStorageInterface.get(location, + datafile.getCreateId(), datafile.getModId()); int bytesRead = 0; byte[] buffer = new byte[BUFSIZ]; while ((bytesRead = is.read(buffer)) > 0) { @@ -105,7 +115,8 @@ public void run() { logger.debug("Removed marker " + marker); logger.debug("Write of " + dsInfo + " completed"); } catch (Exception e) { - logger.error("Write of " + dsInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error("Write of " + dsInfo + " failed due to " + e.getClass() + + " " + e.getMessage()); } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/test/java/org/icatproject/ids/DataSelectionDevTest.java b/src/test/java/org/icatproject/ids/DataSelectionDevTest.java index f0ba796e..0f717ed0 100644 --- a/src/test/java/org/icatproject/ids/DataSelectionDevTest.java +++ b/src/test/java/org/icatproject/ids/DataSelectionDevTest.java @@ -31,8 +31,8 @@ /** * This test was created to fix issue #115 and was run against the Diamond - * pre-production ICAT using specifically selected usernames of users known - * to have access to a lot of data. + * pre-production ICAT using specifically selected usernames of users known to + * have access to a lot of data. * * As well as monitoring the time taken to create the DataSelection, detailed * monitoring of the eclipselink SQL logging was done to confirm that the @@ -74,21 +74,26 @@ public class DataSelectionDevTest { @Before public void setup() throws Exception { testProps = new Properties(); - testProps.load(new FileInputStream("src/test/resources/DataSelectionDevTest.properties")); + testProps.load(new FileInputStream( + "src/test/resources/DataSelectionDevTest.properties")); // set up the SOAP and REST ICAT clients icatUrl = ICATGetter.getCleanUrl(testProps.getProperty("icat.url")); icatService = ICATGetter.getService(icatUrl); restIcat = new org.icatproject.icat.client.ICAT(icatUrl); - // get session IDs for an end user and the reader user (with read-all permissions) + // get session IDs for an end user and the reader user (with read-all + // permissions) String userCredsString = testProps.getProperty("login.user"); userSessionId = TestUtils.login(icatService, userCredsString); System.out.println("userSessionId = " + userSessionId); - List readerCreds = Arrays.asList(testProps.getProperty("login.reader").trim().split("\\s+")); + List readerCreds = Arrays.asList( + testProps.getProperty("login.reader").trim().split("\\s+")); this.readerCreds = readerCreds; investigationIds = testProps.getProperty("investigation.ids"); datasetIds = testProps.getProperty("dataset.ids"); datafileIds = testProps.getProperty("datafile.ids"); - useReaderForPerformance = testProps.getProperty("useReaderForPerformance").equalsIgnoreCase("true"); + useReaderForPerformance = testProps + .getProperty("useReaderForPerformance") + .equalsIgnoreCase("true"); // set up a mocked version of the PropertyHandler setupPropertyHandler(); icatReader = new IcatReader(mockedPropertyHandler); @@ -96,24 +101,32 @@ public void setup() throws Exception { private void setupPropertyHandler() throws URISyntaxException, IcatException_Exception, IcatException { - JsonReader parser = Json.createReader(new ByteArrayInputStream(restIcat.getProperties().getBytes())); + JsonReader parser = Json.createReader( + new ByteArrayInputStream(restIcat.getProperties().getBytes())); maxEntities = parser.readObject().getInt("maxEntities"); when(mockedPropertyHandler.getMaxEntities()).thenReturn(maxEntities); when(mockedPropertyHandler.getIcatService()).thenReturn(icatService); when(mockedPropertyHandler.getRestIcat()).thenReturn(restIcat); when(mockedPropertyHandler.getReader()).thenReturn(readerCreds); - when(mockedPropertyHandler.getUseReaderForPerformance()).thenReturn(useReaderForPerformance); + when(mockedPropertyHandler.getUseReaderForPerformance()) + .thenReturn(useReaderForPerformance); } @Ignore("Test requires a specific ICAT setup to produce meaningful results. See class javadoc comment.") @Test public void testCreateDataSelection() throws Exception { long startMs = System.currentTimeMillis(); - var dataSelectionFactory = DataSelectionServiceFactory.getInstanceOnlyForTesting(mockedPropertyHandler, icatReader); - DataSelectionService dataSelectionService = dataSelectionFactory.getSelectionService(userSessionId,investigationIds, datasetIds, datafileIds, RequestType.GETSIZE); - System.out.println("Creating DataSelection took " + (System.currentTimeMillis() - startMs) + " ms"); - System.out.println("DsInfo size: " + dataSelectionService.getDsInfo().size()); - System.out.println("DfInfo size: " + dataSelectionService.getDfInfo().size()); + var dataSelectionFactory = DataSelectionServiceFactory + .getInstanceOnlyForTesting(mockedPropertyHandler, icatReader); + DataSelectionService dataSelectionService = dataSelectionFactory + .getSelectionService(userSessionId, investigationIds, + datasetIds, datafileIds, RequestType.GETSIZE); + System.out.println("Creating DataSelection took " + + (System.currentTimeMillis() - startMs) + " ms"); + System.out.println( + "DsInfo size: " + dataSelectionService.getDsInfo().size()); + System.out.println( + "DfInfo size: " + dataSelectionService.getDfInfo().size()); // there must be at least one Datafile in the DataSelection assertTrue("message", dataSelectionService.getDfInfo().size() > 0); } diff --git a/src/test/java/org/icatproject/ids/DigestTest.java b/src/test/java/org/icatproject/ids/DigestTest.java index 735e6195..fb09fd81 100644 --- a/src/test/java/org/icatproject/ids/DigestTest.java +++ b/src/test/java/org/icatproject/ids/DigestTest.java @@ -16,15 +16,18 @@ public class DigestTest { @Test public void testCheck() throws Exception { - String a = location + " " + IcatSecurity.digest(1234567L, location, key); + String a = location + " " + + IcatSecurity.digest(1234567L, location, key); - assertEquals(location, LocationHelper.getLocationFromDigest(1234567L, a, key)); + assertEquals(location, + LocationHelper.getLocationFromDigest(1234567L, a, key)); try { LocationHelper.getLocationFromDigest(1234568L, a, key); fail(); } catch (InsufficientPrivilegesException e) { - assertTrue(e.getMessage().contains("does not contain a valid hash")); + assertTrue( + e.getMessage().contains("does not contain a valid hash")); } try { diff --git a/src/test/java/org/icatproject/ids/PreparePackingTest.java b/src/test/java/org/icatproject/ids/PreparePackingTest.java index 6b2a857e..531bca86 100644 --- a/src/test/java/org/icatproject/ids/PreparePackingTest.java +++ b/src/test/java/org/icatproject/ids/PreparePackingTest.java @@ -37,21 +37,24 @@ public void packAndUnpack() throws Exception { long dsid2 = 18L; long invId = 15L; long facilityId = 45L; - dfInfos.put(5L, new DatafileInfo(5L, "dfName", "dfLocation", "createId", "modId", dsid1)); + dfInfos.put(5L, new DatafileInfo(5L, "dfName", "dfLocation", "createId", + "modId", dsid1)); - dfInfos.put(51L, new DatafileInfo(51L, "dfName2", null, "createId", "modId", dsid1)); + dfInfos.put(51L, new DatafileInfo(51L, "dfName2", null, "createId", + "modId", dsid1)); - dsInfos.put(dsid1, new DatasetInfo(dsid1, "dsName", "dsLocation", invId, "invName", - "visitId", facilityId, "facilityName")); + dsInfos.put(dsid1, new DatasetInfo(dsid1, "dsName", "dsLocation", invId, + "invName", "visitId", facilityId, "facilityName")); - dsInfos.put(dsid2, new DatasetInfo(dsid2, "dsName2", null, invId, "invName", "visitId", - facilityId, "facilityName")); + dsInfos.put(dsid2, new DatasetInfo(dsid2, "dsName2", null, invId, + "invName", "visitId", facilityId, "facilityName")); emptyDatasets.add(dsid2); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (OutputStream stream = new BufferedOutputStream(baos)) { - Prepared.pack(stream, zip, compress, dsInfos, dfInfos, emptyDatasets, 51); + Prepared.pack(stream, zip, compress, dsInfos, dfInfos, + emptyDatasets, 51); } System.out.println(baos.toString()); InputStream stream = new ByteArrayInputStream(baos.toByteArray()); diff --git a/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java b/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java index e6960866..aec0b0ee 100644 --- a/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java +++ b/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java @@ -49,7 +49,8 @@ public void t3() throws Exception { os.flush(); } os.close(); - assertEquals("m is " + m, "ABCDEFGHIJKLMONPQRSTUVWXYZ", baos.toString()); + assertEquals("m is " + m, "ABCDEFGHIJKLMONPQRSTUVWXYZ", + baos.toString()); } } @@ -68,7 +69,6 @@ public void t4() throws Exception { } } - @Test public void t5() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); diff --git a/src/test/java/org/icatproject/ids/TestICATGetter.java b/src/test/java/org/icatproject/ids/TestICATGetter.java index 8bfa99c6..3cb52127 100644 --- a/src/test/java/org/icatproject/ids/TestICATGetter.java +++ b/src/test/java/org/icatproject/ids/TestICATGetter.java @@ -10,17 +10,20 @@ public class TestICATGetter { @Test public void testGFUrl() throws Exception { - assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost/ICATService/ICAT?wsdl")); + assertEquals("http://localhost", ICATGetter + .getCleanUrl("http://localhost/ICATService/ICAT?wsdl")); } @Test public void testSlash() throws Exception { - assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost/")); + assertEquals("http://localhost", + ICATGetter.getCleanUrl("http://localhost/")); } @Test public void testClean() throws Exception { - assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost")); + assertEquals("http://localhost", + ICATGetter.getCleanUrl("http://localhost")); } @Test diff --git a/src/test/java/org/icatproject/ids/TestUtils.java b/src/test/java/org/icatproject/ids/TestUtils.java index cbd62530..7b313f42 100644 --- a/src/test/java/org/icatproject/ids/TestUtils.java +++ b/src/test/java/org/icatproject/ids/TestUtils.java @@ -12,15 +12,16 @@ public class TestUtils { /** * Do an ICAT login and get the session ID using a list of credentials of - * the format (as found in the properties files): - * "db username READER password READERpass" + * the format (as found in the properties files): "db username READER + * password READERpass" * * @param icatService the ICAT to log in to * @param credsString a String of credentials in the format described above * @return an ICAT session ID * @throws IcatException_Exception if the login fails */ - public static String login(ICAT icatService, String credsString) throws IcatException_Exception { + public static String login(ICAT icatService, String credsString) + throws IcatException_Exception { List creds = Arrays.asList(credsString.trim().split("\\s+")); Credentials credentials = new Credentials(); List entries = credentials.getEntry(); diff --git a/src/test/java/org/icatproject/ids/TestValueContainer.java b/src/test/java/org/icatproject/ids/TestValueContainer.java index bc3daf98..9dca1278 100644 --- a/src/test/java/org/icatproject/ids/TestValueContainer.java +++ b/src/test/java/org/icatproject/ids/TestValueContainer.java @@ -15,12 +15,11 @@ import org.icatproject.ids.helpers.ValueContainer; import org.junit.Test; - public class TestValueContainer { @Test public void testInvalidValueContainer() throws Exception { - + var vc = ValueContainer.getInvalid(); assertFalse(vc.isVoid()); assertTrue(vc.getType() == ValueContainerType.INVALID); @@ -28,7 +27,7 @@ public void testInvalidValueContainer() throws Exception { @Test public void testVoidValueContainer() throws Exception { - + var vc = ValueContainer.getVoid(); assertTrue(vc.isVoid()); assertTrue(vc.getType() == ValueContainerType.VOID); @@ -37,7 +36,7 @@ public void testVoidValueContainer() throws Exception { @Test(expected = InternalException.class) public void testIntValueContainer() throws Exception { - + var vc = new ValueContainer(1); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -50,7 +49,7 @@ public void testIntValueContainer() throws Exception { @Test(expected = InternalException.class) public void testLongValueContainer() throws Exception { - + var vc = new ValueContainer(1L); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -63,7 +62,7 @@ public void testLongValueContainer() throws Exception { @Test(expected = InternalException.class) public void testBoolValueContainer() throws Exception { - + var vc = new ValueContainer(true); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -76,7 +75,7 @@ public void testBoolValueContainer() throws Exception { @Test(expected = InternalException.class) public void testStringValueContainer() throws Exception { - + var vc = new ValueContainer("test"); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -89,7 +88,7 @@ public void testStringValueContainer() throws Exception { @Test(expected = InternalException.class) public void testInputStreamValueContainer() throws Exception { - + String s = "test InputStream"; var inputStream = new ByteArrayInputStream(s.getBytes()); var vc = new ValueContainer(inputStream); @@ -98,8 +97,9 @@ public void testInputStreamValueContainer() throws Exception { assertFalse(vc.isNull()); assertEquals(vc.getInputStream(), inputStream); - - var ISReader = new InputStreamReader(vc.getInputStream(), StandardCharsets.UTF_8); + + var ISReader = new InputStreamReader(vc.getInputStream(), + StandardCharsets.UTF_8); var BReader = new BufferedReader(ISReader); String textFromStream = BReader.lines().collect(Collectors.joining()); BReader.close(); diff --git a/src/test/java/org/icatproject/ids/TestZipping.java b/src/test/java/org/icatproject/ids/TestZipping.java index 56dce770..03f1ba91 100644 --- a/src/test/java/org/icatproject/ids/TestZipping.java +++ b/src/test/java/org/icatproject/ids/TestZipping.java @@ -19,12 +19,14 @@ public class TestZipping { @Test public void testDuplicates() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos)); + ZipOutputStream zos = new ZipOutputStream( + new BufferedOutputStream(baos)); // zos.setLevel(0); List in = new ArrayList<>(); - for (String entryName : Arrays.asList("abcd/qa", "abcd/qw", "abcd/qw", "abcd/qb", "abcd/qc", "abcd/qw")) { + for (String entryName : Arrays.asList("abcd/qa", "abcd/qw", "abcd/qw", + "abcd/qb", "abcd/qc", "abcd/qw")) { try { zos.putNextEntry(new ZipEntry(entryName)); @@ -42,7 +44,8 @@ public void testDuplicates() throws Exception { assertEquals(4, in.size()); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream( + baos.toByteArray()); ZipInputStream zis = new ZipInputStream(bais); int off = 0; diff --git a/src/test/java/org/icatproject/ids/TidierTest.java b/src/test/java/org/icatproject/ids/TidierTest.java index 8f2762c6..80c6f731 100644 --- a/src/test/java/org/icatproject/ids/TidierTest.java +++ b/src/test/java/org/icatproject/ids/TidierTest.java @@ -85,13 +85,16 @@ public void testCleanPreparedDir() throws Exception { @Test public void testAddStringConstraint() throws Exception { StringBuilder sb1 = new StringBuilder(); - Tidier.addStringConstraint(sb1, "df.location", "/path/to/normal/file", false); + Tidier.addStringConstraint(sb1, "df.location", "/path/to/normal/file", + false); assertEquals(" df.location = '/path/to/normal/file'", sb1.toString()); /* Fix error where a file path contains an apostrophe */ StringBuilder sb2 = new StringBuilder(); - Tidier.addStringConstraint(sb2, "df.location", "/path/to/Person's Files/myscript.py", false); - assertEquals(" df.location = '/path/to/Person''s Files/myscript.py'", sb2.toString()); + Tidier.addStringConstraint(sb2, "df.location", + "/path/to/Person's Files/myscript.py", false); + assertEquals(" df.location = '/path/to/Person''s Files/myscript.py'", + sb2.toString()); } @Test diff --git a/src/test/java/org/icatproject/ids/TransmittingTest.java b/src/test/java/org/icatproject/ids/TransmittingTest.java index 7e2e986c..251a126d 100644 --- a/src/test/java/org/icatproject/ids/TransmittingTest.java +++ b/src/test/java/org/icatproject/ids/TransmittingTest.java @@ -40,10 +40,10 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; - /** - * This test was created when the building of the transmission body was made more comon and generic. - * It should ensure the right structure of the containing json. + * This test was created when the building of the transmission body was made + * more comon and generic. It should ensure the right structure of the + * containing json. */ @RunWith(MockitoJUnitRunner.class) public class TransmittingTest { @@ -58,7 +58,8 @@ public class TransmittingTest { private LockManager mockedLockManager; @Mock private IcatReader mockedReader; - @Mock ArchiveStorageInterface mockedArchiveStorage; + @Mock + ArchiveStorageInterface mockedArchiveStorage; @Mock private ICAT mockedIcat; @@ -85,81 +86,101 @@ private void setup() } @Test - public void testTransmitterBodyForArchiveRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForArchiveRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new ArchiveHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new ArchiveHandler(ip, sessionId, investigationIds, + datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetSizeRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetSizeRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new GetSizeHandler(ip, preparedId, null, null, null, null); + var handler = new GetSizeHandler(ip, preparedId, null, null, null, + null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetSizeHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); + handler = new GetSizeHandler(ip, null, sessionId, investigationIds, + datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetSizeFastRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetSizeFastRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new GetSizeHandlerForFastProcessing(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new GetSizeHandlerForFastProcessing(ip, sessionId, + investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForDeleteRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForDeleteRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new DeleteHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new DeleteHandler(ip, sessionId, investigationIds, + datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetDataFileIdsRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetDataFileIdsRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new GetDataFileIdsHandler(ip, preparedId, null, null, null, null); + var handler = new GetDataFileIdsHandler(ip, preparedId, null, null, + null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetDataFileIdsHandler("192.168.17.1", null, sessionId, investigationIds, datasetIds, datafileIds); + handler = new GetDataFileIdsHandler("192.168.17.1", null, sessionId, + investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetDataRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetDataRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new GetDataHandler(ip, preparedId, null, null, null, null, false, false, "", ""); + var handler = new GetDataHandler(ip, preparedId, null, null, null, null, + false, false, "", ""); String body = handler.provideTransmissionBody(); - assertEquals("{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"transferId\":-1}", body); + assertEquals( + "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"transferId\":-1}", + body); - handler = new GetDataHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds, false, false, "", ""); + handler = new GetDataHandler(ip, null, sessionId, investigationIds, + datasetIds, datafileIds, false, false, "", ""); body = handler.provideTransmissionBody(); - assertEquals("{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"transferId\":-1}", body); + assertEquals( + "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"transferId\":-1}", + body); } @Test - public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() + throws Exception { + setup(); var handler = new GetIcatUrlHandler(ip); @@ -168,8 +189,9 @@ public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() throws Exceptio } @Test - public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() + throws Exception { + setup(); var handler = new GetServiceStatusHandler(ip, sessionId); @@ -178,32 +200,39 @@ public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() throws Ex } @Test - public void testTransmitterBodyForGetStatusRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetStatusRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new GetStatusHandler(ip, preparedId, null, null, null, null); + var handler = new GetStatusHandler(ip, preparedId, null, null, null, + null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetStatusHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); + handler = new GetStatusHandler(ip, null, sessionId, investigationIds, + datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForIsPreparedRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForIsPreparedRequest_shouldBeOk() + throws Exception { + setup(); var handler = new IsPreparedHandler(ip, preparedId); String body = handler.provideTransmissionBody(); - assertEquals("{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}", body); + assertEquals( + "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}", + body); } @Test - public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() + throws Exception { + setup(); var handler = new IsReadOnlyHandler(ip); @@ -212,8 +241,9 @@ public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() throws Exceptio } @Test - public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() + throws Exception { + setup(); var handler = new IsTwoLevelHandler(ip); @@ -222,56 +252,69 @@ public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() throws Exceptio } @Test - public void testTransmitterBodyForPrepareDataRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForPrepareDataRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new PrepareDataHandler(ip, sessionId, investigationIds, datasetIds, datafileIds, false, false); + var handler = new PrepareDataHandler(ip, sessionId, investigationIds, + datasetIds, datafileIds, false, false); String body = handler.provideTransmissionBody(); - assertEquals("{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"preparedId\":\"\"}", body); + assertEquals( + "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"preparedId\":\"\"}", + body); } @Test public void testTransmitterBodyForPutRequest_shouldBeOk() throws Exception { - + setup(); - var handler = new PutHandler(ip, sessionId, new ByteArrayInputStream( "".getBytes() ), "someName", datafileIds , datasetIds, "someDescription", "someDOI", "simeCreateTimeString", "someModTimeString", false, false ); + var handler = new PutHandler(ip, sessionId, + new ByteArrayInputStream("".getBytes()), "someName", + datafileIds, datasetIds, "someDescription", "someDOI", + "simeCreateTimeString", "someModTimeString", false, false); String body = handler.provideTransmissionBody(); assertEquals("{\"userName\":\"TestUser\",\"datafileId\":-1}", body); } @Test - public void testTransmitterBodyForResetRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForResetRequest_shouldBeOk() + throws Exception { + setup(); var handler = new ResetHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new ResetHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); + handler = new ResetHandler(ip, null, sessionId, investigationIds, + datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForRestoreRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForRestoreRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new RestoreHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new RestoreHandler(ip, sessionId, investigationIds, + datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForWriteRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForWriteRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new WriteHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new WriteHandler(ip, sessionId, investigationIds, + datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } diff --git a/src/test/java/org/icatproject/ids/ValidaterTest.java b/src/test/java/org/icatproject/ids/ValidaterTest.java index 6dda09ef..87536004 100644 --- a/src/test/java/org/icatproject/ids/ValidaterTest.java +++ b/src/test/java/org/icatproject/ids/ValidaterTest.java @@ -32,4 +32,3 @@ private void testValidUUID(boolean b, String id) { } } - diff --git a/src/test/java/org/icatproject/ids/integration/BaseTest.java b/src/test/java/org/icatproject/ids/integration/BaseTest.java index 4f0a23d4..e33e0d46 100644 --- a/src/test/java/org/icatproject/ids/integration/BaseTest.java +++ b/src/test/java/org/icatproject/ids/integration/BaseTest.java @@ -60,13 +60,15 @@ public class BaseTest { public class DeleteVisitor extends SimpleFileVisitor { @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) + throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override - public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { + public FileVisitResult postVisitDirectory(Path dir, IOException e) + throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; @@ -80,15 +82,21 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOExce private static long timestamp = System.currentTimeMillis(); - protected Path getDirOnFastStorage(Long dsId) throws IcatException_Exception { - Dataset icatDs = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Investigation", dsId); - return setup.getStorageDir().resolve(Long.toString(icatDs.getInvestigation().getId())) + protected Path getDirOnFastStorage(Long dsId) + throws IcatException_Exception { + Dataset icatDs = (Dataset) icatWS.get(sessionId, + "Dataset INCLUDE Investigation", dsId); + return setup.getStorageDir() + .resolve(Long.toString(icatDs.getInvestigation().getId())) .resolve(Long.toString(icatDs.getId())); } - protected Path getFileOnArchiveStorage(Long dsId) throws IcatException_Exception { - Dataset icatDs = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Investigation", dsId); - return setup.getStorageArchiveDir().resolve(Long.toString(icatDs.getInvestigation().getId())) + protected Path getFileOnArchiveStorage(Long dsId) + throws IcatException_Exception { + Dataset icatDs = (Dataset) icatWS.get(sessionId, + "Dataset INCLUDE Investigation", dsId); + return setup.getStorageArchiveDir() + .resolve(Long.toString(icatDs.getInvestigation().getId())) .resolve(Long.toString(icatDs.getId())); } @@ -117,7 +125,8 @@ public void before() throws Exception { testingClient = new TestingClient(setup.getIdsUrl()); sessionId = setup.getRootSessionId(); waitForIds(); - populateStorage(setup.isTwoLevel(), setup.getStorageUnit(), setup.getKey()); + populateStorage(setup.isTwoLevel(), setup.getStorageUnit(), + setup.getKey()); } protected void checkAbsent(Path file) { @@ -158,7 +167,8 @@ protected void logTime(String msg) { long now = System.currentTimeMillis(); if (msg != null) { if (time != 0) { - System.out.println(msg + " took " + (now - time) / 1000. + "s."); + System.out + .println(msg + " took " + (now - time) / 1000. + "s."); } else { System.out.println(msg); } @@ -166,7 +176,8 @@ protected void logTime(String msg) { time = now; } - protected void checkZipFile(Path file, List ids, int compressedSize) throws IOException { + protected void checkZipFile(Path file, List ids, int compressedSize) + throws IOException { checkZipStream(Files.newInputStream(file), ids, compressedSize, 0); } @@ -205,8 +216,9 @@ protected byte[] getOutput(InputStream stream) throws IOException { } } - private void populateStorage(boolean twoLevel, String storageUnit, String key) - throws IOException, IcatException_Exception, NoSuchAlgorithmException { + private void populateStorage(boolean twoLevel, String storageUnit, + String key) throws IOException, IcatException_Exception, + NoSuchAlgorithmException { clearStorage(); long timestamp = System.currentTimeMillis(); @@ -233,7 +245,8 @@ private void populateStorage(boolean twoLevel, String storageUnit, String key) supportedDatafileFormat.setFacility(fac); supportedDatafileFormat.setName("test_format"); supportedDatafileFormat.setVersion("42.0.0"); - supportedDatafileFormat.setId(icatWS.create(sessionId, supportedDatafileFormat)); + supportedDatafileFormat + .setId(icatWS.create(sessionId, supportedDatafileFormat)); InvestigationType invType = new InvestigationType(); invType.setName("Not null"); @@ -277,25 +290,33 @@ private void populateStorage(boolean twoLevel, String storageUnit, String key) df1.setName("a/df1_" + timestamp); df1.setLocation(ds1Loc + UUID.randomUUID()); df1.setDataset(ds1); - writeToFile(df1, "df1 test content very compressible very compressible", key); + writeToFile(df1, + "df1 test content very compressible very compressible", + key); Datafile df2 = new Datafile(); df2.setName("df2_" + timestamp); df2.setLocation(ds1Loc + UUID.randomUUID()); df2.setDataset(ds1); - writeToFile(df2, "df2 test content very compressible very compressible", key); + writeToFile(df2, + "df2 test content very compressible very compressible", + key); Datafile df3 = new Datafile(); df3.setName("df3_" + timestamp); df3.setLocation(ds2Loc + UUID.randomUUID()); df3.setDataset(ds2); - writeToFile(df3, "df3 test content very compressible very compressible", key); + writeToFile(df3, + "df3 test content very compressible very compressible", + key); Datafile df4 = new Datafile(); df4.setName("df4_" + timestamp); df4.setLocation(ds2Loc + "Person's file"); df4.setDataset(ds2); - writeToFile(df4, "df4 test content very compressible very compressible", key); + writeToFile(df4, + "df4 test content very compressible very compressible", + key); datasetIds.add(ds1.getId()); datasetIds.add(ds2.getId()); @@ -311,14 +332,16 @@ private void populateStorage(boolean twoLevel, String storageUnit, String key) moveDatasetToArchive(storageUnit, ds2, ds2Loc, fac, inv, key); } - newFileLocation = setup.getUpdownDir().resolve("new_file_" + timestamp); + newFileLocation = setup.getUpdownDir() + .resolve("new_file_" + timestamp); Files.createDirectories(newFileLocation.getParent()); byte[] bytes = "new_file test content".getBytes(); OutputStream out = Files.newOutputStream(newFileLocation); out.write(bytes); out.close(); } catch (IllegalAccessError e) { - System.err.println("Could not prepare ICAT db for testing: " + e.getMessage()); + System.err.println( + "Could not prepare ICAT db for testing: " + e.getMessage()); e.printStackTrace(); throw e; } @@ -331,8 +354,8 @@ private void populateStorage(boolean twoLevel, String storageUnit, String key) protected Map ids = new HashMap<>(); protected Map paths = new HashMap<>(); - protected void checkZipStream(InputStream stream, List datafileIdsIn, long compressedSize, int numLeft) - throws IOException { + protected void checkZipStream(InputStream stream, List datafileIdsIn, + long compressedSize, int numLeft) throws IOException { ZipInputStream zis = new ZipInputStream(stream); ZipEntry ze = zis.getNextEntry(); List idsNeeded = new ArrayList<>(datafileIdsIn); @@ -361,7 +384,8 @@ protected void checkStream(InputStream stream, long id) throws IOException { boolean found = false; for (Entry e : ids.entrySet()) { if (id == e.getValue()) { - assertEquals(contents.get(e.getKey()), new String(getOutput(stream))); + assertEquals(contents.get(e.getKey()), + new String(getOutput(stream))); found = true; break; } @@ -370,7 +394,8 @@ protected void checkStream(InputStream stream, long id) throws IOException { } protected void writeToFile(Datafile df, String content, String key) - throws IOException, IcatException_Exception, NoSuchAlgorithmException { + throws IOException, IcatException_Exception, + NoSuchAlgorithmException { Path path = setup.getStorageDir().resolve(df.getLocation()); Files.createDirectories(path.getParent()); byte[] bytes = content.getBytes(); @@ -396,16 +421,16 @@ protected void writeToFile(Datafile df, String content, String key) Dataset ds = df.getDataset(); Investigation inv = ds.getInvestigation(); Facility fac = inv.getFacility(); - paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + URLEncoder.encode(inv.getVisitId(), "UTF-8") - + "/" + ds.getName() + "/" + df.getName(), df.getLocation()); + paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" + + ds.getName() + "/" + df.getName(), df.getLocation()); } - private static final char[] HEX_CHARS = { - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' - }; + private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', + '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; - private String digest(Long id, String location, String key) throws NoSuchAlgorithmException { + private String digest(Long id, String location, String key) + throws NoSuchAlgorithmException { byte[] pattern = (id + location + key).getBytes(); MessageDigest digest = null; digest = MessageDigest.getInstance("SHA-256"); @@ -420,16 +445,19 @@ private String digest(Long id, String location, String key) throws NoSuchAlgorit return new String(hexChars); } - private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, Facility fac, Investigation inv, - String key) throws IOException, IcatException_Exception { - ds = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Datafile", ds.getId()); + private void moveDatasetToArchive(String storageUnit, Dataset ds, + String dsLoc, Facility fac, Investigation inv, String key) + throws IOException, IcatException_Exception { + ds = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Datafile", + ds.getId()); Path top = setup.getStorageDir(); if (storageUnit.equals("DATASET")) { Path zipFile = setup.getStorageArchiveDir().resolve(dsLoc); Files.createDirectories(zipFile.getParent()); - ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile)); + ZipOutputStream zos = new ZipOutputStream( + Files.newOutputStream(zipFile)); zos.setLevel(0); for (Datafile df : ds.getDatafiles()) { @@ -437,12 +465,15 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, if (key == null) { file = top.resolve(df.getLocation()); } else { - file = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); + file = top.resolve(getLocationFromDigest(df.getId(), + df.getLocation())); } InputStream fis = Files.newInputStream(file); - zos.putNextEntry(new ZipEntry("ids/" + fac.getName() + "/" + inv.getName() + "/" - + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" + ds.getName() + "/" + df.getName())); + zos.putNextEntry(new ZipEntry( + "ids/" + fac.getName() + "/" + inv.getName() + "/" + + URLEncoder.encode(inv.getVisitId(), "UTF-8") + + "/" + ds.getName() + "/" + df.getName())); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { @@ -461,9 +492,11 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, if (key == null) { p = top.resolve(df.getLocation()); } else { - p = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); + p = top.resolve(getLocationFromDigest(df.getId(), + df.getLocation())); } - Files.move(p, archive.resolve(p.getFileName()), StandardCopyOption.REPLACE_EXISTING); + Files.move(p, archive.resolve(p.getFileName()), + StandardCopyOption.REPLACE_EXISTING); } } for (Datafile df : ds.getDatafiles()) { @@ -471,7 +504,8 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, if (key == null) { file = top.resolve(df.getLocation()); } else { - file = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); + file = top.resolve( + getLocationFromDigest(df.getId(), df.getLocation())); } Files.deleteIfExists(file); Path parent = file.getParent(); @@ -508,8 +542,10 @@ protected void raceTest() throws Exception { } logTime("File created"); - Investigation inv = (Investigation) icatWS.search(sessionId, "Investigation").get(0); - DatasetType dst = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); + Investigation inv = (Investigation) icatWS + .search(sessionId, "Investigation").get(0); + DatasetType dst = (DatasetType) icatWS.search(sessionId, "DatasetType") + .get(0); Dataset ds = new Dataset(); ds.setName("Big ds"); @@ -519,17 +555,21 @@ protected void raceTest() throws Exception { ds.setId(dsid); for (int i = 0; i < 30; i++) { - testingClient.put(sessionId, Files.newInputStream(path), "uploaded_file" + i, dsid, - supportedDatafileFormat.getId(), "A rather splendid datafile", 201); + testingClient.put(sessionId, Files.newInputStream(path), + "uploaded_file" + i, dsid, supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); } - testingClient.archive(sessionId, new DataSelection().addDataset(dsid), 204); + testingClient.archive(sessionId, new DataSelection().addDataset(dsid), + 204); logTime("Put and archive calls"); waitForIds(300); logTime("Archive complete"); - testingClient.restore(sessionId, new DataSelection().addDataset(dsid), 204); - while (testingClient.getStatus(sessionId, new DataSelection().addDataset(dsid), 200) != Status.ONLINE) { + testingClient.restore(sessionId, new DataSelection().addDataset(dsid), + 204); + while (testingClient.getStatus(sessionId, + new DataSelection().addDataset(dsid), 200) != Status.ONLINE) { Thread.sleep(1000); } logTime("Marked online"); @@ -538,7 +578,8 @@ protected void raceTest() throws Exception { assertTrue(stat.getOpItems().toString(), stat.getOpItems().isEmpty()); - testingClient.delete(sessionId, new DataSelection().addDataset(dsid), 204); + testingClient.delete(sessionId, new DataSelection().addDataset(dsid), + 204); icatWS.delete(sessionId, ds); waitForIds(300); @@ -551,16 +592,20 @@ protected void raceTest() throws Exception { public void getDatafileIdsTest() throws Exception { List ids = testingClient.getDatafileIds(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafile(datafileIds.get(0)), 200); + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), + 200); assertEquals(2, ids.size()); assertTrue(ids.contains(datafileIds.get(0))); assertTrue(ids.contains(datafileIds.get(1))); - ids = testingClient.getDatafileIds(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 200); + ids = testingClient.getDatafileIds(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), 200); assertEquals(1, ids.size()); assertTrue(ids.contains(datafileIds.get(0))); - ids = testingClient.getDatafileIds(sessionId, new DataSelection().addInvestigation(investigationId), 200); + ids = testingClient.getDatafileIds(sessionId, + new DataSelection().addInvestigation(investigationId), 200); assertEquals(4, ids.size()); for (Long id : datafileIds) { assertTrue(ids.contains(id)); @@ -571,24 +616,31 @@ public void getDatafileIdsTest() throws Exception { public void bigDataSelectionTest() throws Exception { String icatUrl = testingClient.getIcatUrl(200).toExternalForm(); ICAT restIcat = new org.icatproject.icat.client.ICAT(icatUrl); - try (JsonReader parser = Json.createReader(new ByteArrayInputStream(restIcat.getProperties().getBytes()))) { - assertEquals("maxEntities must have a fixed value in the icat.server for test to be useful", 20, - parser.readObject().getInt("maxEntities")); + try (JsonReader parser = Json.createReader(new ByteArrayInputStream( + restIcat.getProperties().getBytes()))) { + assertEquals( + "maxEntities must have a fixed value in the icat.server for test to be useful", + 20, parser.readObject().getInt("maxEntities")); } - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); List idList = new ArrayList<>(); for (int i = 0; i < 45; i++) { - Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp + i, datasetIds.get(0), supportedDatafileFormat.getId(), + Long dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp + i, datasetIds.get(0), + supportedDatafileFormat.getId(), "A rather splendid datafile", 201); idList.add(dfid); } waitForIds(); List idList2 = testingClient.getDatafileIds(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafile(datafileIds.get(0)), 200); + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), + 200); assertEquals(47, idList2.size()); for (Long id : idList) { assertTrue(idList2.contains(id)); @@ -605,69 +657,96 @@ public void bigDataSelectionTest() throws Exception { assertTrue(idList2.contains(idList.get(i))); } - idList2 = testingClient.getDatafileIds(sessionId, new DataSelection().addInvestigation(investigationId), 200); + idList2 = testingClient.getDatafileIds(sessionId, + new DataSelection().addInvestigation(investigationId), 200); assertEquals(49, idList2.size()); } public void cloningTest() throws Exception { - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); - assertEquals(104, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 200)); + assertEquals(104, testingClient.getSize(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 200)); Session s = icat.getSession(sessionId); Map m = new HashMap<>(); m.put("name", "newOne"); long dfid = datafileIds.get(0); long ndfid = s.cloneEntity("Datafile", dfid, m); - Datafile df = (Datafile) icatWS.get(sessionId, "Datafile df INCLUDE df.dataset.investigation.facility", ndfid); + Datafile df = (Datafile) icatWS.get(sessionId, + "Datafile df INCLUDE df.dataset.investigation.facility", ndfid); Dataset ds = df.getDataset(); Investigation inv = ds.getInvestigation(); Facility fac = inv.getFacility(); - paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + URLEncoder.encode(inv.getVisitId(), "UTF-8") - + "/" + ds.getName() + "/" + df.getName(), df.getLocation()); + paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" + + ds.getName() + "/" + df.getName(), df.getLocation()); crcs.put(df.getLocation(), df.getChecksum()); fsizes.put(df.getLocation(), df.getFileSize()); - contents.put(df.getLocation(), "df1 test content very compressible very compressible"); + contents.put(df.getLocation(), + "df1 test content very compressible very compressible"); ids.put(df.getLocation(), df.getId()); - assertEquals(156, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 200)); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, 200)) { + assertEquals(156, testingClient.getSize(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 200)); + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, + 200)) { List dfids = new ArrayList<>(datafileIds.subList(0, 2)); dfids.add(ndfid); checkZipStream(stream, dfids, 57, 0); } long dsid = datasetIds.get(0); assertEquals(2, getDirOnFastStorage(dsid).toFile().list().length); - assertEquals("[3]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); - assertEquals(2, getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); - assertEquals("[2]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(1)), 204); - assertEquals(1, getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); - assertEquals("[1]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); - testingClient.delete(sessionId, new DataSelection().addDatafile(ndfid), 204); + assertEquals("[3]", s.search( + "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + + dsid)); + testingClient.delete(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), 204); + assertEquals(2, + getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); + assertEquals("[2]", s.search( + "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + + dsid)); + testingClient.delete(sessionId, + new DataSelection().addDatafile(datafileIds.get(1)), 204); + assertEquals(1, + getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); + assertEquals("[1]", s.search( + "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + + dsid)); + testingClient.delete(sessionId, new DataSelection().addDatafile(ndfid), + 204); assertFalse(getDirOnFastStorage(datasetIds.get(0)).toFile().exists()); - assertEquals("[0]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); + assertEquals("[0]", s.search( + "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + + dsid)); } public void reliabilityTest() throws Exception { - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), + 204); waitForIds(); - Long dfid1 = testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file2_" + timestamp, - datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", 201); + Long dfid1 = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); - testingClient.archive(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); + testingClient.archive(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), + 204); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures() + .isEmpty()); setup.setReliability(0.); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(dfid1), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(dfid1), Flag.NONE, 200); try { while (!testingClient.isPrepared(preparedId, null)) { @@ -676,38 +755,49 @@ public void reliabilityTest() throws Exception { } catch (Exception e) { System.out.println(e); assertEquals("Restore failed", e.getMessage()); - Set failures = testingClient.getServiceStatus(sessionId, 200).getFailures(); + Set failures = testingClient.getServiceStatus(sessionId, 200) + .getFailures(); assertEquals(1, failures.size()); setup.setReliability(1.); testingClient.reset(preparedId, 204); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200) + .getFailures().isEmpty()); while (!testingClient.isPrepared(preparedId, null)) { Thread.sleep(1000); } } setup.setReliability(0.); - Long dfid2 = testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file3_" + timestamp, - datasetIds.get(0), supportedDatafileFormat.getId(), "An even better datafile", "7.1.3", - new Date(420000), new Date(42000), 201); + Long dfid2 = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "An even better datafile", + "7.1.3", new Date(420000), new Date(42000), 201); waitForIds(); System.out.println(testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 200)); + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(dfid2), + 200)); setup.setReliability(1.); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 204); + testingClient.restore(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDatafile(dfid2), 204); waitForIds(); System.out.println(testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 200)); + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(dfid2), + 200)); } protected void reliabilityTest2() throws Exception { - DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(0)); + DataSelection dsel = new DataSelection() + .addDatafile(datafileIds.get(0)); testingClient.archive(sessionId, dsel, 204); waitForIds(); setup.setReliability(0.); - String preparedId = testingClient.prepareData(sessionId, dsel, Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, dsel, + Flag.NONE, 200); try { while (!testingClient.isPrepared(preparedId, null)) { @@ -732,7 +822,8 @@ protected void reliabilityTest2() throws Exception { } protected void reliabilityTest3() throws Exception { - DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(0)); + DataSelection dsel = new DataSelection() + .addDatafile(datafileIds.get(0)); testingClient.archive(sessionId, dsel, 204); waitForIds(); @@ -740,7 +831,8 @@ protected void reliabilityTest3() throws Exception { testingClient.restore(sessionId, dsel, 204); try { - while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { + while (testingClient.getStatus(sessionId, dsel, + null) != Status.ONLINE) { Thread.sleep(1000); } fail("Should throw an error"); @@ -751,7 +843,8 @@ protected void reliabilityTest3() throws Exception { setup.setReliability(1.); testingClient.restore(sessionId, dsel, 204); try { - while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { + while (testingClient.getStatus(sessionId, dsel, + null) != Status.ONLINE) { Thread.sleep(1000); } } catch (Exception e) { @@ -760,7 +853,8 @@ protected void reliabilityTest3() throws Exception { testingClient.reset(sessionId, dsel, 204); testingClient.restore(sessionId, dsel, 204); - while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { + while (testingClient.getStatus(sessionId, dsel, + null) != Status.ONLINE) { Thread.sleep(1000); } } @@ -770,9 +864,11 @@ protected void isPreparedTest() throws Exception { int numDs = 30; int numDf = 19; - Investigation inv = (Investigation) icatWS.search(sessionId, "Investigation INCLUDE Facility").get(0); + Investigation inv = (Investigation) icatWS + .search(sessionId, "Investigation INCLUDE Facility").get(0); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS + .search(sessionId, "DatasetType").get(0); Facility fac = inv.getFacility(); String key = setup.getKey(); @@ -811,7 +907,8 @@ protected void isPreparedTest() throws Exception { logTime("Put calls"); - String preparedId = testingClient.prepareData(sessionId, dsel, Flag.ZIP, 200); + String preparedId = testingClient.prepareData(sessionId, dsel, Flag.ZIP, + 200); logTime("Data prepared"); while (!testingClient.isPrepared(preparedId, 200)) { @@ -829,7 +926,8 @@ protected void isPreparedTest() throws Exception { logTime("Read " + l + " bytes"); } - testingClient.archive(sessionId, new DataSelection().addDataset(dsid), 204); + testingClient.archive(sessionId, new DataSelection().addDataset(dsid), + 204); waitForIds(300); logTime(null); diff --git a/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java index d6cc867a..cad75570 100644 --- a/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java @@ -38,21 +38,24 @@ public static void setup() throws Exception { public void createBogusFiles() throws Exception { long timestamp = System.currentTimeMillis(); - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -67,9 +70,11 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -78,7 +83,8 @@ public void getEmptyDataset() throws Exception { @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); } @@ -86,9 +92,11 @@ public void getSizeEmptyDataset() throws Exception { @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } @@ -97,7 +105,8 @@ public void getNonEmptyDataset() throws Exception { @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); } @@ -105,9 +114,11 @@ public void getSizeNonEmptyDataset() throws Exception { @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -116,7 +127,8 @@ public void getBogusFile() throws Exception { @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); } diff --git a/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java index 5f5b4881..80609a72 100644 --- a/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java @@ -26,9 +26,12 @@ public static void setup() throws Exception { @Test public void deleteDatafileTest() throws Exception { - DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(3)); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); + DataSelection dsel = new DataSelection() + .addDatafile(datafileIds.get(3)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, dsel, 200)); + testingClient.delete(sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), 204); try { testingClient.getStatus(sessionId, dsel, 404); fail(); @@ -44,10 +47,12 @@ public void deleteDatafileTest() throws Exception { public void deleteDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); DataSelection dsel = new DataSelection().addDataset(datasetIds.get(1)); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, dsel, 200)); assertTrue(Files.exists(dirOnFastStorage)); testingClient.delete(sessionId, dsel, 204); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, dsel, 200)); assertFalse(Files.exists(dirOnFastStorage)); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java index 16026ef5..0d1834ea 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java @@ -29,11 +29,13 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", + datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(209L, testingClient.getSize(sessionId, + new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -47,12 +49,15 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", + datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection().addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, + Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -64,59 +69,74 @@ public void getSizePreparedId() throws Exception { @Test public void getSizes1() throws Exception { - assertEquals(208L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); - assertEquals(208L, testingClient.getSize(sessionId, new DataSelection().addDatasets(datasetIds), 200)); - assertEquals(208L, - testingClient.getSize(sessionId, new DataSelection().addInvestigation(investigationId), 200)); assertEquals(208L, testingClient.getSize(sessionId, - new DataSelection().addInvestigation(investigationId).addDatafiles(datafileIds), 200)); - assertEquals(0L, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(2)), 200)); + new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(208L, testingClient.getSize(sessionId, + new DataSelection().addDatasets(datasetIds), 200)); + assertEquals(208L, testingClient.getSize(sessionId, + new DataSelection().addInvestigation(investigationId), 200)); + assertEquals(208L, + testingClient.getSize(sessionId, + new DataSelection().addInvestigation(investigationId) + .addDatafiles(datafileIds), + 200)); + assertEquals(0L, testingClient.getSize(sessionId, + new DataSelection().addDataset(datasetIds.get(2)), 200)); } @Test(expected = NotFoundException.class) public void getSizesBad() throws Exception { - testingClient.getSize(sessionId, new DataSelection().addDatafile(563L), 404); + testingClient.getSize(sessionId, new DataSelection().addDatafile(563L), + 404); } @Test(expected = NotFoundException.class) public void getSizesBad2() throws Exception { - testingClient.getSize(sessionId, new DataSelection().addDatafile(563L).addDatafile(564L), 404); + testingClient.getSize(sessionId, + new DataSelection().addDatafile(563L).addDatafile(564L), 404); } @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0, 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0, + 400)) { } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { + try (InputStream z = testingClient.getData( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + 403)) { } } @Test public void correctBehaviourTestNone() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 0, 200)) { checkStream(stream, datafileIds.get(0)); } } @Test public void correctBehaviourTestCompress() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.COMPRESS, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.COMPRESS, 0, + 200)) { checkZipStream(stream, datafileIds, 36L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.COMPRESS, 0, 200)) { checkStream(stream, datafileIds.get(0)); } @@ -124,25 +144,29 @@ public void correctBehaviourTestCompress() throws Exception { @Test public void correctBehaviourTestZip() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.ZIP, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.ZIP, 0, + 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP, + 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } @Test public void correctBehaviourTestZipAndCompress() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.ZIP_AND_COMPRESS, 0, 200)) { checkZipStream(stream, datafileIds, 36L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP_AND_COMPRESS, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 36L, 0); } @@ -151,25 +175,31 @@ public void correctBehaviourTestZipAndCompress() throws Exception { @Test public void correctBehaviourInvestigation() throws Exception { try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addInvestigation(investigationId), Flag.NONE, 0, 200)) { + new DataSelection().addInvestigation(investigationId), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP, + 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } @Test public void correctBehaviourInvestigations() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addInvestigations(Arrays.asList(investigationId)), Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient + .getData(sessionId, + new DataSelection().addInvestigations( + Arrays.asList(investigationId)), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP, + 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java index 9d844536..1b27b3e3 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java @@ -25,15 +25,17 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, + 400)) { } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); do { Thread.sleep(500); @@ -45,7 +47,8 @@ public void badOffsetFormatTest() throws Exception { @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + try (InputStream z = testingClient + .getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { } } @@ -55,13 +58,15 @@ public void correctBehaviourNoOffsetTest() throws Exception { for (Flag flag : Flag.values()) { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), flag, 200); + new DataSelection().addDatafile(datafileIds.get(0)), flag, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { + try (InputStream stream = testingClient.getData(preparedId, 0, + 200)) { if (flag == Flag.NONE || flag == Flag.COMPRESS) { checkStream(stream, datafileIds.get(0)); } else if (flag == Flag.ZIP) { @@ -74,9 +79,10 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() + throws Exception { + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -90,8 +96,9 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -102,10 +109,13 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() + throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -122,7 +132,8 @@ public void correctBehaviourWithOffsetTest() throws Exception { for (Flag flag : Flag.values()) { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), flag, 200); + new DataSelection().addDatafile(datafileIds.get(0)), flag, + 200); do { Thread.sleep(1000); @@ -130,13 +141,16 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the file twice, with and without an offset byte[] out = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] outOffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); + byte[] outOffset = getOutput( + testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the // offset - System.out.println(flag + ": " + out.length + " " + outOffset.length); + System.out + .println(flag + ": " + out.length + " " + outOffset.length); for (int i = 0; i < outOffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) outOffset[i], (byte) out[i + goodOffset]); + assertEquals("Byte offset: " + i, (byte) outOffset[i], + (byte) out[i + goodOffset]); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java index d5db81b7..272f1a1a 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java @@ -40,17 +40,20 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(sessionId, new DataSelection() + .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTestAnon() throws Exception { - testingClient.getStatus(null, new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(null, new DataSelection() + .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.getStatus(setup.getForbiddenSessionId(), new DataSelection().addDatafiles(datafileIds), 403); + testingClient.getStatus(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), 403); } @Test @@ -59,7 +62,8 @@ public void correctBehaviourTest() throws Exception { Status status; do { Thread.sleep(1000); - status = testingClient.getStatus(sessionId, new DataSelection().addDatafiles(datafileIds), 200); + status = testingClient.getStatus(sessionId, + new DataSelection().addDatafiles(datafileIds), 200); System.out.println("*" + status + "*"); } while (status != Status.ONLINE); @@ -67,8 +71,10 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java index b870eba0..b0c72fb6 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java @@ -33,22 +33,20 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient - .prepareData(sessionId, - new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), - Flag.NONE, 404); + testingClient.prepareData(sessionId, new DataSelection().addDatafiles( + Arrays.asList(1L, 2L, 3L, 99999L)), Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, - 404); + testingClient.prepareData(sessionId, + new DataSelection().addDatafile(99999L), Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient - .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient.prepareData(sessionId, + new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) diff --git a/src/test/java/org/icatproject/ids/integration/one/MiscTest.java b/src/test/java/org/icatproject/ids/integration/one/MiscTest.java index 3baf012c..d5006b9b 100644 --- a/src/test/java/org/icatproject/ids/integration/one/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/MiscTest.java @@ -41,17 +41,20 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); @@ -60,13 +63,15 @@ public void correctBehaviourNoOffsetTest() throws Exception { @Test(expected = NotImplementedException.class) public void restoreNotAvailableTest() throws Exception { - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 501); waitForIds(); } @Test(expected = NotImplementedException.class) public void archiveNotAvailableTest() throws Exception { - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.archive(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 501); waitForIds(); } diff --git a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java index 1943ae01..cf1a064b 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java @@ -29,7 +29,8 @@ public static void beforeClass() throws Exception { int n = 100000; start = System.currentTimeMillis(); - try (OutputStream f = Files.newOutputStream(infile, StandardOpenOption.CREATE)) { + try (OutputStream f = Files.newOutputStream(infile, + StandardOpenOption.CREATE)) { for (int i = 0; i < n; i++) { f.write(junk); } @@ -50,12 +51,13 @@ public static void afterClass() throws Exception { public void putOneFileTest() throws Exception { byte[] junk = new byte[1000]; start = System.currentTimeMillis(); - Long dfid = testingClient.put(sessionId, Files.newInputStream(infile), "big_" + timestamp, datasetIds.get(0), + Long dfid = testingClient.put(sessionId, Files.newInputStream(infile), + "big_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), "A big datafile", 201); ts("store file (put)"); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(dfid), Flag.NONE, 0, - null)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(dfid), Flag.NONE, 0, null)) { boolean first = true; while (stream.read(junk) > 0) { if (first) { @@ -77,14 +79,15 @@ private static void ts(String msg) { public void putAsPostOneFileTest() throws Exception { byte[] junk = new byte[1000]; start = System.currentTimeMillis(); - Long dfid = testingClient.putAsPost(sessionId, Files.newInputStream(infile), "big2_" + timestamp, - datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", null, null, null, - true, 201); + Long dfid = testingClient.putAsPost(sessionId, + Files.newInputStream(infile), "big2_" + timestamp, + datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", null, null, null, true, 201); ts("store file (post)"); int ntot = 0; - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(dfid), Flag.NONE, 0, - null)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(dfid), Flag.NONE, 0, null)) { boolean first = true; int n; diff --git a/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java index acdf7be9..c7129b25 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java @@ -26,27 +26,30 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + testingClient.prepareData("bad sessionId format", + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 400); } @Test(expected = BadRequestException.class) public void noIdsTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection(), Flag.NONE, 400); + testingClient.prepareData("bad sessionId format", new DataSelection(), + Flag.NONE, 400); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); System.out.println(preparedId); assertNotNull(preparedId); } @@ -54,8 +57,9 @@ public void correctBehaviourTest() throws Exception { @Test public void prepareDataset() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -66,14 +70,17 @@ public void prepareDataset() throws Exception { Thread.sleep(1000); } - assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue( + Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareTwoDatasets() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -85,14 +92,16 @@ public void prepareTwoDatasets() throws Exception { Thread.sleep(1000); } - assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue( + Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareDatafile() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -102,14 +111,17 @@ public void prepareDatafile() throws Exception { Thread.sleep(1000); } - assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue( + Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareDatafileAndItsDataset() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -120,7 +132,8 @@ public void prepareDatafileAndItsDataset() throws Exception { Thread.sleep(1000); } - assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue( + Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/PutTest.java b/src/test/java/org/icatproject/ids/integration/one/PutTest.java index 0a6dec0a..4025eda7 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PutTest.java @@ -38,9 +38,11 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); Datafile df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("A rather splendid datafile", df.getDescription()); @@ -49,9 +51,11 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileModTime()); assertTrue(Files.exists(dirOnFastStorage)); - dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); + dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "An even better datafile", + "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -72,9 +76,11 @@ public void putAsPostOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - Long dfid = testingClient.putAsPost(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", null, null, null, true, 201); + Long dfid = testingClient.putAsPost(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "A rather splendid datafile", + null, null, null, true, 201); Datafile df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("A rather splendid datafile", df.getDescription()); @@ -83,9 +89,11 @@ public void putAsPostOneFileTest() throws Exception { assertNull(df.getDatafileModTime()); assertTrue(Files.exists(dirOnFastStorage)); - dfid = testingClient.putAsPost(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "An even better datafile", "7.1.3", new Date(420000), new Date(42000), false, 201); + dfid = testingClient.putAsPost(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "An even better datafile", + "7.1.3", new Date(420000), new Date(42000), false, 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); diff --git a/src/test/java/org/icatproject/ids/integration/one/WriteTest.java b/src/test/java/org/icatproject/ids/integration/one/WriteTest.java index 9a7550a3..58891e59 100644 --- a/src/test/java/org/icatproject/ids/integration/one/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/WriteTest.java @@ -18,7 +18,8 @@ public static void setup() throws Exception { @Test(expected = NotImplementedException.class) public void writeNotAvailableTest() throws Exception { - testingClient.write(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.write(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 501); } } diff --git a/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java b/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java index d5bf557e..5145ecec 100644 --- a/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java @@ -30,13 +30,15 @@ public void restoreThenArchiveDataset() throws Exception { assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); diff --git a/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java index 3ff9adf1..f522a1f8 100644 --- a/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java @@ -47,21 +47,24 @@ public static void setup() throws Exception { @Before public void createBogusFiles() throws Exception { - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -77,9 +80,11 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -88,7 +93,8 @@ public void getEmptyDataset() throws Exception { @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); } @@ -96,14 +102,17 @@ public void getSizeEmptyDataset() throws Exception { @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); testingClient.restore(sessionId, selection, 204); waitForIds(); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, null)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, selection, null)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } @@ -112,7 +121,8 @@ public void getNonEmptyDataset() throws Exception { @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); } @@ -120,14 +130,17 @@ public void getSizeNonEmptyDataset() throws Exception { @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.restore(sessionId, selection, 404); waitForIds(); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, 404)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, selection, 404)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -136,15 +149,17 @@ public void getBogusFile() throws Exception { @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); } /* - * Try the full cycle: upload a new file into a dataset having a bogus file, which triggers a write of the - * dataset to archive storage, archive the dataset, and restore it. Each step must deal gracefully with - * the bogus file in the dataset. + * Try the full cycle: upload a new file into a dataset having a bogus file, + * which triggers a write of the dataset to archive storage, archive the + * dataset, and restore it. Each step must deal gracefully with the bogus + * file in the dataset. */ @Test public void putWriteArchiveRestore() throws Exception { @@ -157,9 +172,11 @@ public void putWriteArchiveRestore() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfulId = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfulId = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, dsId, + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); Datafile dful = (Datafile) icatWS.get(sessionId, "Datafile", dfulId); testingClient.archive(sessionId, selection, 204); waitForIds(); @@ -172,13 +189,16 @@ public void putWriteArchiveRestore() throws Exception { File[] filesList = dirOnFastStorage.toFile().listFiles(); assertEquals(3, filesList.length); Set locations = new HashSet<>(); - Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(0)); + Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", + datafileIds.get(0)); locations.add(getLocationFromDigest(df1.getId(), df1.getLocation())); - Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(1)); + Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", + datafileIds.get(1)); locations.add(getLocationFromDigest(df2.getId(), df2.getLocation())); locations.add(getLocationFromDigest(dful.getId(), dful.getLocation())); for (File file : filesList) { - String location = setup.getStorageDir().relativize(file.toPath()).toString(); + String location = setup.getStorageDir().relativize(file.toPath()) + .toString(); assertTrue(locations.contains(location)); } diff --git a/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java index d8179493..da6b435f 100644 --- a/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java @@ -20,7 +20,8 @@ public static void setup() throws Exception { @Test(expected = DataNotOnlineException.class) public void deleteFromUnrestoredDataset() throws Exception { - testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 503); + testingClient.delete(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 503); } @Test @@ -29,11 +30,13 @@ public void deleteDatafileFromRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); + testingClient.delete(sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), 204); waitForIds(); checkZipFile(fileOnArchiveStorage, datafileIds.subList(2, 3), 36); @@ -44,12 +47,14 @@ public void deleteRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); checkPresent(fileOnArchiveStorage); - testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.delete(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkAbsent(dirOnFastStorage); checkAbsent(fileOnArchiveStorage); diff --git a/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java index 1a3970a0..866f5bf7 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java @@ -31,14 +31,17 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, + 400)) { } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { + try (InputStream z = testingClient.getData( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + 403)) { } } @@ -47,11 +50,13 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", + datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(209L, testingClient.getSize(sessionId, + new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -65,12 +70,15 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", + datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection().addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, + Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -83,16 +91,18 @@ public void getSizePreparedId() throws Exception { @Test public void correctBehaviourTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.NONE, - 0, 503)) { + try (InputStream z = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + 503)) { fail("Should have thrown exception"); } catch (IdsException e) { assertEquals(DataNotOnlineException.class, e.getClass()); } while (true) { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + null)) { checkZipStream(stream, datafileIds, 57, 0); break; } catch (IdsException e) { @@ -105,16 +115,18 @@ public void correctBehaviourTest() throws Exception { @Test public void gettingDatafileRestoresItsDatasetTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)), - Flag.NONE, 0, null)) { + try (InputStream z = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(2)), Flag.NONE, + 0, null)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), Flag.NONE, + 0, 200)) { checkStream(stream, datafileIds.get(3)); } @@ -123,8 +135,9 @@ public void gettingDatafileRestoresItsDatasetTest() throws Exception { @Test public void gettingDatasetUsesCacheTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, null)) { + try (InputStream z = testingClient.getData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, + null)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -132,26 +145,32 @@ public void gettingDatasetUsesCacheTest() throws Exception { waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, + 200)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } } @Test - public void gettingDatafileAndDatasetShouldRestoreBothDatasetsTest() throws Exception { - - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)) - .addDataset(datasetIds.get(0)), Flag.NONE, 0, 503)) { + public void gettingDatafileAndDatasetShouldRestoreBothDatasetsTest() + throws Exception { + + try (InputStream z = testingClient + .getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(2)) + .addDataset(datasetIds.get(0)), + Flag.NONE, 0, 503)) { fail("Should throw exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatasets(datasetIds), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatasets(datasetIds), Flag.NONE, 0, + 200)) { checkZipStream(stream, datafileIds, 57, 0); } } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java index 97f66d5a..c657ef0e 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java @@ -25,15 +25,17 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, + 400)) { } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -45,14 +47,16 @@ public void badOffsetFormatTest() throws Exception { @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + try (InputStream z = testingClient + .getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { } } @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -64,9 +68,10 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() + throws Exception { + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -81,8 +86,9 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -94,10 +100,13 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() + throws Exception { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafiles(datafileIds), Flag.NONE, 200); + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -111,8 +120,9 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exceptio @Test public void correctBehaviourWithOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -120,12 +130,14 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the zip file twice, with and without an offset byte[] zip = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] zipoffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); + byte[] zipoffset = getOutput( + testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the offset System.out.println(zip.length + " " + zipoffset.length); for (int i = 0; i < zipoffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) zipoffset[i], (byte) zip[i + goodOffset]); + assertEquals("Byte offset: " + i, (byte) zipoffset[i], + (byte) zip[i + goodOffset]); } } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java index 8ecd4429..d9a7a651 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java @@ -34,8 +34,8 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, - new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(sessionId, new DataSelection() + .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) @@ -55,8 +55,10 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } @@ -64,7 +66,8 @@ public void getStatusPreparedIdTest() throws Exception { testingClient.archive(sessionId, selection, 204); waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); - // verify that getStatus() as opposed to isPrepared() does not implicitly trigger a restore. + // verify that getStatus() as opposed to isPrepared() does not + // implicitly trigger a restore. waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); } @@ -72,7 +75,8 @@ public void getStatusPreparedIdTest() throws Exception { @Test public void restoringDatafileRestoresItsDatasetTest() throws Exception { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, 200); + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java index c7daebea..e6fa086c 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java @@ -33,22 +33,20 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient - .prepareData(sessionId, - new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), - Flag.NONE, 404); + testingClient.prepareData(sessionId, new DataSelection().addDatafiles( + Arrays.asList(1L, 2L, 3L, 99999L)), Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, - 404); + testingClient.prepareData(sessionId, + new DataSelection().addDatafile(99999L), Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient - .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient.prepareData(sessionId, + new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) diff --git a/src/test/java/org/icatproject/ids/integration/two/MiscTest.java b/src/test/java/org/icatproject/ids/integration/two/MiscTest.java index fa5751d8..2eff91cd 100644 --- a/src/test/java/org/icatproject/ids/integration/two/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/MiscTest.java @@ -40,19 +40,23 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); - assertFalse(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertFalse(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); diff --git a/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java index 891da989..2f9cb527 100644 --- a/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java @@ -28,8 +28,9 @@ public static void setup() throws Exception { public void prepareArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -49,8 +50,10 @@ public void prepareTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -70,8 +73,9 @@ public void prepareTwoArchivedDatasets() throws Exception { public void prepareArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -88,8 +92,11 @@ public void prepareArchivedDatafile() throws Exception { public void prepareArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient + .prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -105,26 +112,29 @@ public void prepareArchivedDatafileAndItsDataset() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + testingClient.prepareData("bad sessionId format", + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 400); } @Test public void noIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, 200); + testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, + 200); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); assertNotNull(preparedId); } @@ -132,14 +142,16 @@ public void correctBehaviourTest() throws Exception { public void prepareRestoredDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -155,16 +167,19 @@ public void prepareTwoRestoredDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), + 204); waitForIds(); checkPresent(dirOnFastStorage1); checkPresent(dirOnFastStorage2); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -179,14 +194,16 @@ public void prepareRestoredDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -201,13 +218,16 @@ public void prepareRestoredDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), + 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/two/PutTest.java b/src/test/java/org/icatproject/ids/integration/two/PutTest.java index 3ca3f8dd..a512a1e7 100644 --- a/src/test/java/org/icatproject/ids/integration/two/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/PutTest.java @@ -32,8 +32,9 @@ public static void setup() throws Exception { @Test(expected = DataNotOnlineException.class) public void putToUnrestoredDataset() throws Exception { - testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file1_" - + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), null, 503); + testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file1_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), null, 503); } @Test @@ -41,15 +42,18 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); waitForIds(); @@ -59,9 +63,11 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileCreateTime()); assertNull(df.getDatafileModTime()); - dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); + dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "An even better datafile", + "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -79,7 +85,8 @@ public void putOneFileTest() throws Exception { assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java b/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java index 5122232e..1d7443f9 100644 --- a/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java @@ -32,9 +32,9 @@ public static void setup() throws Exception { } /* - * Note that we cannot test for DUPLICATE_ENTRY here, because ZipOutputStream() won't allow - * us to create such a defective ZIP file. But that doesn't mean that this error cannot - * occur. + * Note that we cannot test for DUPLICATE_ENTRY here, because + * ZipOutputStream() won't allow us to create such a defective ZIP file. But + * that doesn't mean that this error cannot occur. */ private enum Defect { NONE, MISSING_ENTRY, SPURIOUS_ENTRY, DUPLICATE_ENTRY @@ -43,8 +43,10 @@ private enum Defect { private void cloneZip(Path archivepath, Defect defect) throws IOException { Path savepath = archivepath.getParent().resolve(".sav"); Files.move(archivepath, savepath); - try (ZipOutputStream zipout = new ZipOutputStream(Files.newOutputStream(archivepath))) { - try (ZipInputStream zipin = new ZipInputStream(Files.newInputStream(savepath))) { + try (ZipOutputStream zipout = new ZipOutputStream( + Files.newOutputStream(archivepath))) { + try (ZipInputStream zipin = new ZipInputStream( + Files.newInputStream(savepath))) { ZipEntry entry = zipin.getNextEntry(); boolean first = true; String entryName = ""; diff --git a/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java b/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java index 2755fc9e..0efa59fb 100644 --- a/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java @@ -22,7 +22,8 @@ public void restoreArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); @@ -34,8 +35,9 @@ public void restoreArchivedDataset() throws Exception { public void restoreTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), + 204); waitForIds(); checkPresent(dirOnFastStorage1); @@ -47,7 +49,8 @@ public void restoreArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); @@ -59,8 +62,9 @@ public void restoreArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), + 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/two/WriteTest.java b/src/test/java/org/icatproject/ids/integration/two/WriteTest.java index c9367a43..7052c1df 100644 --- a/src/test/java/org/icatproject/ids/integration/two/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/WriteTest.java @@ -28,8 +28,8 @@ public static void setup() throws Exception { } /** - * In principle, it's always possible to do a write call on - * existing datasets, but it will have no visible effect. + * In principle, it's always possible to do a write call on existing + * datasets, but it will have no visible effect. */ @Test public void restoreThenWriteDataset() throws Exception { @@ -47,18 +47,19 @@ public void restoreThenWriteDataset() throws Exception { } /** - * Create a dataset in ICAT, store the files in main storage, - * then do a write call to IDS to get the dataset written to - * archive storage. + * Create a dataset in ICAT, store the files in main storage, then do a + * write call to IDS to get the dataset written to archive storage. */ @Test public void storeThenWrite() throws Exception { long timestamp = System.currentTimeMillis(); - Investigation inv = (Investigation) icatWS.get(sessionId, "Investigation INCLUDE Facility", investigationId); + Investigation inv = (Investigation) icatWS.get(sessionId, + "Investigation INCLUDE Facility", investigationId); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS + .search(sessionId, "DatasetType").get(0); Dataset ds = new Dataset(); ds.setName("dsWrite_" + timestamp); @@ -72,7 +73,8 @@ public void storeThenWrite() throws Exception { df.setName("dfWrite_" + timestamp); df.setLocation(dsLoc + UUID.randomUUID()); df.setDataset(ds); - writeToFile(df, "some really boring datafile test content", setup.getKey()); + writeToFile(df, "some really boring datafile test content", + setup.getKey()); Long dsId = ds.getId(); Path dirOnFastStorage = getDirOnFastStorage(dsId); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java b/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java index 75557414..aeeff13b 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java @@ -30,13 +30,15 @@ public void restoreThenArchiveDataset() throws Exception { assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java index 670a071a..392c8ad5 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java @@ -47,21 +47,24 @@ public static void setup() throws Exception { @Before public void createBogusFiles() throws Exception { - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -77,9 +80,11 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -88,7 +93,8 @@ public void getEmptyDataset() throws Exception { @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); } @@ -96,14 +102,17 @@ public void getSizeEmptyDataset() throws Exception { @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); testingClient.restore(sessionId, selection, 204); waitForIds(); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, null)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, selection, null)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } @@ -112,7 +121,8 @@ public void getNonEmptyDataset() throws Exception { @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); } @@ -120,14 +130,17 @@ public void getSizeNonEmptyDataset() throws Exception { @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.restore(sessionId, selection, 404); waitForIds(); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, 404)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, selection, 404)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -136,15 +149,17 @@ public void getBogusFile() throws Exception { @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); } /* - * Try the full cycle: upload a new file into a dataset having a bogus file, which triggers a write of the - * datafile to archive storage, archive the dataset, and restore it. Each step must deal gracefully with - * the bogus file in the dataset. + * Try the full cycle: upload a new file into a dataset having a bogus file, + * which triggers a write of the datafile to archive storage, archive the + * dataset, and restore it. Each step must deal gracefully with the bogus + * file in the dataset. */ @Test public void putWriteArchiveRestore() throws Exception { @@ -157,9 +172,11 @@ public void putWriteArchiveRestore() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfulId = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfulId = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, dsId, + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); Datafile dful = (Datafile) icatWS.get(sessionId, "Datafile", dfulId); testingClient.archive(sessionId, selection, 204); waitForIds(); @@ -172,13 +189,16 @@ public void putWriteArchiveRestore() throws Exception { File[] filesList = dirOnFastStorage.toFile().listFiles(); assertEquals(3, filesList.length); Set locations = new HashSet<>(); - Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(0)); + Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", + datafileIds.get(0)); locations.add(getLocationFromDigest(df1.getId(), df1.getLocation())); - Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(1)); + Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", + datafileIds.get(1)); locations.add(getLocationFromDigest(df2.getId(), df2.getLocation())); locations.add(getLocationFromDigest(dful.getId(), dful.getLocation())); for (File file : filesList) { - String location = setup.getStorageDir().relativize(file.toPath()).toString(); + String location = setup.getStorageDir().relativize(file.toPath()) + .toString(); assertTrue(locations.contains(location)); } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java index 063102fb..e92431e7 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java @@ -19,7 +19,8 @@ public static void setup() throws Exception { @Test public void deleteFromUnrestoredDataset() throws Exception { - testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.delete(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); } @Test @@ -27,11 +28,13 @@ public void deleteDatafileFromRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); + testingClient.delete(sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), 204); waitForIds(); } @@ -40,12 +43,14 @@ public void deleteRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); checkPresent(fileOnArchiveStorage); - testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.delete(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkAbsent(dirOnFastStorage); checkAbsent(fileOnArchiveStorage); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java index b6d9e772..f505c837 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java @@ -31,14 +31,17 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, + 400)) { } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { + try (InputStream z = testingClient.getData( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + 403)) { } } @@ -47,11 +50,13 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", + datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(209L, testingClient.getSize(sessionId, + new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -65,12 +70,15 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", + datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection().addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, + Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -83,8 +91,9 @@ public void getSizePreparedId() throws Exception { @Test public void correctBehaviourTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.NONE, - 0, 503)) { + try (InputStream z = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + 503)) { fail("Should have thrown exception"); } catch (IdsException e) { @@ -92,8 +101,9 @@ public void correctBehaviourTest() throws Exception { } while (true) { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + null)) { checkZipStream(stream, datafileIds, 57, 0); break; } catch (IdsException e) { @@ -106,16 +116,18 @@ public void correctBehaviourTest() throws Exception { @Test public void gettingDatafileDoesNotRestoreItsDatasetTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)), - Flag.NONE, 0, 503)) { + try (InputStream z = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(2)), Flag.NONE, + 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), - Flag.NONE, 0, 503)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), Flag.NONE, + 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -126,8 +138,9 @@ public void gettingDatafileDoesNotRestoreItsDatasetTest() throws Exception { @Test public void gettingDatasetUsesCacheTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, 503)) { + try (InputStream z = testingClient.getData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, + 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -135,26 +148,32 @@ public void gettingDatasetUsesCacheTest() throws Exception { waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, + 200)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } } @Test - public void gettingDatafileAndDatasetShouldNotRestoreBothDatasetsTest() throws Exception { - - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)) - .addDataset(datasetIds.get(0)), Flag.NONE, 0, 503)) { + public void gettingDatafileAndDatasetShouldNotRestoreBothDatasetsTest() + throws Exception { + + try (InputStream z = testingClient + .getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(2)) + .addDataset(datasetIds.get(0)), + Flag.NONE, 0, 503)) { fail("Should throw exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatasets(datasetIds), - Flag.NONE, 0, 503)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatasets(datasetIds), Flag.NONE, 0, + 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java index c0fa6eef..200eba93 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java @@ -25,15 +25,17 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, + 400)) { } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -45,14 +47,16 @@ public void badOffsetFormatTest() throws Exception { @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + try (InputStream z = testingClient + .getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { } } @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -64,9 +68,10 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() + throws Exception { + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -81,8 +86,9 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -94,10 +100,13 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() + throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -111,8 +120,9 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exceptio @Test public void correctBehaviourWithOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -120,12 +130,14 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the zip file twice, with and without an offset byte[] zip = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] zipoffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); + byte[] zipoffset = getOutput( + testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the offset System.out.println(zip.length + " " + zipoffset.length); for (int i = 0; i < zipoffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) zipoffset[i], (byte) zip[i + goodOffset]); + assertEquals("Byte offset: " + i, (byte) zipoffset[i], + (byte) zip[i + goodOffset]); } } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java index ed6bed60..2ceadc91 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java @@ -34,8 +34,8 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, - new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(sessionId, new DataSelection() + .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) @@ -55,8 +55,10 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } @@ -64,15 +66,18 @@ public void getStatusPreparedIdTest() throws Exception { testingClient.archive(sessionId, selection, 204); waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); - // verify that getStatus() as opposed to isPrepared() does not implicitly trigger a restore. + // verify that getStatus() as opposed to isPrepared() does not + // implicitly trigger a restore. waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); } @Test - public void restoringDatafileDoesNotRestoresItsDatasetTest() throws Exception { + public void restoringDatafileDoesNotRestoresItsDatasetTest() + throws Exception { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, 200); + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java index 7889492e..6f7c04ae 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java @@ -33,22 +33,20 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient - .prepareData(sessionId, - new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), - Flag.NONE, 404); + testingClient.prepareData(sessionId, new DataSelection().addDatafiles( + Arrays.asList(1L, 2L, 3L, 99999L)), Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, - 404); + testingClient.prepareData(sessionId, + new DataSelection().addDatafile(99999L), Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient - .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient.prepareData(sessionId, + new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) diff --git a/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java b/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java index 011de0ad..674bc721 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java @@ -40,11 +40,13 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); var status = testingClient.getServiceStatus(sessionId, 200); - //System.out.println("### twodf.MiscTest.correctBehaviourNoOffsetTest - status: " + status.toString()); + // System.out.println("### twodf.MiscTest.correctBehaviourNoOffsetTest - + // status: " + status.toString()); assertFalse(status.getOpItems().isEmpty()); @@ -52,10 +54,12 @@ public void correctBehaviourNoOffsetTest() throws Exception { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java index 386a7fff..280e68d8 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java @@ -28,8 +28,9 @@ public static void setup() throws Exception { public void prepareArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -49,8 +50,10 @@ public void prepareTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -70,8 +73,9 @@ public void prepareTwoArchivedDatasets() throws Exception { public void prepareArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -88,8 +92,11 @@ public void prepareArchivedDatafile() throws Exception { public void prepareArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient + .prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -105,26 +112,29 @@ public void prepareArchivedDatafileAndItsDataset() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + testingClient.prepareData("bad sessionId format", + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 400); } @Test public void noIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, 200); + testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, + 200); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); assertNotNull(preparedId); } @@ -132,14 +142,16 @@ public void correctBehaviourTest() throws Exception { public void prepareRestoredDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -155,16 +167,19 @@ public void prepareTwoRestoredDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), + 204); waitForIds(); checkPresent(dirOnFastStorage1); checkPresent(dirOnFastStorage2); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -179,14 +194,16 @@ public void prepareRestoredDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -201,13 +218,16 @@ public void prepareRestoredDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), + 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java b/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java index b960cd6e..ce9ab96f 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java @@ -31,8 +31,9 @@ public static void setup() throws Exception { @Test // Works fine for datafile storage public void putToUnrestoredDataset() throws Exception { - testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file1_" - + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), null, 201); + testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file1_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), null, 201); } @Test @@ -40,15 +41,18 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); waitForIds(); @@ -58,9 +62,11 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileCreateTime()); assertNull(df.getDatafileModTime()); - dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); + dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "An even better datafile", + "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -78,7 +84,8 @@ public void putOneFileTest() throws Exception { assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java b/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java index baf7be8d..e0127ebc 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java @@ -22,9 +22,8 @@ public static void setup() throws Exception { } /* - * Arrange for multiple different operations, requiring conflicting - * locks on the same dataset to be processed at the same time. This - * triggers Bug #82. + * Arrange for multiple different operations, requiring conflicting locks on + * the same dataset to be processed at the same time. This triggers Bug #82. */ @Test public void multiOperationTest() throws Exception { @@ -37,8 +36,9 @@ public void multiOperationTest() throws Exception { assertTrue(Files.exists(dirOnFastStorage)); testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + "uploaded_file_" + timestamp, dsId, + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); testingClient.archive(sessionId, selection, 204); waitForIds(); } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java b/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java index 7125b9a4..4f792af5 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java @@ -20,7 +20,8 @@ public static void setup() throws Exception { @Test public void restoreArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); } @@ -29,8 +30,9 @@ public void restoreArchivedDataset() throws Exception { public void restoreTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), + 204); waitForIds(); checkPresent(dirOnFastStorage1); @@ -42,7 +44,8 @@ public void restoreArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); @@ -54,8 +57,9 @@ public void restoreArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), + 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java b/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java index 876af417..3bae2233 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java @@ -27,8 +27,8 @@ public static void setup() throws Exception { } /** - * In principle, it's always possible to do a write call on - * existing datasets, but it will have no visible effect. + * In principle, it's always possible to do a write call on existing + * datasets, but it will have no visible effect. */ @Test public void restoreThenWriteDataset() throws Exception { @@ -46,18 +46,19 @@ public void restoreThenWriteDataset() throws Exception { } /** - * Create a dataset in ICAT, store the files in main storage, - * then do a write call to IDS to get the dataset written to - * archive storage. + * Create a dataset in ICAT, store the files in main storage, then do a + * write call to IDS to get the dataset written to archive storage. */ @Test public void storeThenWrite() throws Exception { long timestamp = System.currentTimeMillis(); - Investigation inv = (Investigation) icatWS.get(sessionId, "Investigation INCLUDE Facility", investigationId); + Investigation inv = (Investigation) icatWS.get(sessionId, + "Investigation INCLUDE Facility", investigationId); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS + .search(sessionId, "DatasetType").get(0); Dataset ds = new Dataset(); ds.setName("dsWrite_" + timestamp); @@ -71,7 +72,8 @@ public void storeThenWrite() throws Exception { df.setName("dfWrite_" + timestamp); df.setLocation(dsLoc + UUID.randomUUID()); df.setDataset(ds); - writeToFile(df, "some really boring datafile test content", setup.getKey()); + writeToFile(df, "some really boring datafile test content", + setup.getKey()); Long dsId = ds.getId(); Path dirOnFastStorage = getDirOnFastStorage(dsId); diff --git a/src/test/java/org/icatproject/ids/integration/util/Setup.java b/src/test/java/org/icatproject/ids/integration/util/Setup.java index 2392805d..5b628f33 100644 --- a/src/test/java/org/icatproject/ids/integration/util/Setup.java +++ b/src/test/java/org/icatproject/ids/integration/util/Setup.java @@ -57,11 +57,13 @@ public Setup(String runPropertyFile) throws Exception { // Start by reading the test properties Properties testProps = new Properties(); - InputStream is = Setup.class.getClassLoader().getResourceAsStream("test.properties"); + InputStream is = Setup.class.getClassLoader() + .getResourceAsStream("test.properties"); try { testProps.load(is); } catch (Exception e) { - System.err.println("Problem loading test.properties: " + e.getClass() + " " + e.getMessage()); + System.err.println("Problem loading test.properties: " + + e.getClass() + " " + e.getMessage()); } setReliability(1.); @@ -72,16 +74,18 @@ public Setup(String runPropertyFile) throws Exception { String containerHome = System.getProperty("containerHome"); if (containerHome == null) { - System.err.println("containerHome is not defined as a system property"); + System.err.println( + "containerHome is not defined as a system property"); } long time = System.currentTimeMillis(); - ShellCommand sc = new ShellCommand("src/test/scripts/prepare_test.py", "src/test/resources/" + runPropertyFile, - home.toString(), containerHome, serverUrl); + ShellCommand sc = new ShellCommand("src/test/scripts/prepare_test.py", + "src/test/resources/" + runPropertyFile, home.toString(), + containerHome, serverUrl); System.out.println(sc.getStdout() + " " + sc.getStderr()); - System.out.println( - "Setting up " + runPropertyFile + " took " + (System.currentTimeMillis() - time) / 1000. + "seconds"); + System.out.println("Setting up " + runPropertyFile + " took " + + (System.currentTimeMillis() - time) / 1000. + "seconds"); // Having set up the ids.properties file read it find other things CheckedProperties runProperties = new CheckedProperties(); @@ -92,8 +96,10 @@ public Setup(String runPropertyFile) throws Exception { updownDir = home.resolve(testProps.getProperty("updownDir")); icatUrl = runProperties.getURL("icat.url"); ICAT icat = ICATGetter.getService(icatUrl.toString()); - rootSessionId = TestUtils.login(icat, testProps.getProperty("login.root")); - forbiddenSessionId = TestUtils.login(icat, testProps.getProperty("login.unauthorized")); + rootSessionId = TestUtils.login(icat, + testProps.getProperty("login.root")); + forbiddenSessionId = TestUtils.login(icat, + testProps.getProperty("login.unauthorized")); storageDir = runProperties.getPath("plugin.main.dir"); diff --git a/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java b/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java index 6527c191..577d29f5 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java @@ -17,7 +17,8 @@ public class HttpInputStream extends FilterInputStream { * @param httpclient the CloseableHttpClient to close * @param response the response to use and ultimately close */ - public HttpInputStream(CloseableHttpClient httpclient, CloseableHttpResponse response) + public HttpInputStream(CloseableHttpClient httpclient, + CloseableHttpResponse response) throws IllegalStateException, IOException { super(response.getEntity().getContent()); this.response = response; diff --git a/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java b/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java index 7bb22543..37b0ffdb 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java @@ -107,11 +107,14 @@ void storeFailure(Long id) { public String toString() { String result = "lockCount: " + this.lockCount + ", "; result += "lockedIDs: " + lockedDs.size() + "\n"; - for(long lockedId : lockedDs) result += "\t" + lockedId + "\n"; + for (long lockedId : lockedDs) + result += "\t" + lockedId + "\n"; result += "opItems: " + opItems.size() + "\n"; - for(String key : opItems.keySet()) result += "\t" + key + ": " + opItems.get(key) + "\n"; + for (String key : opItems.keySet()) + result += "\t" + key + ": " + opItems.get(key) + "\n"; result += "failures: " + failures.size() + "\n"; - for(long fail : failures) result += " " + fail; + for (long fail : failures) + result += " " + fail; result += "\n"; return result; @@ -129,7 +132,8 @@ public enum Status { public TestingClient(URL idsUrl) { try { - idsUri = new URI(idsUrl.getProtocol(), null, idsUrl.getHost(), idsUrl.getPort(), null, null, null); + idsUri = new URI(idsUrl.getProtocol(), null, idsUrl.getHost(), + idsUrl.getPort(), null, null, null); basePath = idsUrl.getPath(); } catch (URISyntaxException e) { throw new RuntimeException(e); @@ -137,22 +141,28 @@ public TestingClient(URL idsUrl) { } - public void archive(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, - BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { + public void archive(String sessionId, DataSelection data, Integer sc) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, + NotFoundException { URI uri = getUri(getUriBuilder("archive")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(formparams)); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try (CloseableHttpResponse response = httpclient + .execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -160,8 +170,10 @@ public void archive(String sessionId, DataSelection data, Integer sc) throws Not } private void checkStatus(HttpResponse response, Integer sc) - throws InternalException, BadRequestException, DataNotOnlineException, ParseException, IOException, - InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException { + throws InternalException, BadRequestException, + DataNotOnlineException, ParseException, IOException, + InsufficientPrivilegesException, NotImplementedException, + InsufficientStorageException, NotFoundException { StatusLine status = response.getStatusLine(); if (status == null) { throw new InternalException("Status line returned is empty"); @@ -188,7 +200,8 @@ private void checkStatus(HttpResponse response, Integer sc) } String code; String message; - try (JsonReader jsonReader = Json.createReader(new StringReader(error))) { + try (JsonReader jsonReader = Json + .createReader(new StringReader(error))) { JsonObject json = jsonReader.readObject(); code = json.getString("code"); message = json.getString("message"); @@ -227,7 +240,8 @@ private void checkStatus(HttpResponse response, Integer sc) } public void delete(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("delete"); @@ -239,47 +253,56 @@ public void delete(String sessionId, DataSelection data, Integer sc) try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpDelete httpDelete = new HttpDelete(uri); - try (CloseableHttpResponse response = httpclient.execute(httpDelete)) { + try (CloseableHttpResponse response = httpclient + .execute(httpDelete)) { expectNothing(response, sc); } catch (InsufficientStorageException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - private void expectNothing(CloseableHttpResponse response, Integer sc) throws InternalException, - BadRequestException, DataNotOnlineException, ParseException, InsufficientPrivilegesException, - NotImplementedException, InsufficientStorageException, NotFoundException, IOException { + private void expectNothing(CloseableHttpResponse response, Integer sc) + throws InternalException, BadRequestException, + DataNotOnlineException, ParseException, + InsufficientPrivilegesException, NotImplementedException, + InsufficientStorageException, NotFoundException, IOException { checkStatus(response, sc); HttpEntity entity = response.getEntity(); if (entity != null) { if (!EntityUtils.toString(entity).isEmpty()) { - throw new InternalException("No http entity expected in response"); + throw new InternalException( + "No http entity expected in response"); } } } - public URL getIcatUrl(int sc) throws InternalException, ParseException, NotImplementedException { + public URL getIcatUrl(int sc) + throws InternalException, ParseException, NotImplementedException { URI uri = getUri(getUriBuilder("getIcatUrl")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return new URL(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException + | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public InputStream getData(String sessionId, DataSelection data, Flag flags, long offset, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, - InternalException, DataNotOnlineException { + public InputStream getData(String sessionId, DataSelection data, Flag flags, + long offset, Integer sc) throws NotImplementedException, + BadRequestException, InsufficientPrivilegesException, + NotFoundException, InternalException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("getData"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -330,7 +353,8 @@ public InputStream getData(String sessionId, DataSelection data, Flag flags, lon } public InputStream getData(String preparedId, long offset, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("getData"); @@ -372,7 +396,8 @@ public InputStream getData(String preparedId, long offset, Integer sc) } public ServiceStatus getServiceStatus(String sessionId, Integer sc) - throws InternalException, InsufficientPrivilegesException, NotImplementedException { + throws InternalException, InsufficientPrivilegesException, + NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getServiceStatus"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -384,7 +409,8 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); String result = getString(response, sc); - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json + .createReader(new StringReader(result))) { ServiceStatus serviceStatus = new ServiceStatus(); JsonObject rootNode = jsonReader.readObject(); for (JsonValue on : rootNode.getJsonArray("opsQueue")) { @@ -394,7 +420,8 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) } serviceStatus.setLockedCount(rootNode.getInt("lockCount")); for (JsonValue lock : rootNode.getJsonArray("locks")) { - Long dsId = ((JsonObject) lock).getJsonNumber("id").longValueExact(); + Long dsId = ((JsonObject) lock).getJsonNumber("id") + .longValueExact(); serviceStatus.storeLockedDs(dsId); } for (JsonValue num : rootNode.getJsonArray("failures")) { @@ -403,21 +430,25 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) } return serviceStatus; } catch (JsonException e) { - throw new InternalException( - "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); + throw new InternalException("TestingClient " + e.getClass() + + " " + e.getMessage() + " from " + result); } - } catch (InsufficientStorageException | DataNotOnlineException | InternalException | BadRequestException - | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException + | InternalException | BadRequestException + | NotFoundException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public long getSize(String sessionId, DataSelection data, int sc) throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, NotImplementedException { + public long getSize(String sessionId, DataSelection data, int sc) + throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, + NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getSize"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -431,16 +462,19 @@ public long getSize(String sessionId, DataSelection data, int sc) throws BadRequ try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Long.parseLong(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException + | DataNotOnlineException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public long getSize(String preparedId, int sc) throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, NotImplementedException { + public long getSize(String preparedId, int sc) throws BadRequestException, + NotFoundException, InsufficientPrivilegesException, + InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getSize"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); @@ -450,16 +484,20 @@ public long getSize(String preparedId, int sc) throws BadRequestException, NotFo HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { return Long.parseLong(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException + | DataNotOnlineException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public Status getStatus(String sessionId, DataSelection data, Integer sc) throws BadRequestException, - NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { + public Status getStatus(String sessionId, DataSelection data, Integer sc) + throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, + NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getStatus"); if (sessionId != null) { @@ -477,7 +515,8 @@ public Status getStatus(String sessionId, DataSelection data, Integer sc) throws checkResponseConformity(response); return Status.valueOf(getString(response, sc)); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -485,8 +524,10 @@ public Status getStatus(String sessionId, DataSelection data, Integer sc) throws } - public Status getStatus(String preparedId, Integer sc) throws BadRequestException, - NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { + public Status getStatus(String preparedId, Integer sc) + throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, + NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getStatus"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); @@ -498,7 +539,8 @@ public Status getStatus(String preparedId, Integer sc) throws BadRequestExceptio try (CloseableHttpResponse response = httpclient.execute(httpGet)) { return Status.valueOf(getString(response, sc)); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -506,8 +548,10 @@ public Status getStatus(String preparedId, Integer sc) throws BadRequestExceptio } - private String getString(CloseableHttpResponse response, Integer sc) throws InternalException, BadRequestException, - DataNotOnlineException, ParseException, InsufficientPrivilegesException, NotImplementedException, + private String getString(CloseableHttpResponse response, Integer sc) + throws InternalException, BadRequestException, + DataNotOnlineException, ParseException, + InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException, IOException { checkStatus(response, sc); HttpEntity entity = response.getEntity(); @@ -530,7 +574,8 @@ private URIBuilder getUriBuilder(String path) { } public boolean isPrepared(String preparedId, Integer sc) - throws BadRequestException, NotFoundException, InternalException, NotImplementedException { + throws BadRequestException, NotFoundException, InternalException, + NotImplementedException { URIBuilder uriBuilder = getUriBuilder("isPrepared"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); @@ -542,47 +587,56 @@ public boolean isPrepared(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException + | InsufficientPrivilegesException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public boolean isReadOnly(int sc) throws InternalException, NotImplementedException { + public boolean isReadOnly(int sc) + throws InternalException, NotImplementedException { URI uri = getUri(getUriBuilder("isReadOnly")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException + | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public boolean isTwoLevel(int sc) throws InternalException, NotImplementedException { + public boolean isTwoLevel(int sc) + throws InternalException, NotImplementedException { URI uri = getUri(getUriBuilder("isTwoLevel")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException + | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void ping(Integer sc) throws InternalException, NotFoundException, NotImplementedException { + public void ping(Integer sc) throws InternalException, NotFoundException, + NotImplementedException { URI uri = getUri(getUriBuilder("ping")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { @@ -591,26 +645,32 @@ public void ping(Integer sc) throws InternalException, NotFoundException, NotImp checkResponseConformity(response); String result = getString(response, sc); if (!result.equals("IdsOK")) { - throw new NotFoundException("Server gave invalid response: " + result); + throw new NotFoundException( + "Server gave invalid response: " + result); } - } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException + | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public String prepareData(String sessionId, DataSelection data, Flag flags, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, + public String prepareData(String sessionId, DataSelection data, Flag flags, + Integer sc) throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, InternalException { URI uri = getUri(getUriBuilder("prepareData")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue())); } if (flags == Flag.ZIP || flags == Flag.ZIP_AND_COMPRESS) { formparams.add(new BasicNameValuePair("zip", "true")); @@ -622,11 +682,13 @@ public String prepareData(String sessionId, DataSelection data, Flag flags, Inte HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try (CloseableHttpResponse response = httpclient + .execute(httpPost)) { checkResponseConformity(response); return getString(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -634,17 +696,23 @@ public String prepareData(String sessionId, DataSelection data, Flag flags, Inte } - public Long put(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, - String description, Integer sc) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, - NotImplementedException, DataNotOnlineException, InsufficientStorageException { - return put(sessionId, inputStream, name, datasetId, datafileFormatId, description, null, null, null, sc); + public Long put(String sessionId, InputStream inputStream, String name, + long datasetId, long datafileFormatId, String description, + Integer sc) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, NotImplementedException, + DataNotOnlineException, InsufficientStorageException { + return put(sessionId, inputStream, name, datasetId, datafileFormatId, + description, null, null, null, sc); } - public Long put(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, - String description, String doi, Date datafileCreateTime, Date datafileModTime, Integer sc) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, - NotImplementedException, DataNotOnlineException, InsufficientStorageException { + public Long put(String sessionId, InputStream inputStream, String name, + long datasetId, long datafileFormatId, String description, + String doi, Date datafileCreateTime, Date datafileModTime, + Integer sc) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, NotImplementedException, + DataNotOnlineException, InsufficientStorageException { if (inputStream == null) { throw new BadRequestException("Input stream is null"); @@ -652,8 +720,10 @@ public Long put(String sessionId, InputStream inputStream, String name, long dat CRC32 crc = new CRC32(); inputStream = new CheckedInputStream(inputStream, crc); URIBuilder uriBuilder = getUriBuilder("put"); - uriBuilder.setParameter(RequestIdNames.sessionId, sessionId).setParameter("name", name) - .setParameter("datafileFormatId", Long.toString(datafileFormatId)) + uriBuilder.setParameter(RequestIdNames.sessionId, sessionId) + .setParameter("name", name) + .setParameter("datafileFormatId", + Long.toString(datafileFormatId)) .setParameter("datasetId", Long.toString(datasetId)); if (description != null) { uriBuilder.setParameter("description", description); @@ -662,39 +732,49 @@ public Long put(String sessionId, InputStream inputStream, String name, long dat uriBuilder.setParameter("doi", doi); } if (datafileCreateTime != null) { - uriBuilder.setParameter("datafileCreateTime", Long.toString(datafileCreateTime.getTime())); + uriBuilder.setParameter("datafileCreateTime", + Long.toString(datafileCreateTime.getTime())); } if (datafileModTime != null) { - uriBuilder.setParameter("datafileModTime", Long.toString(datafileModTime.getTime())); + uriBuilder.setParameter("datafileModTime", + Long.toString(datafileModTime.getTime())); } URI uri = getUri(uriBuilder); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPut httpPut = new HttpPut(uri); - httpPut.setEntity(new InputStreamEntity(inputStream, ContentType.APPLICATION_OCTET_STREAM)); + httpPut.setEntity(new InputStreamEntity(inputStream, + ContentType.APPLICATION_OCTET_STREAM)); try (CloseableHttpResponse response = httpclient.execute(httpPut)) { String result = getString(response, sc); checkResponseConformity(response); - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json + .createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); - if (rootNode.getJsonNumber("checksum").longValueExact() != crc.getValue()) { - throw new InternalException("Error uploading - the checksum was not as expected"); + if (rootNode.getJsonNumber("checksum").longValueExact() != crc + .getValue()) { + throw new InternalException( + "Error uploading - the checksum was not as expected"); } return rootNode.getJsonNumber("id").longValueExact(); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (NumberFormatException e) { - throw new InternalException("Web service call did not return a valid Long value"); + throw new InternalException( + "Web service call did not return a valid Long value"); } } - public Long putAsPost(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, - String description, String doi, Date datafileCreateTime, Date datafileModTime, boolean wrap, Integer sc) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, - NotImplementedException, DataNotOnlineException, InsufficientStorageException { + public Long putAsPost(String sessionId, InputStream inputStream, + String name, long datasetId, long datafileFormatId, + String description, String doi, Date datafileCreateTime, + Date datafileModTime, boolean wrap, Integer sc) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, NotImplementedException, + DataNotOnlineException, InsufficientStorageException { if (inputStream == null) { throw new BadRequestException("Input stream is null"); @@ -703,29 +783,40 @@ public Long putAsPost(String sessionId, InputStream inputStream, String name, lo inputStream = new CheckedInputStream(inputStream, crc); URI uri = getUri(getUriBuilder("put")); - MultipartEntityBuilder reqEntityBuilder = MultipartEntityBuilder.create() - .addPart(RequestIdNames.sessionId, new StringBody(sessionId, ContentType.TEXT_PLAIN)) - .addPart("datafileFormatId", new StringBody(Long.toString(datafileFormatId), ContentType.TEXT_PLAIN)) + MultipartEntityBuilder reqEntityBuilder = MultipartEntityBuilder + .create() + .addPart(RequestIdNames.sessionId, + new StringBody(sessionId, ContentType.TEXT_PLAIN)) + .addPart("datafileFormatId", + new StringBody(Long.toString(datafileFormatId), + ContentType.TEXT_PLAIN)) .addPart("name", new StringBody(name, ContentType.TEXT_PLAIN)) - .addPart("datasetId", new StringBody(Long.toString(datasetId), ContentType.TEXT_PLAIN)); + .addPart("datasetId", new StringBody(Long.toString(datasetId), + ContentType.TEXT_PLAIN)); if (description != null) { - reqEntityBuilder.addPart("description", new StringBody(description, ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart("description", + new StringBody(description, ContentType.TEXT_PLAIN)); } if (doi != null) { - reqEntityBuilder.addPart("doi", new StringBody(doi, ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart("doi", + new StringBody(doi, ContentType.TEXT_PLAIN)); } if (datafileCreateTime != null) { reqEntityBuilder.addPart("datafileCreateTime", - new StringBody(Long.toString(datafileCreateTime.getTime()), ContentType.TEXT_PLAIN)); + new StringBody(Long.toString(datafileCreateTime.getTime()), + ContentType.TEXT_PLAIN)); } if (datafileModTime != null) { reqEntityBuilder.addPart("datafileModTime", - new StringBody(Long.toString(datafileModTime.getTime()), ContentType.TEXT_PLAIN)); + new StringBody(Long.toString(datafileModTime.getTime()), + ContentType.TEXT_PLAIN)); } if (wrap) { - reqEntityBuilder.addPart("wrap", new StringBody("true", ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart("wrap", + new StringBody("true", ContentType.TEXT_PLAIN)); } - InputStreamBody body = new InputStreamBody(new BufferedInputStream(inputStream), + InputStreamBody body = new InputStreamBody( + new BufferedInputStream(inputStream), ContentType.APPLICATION_OCTET_STREAM, "unreliable"); HttpEntity entity = reqEntityBuilder.addPart("file", body).build(); @@ -739,40 +830,51 @@ public Long putAsPost(String sessionId, InputStream inputStream, String name, lo String prefix = ""; if (result.startsWith(prefix)) { - result = result.substring(prefix.length(), result.length() - suffix.length()); + result = result.substring(prefix.length(), + result.length() - suffix.length()); } - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json + .createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); - if (rootNode.getJsonNumber("checksum").longValueExact() != crc.getValue()) { - throw new InternalException("Error uploading - the checksum was not as expected"); + if (rootNode.getJsonNumber("checksum").longValueExact() != crc + .getValue()) { + throw new InternalException( + "Error uploading - the checksum was not as expected"); } return rootNode.getJsonNumber("id").longValueExact(); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (NumberFormatException e) { - throw new InternalException("Web service call did not return a valid Long value"); + throw new InternalException( + "Web service call did not return a valid Long value"); } } - public void restore(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, - BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { + public void restore(String sessionId, DataSelection data, Integer sc) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, + NotFoundException { URI uri = getUri(getUriBuilder("restore")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try (CloseableHttpResponse response = httpclient + .execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -792,7 +894,8 @@ public List getDatafileIds(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { String result = getString(response, sc); checkResponseConformity(response); - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json + .createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); List ids = new ArrayList<>(); @@ -802,21 +905,24 @@ public List getDatafileIds(String preparedId, Integer sc) } return ids; } catch (JsonException e) { - throw new InternalException( - "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); + throw new InternalException("TestingClient " + e.getClass() + + " " + e.getMessage() + " from " + result); } - } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException - | NotImplementedException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException + | InsufficientPrivilegesException + | NotImplementedException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public List getDatafileIds(String sessionId, DataSelection data, Integer sc) - throws InternalException, BadRequestException, ParseException, NotFoundException { + public List getDatafileIds(String sessionId, DataSelection data, + Integer sc) throws InternalException, BadRequestException, + ParseException, NotFoundException { URIBuilder uriBuilder = getUriBuilder("getDatafileIds"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); for (Entry entry : data.getParameters().entrySet()) { @@ -830,7 +936,8 @@ public List getDatafileIds(String sessionId, DataSelection data, Integer s try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); String result = getString(response, sc); - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json + .createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); List ids = new ArrayList<>(); @@ -840,97 +947,122 @@ public List getDatafileIds(String sessionId, DataSelection data, Integer s } return ids; } catch (JsonException e) { - throw new InternalException( - "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); + throw new InternalException("TestingClient " + e.getClass() + + " " + e.getMessage() + " from " + result); } - } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException - | NotImplementedException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException + | InsufficientPrivilegesException + | NotImplementedException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void reset(String preparedId, Integer sc) throws InternalException, BadRequestException, ParseException, - InsufficientPrivilegesException, NotImplementedException, NotFoundException { + public void reset(String preparedId, Integer sc) + throws InternalException, BadRequestException, ParseException, + InsufficientPrivilegesException, NotImplementedException, + NotFoundException { URI uri = getUri(getUriBuilder("reset")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.preparedId, preparedId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.preparedId, preparedId)); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try (CloseableHttpResponse response = httpclient + .execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void reset(String sessionId, DataSelection data, int sc) throws InternalException, BadRequestException, - ParseException, InsufficientPrivilegesException, NotImplementedException, NotFoundException { + public void reset(String sessionId, DataSelection data, int sc) + throws InternalException, BadRequestException, ParseException, + InsufficientPrivilegesException, NotImplementedException, + NotFoundException { URI uri = getUri(getUriBuilder("reset")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try (CloseableHttpResponse response = httpclient + .execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void write(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, - BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, - DataNotOnlineException { + public void write(String sessionId, DataSelection data, Integer sc) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, + NotFoundException, DataNotOnlineException { URI uri = getUri(getUriBuilder("write")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(formparams)); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try (CloseableHttpResponse response = httpclient + .execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - private void checkResponseConformity(HttpResponse response) throws InternalException { + private void checkResponseConformity(HttpResponse response) + throws InternalException { StatusLine status = response.getStatusLine(); var statusCode = status.getStatusCode(); - if(statusCode == 200 && response.getEntity() != null) { - //we have a status of 200 and a payload. Check for header conformity - Boolean chunked = response.getFirstHeader("Transfer-Encoding") != null ? response.getFirstHeader("Transfer-Encoding").getValue().contains("chunked") - : false; - - if ( (response.getFirstHeader("Content-Length") == null && !chunked) - || (response.getFirstHeader("Content-Length") != null && chunked ) ) { - - throw new InternalException("Responses with payload should either contain the Content-Length header or the 'Transfer-Encoding: chunked' header. It needs exactly one of them."); + if (statusCode == 200 && response.getEntity() != null) { + // we have a status of 200 and a payload. Check for header + // conformity + Boolean chunked = response + .getFirstHeader("Transfer-Encoding") != null + ? response.getFirstHeader("Transfer-Encoding") + .getValue().contains("chunked") + : false; + + if ((response.getFirstHeader("Content-Length") == null && !chunked) + || (response.getFirstHeader("Content-Length") != null + && chunked)) { + + throw new InternalException( + "Responses with payload should either contain the Content-Length header or the 'Transfer-Encoding: chunked' header. It needs exactly one of them."); } } } From a3e8763056d8965e0129b9c0ffec4de775b1e417 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Wed, 12 Jun 2024 08:45:33 +0200 Subject: [PATCH 13/19] Revert "linted code to test ci check" This reverts commit 55ec85320f5274d512454fc833ad9c98678f78f9. --- .../java/org/icatproject/ids/IdsService.java | 700 +++++++----------- src/main/java/org/icatproject/ids/Tidier.java | 138 ++-- .../icatproject/ids/enums/RequestType.java | 19 +- .../ids/exceptions/IdsExceptionMapper.java | 9 +- .../exceptions/NotFoundExceptionMapper.java | 13 +- .../exceptions/RuntimeExceptionMapper.java | 13 +- .../FiniteStateMachine.java | 70 +- ...niteStateMachineForSingleLevelStorage.java | 17 +- ...iteStateMachineForStorageUnitDatafile.java | 201 ++--- ...niteStateMachineForStorageUnitDataset.java | 128 ++-- .../ids/helpers/CORSResponseFilter.java | 4 +- .../icatproject/ids/helpers/Constants.java | 4 +- .../ids/helpers/LocationHelper.java | 10 +- .../ids/helpers/RangeOutputStream.java | 8 +- .../java/org/icatproject/ids/helpers/SO.java | 43 +- .../ids/helpers/ValueContainer.java | 92 +-- .../icatproject/ids/models/DataInfoBase.java | 4 +- .../icatproject/ids/models/DataSelection.java | 16 +- .../icatproject/ids/models/DatafileInfo.java | 26 +- .../icatproject/ids/models/DatasetInfo.java | 43 +- .../org/icatproject/ids/models/Prepared.java | 65 +- .../ids/requestHandlers/ArchiveHandler.java | 13 +- .../ids/requestHandlers/DeleteHandler.java | 29 +- .../GetDataFileIdsHandler.java | 20 +- .../ids/requestHandlers/GetDataHandler.java | 115 +-- .../requestHandlers/GetIcatUrlHandler.java | 10 +- .../GetServiceStatusHandler.java | 18 +- .../ids/requestHandlers/GetStatusHandler.java | 18 +- .../requestHandlers/IsPreparedHandler.java | 24 +- .../requestHandlers/IsReadOnlyHandler.java | 8 +- .../requestHandlers/IsTwoLevelHandler.java | 8 +- .../requestHandlers/PrepareDataHandler.java | 39 +- .../ids/requestHandlers/PutHandler.java | 194 ++--- .../ids/requestHandlers/ResetHandler.java | 18 +- .../ids/requestHandlers/RestoreHandler.java | 14 +- .../ids/requestHandlers/WriteHandler.java | 24 +- .../base/DataControllerBase.java | 40 +- .../base/DataRequestHandler.java | 54 +- .../base/PreparedDataController.java | 26 +- .../base/RequestHandlerBase.java | 73 +- .../base/UnpreparedDataController.java | 39 +- .../getSizeHandlers/GetSizeHandler.java | 35 +- .../GetSizeHandlerForFastProcessing.java | 71 +- .../icatproject/ids/services/ICATGetter.java | 22 +- .../icatproject/ids/services/IcatReader.java | 12 +- .../icatproject/ids/services/LockManager.java | 18 +- .../ids/services/PropertyHandler.java | 108 +-- .../ids/services/ServiceProvider.java | 36 +- .../icatproject/ids/services/Transmitter.java | 9 +- .../ids/services/UnfinishedWorkService.java | 66 +- .../DataSelectionService.java | 151 ++-- .../DataSelectionServiceFactory.java | 360 +++------ ...SelectionServiceForSingleLevelStorage.java | 42 +- ...electionServiceForStorageUnitDatafile.java | 31 +- ...SelectionServiceForStorageUnitDataset.java | 37 +- .../icatproject/ids/thread/DfArchiver.java | 16 +- .../org/icatproject/ids/thread/DfDeleter.java | 10 +- .../icatproject/ids/thread/DfRestorer.java | 18 +- .../org/icatproject/ids/thread/DfWriter.java | 15 +- .../icatproject/ids/thread/DsArchiver.java | 12 +- .../icatproject/ids/thread/DsRestorer.java | 43 +- .../org/icatproject/ids/thread/DsWriter.java | 37 +- .../icatproject/ids/DataSelectionDevTest.java | 39 +- .../java/org/icatproject/ids/DigestTest.java | 9 +- .../icatproject/ids/PreparePackingTest.java | 17 +- .../ids/RangeOutputStreamTest.java | 4 +- .../org/icatproject/ids/TestICATGetter.java | 9 +- .../java/org/icatproject/ids/TestUtils.java | 7 +- .../icatproject/ids/TestValueContainer.java | 20 +- .../java/org/icatproject/ids/TestZipping.java | 9 +- .../java/org/icatproject/ids/TidierTest.java | 9 +- .../org/icatproject/ids/TransmittingTest.java | 157 ++-- .../org/icatproject/ids/ValidaterTest.java | 1 + .../icatproject/ids/integration/BaseTest.java | 308 +++----- .../integration/one/BogusDatafileTest.java | 36 +- .../ids/integration/one/DeleteTest.java | 15 +- .../integration/one/GetDataExplicitTest.java | 102 +-- .../one/GetDataForPreparedIdTest.java | 50 +- .../one/GetStatusExplicitTest.java | 18 +- .../one/GetStatusForPreparedIdTest.java | 14 +- .../ids/integration/one/MiscTest.java | 17 +- .../ids/integration/one/PerformanceTest.java | 21 +- .../ids/integration/one/PrepareDataTest.java | 47 +- .../ids/integration/one/PutTest.java | 32 +- .../ids/integration/one/WriteTest.java | 3 +- .../ids/integration/two/ArchiveTest.java | 6 +- .../integration/two/BogusDatafileTest.java | 66 +- .../ids/integration/two/DeleteTest.java | 15 +- .../integration/two/GetDataExplicitTest.java | 71 +- .../two/GetDataForPreparedIdTest.java | 46 +- .../two/GetStatusExplicitTest.java | 16 +- .../two/GetStatusForPreparedIdTest.java | 14 +- .../ids/integration/two/MiscTest.java | 14 +- .../ids/integration/two/PrepareDataTest.java | 74 +- .../ids/integration/two/PutTest.java | 27 +- .../integration/two/RestoreErrorsTest.java | 12 +- .../ids/integration/two/RestoreTest.java | 16 +- .../ids/integration/two/WriteTest.java | 18 +- .../ids/integration/twodf/ArchiveTest.java | 6 +- .../integration/twodf/BogusDatafileTest.java | 66 +- .../ids/integration/twodf/DeleteTest.java | 15 +- .../twodf/GetDataExplicitTest.java | 71 +- .../twodf/GetDataForPreparedIdTest.java | 48 +- .../twodf/GetStatusExplicitTest.java | 19 +- .../twodf/GetStatusForPreparedIdTest.java | 14 +- .../ids/integration/twodf/MiscTest.java | 14 +- .../integration/twodf/PrepareDataTest.java | 74 +- .../ids/integration/twodf/PutTest.java | 27 +- .../ids/integration/twodf/QueueTest.java | 10 +- .../ids/integration/twodf/RestoreTest.java | 16 +- .../ids/integration/twodf/WriteTest.java | 18 +- .../ids/integration/util/Setup.java | 24 +- .../util/client/HttpInputStream.java | 3 +- .../util/client/TestingClient.java | 450 ++++------- 114 files changed, 2113 insertions(+), 3657 deletions(-) diff --git a/src/main/java/org/icatproject/ids/IdsService.java b/src/main/java/org/icatproject/ids/IdsService.java index f5f6a7be..0d517fa9 100644 --- a/src/main/java/org/icatproject/ids/IdsService.java +++ b/src/main/java/org/icatproject/ids/IdsService.java @@ -6,6 +6,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; + import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import jakarta.ejb.EJB; @@ -70,8 +71,7 @@ @Stateless public class IdsService { - private final static Logger logger = LoggerFactory - .getLogger(IdsService.class); + private final static Logger logger = LoggerFactory.getLogger(IdsService.class); @EJB Transmitter transmitter; @@ -89,6 +89,7 @@ public class IdsService { private UnfinishedWorkService unfinishedWorkService; + @PostConstruct private void init() { @@ -97,19 +98,15 @@ private void init() { logger.info("creating IdsService"); - FiniteStateMachine.createInstance(reader, lockManager, - PropertyHandler.getInstance().getStorageUnit()); + FiniteStateMachine.createInstance(reader, lockManager, PropertyHandler.getInstance().getStorageUnit()); this.fsm = FiniteStateMachine.getInstance(); this.fsm.init(); - ServiceProvider.createInstance(transmitter, fsm, lockManager, - reader); + ServiceProvider.createInstance(transmitter, fsm, lockManager, reader); - var propertyHandler = ServiceProvider.getInstance() - .getPropertyHandler(); + var propertyHandler = ServiceProvider.getInstance().getPropertyHandler(); var archiveStorage = propertyHandler.getArchiveStorage(); var twoLevel = archiveStorage != null; - var preparedDir = propertyHandler.getCacheDir() - .resolve("prepared"); + var preparedDir = propertyHandler.getCacheDir().resolve("prepared"); Files.createDirectories(preparedDir); this.unfinishedWorkService = new UnfinishedWorkService(); @@ -123,17 +120,13 @@ private void init() { java.nio.file.Path datasetDir; if (twoLevel) { - datasetDir = propertyHandler.getCacheDir() - .resolve("dataset"); - var markerDir = propertyHandler.getCacheDir() - .resolve("marker"); + datasetDir = propertyHandler.getCacheDir().resolve("dataset"); + var markerDir = propertyHandler.getCacheDir().resolve("marker"); if (!inited) { Files.createDirectories(datasetDir); Files.createDirectories(markerDir); - this.unfinishedWorkService - .restartUnfinishedWork(markerDir, key); - this.unfinishedWorkService - .cleanDatasetCache(datasetDir); + this.unfinishedWorkService.restartUnfinishedWork(markerDir, key); + this.unfinishedWorkService.cleanDatasetCache(datasetDir); } } @@ -143,53 +136,47 @@ private void init() { } } catch (Throwable e) { logger.error("Won't start ", e); - throw new RuntimeException("IdsService reports " + e.getClass() - + " " + e.getMessage()); + throw new RuntimeException("IdsService reports " + e.getClass() + " " + e.getMessage()); } } + @PreDestroy private void exit() { this.fsm.exit(); logger.info("destroyed IdsService"); } + /** * Archive data specified by the investigationIds, datasetIds and * datafileIds specified along with a sessionId. If two level storage is not * in use this has no effect. * * @title archive - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds If present, a comma separated list of - * investigation id values - * @param datasetIds If present, a comma separated list of data set id - * values or null - * @param datafileIds If present, a comma separated list of datafile id - * values. + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds If present, a comma separated list of investigation id values + * @param datasetIds If present, a comma separated list of data set id values or + * null + * @param datafileIds If present, a comma separated list of datafile id values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException * @throws InternalException * @throws NotFoundException - * @throws DataNotOnlineException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("archive") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void archive(@Context HttpServletRequest request, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, + public void archive(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new ArchiveHandler(request.getRemoteAddr(), sessionId, - investigationIds, datasetIds, datafileIds); + var handler = new ArchiveHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } @@ -198,14 +185,11 @@ public void archive(@Context HttpServletRequest request, * specified along with a sessionId. * * @title delete - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds If present, a comma separated list of - * investigation id values - * @param datasetIds If present, a comma separated list of data set id - * values or null - * @param datafileIds If present, a comma separated list of datafile id - * values. + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds If present, a comma separated list of investigation id values + * @param datasetIds If present, a comma separated list of data set id values or + * null + * @param datafileIds If present, a comma separated list of datafile id values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException @@ -216,20 +200,16 @@ public void archive(@Context HttpServletRequest request, */ @DELETE @Path("delete") - public void delete(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, - InternalException, DataNotOnlineException { - - var handler = new DeleteHandler(request.getRemoteAddr(), sessionId, - investigationIds, datasetIds, datafileIds); + public void delete(@Context HttpServletRequest request, @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds) throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { + + var handler = new DeleteHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } + /** * Return the version of the server * @@ -242,8 +222,7 @@ public void delete(@Context HttpServletRequest request, public String getVersion() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen.writeStartObject().write("version", Constants.API_VERSION) - .writeEnd(); + gen.writeStartObject().write("version", Constants.API_VERSION).writeEnd(); gen.close(); return baos.toString(); } @@ -255,58 +234,43 @@ public String getVersion() { * set. If preparedId is set the compress and zip arguments are not used. * * @title getData - * @param preparedId A valid preparedId returned by a call to - * prepareData - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds A comma separated list of investigation id - * values. + * @param preparedId A valid preparedId returned by a call to prepareData + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds A comma separated list of investigation id values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of data file id values. - * @param compress If true use default compression otherwise no - * compression. This only applies if preparedId is - * not set and if the results are being zipped. - * @param zip If true the data should be zipped. If multiple - * files are requested (or could be because a - * datasetId or investigationId has been specified) - * the data are zipped regardless of the + * @param compress If true use default compression otherwise no compression. This + * only applies if preparedId is not set and if the results are + * being zipped. + * @param zip If true the data should be zipped. If multiple files are + * requested (or could be because a datasetId or investigationId + * has been specified) the data are zipped regardless of the * specification of this flag. * @param outname The file name to put in the returned header - * "ContentDisposition". If it does not end in .zip - * but it is a zip file then a ".zip" will be - * appended. - * @param range A range header which must match "bytes=(\\d+)-" - * to specify an offset i.e. to skip a number of - * bytes. + * "ContentDisposition". If it does not end in .zip but it is a + * zip file then a ".zip" will be appended. + * @param range A range header which must match "bytes=(\\d+)-" to specify an + * offset i.e. to skip a number of bytes. * @return a stream of json data. * @throws BadRequestException * @throws NotFoundException * @throws InternalException * @throws InsufficientPrivilegesException * @throws DataNotOnlineException - * @throws NotImplementedException + * @throws NotImplementedException * @statuscode 200 To indicate success */ @GET @Path("getData") @Produces(MediaType.APPLICATION_OCTET_STREAM) - public Response getData(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds, - @QueryParam("compress") boolean compress, - @QueryParam("zip") boolean zip, - @QueryParam("outname") String outname, - @HeaderParam("Range") String range) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, DataNotOnlineException, - NotImplementedException { - - var handler = new GetDataHandler(request.getRemoteAddr(), preparedId, - sessionId, investigationIds, datasetIds, datafileIds, compress, - zip, outname, range); + public Response getData(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds, + @QueryParam("compress") boolean compress, @QueryParam("zip") boolean zip, + @QueryParam("outname") String outname, @HeaderParam("Range") String range) throws BadRequestException, + NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + + var handler = new GetDataHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds, compress, zip, outname, range); return handler.handle().getResponse(); } @@ -317,12 +281,9 @@ public Response getData(@Context HttpServletRequest request, * set. * * @title getDatafileIds - * @param preparedId A valid preparedId returned by a call to - * prepareData - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds A comma separated list of investigation id - * values. + * @param preparedId A valid preparedId returned by a call to prepareData + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds A comma separated list of investigation id values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of datafile id values. * @return a list of id values @@ -330,26 +291,19 @@ public Response getData(@Context HttpServletRequest request, * @throws InternalException * @throws NotFoundException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getDatafileIds") @Produces(MediaType.APPLICATION_JSON) - public String getDatafileIds(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, InternalException, NotFoundException, - InsufficientPrivilegesException, DataNotOnlineException, - NotImplementedException { - - var handler = new GetDataFileIdsHandler(request.getRemoteAddr(), - preparedId, sessionId, investigationIds, datasetIds, - datafileIds); + public String getDatafileIds(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + + var handler = new GetDataFileIdsHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); return handler.handle().getString(); } @@ -359,21 +313,18 @@ public String getDatafileIds(@Context HttpServletRequest request, * obtained. * * @return the url of the icat server - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("getIcatUrl") @Produces(MediaType.TEXT_PLAIN) - public String getIcatUrl(@Context HttpServletRequest request) - throws InternalException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public String getIcatUrl(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var handler = new GetIcatUrlHandler(request.getRemoteAddr()); return handler.handle().getString(); @@ -384,28 +335,24 @@ public String getIcatUrl(@Context HttpServletRequest request) * privileged to use this call. * * @title getServiceStatus - * @param sessionId A valid ICAT session ID of a user in the IDS - * rootUserNames set. + * @param sessionId A valid ICAT session ID of a user in the IDS rootUserNames + * set. * @return a json string. * @throws InternalException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws BadRequestException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws BadRequestException * @statuscode 200 To indicate success */ @GET @Path("getServiceStatus") @Produces(MediaType.APPLICATION_JSON) - public String getServiceStatus(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.sessionId) String sessionId) - throws InternalException, InsufficientPrivilegesException, - BadRequestException, NotFoundException, DataNotOnlineException, - NotImplementedException { - - var handler = new GetServiceStatusHandler(request.getRemoteAddr(), - sessionId); + public String getServiceStatus(@Context HttpServletRequest request, @QueryParam(RequestIdNames.sessionId) String sessionId) + throws InternalException, InsufficientPrivilegesException, BadRequestException, NotFoundException, DataNotOnlineException, NotImplementedException { + + var handler = new GetServiceStatusHandler(request.getRemoteAddr(), sessionId); return handler.handle().getString(); } @@ -414,53 +361,41 @@ public String getServiceStatus(@Context HttpServletRequest request, * investigationIds, datasetIds and datafileIds along with a sessionId. * * @title getSize - * @param preparedId A valid preparedId returned by a call to - * prepareData - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds If present, a comma separated list of - * investigation id values - * @param datasetIds If present, a comma separated list of data set id - * values or null - * @param datafileIds If present, a comma separated list of data file - * id values. + * @param preparedId A valid preparedId returned by a call to prepareData + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds If present, a comma separated list of investigation id values + * @param datasetIds If present, a comma separated list of data set id values or + * null + * @param datafileIds If present, a comma separated list of data file id values. * @return the size in bytes * @throws BadRequestException * @throws NotFoundException * @throws InsufficientPrivilegesException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getSize") @Produces(MediaType.TEXT_PLAIN) - public long getSize(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, - DataNotOnlineException, NotImplementedException { + public long getSize(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { + var result = ValueContainer.getInvalid(); // trying fast computation - if (sessionId != null) { - var fastHandler = new GetSizeHandlerForFastProcessing( - request.getRemoteAddr(), sessionId, investigationIds, - datasetIds, datafileIds); + if(sessionId != null) { + var fastHandler = new GetSizeHandlerForFastProcessing(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); result = fastHandler.handle(); } // otherwise normal computation - if (result.isInvalid()) { - var handler = new GetSizeHandler(request.getRemoteAddr(), - preparedId, sessionId, investigationIds, datasetIds, - datafileIds); + if(result.isInvalid()) { + var handler = new GetSizeHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); result = handler.handle(); } @@ -472,61 +407,49 @@ public long getSize(@Context HttpServletRequest request, * investigationIds, datasetIds and datafileIds along with a sessionId. * * @title getStatus - * @param preparedId A valid preparedId returned by a call to - * prepareData - * @param sessionId A sessionId returned by a call to the icat - * server. If the sessionId is omitted or null the - * ids reader account will be used which has read - * access to all data. - * @param investigationIds If present, a comma separated list of - * investigation id values - * @param datasetIds If present, a comma separated list of data set id - * values or null - * @param datafileIds If present, a comma separated list of data file - * id values. + * @param preparedId A valid preparedId returned by a call to prepareData + * @param sessionId A sessionId returned by a call to the icat server. If the + * sessionId is omitted or null the ids reader account will be + * used which has read access to all data. + * @param investigationIds If present, a comma separated list of investigation id values + * @param datasetIds If present, a comma separated list of data set id values or + * null + * @param datafileIds If present, a comma separated list of data file id values. * @return a string with "ONLINE" if all data are online, "RESTORING" if one - * or more files are in the process of being restored but none are - * archived and no restoration has been requested or "ARCHIVED" if - * one or more files are archived and and no restoration has been - * requested. + * or more files are in the process of being restored but none are + * archived and no restoration has been requested or "ARCHIVED" if + * one or more files are archived and and no restoration has been + * requested. * @throws BadRequestException * @throws NotFoundException * @throws InsufficientPrivilegesException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getStatus") @Produces(MediaType.TEXT_PLAIN) - public String getStatus(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, - DataNotOnlineException, NotImplementedException { - - // special case for getStatus request: getting status is possible - // without authentification + public String getStatus(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { + + // special case for getStatus request: getting status is possible without authentification if (sessionId == null && preparedId == null) { try { - sessionId = ServiceProvider.getInstance().getIcatReader() - .getSessionId(); + sessionId = ServiceProvider.getInstance().getIcatReader().getSessionId(); } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } } - var handler = new GetStatusHandler(request.getRemoteAddr(), preparedId, - sessionId, investigationIds, datasetIds, datafileIds); + var handler = new GetStatusHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); return handler.handle().getString(); } + /** * Returns true if all the data files are ready to be downloaded. As a side * effect, if any data files are archived and no restoration has been @@ -544,46 +467,39 @@ public String getStatus(@Context HttpServletRequest request, * @throws BadRequestException * @throws NotFoundException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws InsufficientPrivilegesException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws InsufficientPrivilegesException * @statuscode 200 To indicate success */ @GET @Path("isPrepared") @Produces(MediaType.TEXT_PLAIN) - public boolean isPrepared(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, DataNotOnlineException, - NotImplementedException { - - var handler = new IsPreparedHandler(request.getRemoteAddr(), - preparedId); + public boolean isPrepared(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + + var handler = new IsPreparedHandler(request.getRemoteAddr(), preparedId); return handler.handle().getBool(); } /** - * An ids server can be configured to be read only. This returns thenew - * DfProcessQueue() readOnly status of the server. + * An ids server can be configured to be read only. This returns thenew DfProcessQueue() + * readOnly status of the server. * * @title isReadOnly * @return true if readonly, else false - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("isReadOnly") @Produces(MediaType.TEXT_PLAIN) - public boolean isReadOnly(@Context HttpServletRequest request) - throws InternalException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public boolean isReadOnly(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var handler = new IsReadOnlyHandler(request.getRemoteAddr()); return handler.handle().getBool(); @@ -595,21 +511,18 @@ public boolean isReadOnly(@Context HttpServletRequest request) * * @title isTwoLevel * @return true if twoLevel, else false - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("isTwoLevel") @Produces(MediaType.TEXT_PLAIN) - public boolean isTwoLevel(@Context HttpServletRequest request) - throws InternalException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public boolean isTwoLevel(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var handler = new IsTwoLevelHandler(request.getRemoteAddr()); return handler.handle().getBool(); @@ -638,45 +551,37 @@ public String ping() { * omitted, along with a sessionId. * * @title prepareData - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds A comma separated list of investigation id - * values. + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds A comma separated list of investigation id values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of datafile id values. - * @param compress If true use default compression otherwise no - * compression. This only applies if preparedId is - * not set and if the results are being zipped. - * @param zip If true the data should be zipped. If multiple - * files are requested (or could be because a - * datasetId or investigationId has been specified) - * the data are zipped regardless of the + * @param compress If true use default compression otherwise no compression. This + * only applies if preparedId is not set and if the results are + * being zipped. + * @param zip If true the data should be zipped. If multiple files are + * requested (or could be because a datasetId or investigationId + * has been specified) the data are zipped regardless of the * specification of this flag. * @return a string with the preparedId * @throws BadRequestException * @throws InsufficientPrivilegesException * @throws NotFoundException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("prepareData") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.TEXT_PLAIN) - public String prepareData(@Context HttpServletRequest request, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds, - @FormParam("compress") boolean compress, - @FormParam("zip") boolean zip) throws BadRequestException, - InsufficientPrivilegesException, NotFoundException, - InternalException, NotImplementedException, DataNotOnlineException { - - var handler = new PrepareDataHandler(request.getRemoteAddr(), sessionId, - investigationIds, datasetIds, datafileIds, compress, zip); + public String prepareData(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds, @FormParam("compress") boolean compress, + @FormParam("zip") boolean zip) + throws BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, NotImplementedException, DataNotOnlineException { + + var handler = new PrepareDataHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds, compress, zip); return handler.handle().getString(); } @@ -685,23 +590,17 @@ public String prepareData(@Context HttpServletRequest request, * * @title put * @param body The contents of the file to be stored - * @param sessionId A sessionId returned by a call to the icat - * server. + * @param sessionId A sessionId returned by a call to the icat server. * @param name A name to assign to the data file - * @param datafileFormatId The id of the data file format to associate - * with the data file - * @param datasetId The id of the data set to which the data file - * should be associated. - * @param description An optional description to associate with the - * data file - * @param doi An optional description to associate with the - * data file - * @param datafileCreateTime An optional datafileCreateTime to associate - * with the data file - * @param datafileModTime An optional datafileModTime to associate with - * the data file + * @param datafileFormatId The id of the data file format to associate with the data file + * @param datasetId The id of the data set to which the data file should be + * associated. + * @param description An optional description to associate with the data file + * @param doi An optional description to associate with the data file + * @param datafileCreateTime An optional datafileCreateTime to associate with the data file + * @param datafileModTime An optional datafileModTime to associate with the data file * @return a json object with attributes of "id", "checksum", "location" and - * "size"; + * "size"; * @throws BadRequestException * @throws NotFoundException * @throws InternalException @@ -715,21 +614,15 @@ public String prepareData(@Context HttpServletRequest request, @Consumes(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.APPLICATION_JSON) public Response put(@Context HttpServletRequest request, InputStream body, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("name") String name, - @QueryParam("datafileFormatId") String datafileFormatId, - @QueryParam("datasetId") String datasetId, - @QueryParam("description") String description, - @QueryParam("doi") String doi, - @QueryParam("datafileCreateTime") String datafileCreateTime, - @QueryParam("datafileModTime") String datafileModTime) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, NotImplementedException, - DataNotOnlineException { - - var handler = new PutHandler(request.getRemoteAddr(), sessionId, body, - name, datafileFormatId, datasetId, description, doi, - datafileCreateTime, datafileModTime, false, false); + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("name") String name, + @QueryParam("datafileFormatId") String datafileFormatId, @QueryParam("datasetId") String datasetId, + @QueryParam("description") String description, @QueryParam("doi") String doi, + @QueryParam("datafileCreateTime") String datafileCreateTime, + @QueryParam("datafileModTime") String datafileModTime) throws BadRequestException, NotFoundException, + InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { + + var handler = new PutHandler(request.getRemoteAddr(), sessionId, body, name, datafileFormatId, datasetId, + description, doi, datafileCreateTime, datafileModTime, false, false); return handler.handle().getResponse(); } @@ -743,7 +636,7 @@ public Response put(@Context HttpServletRequest request, InputStream body, * @title putAsPost * @param request * @return a json object with attributes of "id", "checksum", "location" and - * "size"; + * "size"; * @throws BadRequestException * @throws NotFoundException * @throws InternalException @@ -756,73 +649,65 @@ public Response put(@Context HttpServletRequest request, InputStream body, @Path("put") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) - public Response putAsPost(@Context HttpServletRequest request) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, NotImplementedException, - DataNotOnlineException { - try { - String sessionId = null; - String name = null; - String datafileFormatId = null; - String datasetId = null; - String description = null; - String doi = null; - String datafileCreateTime = null; - String datafileModTime = null; - Response result = null; - boolean wrap = false; - boolean padding = false; - - // Parse the request - for (Part part : request.getParts()) { - String fieldName = part.getName(); - InputStream stream = part.getInputStream(); - if (part.getSubmittedFileName() == null) { - String value = new String(stream.readAllBytes(), - StandardCharsets.UTF_8); - if (fieldName.equals("sessionId")) { - sessionId = value; - } else if (fieldName.equals("name")) { - name = value; - } else if (fieldName.equals("datafileFormatId")) { - datafileFormatId = value; - } else if (fieldName.equals("datasetId")) { - datasetId = value; - } else if (fieldName.equals("description")) { - description = value; - } else if (fieldName.equals("doi")) { - doi = value; - } else if (fieldName.equals("datafileCreateTime")) { - datafileCreateTime = value; - } else if (fieldName.equals("datafileModTime")) { - datafileModTime = value; - } else if (fieldName.equals("wrap")) { - wrap = (value != null - && value.toUpperCase().equals("TRUE")); - } else if (fieldName.equals("padding")) { - padding = (value != null - && value.toUpperCase().equals("TRUE")); - } else { - throw new BadRequestException("Form field " + fieldName - + "is not recognised"); - } - } else { - if (name == null) { - name = part.getSubmittedFileName(); + public Response putAsPost(@Context HttpServletRequest request) throws BadRequestException, NotFoundException, + InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { + try { + String sessionId = null; + String name = null; + String datafileFormatId = null; + String datasetId = null; + String description = null; + String doi = null; + String datafileCreateTime = null; + String datafileModTime = null; + Response result = null; + boolean wrap = false; + boolean padding = false; + + // Parse the request + for (Part part : request.getParts()) { + String fieldName = part.getName(); + InputStream stream = part.getInputStream(); + if (part.getSubmittedFileName() == null) { + String value = new String(stream.readAllBytes(), StandardCharsets.UTF_8); + if (fieldName.equals("sessionId")) { + sessionId = value; + } else if (fieldName.equals("name")) { + name = value; + } else if (fieldName.equals("datafileFormatId")) { + datafileFormatId = value; + } else if (fieldName.equals("datasetId")) { + datasetId = value; + } else if (fieldName.equals("description")) { + description = value; + } else if (fieldName.equals("doi")) { + doi = value; + } else if (fieldName.equals("datafileCreateTime")) { + datafileCreateTime = value; + } else if (fieldName.equals("datafileModTime")) { + datafileModTime = value; + } else if (fieldName.equals("wrap")) { + wrap = (value != null && value.toUpperCase().equals("TRUE")); + } else if (fieldName.equals("padding")) { + padding = (value != null && value.toUpperCase().equals("TRUE")); + } else { + throw new BadRequestException("Form field " + fieldName + "is not recognised"); + } + } else { + if (name == null) { + name = part.getSubmittedFileName(); + } + var handler = new PutHandler(request.getRemoteAddr(), sessionId, stream, name, datafileFormatId, datasetId, description, + doi, datafileCreateTime, datafileModTime, wrap, padding); + result = handler.handle().getResponse(); + } } - var handler = new PutHandler(request.getRemoteAddr(), - sessionId, stream, name, datafileFormatId, - datasetId, description, doi, datafileCreateTime, - datafileModTime, wrap, padding); - result = handler.handle().getResponse(); + return result; + } catch (IOException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (ServletException e) { + throw new BadRequestException("Multipart content expected"); } - } - return result; - } catch (IOException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); - } catch (ServletException e) { - throw new BadRequestException("Multipart content expected"); - } } /** @@ -837,12 +722,9 @@ public Response putAsPost(@Context HttpServletRequest request) * specified then investigationIds, datasetIds and datafileIds are not used. * * @title reset - * @param preparedId A valid preparedId returned by a call to - * prepareData - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds A comma separated list of investigation id - * values. + * @param preparedId A valid preparedId returned by a call to prepareData + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds A comma separated list of investigation id values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of data file id values. * @return a stream of json data. @@ -850,24 +732,18 @@ public Response putAsPost(@Context HttpServletRequest request) * @throws NotFoundException * @throws InternalException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("reset") - public void reset(@Context HttpServletRequest request, - @FormParam(RequestIdNames.preparedId) String preparedId, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws BadRequestException, InternalException, NotFoundException, - InsufficientPrivilegesException, DataNotOnlineException, - NotImplementedException { - - var handler = new ResetHandler(request.getRemoteAddr(), preparedId, - sessionId, investigationIds, datasetIds, datafileIds); + public void reset(@Context HttpServletRequest request, @FormParam(RequestIdNames.preparedId) String preparedId, + @FormParam(RequestIdNames.sessionId) String sessionId, @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, @FormParam("datafileIds") String datafileIds) + throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + + var handler = new ResetHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } @@ -878,53 +754,45 @@ public void reset(@Context HttpServletRequest request, * in use this has no effect. * * @title restore - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds If present, a comma separated list of - * investigation id values - * @param datasetIds If present, a comma separated list of data set id - * values or null - * @param datafileIds If present, a comma separated list of datafile id - * values. + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds If present, a comma separated list of investigation id values + * @param datasetIds If present, a comma separated list of data set id values or + * null + * @param datafileIds If present, a comma separated list of datafile id values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException * @throws InternalException * @throws NotFoundException - * @throws DataNotOnlineException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("restore") @Consumes("application/x-www-form-urlencoded") - public void restore(@Context HttpServletRequest request, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, + public void restore(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new RestoreHandler(request.getRemoteAddr(), sessionId, - investigationIds, datasetIds, datafileIds); + var handler = new RestoreHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } + /** - * Write data specified by the investigationIds, datasetIds and datafileIds - * specified along with a sessionId to archive storage. If two level storage - * is not in use this has no effect. + * Write data specified by the investigationIds, datasetIds + * and datafileIds specified along with a sessionId to archive + * storage. If two level storage is not in use this has no + * effect. * * @title write - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds If present, a comma separated list of - * investigation id values - * @param datasetIds If present, a comma separated list of data set id - * values or null - * @param datafileIds If present, a comma separated list of datafile id - * values. + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds If present, a comma separated list of investigation id values + * @param datasetIds If present, a comma separated list of data set id values or + * null + * @param datafileIds If present, a comma separated list of datafile id values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException @@ -935,17 +803,13 @@ public void restore(@Context HttpServletRequest request, @POST @Path("write") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void write(@Context HttpServletRequest request, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, + public void write(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new WriteHandler(request.getRemoteAddr(), sessionId, - investigationIds, datasetIds, datafileIds); + var handler = new WriteHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } } diff --git a/src/main/java/org/icatproject/ids/Tidier.java b/src/main/java/org/icatproject/ids/Tidier.java index 7f75328f..9d647de0 100644 --- a/src/main/java/org/icatproject/ids/Tidier.java +++ b/src/main/java/org/icatproject/ids/Tidier.java @@ -55,77 +55,57 @@ public void run() { if (twoLevel) { if (storageUnit == StorageUnit.DATASET) { - List dsInfos = mainStorage.getDatasetsToArchive( - stopArchivingLevel, startArchivingLevel); + List dsInfos = mainStorage.getDatasetsToArchive(stopArchivingLevel, + startArchivingLevel); for (DsInfo dsInfo : dsInfos) { StringBuilder sb = new StringBuilder( "SELECT ds FROM Dataset ds, ds.investigation inv, inv.facility fac WHERE"); boolean andNeeded = false; - andNeeded = addNumericConstraint(sb, "ds.id", - dsInfo.getDsId(), andNeeded); - andNeeded = addStringConstraint(sb, "ds.location", - dsInfo.getDsLocation(), andNeeded); - andNeeded = addStringConstraint(sb, "ds.name", - dsInfo.getDsName(), andNeeded); - - andNeeded = addNumericConstraint(sb, "inv.id", - dsInfo.getInvId(), andNeeded); - andNeeded = addStringConstraint(sb, "inv.name", - dsInfo.getInvName(), andNeeded); - andNeeded = addStringConstraint(sb, "inv.visitId", - dsInfo.getVisitId(), andNeeded); - - andNeeded = addStringConstraint(sb, "fac.name", - dsInfo.getFacilityName(), andNeeded); - andNeeded = addNumericConstraint(sb, "fac.id", - dsInfo.getFacilityId(), andNeeded); + andNeeded = addNumericConstraint(sb, "ds.id", dsInfo.getDsId(), andNeeded); + andNeeded = addStringConstraint(sb, "ds.location", dsInfo.getDsLocation(), andNeeded); + andNeeded = addStringConstraint(sb, "ds.name", dsInfo.getDsName(), andNeeded); + + andNeeded = addNumericConstraint(sb, "inv.id", dsInfo.getInvId(), andNeeded); + andNeeded = addStringConstraint(sb, "inv.name", dsInfo.getInvName(), andNeeded); + andNeeded = addStringConstraint(sb, "inv.visitId", dsInfo.getVisitId(), andNeeded); + + andNeeded = addStringConstraint(sb, "fac.name", dsInfo.getFacilityName(), andNeeded); + andNeeded = addNumericConstraint(sb, "fac.id", dsInfo.getFacilityId(), andNeeded); sb.append(" INCLUDE ds.investigation.facility"); try { int low = 0; while (true) { - String query = sb.toString() + " LIMIT " - + low + "," + tidyBlockSize; + String query = sb.toString() + " LIMIT " + low + "," + tidyBlockSize; List os = reader.search(query); - logger.debug(query + " returns " + os.size() - + " datasets"); + logger.debug(query + " returns " + os.size() + " datasets"); for (Object o : os) { - DatasetInfo dsInfoImpl = new DatasetInfo( - (Dataset) o); - logger.debug("Requesting archive of " - + dsInfoImpl - + " to recover main storage"); - fsm.queue(dsInfoImpl, - DeferredOp.ARCHIVE); + DatasetInfo dsInfoImpl = new DatasetInfo((Dataset) o); + logger.debug( + "Requesting archive of " + dsInfoImpl + " to recover main storage"); + fsm.queue(dsInfoImpl, DeferredOp.ARCHIVE); } if (os.size() < tidyBlockSize) { break; } low += tidyBlockSize; } - } catch (InternalException | IcatException_Exception - | InsufficientPrivilegesException e) { + } catch (InternalException | IcatException_Exception | InsufficientPrivilegesException e) { // Log it and carry on - logger.error( - e.getClass() + " " + e.getMessage()); + logger.error(e.getClass() + " " + e.getMessage()); } } } else if (storageUnit == StorageUnit.DATAFILE) { - List dfInfos = mainStorage - .getDatafilesToArchive(stopArchivingLevel, - startArchivingLevel); + List dfInfos = mainStorage.getDatafilesToArchive(stopArchivingLevel, + startArchivingLevel); for (DfInfo dfInfo : dfInfos) { - StringBuilder sb = new StringBuilder( - "SELECT df FROM Datafile df WHERE"); + StringBuilder sb = new StringBuilder("SELECT df FROM Datafile df WHERE"); boolean andNeeded = false; - andNeeded = addNumericConstraint(sb, "df.id", - dfInfo.getDfId(), andNeeded); - andNeeded = addStringConstraint(sb, "df.createId", - dfInfo.getCreateId(), andNeeded); - andNeeded = addStringConstraint(sb, "df.modId", - dfInfo.getModId(), andNeeded); + andNeeded = addNumericConstraint(sb, "df.id", dfInfo.getDfId(), andNeeded); + andNeeded = addStringConstraint(sb, "df.createId", dfInfo.getCreateId(), andNeeded); + andNeeded = addStringConstraint(sb, "df.modId", dfInfo.getModId(), andNeeded); if (key != null) { if (dfInfo.getDfLocation() != null) { if (andNeeded) { @@ -134,54 +114,42 @@ public void run() { sb.append(" "); andNeeded = true; } - sb.append("df.location" + " LIKE '" - + dfInfo.getDfLocation() + " %'"); + sb.append("df.location" + " LIKE '" + dfInfo.getDfLocation() + " %'"); } } else { - andNeeded = addStringConstraint(sb, - "df.location", + andNeeded = addStringConstraint(sb, "df.location", dfInfo.getDfLocation(), andNeeded); } - andNeeded = addStringConstraint(sb, "df.name", - dfInfo.getDfName(), andNeeded); + andNeeded = addStringConstraint(sb, "df.name", dfInfo.getDfName(), andNeeded); sb.append(" INCLUDE df.dataset"); try { int low = 0; while (true) { - String query = sb.toString() + " LIMIT " - + low + "," + tidyBlockSize; + String query = sb.toString() + " LIMIT " + low + "," + tidyBlockSize; List os = reader.search(query); - logger.debug(query + " returns " + os.size() - + " datafiles"); + logger.debug(query + " returns " + os.size() + " datafiles"); for (Object o : os) { Datafile df = (Datafile) o; - DatafileInfo dfInfoImpl = new DatafileInfo( - df.getId(), df.getName(), - - LocationHelper.getLocation( - df.getId(), - df.getLocation()), - df.getCreateId(), df.getModId(), - df.getDataset().getId()); - - logger.debug("Requesting archive of " - + dfInfoImpl - + " to recover main storage"); - fsm.queue(dfInfoImpl, - DeferredOp.ARCHIVE); + DatafileInfo dfInfoImpl = new DatafileInfo(df.getId(), df.getName(), + + LocationHelper.getLocation(df.getId(), df.getLocation()), df.getCreateId(), + df.getModId(), df.getDataset().getId()); + + + logger.debug( + "Requesting archive of " + dfInfoImpl + " to recover main storage"); + fsm.queue(dfInfoImpl, DeferredOp.ARCHIVE); } if (os.size() < tidyBlockSize) { break; } low += tidyBlockSize; } - } catch (InternalException | IcatException_Exception - | InsufficientPrivilegesException e) { + } catch (InternalException | IcatException_Exception | InsufficientPrivilegesException e) { // Log it and carry on - logger.error( - e.getClass() + " " + e.getMessage()); + logger.error(e.getClass() + " " + e.getMessage()); } } } @@ -197,8 +165,7 @@ public void run() { private final static Logger logger = LoggerFactory.getLogger(Tidier.class); - static boolean addStringConstraint(StringBuilder sb, String var, - String value, boolean andNeeded) { + static boolean addStringConstraint(StringBuilder sb, String var, String value, boolean andNeeded) { if (value != null) { if (andNeeded) { sb.append(" AND "); @@ -211,8 +178,7 @@ static boolean addStringConstraint(StringBuilder sb, String var, return andNeeded; } - static boolean addNumericConstraint(StringBuilder sb, String var, - Long value, boolean andNeeded) { + static boolean addNumericConstraint(StringBuilder sb, String var, Long value, boolean andNeeded) { if (value != null) { if (andNeeded) { sb.append(" AND "); @@ -225,8 +191,7 @@ static boolean addNumericConstraint(StringBuilder sb, String var, return andNeeded; } - static void cleanPreparedDir(Path preparedDir, int preparedCount) - throws IOException { + static void cleanPreparedDir(Path preparedDir, int preparedCount) throws IOException { Map dateMap = new HashMap<>(); File[] files = preparedDir.toFile().listFiles(); @@ -286,13 +251,11 @@ public void init() { try { PropertyHandler propertyHandler = PropertyHandler.getInstance(); - FiniteStateMachine.createInstance(reader, lockManager, - propertyHandler.getStorageUnit()); + FiniteStateMachine.createInstance(reader, lockManager, propertyHandler.getStorageUnit()); this.fsm = FiniteStateMachine.getInstance(); - this.fsm.init(); // if not yet initialized by IdsService do it now - - sizeCheckIntervalMillis = propertyHandler - .getSizeCheckIntervalMillis(); + this.fsm.init(); //if not yet initialized by IdsService do it now + + sizeCheckIntervalMillis = propertyHandler.getSizeCheckIntervalMillis(); preparedCount = propertyHandler.getPreparedCount(); preparedDir = propertyHandler.getCacheDir().resolve("prepared"); Files.createDirectories(preparedDir); @@ -310,8 +273,7 @@ public void init() { logger.info("Tidier started"); } catch (IOException e) { - throw new RuntimeException( - "Tidier reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException("Tidier reports " + e.getClass() + " " + e.getMessage()); } } diff --git a/src/main/java/org/icatproject/ids/enums/RequestType.java b/src/main/java/org/icatproject/ids/enums/RequestType.java index 1f4c0b77..a79f83e5 100644 --- a/src/main/java/org/icatproject/ids/enums/RequestType.java +++ b/src/main/java/org/icatproject/ids/enums/RequestType.java @@ -4,7 +4,20 @@ * This enum contains all defined types of requests to this server */ public enum RequestType { - GETDATA, ARCHIVE, GETICATURL, GETDATAFILEIDS, GETSERVICESTATUS, GETSIZE, - GETSTATUS, ISPREPARED, ISREADONLY, ISTWOLEVEL, PREPAREDATA, PUT, RESET, - RESTORE, WRITE, DELETE + GETDATA, + ARCHIVE, + GETICATURL, + GETDATAFILEIDS, + GETSERVICESTATUS, + GETSIZE, + GETSTATUS, + ISPREPARED, + ISREADONLY, + ISTWOLEVEL, + PREPAREDATA, + PUT, + RESET, + RESTORE, + WRITE, + DELETE } diff --git a/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java index 0c9567db..77920041 100644 --- a/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java @@ -9,9 +9,9 @@ import jakarta.ws.rs.ext.Provider; /** - * Capture any {@link org.icatproject.ids.exceptions.IdsException - * WebServiceException} thrown from {@link org.icatproject.ids.IdsService - * WebService} and generate the appropriate response code and message. + * Capture any {@link org.icatproject.ids.exceptions.IdsException WebServiceException} thrown from + * {@link org.icatproject.ids.IdsService WebService} and generate the appropriate response code and + * message. */ @Provider public class IdsExceptionMapper implements ExceptionMapper { @@ -23,7 +23,6 @@ public Response toResponse(IdsException e) { gen.writeStartObject().write("code", e.getClass().getSimpleName()) .write("message", e.getShortMessage()); gen.writeEnd().close(); - return Response.status(e.getHttpStatusCode()).entity(baos.toString()) - .build(); + return Response.status(e.getHttpStatusCode()).entity(baos.toString()).build(); } } diff --git a/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java index 266b2e8a..f9157c92 100644 --- a/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java @@ -13,11 +13,9 @@ import org.slf4j.LoggerFactory; @Provider -public class NotFoundExceptionMapper - implements ExceptionMapper { +public class NotFoundExceptionMapper implements ExceptionMapper { - private static Logger logger = LoggerFactory - .getLogger(NotFoundExceptionMapper.class); + private static Logger logger = LoggerFactory.getLogger(NotFoundExceptionMapper.class); @Override public Response toResponse(NotFoundException e) { @@ -25,10 +23,7 @@ public Response toResponse(NotFoundException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); gen.writeStartObject().write("code", "NOT_IMPLEMENTED") - .write("message", - "Operation not implemented by this IDS server.") - .writeEnd().close(); - return Response.status(Response.Status.NOT_IMPLEMENTED) - .entity(baos.toString()).build(); + .write("message", "Operation not implemented by this IDS server.").writeEnd().close(); + return Response.status(Response.Status.NOT_IMPLEMENTED).entity(baos.toString()).build(); } } diff --git a/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java index 55249b31..aa38ba0a 100644 --- a/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java @@ -14,11 +14,9 @@ import org.slf4j.LoggerFactory; @Provider -public class RuntimeExceptionMapper - implements ExceptionMapper { +public class RuntimeExceptionMapper implements ExceptionMapper { - private final static Logger logger = LoggerFactory - .getLogger(RuntimeExceptionMapper.class); + private final static Logger logger = LoggerFactory.getLogger(RuntimeExceptionMapper.class); @Override public Response toResponse(RuntimeException e) { @@ -29,9 +27,8 @@ public Response toResponse(RuntimeException e) { baos.reset(); JsonGenerator gen = Json.createGenerator(baos); gen.writeStartObject().write("code", "InternalException") - .write("message", e.getClass() + " " + e.getMessage()) - .writeEnd().close(); - return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR) - .entity(baos.toString()).build(); + .write("message", e.getClass() + " " + e.getMessage()).writeEnd().close(); + return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR).entity(baos.toString()) + .build(); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java index 4e9d4c20..f115c8da 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java @@ -29,6 +29,7 @@ public abstract class FiniteStateMachine { + private static FiniteStateMachine instance; private static Boolean inited = false; @@ -38,52 +39,40 @@ protected FiniteStateMachine(IcatReader reader, LockManager lockManager) { this.lockManager = lockManager; } - public static void createInstance(IcatReader reader, - LockManager lockManager, StorageUnit storageUnit) { + public static void createInstance(IcatReader reader, LockManager lockManager, StorageUnit storageUnit) { - if (instance == null) { - if (storageUnit == StorageUnit.DATAFILE) - instance = new FiniteStateMachineForStorageUnitDatafile(reader, - lockManager); - else if (storageUnit == StorageUnit.DATASET) - instance = new FiniteStateMachineForStorageUnitDataset(reader, - lockManager); + if(instance == null) { + if(storageUnit == StorageUnit.DATAFILE) + instance = new FiniteStateMachineForStorageUnitDatafile(reader, lockManager); + else if(storageUnit == StorageUnit.DATASET) + instance = new FiniteStateMachineForStorageUnitDataset(reader, lockManager); else - instance = new FiniteStateMachineForSingleLevelStorage(reader, - lockManager); + instance = new FiniteStateMachineForSingleLevelStorage(reader, lockManager); } } public static FiniteStateMachine getInstance() { - if (instance != null) { + if(instance != null) { return instance; } - // If this assert was executed: Instance of FiniteStateMachine is not - // created. At First createInstance() has to be called at least once. - throw new RuntimeException( - "Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once."); + // If this assert was executed: Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once. + throw new RuntimeException("Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once."); } - public abstract void queue(DataInfoBase dataInfo, DeferredOp deferredOp) - throws InternalException; - + public abstract void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException; protected abstract void scheduleTimer(); - protected abstract void addDataInfoJson(JsonGenerator gen); public enum RequestedState { - ARCHIVE_REQUESTED, DELETE_REQUESTED, RESTORE_REQUESTED, WRITE_REQUESTED, - WRITE_THEN_ARCHIVE_REQUESTED + ARCHIVE_REQUESTED, DELETE_REQUESTED, RESTORE_REQUESTED, WRITE_REQUESTED, WRITE_THEN_ARCHIVE_REQUESTED } - protected static Logger logger = LoggerFactory - .getLogger(FiniteStateMachine.class); + protected static Logger logger = LoggerFactory.getLogger(FiniteStateMachine.class); /* - * Note that the veriable processOpsDelayMillis is used to either delay all - * deferred datafile operations or to delay dataset writes, depending on the - * setting of storageUnit. + * Note that the veriable processOpsDelayMillis is used to either delay all deferred + * datafile operations or to delay dataset writes, depending on the setting of storageUnit. */ protected long processOpsDelayMillis; @@ -130,8 +119,7 @@ public Set getMaybeOffline() { } /** - * Find any DataFileInfo which are being restored or are queued for - * restoration + * Find any DataFileInfo which are being restored or are queued for restoration */ public Set getRestoring() { Map union; @@ -157,26 +145,23 @@ protected void addDataInfoJsonFromDeferredOpsQueue(JsonGenerator gen) { gen.writeStartArray("opsQueue"); for (Entry entry : union.entrySet()) { DataInfoBase item = entry.getKey(); - gen.writeStartObject().write("data", item.toString()) - .write("request", entry.getValue().name()).writeEnd(); + gen.writeStartObject().write("data", item.toString()).write("request", entry.getValue().name()) + .writeEnd(); } gen.writeEnd(); // end Array("opsQueue") } public String getServiceStatus() throws InternalException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos) - .writeStartObject()) { - + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + this.addDataInfoJson(gen); Collection lockInfo = lockManager.getLockInfo(); gen.write("lockCount", lockInfo.size()); gen.writeStartArray("locks"); for (LockInfo li : lockInfo) { - gen.writeStartObject().write("id", li.id) - .write("type", li.type.name()).write("count", li.count) - .writeEnd(); + gen.writeStartObject().write("id", li.id).write("type", li.type.name()).write("count", li.count).writeEnd(); } gen.writeEnd(); // end Array("locks") @@ -191,25 +176,24 @@ public String getServiceStatus() throws InternalException { return baos.toString(); } + public void init() { try { synchronized (inited) { - if (!inited) { + if(!inited) { propertyHandler = PropertyHandler.getInstance(); - processQueueIntervalMillis = propertyHandler - .getProcessQueueIntervalSeconds() * 1000L; + processQueueIntervalMillis = propertyHandler.getProcessQueueIntervalSeconds() * 1000L; this.scheduleTimer(); - + markerDir = propertyHandler.getCacheDir().resolve("marker"); Files.createDirectories(markerDir); inited = true; } } } catch (IOException e) { - throw new RuntimeException("FiniteStateMachine reports " - + e.getClass() + " " + e.getMessage()); + throw new RuntimeException("FiniteStateMachine reports " + e.getClass() + " " + e.getMessage()); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java index 9a96776a..7ffb349d 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java @@ -8,26 +8,25 @@ import jakarta.json.stream.JsonGenerator; -public class FiniteStateMachineForSingleLevelStorage - extends FiniteStateMachine { +public class FiniteStateMachineForSingleLevelStorage extends FiniteStateMachine { - protected FiniteStateMachineForSingleLevelStorage(IcatReader reader, - LockManager lockManager) { + protected FiniteStateMachineForSingleLevelStorage(IcatReader reader, LockManager lockManager) { super(reader, lockManager); } + @Override - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) - throws InternalException { - throw new InternalException( - "### Operation is not permitted for single level storage"); + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { + throw new InternalException("### Operation is not permitted for single level storage"); } + @Override protected void scheduleTimer() { - // nothing to do here for single level storage + //nothing to do here for single level storage } + @Override protected void addDataInfoJson(JsonGenerator gen) { gen.writeStartArray("opsQueue").writeEnd(); diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java index b634d39c..88543bb1 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java @@ -32,43 +32,38 @@ import jakarta.json.stream.JsonGenerator; -public class FiniteStateMachineForStorageUnitDatafile - extends FiniteStateMachine { +public class FiniteStateMachineForStorageUnitDatafile extends FiniteStateMachine { - protected FiniteStateMachineForStorageUnitDatafile(IcatReader icatReader, - LockManager lockManager) { + protected FiniteStateMachineForStorageUnitDatafile(IcatReader icatReader, LockManager lockManager) { super(icatReader, lockManager); } + @Override protected void scheduleTimer() { - processOpsDelayMillis = propertyHandler.getDelayDatafileOperations() - * 1000L; + processOpsDelayMillis = propertyHandler.getDelayDatafileOperations() * 1000L; timer.schedule(new DfProcessQueue(), processQueueIntervalMillis); - logger.info("DfProcessQueue scheduled to run in " - + processQueueIntervalMillis + " milliseconds"); + logger.info("DfProcessQueue scheduled to run in " + processQueueIntervalMillis + " milliseconds"); } + @Override protected void addDataInfoJson(JsonGenerator gen) { this.addDataInfoJsonFromDeferredOpsQueue(gen); } - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) - throws InternalException { + + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { var dfInfo = (DatafileInfo) dataInfo; - if (dfInfo == null) - throw new InternalException( - "DataInfoBase object could not be casted into a DataFileInfo. Did you handed over a DataSetInfo instead?"); + if(dfInfo == null) throw new InternalException("DataInfoBase object could not be casted into a DataFileInfo. Did you handed over a DataSetInfo instead?"); logger.info("Requesting " + deferredOp + " of datafile " + dfInfo); synchronized (deferredOpsQueue) { if (processOpsTime == null) { - processOpsTime = System.currentTimeMillis() - + processOpsDelayMillis; + processOpsTime = System.currentTimeMillis() + processOpsDelayMillis; final Date d = new Date(processOpsTime); logger.debug("Requesting delay operations till " + d); } @@ -77,75 +72,62 @@ public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) if (state == null) { if (deferredOp == DeferredOp.WRITE) { try { - Path marker = markerDir - .resolve(Long.toString(dfInfo.getId())); + Path marker = markerDir.resolve(Long.toString(dfInfo.getId())); Files.createFile(marker); logger.debug("Created marker " + marker); } catch (FileAlreadyExistsException e) { // Pass will ignore this } catch (IOException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } - deferredOpsQueue.put(dfInfo, - RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.WRITE_REQUESTED); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, - RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dfInfo, - RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.RESTORE_REQUESTED); } else if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, - RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); } } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.RESTORE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, - RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); } } else if (state == RequestedState.DELETE_REQUESTED) { // No way out } else if (state == RequestedState.RESTORE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, - RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, - RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, - RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dfInfo, - RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.WRITE_REQUESTED); } } } } + private class DfProcessQueue extends TimerTask { @Override public void run() { try { synchronized (deferredOpsQueue) { - if (processOpsTime != null - && System.currentTimeMillis() > processOpsTime - && !deferredOpsQueue.isEmpty()) { + if (processOpsTime != null && System.currentTimeMillis() > processOpsTime && !deferredOpsQueue.isEmpty()) { processOpsTime = null; - logger.debug("deferredDfOpsQueue has " - + deferredOpsQueue.size() + " entries"); + logger.debug("deferredDfOpsQueue has " + deferredOpsQueue.size() + " entries"); List writes = new ArrayList<>(); List archives = new ArrayList<>(); List restores = new ArrayList<>(); @@ -156,27 +138,20 @@ public void run() { Map deleteLocks = new HashMap<>(); Map newOps = new HashMap<>(); - final Iterator> it = deferredOpsQueue - .entrySet().iterator(); + final Iterator> it = deferredOpsQueue.entrySet().iterator(); while (it.hasNext()) { - Entry opEntry = it - .next(); + Entry opEntry = it.next(); var dfInfo = (DatafileInfo) opEntry.getKey(); - if (dfInfo == null) - throw new RuntimeException( - "Could not cast DataInfoBase to DataFileInfo. Did you handed over another sub type?"); + if(dfInfo == null) throw new RuntimeException("Could not cast DataInfoBase to DataFileInfo. Did you handed over another sub type?"); Long dsId = dfInfo.getDsId(); DatasetInfo dsInfo; try { - Dataset ds = (Dataset) reader.get( - "Dataset ds INCLUDE ds.investigation.facility", - dsId); + Dataset ds = (Dataset) reader.get("Dataset ds INCLUDE ds.investigation.facility", dsId); dsInfo = new DatasetInfo(ds); } catch (Exception e) { - logger.error("Could not get dsInfo {}: {}.", - dsId, e.getMessage()); + logger.error("Could not get dsInfo {}: {}.", dsId, e.getMessage()); continue; } if (!dataInfoChanging.containsKey(dfInfo)) { @@ -185,20 +160,12 @@ public void run() { if (state == RequestedState.WRITE_REQUESTED) { if (!writeLocks.containsKey(dsId)) { try { - writeLocks.put(dsId, - lockManager.lock(dsInfo, - LockType.SHARED)); + writeLocks.put(dsId, lockManager.lock(dsInfo, LockType.SHARED)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " - + dsId - + ", hold back " - + state); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error("I/O exception " - + e.getMessage() - + " locking " + dsId); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } @@ -208,46 +175,28 @@ public void run() { } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (!writeLocks.containsKey(dsId)) { try { - writeLocks.put(dsId, - lockManager.lock(dsInfo, - LockType.SHARED)); + writeLocks.put(dsId, lockManager.lock(dsInfo, LockType.SHARED)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " - + dsId - + ", hold back " - + state); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error("I/O exception " - + e.getMessage() - + " locking " + dsId); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } it.remove(); - dataInfoChanging.put(dfInfo, - RequestedState.WRITE_REQUESTED); + dataInfoChanging.put(dfInfo, RequestedState.WRITE_REQUESTED); writes.add(dfInfo); - newOps.put(dfInfo, - RequestedState.ARCHIVE_REQUESTED); + newOps.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (!archiveLocks.containsKey(dsId)) { try { - archiveLocks.put(dsId, - lockManager.lock(dsInfo, - LockType.EXCLUSIVE)); + archiveLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " - + dsId - + ", hold back " - + state); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error("I/O exception " - + e.getMessage() - + " locking " + dsId); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } @@ -257,20 +206,12 @@ public void run() { } else if (state == RequestedState.RESTORE_REQUESTED) { if (!restoreLocks.containsKey(dsId)) { try { - restoreLocks.put(dsId, - lockManager.lock(dsInfo, - LockType.EXCLUSIVE)); + restoreLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " - + dsId - + ", hold back " - + state); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error("I/O exception " - + e.getMessage() - + " locking " + dsId); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } @@ -280,20 +221,12 @@ public void run() { } else if (state == RequestedState.DELETE_REQUESTED) { if (!deleteLocks.containsKey(dsId)) { try { - deleteLocks.put(dsId, - lockManager.lock(dsInfo, - LockType.EXCLUSIVE)); + deleteLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " - + dsId - + ", hold back " - + state); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error("I/O exception " - + e.getMessage() - + " locking " + dsId); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } @@ -301,61 +234,41 @@ public void run() { dataInfoChanging.put(dfInfo, state); deletes.add(dfInfo); } else { - throw new AssertionError( - "Impossible state"); + throw new AssertionError("Impossible state"); } } } if (!newOps.isEmpty()) { deferredOpsQueue.putAll(newOps); - logger.debug( - "Adding {} operations to be scheduled next time round", - newOps.size()); + logger.debug("Adding {} operations to be scheduled next time round", newOps.size()); } if (!deferredOpsQueue.isEmpty()) { processOpsTime = 0L; } if (!writes.isEmpty()) { - logger.debug("Launch thread to process " - + writes.size() + " writes"); - Thread w = new Thread(new DfWriter(writes, - propertyHandler, - FiniteStateMachineForStorageUnitDatafile.this, - writeLocks.values())); + logger.debug("Launch thread to process " + writes.size() + " writes"); + Thread w = new Thread(new DfWriter(writes, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, writeLocks.values())); w.start(); } if (!archives.isEmpty()) { - logger.debug("Launch thread to process " - + archives.size() + " archives"); - Thread w = new Thread(new DfArchiver(archives, - propertyHandler, - FiniteStateMachineForStorageUnitDatafile.this, - archiveLocks.values())); + logger.debug("Launch thread to process " + archives.size() + " archives"); + Thread w = new Thread(new DfArchiver(archives, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, archiveLocks.values())); w.start(); } if (!restores.isEmpty()) { - logger.debug("Launch thread to process " - + restores.size() + " restores"); - Thread w = new Thread(new DfRestorer(restores, - propertyHandler, - FiniteStateMachineForStorageUnitDatafile.this, - restoreLocks.values())); + logger.debug("Launch thread to process " + restores.size() + " restores"); + Thread w = new Thread(new DfRestorer(restores, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, restoreLocks.values())); w.start(); } if (!deletes.isEmpty()) { - logger.debug("Launch thread to process " - + deletes.size() + " deletes"); - Thread w = new Thread(new DfDeleter(deletes, - propertyHandler, - FiniteStateMachineForStorageUnitDatafile.this, - deleteLocks.values())); + logger.debug("Launch thread to process " + deletes.size() + " deletes"); + Thread w = new Thread(new DfDeleter(deletes, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, deleteLocks.values())); w.start(); } } } } finally { - timer.schedule(new DfProcessQueue(), - processQueueIntervalMillis); + timer.schedule(new DfProcessQueue(), processQueueIntervalMillis); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java index 9120dee5..cbe0a5cd 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java @@ -26,22 +26,20 @@ import jakarta.json.stream.JsonGenerator; -public class FiniteStateMachineForStorageUnitDataset - extends FiniteStateMachine { +public class FiniteStateMachineForStorageUnitDataset extends FiniteStateMachine { protected Map writeTimes = new HashMap<>(); - protected FiniteStateMachineForStorageUnitDataset(IcatReader reader, - LockManager lockManager) { + protected FiniteStateMachineForStorageUnitDataset(IcatReader reader, LockManager lockManager) { super(reader, lockManager); } + @Override protected void scheduleTimer() { processOpsDelayMillis = propertyHandler.getDelayDatasetWrites() * 1000L; timer.schedule(new DsProcessQueue(), processQueueIntervalMillis); - logger.info("DsProcessQueue scheduled to run in " - + processQueueIntervalMillis + " milliseconds"); + logger.info("DsProcessQueue scheduled to run in " + processQueueIntervalMillis + " milliseconds"); } @Override @@ -49,8 +47,9 @@ protected void addDataInfoJson(JsonGenerator gen) { this.addDataInfoJsonFromDeferredOpsQueue(gen); } - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) - throws InternalException { + + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { + logger.info("Requesting " + deferredOp + " of dataset " + dataInfo); @@ -61,47 +60,41 @@ public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, - RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, - RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.RESTORE_REQUESTED); } } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); - deferredOpsQueue.put(dataInfo, - RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, - RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.RESTORE_REQUESTED); } } else if (state == RequestedState.RESTORE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, - RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { setDelay(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, - RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { setDelay(dataInfo); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, - RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.WRITE_REQUESTED); } } } } + private void requestWrite(DataInfoBase dsInfo) throws InternalException { try { Path marker = markerDir.resolve(Long.toString(dsInfo.getId())); @@ -116,16 +109,16 @@ private void requestWrite(DataInfoBase dsInfo) throws InternalException { setDelay(dsInfo); } + private void setDelay(DataInfoBase dsInfo) { - writeTimes.put(dsInfo, - System.currentTimeMillis() + processOpsDelayMillis); + writeTimes.put(dsInfo, System.currentTimeMillis() + processOpsDelayMillis); if (logger.isDebugEnabled()) { final Date d = new Date(writeTimes.get(dsInfo)); - logger.debug("Requesting delay of writing of dataset " + dsInfo - + " till " + d); + logger.debug("Requesting delay of writing of dataset " + dsInfo + " till " + d); } } + private class DsProcessQueue extends TimerTask { @Override @@ -134,16 +127,13 @@ public void run() { synchronized (deferredOpsQueue) { final long now = System.currentTimeMillis(); Map newOps = new HashMap<>(); - final Iterator> it = deferredOpsQueue - .entrySet().iterator(); + final Iterator> it = deferredOpsQueue.entrySet().iterator(); while (it.hasNext()) { - final Entry opEntry = it - .next(); + final Entry opEntry = it.next(); final var dsInfo = (DatasetInfo) opEntry.getKey(); - if (dsInfo == null) - throw new RuntimeException( - "Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); + + if(dsInfo == null) throw new RuntimeException("Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); if (!dataInfoChanging.containsKey(dsInfo)) { final RequestedState state = opEntry.getValue(); @@ -151,81 +141,50 @@ public void run() { || state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (now > writeTimes.get(dsInfo)) { try { - Lock lock = lockManager.lock(dsInfo, - LockType.SHARED); - logger.debug("Will process " + dsInfo - + " with " + state); + Lock lock = lockManager.lock(dsInfo, LockType.SHARED); + logger.debug("Will process " + dsInfo + " with " + state); writeTimes.remove(dsInfo); - dataInfoChanging.put(dsInfo, - RequestedState.WRITE_REQUESTED); + dataInfoChanging.put(dsInfo, RequestedState.WRITE_REQUESTED); it.remove(); final Thread w = new Thread( - new DsWriter(dsInfo, - propertyHandler, - FiniteStateMachineForStorageUnitDataset.this, - reader, lock)); + new DsWriter(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, reader, lock)); w.start(); if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { - newOps.put(dsInfo, - RequestedState.ARCHIVE_REQUESTED); + newOps.put(dsInfo, RequestedState.ARCHIVE_REQUESTED); } } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " - + dsInfo - + ", hold back process with " - + state); + logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); } catch (IOException e) { - logger.error("I/O exception " - + e.getMessage() + " locking " - + dsInfo); + logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); } } } else if (state == RequestedState.ARCHIVE_REQUESTED) { try { - Lock lock = lockManager.lock(dsInfo, - LockType.EXCLUSIVE); + Lock lock = lockManager.lock(dsInfo, LockType.EXCLUSIVE); it.remove(); - logger.debug("Will process " + dsInfo - + " with " + state); + logger.debug("Will process " + dsInfo + " with " + state); dataInfoChanging.put(dsInfo, state); - final Thread w = new Thread(new DsArchiver( - dsInfo, propertyHandler, - FiniteStateMachineForStorageUnitDataset.this, - lock)); + final Thread w = new Thread( + new DsArchiver(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, lock)); w.start(); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " - + dsInfo - + ", hold back process with " - + state); + logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); } catch (IOException e) { - logger.error( - "I/O exception " + e.getMessage() - + " locking " + dsInfo); + logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); } } else if (state == RequestedState.RESTORE_REQUESTED) { try { - Lock lock = lockManager.lock(dsInfo, - LockType.EXCLUSIVE); - logger.debug("Will process " + dsInfo - + " with " + state); + Lock lock = lockManager.lock(dsInfo, LockType.EXCLUSIVE); + logger.debug("Will process " + dsInfo + " with " + state); dataInfoChanging.put(dsInfo, state); it.remove(); - final Thread w = new Thread(new DsRestorer( - dsInfo, propertyHandler, - FiniteStateMachineForStorageUnitDataset.this, - reader, lock)); + final Thread w = new Thread( + new DsRestorer(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, reader, lock)); w.start(); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " - + dsInfo - + ", hold back process with " - + state); + logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); } catch (IOException e) { - logger.error( - "I/O exception " + e.getMessage() - + " locking " + dsInfo); + logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); } } } @@ -234,8 +193,7 @@ public void run() { } } finally { - timer.schedule(new DsProcessQueue(), - processQueueIntervalMillis); + timer.schedule(new DsProcessQueue(), processQueueIntervalMillis); } } diff --git a/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java b/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java index 8a29aec2..8c813be8 100644 --- a/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java +++ b/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java @@ -11,8 +11,8 @@ @Provider public class CORSResponseFilter implements ContainerResponseFilter { - public void filter(ContainerRequestContext requestContext, - ContainerResponseContext responseContext) throws IOException { + public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) + throws IOException { MultivaluedMap headers = responseContext.getHeaders(); headers.add("Access-Control-Allow-Origin", "*"); diff --git a/src/main/java/org/icatproject/ids/helpers/Constants.java b/src/main/java/org/icatproject/ids/helpers/Constants.java index 298205bf..bc7d4aaa 100644 --- a/src/main/java/org/icatproject/ids/helpers/Constants.java +++ b/src/main/java/org/icatproject/ids/helpers/Constants.java @@ -9,8 +9,8 @@ public class Constants { static { - InputStream inputStream = Constants.class.getClassLoader() - .getResourceAsStream("app.properties"); + InputStream inputStream = Constants.class.getClassLoader().getResourceAsStream( + "app.properties"); Properties p = new Properties(); try { diff --git a/src/main/java/org/icatproject/ids/helpers/LocationHelper.java b/src/main/java/org/icatproject/ids/helpers/LocationHelper.java index 8954cc89..b3c1e8b5 100644 --- a/src/main/java/org/icatproject/ids/helpers/LocationHelper.java +++ b/src/main/java/org/icatproject/ids/helpers/LocationHelper.java @@ -23,8 +23,8 @@ public static String getLocation(long dfid, String location) } } - public static String getLocationFromDigest(long id, String locationWithHash, - String key) + + public static String getLocationFromDigest(long id, String locationWithHash, String key) throws InternalException, InsufficientPrivilegesException { int i = locationWithHash.lastIndexOf(' '); try { @@ -32,13 +32,11 @@ public static String getLocationFromDigest(long id, String locationWithHash, String hash = locationWithHash.substring(i + 1); if (!hash.equals(IcatSecurity.digest(id, location, key))) { throw new InsufficientPrivilegesException( - "Location \"" + locationWithHash - + "\" does not contain a valid hash."); + "Location \"" + locationWithHash + "\" does not contain a valid hash."); } return location; } catch (IndexOutOfBoundsException e) { - throw new InsufficientPrivilegesException("Location \"" - + locationWithHash + "\" does not contain hash."); + throw new InsufficientPrivilegesException("Location \"" + locationWithHash + "\" does not contain hash."); } catch (NoSuchAlgorithmException e) { throw new InternalException(e.getMessage()); } diff --git a/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java b/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java index 208d39a5..ffaa34be 100644 --- a/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java +++ b/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java @@ -21,10 +21,10 @@ public class RangeOutputStream extends FilterOutputStream { * fixed number of bytes following some offset. * * @param os the stream to wrap - * @param min the number of bytes to skip. It may larger than the number - * of bytes in the stream. - * @param count restrict to transmit only this number of bytes. The value - * null indicates that all bytes after the offset should be + * @param min the number of bytes to skip. It may larger than the number of + * bytes in the stream. + * @param count restrict to transmit only this number of bytes. The value null + * indicates that all bytes after the offset should be * transmitted. */ public RangeOutputStream(OutputStream os, long min, Long count) { diff --git a/src/main/java/org/icatproject/ids/helpers/SO.java b/src/main/java/org/icatproject/ids/helpers/SO.java index 254a4e08..58e1ee4a 100644 --- a/src/main/java/org/icatproject/ids/helpers/SO.java +++ b/src/main/java/org/icatproject/ids/helpers/SO.java @@ -38,10 +38,8 @@ public class SO implements StreamingOutput { private static final int BUFSIZ = 2048; private final static Logger logger = LoggerFactory.getLogger(SO.class); - public SO(Map dsInfos, Map dfInfos, - long offset, boolean zip, boolean compress, Lock lock, - Long transferId, String ip, long start, - ServiceProvider serviceProvider) { + public SO(Map dsInfos, Map dfInfos, long offset, boolean zip, boolean compress, + Lock lock, Long transferId, String ip, long start, ServiceProvider serviceProvider) { this.offset = offset; this.zip = zip; this.dsInfos = dsInfos; @@ -64,27 +62,21 @@ public void write(OutputStream output) throws IOException { } byte[] bytes = new byte[BUFSIZ]; if (zip) { - ZipOutputStream zos = new ZipOutputStream( - new BufferedOutputStream(output)); + ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(output)); if (!compress) { zos.setLevel(0); // Otherwise use default compression } - for (DataInfoBase dataInfo : dfInfos.values()) { + for ( DataInfoBase dataInfo : dfInfos.values()) { var dfInfo = (DatafileInfo) dataInfo; logger.debug("Adding " + dfInfo + " to zip"); transfer = dfInfo; - DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId()); - String entryName = this.serviceProvider.getPropertyHandler() - .getZipMapper() - .getFullEntryName((DatasetInfo) dsInfo, - (DatafileInfo) dfInfo); + DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId() ); + String entryName = this.serviceProvider.getPropertyHandler().getZipMapper().getFullEntryName((DatasetInfo)dsInfo, (DatafileInfo)dfInfo); InputStream stream = null; try { zos.putNextEntry(new ZipEntry(entryName)); - stream = this.serviceProvider.getMainStorage().get( - dfInfo.getLocation(), dfInfo.getCreateId(), - dfInfo.getModId()); + stream = this.serviceProvider.getMainStorage().get(dfInfo.getLocation(), dfInfo.getCreateId(), dfInfo.getModId()); int length; while ((length = stream.read(bytes)) >= 0) { zos.write(bytes, 0, length); @@ -99,11 +91,9 @@ public void write(OutputStream output) throws IOException { } zos.close(); } else { - DatafileInfo dfInfo = (DatafileInfo) dfInfos.values().iterator() - .next(); + DatafileInfo dfInfo = (DatafileInfo) dfInfos.values().iterator().next(); transfer = dfInfo; - InputStream stream = this.serviceProvider.getMainStorage().get( - dfInfo.getDfLocation(), dfInfo.getCreateId(), + InputStream stream = this.serviceProvider.getMainStorage().get(dfInfo.getDfLocation(), dfInfo.getCreateId(), dfInfo.getModId()); int length; while ((length = stream.read(bytes)) >= 0) { @@ -115,28 +105,23 @@ public void write(OutputStream output) throws IOException { if (transferId != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos) - .writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { gen.write("transferId", transferId); gen.writeEnd(); } - this.serviceProvider.getTransmitter().processMessage("getData", - ip, baos.toString(), start); + this.serviceProvider.getTransmitter().processMessage("getData", ip, baos.toString(), start); } } catch (IOException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos) - .writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { gen.write("transferId", transferId); gen.write("exceptionClass", e.getClass().toString()); gen.write("exceptionMessage", e.getMessage()); gen.writeEnd(); } - this.serviceProvider.getTransmitter().processMessage("getData", ip, - baos.toString(), start); - logger.error("Failed to stream " + transfer + " due to " - + e.getMessage()); + this.serviceProvider.getTransmitter().processMessage("getData", ip, baos.toString(), start); + logger.error("Failed to stream " + transfer + " due to " + e.getMessage()); throw e; } finally { lock.release(); diff --git a/src/main/java/org/icatproject/ids/helpers/ValueContainer.java b/src/main/java/org/icatproject/ids/helpers/ValueContainer.java index 9638ca1f..bb99efba 100644 --- a/src/main/java/org/icatproject/ids/helpers/ValueContainer.java +++ b/src/main/java/org/icatproject/ids/helpers/ValueContainer.java @@ -22,16 +22,11 @@ private ValueContainer(Object value, ValueContainerType type) { /** * checks if the type of the contained value is the same as the typeToCheck - * * @param typeToCheck the type to be checked if the contained value is of * @throws InternalException if the types don't match an exception is thrown */ - private void checkType(ValueContainerType typeToCheck) - throws InternalException { - if (this.type != typeToCheck) - throw new InternalException( - "This ValueContainer ist not of the needed type " - + typeToCheck + " its type is " + this.type + "."); + private void checkType(ValueContainerType typeToCheck) throws InternalException { + if(this.type != typeToCheck) throw new InternalException("This ValueContainer ist not of the needed type " + typeToCheck + " its type is " + this.type + "."); } public static ValueContainer getInvalid() { @@ -52,7 +47,6 @@ private ValueContainer(Void value) { /** * Creates a ValueContainer of type int - * * @param value the value contained by the container */ public ValueContainer(int value) { @@ -61,7 +55,6 @@ public ValueContainer(int value) { /** * Creates a ValueContainer of type long - * * @param value the value contained by the container */ public ValueContainer(long value) { @@ -70,7 +63,6 @@ public ValueContainer(long value) { /** * Creates a ValueContainer of type String - * * @param value the value contained by the container */ public ValueContainer(String value) { @@ -79,7 +71,6 @@ public ValueContainer(String value) { /** * Creates a ValueContainer of type boolean - * * @param value the value contained by the container */ public ValueContainer(boolean value) { @@ -88,16 +79,14 @@ public ValueContainer(boolean value) { /** * Creates a ValueContainer of type Response - * * @param value the value contained by the container */ public ValueContainer(Response value) { this(value, ValueContainerType.RESPONSE); - } - + } + /** * Creates a ValueContainer of type InputStream - * * @param value the value contained by the container */ public ValueContainer(InputStream value) { @@ -106,7 +95,6 @@ public ValueContainer(InputStream value) { /** * Informs about the type of the contained value - * * @return */ public ValueContainerType getType() { @@ -115,10 +103,8 @@ public ValueContainerType getType() { /** * Tries to return the value of the type int. - * * @return - * @throws InternalException if the container has another type an exception - * will be thrown + * @throws InternalException if the container has another type an exception will be thrown */ public int getInt() throws InternalException { this.checkType(ValueContainerType.INT); @@ -127,10 +113,8 @@ public int getInt() throws InternalException { /** * Tries to return the value of the type long. - * * @return - * @throws InternalException if the container has another type an exception - * will be thrown + * @throws InternalException if the container has another type an exception will be thrown */ public long getLong() throws InternalException { this.checkType(ValueContainerType.LONG); @@ -139,10 +123,8 @@ public long getLong() throws InternalException { /** * Tries to return the value of the type boolean. - * * @return - * @throws InternalException if the container has another type an exception - * will be thrown + * @throws InternalException if the container has another type an exception will be thrown */ public boolean getBool() throws InternalException { this.checkType(ValueContainerType.BOOL); @@ -151,10 +133,8 @@ public boolean getBool() throws InternalException { /** * Tries to return the value of the type String. - * * @return - * @throws InternalException if the container has another type an exception - * will be thrown + * @throws InternalException if the container has another type an exception will be thrown */ public String getString() throws InternalException { this.checkType(ValueContainerType.STRING); @@ -163,10 +143,8 @@ public String getString() throws InternalException { /** * Tries to return the value of the type Response. - * * @return - * @throws InternalException if the container has another type an exception - * will be thrown + * @throws InternalException if the container has another type an exception will be thrown */ public Response getResponse() throws InternalException { this.checkType(ValueContainerType.RESPONSE); @@ -175,10 +153,8 @@ public Response getResponse() throws InternalException { /** * Tries to return the value of the type InputStream. - * * @return - * @throws InternalException if the container has another type an exception - * will be thrown + * @throws InternalException if the container has another type an exception will be thrown */ public InputStream getInputStream() throws InternalException { this.checkType(ValueContainerType.INPUTSTREAM); @@ -187,41 +163,27 @@ public InputStream getInputStream() throws InternalException { @Override public String toString() { - switch (this.type) { - case INVALID: - return "" + ValueContainerType.INVALID; - case VOID: - return "" + ValueContainerType.VOID; - case INT: - return "" + this.value; - case LONG: - return "" + this.value; - case BOOL: - return ((boolean) this.value ? "true" : "false"); - case STRING: - return (String) this.value; - case RESPONSE: - return "Response " + ((Response) this.value).toString(); - case INPUTSTREAM: - return "An InputStream which will be printed here to prevent it from closing (and maybe it is too long)."; - default: - throw new RuntimeException( - "Doesn't know how to make a String vom ValueContainer of type " - + this.type - + ". Please implement a new case is ValueContainer.toString()."); + switch(this.type) { + case INVALID: return ""+ValueContainerType.INVALID; + case VOID: return ""+ValueContainerType.VOID; + case INT: return ""+this.value; + case LONG: return ""+this.value; + case BOOL: return ((boolean)this.value ? "true" : "false"); + case STRING: return (String)this.value; + case RESPONSE: return "Response " + ((Response) this.value).toString(); + case INPUTSTREAM: return "An InputStream which will be printed here to prevent it from closing (and maybe it is too long)."; + default: + throw new RuntimeException("Doesn't know how to make a String vom ValueContainer of type " + this.type + ". Please implement a new case is ValueContainer.toString()."); } } - public boolean isNull() { - return this.isInvalid() && this.value == null; - } + public boolean isNull() { return this.isInvalid() && this.value == null; } - public boolean isInvalid() { - return this.type == ValueContainerType.INVALID; - } + public boolean isInvalid() { return this.type == ValueContainerType.INVALID; } + + public boolean isVoid() { return this.type == ValueContainerType.VOID; } + + - public boolean isVoid() { - return this.type == ValueContainerType.VOID; - } } diff --git a/src/main/java/org/icatproject/ids/models/DataInfoBase.java b/src/main/java/org/icatproject/ids/models/DataInfoBase.java index 57adf664..603a3e7e 100644 --- a/src/main/java/org/icatproject/ids/models/DataInfoBase.java +++ b/src/main/java/org/icatproject/ids/models/DataInfoBase.java @@ -9,7 +9,7 @@ public abstract class DataInfoBase { protected String name; protected String location; - protected DataInfoBase(long id, String name, String location) { + protected DataInfoBase(long id, String name, String location){ this.name = name; this.id = id; this.location = location; @@ -42,5 +42,5 @@ public boolean equals(Object obj) { } return this.id == ((DataInfoBase) obj).getId(); } - + } diff --git a/src/main/java/org/icatproject/ids/models/DataSelection.java b/src/main/java/org/icatproject/ids/models/DataSelection.java index ffb8aa77..1bc59344 100644 --- a/src/main/java/org/icatproject/ids/models/DataSelection.java +++ b/src/main/java/org/icatproject/ids/models/DataSelection.java @@ -15,10 +15,10 @@ public class DataSelection { protected Boolean compress; private long length; - public DataSelection(SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, - Boolean zip, Boolean compress) { + + + public DataSelection(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress) { this.dsInfos = dsInfos; this.dfInfos = dfInfos; @@ -31,10 +31,12 @@ public DataSelection(SortedMap dsInfos, this.compress = compress; } + public SortedMap getDsInfo() { return dsInfos; } + public SortedMap getDfInfo() { return dfInfos; } @@ -51,10 +53,11 @@ public long getLength() { return this.length; } + public boolean mustZip() { // if(this.zip == null) { - return dfids.size() > 1L || !dsids.isEmpty() || !invids.isEmpty() - || (dfids.isEmpty() && dsids.isEmpty() && invids.isEmpty()); + return dfids.size() > 1L || !dsids.isEmpty() || !invids.isEmpty() + || (dfids.isEmpty() && dsids.isEmpty() && invids.isEmpty()); // } // return this.zip; @@ -64,6 +67,7 @@ public boolean isSingleDataset() { return dfids.isEmpty() && dsids.size() == 1 && invids.isEmpty(); } + public Set getEmptyDatasets() { return emptyDatasets; } diff --git a/src/main/java/org/icatproject/ids/models/DatafileInfo.java b/src/main/java/org/icatproject/ids/models/DatafileInfo.java index 4fa20cfd..789cfa79 100644 --- a/src/main/java/org/icatproject/ids/models/DatafileInfo.java +++ b/src/main/java/org/icatproject/ids/models/DatafileInfo.java @@ -3,18 +3,16 @@ import org.icatproject.ids.plugin.DfInfo; /** - * Contains information about a Datafile. Replaces DsInfo in v3 May should - * implement DfInfo interface + * Contains information about a Datafile. Replaces DsInfo in v3 + * May should implement DfInfo interface */ -public class DatafileInfo extends DataInfoBase - implements Comparable, DfInfo { +public class DatafileInfo extends DataInfoBase implements Comparable, DfInfo { protected String createId; - protected String modId; + protected String modId; protected long datasId; - public DatafileInfo(Long id, String name, String location, String createId, - String modId, Long datasId) { + public DatafileInfo(Long id, String name, String location, String createId, String modId, Long datasId) { super(id, name, location); this.createId = createId; @@ -52,17 +50,9 @@ public int compareTo(DatafileInfo o) { // implementing DfInfo @Override - public Long getDfId() { - return this.getId(); - } - + public Long getDfId() { return this.getId(); } @Override - public String getDfLocation() { - return this.getLocation(); - } - + public String getDfLocation() { return this.getLocation(); } @Override - public String getDfName() { - return this.getName(); - } + public String getDfName() { return this.getName(); } } diff --git a/src/main/java/org/icatproject/ids/models/DatasetInfo.java b/src/main/java/org/icatproject/ids/models/DatasetInfo.java index a7ad9de2..be4d9a77 100644 --- a/src/main/java/org/icatproject/ids/models/DatasetInfo.java +++ b/src/main/java/org/icatproject/ids/models/DatasetInfo.java @@ -7,8 +7,8 @@ import org.icatproject.ids.plugin.DsInfo; /** - * Contains information about a Dataset. Replaces DsInfo in v3. May should - * implement DsInfo interface + * Contains information about a Dataset. Replaces DsInfo in v3. + * May should implement DsInfo interface */ public class DatasetInfo extends DataInfoBase implements DsInfo { @@ -18,14 +18,14 @@ public class DatasetInfo extends DataInfoBase implements DsInfo { protected String investigationName; protected String visitId; + public DatasetInfo(Dataset ds) throws InsufficientPrivilegesException { - super(ds.getId(), ds.getName(), ds.getLocation()); + super(ds.getId(), ds.getName(), ds.getLocation()); Investigation investigation = ds.getInvestigation(); if (investigation == null) { throw new InsufficientPrivilegesException( - "Probably not able to read Investigation for dataset id " - + ds.getId()); + "Probably not able to read Investigation for dataset id " + ds.getId()); } Facility facility = investigation.getFacility(); if (facility == null) { @@ -41,9 +41,8 @@ public DatasetInfo(Dataset ds) throws InsufficientPrivilegesException { this.facilityName = facility.getName(); } - public DatasetInfo(Long id, String name, String location, - Long investigationId, String investigationName, String visitId, - Long facilityId, String facilityName) { + + public DatasetInfo(Long id, String name, String location, Long investigationId, String investigationName, String visitId, Long facilityId, String facilityName) { super(id, name, location); this.facilityId = facilityId; @@ -55,8 +54,7 @@ public DatasetInfo(Long id, String name, String location, @Override public String toString() { - return this.investigationId + "/" + this.id + " (" + this.facilityName - + "/" + this.investigationName + "/" + this.visitId + "/" + return this.investigationId + "/" + this.id + " (" + this.facilityName + "/" + this.investigationName + "/" + this.visitId + "/" + this.name + ")"; } @@ -80,31 +78,18 @@ public String getVisitId() { return visitId; } + // implementing DsInfo @Override - public Long getDsId() { - return this.getId(); - } - + public Long getDsId() { return this.getId(); } @Override - public String getDsName() { - return this.getName(); - } - + public String getDsName() { return this.getName(); } @Override - public String getDsLocation() { - return this.getLocation(); - } - + public String getDsLocation() { return this.getLocation(); } @Override - public Long getInvId() { - return this.getInvestigationId(); - } - + public Long getInvId() { return this.getInvestigationId(); } @Override - public String getInvName() { - return this.getInvestigationName(); - } + public String getInvName() { return this.getInvestigationName(); } } diff --git a/src/main/java/org/icatproject/ids/models/Prepared.java b/src/main/java/org/icatproject/ids/models/Prepared.java index 2e557916..5e04d830 100644 --- a/src/main/java/org/icatproject/ids/models/Prepared.java +++ b/src/main/java/org/icatproject/ids/models/Prepared.java @@ -23,9 +23,9 @@ /* This is a POJO with only package access so don't make data private */ public class Prepared { - protected final static Logger logger = LoggerFactory - .getLogger(RequestHandlerBase.class); + protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); + public SortedMap dsInfos; public SortedMap dfInfos; public Set emptyDatasets; @@ -33,9 +33,7 @@ public class Prepared { public boolean zip; public boolean compress; - public Prepared(SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - long fileLength) { + public Prepared(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength) { this.dsInfos = dsInfos; this.dfInfos = dfInfos; this.emptyDatasets = emptyDatasets; @@ -44,17 +42,15 @@ public Prepared(SortedMap dsInfos, this.compress = false; } - public Prepared(SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - long fileLength, Boolean zip, Boolean compress) { + public Prepared(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength, Boolean zip, Boolean compress) { this(dsInfos, dfInfos, emptyDatasets, fileLength); this.zip = zip; this.compress = compress; } - public static void pack(OutputStream stream, boolean zip, boolean compress, - Map dsInfos, Map dfInfos, - Set emptyDatasets, long fileLength) { + + public static void pack(OutputStream stream, boolean zip, boolean compress, Map dsInfos, + Map dfInfos, Set emptyDatasets, long fileLength) { JsonGenerator gen = Json.createGenerator(stream); gen.writeStartObject(); gen.write("zip", zip); @@ -63,16 +59,13 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, gen.writeStartArray("dsInfo"); for (DataInfoBase dataInfo : dsInfos.values()) { - var dsInfo = (DatasetInfo) dataInfo; + var dsInfo = (DatasetInfo)dataInfo; logger.debug("dsInfo " + dsInfo); gen.writeStartObject().write("dsId", dsInfo.getId()) - .write("dsName", dsInfo.getDsName()) - .write("facilityId", dsInfo.getFacilityId()) - .write("facilityName", dsInfo.getFacilityName()) - .write("invId", dsInfo.getInvId()) - .write("invName", dsInfo.getInvName()) - .write("visitId", dsInfo.getVisitId()); + .write("dsName", dsInfo.getDsName()).write("facilityId", dsInfo.getFacilityId()) + .write("facilityName", dsInfo.getFacilityName()).write("invId", dsInfo.getInvId()) + .write("invName", dsInfo.getInvName()).write("visitId", dsInfo.getVisitId()); if (dsInfo.getDsLocation() != null) { gen.write("dsLocation", dsInfo.getDsLocation()); } else { @@ -84,12 +77,10 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, gen.writeStartArray("dfInfo"); for (DataInfoBase dataInfo : dfInfos.values()) { - var dfInfo = (DatafileInfo) dataInfo; + var dfInfo = (DatafileInfo)dataInfo; DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId()); - gen.writeStartObject().write("dsId", dsInfo.getId()) - .write("dfId", dfInfo.getId()) - .write("dfName", dfInfo.getDfName()) - .write("createId", dfInfo.getCreateId()) + gen.writeStartObject().write("dsId", dsInfo.getId()).write("dfId", dfInfo.getId()) + .write("dfName", dfInfo.getDfName()).write("createId", dfInfo.getCreateId()) .write("modId", dfInfo.getModId()); if (dfInfo.getDfLocation() != null) { gen.write("dfLocation", dfInfo.getDfLocation()); @@ -111,6 +102,7 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, } + public static Prepared unpack(InputStream stream) throws InternalException { JsonObject pd; @@ -119,42 +111,35 @@ public static Prepared unpack(InputStream stream) throws InternalException { } var zip = pd.getBoolean("zip"); var compress = pd.getBoolean("compress"); - var fileLength = pd.containsKey("fileLength") ? pd.getInt("fileLength") - : 0; + var fileLength = pd.containsKey("fileLength") ? pd.getInt("fileLength") : 0; SortedMap dsInfos = new TreeMap<>(); SortedMap dfInfos = new TreeMap<>(); Set emptyDatasets = new HashSet<>(); for (JsonValue itemV : pd.getJsonArray("dfInfo")) { JsonObject item = (JsonObject) itemV; - String dfLocation = item.isNull("dfLocation") ? null - : item.getString("dfLocation"); + String dfLocation = item.isNull("dfLocation") ? null : item.getString("dfLocation"); long dfid = item.getJsonNumber("dfId").longValueExact(); - dfInfos.put(dfid, - new DatafileInfo(dfid, item.getString("dfName"), dfLocation, - item.getString("createId"), item.getString("modId"), - item.getJsonNumber("dsId").longValueExact())); + dfInfos.put(dfid, new DatafileInfo(dfid, item.getString("dfName"), + dfLocation, item.getString("createId"), item.getString("modId"), + item.getJsonNumber("dsId").longValueExact())); } for (JsonValue itemV : pd.getJsonArray("dsInfo")) { JsonObject item = (JsonObject) itemV; long dsId = item.getJsonNumber("dsId").longValueExact(); - String dsLocation = item.isNull("dsLocation") ? null - : item.getString("dsLocation"); - dsInfos.put(dsId, new DatasetInfo(dsId, item.getString("dsName"), - dsLocation, item.getJsonNumber("invId").longValueExact(), - item.getString("invName"), item.getString("visitId"), - item.getJsonNumber("facilityId").longValueExact(), - item.getString("facilityName"))); + String dsLocation = item.isNull("dsLocation") ? null : item.getString("dsLocation"); + dsInfos.put(dsId, new DatasetInfo(dsId, item.getString("dsName"), dsLocation, + item.getJsonNumber("invId").longValueExact(), item.getString("invName"), item.getString("visitId"), + item.getJsonNumber("facilityId").longValueExact(), item.getString("facilityName"))); } for (JsonValue itemV : pd.getJsonArray("emptyDs")) { emptyDatasets.add(((JsonNumber) itemV).longValueExact()); } - return new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength, zip, - compress); + return new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength, zip, compress); } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java index df19f640..032e8437 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java @@ -1,5 +1,6 @@ package org.icatproject.ids.requestHandlers; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; @@ -11,17 +12,13 @@ public class ArchiveHandler extends DataRequestHandler { - public ArchiveHandler(String ip, String sessionId, String investigationIds, - String datasetIds, String datafileIds) { - super(RequestType.ARCHIVE, ip, sessionId, investigationIds, datasetIds, - datafileIds); + public ArchiveHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.ARCHIVE, ip, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws NotImplementedException, InternalException { - + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws NotImplementedException, InternalException { + dataSelectionService.scheduleTasks(DeferredOp.ARCHIVE); return ValueContainer.getVoid(); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java index e7ea350b..a4cb44a9 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java @@ -26,52 +26,43 @@ import org.icatproject.ids.services.LockManager.LockType; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; + public class DeleteHandler extends DataRequestHandler { - public DeleteHandler(String ip, String sessionId, String investigationIds, - String datasetIds, String datafileIds) { - super(RequestType.DELETE, ip, sessionId, investigationIds, datasetIds, - datafileIds); + public DeleteHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.DELETE, ip, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { if (readOnly) { - throw new NotImplementedException( - "This operation has been configured to be unavailable"); + throw new NotImplementedException("This operation has been configured to be unavailable"); } var serviceProvider = ServiceProvider.getInstance(); // Do it - try (Lock lock = serviceProvider.getLockManager().lock( - dataSelectionService.getDsInfo().values(), - LockType.EXCLUSIVE)) { + try (Lock lock = serviceProvider.getLockManager().lock(dataSelectionService.getDsInfo().values(), LockType.EXCLUSIVE)) { if (storageUnit == StorageUnit.DATASET) { dataSelectionService.checkOnline(); } /* Now delete from ICAT */ List dfs = new ArrayList<>(); - for (DataInfoBase dfInfo : dataSelectionService.getDfInfo() - .values()) { + for (DataInfoBase dfInfo : dataSelectionService.getDfInfo().values()) { Datafile df = new Datafile(); df.setId(dfInfo.getId()); dfs.add(df); } try { - serviceProvider.getIcat() - .deleteMany(this.dataController.getOperationId(), dfs); + serviceProvider.getIcat().deleteMany(this.dataController.getOperationId(), dfs); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES - || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java index c265418c..7c22c38f 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java @@ -21,31 +21,25 @@ public class GetDataFileIdsHandler extends DataRequestHandler { - public GetDataFileIdsHandler(String ip, String preparedId, String sessionId, - String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETDATAFILEIDS, ip, preparedId, sessionId, - investigationIds, datasetIds, datafileIds); + public GetDataFileIdsHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETDATAFILEIDS, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos) - .writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { - if (this.dataController instanceof PreparedDataController) { + if(this.dataController instanceof PreparedDataController) { gen.write("zip", dataSelectionService.getZip()); gen.write("compress", dataSelectionService.getCompress()); } gen.writeStartArray("ids"); - for (DataInfoBase dfInfo : dataSelectionService.getDfInfo() - .values()) { + for (DataInfoBase dfInfo : dataSelectionService.getDfInfo().values()) { gen.write(dfInfo.getId()); } gen.writeEnd().writeEnd().close(); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java index a3ef818b..8cdb64ce 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java @@ -45,48 +45,40 @@ public class GetDataHandler extends DataRequestHandler { String range; Long transferId; - public GetDataHandler(String ip, String preparedId, String sessionId, - String investigationIds, String datasetIds, String datafileIds, - Boolean compress, Boolean zip, String outname, String range) { - super(RequestType.GETDATA, ip, preparedId, sessionId, investigationIds, - datasetIds, datafileIds); - - this.initializeAdditionallParameters(sessionId, compress, zip, outname, - range); - } + public GetDataHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds, Boolean compress, Boolean zip, String outname, String range) { + super(RequestType.GETDATA, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds ); - private void initializeAdditionallParameters(String sessionId, - Boolean compress, Boolean zip, String outname, String range) { + this.initializeAdditionallParameters(sessionId, compress, zip, outname, range); + } + private void initializeAdditionallParameters(String sessionId, Boolean compress, Boolean zip, String outname, String range) { + this.outname = outname; this.range = range; this.transferId = null; this.rangeRe = Pattern.compile("bytes=(\\d+)-"); - if (sessionId != null) { + if(sessionId != null) { this.compress = compress; this.zip = zip; } } + @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) throws InternalException, - NotImplementedException, BadRequestException, NotFoundException, - InsufficientPrivilegesException, DataNotOnlineException { + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws InternalException, NotImplementedException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException { - if (this.compress == null || this.zip == null) { + if(this.compress == null || this.zip == null) { this.zip = dataSelectionService.getZip(); this.compress = dataSelectionService.getCompress(); } long offset = 0; - if (range != null) { + if ( range != null) { Matcher m = rangeRe.matcher(range); if (!m.matches()) { - throw new BadRequestException( - "The range must match " + rangeRe.pattern()); + throw new BadRequestException("The range must match " + rangeRe.pattern()); } offset = Long.parseLong(m.group(1)); logger.debug("Range " + range + " -> offset " + offset); @@ -95,24 +87,20 @@ public ValueContainer handleDataRequest( return new ValueContainer(this.getData(dataSelectionService, offset)); } - private Response getData(DataSelectionService dataSelectionService, - final long offset) throws BadRequestException, NotFoundException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { - long start = System.currentTimeMillis(); + private Response getData(DataSelectionService dataSelectionService, final long offset) throws BadRequestException, + NotFoundException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - var length = this.zip ? OptionalLong.empty() - : dataSelectionService.getFileLength(); + long start = System.currentTimeMillis(); + + var length = this.zip ? OptionalLong.empty() : dataSelectionService.getFileLength(); - final boolean finalZip = this.dataController.mustZip(zip, - dataSelectionService); + final boolean finalZip = this.dataController.mustZip(zip, dataSelectionService); Lock lock = null; try { var serviceProvider = ServiceProvider.getInstance(); - lock = serviceProvider.getLockManager().lock( - dataSelectionService.getDsInfo().values(), LockType.SHARED); + lock = serviceProvider.getLockManager().lock(dataSelectionService.getDsInfo().values(), LockType.SHARED); if (twoLevel) { dataSelectionService.checkOnline(); @@ -123,16 +111,13 @@ private Response getData(DataSelectionService dataSelectionService, String name; if (outname == null) { if (finalZip) { - name = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss") - .format(new Date()) + ".zip"; + name = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()) + ".zip"; } else { - name = dataSelectionService.getDfInfo().values().iterator() - .next().getName(); + name = dataSelectionService.getDfInfo().values().iterator().next().getName(); } } else { if (finalZip) { - String ext = outname.substring(outname.lastIndexOf(".") + 1, - outname.length()); + String ext = outname.substring(outname.lastIndexOf(".") + 1, outname.length()); if ("zip".equals(ext)) { name = outname; } else { @@ -143,24 +128,17 @@ private Response getData(DataSelectionService dataSelectionService, } } - if (ServiceProvider.getInstance().getPropertyHandler().getLogSet() - .contains(this.getCallType())) { + if (ServiceProvider.getInstance().getPropertyHandler().getLogSet().contains(this.getCallType())) { transferId = transferIdCounter.getAndIncrement(); } - var response = Response - .status(offset == 0 ? HttpURLConnection.HTTP_OK - : HttpURLConnection.HTTP_PARTIAL) - .entity(new SO(dataSelectionService.getDsInfo(), - dataSelectionService.getDfInfo(), offset, finalZip, - compress, lock, transferId, ip, start, - serviceProvider)) - .header("Content-Disposition", - "attachment; filename=\"" + name + "\"") - .header("Accept-Ranges", "bytes"); - length.stream().map(l -> Math.max(0L, l - offset)) + var response = Response.status(offset == 0 ? HttpURLConnection.HTTP_OK : HttpURLConnection.HTTP_PARTIAL) + .entity(new SO(dataSelectionService.getDsInfo(), dataSelectionService.getDfInfo(), offset, finalZip, compress, lock, transferId, ip, start, serviceProvider)) + .header("Content-Disposition", "attachment; filename=\"" + name + "\"").header("Accept-Ranges", "bytes"); + length.stream() + .map(l -> Math.max(0L, l - offset)) .forEach(l -> response.header(CONTENT_LENGTH, l)); - + return response.build(); } catch (AlreadyLockedException e) { @@ -168,7 +146,7 @@ private Response getData(DataSelectionService dataSelectionService, throw new DataNotOnlineException("Data is busy"); } catch (IOException e) { // if (lock != null) { - // lock.release(); + // lock.release(); // } logger.error("I/O error " + e.getMessage()); throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -178,48 +156,38 @@ private Response getData(DataSelectionService dataSelectionService, } } + private void checkDatafilesPresent(Collection dfInfos) throws NotFoundException, InternalException { var serviceProvider = ServiceProvider.getInstance(); /* Check that datafiles have not been deleted before locking */ int n = 0; - StringBuffer sb = new StringBuffer( - "SELECT COUNT(df) from Datafile df WHERE (df.id in ("); + StringBuffer sb = new StringBuffer("SELECT COUNT(df) from Datafile df WHERE (df.id in ("); for (DataInfoBase dfInfo : dfInfos) { if (n != 0) { sb.append(','); } sb.append(dfInfo.getId()); - if (++n == serviceProvider.getPropertyHandler() - .getMaxIdsInQuery()) { + if (++n == serviceProvider.getPropertyHandler().getMaxIdsInQuery()) { try { - if (((Long) serviceProvider.getIcatReader() - .search(sb.append("))").toString()).get(0)) - .intValue() != n) { - throw new NotFoundException( - "One of the data files requested has been deleted"); + if (((Long) serviceProvider.getIcatReader().search(sb.append("))").toString()).get(0)).intValue() != n) { + throw new NotFoundException("One of the data files requested has been deleted"); } n = 0; - sb = new StringBuffer( - "SELECT COUNT(df) from Datafile df WHERE (df.id in ("); + sb = new StringBuffer("SELECT COUNT(df) from Datafile df WHERE (df.id in ("); } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } } } if (n != 0) { try { - if (((Long) serviceProvider.getIcatReader() - .search(sb.append("))").toString()).get(0)) - .intValue() != n) { - throw new NotFoundException( - "One of the datafiles requested has been deleted"); + if (((Long) serviceProvider.getIcatReader().search(sb.append("))").toString()).get(0)).intValue() != n) { + throw new NotFoundException("One of the datafiles requested has been deleted"); } } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } } @@ -232,8 +200,7 @@ public CallType getCallType() { @Override public void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - if (transferId == null) - transferId = -1L; + if(transferId == null) transferId = -1L; gen.write("transferId", transferId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java index 70942624..94a78919 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java @@ -12,6 +12,7 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; import org.icatproject.ids.services.ServiceProvider; + public class GetIcatUrlHandler extends RequestHandlerBase { public GetIcatUrlHandler(String ip) { @@ -19,12 +20,11 @@ public GetIcatUrlHandler(String ip) { } @Override - public ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { - var propertyHandler = ServiceProvider.getInstance() - .getPropertyHandler(); + var propertyHandler = ServiceProvider.getInstance().getPropertyHandler(); return new ValueContainer(propertyHandler.getIcatUrl()); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java index e2c1b106..713c3603 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java @@ -28,27 +28,24 @@ public GetServiceStatusHandler(String ip, String sessionId) { super(RequestType.GETSERVICESTATUS, ip); this.sessionId = sessionId; - rootUserNames = ServiceProvider.getInstance().getPropertyHandler() - .getRootUserNames(); + rootUserNames = ServiceProvider.getInstance().getPropertyHandler().getRootUserNames(); this.serviceProvider = ServiceProvider.getInstance(); } @Override - public ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { try { String uname = serviceProvider.getIcat().getUserName(sessionId); if (!rootUserNames.contains(uname)) { - throw new InsufficientPrivilegesException(uname - + " is not included in the ids rootUserNames set."); + throw new InsufficientPrivilegesException(uname + " is not included in the ids rootUserNames set."); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); if (type == IcatExceptionType.SESSION) { - throw new InsufficientPrivilegesException( - e.getClass() + " " + e.getMessage()); + throw new InsufficientPrivilegesException(e.getClass() + " " + e.getMessage()); } throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -57,8 +54,7 @@ public ValueContainer handleRequest() throws BadRequestException, } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { gen.write("userName", serviceProvider.getIcat().getUserName(sessionId)); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java index 8f4c145f..9da9787c 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java @@ -19,27 +19,21 @@ public class GetStatusHandler extends DataRequestHandler { - public GetStatusHandler(String ip, String preparedId, String sessionId, - String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETSTATUS, ip, preparedId, sessionId, - investigationIds, datasetIds, datafileIds); + public GetStatusHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETSTATUS, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { Status status = Status.ONLINE; var serviceProvider = ServiceProvider.getInstance(); Set restoring = serviceProvider.getFsm().getRestoring(); - Set maybeOffline = serviceProvider.getFsm() - .getMaybeOffline(); - for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos() - .values()) { + Set maybeOffline = serviceProvider.getFsm().getMaybeOffline(); + for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (restoring.contains(dataInfo)) { status = Status.RESTORING; diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java index 53660e58..6f2deba7 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java @@ -34,23 +34,17 @@ class PreparedStatus { private Map preparedStatusMap = new ConcurrentHashMap<>(); @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { // Do it boolean prepared = true; - PreparedStatus status = preparedStatusMap.computeIfAbsent( - this.dataController.getOperationId(), - k -> new PreparedStatus()); + PreparedStatus status = preparedStatusMap.computeIfAbsent(this.dataController.getOperationId(), k -> new PreparedStatus()); if (!status.lock.tryLock()) { - logger.debug( - "Lock held for evaluation of isPrepared for preparedId {}", - this.dataController.getOperationId()); + logger.debug("Lock held for evaluation of isPrepared for preparedId {}", this.dataController.getOperationId()); return new ValueContainer(false); } try { @@ -60,23 +54,19 @@ public ValueContainer handleDataRequest( try { future.get(); } catch (ExecutionException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (InterruptedException e) { // Ignore } finally { status.future = null; } } else { - logger.debug( - "Background process still running for preparedId {}", - this.dataController.getOperationId()); + logger.debug("Background process still running for preparedId {}", this.dataController.getOperationId()); return new ValueContainer(false); } } - prepared = dataSelectionService - .isPrepared(this.dataController.getOperationId()); + prepared = dataSelectionService.isPrepared(this.dataController.getOperationId()); return new ValueContainer(prepared); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java index 15c93a26..a7acf20e 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java @@ -12,16 +12,16 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; public class IsReadOnlyHandler extends RequestHandlerBase { - + public IsReadOnlyHandler(String ip) { super(RequestType.ISREADONLY, ip); } @Override - public ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { return new ValueContainer(this.readOnly); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java index 1ffd2326..f9cc3be1 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java @@ -18,10 +18,10 @@ public IsTwoLevelHandler(String ip) { } @Override - public ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { - + public ValueContainer handleRequest() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + return new ValueContainer(twoLevel); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java index 8d2acde0..625c4ac2 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java @@ -28,25 +28,20 @@ public class PrepareDataHandler extends DataRequestHandler { Boolean zip; String preparedId; - public PrepareDataHandler(String ip, String sessionId, - String investigationIds, String datasetIds, String datafileIds, - Boolean compress, Boolean zip) { - super(RequestType.PREPAREDATA, ip, sessionId, investigationIds, - datasetIds, datafileIds); + public PrepareDataHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds, Boolean compress, Boolean zip) { + super(RequestType.PREPAREDATA,ip, sessionId, investigationIds, datasetIds, datafileIds); this.zip = zip; this.compress = compress; } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { preparedId = UUID.randomUUID().toString(); - + dataSelectionService.restoreDataInfos(); if (dataSelectionService.mustZip()) { @@ -54,14 +49,15 @@ public ValueContainer handleDataRequest( } logger.debug("Writing to " + preparedDir.resolve(preparedId)); - try (OutputStream stream = new BufferedOutputStream( - Files.newOutputStream(preparedDir.resolve(preparedId)))) { - Prepared.pack(stream, zip, compress, - dataSelectionService.getDsInfo(), - dataSelectionService.getDfInfo(), - dataSelectionService.getEmptyDatasets(), - dataSelectionService.getFileLength().isEmpty() ? 0 - : dataSelectionService.getFileLength().getAsLong()); + try (OutputStream stream = new BufferedOutputStream(Files.newOutputStream(preparedDir.resolve(preparedId)))) { + Prepared.pack( stream, + zip, + compress, + dataSelectionService.getDsInfo(), + dataSelectionService.getDfInfo(), + dataSelectionService.getEmptyDatasets(), + dataSelectionService.getFileLength().isEmpty() ? 0 : dataSelectionService.getFileLength().getAsLong() + ); } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -72,14 +68,13 @@ public ValueContainer handleDataRequest( } @Override - public String addCustomParametersToLogString() { + public String addCustomParametersToLogString() { return "zip='" + this.zip + "'' compress='" + compress + "'"; } @Override public void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - if (preparedId == null) - preparedId = ""; + if(preparedId == null) preparedId = ""; gen.write(RequestIdNames.preparedId, preparedId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java index 91778039..3e9bfbd9 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java @@ -79,10 +79,9 @@ public class PutHandler extends RequestHandlerBase { paddedPrefix += "*/window.name='"; } - public PutHandler(String ip, String sessionId, InputStream body, - String name, String datafileFormatIdString, String datasetIdString, - String description, String doi, String datafileCreateTimeString, - String datafileModTimeString, boolean wrap, boolean padding) { + public PutHandler( String ip, String sessionId, InputStream body, String name, String datafileFormatIdString, + String datasetIdString, String description, String doi, String datafileCreateTimeString, + String datafileModTimeString, boolean wrap, boolean padding) { super(RequestType.PUT, ip); this.sessionId = sessionId; @@ -106,61 +105,52 @@ public PutHandler(String ip, String sessionId, InputStream body, } catch (Throwable e) { logger.error("Won't start ", e); - throw new RuntimeException("PutHandler reports " + e.getClass() - + " " + e.getMessage()); + throw new RuntimeException("PutHandler reports " + e.getClass() + " " + e.getMessage()); } } @Override - public ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { try { if (readOnly) { - throw new NotImplementedException( - "This operation has been configured to be unavailable"); + throw new NotImplementedException("This operation has been configured to be unavailable"); } - DataControllerBase.validateUUID(RequestIdNames.sessionId, - sessionId); + DataControllerBase.validateUUID(RequestIdNames.sessionId, sessionId); if (name == null) { throw new BadRequestException("The name parameter must be set"); } if (datafileFormatIdString == null) { - throw new BadRequestException( - "The datafileFormatId parameter must be set"); + throw new BadRequestException("The datafileFormatId parameter must be set"); } long datafileFormatId; try { datafileFormatId = Long.parseLong(datafileFormatIdString); } catch (NumberFormatException e) { - throw new BadRequestException( - "The datafileFormatId parameter must be numeric"); + throw new BadRequestException("The datafileFormatId parameter must be numeric"); } if (datasetIdString == null) { - throw new BadRequestException( - "The datasetId parameter must be set"); + throw new BadRequestException("The datasetId parameter must be set"); } long datasetId; try { datasetId = Long.parseLong(datasetIdString); } catch (NumberFormatException e) { - throw new BadRequestException( - "The datasetId parameter must be numeric"); + throw new BadRequestException("The datasetId parameter must be numeric"); } Long datafileCreateTime = null; if (datafileCreateTimeString != null) { try { - datafileCreateTime = Long - .parseLong(datafileCreateTimeString); + datafileCreateTime = Long.parseLong(datafileCreateTimeString); } catch (NumberFormatException e) { - throw new BadRequestException( - "The datafileCreateTime parameter must be numeric"); + throw new BadRequestException("The datafileCreateTime parameter must be numeric"); } } @@ -169,20 +159,17 @@ public ValueContainer handleRequest() throws BadRequestException, try { datafileModTime = Long.parseLong(datafileModTimeString); } catch (NumberFormatException e) { - throw new BadRequestException( - "The datafileModTime parameter must be numeric"); + throw new BadRequestException("The datafileModTime parameter must be numeric"); } } // Do it Dataset ds; try { - ds = (Dataset) serviceProvider.getIcat().get(sessionId, - "Dataset INCLUDE Investigation, Facility", datasetId); + ds = (Dataset) serviceProvider.getIcat().get(sessionId, "Dataset INCLUDE Investigation, Facility", datasetId); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES - || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -192,74 +179,59 @@ public ValueContainer handleRequest() throws BadRequestException, } DatasetInfo dsInfo = new DatasetInfo(ds); - try (Lock lock = serviceProvider.getLockManager().lock(dsInfo, - LockType.SHARED)) { + try (Lock lock = serviceProvider.getLockManager().lock(dsInfo, LockType.SHARED)) { if (storageUnit == StorageUnit.DATASET) { var dfInfos = new TreeMap(); Set emptyDatasets = new HashSet<>(); try { - List counts = serviceProvider.getIcat().search( - sessionId, "COUNT(Datafile) <-> Dataset [id=" - + dsInfo.getId() + "]"); + List counts = serviceProvider.getIcat().search(sessionId, + "COUNT(Datafile) <-> Dataset [id=" + dsInfo.getId() + "]"); if ((Long) counts.get(0) == 0) { emptyDatasets.add(dsInfo.getId()); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES - || type == IcatExceptionType.SESSION) { - throw new InsufficientPrivilegesException( - e.getMessage()); + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } - throw new InternalException( - type + " " + e.getMessage()); + throw new InternalException(type + " " + e.getMessage()); } var dsInfos = new TreeMap(); dsInfos.put(dsInfo.getId(), dsInfo); - DataSelectionService dataSelectionService = DataSelectionServiceFactory - .getService(dsInfos, dfInfos, emptyDatasets, 0, - this.getRequestType()); + DataSelectionService dataSelectionService = DataSelectionServiceFactory.getService(dsInfos, dfInfos, emptyDatasets, 0, this.getRequestType()); dataSelectionService.checkOnline(); } CRC32 crc = new CRC32(); - CheckedWithSizeInputStream is = new CheckedWithSizeInputStream( - body, crc); + CheckedWithSizeInputStream is = new CheckedWithSizeInputStream(body, crc); String location; try { - location = serviceProvider.getMainStorage().put(dsInfo, - name, is); + location = serviceProvider.getMainStorage().put(dsInfo, name, is); } catch (IllegalArgumentException e) { - throw new BadRequestException( - "Illegal filename or dataset: " + e.getMessage()); + throw new BadRequestException("Illegal filename or dataset: " + e.getMessage()); } is.close(); long checksum = crc.getValue(); long size = is.getSize(); - + try { - dfId = registerDatafile(sessionId, name, datafileFormatId, - location, checksum, size, ds, description, doi, - datafileCreateTime, datafileModTime); - } catch (InsufficientPrivilegesException | NotFoundException - | InternalException | BadRequestException e) { - logger.debug("Problem with registration " + e.getClass() - + " " + e.getMessage() + dfId = registerDatafile(sessionId, name, datafileFormatId, location, checksum, size, ds, + description, doi, datafileCreateTime, datafileModTime); + } catch (InsufficientPrivilegesException | NotFoundException | InternalException + | BadRequestException e) { + logger.debug("Problem with registration " + e.getClass() + " " + e.getMessage() + " datafile will now be deleted"); String userId = null; try { - userId = serviceProvider.getIcat() - .getUserName(sessionId); + userId = serviceProvider.getIcat().getUserName(sessionId); } catch (IcatException_Exception e1) { - logger.error("Unable to get user name for session " - + sessionId + " so mainStorage.delete of " + logger.error("Unable to get user name for session " + sessionId + " so mainStorage.delete of " + location + " may fail"); } - serviceProvider.getMainStorage().delete(location, userId, - userId); + serviceProvider.getMainStorage().delete(location, userId, userId); throw e; } @@ -268,80 +240,62 @@ public ValueContainer handleRequest() throws BadRequestException, } else if (storageUnit == StorageUnit.DATAFILE) { Datafile df; try { - df = (Datafile) serviceProvider.getIcatReader() - .get("Datafile", dfId); + df = (Datafile) serviceProvider.getIcatReader().get("Datafile", dfId); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() - + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } - serviceProvider.getFsm() - .queue(new DatafileInfo(dfId, name, location, - df.getCreateId(), df.getModId(), - dsInfo.getId()), DeferredOp.WRITE); + serviceProvider.getFsm().queue(new DatafileInfo(dfId, name, location, df.getCreateId(), df.getModId(), dsInfo.getId()), + DeferredOp.WRITE); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Json.createGenerator(baos).writeStartObject().write("id", dfId) - .write("checksum", checksum) - .write("location", - location.replace("\\", "\\\\").replace("'", - "\\'")) - .write("size", size).writeEnd().close(); - String resp = wrap ? prefix + baos.toString() + suffix - : baos.toString(); - - return new ValueContainer( - Response.status(HttpURLConnection.HTTP_CREATED) - .entity(resp).build()); + Json.createGenerator(baos).writeStartObject().write("id", dfId).write("checksum", checksum) + .write("location", location.replace("\\", "\\\\").replace("'", "\\'")).write("size", size) + .writeEnd().close(); + String resp = wrap ? prefix + baos.toString() + suffix : baos.toString(); + + return new ValueContainer(Response.status(HttpURLConnection.HTTP_CREATED).entity(resp).build()); } catch (AlreadyLockedException e) { logger.debug("Could not acquire lock, put failed"); throw new DataNotOnlineException("Data is busy"); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " putting " - + name + " to Dataset with id " + datasetIdString); - throw new InternalException( - e.getClass() + " " + e.getMessage()); + logger.error("I/O exception " + e.getMessage() + " putting " + name + " to Dataset with id " + + datasetIdString); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IdsException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen.writeStartObject().write("code", e.getClass().getSimpleName()) - .write("message", e.getShortMessage()); + gen.writeStartObject().write("code", e.getClass().getSimpleName()).write("message", e.getShortMessage()); gen.writeEnd().close(); if (wrap) { String pre = padding ? paddedPrefix : prefix; - return new ValueContainer(Response - .status(e.getHttpStatusCode()).entity(pre - + baos.toString().replace("'", "\\'") + suffix) + return new ValueContainer(Response.status(e.getHttpStatusCode()).entity(pre + baos.toString().replace("'", "\\'") + suffix) .build()); } else { - return new ValueContainer(Response.status(e.getHttpStatusCode()) - .entity(baos.toString()).build()); + return new ValueContainer(Response.status(e.getHttpStatusCode()).entity(baos.toString()).build()); } } } - private Long registerDatafile(String sessionId, String name, - long datafileFormatId, String location, long checksum, long size, - Dataset dataset, String description, String doi, - Long datafileCreateTime, Long datafileModTime) - throws InsufficientPrivilegesException, NotFoundException, - InternalException, BadRequestException { + private Long registerDatafile(String sessionId, String name, long datafileFormatId, String location, long checksum, + long size, Dataset dataset, String description, String doi, Long datafileCreateTime, Long datafileModTime) + throws InsufficientPrivilegesException, NotFoundException, InternalException, BadRequestException { + + var serviceProvider = ServiceProvider.getInstance(); ICAT icat = serviceProvider.getIcat(); - + final Datafile df = new Datafile(); DatafileFormat format; try { - format = (DatafileFormat) icat.get(sessionId, "DatafileFormat", - datafileFormatId); + format = (DatafileFormat) icat.get(sessionId, "DatafileFormat", datafileFormatId); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES - || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -361,14 +315,12 @@ private Long registerDatafile(String sessionId, String name, if (datafileCreateTime != null) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTimeInMillis(datafileCreateTime); - df.setDatafileCreateTime( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); + df.setDatafileCreateTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); } if (datafileModTime != null) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTimeInMillis(datafileModTime); - df.setDatafileModTime( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); + df.setDatafileModTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); } try { long dfId = icat.create(sessionId, df); @@ -376,18 +328,15 @@ private Long registerDatafile(String sessionId, String name, String key = serviceProvider.getPropertyHandler().getKey(); if (key != null) { - df.setLocation(location + " " - + IcatSecurity.digest(dfId, location, key)); + df.setLocation(location + " " + IcatSecurity.digest(dfId, location, key)); icat.update(sessionId, df); } - logger.debug("Registered datafile for dataset {} for {}", - dataset.getId(), name + " at " + location); + logger.debug("Registered datafile for dataset {} for {}", dataset.getId(), name + " at " + location); return dfId; } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES - || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.VALIDATION) { @@ -401,15 +350,12 @@ private Long registerDatafile(String sessionId, String name, public String addParametersToLogString() { return "name='" + name + "' " + "datafileFormatId='" - + datafileFormatIdString + "' " + "datasetId='" - + datasetIdString + "' " + "description='" + description + "' " - + "doi='" + doi + "' " + "datafileCreateTime='" - + datafileCreateTimeString + "' " + "datafileModTime='" - + datafileModTimeString + "'"; + + datafileFormatIdString + "' " + "datasetId='" + datasetIdString + "' " + "description='" + + description + "' " + "doi='" + doi + "' " + "datafileCreateTime='" + datafileCreateTimeString + + "' " + "datafileModTime='" + datafileModTimeString + "'"; } - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { gen.write("userName", serviceProvider.getIcat().getUserName(sessionId)); gen.write("datafileId", dfId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java index b8f6e667..2fff2bb3 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java @@ -15,24 +15,20 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; + public class ResetHandler extends DataRequestHandler { - public ResetHandler(String ip, String preparedId, String sessionId, - String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.RESET, ip, preparedId, sessionId, investigationIds, - datasetIds, datafileIds); + public ResetHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.RESET, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { FiniteStateMachine fsm = ServiceProvider.getInstance().getFsm(); - for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos() - .values()) { + for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { fsm.recordSuccess(dataInfo.getId()); } @@ -43,5 +39,7 @@ public ValueContainer handleDataRequest( public CallType getCallType() { return CallType.MIGRATE; } + + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java index 3f3c1e81..77e0c7e5 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java @@ -15,20 +15,16 @@ public class RestoreHandler extends DataRequestHandler { - public RestoreHandler(String ip, String sessionId, String investigationIds, - String datasetIds, String datafileIds) { - super(RequestType.RESTORE, ip, sessionId, investigationIds, datasetIds, - datafileIds); + public RestoreHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.RESTORE, ip, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - dataSelectionService.scheduleTasks(DeferredOp.RESTORE); + dataSelectionService.scheduleTasks(DeferredOp.RESTORE); return ValueContainer.getVoid(); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java index 31b53ca9..c6f994f4 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java @@ -23,41 +23,33 @@ public class WriteHandler extends DataRequestHandler { - public WriteHandler(String ip, String sessionId, String investigationIds, - String datasetIds, String datafileIds) { - super(RequestType.WRITE, ip, sessionId, investigationIds, datasetIds, - datafileIds); + public WriteHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.WRITE, ip, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var serviceProvider = ServiceProvider.getInstance(); if (!serviceProvider.getPropertyHandler().getEnableWrite()) { - throw new NotImplementedException( - "This operation has been configured to be unavailable"); + throw new NotImplementedException("This operation has been configured to be unavailable"); } Map dsInfos = dataSelectionService.getDsInfo(); - try (Lock lock = serviceProvider.getLockManager().lock(dsInfos.values(), - LockType.SHARED)) { + try (Lock lock = serviceProvider.getLockManager().lock(dsInfos.values(), LockType.SHARED)) { if (twoLevel) { boolean maybeOffline = false; - for (DataInfoBase dataInfo : dataSelectionService - .getPrimaryDataInfos().values()) { + for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { if (!dataSelectionService.existsInMainStorage(dataInfo)) { maybeOffline = true; } } if (maybeOffline) { - throw new DataNotOnlineException( - "Requested data is not online, write request refused"); + throw new DataNotOnlineException("Requested data is not online, write request refused"); } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java b/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java index fefc5a49..1291e98e 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java @@ -19,59 +19,45 @@ public abstract class DataControllerBase { /** * matches standard UUID format of 8-4-4-4-12 hexadecimal digits */ - public static final Pattern uuidRegExp = Pattern.compile( - "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); + public static final Pattern uuidRegExp = Pattern + .compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); - public abstract DataSelectionService provideDataSelectionService( - RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, - InsufficientPrivilegesException, NotImplementedException; + public abstract DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException; public abstract void validateUUID() throws BadRequestException; public abstract String addParametersToLogString(); - public abstract void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException; + public abstract void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException; - public abstract boolean mustZip(boolean zip, - DataSelectionService dataSelectionService); + public abstract boolean mustZip(boolean zip, DataSelectionService dataSelectionService); public abstract String getOperationId(); /** - * returns the current sessionId. If it's null or not defined it should be - * created - * + * returns the current sessionId. If it's null or not defined it should be created * @return - * @throws InternalException + * @throws InternalException */ public abstract String forceGetSessionId() throws InternalException; /** * Provides a validity check for UUIDs - * - * @param thing You can give here a name of the prameter or whatever has - * been checked here (to provide a qualified error message if - * needed). - * @param id The String which has to be checked if it is a valid UUID + * @param thing You can give here a name of the prameter or whatever has been checked here (to provide a qualified error message if needed). + * @param id The String which has to be checked if it is a valid UUID * @throws BadRequestException */ - public static void validateUUID(String thing, String id) - throws BadRequestException { + public static void validateUUID(String thing, String id) throws BadRequestException { if (id == null || !uuidRegExp.matcher(id).matches()) - throw new BadRequestException("The " + thing + " parameter '" + id - + "' is not a valid UUID"); + throw new BadRequestException("The " + thing + " parameter '" + id + "' is not a valid UUID"); } protected String createSessionId() throws InternalException { String sessionId; try { - sessionId = ServiceProvider.getInstance().getIcatReader() - .getSessionId(); + sessionId = ServiceProvider.getInstance().getIcatReader().getSessionId(); } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } return sessionId; } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java index 0a730d33..72576fa5 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java @@ -13,34 +13,29 @@ import jakarta.json.stream.JsonGenerator; + public abstract class DataRequestHandler extends RequestHandlerBase { protected DataControllerBase dataController; - protected DataRequestHandler(RequestType requestType, String ip, - String sessionId, String investigationIds, String datasetIds, - String datafileIds) { + + protected DataRequestHandler(RequestType requestType, String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { super(requestType, ip); - this.dataController = new UnpreparedDataController(sessionId, - investigationIds, datasetIds, datafileIds); + this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); } - protected DataRequestHandler(RequestType requestType, String ip, - String preparedId) { + protected DataRequestHandler(RequestType requestType, String ip, String preparedId) { super(requestType, ip); this.dataController = new PreparedDataController(preparedId); } - protected DataRequestHandler(RequestType requestType, String ip, - String preparedId, String sessionId, String investigationIds, - String datasetIds, String datafileIds) { + protected DataRequestHandler(RequestType requestType, String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { super(requestType, ip); - if (sessionId != null) { - this.dataController = new UnpreparedDataController(sessionId, - investigationIds, datasetIds, datafileIds); + if(sessionId != null) { + this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); } else { this.dataController = new PreparedDataController(preparedId); } @@ -48,14 +43,11 @@ protected DataRequestHandler(RequestType requestType, String ip, } @Override - protected ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { + protected ValueContainer handleRequest() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { this.dataController.validateUUID(); - DataSelectionService dataSelectionService = this.dataController - .provideDataSelectionService(this.requestType); + DataSelectionService dataSelectionService = this.dataController.provideDataSelectionService(this.requestType); ValueContainer result = this.handleDataRequest(dataSelectionService); return result; @@ -63,38 +55,26 @@ protected ValueContainer handleRequest() throws BadRequestException, } @Override - protected void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { this.dataController.addParametersToTransmitterJSON(gen); this.addCustomParametersToTransmitterJSON(gen); } - protected abstract ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws NotImplementedException, InternalException, - BadRequestException, NotFoundException, - InsufficientPrivilegesException, DataNotOnlineException; + protected abstract ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws NotImplementedException, InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException; @Override protected String addParametersToLogString() { - return this.dataController.addParametersToLogString() + " " - + this.addCustomParametersToLogString(); + return this.dataController.addParametersToLogString() + " " + this.addCustomParametersToLogString(); } /** - * Override this method in your concrete DataRequestHandler to add custom - * parameters to the JSON which will be transmitted. - * + * Override this method in your concrete DataRequestHandler to add custom parameters to the JSON which will be transmitted. * @param gen */ - protected void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - } + protected void addCustomParametersToTransmitterJSON(JsonGenerator gen) {} /** - * Override this method in your concrete DataRequestHandler to add custom - * parameters to the log output. + * Override this method in your concrete DataRequestHandler to add custom parameters to the log output. */ - protected String addCustomParametersToLogString() { - return ""; - } + protected String addCustomParametersToLogString() { return "";} } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java b/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java index c15203ab..f9fe6519 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java @@ -24,8 +24,7 @@ public class PreparedDataController extends DataControllerBase { - protected final static Logger logger = LoggerFactory - .getLogger(RequestHandlerBase.class); + protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); String preparedId; @@ -35,21 +34,16 @@ public PreparedDataController(String preparedId) { } @Override - public DataSelectionService provideDataSelectionService( - RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, - InsufficientPrivilegesException, NotImplementedException { - - var preparedDir = ServiceProvider.getInstance().getPropertyHandler() - .getCacheDir().resolve("prepared"); + public DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, + BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + + var preparedDir = ServiceProvider.getInstance().getPropertyHandler().getCacheDir().resolve("prepared"); Prepared prepared; - try (InputStream stream = Files - .newInputStream(preparedDir.resolve(preparedId))) { + try (InputStream stream = Files.newInputStream(preparedDir.resolve(preparedId))) { prepared = Prepared.unpack(stream); } catch (NoSuchFileException e) { - throw new NotFoundException( - "The preparedId " + preparedId + " is not known"); + throw new NotFoundException("The preparedId " + preparedId + " is not known"); } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -68,14 +62,12 @@ public String addParametersToLogString() { } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { gen.write(RequestIdNames.preparedId, this.preparedId); } @Override - public boolean mustZip(boolean zip, - DataSelectionService dataSelectionService) { + public boolean mustZip(boolean zip, DataSelectionService dataSelectionService) { return zip; } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java b/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java index 79d741f5..9524b7a0 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java @@ -23,15 +23,14 @@ import org.icatproject.ids.helpers.ValueContainer; import org.icatproject.ids.services.ServiceProvider; + /** - * This base class represents all common properties and methods which are needed - * by each request handler. Request handlers schould be added to the internal - * request handler list in RequestHandlerService, to be able to be called. + * This base class represents all common properties and methods which are needed by each request handler. + * Request handlers schould be added to the internal request handler list in RequestHandlerService, to be able to be called. */ public abstract class RequestHandlerBase { - protected final static Logger logger = LoggerFactory - .getLogger(RequestHandlerBase.class); + protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); protected Path preparedDir; protected boolean twoLevel; protected StorageUnit storageUnit; @@ -42,10 +41,11 @@ public abstract class RequestHandlerBase { /** * matches standard UUID format of 8-4-4-4-12 hexadecimal digits */ - public static final Pattern uuidRegExp = Pattern.compile( - "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); + public static final Pattern uuidRegExp = Pattern + .compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); + - protected RequestHandlerBase(RequestType requestType, String ip) { + protected RequestHandlerBase(RequestType requestType, String ip ) { this.requestType = requestType; this.ip = ip; @@ -58,9 +58,9 @@ protected RequestHandlerBase(RequestType requestType, String ip) { this.readOnly = propertyHandler.getReadOnly(); } + /** * Informs about the RequestType the handler ist providing a handling for. - * * @return */ public RequestType getRequestType() { @@ -68,9 +68,7 @@ public RequestType getRequestType() { } /** - * The core method of each request handler. It has to be overwritten in the - * concrete implementation to provide an individual request handling - * + * The core method of each request handler. It has to be overwritten in the concrete implementation to provide an individual request handling * @return A ValueContainer with an indiviadual result type. * @throws BadRequestException * @throws InternalException @@ -79,15 +77,11 @@ public RequestType getRequestType() { * @throws DataNotOnlineException * @throws NotImplementedException */ - public ValueContainer handle() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handle() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { // some preprocessing long start = System.currentTimeMillis(); - logger.info("New webservice request: " - + this.requestType.toString().toLowerCase() + " " - + this.addParametersToLogString()); + logger.info("New webservice request: " + this.requestType.toString().toLowerCase() + " " + this.addParametersToLogString()); // Do it ValueContainer result = this.handleRequest(); @@ -99,56 +93,44 @@ public ValueContainer handle() throws BadRequestException, } + private void transmit(long start) throws BadRequestException { - if (ServiceProvider.getInstance().getLogSet() - .contains(this.getCallType())) { + if (ServiceProvider.getInstance().getLogSet().contains(this.getCallType())) { try { String body = this.provideTransmissionBody(); - ServiceProvider.getInstance().getTransmitter() - .processMessage("archive", ip, body, start); + ServiceProvider.getInstance().getTransmitter().processMessage("archive", ip, body, start); } catch (IcatException_Exception e) { - logger.error("Failed to prepare jms message " + e.getClass() - + " " + e.getMessage()); + logger.error("Failed to prepare jms message " + e.getClass() + " " + e.getMessage()); } } } - public String provideTransmissionBody() - throws BadRequestException, IcatException_Exception { + public String provideTransmissionBody() throws BadRequestException, IcatException_Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos) - .writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { this.addParametersToTransmitterJSON(gen); gen.writeEnd(); } return baos.toString(); } + /** - * Override to add additional parameters to the log output for the current - * request - * + * Override to add additional parameters to the log output for the current request * @return */ - protected String addParametersToLogString() { - return ""; - } + protected String addParametersToLogString() { return ""; } /** * Override to add additional parameters to the transmitter JSON - * * @param gen * @throws IcatException_Exception * @throws BadRequestException */ - protected void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { - } + protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException {} /** - * The core method of each request handler. It has to be overwritten in the - * concrete implementation to provide an individual request handling - * + * The core method of each request handler. It has to be overwritten in the concrete implementation to provide an individual request handling * @return A ValueContainer with an indiviadual result type. * @throws BadRequestException * @throws InternalException @@ -157,15 +139,10 @@ protected void addParametersToTransmitterJSON(JsonGenerator gen) * @throws DataNotOnlineException * @throws NotImplementedException */ - protected abstract ValueContainer handleRequest() - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException; + protected abstract ValueContainer handleRequest() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException; /** - * each handler should provide its own CallType which is needed to create - * the Transmitter message - * + * each handler should provide its own CallType which is needed to create the Transmitter message * @return the Calltype of the request */ protected abstract CallType getCallType(); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java b/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java index fbc66012..57668b02 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java @@ -20,8 +20,7 @@ public class UnpreparedDataController extends DataControllerBase { public String datasetIds; public String datafileIds; - public UnpreparedDataController(String sessionId, String investigationIds, - String datasetIds, String datafileIds) { + public UnpreparedDataController(String sessionId, String investigationIds, String datasetIds, String datafileIds) { this.sessionId = sessionId; this.investigationIds = investigationIds; this.datasetIds = datasetIds; @@ -36,48 +35,41 @@ public void validateUUID() throws BadRequestException { @Override public String addParametersToLogString() { return "investigationIds='" + investigationIds + "' " + "datasetIds='" - + datasetIds + "' " + "datafileIds='" + datafileIds + "'"; + + datasetIds + "' " + "datafileIds='" + datafileIds + "'"; } @Override - public DataSelectionService provideDataSelectionService( - RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, - InsufficientPrivilegesException, NotImplementedException { - return DataSelectionServiceFactory.getService(sessionId, - investigationIds, datasetIds, datafileIds, requestType); + public DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + return DataSelectionServiceFactory.getService(sessionId, investigationIds, datasetIds, datafileIds, requestType); } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { - gen.write("userName", - ServiceProvider.getInstance().getIcat().getUserName(sessionId)); + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + gen.write("userName", ServiceProvider.getInstance().getIcat().getUserName(sessionId)); addIds(gen, investigationIds, datasetIds, datafileIds); } - protected void addIds(JsonGenerator gen, String investigationIds, - String datasetIds, String datafileIds) throws BadRequestException { + + + protected void addIds(JsonGenerator gen, String investigationIds, String datasetIds, String datafileIds) + throws BadRequestException { if (investigationIds != null) { gen.writeStartArray("investigationIds"); - for (long invid : DataSelectionService - .getValidIds("investigationIds", investigationIds)) { + for (long invid : DataSelectionService.getValidIds("investigationIds", investigationIds)) { gen.write(invid); } gen.writeEnd(); } if (datasetIds != null) { gen.writeStartArray("datasetIds"); - for (long invid : DataSelectionService.getValidIds("datasetIds", - datasetIds)) { + for (long invid : DataSelectionService.getValidIds("datasetIds", datasetIds)) { gen.write(invid); } gen.writeEnd(); } if (datafileIds != null) { gen.writeStartArray("datafileIds"); - for (long invid : DataSelectionService.getValidIds("datafileIds", - datafileIds)) { + for (long invid : DataSelectionService.getValidIds("datafileIds", datafileIds)) { gen.write(invid); } gen.writeEnd(); @@ -85,8 +77,7 @@ protected void addIds(JsonGenerator gen, String investigationIds, } @Override - public boolean mustZip(boolean zip, - DataSelectionService dataSelectionService) { + public boolean mustZip(boolean zip, DataSelectionService dataSelectionService) { return zip ? true : dataSelectionService.mustZip(); } @@ -100,7 +91,7 @@ public String forceGetSessionId() throws InternalException { if (this.sessionId == null) { this.sessionId = this.createSessionId(); } - + return this.sessionId; } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java index 29e1130d..0b4aa315 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java @@ -19,32 +19,25 @@ public class GetSizeHandler extends DataRequestHandler { - public GetSizeHandler(String ip, String preparedId, String sessionId, - String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETSIZE, ip, preparedId, sessionId, investigationIds, - datasetIds, datafileIds); + public GetSizeHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETSIZE, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - + logger.debug("Slow computation for normal case"); long size = 0; - size = this.updateSizeFromDataInfoIds(size, - dataSelectionService.getDfInfo(), - this.dataController.forceGetSessionId()); + size = this.updateSizeFromDataInfoIds(size, dataSelectionService.getDfInfo(), this.dataController.forceGetSessionId()); return new ValueContainer(size); } - protected long updateSizeFromDataInfoIds(long size, - Map dataInfos, String sessionId) - throws InternalException { + + protected long updateSizeFromDataInfoIds(long size, Map dataInfos, String sessionId) throws InternalException { StringBuilder sb = new StringBuilder(); int n = 0; for (DataInfoBase dataInfo : dataInfos.values()) { @@ -65,13 +58,11 @@ protected long updateSizeFromDataInfoIds(long size, return size; } - private long getSizeFor(String sessionId, StringBuilder sb) - throws InternalException { - String query = "SELECT SUM(df.fileSize) from Datafile df WHERE df.id IN (" - + sb.toString() + ") AND df.location IS NOT NULL"; + + private long getSizeFor(String sessionId, StringBuilder sb) throws InternalException { + String query = "SELECT SUM(df.fileSize) from Datafile df WHERE df.id IN (" + sb.toString() + ") AND df.location IS NOT NULL"; try { - return (Long) ServiceProvider.getInstance().getIcat() - .search(sessionId, query).get(0); + return (Long) ServiceProvider.getInstance().getIcat().search(sessionId, query).get(0); } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (IndexOutOfBoundsException e) { @@ -83,5 +74,5 @@ private long getSizeFor(String sessionId, StringBuilder sb) public CallType getCallType() { return CallType.INFO; } - + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java index a3abf50d..6b57cd9b 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java @@ -24,58 +24,44 @@ public class GetSizeHandlerForFastProcessing extends RequestHandlerBase { UnpreparedDataController dataController; - public GetSizeHandlerForFastProcessing(String ip, String sessionId, - String investigationIds, String datasetIds, String datafileIds) { + public GetSizeHandlerForFastProcessing(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { super(RequestType.GETSIZE, ip); - this.dataController = new UnpreparedDataController(sessionId, - investigationIds, datasetIds, datafileIds); + this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { - + public ValueContainer handleRequest() throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + this.dataController.validateUUID(); var serviceProvider = ServiceProvider.getInstance(); - List dfids = DataSelectionService.getValidIds("datafileIds", - dataController.datafileIds); - List dsids = DataSelectionService.getValidIds("datasetIds", - dataController.datasetIds); - List invids = DataSelectionService.getValidIds("investigationIds", - dataController.investigationIds); + List dfids = DataSelectionService.getValidIds("datafileIds", dataController.datafileIds); + List dsids = DataSelectionService.getValidIds("datasetIds", dataController.datasetIds); + List invids = DataSelectionService.getValidIds("investigationIds", dataController.investigationIds); + if (dfids.size() + dsids.size() + invids.size() == 1) { long size = 0; - size = getSizeFor(dataController.getOperationId(), invids, - "df.dataset.investigation.id") - + getSizeFor(dataController.getOperationId(), dsids, - "df.dataset.id") - + getSizeFor(dataController.getOperationId(), dfids, - "df.id"); + size = getSizeFor(dataController.getOperationId(), invids, "df.dataset.investigation.id") + + getSizeFor(dataController.getOperationId(), dsids, "df.dataset.id") + + getSizeFor(dataController.getOperationId(), dfids, "df.id"); logger.debug("Fast computation for simple case"); if (size == 0) { try { if (dfids.size() != 0) { - Datafile datafile = (Datafile) serviceProvider.getIcat() - .get(dataController.getOperationId(), - "Datafile", dfids.get(0)); + Datafile datafile = (Datafile) serviceProvider.getIcat().get(dataController.getOperationId(), "Datafile", dfids.get(0)); if (datafile.getLocation() == null) { throw new NotFoundException("Datafile not found"); } } if (dsids.size() != 0) { - serviceProvider.getIcat().get( - dataController.getOperationId(), "Dataset", - dsids.get(0)); + serviceProvider.getIcat().get(dataController.getOperationId(), "Dataset", dsids.get(0)); } if (invids.size() != 0) { - serviceProvider.getIcat().get( - dataController.getOperationId(), - "Investigation", invids.get(0)); + serviceProvider.getIcat().get(dataController.getOperationId(), "Investigation", invids.get(0)); } } catch (IcatException_Exception e) { throw new NotFoundException(e.getMessage()); @@ -84,10 +70,10 @@ public ValueContainer handleRequest() throws BadRequestException, return new ValueContainer(size); } + + return ValueContainer.getInvalid(); //is case of fast computation is not the right way. - return ValueContainer.getInvalid(); // is case of fast computation is - // not the right way. - + } @Override @@ -95,17 +81,13 @@ public CallType getCallType() { return CallType.INFO; } - public String addParametersToLogString() { - return this.dataController.addParametersToLogString(); - } + public String addParametersToLogString() { return this.dataController.addParametersToLogString(); } - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { this.dataController.addParametersToTransmitterJSON(gen); } - private long getSizeFor(String sessionId, List ids, String where) - throws InternalException { + private long getSizeFor(String sessionId, List ids, String where) throws InternalException { long size = 0; if (ids != null) { @@ -130,19 +112,16 @@ private long getSizeFor(String sessionId, List ids, String where) return size; } - private long evalSizeFor(String sessionId, String where, StringBuilder sb) - throws InternalException { - String query = "SELECT SUM(df.fileSize) from Datafile df WHERE " + where - + " IN (" + sb.toString() + ") AND df.location IS NOT NULL"; + private long evalSizeFor(String sessionId, String where, StringBuilder sb) throws InternalException { + String query = "SELECT SUM(df.fileSize) from Datafile df WHERE " + where + " IN (" + sb.toString() + ") AND df.location IS NOT NULL"; logger.debug("icat query for size: {}", query); try { - return (Long) ServiceProvider.getInstance().getIcat() - .search(sessionId, query).get(0); + return (Long) ServiceProvider.getInstance().getIcat().search(sessionId, query).get(0); } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (IndexOutOfBoundsException e) { return 0L; } } - + } diff --git a/src/main/java/org/icatproject/ids/services/ICATGetter.java b/src/main/java/org/icatproject/ids/services/ICATGetter.java index d62bd126..4073c3ce 100644 --- a/src/main/java/org/icatproject/ids/services/ICATGetter.java +++ b/src/main/java/org/icatproject/ids/services/ICATGetter.java @@ -17,21 +17,19 @@ */ public class ICATGetter { - private static String[] suffices = new String[] { "ICATService/ICAT?wsdl", - "icat/ICAT?wsdl" }; + private static String[] suffices = new String[]{"ICATService/ICAT?wsdl", "icat/ICAT?wsdl"}; /** * Provide access to an ICAT SOAP web service with the basic URL string * provided. This exists to hide the differences between containers. * * @param urlString the url of the machine to be contacted. If the url has a - * non-empty file part it is used unchanged, otherwise - * suffices are tried suitable for Glassfish and WildFly. + * non-empty file part it is used unchanged, otherwise suffices + * are tried suitable for Glassfish and WildFly. * @return an ICAT * @throws IcatException_Exception if something is wrong */ - public static ICAT getService(String urlString) - throws IcatException_Exception { + public static ICAT getService(String urlString) throws IcatException_Exception { if (urlString == null) { throwSessionException("Argument to constructor must not be null"); @@ -49,16 +47,14 @@ public static ICAT getService(String urlString) for (String suffix : suffices) { String icatUrlWsdl = urlString + "/" + suffix; try { - icatService = new ICATService(new URL(icatUrlWsdl)) - .getICATPort(); + icatService = new ICATService(new URL(icatUrlWsdl)).getICATPort(); icatService.getApiVersion(); return icatService; } catch (MalformedURLException e) { throwSessionException("Invalid URL"); } catch (WebServiceException e) { Throwable cause = e.getCause(); - if (cause != null - && cause.getMessage().contains("security")) { + if (cause != null && cause.getMessage().contains("security")) { throwSessionException(cause.getMessage()); } } catch (Exception e) { @@ -90,8 +86,7 @@ public static String getCleanUrl(String urlString) { for (String suffix : suffices) { suffix = "/" + suffix; if (urlString.endsWith(suffix)) { - return urlString.substring(0, - urlString.length() - suffix.length()); + return urlString.substring(0, urlString.length() - suffix.length()); } } if (urlString.endsWith("/")) { @@ -100,8 +95,7 @@ public static String getCleanUrl(String urlString) { return urlString; } - private static void throwSessionException(String msg) - throws IcatException_Exception { + private static void throwSessionException(String msg) throws IcatException_Exception { IcatException except = new IcatException(); except.setMessage(msg); except.setType(IcatExceptionType.SESSION); diff --git a/src/main/java/org/icatproject/ids/services/IcatReader.java b/src/main/java/org/icatproject/ids/services/IcatReader.java index ab184adf..9f5adbb8 100644 --- a/src/main/java/org/icatproject/ids/services/IcatReader.java +++ b/src/main/java/org/icatproject/ids/services/IcatReader.java @@ -22,8 +22,7 @@ public class IcatReader { private String sessionId; - private final static Logger logger = LoggerFactory - .getLogger(IcatReader.class); + private final static Logger logger = LoggerFactory.getLogger(IcatReader.class); private PropertyHandler propertyHandler; @@ -32,8 +31,7 @@ public IcatReader() { } - // primarily required for testing to make it possible to use a mocked - // PropertyHandler + // primarily required for testing to make it possible to use a mocked PropertyHandler public IcatReader(PropertyHandler propertyHandler) { this.propertyHandler = propertyHandler; init(); @@ -45,8 +43,7 @@ private void init() { login(); logger.info("Reader started"); } catch (Exception e) { - throw new RuntimeException( - "Reader reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException("Reader reports " + e.getClass() + " " + e.getMessage()); } } @@ -70,8 +67,7 @@ private void login() throws IcatException_Exception { } } - public EntityBaseBean get(String query, long id) - throws IcatException_Exception { + public EntityBaseBean get(String query, long id) throws IcatException_Exception { try { return icat.get(sessionId, query, id); } catch (IcatException_Exception e) { diff --git a/src/main/java/org/icatproject/ids/services/LockManager.java b/src/main/java/org/icatproject/ids/services/LockManager.java index 0e42bf25..49beba05 100644 --- a/src/main/java/org/icatproject/ids/services/LockManager.java +++ b/src/main/java/org/icatproject/ids/services/LockManager.java @@ -92,9 +92,7 @@ public void release() { try { storageLock.close(); } catch (Exception e) { - logger.error( - "Error while closing lock on {} in the storage plugin: {}.", - id, e.getMessage()); + logger.error("Error while closing lock on {} in the storage plugin: {}.", id, e.getMessage()); } } logger.debug("Released a lock on {}.", id); @@ -133,8 +131,7 @@ private void init() { logger.debug("LockManager initialized."); } - public Lock lock(DatasetInfo ds, LockType type) - throws AlreadyLockedException, IOException { + public Lock lock(DatasetInfo ds, LockType type) throws AlreadyLockedException, IOException { Long id = ds.getDsId(); assert id != null; synchronized (lockMap) { @@ -142,8 +139,7 @@ public Lock lock(DatasetInfo ds, LockType type) if (le == null) { le = new LockEntry(id, type); } else { - if (type == LockType.EXCLUSIVE - || le.type == LockType.EXCLUSIVE) { + if (type == LockType.EXCLUSIVE || le.type == LockType.EXCLUSIVE) { throw new AlreadyLockedException(); } } @@ -160,16 +156,12 @@ public Lock lock(DatasetInfo ds, LockType type) } } - public Lock lock(Collection datasets, LockType type) - throws AlreadyLockedException, IOException, InternalException { + public Lock lock(Collection datasets, LockType type) throws AlreadyLockedException, IOException, InternalException { LockCollection locks = new LockCollection(); try { for (DataInfoBase dataInfo : datasets) { DatasetInfo ds = (DatasetInfo) dataInfo; - if (ds == null) - throw new InternalException("Could not cast " - + dataInfo.getClass() - + " to DataSetInfo. Did you handed over another sub type of DataInfoBase? "); + if(ds == null) throw new InternalException("Could not cast " + dataInfo.getClass() + " to DataSetInfo. Did you handed over another sub type of DataInfoBase? "); locks.add(lock(ds, type)); } } catch (AlreadyLockedException | IOException e) { diff --git a/src/main/java/org/icatproject/ids/services/PropertyHandler.java b/src/main/java/org/icatproject/ids/services/PropertyHandler.java index ff66afad..e3cbe354 100644 --- a/src/main/java/org/icatproject/ids/services/PropertyHandler.java +++ b/src/main/java/org/icatproject/ids/services/PropertyHandler.java @@ -36,8 +36,7 @@ public class PropertyHandler { private static PropertyHandler instance = null; - private static final Logger logger = LoggerFactory - .getLogger(PropertyHandler.class); + private static final Logger logger = LoggerFactory.getLogger(PropertyHandler.class); public synchronized static PropertyHandler getInstance() { if (instance == null) { @@ -97,16 +96,12 @@ private PropertyHandler() { } preparedCount = props.getPositiveInt("preparedCount"); - processQueueIntervalSeconds = props - .getPositiveLong("processQueueIntervalSeconds"); - rootUserNames = new HashSet<>(Arrays.asList( - props.getString("rootUserNames").trim().split("\\s+"))); + processQueueIntervalSeconds = props.getPositiveLong("processQueueIntervalSeconds"); + rootUserNames = new HashSet<>(Arrays.asList(props.getString("rootUserNames").trim().split("\\s+"))); - reader = Arrays - .asList(props.getString("reader").trim().split("\\s+")); + reader = Arrays.asList(props.getString("reader").trim().split("\\s+")); if (reader.size() % 2 != 1) { - throw new IllegalStateException( - "reader must have an odd number of words"); + throw new IllegalStateException("reader must have an odd number of words"); } readOnly = props.getBoolean("readOnly", false); @@ -116,8 +111,7 @@ private PropertyHandler() { enableWrite = !readOnly; } - sizeCheckIntervalMillis = props - .getPositiveInt("sizeCheckIntervalSeconds") * 1000L; + sizeCheckIntervalMillis = props.getPositiveInt("sizeCheckIntervalSeconds") * 1000L; if (props.has("key")) { key = props.getString("key"); @@ -129,17 +123,14 @@ private PropertyHandler() { zipMapper = klass.getConstructor().newInstance(); logger.debug("ZipMapper initialised"); - } catch (ClassNotFoundException | InstantiationException - | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException - | SecurityException e) { + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException | SecurityException e) { abort(e.getClass() + " " + e.getMessage()); } // Now get simple properties to pass to the plugins Properties simpleProps = new Properties(); - try (InputStream is = getClass().getClassLoader() - .getResourceAsStream("run.properties")) { + try (InputStream is = getClass().getClassLoader().getResourceAsStream("run.properties")) { simpleProps.load(is); } catch (IOException e) { abort(e.getClass() + " " + e.getMessage()); @@ -148,67 +139,54 @@ private PropertyHandler() { try { Class klass = (Class) Class .forName(props.getString("plugin.main.class")); - mainStorage = klass.getConstructor(Properties.class) - .newInstance(simpleProps); + mainStorage = klass.getConstructor(Properties.class).newInstance(simpleProps); logger.debug("mainStorage initialised"); - } catch (ClassNotFoundException | InstantiationException - | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException - | SecurityException e) { + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException | SecurityException e) { logger.error("Plugin failed...", e); abort(e.getClass() + " " + e.getMessage()); } if (!props.has("plugin.archive.class")) { - logger.info( - "Property plugin.archive.class not set, single storage enabled."); + logger.info("Property plugin.archive.class not set, single storage enabled."); } else { try { Class klass = (Class) Class .forName(props.getString("plugin.archive.class")); - archiveStorage = klass.getConstructor(Properties.class) - .newInstance(simpleProps); + archiveStorage = klass.getConstructor(Properties.class).newInstance(simpleProps); logger.debug("archiveStorage initialised"); - } catch (ClassNotFoundException | InstantiationException - | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException - | SecurityException e) { + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException + | IllegalArgumentException | InvocationTargetException | NoSuchMethodException + | SecurityException e) { logger.error("Plugin failed...", e); abort(e.getClass() + " " + e.getMessage()); } - startArchivingLevel = props - .getPositiveLong("startArchivingLevel1024bytes") * 1024; - stopArchivingLevel = props - .getPositiveLong("stopArchivingLevel1024bytes") * 1024; + startArchivingLevel = props.getPositiveLong("startArchivingLevel1024bytes") * 1024; + stopArchivingLevel = props.getPositiveLong("stopArchivingLevel1024bytes") * 1024; if (stopArchivingLevel >= startArchivingLevel) { abort("startArchivingLevel1024bytes must be greater than stopArchivingLevel1024bytes"); } try { String storageUnitName = props.getString("storageUnit"); - storageUnit = StorageUnit - .valueOf(storageUnitName.toUpperCase()); + storageUnit = StorageUnit.valueOf(storageUnitName.toUpperCase()); } catch (IllegalArgumentException e) { List vs = new ArrayList<>(); for (StorageUnit s : StorageUnit.values()) { vs.add(s.name()); } - abort("storageUnit value " + props.getString("storageUnit") - + " must be taken from " + vs); + abort("storageUnit value " + props.getString("storageUnit") + " must be taken from " + vs); } if (storageUnit == StorageUnit.DATASET) { - delayDatasetWrites = props - .getPositiveLong("delayDatasetWritesSeconds"); + delayDatasetWrites = props.getPositiveLong("delayDatasetWritesSeconds"); } else if (storageUnit == StorageUnit.DATAFILE) { - delayDatafileOperations = props - .getPositiveLong("delayDatafileOperationsSeconds"); + delayDatafileOperations = props.getPositiveLong("delayDatafileOperationsSeconds"); } tidyBlockSize = props.getPositiveInt("tidyBlockSize"); } try { - cacheDir = props.getFile("cache.dir").getCanonicalFile() - .toPath(); + cacheDir = props.getFile("cache.dir").getCanonicalFile().toPath(); } catch (IOException e) { abort("IOException " + e.getMessage()); } @@ -219,31 +197,25 @@ private PropertyHandler() { maxIdsInQuery = props.getPositiveInt("maxIdsInQuery"); /* JMS stuff */ - jmsTopicConnectionFactory = props.getString( - "jms.topicConnectionFactory", + jmsTopicConnectionFactory = props.getString("jms.topicConnectionFactory", "java:comp/DefaultJMSConnectionFactory"); /* Call logging categories */ if (props.has("log.list")) { - for (String callTypeString : props.getString("log.list") - .split("\\s+")) { + for (String callTypeString : props.getString("log.list").split("\\s+")) { try { - logSet.add( - CallType.valueOf(callTypeString.toUpperCase())); + logSet.add(CallType.valueOf(callTypeString.toUpperCase())); } catch (IllegalArgumentException e) { - abort("Value " + callTypeString - + " in log.list must be chosen from " + abort("Value " + callTypeString + " in log.list must be chosen from " + Arrays.asList(CallType.values())); } } logger.info("log.list: " + logSet); } else { - logger.info( - "'log.list' entry not present so no JMS call logging will be performed"); + logger.info("'log.list' entry not present so no JMS call logging will be performed"); } - useReaderForPerformance = props - .getBoolean("useReaderForPerformance", false); + useReaderForPerformance = props.getBoolean("useReaderForPerformance", false); } catch (CheckedPropertyException e) { abort(e.getMessage()); @@ -275,8 +247,7 @@ public synchronized ICAT getIcatService() { try { icatService = ICATGetter.getService(icatUrl); } catch (IcatException_Exception e) { - String msg = "Problem finding ICAT API version at " + icatUrl - + ": " + e.getFaultInfo().getType() + " " + String msg = "Problem finding ICAT API version at " + icatUrl + ": " + e.getFaultInfo().getType() + " " + e.getMessage(); logger.error(msg); try { @@ -314,19 +285,15 @@ public synchronized int getMaxEntities() { // will produce an error message. while (maxEntities == null) { try { - org.icatproject.icat.client.ICAT ricat = new org.icatproject.icat.client.ICAT( - icatUrl); + org.icatproject.icat.client.ICAT ricat = new org.icatproject.icat.client.ICAT(icatUrl); try (JsonReader parser = Json - .createReader(new ByteArrayInputStream( - ricat.getProperties().getBytes()))) { + .createReader(new ByteArrayInputStream(ricat.getProperties().getBytes()))) { maxEntities = parser.readObject().getInt("maxEntities"); - logger.info( - "maxEntities from the ICAT.server {} version {} is {}", - icatUrl, ricat.getVersion(), maxEntities); + logger.info("maxEntities from the ICAT.server {} version {} is {}", icatUrl, ricat.getVersion(), + maxEntities); } catch (Exception e) { - String msg = "Problem finding 1 ICAT API version " - + e.getClass() + " " + e.getMessage(); + String msg = "Problem finding 1 ICAT API version " + e.getClass() + " " + e.getMessage(); logger.error(msg); try { Thread.sleep(10000); @@ -335,8 +302,7 @@ public synchronized int getMaxEntities() { } } } catch (URISyntaxException e) { - String msg = "Problem finding 2 ICAT API version " - + e.getClass() + " " + e.getMessage(); + String msg = "Problem finding 2 ICAT API version " + e.getClass() + " " + e.getMessage(); logger.error(msg); throw new IllegalStateException(msg); } diff --git a/src/main/java/org/icatproject/ids/services/ServiceProvider.java b/src/main/java/org/icatproject/ids/services/ServiceProvider.java index e06194ea..dc179ad5 100644 --- a/src/main/java/org/icatproject/ids/services/ServiceProvider.java +++ b/src/main/java/org/icatproject/ids/services/ServiceProvider.java @@ -8,9 +8,8 @@ import org.icatproject.ids.plugin.MainStorageInterface; /** - * This class provides multiple services to the developer Maybe it is just for - * the redesign for version 3 and will later be replaced with dependency - * injection, when it will be more clear where which service is used. + * This class provides multiple services to the developer + * Maybe it is just for the redesign for version 3 and will later be replaced with dependency injection, when it will be more clear where which service is used. */ public class ServiceProvider { @@ -22,9 +21,7 @@ public class ServiceProvider { private IcatReader icatReader; private PropertyHandler propertyHandler; - private ServiceProvider(PropertyHandler propertyHandler, - Transmitter transmitter, FiniteStateMachine fsm, - LockManager lockManager, IcatReader reader) { + private ServiceProvider(PropertyHandler propertyHandler, Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { this.transmitter = transmitter; this.fsm = fsm; this.lockManager = lockManager; @@ -33,37 +30,27 @@ private ServiceProvider(PropertyHandler propertyHandler, } /** - * At first, the ServiceProvider has to be created. Do not call - * getInstance() before you have called createInstande() - * + * At first, the ServiceProvider has to be created. Do not call getInstance() before you have called createInstande() * @param transmitter * @param fsm * @param lockManager * @param reader */ - public static void createInstance(Transmitter transmitter, - FiniteStateMachine fsm, LockManager lockManager, - IcatReader reader) { + public static void createInstance(Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { - createInstance(PropertyHandler.getInstance(), transmitter, fsm, - lockManager, reader); + createInstance(PropertyHandler.getInstance(), transmitter, fsm, lockManager, reader); } - public static void createInstance(PropertyHandler propertyHandler, - Transmitter transmitter, FiniteStateMachine fsm, - LockManager lockManager, IcatReader reader) { + public static void createInstance(PropertyHandler propertyHandler, Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { - if (instance != null) - return; + if(instance != null) return; - instance = new ServiceProvider(propertyHandler, transmitter, fsm, - lockManager, reader); + instance = new ServiceProvider(propertyHandler, transmitter, fsm, lockManager, reader); } public static ServiceProvider getInstance() { - if (instance == null) { - throw new RuntimeException( - "ServiceProvider is not yet instantiated, please call createInstance at first."); + if(instance == null) { + throw new RuntimeException("ServiceProvider is not yet instantiated, please call createInstance at first."); } return instance; } @@ -100,4 +87,5 @@ public Set getLogSet() { return PropertyHandler.getInstance().getLogSet(); } + } diff --git a/src/main/java/org/icatproject/ids/services/Transmitter.java b/src/main/java/org/icatproject/ids/services/Transmitter.java index 267e4940..996522cb 100644 --- a/src/main/java/org/icatproject/ids/services/Transmitter.java +++ b/src/main/java/org/icatproject/ids/services/Transmitter.java @@ -59,15 +59,12 @@ private void exit() { } } - public void processMessage(String operation, String ip, String body, - long startMillis) { - try (Session jmsSession = topicConnection.createSession(false, - Session.AUTO_ACKNOWLEDGE)) { + public void processMessage(String operation, String ip, String body, long startMillis) { + try (Session jmsSession = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE)) { TextMessage jmsg = jmsSession.createTextMessage(body); jmsg.setStringProperty("operation", operation); jmsg.setStringProperty("ip", ip); - jmsg.setLongProperty("millis", - System.currentTimeMillis() - startMillis); + jmsg.setLongProperty("millis", System.currentTimeMillis() - startMillis); jmsg.setLongProperty("start", startMillis); MessageProducer jmsProducer = jmsSession.createProducer(topic); jmsProducer.send(jmsg); diff --git a/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java b/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java index 42a5d949..5f9b372b 100644 --- a/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java +++ b/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java @@ -21,45 +21,37 @@ import org.slf4j.LoggerFactory; /** - * This class is to handle not finished work. It could be redesigned with sub - * classes depending on StorageUnit, but it isn't worth it i guess, just for one - * abstract method (loadDataInfo()). + * This class is to handle not finished work. + * It could be redesigned with sub classes depending on StorageUnit, but it isn't worth it i guess, just for one abstract method (loadDataInfo()). */ public class UnfinishedWorkService { - protected final static Logger logger = LoggerFactory - .getLogger(UnfinishedWorkService.class); - public UnfinishedWorkService() { + protected final static Logger logger = LoggerFactory.getLogger(UnfinishedWorkService.class); + public UnfinishedWorkService() { + } - public void restartUnfinishedWork(Path markerDir, String key) - throws InternalException { + public void restartUnfinishedWork(Path markerDir, String key) throws InternalException { try { var serviceProvider = ServiceProvider.getInstance(); - StorageUnit storageUnit = serviceProvider.getPropertyHandler() - .getStorageUnit(); + StorageUnit storageUnit = serviceProvider.getPropertyHandler().getStorageUnit(); for (File file : markerDir.toFile().listFiles()) { - if (storageUnit == null) - break; - - long id = Long - .parseLong(file.toPath().getFileName().toString()); + if(storageUnit == null) break; + + long id = Long.parseLong(file.toPath().getFileName().toString()); try { DataInfoBase dataInfo = this.loadDataInfo(storageUnit, id); serviceProvider.getFsm().queue(dataInfo, DeferredOp.WRITE); - logger.info("Queued " + storageUnit.toString().toLowerCase() - + " with id " + id + " " + dataInfo + logger.info("Queued " + storageUnit.toString().toLowerCase() + " with id " + id + " " + dataInfo + " to be written as it was not written out previously by IDS"); } catch (IcatException_Exception e) { - if (e.getFaultInfo() - .getType() == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { - logger.warn(storageUnit.toString().toLowerCase() - + " with id " + id + if (e.getFaultInfo().getType() == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { + logger.warn( storageUnit.toString().toLowerCase() + " with id " + id + " was not written out by IDS and now no longer known to ICAT"); Files.delete(file.toPath()); } else { @@ -72,6 +64,7 @@ public void restartUnfinishedWork(Path markerDir, String key) } } + public void cleanPreparedDir(Path preparedDir) { for (File file : preparedDir.toFile().listFiles()) { Path path = file.toPath(); @@ -87,8 +80,7 @@ public void cleanPreparedDir(Path preparedDir) { } thisSize += Files.size(path); Files.delete(path); - logger.debug("Deleted " + path + " to reclaim " + thisSize - + " bytes"); + logger.debug("Deleted " + path + " to reclaim " + thisSize + " bytes"); } catch (IOException e) { logger.debug("Failed to delete " + path + e.getMessage()); } @@ -96,37 +88,31 @@ public void cleanPreparedDir(Path preparedDir) { } } + public void cleanDatasetCache(Path datasetDir) { for (File dsFile : datasetDir.toFile().listFiles()) { Path path = dsFile.toPath(); try { long thisSize = Files.size(path); Files.delete(path); - logger.debug("Deleted " + path + " to reclaim " + thisSize - + " bytes"); + logger.debug("Deleted " + path + " to reclaim " + thisSize + " bytes"); } catch (IOException e) { - logger.debug("Failed to delete " + path + " " + e.getClass() - + " " + e.getMessage()); + logger.debug("Failed to delete " + path + " " + e.getClass() + " " + e.getMessage()); } } } - private DataInfoBase loadDataInfo(StorageUnit storageUnit, long id) - throws IcatException_Exception, InsufficientPrivilegesException, - InternalException { - if (storageUnit == StorageUnit.DATASET) { - Dataset ds = (Dataset) ServiceProvider.getInstance().getIcatReader() - .get("Dataset ds INCLUDE ds.investigation.facility", id); + + private DataInfoBase loadDataInfo(StorageUnit storageUnit, long id) throws IcatException_Exception, InsufficientPrivilegesException, InternalException { + if(storageUnit == StorageUnit.DATASET) { + Dataset ds = (Dataset) ServiceProvider.getInstance().getIcatReader().get("Dataset ds INCLUDE ds.investigation.facility", id); return new DatasetInfo(ds); } - if (storageUnit == StorageUnit.DATAFILE) { - Datafile df = (Datafile) ServiceProvider.getInstance() - .getIcatReader().get("Datafile ds INCLUDE ds.dataset", id); - String location = LocationHelper.getLocation(df.getId(), - df.getLocation()); - return new DatafileInfo(id, df.getName(), location, - df.getCreateId(), df.getModId(), df.getDataset().getId()); + if(storageUnit == StorageUnit.DATAFILE) { + Datafile df = (Datafile) ServiceProvider.getInstance().getIcatReader().get("Datafile ds INCLUDE ds.dataset", id); + String location = LocationHelper.getLocation(df.getId(), df.getLocation()); + return new DatafileInfo(id, df.getName(), location, df.getCreateId(), df.getModId(), df.getDataset().getId()); } return null; diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java index d5042bec..650e410f 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java @@ -32,16 +32,14 @@ public abstract class DataSelectionService { - protected final static Logger logger = LoggerFactory - .getLogger(DataSelectionService.class); + protected final static Logger logger = LoggerFactory.getLogger(DataSelectionService.class); protected DataSelection dataSelection; protected RequestType requestType; + protected static ExecutorService threadPool; - static { - threadPool = Executors.newCachedThreadPool(); - } + static { threadPool = Executors.newCachedThreadPool(); } private Map preparedStatusMap = new ConcurrentHashMap<>(); @@ -52,54 +50,41 @@ class PreparedStatus { } - protected DataSelectionService(SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, - Boolean zip, Boolean compress, RequestType requestType) { + protected DataSelectionService(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { - this.dataSelection = new DataSelection(dsInfos, dfInfos, emptyDatasets, - invids2, dsids, dfids, length, zip, compress); + this.dataSelection = new DataSelection(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress); this.requestType = requestType; } - public abstract boolean isPrepared(String preparedId) - throws InternalException; + public abstract boolean isPrepared(String preparedId) throws InternalException; /** - * To get the DataInfos that is currently worked with, depending on - * StorageUnit - * + * To get the DataInfos that is currently worked with, depending on StorageUnit * @return */ public abstract SortedMap getPrimaryDataInfos(); - public abstract boolean existsInMainStorage(DataInfoBase dataInfo) - throws InternalException; + public abstract boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException; - public abstract void queueDelete() - throws NotImplementedException, InternalException; + public abstract void queueDelete() throws NotImplementedException, InternalException; - public abstract void scheduleTasks(DeferredOp operation) - throws NotImplementedException, InternalException; + public abstract void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException; - // TODO: maybe implementing this method here whould be the better way, than - // making it abstract. But we will miss the NotImplementedException for - // SingleLevelStorage in that case. + //TODO: maybe implementing this method here whould be the better way, than making it abstract. But we will miss the NotImplementedException for SingleLevelStorage in that case. // should we change the tests and the behavior of the ids server here - // public void scheduleTasks(DeferredOp operation) throws - // NotImplementedException, InternalException { - // for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { // in - // case of SingleLevelStorage an empty map is returned and nothing will - // happen (even not an exception). - // ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); - // } + // public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { + // for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { // in case of SingleLevelStorage an empty map is returned and nothing will happen (even not an exception). + // ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); + // } // } public SortedMap getDsInfo() { return this.dataSelection.getDsInfo(); } + public SortedMap getDfInfo() { return this.dataSelection.getDfInfo(); } @@ -116,6 +101,7 @@ public long getLength() { return this.dataSelection.getLength(); } + public boolean mustZip() { return this.dataSelection.mustZip(); } @@ -124,22 +110,21 @@ public boolean isSingleDataset() { return this.dataSelection.isSingleDataset(); } + public Set getEmptyDatasets() { return this.dataSelection.getEmptyDatasets(); } + /** - * tries to extract a list of ids from a comma separated id string. No - * spaces or leading 0's. Also accepts null. - * - * @param thing the name of the id list - for better error message - * @param idList a String which shoald contain long numbers seperated by - * commas + * tries to extract a list of ids from a comma separated id string. No spaces or leading 0's. Also + * accepts null. + * @param thing the name of the id list - for better error message + * @param idList a String which shoald contain long numbers seperated by commas * @return list of long numbers - the extracted id values * @throws BadRequestException */ - public static List getValidIds(String thing, String idList) - throws BadRequestException { + public static List getValidIds(String thing, String idList) throws BadRequestException { List result; if (idList == null) { @@ -151,8 +136,7 @@ public static List getValidIds(String thing, String idList) try { result.add(Long.parseLong(id)); } catch (NumberFormatException e) { - throw new BadRequestException("The " + thing - + " parameter '" + idList + "' is not a valid " + throw new BadRequestException("The " + thing + " parameter '" + idList + "' is not a valid " + "string representation of a comma separated list of longs"); } } @@ -160,8 +144,8 @@ public static List getValidIds(String thing, String idList) return result; } - private boolean restoreIfOffline(DataInfoBase dataInfo) - throws InternalException { + + private boolean restoreIfOffline(DataInfoBase dataInfo) throws InternalException { boolean maybeOffline = false; var serviceProvider = ServiceProvider.getInstance(); if (serviceProvider.getFsm().getMaybeOffline().contains(dataInfo)) { @@ -173,7 +157,8 @@ private boolean restoreIfOffline(DataInfoBase dataInfo) return maybeOffline; } - public void checkOnline() throws InternalException, DataNotOnlineException { + + public void checkOnline()throws InternalException, DataNotOnlineException { boolean maybeOffline = false; for (DataInfoBase dfInfo : this.getPrimaryDataInfos().values()) { @@ -187,59 +172,51 @@ public void checkOnline() throws InternalException, DataNotOnlineException { } } + public void restoreDataInfos() { var dataInfos = this.getPrimaryDataInfos().values(); - if (!dataInfos.isEmpty()) { + if(!dataInfos.isEmpty()) { for (DataInfoBase dataInfo : dataInfos) { - ServiceProvider.getInstance().getFsm() - .recordSuccess(dataInfo.getId()); + ServiceProvider.getInstance().getFsm().recordSuccess(dataInfo.getId()); } threadPool.submit(new RestoreDataInfoTask(dataInfos, this, false)); } } + public OptionalLong getFileLength() { - if (this.dataSelection.getDfInfo().isEmpty() - || this.dataSelection.mustZip()) { + if (this.dataSelection.getDfInfo().isEmpty() || this.dataSelection.mustZip()) { return OptionalLong.empty(); } return OptionalLong.of(this.dataSelection.getLength()); } - protected boolean areDataInfosPrepared(String preparedId) - throws InternalException { + + protected boolean areDataInfosPrepared(String preparedId) throws InternalException { boolean prepared = true; var serviceProvider = ServiceProvider.getInstance(); - PreparedStatus status = preparedStatusMap.computeIfAbsent(preparedId, - k -> new PreparedStatus()); + PreparedStatus status = preparedStatusMap.computeIfAbsent(preparedId, k -> new PreparedStatus()); - Collection toCheck = status.fromElement == null - ? this.getPrimaryDataInfos().values() - : this.getPrimaryDataInfos().tailMap(status.fromElement) - .values(); + Collection toCheck = status.fromElement == null ? this.getPrimaryDataInfos().values() + : this.getPrimaryDataInfos().tailMap(status.fromElement).values(); logger.debug("Will check online status of {} entries", toCheck.size()); for (DataInfoBase dataInfo : toCheck) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (this.restoreIfOffline(dataInfo)) { prepared = false; status.fromElement = dataInfo.getId(); - toCheck = this.getPrimaryDataInfos().tailMap(status.fromElement) - .values(); - logger.debug("Will check in background status of {} entries", - toCheck.size()); - status.future = threadPool - .submit(new RestoreDataInfoTask(toCheck, this, true)); + toCheck = this.getPrimaryDataInfos().tailMap(status.fromElement).values(); + logger.debug("Will check in background status of {} entries", toCheck.size()); + status.future = threadPool.submit(new RestoreDataInfoTask(toCheck, this, true)); break; } } if (prepared) { toCheck = status.fromElement == null ? Collections.emptySet() - : this.getPrimaryDataInfos().headMap(status.fromElement) - .values(); - logger.debug("Will check finally online status of {} entries", - toCheck.size()); + : this.getPrimaryDataInfos().headMap(status.fromElement).values(); + logger.debug("Will check finally online status of {} entries", toCheck.size()); for (DataInfoBase dataInfo : toCheck) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (this.restoreIfOffline(dataInfo)) { @@ -251,52 +228,47 @@ protected boolean areDataInfosPrepared(String preparedId) return prepared; } + public void delete() throws InternalException, NotImplementedException { var serviceProvider = ServiceProvider.getInstance(); /* - * Delete the local copy directly rather than queueing it as it has been - * removed from ICAT so will not be accessible to any subsequent IDS - * calls. + * Delete the local copy directly rather than queueing it as it has + * been removed from ICAT so will not be accessible to any + * subsequent IDS calls. */ for (DataInfoBase dataInfo : this.dataSelection.getDfInfo().values()) { var dfInfo = (DatafileInfo) dataInfo; String location = dataInfo.getLocation(); try { - if ((long) serviceProvider.getIcatReader().search( - "SELECT COUNT(df) FROM Datafile df WHERE df.location LIKE '" - + location.replaceAll("'", "''") + "%'") + if ((long) serviceProvider.getIcatReader() + .search("SELECT COUNT(df) FROM Datafile df WHERE df.location LIKE '" + location.replaceAll("'", "''") + "%'") .get(0) == 0) { if (serviceProvider.getMainStorage().exists(location)) { - logger.debug("Delete physical file " + location - + " from main storage"); - serviceProvider.getMainStorage().delete(location, - dfInfo.getCreateId(), dfInfo.getModId()); + logger.debug("Delete physical file " + location + " from main storage"); + serviceProvider.getMainStorage().delete(location, dfInfo.getCreateId(), dfInfo.getModId()); } } } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } catch (IOException e) { - logger.error( - "I/O error " + e.getMessage() + " deleting " + dfInfo); - throw new InternalException( - e.getClass() + " " + e.getMessage()); + logger.error("I/O error " + e.getMessage() + " deleting " + dfInfo); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } this.queueDelete(); } + private class RestoreDataInfoTask implements Callable { private Collection dataInfos; private DataSelectionService dataselection; private boolean checkFailure; - public RestoreDataInfoTask(Collection dataInfos, - DataSelectionService dataSelection, boolean checkFailure) { + public RestoreDataInfoTask(Collection dataInfos, DataSelectionService dataSelection, boolean checkFailure) { this.dataInfos = dataInfos; this.dataselection = dataSelection; this.checkFailure = checkFailure; @@ -305,14 +277,13 @@ public RestoreDataInfoTask(Collection dataInfos, @Override public Void call() throws Exception { for (DataInfoBase dataInfo : dataInfos) { - if (checkFailure) - ServiceProvider.getInstance().getFsm() - .checkFailure(dataInfo.getId()); + if(checkFailure) + ServiceProvider.getInstance().getFsm().checkFailure(dataInfo.getId()); dataselection.restoreIfOffline(dataInfo); } return null; } - + } } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java index 44e622a2..25ff3ccb 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java @@ -42,8 +42,7 @@ public class DataSelectionServiceFactory { - private final static Logger logger = LoggerFactory - .getLogger(DataSelectionServiceFactory.class); + private final static Logger logger = LoggerFactory.getLogger(DataSelectionServiceFactory.class); private static DataSelectionServiceFactory instance = null; @@ -58,41 +57,30 @@ public enum Returns { DATASETS, DATASETS_AND_DATAFILES, DATAFILES } - public static DataSelectionServiceFactory getInstance() - throws InternalException { + public static DataSelectionServiceFactory getInstance() throws InternalException { if (instance == null) { var serviceProvider = ServiceProvider.getInstance(); - instance = new DataSelectionServiceFactory( - serviceProvider.getPropertyHandler(), - serviceProvider.getIcatReader()); + instance = new DataSelectionServiceFactory(serviceProvider.getPropertyHandler(), serviceProvider.getIcatReader()); } return instance; } - public static DataSelectionServiceFactory getInstanceOnlyForTesting( - PropertyHandler propertyHandler, IcatReader reader) - throws InternalException { + public static DataSelectionServiceFactory getInstanceOnlyForTesting(PropertyHandler propertyHandler, IcatReader reader) throws InternalException { if (instance == null) { instance = new DataSelectionServiceFactory(propertyHandler, reader); } return instance; } - public static DataSelectionService getService(String userSessionId, - String investigationIds, String datasetIds, String datafileIds, - RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, - InsufficientPrivilegesException, NotImplementedException { + public static DataSelectionService getService(String userSessionId, String investigationIds, String datasetIds, String datafileIds, RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - return DataSelectionServiceFactory.getInstance().getSelectionService( - userSessionId, investigationIds, datasetIds, datafileIds, - requestType); + return DataSelectionServiceFactory.getInstance().getSelectionService(userSessionId, investigationIds, datasetIds, datafileIds, requestType); } + /** - * created a DataSelectionBase object from already created data infos. - * That's why no infos must be loaded from ICAT. - * + * created a DataSelectionBase object from already created data infos. That's why no infos must be loaded from ICAT. * @param dsInfos * @param dfInfos * @param emptyDatasets @@ -101,41 +89,33 @@ public static DataSelectionService getService(String userSessionId, * @return * @throws InternalException */ - public static DataSelectionService getService( - SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - long fileLength, RequestType requestType) throws InternalException { + public static DataSelectionService getService(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength, RequestType requestType) throws InternalException { - var prepared = new Prepared(dsInfos, dfInfos, emptyDatasets, - fileLength); + var prepared = new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength); return DataSelectionServiceFactory.getService(prepared, requestType); } - public static DataSelectionService getService(Prepared prepared, - RequestType requestType) throws InternalException { + public static DataSelectionService getService(Prepared prepared, RequestType requestType) throws InternalException { List dsids = new ArrayList(prepared.dsInfos.keySet()); List dfids = new ArrayList(); List invIds = new ArrayList(); var dataFileInfos = new HashMap(); - for (DataInfoBase dfInfo : prepared.dfInfos.values()) { + for(DataInfoBase dfInfo: prepared.dfInfos.values()) { dfids.add(dfInfo.getId()); dataFileInfos.put(dfInfo.getId(), dfInfo); dfids.add(dfInfo.getId()); } - for (DataInfoBase dsInfo : prepared.dsInfos.values()) { + for(DataInfoBase dsInfo : prepared.dsInfos.values()) { dsids.add(dsInfo.getId()); - invIds.add(((DatasetInfo) dsInfo).getInvId()); + invIds.add( ((DatasetInfo)dsInfo).getInvId() ); } - return DataSelectionServiceFactory.getInstance().createSelectionService( - prepared.dsInfos, prepared.dfInfos, prepared.emptyDatasets, - invIds, dsids, dfids, prepared.fileLength, prepared.zip, - prepared.compress, requestType); + return DataSelectionServiceFactory.getInstance().createSelectionService(prepared.dsInfos, prepared.dfInfos, prepared.emptyDatasets, invIds, dsids, dfids, prepared.fileLength, prepared.zip, prepared.compress, requestType); } - private DataSelectionServiceFactory(PropertyHandler propertyHandler, - IcatReader reader) throws InternalException { + private DataSelectionServiceFactory(PropertyHandler propertyHandler, IcatReader reader) throws InternalException + { logger.info("### Constructing..."); this.propertyHandler = propertyHandler; this.icat = propertyHandler.getIcatService(); @@ -148,66 +128,47 @@ private DataSelectionServiceFactory(PropertyHandler propertyHandler, logger.info("### Constructing finished"); } - private DataSelectionService createSelectionService( - SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - List invids, List dsids, List dfids, long length, - Boolean zip, Boolean compress, RequestType requestType) - throws InternalException { + private DataSelectionService createSelectionService(SortedMap dsInfos, SortedMap dfInfos, + Set emptyDatasets, List invids, List dsids, + List dfids, long length, Boolean zip, Boolean compress, + RequestType requestType) throws InternalException { StorageUnit storageUnit = this.propertyHandler.getStorageUnit(); - if (storageUnit == null) - return new DataSelectionServiceForSingleLevelStorage(dsInfos, - dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, - compress, requestType); + if(storageUnit == null ) + return new DataSelectionServiceForSingleLevelStorage(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); else if (storageUnit == StorageUnit.DATAFILE) - return new DataSelectionServiceForStorageUnitDatafile(dsInfos, - dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, - compress, requestType); + return new DataSelectionServiceForStorageUnitDatafile(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); - else if (storageUnit == StorageUnit.DATASET) - return new DataSelectionServiceForStorageUnitDataset(dsInfos, - dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, - compress, requestType); + else if(storageUnit == StorageUnit.DATASET) + return new DataSelectionServiceForStorageUnitDataset(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); - else - throw new InternalException("StorageUnit " + storageUnit - + " unknown. Maybe you forgot to handle a new StorageUnit here?"); + else throw new InternalException("StorageUnit " + storageUnit + " unknown. Maybe you forgot to handle a new StorageUnit here?"); } - public DataSelectionService getSelectionService(String userSessionId, - String investigationIds, String datasetIds, String datafileIds, - RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, - InsufficientPrivilegesException, NotImplementedException { - + public DataSelectionService getSelectionService( String userSessionId, String investigationIds, String datasetIds, String datafileIds, RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + List dfids = getValidIds("datafileIds", datafileIds); List dsids = getValidIds("datasetIds", datasetIds); List invids = getValidIds("investigationIds", investigationIds); Returns returns = this.getReturns(requestType); - boolean dfWanted = returns == Returns.DATASETS_AND_DATAFILES - || returns == Returns.DATAFILES; - boolean dsWanted = returns == Returns.DATASETS_AND_DATAFILES - || returns == Returns.DATASETS; + boolean dfWanted = returns == Returns.DATASETS_AND_DATAFILES || returns == Returns.DATAFILES; + boolean dsWanted = returns == Returns.DATASETS_AND_DATAFILES || returns == Returns.DATASETS; Session userRestSession = restIcat.getSession(userSessionId); // by default use the user's REST ICAT session Session restSessionToUse = userRestSession; - + try { - logger.debug("useReaderForPerformance = {}", - propertyHandler.getUseReaderForPerformance()); + logger.debug("useReaderForPerformance = {}", propertyHandler.getUseReaderForPerformance()); if (propertyHandler.getUseReaderForPerformance()) { - // if this is set, use a REST session for the reader account - // where possible - // to improve performance due to the final database queries - // being simpler - restSessionToUse = restIcat - .getSession(this.icatReader.getSessionId()); + // if this is set, use a REST session for the reader account where possible + // to improve performance due to the final database queries being simpler + restSessionToUse = restIcat.getSession(this.icatReader.getSessionId()); } } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -215,23 +176,20 @@ public DataSelectionService getSelectionService(String userSessionId, logger.debug("dfids: {} dsids: {} invids: {}", dfids, dsids, invids); - return prepareFromIds(dfWanted, dsWanted, dfids, dsids, invids, - userSessionId, restSessionToUse, userRestSession, requestType); + return prepareFromIds(dfWanted, dsWanted, dfids, dsids, invids, userSessionId, restSessionToUse, userRestSession, requestType); } - private DataSelectionService prepareFromIds(boolean dfWanted, - boolean dsWanted, List dfids, List dsids, - List invids, String userSessionId, Session restSessionToUse, - Session userRestSession, RequestType requestType) - throws NotFoundException, InsufficientPrivilegesException, - InternalException, BadRequestException { + + + private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, List dfids, List dsids, List invids, String userSessionId, Session restSessionToUse, Session userRestSession, RequestType requestType) + throws NotFoundException, InsufficientPrivilegesException, InternalException, BadRequestException { var dsInfos = new TreeMap(); var emptyDatasets = new HashSet(); var dfInfos = new TreeMap(); long length = 0; - if (dfWanted) { // redundant ? + if (dfWanted) { //redundant ? dfInfos = new TreeMap(); } @@ -239,22 +197,18 @@ private DataSelectionService prepareFromIds(boolean dfWanted, for (Long dfid : dfids) { List dss = icat.search(userSessionId, - "SELECT ds FROM Dataset ds JOIN ds.datafiles df WHERE df.id = " - + dfid + "SELECT ds FROM Dataset ds JOIN ds.datafiles df WHERE df.id = " + dfid + " AND df.location IS NOT NULL INCLUDE ds.investigation.facility"); if (dss.size() == 1) { Dataset ds = (Dataset) dss.get(0); long dsid = ds.getId(); dsInfos.put(dsid, new DatasetInfo(ds)); if (dfWanted) { - Datafile df = (Datafile) icat.get(userSessionId, - "Datafile", dfid); + Datafile df = (Datafile) icat.get(userSessionId, "Datafile", dfid); length += df.getFileSize(); - String location = LocationHelper.getLocation(dfid, - df.getLocation()); - dfInfos.put(df.getId(), - new DatafileInfo(dfid, df.getName(), location, - df.getCreateId(), df.getModId(), dsid)); + String location = LocationHelper.getLocation(dfid, df.getLocation()); + dfInfos.put( df.getId(), + new DatafileInfo(dfid, df.getName(), location, df.getCreateId(), df.getModId(), dsid)); } } else { // Next line may reveal a permissions problem @@ -264,18 +218,13 @@ private DataSelectionService prepareFromIds(boolean dfWanted, } for (Long dsid : dsids) { - Dataset ds = (Dataset) icat.get(userSessionId, - "Dataset ds INCLUDE ds.investigation.facility", dsid); + Dataset ds = (Dataset) icat.get(userSessionId, "Dataset ds INCLUDE ds.investigation.facility", dsid); dsInfos.put(dsid, new DatasetInfo(ds)); - // dataset access for the user has been checked so the REST - // session for the - // reader account can be used if the IDS setting to allow this - // is enabled + // dataset access for the user has been checked so the REST session for the + // reader account can be used if the IDS setting to allow this is enabled String query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid + " AND df.location IS NOT NULL"; - JsonArray result = Json - .createReader(new ByteArrayInputStream( - restSessionToUse.search(query).getBytes())) + JsonArray result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())) .readArray().getJsonArray(0); if (result.getJsonNumber(2).longValueExact() == 0) { // Count 0 emptyDatasets.add(dsid); @@ -287,38 +236,31 @@ private DataSelectionService prepareFromIds(boolean dfWanted, for (Long invid : invids) { String query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + invid; - JsonArray result = Json - .createReader(new ByteArrayInputStream( - userRestSession.search(query).getBytes())) + JsonArray result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())) .readArray().getJsonArray(0); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, - userRestSession, restSessionToUse, result); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUse, result); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES - || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } else if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } else { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IcatException e) { - org.icatproject.icat.client.IcatException.IcatExceptionType type = e - .getType(); + org.icatproject.icat.client.IcatException.IcatExceptionType type = e.getType(); if (type == org.icatproject.icat.client.IcatException.IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == org.icatproject.icat.client.IcatException.IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } else if (type == org.icatproject.icat.client.IcatException.IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } else { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } /* @@ -330,17 +272,16 @@ private DataSelectionService prepareFromIds(boolean dfWanted, emptyDatasets = null; } - return this.createSelectionService(dsInfos, dfInfos, emptyDatasets, - invids, dsids, dfids, length, false, false, requestType); + return this.createSelectionService(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, false, false, requestType); } + /** * Checks to see if the investigation, dataset or datafile id list is a * valid comma separated list of longs. No spaces or leading 0's. Also * accepts null. */ - public static List getValidIds(String thing, String idList) - throws BadRequestException { + public static List getValidIds(String thing, String idList) throws BadRequestException { List result; if (idList == null) { @@ -352,8 +293,7 @@ public static List getValidIds(String thing, String idList) try { result.add(Long.parseLong(id)); } catch (NumberFormatException e) { - throw new BadRequestException("The " + thing - + " parameter '" + idList + "' is not a valid " + throw new BadRequestException("The " + thing + " parameter '" + idList + "' is not a valid " + "string representation of a comma separated list of longs"); } } @@ -361,13 +301,10 @@ public static List getValidIds(String thing, String idList) return result; } - private void manyDfs(Map dfInfos, long dsid, - Session restSessionToUse, JsonArray result) throws IcatException, - InsufficientPrivilegesException, InternalException { - // dataset access for the user has been checked so the REST session for - // the - // reader account can be used if the IDS setting to allow this is - // enabled + private void manyDfs(Map dfInfos, long dsid, Session restSessionToUse, JsonArray result) + throws IcatException, InsufficientPrivilegesException, InternalException { + // dataset access for the user has been checked so the REST session for the + // reader account can be used if the IDS setting to allow this is enabled long min = result.getJsonNumber(0).longValueExact(); long max = result.getJsonNumber(1).longValueExact(); long count = result.getJsonNumber(2).longValueExact(); @@ -375,52 +312,33 @@ private void manyDfs(Map dfInfos, long dsid, if (count != 0) { if (count <= maxEntities) { String query = "SELECT df.id, df.name, df.location, df.createId, df.modId FROM Datafile df WHERE df.dataset.id = " - + dsid - + " AND df.location IS NOT NULL AND df.id BETWEEN " - + min + " AND " + max; - result = Json - .createReader(new ByteArrayInputStream( - restSessionToUse.search(query).getBytes())) - .readArray(); + + dsid + " AND df.location IS NOT NULL AND df.id BETWEEN " + min + " AND " + max; + result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray(); for (JsonValue tupV : result) { JsonArray tup = (JsonArray) tupV; long dfid = tup.getJsonNumber(0).longValueExact(); - String location = LocationHelper.getLocation(dfid, - tup.getString(2, null)); + String location = LocationHelper.getLocation(dfid, tup.getString(2, null)); dfInfos.put(dfid, - new DatafileInfo(dfid, tup.getString(1), location, - tup.getString(3), tup.getString(4), dsid)); + new DatafileInfo(dfid, tup.getString(1), location, tup.getString(3), tup.getString(4), dsid)); } } else { long half = (min + max) / 2; String query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " - + dsid - + " AND df.location IS NOT NULL AND df.id BETWEEN " - + min + " AND " + half; - result = Json - .createReader(new ByteArrayInputStream( - restSessionToUse.search(query).getBytes())) - .readArray().getJsonArray(0); + + dsid + " AND df.location IS NOT NULL AND df.id BETWEEN " + min + " AND " + half; + result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray() + .getJsonArray(0); manyDfs(dfInfos, dsid, restSessionToUse, result); - query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " - + dsid - + " AND df.location IS NOT NULL AND df.id BETWEEN " - + (half + 1) + " AND " + max; - result = Json - .createReader(new ByteArrayInputStream( - restSessionToUse.search(query).getBytes())) - .readArray().getJsonArray(0); + query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid + + " AND df.location IS NOT NULL AND df.id BETWEEN " + (half + 1) + " AND " + max; + result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray() + .getJsonArray(0); manyDfs(dfInfos, dsid, restSessionToUse, result); } } } - private void manyDss(Map dsInfos, - HashSet emptyDatasets, Map dfInfos, - Long invid, boolean dfWanted, Session userRestSession, - Session restSessionToUseForDfs, JsonArray result) - throws IcatException, InsufficientPrivilegesException, - InternalException { + private void manyDss(Map dsInfos, HashSet emptyDatasets, Map dfInfos, Long invid, boolean dfWanted, Session userRestSession, Session restSessionToUseForDfs, JsonArray result) + throws IcatException, InsufficientPrivilegesException, InternalException { long min = result.getJsonNumber(0).longValueExact(); long max = result.getJsonNumber(1).longValueExact(); long count = result.getJsonNumber(2).longValueExact(); @@ -429,10 +347,7 @@ private void manyDss(Map dsInfos, if (count <= maxEntities) { String query = "SELECT inv.name, inv.visitId, inv.facility.id, inv.facility.name FROM Investigation inv WHERE inv.id = " + invid; - result = Json - .createReader(new ByteArrayInputStream( - userRestSession.search(query).getBytes())) - .readArray(); + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); if (result.size() == 0) { return; } @@ -442,25 +357,18 @@ private void manyDss(Map dsInfos, long facilityId = result.getJsonNumber(2).longValueExact(); String facilityName = result.getString(3); - query = "SELECT ds.id, ds.name, ds.location FROM Dataset ds WHERE ds.investigation.id = " - + invid + " AND ds.id BETWEEN " + min + " AND " + max; - result = Json - .createReader(new ByteArrayInputStream( - userRestSession.search(query).getBytes())) - .readArray(); + query = "SELECT ds.id, ds.name, ds.location FROM Dataset ds WHERE ds.investigation.id = " + invid + + " AND ds.id BETWEEN " + min + " AND " + max; + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); for (JsonValue tupV : result) { JsonArray tup = (JsonArray) tupV; long dsid = tup.getJsonNumber(0).longValueExact(); - dsInfos.put(dsid, - new DatasetInfo(dsid, tup.getString(1), - tup.getString(2, null), invid, invName, - visitId, facilityId, facilityName)); + dsInfos.put(dsid, new DatasetInfo(dsid, tup.getString(1), tup.getString(2, null), invid, invName, + visitId, facilityId, facilityName)); query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid + " AND df.location IS NOT NULL"; - result = Json - .createReader(new ByteArrayInputStream( - userRestSession.search(query).getBytes())) + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())) .readArray().getJsonArray(0); if (result.getJsonNumber(2).longValueExact() == 0) { emptyDatasets.add(dsid); @@ -473,21 +381,13 @@ private void manyDss(Map dsInfos, long half = (min + max) / 2; String query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + invid + " AND ds.id BETWEEN " + min + " AND " + half; - result = Json - .createReader(new ByteArrayInputStream( - userRestSession.search(query).getBytes())) - .readArray(); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, - userRestSession, restSessionToUseForDfs, result); + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUseForDfs, result); query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " - + invid + " AND ds.id BETWEEN " + half + 1 + " AND " - + max; - result = Json - .createReader(new ByteArrayInputStream( - userRestSession.search(query).getBytes())) - .readArray().getJsonArray(0); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, - userRestSession, restSessionToUseForDfs, result); + + invid + " AND ds.id BETWEEN " + half + 1 + " AND " + max; + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray() + .getJsonArray(0); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUseForDfs, result); } } @@ -498,60 +398,42 @@ private void createRequestTypeToReturnsMapping() throws InternalException { this.requestTypeToReturnsMapping = new HashMap(); StorageUnit storageUnit = this.propertyHandler.getStorageUnit(); - this.requestTypeToReturnsMapping.put(RequestType.DELETE, - Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETDATAFILEIDS, - Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETSIZE, - Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.PREPAREDATA, - Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.RESET, - Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.WRITE, - Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETDATA, - Returns.DATASETS_AND_DATAFILES); - - if (storageUnit == null) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, - Returns.DATASETS); - } else if (storageUnit == StorageUnit.DATAFILE) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, - Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.RESTORE, - Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, - Returns.DATAFILES); - } else if (storageUnit == StorageUnit.DATASET) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, - Returns.DATASETS); - this.requestTypeToReturnsMapping.put(RequestType.RESTORE, - Returns.DATASETS); - this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, - Returns.DATASETS); + this.requestTypeToReturnsMapping.put(RequestType.DELETE, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETDATAFILEIDS, Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETSIZE, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.PREPAREDATA, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.RESET, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.WRITE, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETDATA, Returns.DATASETS_AND_DATAFILES); + + if(storageUnit == null ) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATASETS); + } + else if (storageUnit == StorageUnit.DATAFILE) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.RESTORE, Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, Returns.DATAFILES); } + else if(storageUnit == StorageUnit.DATASET) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATASETS); + this.requestTypeToReturnsMapping.put(RequestType.RESTORE, Returns.DATASETS); + this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, Returns.DATASETS); + } + - else - throw new InternalException("StorageUnit " + storageUnit - + " unknown. Maybe you forgot to handle a new StorageUnit here?"); + else throw new InternalException("StorageUnit " + storageUnit + " unknown. Maybe you forgot to handle a new StorageUnit here?"); } - private Returns getReturns(RequestType requestType) - throws NotImplementedException { + private Returns getReturns(RequestType requestType) throws NotImplementedException { - if (this.requestTypeToReturnsMapping.containsKey(requestType)) + if(this.requestTypeToReturnsMapping.containsKey(requestType)) return this.requestTypeToReturnsMapping.get(requestType); + // is this needed here? - // if(this.propertyHandler.getStorageUnit() == null) throw new - // NotImplementedException("This operation is unavailable for single - // level storage"); - - throw new NotImplementedException("There is to mapping for RequestType." - + requestType + " and StorageUnit." - + this.propertyHandler.getStorageUnit() - + " defined. Did you forgot to register it in createRequestTypeToReturnsMapping()?"); + //if(this.propertyHandler.getStorageUnit() == null) throw new NotImplementedException("This operation is unavailable for single level storage"); + + throw new NotImplementedException("There is to mapping for RequestType." + requestType + " and StorageUnit." + this.propertyHandler.getStorageUnit() + " defined. Did you forgot to register it in createRequestTypeToReturnsMapping()?"); } } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java index c1a6a8a5..b7915daa 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java @@ -11,49 +11,47 @@ import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.models.DataInfoBase; -public class DataSelectionServiceForSingleLevelStorage - extends DataSelectionService { +public class DataSelectionServiceForSingleLevelStorage extends DataSelectionService { - protected DataSelectionServiceForSingleLevelStorage( - SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, - Boolean zip, Boolean compress, RequestType requestType) { - - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, - zip, compress, requestType); + protected DataSelectionServiceForSingleLevelStorage(SortedMap dsInfos, SortedMap dfInfos, + Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { + + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); } + @Override public SortedMap getPrimaryDataInfos() { return new TreeMap(); } - @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) - throws InternalException { - throw new InternalException( - "This operation is unavailable for single level storage"); + @Override + public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { + + throw new InternalException("This operation is unavailable for single level storage"); } + @Override public boolean isPrepared(String preparedId) throws InternalException { return true; } + @Override - public void queueDelete() - throws NotImplementedException, InternalException { - // nothing todo for single level storage + public void queueDelete() throws NotImplementedException, InternalException { + //nothing todo for single level storage } + @Override - public void scheduleTasks(DeferredOp operation) - throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { - throw new NotImplementedException( - "This operation is unavailable for single level storage"); + throw new NotImplementedException("This operation is unavailable for single level storage"); } + + + } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java index 18da82fb..f744fa8f 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java @@ -11,49 +11,44 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.ServiceProvider; -public class DataSelectionServiceForStorageUnitDatafile - extends DataSelectionService { +public class DataSelectionServiceForStorageUnitDatafile extends DataSelectionService { - protected DataSelectionServiceForStorageUnitDatafile( - SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, - Boolean zip, Boolean compress, RequestType requestType) { + protected DataSelectionServiceForStorageUnitDatafile(SortedMap dsInfos, SortedMap dfInfos, + Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, - zip, compress, requestType); + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); } + @Override public SortedMap getPrimaryDataInfos() { return this.dataSelection.getDfInfo(); } @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) - throws InternalException { - return ServiceProvider.getInstance().getMainStorage() - .exists(dataInfo.getLocation()); + public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { + return ServiceProvider.getInstance().getMainStorage().exists(dataInfo.getLocation()); } + @Override public boolean isPrepared(String preparedId) throws InternalException { return areDataInfosPrepared(preparedId); } + @Override - public void queueDelete() - throws NotImplementedException, InternalException { + public void queueDelete() throws NotImplementedException, InternalException { this.scheduleTasks(DeferredOp.DELETE); } + @Override - public void scheduleTasks(DeferredOp operation) - throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); } } - + } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java index d75a4920..ff1b5c65 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java @@ -12,56 +12,49 @@ import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.services.ServiceProvider; -public class DataSelectionServiceForStorageUnitDataset - extends DataSelectionService { +public class DataSelectionServiceForStorageUnitDataset extends DataSelectionService { - protected DataSelectionServiceForStorageUnitDataset( - SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, - Boolean zip, Boolean compress, RequestType requestType) { + protected DataSelectionServiceForStorageUnitDataset(SortedMap dsInfos, SortedMap dfInfos, + Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, - zip, compress, requestType); + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); } + @Override public SortedMap getPrimaryDataInfos() { return this.dataSelection.getDsInfo(); } @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) - throws InternalException { + public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { var dsInfo = (DatasetInfo) dataInfo; - if (dsInfo == null) - throw new InternalException( - "Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); - - return this.dataSelection.getEmptyDatasets().contains(dataInfo.getId()) - || ServiceProvider.getInstance().getMainStorage() - .exists(dsInfo); + if(dsInfo == null) throw new InternalException("Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); + + return this.dataSelection.getEmptyDatasets().contains(dataInfo.getId()) || ServiceProvider.getInstance().getMainStorage().exists(dsInfo); } + @Override public boolean isPrepared(String preparedId) throws InternalException { return this.areDataInfosPrepared(preparedId); } + @Override - public void queueDelete() - throws NotImplementedException, InternalException { + public void queueDelete() throws NotImplementedException, InternalException { this.scheduleTasks(DeferredOp.WRITE); } + @Override - public void scheduleTasks(DeferredOp operation) - throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); } } + } diff --git a/src/main/java/org/icatproject/ids/thread/DfArchiver.java b/src/main/java/org/icatproject/ids/thread/DfArchiver.java index 7dc6b74a..15df979f 100644 --- a/src/main/java/org/icatproject/ids/thread/DfArchiver.java +++ b/src/main/java/org/icatproject/ids/thread/DfArchiver.java @@ -17,8 +17,7 @@ * Removes datafiles from the fast storage (doesn't write them to archive storage) */ public class DfArchiver implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DfArchiver.class); + private final static Logger logger = LoggerFactory.getLogger(DfArchiver.class); private MainStorageInterface mainStorageInterface; private FiniteStateMachine fsm; @@ -26,9 +25,7 @@ public class DfArchiver implements Runnable { private Path markerDir; private Collection locks; - public DfArchiver(List dfInfos, - PropertyHandler propertyHandler, FiniteStateMachine fsm, - Collection locks) { + public DfArchiver(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { this.dfInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -41,19 +38,16 @@ public void run() { try { for (DatafileInfo dfInfo : dfInfos) { try { - if (Files.exists(markerDir - .resolve(Long.toString(dfInfo.getDfId())))) { + if (Files.exists(markerDir.resolve(Long.toString(dfInfo.getDfId())))) { logger.error("Archive of " + dfInfo + " not carried out because a write to secondary storage operation failed previously"); } else { String dfLocation = dfInfo.getDfLocation(); - mainStorageInterface.delete(dfLocation, - dfInfo.getCreateId(), dfInfo.getModId()); + mainStorageInterface.delete(dfLocation, dfInfo.getCreateId(), dfInfo.getModId()); logger.debug("Archive of " + dfInfo + " completed"); } } catch (Exception e) { - logger.error("Archive of " + dfInfo + " failed due to " - + e.getClass() + " " + e.getMessage()); + logger.error("Archive of " + dfInfo + " failed due to " + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dfInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DfDeleter.java b/src/main/java/org/icatproject/ids/thread/DfDeleter.java index dd4afc0c..e0b4dab6 100644 --- a/src/main/java/org/icatproject/ids/thread/DfDeleter.java +++ b/src/main/java/org/icatproject/ids/thread/DfDeleter.java @@ -16,17 +16,14 @@ */ public class DfDeleter implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DfDeleter.class); + private final static Logger logger = LoggerFactory.getLogger(DfDeleter.class); private FiniteStateMachine fsm; private ArchiveStorageInterface archiveStorageInterface; private List dfInfos; private Collection locks; - public DfDeleter(List dfInfos, - PropertyHandler propertyHandler, FiniteStateMachine fsm, - Collection locks) { + public DfDeleter(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { this.dfInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -42,8 +39,7 @@ public void run() { archiveStorageInterface.delete(dfLocation); logger.debug("Delete of " + dfInfo + " completed"); } catch (Exception e) { - logger.error("Delete of " + dfInfo + " failed due to " - + e.getClass() + " " + e.getMessage()); + logger.error("Delete of " + dfInfo + " failed due to " + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dfInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DfRestorer.java b/src/main/java/org/icatproject/ids/thread/DfRestorer.java index 5c75d189..a97327b5 100644 --- a/src/main/java/org/icatproject/ids/thread/DfRestorer.java +++ b/src/main/java/org/icatproject/ids/thread/DfRestorer.java @@ -21,8 +21,7 @@ */ public class DfRestorer implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DfRestorer.class); + private final static Logger logger = LoggerFactory.getLogger(DfRestorer.class); private MainStorageInterface mainStorageInterface; private ArchiveStorageInterface archiveStorageInterface; @@ -30,9 +29,7 @@ public class DfRestorer implements Runnable { private List dataFileInfos; private Collection locks; - public DfRestorer(List dfInfos, - PropertyHandler propertyHandler, FiniteStateMachine fsm, - Collection locks) { + public DfRestorer(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { this.dataFileInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -64,14 +61,14 @@ public void run() { } } - // TODO: This is additional conversion caused by the redesign :-( + //TODO: This is additional conversion caused by the redesign :-( List dfInfos = new ArrayList<>(); - for (DfInfo dfInfo : this.dataFileInfos) { + for(DfInfo dfInfo : this.dataFileInfos) { dfInfos.add(dfInfo); } - Set failures = archiveStorageInterface - .restore(mainStorageInterface, dfInfos); + + Set failures = archiveStorageInterface.restore(mainStorageInterface, dfInfos); for (DatafileInfo dfInfo : dataFileInfos) { if (failures.contains(dfInfo)) { fsm.recordFailure(dfInfo.getDfId()); @@ -84,8 +81,7 @@ public void run() { } } catch (Exception e) { for (DatafileInfo dfInfo : dataFileInfos) { - logger.error("Restore of " + dfInfo + " failed " + e.getClass() - + " " + e.getMessage()); + logger.error("Restore of " + dfInfo + " failed " + e.getClass() + " " + e.getMessage()); fsm.removeFromChanging(dfInfo); } } finally { diff --git a/src/main/java/org/icatproject/ids/thread/DfWriter.java b/src/main/java/org/icatproject/ids/thread/DfWriter.java index 05faa301..09881acc 100644 --- a/src/main/java/org/icatproject/ids/thread/DfWriter.java +++ b/src/main/java/org/icatproject/ids/thread/DfWriter.java @@ -20,8 +20,7 @@ */ public class DfWriter implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DfWriter.class); + private final static Logger logger = LoggerFactory.getLogger(DfWriter.class); private FiniteStateMachine fsm; private MainStorageInterface mainStorageInterface; @@ -30,8 +29,7 @@ public class DfWriter implements Runnable { private List dataFileInfos; private Collection locks; - public DfWriter(List dfInfos, PropertyHandler propertyHandler, - FiniteStateMachine fsm, Collection locks) { + public DfWriter(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { this.dataFileInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -45,17 +43,14 @@ public void run() { try { for (DatafileInfo dataFileInfo : dataFileInfos) { String dfLocation = dataFileInfo.getDfLocation(); - try (InputStream is = mainStorageInterface.get(dfLocation, - dataFileInfo.getCreateId(), dataFileInfo.getModId())) { + try (InputStream is = mainStorageInterface.get(dfLocation, dataFileInfo.getCreateId(), dataFileInfo.getModId())) { archiveStorageInterface.put(is, dfLocation); - Path marker = markerDir - .resolve(Long.toString(dataFileInfo.getDfId())); + Path marker = markerDir.resolve(Long.toString(dataFileInfo.getDfId())); Files.deleteIfExists(marker); logger.debug("Removed marker " + marker); logger.debug("Write of " + dataFileInfo + " completed"); } catch (Exception e) { - logger.error("Write of " + dataFileInfo + " failed due to " - + e.getClass() + " " + e.getMessage()); + logger.error("Write of " + dataFileInfo + " failed due to " + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dataFileInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DsArchiver.java b/src/main/java/org/icatproject/ids/thread/DsArchiver.java index a017b182..81c2c664 100644 --- a/src/main/java/org/icatproject/ids/thread/DsArchiver.java +++ b/src/main/java/org/icatproject/ids/thread/DsArchiver.java @@ -15,8 +15,7 @@ * Removes datasets from the fast storage (doesn't write them to slow storage) */ public class DsArchiver implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DsArchiver.class); + private final static Logger logger = LoggerFactory.getLogger(DsArchiver.class); private DatasetInfo dsInfo; private MainStorageInterface mainStorageInterface; @@ -24,8 +23,7 @@ public class DsArchiver implements Runnable { private Path markerDir; private Lock lock; - public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, - FiniteStateMachine fsm, Lock lock) { + public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; mainStorageInterface = propertyHandler.getMainStorage(); @@ -36,8 +34,7 @@ public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, @Override public void run() { try { - if (Files.exists( - markerDir.resolve(Long.toString(dsInfo.getDsId())))) { + if (Files.exists(markerDir.resolve(Long.toString(dsInfo.getDsId())))) { logger.error("Archive of " + dsInfo + " not carried out because a write to secondary storage operation failed previously"); } else { @@ -45,8 +42,7 @@ public void run() { logger.debug("Archive of " + dsInfo + " completed"); } } catch (Exception e) { - logger.error("Archive of " + dsInfo + " failed due to " - + e.getMessage()); + logger.error("Archive of " + dsInfo + " failed due to " + e.getMessage()); } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/main/java/org/icatproject/ids/thread/DsRestorer.java b/src/main/java/org/icatproject/ids/thread/DsRestorer.java index 9e4e73d6..20c7b49a 100644 --- a/src/main/java/org/icatproject/ids/thread/DsRestorer.java +++ b/src/main/java/org/icatproject/ids/thread/DsRestorer.java @@ -31,8 +31,7 @@ */ public class DsRestorer implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DsRestorer.class); + private final static Logger logger = LoggerFactory.getLogger(DsRestorer.class); private DatasetInfo dsInfo; @@ -47,8 +46,7 @@ public class DsRestorer implements Runnable { private ZipMapperInterface zipMapper; private Lock lock; - public DsRestorer(DatasetInfo dsInfo, PropertyHandler propertyHandler, - FiniteStateMachine fsm, IcatReader reader, Lock lock) { + public DsRestorer(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, IcatReader reader, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; zipMapper = propertyHandler.getZipMapper(); @@ -73,48 +71,39 @@ public void run() { long size = 0; int n = 0; - List datafiles = ((Dataset) reader - .get("Dataset INCLUDE Datafile", dsInfo.getDsId())) + List datafiles = ((Dataset) reader.get("Dataset INCLUDE Datafile", dsInfo.getDsId())) .getDatafiles(); - Map nameToLocalMap = new HashMap<>( - datafiles.size()); + Map nameToLocalMap = new HashMap<>(datafiles.size()); for (Datafile datafile : datafiles) { if (datafile.getLocation() == null) { continue; } - nameToLocalMap.put(datafile.getName(), LocationHelper - .getLocation(datafile.getId(), datafile.getLocation())); + nameToLocalMap.put(datafile.getName(), LocationHelper.getLocation(datafile.getId(), datafile.getLocation())); size += datafile.getFileSize(); n++; } - logger.debug("Restoring dataset " + dsInfo.getInvId() + "/" - + dsInfo.getDsId() + " with " + n + " files of total size " - + size); + logger.debug("Restoring dataset " + dsInfo.getInvId() + "/" + dsInfo.getDsId() + " with " + n + + " files of total size " + size); // Get the file into the dataset cache - Path datasetCachePath = Files.createTempFile(datasetCache, null, - null); + Path datasetCachePath = Files.createTempFile(datasetCache, null, null); archiveStorageInterface.get(dsInfo, datasetCachePath); // Now split file and store it locally - logger.debug("Unpacking dataset " + dsInfo.getInvId() + "/" - + dsInfo.getDsId() + " with " + n + " files of total size " - + size); - ZipInputStream zis = new ZipInputStream( - Files.newInputStream(datasetCachePath)); + logger.debug("Unpacking dataset " + dsInfo.getInvId() + "/" + dsInfo.getDsId() + " with " + n + + " files of total size " + size); + ZipInputStream zis = new ZipInputStream(Files.newInputStream(datasetCachePath)); ZipEntry ze = zis.getNextEntry(); Set seen = new HashSet<>(); while (ze != null) { String dfName = zipMapper.getFileName(ze.getName()); if (seen.contains(dfName)) { - throw new RuntimeException("Corrupt archive for " + dsInfo - + ": duplicate entry " + dfName); + throw new RuntimeException("Corrupt archive for " + dsInfo + ": duplicate entry " + dfName); } String location = nameToLocalMap.get(dfName); if (location == null) { - throw new RuntimeException("Corrupt archive for " + dsInfo - + ": spurious entry " + dfName); + throw new RuntimeException("Corrupt archive for " + dsInfo + ": spurious entry " + dfName); } mainStorageInterface.put(zis, location); ze = zis.getNextEntry(); @@ -122,16 +111,14 @@ public void run() { } zis.close(); if (!seen.equals(nameToLocalMap.keySet())) { - throw new RuntimeException( - "Corrupt archive for " + dsInfo + ": missing entries"); + throw new RuntimeException("Corrupt archive for " + dsInfo + ": missing entries"); } Files.delete(datasetCachePath); fsm.recordSuccess(dsInfo.getDsId()); logger.debug("Restore of " + dsInfo + " completed"); } catch (Exception e) { fsm.recordFailure(dsInfo.getDsId()); - logger.error("Restore of " + dsInfo + " failed due to " - + e.getClass() + " " + e.getMessage()); + logger.error("Restore of " + dsInfo + " failed due to " + e.getClass() + " " + e.getMessage()); try { mainStorageInterface.delete(dsInfo); } catch (IOException e2) { diff --git a/src/main/java/org/icatproject/ids/thread/DsWriter.java b/src/main/java/org/icatproject/ids/thread/DsWriter.java index 55e65f21..e5c1843b 100644 --- a/src/main/java/org/icatproject/ids/thread/DsWriter.java +++ b/src/main/java/org/icatproject/ids/thread/DsWriter.java @@ -30,8 +30,7 @@ */ public class DsWriter implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DsWriter.class); + private final static Logger logger = LoggerFactory.getLogger(DsWriter.class); private static final int BUFSIZ = 1024; private DatasetInfo dsInfo; @@ -44,8 +43,7 @@ public class DsWriter implements Runnable { private ZipMapperInterface zipMapper; private Lock lock; - public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, - FiniteStateMachine fsm, IcatReader reader, Lock lock) { + public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, IcatReader reader, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; this.zipMapper = propertyHandler.getZipMapper(); @@ -61,35 +59,27 @@ public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, public void run() { try { if (!mainStorageInterface.exists(dsInfo)) { - logger.info("No files present in main storage for " + dsInfo - + " - will delete archive"); + logger.info("No files present in main storage for " + dsInfo + " - will delete archive"); archiveStorageInterface.delete(dsInfo); } else { - Path datasetCachePath = Files.createTempFile(datasetCache, null, - null); + Path datasetCachePath = Files.createTempFile(datasetCache, null, null); logger.debug("Creating " + datasetCachePath); - List datafiles = ((Dataset) reader - .get("Dataset INCLUDE Datafile", dsInfo.getDsId())) + List datafiles = ((Dataset) reader.get("Dataset INCLUDE Datafile", dsInfo.getDsId())) .getDatafiles(); - ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream( - datasetCachePath, StandardOpenOption.CREATE)); + ZipOutputStream zos = new ZipOutputStream( + Files.newOutputStream(datasetCachePath, StandardOpenOption.CREATE)); for (Datafile datafile : datafiles) { if (datafile.getLocation() == null) { continue; } - String location = LocationHelper.getLocation( - datafile.getId(), datafile.getLocation()); + String location = LocationHelper.getLocation(datafile.getId(), datafile.getLocation()); InputStream is = null; try { - zos.putNextEntry( - new ZipEntry(zipMapper.getFullEntryName(dsInfo, - new DatafileInfo(datafile.getId(), - datafile.getName(), location, - datafile.getCreateId(), - datafile.getModId(), 0L)))); - is = mainStorageInterface.get(location, - datafile.getCreateId(), datafile.getModId()); + zos.putNextEntry(new ZipEntry( + zipMapper.getFullEntryName(dsInfo, new DatafileInfo(datafile.getId(), datafile.getName(), + location, datafile.getCreateId(), datafile.getModId(), 0L)))); + is = mainStorageInterface.get(location, datafile.getCreateId(), datafile.getModId()); int bytesRead = 0; byte[] buffer = new byte[BUFSIZ]; while ((bytesRead = is.read(buffer)) > 0) { @@ -115,8 +105,7 @@ public void run() { logger.debug("Removed marker " + marker); logger.debug("Write of " + dsInfo + " completed"); } catch (Exception e) { - logger.error("Write of " + dsInfo + " failed due to " + e.getClass() - + " " + e.getMessage()); + logger.error("Write of " + dsInfo + " failed due to " + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/test/java/org/icatproject/ids/DataSelectionDevTest.java b/src/test/java/org/icatproject/ids/DataSelectionDevTest.java index 0f717ed0..f0ba796e 100644 --- a/src/test/java/org/icatproject/ids/DataSelectionDevTest.java +++ b/src/test/java/org/icatproject/ids/DataSelectionDevTest.java @@ -31,8 +31,8 @@ /** * This test was created to fix issue #115 and was run against the Diamond - * pre-production ICAT using specifically selected usernames of users known to - * have access to a lot of data. + * pre-production ICAT using specifically selected usernames of users known + * to have access to a lot of data. * * As well as monitoring the time taken to create the DataSelection, detailed * monitoring of the eclipselink SQL logging was done to confirm that the @@ -74,26 +74,21 @@ public class DataSelectionDevTest { @Before public void setup() throws Exception { testProps = new Properties(); - testProps.load(new FileInputStream( - "src/test/resources/DataSelectionDevTest.properties")); + testProps.load(new FileInputStream("src/test/resources/DataSelectionDevTest.properties")); // set up the SOAP and REST ICAT clients icatUrl = ICATGetter.getCleanUrl(testProps.getProperty("icat.url")); icatService = ICATGetter.getService(icatUrl); restIcat = new org.icatproject.icat.client.ICAT(icatUrl); - // get session IDs for an end user and the reader user (with read-all - // permissions) + // get session IDs for an end user and the reader user (with read-all permissions) String userCredsString = testProps.getProperty("login.user"); userSessionId = TestUtils.login(icatService, userCredsString); System.out.println("userSessionId = " + userSessionId); - List readerCreds = Arrays.asList( - testProps.getProperty("login.reader").trim().split("\\s+")); + List readerCreds = Arrays.asList(testProps.getProperty("login.reader").trim().split("\\s+")); this.readerCreds = readerCreds; investigationIds = testProps.getProperty("investigation.ids"); datasetIds = testProps.getProperty("dataset.ids"); datafileIds = testProps.getProperty("datafile.ids"); - useReaderForPerformance = testProps - .getProperty("useReaderForPerformance") - .equalsIgnoreCase("true"); + useReaderForPerformance = testProps.getProperty("useReaderForPerformance").equalsIgnoreCase("true"); // set up a mocked version of the PropertyHandler setupPropertyHandler(); icatReader = new IcatReader(mockedPropertyHandler); @@ -101,32 +96,24 @@ public void setup() throws Exception { private void setupPropertyHandler() throws URISyntaxException, IcatException_Exception, IcatException { - JsonReader parser = Json.createReader( - new ByteArrayInputStream(restIcat.getProperties().getBytes())); + JsonReader parser = Json.createReader(new ByteArrayInputStream(restIcat.getProperties().getBytes())); maxEntities = parser.readObject().getInt("maxEntities"); when(mockedPropertyHandler.getMaxEntities()).thenReturn(maxEntities); when(mockedPropertyHandler.getIcatService()).thenReturn(icatService); when(mockedPropertyHandler.getRestIcat()).thenReturn(restIcat); when(mockedPropertyHandler.getReader()).thenReturn(readerCreds); - when(mockedPropertyHandler.getUseReaderForPerformance()) - .thenReturn(useReaderForPerformance); + when(mockedPropertyHandler.getUseReaderForPerformance()).thenReturn(useReaderForPerformance); } @Ignore("Test requires a specific ICAT setup to produce meaningful results. See class javadoc comment.") @Test public void testCreateDataSelection() throws Exception { long startMs = System.currentTimeMillis(); - var dataSelectionFactory = DataSelectionServiceFactory - .getInstanceOnlyForTesting(mockedPropertyHandler, icatReader); - DataSelectionService dataSelectionService = dataSelectionFactory - .getSelectionService(userSessionId, investigationIds, - datasetIds, datafileIds, RequestType.GETSIZE); - System.out.println("Creating DataSelection took " - + (System.currentTimeMillis() - startMs) + " ms"); - System.out.println( - "DsInfo size: " + dataSelectionService.getDsInfo().size()); - System.out.println( - "DfInfo size: " + dataSelectionService.getDfInfo().size()); + var dataSelectionFactory = DataSelectionServiceFactory.getInstanceOnlyForTesting(mockedPropertyHandler, icatReader); + DataSelectionService dataSelectionService = dataSelectionFactory.getSelectionService(userSessionId,investigationIds, datasetIds, datafileIds, RequestType.GETSIZE); + System.out.println("Creating DataSelection took " + (System.currentTimeMillis() - startMs) + " ms"); + System.out.println("DsInfo size: " + dataSelectionService.getDsInfo().size()); + System.out.println("DfInfo size: " + dataSelectionService.getDfInfo().size()); // there must be at least one Datafile in the DataSelection assertTrue("message", dataSelectionService.getDfInfo().size() > 0); } diff --git a/src/test/java/org/icatproject/ids/DigestTest.java b/src/test/java/org/icatproject/ids/DigestTest.java index fb09fd81..735e6195 100644 --- a/src/test/java/org/icatproject/ids/DigestTest.java +++ b/src/test/java/org/icatproject/ids/DigestTest.java @@ -16,18 +16,15 @@ public class DigestTest { @Test public void testCheck() throws Exception { - String a = location + " " - + IcatSecurity.digest(1234567L, location, key); + String a = location + " " + IcatSecurity.digest(1234567L, location, key); - assertEquals(location, - LocationHelper.getLocationFromDigest(1234567L, a, key)); + assertEquals(location, LocationHelper.getLocationFromDigest(1234567L, a, key)); try { LocationHelper.getLocationFromDigest(1234568L, a, key); fail(); } catch (InsufficientPrivilegesException e) { - assertTrue( - e.getMessage().contains("does not contain a valid hash")); + assertTrue(e.getMessage().contains("does not contain a valid hash")); } try { diff --git a/src/test/java/org/icatproject/ids/PreparePackingTest.java b/src/test/java/org/icatproject/ids/PreparePackingTest.java index 531bca86..6b2a857e 100644 --- a/src/test/java/org/icatproject/ids/PreparePackingTest.java +++ b/src/test/java/org/icatproject/ids/PreparePackingTest.java @@ -37,24 +37,21 @@ public void packAndUnpack() throws Exception { long dsid2 = 18L; long invId = 15L; long facilityId = 45L; - dfInfos.put(5L, new DatafileInfo(5L, "dfName", "dfLocation", "createId", - "modId", dsid1)); + dfInfos.put(5L, new DatafileInfo(5L, "dfName", "dfLocation", "createId", "modId", dsid1)); - dfInfos.put(51L, new DatafileInfo(51L, "dfName2", null, "createId", - "modId", dsid1)); + dfInfos.put(51L, new DatafileInfo(51L, "dfName2", null, "createId", "modId", dsid1)); - dsInfos.put(dsid1, new DatasetInfo(dsid1, "dsName", "dsLocation", invId, - "invName", "visitId", facilityId, "facilityName")); + dsInfos.put(dsid1, new DatasetInfo(dsid1, "dsName", "dsLocation", invId, "invName", + "visitId", facilityId, "facilityName")); - dsInfos.put(dsid2, new DatasetInfo(dsid2, "dsName2", null, invId, - "invName", "visitId", facilityId, "facilityName")); + dsInfos.put(dsid2, new DatasetInfo(dsid2, "dsName2", null, invId, "invName", "visitId", + facilityId, "facilityName")); emptyDatasets.add(dsid2); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (OutputStream stream = new BufferedOutputStream(baos)) { - Prepared.pack(stream, zip, compress, dsInfos, dfInfos, - emptyDatasets, 51); + Prepared.pack(stream, zip, compress, dsInfos, dfInfos, emptyDatasets, 51); } System.out.println(baos.toString()); InputStream stream = new ByteArrayInputStream(baos.toByteArray()); diff --git a/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java b/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java index aec0b0ee..e6960866 100644 --- a/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java +++ b/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java @@ -49,8 +49,7 @@ public void t3() throws Exception { os.flush(); } os.close(); - assertEquals("m is " + m, "ABCDEFGHIJKLMONPQRSTUVWXYZ", - baos.toString()); + assertEquals("m is " + m, "ABCDEFGHIJKLMONPQRSTUVWXYZ", baos.toString()); } } @@ -69,6 +68,7 @@ public void t4() throws Exception { } } + @Test public void t5() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); diff --git a/src/test/java/org/icatproject/ids/TestICATGetter.java b/src/test/java/org/icatproject/ids/TestICATGetter.java index 3cb52127..8bfa99c6 100644 --- a/src/test/java/org/icatproject/ids/TestICATGetter.java +++ b/src/test/java/org/icatproject/ids/TestICATGetter.java @@ -10,20 +10,17 @@ public class TestICATGetter { @Test public void testGFUrl() throws Exception { - assertEquals("http://localhost", ICATGetter - .getCleanUrl("http://localhost/ICATService/ICAT?wsdl")); + assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost/ICATService/ICAT?wsdl")); } @Test public void testSlash() throws Exception { - assertEquals("http://localhost", - ICATGetter.getCleanUrl("http://localhost/")); + assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost/")); } @Test public void testClean() throws Exception { - assertEquals("http://localhost", - ICATGetter.getCleanUrl("http://localhost")); + assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost")); } @Test diff --git a/src/test/java/org/icatproject/ids/TestUtils.java b/src/test/java/org/icatproject/ids/TestUtils.java index 7b313f42..cbd62530 100644 --- a/src/test/java/org/icatproject/ids/TestUtils.java +++ b/src/test/java/org/icatproject/ids/TestUtils.java @@ -12,16 +12,15 @@ public class TestUtils { /** * Do an ICAT login and get the session ID using a list of credentials of - * the format (as found in the properties files): "db username READER - * password READERpass" + * the format (as found in the properties files): + * "db username READER password READERpass" * * @param icatService the ICAT to log in to * @param credsString a String of credentials in the format described above * @return an ICAT session ID * @throws IcatException_Exception if the login fails */ - public static String login(ICAT icatService, String credsString) - throws IcatException_Exception { + public static String login(ICAT icatService, String credsString) throws IcatException_Exception { List creds = Arrays.asList(credsString.trim().split("\\s+")); Credentials credentials = new Credentials(); List entries = credentials.getEntry(); diff --git a/src/test/java/org/icatproject/ids/TestValueContainer.java b/src/test/java/org/icatproject/ids/TestValueContainer.java index 9dca1278..bc3daf98 100644 --- a/src/test/java/org/icatproject/ids/TestValueContainer.java +++ b/src/test/java/org/icatproject/ids/TestValueContainer.java @@ -15,11 +15,12 @@ import org.icatproject.ids.helpers.ValueContainer; import org.junit.Test; + public class TestValueContainer { @Test public void testInvalidValueContainer() throws Exception { - + var vc = ValueContainer.getInvalid(); assertFalse(vc.isVoid()); assertTrue(vc.getType() == ValueContainerType.INVALID); @@ -27,7 +28,7 @@ public void testInvalidValueContainer() throws Exception { @Test public void testVoidValueContainer() throws Exception { - + var vc = ValueContainer.getVoid(); assertTrue(vc.isVoid()); assertTrue(vc.getType() == ValueContainerType.VOID); @@ -36,7 +37,7 @@ public void testVoidValueContainer() throws Exception { @Test(expected = InternalException.class) public void testIntValueContainer() throws Exception { - + var vc = new ValueContainer(1); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -49,7 +50,7 @@ public void testIntValueContainer() throws Exception { @Test(expected = InternalException.class) public void testLongValueContainer() throws Exception { - + var vc = new ValueContainer(1L); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -62,7 +63,7 @@ public void testLongValueContainer() throws Exception { @Test(expected = InternalException.class) public void testBoolValueContainer() throws Exception { - + var vc = new ValueContainer(true); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -75,7 +76,7 @@ public void testBoolValueContainer() throws Exception { @Test(expected = InternalException.class) public void testStringValueContainer() throws Exception { - + var vc = new ValueContainer("test"); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -88,7 +89,7 @@ public void testStringValueContainer() throws Exception { @Test(expected = InternalException.class) public void testInputStreamValueContainer() throws Exception { - + String s = "test InputStream"; var inputStream = new ByteArrayInputStream(s.getBytes()); var vc = new ValueContainer(inputStream); @@ -97,9 +98,8 @@ public void testInputStreamValueContainer() throws Exception { assertFalse(vc.isNull()); assertEquals(vc.getInputStream(), inputStream); - - var ISReader = new InputStreamReader(vc.getInputStream(), - StandardCharsets.UTF_8); + + var ISReader = new InputStreamReader(vc.getInputStream(), StandardCharsets.UTF_8); var BReader = new BufferedReader(ISReader); String textFromStream = BReader.lines().collect(Collectors.joining()); BReader.close(); diff --git a/src/test/java/org/icatproject/ids/TestZipping.java b/src/test/java/org/icatproject/ids/TestZipping.java index 03f1ba91..56dce770 100644 --- a/src/test/java/org/icatproject/ids/TestZipping.java +++ b/src/test/java/org/icatproject/ids/TestZipping.java @@ -19,14 +19,12 @@ public class TestZipping { @Test public void testDuplicates() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ZipOutputStream zos = new ZipOutputStream( - new BufferedOutputStream(baos)); + ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos)); // zos.setLevel(0); List in = new ArrayList<>(); - for (String entryName : Arrays.asList("abcd/qa", "abcd/qw", "abcd/qw", - "abcd/qb", "abcd/qc", "abcd/qw")) { + for (String entryName : Arrays.asList("abcd/qa", "abcd/qw", "abcd/qw", "abcd/qb", "abcd/qc", "abcd/qw")) { try { zos.putNextEntry(new ZipEntry(entryName)); @@ -44,8 +42,7 @@ public void testDuplicates() throws Exception { assertEquals(4, in.size()); - ByteArrayInputStream bais = new ByteArrayInputStream( - baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ZipInputStream zis = new ZipInputStream(bais); int off = 0; diff --git a/src/test/java/org/icatproject/ids/TidierTest.java b/src/test/java/org/icatproject/ids/TidierTest.java index 80c6f731..8f2762c6 100644 --- a/src/test/java/org/icatproject/ids/TidierTest.java +++ b/src/test/java/org/icatproject/ids/TidierTest.java @@ -85,16 +85,13 @@ public void testCleanPreparedDir() throws Exception { @Test public void testAddStringConstraint() throws Exception { StringBuilder sb1 = new StringBuilder(); - Tidier.addStringConstraint(sb1, "df.location", "/path/to/normal/file", - false); + Tidier.addStringConstraint(sb1, "df.location", "/path/to/normal/file", false); assertEquals(" df.location = '/path/to/normal/file'", sb1.toString()); /* Fix error where a file path contains an apostrophe */ StringBuilder sb2 = new StringBuilder(); - Tidier.addStringConstraint(sb2, "df.location", - "/path/to/Person's Files/myscript.py", false); - assertEquals(" df.location = '/path/to/Person''s Files/myscript.py'", - sb2.toString()); + Tidier.addStringConstraint(sb2, "df.location", "/path/to/Person's Files/myscript.py", false); + assertEquals(" df.location = '/path/to/Person''s Files/myscript.py'", sb2.toString()); } @Test diff --git a/src/test/java/org/icatproject/ids/TransmittingTest.java b/src/test/java/org/icatproject/ids/TransmittingTest.java index 251a126d..7e2e986c 100644 --- a/src/test/java/org/icatproject/ids/TransmittingTest.java +++ b/src/test/java/org/icatproject/ids/TransmittingTest.java @@ -40,10 +40,10 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; + /** - * This test was created when the building of the transmission body was made - * more comon and generic. It should ensure the right structure of the - * containing json. + * This test was created when the building of the transmission body was made more comon and generic. + * It should ensure the right structure of the containing json. */ @RunWith(MockitoJUnitRunner.class) public class TransmittingTest { @@ -58,8 +58,7 @@ public class TransmittingTest { private LockManager mockedLockManager; @Mock private IcatReader mockedReader; - @Mock - ArchiveStorageInterface mockedArchiveStorage; + @Mock ArchiveStorageInterface mockedArchiveStorage; @Mock private ICAT mockedIcat; @@ -86,101 +85,81 @@ private void setup() } @Test - public void testTransmitterBodyForArchiveRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForArchiveRequest_shouldBeOk() throws Exception { + setup(); - var handler = new ArchiveHandler(ip, sessionId, investigationIds, - datasetIds, datafileIds); + var handler = new ArchiveHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetSizeRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetSizeRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetSizeHandler(ip, preparedId, null, null, null, - null); + var handler = new GetSizeHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetSizeHandler(ip, null, sessionId, investigationIds, - datasetIds, datafileIds); + handler = new GetSizeHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetSizeFastRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetSizeFastRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetSizeHandlerForFastProcessing(ip, sessionId, - investigationIds, datasetIds, datafileIds); + var handler = new GetSizeHandlerForFastProcessing(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForDeleteRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForDeleteRequest_shouldBeOk() throws Exception { + setup(); - var handler = new DeleteHandler(ip, sessionId, investigationIds, - datasetIds, datafileIds); + var handler = new DeleteHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetDataFileIdsRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetDataFileIdsRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetDataFileIdsHandler(ip, preparedId, null, null, - null, null); + var handler = new GetDataFileIdsHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetDataFileIdsHandler("192.168.17.1", null, sessionId, - investigationIds, datasetIds, datafileIds); + handler = new GetDataFileIdsHandler("192.168.17.1", null, sessionId, investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetDataRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetDataRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetDataHandler(ip, preparedId, null, null, null, null, - false, false, "", ""); + var handler = new GetDataHandler(ip, preparedId, null, null, null, null, false, false, "", ""); String body = handler.provideTransmissionBody(); - assertEquals( - "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"transferId\":-1}", - body); + assertEquals("{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"transferId\":-1}", body); - handler = new GetDataHandler(ip, null, sessionId, investigationIds, - datasetIds, datafileIds, false, false, "", ""); + handler = new GetDataHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds, false, false, "", ""); body = handler.provideTransmissionBody(); - assertEquals( - "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"transferId\":-1}", - body); + assertEquals("{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"transferId\":-1}", body); } @Test - public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() throws Exception { + setup(); var handler = new GetIcatUrlHandler(ip); @@ -189,9 +168,8 @@ public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() } @Test - public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() throws Exception { + setup(); var handler = new GetServiceStatusHandler(ip, sessionId); @@ -200,39 +178,32 @@ public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() } @Test - public void testTransmitterBodyForGetStatusRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetStatusRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetStatusHandler(ip, preparedId, null, null, null, - null); + var handler = new GetStatusHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetStatusHandler(ip, null, sessionId, investigationIds, - datasetIds, datafileIds); + handler = new GetStatusHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForIsPreparedRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForIsPreparedRequest_shouldBeOk() throws Exception { + setup(); var handler = new IsPreparedHandler(ip, preparedId); String body = handler.provideTransmissionBody(); - assertEquals( - "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}", - body); + assertEquals("{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}", body); } @Test - public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() throws Exception { + setup(); var handler = new IsReadOnlyHandler(ip); @@ -241,9 +212,8 @@ public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() } @Test - public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() throws Exception { + setup(); var handler = new IsTwoLevelHandler(ip); @@ -252,69 +222,56 @@ public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() } @Test - public void testTransmitterBodyForPrepareDataRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForPrepareDataRequest_shouldBeOk() throws Exception { + setup(); - var handler = new PrepareDataHandler(ip, sessionId, investigationIds, - datasetIds, datafileIds, false, false); + var handler = new PrepareDataHandler(ip, sessionId, investigationIds, datasetIds, datafileIds, false, false); String body = handler.provideTransmissionBody(); - assertEquals( - "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"preparedId\":\"\"}", - body); + assertEquals("{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"preparedId\":\"\"}", body); } @Test public void testTransmitterBodyForPutRequest_shouldBeOk() throws Exception { - + setup(); - var handler = new PutHandler(ip, sessionId, - new ByteArrayInputStream("".getBytes()), "someName", - datafileIds, datasetIds, "someDescription", "someDOI", - "simeCreateTimeString", "someModTimeString", false, false); + var handler = new PutHandler(ip, sessionId, new ByteArrayInputStream( "".getBytes() ), "someName", datafileIds , datasetIds, "someDescription", "someDOI", "simeCreateTimeString", "someModTimeString", false, false ); String body = handler.provideTransmissionBody(); assertEquals("{\"userName\":\"TestUser\",\"datafileId\":-1}", body); } @Test - public void testTransmitterBodyForResetRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForResetRequest_shouldBeOk() throws Exception { + setup(); var handler = new ResetHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new ResetHandler(ip, null, sessionId, investigationIds, - datasetIds, datafileIds); + handler = new ResetHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForRestoreRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForRestoreRequest_shouldBeOk() throws Exception { + setup(); - var handler = new RestoreHandler(ip, sessionId, investigationIds, - datasetIds, datafileIds); + var handler = new RestoreHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForWriteRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForWriteRequest_shouldBeOk() throws Exception { + setup(); - var handler = new WriteHandler(ip, sessionId, investigationIds, - datasetIds, datafileIds); + var handler = new WriteHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } diff --git a/src/test/java/org/icatproject/ids/ValidaterTest.java b/src/test/java/org/icatproject/ids/ValidaterTest.java index 87536004..6dda09ef 100644 --- a/src/test/java/org/icatproject/ids/ValidaterTest.java +++ b/src/test/java/org/icatproject/ids/ValidaterTest.java @@ -32,3 +32,4 @@ private void testValidUUID(boolean b, String id) { } } + diff --git a/src/test/java/org/icatproject/ids/integration/BaseTest.java b/src/test/java/org/icatproject/ids/integration/BaseTest.java index e33e0d46..4f0a23d4 100644 --- a/src/test/java/org/icatproject/ids/integration/BaseTest.java +++ b/src/test/java/org/icatproject/ids/integration/BaseTest.java @@ -60,15 +60,13 @@ public class BaseTest { public class DeleteVisitor extends SimpleFileVisitor { @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) - throws IOException { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override - public FileVisitResult postVisitDirectory(Path dir, IOException e) - throws IOException { + public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; @@ -82,21 +80,15 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) private static long timestamp = System.currentTimeMillis(); - protected Path getDirOnFastStorage(Long dsId) - throws IcatException_Exception { - Dataset icatDs = (Dataset) icatWS.get(sessionId, - "Dataset INCLUDE Investigation", dsId); - return setup.getStorageDir() - .resolve(Long.toString(icatDs.getInvestigation().getId())) + protected Path getDirOnFastStorage(Long dsId) throws IcatException_Exception { + Dataset icatDs = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Investigation", dsId); + return setup.getStorageDir().resolve(Long.toString(icatDs.getInvestigation().getId())) .resolve(Long.toString(icatDs.getId())); } - protected Path getFileOnArchiveStorage(Long dsId) - throws IcatException_Exception { - Dataset icatDs = (Dataset) icatWS.get(sessionId, - "Dataset INCLUDE Investigation", dsId); - return setup.getStorageArchiveDir() - .resolve(Long.toString(icatDs.getInvestigation().getId())) + protected Path getFileOnArchiveStorage(Long dsId) throws IcatException_Exception { + Dataset icatDs = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Investigation", dsId); + return setup.getStorageArchiveDir().resolve(Long.toString(icatDs.getInvestigation().getId())) .resolve(Long.toString(icatDs.getId())); } @@ -125,8 +117,7 @@ public void before() throws Exception { testingClient = new TestingClient(setup.getIdsUrl()); sessionId = setup.getRootSessionId(); waitForIds(); - populateStorage(setup.isTwoLevel(), setup.getStorageUnit(), - setup.getKey()); + populateStorage(setup.isTwoLevel(), setup.getStorageUnit(), setup.getKey()); } protected void checkAbsent(Path file) { @@ -167,8 +158,7 @@ protected void logTime(String msg) { long now = System.currentTimeMillis(); if (msg != null) { if (time != 0) { - System.out - .println(msg + " took " + (now - time) / 1000. + "s."); + System.out.println(msg + " took " + (now - time) / 1000. + "s."); } else { System.out.println(msg); } @@ -176,8 +166,7 @@ protected void logTime(String msg) { time = now; } - protected void checkZipFile(Path file, List ids, int compressedSize) - throws IOException { + protected void checkZipFile(Path file, List ids, int compressedSize) throws IOException { checkZipStream(Files.newInputStream(file), ids, compressedSize, 0); } @@ -216,9 +205,8 @@ protected byte[] getOutput(InputStream stream) throws IOException { } } - private void populateStorage(boolean twoLevel, String storageUnit, - String key) throws IOException, IcatException_Exception, - NoSuchAlgorithmException { + private void populateStorage(boolean twoLevel, String storageUnit, String key) + throws IOException, IcatException_Exception, NoSuchAlgorithmException { clearStorage(); long timestamp = System.currentTimeMillis(); @@ -245,8 +233,7 @@ private void populateStorage(boolean twoLevel, String storageUnit, supportedDatafileFormat.setFacility(fac); supportedDatafileFormat.setName("test_format"); supportedDatafileFormat.setVersion("42.0.0"); - supportedDatafileFormat - .setId(icatWS.create(sessionId, supportedDatafileFormat)); + supportedDatafileFormat.setId(icatWS.create(sessionId, supportedDatafileFormat)); InvestigationType invType = new InvestigationType(); invType.setName("Not null"); @@ -290,33 +277,25 @@ private void populateStorage(boolean twoLevel, String storageUnit, df1.setName("a/df1_" + timestamp); df1.setLocation(ds1Loc + UUID.randomUUID()); df1.setDataset(ds1); - writeToFile(df1, - "df1 test content very compressible very compressible", - key); + writeToFile(df1, "df1 test content very compressible very compressible", key); Datafile df2 = new Datafile(); df2.setName("df2_" + timestamp); df2.setLocation(ds1Loc + UUID.randomUUID()); df2.setDataset(ds1); - writeToFile(df2, - "df2 test content very compressible very compressible", - key); + writeToFile(df2, "df2 test content very compressible very compressible", key); Datafile df3 = new Datafile(); df3.setName("df3_" + timestamp); df3.setLocation(ds2Loc + UUID.randomUUID()); df3.setDataset(ds2); - writeToFile(df3, - "df3 test content very compressible very compressible", - key); + writeToFile(df3, "df3 test content very compressible very compressible", key); Datafile df4 = new Datafile(); df4.setName("df4_" + timestamp); df4.setLocation(ds2Loc + "Person's file"); df4.setDataset(ds2); - writeToFile(df4, - "df4 test content very compressible very compressible", - key); + writeToFile(df4, "df4 test content very compressible very compressible", key); datasetIds.add(ds1.getId()); datasetIds.add(ds2.getId()); @@ -332,16 +311,14 @@ private void populateStorage(boolean twoLevel, String storageUnit, moveDatasetToArchive(storageUnit, ds2, ds2Loc, fac, inv, key); } - newFileLocation = setup.getUpdownDir() - .resolve("new_file_" + timestamp); + newFileLocation = setup.getUpdownDir().resolve("new_file_" + timestamp); Files.createDirectories(newFileLocation.getParent()); byte[] bytes = "new_file test content".getBytes(); OutputStream out = Files.newOutputStream(newFileLocation); out.write(bytes); out.close(); } catch (IllegalAccessError e) { - System.err.println( - "Could not prepare ICAT db for testing: " + e.getMessage()); + System.err.println("Could not prepare ICAT db for testing: " + e.getMessage()); e.printStackTrace(); throw e; } @@ -354,8 +331,8 @@ private void populateStorage(boolean twoLevel, String storageUnit, protected Map ids = new HashMap<>(); protected Map paths = new HashMap<>(); - protected void checkZipStream(InputStream stream, List datafileIdsIn, - long compressedSize, int numLeft) throws IOException { + protected void checkZipStream(InputStream stream, List datafileIdsIn, long compressedSize, int numLeft) + throws IOException { ZipInputStream zis = new ZipInputStream(stream); ZipEntry ze = zis.getNextEntry(); List idsNeeded = new ArrayList<>(datafileIdsIn); @@ -384,8 +361,7 @@ protected void checkStream(InputStream stream, long id) throws IOException { boolean found = false; for (Entry e : ids.entrySet()) { if (id == e.getValue()) { - assertEquals(contents.get(e.getKey()), - new String(getOutput(stream))); + assertEquals(contents.get(e.getKey()), new String(getOutput(stream))); found = true; break; } @@ -394,8 +370,7 @@ protected void checkStream(InputStream stream, long id) throws IOException { } protected void writeToFile(Datafile df, String content, String key) - throws IOException, IcatException_Exception, - NoSuchAlgorithmException { + throws IOException, IcatException_Exception, NoSuchAlgorithmException { Path path = setup.getStorageDir().resolve(df.getLocation()); Files.createDirectories(path.getParent()); byte[] bytes = content.getBytes(); @@ -421,16 +396,16 @@ protected void writeToFile(Datafile df, String content, String key) Dataset ds = df.getDataset(); Investigation inv = ds.getInvestigation(); Facility fac = inv.getFacility(); - paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" - + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" - + ds.getName() + "/" + df.getName(), df.getLocation()); + paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + URLEncoder.encode(inv.getVisitId(), "UTF-8") + + "/" + ds.getName() + "/" + df.getName(), df.getLocation()); } - private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', - '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + private static final char[] HEX_CHARS = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' + }; - private String digest(Long id, String location, String key) - throws NoSuchAlgorithmException { + private String digest(Long id, String location, String key) throws NoSuchAlgorithmException { byte[] pattern = (id + location + key).getBytes(); MessageDigest digest = null; digest = MessageDigest.getInstance("SHA-256"); @@ -445,19 +420,16 @@ private String digest(Long id, String location, String key) return new String(hexChars); } - private void moveDatasetToArchive(String storageUnit, Dataset ds, - String dsLoc, Facility fac, Investigation inv, String key) - throws IOException, IcatException_Exception { - ds = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Datafile", - ds.getId()); + private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, Facility fac, Investigation inv, + String key) throws IOException, IcatException_Exception { + ds = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Datafile", ds.getId()); Path top = setup.getStorageDir(); if (storageUnit.equals("DATASET")) { Path zipFile = setup.getStorageArchiveDir().resolve(dsLoc); Files.createDirectories(zipFile.getParent()); - ZipOutputStream zos = new ZipOutputStream( - Files.newOutputStream(zipFile)); + ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile)); zos.setLevel(0); for (Datafile df : ds.getDatafiles()) { @@ -465,15 +437,12 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, if (key == null) { file = top.resolve(df.getLocation()); } else { - file = top.resolve(getLocationFromDigest(df.getId(), - df.getLocation())); + file = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); } InputStream fis = Files.newInputStream(file); - zos.putNextEntry(new ZipEntry( - "ids/" + fac.getName() + "/" + inv.getName() + "/" - + URLEncoder.encode(inv.getVisitId(), "UTF-8") - + "/" + ds.getName() + "/" + df.getName())); + zos.putNextEntry(new ZipEntry("ids/" + fac.getName() + "/" + inv.getName() + "/" + + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" + ds.getName() + "/" + df.getName())); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { @@ -492,11 +461,9 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, if (key == null) { p = top.resolve(df.getLocation()); } else { - p = top.resolve(getLocationFromDigest(df.getId(), - df.getLocation())); + p = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); } - Files.move(p, archive.resolve(p.getFileName()), - StandardCopyOption.REPLACE_EXISTING); + Files.move(p, archive.resolve(p.getFileName()), StandardCopyOption.REPLACE_EXISTING); } } for (Datafile df : ds.getDatafiles()) { @@ -504,8 +471,7 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, if (key == null) { file = top.resolve(df.getLocation()); } else { - file = top.resolve( - getLocationFromDigest(df.getId(), df.getLocation())); + file = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); } Files.deleteIfExists(file); Path parent = file.getParent(); @@ -542,10 +508,8 @@ protected void raceTest() throws Exception { } logTime("File created"); - Investigation inv = (Investigation) icatWS - .search(sessionId, "Investigation").get(0); - DatasetType dst = (DatasetType) icatWS.search(sessionId, "DatasetType") - .get(0); + Investigation inv = (Investigation) icatWS.search(sessionId, "Investigation").get(0); + DatasetType dst = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); Dataset ds = new Dataset(); ds.setName("Big ds"); @@ -555,21 +519,17 @@ protected void raceTest() throws Exception { ds.setId(dsid); for (int i = 0; i < 30; i++) { - testingClient.put(sessionId, Files.newInputStream(path), - "uploaded_file" + i, dsid, supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + testingClient.put(sessionId, Files.newInputStream(path), "uploaded_file" + i, dsid, + supportedDatafileFormat.getId(), "A rather splendid datafile", 201); } - testingClient.archive(sessionId, new DataSelection().addDataset(dsid), - 204); + testingClient.archive(sessionId, new DataSelection().addDataset(dsid), 204); logTime("Put and archive calls"); waitForIds(300); logTime("Archive complete"); - testingClient.restore(sessionId, new DataSelection().addDataset(dsid), - 204); - while (testingClient.getStatus(sessionId, - new DataSelection().addDataset(dsid), 200) != Status.ONLINE) { + testingClient.restore(sessionId, new DataSelection().addDataset(dsid), 204); + while (testingClient.getStatus(sessionId, new DataSelection().addDataset(dsid), 200) != Status.ONLINE) { Thread.sleep(1000); } logTime("Marked online"); @@ -578,8 +538,7 @@ protected void raceTest() throws Exception { assertTrue(stat.getOpItems().toString(), stat.getOpItems().isEmpty()); - testingClient.delete(sessionId, new DataSelection().addDataset(dsid), - 204); + testingClient.delete(sessionId, new DataSelection().addDataset(dsid), 204); icatWS.delete(sessionId, ds); waitForIds(300); @@ -592,20 +551,16 @@ protected void raceTest() throws Exception { public void getDatafileIdsTest() throws Exception { List ids = testingClient.getDatafileIds(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), - 200); + new DataSelection().addDataset(datasetIds.get(0)).addDatafile(datafileIds.get(0)), 200); assertEquals(2, ids.size()); assertTrue(ids.contains(datafileIds.get(0))); assertTrue(ids.contains(datafileIds.get(1))); - ids = testingClient.getDatafileIds(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), 200); + ids = testingClient.getDatafileIds(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 200); assertEquals(1, ids.size()); assertTrue(ids.contains(datafileIds.get(0))); - ids = testingClient.getDatafileIds(sessionId, - new DataSelection().addInvestigation(investigationId), 200); + ids = testingClient.getDatafileIds(sessionId, new DataSelection().addInvestigation(investigationId), 200); assertEquals(4, ids.size()); for (Long id : datafileIds) { assertTrue(ids.contains(id)); @@ -616,31 +571,24 @@ public void getDatafileIdsTest() throws Exception { public void bigDataSelectionTest() throws Exception { String icatUrl = testingClient.getIcatUrl(200).toExternalForm(); ICAT restIcat = new org.icatproject.icat.client.ICAT(icatUrl); - try (JsonReader parser = Json.createReader(new ByteArrayInputStream( - restIcat.getProperties().getBytes()))) { - assertEquals( - "maxEntities must have a fixed value in the icat.server for test to be useful", - 20, parser.readObject().getInt("maxEntities")); + try (JsonReader parser = Json.createReader(new ByteArrayInputStream(restIcat.getProperties().getBytes()))) { + assertEquals("maxEntities must have a fixed value in the icat.server for test to be useful", 20, + parser.readObject().getInt("maxEntities")); } - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); List idList = new ArrayList<>(); for (int i = 0; i < 45; i++) { - Long dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp + i, datasetIds.get(0), - supportedDatafileFormat.getId(), + Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp + i, datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", 201); idList.add(dfid); } waitForIds(); List idList2 = testingClient.getDatafileIds(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), - 200); + new DataSelection().addDataset(datasetIds.get(0)).addDatafile(datafileIds.get(0)), 200); assertEquals(47, idList2.size()); for (Long id : idList) { assertTrue(idList2.contains(id)); @@ -657,96 +605,69 @@ public void bigDataSelectionTest() throws Exception { assertTrue(idList2.contains(idList.get(i))); } - idList2 = testingClient.getDatafileIds(sessionId, - new DataSelection().addInvestigation(investigationId), 200); + idList2 = testingClient.getDatafileIds(sessionId, new DataSelection().addInvestigation(investigationId), 200); assertEquals(49, idList2.size()); } public void cloningTest() throws Exception { - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); - assertEquals(104, testingClient.getSize(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 200)); + assertEquals(104, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 200)); Session s = icat.getSession(sessionId); Map m = new HashMap<>(); m.put("name", "newOne"); long dfid = datafileIds.get(0); long ndfid = s.cloneEntity("Datafile", dfid, m); - Datafile df = (Datafile) icatWS.get(sessionId, - "Datafile df INCLUDE df.dataset.investigation.facility", ndfid); + Datafile df = (Datafile) icatWS.get(sessionId, "Datafile df INCLUDE df.dataset.investigation.facility", ndfid); Dataset ds = df.getDataset(); Investigation inv = ds.getInvestigation(); Facility fac = inv.getFacility(); - paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" - + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" - + ds.getName() + "/" + df.getName(), df.getLocation()); + paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + URLEncoder.encode(inv.getVisitId(), "UTF-8") + + "/" + ds.getName() + "/" + df.getName(), df.getLocation()); crcs.put(df.getLocation(), df.getChecksum()); fsizes.put(df.getLocation(), df.getFileSize()); - contents.put(df.getLocation(), - "df1 test content very compressible very compressible"); + contents.put(df.getLocation(), "df1 test content very compressible very compressible"); ids.put(df.getLocation(), df.getId()); - assertEquals(156, testingClient.getSize(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 200)); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, - 200)) { + assertEquals(156, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 200)); + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, 200)) { List dfids = new ArrayList<>(datafileIds.subList(0, 2)); dfids.add(ndfid); checkZipStream(stream, dfids, 57, 0); } long dsid = datasetIds.get(0); assertEquals(2, getDirOnFastStorage(dsid).toFile().list().length); - assertEquals("[3]", s.search( - "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " - + dsid)); - testingClient.delete(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), 204); - assertEquals(2, - getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); - assertEquals("[2]", s.search( - "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " - + dsid)); - testingClient.delete(sessionId, - new DataSelection().addDatafile(datafileIds.get(1)), 204); - assertEquals(1, - getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); - assertEquals("[1]", s.search( - "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " - + dsid)); - testingClient.delete(sessionId, new DataSelection().addDatafile(ndfid), - 204); + assertEquals("[3]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + assertEquals(2, getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); + assertEquals("[2]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(1)), 204); + assertEquals(1, getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); + assertEquals("[1]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); + testingClient.delete(sessionId, new DataSelection().addDatafile(ndfid), 204); assertFalse(getDirOnFastStorage(datasetIds.get(0)).toFile().exists()); - assertEquals("[0]", s.search( - "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " - + dsid)); + assertEquals("[0]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); } public void reliabilityTest() throws Exception { - testingClient.restore(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), - 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); waitForIds(); - Long dfid1 = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + Long dfid1 = testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file2_" + timestamp, + datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", 201); - testingClient.archive(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), - 204); + testingClient.archive(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures().isEmpty()); setup.setReliability(0.); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(dfid1), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(dfid1), Flag.NONE, + 200); try { while (!testingClient.isPrepared(preparedId, null)) { @@ -755,49 +676,38 @@ public void reliabilityTest() throws Exception { } catch (Exception e) { System.out.println(e); assertEquals("Restore failed", e.getMessage()); - Set failures = testingClient.getServiceStatus(sessionId, 200) - .getFailures(); + Set failures = testingClient.getServiceStatus(sessionId, 200).getFailures(); assertEquals(1, failures.size()); setup.setReliability(1.); testingClient.reset(preparedId, 204); - assertTrue(testingClient.getServiceStatus(sessionId, 200) - .getFailures().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures().isEmpty()); while (!testingClient.isPrepared(preparedId, null)) { Thread.sleep(1000); } } setup.setReliability(0.); - Long dfid2 = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "An even better datafile", - "7.1.3", new Date(420000), new Date(42000), 201); + Long dfid2 = testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file3_" + timestamp, + datasetIds.get(0), supportedDatafileFormat.getId(), "An even better datafile", "7.1.3", + new Date(420000), new Date(42000), 201); waitForIds(); System.out.println(testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(dfid2), - 200)); + new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 200)); setup.setReliability(1.); - testingClient.restore(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDatafile(dfid2), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 204); waitForIds(); System.out.println(testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(dfid2), - 200)); + new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 200)); } protected void reliabilityTest2() throws Exception { - DataSelection dsel = new DataSelection() - .addDatafile(datafileIds.get(0)); + DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(0)); testingClient.archive(sessionId, dsel, 204); waitForIds(); setup.setReliability(0.); - String preparedId = testingClient.prepareData(sessionId, dsel, - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, dsel, Flag.NONE, 200); try { while (!testingClient.isPrepared(preparedId, null)) { @@ -822,8 +732,7 @@ protected void reliabilityTest2() throws Exception { } protected void reliabilityTest3() throws Exception { - DataSelection dsel = new DataSelection() - .addDatafile(datafileIds.get(0)); + DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(0)); testingClient.archive(sessionId, dsel, 204); waitForIds(); @@ -831,8 +740,7 @@ protected void reliabilityTest3() throws Exception { testingClient.restore(sessionId, dsel, 204); try { - while (testingClient.getStatus(sessionId, dsel, - null) != Status.ONLINE) { + while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { Thread.sleep(1000); } fail("Should throw an error"); @@ -843,8 +751,7 @@ protected void reliabilityTest3() throws Exception { setup.setReliability(1.); testingClient.restore(sessionId, dsel, 204); try { - while (testingClient.getStatus(sessionId, dsel, - null) != Status.ONLINE) { + while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { Thread.sleep(1000); } } catch (Exception e) { @@ -853,8 +760,7 @@ protected void reliabilityTest3() throws Exception { testingClient.reset(sessionId, dsel, 204); testingClient.restore(sessionId, dsel, 204); - while (testingClient.getStatus(sessionId, dsel, - null) != Status.ONLINE) { + while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { Thread.sleep(1000); } } @@ -864,11 +770,9 @@ protected void isPreparedTest() throws Exception { int numDs = 30; int numDf = 19; - Investigation inv = (Investigation) icatWS - .search(sessionId, "Investigation INCLUDE Facility").get(0); + Investigation inv = (Investigation) icatWS.search(sessionId, "Investigation INCLUDE Facility").get(0); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS - .search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); Facility fac = inv.getFacility(); String key = setup.getKey(); @@ -907,8 +811,7 @@ protected void isPreparedTest() throws Exception { logTime("Put calls"); - String preparedId = testingClient.prepareData(sessionId, dsel, Flag.ZIP, - 200); + String preparedId = testingClient.prepareData(sessionId, dsel, Flag.ZIP, 200); logTime("Data prepared"); while (!testingClient.isPrepared(preparedId, 200)) { @@ -926,8 +829,7 @@ protected void isPreparedTest() throws Exception { logTime("Read " + l + " bytes"); } - testingClient.archive(sessionId, new DataSelection().addDataset(dsid), - 204); + testingClient.archive(sessionId, new DataSelection().addDataset(dsid), 204); waitForIds(300); logTime(null); diff --git a/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java index cad75570..d6cc867a 100644 --- a/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java @@ -38,24 +38,21 @@ public static void setup() throws Exception { public void createBogusFiles() throws Exception { long timestamp = System.currentTimeMillis(); - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -70,11 +67,9 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -83,8 +78,7 @@ public void getEmptyDataset() throws Exception { @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); } @@ -92,11 +86,9 @@ public void getSizeEmptyDataset() throws Exception { @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } @@ -105,8 +97,7 @@ public void getNonEmptyDataset() throws Exception { @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); } @@ -114,11 +105,9 @@ public void getSizeNonEmptyDataset() throws Exception { @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, 404)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -127,8 +116,7 @@ public void getBogusFile() throws Exception { @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); } diff --git a/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java index 80609a72..5f5b4881 100644 --- a/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java @@ -26,12 +26,9 @@ public static void setup() throws Exception { @Test public void deleteDatafileTest() throws Exception { - DataSelection dsel = new DataSelection() - .addDatafile(datafileIds.get(3)); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, dsel, 200)); - testingClient.delete(sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), 204); + DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(3)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); try { testingClient.getStatus(sessionId, dsel, 404); fail(); @@ -47,12 +44,10 @@ public void deleteDatafileTest() throws Exception { public void deleteDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); DataSelection dsel = new DataSelection().addDataset(datasetIds.get(1)); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, dsel, 200)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); assertTrue(Files.exists(dirOnFastStorage)); testingClient.delete(sessionId, dsel, 204); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, dsel, 200)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); assertFalse(Files.exists(dirOnFastStorage)); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java index 0d1834ea..16026ef5 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java @@ -29,13 +29,11 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", - datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, - new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -49,15 +47,12 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", - datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection() - .addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, - Flag.NONE, 200); + DataSelection selection = new DataSelection().addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -69,74 +64,59 @@ public void getSizePreparedId() throws Exception { @Test public void getSizes1() throws Exception { - assertEquals(208L, testingClient.getSize(sessionId, - new DataSelection().addDatafiles(datafileIds), 200)); - assertEquals(208L, testingClient.getSize(sessionId, - new DataSelection().addDatasets(datasetIds), 200)); - assertEquals(208L, testingClient.getSize(sessionId, - new DataSelection().addInvestigation(investigationId), 200)); + assertEquals(208L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(208L, testingClient.getSize(sessionId, new DataSelection().addDatasets(datasetIds), 200)); assertEquals(208L, - testingClient.getSize(sessionId, - new DataSelection().addInvestigation(investigationId) - .addDatafiles(datafileIds), - 200)); - assertEquals(0L, testingClient.getSize(sessionId, - new DataSelection().addDataset(datasetIds.get(2)), 200)); + testingClient.getSize(sessionId, new DataSelection().addInvestigation(investigationId), 200)); + assertEquals(208L, testingClient.getSize(sessionId, + new DataSelection().addInvestigation(investigationId).addDatafiles(datafileIds), 200)); + assertEquals(0L, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(2)), 200)); } @Test(expected = NotFoundException.class) public void getSizesBad() throws Exception { - testingClient.getSize(sessionId, new DataSelection().addDatafile(563L), - 404); + testingClient.getSize(sessionId, new DataSelection().addDatafile(563L), 404); } @Test(expected = NotFoundException.class) public void getSizesBad2() throws Exception { - testingClient.getSize(sessionId, - new DataSelection().addDatafile(563L).addDatafile(564L), 404); + testingClient.getSize(sessionId, new DataSelection().addDatafile(563L).addDatafile(564L), 404); } @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0, - 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0, 400)) { } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - 403)) { + try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { } } @Test public void correctBehaviourTestNone() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 0, 200)) { checkStream(stream, datafileIds.get(0)); } } @Test public void correctBehaviourTestCompress() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.COMPRESS, 0, - 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.COMPRESS, 0, 200)) { checkZipStream(stream, datafileIds, 36L, 0); } - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), Flag.COMPRESS, 0, 200)) { checkStream(stream, datafileIds.get(0)); } @@ -144,29 +124,25 @@ public void correctBehaviourTestCompress() throws Exception { @Test public void correctBehaviourTestZip() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.ZIP, 0, - 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.ZIP, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP, - 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } @Test public void correctBehaviourTestZipAndCompress() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.ZIP_AND_COMPRESS, 0, 200)) { checkZipStream(stream, datafileIds, 36L, 0); } - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP_AND_COMPRESS, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 36L, 0); } @@ -175,31 +151,25 @@ public void correctBehaviourTestZipAndCompress() throws Exception { @Test public void correctBehaviourInvestigation() throws Exception { try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addInvestigation(investigationId), - Flag.NONE, 0, 200)) { + new DataSelection().addInvestigation(investigationId), Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP, - 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } @Test public void correctBehaviourInvestigations() throws Exception { - try (InputStream stream = testingClient - .getData(sessionId, - new DataSelection().addInvestigations( - Arrays.asList(investigationId)), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addInvestigations(Arrays.asList(investigationId)), Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP, - 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java index 1b27b3e3..9d844536 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java @@ -25,17 +25,15 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, - 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); do { Thread.sleep(500); @@ -47,8 +45,7 @@ public void badOffsetFormatTest() throws Exception { @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient - .getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { } } @@ -58,15 +55,13 @@ public void correctBehaviourNoOffsetTest() throws Exception { for (Flag flag : Flag.values()) { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), flag, - 200); + new DataSelection().addDatafile(datafileIds.get(0)), flag, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - try (InputStream stream = testingClient.getData(preparedId, 0, - 200)) { + try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { if (flag == Flag.NONE || flag == Flag.COMPRESS) { checkStream(stream, datafileIds.get(0)); } else if (flag == Flag.ZIP) { @@ -79,10 +74,9 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() - throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -96,9 +90,8 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -109,13 +102,10 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() - throws Exception { + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -132,8 +122,7 @@ public void correctBehaviourWithOffsetTest() throws Exception { for (Flag flag : Flag.values()) { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), flag, - 200); + new DataSelection().addDatafile(datafileIds.get(0)), flag, 200); do { Thread.sleep(1000); @@ -141,16 +130,13 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the file twice, with and without an offset byte[] out = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] outOffset = getOutput( - testingClient.getData(preparedId, goodOffset, 206)); + byte[] outOffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the // offset - System.out - .println(flag + ": " + out.length + " " + outOffset.length); + System.out.println(flag + ": " + out.length + " " + outOffset.length); for (int i = 0; i < outOffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) outOffset[i], - (byte) out[i + goodOffset]); + assertEquals("Byte offset: " + i, (byte) outOffset[i], (byte) out[i + goodOffset]); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java index 272f1a1a..d5db81b7 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java @@ -40,20 +40,17 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, new DataSelection() - .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(sessionId, new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTestAnon() throws Exception { - testingClient.getStatus(null, new DataSelection() - .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(null, new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.getStatus(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), 403); + testingClient.getStatus(setup.getForbiddenSessionId(), new DataSelection().addDatafiles(datafileIds), 403); } @Test @@ -62,8 +59,7 @@ public void correctBehaviourTest() throws Exception { Status status; do { Thread.sleep(1000); - status = testingClient.getStatus(sessionId, - new DataSelection().addDatafiles(datafileIds), 200); + status = testingClient.getStatus(sessionId, new DataSelection().addDatafiles(datafileIds), 200); System.out.println("*" + status + "*"); } while (status != Status.ONLINE); @@ -71,10 +67,8 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, - Flag.NONE, 200); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java index b0c72fb6..b870eba0 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java @@ -33,20 +33,22 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafiles( - Arrays.asList(1L, 2L, 3L, 99999L)), Flag.NONE, 404); + testingClient + .prepareData(sessionId, + new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), + Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, - new DataSelection().addDatafile(99999L), Flag.NONE, 404); + testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, + 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, - new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient + .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) diff --git a/src/test/java/org/icatproject/ids/integration/one/MiscTest.java b/src/test/java/org/icatproject/ids/integration/one/MiscTest.java index d5006b9b..3baf012c 100644 --- a/src/test/java/org/icatproject/ids/integration/one/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/MiscTest.java @@ -41,20 +41,17 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); @@ -63,15 +60,13 @@ public void correctBehaviourNoOffsetTest() throws Exception { @Test(expected = NotImplementedException.class) public void restoreNotAvailableTest() throws Exception { - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); waitForIds(); } @Test(expected = NotImplementedException.class) public void archiveNotAvailableTest() throws Exception { - testingClient.archive(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); waitForIds(); } diff --git a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java index cf1a064b..1943ae01 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java @@ -29,8 +29,7 @@ public static void beforeClass() throws Exception { int n = 100000; start = System.currentTimeMillis(); - try (OutputStream f = Files.newOutputStream(infile, - StandardOpenOption.CREATE)) { + try (OutputStream f = Files.newOutputStream(infile, StandardOpenOption.CREATE)) { for (int i = 0; i < n; i++) { f.write(junk); } @@ -51,13 +50,12 @@ public static void afterClass() throws Exception { public void putOneFileTest() throws Exception { byte[] junk = new byte[1000]; start = System.currentTimeMillis(); - Long dfid = testingClient.put(sessionId, Files.newInputStream(infile), - "big_" + timestamp, datasetIds.get(0), + Long dfid = testingClient.put(sessionId, Files.newInputStream(infile), "big_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), "A big datafile", 201); ts("store file (put)"); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(dfid), Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(dfid), Flag.NONE, 0, + null)) { boolean first = true; while (stream.read(junk) > 0) { if (first) { @@ -79,15 +77,14 @@ private static void ts(String msg) { public void putAsPostOneFileTest() throws Exception { byte[] junk = new byte[1000]; start = System.currentTimeMillis(); - Long dfid = testingClient.putAsPost(sessionId, - Files.newInputStream(infile), "big2_" + timestamp, - datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", null, null, null, true, 201); + Long dfid = testingClient.putAsPost(sessionId, Files.newInputStream(infile), "big2_" + timestamp, + datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", null, null, null, + true, 201); ts("store file (post)"); int ntot = 0; - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(dfid), Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(dfid), Flag.NONE, 0, + null)) { boolean first = true; int n; diff --git a/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java index c7129b25..acdf7be9 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java @@ -26,30 +26,27 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 400); } @Test(expected = BadRequestException.class) public void noIdsTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection(), - Flag.NONE, 400); + testingClient.prepareData("bad sessionId format", new DataSelection(), Flag.NONE, 400); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 403); + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); System.out.println(preparedId); assertNotNull(preparedId); } @@ -57,9 +54,8 @@ public void correctBehaviourTest() throws Exception { @Test public void prepareDataset() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -70,17 +66,14 @@ public void prepareDataset() throws Exception { Thread.sleep(1000); } - assertTrue( - Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareTwoDatasets() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -92,16 +85,14 @@ public void prepareTwoDatasets() throws Exception { Thread.sleep(1000); } - assertTrue( - Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareDatafile() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -111,17 +102,14 @@ public void prepareDatafile() throws Exception { Thread.sleep(1000); } - assertTrue( - Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareDatafileAndItsDataset() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -132,8 +120,7 @@ public void prepareDatafileAndItsDataset() throws Exception { Thread.sleep(1000); } - assertTrue( - Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/PutTest.java b/src/test/java/org/icatproject/ids/integration/one/PutTest.java index 4025eda7..0a6dec0a 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PutTest.java @@ -38,11 +38,9 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - Long dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); Datafile df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("A rather splendid datafile", df.getDescription()); @@ -51,11 +49,9 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileModTime()); assertTrue(Files.exists(dirOnFastStorage)); - dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "An even better datafile", - "7.1.3", new Date(420000), new Date(42000), 201); + dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -76,11 +72,9 @@ public void putAsPostOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - Long dfid = testingClient.putAsPost(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "A rather splendid datafile", - null, null, null, true, 201); + Long dfid = testingClient.putAsPost(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", null, null, null, true, 201); Datafile df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("A rather splendid datafile", df.getDescription()); @@ -89,11 +83,9 @@ public void putAsPostOneFileTest() throws Exception { assertNull(df.getDatafileModTime()); assertTrue(Files.exists(dirOnFastStorage)); - dfid = testingClient.putAsPost(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "An even better datafile", - "7.1.3", new Date(420000), new Date(42000), false, 201); + dfid = testingClient.putAsPost(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "An even better datafile", "7.1.3", new Date(420000), new Date(42000), false, 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); diff --git a/src/test/java/org/icatproject/ids/integration/one/WriteTest.java b/src/test/java/org/icatproject/ids/integration/one/WriteTest.java index 58891e59..9a7550a3 100644 --- a/src/test/java/org/icatproject/ids/integration/one/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/WriteTest.java @@ -18,8 +18,7 @@ public static void setup() throws Exception { @Test(expected = NotImplementedException.class) public void writeNotAvailableTest() throws Exception { - testingClient.write(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.write(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); } } diff --git a/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java b/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java index 5145ecec..d5bf557e 100644 --- a/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java @@ -30,15 +30,13 @@ public void restoreThenArchiveDataset() throws Exception { assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); diff --git a/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java index f522a1f8..3ff9adf1 100644 --- a/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java @@ -47,24 +47,21 @@ public static void setup() throws Exception { @Before public void createBogusFiles() throws Exception { - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -80,11 +77,9 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -93,8 +88,7 @@ public void getEmptyDataset() throws Exception { @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); } @@ -102,17 +96,14 @@ public void getSizeEmptyDataset() throws Exception { @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); testingClient.restore(sessionId, selection, 204); waitForIds(); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, selection, null)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, null)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } @@ -121,8 +112,7 @@ public void getNonEmptyDataset() throws Exception { @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); } @@ -130,17 +120,14 @@ public void getSizeNonEmptyDataset() throws Exception { @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.restore(sessionId, selection, 404); waitForIds(); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, selection, 404)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, 404)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, 404)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -149,17 +136,15 @@ public void getBogusFile() throws Exception { @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); } /* - * Try the full cycle: upload a new file into a dataset having a bogus file, - * which triggers a write of the dataset to archive storage, archive the - * dataset, and restore it. Each step must deal gracefully with the bogus - * file in the dataset. + * Try the full cycle: upload a new file into a dataset having a bogus file, which triggers a write of the + * dataset to archive storage, archive the dataset, and restore it. Each step must deal gracefully with + * the bogus file in the dataset. */ @Test public void putWriteArchiveRestore() throws Exception { @@ -172,11 +157,9 @@ public void putWriteArchiveRestore() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfulId = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + Long dfulId = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); Datafile dful = (Datafile) icatWS.get(sessionId, "Datafile", dfulId); testingClient.archive(sessionId, selection, 204); waitForIds(); @@ -189,16 +172,13 @@ public void putWriteArchiveRestore() throws Exception { File[] filesList = dirOnFastStorage.toFile().listFiles(); assertEquals(3, filesList.length); Set locations = new HashSet<>(); - Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", - datafileIds.get(0)); + Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(0)); locations.add(getLocationFromDigest(df1.getId(), df1.getLocation())); - Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", - datafileIds.get(1)); + Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(1)); locations.add(getLocationFromDigest(df2.getId(), df2.getLocation())); locations.add(getLocationFromDigest(dful.getId(), dful.getLocation())); for (File file : filesList) { - String location = setup.getStorageDir().relativize(file.toPath()) - .toString(); + String location = setup.getStorageDir().relativize(file.toPath()).toString(); assertTrue(locations.contains(location)); } diff --git a/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java index da6b435f..d8179493 100644 --- a/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java @@ -20,8 +20,7 @@ public static void setup() throws Exception { @Test(expected = DataNotOnlineException.class) public void deleteFromUnrestoredDataset() throws Exception { - testingClient.delete(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 503); + testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 503); } @Test @@ -30,13 +29,11 @@ public void deleteDatafileFromRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); - testingClient.delete(sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), 204); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); waitForIds(); checkZipFile(fileOnArchiveStorage, datafileIds.subList(2, 3), 36); @@ -47,14 +44,12 @@ public void deleteRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); checkPresent(fileOnArchiveStorage); - testingClient.delete(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkAbsent(dirOnFastStorage); checkAbsent(fileOnArchiveStorage); diff --git a/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java index 866f5bf7..1a3970a0 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java @@ -31,17 +31,14 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, - 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - 403)) { + try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { } } @@ -50,13 +47,11 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", - datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, - new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -70,15 +65,12 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", - datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection() - .addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, - Flag.NONE, 200); + DataSelection selection = new DataSelection().addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -91,18 +83,16 @@ public void getSizePreparedId() throws Exception { @Test public void correctBehaviourTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - 503)) { + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.NONE, + 0, 503)) { fail("Should have thrown exception"); } catch (IdsException e) { assertEquals(DataNotOnlineException.class, e.getClass()); } while (true) { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - null)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds, 57, 0); break; } catch (IdsException e) { @@ -115,18 +105,16 @@ public void correctBehaviourTest() throws Exception { @Test public void gettingDatafileRestoresItsDatasetTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(2)), Flag.NONE, - 0, null)) { + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)), + Flag.NONE, 0, null)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), Flag.NONE, - 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), + Flag.NONE, 0, 200)) { checkStream(stream, datafileIds.get(3)); } @@ -135,9 +123,8 @@ public void gettingDatafileRestoresItsDatasetTest() throws Exception { @Test public void gettingDatasetUsesCacheTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, - null)) { + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, null)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -145,32 +132,26 @@ public void gettingDatasetUsesCacheTest() throws Exception { waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, - 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } } @Test - public void gettingDatafileAndDatasetShouldRestoreBothDatasetsTest() - throws Exception { - - try (InputStream z = testingClient - .getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(2)) - .addDataset(datasetIds.get(0)), - Flag.NONE, 0, 503)) { + public void gettingDatafileAndDatasetShouldRestoreBothDatasetsTest() throws Exception { + + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)) + .addDataset(datasetIds.get(0)), Flag.NONE, 0, 503)) { fail("Should throw exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatasets(datasetIds), Flag.NONE, 0, - 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatasets(datasetIds), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57, 0); } } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java index c657ef0e..97f66d5a 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java @@ -25,17 +25,15 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, - 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -47,16 +45,14 @@ public void badOffsetFormatTest() throws Exception { @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient - .getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { } } @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -68,10 +64,9 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() - throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -86,9 +81,8 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -100,13 +94,10 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() - throws Exception { + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), - Flag.NONE, 200); + new DataSelection().addDataset(datasetIds.get(0)).addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -120,9 +111,8 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() @Test public void correctBehaviourWithOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -130,14 +120,12 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the zip file twice, with and without an offset byte[] zip = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] zipoffset = getOutput( - testingClient.getData(preparedId, goodOffset, 206)); + byte[] zipoffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the offset System.out.println(zip.length + " " + zipoffset.length); for (int i = 0; i < zipoffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) zipoffset[i], - (byte) zip[i + goodOffset]); + assertEquals("Byte offset: " + i, (byte) zipoffset[i], (byte) zip[i + goodOffset]); } } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java index d9a7a651..8ecd4429 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java @@ -34,8 +34,8 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, new DataSelection() - .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(sessionId, + new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) @@ -55,10 +55,8 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, - Flag.NONE, 200); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } @@ -66,8 +64,7 @@ public void getStatusPreparedIdTest() throws Exception { testingClient.archive(sessionId, selection, 204); waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); - // verify that getStatus() as opposed to isPrepared() does not - // implicitly trigger a restore. + // verify that getStatus() as opposed to isPrepared() does not implicitly trigger a restore. waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); } @@ -75,8 +72,7 @@ public void getStatusPreparedIdTest() throws Exception { @Test public void restoringDatafileRestoresItsDatasetTest() throws Exception { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java index e6fa086c..c7daebea 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java @@ -33,20 +33,22 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafiles( - Arrays.asList(1L, 2L, 3L, 99999L)), Flag.NONE, 404); + testingClient + .prepareData(sessionId, + new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), + Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, - new DataSelection().addDatafile(99999L), Flag.NONE, 404); + testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, + 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, - new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient + .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) diff --git a/src/test/java/org/icatproject/ids/integration/two/MiscTest.java b/src/test/java/org/icatproject/ids/integration/two/MiscTest.java index 2eff91cd..fa5751d8 100644 --- a/src/test/java/org/icatproject/ids/integration/two/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/MiscTest.java @@ -40,23 +40,19 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); - assertFalse(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertFalse(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); diff --git a/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java index 2f9cb527..891da989 100644 --- a/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java @@ -28,9 +28,8 @@ public static void setup() throws Exception { public void prepareArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -50,10 +49,8 @@ public void prepareTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -73,9 +70,8 @@ public void prepareTwoArchivedDatasets() throws Exception { public void prepareArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -92,11 +88,8 @@ public void prepareArchivedDatafile() throws Exception { public void prepareArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient - .prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -112,29 +105,26 @@ public void prepareArchivedDatafileAndItsDataset() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 400); } @Test public void noIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, - 200); + testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, 200); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 403); + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); assertNotNull(preparedId); } @@ -142,16 +132,14 @@ public void correctBehaviourTest() throws Exception { public void prepareRestoredDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -167,19 +155,16 @@ public void prepareTwoRestoredDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), - 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage1); checkPresent(dirOnFastStorage2); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -194,16 +179,14 @@ public void prepareRestoredDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -218,16 +201,13 @@ public void prepareRestoredDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection() - .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), - 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/two/PutTest.java b/src/test/java/org/icatproject/ids/integration/two/PutTest.java index a512a1e7..3ca3f8dd 100644 --- a/src/test/java/org/icatproject/ids/integration/two/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/PutTest.java @@ -32,9 +32,8 @@ public static void setup() throws Exception { @Test(expected = DataNotOnlineException.class) public void putToUnrestoredDataset() throws Exception { - testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file1_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), null, 503); + testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file1_" + + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), null, 503); } @Test @@ -42,18 +41,15 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); waitForIds(); @@ -63,11 +59,9 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileCreateTime()); assertNull(df.getDatafileModTime()); - dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "An even better datafile", - "7.1.3", new Date(420000), new Date(42000), 201); + dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -85,8 +79,7 @@ public void putOneFileTest() throws Exception { assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java b/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java index 1d7443f9..5122232e 100644 --- a/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java @@ -32,9 +32,9 @@ public static void setup() throws Exception { } /* - * Note that we cannot test for DUPLICATE_ENTRY here, because - * ZipOutputStream() won't allow us to create such a defective ZIP file. But - * that doesn't mean that this error cannot occur. + * Note that we cannot test for DUPLICATE_ENTRY here, because ZipOutputStream() won't allow + * us to create such a defective ZIP file. But that doesn't mean that this error cannot + * occur. */ private enum Defect { NONE, MISSING_ENTRY, SPURIOUS_ENTRY, DUPLICATE_ENTRY @@ -43,10 +43,8 @@ private enum Defect { private void cloneZip(Path archivepath, Defect defect) throws IOException { Path savepath = archivepath.getParent().resolve(".sav"); Files.move(archivepath, savepath); - try (ZipOutputStream zipout = new ZipOutputStream( - Files.newOutputStream(archivepath))) { - try (ZipInputStream zipin = new ZipInputStream( - Files.newInputStream(savepath))) { + try (ZipOutputStream zipout = new ZipOutputStream(Files.newOutputStream(archivepath))) { + try (ZipInputStream zipin = new ZipInputStream(Files.newInputStream(savepath))) { ZipEntry entry = zipin.getNextEntry(); boolean first = true; String entryName = ""; diff --git a/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java b/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java index 0efa59fb..2755fc9e 100644 --- a/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java @@ -22,8 +22,7 @@ public void restoreArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); @@ -35,9 +34,8 @@ public void restoreArchivedDataset() throws Exception { public void restoreTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), - 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage1); @@ -49,8 +47,7 @@ public void restoreArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); @@ -62,9 +59,8 @@ public void restoreArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection() - .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), - 204); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/two/WriteTest.java b/src/test/java/org/icatproject/ids/integration/two/WriteTest.java index 7052c1df..c9367a43 100644 --- a/src/test/java/org/icatproject/ids/integration/two/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/WriteTest.java @@ -28,8 +28,8 @@ public static void setup() throws Exception { } /** - * In principle, it's always possible to do a write call on existing - * datasets, but it will have no visible effect. + * In principle, it's always possible to do a write call on + * existing datasets, but it will have no visible effect. */ @Test public void restoreThenWriteDataset() throws Exception { @@ -47,19 +47,18 @@ public void restoreThenWriteDataset() throws Exception { } /** - * Create a dataset in ICAT, store the files in main storage, then do a - * write call to IDS to get the dataset written to archive storage. + * Create a dataset in ICAT, store the files in main storage, + * then do a write call to IDS to get the dataset written to + * archive storage. */ @Test public void storeThenWrite() throws Exception { long timestamp = System.currentTimeMillis(); - Investigation inv = (Investigation) icatWS.get(sessionId, - "Investigation INCLUDE Facility", investigationId); + Investigation inv = (Investigation) icatWS.get(sessionId, "Investigation INCLUDE Facility", investigationId); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS - .search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); Dataset ds = new Dataset(); ds.setName("dsWrite_" + timestamp); @@ -73,8 +72,7 @@ public void storeThenWrite() throws Exception { df.setName("dfWrite_" + timestamp); df.setLocation(dsLoc + UUID.randomUUID()); df.setDataset(ds); - writeToFile(df, "some really boring datafile test content", - setup.getKey()); + writeToFile(df, "some really boring datafile test content", setup.getKey()); Long dsId = ds.getId(); Path dirOnFastStorage = getDirOnFastStorage(dsId); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java b/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java index aeeff13b..75557414 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java @@ -30,15 +30,13 @@ public void restoreThenArchiveDataset() throws Exception { assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java index 392c8ad5..670a071a 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java @@ -47,24 +47,21 @@ public static void setup() throws Exception { @Before public void createBogusFiles() throws Exception { - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -80,11 +77,9 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -93,8 +88,7 @@ public void getEmptyDataset() throws Exception { @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); } @@ -102,17 +96,14 @@ public void getSizeEmptyDataset() throws Exception { @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); testingClient.restore(sessionId, selection, 204); waitForIds(); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, selection, null)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, null)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } @@ -121,8 +112,7 @@ public void getNonEmptyDataset() throws Exception { @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); } @@ -130,17 +120,14 @@ public void getSizeNonEmptyDataset() throws Exception { @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.restore(sessionId, selection, 404); waitForIds(); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, selection, 404)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, 404)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, 404)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -149,17 +136,15 @@ public void getBogusFile() throws Exception { @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); } /* - * Try the full cycle: upload a new file into a dataset having a bogus file, - * which triggers a write of the datafile to archive storage, archive the - * dataset, and restore it. Each step must deal gracefully with the bogus - * file in the dataset. + * Try the full cycle: upload a new file into a dataset having a bogus file, which triggers a write of the + * datafile to archive storage, archive the dataset, and restore it. Each step must deal gracefully with + * the bogus file in the dataset. */ @Test public void putWriteArchiveRestore() throws Exception { @@ -172,11 +157,9 @@ public void putWriteArchiveRestore() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfulId = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + Long dfulId = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); Datafile dful = (Datafile) icatWS.get(sessionId, "Datafile", dfulId); testingClient.archive(sessionId, selection, 204); waitForIds(); @@ -189,16 +172,13 @@ public void putWriteArchiveRestore() throws Exception { File[] filesList = dirOnFastStorage.toFile().listFiles(); assertEquals(3, filesList.length); Set locations = new HashSet<>(); - Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", - datafileIds.get(0)); + Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(0)); locations.add(getLocationFromDigest(df1.getId(), df1.getLocation())); - Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", - datafileIds.get(1)); + Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(1)); locations.add(getLocationFromDigest(df2.getId(), df2.getLocation())); locations.add(getLocationFromDigest(dful.getId(), dful.getLocation())); for (File file : filesList) { - String location = setup.getStorageDir().relativize(file.toPath()) - .toString(); + String location = setup.getStorageDir().relativize(file.toPath()).toString(); assertTrue(locations.contains(location)); } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java index e92431e7..063102fb 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java @@ -19,8 +19,7 @@ public static void setup() throws Exception { @Test public void deleteFromUnrestoredDataset() throws Exception { - testingClient.delete(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); } @Test @@ -28,13 +27,11 @@ public void deleteDatafileFromRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); - testingClient.delete(sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), 204); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); waitForIds(); } @@ -43,14 +40,12 @@ public void deleteRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); checkPresent(fileOnArchiveStorage); - testingClient.delete(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkAbsent(dirOnFastStorage); checkAbsent(fileOnArchiveStorage); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java index f505c837..b6d9e772 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java @@ -31,17 +31,14 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, - 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - 403)) { + try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { } } @@ -50,13 +47,11 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", - datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, - new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -70,15 +65,12 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", - datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection() - .addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, - Flag.NONE, 200); + DataSelection selection = new DataSelection().addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -91,9 +83,8 @@ public void getSizePreparedId() throws Exception { @Test public void correctBehaviourTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - 503)) { + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.NONE, + 0, 503)) { fail("Should have thrown exception"); } catch (IdsException e) { @@ -101,9 +92,8 @@ public void correctBehaviourTest() throws Exception { } while (true) { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - null)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds, 57, 0); break; } catch (IdsException e) { @@ -116,18 +106,16 @@ public void correctBehaviourTest() throws Exception { @Test public void gettingDatafileDoesNotRestoreItsDatasetTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(2)), Flag.NONE, - 0, 503)) { + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)), + Flag.NONE, 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), Flag.NONE, - 0, 503)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), + Flag.NONE, 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -138,9 +126,8 @@ public void gettingDatafileDoesNotRestoreItsDatasetTest() throws Exception { @Test public void gettingDatasetUsesCacheTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, - 503)) { + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -148,32 +135,26 @@ public void gettingDatasetUsesCacheTest() throws Exception { waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, - 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } } @Test - public void gettingDatafileAndDatasetShouldNotRestoreBothDatasetsTest() - throws Exception { - - try (InputStream z = testingClient - .getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(2)) - .addDataset(datasetIds.get(0)), - Flag.NONE, 0, 503)) { + public void gettingDatafileAndDatasetShouldNotRestoreBothDatasetsTest() throws Exception { + + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)) + .addDataset(datasetIds.get(0)), Flag.NONE, 0, 503)) { fail("Should throw exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatasets(datasetIds), Flag.NONE, 0, - 503)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatasets(datasetIds), + Flag.NONE, 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java index 200eba93..c0fa6eef 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java @@ -25,17 +25,15 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, - 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -47,16 +45,14 @@ public void badOffsetFormatTest() throws Exception { @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient - .getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { } } @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -68,10 +64,9 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() - throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -86,9 +81,8 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -100,13 +94,10 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() - throws Exception { + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -120,9 +111,8 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() @Test public void correctBehaviourWithOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -130,14 +120,12 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the zip file twice, with and without an offset byte[] zip = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] zipoffset = getOutput( - testingClient.getData(preparedId, goodOffset, 206)); + byte[] zipoffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the offset System.out.println(zip.length + " " + zipoffset.length); for (int i = 0; i < zipoffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) zipoffset[i], - (byte) zip[i + goodOffset]); + assertEquals("Byte offset: " + i, (byte) zipoffset[i], (byte) zip[i + goodOffset]); } } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java index 2ceadc91..ed6bed60 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java @@ -34,8 +34,8 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, new DataSelection() - .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(sessionId, + new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) @@ -55,10 +55,8 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, - Flag.NONE, 200); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } @@ -66,18 +64,15 @@ public void getStatusPreparedIdTest() throws Exception { testingClient.archive(sessionId, selection, 204); waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); - // verify that getStatus() as opposed to isPrepared() does not - // implicitly trigger a restore. + // verify that getStatus() as opposed to isPrepared() does not implicitly trigger a restore. waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); } @Test - public void restoringDatafileDoesNotRestoresItsDatasetTest() - throws Exception { + public void restoringDatafileDoesNotRestoresItsDatasetTest() throws Exception { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java index 6f7c04ae..7889492e 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java @@ -33,20 +33,22 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafiles( - Arrays.asList(1L, 2L, 3L, 99999L)), Flag.NONE, 404); + testingClient + .prepareData(sessionId, + new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), + Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, - new DataSelection().addDatafile(99999L), Flag.NONE, 404); + testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, + 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, - new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient + .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) diff --git a/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java b/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java index 674bc721..011de0ad 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java @@ -40,13 +40,11 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); var status = testingClient.getServiceStatus(sessionId, 200); - // System.out.println("### twodf.MiscTest.correctBehaviourNoOffsetTest - - // status: " + status.toString()); + //System.out.println("### twodf.MiscTest.correctBehaviourNoOffsetTest - status: " + status.toString()); assertFalse(status.getOpItems().isEmpty()); @@ -54,12 +52,10 @@ public void correctBehaviourNoOffsetTest() throws Exception { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java index 280e68d8..386a7fff 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java @@ -28,9 +28,8 @@ public static void setup() throws Exception { public void prepareArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -50,10 +49,8 @@ public void prepareTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -73,9 +70,8 @@ public void prepareTwoArchivedDatasets() throws Exception { public void prepareArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -92,11 +88,8 @@ public void prepareArchivedDatafile() throws Exception { public void prepareArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient - .prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -112,29 +105,26 @@ public void prepareArchivedDatafileAndItsDataset() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 400); } @Test public void noIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, - 200); + testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, 200); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 403); + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); assertNotNull(preparedId); } @@ -142,16 +132,14 @@ public void correctBehaviourTest() throws Exception { public void prepareRestoredDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -167,19 +155,16 @@ public void prepareTwoRestoredDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), - 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage1); checkPresent(dirOnFastStorage2); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -194,16 +179,14 @@ public void prepareRestoredDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -218,16 +201,13 @@ public void prepareRestoredDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection() - .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), - 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java b/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java index ce9ab96f..b960cd6e 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java @@ -31,9 +31,8 @@ public static void setup() throws Exception { @Test // Works fine for datafile storage public void putToUnrestoredDataset() throws Exception { - testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file1_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), null, 201); + testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file1_" + + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), null, 201); } @Test @@ -41,18 +40,15 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); waitForIds(); @@ -62,11 +58,9 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileCreateTime()); assertNull(df.getDatafileModTime()); - dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "An even better datafile", - "7.1.3", new Date(420000), new Date(42000), 201); + dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -84,8 +78,7 @@ public void putOneFileTest() throws Exception { assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java b/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java index e0127ebc..baf7be8d 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java @@ -22,8 +22,9 @@ public static void setup() throws Exception { } /* - * Arrange for multiple different operations, requiring conflicting locks on - * the same dataset to be processed at the same time. This triggers Bug #82. + * Arrange for multiple different operations, requiring conflicting + * locks on the same dataset to be processed at the same time. This + * triggers Bug #82. */ @Test public void multiOperationTest() throws Exception { @@ -36,9 +37,8 @@ public void multiOperationTest() throws Exception { assertTrue(Files.exists(dirOnFastStorage)); testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); testingClient.archive(sessionId, selection, 204); waitForIds(); } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java b/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java index 4f792af5..7125b9a4 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java @@ -20,8 +20,7 @@ public static void setup() throws Exception { @Test public void restoreArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); } @@ -30,9 +29,8 @@ public void restoreArchivedDataset() throws Exception { public void restoreTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), - 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage1); @@ -44,8 +42,7 @@ public void restoreArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); @@ -57,9 +54,8 @@ public void restoreArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection() - .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), - 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java b/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java index 3bae2233..876af417 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java @@ -27,8 +27,8 @@ public static void setup() throws Exception { } /** - * In principle, it's always possible to do a write call on existing - * datasets, but it will have no visible effect. + * In principle, it's always possible to do a write call on + * existing datasets, but it will have no visible effect. */ @Test public void restoreThenWriteDataset() throws Exception { @@ -46,19 +46,18 @@ public void restoreThenWriteDataset() throws Exception { } /** - * Create a dataset in ICAT, store the files in main storage, then do a - * write call to IDS to get the dataset written to archive storage. + * Create a dataset in ICAT, store the files in main storage, + * then do a write call to IDS to get the dataset written to + * archive storage. */ @Test public void storeThenWrite() throws Exception { long timestamp = System.currentTimeMillis(); - Investigation inv = (Investigation) icatWS.get(sessionId, - "Investigation INCLUDE Facility", investigationId); + Investigation inv = (Investigation) icatWS.get(sessionId, "Investigation INCLUDE Facility", investigationId); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS - .search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); Dataset ds = new Dataset(); ds.setName("dsWrite_" + timestamp); @@ -72,8 +71,7 @@ public void storeThenWrite() throws Exception { df.setName("dfWrite_" + timestamp); df.setLocation(dsLoc + UUID.randomUUID()); df.setDataset(ds); - writeToFile(df, "some really boring datafile test content", - setup.getKey()); + writeToFile(df, "some really boring datafile test content", setup.getKey()); Long dsId = ds.getId(); Path dirOnFastStorage = getDirOnFastStorage(dsId); diff --git a/src/test/java/org/icatproject/ids/integration/util/Setup.java b/src/test/java/org/icatproject/ids/integration/util/Setup.java index 5b628f33..2392805d 100644 --- a/src/test/java/org/icatproject/ids/integration/util/Setup.java +++ b/src/test/java/org/icatproject/ids/integration/util/Setup.java @@ -57,13 +57,11 @@ public Setup(String runPropertyFile) throws Exception { // Start by reading the test properties Properties testProps = new Properties(); - InputStream is = Setup.class.getClassLoader() - .getResourceAsStream("test.properties"); + InputStream is = Setup.class.getClassLoader().getResourceAsStream("test.properties"); try { testProps.load(is); } catch (Exception e) { - System.err.println("Problem loading test.properties: " - + e.getClass() + " " + e.getMessage()); + System.err.println("Problem loading test.properties: " + e.getClass() + " " + e.getMessage()); } setReliability(1.); @@ -74,18 +72,16 @@ public Setup(String runPropertyFile) throws Exception { String containerHome = System.getProperty("containerHome"); if (containerHome == null) { - System.err.println( - "containerHome is not defined as a system property"); + System.err.println("containerHome is not defined as a system property"); } long time = System.currentTimeMillis(); - ShellCommand sc = new ShellCommand("src/test/scripts/prepare_test.py", - "src/test/resources/" + runPropertyFile, home.toString(), - containerHome, serverUrl); + ShellCommand sc = new ShellCommand("src/test/scripts/prepare_test.py", "src/test/resources/" + runPropertyFile, + home.toString(), containerHome, serverUrl); System.out.println(sc.getStdout() + " " + sc.getStderr()); - System.out.println("Setting up " + runPropertyFile + " took " - + (System.currentTimeMillis() - time) / 1000. + "seconds"); + System.out.println( + "Setting up " + runPropertyFile + " took " + (System.currentTimeMillis() - time) / 1000. + "seconds"); // Having set up the ids.properties file read it find other things CheckedProperties runProperties = new CheckedProperties(); @@ -96,10 +92,8 @@ public Setup(String runPropertyFile) throws Exception { updownDir = home.resolve(testProps.getProperty("updownDir")); icatUrl = runProperties.getURL("icat.url"); ICAT icat = ICATGetter.getService(icatUrl.toString()); - rootSessionId = TestUtils.login(icat, - testProps.getProperty("login.root")); - forbiddenSessionId = TestUtils.login(icat, - testProps.getProperty("login.unauthorized")); + rootSessionId = TestUtils.login(icat, testProps.getProperty("login.root")); + forbiddenSessionId = TestUtils.login(icat, testProps.getProperty("login.unauthorized")); storageDir = runProperties.getPath("plugin.main.dir"); diff --git a/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java b/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java index 577d29f5..6527c191 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java @@ -17,8 +17,7 @@ public class HttpInputStream extends FilterInputStream { * @param httpclient the CloseableHttpClient to close * @param response the response to use and ultimately close */ - public HttpInputStream(CloseableHttpClient httpclient, - CloseableHttpResponse response) + public HttpInputStream(CloseableHttpClient httpclient, CloseableHttpResponse response) throws IllegalStateException, IOException { super(response.getEntity().getContent()); this.response = response; diff --git a/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java b/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java index 37b0ffdb..7bb22543 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java @@ -107,14 +107,11 @@ void storeFailure(Long id) { public String toString() { String result = "lockCount: " + this.lockCount + ", "; result += "lockedIDs: " + lockedDs.size() + "\n"; - for (long lockedId : lockedDs) - result += "\t" + lockedId + "\n"; + for(long lockedId : lockedDs) result += "\t" + lockedId + "\n"; result += "opItems: " + opItems.size() + "\n"; - for (String key : opItems.keySet()) - result += "\t" + key + ": " + opItems.get(key) + "\n"; + for(String key : opItems.keySet()) result += "\t" + key + ": " + opItems.get(key) + "\n"; result += "failures: " + failures.size() + "\n"; - for (long fail : failures) - result += " " + fail; + for(long fail : failures) result += " " + fail; result += "\n"; return result; @@ -132,8 +129,7 @@ public enum Status { public TestingClient(URL idsUrl) { try { - idsUri = new URI(idsUrl.getProtocol(), null, idsUrl.getHost(), - idsUrl.getPort(), null, null, null); + idsUri = new URI(idsUrl.getProtocol(), null, idsUrl.getHost(), idsUrl.getPort(), null, null, null); basePath = idsUrl.getPath(); } catch (URISyntaxException e) { throw new RuntimeException(e); @@ -141,28 +137,22 @@ public TestingClient(URL idsUrl) { } - public void archive(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, - NotFoundException { + public void archive(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, + BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { URI uri = getUri(getUriBuilder("archive")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(formparams)); - try (CloseableHttpResponse response = httpclient - .execute(httpPost)) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -170,10 +160,8 @@ public void archive(String sessionId, DataSelection data, Integer sc) } private void checkStatus(HttpResponse response, Integer sc) - throws InternalException, BadRequestException, - DataNotOnlineException, ParseException, IOException, - InsufficientPrivilegesException, NotImplementedException, - InsufficientStorageException, NotFoundException { + throws InternalException, BadRequestException, DataNotOnlineException, ParseException, IOException, + InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException { StatusLine status = response.getStatusLine(); if (status == null) { throw new InternalException("Status line returned is empty"); @@ -200,8 +188,7 @@ private void checkStatus(HttpResponse response, Integer sc) } String code; String message; - try (JsonReader jsonReader = Json - .createReader(new StringReader(error))) { + try (JsonReader jsonReader = Json.createReader(new StringReader(error))) { JsonObject json = jsonReader.readObject(); code = json.getString("code"); message = json.getString("message"); @@ -240,8 +227,7 @@ private void checkStatus(HttpResponse response, Integer sc) } public void delete(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("delete"); @@ -253,56 +239,47 @@ public void delete(String sessionId, DataSelection data, Integer sc) try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpDelete httpDelete = new HttpDelete(uri); - try (CloseableHttpResponse response = httpclient - .execute(httpDelete)) { + try (CloseableHttpResponse response = httpclient.execute(httpDelete)) { expectNothing(response, sc); } catch (InsufficientStorageException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - private void expectNothing(CloseableHttpResponse response, Integer sc) - throws InternalException, BadRequestException, - DataNotOnlineException, ParseException, - InsufficientPrivilegesException, NotImplementedException, - InsufficientStorageException, NotFoundException, IOException { + private void expectNothing(CloseableHttpResponse response, Integer sc) throws InternalException, + BadRequestException, DataNotOnlineException, ParseException, InsufficientPrivilegesException, + NotImplementedException, InsufficientStorageException, NotFoundException, IOException { checkStatus(response, sc); HttpEntity entity = response.getEntity(); if (entity != null) { if (!EntityUtils.toString(entity).isEmpty()) { - throw new InternalException( - "No http entity expected in response"); + throw new InternalException("No http entity expected in response"); } } } - public URL getIcatUrl(int sc) - throws InternalException, ParseException, NotImplementedException { + public URL getIcatUrl(int sc) throws InternalException, ParseException, NotImplementedException { URI uri = getUri(getUriBuilder("getIcatUrl")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return new URL(getString(response, sc)); - } catch (IOException | InsufficientStorageException - | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public InputStream getData(String sessionId, DataSelection data, Flag flags, - long offset, Integer sc) throws NotImplementedException, - BadRequestException, InsufficientPrivilegesException, - NotFoundException, InternalException, DataNotOnlineException { + public InputStream getData(String sessionId, DataSelection data, Flag flags, long offset, Integer sc) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, + InternalException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("getData"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -353,8 +330,7 @@ public InputStream getData(String sessionId, DataSelection data, Flag flags, } public InputStream getData(String preparedId, long offset, Integer sc) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("getData"); @@ -396,8 +372,7 @@ public InputStream getData(String preparedId, long offset, Integer sc) } public ServiceStatus getServiceStatus(String sessionId, Integer sc) - throws InternalException, InsufficientPrivilegesException, - NotImplementedException { + throws InternalException, InsufficientPrivilegesException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getServiceStatus"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -409,8 +384,7 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); String result = getString(response, sc); - try (JsonReader jsonReader = Json - .createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { ServiceStatus serviceStatus = new ServiceStatus(); JsonObject rootNode = jsonReader.readObject(); for (JsonValue on : rootNode.getJsonArray("opsQueue")) { @@ -420,8 +394,7 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) } serviceStatus.setLockedCount(rootNode.getInt("lockCount")); for (JsonValue lock : rootNode.getJsonArray("locks")) { - Long dsId = ((JsonObject) lock).getJsonNumber("id") - .longValueExact(); + Long dsId = ((JsonObject) lock).getJsonNumber("id").longValueExact(); serviceStatus.storeLockedDs(dsId); } for (JsonValue num : rootNode.getJsonArray("failures")) { @@ -430,25 +403,21 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) } return serviceStatus; } catch (JsonException e) { - throw new InternalException("TestingClient " + e.getClass() - + " " + e.getMessage() + " from " + result); + throw new InternalException( + "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); } - } catch (InsufficientStorageException | DataNotOnlineException - | InternalException | BadRequestException - | NotFoundException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException | InternalException | BadRequestException + | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public long getSize(String sessionId, DataSelection data, int sc) - throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, - NotImplementedException { + public long getSize(String sessionId, DataSelection data, int sc) throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getSize"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -462,19 +431,16 @@ public long getSize(String sessionId, DataSelection data, int sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Long.parseLong(getString(response, sc)); - } catch (IOException | InsufficientStorageException - | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException | DataNotOnlineException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public long getSize(String preparedId, int sc) throws BadRequestException, - NotFoundException, InsufficientPrivilegesException, - InternalException, NotImplementedException { + public long getSize(String preparedId, int sc) throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getSize"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); @@ -484,20 +450,16 @@ public long getSize(String preparedId, int sc) throws BadRequestException, HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { return Long.parseLong(getString(response, sc)); - } catch (IOException | InsufficientStorageException - | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException | DataNotOnlineException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public Status getStatus(String sessionId, DataSelection data, Integer sc) - throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, - NotImplementedException { + public Status getStatus(String sessionId, DataSelection data, Integer sc) throws BadRequestException, + NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getStatus"); if (sessionId != null) { @@ -515,8 +477,7 @@ public Status getStatus(String sessionId, DataSelection data, Integer sc) checkResponseConformity(response); return Status.valueOf(getString(response, sc)); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -524,10 +485,8 @@ public Status getStatus(String sessionId, DataSelection data, Integer sc) } - public Status getStatus(String preparedId, Integer sc) - throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, - NotImplementedException { + public Status getStatus(String preparedId, Integer sc) throws BadRequestException, + NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getStatus"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); @@ -539,8 +498,7 @@ public Status getStatus(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { return Status.valueOf(getString(response, sc)); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -548,10 +506,8 @@ public Status getStatus(String preparedId, Integer sc) } - private String getString(CloseableHttpResponse response, Integer sc) - throws InternalException, BadRequestException, - DataNotOnlineException, ParseException, - InsufficientPrivilegesException, NotImplementedException, + private String getString(CloseableHttpResponse response, Integer sc) throws InternalException, BadRequestException, + DataNotOnlineException, ParseException, InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException, IOException { checkStatus(response, sc); HttpEntity entity = response.getEntity(); @@ -574,8 +530,7 @@ private URIBuilder getUriBuilder(String path) { } public boolean isPrepared(String preparedId, Integer sc) - throws BadRequestException, NotFoundException, InternalException, - NotImplementedException { + throws BadRequestException, NotFoundException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("isPrepared"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); @@ -587,56 +542,47 @@ public boolean isPrepared(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (InsufficientStorageException | DataNotOnlineException - | InsufficientPrivilegesException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public boolean isReadOnly(int sc) - throws InternalException, NotImplementedException { + public boolean isReadOnly(int sc) throws InternalException, NotImplementedException { URI uri = getUri(getUriBuilder("isReadOnly")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (IOException | InsufficientStorageException - | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public boolean isTwoLevel(int sc) - throws InternalException, NotImplementedException { + public boolean isTwoLevel(int sc) throws InternalException, NotImplementedException { URI uri = getUri(getUriBuilder("isTwoLevel")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (IOException | InsufficientStorageException - | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void ping(Integer sc) throws InternalException, NotFoundException, - NotImplementedException { + public void ping(Integer sc) throws InternalException, NotFoundException, NotImplementedException { URI uri = getUri(getUriBuilder("ping")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { @@ -645,32 +591,26 @@ public void ping(Integer sc) throws InternalException, NotFoundException, checkResponseConformity(response); String result = getString(response, sc); if (!result.equals("IdsOK")) { - throw new NotFoundException( - "Server gave invalid response: " + result); + throw new NotFoundException("Server gave invalid response: " + result); } - } catch (IOException | InsufficientStorageException - | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public String prepareData(String sessionId, DataSelection data, Flag flags, - Integer sc) throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, + public String prepareData(String sessionId, DataSelection data, Flag flags, Integer sc) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException { URI uri = getUri(getUriBuilder("prepareData")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } if (flags == Flag.ZIP || flags == Flag.ZIP_AND_COMPRESS) { formparams.add(new BasicNameValuePair("zip", "true")); @@ -682,13 +622,11 @@ public String prepareData(String sessionId, DataSelection data, Flag flags, HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient - .execute(httpPost)) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { checkResponseConformity(response); return getString(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -696,23 +634,17 @@ public String prepareData(String sessionId, DataSelection data, Flag flags, } - public Long put(String sessionId, InputStream inputStream, String name, - long datasetId, long datafileFormatId, String description, - Integer sc) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, NotImplementedException, - DataNotOnlineException, InsufficientStorageException { - return put(sessionId, inputStream, name, datasetId, datafileFormatId, - description, null, null, null, sc); + public Long put(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, + String description, Integer sc) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, + NotImplementedException, DataNotOnlineException, InsufficientStorageException { + return put(sessionId, inputStream, name, datasetId, datafileFormatId, description, null, null, null, sc); } - public Long put(String sessionId, InputStream inputStream, String name, - long datasetId, long datafileFormatId, String description, - String doi, Date datafileCreateTime, Date datafileModTime, - Integer sc) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, NotImplementedException, - DataNotOnlineException, InsufficientStorageException { + public Long put(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, + String description, String doi, Date datafileCreateTime, Date datafileModTime, Integer sc) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, + NotImplementedException, DataNotOnlineException, InsufficientStorageException { if (inputStream == null) { throw new BadRequestException("Input stream is null"); @@ -720,10 +652,8 @@ public Long put(String sessionId, InputStream inputStream, String name, CRC32 crc = new CRC32(); inputStream = new CheckedInputStream(inputStream, crc); URIBuilder uriBuilder = getUriBuilder("put"); - uriBuilder.setParameter(RequestIdNames.sessionId, sessionId) - .setParameter("name", name) - .setParameter("datafileFormatId", - Long.toString(datafileFormatId)) + uriBuilder.setParameter(RequestIdNames.sessionId, sessionId).setParameter("name", name) + .setParameter("datafileFormatId", Long.toString(datafileFormatId)) .setParameter("datasetId", Long.toString(datasetId)); if (description != null) { uriBuilder.setParameter("description", description); @@ -732,49 +662,39 @@ public Long put(String sessionId, InputStream inputStream, String name, uriBuilder.setParameter("doi", doi); } if (datafileCreateTime != null) { - uriBuilder.setParameter("datafileCreateTime", - Long.toString(datafileCreateTime.getTime())); + uriBuilder.setParameter("datafileCreateTime", Long.toString(datafileCreateTime.getTime())); } if (datafileModTime != null) { - uriBuilder.setParameter("datafileModTime", - Long.toString(datafileModTime.getTime())); + uriBuilder.setParameter("datafileModTime", Long.toString(datafileModTime.getTime())); } URI uri = getUri(uriBuilder); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPut httpPut = new HttpPut(uri); - httpPut.setEntity(new InputStreamEntity(inputStream, - ContentType.APPLICATION_OCTET_STREAM)); + httpPut.setEntity(new InputStreamEntity(inputStream, ContentType.APPLICATION_OCTET_STREAM)); try (CloseableHttpResponse response = httpclient.execute(httpPut)) { String result = getString(response, sc); checkResponseConformity(response); - try (JsonReader jsonReader = Json - .createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); - if (rootNode.getJsonNumber("checksum").longValueExact() != crc - .getValue()) { - throw new InternalException( - "Error uploading - the checksum was not as expected"); + if (rootNode.getJsonNumber("checksum").longValueExact() != crc.getValue()) { + throw new InternalException("Error uploading - the checksum was not as expected"); } return rootNode.getJsonNumber("id").longValueExact(); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (NumberFormatException e) { - throw new InternalException( - "Web service call did not return a valid Long value"); + throw new InternalException("Web service call did not return a valid Long value"); } } - public Long putAsPost(String sessionId, InputStream inputStream, - String name, long datasetId, long datafileFormatId, - String description, String doi, Date datafileCreateTime, - Date datafileModTime, boolean wrap, Integer sc) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, NotImplementedException, - DataNotOnlineException, InsufficientStorageException { + public Long putAsPost(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, + String description, String doi, Date datafileCreateTime, Date datafileModTime, boolean wrap, Integer sc) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, + NotImplementedException, DataNotOnlineException, InsufficientStorageException { if (inputStream == null) { throw new BadRequestException("Input stream is null"); @@ -783,40 +703,29 @@ public Long putAsPost(String sessionId, InputStream inputStream, inputStream = new CheckedInputStream(inputStream, crc); URI uri = getUri(getUriBuilder("put")); - MultipartEntityBuilder reqEntityBuilder = MultipartEntityBuilder - .create() - .addPart(RequestIdNames.sessionId, - new StringBody(sessionId, ContentType.TEXT_PLAIN)) - .addPart("datafileFormatId", - new StringBody(Long.toString(datafileFormatId), - ContentType.TEXT_PLAIN)) + MultipartEntityBuilder reqEntityBuilder = MultipartEntityBuilder.create() + .addPart(RequestIdNames.sessionId, new StringBody(sessionId, ContentType.TEXT_PLAIN)) + .addPart("datafileFormatId", new StringBody(Long.toString(datafileFormatId), ContentType.TEXT_PLAIN)) .addPart("name", new StringBody(name, ContentType.TEXT_PLAIN)) - .addPart("datasetId", new StringBody(Long.toString(datasetId), - ContentType.TEXT_PLAIN)); + .addPart("datasetId", new StringBody(Long.toString(datasetId), ContentType.TEXT_PLAIN)); if (description != null) { - reqEntityBuilder.addPart("description", - new StringBody(description, ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart("description", new StringBody(description, ContentType.TEXT_PLAIN)); } if (doi != null) { - reqEntityBuilder.addPart("doi", - new StringBody(doi, ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart("doi", new StringBody(doi, ContentType.TEXT_PLAIN)); } if (datafileCreateTime != null) { reqEntityBuilder.addPart("datafileCreateTime", - new StringBody(Long.toString(datafileCreateTime.getTime()), - ContentType.TEXT_PLAIN)); + new StringBody(Long.toString(datafileCreateTime.getTime()), ContentType.TEXT_PLAIN)); } if (datafileModTime != null) { reqEntityBuilder.addPart("datafileModTime", - new StringBody(Long.toString(datafileModTime.getTime()), - ContentType.TEXT_PLAIN)); + new StringBody(Long.toString(datafileModTime.getTime()), ContentType.TEXT_PLAIN)); } if (wrap) { - reqEntityBuilder.addPart("wrap", - new StringBody("true", ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart("wrap", new StringBody("true", ContentType.TEXT_PLAIN)); } - InputStreamBody body = new InputStreamBody( - new BufferedInputStream(inputStream), + InputStreamBody body = new InputStreamBody(new BufferedInputStream(inputStream), ContentType.APPLICATION_OCTET_STREAM, "unreliable"); HttpEntity entity = reqEntityBuilder.addPart("file", body).build(); @@ -830,51 +739,40 @@ public Long putAsPost(String sessionId, InputStream inputStream, String prefix = ""; if (result.startsWith(prefix)) { - result = result.substring(prefix.length(), - result.length() - suffix.length()); + result = result.substring(prefix.length(), result.length() - suffix.length()); } - try (JsonReader jsonReader = Json - .createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); - if (rootNode.getJsonNumber("checksum").longValueExact() != crc - .getValue()) { - throw new InternalException( - "Error uploading - the checksum was not as expected"); + if (rootNode.getJsonNumber("checksum").longValueExact() != crc.getValue()) { + throw new InternalException("Error uploading - the checksum was not as expected"); } return rootNode.getJsonNumber("id").longValueExact(); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (NumberFormatException e) { - throw new InternalException( - "Web service call did not return a valid Long value"); + throw new InternalException("Web service call did not return a valid Long value"); } } - public void restore(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, - NotFoundException { + public void restore(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, + BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { URI uri = getUri(getUriBuilder("restore")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient - .execute(httpPost)) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -894,8 +792,7 @@ public List getDatafileIds(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { String result = getString(response, sc); checkResponseConformity(response); - try (JsonReader jsonReader = Json - .createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); List ids = new ArrayList<>(); @@ -905,24 +802,21 @@ public List getDatafileIds(String preparedId, Integer sc) } return ids; } catch (JsonException e) { - throw new InternalException("TestingClient " + e.getClass() - + " " + e.getMessage() + " from " + result); + throw new InternalException( + "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); } - } catch (InsufficientStorageException | DataNotOnlineException - | InsufficientPrivilegesException - | NotImplementedException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException + | NotImplementedException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public List getDatafileIds(String sessionId, DataSelection data, - Integer sc) throws InternalException, BadRequestException, - ParseException, NotFoundException { + public List getDatafileIds(String sessionId, DataSelection data, Integer sc) + throws InternalException, BadRequestException, ParseException, NotFoundException { URIBuilder uriBuilder = getUriBuilder("getDatafileIds"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); for (Entry entry : data.getParameters().entrySet()) { @@ -936,8 +830,7 @@ public List getDatafileIds(String sessionId, DataSelection data, try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); String result = getString(response, sc); - try (JsonReader jsonReader = Json - .createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); List ids = new ArrayList<>(); @@ -947,122 +840,97 @@ public List getDatafileIds(String sessionId, DataSelection data, } return ids; } catch (JsonException e) { - throw new InternalException("TestingClient " + e.getClass() - + " " + e.getMessage() + " from " + result); + throw new InternalException( + "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); } - } catch (InsufficientStorageException | DataNotOnlineException - | InsufficientPrivilegesException - | NotImplementedException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException + | NotImplementedException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void reset(String preparedId, Integer sc) - throws InternalException, BadRequestException, ParseException, - InsufficientPrivilegesException, NotImplementedException, - NotFoundException { + public void reset(String preparedId, Integer sc) throws InternalException, BadRequestException, ParseException, + InsufficientPrivilegesException, NotImplementedException, NotFoundException { URI uri = getUri(getUriBuilder("reset")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.preparedId, preparedId)); + formparams.add(new BasicNameValuePair(RequestIdNames.preparedId, preparedId)); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient - .execute(httpPost)) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void reset(String sessionId, DataSelection data, int sc) - throws InternalException, BadRequestException, ParseException, - InsufficientPrivilegesException, NotImplementedException, - NotFoundException { + public void reset(String sessionId, DataSelection data, int sc) throws InternalException, BadRequestException, + ParseException, InsufficientPrivilegesException, NotImplementedException, NotFoundException { URI uri = getUri(getUriBuilder("reset")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient - .execute(httpPost)) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void write(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, - NotFoundException, DataNotOnlineException { + public void write(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, + BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, + DataNotOnlineException { URI uri = getUri(getUriBuilder("write")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(formparams)); - try (CloseableHttpResponse response = httpclient - .execute(httpPost)) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - private void checkResponseConformity(HttpResponse response) - throws InternalException { + private void checkResponseConformity(HttpResponse response) throws InternalException { StatusLine status = response.getStatusLine(); var statusCode = status.getStatusCode(); - if (statusCode == 200 && response.getEntity() != null) { - // we have a status of 200 and a payload. Check for header - // conformity - Boolean chunked = response - .getFirstHeader("Transfer-Encoding") != null - ? response.getFirstHeader("Transfer-Encoding") - .getValue().contains("chunked") - : false; - - if ((response.getFirstHeader("Content-Length") == null && !chunked) - || (response.getFirstHeader("Content-Length") != null - && chunked)) { - - throw new InternalException( - "Responses with payload should either contain the Content-Length header or the 'Transfer-Encoding: chunked' header. It needs exactly one of them."); + if(statusCode == 200 && response.getEntity() != null) { + //we have a status of 200 and a payload. Check for header conformity + Boolean chunked = response.getFirstHeader("Transfer-Encoding") != null ? response.getFirstHeader("Transfer-Encoding").getValue().contains("chunked") + : false; + + if ( (response.getFirstHeader("Content-Length") == null && !chunked) + || (response.getFirstHeader("Content-Length") != null && chunked ) ) { + + throw new InternalException("Responses with payload should either contain the Content-Length header or the 'Transfer-Encoding: chunked' header. It needs exactly one of them."); } } } From cd48ccca7691d822866ec9c5c284bb0fa36dd86d Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Wed, 12 Jun 2024 09:48:02 +0200 Subject: [PATCH 14/19] Revomed formatter because of the lack of an import order feature --- eclipse-formatter-config.xml | 404 ----------------------------------- pom.xml | 43 ---- 2 files changed, 447 deletions(-) delete mode 100644 eclipse-formatter-config.xml diff --git a/eclipse-formatter-config.xml b/eclipse-formatter-config.xml deleted file mode 100644 index 8c3a01e3..00000000 --- a/eclipse-formatter-config.xml +++ /dev/null @@ -1,404 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 39f071f5..9607b7ad 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,6 @@ UTF-8 https://repo.icatproject.org/repo github - format @@ -29,11 +28,6 @@ MireDot Releases https://secure-nexus.miredot.com/content/repositories/miredot - - net.revelc.code.formatter - formatter-maven-plugin Repo - https://mvnrepository.com/artifact/net.revelc.code.formatter/ - @@ -135,13 +129,6 @@ test - - - net.revelc.code.formatter - formatter-maven-plugin - 2.24.1 - - @@ -428,40 +415,10 @@ - - net.revelc.code.formatter - formatter-maven-plugin - 2.23.0 - - ${project.basedir}/eclipse-formatter-config.xml - - - - - ${formatter.goal} - - - - - - - cibuild - - - env.CIBUILD - - - - - validate - - - - From 7ffb2186a9b79f9efa9666ac347e0068da19bef8 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Wed, 12 Jun 2024 11:11:24 +0200 Subject: [PATCH 15/19] using spotless for managing eclipse formatter and import order management --- README.md | 10 +- eclipse-formatter.xml | 404 ++++++++++++++++++++++++++++++++++++++++++ pom.xml | 55 ++++++ 3 files changed, 465 insertions(+), 4 deletions(-) create mode 100644 eclipse-formatter.xml diff --git a/README.md b/README.md index 4aa195e1..62844851 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,15 @@ # ids.server: Server part of ICAT Data Server (IDS) ## Linting -The [formatter-maven-plugin](https://code.revelc.net/formatter-maven-plugin/) is used here for linting. It uses the same configuration like the Eclipse code formatting feature. +The [spotless-maven-plugin](https://mvnrepository.com/artifact/com.diffplug.spotless/spotless-maven-plugin) is used here for linting. It uses the Eclipse code formatting feature. The guidelines are defined in `eclipse-formatter.xml` which was initially exported from eclipse (so you can do it again: define rules in eclipse an export the file to use it here). + An automatically code formatting is triggered while local build. In CI builds the code is only validated. You also can execute the commands manually: -* `mvn formatter:validate` for validation -* `mvn formatter:format` to update the source code like it is defined in the formatters configuration. +* `mvn spotless:check` for validation +* `mvn spotless:apply` to update the source code like it is defined in the formatters configuration. -The rules for the formatter are defined in `eclipse-formatter-config.xml`. +### Import order +The import order is also validated and applied by the spotless-maven-plugin. It is defined in its plugin configuration in the `pom.xml`. You can find it in `plugin.configuration.java.importOrder.order` ## Further Information [![Build Status](https://github.com/icatproject/ids.server/workflows/CI%20Build/badge.svg?branch=master)](https://github.com/icatproject/ids.server/actions?query=workflow%3A%22CI+Build%22) diff --git a/eclipse-formatter.xml b/eclipse-formatter.xml new file mode 100644 index 00000000..8c3a01e3 --- /dev/null +++ b/eclipse-formatter.xml @@ -0,0 +1,404 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 9607b7ad..2b4ac015 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,7 @@ UTF-8 https://repo.icatproject.org/repo github + apply @@ -28,6 +29,11 @@ MireDot Releases https://secure-nexus.miredot.com/content/repositories/miredot + + com.diffplug.spotless + spotless-maven-plugin Repo + https://mvnrepository.com/artifact/com.diffplug.spotless/ + @@ -129,6 +135,13 @@ test + + + com.diffplug.spotless + spotless-maven-plugin + 2.43.0 + + @@ -415,10 +428,52 @@ + + com.diffplug.spotless + spotless-maven-plugin + 2.43.0 + + + + src/main/java/**/*.java + src/test/java/**/*.java + + + ${project.basedir}/eclipse-formatter.xml + + + java|jakarta|org|org.icatproject|org.icatproject.ids + + + + + + + ${spotless.goal} + + + + + + + + cibuild + + + env.CIBUILD + + + + + check + + + + From 4b5b75866176b2429b45101b0ecd516c03a572d8 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Wed, 12 Jun 2024 11:59:16 +0200 Subject: [PATCH 16/19] spotless: moved import order to file --- README.md | 2 +- eclipse-importorder.txt | 7 +++++++ pom.xml | 5 +++-- 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 eclipse-importorder.txt diff --git a/README.md b/README.md index 62844851..dd28dbd1 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ You also can execute the commands manually: * `mvn spotless:apply` to update the source code like it is defined in the formatters configuration. ### Import order -The import order is also validated and applied by the spotless-maven-plugin. It is defined in its plugin configuration in the `pom.xml`. You can find it in `plugin.configuration.java.importOrder.order` +The import order is also validated and applied by the spotless-maven-plugin. It is defined in the file `eclipse-importorder.txt` and referenced in `pom.xml`. It is needed that it has a compatible format like the eclipse built-in import order configuration (better its exported file). ## Further Information [![Build Status](https://github.com/icatproject/ids.server/workflows/CI%20Build/badge.svg?branch=master)](https://github.com/icatproject/ids.server/actions?query=workflow%3A%22CI+Build%22) diff --git a/eclipse-importorder.txt b/eclipse-importorder.txt new file mode 100644 index 00000000..a7cc5e1e --- /dev/null +++ b/eclipse-importorder.txt @@ -0,0 +1,7 @@ +#Organize Import Order +#Wed Jun 12 08:50:43 CEST 2024 +0=java +1=jakarta +2=org +3=org.icatproject +4=org.icatproject.ids \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2b4ac015..99568a5a 100644 --- a/pom.xml +++ b/pom.xml @@ -442,15 +442,16 @@ ${project.basedir}/eclipse-formatter.xml - java|jakarta|org|org.icatproject|org.icatproject.ids + ${project.basedir}/eclipse-importorder.txt - ${spotless.goal} + ${spotless.goal} + compile From 3d45da7e244dd382f5997e5f45e31127addfd726 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:53:43 +0200 Subject: [PATCH 17/19] linted code to test ci check --- .../java/org/icatproject/ids/IdsService.java | 704 +++++++++++------- src/main/java/org/icatproject/ids/Tidier.java | 139 ++-- .../icatproject/ids/enums/RequestType.java | 19 +- .../ids/exceptions/IdsExceptionMapper.java | 9 +- .../exceptions/NotFoundExceptionMapper.java | 13 +- .../exceptions/RuntimeExceptionMapper.java | 13 +- .../FiniteStateMachine.java | 73 +- ...niteStateMachineForSingleLevelStorage.java | 21 +- ...iteStateMachineForStorageUnitDatafile.java | 209 ++++-- ...niteStateMachineForStorageUnitDataset.java | 132 ++-- .../ids/helpers/CORSResponseFilter.java | 4 +- .../icatproject/ids/helpers/Constants.java | 4 +- .../ids/helpers/LocationHelper.java | 13 +- .../ids/helpers/RangeOutputStream.java | 8 +- .../java/org/icatproject/ids/helpers/SO.java | 60 +- .../ids/helpers/ValueContainer.java | 96 ++- .../icatproject/ids/models/DataInfoBase.java | 4 +- .../icatproject/ids/models/DataSelection.java | 16 +- .../icatproject/ids/models/DatafileInfo.java | 26 +- .../icatproject/ids/models/DatasetInfo.java | 44 +- .../org/icatproject/ids/models/Prepared.java | 76 +- .../ids/requestHandlers/ArchiveHandler.java | 13 +- .../ids/requestHandlers/DeleteHandler.java | 32 +- .../GetDataFileIdsHandler.java | 26 +- .../ids/requestHandlers/GetDataHandler.java | 125 ++-- .../requestHandlers/GetIcatUrlHandler.java | 10 +- .../GetServiceStatusHandler.java | 23 +- .../ids/requestHandlers/GetStatusHandler.java | 18 +- .../requestHandlers/IsPreparedHandler.java | 24 +- .../requestHandlers/IsReadOnlyHandler.java | 8 +- .../requestHandlers/IsTwoLevelHandler.java | 8 +- .../requestHandlers/PrepareDataHandler.java | 43 +- .../ids/requestHandlers/PutHandler.java | 209 ++++-- .../ids/requestHandlers/ResetHandler.java | 18 +- .../ids/requestHandlers/RestoreHandler.java | 14 +- .../ids/requestHandlers/WriteHandler.java | 26 +- .../base/DataControllerBase.java | 45 +- .../base/DataRequestHandler.java | 59 +- .../base/PreparedDataController.java | 36 +- .../base/RequestHandlerBase.java | 80 +- .../base/UnpreparedDataController.java | 44 +- .../getSizeHandlers/GetSizeHandler.java | 36 +- .../GetSizeHandlerForFastProcessing.java | 76 +- .../icatproject/ids/services/ICATGetter.java | 22 +- .../icatproject/ids/services/IcatReader.java | 12 +- .../icatproject/ids/services/LockManager.java | 19 +- .../ids/services/PropertyHandler.java | 113 ++- .../ids/services/ServiceProvider.java | 37 +- .../icatproject/ids/services/Transmitter.java | 9 +- .../ids/services/UnfinishedWorkService.java | 74 +- .../DataSelectionService.java | 159 ++-- .../DataSelectionServiceFactory.java | 376 ++++++---- ...SelectionServiceForSingleLevelStorage.java | 42 +- ...electionServiceForStorageUnitDatafile.java | 31 +- ...SelectionServiceForStorageUnitDataset.java | 37 +- .../icatproject/ids/thread/DfArchiver.java | 19 +- .../org/icatproject/ids/thread/DfDeleter.java | 13 +- .../icatproject/ids/thread/DfRestorer.java | 21 +- .../org/icatproject/ids/thread/DfWriter.java | 18 +- .../icatproject/ids/thread/DsArchiver.java | 15 +- .../icatproject/ids/thread/DsRestorer.java | 46 +- .../org/icatproject/ids/thread/DsWriter.java | 40 +- .../icatproject/ids/DataSelectionDevTest.java | 45 +- .../java/org/icatproject/ids/DigestTest.java | 13 +- .../icatproject/ids/PreparePackingTest.java | 32 +- .../ids/RangeOutputStreamTest.java | 9 +- .../org/icatproject/ids/TestICATGetter.java | 11 +- .../java/org/icatproject/ids/TestUtils.java | 7 +- .../icatproject/ids/TestValueContainer.java | 23 +- .../java/org/icatproject/ids/TestZipping.java | 12 +- .../java/org/icatproject/ids/TidierTest.java | 16 +- .../org/icatproject/ids/TransmittingTest.java | 167 +++-- .../org/icatproject/ids/ValidaterTest.java | 2 +- .../icatproject/ids/integration/BaseTest.java | 318 +++++--- .../integration/one/BogusDatafileTest.java | 40 +- .../ids/integration/one/DeleteTest.java | 22 +- .../integration/one/GetDataExplicitTest.java | 106 ++- .../one/GetDataForPreparedIdTest.java | 53 +- .../one/GetStatusExplicitTest.java | 21 +- .../one/GetStatusForPreparedIdTest.java | 14 +- .../ids/integration/one/MiscTest.java | 22 +- .../ids/integration/one/PerformanceTest.java | 21 +- .../ids/integration/one/PrepareDataTest.java | 54 +- .../ids/integration/one/PutTest.java | 40 +- .../ids/integration/one/WriteTest.java | 3 +- .../ids/integration/two/ArchiveTest.java | 11 +- .../integration/two/BogusDatafileTest.java | 74 +- .../ids/integration/two/DeleteTest.java | 15 +- .../integration/two/GetDataExplicitTest.java | 77 +- .../two/GetDataForPreparedIdTest.java | 49 +- .../two/GetStatusExplicitTest.java | 19 +- .../two/GetStatusForPreparedIdTest.java | 14 +- .../ids/integration/two/MiscTest.java | 19 +- .../ids/integration/two/PrepareDataTest.java | 81 +- .../ids/integration/two/PutTest.java | 37 +- .../integration/two/RestoreErrorsTest.java | 17 +- .../ids/integration/two/RestoreTest.java | 16 +- .../ids/integration/two/WriteTest.java | 19 +- .../ids/integration/twodf/ArchiveTest.java | 11 +- .../integration/twodf/BogusDatafileTest.java | 74 +- .../ids/integration/twodf/DeleteTest.java | 15 +- .../twodf/GetDataExplicitTest.java | 77 +- .../twodf/GetDataForPreparedIdTest.java | 51 +- .../twodf/GetStatusExplicitTest.java | 22 +- .../twodf/GetStatusForPreparedIdTest.java | 14 +- .../ids/integration/twodf/MiscTest.java | 19 +- .../integration/twodf/PrepareDataTest.java | 81 +- .../ids/integration/twodf/PutTest.java | 37 +- .../ids/integration/twodf/QueueTest.java | 13 +- .../ids/integration/twodf/RestoreTest.java | 16 +- .../ids/integration/twodf/WriteTest.java | 19 +- .../ids/integration/util/Setup.java | 29 +- .../util/client/HttpInputStream.java | 3 +- .../util/client/TestingClient.java | 455 +++++++---- 114 files changed, 3915 insertions(+), 2279 deletions(-) diff --git a/src/main/java/org/icatproject/ids/IdsService.java b/src/main/java/org/icatproject/ids/IdsService.java index 0d517fa9..c0a57cd0 100644 --- a/src/main/java/org/icatproject/ids/IdsService.java +++ b/src/main/java/org/icatproject/ids/IdsService.java @@ -6,7 +6,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; - import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import jakarta.ejb.EJB; @@ -23,16 +22,18 @@ import jakarta.ws.rs.HeaderParam; import jakarta.ws.rs.POST; import jakarta.ws.rs.PUT; +import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; -import jakarta.ws.rs.Path; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.RequestIdNames; import org.icatproject.ids.exceptions.BadRequestException; import org.icatproject.ids.exceptions.DataNotOnlineException; @@ -71,7 +72,8 @@ @Stateless public class IdsService { - private final static Logger logger = LoggerFactory.getLogger(IdsService.class); + private final static Logger logger = LoggerFactory + .getLogger(IdsService.class); @EJB Transmitter transmitter; @@ -89,7 +91,6 @@ public class IdsService { private UnfinishedWorkService unfinishedWorkService; - @PostConstruct private void init() { @@ -98,15 +99,19 @@ private void init() { logger.info("creating IdsService"); - FiniteStateMachine.createInstance(reader, lockManager, PropertyHandler.getInstance().getStorageUnit()); + FiniteStateMachine.createInstance(reader, lockManager, + PropertyHandler.getInstance().getStorageUnit()); this.fsm = FiniteStateMachine.getInstance(); this.fsm.init(); - ServiceProvider.createInstance(transmitter, fsm, lockManager, reader); + ServiceProvider.createInstance(transmitter, fsm, lockManager, + reader); - var propertyHandler = ServiceProvider.getInstance().getPropertyHandler(); + var propertyHandler = ServiceProvider.getInstance() + .getPropertyHandler(); var archiveStorage = propertyHandler.getArchiveStorage(); var twoLevel = archiveStorage != null; - var preparedDir = propertyHandler.getCacheDir().resolve("prepared"); + var preparedDir = propertyHandler.getCacheDir() + .resolve("prepared"); Files.createDirectories(preparedDir); this.unfinishedWorkService = new UnfinishedWorkService(); @@ -120,13 +125,17 @@ private void init() { java.nio.file.Path datasetDir; if (twoLevel) { - datasetDir = propertyHandler.getCacheDir().resolve("dataset"); - var markerDir = propertyHandler.getCacheDir().resolve("marker"); + datasetDir = propertyHandler.getCacheDir() + .resolve("dataset"); + var markerDir = propertyHandler.getCacheDir() + .resolve("marker"); if (!inited) { Files.createDirectories(datasetDir); Files.createDirectories(markerDir); - this.unfinishedWorkService.restartUnfinishedWork(markerDir, key); - this.unfinishedWorkService.cleanDatasetCache(datasetDir); + this.unfinishedWorkService + .restartUnfinishedWork(markerDir, key); + this.unfinishedWorkService + .cleanDatasetCache(datasetDir); } } @@ -136,47 +145,53 @@ private void init() { } } catch (Throwable e) { logger.error("Won't start ", e); - throw new RuntimeException("IdsService reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException("IdsService reports " + e.getClass() + + " " + e.getMessage()); } } - @PreDestroy private void exit() { this.fsm.exit(); logger.info("destroyed IdsService"); } - /** * Archive data specified by the investigationIds, datasetIds and * datafileIds specified along with a sessionId. If two level storage is not * in use this has no effect. * * @title archive - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds If present, a comma separated list of investigation id values - * @param datasetIds If present, a comma separated list of data set id values or - * null - * @param datafileIds If present, a comma separated list of datafile id values. + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds If present, a comma separated list of + * investigation id values + * @param datasetIds If present, a comma separated list of data set id + * values or null + * @param datafileIds If present, a comma separated list of datafile id + * values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException * @throws InternalException * @throws NotFoundException - * @throws DataNotOnlineException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("archive") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void archive(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, + public void archive(@Context HttpServletRequest request, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new ArchiveHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + var handler = new ArchiveHandler(request.getRemoteAddr(), sessionId, + investigationIds, datasetIds, datafileIds); handler.handle(); } @@ -185,11 +200,14 @@ public void archive(@Context HttpServletRequest request, @FormParam(RequestIdNam * specified along with a sessionId. * * @title delete - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds If present, a comma separated list of investigation id values - * @param datasetIds If present, a comma separated list of data set id values or - * null - * @param datafileIds If present, a comma separated list of datafile id values. + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds If present, a comma separated list of + * investigation id values + * @param datasetIds If present, a comma separated list of data set id + * values or null + * @param datafileIds If present, a comma separated list of datafile id + * values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException @@ -200,16 +218,20 @@ public void archive(@Context HttpServletRequest request, @FormParam(RequestIdNam */ @DELETE @Path("delete") - public void delete(@Context HttpServletRequest request, @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds) throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { - - var handler = new DeleteHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + public void delete(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, + InternalException, DataNotOnlineException { + + var handler = new DeleteHandler(request.getRemoteAddr(), sessionId, + investigationIds, datasetIds, datafileIds); handler.handle(); } - /** * Return the version of the server * @@ -222,7 +244,8 @@ public void delete(@Context HttpServletRequest request, @QueryParam(RequestIdNam public String getVersion() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen.writeStartObject().write("version", Constants.API_VERSION).writeEnd(); + gen.writeStartObject().write("version", Constants.API_VERSION) + .writeEnd(); gen.close(); return baos.toString(); } @@ -234,43 +257,58 @@ public String getVersion() { * set. If preparedId is set the compress and zip arguments are not used. * * @title getData - * @param preparedId A valid preparedId returned by a call to prepareData - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds A comma separated list of investigation id values. + * @param preparedId A valid preparedId returned by a call to + * prepareData + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds A comma separated list of investigation id + * values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of data file id values. - * @param compress If true use default compression otherwise no compression. This - * only applies if preparedId is not set and if the results are - * being zipped. - * @param zip If true the data should be zipped. If multiple files are - * requested (or could be because a datasetId or investigationId - * has been specified) the data are zipped regardless of the + * @param compress If true use default compression otherwise no + * compression. This only applies if preparedId is + * not set and if the results are being zipped. + * @param zip If true the data should be zipped. If multiple + * files are requested (or could be because a + * datasetId or investigationId has been specified) + * the data are zipped regardless of the * specification of this flag. * @param outname The file name to put in the returned header - * "ContentDisposition". If it does not end in .zip but it is a - * zip file then a ".zip" will be appended. - * @param range A range header which must match "bytes=(\\d+)-" to specify an - * offset i.e. to skip a number of bytes. + * "ContentDisposition". If it does not end in .zip + * but it is a zip file then a ".zip" will be + * appended. + * @param range A range header which must match "bytes=(\\d+)-" + * to specify an offset i.e. to skip a number of + * bytes. * @return a stream of json data. * @throws BadRequestException * @throws NotFoundException * @throws InternalException * @throws InsufficientPrivilegesException * @throws DataNotOnlineException - * @throws NotImplementedException + * @throws NotImplementedException * @statuscode 200 To indicate success */ @GET @Path("getData") @Produces(MediaType.APPLICATION_OCTET_STREAM) - public Response getData(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds, - @QueryParam("compress") boolean compress, @QueryParam("zip") boolean zip, - @QueryParam("outname") String outname, @HeaderParam("Range") String range) throws BadRequestException, - NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - - var handler = new GetDataHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds, compress, zip, outname, range); + public Response getData(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds, + @QueryParam("compress") boolean compress, + @QueryParam("zip") boolean zip, + @QueryParam("outname") String outname, + @HeaderParam("Range") String range) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, DataNotOnlineException, + NotImplementedException { + + var handler = new GetDataHandler(request.getRemoteAddr(), preparedId, + sessionId, investigationIds, datasetIds, datafileIds, compress, + zip, outname, range); return handler.handle().getResponse(); } @@ -281,9 +319,12 @@ public Response getData(@Context HttpServletRequest request, @QueryParam(Request * set. * * @title getDatafileIds - * @param preparedId A valid preparedId returned by a call to prepareData - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds A comma separated list of investigation id values. + * @param preparedId A valid preparedId returned by a call to + * prepareData + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds A comma separated list of investigation id + * values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of datafile id values. * @return a list of id values @@ -291,19 +332,26 @@ public Response getData(@Context HttpServletRequest request, @QueryParam(Request * @throws InternalException * @throws NotFoundException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getDatafileIds") @Produces(MediaType.APPLICATION_JSON) - public String getDatafileIds(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - - var handler = new GetDataFileIdsHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public String getDatafileIds(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, InternalException, NotFoundException, + InsufficientPrivilegesException, DataNotOnlineException, + NotImplementedException { + + var handler = new GetDataFileIdsHandler(request.getRemoteAddr(), + preparedId, sessionId, investigationIds, datasetIds, + datafileIds); return handler.handle().getString(); } @@ -313,18 +361,21 @@ public String getDatafileIds(@Context HttpServletRequest request, @QueryParam(Re * obtained. * * @return the url of the icat server - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("getIcatUrl") @Produces(MediaType.TEXT_PLAIN) - public String getIcatUrl(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public String getIcatUrl(@Context HttpServletRequest request) + throws InternalException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { var handler = new GetIcatUrlHandler(request.getRemoteAddr()); return handler.handle().getString(); @@ -335,24 +386,28 @@ public String getIcatUrl(@Context HttpServletRequest request) throws InternalExc * privileged to use this call. * * @title getServiceStatus - * @param sessionId A valid ICAT session ID of a user in the IDS rootUserNames - * set. + * @param sessionId A valid ICAT session ID of a user in the IDS + * rootUserNames set. * @return a json string. * @throws InternalException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws BadRequestException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws BadRequestException * @statuscode 200 To indicate success */ @GET @Path("getServiceStatus") @Produces(MediaType.APPLICATION_JSON) - public String getServiceStatus(@Context HttpServletRequest request, @QueryParam(RequestIdNames.sessionId) String sessionId) - throws InternalException, InsufficientPrivilegesException, BadRequestException, NotFoundException, DataNotOnlineException, NotImplementedException { - - var handler = new GetServiceStatusHandler(request.getRemoteAddr(), sessionId); + public String getServiceStatus(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.sessionId) String sessionId) + throws InternalException, InsufficientPrivilegesException, + BadRequestException, NotFoundException, DataNotOnlineException, + NotImplementedException { + + var handler = new GetServiceStatusHandler(request.getRemoteAddr(), + sessionId); return handler.handle().getString(); } @@ -361,41 +416,53 @@ public String getServiceStatus(@Context HttpServletRequest request, @QueryParam( * investigationIds, datasetIds and datafileIds along with a sessionId. * * @title getSize - * @param preparedId A valid preparedId returned by a call to prepareData - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds If present, a comma separated list of investigation id values - * @param datasetIds If present, a comma separated list of data set id values or - * null - * @param datafileIds If present, a comma separated list of data file id values. + * @param preparedId A valid preparedId returned by a call to + * prepareData + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds If present, a comma separated list of + * investigation id values + * @param datasetIds If present, a comma separated list of data set id + * values or null + * @param datafileIds If present, a comma separated list of data file + * id values. * @return the size in bytes * @throws BadRequestException * @throws NotFoundException * @throws InsufficientPrivilegesException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getSize") @Produces(MediaType.TEXT_PLAIN) - public long getSize(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { - + public long getSize(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, + DataNotOnlineException, NotImplementedException { var result = ValueContainer.getInvalid(); // trying fast computation - if(sessionId != null) { - var fastHandler = new GetSizeHandlerForFastProcessing(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + if (sessionId != null) { + var fastHandler = new GetSizeHandlerForFastProcessing( + request.getRemoteAddr(), sessionId, investigationIds, + datasetIds, datafileIds); result = fastHandler.handle(); } // otherwise normal computation - if(result.isInvalid()) { - var handler = new GetSizeHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); + if (result.isInvalid()) { + var handler = new GetSizeHandler(request.getRemoteAddr(), + preparedId, sessionId, investigationIds, datasetIds, + datafileIds); result = handler.handle(); } @@ -407,49 +474,61 @@ public long getSize(@Context HttpServletRequest request, @QueryParam(RequestIdNa * investigationIds, datasetIds and datafileIds along with a sessionId. * * @title getStatus - * @param preparedId A valid preparedId returned by a call to prepareData - * @param sessionId A sessionId returned by a call to the icat server. If the - * sessionId is omitted or null the ids reader account will be - * used which has read access to all data. - * @param investigationIds If present, a comma separated list of investigation id values - * @param datasetIds If present, a comma separated list of data set id values or - * null - * @param datafileIds If present, a comma separated list of data file id values. + * @param preparedId A valid preparedId returned by a call to + * prepareData + * @param sessionId A sessionId returned by a call to the icat + * server. If the sessionId is omitted or null the + * ids reader account will be used which has read + * access to all data. + * @param investigationIds If present, a comma separated list of + * investigation id values + * @param datasetIds If present, a comma separated list of data set id + * values or null + * @param datafileIds If present, a comma separated list of data file + * id values. * @return a string with "ONLINE" if all data are online, "RESTORING" if one - * or more files are in the process of being restored but none are - * archived and no restoration has been requested or "ARCHIVED" if - * one or more files are archived and and no restoration has been - * requested. + * or more files are in the process of being restored but none are + * archived and no restoration has been requested or "ARCHIVED" if + * one or more files are archived and and no restoration has been + * requested. * @throws BadRequestException * @throws NotFoundException * @throws InsufficientPrivilegesException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getStatus") @Produces(MediaType.TEXT_PLAIN) - public String getStatus(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { - - // special case for getStatus request: getting status is possible without authentification + public String getStatus(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, + DataNotOnlineException, NotImplementedException { + + // special case for getStatus request: getting status is possible + // without authentification if (sessionId == null && preparedId == null) { try { - sessionId = ServiceProvider.getInstance().getIcatReader().getSessionId(); + sessionId = ServiceProvider.getInstance().getIcatReader() + .getSessionId(); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage()); } } - var handler = new GetStatusHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new GetStatusHandler(request.getRemoteAddr(), preparedId, + sessionId, investigationIds, datasetIds, datafileIds); return handler.handle().getString(); } - /** * Returns true if all the data files are ready to be downloaded. As a side * effect, if any data files are archived and no restoration has been @@ -467,39 +546,46 @@ public String getStatus(@Context HttpServletRequest request, @QueryParam(Request * @throws BadRequestException * @throws NotFoundException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws InsufficientPrivilegesException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws InsufficientPrivilegesException * @statuscode 200 To indicate success */ @GET @Path("isPrepared") @Produces(MediaType.TEXT_PLAIN) - public boolean isPrepared(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - - var handler = new IsPreparedHandler(request.getRemoteAddr(), preparedId); + public boolean isPrepared(@Context HttpServletRequest request, + @QueryParam(RequestIdNames.preparedId) String preparedId) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, DataNotOnlineException, + NotImplementedException { + + var handler = new IsPreparedHandler(request.getRemoteAddr(), + preparedId); return handler.handle().getBool(); } /** - * An ids server can be configured to be read only. This returns thenew DfProcessQueue() - * readOnly status of the server. + * An ids server can be configured to be read only. This returns thenew + * DfProcessQueue() readOnly status of the server. * * @title isReadOnly * @return true if readonly, else false - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("isReadOnly") @Produces(MediaType.TEXT_PLAIN) - public boolean isReadOnly(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public boolean isReadOnly(@Context HttpServletRequest request) + throws InternalException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { var handler = new IsReadOnlyHandler(request.getRemoteAddr()); return handler.handle().getBool(); @@ -511,18 +597,21 @@ public boolean isReadOnly(@Context HttpServletRequest request) throws InternalEx * * @title isTwoLevel * @return true if twoLevel, else false - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("isTwoLevel") @Produces(MediaType.TEXT_PLAIN) - public boolean isTwoLevel(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public boolean isTwoLevel(@Context HttpServletRequest request) + throws InternalException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { var handler = new IsTwoLevelHandler(request.getRemoteAddr()); return handler.handle().getBool(); @@ -551,37 +640,45 @@ public String ping() { * omitted, along with a sessionId. * * @title prepareData - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds A comma separated list of investigation id values. + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds A comma separated list of investigation id + * values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of datafile id values. - * @param compress If true use default compression otherwise no compression. This - * only applies if preparedId is not set and if the results are - * being zipped. - * @param zip If true the data should be zipped. If multiple files are - * requested (or could be because a datasetId or investigationId - * has been specified) the data are zipped regardless of the + * @param compress If true use default compression otherwise no + * compression. This only applies if preparedId is + * not set and if the results are being zipped. + * @param zip If true the data should be zipped. If multiple + * files are requested (or could be because a + * datasetId or investigationId has been specified) + * the data are zipped regardless of the * specification of this flag. * @return a string with the preparedId * @throws BadRequestException * @throws InsufficientPrivilegesException * @throws NotFoundException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("prepareData") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.TEXT_PLAIN) - public String prepareData(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds, @FormParam("compress") boolean compress, - @FormParam("zip") boolean zip) - throws BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, NotImplementedException, DataNotOnlineException { - - var handler = new PrepareDataHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds, compress, zip); + public String prepareData(@Context HttpServletRequest request, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds, + @FormParam("compress") boolean compress, + @FormParam("zip") boolean zip) throws BadRequestException, + InsufficientPrivilegesException, NotFoundException, + InternalException, NotImplementedException, DataNotOnlineException { + + var handler = new PrepareDataHandler(request.getRemoteAddr(), sessionId, + investigationIds, datasetIds, datafileIds, compress, zip); return handler.handle().getString(); } @@ -590,17 +687,23 @@ public String prepareData(@Context HttpServletRequest request, @FormParam(Reques * * @title put * @param body The contents of the file to be stored - * @param sessionId A sessionId returned by a call to the icat server. + * @param sessionId A sessionId returned by a call to the icat + * server. * @param name A name to assign to the data file - * @param datafileFormatId The id of the data file format to associate with the data file - * @param datasetId The id of the data set to which the data file should be - * associated. - * @param description An optional description to associate with the data file - * @param doi An optional description to associate with the data file - * @param datafileCreateTime An optional datafileCreateTime to associate with the data file - * @param datafileModTime An optional datafileModTime to associate with the data file + * @param datafileFormatId The id of the data file format to associate + * with the data file + * @param datasetId The id of the data set to which the data file + * should be associated. + * @param description An optional description to associate with the + * data file + * @param doi An optional description to associate with the + * data file + * @param datafileCreateTime An optional datafileCreateTime to associate + * with the data file + * @param datafileModTime An optional datafileModTime to associate with + * the data file * @return a json object with attributes of "id", "checksum", "location" and - * "size"; + * "size"; * @throws BadRequestException * @throws NotFoundException * @throws InternalException @@ -614,15 +717,21 @@ public String prepareData(@Context HttpServletRequest request, @FormParam(Reques @Consumes(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.APPLICATION_JSON) public Response put(@Context HttpServletRequest request, InputStream body, - @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("name") String name, - @QueryParam("datafileFormatId") String datafileFormatId, @QueryParam("datasetId") String datasetId, - @QueryParam("description") String description, @QueryParam("doi") String doi, - @QueryParam("datafileCreateTime") String datafileCreateTime, - @QueryParam("datafileModTime") String datafileModTime) throws BadRequestException, NotFoundException, - InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { - - var handler = new PutHandler(request.getRemoteAddr(), sessionId, body, name, datafileFormatId, datasetId, - description, doi, datafileCreateTime, datafileModTime, false, false); + @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("name") String name, + @QueryParam("datafileFormatId") String datafileFormatId, + @QueryParam("datasetId") String datasetId, + @QueryParam("description") String description, + @QueryParam("doi") String doi, + @QueryParam("datafileCreateTime") String datafileCreateTime, + @QueryParam("datafileModTime") String datafileModTime) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, NotImplementedException, + DataNotOnlineException { + + var handler = new PutHandler(request.getRemoteAddr(), sessionId, body, + name, datafileFormatId, datasetId, description, doi, + datafileCreateTime, datafileModTime, false, false); return handler.handle().getResponse(); } @@ -636,7 +745,7 @@ public Response put(@Context HttpServletRequest request, InputStream body, * @title putAsPost * @param request * @return a json object with attributes of "id", "checksum", "location" and - * "size"; + * "size"; * @throws BadRequestException * @throws NotFoundException * @throws InternalException @@ -649,65 +758,73 @@ public Response put(@Context HttpServletRequest request, InputStream body, @Path("put") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) - public Response putAsPost(@Context HttpServletRequest request) throws BadRequestException, NotFoundException, - InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { - try { - String sessionId = null; - String name = null; - String datafileFormatId = null; - String datasetId = null; - String description = null; - String doi = null; - String datafileCreateTime = null; - String datafileModTime = null; - Response result = null; - boolean wrap = false; - boolean padding = false; - - // Parse the request - for (Part part : request.getParts()) { - String fieldName = part.getName(); - InputStream stream = part.getInputStream(); - if (part.getSubmittedFileName() == null) { - String value = new String(stream.readAllBytes(), StandardCharsets.UTF_8); - if (fieldName.equals("sessionId")) { - sessionId = value; - } else if (fieldName.equals("name")) { - name = value; - } else if (fieldName.equals("datafileFormatId")) { - datafileFormatId = value; - } else if (fieldName.equals("datasetId")) { - datasetId = value; - } else if (fieldName.equals("description")) { - description = value; - } else if (fieldName.equals("doi")) { - doi = value; - } else if (fieldName.equals("datafileCreateTime")) { - datafileCreateTime = value; - } else if (fieldName.equals("datafileModTime")) { - datafileModTime = value; - } else if (fieldName.equals("wrap")) { - wrap = (value != null && value.toUpperCase().equals("TRUE")); - } else if (fieldName.equals("padding")) { - padding = (value != null && value.toUpperCase().equals("TRUE")); - } else { - throw new BadRequestException("Form field " + fieldName + "is not recognised"); - } - } else { - if (name == null) { - name = part.getSubmittedFileName(); - } - var handler = new PutHandler(request.getRemoteAddr(), sessionId, stream, name, datafileFormatId, datasetId, description, - doi, datafileCreateTime, datafileModTime, wrap, padding); - result = handler.handle().getResponse(); - } + public Response putAsPost(@Context HttpServletRequest request) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, NotImplementedException, + DataNotOnlineException { + try { + String sessionId = null; + String name = null; + String datafileFormatId = null; + String datasetId = null; + String description = null; + String doi = null; + String datafileCreateTime = null; + String datafileModTime = null; + Response result = null; + boolean wrap = false; + boolean padding = false; + + // Parse the request + for (Part part : request.getParts()) { + String fieldName = part.getName(); + InputStream stream = part.getInputStream(); + if (part.getSubmittedFileName() == null) { + String value = new String(stream.readAllBytes(), + StandardCharsets.UTF_8); + if (fieldName.equals("sessionId")) { + sessionId = value; + } else if (fieldName.equals("name")) { + name = value; + } else if (fieldName.equals("datafileFormatId")) { + datafileFormatId = value; + } else if (fieldName.equals("datasetId")) { + datasetId = value; + } else if (fieldName.equals("description")) { + description = value; + } else if (fieldName.equals("doi")) { + doi = value; + } else if (fieldName.equals("datafileCreateTime")) { + datafileCreateTime = value; + } else if (fieldName.equals("datafileModTime")) { + datafileModTime = value; + } else if (fieldName.equals("wrap")) { + wrap = (value != null + && value.toUpperCase().equals("TRUE")); + } else if (fieldName.equals("padding")) { + padding = (value != null + && value.toUpperCase().equals("TRUE")); + } else { + throw new BadRequestException("Form field " + fieldName + + "is not recognised"); + } + } else { + if (name == null) { + name = part.getSubmittedFileName(); } - return result; - } catch (IOException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); - } catch (ServletException e) { - throw new BadRequestException("Multipart content expected"); + var handler = new PutHandler(request.getRemoteAddr(), + sessionId, stream, name, datafileFormatId, + datasetId, description, doi, datafileCreateTime, + datafileModTime, wrap, padding); + result = handler.handle().getResponse(); } + } + return result; + } catch (IOException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (ServletException e) { + throw new BadRequestException("Multipart content expected"); + } } /** @@ -722,9 +839,12 @@ public Response putAsPost(@Context HttpServletRequest request) throws BadRequest * specified then investigationIds, datasetIds and datafileIds are not used. * * @title reset - * @param preparedId A valid preparedId returned by a call to prepareData - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds A comma separated list of investigation id values. + * @param preparedId A valid preparedId returned by a call to + * prepareData + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds A comma separated list of investigation id + * values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of data file id values. * @return a stream of json data. @@ -732,18 +852,24 @@ public Response putAsPost(@Context HttpServletRequest request) throws BadRequest * @throws NotFoundException * @throws InternalException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("reset") - public void reset(@Context HttpServletRequest request, @FormParam(RequestIdNames.preparedId) String preparedId, - @FormParam(RequestIdNames.sessionId) String sessionId, @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, @FormParam("datafileIds") String datafileIds) - throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { - - var handler = new ResetHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public void reset(@Context HttpServletRequest request, + @FormParam(RequestIdNames.preparedId) String preparedId, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws BadRequestException, InternalException, NotFoundException, + InsufficientPrivilegesException, DataNotOnlineException, + NotImplementedException { + + var handler = new ResetHandler(request.getRemoteAddr(), preparedId, + sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } @@ -754,45 +880,53 @@ public void reset(@Context HttpServletRequest request, @FormParam(RequestIdNames * in use this has no effect. * * @title restore - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds If present, a comma separated list of investigation id values - * @param datasetIds If present, a comma separated list of data set id values or - * null - * @param datafileIds If present, a comma separated list of datafile id values. + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds If present, a comma separated list of + * investigation id values + * @param datasetIds If present, a comma separated list of data set id + * values or null + * @param datafileIds If present, a comma separated list of datafile id + * values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException * @throws InternalException * @throws NotFoundException - * @throws DataNotOnlineException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("restore") @Consumes("application/x-www-form-urlencoded") - public void restore(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, + public void restore(@Context HttpServletRequest request, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new RestoreHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + var handler = new RestoreHandler(request.getRemoteAddr(), sessionId, + investigationIds, datasetIds, datafileIds); handler.handle(); } - /** - * Write data specified by the investigationIds, datasetIds - * and datafileIds specified along with a sessionId to archive - * storage. If two level storage is not in use this has no - * effect. + * Write data specified by the investigationIds, datasetIds and datafileIds + * specified along with a sessionId to archive storage. If two level storage + * is not in use this has no effect. * * @title write - * @param sessionId A sessionId returned by a call to the icat server. - * @param investigationIds If present, a comma separated list of investigation id values - * @param datasetIds If present, a comma separated list of data set id values or - * null - * @param datafileIds If present, a comma separated list of datafile id values. + * @param sessionId A sessionId returned by a call to the icat + * server. + * @param investigationIds If present, a comma separated list of + * investigation id values + * @param datasetIds If present, a comma separated list of data set id + * values or null + * @param datafileIds If present, a comma separated list of datafile id + * values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException @@ -803,13 +937,17 @@ public void restore(@Context HttpServletRequest request, @FormParam(RequestIdNam @POST @Path("write") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void write(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, + public void write(@Context HttpServletRequest request, + @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new WriteHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); + var handler = new WriteHandler(request.getRemoteAddr(), sessionId, + investigationIds, datasetIds, datafileIds); handler.handle(); } } diff --git a/src/main/java/org/icatproject/ids/Tidier.java b/src/main/java/org/icatproject/ids/Tidier.java index 9d647de0..d9efa850 100644 --- a/src/main/java/org/icatproject/ids/Tidier.java +++ b/src/main/java/org/icatproject/ids/Tidier.java @@ -24,6 +24,7 @@ import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.exceptions.InsufficientPrivilegesException; @@ -55,57 +56,77 @@ public void run() { if (twoLevel) { if (storageUnit == StorageUnit.DATASET) { - List dsInfos = mainStorage.getDatasetsToArchive(stopArchivingLevel, - startArchivingLevel); + List dsInfos = mainStorage.getDatasetsToArchive( + stopArchivingLevel, startArchivingLevel); for (DsInfo dsInfo : dsInfos) { StringBuilder sb = new StringBuilder( "SELECT ds FROM Dataset ds, ds.investigation inv, inv.facility fac WHERE"); boolean andNeeded = false; - andNeeded = addNumericConstraint(sb, "ds.id", dsInfo.getDsId(), andNeeded); - andNeeded = addStringConstraint(sb, "ds.location", dsInfo.getDsLocation(), andNeeded); - andNeeded = addStringConstraint(sb, "ds.name", dsInfo.getDsName(), andNeeded); - - andNeeded = addNumericConstraint(sb, "inv.id", dsInfo.getInvId(), andNeeded); - andNeeded = addStringConstraint(sb, "inv.name", dsInfo.getInvName(), andNeeded); - andNeeded = addStringConstraint(sb, "inv.visitId", dsInfo.getVisitId(), andNeeded); - - andNeeded = addStringConstraint(sb, "fac.name", dsInfo.getFacilityName(), andNeeded); - andNeeded = addNumericConstraint(sb, "fac.id", dsInfo.getFacilityId(), andNeeded); + andNeeded = addNumericConstraint(sb, "ds.id", + dsInfo.getDsId(), andNeeded); + andNeeded = addStringConstraint(sb, "ds.location", + dsInfo.getDsLocation(), andNeeded); + andNeeded = addStringConstraint(sb, "ds.name", + dsInfo.getDsName(), andNeeded); + + andNeeded = addNumericConstraint(sb, "inv.id", + dsInfo.getInvId(), andNeeded); + andNeeded = addStringConstraint(sb, "inv.name", + dsInfo.getInvName(), andNeeded); + andNeeded = addStringConstraint(sb, "inv.visitId", + dsInfo.getVisitId(), andNeeded); + + andNeeded = addStringConstraint(sb, "fac.name", + dsInfo.getFacilityName(), andNeeded); + andNeeded = addNumericConstraint(sb, "fac.id", + dsInfo.getFacilityId(), andNeeded); sb.append(" INCLUDE ds.investigation.facility"); try { int low = 0; while (true) { - String query = sb.toString() + " LIMIT " + low + "," + tidyBlockSize; + String query = sb.toString() + " LIMIT " + + low + "," + tidyBlockSize; List os = reader.search(query); - logger.debug(query + " returns " + os.size() + " datasets"); + logger.debug(query + " returns " + os.size() + + " datasets"); for (Object o : os) { - DatasetInfo dsInfoImpl = new DatasetInfo((Dataset) o); - logger.debug( - "Requesting archive of " + dsInfoImpl + " to recover main storage"); - fsm.queue(dsInfoImpl, DeferredOp.ARCHIVE); + DatasetInfo dsInfoImpl = new DatasetInfo( + (Dataset) o); + logger.debug("Requesting archive of " + + dsInfoImpl + + " to recover main storage"); + fsm.queue(dsInfoImpl, + DeferredOp.ARCHIVE); } if (os.size() < tidyBlockSize) { break; } low += tidyBlockSize; } - } catch (InternalException | IcatException_Exception | InsufficientPrivilegesException e) { + } catch (InternalException | IcatException_Exception + | InsufficientPrivilegesException e) { // Log it and carry on - logger.error(e.getClass() + " " + e.getMessage()); + logger.error( + e.getClass() + " " + e.getMessage()); } } } else if (storageUnit == StorageUnit.DATAFILE) { - List dfInfos = mainStorage.getDatafilesToArchive(stopArchivingLevel, - startArchivingLevel); + List dfInfos = mainStorage + .getDatafilesToArchive(stopArchivingLevel, + startArchivingLevel); for (DfInfo dfInfo : dfInfos) { - StringBuilder sb = new StringBuilder("SELECT df FROM Datafile df WHERE"); + StringBuilder sb = new StringBuilder( + "SELECT df FROM Datafile df WHERE"); boolean andNeeded = false; - andNeeded = addNumericConstraint(sb, "df.id", dfInfo.getDfId(), andNeeded); - andNeeded = addStringConstraint(sb, "df.createId", dfInfo.getCreateId(), andNeeded); - andNeeded = addStringConstraint(sb, "df.modId", dfInfo.getModId(), andNeeded); + andNeeded = addNumericConstraint(sb, "df.id", + dfInfo.getDfId(), andNeeded); + andNeeded = addStringConstraint(sb, "df.createId", + dfInfo.getCreateId(), andNeeded); + andNeeded = addStringConstraint(sb, "df.modId", + dfInfo.getModId(), andNeeded); if (key != null) { if (dfInfo.getDfLocation() != null) { if (andNeeded) { @@ -114,42 +135,54 @@ public void run() { sb.append(" "); andNeeded = true; } - sb.append("df.location" + " LIKE '" + dfInfo.getDfLocation() + " %'"); + sb.append("df.location" + " LIKE '" + + dfInfo.getDfLocation() + " %'"); } } else { - andNeeded = addStringConstraint(sb, "df.location", + andNeeded = addStringConstraint(sb, + "df.location", dfInfo.getDfLocation(), andNeeded); } - andNeeded = addStringConstraint(sb, "df.name", dfInfo.getDfName(), andNeeded); + andNeeded = addStringConstraint(sb, "df.name", + dfInfo.getDfName(), andNeeded); sb.append(" INCLUDE df.dataset"); try { int low = 0; while (true) { - String query = sb.toString() + " LIMIT " + low + "," + tidyBlockSize; + String query = sb.toString() + " LIMIT " + + low + "," + tidyBlockSize; List os = reader.search(query); - logger.debug(query + " returns " + os.size() + " datafiles"); + logger.debug(query + " returns " + os.size() + + " datafiles"); for (Object o : os) { Datafile df = (Datafile) o; - DatafileInfo dfInfoImpl = new DatafileInfo(df.getId(), df.getName(), - - LocationHelper.getLocation(df.getId(), df.getLocation()), df.getCreateId(), - df.getModId(), df.getDataset().getId()); - - - logger.debug( - "Requesting archive of " + dfInfoImpl + " to recover main storage"); - fsm.queue(dfInfoImpl, DeferredOp.ARCHIVE); + DatafileInfo dfInfoImpl = new DatafileInfo( + df.getId(), df.getName(), + + LocationHelper.getLocation( + df.getId(), + df.getLocation()), + df.getCreateId(), df.getModId(), + df.getDataset().getId()); + + logger.debug("Requesting archive of " + + dfInfoImpl + + " to recover main storage"); + fsm.queue(dfInfoImpl, + DeferredOp.ARCHIVE); } if (os.size() < tidyBlockSize) { break; } low += tidyBlockSize; } - } catch (InternalException | IcatException_Exception | InsufficientPrivilegesException e) { + } catch (InternalException | IcatException_Exception + | InsufficientPrivilegesException e) { // Log it and carry on - logger.error(e.getClass() + " " + e.getMessage()); + logger.error( + e.getClass() + " " + e.getMessage()); } } } @@ -165,7 +198,8 @@ public void run() { private final static Logger logger = LoggerFactory.getLogger(Tidier.class); - static boolean addStringConstraint(StringBuilder sb, String var, String value, boolean andNeeded) { + static boolean addStringConstraint(StringBuilder sb, String var, + String value, boolean andNeeded) { if (value != null) { if (andNeeded) { sb.append(" AND "); @@ -178,7 +212,8 @@ static boolean addStringConstraint(StringBuilder sb, String var, String value, b return andNeeded; } - static boolean addNumericConstraint(StringBuilder sb, String var, Long value, boolean andNeeded) { + static boolean addNumericConstraint(StringBuilder sb, String var, + Long value, boolean andNeeded) { if (value != null) { if (andNeeded) { sb.append(" AND "); @@ -191,7 +226,8 @@ static boolean addNumericConstraint(StringBuilder sb, String var, Long value, bo return andNeeded; } - static void cleanPreparedDir(Path preparedDir, int preparedCount) throws IOException { + static void cleanPreparedDir(Path preparedDir, int preparedCount) + throws IOException { Map dateMap = new HashMap<>(); File[] files = preparedDir.toFile().listFiles(); @@ -251,11 +287,13 @@ public void init() { try { PropertyHandler propertyHandler = PropertyHandler.getInstance(); - FiniteStateMachine.createInstance(reader, lockManager, propertyHandler.getStorageUnit()); + FiniteStateMachine.createInstance(reader, lockManager, + propertyHandler.getStorageUnit()); this.fsm = FiniteStateMachine.getInstance(); - this.fsm.init(); //if not yet initialized by IdsService do it now - - sizeCheckIntervalMillis = propertyHandler.getSizeCheckIntervalMillis(); + this.fsm.init(); // if not yet initialized by IdsService do it now + + sizeCheckIntervalMillis = propertyHandler + .getSizeCheckIntervalMillis(); preparedCount = propertyHandler.getPreparedCount(); preparedDir = propertyHandler.getCacheDir().resolve("prepared"); Files.createDirectories(preparedDir); @@ -273,7 +311,8 @@ public void init() { logger.info("Tidier started"); } catch (IOException e) { - throw new RuntimeException("Tidier reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException( + "Tidier reports " + e.getClass() + " " + e.getMessage()); } } diff --git a/src/main/java/org/icatproject/ids/enums/RequestType.java b/src/main/java/org/icatproject/ids/enums/RequestType.java index a79f83e5..1f4c0b77 100644 --- a/src/main/java/org/icatproject/ids/enums/RequestType.java +++ b/src/main/java/org/icatproject/ids/enums/RequestType.java @@ -4,20 +4,7 @@ * This enum contains all defined types of requests to this server */ public enum RequestType { - GETDATA, - ARCHIVE, - GETICATURL, - GETDATAFILEIDS, - GETSERVICESTATUS, - GETSIZE, - GETSTATUS, - ISPREPARED, - ISREADONLY, - ISTWOLEVEL, - PREPAREDATA, - PUT, - RESET, - RESTORE, - WRITE, - DELETE + GETDATA, ARCHIVE, GETICATURL, GETDATAFILEIDS, GETSERVICESTATUS, GETSIZE, + GETSTATUS, ISPREPARED, ISREADONLY, ISTWOLEVEL, PREPAREDATA, PUT, RESET, + RESTORE, WRITE, DELETE } diff --git a/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java index 77920041..0c9567db 100644 --- a/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java @@ -9,9 +9,9 @@ import jakarta.ws.rs.ext.Provider; /** - * Capture any {@link org.icatproject.ids.exceptions.IdsException WebServiceException} thrown from - * {@link org.icatproject.ids.IdsService WebService} and generate the appropriate response code and - * message. + * Capture any {@link org.icatproject.ids.exceptions.IdsException + * WebServiceException} thrown from {@link org.icatproject.ids.IdsService + * WebService} and generate the appropriate response code and message. */ @Provider public class IdsExceptionMapper implements ExceptionMapper { @@ -23,6 +23,7 @@ public Response toResponse(IdsException e) { gen.writeStartObject().write("code", e.getClass().getSimpleName()) .write("message", e.getShortMessage()); gen.writeEnd().close(); - return Response.status(e.getHttpStatusCode()).entity(baos.toString()).build(); + return Response.status(e.getHttpStatusCode()).entity(baos.toString()) + .build(); } } diff --git a/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java index f9157c92..266b2e8a 100644 --- a/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java @@ -13,9 +13,11 @@ import org.slf4j.LoggerFactory; @Provider -public class NotFoundExceptionMapper implements ExceptionMapper { +public class NotFoundExceptionMapper + implements ExceptionMapper { - private static Logger logger = LoggerFactory.getLogger(NotFoundExceptionMapper.class); + private static Logger logger = LoggerFactory + .getLogger(NotFoundExceptionMapper.class); @Override public Response toResponse(NotFoundException e) { @@ -23,7 +25,10 @@ public Response toResponse(NotFoundException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); gen.writeStartObject().write("code", "NOT_IMPLEMENTED") - .write("message", "Operation not implemented by this IDS server.").writeEnd().close(); - return Response.status(Response.Status.NOT_IMPLEMENTED).entity(baos.toString()).build(); + .write("message", + "Operation not implemented by this IDS server.") + .writeEnd().close(); + return Response.status(Response.Status.NOT_IMPLEMENTED) + .entity(baos.toString()).build(); } } diff --git a/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java index aa38ba0a..55249b31 100644 --- a/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java @@ -14,9 +14,11 @@ import org.slf4j.LoggerFactory; @Provider -public class RuntimeExceptionMapper implements ExceptionMapper { +public class RuntimeExceptionMapper + implements ExceptionMapper { - private final static Logger logger = LoggerFactory.getLogger(RuntimeExceptionMapper.class); + private final static Logger logger = LoggerFactory + .getLogger(RuntimeExceptionMapper.class); @Override public Response toResponse(RuntimeException e) { @@ -27,8 +29,9 @@ public Response toResponse(RuntimeException e) { baos.reset(); JsonGenerator gen = Json.createGenerator(baos); gen.writeStartObject().write("code", "InternalException") - .write("message", e.getClass() + " " + e.getMessage()).writeEnd().close(); - return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR).entity(baos.toString()) - .build(); + .write("message", e.getClass() + " " + e.getMessage()) + .writeEnd().close(); + return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR) + .entity(baos.toString()).build(); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java index f115c8da..78a88ae5 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java @@ -18,18 +18,18 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.IcatReader; import org.icatproject.ids.services.LockManager; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.LockInfo; +import org.icatproject.ids.services.PropertyHandler; public abstract class FiniteStateMachine { - private static FiniteStateMachine instance; private static Boolean inited = false; @@ -39,40 +39,52 @@ protected FiniteStateMachine(IcatReader reader, LockManager lockManager) { this.lockManager = lockManager; } - public static void createInstance(IcatReader reader, LockManager lockManager, StorageUnit storageUnit) { + public static void createInstance(IcatReader reader, + LockManager lockManager, StorageUnit storageUnit) { - if(instance == null) { - if(storageUnit == StorageUnit.DATAFILE) - instance = new FiniteStateMachineForStorageUnitDatafile(reader, lockManager); - else if(storageUnit == StorageUnit.DATASET) - instance = new FiniteStateMachineForStorageUnitDataset(reader, lockManager); + if (instance == null) { + if (storageUnit == StorageUnit.DATAFILE) + instance = new FiniteStateMachineForStorageUnitDatafile(reader, + lockManager); + else if (storageUnit == StorageUnit.DATASET) + instance = new FiniteStateMachineForStorageUnitDataset(reader, + lockManager); else - instance = new FiniteStateMachineForSingleLevelStorage(reader, lockManager); + instance = new FiniteStateMachineForSingleLevelStorage(reader, + lockManager); } } public static FiniteStateMachine getInstance() { - if(instance != null) { + if (instance != null) { return instance; } - // If this assert was executed: Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once. - throw new RuntimeException("Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once."); + // If this assert was executed: Instance of FiniteStateMachine is not + // created. At First createInstance() has to be called at least once. + throw new RuntimeException( + "Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once."); } - public abstract void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException; + public abstract void queue(DataInfoBase dataInfo, DeferredOp deferredOp) + throws InternalException; + protected abstract void scheduleTimer(); + protected abstract void addDataInfoJson(JsonGenerator gen); public enum RequestedState { - ARCHIVE_REQUESTED, DELETE_REQUESTED, RESTORE_REQUESTED, WRITE_REQUESTED, WRITE_THEN_ARCHIVE_REQUESTED + ARCHIVE_REQUESTED, DELETE_REQUESTED, RESTORE_REQUESTED, WRITE_REQUESTED, + WRITE_THEN_ARCHIVE_REQUESTED } - protected static Logger logger = LoggerFactory.getLogger(FiniteStateMachine.class); + protected static Logger logger = LoggerFactory + .getLogger(FiniteStateMachine.class); /* - * Note that the veriable processOpsDelayMillis is used to either delay all deferred - * datafile operations or to delay dataset writes, depending on the setting of storageUnit. + * Note that the veriable processOpsDelayMillis is used to either delay all + * deferred datafile operations or to delay dataset writes, depending on the + * setting of storageUnit. */ protected long processOpsDelayMillis; @@ -119,7 +131,8 @@ public Set getMaybeOffline() { } /** - * Find any DataFileInfo which are being restored or are queued for restoration + * Find any DataFileInfo which are being restored or are queued for + * restoration */ public Set getRestoring() { Map union; @@ -145,23 +158,26 @@ protected void addDataInfoJsonFromDeferredOpsQueue(JsonGenerator gen) { gen.writeStartArray("opsQueue"); for (Entry entry : union.entrySet()) { DataInfoBase item = entry.getKey(); - gen.writeStartObject().write("data", item.toString()).write("request", entry.getValue().name()) - .writeEnd(); + gen.writeStartObject().write("data", item.toString()) + .write("request", entry.getValue().name()).writeEnd(); } gen.writeEnd(); // end Array("opsQueue") } public String getServiceStatus() throws InternalException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { - + try (JsonGenerator gen = Json.createGenerator(baos) + .writeStartObject()) { + this.addDataInfoJson(gen); Collection lockInfo = lockManager.getLockInfo(); gen.write("lockCount", lockInfo.size()); gen.writeStartArray("locks"); for (LockInfo li : lockInfo) { - gen.writeStartObject().write("id", li.id).write("type", li.type.name()).write("count", li.count).writeEnd(); + gen.writeStartObject().write("id", li.id) + .write("type", li.type.name()).write("count", li.count) + .writeEnd(); } gen.writeEnd(); // end Array("locks") @@ -176,24 +192,25 @@ public String getServiceStatus() throws InternalException { return baos.toString(); } - public void init() { try { synchronized (inited) { - if(!inited) { + if (!inited) { propertyHandler = PropertyHandler.getInstance(); - processQueueIntervalMillis = propertyHandler.getProcessQueueIntervalSeconds() * 1000L; + processQueueIntervalMillis = propertyHandler + .getProcessQueueIntervalSeconds() * 1000L; this.scheduleTimer(); - + markerDir = propertyHandler.getCacheDir().resolve("marker"); Files.createDirectories(markerDir); inited = true; } } } catch (IOException e) { - throw new RuntimeException("FiniteStateMachine reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException("FiniteStateMachine reports " + + e.getClass() + " " + e.getMessage()); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java index 7ffb349d..bfa85337 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java @@ -1,32 +1,33 @@ package org.icatproject.ids.finiteStateMachine; +import jakarta.json.stream.JsonGenerator; + import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.IcatReader; import org.icatproject.ids.services.LockManager; -import jakarta.json.stream.JsonGenerator; - -public class FiniteStateMachineForSingleLevelStorage extends FiniteStateMachine { +public class FiniteStateMachineForSingleLevelStorage + extends FiniteStateMachine { - protected FiniteStateMachineForSingleLevelStorage(IcatReader reader, LockManager lockManager) { + protected FiniteStateMachineForSingleLevelStorage(IcatReader reader, + LockManager lockManager) { super(reader, lockManager); } - @Override - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { - throw new InternalException("### Operation is not permitted for single level storage"); + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) + throws InternalException { + throw new InternalException( + "### Operation is not permitted for single level storage"); } - @Override protected void scheduleTimer() { - //nothing to do here for single level storage + // nothing to do here for single level storage } - @Override protected void addDataInfoJson(JsonGenerator gen) { gen.writeStartArray("opsQueue").writeEnd(); diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java index 88543bb1..29113729 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java @@ -11,14 +11,16 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; - import java.util.TimerTask; +import jakarta.json.stream.JsonGenerator; + import org.icatproject.Dataset; + import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.exceptions.InternalException; -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.services.IcatReader; @@ -30,40 +32,43 @@ import org.icatproject.ids.thread.DfRestorer; import org.icatproject.ids.thread.DfWriter; -import jakarta.json.stream.JsonGenerator; - -public class FiniteStateMachineForStorageUnitDatafile extends FiniteStateMachine { +public class FiniteStateMachineForStorageUnitDatafile + extends FiniteStateMachine { - protected FiniteStateMachineForStorageUnitDatafile(IcatReader icatReader, LockManager lockManager) { + protected FiniteStateMachineForStorageUnitDatafile(IcatReader icatReader, + LockManager lockManager) { super(icatReader, lockManager); } - @Override protected void scheduleTimer() { - processOpsDelayMillis = propertyHandler.getDelayDatafileOperations() * 1000L; + processOpsDelayMillis = propertyHandler.getDelayDatafileOperations() + * 1000L; timer.schedule(new DfProcessQueue(), processQueueIntervalMillis); - logger.info("DfProcessQueue scheduled to run in " + processQueueIntervalMillis + " milliseconds"); + logger.info("DfProcessQueue scheduled to run in " + + processQueueIntervalMillis + " milliseconds"); } - @Override protected void addDataInfoJson(JsonGenerator gen) { this.addDataInfoJsonFromDeferredOpsQueue(gen); } - - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) + throws InternalException { var dfInfo = (DatafileInfo) dataInfo; - if(dfInfo == null) throw new InternalException("DataInfoBase object could not be casted into a DataFileInfo. Did you handed over a DataSetInfo instead?"); + if (dfInfo == null) + throw new InternalException( + "DataInfoBase object could not be casted into a DataFileInfo. Did you handed over a DataSetInfo instead?"); logger.info("Requesting " + deferredOp + " of datafile " + dfInfo); synchronized (deferredOpsQueue) { if (processOpsTime == null) { - processOpsTime = System.currentTimeMillis() + processOpsDelayMillis; + processOpsTime = System.currentTimeMillis() + + processOpsDelayMillis; final Date d = new Date(processOpsTime); logger.debug("Requesting delay operations till " + d); } @@ -72,62 +77,75 @@ public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalE if (state == null) { if (deferredOp == DeferredOp.WRITE) { try { - Path marker = markerDir.resolve(Long.toString(dfInfo.getId())); + Path marker = markerDir + .resolve(Long.toString(dfInfo.getId())); Files.createFile(marker); logger.debug("Created marker " + marker); } catch (FileAlreadyExistsException e) { // Pass will ignore this } catch (IOException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } - deferredOpsQueue.put(dfInfo, RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.WRITE_REQUESTED); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dfInfo, RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.RESTORE_REQUESTED); } else if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.DELETE_REQUESTED); } } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.RESTORE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.DELETE_REQUESTED); } } else if (state == RequestedState.DELETE_REQUESTED) { // No way out } else if (state == RequestedState.RESTORE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.DELETE_REQUESTED); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dfInfo, RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put(dfInfo, + RequestedState.WRITE_REQUESTED); } } } } - private class DfProcessQueue extends TimerTask { @Override public void run() { try { synchronized (deferredOpsQueue) { - if (processOpsTime != null && System.currentTimeMillis() > processOpsTime && !deferredOpsQueue.isEmpty()) { + if (processOpsTime != null + && System.currentTimeMillis() > processOpsTime + && !deferredOpsQueue.isEmpty()) { processOpsTime = null; - logger.debug("deferredDfOpsQueue has " + deferredOpsQueue.size() + " entries"); + logger.debug("deferredDfOpsQueue has " + + deferredOpsQueue.size() + " entries"); List writes = new ArrayList<>(); List archives = new ArrayList<>(); List restores = new ArrayList<>(); @@ -138,20 +156,27 @@ public void run() { Map deleteLocks = new HashMap<>(); Map newOps = new HashMap<>(); - final Iterator> it = deferredOpsQueue.entrySet().iterator(); + final Iterator> it = deferredOpsQueue + .entrySet().iterator(); while (it.hasNext()) { - Entry opEntry = it.next(); + Entry opEntry = it + .next(); var dfInfo = (DatafileInfo) opEntry.getKey(); - if(dfInfo == null) throw new RuntimeException("Could not cast DataInfoBase to DataFileInfo. Did you handed over another sub type?"); + if (dfInfo == null) + throw new RuntimeException( + "Could not cast DataInfoBase to DataFileInfo. Did you handed over another sub type?"); Long dsId = dfInfo.getDsId(); DatasetInfo dsInfo; try { - Dataset ds = (Dataset) reader.get("Dataset ds INCLUDE ds.investigation.facility", dsId); + Dataset ds = (Dataset) reader.get( + "Dataset ds INCLUDE ds.investigation.facility", + dsId); dsInfo = new DatasetInfo(ds); } catch (Exception e) { - logger.error("Could not get dsInfo {}: {}.", dsId, e.getMessage()); + logger.error("Could not get dsInfo {}: {}.", + dsId, e.getMessage()); continue; } if (!dataInfoChanging.containsKey(dfInfo)) { @@ -160,12 +185,20 @@ public void run() { if (state == RequestedState.WRITE_REQUESTED) { if (!writeLocks.containsKey(dsId)) { try { - writeLocks.put(dsId, lockManager.lock(dsInfo, LockType.SHARED)); + writeLocks.put(dsId, + lockManager.lock(dsInfo, + LockType.SHARED)); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error("I/O exception " + + e.getMessage() + + " locking " + dsId); continue; } } @@ -175,28 +208,46 @@ public void run() { } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (!writeLocks.containsKey(dsId)) { try { - writeLocks.put(dsId, lockManager.lock(dsInfo, LockType.SHARED)); + writeLocks.put(dsId, + lockManager.lock(dsInfo, + LockType.SHARED)); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error("I/O exception " + + e.getMessage() + + " locking " + dsId); continue; } } it.remove(); - dataInfoChanging.put(dfInfo, RequestedState.WRITE_REQUESTED); + dataInfoChanging.put(dfInfo, + RequestedState.WRITE_REQUESTED); writes.add(dfInfo); - newOps.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); + newOps.put(dfInfo, + RequestedState.ARCHIVE_REQUESTED); } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (!archiveLocks.containsKey(dsId)) { try { - archiveLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); + archiveLocks.put(dsId, + lockManager.lock(dsInfo, + LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error("I/O exception " + + e.getMessage() + + " locking " + dsId); continue; } } @@ -206,12 +257,20 @@ public void run() { } else if (state == RequestedState.RESTORE_REQUESTED) { if (!restoreLocks.containsKey(dsId)) { try { - restoreLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); + restoreLocks.put(dsId, + lockManager.lock(dsInfo, + LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error("I/O exception " + + e.getMessage() + + " locking " + dsId); continue; } } @@ -221,12 +280,20 @@ public void run() { } else if (state == RequestedState.DELETE_REQUESTED) { if (!deleteLocks.containsKey(dsId)) { try { - deleteLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); + deleteLocks.put(dsId, + lockManager.lock(dsInfo, + LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); + logger.debug( + "Could not acquire lock on " + + dsId + + ", hold back " + + state); continue; } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsId); + logger.error("I/O exception " + + e.getMessage() + + " locking " + dsId); continue; } } @@ -234,41 +301,61 @@ public void run() { dataInfoChanging.put(dfInfo, state); deletes.add(dfInfo); } else { - throw new AssertionError("Impossible state"); + throw new AssertionError( + "Impossible state"); } } } if (!newOps.isEmpty()) { deferredOpsQueue.putAll(newOps); - logger.debug("Adding {} operations to be scheduled next time round", newOps.size()); + logger.debug( + "Adding {} operations to be scheduled next time round", + newOps.size()); } if (!deferredOpsQueue.isEmpty()) { processOpsTime = 0L; } if (!writes.isEmpty()) { - logger.debug("Launch thread to process " + writes.size() + " writes"); - Thread w = new Thread(new DfWriter(writes, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, writeLocks.values())); + logger.debug("Launch thread to process " + + writes.size() + " writes"); + Thread w = new Thread(new DfWriter(writes, + propertyHandler, + FiniteStateMachineForStorageUnitDatafile.this, + writeLocks.values())); w.start(); } if (!archives.isEmpty()) { - logger.debug("Launch thread to process " + archives.size() + " archives"); - Thread w = new Thread(new DfArchiver(archives, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, archiveLocks.values())); + logger.debug("Launch thread to process " + + archives.size() + " archives"); + Thread w = new Thread(new DfArchiver(archives, + propertyHandler, + FiniteStateMachineForStorageUnitDatafile.this, + archiveLocks.values())); w.start(); } if (!restores.isEmpty()) { - logger.debug("Launch thread to process " + restores.size() + " restores"); - Thread w = new Thread(new DfRestorer(restores, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, restoreLocks.values())); + logger.debug("Launch thread to process " + + restores.size() + " restores"); + Thread w = new Thread(new DfRestorer(restores, + propertyHandler, + FiniteStateMachineForStorageUnitDatafile.this, + restoreLocks.values())); w.start(); } if (!deletes.isEmpty()) { - logger.debug("Launch thread to process " + deletes.size() + " deletes"); - Thread w = new Thread(new DfDeleter(deletes, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, deleteLocks.values())); + logger.debug("Launch thread to process " + + deletes.size() + " deletes"); + Thread w = new Thread(new DfDeleter(deletes, + propertyHandler, + FiniteStateMachineForStorageUnitDatafile.this, + deleteLocks.values())); w.start(); } } } } finally { - timer.schedule(new DfProcessQueue(), processQueueIntervalMillis); + timer.schedule(new DfProcessQueue(), + processQueueIntervalMillis); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java index cbe0a5cd..6eeebfb7 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java @@ -11,6 +11,8 @@ import java.util.Map.Entry; import java.util.TimerTask; +import jakarta.json.stream.JsonGenerator; + import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; @@ -24,22 +26,22 @@ import org.icatproject.ids.thread.DsRestorer; import org.icatproject.ids.thread.DsWriter; -import jakarta.json.stream.JsonGenerator; - -public class FiniteStateMachineForStorageUnitDataset extends FiniteStateMachine { +public class FiniteStateMachineForStorageUnitDataset + extends FiniteStateMachine { protected Map writeTimes = new HashMap<>(); - protected FiniteStateMachineForStorageUnitDataset(IcatReader reader, LockManager lockManager) { + protected FiniteStateMachineForStorageUnitDataset(IcatReader reader, + LockManager lockManager) { super(reader, lockManager); } - @Override protected void scheduleTimer() { processOpsDelayMillis = propertyHandler.getDelayDatasetWrites() * 1000L; timer.schedule(new DsProcessQueue(), processQueueIntervalMillis); - logger.info("DsProcessQueue scheduled to run in " + processQueueIntervalMillis + " milliseconds"); + logger.info("DsProcessQueue scheduled to run in " + + processQueueIntervalMillis + " milliseconds"); } @Override @@ -47,9 +49,8 @@ protected void addDataInfoJson(JsonGenerator gen) { this.addDataInfoJsonFromDeferredOpsQueue(gen); } - - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { - + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) + throws InternalException { logger.info("Requesting " + deferredOp + " of dataset " + dataInfo); @@ -60,41 +61,47 @@ public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalE if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.RESTORE_REQUESTED); } } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); - deferredOpsQueue.put(dataInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.RESTORE_REQUESTED); } } else if (state == RequestedState.RESTORE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { setDelay(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { setDelay(dataInfo); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put(dataInfo, + RequestedState.WRITE_REQUESTED); } } } } - private void requestWrite(DataInfoBase dsInfo) throws InternalException { try { Path marker = markerDir.resolve(Long.toString(dsInfo.getId())); @@ -109,16 +116,16 @@ private void requestWrite(DataInfoBase dsInfo) throws InternalException { setDelay(dsInfo); } - private void setDelay(DataInfoBase dsInfo) { - writeTimes.put(dsInfo, System.currentTimeMillis() + processOpsDelayMillis); + writeTimes.put(dsInfo, + System.currentTimeMillis() + processOpsDelayMillis); if (logger.isDebugEnabled()) { final Date d = new Date(writeTimes.get(dsInfo)); - logger.debug("Requesting delay of writing of dataset " + dsInfo + " till " + d); + logger.debug("Requesting delay of writing of dataset " + dsInfo + + " till " + d); } } - private class DsProcessQueue extends TimerTask { @Override @@ -127,13 +134,16 @@ public void run() { synchronized (deferredOpsQueue) { final long now = System.currentTimeMillis(); Map newOps = new HashMap<>(); - final Iterator> it = deferredOpsQueue.entrySet().iterator(); + final Iterator> it = deferredOpsQueue + .entrySet().iterator(); while (it.hasNext()) { - final Entry opEntry = it.next(); + final Entry opEntry = it + .next(); final var dsInfo = (DatasetInfo) opEntry.getKey(); - - if(dsInfo == null) throw new RuntimeException("Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); + if (dsInfo == null) + throw new RuntimeException( + "Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); if (!dataInfoChanging.containsKey(dsInfo)) { final RequestedState state = opEntry.getValue(); @@ -141,50 +151,81 @@ public void run() { || state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (now > writeTimes.get(dsInfo)) { try { - Lock lock = lockManager.lock(dsInfo, LockType.SHARED); - logger.debug("Will process " + dsInfo + " with " + state); + Lock lock = lockManager.lock(dsInfo, + LockType.SHARED); + logger.debug("Will process " + dsInfo + + " with " + state); writeTimes.remove(dsInfo); - dataInfoChanging.put(dsInfo, RequestedState.WRITE_REQUESTED); + dataInfoChanging.put(dsInfo, + RequestedState.WRITE_REQUESTED); it.remove(); final Thread w = new Thread( - new DsWriter(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, reader, lock)); + new DsWriter(dsInfo, + propertyHandler, + FiniteStateMachineForStorageUnitDataset.this, + reader, lock)); w.start(); if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { - newOps.put(dsInfo, RequestedState.ARCHIVE_REQUESTED); + newOps.put(dsInfo, + RequestedState.ARCHIVE_REQUESTED); } } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); + logger.debug( + "Could not acquire lock on " + + dsInfo + + ", hold back process with " + + state); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); + logger.error("I/O exception " + + e.getMessage() + " locking " + + dsInfo); } } } else if (state == RequestedState.ARCHIVE_REQUESTED) { try { - Lock lock = lockManager.lock(dsInfo, LockType.EXCLUSIVE); + Lock lock = lockManager.lock(dsInfo, + LockType.EXCLUSIVE); it.remove(); - logger.debug("Will process " + dsInfo + " with " + state); + logger.debug("Will process " + dsInfo + + " with " + state); dataInfoChanging.put(dsInfo, state); - final Thread w = new Thread( - new DsArchiver(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, lock)); + final Thread w = new Thread(new DsArchiver( + dsInfo, propertyHandler, + FiniteStateMachineForStorageUnitDataset.this, + lock)); w.start(); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); + logger.debug("Could not acquire lock on " + + dsInfo + + ", hold back process with " + + state); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); + logger.error( + "I/O exception " + e.getMessage() + + " locking " + dsInfo); } } else if (state == RequestedState.RESTORE_REQUESTED) { try { - Lock lock = lockManager.lock(dsInfo, LockType.EXCLUSIVE); - logger.debug("Will process " + dsInfo + " with " + state); + Lock lock = lockManager.lock(dsInfo, + LockType.EXCLUSIVE); + logger.debug("Will process " + dsInfo + + " with " + state); dataInfoChanging.put(dsInfo, state); it.remove(); - final Thread w = new Thread( - new DsRestorer(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, reader, lock)); + final Thread w = new Thread(new DsRestorer( + dsInfo, propertyHandler, + FiniteStateMachineForStorageUnitDataset.this, + reader, lock)); w.start(); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); + logger.debug("Could not acquire lock on " + + dsInfo + + ", hold back process with " + + state); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); + logger.error( + "I/O exception " + e.getMessage() + + " locking " + dsInfo); } } } @@ -193,7 +234,8 @@ public void run() { } } finally { - timer.schedule(new DsProcessQueue(), processQueueIntervalMillis); + timer.schedule(new DsProcessQueue(), + processQueueIntervalMillis); } } diff --git a/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java b/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java index 8c813be8..8a29aec2 100644 --- a/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java +++ b/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java @@ -11,8 +11,8 @@ @Provider public class CORSResponseFilter implements ContainerResponseFilter { - public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) - throws IOException { + public void filter(ContainerRequestContext requestContext, + ContainerResponseContext responseContext) throws IOException { MultivaluedMap headers = responseContext.getHeaders(); headers.add("Access-Control-Allow-Origin", "*"); diff --git a/src/main/java/org/icatproject/ids/helpers/Constants.java b/src/main/java/org/icatproject/ids/helpers/Constants.java index bc7d4aaa..298205bf 100644 --- a/src/main/java/org/icatproject/ids/helpers/Constants.java +++ b/src/main/java/org/icatproject/ids/helpers/Constants.java @@ -9,8 +9,8 @@ public class Constants { static { - InputStream inputStream = Constants.class.getClassLoader().getResourceAsStream( - "app.properties"); + InputStream inputStream = Constants.class.getClassLoader() + .getResourceAsStream("app.properties"); Properties p = new Properties(); try { diff --git a/src/main/java/org/icatproject/ids/helpers/LocationHelper.java b/src/main/java/org/icatproject/ids/helpers/LocationHelper.java index b3c1e8b5..b4149116 100644 --- a/src/main/java/org/icatproject/ids/helpers/LocationHelper.java +++ b/src/main/java/org/icatproject/ids/helpers/LocationHelper.java @@ -2,10 +2,11 @@ import java.security.NoSuchAlgorithmException; +import org.icatproject.utils.IcatSecurity; + import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.services.ServiceProvider; -import org.icatproject.utils.IcatSecurity; public class LocationHelper { @@ -23,8 +24,8 @@ public static String getLocation(long dfid, String location) } } - - public static String getLocationFromDigest(long id, String locationWithHash, String key) + public static String getLocationFromDigest(long id, String locationWithHash, + String key) throws InternalException, InsufficientPrivilegesException { int i = locationWithHash.lastIndexOf(' '); try { @@ -32,11 +33,13 @@ public static String getLocationFromDigest(long id, String locationWithHash, Str String hash = locationWithHash.substring(i + 1); if (!hash.equals(IcatSecurity.digest(id, location, key))) { throw new InsufficientPrivilegesException( - "Location \"" + locationWithHash + "\" does not contain a valid hash."); + "Location \"" + locationWithHash + + "\" does not contain a valid hash."); } return location; } catch (IndexOutOfBoundsException e) { - throw new InsufficientPrivilegesException("Location \"" + locationWithHash + "\" does not contain hash."); + throw new InsufficientPrivilegesException("Location \"" + + locationWithHash + "\" does not contain hash."); } catch (NoSuchAlgorithmException e) { throw new InternalException(e.getMessage()); } diff --git a/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java b/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java index ffaa34be..208d39a5 100644 --- a/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java +++ b/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java @@ -21,10 +21,10 @@ public class RangeOutputStream extends FilterOutputStream { * fixed number of bytes following some offset. * * @param os the stream to wrap - * @param min the number of bytes to skip. It may larger than the number of - * bytes in the stream. - * @param count restrict to transmit only this number of bytes. The value null - * indicates that all bytes after the offset should be + * @param min the number of bytes to skip. It may larger than the number + * of bytes in the stream. + * @param count restrict to transmit only this number of bytes. The value + * null indicates that all bytes after the offset should be * transmitted. */ public RangeOutputStream(OutputStream os, long min, Long count) { diff --git a/src/main/java/org/icatproject/ids/helpers/SO.java b/src/main/java/org/icatproject/ids/helpers/SO.java index 58e1ee4a..848fe6c4 100644 --- a/src/main/java/org/icatproject/ids/helpers/SO.java +++ b/src/main/java/org/icatproject/ids/helpers/SO.java @@ -10,18 +10,19 @@ import java.util.zip.ZipException; import java.util.zip.ZipOutputStream; -import org.icatproject.ids.models.DatafileInfo; -import org.icatproject.ids.models.DataInfoBase; -import org.icatproject.ids.models.DatasetInfo; -import org.icatproject.ids.services.ServiceProvider; -import org.icatproject.ids.services.LockManager.Lock; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import jakarta.json.Json; import jakarta.json.stream.JsonGenerator; import jakarta.ws.rs.core.StreamingOutput; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.icatproject.ids.models.DataInfoBase; +import org.icatproject.ids.models.DatafileInfo; +import org.icatproject.ids.models.DatasetInfo; +import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.ServiceProvider; + public class SO implements StreamingOutput { private long offset; @@ -38,8 +39,10 @@ public class SO implements StreamingOutput { private static final int BUFSIZ = 2048; private final static Logger logger = LoggerFactory.getLogger(SO.class); - public SO(Map dsInfos, Map dfInfos, long offset, boolean zip, boolean compress, - Lock lock, Long transferId, String ip, long start, ServiceProvider serviceProvider) { + public SO(Map dsInfos, Map dfInfos, + long offset, boolean zip, boolean compress, Lock lock, + Long transferId, String ip, long start, + ServiceProvider serviceProvider) { this.offset = offset; this.zip = zip; this.dsInfos = dsInfos; @@ -62,21 +65,27 @@ public void write(OutputStream output) throws IOException { } byte[] bytes = new byte[BUFSIZ]; if (zip) { - ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(output)); + ZipOutputStream zos = new ZipOutputStream( + new BufferedOutputStream(output)); if (!compress) { zos.setLevel(0); // Otherwise use default compression } - for ( DataInfoBase dataInfo : dfInfos.values()) { + for (DataInfoBase dataInfo : dfInfos.values()) { var dfInfo = (DatafileInfo) dataInfo; logger.debug("Adding " + dfInfo + " to zip"); transfer = dfInfo; - DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId() ); - String entryName = this.serviceProvider.getPropertyHandler().getZipMapper().getFullEntryName((DatasetInfo)dsInfo, (DatafileInfo)dfInfo); + DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId()); + String entryName = this.serviceProvider.getPropertyHandler() + .getZipMapper() + .getFullEntryName((DatasetInfo) dsInfo, + (DatafileInfo) dfInfo); InputStream stream = null; try { zos.putNextEntry(new ZipEntry(entryName)); - stream = this.serviceProvider.getMainStorage().get(dfInfo.getLocation(), dfInfo.getCreateId(), dfInfo.getModId()); + stream = this.serviceProvider.getMainStorage().get( + dfInfo.getLocation(), dfInfo.getCreateId(), + dfInfo.getModId()); int length; while ((length = stream.read(bytes)) >= 0) { zos.write(bytes, 0, length); @@ -91,9 +100,11 @@ public void write(OutputStream output) throws IOException { } zos.close(); } else { - DatafileInfo dfInfo = (DatafileInfo) dfInfos.values().iterator().next(); + DatafileInfo dfInfo = (DatafileInfo) dfInfos.values().iterator() + .next(); transfer = dfInfo; - InputStream stream = this.serviceProvider.getMainStorage().get(dfInfo.getDfLocation(), dfInfo.getCreateId(), + InputStream stream = this.serviceProvider.getMainStorage().get( + dfInfo.getDfLocation(), dfInfo.getCreateId(), dfInfo.getModId()); int length; while ((length = stream.read(bytes)) >= 0) { @@ -105,23 +116,28 @@ public void write(OutputStream output) throws IOException { if (transferId != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos) + .writeStartObject()) { gen.write("transferId", transferId); gen.writeEnd(); } - this.serviceProvider.getTransmitter().processMessage("getData", ip, baos.toString(), start); + this.serviceProvider.getTransmitter().processMessage("getData", + ip, baos.toString(), start); } } catch (IOException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos) + .writeStartObject()) { gen.write("transferId", transferId); gen.write("exceptionClass", e.getClass().toString()); gen.write("exceptionMessage", e.getMessage()); gen.writeEnd(); } - this.serviceProvider.getTransmitter().processMessage("getData", ip, baos.toString(), start); - logger.error("Failed to stream " + transfer + " due to " + e.getMessage()); + this.serviceProvider.getTransmitter().processMessage("getData", ip, + baos.toString(), start); + logger.error("Failed to stream " + transfer + " due to " + + e.getMessage()); throw e; } finally { lock.release(); diff --git a/src/main/java/org/icatproject/ids/helpers/ValueContainer.java b/src/main/java/org/icatproject/ids/helpers/ValueContainer.java index bb99efba..2ebedf30 100644 --- a/src/main/java/org/icatproject/ids/helpers/ValueContainer.java +++ b/src/main/java/org/icatproject/ids/helpers/ValueContainer.java @@ -2,11 +2,11 @@ import java.io.InputStream; +import jakarta.ws.rs.core.Response; + import org.icatproject.ids.enums.ValueContainerType; import org.icatproject.ids.exceptions.InternalException; -import jakarta.ws.rs.core.Response; - /** * This class provides a container vor carrying different types of values */ @@ -22,11 +22,16 @@ private ValueContainer(Object value, ValueContainerType type) { /** * checks if the type of the contained value is the same as the typeToCheck + * * @param typeToCheck the type to be checked if the contained value is of * @throws InternalException if the types don't match an exception is thrown */ - private void checkType(ValueContainerType typeToCheck) throws InternalException { - if(this.type != typeToCheck) throw new InternalException("This ValueContainer ist not of the needed type " + typeToCheck + " its type is " + this.type + "."); + private void checkType(ValueContainerType typeToCheck) + throws InternalException { + if (this.type != typeToCheck) + throw new InternalException( + "This ValueContainer ist not of the needed type " + + typeToCheck + " its type is " + this.type + "."); } public static ValueContainer getInvalid() { @@ -47,6 +52,7 @@ private ValueContainer(Void value) { /** * Creates a ValueContainer of type int + * * @param value the value contained by the container */ public ValueContainer(int value) { @@ -55,6 +61,7 @@ public ValueContainer(int value) { /** * Creates a ValueContainer of type long + * * @param value the value contained by the container */ public ValueContainer(long value) { @@ -63,6 +70,7 @@ public ValueContainer(long value) { /** * Creates a ValueContainer of type String + * * @param value the value contained by the container */ public ValueContainer(String value) { @@ -71,6 +79,7 @@ public ValueContainer(String value) { /** * Creates a ValueContainer of type boolean + * * @param value the value contained by the container */ public ValueContainer(boolean value) { @@ -79,14 +88,16 @@ public ValueContainer(boolean value) { /** * Creates a ValueContainer of type Response + * * @param value the value contained by the container */ public ValueContainer(Response value) { this(value, ValueContainerType.RESPONSE); - } - + } + /** * Creates a ValueContainer of type InputStream + * * @param value the value contained by the container */ public ValueContainer(InputStream value) { @@ -95,6 +106,7 @@ public ValueContainer(InputStream value) { /** * Informs about the type of the contained value + * * @return */ public ValueContainerType getType() { @@ -103,8 +115,10 @@ public ValueContainerType getType() { /** * Tries to return the value of the type int. + * * @return - * @throws InternalException if the container has another type an exception will be thrown + * @throws InternalException if the container has another type an exception + * will be thrown */ public int getInt() throws InternalException { this.checkType(ValueContainerType.INT); @@ -113,8 +127,10 @@ public int getInt() throws InternalException { /** * Tries to return the value of the type long. + * * @return - * @throws InternalException if the container has another type an exception will be thrown + * @throws InternalException if the container has another type an exception + * will be thrown */ public long getLong() throws InternalException { this.checkType(ValueContainerType.LONG); @@ -123,8 +139,10 @@ public long getLong() throws InternalException { /** * Tries to return the value of the type boolean. + * * @return - * @throws InternalException if the container has another type an exception will be thrown + * @throws InternalException if the container has another type an exception + * will be thrown */ public boolean getBool() throws InternalException { this.checkType(ValueContainerType.BOOL); @@ -133,8 +151,10 @@ public boolean getBool() throws InternalException { /** * Tries to return the value of the type String. + * * @return - * @throws InternalException if the container has another type an exception will be thrown + * @throws InternalException if the container has another type an exception + * will be thrown */ public String getString() throws InternalException { this.checkType(ValueContainerType.STRING); @@ -143,8 +163,10 @@ public String getString() throws InternalException { /** * Tries to return the value of the type Response. + * * @return - * @throws InternalException if the container has another type an exception will be thrown + * @throws InternalException if the container has another type an exception + * will be thrown */ public Response getResponse() throws InternalException { this.checkType(ValueContainerType.RESPONSE); @@ -153,8 +175,10 @@ public Response getResponse() throws InternalException { /** * Tries to return the value of the type InputStream. + * * @return - * @throws InternalException if the container has another type an exception will be thrown + * @throws InternalException if the container has another type an exception + * will be thrown */ public InputStream getInputStream() throws InternalException { this.checkType(ValueContainerType.INPUTSTREAM); @@ -163,27 +187,41 @@ public InputStream getInputStream() throws InternalException { @Override public String toString() { - switch(this.type) { - case INVALID: return ""+ValueContainerType.INVALID; - case VOID: return ""+ValueContainerType.VOID; - case INT: return ""+this.value; - case LONG: return ""+this.value; - case BOOL: return ((boolean)this.value ? "true" : "false"); - case STRING: return (String)this.value; - case RESPONSE: return "Response " + ((Response) this.value).toString(); - case INPUTSTREAM: return "An InputStream which will be printed here to prevent it from closing (and maybe it is too long)."; - default: - throw new RuntimeException("Doesn't know how to make a String vom ValueContainer of type " + this.type + ". Please implement a new case is ValueContainer.toString()."); + switch (this.type) { + case INVALID: + return "" + ValueContainerType.INVALID; + case VOID: + return "" + ValueContainerType.VOID; + case INT: + return "" + this.value; + case LONG: + return "" + this.value; + case BOOL: + return ((boolean) this.value ? "true" : "false"); + case STRING: + return (String) this.value; + case RESPONSE: + return "Response " + ((Response) this.value).toString(); + case INPUTSTREAM: + return "An InputStream which will be printed here to prevent it from closing (and maybe it is too long)."; + default: + throw new RuntimeException( + "Doesn't know how to make a String vom ValueContainer of type " + + this.type + + ". Please implement a new case is ValueContainer.toString()."); } } - public boolean isNull() { return this.isInvalid() && this.value == null; } - - public boolean isInvalid() { return this.type == ValueContainerType.INVALID; } - - public boolean isVoid() { return this.type == ValueContainerType.VOID; } + public boolean isNull() { + return this.isInvalid() && this.value == null; + } - + public boolean isInvalid() { + return this.type == ValueContainerType.INVALID; + } + public boolean isVoid() { + return this.type == ValueContainerType.VOID; + } } diff --git a/src/main/java/org/icatproject/ids/models/DataInfoBase.java b/src/main/java/org/icatproject/ids/models/DataInfoBase.java index 603a3e7e..57adf664 100644 --- a/src/main/java/org/icatproject/ids/models/DataInfoBase.java +++ b/src/main/java/org/icatproject/ids/models/DataInfoBase.java @@ -9,7 +9,7 @@ public abstract class DataInfoBase { protected String name; protected String location; - protected DataInfoBase(long id, String name, String location){ + protected DataInfoBase(long id, String name, String location) { this.name = name; this.id = id; this.location = location; @@ -42,5 +42,5 @@ public boolean equals(Object obj) { } return this.id == ((DataInfoBase) obj).getId(); } - + } diff --git a/src/main/java/org/icatproject/ids/models/DataSelection.java b/src/main/java/org/icatproject/ids/models/DataSelection.java index 1bc59344..ffb8aa77 100644 --- a/src/main/java/org/icatproject/ids/models/DataSelection.java +++ b/src/main/java/org/icatproject/ids/models/DataSelection.java @@ -15,10 +15,10 @@ public class DataSelection { protected Boolean compress; private long length; - - - public DataSelection(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress) { + public DataSelection(SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, + Boolean zip, Boolean compress) { this.dsInfos = dsInfos; this.dfInfos = dfInfos; @@ -31,12 +31,10 @@ public DataSelection(SortedMap dsInfos, SortedMap getDsInfo() { return dsInfos; } - public SortedMap getDfInfo() { return dfInfos; } @@ -53,11 +51,10 @@ public long getLength() { return this.length; } - public boolean mustZip() { // if(this.zip == null) { - return dfids.size() > 1L || !dsids.isEmpty() || !invids.isEmpty() - || (dfids.isEmpty() && dsids.isEmpty() && invids.isEmpty()); + return dfids.size() > 1L || !dsids.isEmpty() || !invids.isEmpty() + || (dfids.isEmpty() && dsids.isEmpty() && invids.isEmpty()); // } // return this.zip; @@ -67,7 +64,6 @@ public boolean isSingleDataset() { return dfids.isEmpty() && dsids.size() == 1 && invids.isEmpty(); } - public Set getEmptyDatasets() { return emptyDatasets; } diff --git a/src/main/java/org/icatproject/ids/models/DatafileInfo.java b/src/main/java/org/icatproject/ids/models/DatafileInfo.java index 789cfa79..4fa20cfd 100644 --- a/src/main/java/org/icatproject/ids/models/DatafileInfo.java +++ b/src/main/java/org/icatproject/ids/models/DatafileInfo.java @@ -3,16 +3,18 @@ import org.icatproject.ids.plugin.DfInfo; /** - * Contains information about a Datafile. Replaces DsInfo in v3 - * May should implement DfInfo interface + * Contains information about a Datafile. Replaces DsInfo in v3 May should + * implement DfInfo interface */ -public class DatafileInfo extends DataInfoBase implements Comparable, DfInfo { +public class DatafileInfo extends DataInfoBase + implements Comparable, DfInfo { protected String createId; - protected String modId; + protected String modId; protected long datasId; - public DatafileInfo(Long id, String name, String location, String createId, String modId, Long datasId) { + public DatafileInfo(Long id, String name, String location, String createId, + String modId, Long datasId) { super(id, name, location); this.createId = createId; @@ -50,9 +52,17 @@ public int compareTo(DatafileInfo o) { // implementing DfInfo @Override - public Long getDfId() { return this.getId(); } + public Long getDfId() { + return this.getId(); + } + @Override - public String getDfLocation() { return this.getLocation(); } + public String getDfLocation() { + return this.getLocation(); + } + @Override - public String getDfName() { return this.getName(); } + public String getDfName() { + return this.getName(); + } } diff --git a/src/main/java/org/icatproject/ids/models/DatasetInfo.java b/src/main/java/org/icatproject/ids/models/DatasetInfo.java index be4d9a77..72c797a8 100644 --- a/src/main/java/org/icatproject/ids/models/DatasetInfo.java +++ b/src/main/java/org/icatproject/ids/models/DatasetInfo.java @@ -3,12 +3,13 @@ import org.icatproject.Dataset; import org.icatproject.Facility; import org.icatproject.Investigation; + import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.plugin.DsInfo; /** - * Contains information about a Dataset. Replaces DsInfo in v3. - * May should implement DsInfo interface + * Contains information about a Dataset. Replaces DsInfo in v3. May should + * implement DsInfo interface */ public class DatasetInfo extends DataInfoBase implements DsInfo { @@ -18,14 +19,14 @@ public class DatasetInfo extends DataInfoBase implements DsInfo { protected String investigationName; protected String visitId; - public DatasetInfo(Dataset ds) throws InsufficientPrivilegesException { - super(ds.getId(), ds.getName(), ds.getLocation()); + super(ds.getId(), ds.getName(), ds.getLocation()); Investigation investigation = ds.getInvestigation(); if (investigation == null) { throw new InsufficientPrivilegesException( - "Probably not able to read Investigation for dataset id " + ds.getId()); + "Probably not able to read Investigation for dataset id " + + ds.getId()); } Facility facility = investigation.getFacility(); if (facility == null) { @@ -41,8 +42,9 @@ public DatasetInfo(Dataset ds) throws InsufficientPrivilegesException { this.facilityName = facility.getName(); } - - public DatasetInfo(Long id, String name, String location, Long investigationId, String investigationName, String visitId, Long facilityId, String facilityName) { + public DatasetInfo(Long id, String name, String location, + Long investigationId, String investigationName, String visitId, + Long facilityId, String facilityName) { super(id, name, location); this.facilityId = facilityId; @@ -54,7 +56,8 @@ public DatasetInfo(Long id, String name, String location, Long investigationId, @Override public String toString() { - return this.investigationId + "/" + this.id + " (" + this.facilityName + "/" + this.investigationName + "/" + this.visitId + "/" + return this.investigationId + "/" + this.id + " (" + this.facilityName + + "/" + this.investigationName + "/" + this.visitId + "/" + this.name + ")"; } @@ -78,18 +81,31 @@ public String getVisitId() { return visitId; } - // implementing DsInfo @Override - public Long getDsId() { return this.getId(); } + public Long getDsId() { + return this.getId(); + } + @Override - public String getDsName() { return this.getName(); } + public String getDsName() { + return this.getName(); + } + @Override - public String getDsLocation() { return this.getLocation(); } + public String getDsLocation() { + return this.getLocation(); + } + @Override - public Long getInvId() { return this.getInvestigationId(); } + public Long getInvId() { + return this.getInvestigationId(); + } + @Override - public String getInvName() { return this.getInvestigationName(); } + public String getInvName() { + return this.getInvestigationName(); + } } diff --git a/src/main/java/org/icatproject/ids/models/Prepared.java b/src/main/java/org/icatproject/ids/models/Prepared.java index 5e04d830..0f5bcc21 100644 --- a/src/main/java/org/icatproject/ids/models/Prepared.java +++ b/src/main/java/org/icatproject/ids/models/Prepared.java @@ -8,11 +8,6 @@ import java.util.SortedMap; import java.util.TreeMap; -import org.icatproject.ids.exceptions.InternalException; -import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import jakarta.json.Json; import jakarta.json.JsonNumber; import jakarta.json.JsonObject; @@ -20,12 +15,18 @@ import jakarta.json.JsonValue; import jakarta.json.stream.JsonGenerator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.icatproject.ids.exceptions.InternalException; +import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; + /* This is a POJO with only package access so don't make data private */ public class Prepared { - protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); + protected final static Logger logger = LoggerFactory + .getLogger(RequestHandlerBase.class); - public SortedMap dsInfos; public SortedMap dfInfos; public Set emptyDatasets; @@ -33,7 +34,9 @@ public class Prepared { public boolean zip; public boolean compress; - public Prepared(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength) { + public Prepared(SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + long fileLength) { this.dsInfos = dsInfos; this.dfInfos = dfInfos; this.emptyDatasets = emptyDatasets; @@ -42,15 +45,17 @@ public Prepared(SortedMap dsInfos, SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength, Boolean zip, Boolean compress) { + public Prepared(SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + long fileLength, Boolean zip, Boolean compress) { this(dsInfos, dfInfos, emptyDatasets, fileLength); this.zip = zip; this.compress = compress; } - - public static void pack(OutputStream stream, boolean zip, boolean compress, Map dsInfos, - Map dfInfos, Set emptyDatasets, long fileLength) { + public static void pack(OutputStream stream, boolean zip, boolean compress, + Map dsInfos, Map dfInfos, + Set emptyDatasets, long fileLength) { JsonGenerator gen = Json.createGenerator(stream); gen.writeStartObject(); gen.write("zip", zip); @@ -59,13 +64,16 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, Map< gen.writeStartArray("dsInfo"); for (DataInfoBase dataInfo : dsInfos.values()) { - var dsInfo = (DatasetInfo)dataInfo; + var dsInfo = (DatasetInfo) dataInfo; logger.debug("dsInfo " + dsInfo); gen.writeStartObject().write("dsId", dsInfo.getId()) - .write("dsName", dsInfo.getDsName()).write("facilityId", dsInfo.getFacilityId()) - .write("facilityName", dsInfo.getFacilityName()).write("invId", dsInfo.getInvId()) - .write("invName", dsInfo.getInvName()).write("visitId", dsInfo.getVisitId()); + .write("dsName", dsInfo.getDsName()) + .write("facilityId", dsInfo.getFacilityId()) + .write("facilityName", dsInfo.getFacilityName()) + .write("invId", dsInfo.getInvId()) + .write("invName", dsInfo.getInvName()) + .write("visitId", dsInfo.getVisitId()); if (dsInfo.getDsLocation() != null) { gen.write("dsLocation", dsInfo.getDsLocation()); } else { @@ -77,10 +85,12 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, Map< gen.writeStartArray("dfInfo"); for (DataInfoBase dataInfo : dfInfos.values()) { - var dfInfo = (DatafileInfo)dataInfo; + var dfInfo = (DatafileInfo) dataInfo; DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId()); - gen.writeStartObject().write("dsId", dsInfo.getId()).write("dfId", dfInfo.getId()) - .write("dfName", dfInfo.getDfName()).write("createId", dfInfo.getCreateId()) + gen.writeStartObject().write("dsId", dsInfo.getId()) + .write("dfId", dfInfo.getId()) + .write("dfName", dfInfo.getDfName()) + .write("createId", dfInfo.getCreateId()) .write("modId", dfInfo.getModId()); if (dfInfo.getDfLocation() != null) { gen.write("dfLocation", dfInfo.getDfLocation()); @@ -102,7 +112,6 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, Map< } - public static Prepared unpack(InputStream stream) throws InternalException { JsonObject pd; @@ -111,35 +120,42 @@ public static Prepared unpack(InputStream stream) throws InternalException { } var zip = pd.getBoolean("zip"); var compress = pd.getBoolean("compress"); - var fileLength = pd.containsKey("fileLength") ? pd.getInt("fileLength") : 0; + var fileLength = pd.containsKey("fileLength") ? pd.getInt("fileLength") + : 0; SortedMap dsInfos = new TreeMap<>(); SortedMap dfInfos = new TreeMap<>(); Set emptyDatasets = new HashSet<>(); for (JsonValue itemV : pd.getJsonArray("dfInfo")) { JsonObject item = (JsonObject) itemV; - String dfLocation = item.isNull("dfLocation") ? null : item.getString("dfLocation"); + String dfLocation = item.isNull("dfLocation") ? null + : item.getString("dfLocation"); long dfid = item.getJsonNumber("dfId").longValueExact(); - dfInfos.put(dfid, new DatafileInfo(dfid, item.getString("dfName"), - dfLocation, item.getString("createId"), item.getString("modId"), - item.getJsonNumber("dsId").longValueExact())); + dfInfos.put(dfid, + new DatafileInfo(dfid, item.getString("dfName"), dfLocation, + item.getString("createId"), item.getString("modId"), + item.getJsonNumber("dsId").longValueExact())); } for (JsonValue itemV : pd.getJsonArray("dsInfo")) { JsonObject item = (JsonObject) itemV; long dsId = item.getJsonNumber("dsId").longValueExact(); - String dsLocation = item.isNull("dsLocation") ? null : item.getString("dsLocation"); - dsInfos.put(dsId, new DatasetInfo(dsId, item.getString("dsName"), dsLocation, - item.getJsonNumber("invId").longValueExact(), item.getString("invName"), item.getString("visitId"), - item.getJsonNumber("facilityId").longValueExact(), item.getString("facilityName"))); + String dsLocation = item.isNull("dsLocation") ? null + : item.getString("dsLocation"); + dsInfos.put(dsId, new DatasetInfo(dsId, item.getString("dsName"), + dsLocation, item.getJsonNumber("invId").longValueExact(), + item.getString("invName"), item.getString("visitId"), + item.getJsonNumber("facilityId").longValueExact(), + item.getString("facilityName"))); } for (JsonValue itemV : pd.getJsonArray("emptyDs")) { emptyDatasets.add(((JsonNumber) itemV).longValueExact()); } - return new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength, zip, compress); + return new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength, zip, + compress); } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java index 032e8437..df19f640 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java @@ -1,6 +1,5 @@ package org.icatproject.ids.requestHandlers; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; @@ -12,13 +11,17 @@ public class ArchiveHandler extends DataRequestHandler { - public ArchiveHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.ARCHIVE, ip, sessionId, investigationIds, datasetIds, datafileIds); + public ArchiveHandler(String ip, String sessionId, String investigationIds, + String datasetIds, String datafileIds) { + super(RequestType.ARCHIVE, ip, sessionId, investigationIds, datasetIds, + datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws NotImplementedException, InternalException { - + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws NotImplementedException, InternalException { + dataSelectionService.scheduleTasks(DeferredOp.ARCHIVE); return ValueContainer.getVoid(); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java index a4cb44a9..b952f2b6 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java @@ -8,6 +8,7 @@ import org.icatproject.EntityBaseBean; import org.icatproject.IcatExceptionType; import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.enums.StorageUnit; @@ -21,48 +22,57 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataRequestHandler; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; - public class DeleteHandler extends DataRequestHandler { - public DeleteHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.DELETE, ip, sessionId, investigationIds, datasetIds, datafileIds); + public DeleteHandler(String ip, String sessionId, String investigationIds, + String datasetIds, String datafileIds) { + super(RequestType.DELETE, ip, sessionId, investigationIds, datasetIds, + datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { if (readOnly) { - throw new NotImplementedException("This operation has been configured to be unavailable"); + throw new NotImplementedException( + "This operation has been configured to be unavailable"); } var serviceProvider = ServiceProvider.getInstance(); // Do it - try (Lock lock = serviceProvider.getLockManager().lock(dataSelectionService.getDsInfo().values(), LockType.EXCLUSIVE)) { + try (Lock lock = serviceProvider.getLockManager().lock( + dataSelectionService.getDsInfo().values(), + LockType.EXCLUSIVE)) { if (storageUnit == StorageUnit.DATASET) { dataSelectionService.checkOnline(); } /* Now delete from ICAT */ List dfs = new ArrayList<>(); - for (DataInfoBase dfInfo : dataSelectionService.getDfInfo().values()) { + for (DataInfoBase dfInfo : dataSelectionService.getDfInfo() + .values()) { Datafile df = new Datafile(); df.setId(dfInfo.getId()); dfs.add(df); } try { - serviceProvider.getIcat().deleteMany(this.dataController.getOperationId(), dfs); + serviceProvider.getIcat() + .deleteMany(this.dataController.getOperationId(), dfs); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES + || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java index 7c22c38f..79d59221 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java @@ -2,6 +2,9 @@ import java.io.ByteArrayOutputStream; +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -16,30 +19,33 @@ import org.icatproject.ids.requestHandlers.base.PreparedDataController; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; - public class GetDataFileIdsHandler extends DataRequestHandler { - public GetDataFileIdsHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETDATAFILEIDS, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public GetDataFileIdsHandler(String ip, String preparedId, String sessionId, + String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETDATAFILEIDS, ip, preparedId, sessionId, + investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos) + .writeStartObject()) { - if(this.dataController instanceof PreparedDataController) { + if (this.dataController instanceof PreparedDataController) { gen.write("zip", dataSelectionService.getZip()); gen.write("compress", dataSelectionService.getCompress()); } gen.writeStartArray("ids"); - for (DataInfoBase dfInfo : dataSelectionService.getDfInfo().values()) { + for (DataInfoBase dfInfo : dataSelectionService.getDfInfo() + .values()) { gen.write(dfInfo.getId()); } gen.writeEnd().writeEnd().close(); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java index 8cdb64ce..e2b03865 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java @@ -1,5 +1,7 @@ package org.icatproject.ids.requestHandlers; +import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH; + import java.io.IOException; import java.net.HttpURLConnection; import java.text.SimpleDateFormat; @@ -10,7 +12,11 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import jakarta.json.stream.JsonGenerator; +import jakarta.ws.rs.core.Response; + import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -25,15 +31,11 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataRequestHandler; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; -import jakarta.json.stream.JsonGenerator; -import jakarta.ws.rs.core.Response; -import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH; - public class GetDataHandler extends DataRequestHandler { private Pattern rangeRe; @@ -45,40 +47,48 @@ public class GetDataHandler extends DataRequestHandler { String range; Long transferId; - public GetDataHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds, Boolean compress, Boolean zip, String outname, String range) { - super(RequestType.GETDATA, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds ); + public GetDataHandler(String ip, String preparedId, String sessionId, + String investigationIds, String datasetIds, String datafileIds, + Boolean compress, Boolean zip, String outname, String range) { + super(RequestType.GETDATA, ip, preparedId, sessionId, investigationIds, + datasetIds, datafileIds); - this.initializeAdditionallParameters(sessionId, compress, zip, outname, range); + this.initializeAdditionallParameters(sessionId, compress, zip, outname, + range); } - private void initializeAdditionallParameters(String sessionId, Boolean compress, Boolean zip, String outname, String range) { - + private void initializeAdditionallParameters(String sessionId, + Boolean compress, Boolean zip, String outname, String range) { + this.outname = outname; this.range = range; this.transferId = null; this.rangeRe = Pattern.compile("bytes=(\\d+)-"); - if(sessionId != null) { + if (sessionId != null) { this.compress = compress; this.zip = zip; } } - @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws InternalException, NotImplementedException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException { + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) throws InternalException, + NotImplementedException, BadRequestException, NotFoundException, + InsufficientPrivilegesException, DataNotOnlineException { - if(this.compress == null || this.zip == null) { + if (this.compress == null || this.zip == null) { this.zip = dataSelectionService.getZip(); this.compress = dataSelectionService.getCompress(); } long offset = 0; - if ( range != null) { + if (range != null) { Matcher m = rangeRe.matcher(range); if (!m.matches()) { - throw new BadRequestException("The range must match " + rangeRe.pattern()); + throw new BadRequestException( + "The range must match " + rangeRe.pattern()); } offset = Long.parseLong(m.group(1)); logger.debug("Range " + range + " -> offset " + offset); @@ -87,20 +97,24 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic return new ValueContainer(this.getData(dataSelectionService, offset)); } - - private Response getData(DataSelectionService dataSelectionService, final long offset) throws BadRequestException, - NotFoundException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + private Response getData(DataSelectionService dataSelectionService, + final long offset) throws BadRequestException, NotFoundException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { long start = System.currentTimeMillis(); - - var length = this.zip ? OptionalLong.empty() : dataSelectionService.getFileLength(); - final boolean finalZip = this.dataController.mustZip(zip, dataSelectionService); + var length = this.zip ? OptionalLong.empty() + : dataSelectionService.getFileLength(); + + final boolean finalZip = this.dataController.mustZip(zip, + dataSelectionService); Lock lock = null; try { var serviceProvider = ServiceProvider.getInstance(); - lock = serviceProvider.getLockManager().lock(dataSelectionService.getDsInfo().values(), LockType.SHARED); + lock = serviceProvider.getLockManager().lock( + dataSelectionService.getDsInfo().values(), LockType.SHARED); if (twoLevel) { dataSelectionService.checkOnline(); @@ -111,13 +125,16 @@ private Response getData(DataSelectionService dataSelectionService, final long o String name; if (outname == null) { if (finalZip) { - name = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()) + ".zip"; + name = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss") + .format(new Date()) + ".zip"; } else { - name = dataSelectionService.getDfInfo().values().iterator().next().getName(); + name = dataSelectionService.getDfInfo().values().iterator() + .next().getName(); } } else { if (finalZip) { - String ext = outname.substring(outname.lastIndexOf(".") + 1, outname.length()); + String ext = outname.substring(outname.lastIndexOf(".") + 1, + outname.length()); if ("zip".equals(ext)) { name = outname; } else { @@ -128,17 +145,24 @@ private Response getData(DataSelectionService dataSelectionService, final long o } } - if (ServiceProvider.getInstance().getPropertyHandler().getLogSet().contains(this.getCallType())) { + if (ServiceProvider.getInstance().getPropertyHandler().getLogSet() + .contains(this.getCallType())) { transferId = transferIdCounter.getAndIncrement(); } - var response = Response.status(offset == 0 ? HttpURLConnection.HTTP_OK : HttpURLConnection.HTTP_PARTIAL) - .entity(new SO(dataSelectionService.getDsInfo(), dataSelectionService.getDfInfo(), offset, finalZip, compress, lock, transferId, ip, start, serviceProvider)) - .header("Content-Disposition", "attachment; filename=\"" + name + "\"").header("Accept-Ranges", "bytes"); - length.stream() - .map(l -> Math.max(0L, l - offset)) + var response = Response + .status(offset == 0 ? HttpURLConnection.HTTP_OK + : HttpURLConnection.HTTP_PARTIAL) + .entity(new SO(dataSelectionService.getDsInfo(), + dataSelectionService.getDfInfo(), offset, finalZip, + compress, lock, transferId, ip, start, + serviceProvider)) + .header("Content-Disposition", + "attachment; filename=\"" + name + "\"") + .header("Accept-Ranges", "bytes"); + length.stream().map(l -> Math.max(0L, l - offset)) .forEach(l -> response.header(CONTENT_LENGTH, l)); - + return response.build(); } catch (AlreadyLockedException e) { @@ -146,7 +170,7 @@ private Response getData(DataSelectionService dataSelectionService, final long o throw new DataNotOnlineException("Data is busy"); } catch (IOException e) { // if (lock != null) { - // lock.release(); + // lock.release(); // } logger.error("I/O error " + e.getMessage()); throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -156,38 +180,48 @@ private Response getData(DataSelectionService dataSelectionService, final long o } } - private void checkDatafilesPresent(Collection dfInfos) throws NotFoundException, InternalException { var serviceProvider = ServiceProvider.getInstance(); /* Check that datafiles have not been deleted before locking */ int n = 0; - StringBuffer sb = new StringBuffer("SELECT COUNT(df) from Datafile df WHERE (df.id in ("); + StringBuffer sb = new StringBuffer( + "SELECT COUNT(df) from Datafile df WHERE (df.id in ("); for (DataInfoBase dfInfo : dfInfos) { if (n != 0) { sb.append(','); } sb.append(dfInfo.getId()); - if (++n == serviceProvider.getPropertyHandler().getMaxIdsInQuery()) { + if (++n == serviceProvider.getPropertyHandler() + .getMaxIdsInQuery()) { try { - if (((Long) serviceProvider.getIcatReader().search(sb.append("))").toString()).get(0)).intValue() != n) { - throw new NotFoundException("One of the data files requested has been deleted"); + if (((Long) serviceProvider.getIcatReader() + .search(sb.append("))").toString()).get(0)) + .intValue() != n) { + throw new NotFoundException( + "One of the data files requested has been deleted"); } n = 0; - sb = new StringBuffer("SELECT COUNT(df) from Datafile df WHERE (df.id in ("); + sb = new StringBuffer( + "SELECT COUNT(df) from Datafile df WHERE (df.id in ("); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage()); } } } if (n != 0) { try { - if (((Long) serviceProvider.getIcatReader().search(sb.append("))").toString()).get(0)).intValue() != n) { - throw new NotFoundException("One of the datafiles requested has been deleted"); + if (((Long) serviceProvider.getIcatReader() + .search(sb.append("))").toString()).get(0)) + .intValue() != n) { + throw new NotFoundException( + "One of the datafiles requested has been deleted"); } } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage()); } } @@ -200,7 +234,8 @@ public CallType getCallType() { @Override public void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - if(transferId == null) transferId = -1L; + if (transferId == null) + transferId = -1L; gen.write("transferId", transferId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java index 94a78919..70942624 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java @@ -12,7 +12,6 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; import org.icatproject.ids.services.ServiceProvider; - public class GetIcatUrlHandler extends RequestHandlerBase { public GetIcatUrlHandler(String ip) { @@ -20,11 +19,12 @@ public GetIcatUrlHandler(String ip) { } @Override - public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { - var propertyHandler = ServiceProvider.getInstance().getPropertyHandler(); + var propertyHandler = ServiceProvider.getInstance() + .getPropertyHandler(); return new ValueContainer(propertyHandler.getIcatUrl()); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java index 713c3603..04909b0b 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java @@ -2,8 +2,11 @@ import java.util.Set; +import jakarta.json.stream.JsonGenerator; + import org.icatproject.IcatExceptionType; import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -16,8 +19,6 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; import org.icatproject.ids.services.ServiceProvider; -import jakarta.json.stream.JsonGenerator; - public class GetServiceStatusHandler extends RequestHandlerBase { private Set rootUserNames; @@ -28,24 +29,27 @@ public GetServiceStatusHandler(String ip, String sessionId) { super(RequestType.GETSERVICESTATUS, ip); this.sessionId = sessionId; - rootUserNames = ServiceProvider.getInstance().getPropertyHandler().getRootUserNames(); + rootUserNames = ServiceProvider.getInstance().getPropertyHandler() + .getRootUserNames(); this.serviceProvider = ServiceProvider.getInstance(); } @Override - public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { try { String uname = serviceProvider.getIcat().getUserName(sessionId); if (!rootUserNames.contains(uname)) { - throw new InsufficientPrivilegesException(uname + " is not included in the ids rootUserNames set."); + throw new InsufficientPrivilegesException(uname + + " is not included in the ids rootUserNames set."); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); if (type == IcatExceptionType.SESSION) { - throw new InsufficientPrivilegesException(e.getClass() + " " + e.getMessage()); + throw new InsufficientPrivilegesException( + e.getClass() + " " + e.getMessage()); } throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -54,7 +58,8 @@ public ValueContainer handleRequest() } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { gen.write("userName", serviceProvider.getIcat().getUserName(sessionId)); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java index 9da9787c..8f4c145f 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java @@ -19,21 +19,27 @@ public class GetStatusHandler extends DataRequestHandler { - public GetStatusHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETSTATUS, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public GetStatusHandler(String ip, String preparedId, String sessionId, + String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETSTATUS, ip, preparedId, sessionId, + investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { Status status = Status.ONLINE; var serviceProvider = ServiceProvider.getInstance(); Set restoring = serviceProvider.getFsm().getRestoring(); - Set maybeOffline = serviceProvider.getFsm().getMaybeOffline(); - for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { + Set maybeOffline = serviceProvider.getFsm() + .getMaybeOffline(); + for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos() + .values()) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (restoring.contains(dataInfo)) { status = Status.RESTORING; diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java index 6f2deba7..53660e58 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java @@ -34,17 +34,23 @@ class PreparedStatus { private Map preparedStatusMap = new ConcurrentHashMap<>(); @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { // Do it boolean prepared = true; - PreparedStatus status = preparedStatusMap.computeIfAbsent(this.dataController.getOperationId(), k -> new PreparedStatus()); + PreparedStatus status = preparedStatusMap.computeIfAbsent( + this.dataController.getOperationId(), + k -> new PreparedStatus()); if (!status.lock.tryLock()) { - logger.debug("Lock held for evaluation of isPrepared for preparedId {}", this.dataController.getOperationId()); + logger.debug( + "Lock held for evaluation of isPrepared for preparedId {}", + this.dataController.getOperationId()); return new ValueContainer(false); } try { @@ -54,19 +60,23 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic try { future.get(); } catch (ExecutionException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } catch (InterruptedException e) { // Ignore } finally { status.future = null; } } else { - logger.debug("Background process still running for preparedId {}", this.dataController.getOperationId()); + logger.debug( + "Background process still running for preparedId {}", + this.dataController.getOperationId()); return new ValueContainer(false); } } - prepared = dataSelectionService.isPrepared(this.dataController.getOperationId()); + prepared = dataSelectionService + .isPrepared(this.dataController.getOperationId()); return new ValueContainer(prepared); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java index a7acf20e..15c93a26 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java @@ -12,16 +12,16 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; public class IsReadOnlyHandler extends RequestHandlerBase { - + public IsReadOnlyHandler(String ip) { super(RequestType.ISREADONLY, ip); } @Override - public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { return new ValueContainer(this.readOnly); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java index f9cc3be1..1ffd2326 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java @@ -18,10 +18,10 @@ public IsTwoLevelHandler(String ip) { } @Override - public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { - + public ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { + return new ValueContainer(twoLevel); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java index 625c4ac2..dc7edad6 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java @@ -6,6 +6,8 @@ import java.nio.file.Files; import java.util.UUID; +import jakarta.json.stream.JsonGenerator; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestIdNames; import org.icatproject.ids.enums.RequestType; @@ -20,28 +22,31 @@ import org.icatproject.ids.requestHandlers.base.DataRequestHandler; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; -import jakarta.json.stream.JsonGenerator; - public class PrepareDataHandler extends DataRequestHandler { Boolean compress; Boolean zip; String preparedId; - public PrepareDataHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds, Boolean compress, Boolean zip) { - super(RequestType.PREPAREDATA,ip, sessionId, investigationIds, datasetIds, datafileIds); + public PrepareDataHandler(String ip, String sessionId, + String investigationIds, String datasetIds, String datafileIds, + Boolean compress, Boolean zip) { + super(RequestType.PREPAREDATA, ip, sessionId, investigationIds, + datasetIds, datafileIds); this.zip = zip; this.compress = compress; } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { preparedId = UUID.randomUUID().toString(); - + dataSelectionService.restoreDataInfos(); if (dataSelectionService.mustZip()) { @@ -49,15 +54,14 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic } logger.debug("Writing to " + preparedDir.resolve(preparedId)); - try (OutputStream stream = new BufferedOutputStream(Files.newOutputStream(preparedDir.resolve(preparedId)))) { - Prepared.pack( stream, - zip, - compress, - dataSelectionService.getDsInfo(), - dataSelectionService.getDfInfo(), - dataSelectionService.getEmptyDatasets(), - dataSelectionService.getFileLength().isEmpty() ? 0 : dataSelectionService.getFileLength().getAsLong() - ); + try (OutputStream stream = new BufferedOutputStream( + Files.newOutputStream(preparedDir.resolve(preparedId)))) { + Prepared.pack(stream, zip, compress, + dataSelectionService.getDsInfo(), + dataSelectionService.getDfInfo(), + dataSelectionService.getEmptyDatasets(), + dataSelectionService.getFileLength().isEmpty() ? 0 + : dataSelectionService.getFileLength().getAsLong()); } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -68,13 +72,14 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic } @Override - public String addCustomParametersToLogString() { + public String addCustomParametersToLogString() { return "zip='" + this.zip + "'' compress='" + compress + "'"; } @Override public void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - if(preparedId == null) preparedId = ""; + if (preparedId == null) + preparedId = ""; gen.write(RequestIdNames.preparedId, preparedId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java index 3e9bfbd9..7574ca25 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java @@ -13,12 +13,18 @@ import java.util.zip.CRC32; import javax.xml.datatype.DatatypeFactory; +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; +import jakarta.ws.rs.core.Response; + import org.icatproject.Datafile; import org.icatproject.DatafileFormat; import org.icatproject.Dataset; import org.icatproject.ICAT; import org.icatproject.IcatExceptionType; import org.icatproject.IcatException_Exception; +import org.icatproject.utils.IcatSecurity; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestIdNames; @@ -33,22 +39,17 @@ import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.helpers.CheckedWithSizeInputStream; import org.icatproject.ids.helpers.ValueContainer; -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataControllerBase; import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; import org.icatproject.ids.services.dataSelectionService.DataSelectionServiceFactory; -import org.icatproject.utils.IcatSecurity; - -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; -import jakarta.ws.rs.core.Response; public class PutHandler extends RequestHandlerBase { @@ -79,9 +80,10 @@ public class PutHandler extends RequestHandlerBase { paddedPrefix += "*/window.name='"; } - public PutHandler( String ip, String sessionId, InputStream body, String name, String datafileFormatIdString, - String datasetIdString, String description, String doi, String datafileCreateTimeString, - String datafileModTimeString, boolean wrap, boolean padding) { + public PutHandler(String ip, String sessionId, InputStream body, + String name, String datafileFormatIdString, String datasetIdString, + String description, String doi, String datafileCreateTimeString, + String datafileModTimeString, boolean wrap, boolean padding) { super(RequestType.PUT, ip); this.sessionId = sessionId; @@ -105,52 +107,61 @@ public PutHandler( String ip, String sessionId, InputStream body, String name, } catch (Throwable e) { logger.error("Won't start ", e); - throw new RuntimeException("PutHandler reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException("PutHandler reports " + e.getClass() + + " " + e.getMessage()); } } @Override - public ValueContainer handleRequest() - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { try { if (readOnly) { - throw new NotImplementedException("This operation has been configured to be unavailable"); + throw new NotImplementedException( + "This operation has been configured to be unavailable"); } - DataControllerBase.validateUUID(RequestIdNames.sessionId, sessionId); + DataControllerBase.validateUUID(RequestIdNames.sessionId, + sessionId); if (name == null) { throw new BadRequestException("The name parameter must be set"); } if (datafileFormatIdString == null) { - throw new BadRequestException("The datafileFormatId parameter must be set"); + throw new BadRequestException( + "The datafileFormatId parameter must be set"); } long datafileFormatId; try { datafileFormatId = Long.parseLong(datafileFormatIdString); } catch (NumberFormatException e) { - throw new BadRequestException("The datafileFormatId parameter must be numeric"); + throw new BadRequestException( + "The datafileFormatId parameter must be numeric"); } if (datasetIdString == null) { - throw new BadRequestException("The datasetId parameter must be set"); + throw new BadRequestException( + "The datasetId parameter must be set"); } long datasetId; try { datasetId = Long.parseLong(datasetIdString); } catch (NumberFormatException e) { - throw new BadRequestException("The datasetId parameter must be numeric"); + throw new BadRequestException( + "The datasetId parameter must be numeric"); } Long datafileCreateTime = null; if (datafileCreateTimeString != null) { try { - datafileCreateTime = Long.parseLong(datafileCreateTimeString); + datafileCreateTime = Long + .parseLong(datafileCreateTimeString); } catch (NumberFormatException e) { - throw new BadRequestException("The datafileCreateTime parameter must be numeric"); + throw new BadRequestException( + "The datafileCreateTime parameter must be numeric"); } } @@ -159,17 +170,20 @@ public ValueContainer handleRequest() try { datafileModTime = Long.parseLong(datafileModTimeString); } catch (NumberFormatException e) { - throw new BadRequestException("The datafileModTime parameter must be numeric"); + throw new BadRequestException( + "The datafileModTime parameter must be numeric"); } } // Do it Dataset ds; try { - ds = (Dataset) serviceProvider.getIcat().get(sessionId, "Dataset INCLUDE Investigation, Facility", datasetId); + ds = (Dataset) serviceProvider.getIcat().get(sessionId, + "Dataset INCLUDE Investigation, Facility", datasetId); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES + || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -179,59 +193,74 @@ public ValueContainer handleRequest() } DatasetInfo dsInfo = new DatasetInfo(ds); - try (Lock lock = serviceProvider.getLockManager().lock(dsInfo, LockType.SHARED)) { + try (Lock lock = serviceProvider.getLockManager().lock(dsInfo, + LockType.SHARED)) { if (storageUnit == StorageUnit.DATASET) { var dfInfos = new TreeMap(); Set emptyDatasets = new HashSet<>(); try { - List counts = serviceProvider.getIcat().search(sessionId, - "COUNT(Datafile) <-> Dataset [id=" + dsInfo.getId() + "]"); + List counts = serviceProvider.getIcat().search( + sessionId, "COUNT(Datafile) <-> Dataset [id=" + + dsInfo.getId() + "]"); if ((Long) counts.get(0) == 0) { emptyDatasets.add(dsInfo.getId()); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { - throw new InsufficientPrivilegesException(e.getMessage()); + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES + || type == IcatExceptionType.SESSION) { + throw new InsufficientPrivilegesException( + e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } - throw new InternalException(type + " " + e.getMessage()); + throw new InternalException( + type + " " + e.getMessage()); } var dsInfos = new TreeMap(); dsInfos.put(dsInfo.getId(), dsInfo); - DataSelectionService dataSelectionService = DataSelectionServiceFactory.getService(dsInfos, dfInfos, emptyDatasets, 0, this.getRequestType()); + DataSelectionService dataSelectionService = DataSelectionServiceFactory + .getService(dsInfos, dfInfos, emptyDatasets, 0, + this.getRequestType()); dataSelectionService.checkOnline(); } CRC32 crc = new CRC32(); - CheckedWithSizeInputStream is = new CheckedWithSizeInputStream(body, crc); + CheckedWithSizeInputStream is = new CheckedWithSizeInputStream( + body, crc); String location; try { - location = serviceProvider.getMainStorage().put(dsInfo, name, is); + location = serviceProvider.getMainStorage().put(dsInfo, + name, is); } catch (IllegalArgumentException e) { - throw new BadRequestException("Illegal filename or dataset: " + e.getMessage()); + throw new BadRequestException( + "Illegal filename or dataset: " + e.getMessage()); } is.close(); long checksum = crc.getValue(); long size = is.getSize(); - + try { - dfId = registerDatafile(sessionId, name, datafileFormatId, location, checksum, size, ds, - description, doi, datafileCreateTime, datafileModTime); - } catch (InsufficientPrivilegesException | NotFoundException | InternalException - | BadRequestException e) { - logger.debug("Problem with registration " + e.getClass() + " " + e.getMessage() + dfId = registerDatafile(sessionId, name, datafileFormatId, + location, checksum, size, ds, description, doi, + datafileCreateTime, datafileModTime); + } catch (InsufficientPrivilegesException | NotFoundException + | InternalException | BadRequestException e) { + logger.debug("Problem with registration " + e.getClass() + + " " + e.getMessage() + " datafile will now be deleted"); String userId = null; try { - userId = serviceProvider.getIcat().getUserName(sessionId); + userId = serviceProvider.getIcat() + .getUserName(sessionId); } catch (IcatException_Exception e1) { - logger.error("Unable to get user name for session " + sessionId + " so mainStorage.delete of " + logger.error("Unable to get user name for session " + + sessionId + " so mainStorage.delete of " + location + " may fail"); } - serviceProvider.getMainStorage().delete(location, userId, userId); + serviceProvider.getMainStorage().delete(location, userId, + userId); throw e; } @@ -240,62 +269,80 @@ public ValueContainer handleRequest() } else if (storageUnit == StorageUnit.DATAFILE) { Datafile df; try { - df = (Datafile) serviceProvider.getIcatReader().get("Datafile", dfId); + df = (Datafile) serviceProvider.getIcatReader() + .get("Datafile", dfId); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + + " " + e.getMessage()); } - serviceProvider.getFsm().queue(new DatafileInfo(dfId, name, location, df.getCreateId(), df.getModId(), dsInfo.getId()), - DeferredOp.WRITE); + serviceProvider.getFsm() + .queue(new DatafileInfo(dfId, name, location, + df.getCreateId(), df.getModId(), + dsInfo.getId()), DeferredOp.WRITE); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Json.createGenerator(baos).writeStartObject().write("id", dfId).write("checksum", checksum) - .write("location", location.replace("\\", "\\\\").replace("'", "\\'")).write("size", size) - .writeEnd().close(); - String resp = wrap ? prefix + baos.toString() + suffix : baos.toString(); - - return new ValueContainer(Response.status(HttpURLConnection.HTTP_CREATED).entity(resp).build()); + Json.createGenerator(baos).writeStartObject().write("id", dfId) + .write("checksum", checksum) + .write("location", + location.replace("\\", "\\\\").replace("'", + "\\'")) + .write("size", size).writeEnd().close(); + String resp = wrap ? prefix + baos.toString() + suffix + : baos.toString(); + + return new ValueContainer( + Response.status(HttpURLConnection.HTTP_CREATED) + .entity(resp).build()); } catch (AlreadyLockedException e) { logger.debug("Could not acquire lock, put failed"); throw new DataNotOnlineException("Data is busy"); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " putting " + name + " to Dataset with id " - + datasetIdString); - throw new InternalException(e.getClass() + " " + e.getMessage()); + logger.error("I/O exception " + e.getMessage() + " putting " + + name + " to Dataset with id " + datasetIdString); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IdsException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen.writeStartObject().write("code", e.getClass().getSimpleName()).write("message", e.getShortMessage()); + gen.writeStartObject().write("code", e.getClass().getSimpleName()) + .write("message", e.getShortMessage()); gen.writeEnd().close(); if (wrap) { String pre = padding ? paddedPrefix : prefix; - return new ValueContainer(Response.status(e.getHttpStatusCode()).entity(pre + baos.toString().replace("'", "\\'") + suffix) + return new ValueContainer(Response + .status(e.getHttpStatusCode()).entity(pre + + baos.toString().replace("'", "\\'") + suffix) .build()); } else { - return new ValueContainer(Response.status(e.getHttpStatusCode()).entity(baos.toString()).build()); + return new ValueContainer(Response.status(e.getHttpStatusCode()) + .entity(baos.toString()).build()); } } } + private Long registerDatafile(String sessionId, String name, + long datafileFormatId, String location, long checksum, long size, + Dataset dataset, String description, String doi, + Long datafileCreateTime, Long datafileModTime) + throws InsufficientPrivilegesException, NotFoundException, + InternalException, BadRequestException { - private Long registerDatafile(String sessionId, String name, long datafileFormatId, String location, long checksum, - long size, Dataset dataset, String description, String doi, Long datafileCreateTime, Long datafileModTime) - throws InsufficientPrivilegesException, NotFoundException, InternalException, BadRequestException { - - var serviceProvider = ServiceProvider.getInstance(); ICAT icat = serviceProvider.getIcat(); - + final Datafile df = new Datafile(); DatafileFormat format; try { - format = (DatafileFormat) icat.get(sessionId, "DatafileFormat", datafileFormatId); + format = (DatafileFormat) icat.get(sessionId, "DatafileFormat", + datafileFormatId); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES + || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -315,12 +362,14 @@ private Long registerDatafile(String sessionId, String name, long datafileFormat if (datafileCreateTime != null) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTimeInMillis(datafileCreateTime); - df.setDatafileCreateTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); + df.setDatafileCreateTime( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); } if (datafileModTime != null) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTimeInMillis(datafileModTime); - df.setDatafileModTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); + df.setDatafileModTime( + datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); } try { long dfId = icat.create(sessionId, df); @@ -328,15 +377,18 @@ private Long registerDatafile(String sessionId, String name, long datafileFormat String key = serviceProvider.getPropertyHandler().getKey(); if (key != null) { - df.setLocation(location + " " + IcatSecurity.digest(dfId, location, key)); + df.setLocation(location + " " + + IcatSecurity.digest(dfId, location, key)); icat.update(sessionId, df); } - logger.debug("Registered datafile for dataset {} for {}", dataset.getId(), name + " at " + location); + logger.debug("Registered datafile for dataset {} for {}", + dataset.getId(), name + " at " + location); return dfId; } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES + || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.VALIDATION) { @@ -350,12 +402,15 @@ private Long registerDatafile(String sessionId, String name, long datafileFormat public String addParametersToLogString() { return "name='" + name + "' " + "datafileFormatId='" - + datafileFormatIdString + "' " + "datasetId='" + datasetIdString + "' " + "description='" - + description + "' " + "doi='" + doi + "' " + "datafileCreateTime='" + datafileCreateTimeString - + "' " + "datafileModTime='" + datafileModTimeString + "'"; + + datafileFormatIdString + "' " + "datasetId='" + + datasetIdString + "' " + "description='" + description + "' " + + "doi='" + doi + "' " + "datafileCreateTime='" + + datafileCreateTimeString + "' " + "datafileModTime='" + + datafileModTimeString + "'"; } - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { gen.write("userName", serviceProvider.getIcat().getUserName(sessionId)); gen.write("datafileId", dfId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java index 2fff2bb3..b8f6e667 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java @@ -15,20 +15,24 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; - public class ResetHandler extends DataRequestHandler { - public ResetHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.RESET, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public ResetHandler(String ip, String preparedId, String sessionId, + String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.RESET, ip, preparedId, sessionId, investigationIds, + datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { FiniteStateMachine fsm = ServiceProvider.getInstance().getFsm(); - for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { + for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos() + .values()) { fsm.recordSuccess(dataInfo.getId()); } @@ -39,7 +43,5 @@ public ValueContainer handleDataRequest(DataSelectionService dataSelectionServic public CallType getCallType() { return CallType.MIGRATE; } - - } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java index 77e0c7e5..3f3c1e81 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java @@ -15,16 +15,20 @@ public class RestoreHandler extends DataRequestHandler { - public RestoreHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.RESTORE, ip, sessionId, investigationIds, datasetIds, datafileIds); + public RestoreHandler(String ip, String sessionId, String investigationIds, + String datasetIds, String datafileIds) { + super(RequestType.RESTORE, ip, sessionId, investigationIds, datasetIds, + datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - dataSelectionService.scheduleTasks(DeferredOp.RESTORE); + dataSelectionService.scheduleTasks(DeferredOp.RESTORE); return ValueContainer.getVoid(); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java index c6f994f4..59d564ad 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java @@ -16,40 +16,48 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataRequestHandler; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; public class WriteHandler extends DataRequestHandler { - public WriteHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.WRITE, ip, sessionId, investigationIds, datasetIds, datafileIds); + public WriteHandler(String ip, String sessionId, String investigationIds, + String datasetIds, String datafileIds) { + super(RequestType.WRITE, ip, sessionId, investigationIds, datasetIds, + datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var serviceProvider = ServiceProvider.getInstance(); if (!serviceProvider.getPropertyHandler().getEnableWrite()) { - throw new NotImplementedException("This operation has been configured to be unavailable"); + throw new NotImplementedException( + "This operation has been configured to be unavailable"); } Map dsInfos = dataSelectionService.getDsInfo(); - try (Lock lock = serviceProvider.getLockManager().lock(dsInfos.values(), LockType.SHARED)) { + try (Lock lock = serviceProvider.getLockManager().lock(dsInfos.values(), + LockType.SHARED)) { if (twoLevel) { boolean maybeOffline = false; - for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { + for (DataInfoBase dataInfo : dataSelectionService + .getPrimaryDataInfos().values()) { if (!dataSelectionService.existsInMainStorage(dataInfo)) { maybeOffline = true; } } if (maybeOffline) { - throw new DataNotOnlineException("Requested data is not online, write request refused"); + throw new DataNotOnlineException( + "Requested data is not online, write request refused"); } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java b/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java index 1291e98e..25d6eeab 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java @@ -2,7 +2,10 @@ import java.util.regex.Pattern; +import jakarta.json.stream.JsonGenerator; + import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; import org.icatproject.ids.exceptions.InsufficientPrivilegesException; @@ -12,52 +15,64 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; -import jakarta.json.stream.JsonGenerator; - public abstract class DataControllerBase { /** * matches standard UUID format of 8-4-4-4-12 hexadecimal digits */ - public static final Pattern uuidRegExp = Pattern - .compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); + public static final Pattern uuidRegExp = Pattern.compile( + "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); - public abstract DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException; + public abstract DataSelectionService provideDataSelectionService( + RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, + InsufficientPrivilegesException, NotImplementedException; public abstract void validateUUID() throws BadRequestException; public abstract String addParametersToLogString(); - public abstract void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException; + public abstract void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException; - public abstract boolean mustZip(boolean zip, DataSelectionService dataSelectionService); + public abstract boolean mustZip(boolean zip, + DataSelectionService dataSelectionService); public abstract String getOperationId(); /** - * returns the current sessionId. If it's null or not defined it should be created + * returns the current sessionId. If it's null or not defined it should be + * created + * * @return - * @throws InternalException + * @throws InternalException */ public abstract String forceGetSessionId() throws InternalException; /** * Provides a validity check for UUIDs - * @param thing You can give here a name of the prameter or whatever has been checked here (to provide a qualified error message if needed). - * @param id The String which has to be checked if it is a valid UUID + * + * @param thing You can give here a name of the prameter or whatever has + * been checked here (to provide a qualified error message if + * needed). + * @param id The String which has to be checked if it is a valid UUID * @throws BadRequestException */ - public static void validateUUID(String thing, String id) throws BadRequestException { + public static void validateUUID(String thing, String id) + throws BadRequestException { if (id == null || !uuidRegExp.matcher(id).matches()) - throw new BadRequestException("The " + thing + " parameter '" + id + "' is not a valid UUID"); + throw new BadRequestException("The " + thing + " parameter '" + id + + "' is not a valid UUID"); } protected String createSessionId() throws InternalException { String sessionId; try { - sessionId = ServiceProvider.getInstance().getIcatReader().getSessionId(); + sessionId = ServiceProvider.getInstance().getIcatReader() + .getSessionId(); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage()); } return sessionId; } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java index 72576fa5..691bcee8 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java @@ -1,6 +1,9 @@ package org.icatproject.ids.requestHandlers.base; +import jakarta.json.stream.JsonGenerator; + import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; import org.icatproject.ids.exceptions.DataNotOnlineException; @@ -11,31 +14,34 @@ import org.icatproject.ids.helpers.ValueContainer; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; -import jakarta.json.stream.JsonGenerator; - - public abstract class DataRequestHandler extends RequestHandlerBase { protected DataControllerBase dataController; - - protected DataRequestHandler(RequestType requestType, String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + protected DataRequestHandler(RequestType requestType, String ip, + String sessionId, String investigationIds, String datasetIds, + String datafileIds) { super(requestType, ip); - this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); + this.dataController = new UnpreparedDataController(sessionId, + investigationIds, datasetIds, datafileIds); } - protected DataRequestHandler(RequestType requestType, String ip, String preparedId) { + protected DataRequestHandler(RequestType requestType, String ip, + String preparedId) { super(requestType, ip); this.dataController = new PreparedDataController(preparedId); } - protected DataRequestHandler(RequestType requestType, String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + protected DataRequestHandler(RequestType requestType, String ip, + String preparedId, String sessionId, String investigationIds, + String datasetIds, String datafileIds) { super(requestType, ip); - if(sessionId != null) { - this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); + if (sessionId != null) { + this.dataController = new UnpreparedDataController(sessionId, + investigationIds, datasetIds, datafileIds); } else { this.dataController = new PreparedDataController(preparedId); } @@ -43,11 +49,14 @@ protected DataRequestHandler(RequestType requestType, String ip, String prepared } @Override - protected ValueContainer handleRequest() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + protected ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { this.dataController.validateUUID(); - DataSelectionService dataSelectionService = this.dataController.provideDataSelectionService(this.requestType); + DataSelectionService dataSelectionService = this.dataController + .provideDataSelectionService(this.requestType); ValueContainer result = this.handleDataRequest(dataSelectionService); return result; @@ -55,26 +64,38 @@ protected ValueContainer handleRequest() throws BadRequestException, InternalExc } @Override - protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + protected void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { this.dataController.addParametersToTransmitterJSON(gen); this.addCustomParametersToTransmitterJSON(gen); } - protected abstract ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws NotImplementedException, InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException; + protected abstract ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws NotImplementedException, InternalException, + BadRequestException, NotFoundException, + InsufficientPrivilegesException, DataNotOnlineException; @Override protected String addParametersToLogString() { - return this.dataController.addParametersToLogString() + " " + this.addCustomParametersToLogString(); + return this.dataController.addParametersToLogString() + " " + + this.addCustomParametersToLogString(); } /** - * Override this method in your concrete DataRequestHandler to add custom parameters to the JSON which will be transmitted. + * Override this method in your concrete DataRequestHandler to add custom + * parameters to the JSON which will be transmitted. + * * @param gen */ - protected void addCustomParametersToTransmitterJSON(JsonGenerator gen) {} + protected void addCustomParametersToTransmitterJSON(JsonGenerator gen) { + } /** - * Override this method in your concrete DataRequestHandler to add custom parameters to the log output. + * Override this method in your concrete DataRequestHandler to add custom + * parameters to the log output. */ - protected String addCustomParametersToLogString() { return "";} + protected String addCustomParametersToLogString() { + return ""; + } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java b/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java index f9fe6519..b1eb3c54 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java @@ -5,7 +5,13 @@ import java.nio.file.Files; import java.nio.file.NoSuchFileException; +import jakarta.json.stream.JsonGenerator; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.RequestIdNames; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -17,14 +23,11 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; import org.icatproject.ids.services.dataSelectionService.DataSelectionServiceFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import jakarta.json.stream.JsonGenerator; public class PreparedDataController extends DataControllerBase { - protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); + protected final static Logger logger = LoggerFactory + .getLogger(RequestHandlerBase.class); String preparedId; @@ -34,16 +37,21 @@ public PreparedDataController(String preparedId) { } @Override - public DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, - BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - - var preparedDir = ServiceProvider.getInstance().getPropertyHandler().getCacheDir().resolve("prepared"); + public DataSelectionService provideDataSelectionService( + RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, + InsufficientPrivilegesException, NotImplementedException { + + var preparedDir = ServiceProvider.getInstance().getPropertyHandler() + .getCacheDir().resolve("prepared"); Prepared prepared; - try (InputStream stream = Files.newInputStream(preparedDir.resolve(preparedId))) { + try (InputStream stream = Files + .newInputStream(preparedDir.resolve(preparedId))) { prepared = Prepared.unpack(stream); } catch (NoSuchFileException e) { - throw new NotFoundException("The preparedId " + preparedId + " is not known"); + throw new NotFoundException( + "The preparedId " + preparedId + " is not known"); } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -62,12 +70,14 @@ public String addParametersToLogString() { } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { gen.write(RequestIdNames.preparedId, this.preparedId); } @Override - public boolean mustZip(boolean zip, DataSelectionService dataSelectionService) { + public boolean mustZip(boolean zip, + DataSelectionService dataSelectionService) { return zip; } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java b/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java index 9524b7a0..ba583b93 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java @@ -4,13 +4,14 @@ import java.nio.file.Path; import java.util.regex.Pattern; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import jakarta.json.Json; import jakarta.json.stream.JsonGenerator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.enums.StorageUnit; @@ -23,14 +24,15 @@ import org.icatproject.ids.helpers.ValueContainer; import org.icatproject.ids.services.ServiceProvider; - /** - * This base class represents all common properties and methods which are needed by each request handler. - * Request handlers schould be added to the internal request handler list in RequestHandlerService, to be able to be called. + * This base class represents all common properties and methods which are needed + * by each request handler. Request handlers schould be added to the internal + * request handler list in RequestHandlerService, to be able to be called. */ public abstract class RequestHandlerBase { - protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); + protected final static Logger logger = LoggerFactory + .getLogger(RequestHandlerBase.class); protected Path preparedDir; protected boolean twoLevel; protected StorageUnit storageUnit; @@ -41,11 +43,10 @@ public abstract class RequestHandlerBase { /** * matches standard UUID format of 8-4-4-4-12 hexadecimal digits */ - public static final Pattern uuidRegExp = Pattern - .compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); - + public static final Pattern uuidRegExp = Pattern.compile( + "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); - protected RequestHandlerBase(RequestType requestType, String ip ) { + protected RequestHandlerBase(RequestType requestType, String ip) { this.requestType = requestType; this.ip = ip; @@ -58,9 +59,9 @@ protected RequestHandlerBase(RequestType requestType, String ip ) { this.readOnly = propertyHandler.getReadOnly(); } - /** * Informs about the RequestType the handler ist providing a handling for. + * * @return */ public RequestType getRequestType() { @@ -68,7 +69,9 @@ public RequestType getRequestType() { } /** - * The core method of each request handler. It has to be overwritten in the concrete implementation to provide an individual request handling + * The core method of each request handler. It has to be overwritten in the + * concrete implementation to provide an individual request handling + * * @return A ValueContainer with an indiviadual result type. * @throws BadRequestException * @throws InternalException @@ -77,11 +80,15 @@ public RequestType getRequestType() { * @throws DataNotOnlineException * @throws NotImplementedException */ - public ValueContainer handle() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handle() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { // some preprocessing long start = System.currentTimeMillis(); - logger.info("New webservice request: " + this.requestType.toString().toLowerCase() + " " + this.addParametersToLogString()); + logger.info("New webservice request: " + + this.requestType.toString().toLowerCase() + " " + + this.addParametersToLogString()); // Do it ValueContainer result = this.handleRequest(); @@ -93,44 +100,56 @@ public ValueContainer handle() throws BadRequestException, InternalException, In } - private void transmit(long start) throws BadRequestException { - if (ServiceProvider.getInstance().getLogSet().contains(this.getCallType())) { + if (ServiceProvider.getInstance().getLogSet() + .contains(this.getCallType())) { try { String body = this.provideTransmissionBody(); - ServiceProvider.getInstance().getTransmitter().processMessage("archive", ip, body, start); + ServiceProvider.getInstance().getTransmitter() + .processMessage("archive", ip, body, start); } catch (IcatException_Exception e) { - logger.error("Failed to prepare jms message " + e.getClass() + " " + e.getMessage()); + logger.error("Failed to prepare jms message " + e.getClass() + + " " + e.getMessage()); } } } - public String provideTransmissionBody() throws BadRequestException, IcatException_Exception { + public String provideTransmissionBody() + throws BadRequestException, IcatException_Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos) + .writeStartObject()) { this.addParametersToTransmitterJSON(gen); gen.writeEnd(); } return baos.toString(); } - /** - * Override to add additional parameters to the log output for the current request + * Override to add additional parameters to the log output for the current + * request + * * @return */ - protected String addParametersToLogString() { return ""; } + protected String addParametersToLogString() { + return ""; + } /** * Override to add additional parameters to the transmitter JSON + * * @param gen * @throws IcatException_Exception * @throws BadRequestException */ - protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException {} + protected void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { + } /** - * The core method of each request handler. It has to be overwritten in the concrete implementation to provide an individual request handling + * The core method of each request handler. It has to be overwritten in the + * concrete implementation to provide an individual request handling + * * @return A ValueContainer with an indiviadual result type. * @throws BadRequestException * @throws InternalException @@ -139,10 +158,15 @@ protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatExce * @throws DataNotOnlineException * @throws NotImplementedException */ - protected abstract ValueContainer handleRequest() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException; + protected abstract ValueContainer handleRequest() + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException; /** - * each handler should provide its own CallType which is needed to create the Transmitter message + * each handler should provide its own CallType which is needed to create + * the Transmitter message + * * @return the Calltype of the request */ protected abstract CallType getCallType(); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java b/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java index 57668b02..f3dac573 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java @@ -1,6 +1,9 @@ package org.icatproject.ids.requestHandlers.base; +import jakarta.json.stream.JsonGenerator; + import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; import org.icatproject.ids.exceptions.InsufficientPrivilegesException; @@ -11,8 +14,6 @@ import org.icatproject.ids.services.dataSelectionService.DataSelectionService; import org.icatproject.ids.services.dataSelectionService.DataSelectionServiceFactory; -import jakarta.json.stream.JsonGenerator; - public class UnpreparedDataController extends DataControllerBase { public String sessionId; @@ -20,7 +21,8 @@ public class UnpreparedDataController extends DataControllerBase { public String datasetIds; public String datafileIds; - public UnpreparedDataController(String sessionId, String investigationIds, String datasetIds, String datafileIds) { + public UnpreparedDataController(String sessionId, String investigationIds, + String datasetIds, String datafileIds) { this.sessionId = sessionId; this.investigationIds = investigationIds; this.datasetIds = datasetIds; @@ -35,41 +37,48 @@ public void validateUUID() throws BadRequestException { @Override public String addParametersToLogString() { return "investigationIds='" + investigationIds + "' " + "datasetIds='" - + datasetIds + "' " + "datafileIds='" + datafileIds + "'"; + + datasetIds + "' " + "datafileIds='" + datafileIds + "'"; } @Override - public DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - return DataSelectionServiceFactory.getService(sessionId, investigationIds, datasetIds, datafileIds, requestType); + public DataSelectionService provideDataSelectionService( + RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, + InsufficientPrivilegesException, NotImplementedException { + return DataSelectionServiceFactory.getService(sessionId, + investigationIds, datasetIds, datafileIds, requestType); } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { - gen.write("userName", ServiceProvider.getInstance().getIcat().getUserName(sessionId)); + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { + gen.write("userName", + ServiceProvider.getInstance().getIcat().getUserName(sessionId)); addIds(gen, investigationIds, datasetIds, datafileIds); } - - - protected void addIds(JsonGenerator gen, String investigationIds, String datasetIds, String datafileIds) - throws BadRequestException { + protected void addIds(JsonGenerator gen, String investigationIds, + String datasetIds, String datafileIds) throws BadRequestException { if (investigationIds != null) { gen.writeStartArray("investigationIds"); - for (long invid : DataSelectionService.getValidIds("investigationIds", investigationIds)) { + for (long invid : DataSelectionService + .getValidIds("investigationIds", investigationIds)) { gen.write(invid); } gen.writeEnd(); } if (datasetIds != null) { gen.writeStartArray("datasetIds"); - for (long invid : DataSelectionService.getValidIds("datasetIds", datasetIds)) { + for (long invid : DataSelectionService.getValidIds("datasetIds", + datasetIds)) { gen.write(invid); } gen.writeEnd(); } if (datafileIds != null) { gen.writeStartArray("datafileIds"); - for (long invid : DataSelectionService.getValidIds("datafileIds", datafileIds)) { + for (long invid : DataSelectionService.getValidIds("datafileIds", + datafileIds)) { gen.write(invid); } gen.writeEnd(); @@ -77,7 +86,8 @@ protected void addIds(JsonGenerator gen, String investigationIds, String dataset } @Override - public boolean mustZip(boolean zip, DataSelectionService dataSelectionService) { + public boolean mustZip(boolean zip, + DataSelectionService dataSelectionService) { return zip ? true : dataSelectionService.mustZip(); } @@ -91,7 +101,7 @@ public String forceGetSessionId() throws InternalException { if (this.sessionId == null) { this.sessionId = this.createSessionId(); } - + return this.sessionId; } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java index 0b4aa315..1b360bc3 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java @@ -3,6 +3,7 @@ import java.util.Map; import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -19,25 +20,32 @@ public class GetSizeHandler extends DataRequestHandler { - public GetSizeHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETSIZE, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); + public GetSizeHandler(String ip, String preparedId, String sessionId, + String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETSIZE, ip, preparedId, sessionId, investigationIds, + datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest( + DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - + logger.debug("Slow computation for normal case"); long size = 0; - size = this.updateSizeFromDataInfoIds(size, dataSelectionService.getDfInfo(), this.dataController.forceGetSessionId()); + size = this.updateSizeFromDataInfoIds(size, + dataSelectionService.getDfInfo(), + this.dataController.forceGetSessionId()); return new ValueContainer(size); } - - protected long updateSizeFromDataInfoIds(long size, Map dataInfos, String sessionId) throws InternalException { + protected long updateSizeFromDataInfoIds(long size, + Map dataInfos, String sessionId) + throws InternalException { StringBuilder sb = new StringBuilder(); int n = 0; for (DataInfoBase dataInfo : dataInfos.values()) { @@ -58,11 +66,13 @@ protected long updateSizeFromDataInfoIds(long size, Map data return size; } - - private long getSizeFor(String sessionId, StringBuilder sb) throws InternalException { - String query = "SELECT SUM(df.fileSize) from Datafile df WHERE df.id IN (" + sb.toString() + ") AND df.location IS NOT NULL"; + private long getSizeFor(String sessionId, StringBuilder sb) + throws InternalException { + String query = "SELECT SUM(df.fileSize) from Datafile df WHERE df.id IN (" + + sb.toString() + ") AND df.location IS NOT NULL"; try { - return (Long) ServiceProvider.getInstance().getIcat().search(sessionId, query).get(0); + return (Long) ServiceProvider.getInstance().getIcat() + .search(sessionId, query).get(0); } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (IndexOutOfBoundsException e) { @@ -74,5 +84,5 @@ private long getSizeFor(String sessionId, StringBuilder sb) throws InternalExcep public CallType getCallType() { return CallType.INFO; } - + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java index 6b57cd9b..3fab7574 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java @@ -2,8 +2,11 @@ import java.util.List; +import jakarta.json.stream.JsonGenerator; + import org.icatproject.Datafile; import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -18,50 +21,62 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; -import jakarta.json.stream.JsonGenerator; - public class GetSizeHandlerForFastProcessing extends RequestHandlerBase { UnpreparedDataController dataController; - public GetSizeHandlerForFastProcessing(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + public GetSizeHandlerForFastProcessing(String ip, String sessionId, + String investigationIds, String datasetIds, String datafileIds) { super(RequestType.GETSIZE, ip); - this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); + this.dataController = new UnpreparedDataController(sessionId, + investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleRequest() throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - + public ValueContainer handleRequest() throws BadRequestException, + InternalException, InsufficientPrivilegesException, + NotFoundException, DataNotOnlineException, NotImplementedException { + this.dataController.validateUUID(); var serviceProvider = ServiceProvider.getInstance(); - List dfids = DataSelectionService.getValidIds("datafileIds", dataController.datafileIds); - List dsids = DataSelectionService.getValidIds("datasetIds", dataController.datasetIds); - List invids = DataSelectionService.getValidIds("investigationIds", dataController.investigationIds); + List dfids = DataSelectionService.getValidIds("datafileIds", + dataController.datafileIds); + List dsids = DataSelectionService.getValidIds("datasetIds", + dataController.datasetIds); + List invids = DataSelectionService.getValidIds("investigationIds", + dataController.investigationIds); - if (dfids.size() + dsids.size() + invids.size() == 1) { long size = 0; - size = getSizeFor(dataController.getOperationId(), invids, "df.dataset.investigation.id") - + getSizeFor(dataController.getOperationId(), dsids, "df.dataset.id") - + getSizeFor(dataController.getOperationId(), dfids, "df.id"); + size = getSizeFor(dataController.getOperationId(), invids, + "df.dataset.investigation.id") + + getSizeFor(dataController.getOperationId(), dsids, + "df.dataset.id") + + getSizeFor(dataController.getOperationId(), dfids, + "df.id"); logger.debug("Fast computation for simple case"); if (size == 0) { try { if (dfids.size() != 0) { - Datafile datafile = (Datafile) serviceProvider.getIcat().get(dataController.getOperationId(), "Datafile", dfids.get(0)); + Datafile datafile = (Datafile) serviceProvider.getIcat() + .get(dataController.getOperationId(), + "Datafile", dfids.get(0)); if (datafile.getLocation() == null) { throw new NotFoundException("Datafile not found"); } } if (dsids.size() != 0) { - serviceProvider.getIcat().get(dataController.getOperationId(), "Dataset", dsids.get(0)); + serviceProvider.getIcat().get( + dataController.getOperationId(), "Dataset", + dsids.get(0)); } if (invids.size() != 0) { - serviceProvider.getIcat().get(dataController.getOperationId(), "Investigation", invids.get(0)); + serviceProvider.getIcat().get( + dataController.getOperationId(), + "Investigation", invids.get(0)); } } catch (IcatException_Exception e) { throw new NotFoundException(e.getMessage()); @@ -70,10 +85,10 @@ public ValueContainer handleRequest() throws BadRequestException, InternalExcept return new ValueContainer(size); } - - return ValueContainer.getInvalid(); //is case of fast computation is not the right way. - + return ValueContainer.getInvalid(); // is case of fast computation is + // not the right way. + } @Override @@ -81,13 +96,17 @@ public CallType getCallType() { return CallType.INFO; } - public String addParametersToLogString() { return this.dataController.addParametersToLogString(); } + public String addParametersToLogString() { + return this.dataController.addParametersToLogString(); + } - public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) + throws IcatException_Exception, BadRequestException { this.dataController.addParametersToTransmitterJSON(gen); } - private long getSizeFor(String sessionId, List ids, String where) throws InternalException { + private long getSizeFor(String sessionId, List ids, String where) + throws InternalException { long size = 0; if (ids != null) { @@ -112,16 +131,19 @@ private long getSizeFor(String sessionId, List ids, String where) throws I return size; } - private long evalSizeFor(String sessionId, String where, StringBuilder sb) throws InternalException { - String query = "SELECT SUM(df.fileSize) from Datafile df WHERE " + where + " IN (" + sb.toString() + ") AND df.location IS NOT NULL"; + private long evalSizeFor(String sessionId, String where, StringBuilder sb) + throws InternalException { + String query = "SELECT SUM(df.fileSize) from Datafile df WHERE " + where + + " IN (" + sb.toString() + ") AND df.location IS NOT NULL"; logger.debug("icat query for size: {}", query); try { - return (Long) ServiceProvider.getInstance().getIcat().search(sessionId, query).get(0); + return (Long) ServiceProvider.getInstance().getIcat() + .search(sessionId, query).get(0); } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (IndexOutOfBoundsException e) { return 0L; } } - + } diff --git a/src/main/java/org/icatproject/ids/services/ICATGetter.java b/src/main/java/org/icatproject/ids/services/ICATGetter.java index 4073c3ce..d62bd126 100644 --- a/src/main/java/org/icatproject/ids/services/ICATGetter.java +++ b/src/main/java/org/icatproject/ids/services/ICATGetter.java @@ -17,19 +17,21 @@ */ public class ICATGetter { - private static String[] suffices = new String[]{"ICATService/ICAT?wsdl", "icat/ICAT?wsdl"}; + private static String[] suffices = new String[] { "ICATService/ICAT?wsdl", + "icat/ICAT?wsdl" }; /** * Provide access to an ICAT SOAP web service with the basic URL string * provided. This exists to hide the differences between containers. * * @param urlString the url of the machine to be contacted. If the url has a - * non-empty file part it is used unchanged, otherwise suffices - * are tried suitable for Glassfish and WildFly. + * non-empty file part it is used unchanged, otherwise + * suffices are tried suitable for Glassfish and WildFly. * @return an ICAT * @throws IcatException_Exception if something is wrong */ - public static ICAT getService(String urlString) throws IcatException_Exception { + public static ICAT getService(String urlString) + throws IcatException_Exception { if (urlString == null) { throwSessionException("Argument to constructor must not be null"); @@ -47,14 +49,16 @@ public static ICAT getService(String urlString) throws IcatException_Exception { for (String suffix : suffices) { String icatUrlWsdl = urlString + "/" + suffix; try { - icatService = new ICATService(new URL(icatUrlWsdl)).getICATPort(); + icatService = new ICATService(new URL(icatUrlWsdl)) + .getICATPort(); icatService.getApiVersion(); return icatService; } catch (MalformedURLException e) { throwSessionException("Invalid URL"); } catch (WebServiceException e) { Throwable cause = e.getCause(); - if (cause != null && cause.getMessage().contains("security")) { + if (cause != null + && cause.getMessage().contains("security")) { throwSessionException(cause.getMessage()); } } catch (Exception e) { @@ -86,7 +90,8 @@ public static String getCleanUrl(String urlString) { for (String suffix : suffices) { suffix = "/" + suffix; if (urlString.endsWith(suffix)) { - return urlString.substring(0, urlString.length() - suffix.length()); + return urlString.substring(0, + urlString.length() - suffix.length()); } } if (urlString.endsWith("/")) { @@ -95,7 +100,8 @@ public static String getCleanUrl(String urlString) { return urlString; } - private static void throwSessionException(String msg) throws IcatException_Exception { + private static void throwSessionException(String msg) + throws IcatException_Exception { IcatException except = new IcatException(); except.setMessage(msg); except.setType(IcatExceptionType.SESSION); diff --git a/src/main/java/org/icatproject/ids/services/IcatReader.java b/src/main/java/org/icatproject/ids/services/IcatReader.java index 9f5adbb8..ab184adf 100644 --- a/src/main/java/org/icatproject/ids/services/IcatReader.java +++ b/src/main/java/org/icatproject/ids/services/IcatReader.java @@ -22,7 +22,8 @@ public class IcatReader { private String sessionId; - private final static Logger logger = LoggerFactory.getLogger(IcatReader.class); + private final static Logger logger = LoggerFactory + .getLogger(IcatReader.class); private PropertyHandler propertyHandler; @@ -31,7 +32,8 @@ public IcatReader() { } - // primarily required for testing to make it possible to use a mocked PropertyHandler + // primarily required for testing to make it possible to use a mocked + // PropertyHandler public IcatReader(PropertyHandler propertyHandler) { this.propertyHandler = propertyHandler; init(); @@ -43,7 +45,8 @@ private void init() { login(); logger.info("Reader started"); } catch (Exception e) { - throw new RuntimeException("Reader reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException( + "Reader reports " + e.getClass() + " " + e.getMessage()); } } @@ -67,7 +70,8 @@ private void login() throws IcatException_Exception { } } - public EntityBaseBean get(String query, long id) throws IcatException_Exception { + public EntityBaseBean get(String query, long id) + throws IcatException_Exception { try { return icat.get(sessionId, query, id); } catch (IcatException_Exception e) { diff --git a/src/main/java/org/icatproject/ids/services/LockManager.java b/src/main/java/org/icatproject/ids/services/LockManager.java index 49beba05..59d6fdcb 100644 --- a/src/main/java/org/icatproject/ids/services/LockManager.java +++ b/src/main/java/org/icatproject/ids/services/LockManager.java @@ -11,6 +11,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; @@ -92,7 +93,9 @@ public void release() { try { storageLock.close(); } catch (Exception e) { - logger.error("Error while closing lock on {} in the storage plugin: {}.", id, e.getMessage()); + logger.error( + "Error while closing lock on {} in the storage plugin: {}.", + id, e.getMessage()); } } logger.debug("Released a lock on {}.", id); @@ -131,7 +134,8 @@ private void init() { logger.debug("LockManager initialized."); } - public Lock lock(DatasetInfo ds, LockType type) throws AlreadyLockedException, IOException { + public Lock lock(DatasetInfo ds, LockType type) + throws AlreadyLockedException, IOException { Long id = ds.getDsId(); assert id != null; synchronized (lockMap) { @@ -139,7 +143,8 @@ public Lock lock(DatasetInfo ds, LockType type) throws AlreadyLockedException, I if (le == null) { le = new LockEntry(id, type); } else { - if (type == LockType.EXCLUSIVE || le.type == LockType.EXCLUSIVE) { + if (type == LockType.EXCLUSIVE + || le.type == LockType.EXCLUSIVE) { throw new AlreadyLockedException(); } } @@ -156,12 +161,16 @@ public Lock lock(DatasetInfo ds, LockType type) throws AlreadyLockedException, I } } - public Lock lock(Collection datasets, LockType type) throws AlreadyLockedException, IOException, InternalException { + public Lock lock(Collection datasets, LockType type) + throws AlreadyLockedException, IOException, InternalException { LockCollection locks = new LockCollection(); try { for (DataInfoBase dataInfo : datasets) { DatasetInfo ds = (DatasetInfo) dataInfo; - if(ds == null) throw new InternalException("Could not cast " + dataInfo.getClass() + " to DataSetInfo. Did you handed over another sub type of DataInfoBase? "); + if (ds == null) + throw new InternalException("Could not cast " + + dataInfo.getClass() + + " to DataSetInfo. Did you handed over another sub type of DataInfoBase? "); locks.add(lock(ds, type)); } } catch (AlreadyLockedException | IOException e) { diff --git a/src/main/java/org/icatproject/ids/services/PropertyHandler.java b/src/main/java/org/icatproject/ids/services/PropertyHandler.java index e3cbe354..ac5e611c 100644 --- a/src/main/java/org/icatproject/ids/services/PropertyHandler.java +++ b/src/main/java/org/icatproject/ids/services/PropertyHandler.java @@ -22,13 +22,14 @@ import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; +import org.icatproject.utils.CheckedProperties; +import org.icatproject.utils.CheckedProperties.CheckedPropertyException; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.plugin.ArchiveStorageInterface; import org.icatproject.ids.plugin.MainStorageInterface; import org.icatproject.ids.plugin.ZipMapperInterface; -import org.icatproject.utils.CheckedProperties; -import org.icatproject.utils.CheckedProperties.CheckedPropertyException; /* * Load the properties specified in the properties file ids.properties. @@ -36,7 +37,8 @@ public class PropertyHandler { private static PropertyHandler instance = null; - private static final Logger logger = LoggerFactory.getLogger(PropertyHandler.class); + private static final Logger logger = LoggerFactory + .getLogger(PropertyHandler.class); public synchronized static PropertyHandler getInstance() { if (instance == null) { @@ -96,12 +98,16 @@ private PropertyHandler() { } preparedCount = props.getPositiveInt("preparedCount"); - processQueueIntervalSeconds = props.getPositiveLong("processQueueIntervalSeconds"); - rootUserNames = new HashSet<>(Arrays.asList(props.getString("rootUserNames").trim().split("\\s+"))); + processQueueIntervalSeconds = props + .getPositiveLong("processQueueIntervalSeconds"); + rootUserNames = new HashSet<>(Arrays.asList( + props.getString("rootUserNames").trim().split("\\s+"))); - reader = Arrays.asList(props.getString("reader").trim().split("\\s+")); + reader = Arrays + .asList(props.getString("reader").trim().split("\\s+")); if (reader.size() % 2 != 1) { - throw new IllegalStateException("reader must have an odd number of words"); + throw new IllegalStateException( + "reader must have an odd number of words"); } readOnly = props.getBoolean("readOnly", false); @@ -111,7 +117,8 @@ private PropertyHandler() { enableWrite = !readOnly; } - sizeCheckIntervalMillis = props.getPositiveInt("sizeCheckIntervalSeconds") * 1000L; + sizeCheckIntervalMillis = props + .getPositiveInt("sizeCheckIntervalSeconds") * 1000L; if (props.has("key")) { key = props.getString("key"); @@ -123,14 +130,17 @@ private PropertyHandler() { zipMapper = klass.getConstructor().newInstance(); logger.debug("ZipMapper initialised"); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException | SecurityException e) { + } catch (ClassNotFoundException | InstantiationException + | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException + | SecurityException e) { abort(e.getClass() + " " + e.getMessage()); } // Now get simple properties to pass to the plugins Properties simpleProps = new Properties(); - try (InputStream is = getClass().getClassLoader().getResourceAsStream("run.properties")) { + try (InputStream is = getClass().getClassLoader() + .getResourceAsStream("run.properties")) { simpleProps.load(is); } catch (IOException e) { abort(e.getClass() + " " + e.getMessage()); @@ -139,54 +149,67 @@ private PropertyHandler() { try { Class klass = (Class) Class .forName(props.getString("plugin.main.class")); - mainStorage = klass.getConstructor(Properties.class).newInstance(simpleProps); + mainStorage = klass.getConstructor(Properties.class) + .newInstance(simpleProps); logger.debug("mainStorage initialised"); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException | SecurityException e) { + } catch (ClassNotFoundException | InstantiationException + | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException + | SecurityException e) { logger.error("Plugin failed...", e); abort(e.getClass() + " " + e.getMessage()); } if (!props.has("plugin.archive.class")) { - logger.info("Property plugin.archive.class not set, single storage enabled."); + logger.info( + "Property plugin.archive.class not set, single storage enabled."); } else { try { Class klass = (Class) Class .forName(props.getString("plugin.archive.class")); - archiveStorage = klass.getConstructor(Properties.class).newInstance(simpleProps); + archiveStorage = klass.getConstructor(Properties.class) + .newInstance(simpleProps); logger.debug("archiveStorage initialised"); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException - | IllegalArgumentException | InvocationTargetException | NoSuchMethodException - | SecurityException e) { + } catch (ClassNotFoundException | InstantiationException + | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException + | SecurityException e) { logger.error("Plugin failed...", e); abort(e.getClass() + " " + e.getMessage()); } - startArchivingLevel = props.getPositiveLong("startArchivingLevel1024bytes") * 1024; - stopArchivingLevel = props.getPositiveLong("stopArchivingLevel1024bytes") * 1024; + startArchivingLevel = props + .getPositiveLong("startArchivingLevel1024bytes") * 1024; + stopArchivingLevel = props + .getPositiveLong("stopArchivingLevel1024bytes") * 1024; if (stopArchivingLevel >= startArchivingLevel) { abort("startArchivingLevel1024bytes must be greater than stopArchivingLevel1024bytes"); } try { String storageUnitName = props.getString("storageUnit"); - storageUnit = StorageUnit.valueOf(storageUnitName.toUpperCase()); + storageUnit = StorageUnit + .valueOf(storageUnitName.toUpperCase()); } catch (IllegalArgumentException e) { List vs = new ArrayList<>(); for (StorageUnit s : StorageUnit.values()) { vs.add(s.name()); } - abort("storageUnit value " + props.getString("storageUnit") + " must be taken from " + vs); + abort("storageUnit value " + props.getString("storageUnit") + + " must be taken from " + vs); } if (storageUnit == StorageUnit.DATASET) { - delayDatasetWrites = props.getPositiveLong("delayDatasetWritesSeconds"); + delayDatasetWrites = props + .getPositiveLong("delayDatasetWritesSeconds"); } else if (storageUnit == StorageUnit.DATAFILE) { - delayDatafileOperations = props.getPositiveLong("delayDatafileOperationsSeconds"); + delayDatafileOperations = props + .getPositiveLong("delayDatafileOperationsSeconds"); } tidyBlockSize = props.getPositiveInt("tidyBlockSize"); } try { - cacheDir = props.getFile("cache.dir").getCanonicalFile().toPath(); + cacheDir = props.getFile("cache.dir").getCanonicalFile() + .toPath(); } catch (IOException e) { abort("IOException " + e.getMessage()); } @@ -197,25 +220,31 @@ private PropertyHandler() { maxIdsInQuery = props.getPositiveInt("maxIdsInQuery"); /* JMS stuff */ - jmsTopicConnectionFactory = props.getString("jms.topicConnectionFactory", + jmsTopicConnectionFactory = props.getString( + "jms.topicConnectionFactory", "java:comp/DefaultJMSConnectionFactory"); /* Call logging categories */ if (props.has("log.list")) { - for (String callTypeString : props.getString("log.list").split("\\s+")) { + for (String callTypeString : props.getString("log.list") + .split("\\s+")) { try { - logSet.add(CallType.valueOf(callTypeString.toUpperCase())); + logSet.add( + CallType.valueOf(callTypeString.toUpperCase())); } catch (IllegalArgumentException e) { - abort("Value " + callTypeString + " in log.list must be chosen from " + abort("Value " + callTypeString + + " in log.list must be chosen from " + Arrays.asList(CallType.values())); } } logger.info("log.list: " + logSet); } else { - logger.info("'log.list' entry not present so no JMS call logging will be performed"); + logger.info( + "'log.list' entry not present so no JMS call logging will be performed"); } - useReaderForPerformance = props.getBoolean("useReaderForPerformance", false); + useReaderForPerformance = props + .getBoolean("useReaderForPerformance", false); } catch (CheckedPropertyException e) { abort(e.getMessage()); @@ -247,7 +276,8 @@ public synchronized ICAT getIcatService() { try { icatService = ICATGetter.getService(icatUrl); } catch (IcatException_Exception e) { - String msg = "Problem finding ICAT API version at " + icatUrl + ": " + e.getFaultInfo().getType() + " " + String msg = "Problem finding ICAT API version at " + icatUrl + + ": " + e.getFaultInfo().getType() + " " + e.getMessage(); logger.error(msg); try { @@ -285,15 +315,19 @@ public synchronized int getMaxEntities() { // will produce an error message. while (maxEntities == null) { try { - org.icatproject.icat.client.ICAT ricat = new org.icatproject.icat.client.ICAT(icatUrl); + org.icatproject.icat.client.ICAT ricat = new org.icatproject.icat.client.ICAT( + icatUrl); try (JsonReader parser = Json - .createReader(new ByteArrayInputStream(ricat.getProperties().getBytes()))) { + .createReader(new ByteArrayInputStream( + ricat.getProperties().getBytes()))) { maxEntities = parser.readObject().getInt("maxEntities"); - logger.info("maxEntities from the ICAT.server {} version {} is {}", icatUrl, ricat.getVersion(), - maxEntities); + logger.info( + "maxEntities from the ICAT.server {} version {} is {}", + icatUrl, ricat.getVersion(), maxEntities); } catch (Exception e) { - String msg = "Problem finding 1 ICAT API version " + e.getClass() + " " + e.getMessage(); + String msg = "Problem finding 1 ICAT API version " + + e.getClass() + " " + e.getMessage(); logger.error(msg); try { Thread.sleep(10000); @@ -302,7 +336,8 @@ public synchronized int getMaxEntities() { } } } catch (URISyntaxException e) { - String msg = "Problem finding 2 ICAT API version " + e.getClass() + " " + e.getMessage(); + String msg = "Problem finding 2 ICAT API version " + + e.getClass() + " " + e.getMessage(); logger.error(msg); throw new IllegalStateException(msg); } diff --git a/src/main/java/org/icatproject/ids/services/ServiceProvider.java b/src/main/java/org/icatproject/ids/services/ServiceProvider.java index dc179ad5..bc999f65 100644 --- a/src/main/java/org/icatproject/ids/services/ServiceProvider.java +++ b/src/main/java/org/icatproject/ids/services/ServiceProvider.java @@ -3,13 +3,15 @@ import java.util.Set; import org.icatproject.ICAT; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.plugin.MainStorageInterface; /** - * This class provides multiple services to the developer - * Maybe it is just for the redesign for version 3 and will later be replaced with dependency injection, when it will be more clear where which service is used. + * This class provides multiple services to the developer Maybe it is just for + * the redesign for version 3 and will later be replaced with dependency + * injection, when it will be more clear where which service is used. */ public class ServiceProvider { @@ -21,7 +23,9 @@ public class ServiceProvider { private IcatReader icatReader; private PropertyHandler propertyHandler; - private ServiceProvider(PropertyHandler propertyHandler, Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { + private ServiceProvider(PropertyHandler propertyHandler, + Transmitter transmitter, FiniteStateMachine fsm, + LockManager lockManager, IcatReader reader) { this.transmitter = transmitter; this.fsm = fsm; this.lockManager = lockManager; @@ -30,27 +34,37 @@ private ServiceProvider(PropertyHandler propertyHandler, Transmitter transmitter } /** - * At first, the ServiceProvider has to be created. Do not call getInstance() before you have called createInstande() + * At first, the ServiceProvider has to be created. Do not call + * getInstance() before you have called createInstande() + * * @param transmitter * @param fsm * @param lockManager * @param reader */ - public static void createInstance(Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { + public static void createInstance(Transmitter transmitter, + FiniteStateMachine fsm, LockManager lockManager, + IcatReader reader) { - createInstance(PropertyHandler.getInstance(), transmitter, fsm, lockManager, reader); + createInstance(PropertyHandler.getInstance(), transmitter, fsm, + lockManager, reader); } - public static void createInstance(PropertyHandler propertyHandler, Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { + public static void createInstance(PropertyHandler propertyHandler, + Transmitter transmitter, FiniteStateMachine fsm, + LockManager lockManager, IcatReader reader) { - if(instance != null) return; + if (instance != null) + return; - instance = new ServiceProvider(propertyHandler, transmitter, fsm, lockManager, reader); + instance = new ServiceProvider(propertyHandler, transmitter, fsm, + lockManager, reader); } public static ServiceProvider getInstance() { - if(instance == null) { - throw new RuntimeException("ServiceProvider is not yet instantiated, please call createInstance at first."); + if (instance == null) { + throw new RuntimeException( + "ServiceProvider is not yet instantiated, please call createInstance at first."); } return instance; } @@ -87,5 +101,4 @@ public Set getLogSet() { return PropertyHandler.getInstance().getLogSet(); } - } diff --git a/src/main/java/org/icatproject/ids/services/Transmitter.java b/src/main/java/org/icatproject/ids/services/Transmitter.java index 996522cb..267e4940 100644 --- a/src/main/java/org/icatproject/ids/services/Transmitter.java +++ b/src/main/java/org/icatproject/ids/services/Transmitter.java @@ -59,12 +59,15 @@ private void exit() { } } - public void processMessage(String operation, String ip, String body, long startMillis) { - try (Session jmsSession = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE)) { + public void processMessage(String operation, String ip, String body, + long startMillis) { + try (Session jmsSession = topicConnection.createSession(false, + Session.AUTO_ACKNOWLEDGE)) { TextMessage jmsg = jmsSession.createTextMessage(body); jmsg.setStringProperty("operation", operation); jmsg.setStringProperty("ip", ip); - jmsg.setLongProperty("millis", System.currentTimeMillis() - startMillis); + jmsg.setLongProperty("millis", + System.currentTimeMillis() - startMillis); jmsg.setLongProperty("start", startMillis); MessageProducer jmsProducer = jmsSession.createProducer(topic); jmsProducer.send(jmsg); diff --git a/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java b/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java index 5f9b372b..ed5c2031 100644 --- a/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java +++ b/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java @@ -5,53 +5,63 @@ import java.nio.file.Files; import java.nio.file.Path; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.IcatExceptionType; import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.helpers.LocationHelper; -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DatasetInfo; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** - * This class is to handle not finished work. - * It could be redesigned with sub classes depending on StorageUnit, but it isn't worth it i guess, just for one abstract method (loadDataInfo()). + * This class is to handle not finished work. It could be redesigned with sub + * classes depending on StorageUnit, but it isn't worth it i guess, just for one + * abstract method (loadDataInfo()). */ public class UnfinishedWorkService { - - protected final static Logger logger = LoggerFactory.getLogger(UnfinishedWorkService.class); + protected final static Logger logger = LoggerFactory + .getLogger(UnfinishedWorkService.class); public UnfinishedWorkService() { - + } - public void restartUnfinishedWork(Path markerDir, String key) throws InternalException { + public void restartUnfinishedWork(Path markerDir, String key) + throws InternalException { try { var serviceProvider = ServiceProvider.getInstance(); - StorageUnit storageUnit = serviceProvider.getPropertyHandler().getStorageUnit(); + StorageUnit storageUnit = serviceProvider.getPropertyHandler() + .getStorageUnit(); for (File file : markerDir.toFile().listFiles()) { - if(storageUnit == null) break; - - long id = Long.parseLong(file.toPath().getFileName().toString()); + if (storageUnit == null) + break; + + long id = Long + .parseLong(file.toPath().getFileName().toString()); try { DataInfoBase dataInfo = this.loadDataInfo(storageUnit, id); serviceProvider.getFsm().queue(dataInfo, DeferredOp.WRITE); - logger.info("Queued " + storageUnit.toString().toLowerCase() + " with id " + id + " " + dataInfo + logger.info("Queued " + storageUnit.toString().toLowerCase() + + " with id " + id + " " + dataInfo + " to be written as it was not written out previously by IDS"); } catch (IcatException_Exception e) { - if (e.getFaultInfo().getType() == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { - logger.warn( storageUnit.toString().toLowerCase() + " with id " + id + if (e.getFaultInfo() + .getType() == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { + logger.warn(storageUnit.toString().toLowerCase() + + " with id " + id + " was not written out by IDS and now no longer known to ICAT"); Files.delete(file.toPath()); } else { @@ -64,7 +74,6 @@ public void restartUnfinishedWork(Path markerDir, String key) throws InternalExc } } - public void cleanPreparedDir(Path preparedDir) { for (File file : preparedDir.toFile().listFiles()) { Path path = file.toPath(); @@ -80,7 +89,8 @@ public void cleanPreparedDir(Path preparedDir) { } thisSize += Files.size(path); Files.delete(path); - logger.debug("Deleted " + path + " to reclaim " + thisSize + " bytes"); + logger.debug("Deleted " + path + " to reclaim " + thisSize + + " bytes"); } catch (IOException e) { logger.debug("Failed to delete " + path + e.getMessage()); } @@ -88,31 +98,37 @@ public void cleanPreparedDir(Path preparedDir) { } } - public void cleanDatasetCache(Path datasetDir) { for (File dsFile : datasetDir.toFile().listFiles()) { Path path = dsFile.toPath(); try { long thisSize = Files.size(path); Files.delete(path); - logger.debug("Deleted " + path + " to reclaim " + thisSize + " bytes"); + logger.debug("Deleted " + path + " to reclaim " + thisSize + + " bytes"); } catch (IOException e) { - logger.debug("Failed to delete " + path + " " + e.getClass() + " " + e.getMessage()); + logger.debug("Failed to delete " + path + " " + e.getClass() + + " " + e.getMessage()); } } } - - private DataInfoBase loadDataInfo(StorageUnit storageUnit, long id) throws IcatException_Exception, InsufficientPrivilegesException, InternalException { - if(storageUnit == StorageUnit.DATASET) { - Dataset ds = (Dataset) ServiceProvider.getInstance().getIcatReader().get("Dataset ds INCLUDE ds.investigation.facility", id); + private DataInfoBase loadDataInfo(StorageUnit storageUnit, long id) + throws IcatException_Exception, InsufficientPrivilegesException, + InternalException { + if (storageUnit == StorageUnit.DATASET) { + Dataset ds = (Dataset) ServiceProvider.getInstance().getIcatReader() + .get("Dataset ds INCLUDE ds.investigation.facility", id); return new DatasetInfo(ds); } - if(storageUnit == StorageUnit.DATAFILE) { - Datafile df = (Datafile) ServiceProvider.getInstance().getIcatReader().get("Datafile ds INCLUDE ds.dataset", id); - String location = LocationHelper.getLocation(df.getId(), df.getLocation()); - return new DatafileInfo(id, df.getName(), location, df.getCreateId(), df.getModId(), df.getDataset().getId()); + if (storageUnit == StorageUnit.DATAFILE) { + Datafile df = (Datafile) ServiceProvider.getInstance() + .getIcatReader().get("Datafile ds INCLUDE ds.dataset", id); + String location = LocationHelper.getLocation(df.getId(), + df.getLocation()); + return new DatafileInfo(id, df.getName(), location, + df.getCreateId(), df.getModId(), df.getDataset().getId()); } return null; diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java index 650e410f..c0a5f1cf 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java @@ -16,30 +16,34 @@ import java.util.concurrent.Future; import java.util.concurrent.locks.ReentrantLock; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.icatproject.IcatException_Exception; + import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; import org.icatproject.ids.exceptions.DataNotOnlineException; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.exceptions.NotImplementedException; -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DataSelection; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.services.ServiceProvider; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public abstract class DataSelectionService { - protected final static Logger logger = LoggerFactory.getLogger(DataSelectionService.class); + protected final static Logger logger = LoggerFactory + .getLogger(DataSelectionService.class); protected DataSelection dataSelection; protected RequestType requestType; - protected static ExecutorService threadPool; - static { threadPool = Executors.newCachedThreadPool(); } + static { + threadPool = Executors.newCachedThreadPool(); + } private Map preparedStatusMap = new ConcurrentHashMap<>(); @@ -50,41 +54,54 @@ class PreparedStatus { } - protected DataSelectionService(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { + protected DataSelectionService(SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, + Boolean zip, Boolean compress, RequestType requestType) { - this.dataSelection = new DataSelection(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress); + this.dataSelection = new DataSelection(dsInfos, dfInfos, emptyDatasets, + invids2, dsids, dfids, length, zip, compress); this.requestType = requestType; } - public abstract boolean isPrepared(String preparedId) throws InternalException; + public abstract boolean isPrepared(String preparedId) + throws InternalException; /** - * To get the DataInfos that is currently worked with, depending on StorageUnit + * To get the DataInfos that is currently worked with, depending on + * StorageUnit + * * @return */ public abstract SortedMap getPrimaryDataInfos(); - public abstract boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException; + public abstract boolean existsInMainStorage(DataInfoBase dataInfo) + throws InternalException; - public abstract void queueDelete() throws NotImplementedException, InternalException; + public abstract void queueDelete() + throws NotImplementedException, InternalException; - public abstract void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException; + public abstract void scheduleTasks(DeferredOp operation) + throws NotImplementedException, InternalException; - //TODO: maybe implementing this method here whould be the better way, than making it abstract. But we will miss the NotImplementedException for SingleLevelStorage in that case. + // TODO: maybe implementing this method here whould be the better way, than + // making it abstract. But we will miss the NotImplementedException for + // SingleLevelStorage in that case. // should we change the tests and the behavior of the ids server here - // public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { - // for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { // in case of SingleLevelStorage an empty map is returned and nothing will happen (even not an exception). - // ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); - // } + // public void scheduleTasks(DeferredOp operation) throws + // NotImplementedException, InternalException { + // for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { // in + // case of SingleLevelStorage an empty map is returned and nothing will + // happen (even not an exception). + // ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); + // } // } public SortedMap getDsInfo() { return this.dataSelection.getDsInfo(); } - public SortedMap getDfInfo() { return this.dataSelection.getDfInfo(); } @@ -101,7 +118,6 @@ public long getLength() { return this.dataSelection.getLength(); } - public boolean mustZip() { return this.dataSelection.mustZip(); } @@ -110,21 +126,22 @@ public boolean isSingleDataset() { return this.dataSelection.isSingleDataset(); } - public Set getEmptyDatasets() { return this.dataSelection.getEmptyDatasets(); } - /** - * tries to extract a list of ids from a comma separated id string. No spaces or leading 0's. Also - * accepts null. - * @param thing the name of the id list - for better error message - * @param idList a String which shoald contain long numbers seperated by commas + * tries to extract a list of ids from a comma separated id string. No + * spaces or leading 0's. Also accepts null. + * + * @param thing the name of the id list - for better error message + * @param idList a String which shoald contain long numbers seperated by + * commas * @return list of long numbers - the extracted id values * @throws BadRequestException */ - public static List getValidIds(String thing, String idList) throws BadRequestException { + public static List getValidIds(String thing, String idList) + throws BadRequestException { List result; if (idList == null) { @@ -136,7 +153,8 @@ public static List getValidIds(String thing, String idList) throws BadRequ try { result.add(Long.parseLong(id)); } catch (NumberFormatException e) { - throw new BadRequestException("The " + thing + " parameter '" + idList + "' is not a valid " + throw new BadRequestException("The " + thing + + " parameter '" + idList + "' is not a valid " + "string representation of a comma separated list of longs"); } } @@ -144,8 +162,8 @@ public static List getValidIds(String thing, String idList) throws BadRequ return result; } - - private boolean restoreIfOffline(DataInfoBase dataInfo) throws InternalException { + private boolean restoreIfOffline(DataInfoBase dataInfo) + throws InternalException { boolean maybeOffline = false; var serviceProvider = ServiceProvider.getInstance(); if (serviceProvider.getFsm().getMaybeOffline().contains(dataInfo)) { @@ -157,8 +175,7 @@ private boolean restoreIfOffline(DataInfoBase dataInfo) throws InternalException return maybeOffline; } - - public void checkOnline()throws InternalException, DataNotOnlineException { + public void checkOnline() throws InternalException, DataNotOnlineException { boolean maybeOffline = false; for (DataInfoBase dfInfo : this.getPrimaryDataInfos().values()) { @@ -172,51 +189,59 @@ public void checkOnline()throws InternalException, DataNotOnlineException { } } - public void restoreDataInfos() { var dataInfos = this.getPrimaryDataInfos().values(); - if(!dataInfos.isEmpty()) { + if (!dataInfos.isEmpty()) { for (DataInfoBase dataInfo : dataInfos) { - ServiceProvider.getInstance().getFsm().recordSuccess(dataInfo.getId()); + ServiceProvider.getInstance().getFsm() + .recordSuccess(dataInfo.getId()); } threadPool.submit(new RestoreDataInfoTask(dataInfos, this, false)); } } - public OptionalLong getFileLength() { - if (this.dataSelection.getDfInfo().isEmpty() || this.dataSelection.mustZip()) { + if (this.dataSelection.getDfInfo().isEmpty() + || this.dataSelection.mustZip()) { return OptionalLong.empty(); } return OptionalLong.of(this.dataSelection.getLength()); } - - protected boolean areDataInfosPrepared(String preparedId) throws InternalException { + protected boolean areDataInfosPrepared(String preparedId) + throws InternalException { boolean prepared = true; var serviceProvider = ServiceProvider.getInstance(); - PreparedStatus status = preparedStatusMap.computeIfAbsent(preparedId, k -> new PreparedStatus()); + PreparedStatus status = preparedStatusMap.computeIfAbsent(preparedId, + k -> new PreparedStatus()); - Collection toCheck = status.fromElement == null ? this.getPrimaryDataInfos().values() - : this.getPrimaryDataInfos().tailMap(status.fromElement).values(); + Collection toCheck = status.fromElement == null + ? this.getPrimaryDataInfos().values() + : this.getPrimaryDataInfos().tailMap(status.fromElement) + .values(); logger.debug("Will check online status of {} entries", toCheck.size()); for (DataInfoBase dataInfo : toCheck) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (this.restoreIfOffline(dataInfo)) { prepared = false; status.fromElement = dataInfo.getId(); - toCheck = this.getPrimaryDataInfos().tailMap(status.fromElement).values(); - logger.debug("Will check in background status of {} entries", toCheck.size()); - status.future = threadPool.submit(new RestoreDataInfoTask(toCheck, this, true)); + toCheck = this.getPrimaryDataInfos().tailMap(status.fromElement) + .values(); + logger.debug("Will check in background status of {} entries", + toCheck.size()); + status.future = threadPool + .submit(new RestoreDataInfoTask(toCheck, this, true)); break; } } if (prepared) { toCheck = status.fromElement == null ? Collections.emptySet() - : this.getPrimaryDataInfos().headMap(status.fromElement).values(); - logger.debug("Will check finally online status of {} entries", toCheck.size()); + : this.getPrimaryDataInfos().headMap(status.fromElement) + .values(); + logger.debug("Will check finally online status of {} entries", + toCheck.size()); for (DataInfoBase dataInfo : toCheck) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (this.restoreIfOffline(dataInfo)) { @@ -228,47 +253,52 @@ protected boolean areDataInfosPrepared(String preparedId) throws InternalExcepti return prepared; } - public void delete() throws InternalException, NotImplementedException { var serviceProvider = ServiceProvider.getInstance(); /* - * Delete the local copy directly rather than queueing it as it has - * been removed from ICAT so will not be accessible to any - * subsequent IDS calls. + * Delete the local copy directly rather than queueing it as it has been + * removed from ICAT so will not be accessible to any subsequent IDS + * calls. */ for (DataInfoBase dataInfo : this.dataSelection.getDfInfo().values()) { var dfInfo = (DatafileInfo) dataInfo; String location = dataInfo.getLocation(); try { - if ((long) serviceProvider.getIcatReader() - .search("SELECT COUNT(df) FROM Datafile df WHERE df.location LIKE '" + location.replaceAll("'", "''") + "%'") + if ((long) serviceProvider.getIcatReader().search( + "SELECT COUNT(df) FROM Datafile df WHERE df.location LIKE '" + + location.replaceAll("'", "''") + "%'") .get(0) == 0) { if (serviceProvider.getMainStorage().exists(location)) { - logger.debug("Delete physical file " + location + " from main storage"); - serviceProvider.getMainStorage().delete(location, dfInfo.getCreateId(), dfInfo.getModId()); + logger.debug("Delete physical file " + location + + " from main storage"); + serviceProvider.getMainStorage().delete(location, + dfInfo.getCreateId(), dfInfo.getModId()); } } } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException( + e.getFaultInfo().getType() + " " + e.getMessage()); } catch (IOException e) { - logger.error("I/O error " + e.getMessage() + " deleting " + dfInfo); - throw new InternalException(e.getClass() + " " + e.getMessage()); + logger.error( + "I/O error " + e.getMessage() + " deleting " + dfInfo); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } this.queueDelete(); } - private class RestoreDataInfoTask implements Callable { private Collection dataInfos; private DataSelectionService dataselection; private boolean checkFailure; - public RestoreDataInfoTask(Collection dataInfos, DataSelectionService dataSelection, boolean checkFailure) { + public RestoreDataInfoTask(Collection dataInfos, + DataSelectionService dataSelection, boolean checkFailure) { this.dataInfos = dataInfos; this.dataselection = dataSelection; this.checkFailure = checkFailure; @@ -277,13 +307,14 @@ public RestoreDataInfoTask(Collection dataInfos, DataSelectionServ @Override public Void call() throws Exception { for (DataInfoBase dataInfo : dataInfos) { - if(checkFailure) - ServiceProvider.getInstance().getFsm().checkFailure(dataInfo.getId()); + if (checkFailure) + ServiceProvider.getInstance().getFsm() + .checkFailure(dataInfo.getId()); dataselection.restoreIfOffline(dataInfo); } return null; } - + } } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java index 25ff3ccb..e05f76aa 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java @@ -11,6 +11,13 @@ import java.util.SortedMap; import java.util.TreeMap; +import jakarta.json.Json; +import jakarta.json.JsonArray; +import jakarta.json.JsonValue; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ICAT; @@ -18,6 +25,7 @@ import org.icatproject.IcatException_Exception; import org.icatproject.icat.client.IcatException; import org.icatproject.icat.client.Session; + import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.exceptions.BadRequestException; @@ -26,23 +34,18 @@ import org.icatproject.ids.exceptions.NotFoundException; import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.helpers.LocationHelper; -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.models.Prepared; import org.icatproject.ids.services.IcatReader; import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.ServiceProvider; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import jakarta.json.Json; -import jakarta.json.JsonArray; -import jakarta.json.JsonValue; public class DataSelectionServiceFactory { - private final static Logger logger = LoggerFactory.getLogger(DataSelectionServiceFactory.class); + private final static Logger logger = LoggerFactory + .getLogger(DataSelectionServiceFactory.class); private static DataSelectionServiceFactory instance = null; @@ -57,30 +60,41 @@ public enum Returns { DATASETS, DATASETS_AND_DATAFILES, DATAFILES } - public static DataSelectionServiceFactory getInstance() throws InternalException { + public static DataSelectionServiceFactory getInstance() + throws InternalException { if (instance == null) { var serviceProvider = ServiceProvider.getInstance(); - instance = new DataSelectionServiceFactory(serviceProvider.getPropertyHandler(), serviceProvider.getIcatReader()); + instance = new DataSelectionServiceFactory( + serviceProvider.getPropertyHandler(), + serviceProvider.getIcatReader()); } return instance; } - public static DataSelectionServiceFactory getInstanceOnlyForTesting(PropertyHandler propertyHandler, IcatReader reader) throws InternalException { + public static DataSelectionServiceFactory getInstanceOnlyForTesting( + PropertyHandler propertyHandler, IcatReader reader) + throws InternalException { if (instance == null) { instance = new DataSelectionServiceFactory(propertyHandler, reader); } return instance; } - public static DataSelectionService getService(String userSessionId, String investigationIds, String datasetIds, String datafileIds, RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + public static DataSelectionService getService(String userSessionId, + String investigationIds, String datasetIds, String datafileIds, + RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, + InsufficientPrivilegesException, NotImplementedException { - return DataSelectionServiceFactory.getInstance().getSelectionService(userSessionId, investigationIds, datasetIds, datafileIds, requestType); + return DataSelectionServiceFactory.getInstance().getSelectionService( + userSessionId, investigationIds, datasetIds, datafileIds, + requestType); } - /** - * created a DataSelectionBase object from already created data infos. That's why no infos must be loaded from ICAT. + * created a DataSelectionBase object from already created data infos. + * That's why no infos must be loaded from ICAT. + * * @param dsInfos * @param dfInfos * @param emptyDatasets @@ -89,33 +103,41 @@ public static DataSelectionService getService(String userSessionId, String inves * @return * @throws InternalException */ - public static DataSelectionService getService(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength, RequestType requestType) throws InternalException { + public static DataSelectionService getService( + SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + long fileLength, RequestType requestType) throws InternalException { - var prepared = new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength); + var prepared = new Prepared(dsInfos, dfInfos, emptyDatasets, + fileLength); return DataSelectionServiceFactory.getService(prepared, requestType); } - public static DataSelectionService getService(Prepared prepared, RequestType requestType) throws InternalException { + public static DataSelectionService getService(Prepared prepared, + RequestType requestType) throws InternalException { List dsids = new ArrayList(prepared.dsInfos.keySet()); List dfids = new ArrayList(); List invIds = new ArrayList(); var dataFileInfos = new HashMap(); - for(DataInfoBase dfInfo: prepared.dfInfos.values()) { + for (DataInfoBase dfInfo : prepared.dfInfos.values()) { dfids.add(dfInfo.getId()); dataFileInfos.put(dfInfo.getId(), dfInfo); dfids.add(dfInfo.getId()); } - for(DataInfoBase dsInfo : prepared.dsInfos.values()) { + for (DataInfoBase dsInfo : prepared.dsInfos.values()) { dsids.add(dsInfo.getId()); - invIds.add( ((DatasetInfo)dsInfo).getInvId() ); + invIds.add(((DatasetInfo) dsInfo).getInvId()); } - return DataSelectionServiceFactory.getInstance().createSelectionService(prepared.dsInfos, prepared.dfInfos, prepared.emptyDatasets, invIds, dsids, dfids, prepared.fileLength, prepared.zip, prepared.compress, requestType); + return DataSelectionServiceFactory.getInstance().createSelectionService( + prepared.dsInfos, prepared.dfInfos, prepared.emptyDatasets, + invIds, dsids, dfids, prepared.fileLength, prepared.zip, + prepared.compress, requestType); } - private DataSelectionServiceFactory(PropertyHandler propertyHandler, IcatReader reader) throws InternalException - { + private DataSelectionServiceFactory(PropertyHandler propertyHandler, + IcatReader reader) throws InternalException { logger.info("### Constructing..."); this.propertyHandler = propertyHandler; this.icat = propertyHandler.getIcatService(); @@ -128,47 +150,66 @@ private DataSelectionServiceFactory(PropertyHandler propertyHandler, IcatReader logger.info("### Constructing finished"); } - private DataSelectionService createSelectionService(SortedMap dsInfos, SortedMap dfInfos, - Set emptyDatasets, List invids, List dsids, - List dfids, long length, Boolean zip, Boolean compress, - RequestType requestType) throws InternalException { + private DataSelectionService createSelectionService( + SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + List invids, List dsids, List dfids, long length, + Boolean zip, Boolean compress, RequestType requestType) + throws InternalException { StorageUnit storageUnit = this.propertyHandler.getStorageUnit(); - if(storageUnit == null ) - return new DataSelectionServiceForSingleLevelStorage(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); + if (storageUnit == null) + return new DataSelectionServiceForSingleLevelStorage(dsInfos, + dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, + compress, requestType); else if (storageUnit == StorageUnit.DATAFILE) - return new DataSelectionServiceForStorageUnitDatafile(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); + return new DataSelectionServiceForStorageUnitDatafile(dsInfos, + dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, + compress, requestType); - else if(storageUnit == StorageUnit.DATASET) - return new DataSelectionServiceForStorageUnitDataset(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); + else if (storageUnit == StorageUnit.DATASET) + return new DataSelectionServiceForStorageUnitDataset(dsInfos, + dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, + compress, requestType); - else throw new InternalException("StorageUnit " + storageUnit + " unknown. Maybe you forgot to handle a new StorageUnit here?"); + else + throw new InternalException("StorageUnit " + storageUnit + + " unknown. Maybe you forgot to handle a new StorageUnit here?"); } - public DataSelectionService getSelectionService( String userSessionId, String investigationIds, String datasetIds, String datafileIds, RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - + public DataSelectionService getSelectionService(String userSessionId, + String investigationIds, String datasetIds, String datafileIds, + RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, + InsufficientPrivilegesException, NotImplementedException { + List dfids = getValidIds("datafileIds", datafileIds); List dsids = getValidIds("datasetIds", datasetIds); List invids = getValidIds("investigationIds", investigationIds); Returns returns = this.getReturns(requestType); - boolean dfWanted = returns == Returns.DATASETS_AND_DATAFILES || returns == Returns.DATAFILES; - boolean dsWanted = returns == Returns.DATASETS_AND_DATAFILES || returns == Returns.DATASETS; + boolean dfWanted = returns == Returns.DATASETS_AND_DATAFILES + || returns == Returns.DATAFILES; + boolean dsWanted = returns == Returns.DATASETS_AND_DATAFILES + || returns == Returns.DATASETS; Session userRestSession = restIcat.getSession(userSessionId); // by default use the user's REST ICAT session Session restSessionToUse = userRestSession; - + try { - logger.debug("useReaderForPerformance = {}", propertyHandler.getUseReaderForPerformance()); + logger.debug("useReaderForPerformance = {}", + propertyHandler.getUseReaderForPerformance()); if (propertyHandler.getUseReaderForPerformance()) { - // if this is set, use a REST session for the reader account where possible - // to improve performance due to the final database queries being simpler - restSessionToUse = restIcat.getSession(this.icatReader.getSessionId()); + // if this is set, use a REST session for the reader account + // where possible + // to improve performance due to the final database queries + // being simpler + restSessionToUse = restIcat + .getSession(this.icatReader.getSessionId()); } } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -176,20 +217,23 @@ public DataSelectionService getSelectionService( String userSessionId, String in logger.debug("dfids: {} dsids: {} invids: {}", dfids, dsids, invids); - return prepareFromIds(dfWanted, dsWanted, dfids, dsids, invids, userSessionId, restSessionToUse, userRestSession, requestType); + return prepareFromIds(dfWanted, dsWanted, dfids, dsids, invids, + userSessionId, restSessionToUse, userRestSession, requestType); } - - - private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, List dfids, List dsids, List invids, String userSessionId, Session restSessionToUse, Session userRestSession, RequestType requestType) - throws NotFoundException, InsufficientPrivilegesException, InternalException, BadRequestException { + private DataSelectionService prepareFromIds(boolean dfWanted, + boolean dsWanted, List dfids, List dsids, + List invids, String userSessionId, Session restSessionToUse, + Session userRestSession, RequestType requestType) + throws NotFoundException, InsufficientPrivilegesException, + InternalException, BadRequestException { var dsInfos = new TreeMap(); var emptyDatasets = new HashSet(); var dfInfos = new TreeMap(); long length = 0; - if (dfWanted) { //redundant ? + if (dfWanted) { // redundant ? dfInfos = new TreeMap(); } @@ -197,18 +241,22 @@ private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, for (Long dfid : dfids) { List dss = icat.search(userSessionId, - "SELECT ds FROM Dataset ds JOIN ds.datafiles df WHERE df.id = " + dfid + "SELECT ds FROM Dataset ds JOIN ds.datafiles df WHERE df.id = " + + dfid + " AND df.location IS NOT NULL INCLUDE ds.investigation.facility"); if (dss.size() == 1) { Dataset ds = (Dataset) dss.get(0); long dsid = ds.getId(); dsInfos.put(dsid, new DatasetInfo(ds)); if (dfWanted) { - Datafile df = (Datafile) icat.get(userSessionId, "Datafile", dfid); + Datafile df = (Datafile) icat.get(userSessionId, + "Datafile", dfid); length += df.getFileSize(); - String location = LocationHelper.getLocation(dfid, df.getLocation()); - dfInfos.put( df.getId(), - new DatafileInfo(dfid, df.getName(), location, df.getCreateId(), df.getModId(), dsid)); + String location = LocationHelper.getLocation(dfid, + df.getLocation()); + dfInfos.put(df.getId(), + new DatafileInfo(dfid, df.getName(), location, + df.getCreateId(), df.getModId(), dsid)); } } else { // Next line may reveal a permissions problem @@ -218,13 +266,18 @@ private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, } for (Long dsid : dsids) { - Dataset ds = (Dataset) icat.get(userSessionId, "Dataset ds INCLUDE ds.investigation.facility", dsid); + Dataset ds = (Dataset) icat.get(userSessionId, + "Dataset ds INCLUDE ds.investigation.facility", dsid); dsInfos.put(dsid, new DatasetInfo(ds)); - // dataset access for the user has been checked so the REST session for the - // reader account can be used if the IDS setting to allow this is enabled + // dataset access for the user has been checked so the REST + // session for the + // reader account can be used if the IDS setting to allow this + // is enabled String query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid + " AND df.location IS NOT NULL"; - JsonArray result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())) + JsonArray result = Json + .createReader(new ByteArrayInputStream( + restSessionToUse.search(query).getBytes())) .readArray().getJsonArray(0); if (result.getJsonNumber(2).longValueExact() == 0) { // Count 0 emptyDatasets.add(dsid); @@ -236,31 +289,38 @@ private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, for (Long invid : invids) { String query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + invid; - JsonArray result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())) + JsonArray result = Json + .createReader(new ByteArrayInputStream( + userRestSession.search(query).getBytes())) .readArray().getJsonArray(0); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUse, result); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, + userRestSession, restSessionToUse, result); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES + || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } else if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } else { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IcatException e) { - org.icatproject.icat.client.IcatException.IcatExceptionType type = e.getType(); + org.icatproject.icat.client.IcatException.IcatExceptionType type = e + .getType(); if (type == org.icatproject.icat.client.IcatException.IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == org.icatproject.icat.client.IcatException.IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } else if (type == org.icatproject.icat.client.IcatException.IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } else { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } /* @@ -272,16 +332,17 @@ private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, emptyDatasets = null; } - return this.createSelectionService(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, false, false, requestType); + return this.createSelectionService(dsInfos, dfInfos, emptyDatasets, + invids, dsids, dfids, length, false, false, requestType); } - /** * Checks to see if the investigation, dataset or datafile id list is a * valid comma separated list of longs. No spaces or leading 0's. Also * accepts null. */ - public static List getValidIds(String thing, String idList) throws BadRequestException { + public static List getValidIds(String thing, String idList) + throws BadRequestException { List result; if (idList == null) { @@ -293,7 +354,8 @@ public static List getValidIds(String thing, String idList) throws BadRequ try { result.add(Long.parseLong(id)); } catch (NumberFormatException e) { - throw new BadRequestException("The " + thing + " parameter '" + idList + "' is not a valid " + throw new BadRequestException("The " + thing + + " parameter '" + idList + "' is not a valid " + "string representation of a comma separated list of longs"); } } @@ -301,10 +363,13 @@ public static List getValidIds(String thing, String idList) throws BadRequ return result; } - private void manyDfs(Map dfInfos, long dsid, Session restSessionToUse, JsonArray result) - throws IcatException, InsufficientPrivilegesException, InternalException { - // dataset access for the user has been checked so the REST session for the - // reader account can be used if the IDS setting to allow this is enabled + private void manyDfs(Map dfInfos, long dsid, + Session restSessionToUse, JsonArray result) throws IcatException, + InsufficientPrivilegesException, InternalException { + // dataset access for the user has been checked so the REST session for + // the + // reader account can be used if the IDS setting to allow this is + // enabled long min = result.getJsonNumber(0).longValueExact(); long max = result.getJsonNumber(1).longValueExact(); long count = result.getJsonNumber(2).longValueExact(); @@ -312,33 +377,52 @@ private void manyDfs(Map dfInfos, long dsid, Session restSes if (count != 0) { if (count <= maxEntities) { String query = "SELECT df.id, df.name, df.location, df.createId, df.modId FROM Datafile df WHERE df.dataset.id = " - + dsid + " AND df.location IS NOT NULL AND df.id BETWEEN " + min + " AND " + max; - result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray(); + + dsid + + " AND df.location IS NOT NULL AND df.id BETWEEN " + + min + " AND " + max; + result = Json + .createReader(new ByteArrayInputStream( + restSessionToUse.search(query).getBytes())) + .readArray(); for (JsonValue tupV : result) { JsonArray tup = (JsonArray) tupV; long dfid = tup.getJsonNumber(0).longValueExact(); - String location = LocationHelper.getLocation(dfid, tup.getString(2, null)); + String location = LocationHelper.getLocation(dfid, + tup.getString(2, null)); dfInfos.put(dfid, - new DatafileInfo(dfid, tup.getString(1), location, tup.getString(3), tup.getString(4), dsid)); + new DatafileInfo(dfid, tup.getString(1), location, + tup.getString(3), tup.getString(4), dsid)); } } else { long half = (min + max) / 2; String query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " - + dsid + " AND df.location IS NOT NULL AND df.id BETWEEN " + min + " AND " + half; - result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray() - .getJsonArray(0); + + dsid + + " AND df.location IS NOT NULL AND df.id BETWEEN " + + min + " AND " + half; + result = Json + .createReader(new ByteArrayInputStream( + restSessionToUse.search(query).getBytes())) + .readArray().getJsonArray(0); manyDfs(dfInfos, dsid, restSessionToUse, result); - query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid - + " AND df.location IS NOT NULL AND df.id BETWEEN " + (half + 1) + " AND " + max; - result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray() - .getJsonArray(0); + query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + + dsid + + " AND df.location IS NOT NULL AND df.id BETWEEN " + + (half + 1) + " AND " + max; + result = Json + .createReader(new ByteArrayInputStream( + restSessionToUse.search(query).getBytes())) + .readArray().getJsonArray(0); manyDfs(dfInfos, dsid, restSessionToUse, result); } } } - private void manyDss(Map dsInfos, HashSet emptyDatasets, Map dfInfos, Long invid, boolean dfWanted, Session userRestSession, Session restSessionToUseForDfs, JsonArray result) - throws IcatException, InsufficientPrivilegesException, InternalException { + private void manyDss(Map dsInfos, + HashSet emptyDatasets, Map dfInfos, + Long invid, boolean dfWanted, Session userRestSession, + Session restSessionToUseForDfs, JsonArray result) + throws IcatException, InsufficientPrivilegesException, + InternalException { long min = result.getJsonNumber(0).longValueExact(); long max = result.getJsonNumber(1).longValueExact(); long count = result.getJsonNumber(2).longValueExact(); @@ -347,7 +431,10 @@ private void manyDss(Map dsInfos, HashSet emptyDataset if (count <= maxEntities) { String query = "SELECT inv.name, inv.visitId, inv.facility.id, inv.facility.name FROM Investigation inv WHERE inv.id = " + invid; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); + result = Json + .createReader(new ByteArrayInputStream( + userRestSession.search(query).getBytes())) + .readArray(); if (result.size() == 0) { return; } @@ -357,18 +444,25 @@ private void manyDss(Map dsInfos, HashSet emptyDataset long facilityId = result.getJsonNumber(2).longValueExact(); String facilityName = result.getString(3); - query = "SELECT ds.id, ds.name, ds.location FROM Dataset ds WHERE ds.investigation.id = " + invid - + " AND ds.id BETWEEN " + min + " AND " + max; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); + query = "SELECT ds.id, ds.name, ds.location FROM Dataset ds WHERE ds.investigation.id = " + + invid + " AND ds.id BETWEEN " + min + " AND " + max; + result = Json + .createReader(new ByteArrayInputStream( + userRestSession.search(query).getBytes())) + .readArray(); for (JsonValue tupV : result) { JsonArray tup = (JsonArray) tupV; long dsid = tup.getJsonNumber(0).longValueExact(); - dsInfos.put(dsid, new DatasetInfo(dsid, tup.getString(1), tup.getString(2, null), invid, invName, - visitId, facilityId, facilityName)); + dsInfos.put(dsid, + new DatasetInfo(dsid, tup.getString(1), + tup.getString(2, null), invid, invName, + visitId, facilityId, facilityName)); query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid + " AND df.location IS NOT NULL"; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())) + result = Json + .createReader(new ByteArrayInputStream( + userRestSession.search(query).getBytes())) .readArray().getJsonArray(0); if (result.getJsonNumber(2).longValueExact() == 0) { emptyDatasets.add(dsid); @@ -381,13 +475,21 @@ private void manyDss(Map dsInfos, HashSet emptyDataset long half = (min + max) / 2; String query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + invid + " AND ds.id BETWEEN " + min + " AND " + half; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUseForDfs, result); + result = Json + .createReader(new ByteArrayInputStream( + userRestSession.search(query).getBytes())) + .readArray(); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, + userRestSession, restSessionToUseForDfs, result); query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " - + invid + " AND ds.id BETWEEN " + half + 1 + " AND " + max; - result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray() - .getJsonArray(0); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUseForDfs, result); + + invid + " AND ds.id BETWEEN " + half + 1 + " AND " + + max; + result = Json + .createReader(new ByteArrayInputStream( + userRestSession.search(query).getBytes())) + .readArray().getJsonArray(0); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, + userRestSession, restSessionToUseForDfs, result); } } @@ -398,42 +500,60 @@ private void createRequestTypeToReturnsMapping() throws InternalException { this.requestTypeToReturnsMapping = new HashMap(); StorageUnit storageUnit = this.propertyHandler.getStorageUnit(); - this.requestTypeToReturnsMapping.put(RequestType.DELETE, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETDATAFILEIDS, Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETSIZE, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.PREPAREDATA, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.RESET, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.WRITE, Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETDATA, Returns.DATASETS_AND_DATAFILES); - - if(storageUnit == null ) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATASETS); - } - else if (storageUnit == StorageUnit.DATAFILE) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.RESTORE, Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.DELETE, + Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETDATAFILEIDS, + Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETSIZE, + Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.PREPAREDATA, + Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.RESET, + Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.WRITE, + Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETDATA, + Returns.DATASETS_AND_DATAFILES); + + if (storageUnit == null) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, + Returns.DATASETS); + } else if (storageUnit == StorageUnit.DATAFILE) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, + Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.RESTORE, + Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, + Returns.DATAFILES); + } else if (storageUnit == StorageUnit.DATASET) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, + Returns.DATASETS); + this.requestTypeToReturnsMapping.put(RequestType.RESTORE, + Returns.DATASETS); + this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, + Returns.DATASETS); } - else if(storageUnit == StorageUnit.DATASET) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATASETS); - this.requestTypeToReturnsMapping.put(RequestType.RESTORE, Returns.DATASETS); - this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, Returns.DATASETS); - } - - else throw new InternalException("StorageUnit " + storageUnit + " unknown. Maybe you forgot to handle a new StorageUnit here?"); + else + throw new InternalException("StorageUnit " + storageUnit + + " unknown. Maybe you forgot to handle a new StorageUnit here?"); } - private Returns getReturns(RequestType requestType) throws NotImplementedException { + private Returns getReturns(RequestType requestType) + throws NotImplementedException { - if(this.requestTypeToReturnsMapping.containsKey(requestType)) + if (this.requestTypeToReturnsMapping.containsKey(requestType)) return this.requestTypeToReturnsMapping.get(requestType); - // is this needed here? - //if(this.propertyHandler.getStorageUnit() == null) throw new NotImplementedException("This operation is unavailable for single level storage"); - - throw new NotImplementedException("There is to mapping for RequestType." + requestType + " and StorageUnit." + this.propertyHandler.getStorageUnit() + " defined. Did you forgot to register it in createRequestTypeToReturnsMapping()?"); + // if(this.propertyHandler.getStorageUnit() == null) throw new + // NotImplementedException("This operation is unavailable for single + // level storage"); + + throw new NotImplementedException("There is to mapping for RequestType." + + requestType + " and StorageUnit." + + this.propertyHandler.getStorageUnit() + + " defined. Did you forgot to register it in createRequestTypeToReturnsMapping()?"); } } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java index b7915daa..c1a6a8a5 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java @@ -11,47 +11,49 @@ import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.models.DataInfoBase; -public class DataSelectionServiceForSingleLevelStorage extends DataSelectionService { +public class DataSelectionServiceForSingleLevelStorage + extends DataSelectionService { - protected DataSelectionServiceForSingleLevelStorage(SortedMap dsInfos, SortedMap dfInfos, - Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { - - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); - } + protected DataSelectionServiceForSingleLevelStorage( + SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, + Boolean zip, Boolean compress, RequestType requestType) { + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, + zip, compress, requestType); + } @Override public SortedMap getPrimaryDataInfos() { return new TreeMap(); } - @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { - - throw new InternalException("This operation is unavailable for single level storage"); - } + public boolean existsInMainStorage(DataInfoBase dataInfo) + throws InternalException { + throw new InternalException( + "This operation is unavailable for single level storage"); + } @Override public boolean isPrepared(String preparedId) throws InternalException { return true; } - @Override - public void queueDelete() throws NotImplementedException, InternalException { - //nothing todo for single level storage + public void queueDelete() + throws NotImplementedException, InternalException { + // nothing todo for single level storage } - @Override - public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) + throws NotImplementedException, InternalException { - throw new NotImplementedException("This operation is unavailable for single level storage"); + throw new NotImplementedException( + "This operation is unavailable for single level storage"); } - - - } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java index f744fa8f..18da82fb 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java @@ -11,44 +11,49 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.ServiceProvider; -public class DataSelectionServiceForStorageUnitDatafile extends DataSelectionService { +public class DataSelectionServiceForStorageUnitDatafile + extends DataSelectionService { - protected DataSelectionServiceForStorageUnitDatafile(SortedMap dsInfos, SortedMap dfInfos, - Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { + protected DataSelectionServiceForStorageUnitDatafile( + SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, + Boolean zip, Boolean compress, RequestType requestType) { - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, + zip, compress, requestType); } - @Override public SortedMap getPrimaryDataInfos() { return this.dataSelection.getDfInfo(); } @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { - return ServiceProvider.getInstance().getMainStorage().exists(dataInfo.getLocation()); + public boolean existsInMainStorage(DataInfoBase dataInfo) + throws InternalException { + return ServiceProvider.getInstance().getMainStorage() + .exists(dataInfo.getLocation()); } - @Override public boolean isPrepared(String preparedId) throws InternalException { return areDataInfosPrepared(preparedId); } - @Override - public void queueDelete() throws NotImplementedException, InternalException { + public void queueDelete() + throws NotImplementedException, InternalException { this.scheduleTasks(DeferredOp.DELETE); } - @Override - public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) + throws NotImplementedException, InternalException { for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); } } - + } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java index ff1b5c65..d75a4920 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java @@ -12,49 +12,56 @@ import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.services.ServiceProvider; -public class DataSelectionServiceForStorageUnitDataset extends DataSelectionService { +public class DataSelectionServiceForStorageUnitDataset + extends DataSelectionService { - protected DataSelectionServiceForStorageUnitDataset(SortedMap dsInfos, SortedMap dfInfos, - Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { + protected DataSelectionServiceForStorageUnitDataset( + SortedMap dsInfos, + SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, + Boolean zip, Boolean compress, RequestType requestType) { - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, + zip, compress, requestType); } - @Override public SortedMap getPrimaryDataInfos() { return this.dataSelection.getDsInfo(); } @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { + public boolean existsInMainStorage(DataInfoBase dataInfo) + throws InternalException { var dsInfo = (DatasetInfo) dataInfo; - if(dsInfo == null) throw new InternalException("Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); - - return this.dataSelection.getEmptyDatasets().contains(dataInfo.getId()) || ServiceProvider.getInstance().getMainStorage().exists(dsInfo); - } + if (dsInfo == null) + throw new InternalException( + "Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); + return this.dataSelection.getEmptyDatasets().contains(dataInfo.getId()) + || ServiceProvider.getInstance().getMainStorage() + .exists(dsInfo); + } @Override public boolean isPrepared(String preparedId) throws InternalException { return this.areDataInfosPrepared(preparedId); } - @Override - public void queueDelete() throws NotImplementedException, InternalException { + public void queueDelete() + throws NotImplementedException, InternalException { this.scheduleTasks(DeferredOp.WRITE); } - @Override - public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) + throws NotImplementedException, InternalException { for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); } } - } diff --git a/src/main/java/org/icatproject/ids/thread/DfArchiver.java b/src/main/java/org/icatproject/ids/thread/DfArchiver.java index 15df979f..d1286741 100644 --- a/src/main/java/org/icatproject/ids/thread/DfArchiver.java +++ b/src/main/java/org/icatproject/ids/thread/DfArchiver.java @@ -7,17 +7,19 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; /* * Removes datafiles from the fast storage (doesn't write them to archive storage) */ public class DfArchiver implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DfArchiver.class); + private final static Logger logger = LoggerFactory + .getLogger(DfArchiver.class); private MainStorageInterface mainStorageInterface; private FiniteStateMachine fsm; @@ -25,7 +27,9 @@ public class DfArchiver implements Runnable { private Path markerDir; private Collection locks; - public DfArchiver(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { + public DfArchiver(List dfInfos, + PropertyHandler propertyHandler, FiniteStateMachine fsm, + Collection locks) { this.dfInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -38,16 +42,19 @@ public void run() { try { for (DatafileInfo dfInfo : dfInfos) { try { - if (Files.exists(markerDir.resolve(Long.toString(dfInfo.getDfId())))) { + if (Files.exists(markerDir + .resolve(Long.toString(dfInfo.getDfId())))) { logger.error("Archive of " + dfInfo + " not carried out because a write to secondary storage operation failed previously"); } else { String dfLocation = dfInfo.getDfLocation(); - mainStorageInterface.delete(dfLocation, dfInfo.getCreateId(), dfInfo.getModId()); + mainStorageInterface.delete(dfLocation, + dfInfo.getCreateId(), dfInfo.getModId()); logger.debug("Archive of " + dfInfo + " completed"); } } catch (Exception e) { - logger.error("Archive of " + dfInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error("Archive of " + dfInfo + " failed due to " + + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dfInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DfDeleter.java b/src/main/java/org/icatproject/ids/thread/DfDeleter.java index e0b4dab6..5f1dd0cc 100644 --- a/src/main/java/org/icatproject/ids/thread/DfDeleter.java +++ b/src/main/java/org/icatproject/ids/thread/DfDeleter.java @@ -5,25 +5,29 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.ArchiveStorageInterface; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; /** * Delete datafiles from archive */ public class DfDeleter implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DfDeleter.class); + private final static Logger logger = LoggerFactory + .getLogger(DfDeleter.class); private FiniteStateMachine fsm; private ArchiveStorageInterface archiveStorageInterface; private List dfInfos; private Collection locks; - public DfDeleter(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { + public DfDeleter(List dfInfos, + PropertyHandler propertyHandler, FiniteStateMachine fsm, + Collection locks) { this.dfInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -39,7 +43,8 @@ public void run() { archiveStorageInterface.delete(dfLocation); logger.debug("Delete of " + dfInfo + " completed"); } catch (Exception e) { - logger.error("Delete of " + dfInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error("Delete of " + dfInfo + " failed due to " + + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dfInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DfRestorer.java b/src/main/java/org/icatproject/ids/thread/DfRestorer.java index a97327b5..3c38baa6 100644 --- a/src/main/java/org/icatproject/ids/thread/DfRestorer.java +++ b/src/main/java/org/icatproject/ids/thread/DfRestorer.java @@ -8,20 +8,22 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.ArchiveStorageInterface; import org.icatproject.ids.plugin.DfInfo; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; /* * Restores datafiles from the slow to the fast storage. */ public class DfRestorer implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DfRestorer.class); + private final static Logger logger = LoggerFactory + .getLogger(DfRestorer.class); private MainStorageInterface mainStorageInterface; private ArchiveStorageInterface archiveStorageInterface; @@ -29,7 +31,9 @@ public class DfRestorer implements Runnable { private List dataFileInfos; private Collection locks; - public DfRestorer(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { + public DfRestorer(List dfInfos, + PropertyHandler propertyHandler, FiniteStateMachine fsm, + Collection locks) { this.dataFileInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -61,14 +65,14 @@ public void run() { } } - //TODO: This is additional conversion caused by the redesign :-( + // TODO: This is additional conversion caused by the redesign :-( List dfInfos = new ArrayList<>(); - for(DfInfo dfInfo : this.dataFileInfos) { + for (DfInfo dfInfo : this.dataFileInfos) { dfInfos.add(dfInfo); } - - Set failures = archiveStorageInterface.restore(mainStorageInterface, dfInfos); + Set failures = archiveStorageInterface + .restore(mainStorageInterface, dfInfos); for (DatafileInfo dfInfo : dataFileInfos) { if (failures.contains(dfInfo)) { fsm.recordFailure(dfInfo.getDfId()); @@ -81,7 +85,8 @@ public void run() { } } catch (Exception e) { for (DatafileInfo dfInfo : dataFileInfos) { - logger.error("Restore of " + dfInfo + " failed " + e.getClass() + " " + e.getMessage()); + logger.error("Restore of " + dfInfo + " failed " + e.getClass() + + " " + e.getMessage()); fsm.removeFromChanging(dfInfo); } } finally { diff --git a/src/main/java/org/icatproject/ids/thread/DfWriter.java b/src/main/java/org/icatproject/ids/thread/DfWriter.java index 09881acc..82ee3052 100644 --- a/src/main/java/org/icatproject/ids/thread/DfWriter.java +++ b/src/main/java/org/icatproject/ids/thread/DfWriter.java @@ -8,19 +8,21 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.ArchiveStorageInterface; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; /** * Copies datafiles from main to archive */ public class DfWriter implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DfWriter.class); + private final static Logger logger = LoggerFactory + .getLogger(DfWriter.class); private FiniteStateMachine fsm; private MainStorageInterface mainStorageInterface; @@ -29,7 +31,8 @@ public class DfWriter implements Runnable { private List dataFileInfos; private Collection locks; - public DfWriter(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { + public DfWriter(List dfInfos, PropertyHandler propertyHandler, + FiniteStateMachine fsm, Collection locks) { this.dataFileInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -43,14 +46,17 @@ public void run() { try { for (DatafileInfo dataFileInfo : dataFileInfos) { String dfLocation = dataFileInfo.getDfLocation(); - try (InputStream is = mainStorageInterface.get(dfLocation, dataFileInfo.getCreateId(), dataFileInfo.getModId())) { + try (InputStream is = mainStorageInterface.get(dfLocation, + dataFileInfo.getCreateId(), dataFileInfo.getModId())) { archiveStorageInterface.put(is, dfLocation); - Path marker = markerDir.resolve(Long.toString(dataFileInfo.getDfId())); + Path marker = markerDir + .resolve(Long.toString(dataFileInfo.getDfId())); Files.deleteIfExists(marker); logger.debug("Removed marker " + marker); logger.debug("Write of " + dataFileInfo + " completed"); } catch (Exception e) { - logger.error("Write of " + dataFileInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error("Write of " + dataFileInfo + " failed due to " + + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dataFileInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DsArchiver.java b/src/main/java/org/icatproject/ids/thread/DsArchiver.java index 81c2c664..02c84314 100644 --- a/src/main/java/org/icatproject/ids/thread/DsArchiver.java +++ b/src/main/java/org/icatproject/ids/thread/DsArchiver.java @@ -5,17 +5,19 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; /* * Removes datasets from the fast storage (doesn't write them to slow storage) */ public class DsArchiver implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DsArchiver.class); + private final static Logger logger = LoggerFactory + .getLogger(DsArchiver.class); private DatasetInfo dsInfo; private MainStorageInterface mainStorageInterface; @@ -23,7 +25,8 @@ public class DsArchiver implements Runnable { private Path markerDir; private Lock lock; - public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, Lock lock) { + public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, + FiniteStateMachine fsm, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; mainStorageInterface = propertyHandler.getMainStorage(); @@ -34,7 +37,8 @@ public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteSta @Override public void run() { try { - if (Files.exists(markerDir.resolve(Long.toString(dsInfo.getDsId())))) { + if (Files.exists( + markerDir.resolve(Long.toString(dsInfo.getDsId())))) { logger.error("Archive of " + dsInfo + " not carried out because a write to secondary storage operation failed previously"); } else { @@ -42,7 +46,8 @@ public void run() { logger.debug("Archive of " + dsInfo + " completed"); } } catch (Exception e) { - logger.error("Archive of " + dsInfo + " failed due to " + e.getMessage()); + logger.error("Archive of " + dsInfo + " failed due to " + + e.getMessage()); } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/main/java/org/icatproject/ids/thread/DsRestorer.java b/src/main/java/org/icatproject/ids/thread/DsRestorer.java index 20c7b49a..dc142398 100644 --- a/src/main/java/org/icatproject/ids/thread/DsRestorer.java +++ b/src/main/java/org/icatproject/ids/thread/DsRestorer.java @@ -16,6 +16,7 @@ import org.icatproject.Datafile; import org.icatproject.Dataset; + import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.helpers.LocationHelper; import org.icatproject.ids.models.DatasetInfo; @@ -23,15 +24,16 @@ import org.icatproject.ids.plugin.MainStorageInterface; import org.icatproject.ids.plugin.ZipMapperInterface; import org.icatproject.ids.services.IcatReader; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; /* * Restores datafiles from the slow to the fast storage. */ public class DsRestorer implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DsRestorer.class); + private final static Logger logger = LoggerFactory + .getLogger(DsRestorer.class); private DatasetInfo dsInfo; @@ -46,7 +48,8 @@ public class DsRestorer implements Runnable { private ZipMapperInterface zipMapper; private Lock lock; - public DsRestorer(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, IcatReader reader, Lock lock) { + public DsRestorer(DatasetInfo dsInfo, PropertyHandler propertyHandler, + FiniteStateMachine fsm, IcatReader reader, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; zipMapper = propertyHandler.getZipMapper(); @@ -71,39 +74,48 @@ public void run() { long size = 0; int n = 0; - List datafiles = ((Dataset) reader.get("Dataset INCLUDE Datafile", dsInfo.getDsId())) + List datafiles = ((Dataset) reader + .get("Dataset INCLUDE Datafile", dsInfo.getDsId())) .getDatafiles(); - Map nameToLocalMap = new HashMap<>(datafiles.size()); + Map nameToLocalMap = new HashMap<>( + datafiles.size()); for (Datafile datafile : datafiles) { if (datafile.getLocation() == null) { continue; } - nameToLocalMap.put(datafile.getName(), LocationHelper.getLocation(datafile.getId(), datafile.getLocation())); + nameToLocalMap.put(datafile.getName(), LocationHelper + .getLocation(datafile.getId(), datafile.getLocation())); size += datafile.getFileSize(); n++; } - logger.debug("Restoring dataset " + dsInfo.getInvId() + "/" + dsInfo.getDsId() + " with " + n - + " files of total size " + size); + logger.debug("Restoring dataset " + dsInfo.getInvId() + "/" + + dsInfo.getDsId() + " with " + n + " files of total size " + + size); // Get the file into the dataset cache - Path datasetCachePath = Files.createTempFile(datasetCache, null, null); + Path datasetCachePath = Files.createTempFile(datasetCache, null, + null); archiveStorageInterface.get(dsInfo, datasetCachePath); // Now split file and store it locally - logger.debug("Unpacking dataset " + dsInfo.getInvId() + "/" + dsInfo.getDsId() + " with " + n - + " files of total size " + size); - ZipInputStream zis = new ZipInputStream(Files.newInputStream(datasetCachePath)); + logger.debug("Unpacking dataset " + dsInfo.getInvId() + "/" + + dsInfo.getDsId() + " with " + n + " files of total size " + + size); + ZipInputStream zis = new ZipInputStream( + Files.newInputStream(datasetCachePath)); ZipEntry ze = zis.getNextEntry(); Set seen = new HashSet<>(); while (ze != null) { String dfName = zipMapper.getFileName(ze.getName()); if (seen.contains(dfName)) { - throw new RuntimeException("Corrupt archive for " + dsInfo + ": duplicate entry " + dfName); + throw new RuntimeException("Corrupt archive for " + dsInfo + + ": duplicate entry " + dfName); } String location = nameToLocalMap.get(dfName); if (location == null) { - throw new RuntimeException("Corrupt archive for " + dsInfo + ": spurious entry " + dfName); + throw new RuntimeException("Corrupt archive for " + dsInfo + + ": spurious entry " + dfName); } mainStorageInterface.put(zis, location); ze = zis.getNextEntry(); @@ -111,14 +123,16 @@ public void run() { } zis.close(); if (!seen.equals(nameToLocalMap.keySet())) { - throw new RuntimeException("Corrupt archive for " + dsInfo + ": missing entries"); + throw new RuntimeException( + "Corrupt archive for " + dsInfo + ": missing entries"); } Files.delete(datasetCachePath); fsm.recordSuccess(dsInfo.getDsId()); logger.debug("Restore of " + dsInfo + " completed"); } catch (Exception e) { fsm.recordFailure(dsInfo.getDsId()); - logger.error("Restore of " + dsInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error("Restore of " + dsInfo + " failed due to " + + e.getClass() + " " + e.getMessage()); try { mainStorageInterface.delete(dsInfo); } catch (IOException e2) { diff --git a/src/main/java/org/icatproject/ids/thread/DsWriter.java b/src/main/java/org/icatproject/ids/thread/DsWriter.java index e5c1843b..472dfab0 100644 --- a/src/main/java/org/icatproject/ids/thread/DsWriter.java +++ b/src/main/java/org/icatproject/ids/thread/DsWriter.java @@ -14,6 +14,7 @@ import org.icatproject.Datafile; import org.icatproject.Dataset; + import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.helpers.LocationHelper; import org.icatproject.ids.models.DatafileInfo; @@ -22,15 +23,16 @@ import org.icatproject.ids.plugin.MainStorageInterface; import org.icatproject.ids.plugin.ZipMapperInterface; import org.icatproject.ids.services.IcatReader; -import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.LockManager.Lock; +import org.icatproject.ids.services.PropertyHandler; /** * Copies dataset from main to archive */ public class DsWriter implements Runnable { - private final static Logger logger = LoggerFactory.getLogger(DsWriter.class); + private final static Logger logger = LoggerFactory + .getLogger(DsWriter.class); private static final int BUFSIZ = 1024; private DatasetInfo dsInfo; @@ -43,7 +45,8 @@ public class DsWriter implements Runnable { private ZipMapperInterface zipMapper; private Lock lock; - public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, IcatReader reader, Lock lock) { + public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, + FiniteStateMachine fsm, IcatReader reader, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; this.zipMapper = propertyHandler.getZipMapper(); @@ -59,27 +62,35 @@ public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteState public void run() { try { if (!mainStorageInterface.exists(dsInfo)) { - logger.info("No files present in main storage for " + dsInfo + " - will delete archive"); + logger.info("No files present in main storage for " + dsInfo + + " - will delete archive"); archiveStorageInterface.delete(dsInfo); } else { - Path datasetCachePath = Files.createTempFile(datasetCache, null, null); + Path datasetCachePath = Files.createTempFile(datasetCache, null, + null); logger.debug("Creating " + datasetCachePath); - List datafiles = ((Dataset) reader.get("Dataset INCLUDE Datafile", dsInfo.getDsId())) + List datafiles = ((Dataset) reader + .get("Dataset INCLUDE Datafile", dsInfo.getDsId())) .getDatafiles(); - ZipOutputStream zos = new ZipOutputStream( - Files.newOutputStream(datasetCachePath, StandardOpenOption.CREATE)); + ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream( + datasetCachePath, StandardOpenOption.CREATE)); for (Datafile datafile : datafiles) { if (datafile.getLocation() == null) { continue; } - String location = LocationHelper.getLocation(datafile.getId(), datafile.getLocation()); + String location = LocationHelper.getLocation( + datafile.getId(), datafile.getLocation()); InputStream is = null; try { - zos.putNextEntry(new ZipEntry( - zipMapper.getFullEntryName(dsInfo, new DatafileInfo(datafile.getId(), datafile.getName(), - location, datafile.getCreateId(), datafile.getModId(), 0L)))); - is = mainStorageInterface.get(location, datafile.getCreateId(), datafile.getModId()); + zos.putNextEntry( + new ZipEntry(zipMapper.getFullEntryName(dsInfo, + new DatafileInfo(datafile.getId(), + datafile.getName(), location, + datafile.getCreateId(), + datafile.getModId(), 0L)))); + is = mainStorageInterface.get(location, + datafile.getCreateId(), datafile.getModId()); int bytesRead = 0; byte[] buffer = new byte[BUFSIZ]; while ((bytesRead = is.read(buffer)) > 0) { @@ -105,7 +116,8 @@ public void run() { logger.debug("Removed marker " + marker); logger.debug("Write of " + dsInfo + " completed"); } catch (Exception e) { - logger.error("Write of " + dsInfo + " failed due to " + e.getClass() + " " + e.getMessage()); + logger.error("Write of " + dsInfo + " failed due to " + e.getClass() + + " " + e.getMessage()); } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/test/java/org/icatproject/ids/DataSelectionDevTest.java b/src/test/java/org/icatproject/ids/DataSelectionDevTest.java index f0ba796e..1dbc55eb 100644 --- a/src/test/java/org/icatproject/ids/DataSelectionDevTest.java +++ b/src/test/java/org/icatproject/ids/DataSelectionDevTest.java @@ -1,5 +1,8 @@ package org.icatproject.ids; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.net.URISyntaxException; @@ -10,18 +13,17 @@ import jakarta.json.Json; import jakarta.json.JsonReader; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import static org.mockito.Mockito.when; import org.mockito.junit.MockitoJUnitRunner; import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; import org.icatproject.icat.client.IcatException; + import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.services.ICATGetter; import org.icatproject.ids.services.IcatReader; @@ -31,8 +33,8 @@ /** * This test was created to fix issue #115 and was run against the Diamond - * pre-production ICAT using specifically selected usernames of users known - * to have access to a lot of data. + * pre-production ICAT using specifically selected usernames of users known to + * have access to a lot of data. * * As well as monitoring the time taken to create the DataSelection, detailed * monitoring of the eclipselink SQL logging was done to confirm that the @@ -74,21 +76,26 @@ public class DataSelectionDevTest { @Before public void setup() throws Exception { testProps = new Properties(); - testProps.load(new FileInputStream("src/test/resources/DataSelectionDevTest.properties")); + testProps.load(new FileInputStream( + "src/test/resources/DataSelectionDevTest.properties")); // set up the SOAP and REST ICAT clients icatUrl = ICATGetter.getCleanUrl(testProps.getProperty("icat.url")); icatService = ICATGetter.getService(icatUrl); restIcat = new org.icatproject.icat.client.ICAT(icatUrl); - // get session IDs for an end user and the reader user (with read-all permissions) + // get session IDs for an end user and the reader user (with read-all + // permissions) String userCredsString = testProps.getProperty("login.user"); userSessionId = TestUtils.login(icatService, userCredsString); System.out.println("userSessionId = " + userSessionId); - List readerCreds = Arrays.asList(testProps.getProperty("login.reader").trim().split("\\s+")); + List readerCreds = Arrays.asList( + testProps.getProperty("login.reader").trim().split("\\s+")); this.readerCreds = readerCreds; investigationIds = testProps.getProperty("investigation.ids"); datasetIds = testProps.getProperty("dataset.ids"); datafileIds = testProps.getProperty("datafile.ids"); - useReaderForPerformance = testProps.getProperty("useReaderForPerformance").equalsIgnoreCase("true"); + useReaderForPerformance = testProps + .getProperty("useReaderForPerformance") + .equalsIgnoreCase("true"); // set up a mocked version of the PropertyHandler setupPropertyHandler(); icatReader = new IcatReader(mockedPropertyHandler); @@ -96,24 +103,32 @@ public void setup() throws Exception { private void setupPropertyHandler() throws URISyntaxException, IcatException_Exception, IcatException { - JsonReader parser = Json.createReader(new ByteArrayInputStream(restIcat.getProperties().getBytes())); + JsonReader parser = Json.createReader( + new ByteArrayInputStream(restIcat.getProperties().getBytes())); maxEntities = parser.readObject().getInt("maxEntities"); when(mockedPropertyHandler.getMaxEntities()).thenReturn(maxEntities); when(mockedPropertyHandler.getIcatService()).thenReturn(icatService); when(mockedPropertyHandler.getRestIcat()).thenReturn(restIcat); when(mockedPropertyHandler.getReader()).thenReturn(readerCreds); - when(mockedPropertyHandler.getUseReaderForPerformance()).thenReturn(useReaderForPerformance); + when(mockedPropertyHandler.getUseReaderForPerformance()) + .thenReturn(useReaderForPerformance); } @Ignore("Test requires a specific ICAT setup to produce meaningful results. See class javadoc comment.") @Test public void testCreateDataSelection() throws Exception { long startMs = System.currentTimeMillis(); - var dataSelectionFactory = DataSelectionServiceFactory.getInstanceOnlyForTesting(mockedPropertyHandler, icatReader); - DataSelectionService dataSelectionService = dataSelectionFactory.getSelectionService(userSessionId,investigationIds, datasetIds, datafileIds, RequestType.GETSIZE); - System.out.println("Creating DataSelection took " + (System.currentTimeMillis() - startMs) + " ms"); - System.out.println("DsInfo size: " + dataSelectionService.getDsInfo().size()); - System.out.println("DfInfo size: " + dataSelectionService.getDfInfo().size()); + var dataSelectionFactory = DataSelectionServiceFactory + .getInstanceOnlyForTesting(mockedPropertyHandler, icatReader); + DataSelectionService dataSelectionService = dataSelectionFactory + .getSelectionService(userSessionId, investigationIds, + datasetIds, datafileIds, RequestType.GETSIZE); + System.out.println("Creating DataSelection took " + + (System.currentTimeMillis() - startMs) + " ms"); + System.out.println( + "DsInfo size: " + dataSelectionService.getDsInfo().size()); + System.out.println( + "DfInfo size: " + dataSelectionService.getDfInfo().size()); // there must be at least one Datafile in the DataSelection assertTrue("message", dataSelectionService.getDfInfo().size() > 0); } diff --git a/src/test/java/org/icatproject/ids/DigestTest.java b/src/test/java/org/icatproject/ids/DigestTest.java index 735e6195..1e3d4199 100644 --- a/src/test/java/org/icatproject/ids/DigestTest.java +++ b/src/test/java/org/icatproject/ids/DigestTest.java @@ -3,11 +3,13 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; + import org.junit.Test; +import org.icatproject.utils.IcatSecurity; + import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.helpers.LocationHelper; -import org.icatproject.utils.IcatSecurity; public class DigestTest { @@ -16,15 +18,18 @@ public class DigestTest { @Test public void testCheck() throws Exception { - String a = location + " " + IcatSecurity.digest(1234567L, location, key); + String a = location + " " + + IcatSecurity.digest(1234567L, location, key); - assertEquals(location, LocationHelper.getLocationFromDigest(1234567L, a, key)); + assertEquals(location, + LocationHelper.getLocationFromDigest(1234567L, a, key)); try { LocationHelper.getLocationFromDigest(1234568L, a, key); fail(); } catch (InsufficientPrivilegesException e) { - assertTrue(e.getMessage().contains("does not contain a valid hash")); + assertTrue( + e.getMessage().contains("does not contain a valid hash")); } try { diff --git a/src/test/java/org/icatproject/ids/PreparePackingTest.java b/src/test/java/org/icatproject/ids/PreparePackingTest.java index 6b2a857e..4aee2710 100644 --- a/src/test/java/org/icatproject/ids/PreparePackingTest.java +++ b/src/test/java/org/icatproject/ids/PreparePackingTest.java @@ -1,5 +1,11 @@ package org.icatproject.ids; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -11,17 +17,12 @@ import java.util.Map.Entry; import java.util.Set; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import org.junit.Test; -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.models.Prepared; -import org.junit.Test; public class PreparePackingTest { @@ -37,21 +38,24 @@ public void packAndUnpack() throws Exception { long dsid2 = 18L; long invId = 15L; long facilityId = 45L; - dfInfos.put(5L, new DatafileInfo(5L, "dfName", "dfLocation", "createId", "modId", dsid1)); + dfInfos.put(5L, new DatafileInfo(5L, "dfName", "dfLocation", "createId", + "modId", dsid1)); - dfInfos.put(51L, new DatafileInfo(51L, "dfName2", null, "createId", "modId", dsid1)); + dfInfos.put(51L, new DatafileInfo(51L, "dfName2", null, "createId", + "modId", dsid1)); - dsInfos.put(dsid1, new DatasetInfo(dsid1, "dsName", "dsLocation", invId, "invName", - "visitId", facilityId, "facilityName")); + dsInfos.put(dsid1, new DatasetInfo(dsid1, "dsName", "dsLocation", invId, + "invName", "visitId", facilityId, "facilityName")); - dsInfos.put(dsid2, new DatasetInfo(dsid2, "dsName2", null, invId, "invName", "visitId", - facilityId, "facilityName")); + dsInfos.put(dsid2, new DatasetInfo(dsid2, "dsName2", null, invId, + "invName", "visitId", facilityId, "facilityName")); emptyDatasets.add(dsid2); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (OutputStream stream = new BufferedOutputStream(baos)) { - Prepared.pack(stream, zip, compress, dsInfos, dfInfos, emptyDatasets, 51); + Prepared.pack(stream, zip, compress, dsInfos, dfInfos, + emptyDatasets, 51); } System.out.println(baos.toString()); InputStream stream = new ByteArrayInputStream(baos.toByteArray()); diff --git a/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java b/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java index e6960866..ae06d898 100644 --- a/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java +++ b/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java @@ -1,12 +1,13 @@ package org.icatproject.ids; +import static org.junit.Assert.assertEquals; + import java.io.ByteArrayOutputStream; import java.io.OutputStream; -import static org.junit.Assert.assertEquals; +import org.junit.Test; import org.icatproject.ids.helpers.RangeOutputStream; -import org.junit.Test; public class RangeOutputStreamTest { @@ -49,7 +50,8 @@ public void t3() throws Exception { os.flush(); } os.close(); - assertEquals("m is " + m, "ABCDEFGHIJKLMONPQRSTUVWXYZ", baos.toString()); + assertEquals("m is " + m, "ABCDEFGHIJKLMONPQRSTUVWXYZ", + baos.toString()); } } @@ -68,7 +70,6 @@ public void t4() throws Exception { } } - @Test public void t5() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); diff --git a/src/test/java/org/icatproject/ids/TestICATGetter.java b/src/test/java/org/icatproject/ids/TestICATGetter.java index 8bfa99c6..7470024f 100644 --- a/src/test/java/org/icatproject/ids/TestICATGetter.java +++ b/src/test/java/org/icatproject/ids/TestICATGetter.java @@ -1,26 +1,31 @@ package org.icatproject.ids; import static org.junit.Assert.assertEquals; + import org.junit.Test; import org.icatproject.ICAT; + import org.icatproject.ids.services.ICATGetter; public class TestICATGetter { @Test public void testGFUrl() throws Exception { - assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost/ICATService/ICAT?wsdl")); + assertEquals("http://localhost", ICATGetter + .getCleanUrl("http://localhost/ICATService/ICAT?wsdl")); } @Test public void testSlash() throws Exception { - assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost/")); + assertEquals("http://localhost", + ICATGetter.getCleanUrl("http://localhost/")); } @Test public void testClean() throws Exception { - assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost")); + assertEquals("http://localhost", + ICATGetter.getCleanUrl("http://localhost")); } @Test diff --git a/src/test/java/org/icatproject/ids/TestUtils.java b/src/test/java/org/icatproject/ids/TestUtils.java index cbd62530..7b313f42 100644 --- a/src/test/java/org/icatproject/ids/TestUtils.java +++ b/src/test/java/org/icatproject/ids/TestUtils.java @@ -12,15 +12,16 @@ public class TestUtils { /** * Do an ICAT login and get the session ID using a list of credentials of - * the format (as found in the properties files): - * "db username READER password READERpass" + * the format (as found in the properties files): "db username READER + * password READERpass" * * @param icatService the ICAT to log in to * @param credsString a String of credentials in the format described above * @return an ICAT session ID * @throws IcatException_Exception if the login fails */ - public static String login(ICAT icatService, String credsString) throws IcatException_Exception { + public static String login(ICAT icatService, String credsString) + throws IcatException_Exception { List creds = Arrays.asList(credsString.trim().split("\\s+")); Credentials credentials = new Credentials(); List entries = credentials.getEntry(); diff --git a/src/test/java/org/icatproject/ids/TestValueContainer.java b/src/test/java/org/icatproject/ids/TestValueContainer.java index bc3daf98..f592c7c5 100644 --- a/src/test/java/org/icatproject/ids/TestValueContainer.java +++ b/src/test/java/org/icatproject/ids/TestValueContainer.java @@ -10,17 +10,17 @@ import java.nio.charset.StandardCharsets; import java.util.stream.Collectors; +import org.junit.Test; + import org.icatproject.ids.enums.ValueContainerType; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.helpers.ValueContainer; -import org.junit.Test; - public class TestValueContainer { @Test public void testInvalidValueContainer() throws Exception { - + var vc = ValueContainer.getInvalid(); assertFalse(vc.isVoid()); assertTrue(vc.getType() == ValueContainerType.INVALID); @@ -28,7 +28,7 @@ public void testInvalidValueContainer() throws Exception { @Test public void testVoidValueContainer() throws Exception { - + var vc = ValueContainer.getVoid(); assertTrue(vc.isVoid()); assertTrue(vc.getType() == ValueContainerType.VOID); @@ -37,7 +37,7 @@ public void testVoidValueContainer() throws Exception { @Test(expected = InternalException.class) public void testIntValueContainer() throws Exception { - + var vc = new ValueContainer(1); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -50,7 +50,7 @@ public void testIntValueContainer() throws Exception { @Test(expected = InternalException.class) public void testLongValueContainer() throws Exception { - + var vc = new ValueContainer(1L); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -63,7 +63,7 @@ public void testLongValueContainer() throws Exception { @Test(expected = InternalException.class) public void testBoolValueContainer() throws Exception { - + var vc = new ValueContainer(true); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -76,7 +76,7 @@ public void testBoolValueContainer() throws Exception { @Test(expected = InternalException.class) public void testStringValueContainer() throws Exception { - + var vc = new ValueContainer("test"); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -89,7 +89,7 @@ public void testStringValueContainer() throws Exception { @Test(expected = InternalException.class) public void testInputStreamValueContainer() throws Exception { - + String s = "test InputStream"; var inputStream = new ByteArrayInputStream(s.getBytes()); var vc = new ValueContainer(inputStream); @@ -98,8 +98,9 @@ public void testInputStreamValueContainer() throws Exception { assertFalse(vc.isNull()); assertEquals(vc.getInputStream(), inputStream); - - var ISReader = new InputStreamReader(vc.getInputStream(), StandardCharsets.UTF_8); + + var ISReader = new InputStreamReader(vc.getInputStream(), + StandardCharsets.UTF_8); var BReader = new BufferedReader(ISReader); String textFromStream = BReader.lines().collect(Collectors.joining()); BReader.close(); diff --git a/src/test/java/org/icatproject/ids/TestZipping.java b/src/test/java/org/icatproject/ids/TestZipping.java index 56dce770..34f92125 100644 --- a/src/test/java/org/icatproject/ids/TestZipping.java +++ b/src/test/java/org/icatproject/ids/TestZipping.java @@ -1,5 +1,7 @@ package org.icatproject.ids; +import static org.junit.Assert.assertEquals; + import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -11,7 +13,6 @@ import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; -import static org.junit.Assert.assertEquals; import org.junit.Test; public class TestZipping { @@ -19,12 +20,14 @@ public class TestZipping { @Test public void testDuplicates() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos)); + ZipOutputStream zos = new ZipOutputStream( + new BufferedOutputStream(baos)); // zos.setLevel(0); List in = new ArrayList<>(); - for (String entryName : Arrays.asList("abcd/qa", "abcd/qw", "abcd/qw", "abcd/qb", "abcd/qc", "abcd/qw")) { + for (String entryName : Arrays.asList("abcd/qa", "abcd/qw", "abcd/qw", + "abcd/qb", "abcd/qc", "abcd/qw")) { try { zos.putNextEntry(new ZipEntry(entryName)); @@ -42,7 +45,8 @@ public void testDuplicates() throws Exception { assertEquals(4, in.size()); - ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream( + baos.toByteArray()); ZipInputStream zis = new ZipInputStream(bais); int off = 0; diff --git a/src/test/java/org/icatproject/ids/TidierTest.java b/src/test/java/org/icatproject/ids/TidierTest.java index 8f2762c6..b4a5096a 100644 --- a/src/test/java/org/icatproject/ids/TidierTest.java +++ b/src/test/java/org/icatproject/ids/TidierTest.java @@ -1,13 +1,14 @@ package org.icatproject.ids; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.io.ByteArrayInputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -85,13 +86,16 @@ public void testCleanPreparedDir() throws Exception { @Test public void testAddStringConstraint() throws Exception { StringBuilder sb1 = new StringBuilder(); - Tidier.addStringConstraint(sb1, "df.location", "/path/to/normal/file", false); + Tidier.addStringConstraint(sb1, "df.location", "/path/to/normal/file", + false); assertEquals(" df.location = '/path/to/normal/file'", sb1.toString()); /* Fix error where a file path contains an apostrophe */ StringBuilder sb2 = new StringBuilder(); - Tidier.addStringConstraint(sb2, "df.location", "/path/to/Person's Files/myscript.py", false); - assertEquals(" df.location = '/path/to/Person''s Files/myscript.py'", sb2.toString()); + Tidier.addStringConstraint(sb2, "df.location", + "/path/to/Person's Files/myscript.py", false); + assertEquals(" df.location = '/path/to/Person''s Files/myscript.py'", + sb2.toString()); } @Test diff --git a/src/test/java/org/icatproject/ids/TransmittingTest.java b/src/test/java/org/icatproject/ids/TransmittingTest.java index 7e2e986c..205822de 100644 --- a/src/test/java/org/icatproject/ids/TransmittingTest.java +++ b/src/test/java/org/icatproject/ids/TransmittingTest.java @@ -7,9 +7,15 @@ import java.net.URISyntaxException; import java.nio.file.Paths; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; import org.icatproject.icat.client.IcatException; + import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.plugin.ArchiveStorageInterface; @@ -35,15 +41,11 @@ import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.Transmitter; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - /** - * This test was created when the building of the transmission body was made more comon and generic. - * It should ensure the right structure of the containing json. + * This test was created when the building of the transmission body was made + * more comon and generic. It should ensure the right structure of the + * containing json. */ @RunWith(MockitoJUnitRunner.class) public class TransmittingTest { @@ -58,7 +60,8 @@ public class TransmittingTest { private LockManager mockedLockManager; @Mock private IcatReader mockedReader; - @Mock ArchiveStorageInterface mockedArchiveStorage; + @Mock + ArchiveStorageInterface mockedArchiveStorage; @Mock private ICAT mockedIcat; @@ -85,81 +88,101 @@ private void setup() } @Test - public void testTransmitterBodyForArchiveRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForArchiveRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new ArchiveHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new ArchiveHandler(ip, sessionId, investigationIds, + datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetSizeRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetSizeRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new GetSizeHandler(ip, preparedId, null, null, null, null); + var handler = new GetSizeHandler(ip, preparedId, null, null, null, + null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetSizeHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); + handler = new GetSizeHandler(ip, null, sessionId, investigationIds, + datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetSizeFastRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetSizeFastRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new GetSizeHandlerForFastProcessing(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new GetSizeHandlerForFastProcessing(ip, sessionId, + investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForDeleteRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForDeleteRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new DeleteHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new DeleteHandler(ip, sessionId, investigationIds, + datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetDataFileIdsRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetDataFileIdsRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new GetDataFileIdsHandler(ip, preparedId, null, null, null, null); + var handler = new GetDataFileIdsHandler(ip, preparedId, null, null, + null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetDataFileIdsHandler("192.168.17.1", null, sessionId, investigationIds, datasetIds, datafileIds); + handler = new GetDataFileIdsHandler("192.168.17.1", null, sessionId, + investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetDataRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetDataRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new GetDataHandler(ip, preparedId, null, null, null, null, false, false, "", ""); + var handler = new GetDataHandler(ip, preparedId, null, null, null, null, + false, false, "", ""); String body = handler.provideTransmissionBody(); - assertEquals("{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"transferId\":-1}", body); + assertEquals( + "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"transferId\":-1}", + body); - handler = new GetDataHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds, false, false, "", ""); + handler = new GetDataHandler(ip, null, sessionId, investigationIds, + datasetIds, datafileIds, false, false, "", ""); body = handler.provideTransmissionBody(); - assertEquals("{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"transferId\":-1}", body); + assertEquals( + "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"transferId\":-1}", + body); } @Test - public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() + throws Exception { + setup(); var handler = new GetIcatUrlHandler(ip); @@ -168,8 +191,9 @@ public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() throws Exceptio } @Test - public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() + throws Exception { + setup(); var handler = new GetServiceStatusHandler(ip, sessionId); @@ -178,32 +202,39 @@ public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() throws Ex } @Test - public void testTransmitterBodyForGetStatusRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForGetStatusRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new GetStatusHandler(ip, preparedId, null, null, null, null); + var handler = new GetStatusHandler(ip, preparedId, null, null, null, + null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetStatusHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); + handler = new GetStatusHandler(ip, null, sessionId, investigationIds, + datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForIsPreparedRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForIsPreparedRequest_shouldBeOk() + throws Exception { + setup(); var handler = new IsPreparedHandler(ip, preparedId); String body = handler.provideTransmissionBody(); - assertEquals("{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}", body); + assertEquals( + "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}", + body); } @Test - public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() + throws Exception { + setup(); var handler = new IsReadOnlyHandler(ip); @@ -212,8 +243,9 @@ public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() throws Exceptio } @Test - public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() + throws Exception { + setup(); var handler = new IsTwoLevelHandler(ip); @@ -222,56 +254,69 @@ public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() throws Exceptio } @Test - public void testTransmitterBodyForPrepareDataRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForPrepareDataRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new PrepareDataHandler(ip, sessionId, investigationIds, datasetIds, datafileIds, false, false); + var handler = new PrepareDataHandler(ip, sessionId, investigationIds, + datasetIds, datafileIds, false, false); String body = handler.provideTransmissionBody(); - assertEquals("{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"preparedId\":\"\"}", body); + assertEquals( + "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"preparedId\":\"\"}", + body); } @Test public void testTransmitterBodyForPutRequest_shouldBeOk() throws Exception { - + setup(); - var handler = new PutHandler(ip, sessionId, new ByteArrayInputStream( "".getBytes() ), "someName", datafileIds , datasetIds, "someDescription", "someDOI", "simeCreateTimeString", "someModTimeString", false, false ); + var handler = new PutHandler(ip, sessionId, + new ByteArrayInputStream("".getBytes()), "someName", + datafileIds, datasetIds, "someDescription", "someDOI", + "simeCreateTimeString", "someModTimeString", false, false); String body = handler.provideTransmissionBody(); assertEquals("{\"userName\":\"TestUser\",\"datafileId\":-1}", body); } @Test - public void testTransmitterBodyForResetRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForResetRequest_shouldBeOk() + throws Exception { + setup(); var handler = new ResetHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new ResetHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); + handler = new ResetHandler(ip, null, sessionId, investigationIds, + datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForRestoreRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForRestoreRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new RestoreHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new RestoreHandler(ip, sessionId, investigationIds, + datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForWriteRequest_shouldBeOk() throws Exception { - + public void testTransmitterBodyForWriteRequest_shouldBeOk() + throws Exception { + setup(); - var handler = new WriteHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); + var handler = new WriteHandler(ip, sessionId, investigationIds, + datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } diff --git a/src/test/java/org/icatproject/ids/ValidaterTest.java b/src/test/java/org/icatproject/ids/ValidaterTest.java index 6dda09ef..2e9b05ec 100644 --- a/src/test/java/org/icatproject/ids/ValidaterTest.java +++ b/src/test/java/org/icatproject/ids/ValidaterTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; + import org.junit.Test; import org.icatproject.ids.exceptions.BadRequestException; @@ -32,4 +33,3 @@ private void testValidUUID(boolean b, String id) { } } - diff --git a/src/test/java/org/icatproject/ids/integration/BaseTest.java b/src/test/java/org/icatproject/ids/integration/BaseTest.java index 4f0a23d4..27cee18a 100644 --- a/src/test/java/org/icatproject/ids/integration/BaseTest.java +++ b/src/test/java/org/icatproject/ids/integration/BaseTest.java @@ -1,5 +1,10 @@ package org.icatproject.ids.integration; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -30,10 +35,6 @@ import jakarta.json.Json; import jakarta.json.JsonReader; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import org.junit.Before; import org.icatproject.Datafile; @@ -47,6 +48,7 @@ import org.icatproject.InvestigationType; import org.icatproject.icat.client.ICAT; import org.icatproject.icat.client.Session; + import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.TestingClient; @@ -60,13 +62,15 @@ public class BaseTest { public class DeleteVisitor extends SimpleFileVisitor { @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) + throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override - public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { + public FileVisitResult postVisitDirectory(Path dir, IOException e) + throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; @@ -80,15 +84,21 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOExce private static long timestamp = System.currentTimeMillis(); - protected Path getDirOnFastStorage(Long dsId) throws IcatException_Exception { - Dataset icatDs = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Investigation", dsId); - return setup.getStorageDir().resolve(Long.toString(icatDs.getInvestigation().getId())) + protected Path getDirOnFastStorage(Long dsId) + throws IcatException_Exception { + Dataset icatDs = (Dataset) icatWS.get(sessionId, + "Dataset INCLUDE Investigation", dsId); + return setup.getStorageDir() + .resolve(Long.toString(icatDs.getInvestigation().getId())) .resolve(Long.toString(icatDs.getId())); } - protected Path getFileOnArchiveStorage(Long dsId) throws IcatException_Exception { - Dataset icatDs = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Investigation", dsId); - return setup.getStorageArchiveDir().resolve(Long.toString(icatDs.getInvestigation().getId())) + protected Path getFileOnArchiveStorage(Long dsId) + throws IcatException_Exception { + Dataset icatDs = (Dataset) icatWS.get(sessionId, + "Dataset INCLUDE Investigation", dsId); + return setup.getStorageArchiveDir() + .resolve(Long.toString(icatDs.getInvestigation().getId())) .resolve(Long.toString(icatDs.getId())); } @@ -117,7 +127,8 @@ public void before() throws Exception { testingClient = new TestingClient(setup.getIdsUrl()); sessionId = setup.getRootSessionId(); waitForIds(); - populateStorage(setup.isTwoLevel(), setup.getStorageUnit(), setup.getKey()); + populateStorage(setup.isTwoLevel(), setup.getStorageUnit(), + setup.getKey()); } protected void checkAbsent(Path file) { @@ -158,7 +169,8 @@ protected void logTime(String msg) { long now = System.currentTimeMillis(); if (msg != null) { if (time != 0) { - System.out.println(msg + " took " + (now - time) / 1000. + "s."); + System.out + .println(msg + " took " + (now - time) / 1000. + "s."); } else { System.out.println(msg); } @@ -166,7 +178,8 @@ protected void logTime(String msg) { time = now; } - protected void checkZipFile(Path file, List ids, int compressedSize) throws IOException { + protected void checkZipFile(Path file, List ids, int compressedSize) + throws IOException { checkZipStream(Files.newInputStream(file), ids, compressedSize, 0); } @@ -205,8 +218,9 @@ protected byte[] getOutput(InputStream stream) throws IOException { } } - private void populateStorage(boolean twoLevel, String storageUnit, String key) - throws IOException, IcatException_Exception, NoSuchAlgorithmException { + private void populateStorage(boolean twoLevel, String storageUnit, + String key) throws IOException, IcatException_Exception, + NoSuchAlgorithmException { clearStorage(); long timestamp = System.currentTimeMillis(); @@ -233,7 +247,8 @@ private void populateStorage(boolean twoLevel, String storageUnit, String key) supportedDatafileFormat.setFacility(fac); supportedDatafileFormat.setName("test_format"); supportedDatafileFormat.setVersion("42.0.0"); - supportedDatafileFormat.setId(icatWS.create(sessionId, supportedDatafileFormat)); + supportedDatafileFormat + .setId(icatWS.create(sessionId, supportedDatafileFormat)); InvestigationType invType = new InvestigationType(); invType.setName("Not null"); @@ -277,25 +292,33 @@ private void populateStorage(boolean twoLevel, String storageUnit, String key) df1.setName("a/df1_" + timestamp); df1.setLocation(ds1Loc + UUID.randomUUID()); df1.setDataset(ds1); - writeToFile(df1, "df1 test content very compressible very compressible", key); + writeToFile(df1, + "df1 test content very compressible very compressible", + key); Datafile df2 = new Datafile(); df2.setName("df2_" + timestamp); df2.setLocation(ds1Loc + UUID.randomUUID()); df2.setDataset(ds1); - writeToFile(df2, "df2 test content very compressible very compressible", key); + writeToFile(df2, + "df2 test content very compressible very compressible", + key); Datafile df3 = new Datafile(); df3.setName("df3_" + timestamp); df3.setLocation(ds2Loc + UUID.randomUUID()); df3.setDataset(ds2); - writeToFile(df3, "df3 test content very compressible very compressible", key); + writeToFile(df3, + "df3 test content very compressible very compressible", + key); Datafile df4 = new Datafile(); df4.setName("df4_" + timestamp); df4.setLocation(ds2Loc + "Person's file"); df4.setDataset(ds2); - writeToFile(df4, "df4 test content very compressible very compressible", key); + writeToFile(df4, + "df4 test content very compressible very compressible", + key); datasetIds.add(ds1.getId()); datasetIds.add(ds2.getId()); @@ -311,14 +334,16 @@ private void populateStorage(boolean twoLevel, String storageUnit, String key) moveDatasetToArchive(storageUnit, ds2, ds2Loc, fac, inv, key); } - newFileLocation = setup.getUpdownDir().resolve("new_file_" + timestamp); + newFileLocation = setup.getUpdownDir() + .resolve("new_file_" + timestamp); Files.createDirectories(newFileLocation.getParent()); byte[] bytes = "new_file test content".getBytes(); OutputStream out = Files.newOutputStream(newFileLocation); out.write(bytes); out.close(); } catch (IllegalAccessError e) { - System.err.println("Could not prepare ICAT db for testing: " + e.getMessage()); + System.err.println( + "Could not prepare ICAT db for testing: " + e.getMessage()); e.printStackTrace(); throw e; } @@ -331,8 +356,8 @@ private void populateStorage(boolean twoLevel, String storageUnit, String key) protected Map ids = new HashMap<>(); protected Map paths = new HashMap<>(); - protected void checkZipStream(InputStream stream, List datafileIdsIn, long compressedSize, int numLeft) - throws IOException { + protected void checkZipStream(InputStream stream, List datafileIdsIn, + long compressedSize, int numLeft) throws IOException { ZipInputStream zis = new ZipInputStream(stream); ZipEntry ze = zis.getNextEntry(); List idsNeeded = new ArrayList<>(datafileIdsIn); @@ -361,7 +386,8 @@ protected void checkStream(InputStream stream, long id) throws IOException { boolean found = false; for (Entry e : ids.entrySet()) { if (id == e.getValue()) { - assertEquals(contents.get(e.getKey()), new String(getOutput(stream))); + assertEquals(contents.get(e.getKey()), + new String(getOutput(stream))); found = true; break; } @@ -370,7 +396,8 @@ protected void checkStream(InputStream stream, long id) throws IOException { } protected void writeToFile(Datafile df, String content, String key) - throws IOException, IcatException_Exception, NoSuchAlgorithmException { + throws IOException, IcatException_Exception, + NoSuchAlgorithmException { Path path = setup.getStorageDir().resolve(df.getLocation()); Files.createDirectories(path.getParent()); byte[] bytes = content.getBytes(); @@ -396,16 +423,16 @@ protected void writeToFile(Datafile df, String content, String key) Dataset ds = df.getDataset(); Investigation inv = ds.getInvestigation(); Facility fac = inv.getFacility(); - paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + URLEncoder.encode(inv.getVisitId(), "UTF-8") - + "/" + ds.getName() + "/" + df.getName(), df.getLocation()); + paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" + + ds.getName() + "/" + df.getName(), df.getLocation()); } - private static final char[] HEX_CHARS = { - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' - }; + private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', + '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; - private String digest(Long id, String location, String key) throws NoSuchAlgorithmException { + private String digest(Long id, String location, String key) + throws NoSuchAlgorithmException { byte[] pattern = (id + location + key).getBytes(); MessageDigest digest = null; digest = MessageDigest.getInstance("SHA-256"); @@ -420,16 +447,19 @@ private String digest(Long id, String location, String key) throws NoSuchAlgorit return new String(hexChars); } - private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, Facility fac, Investigation inv, - String key) throws IOException, IcatException_Exception { - ds = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Datafile", ds.getId()); + private void moveDatasetToArchive(String storageUnit, Dataset ds, + String dsLoc, Facility fac, Investigation inv, String key) + throws IOException, IcatException_Exception { + ds = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Datafile", + ds.getId()); Path top = setup.getStorageDir(); if (storageUnit.equals("DATASET")) { Path zipFile = setup.getStorageArchiveDir().resolve(dsLoc); Files.createDirectories(zipFile.getParent()); - ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile)); + ZipOutputStream zos = new ZipOutputStream( + Files.newOutputStream(zipFile)); zos.setLevel(0); for (Datafile df : ds.getDatafiles()) { @@ -437,12 +467,15 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, if (key == null) { file = top.resolve(df.getLocation()); } else { - file = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); + file = top.resolve(getLocationFromDigest(df.getId(), + df.getLocation())); } InputStream fis = Files.newInputStream(file); - zos.putNextEntry(new ZipEntry("ids/" + fac.getName() + "/" + inv.getName() + "/" - + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" + ds.getName() + "/" + df.getName())); + zos.putNextEntry(new ZipEntry( + "ids/" + fac.getName() + "/" + inv.getName() + "/" + + URLEncoder.encode(inv.getVisitId(), "UTF-8") + + "/" + ds.getName() + "/" + df.getName())); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { @@ -461,9 +494,11 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, if (key == null) { p = top.resolve(df.getLocation()); } else { - p = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); + p = top.resolve(getLocationFromDigest(df.getId(), + df.getLocation())); } - Files.move(p, archive.resolve(p.getFileName()), StandardCopyOption.REPLACE_EXISTING); + Files.move(p, archive.resolve(p.getFileName()), + StandardCopyOption.REPLACE_EXISTING); } } for (Datafile df : ds.getDatafiles()) { @@ -471,7 +506,8 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, if (key == null) { file = top.resolve(df.getLocation()); } else { - file = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); + file = top.resolve( + getLocationFromDigest(df.getId(), df.getLocation())); } Files.deleteIfExists(file); Path parent = file.getParent(); @@ -508,8 +544,10 @@ protected void raceTest() throws Exception { } logTime("File created"); - Investigation inv = (Investigation) icatWS.search(sessionId, "Investigation").get(0); - DatasetType dst = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); + Investigation inv = (Investigation) icatWS + .search(sessionId, "Investigation").get(0); + DatasetType dst = (DatasetType) icatWS.search(sessionId, "DatasetType") + .get(0); Dataset ds = new Dataset(); ds.setName("Big ds"); @@ -519,17 +557,21 @@ protected void raceTest() throws Exception { ds.setId(dsid); for (int i = 0; i < 30; i++) { - testingClient.put(sessionId, Files.newInputStream(path), "uploaded_file" + i, dsid, - supportedDatafileFormat.getId(), "A rather splendid datafile", 201); + testingClient.put(sessionId, Files.newInputStream(path), + "uploaded_file" + i, dsid, supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); } - testingClient.archive(sessionId, new DataSelection().addDataset(dsid), 204); + testingClient.archive(sessionId, new DataSelection().addDataset(dsid), + 204); logTime("Put and archive calls"); waitForIds(300); logTime("Archive complete"); - testingClient.restore(sessionId, new DataSelection().addDataset(dsid), 204); - while (testingClient.getStatus(sessionId, new DataSelection().addDataset(dsid), 200) != Status.ONLINE) { + testingClient.restore(sessionId, new DataSelection().addDataset(dsid), + 204); + while (testingClient.getStatus(sessionId, + new DataSelection().addDataset(dsid), 200) != Status.ONLINE) { Thread.sleep(1000); } logTime("Marked online"); @@ -538,7 +580,8 @@ protected void raceTest() throws Exception { assertTrue(stat.getOpItems().toString(), stat.getOpItems().isEmpty()); - testingClient.delete(sessionId, new DataSelection().addDataset(dsid), 204); + testingClient.delete(sessionId, new DataSelection().addDataset(dsid), + 204); icatWS.delete(sessionId, ds); waitForIds(300); @@ -551,16 +594,20 @@ protected void raceTest() throws Exception { public void getDatafileIdsTest() throws Exception { List ids = testingClient.getDatafileIds(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafile(datafileIds.get(0)), 200); + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), + 200); assertEquals(2, ids.size()); assertTrue(ids.contains(datafileIds.get(0))); assertTrue(ids.contains(datafileIds.get(1))); - ids = testingClient.getDatafileIds(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 200); + ids = testingClient.getDatafileIds(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), 200); assertEquals(1, ids.size()); assertTrue(ids.contains(datafileIds.get(0))); - ids = testingClient.getDatafileIds(sessionId, new DataSelection().addInvestigation(investigationId), 200); + ids = testingClient.getDatafileIds(sessionId, + new DataSelection().addInvestigation(investigationId), 200); assertEquals(4, ids.size()); for (Long id : datafileIds) { assertTrue(ids.contains(id)); @@ -571,24 +618,31 @@ public void getDatafileIdsTest() throws Exception { public void bigDataSelectionTest() throws Exception { String icatUrl = testingClient.getIcatUrl(200).toExternalForm(); ICAT restIcat = new org.icatproject.icat.client.ICAT(icatUrl); - try (JsonReader parser = Json.createReader(new ByteArrayInputStream(restIcat.getProperties().getBytes()))) { - assertEquals("maxEntities must have a fixed value in the icat.server for test to be useful", 20, - parser.readObject().getInt("maxEntities")); + try (JsonReader parser = Json.createReader(new ByteArrayInputStream( + restIcat.getProperties().getBytes()))) { + assertEquals( + "maxEntities must have a fixed value in the icat.server for test to be useful", + 20, parser.readObject().getInt("maxEntities")); } - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); List idList = new ArrayList<>(); for (int i = 0; i < 45; i++) { - Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp + i, datasetIds.get(0), supportedDatafileFormat.getId(), + Long dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp + i, datasetIds.get(0), + supportedDatafileFormat.getId(), "A rather splendid datafile", 201); idList.add(dfid); } waitForIds(); List idList2 = testingClient.getDatafileIds(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafile(datafileIds.get(0)), 200); + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), + 200); assertEquals(47, idList2.size()); for (Long id : idList) { assertTrue(idList2.contains(id)); @@ -605,69 +659,96 @@ public void bigDataSelectionTest() throws Exception { assertTrue(idList2.contains(idList.get(i))); } - idList2 = testingClient.getDatafileIds(sessionId, new DataSelection().addInvestigation(investigationId), 200); + idList2 = testingClient.getDatafileIds(sessionId, + new DataSelection().addInvestigation(investigationId), 200); assertEquals(49, idList2.size()); } public void cloningTest() throws Exception { - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); - assertEquals(104, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 200)); + assertEquals(104, testingClient.getSize(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 200)); Session s = icat.getSession(sessionId); Map m = new HashMap<>(); m.put("name", "newOne"); long dfid = datafileIds.get(0); long ndfid = s.cloneEntity("Datafile", dfid, m); - Datafile df = (Datafile) icatWS.get(sessionId, "Datafile df INCLUDE df.dataset.investigation.facility", ndfid); + Datafile df = (Datafile) icatWS.get(sessionId, + "Datafile df INCLUDE df.dataset.investigation.facility", ndfid); Dataset ds = df.getDataset(); Investigation inv = ds.getInvestigation(); Facility fac = inv.getFacility(); - paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + URLEncoder.encode(inv.getVisitId(), "UTF-8") - + "/" + ds.getName() + "/" + df.getName(), df.getLocation()); + paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" + + ds.getName() + "/" + df.getName(), df.getLocation()); crcs.put(df.getLocation(), df.getChecksum()); fsizes.put(df.getLocation(), df.getFileSize()); - contents.put(df.getLocation(), "df1 test content very compressible very compressible"); + contents.put(df.getLocation(), + "df1 test content very compressible very compressible"); ids.put(df.getLocation(), df.getId()); - assertEquals(156, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 200)); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, 200)) { + assertEquals(156, testingClient.getSize(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 200)); + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, + 200)) { List dfids = new ArrayList<>(datafileIds.subList(0, 2)); dfids.add(ndfid); checkZipStream(stream, dfids, 57, 0); } long dsid = datasetIds.get(0); assertEquals(2, getDirOnFastStorage(dsid).toFile().list().length); - assertEquals("[3]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); - assertEquals(2, getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); - assertEquals("[2]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(1)), 204); - assertEquals(1, getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); - assertEquals("[1]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); - testingClient.delete(sessionId, new DataSelection().addDatafile(ndfid), 204); + assertEquals("[3]", s.search( + "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + + dsid)); + testingClient.delete(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), 204); + assertEquals(2, + getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); + assertEquals("[2]", s.search( + "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + + dsid)); + testingClient.delete(sessionId, + new DataSelection().addDatafile(datafileIds.get(1)), 204); + assertEquals(1, + getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); + assertEquals("[1]", s.search( + "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + + dsid)); + testingClient.delete(sessionId, new DataSelection().addDatafile(ndfid), + 204); assertFalse(getDirOnFastStorage(datasetIds.get(0)).toFile().exists()); - assertEquals("[0]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); + assertEquals("[0]", s.search( + "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + + dsid)); } public void reliabilityTest() throws Exception { - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), + 204); waitForIds(); - Long dfid1 = testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file2_" + timestamp, - datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", 201); + Long dfid1 = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); - testingClient.archive(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); + testingClient.archive(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), + 204); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures() + .isEmpty()); setup.setReliability(0.); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(dfid1), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(dfid1), Flag.NONE, 200); try { while (!testingClient.isPrepared(preparedId, null)) { @@ -676,38 +757,49 @@ public void reliabilityTest() throws Exception { } catch (Exception e) { System.out.println(e); assertEquals("Restore failed", e.getMessage()); - Set failures = testingClient.getServiceStatus(sessionId, 200).getFailures(); + Set failures = testingClient.getServiceStatus(sessionId, 200) + .getFailures(); assertEquals(1, failures.size()); setup.setReliability(1.); testingClient.reset(preparedId, 204); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200) + .getFailures().isEmpty()); while (!testingClient.isPrepared(preparedId, null)) { Thread.sleep(1000); } } setup.setReliability(0.); - Long dfid2 = testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file3_" + timestamp, - datasetIds.get(0), supportedDatafileFormat.getId(), "An even better datafile", "7.1.3", - new Date(420000), new Date(42000), 201); + Long dfid2 = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "An even better datafile", + "7.1.3", new Date(420000), new Date(42000), 201); waitForIds(); System.out.println(testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 200)); + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(dfid2), + 200)); setup.setReliability(1.); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 204); + testingClient.restore(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDatafile(dfid2), 204); waitForIds(); System.out.println(testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 200)); + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(dfid2), + 200)); } protected void reliabilityTest2() throws Exception { - DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(0)); + DataSelection dsel = new DataSelection() + .addDatafile(datafileIds.get(0)); testingClient.archive(sessionId, dsel, 204); waitForIds(); setup.setReliability(0.); - String preparedId = testingClient.prepareData(sessionId, dsel, Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, dsel, + Flag.NONE, 200); try { while (!testingClient.isPrepared(preparedId, null)) { @@ -732,7 +824,8 @@ protected void reliabilityTest2() throws Exception { } protected void reliabilityTest3() throws Exception { - DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(0)); + DataSelection dsel = new DataSelection() + .addDatafile(datafileIds.get(0)); testingClient.archive(sessionId, dsel, 204); waitForIds(); @@ -740,7 +833,8 @@ protected void reliabilityTest3() throws Exception { testingClient.restore(sessionId, dsel, 204); try { - while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { + while (testingClient.getStatus(sessionId, dsel, + null) != Status.ONLINE) { Thread.sleep(1000); } fail("Should throw an error"); @@ -751,7 +845,8 @@ protected void reliabilityTest3() throws Exception { setup.setReliability(1.); testingClient.restore(sessionId, dsel, 204); try { - while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { + while (testingClient.getStatus(sessionId, dsel, + null) != Status.ONLINE) { Thread.sleep(1000); } } catch (Exception e) { @@ -760,7 +855,8 @@ protected void reliabilityTest3() throws Exception { testingClient.reset(sessionId, dsel, 204); testingClient.restore(sessionId, dsel, 204); - while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { + while (testingClient.getStatus(sessionId, dsel, + null) != Status.ONLINE) { Thread.sleep(1000); } } @@ -770,9 +866,11 @@ protected void isPreparedTest() throws Exception { int numDs = 30; int numDf = 19; - Investigation inv = (Investigation) icatWS.search(sessionId, "Investigation INCLUDE Facility").get(0); + Investigation inv = (Investigation) icatWS + .search(sessionId, "Investigation INCLUDE Facility").get(0); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS + .search(sessionId, "DatasetType").get(0); Facility fac = inv.getFacility(); String key = setup.getKey(); @@ -811,7 +909,8 @@ protected void isPreparedTest() throws Exception { logTime("Put calls"); - String preparedId = testingClient.prepareData(sessionId, dsel, Flag.ZIP, 200); + String preparedId = testingClient.prepareData(sessionId, dsel, Flag.ZIP, + 200); logTime("Data prepared"); while (!testingClient.isPrepared(preparedId, 200)) { @@ -829,7 +928,8 @@ protected void isPreparedTest() throws Exception { logTime("Read " + l + " bytes"); } - testingClient.archive(sessionId, new DataSelection().addDataset(dsid), 204); + testingClient.archive(sessionId, new DataSelection().addDataset(dsid), + 204); waitForIds(300); logTime(null); diff --git a/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java index d6cc867a..2daa2b7e 100644 --- a/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java @@ -1,15 +1,17 @@ package org.icatproject.ids.integration.one; +import static org.junit.Assert.assertEquals; + import java.io.InputStream; import java.util.Collections; -import static org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; import org.icatproject.Dataset; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; @@ -38,21 +40,24 @@ public static void setup() throws Exception { public void createBogusFiles() throws Exception { long timestamp = System.currentTimeMillis(); - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -67,9 +72,11 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -78,7 +85,8 @@ public void getEmptyDataset() throws Exception { @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); } @@ -86,9 +94,11 @@ public void getSizeEmptyDataset() throws Exception { @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } @@ -97,7 +107,8 @@ public void getNonEmptyDataset() throws Exception { @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); } @@ -105,9 +116,11 @@ public void getSizeNonEmptyDataset() throws Exception { @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -116,7 +129,8 @@ public void getBogusFile() throws Exception { @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); } diff --git a/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java index 5f5b4881..7314e6d1 100644 --- a/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java @@ -1,12 +1,13 @@ package org.icatproject.ids.integration.one; -import java.nio.file.Files; -import java.nio.file.Path; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; + +import java.nio.file.Files; +import java.nio.file.Path; + import org.junit.BeforeClass; import org.junit.Test; @@ -26,9 +27,12 @@ public static void setup() throws Exception { @Test public void deleteDatafileTest() throws Exception { - DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(3)); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); + DataSelection dsel = new DataSelection() + .addDatafile(datafileIds.get(3)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, dsel, 200)); + testingClient.delete(sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), 204); try { testingClient.getStatus(sessionId, dsel, 404); fail(); @@ -44,10 +48,12 @@ public void deleteDatafileTest() throws Exception { public void deleteDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); DataSelection dsel = new DataSelection().addDataset(datasetIds.get(1)); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, dsel, 200)); assertTrue(Files.exists(dirOnFastStorage)); testingClient.delete(sessionId, dsel, 204); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, dsel, 200)); assertFalse(Files.exists(dirOnFastStorage)); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java index 16026ef5..f7340ec2 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java @@ -1,13 +1,15 @@ package org.icatproject.ids.integration.one; +import static org.junit.Assert.assertEquals; + import java.io.InputStream; import java.util.Arrays; -import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -29,11 +31,13 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", + datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(209L, testingClient.getSize(sessionId, + new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -47,12 +51,15 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", + datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection().addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, + Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -64,59 +71,74 @@ public void getSizePreparedId() throws Exception { @Test public void getSizes1() throws Exception { - assertEquals(208L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); - assertEquals(208L, testingClient.getSize(sessionId, new DataSelection().addDatasets(datasetIds), 200)); - assertEquals(208L, - testingClient.getSize(sessionId, new DataSelection().addInvestigation(investigationId), 200)); assertEquals(208L, testingClient.getSize(sessionId, - new DataSelection().addInvestigation(investigationId).addDatafiles(datafileIds), 200)); - assertEquals(0L, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(2)), 200)); + new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(208L, testingClient.getSize(sessionId, + new DataSelection().addDatasets(datasetIds), 200)); + assertEquals(208L, testingClient.getSize(sessionId, + new DataSelection().addInvestigation(investigationId), 200)); + assertEquals(208L, + testingClient.getSize(sessionId, + new DataSelection().addInvestigation(investigationId) + .addDatafiles(datafileIds), + 200)); + assertEquals(0L, testingClient.getSize(sessionId, + new DataSelection().addDataset(datasetIds.get(2)), 200)); } @Test(expected = NotFoundException.class) public void getSizesBad() throws Exception { - testingClient.getSize(sessionId, new DataSelection().addDatafile(563L), 404); + testingClient.getSize(sessionId, new DataSelection().addDatafile(563L), + 404); } @Test(expected = NotFoundException.class) public void getSizesBad2() throws Exception { - testingClient.getSize(sessionId, new DataSelection().addDatafile(563L).addDatafile(564L), 404); + testingClient.getSize(sessionId, + new DataSelection().addDatafile(563L).addDatafile(564L), 404); } @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0, 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0, + 400)) { } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { + try (InputStream z = testingClient.getData( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + 403)) { } } @Test public void correctBehaviourTestNone() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 0, 200)) { checkStream(stream, datafileIds.get(0)); } } @Test public void correctBehaviourTestCompress() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.COMPRESS, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.COMPRESS, 0, + 200)) { checkZipStream(stream, datafileIds, 36L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.COMPRESS, 0, 200)) { checkStream(stream, datafileIds.get(0)); } @@ -124,25 +146,29 @@ public void correctBehaviourTestCompress() throws Exception { @Test public void correctBehaviourTestZip() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.ZIP, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.ZIP, 0, + 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP, + 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } @Test public void correctBehaviourTestZipAndCompress() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.ZIP_AND_COMPRESS, 0, 200)) { checkZipStream(stream, datafileIds, 36L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP_AND_COMPRESS, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 36L, 0); } @@ -151,25 +177,31 @@ public void correctBehaviourTestZipAndCompress() throws Exception { @Test public void correctBehaviourInvestigation() throws Exception { try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addInvestigation(investigationId), Flag.NONE, 0, 200)) { + new DataSelection().addInvestigation(investigationId), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP, + 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } @Test public void correctBehaviourInvestigations() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addInvestigations(Arrays.asList(investigationId)), Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient + .getData(sessionId, + new DataSelection().addInvestigations( + Arrays.asList(investigationId)), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.ZIP, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP, + 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java index 9d844536..997a11cf 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java @@ -1,8 +1,9 @@ package org.icatproject.ids.integration.one; +import static org.junit.Assert.assertEquals; + import java.io.InputStream; -import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; @@ -25,15 +26,17 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, + 400)) { } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); do { Thread.sleep(500); @@ -45,7 +48,8 @@ public void badOffsetFormatTest() throws Exception { @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + try (InputStream z = testingClient + .getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { } } @@ -55,13 +59,15 @@ public void correctBehaviourNoOffsetTest() throws Exception { for (Flag flag : Flag.values()) { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), flag, 200); + new DataSelection().addDatafile(datafileIds.get(0)), flag, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { + try (InputStream stream = testingClient.getData(preparedId, 0, + 200)) { if (flag == Flag.NONE || flag == Flag.COMPRESS) { checkStream(stream, datafileIds.get(0)); } else if (flag == Flag.ZIP) { @@ -74,9 +80,10 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() + throws Exception { + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -90,8 +97,9 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -102,10 +110,13 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() + throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -122,7 +133,8 @@ public void correctBehaviourWithOffsetTest() throws Exception { for (Flag flag : Flag.values()) { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), flag, 200); + new DataSelection().addDatafile(datafileIds.get(0)), flag, + 200); do { Thread.sleep(1000); @@ -130,13 +142,16 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the file twice, with and without an offset byte[] out = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] outOffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); + byte[] outOffset = getOutput( + testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the // offset - System.out.println(flag + ": " + out.length + " " + outOffset.length); + System.out + .println(flag + ": " + out.length + " " + outOffset.length); for (int i = 0; i < outOffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) outOffset[i], (byte) out[i + goodOffset]); + assertEquals("Byte offset: " + i, (byte) outOffset[i], + (byte) out[i + goodOffset]); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java index d5db81b7..f2a82f32 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java @@ -1,8 +1,9 @@ package org.icatproject.ids.integration.one; +import static org.junit.Assert.assertEquals; + import java.util.Arrays; -import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; @@ -40,17 +41,20 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(sessionId, new DataSelection() + .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTestAnon() throws Exception { - testingClient.getStatus(null, new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(null, new DataSelection() + .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.getStatus(setup.getForbiddenSessionId(), new DataSelection().addDatafiles(datafileIds), 403); + testingClient.getStatus(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), 403); } @Test @@ -59,7 +63,8 @@ public void correctBehaviourTest() throws Exception { Status status; do { Thread.sleep(1000); - status = testingClient.getStatus(sessionId, new DataSelection().addDatafiles(datafileIds), 200); + status = testingClient.getStatus(sessionId, + new DataSelection().addDatafiles(datafileIds), 200); System.out.println("*" + status + "*"); } while (status != Status.ONLINE); @@ -67,8 +72,10 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java index b870eba0..b0c72fb6 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java @@ -33,22 +33,20 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient - .prepareData(sessionId, - new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), - Flag.NONE, 404); + testingClient.prepareData(sessionId, new DataSelection().addDatafiles( + Arrays.asList(1L, 2L, 3L, 99999L)), Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, - 404); + testingClient.prepareData(sessionId, + new DataSelection().addDatafile(99999L), Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient - .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient.prepareData(sessionId, + new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) diff --git a/src/test/java/org/icatproject/ids/integration/one/MiscTest.java b/src/test/java/org/icatproject/ids/integration/one/MiscTest.java index 3baf012c..d5eaffe7 100644 --- a/src/test/java/org/icatproject/ids/integration/one/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/MiscTest.java @@ -1,9 +1,10 @@ package org.icatproject.ids.integration.one; -import java.io.InputStream; - import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; + +import java.io.InputStream; + import org.junit.BeforeClass; import org.junit.Test; @@ -41,17 +42,20 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); @@ -60,13 +64,15 @@ public void correctBehaviourNoOffsetTest() throws Exception { @Test(expected = NotImplementedException.class) public void restoreNotAvailableTest() throws Exception { - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 501); waitForIds(); } @Test(expected = NotImplementedException.class) public void archiveNotAvailableTest() throws Exception { - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.archive(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 501); waitForIds(); } diff --git a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java index 1943ae01..cf1a064b 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java @@ -29,7 +29,8 @@ public static void beforeClass() throws Exception { int n = 100000; start = System.currentTimeMillis(); - try (OutputStream f = Files.newOutputStream(infile, StandardOpenOption.CREATE)) { + try (OutputStream f = Files.newOutputStream(infile, + StandardOpenOption.CREATE)) { for (int i = 0; i < n; i++) { f.write(junk); } @@ -50,12 +51,13 @@ public static void afterClass() throws Exception { public void putOneFileTest() throws Exception { byte[] junk = new byte[1000]; start = System.currentTimeMillis(); - Long dfid = testingClient.put(sessionId, Files.newInputStream(infile), "big_" + timestamp, datasetIds.get(0), + Long dfid = testingClient.put(sessionId, Files.newInputStream(infile), + "big_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), "A big datafile", 201); ts("store file (put)"); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(dfid), Flag.NONE, 0, - null)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(dfid), Flag.NONE, 0, null)) { boolean first = true; while (stream.read(junk) > 0) { if (first) { @@ -77,14 +79,15 @@ private static void ts(String msg) { public void putAsPostOneFileTest() throws Exception { byte[] junk = new byte[1000]; start = System.currentTimeMillis(); - Long dfid = testingClient.putAsPost(sessionId, Files.newInputStream(infile), "big2_" + timestamp, - datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", null, null, null, - true, 201); + Long dfid = testingClient.putAsPost(sessionId, + Files.newInputStream(infile), "big2_" + timestamp, + datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", null, null, null, true, 201); ts("store file (post)"); int ntot = 0; - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(dfid), Flag.NONE, 0, - null)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(dfid), Flag.NONE, 0, null)) { boolean first = true; int n; diff --git a/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java index acdf7be9..6e56832a 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java @@ -1,11 +1,12 @@ package org.icatproject.ids.integration.one; -import java.nio.file.Files; -import java.util.List; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; + +import java.nio.file.Files; +import java.util.List; + import org.junit.BeforeClass; import org.junit.Test; @@ -26,27 +27,30 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + testingClient.prepareData("bad sessionId format", + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 400); } @Test(expected = BadRequestException.class) public void noIdsTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection(), Flag.NONE, 400); + testingClient.prepareData("bad sessionId format", new DataSelection(), + Flag.NONE, 400); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); System.out.println(preparedId); assertNotNull(preparedId); } @@ -54,8 +58,9 @@ public void correctBehaviourTest() throws Exception { @Test public void prepareDataset() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -66,14 +71,17 @@ public void prepareDataset() throws Exception { Thread.sleep(1000); } - assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue( + Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareTwoDatasets() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -85,14 +93,16 @@ public void prepareTwoDatasets() throws Exception { Thread.sleep(1000); } - assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue( + Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareDatafile() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -102,14 +112,17 @@ public void prepareDatafile() throws Exception { Thread.sleep(1000); } - assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue( + Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareDatafileAndItsDataset() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -120,7 +133,8 @@ public void prepareDatafileAndItsDataset() throws Exception { Thread.sleep(1000); } - assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue( + Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/PutTest.java b/src/test/java/org/icatproject/ids/integration/one/PutTest.java index 0a6dec0a..278383d8 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PutTest.java @@ -1,18 +1,20 @@ package org.icatproject.ids.integration.one; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + import java.nio.file.Files; import java.nio.file.Path; import java.util.Date; import java.util.GregorianCalendar; import javax.xml.datatype.DatatypeFactory; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -38,9 +40,11 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); Datafile df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("A rather splendid datafile", df.getDescription()); @@ -49,9 +53,11 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileModTime()); assertTrue(Files.exists(dirOnFastStorage)); - dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); + dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "An even better datafile", + "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -72,9 +78,11 @@ public void putAsPostOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - Long dfid = testingClient.putAsPost(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", null, null, null, true, 201); + Long dfid = testingClient.putAsPost(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "A rather splendid datafile", + null, null, null, true, 201); Datafile df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("A rather splendid datafile", df.getDescription()); @@ -83,9 +91,11 @@ public void putAsPostOneFileTest() throws Exception { assertNull(df.getDatafileModTime()); assertTrue(Files.exists(dirOnFastStorage)); - dfid = testingClient.putAsPost(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "An even better datafile", "7.1.3", new Date(420000), new Date(42000), false, 201); + dfid = testingClient.putAsPost(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "An even better datafile", + "7.1.3", new Date(420000), new Date(42000), false, 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); diff --git a/src/test/java/org/icatproject/ids/integration/one/WriteTest.java b/src/test/java/org/icatproject/ids/integration/one/WriteTest.java index 9a7550a3..58891e59 100644 --- a/src/test/java/org/icatproject/ids/integration/one/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/WriteTest.java @@ -18,7 +18,8 @@ public static void setup() throws Exception { @Test(expected = NotImplementedException.class) public void writeNotAvailableTest() throws Exception { - testingClient.write(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.write(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 501); } } diff --git a/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java b/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java index d5bf557e..56c39c26 100644 --- a/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java @@ -1,11 +1,12 @@ package org.icatproject.ids.integration.two; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -30,13 +31,15 @@ public void restoreThenArchiveDataset() throws Exception { assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); diff --git a/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java index 3ff9adf1..90dce2b6 100644 --- a/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java @@ -1,5 +1,9 @@ package org.icatproject.ids.integration.two; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.io.File; import java.io.InputStream; import java.nio.file.Files; @@ -8,15 +12,13 @@ import java.util.HashSet; import java.util.Set; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; import org.icatproject.Dataset; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; @@ -47,21 +49,24 @@ public static void setup() throws Exception { @Before public void createBogusFiles() throws Exception { - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -77,9 +82,11 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -88,7 +95,8 @@ public void getEmptyDataset() throws Exception { @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); } @@ -96,14 +104,17 @@ public void getSizeEmptyDataset() throws Exception { @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); testingClient.restore(sessionId, selection, 204); waitForIds(); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, null)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, selection, null)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } @@ -112,7 +123,8 @@ public void getNonEmptyDataset() throws Exception { @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); } @@ -120,14 +132,17 @@ public void getSizeNonEmptyDataset() throws Exception { @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.restore(sessionId, selection, 404); waitForIds(); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, 404)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, selection, 404)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -136,15 +151,17 @@ public void getBogusFile() throws Exception { @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); } /* - * Try the full cycle: upload a new file into a dataset having a bogus file, which triggers a write of the - * dataset to archive storage, archive the dataset, and restore it. Each step must deal gracefully with - * the bogus file in the dataset. + * Try the full cycle: upload a new file into a dataset having a bogus file, + * which triggers a write of the dataset to archive storage, archive the + * dataset, and restore it. Each step must deal gracefully with the bogus + * file in the dataset. */ @Test public void putWriteArchiveRestore() throws Exception { @@ -157,9 +174,11 @@ public void putWriteArchiveRestore() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfulId = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfulId = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, dsId, + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); Datafile dful = (Datafile) icatWS.get(sessionId, "Datafile", dfulId); testingClient.archive(sessionId, selection, 204); waitForIds(); @@ -172,13 +191,16 @@ public void putWriteArchiveRestore() throws Exception { File[] filesList = dirOnFastStorage.toFile().listFiles(); assertEquals(3, filesList.length); Set locations = new HashSet<>(); - Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(0)); + Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", + datafileIds.get(0)); locations.add(getLocationFromDigest(df1.getId(), df1.getLocation())); - Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(1)); + Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", + datafileIds.get(1)); locations.add(getLocationFromDigest(df2.getId(), df2.getLocation())); locations.add(getLocationFromDigest(dful.getId(), dful.getLocation())); for (File file : filesList) { - String location = setup.getStorageDir().relativize(file.toPath()).toString(); + String location = setup.getStorageDir().relativize(file.toPath()) + .toString(); assertTrue(locations.contains(location)); } diff --git a/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java index d8179493..da6b435f 100644 --- a/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java @@ -20,7 +20,8 @@ public static void setup() throws Exception { @Test(expected = DataNotOnlineException.class) public void deleteFromUnrestoredDataset() throws Exception { - testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 503); + testingClient.delete(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 503); } @Test @@ -29,11 +30,13 @@ public void deleteDatafileFromRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); + testingClient.delete(sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), 204); waitForIds(); checkZipFile(fileOnArchiveStorage, datafileIds.subList(2, 3), 36); @@ -44,12 +47,14 @@ public void deleteRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); checkPresent(fileOnArchiveStorage); - testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.delete(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkAbsent(dirOnFastStorage); checkAbsent(fileOnArchiveStorage); diff --git a/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java index 1a3970a0..30e012b3 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java @@ -1,13 +1,15 @@ package org.icatproject.ids.integration.two; -import java.io.InputStream; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; + +import java.io.InputStream; + import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -31,14 +33,17 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, + 400)) { } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { + try (InputStream z = testingClient.getData( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + 403)) { } } @@ -47,11 +52,13 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", + datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(209L, testingClient.getSize(sessionId, + new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -65,12 +72,15 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", + datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection().addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, + Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -83,16 +93,18 @@ public void getSizePreparedId() throws Exception { @Test public void correctBehaviourTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.NONE, - 0, 503)) { + try (InputStream z = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + 503)) { fail("Should have thrown exception"); } catch (IdsException e) { assertEquals(DataNotOnlineException.class, e.getClass()); } while (true) { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + null)) { checkZipStream(stream, datafileIds, 57, 0); break; } catch (IdsException e) { @@ -105,16 +117,18 @@ public void correctBehaviourTest() throws Exception { @Test public void gettingDatafileRestoresItsDatasetTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)), - Flag.NONE, 0, null)) { + try (InputStream z = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(2)), Flag.NONE, + 0, null)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), Flag.NONE, + 0, 200)) { checkStream(stream, datafileIds.get(3)); } @@ -123,8 +137,9 @@ public void gettingDatafileRestoresItsDatasetTest() throws Exception { @Test public void gettingDatasetUsesCacheTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, null)) { + try (InputStream z = testingClient.getData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, + null)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -132,26 +147,32 @@ public void gettingDatasetUsesCacheTest() throws Exception { waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, + 200)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } } @Test - public void gettingDatafileAndDatasetShouldRestoreBothDatasetsTest() throws Exception { - - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)) - .addDataset(datasetIds.get(0)), Flag.NONE, 0, 503)) { + public void gettingDatafileAndDatasetShouldRestoreBothDatasetsTest() + throws Exception { + + try (InputStream z = testingClient + .getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(2)) + .addDataset(datasetIds.get(0)), + Flag.NONE, 0, 503)) { fail("Should throw exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatasets(datasetIds), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatasets(datasetIds), Flag.NONE, 0, + 200)) { checkZipStream(stream, datafileIds, 57, 0); } } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java index 97f66d5a..8e5311fa 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java @@ -1,8 +1,9 @@ package org.icatproject.ids.integration.two; +import static org.junit.Assert.assertEquals; + import java.io.InputStream; -import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; @@ -25,15 +26,17 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, + 400)) { } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -45,14 +48,16 @@ public void badOffsetFormatTest() throws Exception { @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + try (InputStream z = testingClient + .getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { } } @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -64,9 +69,10 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() + throws Exception { + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -81,8 +87,9 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -94,10 +101,13 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() + throws Exception { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDatafiles(datafileIds), Flag.NONE, 200); + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -111,8 +121,9 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exceptio @Test public void correctBehaviourWithOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -120,12 +131,14 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the zip file twice, with and without an offset byte[] zip = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] zipoffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); + byte[] zipoffset = getOutput( + testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the offset System.out.println(zip.length + " " + zipoffset.length); for (int i = 0; i < zipoffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) zipoffset[i], (byte) zip[i + goodOffset]); + assertEquals("Byte offset: " + i, (byte) zipoffset[i], + (byte) zip[i + goodOffset]); } } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java index 8ecd4429..4207e6c5 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java @@ -1,8 +1,9 @@ package org.icatproject.ids.integration.two; +import static org.junit.Assert.assertEquals; + import java.util.Arrays; -import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; @@ -34,8 +35,8 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, - new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(sessionId, new DataSelection() + .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) @@ -55,8 +56,10 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } @@ -64,7 +67,8 @@ public void getStatusPreparedIdTest() throws Exception { testingClient.archive(sessionId, selection, 204); waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); - // verify that getStatus() as opposed to isPrepared() does not implicitly trigger a restore. + // verify that getStatus() as opposed to isPrepared() does not + // implicitly trigger a restore. waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); } @@ -72,7 +76,8 @@ public void getStatusPreparedIdTest() throws Exception { @Test public void restoringDatafileRestoresItsDatasetTest() throws Exception { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, 200); + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java index c7daebea..e6fa086c 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java @@ -33,22 +33,20 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient - .prepareData(sessionId, - new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), - Flag.NONE, 404); + testingClient.prepareData(sessionId, new DataSelection().addDatafiles( + Arrays.asList(1L, 2L, 3L, 99999L)), Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, - 404); + testingClient.prepareData(sessionId, + new DataSelection().addDatafile(99999L), Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient - .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient.prepareData(sessionId, + new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) diff --git a/src/test/java/org/icatproject/ids/integration/two/MiscTest.java b/src/test/java/org/icatproject/ids/integration/two/MiscTest.java index fa5751d8..e7707492 100644 --- a/src/test/java/org/icatproject/ids/integration/two/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/MiscTest.java @@ -1,9 +1,10 @@ package org.icatproject.ids.integration.two; -import java.io.InputStream; - import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; + +import java.io.InputStream; + import org.junit.BeforeClass; import org.junit.Test; @@ -40,19 +41,23 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); - assertFalse(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertFalse(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); diff --git a/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java index 891da989..b7cf5608 100644 --- a/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java @@ -1,11 +1,12 @@ package org.icatproject.ids.integration.two; -import java.nio.file.Path; -import java.util.List; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; + +import java.nio.file.Path; +import java.util.List; + import org.junit.BeforeClass; import org.junit.Test; @@ -28,8 +29,9 @@ public static void setup() throws Exception { public void prepareArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -49,8 +51,10 @@ public void prepareTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -70,8 +74,9 @@ public void prepareTwoArchivedDatasets() throws Exception { public void prepareArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -88,8 +93,11 @@ public void prepareArchivedDatafile() throws Exception { public void prepareArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient + .prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -105,26 +113,29 @@ public void prepareArchivedDatafileAndItsDataset() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + testingClient.prepareData("bad sessionId format", + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 400); } @Test public void noIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, 200); + testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, + 200); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); assertNotNull(preparedId); } @@ -132,14 +143,16 @@ public void correctBehaviourTest() throws Exception { public void prepareRestoredDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -155,16 +168,19 @@ public void prepareTwoRestoredDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), + 204); waitForIds(); checkPresent(dirOnFastStorage1); checkPresent(dirOnFastStorage2); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -179,14 +195,16 @@ public void prepareRestoredDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -201,13 +219,16 @@ public void prepareRestoredDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), + 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/two/PutTest.java b/src/test/java/org/icatproject/ids/integration/two/PutTest.java index 3ca3f8dd..23f44369 100644 --- a/src/test/java/org/icatproject/ids/integration/two/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/PutTest.java @@ -1,19 +1,21 @@ package org.icatproject.ids.integration.two; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + import java.nio.file.Files; import java.nio.file.Path; import java.util.Date; import java.util.GregorianCalendar; import javax.xml.datatype.DatatypeFactory; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataNotOnlineException; @@ -32,8 +34,9 @@ public static void setup() throws Exception { @Test(expected = DataNotOnlineException.class) public void putToUnrestoredDataset() throws Exception { - testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file1_" - + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), null, 503); + testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file1_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), null, 503); } @Test @@ -41,15 +44,18 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); waitForIds(); @@ -59,9 +65,11 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileCreateTime()); assertNull(df.getDatafileModTime()); - dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); + dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "An even better datafile", + "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -79,7 +87,8 @@ public void putOneFileTest() throws Exception { assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java b/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java index 5122232e..89a868fa 100644 --- a/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java @@ -5,6 +5,9 @@ * in archive storage having unexpected content. */ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -12,8 +15,6 @@ import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import org.junit.BeforeClass; import org.junit.Test; @@ -32,9 +33,9 @@ public static void setup() throws Exception { } /* - * Note that we cannot test for DUPLICATE_ENTRY here, because ZipOutputStream() won't allow - * us to create such a defective ZIP file. But that doesn't mean that this error cannot - * occur. + * Note that we cannot test for DUPLICATE_ENTRY here, because + * ZipOutputStream() won't allow us to create such a defective ZIP file. But + * that doesn't mean that this error cannot occur. */ private enum Defect { NONE, MISSING_ENTRY, SPURIOUS_ENTRY, DUPLICATE_ENTRY @@ -43,8 +44,10 @@ private enum Defect { private void cloneZip(Path archivepath, Defect defect) throws IOException { Path savepath = archivepath.getParent().resolve(".sav"); Files.move(archivepath, savepath); - try (ZipOutputStream zipout = new ZipOutputStream(Files.newOutputStream(archivepath))) { - try (ZipInputStream zipin = new ZipInputStream(Files.newInputStream(savepath))) { + try (ZipOutputStream zipout = new ZipOutputStream( + Files.newOutputStream(archivepath))) { + try (ZipInputStream zipin = new ZipInputStream( + Files.newInputStream(savepath))) { ZipEntry entry = zipin.getNextEntry(); boolean first = true; String entryName = ""; diff --git a/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java b/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java index 2755fc9e..0efa59fb 100644 --- a/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java @@ -22,7 +22,8 @@ public void restoreArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); @@ -34,8 +35,9 @@ public void restoreArchivedDataset() throws Exception { public void restoreTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), + 204); waitForIds(); checkPresent(dirOnFastStorage1); @@ -47,7 +49,8 @@ public void restoreArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); @@ -59,8 +62,9 @@ public void restoreArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), + 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/two/WriteTest.java b/src/test/java/org/icatproject/ids/integration/two/WriteTest.java index c9367a43..0da7c9f8 100644 --- a/src/test/java/org/icatproject/ids/integration/two/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/WriteTest.java @@ -12,6 +12,7 @@ import org.icatproject.Dataset; import org.icatproject.DatasetType; import org.icatproject.Investigation; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -28,8 +29,8 @@ public static void setup() throws Exception { } /** - * In principle, it's always possible to do a write call on - * existing datasets, but it will have no visible effect. + * In principle, it's always possible to do a write call on existing + * datasets, but it will have no visible effect. */ @Test public void restoreThenWriteDataset() throws Exception { @@ -47,18 +48,19 @@ public void restoreThenWriteDataset() throws Exception { } /** - * Create a dataset in ICAT, store the files in main storage, - * then do a write call to IDS to get the dataset written to - * archive storage. + * Create a dataset in ICAT, store the files in main storage, then do a + * write call to IDS to get the dataset written to archive storage. */ @Test public void storeThenWrite() throws Exception { long timestamp = System.currentTimeMillis(); - Investigation inv = (Investigation) icatWS.get(sessionId, "Investigation INCLUDE Facility", investigationId); + Investigation inv = (Investigation) icatWS.get(sessionId, + "Investigation INCLUDE Facility", investigationId); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS + .search(sessionId, "DatasetType").get(0); Dataset ds = new Dataset(); ds.setName("dsWrite_" + timestamp); @@ -72,7 +74,8 @@ public void storeThenWrite() throws Exception { df.setName("dfWrite_" + timestamp); df.setLocation(dsLoc + UUID.randomUUID()); df.setDataset(ds); - writeToFile(df, "some really boring datafile test content", setup.getKey()); + writeToFile(df, "some really boring datafile test content", + setup.getKey()); Long dsId = ds.getId(); Path dirOnFastStorage = getDirOnFastStorage(dsId); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java b/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java index 75557414..ab073d89 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java @@ -1,11 +1,12 @@ package org.icatproject.ids.integration.twodf; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -30,13 +31,15 @@ public void restoreThenArchiveDataset() throws Exception { assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java index 670a071a..17f56ce2 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java @@ -1,5 +1,9 @@ package org.icatproject.ids.integration.twodf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.io.File; import java.io.InputStream; import java.nio.file.Files; @@ -8,15 +12,13 @@ import java.util.HashSet; import java.util.Set; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; import org.icatproject.Dataset; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; @@ -47,21 +49,24 @@ public static void setup() throws Exception { @Before public void createBogusFiles() throws Exception { - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", + datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -77,9 +82,11 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -88,7 +95,8 @@ public void getEmptyDataset() throws Exception { @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); } @@ -96,14 +104,17 @@ public void getSizeEmptyDataset() throws Exception { @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); testingClient.restore(sessionId, selection, 204); waitForIds(); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, null)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, selection, null)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } @@ -112,7 +123,8 @@ public void getNonEmptyDataset() throws Exception { @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection() + .addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); } @@ -120,14 +132,17 @@ public void getSizeNonEmptyDataset() throws Exception { @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.restore(sessionId, selection, 404); waitForIds(); - assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, 404)); + assertEquals(Status.ONLINE, + testingClient.getStatus(sessionId, selection, 404)); - try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { + try (InputStream stream = testingClient.getData(sessionId, selection, + Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -136,15 +151,17 @@ public void getBogusFile() throws Exception { @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); } /* - * Try the full cycle: upload a new file into a dataset having a bogus file, which triggers a write of the - * datafile to archive storage, archive the dataset, and restore it. Each step must deal gracefully with - * the bogus file in the dataset. + * Try the full cycle: upload a new file into a dataset having a bogus file, + * which triggers a write of the datafile to archive storage, archive the + * dataset, and restore it. Each step must deal gracefully with the bogus + * file in the dataset. */ @Test public void putWriteArchiveRestore() throws Exception { @@ -157,9 +174,11 @@ public void putWriteArchiveRestore() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfulId = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfulId = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, dsId, + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); Datafile dful = (Datafile) icatWS.get(sessionId, "Datafile", dfulId); testingClient.archive(sessionId, selection, 204); waitForIds(); @@ -172,13 +191,16 @@ public void putWriteArchiveRestore() throws Exception { File[] filesList = dirOnFastStorage.toFile().listFiles(); assertEquals(3, filesList.length); Set locations = new HashSet<>(); - Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(0)); + Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", + datafileIds.get(0)); locations.add(getLocationFromDigest(df1.getId(), df1.getLocation())); - Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(1)); + Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", + datafileIds.get(1)); locations.add(getLocationFromDigest(df2.getId(), df2.getLocation())); locations.add(getLocationFromDigest(dful.getId(), dful.getLocation())); for (File file : filesList) { - String location = setup.getStorageDir().relativize(file.toPath()).toString(); + String location = setup.getStorageDir().relativize(file.toPath()) + .toString(); assertTrue(locations.contains(location)); } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java index 063102fb..e92431e7 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java @@ -19,7 +19,8 @@ public static void setup() throws Exception { @Test public void deleteFromUnrestoredDataset() throws Exception { - testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.delete(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); } @Test @@ -27,11 +28,13 @@ public void deleteDatafileFromRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); - testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); + testingClient.delete(sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), 204); waitForIds(); } @@ -40,12 +43,14 @@ public void deleteRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); checkPresent(fileOnArchiveStorage); - testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.delete(sessionId, + new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkAbsent(dirOnFastStorage); checkAbsent(fileOnArchiveStorage); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java index b6d9e772..e5734be0 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java @@ -1,13 +1,15 @@ package org.icatproject.ids.integration.twodf; -import java.io.InputStream; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; + +import java.io.InputStream; + import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -31,14 +33,17 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, + 400)) { } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { + try (InputStream z = testingClient.getData( + setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + 403)) { } } @@ -47,11 +52,13 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", + datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(209L, testingClient.getSize(sessionId, + new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -65,12 +72,15 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", + datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection().addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, + Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -83,8 +93,9 @@ public void getSizePreparedId() throws Exception { @Test public void correctBehaviourTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.NONE, - 0, 503)) { + try (InputStream z = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + 503)) { fail("Should have thrown exception"); } catch (IdsException e) { @@ -92,8 +103,9 @@ public void correctBehaviourTest() throws Exception { } while (true) { - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, + null)) { checkZipStream(stream, datafileIds, 57, 0); break; } catch (IdsException e) { @@ -106,16 +118,18 @@ public void correctBehaviourTest() throws Exception { @Test public void gettingDatafileDoesNotRestoreItsDatasetTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)), - Flag.NONE, 0, 503)) { + try (InputStream z = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(2)), Flag.NONE, + 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), - Flag.NONE, 0, 503)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(3)), Flag.NONE, + 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -126,8 +140,9 @@ public void gettingDatafileDoesNotRestoreItsDatasetTest() throws Exception { @Test public void gettingDatasetUsesCacheTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, 503)) { + try (InputStream z = testingClient.getData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, + 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -135,26 +150,32 @@ public void gettingDatasetUsesCacheTest() throws Exception { waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, + 200)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } } @Test - public void gettingDatafileAndDatasetShouldNotRestoreBothDatasetsTest() throws Exception { - - try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)) - .addDataset(datasetIds.get(0)), Flag.NONE, 0, 503)) { + public void gettingDatafileAndDatasetShouldNotRestoreBothDatasetsTest() + throws Exception { + + try (InputStream z = testingClient + .getData(sessionId, + new DataSelection().addDatafile(datafileIds.get(2)) + .addDataset(datasetIds.get(0)), + Flag.NONE, 0, 503)) { fail("Should throw exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatasets(datasetIds), - Flag.NONE, 0, 503)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addDatasets(datasetIds), Flag.NONE, 0, + 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java index c0fa6eef..14cc376c 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java @@ -1,8 +1,9 @@ package org.icatproject.ids.integration.twodf; +import static org.junit.Assert.assertEquals; + import java.io.InputStream; -import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; @@ -25,15 +26,17 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, + 400)) { } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -45,14 +48,16 @@ public void badOffsetFormatTest() throws Exception { @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + try (InputStream z = testingClient + .getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { } } @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -64,9 +69,10 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() + throws Exception { + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -81,8 +87,9 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -94,10 +101,13 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() + throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -111,8 +121,9 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exceptio @Test public void correctBehaviourWithOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -120,12 +131,14 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the zip file twice, with and without an offset byte[] zip = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] zipoffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); + byte[] zipoffset = getOutput( + testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the offset System.out.println(zip.length + " " + zipoffset.length); for (int i = 0; i < zipoffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) zipoffset[i], (byte) zip[i + goodOffset]); + assertEquals("Byte offset: " + i, (byte) zipoffset[i], + (byte) zip[i + goodOffset]); } } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java index ed6bed60..d8893903 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java @@ -1,8 +1,9 @@ package org.icatproject.ids.integration.twodf; +import static org.junit.Assert.assertEquals; + import java.util.Arrays; -import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; @@ -34,8 +35,8 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, - new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(sessionId, new DataSelection() + .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) @@ -55,8 +56,10 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); + DataSelection selection = new DataSelection() + .addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } @@ -64,15 +67,18 @@ public void getStatusPreparedIdTest() throws Exception { testingClient.archive(sessionId, selection, 204); waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); - // verify that getStatus() as opposed to isPrepared() does not implicitly trigger a restore. + // verify that getStatus() as opposed to isPrepared() does not + // implicitly trigger a restore. waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); } @Test - public void restoringDatafileDoesNotRestoresItsDatasetTest() throws Exception { + public void restoringDatafileDoesNotRestoresItsDatasetTest() + throws Exception { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, 200); + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java index 7889492e..6f7c04ae 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java @@ -33,22 +33,20 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient - .prepareData(sessionId, - new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), - Flag.NONE, 404); + testingClient.prepareData(sessionId, new DataSelection().addDatafiles( + Arrays.asList(1L, 2L, 3L, 99999L)), Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, - 404); + testingClient.prepareData(sessionId, + new DataSelection().addDatafile(99999L), Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient - .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient.prepareData(sessionId, + new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) diff --git a/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java b/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java index 011de0ad..de551964 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java @@ -1,9 +1,10 @@ package org.icatproject.ids.integration.twodf; -import java.io.InputStream; - import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; + +import java.io.InputStream; + import org.junit.BeforeClass; import org.junit.Test; @@ -40,11 +41,13 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); var status = testingClient.getServiceStatus(sessionId, 200); - //System.out.println("### twodf.MiscTest.correctBehaviourNoOffsetTest - status: " + status.toString()); + // System.out.println("### twodf.MiscTest.correctBehaviourNoOffsetTest - + // status: " + status.toString()); assertFalse(status.getOpItems().isEmpty()); @@ -52,10 +55,12 @@ public void correctBehaviourNoOffsetTest() throws Exception { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() + .isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java index 386a7fff..b339647d 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java @@ -1,11 +1,12 @@ package org.icatproject.ids.integration.twodf; -import java.nio.file.Path; -import java.util.List; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; + +import java.nio.file.Path; +import java.util.List; + import org.junit.BeforeClass; import org.junit.Test; @@ -28,8 +29,9 @@ public static void setup() throws Exception { public void prepareArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -49,8 +51,10 @@ public void prepareTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -70,8 +74,9 @@ public void prepareTwoArchivedDatasets() throws Exception { public void prepareArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -88,8 +93,11 @@ public void prepareArchivedDatafile() throws Exception { public void prepareArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient + .prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -105,26 +113,29 @@ public void prepareArchivedDatafileAndItsDataset() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + testingClient.prepareData("bad sessionId format", + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 400); } @Test public void noIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, 200); + testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, + 200); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); assertNotNull(preparedId); } @@ -132,14 +143,16 @@ public void correctBehaviourTest() throws Exception { public void prepareRestoredDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -155,16 +168,19 @@ public void prepareTwoRestoredDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), + 204); waitForIds(); checkPresent(dirOnFastStorage1); checkPresent(dirOnFastStorage2); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -179,14 +195,16 @@ public void prepareRestoredDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, + 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -201,13 +219,16 @@ public void prepareRestoredDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), + 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java b/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java index b960cd6e..4854589f 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java @@ -1,19 +1,21 @@ package org.icatproject.ids.integration.twodf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + import java.nio.file.Files; import java.nio.file.Path; import java.util.Date; import java.util.GregorianCalendar; import javax.xml.datatype.DatatypeFactory; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; @@ -31,8 +33,9 @@ public static void setup() throws Exception { @Test // Works fine for datafile storage public void putToUnrestoredDataset() throws Exception { - testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file1_" - + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), null, 201); + testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file1_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), null, 201); } @Test @@ -40,15 +43,18 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + Long dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); waitForIds(); @@ -58,9 +64,11 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileCreateTime()); assertNull(df.getDatafileModTime()); - dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); + dfid = testingClient.put(sessionId, + Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), + supportedDatafileFormat.getId(), "An even better datafile", + "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -78,7 +86,8 @@ public void putOneFileTest() throws Exception { assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java b/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java index baf7be8d..1e6b34e1 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java @@ -1,9 +1,10 @@ package org.icatproject.ids.integration.twodf; +import static org.junit.Assert.assertTrue; + import java.nio.file.Files; import java.nio.file.Path; -import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -22,9 +23,8 @@ public static void setup() throws Exception { } /* - * Arrange for multiple different operations, requiring conflicting - * locks on the same dataset to be processed at the same time. This - * triggers Bug #82. + * Arrange for multiple different operations, requiring conflicting locks on + * the same dataset to be processed at the same time. This triggers Bug #82. */ @Test public void multiOperationTest() throws Exception { @@ -37,8 +37,9 @@ public void multiOperationTest() throws Exception { assertTrue(Files.exists(dirOnFastStorage)); testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + "uploaded_file_" + timestamp, dsId, + supportedDatafileFormat.getId(), "A rather splendid datafile", + 201); testingClient.archive(sessionId, selection, 204); waitForIds(); } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java b/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java index 7125b9a4..4f792af5 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java @@ -20,7 +20,8 @@ public static void setup() throws Exception { @Test public void restoreArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); } @@ -29,8 +30,9 @@ public void restoreArchivedDataset() throws Exception { public void restoreTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), + 204); waitForIds(); checkPresent(dirOnFastStorage1); @@ -42,7 +44,8 @@ public void restoreArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); @@ -54,8 +57,9 @@ public void restoreArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection() + .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), + 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java b/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java index 876af417..9b66996d 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java @@ -11,6 +11,7 @@ import org.icatproject.Dataset; import org.icatproject.DatasetType; import org.icatproject.Investigation; + import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -27,8 +28,8 @@ public static void setup() throws Exception { } /** - * In principle, it's always possible to do a write call on - * existing datasets, but it will have no visible effect. + * In principle, it's always possible to do a write call on existing + * datasets, but it will have no visible effect. */ @Test public void restoreThenWriteDataset() throws Exception { @@ -46,18 +47,19 @@ public void restoreThenWriteDataset() throws Exception { } /** - * Create a dataset in ICAT, store the files in main storage, - * then do a write call to IDS to get the dataset written to - * archive storage. + * Create a dataset in ICAT, store the files in main storage, then do a + * write call to IDS to get the dataset written to archive storage. */ @Test public void storeThenWrite() throws Exception { long timestamp = System.currentTimeMillis(); - Investigation inv = (Investigation) icatWS.get(sessionId, "Investigation INCLUDE Facility", investigationId); + Investigation inv = (Investigation) icatWS.get(sessionId, + "Investigation INCLUDE Facility", investigationId); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS + .search(sessionId, "DatasetType").get(0); Dataset ds = new Dataset(); ds.setName("dsWrite_" + timestamp); @@ -71,7 +73,8 @@ public void storeThenWrite() throws Exception { df.setName("dfWrite_" + timestamp); df.setLocation(dsLoc + UUID.randomUUID()); df.setDataset(ds); - writeToFile(df, "some really boring datafile test content", setup.getKey()); + writeToFile(df, "some really boring datafile test content", + setup.getKey()); Long dsId = ds.getId(); Path dirOnFastStorage = getDirOnFastStorage(dsId); diff --git a/src/test/java/org/icatproject/ids/integration/util/Setup.java b/src/test/java/org/icatproject/ids/integration/util/Setup.java index 2392805d..8273355b 100644 --- a/src/test/java/org/icatproject/ids/integration/util/Setup.java +++ b/src/test/java/org/icatproject/ids/integration/util/Setup.java @@ -10,11 +10,12 @@ import java.util.Properties; import org.icatproject.ICAT; -import org.icatproject.ids.TestUtils; -import org.icatproject.ids.services.ICATGetter; import org.icatproject.utils.CheckedProperties; import org.icatproject.utils.ShellCommand; +import org.icatproject.ids.TestUtils; +import org.icatproject.ids.services.ICATGetter; + /* * Setup the test environment for the IDS. This is done by reading property * values from the test.properties file and calling this class before @@ -57,11 +58,13 @@ public Setup(String runPropertyFile) throws Exception { // Start by reading the test properties Properties testProps = new Properties(); - InputStream is = Setup.class.getClassLoader().getResourceAsStream("test.properties"); + InputStream is = Setup.class.getClassLoader() + .getResourceAsStream("test.properties"); try { testProps.load(is); } catch (Exception e) { - System.err.println("Problem loading test.properties: " + e.getClass() + " " + e.getMessage()); + System.err.println("Problem loading test.properties: " + + e.getClass() + " " + e.getMessage()); } setReliability(1.); @@ -72,16 +75,18 @@ public Setup(String runPropertyFile) throws Exception { String containerHome = System.getProperty("containerHome"); if (containerHome == null) { - System.err.println("containerHome is not defined as a system property"); + System.err.println( + "containerHome is not defined as a system property"); } long time = System.currentTimeMillis(); - ShellCommand sc = new ShellCommand("src/test/scripts/prepare_test.py", "src/test/resources/" + runPropertyFile, - home.toString(), containerHome, serverUrl); + ShellCommand sc = new ShellCommand("src/test/scripts/prepare_test.py", + "src/test/resources/" + runPropertyFile, home.toString(), + containerHome, serverUrl); System.out.println(sc.getStdout() + " " + sc.getStderr()); - System.out.println( - "Setting up " + runPropertyFile + " took " + (System.currentTimeMillis() - time) / 1000. + "seconds"); + System.out.println("Setting up " + runPropertyFile + " took " + + (System.currentTimeMillis() - time) / 1000. + "seconds"); // Having set up the ids.properties file read it find other things CheckedProperties runProperties = new CheckedProperties(); @@ -92,8 +97,10 @@ public Setup(String runPropertyFile) throws Exception { updownDir = home.resolve(testProps.getProperty("updownDir")); icatUrl = runProperties.getURL("icat.url"); ICAT icat = ICATGetter.getService(icatUrl.toString()); - rootSessionId = TestUtils.login(icat, testProps.getProperty("login.root")); - forbiddenSessionId = TestUtils.login(icat, testProps.getProperty("login.unauthorized")); + rootSessionId = TestUtils.login(icat, + testProps.getProperty("login.root")); + forbiddenSessionId = TestUtils.login(icat, + testProps.getProperty("login.unauthorized")); storageDir = runProperties.getPath("plugin.main.dir"); diff --git a/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java b/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java index 6527c191..577d29f5 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java @@ -17,7 +17,8 @@ public class HttpInputStream extends FilterInputStream { * @param httpclient the CloseableHttpClient to close * @param response the response to use and ultimately close */ - public HttpInputStream(CloseableHttpClient httpclient, CloseableHttpResponse response) + public HttpInputStream(CloseableHttpClient httpclient, + CloseableHttpResponse response) throws IllegalStateException, IOException { super(response.getEntity().getContent()); this.response = response; diff --git a/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java b/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java index 7bb22543..89f4d391 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java @@ -1,5 +1,7 @@ package org.icatproject.ids.integration.util.client; +import static org.junit.Assert.fail; + import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; @@ -46,9 +48,8 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; -import org.icatproject.ids.enums.RequestIdNames; -import static org.junit.Assert.fail; +import org.icatproject.ids.enums.RequestIdNames; public class TestingClient { @@ -107,11 +108,14 @@ void storeFailure(Long id) { public String toString() { String result = "lockCount: " + this.lockCount + ", "; result += "lockedIDs: " + lockedDs.size() + "\n"; - for(long lockedId : lockedDs) result += "\t" + lockedId + "\n"; + for (long lockedId : lockedDs) + result += "\t" + lockedId + "\n"; result += "opItems: " + opItems.size() + "\n"; - for(String key : opItems.keySet()) result += "\t" + key + ": " + opItems.get(key) + "\n"; + for (String key : opItems.keySet()) + result += "\t" + key + ": " + opItems.get(key) + "\n"; result += "failures: " + failures.size() + "\n"; - for(long fail : failures) result += " " + fail; + for (long fail : failures) + result += " " + fail; result += "\n"; return result; @@ -129,7 +133,8 @@ public enum Status { public TestingClient(URL idsUrl) { try { - idsUri = new URI(idsUrl.getProtocol(), null, idsUrl.getHost(), idsUrl.getPort(), null, null, null); + idsUri = new URI(idsUrl.getProtocol(), null, idsUrl.getHost(), + idsUrl.getPort(), null, null, null); basePath = idsUrl.getPath(); } catch (URISyntaxException e) { throw new RuntimeException(e); @@ -137,22 +142,28 @@ public TestingClient(URL idsUrl) { } - public void archive(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, - BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { + public void archive(String sessionId, DataSelection data, Integer sc) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, + NotFoundException { URI uri = getUri(getUriBuilder("archive")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(formparams)); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try (CloseableHttpResponse response = httpclient + .execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -160,8 +171,10 @@ public void archive(String sessionId, DataSelection data, Integer sc) throws Not } private void checkStatus(HttpResponse response, Integer sc) - throws InternalException, BadRequestException, DataNotOnlineException, ParseException, IOException, - InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException { + throws InternalException, BadRequestException, + DataNotOnlineException, ParseException, IOException, + InsufficientPrivilegesException, NotImplementedException, + InsufficientStorageException, NotFoundException { StatusLine status = response.getStatusLine(); if (status == null) { throw new InternalException("Status line returned is empty"); @@ -188,7 +201,8 @@ private void checkStatus(HttpResponse response, Integer sc) } String code; String message; - try (JsonReader jsonReader = Json.createReader(new StringReader(error))) { + try (JsonReader jsonReader = Json + .createReader(new StringReader(error))) { JsonObject json = jsonReader.readObject(); code = json.getString("code"); message = json.getString("message"); @@ -227,7 +241,8 @@ private void checkStatus(HttpResponse response, Integer sc) } public void delete(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("delete"); @@ -239,47 +254,56 @@ public void delete(String sessionId, DataSelection data, Integer sc) try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpDelete httpDelete = new HttpDelete(uri); - try (CloseableHttpResponse response = httpclient.execute(httpDelete)) { + try (CloseableHttpResponse response = httpclient + .execute(httpDelete)) { expectNothing(response, sc); } catch (InsufficientStorageException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - private void expectNothing(CloseableHttpResponse response, Integer sc) throws InternalException, - BadRequestException, DataNotOnlineException, ParseException, InsufficientPrivilegesException, - NotImplementedException, InsufficientStorageException, NotFoundException, IOException { + private void expectNothing(CloseableHttpResponse response, Integer sc) + throws InternalException, BadRequestException, + DataNotOnlineException, ParseException, + InsufficientPrivilegesException, NotImplementedException, + InsufficientStorageException, NotFoundException, IOException { checkStatus(response, sc); HttpEntity entity = response.getEntity(); if (entity != null) { if (!EntityUtils.toString(entity).isEmpty()) { - throw new InternalException("No http entity expected in response"); + throw new InternalException( + "No http entity expected in response"); } } } - public URL getIcatUrl(int sc) throws InternalException, ParseException, NotImplementedException { + public URL getIcatUrl(int sc) + throws InternalException, ParseException, NotImplementedException { URI uri = getUri(getUriBuilder("getIcatUrl")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return new URL(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException + | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public InputStream getData(String sessionId, DataSelection data, Flag flags, long offset, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, - InternalException, DataNotOnlineException { + public InputStream getData(String sessionId, DataSelection data, Flag flags, + long offset, Integer sc) throws NotImplementedException, + BadRequestException, InsufficientPrivilegesException, + NotFoundException, InternalException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("getData"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -330,7 +354,8 @@ public InputStream getData(String sessionId, DataSelection data, Flag flags, lon } public InputStream getData(String preparedId, long offset, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("getData"); @@ -372,7 +397,8 @@ public InputStream getData(String preparedId, long offset, Integer sc) } public ServiceStatus getServiceStatus(String sessionId, Integer sc) - throws InternalException, InsufficientPrivilegesException, NotImplementedException { + throws InternalException, InsufficientPrivilegesException, + NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getServiceStatus"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -384,7 +410,8 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); String result = getString(response, sc); - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json + .createReader(new StringReader(result))) { ServiceStatus serviceStatus = new ServiceStatus(); JsonObject rootNode = jsonReader.readObject(); for (JsonValue on : rootNode.getJsonArray("opsQueue")) { @@ -394,7 +421,8 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) } serviceStatus.setLockedCount(rootNode.getInt("lockCount")); for (JsonValue lock : rootNode.getJsonArray("locks")) { - Long dsId = ((JsonObject) lock).getJsonNumber("id").longValueExact(); + Long dsId = ((JsonObject) lock).getJsonNumber("id") + .longValueExact(); serviceStatus.storeLockedDs(dsId); } for (JsonValue num : rootNode.getJsonArray("failures")) { @@ -403,21 +431,25 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) } return serviceStatus; } catch (JsonException e) { - throw new InternalException( - "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); + throw new InternalException("TestingClient " + e.getClass() + + " " + e.getMessage() + " from " + result); } - } catch (InsufficientStorageException | DataNotOnlineException | InternalException | BadRequestException - | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException + | InternalException | BadRequestException + | NotFoundException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public long getSize(String sessionId, DataSelection data, int sc) throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, NotImplementedException { + public long getSize(String sessionId, DataSelection data, int sc) + throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, + NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getSize"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -431,16 +463,19 @@ public long getSize(String sessionId, DataSelection data, int sc) throws BadRequ try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Long.parseLong(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException + | DataNotOnlineException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public long getSize(String preparedId, int sc) throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, NotImplementedException { + public long getSize(String preparedId, int sc) throws BadRequestException, + NotFoundException, InsufficientPrivilegesException, + InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getSize"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); @@ -450,16 +485,20 @@ public long getSize(String preparedId, int sc) throws BadRequestException, NotFo HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { return Long.parseLong(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException + | DataNotOnlineException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public Status getStatus(String sessionId, DataSelection data, Integer sc) throws BadRequestException, - NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { + public Status getStatus(String sessionId, DataSelection data, Integer sc) + throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, + NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getStatus"); if (sessionId != null) { @@ -477,7 +516,8 @@ public Status getStatus(String sessionId, DataSelection data, Integer sc) throws checkResponseConformity(response); return Status.valueOf(getString(response, sc)); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -485,8 +525,10 @@ public Status getStatus(String sessionId, DataSelection data, Integer sc) throws } - public Status getStatus(String preparedId, Integer sc) throws BadRequestException, - NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { + public Status getStatus(String preparedId, Integer sc) + throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, + NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getStatus"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); @@ -498,7 +540,8 @@ public Status getStatus(String preparedId, Integer sc) throws BadRequestExceptio try (CloseableHttpResponse response = httpclient.execute(httpGet)) { return Status.valueOf(getString(response, sc)); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -506,8 +549,10 @@ public Status getStatus(String preparedId, Integer sc) throws BadRequestExceptio } - private String getString(CloseableHttpResponse response, Integer sc) throws InternalException, BadRequestException, - DataNotOnlineException, ParseException, InsufficientPrivilegesException, NotImplementedException, + private String getString(CloseableHttpResponse response, Integer sc) + throws InternalException, BadRequestException, + DataNotOnlineException, ParseException, + InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException, IOException { checkStatus(response, sc); HttpEntity entity = response.getEntity(); @@ -530,7 +575,8 @@ private URIBuilder getUriBuilder(String path) { } public boolean isPrepared(String preparedId, Integer sc) - throws BadRequestException, NotFoundException, InternalException, NotImplementedException { + throws BadRequestException, NotFoundException, InternalException, + NotImplementedException { URIBuilder uriBuilder = getUriBuilder("isPrepared"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); @@ -542,47 +588,56 @@ public boolean isPrepared(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException + | InsufficientPrivilegesException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public boolean isReadOnly(int sc) throws InternalException, NotImplementedException { + public boolean isReadOnly(int sc) + throws InternalException, NotImplementedException { URI uri = getUri(getUriBuilder("isReadOnly")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException + | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public boolean isTwoLevel(int sc) throws InternalException, NotImplementedException { + public boolean isTwoLevel(int sc) + throws InternalException, NotImplementedException { URI uri = getUri(getUriBuilder("isTwoLevel")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException + | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void ping(Integer sc) throws InternalException, NotFoundException, NotImplementedException { + public void ping(Integer sc) throws InternalException, NotFoundException, + NotImplementedException { URI uri = getUri(getUriBuilder("ping")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { @@ -591,26 +646,32 @@ public void ping(Integer sc) throws InternalException, NotFoundException, NotImp checkResponseConformity(response); String result = getString(response, sc); if (!result.equals("IdsOK")) { - throw new NotFoundException("Server gave invalid response: " + result); + throw new NotFoundException( + "Server gave invalid response: " + result); } - } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException + | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public String prepareData(String sessionId, DataSelection data, Flag flags, Integer sc) - throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, + public String prepareData(String sessionId, DataSelection data, Flag flags, + Integer sc) throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, InternalException { URI uri = getUri(getUriBuilder("prepareData")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue())); } if (flags == Flag.ZIP || flags == Flag.ZIP_AND_COMPRESS) { formparams.add(new BasicNameValuePair("zip", "true")); @@ -622,11 +683,13 @@ public String prepareData(String sessionId, DataSelection data, Flag flags, Inte HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try (CloseableHttpResponse response = httpclient + .execute(httpPost)) { checkResponseConformity(response); return getString(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -634,17 +697,23 @@ public String prepareData(String sessionId, DataSelection data, Flag flags, Inte } - public Long put(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, - String description, Integer sc) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, - NotImplementedException, DataNotOnlineException, InsufficientStorageException { - return put(sessionId, inputStream, name, datasetId, datafileFormatId, description, null, null, null, sc); + public Long put(String sessionId, InputStream inputStream, String name, + long datasetId, long datafileFormatId, String description, + Integer sc) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, NotImplementedException, + DataNotOnlineException, InsufficientStorageException { + return put(sessionId, inputStream, name, datasetId, datafileFormatId, + description, null, null, null, sc); } - public Long put(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, - String description, String doi, Date datafileCreateTime, Date datafileModTime, Integer sc) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, - NotImplementedException, DataNotOnlineException, InsufficientStorageException { + public Long put(String sessionId, InputStream inputStream, String name, + long datasetId, long datafileFormatId, String description, + String doi, Date datafileCreateTime, Date datafileModTime, + Integer sc) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, NotImplementedException, + DataNotOnlineException, InsufficientStorageException { if (inputStream == null) { throw new BadRequestException("Input stream is null"); @@ -652,8 +721,10 @@ public Long put(String sessionId, InputStream inputStream, String name, long dat CRC32 crc = new CRC32(); inputStream = new CheckedInputStream(inputStream, crc); URIBuilder uriBuilder = getUriBuilder("put"); - uriBuilder.setParameter(RequestIdNames.sessionId, sessionId).setParameter("name", name) - .setParameter("datafileFormatId", Long.toString(datafileFormatId)) + uriBuilder.setParameter(RequestIdNames.sessionId, sessionId) + .setParameter("name", name) + .setParameter("datafileFormatId", + Long.toString(datafileFormatId)) .setParameter("datasetId", Long.toString(datasetId)); if (description != null) { uriBuilder.setParameter("description", description); @@ -662,39 +733,49 @@ public Long put(String sessionId, InputStream inputStream, String name, long dat uriBuilder.setParameter("doi", doi); } if (datafileCreateTime != null) { - uriBuilder.setParameter("datafileCreateTime", Long.toString(datafileCreateTime.getTime())); + uriBuilder.setParameter("datafileCreateTime", + Long.toString(datafileCreateTime.getTime())); } if (datafileModTime != null) { - uriBuilder.setParameter("datafileModTime", Long.toString(datafileModTime.getTime())); + uriBuilder.setParameter("datafileModTime", + Long.toString(datafileModTime.getTime())); } URI uri = getUri(uriBuilder); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPut httpPut = new HttpPut(uri); - httpPut.setEntity(new InputStreamEntity(inputStream, ContentType.APPLICATION_OCTET_STREAM)); + httpPut.setEntity(new InputStreamEntity(inputStream, + ContentType.APPLICATION_OCTET_STREAM)); try (CloseableHttpResponse response = httpclient.execute(httpPut)) { String result = getString(response, sc); checkResponseConformity(response); - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json + .createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); - if (rootNode.getJsonNumber("checksum").longValueExact() != crc.getValue()) { - throw new InternalException("Error uploading - the checksum was not as expected"); + if (rootNode.getJsonNumber("checksum").longValueExact() != crc + .getValue()) { + throw new InternalException( + "Error uploading - the checksum was not as expected"); } return rootNode.getJsonNumber("id").longValueExact(); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (NumberFormatException e) { - throw new InternalException("Web service call did not return a valid Long value"); + throw new InternalException( + "Web service call did not return a valid Long value"); } } - public Long putAsPost(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, - String description, String doi, Date datafileCreateTime, Date datafileModTime, boolean wrap, Integer sc) - throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, - NotImplementedException, DataNotOnlineException, InsufficientStorageException { + public Long putAsPost(String sessionId, InputStream inputStream, + String name, long datasetId, long datafileFormatId, + String description, String doi, Date datafileCreateTime, + Date datafileModTime, boolean wrap, Integer sc) + throws BadRequestException, NotFoundException, InternalException, + InsufficientPrivilegesException, NotImplementedException, + DataNotOnlineException, InsufficientStorageException { if (inputStream == null) { throw new BadRequestException("Input stream is null"); @@ -703,29 +784,40 @@ public Long putAsPost(String sessionId, InputStream inputStream, String name, lo inputStream = new CheckedInputStream(inputStream, crc); URI uri = getUri(getUriBuilder("put")); - MultipartEntityBuilder reqEntityBuilder = MultipartEntityBuilder.create() - .addPart(RequestIdNames.sessionId, new StringBody(sessionId, ContentType.TEXT_PLAIN)) - .addPart("datafileFormatId", new StringBody(Long.toString(datafileFormatId), ContentType.TEXT_PLAIN)) + MultipartEntityBuilder reqEntityBuilder = MultipartEntityBuilder + .create() + .addPart(RequestIdNames.sessionId, + new StringBody(sessionId, ContentType.TEXT_PLAIN)) + .addPart("datafileFormatId", + new StringBody(Long.toString(datafileFormatId), + ContentType.TEXT_PLAIN)) .addPart("name", new StringBody(name, ContentType.TEXT_PLAIN)) - .addPart("datasetId", new StringBody(Long.toString(datasetId), ContentType.TEXT_PLAIN)); + .addPart("datasetId", new StringBody(Long.toString(datasetId), + ContentType.TEXT_PLAIN)); if (description != null) { - reqEntityBuilder.addPart("description", new StringBody(description, ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart("description", + new StringBody(description, ContentType.TEXT_PLAIN)); } if (doi != null) { - reqEntityBuilder.addPart("doi", new StringBody(doi, ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart("doi", + new StringBody(doi, ContentType.TEXT_PLAIN)); } if (datafileCreateTime != null) { reqEntityBuilder.addPart("datafileCreateTime", - new StringBody(Long.toString(datafileCreateTime.getTime()), ContentType.TEXT_PLAIN)); + new StringBody(Long.toString(datafileCreateTime.getTime()), + ContentType.TEXT_PLAIN)); } if (datafileModTime != null) { reqEntityBuilder.addPart("datafileModTime", - new StringBody(Long.toString(datafileModTime.getTime()), ContentType.TEXT_PLAIN)); + new StringBody(Long.toString(datafileModTime.getTime()), + ContentType.TEXT_PLAIN)); } if (wrap) { - reqEntityBuilder.addPart("wrap", new StringBody("true", ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart("wrap", + new StringBody("true", ContentType.TEXT_PLAIN)); } - InputStreamBody body = new InputStreamBody(new BufferedInputStream(inputStream), + InputStreamBody body = new InputStreamBody( + new BufferedInputStream(inputStream), ContentType.APPLICATION_OCTET_STREAM, "unreliable"); HttpEntity entity = reqEntityBuilder.addPart("file", body).build(); @@ -739,40 +831,51 @@ public Long putAsPost(String sessionId, InputStream inputStream, String name, lo String prefix = ""; if (result.startsWith(prefix)) { - result = result.substring(prefix.length(), result.length() - suffix.length()); + result = result.substring(prefix.length(), + result.length() - suffix.length()); } - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json + .createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); - if (rootNode.getJsonNumber("checksum").longValueExact() != crc.getValue()) { - throw new InternalException("Error uploading - the checksum was not as expected"); + if (rootNode.getJsonNumber("checksum").longValueExact() != crc + .getValue()) { + throw new InternalException( + "Error uploading - the checksum was not as expected"); } return rootNode.getJsonNumber("id").longValueExact(); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (NumberFormatException e) { - throw new InternalException("Web service call did not return a valid Long value"); + throw new InternalException( + "Web service call did not return a valid Long value"); } } - public void restore(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, - BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { + public void restore(String sessionId, DataSelection data, Integer sc) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, + NotFoundException { URI uri = getUri(getUriBuilder("restore")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try (CloseableHttpResponse response = httpclient + .execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -792,7 +895,8 @@ public List getDatafileIds(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { String result = getString(response, sc); checkResponseConformity(response); - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json + .createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); List ids = new ArrayList<>(); @@ -802,21 +906,24 @@ public List getDatafileIds(String preparedId, Integer sc) } return ids; } catch (JsonException e) { - throw new InternalException( - "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); + throw new InternalException("TestingClient " + e.getClass() + + " " + e.getMessage() + " from " + result); } - } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException - | NotImplementedException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException + | InsufficientPrivilegesException + | NotImplementedException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public List getDatafileIds(String sessionId, DataSelection data, Integer sc) - throws InternalException, BadRequestException, ParseException, NotFoundException { + public List getDatafileIds(String sessionId, DataSelection data, + Integer sc) throws InternalException, BadRequestException, + ParseException, NotFoundException { URIBuilder uriBuilder = getUriBuilder("getDatafileIds"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); for (Entry entry : data.getParameters().entrySet()) { @@ -830,7 +937,8 @@ public List getDatafileIds(String sessionId, DataSelection data, Integer s try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); String result = getString(response, sc); - try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json + .createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); List ids = new ArrayList<>(); @@ -840,97 +948,122 @@ public List getDatafileIds(String sessionId, DataSelection data, Integer s } return ids; } catch (JsonException e) { - throw new InternalException( - "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); + throw new InternalException("TestingClient " + e.getClass() + + " " + e.getMessage() + " from " + result); } - } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException - | NotImplementedException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException + | InsufficientPrivilegesException + | NotImplementedException e) { + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void reset(String preparedId, Integer sc) throws InternalException, BadRequestException, ParseException, - InsufficientPrivilegesException, NotImplementedException, NotFoundException { + public void reset(String preparedId, Integer sc) + throws InternalException, BadRequestException, ParseException, + InsufficientPrivilegesException, NotImplementedException, + NotFoundException { URI uri = getUri(getUriBuilder("reset")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.preparedId, preparedId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.preparedId, preparedId)); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try (CloseableHttpResponse response = httpclient + .execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void reset(String sessionId, DataSelection data, int sc) throws InternalException, BadRequestException, - ParseException, InsufficientPrivilegesException, NotImplementedException, NotFoundException { + public void reset(String sessionId, DataSelection data, int sc) + throws InternalException, BadRequestException, ParseException, + InsufficientPrivilegesException, NotImplementedException, + NotFoundException { URI uri = getUri(getUriBuilder("reset")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try (CloseableHttpResponse response = httpclient + .execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void write(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, - BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, - DataNotOnlineException { + public void write(String sessionId, DataSelection data, Integer sc) + throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, InternalException, + NotFoundException, DataNotOnlineException { URI uri = getUri(getUriBuilder("write")); List formparams = new ArrayList<>(); - formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add( + new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add( + new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(formparams)); - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + try (CloseableHttpResponse response = httpclient + .execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); + throw new InternalException( + e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - private void checkResponseConformity(HttpResponse response) throws InternalException { + private void checkResponseConformity(HttpResponse response) + throws InternalException { StatusLine status = response.getStatusLine(); var statusCode = status.getStatusCode(); - if(statusCode == 200 && response.getEntity() != null) { - //we have a status of 200 and a payload. Check for header conformity - Boolean chunked = response.getFirstHeader("Transfer-Encoding") != null ? response.getFirstHeader("Transfer-Encoding").getValue().contains("chunked") - : false; - - if ( (response.getFirstHeader("Content-Length") == null && !chunked) - || (response.getFirstHeader("Content-Length") != null && chunked ) ) { - - throw new InternalException("Responses with payload should either contain the Content-Length header or the 'Transfer-Encoding: chunked' header. It needs exactly one of them."); + if (statusCode == 200 && response.getEntity() != null) { + // we have a status of 200 and a payload. Check for header + // conformity + Boolean chunked = response + .getFirstHeader("Transfer-Encoding") != null + ? response.getFirstHeader("Transfer-Encoding") + .getValue().contains("chunked") + : false; + + if ((response.getFirstHeader("Content-Length") == null && !chunked) + || (response.getFirstHeader("Content-Length") != null + && chunked)) { + + throw new InternalException( + "Responses with payload should either contain the Content-Length header or the 'Transfer-Encoding: chunked' header. It needs exactly one of them."); } } } From d0357d7287a4b05ebd8cdc97192ecb9a4052b2d7 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:13:10 +0200 Subject: [PATCH 18/19] Revert "linted code to test ci check" This reverts commit 3d45da7e244dd382f5997e5f45e31127addfd726. --- .../java/org/icatproject/ids/IdsService.java | 704 +++++++----------- src/main/java/org/icatproject/ids/Tidier.java | 139 ++-- .../icatproject/ids/enums/RequestType.java | 19 +- .../ids/exceptions/IdsExceptionMapper.java | 9 +- .../exceptions/NotFoundExceptionMapper.java | 13 +- .../exceptions/RuntimeExceptionMapper.java | 13 +- .../FiniteStateMachine.java | 73 +- ...niteStateMachineForSingleLevelStorage.java | 21 +- ...iteStateMachineForStorageUnitDatafile.java | 209 ++---- ...niteStateMachineForStorageUnitDataset.java | 132 ++-- .../ids/helpers/CORSResponseFilter.java | 4 +- .../icatproject/ids/helpers/Constants.java | 4 +- .../ids/helpers/LocationHelper.java | 13 +- .../ids/helpers/RangeOutputStream.java | 8 +- .../java/org/icatproject/ids/helpers/SO.java | 60 +- .../ids/helpers/ValueContainer.java | 96 +-- .../icatproject/ids/models/DataInfoBase.java | 4 +- .../icatproject/ids/models/DataSelection.java | 16 +- .../icatproject/ids/models/DatafileInfo.java | 26 +- .../icatproject/ids/models/DatasetInfo.java | 44 +- .../org/icatproject/ids/models/Prepared.java | 76 +- .../ids/requestHandlers/ArchiveHandler.java | 13 +- .../ids/requestHandlers/DeleteHandler.java | 32 +- .../GetDataFileIdsHandler.java | 26 +- .../ids/requestHandlers/GetDataHandler.java | 125 ++-- .../requestHandlers/GetIcatUrlHandler.java | 10 +- .../GetServiceStatusHandler.java | 23 +- .../ids/requestHandlers/GetStatusHandler.java | 18 +- .../requestHandlers/IsPreparedHandler.java | 24 +- .../requestHandlers/IsReadOnlyHandler.java | 8 +- .../requestHandlers/IsTwoLevelHandler.java | 8 +- .../requestHandlers/PrepareDataHandler.java | 43 +- .../ids/requestHandlers/PutHandler.java | 209 ++---- .../ids/requestHandlers/ResetHandler.java | 18 +- .../ids/requestHandlers/RestoreHandler.java | 14 +- .../ids/requestHandlers/WriteHandler.java | 26 +- .../base/DataControllerBase.java | 45 +- .../base/DataRequestHandler.java | 59 +- .../base/PreparedDataController.java | 36 +- .../base/RequestHandlerBase.java | 80 +- .../base/UnpreparedDataController.java | 44 +- .../getSizeHandlers/GetSizeHandler.java | 36 +- .../GetSizeHandlerForFastProcessing.java | 76 +- .../icatproject/ids/services/ICATGetter.java | 22 +- .../icatproject/ids/services/IcatReader.java | 12 +- .../icatproject/ids/services/LockManager.java | 19 +- .../ids/services/PropertyHandler.java | 113 +-- .../ids/services/ServiceProvider.java | 37 +- .../icatproject/ids/services/Transmitter.java | 9 +- .../ids/services/UnfinishedWorkService.java | 74 +- .../DataSelectionService.java | 159 ++-- .../DataSelectionServiceFactory.java | 376 ++++------ ...SelectionServiceForSingleLevelStorage.java | 42 +- ...electionServiceForStorageUnitDatafile.java | 31 +- ...SelectionServiceForStorageUnitDataset.java | 37 +- .../icatproject/ids/thread/DfArchiver.java | 19 +- .../org/icatproject/ids/thread/DfDeleter.java | 13 +- .../icatproject/ids/thread/DfRestorer.java | 21 +- .../org/icatproject/ids/thread/DfWriter.java | 18 +- .../icatproject/ids/thread/DsArchiver.java | 15 +- .../icatproject/ids/thread/DsRestorer.java | 46 +- .../org/icatproject/ids/thread/DsWriter.java | 40 +- .../icatproject/ids/DataSelectionDevTest.java | 45 +- .../java/org/icatproject/ids/DigestTest.java | 13 +- .../icatproject/ids/PreparePackingTest.java | 32 +- .../ids/RangeOutputStreamTest.java | 9 +- .../org/icatproject/ids/TestICATGetter.java | 11 +- .../java/org/icatproject/ids/TestUtils.java | 7 +- .../icatproject/ids/TestValueContainer.java | 23 +- .../java/org/icatproject/ids/TestZipping.java | 12 +- .../java/org/icatproject/ids/TidierTest.java | 16 +- .../org/icatproject/ids/TransmittingTest.java | 167 ++--- .../org/icatproject/ids/ValidaterTest.java | 2 +- .../icatproject/ids/integration/BaseTest.java | 318 +++----- .../integration/one/BogusDatafileTest.java | 40 +- .../ids/integration/one/DeleteTest.java | 22 +- .../integration/one/GetDataExplicitTest.java | 106 +-- .../one/GetDataForPreparedIdTest.java | 53 +- .../one/GetStatusExplicitTest.java | 21 +- .../one/GetStatusForPreparedIdTest.java | 14 +- .../ids/integration/one/MiscTest.java | 22 +- .../ids/integration/one/PerformanceTest.java | 21 +- .../ids/integration/one/PrepareDataTest.java | 54 +- .../ids/integration/one/PutTest.java | 40 +- .../ids/integration/one/WriteTest.java | 3 +- .../ids/integration/two/ArchiveTest.java | 11 +- .../integration/two/BogusDatafileTest.java | 74 +- .../ids/integration/two/DeleteTest.java | 15 +- .../integration/two/GetDataExplicitTest.java | 77 +- .../two/GetDataForPreparedIdTest.java | 49 +- .../two/GetStatusExplicitTest.java | 19 +- .../two/GetStatusForPreparedIdTest.java | 14 +- .../ids/integration/two/MiscTest.java | 19 +- .../ids/integration/two/PrepareDataTest.java | 81 +- .../ids/integration/two/PutTest.java | 37 +- .../integration/two/RestoreErrorsTest.java | 17 +- .../ids/integration/two/RestoreTest.java | 16 +- .../ids/integration/two/WriteTest.java | 19 +- .../ids/integration/twodf/ArchiveTest.java | 11 +- .../integration/twodf/BogusDatafileTest.java | 74 +- .../ids/integration/twodf/DeleteTest.java | 15 +- .../twodf/GetDataExplicitTest.java | 77 +- .../twodf/GetDataForPreparedIdTest.java | 51 +- .../twodf/GetStatusExplicitTest.java | 22 +- .../twodf/GetStatusForPreparedIdTest.java | 14 +- .../ids/integration/twodf/MiscTest.java | 19 +- .../integration/twodf/PrepareDataTest.java | 81 +- .../ids/integration/twodf/PutTest.java | 37 +- .../ids/integration/twodf/QueueTest.java | 13 +- .../ids/integration/twodf/RestoreTest.java | 16 +- .../ids/integration/twodf/WriteTest.java | 19 +- .../ids/integration/util/Setup.java | 29 +- .../util/client/HttpInputStream.java | 3 +- .../util/client/TestingClient.java | 455 ++++------- 114 files changed, 2279 insertions(+), 3915 deletions(-) diff --git a/src/main/java/org/icatproject/ids/IdsService.java b/src/main/java/org/icatproject/ids/IdsService.java index c0a57cd0..0d517fa9 100644 --- a/src/main/java/org/icatproject/ids/IdsService.java +++ b/src/main/java/org/icatproject/ids/IdsService.java @@ -6,6 +6,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; + import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import jakarta.ejb.EJB; @@ -22,18 +23,16 @@ import jakarta.ws.rs.HeaderParam; import jakarta.ws.rs.POST; import jakarta.ws.rs.PUT; -import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.Path; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.icatproject.IcatException_Exception; - import org.icatproject.ids.enums.RequestIdNames; import org.icatproject.ids.exceptions.BadRequestException; import org.icatproject.ids.exceptions.DataNotOnlineException; @@ -72,8 +71,7 @@ @Stateless public class IdsService { - private final static Logger logger = LoggerFactory - .getLogger(IdsService.class); + private final static Logger logger = LoggerFactory.getLogger(IdsService.class); @EJB Transmitter transmitter; @@ -91,6 +89,7 @@ public class IdsService { private UnfinishedWorkService unfinishedWorkService; + @PostConstruct private void init() { @@ -99,19 +98,15 @@ private void init() { logger.info("creating IdsService"); - FiniteStateMachine.createInstance(reader, lockManager, - PropertyHandler.getInstance().getStorageUnit()); + FiniteStateMachine.createInstance(reader, lockManager, PropertyHandler.getInstance().getStorageUnit()); this.fsm = FiniteStateMachine.getInstance(); this.fsm.init(); - ServiceProvider.createInstance(transmitter, fsm, lockManager, - reader); + ServiceProvider.createInstance(transmitter, fsm, lockManager, reader); - var propertyHandler = ServiceProvider.getInstance() - .getPropertyHandler(); + var propertyHandler = ServiceProvider.getInstance().getPropertyHandler(); var archiveStorage = propertyHandler.getArchiveStorage(); var twoLevel = archiveStorage != null; - var preparedDir = propertyHandler.getCacheDir() - .resolve("prepared"); + var preparedDir = propertyHandler.getCacheDir().resolve("prepared"); Files.createDirectories(preparedDir); this.unfinishedWorkService = new UnfinishedWorkService(); @@ -125,17 +120,13 @@ private void init() { java.nio.file.Path datasetDir; if (twoLevel) { - datasetDir = propertyHandler.getCacheDir() - .resolve("dataset"); - var markerDir = propertyHandler.getCacheDir() - .resolve("marker"); + datasetDir = propertyHandler.getCacheDir().resolve("dataset"); + var markerDir = propertyHandler.getCacheDir().resolve("marker"); if (!inited) { Files.createDirectories(datasetDir); Files.createDirectories(markerDir); - this.unfinishedWorkService - .restartUnfinishedWork(markerDir, key); - this.unfinishedWorkService - .cleanDatasetCache(datasetDir); + this.unfinishedWorkService.restartUnfinishedWork(markerDir, key); + this.unfinishedWorkService.cleanDatasetCache(datasetDir); } } @@ -145,53 +136,47 @@ private void init() { } } catch (Throwable e) { logger.error("Won't start ", e); - throw new RuntimeException("IdsService reports " + e.getClass() - + " " + e.getMessage()); + throw new RuntimeException("IdsService reports " + e.getClass() + " " + e.getMessage()); } } + @PreDestroy private void exit() { this.fsm.exit(); logger.info("destroyed IdsService"); } + /** * Archive data specified by the investigationIds, datasetIds and * datafileIds specified along with a sessionId. If two level storage is not * in use this has no effect. * * @title archive - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds If present, a comma separated list of - * investigation id values - * @param datasetIds If present, a comma separated list of data set id - * values or null - * @param datafileIds If present, a comma separated list of datafile id - * values. + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds If present, a comma separated list of investigation id values + * @param datasetIds If present, a comma separated list of data set id values or + * null + * @param datafileIds If present, a comma separated list of datafile id values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException * @throws InternalException * @throws NotFoundException - * @throws DataNotOnlineException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("archive") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void archive(@Context HttpServletRequest request, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, + public void archive(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new ArchiveHandler(request.getRemoteAddr(), sessionId, - investigationIds, datasetIds, datafileIds); + var handler = new ArchiveHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } @@ -200,14 +185,11 @@ public void archive(@Context HttpServletRequest request, * specified along with a sessionId. * * @title delete - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds If present, a comma separated list of - * investigation id values - * @param datasetIds If present, a comma separated list of data set id - * values or null - * @param datafileIds If present, a comma separated list of datafile id - * values. + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds If present, a comma separated list of investigation id values + * @param datasetIds If present, a comma separated list of data set id values or + * null + * @param datafileIds If present, a comma separated list of datafile id values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException @@ -218,20 +200,16 @@ public void archive(@Context HttpServletRequest request, */ @DELETE @Path("delete") - public void delete(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, - InternalException, DataNotOnlineException { - - var handler = new DeleteHandler(request.getRemoteAddr(), sessionId, - investigationIds, datasetIds, datafileIds); + public void delete(@Context HttpServletRequest request, @QueryParam(RequestIdNames.sessionId) String sessionId, + @QueryParam("investigationIds") String investigationIds, @QueryParam("datasetIds") String datasetIds, + @QueryParam("datafileIds") String datafileIds) throws NotImplementedException, BadRequestException, + InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { + + var handler = new DeleteHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } + /** * Return the version of the server * @@ -244,8 +222,7 @@ public void delete(@Context HttpServletRequest request, public String getVersion() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen.writeStartObject().write("version", Constants.API_VERSION) - .writeEnd(); + gen.writeStartObject().write("version", Constants.API_VERSION).writeEnd(); gen.close(); return baos.toString(); } @@ -257,58 +234,43 @@ public String getVersion() { * set. If preparedId is set the compress and zip arguments are not used. * * @title getData - * @param preparedId A valid preparedId returned by a call to - * prepareData - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds A comma separated list of investigation id - * values. + * @param preparedId A valid preparedId returned by a call to prepareData + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds A comma separated list of investigation id values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of data file id values. - * @param compress If true use default compression otherwise no - * compression. This only applies if preparedId is - * not set and if the results are being zipped. - * @param zip If true the data should be zipped. If multiple - * files are requested (or could be because a - * datasetId or investigationId has been specified) - * the data are zipped regardless of the + * @param compress If true use default compression otherwise no compression. This + * only applies if preparedId is not set and if the results are + * being zipped. + * @param zip If true the data should be zipped. If multiple files are + * requested (or could be because a datasetId or investigationId + * has been specified) the data are zipped regardless of the * specification of this flag. * @param outname The file name to put in the returned header - * "ContentDisposition". If it does not end in .zip - * but it is a zip file then a ".zip" will be - * appended. - * @param range A range header which must match "bytes=(\\d+)-" - * to specify an offset i.e. to skip a number of - * bytes. + * "ContentDisposition". If it does not end in .zip but it is a + * zip file then a ".zip" will be appended. + * @param range A range header which must match "bytes=(\\d+)-" to specify an + * offset i.e. to skip a number of bytes. * @return a stream of json data. * @throws BadRequestException * @throws NotFoundException * @throws InternalException * @throws InsufficientPrivilegesException * @throws DataNotOnlineException - * @throws NotImplementedException + * @throws NotImplementedException * @statuscode 200 To indicate success */ @GET @Path("getData") @Produces(MediaType.APPLICATION_OCTET_STREAM) - public Response getData(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds, - @QueryParam("compress") boolean compress, - @QueryParam("zip") boolean zip, - @QueryParam("outname") String outname, - @HeaderParam("Range") String range) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, DataNotOnlineException, - NotImplementedException { - - var handler = new GetDataHandler(request.getRemoteAddr(), preparedId, - sessionId, investigationIds, datasetIds, datafileIds, compress, - zip, outname, range); + public Response getData(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds, + @QueryParam("compress") boolean compress, @QueryParam("zip") boolean zip, + @QueryParam("outname") String outname, @HeaderParam("Range") String range) throws BadRequestException, + NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + + var handler = new GetDataHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds, compress, zip, outname, range); return handler.handle().getResponse(); } @@ -319,12 +281,9 @@ public Response getData(@Context HttpServletRequest request, * set. * * @title getDatafileIds - * @param preparedId A valid preparedId returned by a call to - * prepareData - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds A comma separated list of investigation id - * values. + * @param preparedId A valid preparedId returned by a call to prepareData + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds A comma separated list of investigation id values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of datafile id values. * @return a list of id values @@ -332,26 +291,19 @@ public Response getData(@Context HttpServletRequest request, * @throws InternalException * @throws NotFoundException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getDatafileIds") @Produces(MediaType.APPLICATION_JSON) - public String getDatafileIds(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, InternalException, NotFoundException, - InsufficientPrivilegesException, DataNotOnlineException, - NotImplementedException { - - var handler = new GetDataFileIdsHandler(request.getRemoteAddr(), - preparedId, sessionId, investigationIds, datasetIds, - datafileIds); + public String getDatafileIds(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + + var handler = new GetDataFileIdsHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); return handler.handle().getString(); } @@ -361,21 +313,18 @@ public String getDatafileIds(@Context HttpServletRequest request, * obtained. * * @return the url of the icat server - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("getIcatUrl") @Produces(MediaType.TEXT_PLAIN) - public String getIcatUrl(@Context HttpServletRequest request) - throws InternalException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public String getIcatUrl(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var handler = new GetIcatUrlHandler(request.getRemoteAddr()); return handler.handle().getString(); @@ -386,28 +335,24 @@ public String getIcatUrl(@Context HttpServletRequest request) * privileged to use this call. * * @title getServiceStatus - * @param sessionId A valid ICAT session ID of a user in the IDS - * rootUserNames set. + * @param sessionId A valid ICAT session ID of a user in the IDS rootUserNames + * set. * @return a json string. * @throws InternalException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws BadRequestException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws BadRequestException * @statuscode 200 To indicate success */ @GET @Path("getServiceStatus") @Produces(MediaType.APPLICATION_JSON) - public String getServiceStatus(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.sessionId) String sessionId) - throws InternalException, InsufficientPrivilegesException, - BadRequestException, NotFoundException, DataNotOnlineException, - NotImplementedException { - - var handler = new GetServiceStatusHandler(request.getRemoteAddr(), - sessionId); + public String getServiceStatus(@Context HttpServletRequest request, @QueryParam(RequestIdNames.sessionId) String sessionId) + throws InternalException, InsufficientPrivilegesException, BadRequestException, NotFoundException, DataNotOnlineException, NotImplementedException { + + var handler = new GetServiceStatusHandler(request.getRemoteAddr(), sessionId); return handler.handle().getString(); } @@ -416,53 +361,41 @@ public String getServiceStatus(@Context HttpServletRequest request, * investigationIds, datasetIds and datafileIds along with a sessionId. * * @title getSize - * @param preparedId A valid preparedId returned by a call to - * prepareData - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds If present, a comma separated list of - * investigation id values - * @param datasetIds If present, a comma separated list of data set id - * values or null - * @param datafileIds If present, a comma separated list of data file - * id values. + * @param preparedId A valid preparedId returned by a call to prepareData + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds If present, a comma separated list of investigation id values + * @param datasetIds If present, a comma separated list of data set id values or + * null + * @param datafileIds If present, a comma separated list of data file id values. * @return the size in bytes * @throws BadRequestException * @throws NotFoundException * @throws InsufficientPrivilegesException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getSize") @Produces(MediaType.TEXT_PLAIN) - public long getSize(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, - DataNotOnlineException, NotImplementedException { + public long getSize(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { + var result = ValueContainer.getInvalid(); // trying fast computation - if (sessionId != null) { - var fastHandler = new GetSizeHandlerForFastProcessing( - request.getRemoteAddr(), sessionId, investigationIds, - datasetIds, datafileIds); + if(sessionId != null) { + var fastHandler = new GetSizeHandlerForFastProcessing(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); result = fastHandler.handle(); } // otherwise normal computation - if (result.isInvalid()) { - var handler = new GetSizeHandler(request.getRemoteAddr(), - preparedId, sessionId, investigationIds, datasetIds, - datafileIds); + if(result.isInvalid()) { + var handler = new GetSizeHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); result = handler.handle(); } @@ -474,61 +407,49 @@ public long getSize(@Context HttpServletRequest request, * investigationIds, datasetIds and datafileIds along with a sessionId. * * @title getStatus - * @param preparedId A valid preparedId returned by a call to - * prepareData - * @param sessionId A sessionId returned by a call to the icat - * server. If the sessionId is omitted or null the - * ids reader account will be used which has read - * access to all data. - * @param investigationIds If present, a comma separated list of - * investigation id values - * @param datasetIds If present, a comma separated list of data set id - * values or null - * @param datafileIds If present, a comma separated list of data file - * id values. + * @param preparedId A valid preparedId returned by a call to prepareData + * @param sessionId A sessionId returned by a call to the icat server. If the + * sessionId is omitted or null the ids reader account will be + * used which has read access to all data. + * @param investigationIds If present, a comma separated list of investigation id values + * @param datasetIds If present, a comma separated list of data set id values or + * null + * @param datafileIds If present, a comma separated list of data file id values. * @return a string with "ONLINE" if all data are online, "RESTORING" if one - * or more files are in the process of being restored but none are - * archived and no restoration has been requested or "ARCHIVED" if - * one or more files are archived and and no restoration has been - * requested. + * or more files are in the process of being restored but none are + * archived and no restoration has been requested or "ARCHIVED" if + * one or more files are archived and and no restoration has been + * requested. * @throws BadRequestException * @throws NotFoundException * @throws InsufficientPrivilegesException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @GET @Path("getStatus") @Produces(MediaType.TEXT_PLAIN) - public String getStatus(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("investigationIds") String investigationIds, - @QueryParam("datasetIds") String datasetIds, - @QueryParam("datafileIds") String datafileIds) - throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, - DataNotOnlineException, NotImplementedException { - - // special case for getStatus request: getting status is possible - // without authentification + public String getStatus(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId, + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("investigationIds") String investigationIds, + @QueryParam("datasetIds") String datasetIds, @QueryParam("datafileIds") String datafileIds) + throws BadRequestException, NotFoundException, InsufficientPrivilegesException, InternalException, DataNotOnlineException, NotImplementedException { + + // special case for getStatus request: getting status is possible without authentification if (sessionId == null && preparedId == null) { try { - sessionId = ServiceProvider.getInstance().getIcatReader() - .getSessionId(); + sessionId = ServiceProvider.getInstance().getIcatReader().getSessionId(); } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } } - var handler = new GetStatusHandler(request.getRemoteAddr(), preparedId, - sessionId, investigationIds, datasetIds, datafileIds); + var handler = new GetStatusHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); return handler.handle().getString(); } + /** * Returns true if all the data files are ready to be downloaded. As a side * effect, if any data files are archived and no restoration has been @@ -546,46 +467,39 @@ public String getStatus(@Context HttpServletRequest request, * @throws BadRequestException * @throws NotFoundException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws InsufficientPrivilegesException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws InsufficientPrivilegesException * @statuscode 200 To indicate success */ @GET @Path("isPrepared") @Produces(MediaType.TEXT_PLAIN) - public boolean isPrepared(@Context HttpServletRequest request, - @QueryParam(RequestIdNames.preparedId) String preparedId) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, DataNotOnlineException, - NotImplementedException { - - var handler = new IsPreparedHandler(request.getRemoteAddr(), - preparedId); + public boolean isPrepared(@Context HttpServletRequest request, @QueryParam(RequestIdNames.preparedId) String preparedId) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + + var handler = new IsPreparedHandler(request.getRemoteAddr(), preparedId); return handler.handle().getBool(); } /** - * An ids server can be configured to be read only. This returns thenew - * DfProcessQueue() readOnly status of the server. + * An ids server can be configured to be read only. This returns thenew DfProcessQueue() + * readOnly status of the server. * * @title isReadOnly * @return true if readonly, else false - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("isReadOnly") @Produces(MediaType.TEXT_PLAIN) - public boolean isReadOnly(@Context HttpServletRequest request) - throws InternalException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public boolean isReadOnly(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var handler = new IsReadOnlyHandler(request.getRemoteAddr()); return handler.handle().getBool(); @@ -597,21 +511,18 @@ public boolean isReadOnly(@Context HttpServletRequest request) * * @title isTwoLevel * @return true if twoLevel, else false - * @throws NotImplementedException - * @throws DataNotOnlineException - * @throws NotFoundException - * @throws InsufficientPrivilegesException - * @throws BadRequestException - * @throws InternalException + * @throws NotImplementedException + * @throws DataNotOnlineException + * @throws NotFoundException + * @throws InsufficientPrivilegesException + * @throws BadRequestException + * @throws InternalException * @statuscode 200 To indicate success */ @GET @Path("isTwoLevel") @Produces(MediaType.TEXT_PLAIN) - public boolean isTwoLevel(@Context HttpServletRequest request) - throws InternalException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public boolean isTwoLevel(@Context HttpServletRequest request) throws InternalException, BadRequestException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var handler = new IsTwoLevelHandler(request.getRemoteAddr()); return handler.handle().getBool(); @@ -640,45 +551,37 @@ public String ping() { * omitted, along with a sessionId. * * @title prepareData - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds A comma separated list of investigation id - * values. + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds A comma separated list of investigation id values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of datafile id values. - * @param compress If true use default compression otherwise no - * compression. This only applies if preparedId is - * not set and if the results are being zipped. - * @param zip If true the data should be zipped. If multiple - * files are requested (or could be because a - * datasetId or investigationId has been specified) - * the data are zipped regardless of the + * @param compress If true use default compression otherwise no compression. This + * only applies if preparedId is not set and if the results are + * being zipped. + * @param zip If true the data should be zipped. If multiple files are + * requested (or could be because a datasetId or investigationId + * has been specified) the data are zipped regardless of the * specification of this flag. * @return a string with the preparedId * @throws BadRequestException * @throws InsufficientPrivilegesException * @throws NotFoundException * @throws InternalException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("prepareData") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.TEXT_PLAIN) - public String prepareData(@Context HttpServletRequest request, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds, - @FormParam("compress") boolean compress, - @FormParam("zip") boolean zip) throws BadRequestException, - InsufficientPrivilegesException, NotFoundException, - InternalException, NotImplementedException, DataNotOnlineException { - - var handler = new PrepareDataHandler(request.getRemoteAddr(), sessionId, - investigationIds, datasetIds, datafileIds, compress, zip); + public String prepareData(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds, @FormParam("compress") boolean compress, + @FormParam("zip") boolean zip) + throws BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, NotImplementedException, DataNotOnlineException { + + var handler = new PrepareDataHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds, compress, zip); return handler.handle().getString(); } @@ -687,23 +590,17 @@ public String prepareData(@Context HttpServletRequest request, * * @title put * @param body The contents of the file to be stored - * @param sessionId A sessionId returned by a call to the icat - * server. + * @param sessionId A sessionId returned by a call to the icat server. * @param name A name to assign to the data file - * @param datafileFormatId The id of the data file format to associate - * with the data file - * @param datasetId The id of the data set to which the data file - * should be associated. - * @param description An optional description to associate with the - * data file - * @param doi An optional description to associate with the - * data file - * @param datafileCreateTime An optional datafileCreateTime to associate - * with the data file - * @param datafileModTime An optional datafileModTime to associate with - * the data file + * @param datafileFormatId The id of the data file format to associate with the data file + * @param datasetId The id of the data set to which the data file should be + * associated. + * @param description An optional description to associate with the data file + * @param doi An optional description to associate with the data file + * @param datafileCreateTime An optional datafileCreateTime to associate with the data file + * @param datafileModTime An optional datafileModTime to associate with the data file * @return a json object with attributes of "id", "checksum", "location" and - * "size"; + * "size"; * @throws BadRequestException * @throws NotFoundException * @throws InternalException @@ -717,21 +614,15 @@ public String prepareData(@Context HttpServletRequest request, @Consumes(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.APPLICATION_JSON) public Response put(@Context HttpServletRequest request, InputStream body, - @QueryParam(RequestIdNames.sessionId) String sessionId, - @QueryParam("name") String name, - @QueryParam("datafileFormatId") String datafileFormatId, - @QueryParam("datasetId") String datasetId, - @QueryParam("description") String description, - @QueryParam("doi") String doi, - @QueryParam("datafileCreateTime") String datafileCreateTime, - @QueryParam("datafileModTime") String datafileModTime) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, NotImplementedException, - DataNotOnlineException { - - var handler = new PutHandler(request.getRemoteAddr(), sessionId, body, - name, datafileFormatId, datasetId, description, doi, - datafileCreateTime, datafileModTime, false, false); + @QueryParam(RequestIdNames.sessionId) String sessionId, @QueryParam("name") String name, + @QueryParam("datafileFormatId") String datafileFormatId, @QueryParam("datasetId") String datasetId, + @QueryParam("description") String description, @QueryParam("doi") String doi, + @QueryParam("datafileCreateTime") String datafileCreateTime, + @QueryParam("datafileModTime") String datafileModTime) throws BadRequestException, NotFoundException, + InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { + + var handler = new PutHandler(request.getRemoteAddr(), sessionId, body, name, datafileFormatId, datasetId, + description, doi, datafileCreateTime, datafileModTime, false, false); return handler.handle().getResponse(); } @@ -745,7 +636,7 @@ public Response put(@Context HttpServletRequest request, InputStream body, * @title putAsPost * @param request * @return a json object with attributes of "id", "checksum", "location" and - * "size"; + * "size"; * @throws BadRequestException * @throws NotFoundException * @throws InternalException @@ -758,73 +649,65 @@ public Response put(@Context HttpServletRequest request, InputStream body, @Path("put") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) - public Response putAsPost(@Context HttpServletRequest request) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, NotImplementedException, - DataNotOnlineException { - try { - String sessionId = null; - String name = null; - String datafileFormatId = null; - String datasetId = null; - String description = null; - String doi = null; - String datafileCreateTime = null; - String datafileModTime = null; - Response result = null; - boolean wrap = false; - boolean padding = false; - - // Parse the request - for (Part part : request.getParts()) { - String fieldName = part.getName(); - InputStream stream = part.getInputStream(); - if (part.getSubmittedFileName() == null) { - String value = new String(stream.readAllBytes(), - StandardCharsets.UTF_8); - if (fieldName.equals("sessionId")) { - sessionId = value; - } else if (fieldName.equals("name")) { - name = value; - } else if (fieldName.equals("datafileFormatId")) { - datafileFormatId = value; - } else if (fieldName.equals("datasetId")) { - datasetId = value; - } else if (fieldName.equals("description")) { - description = value; - } else if (fieldName.equals("doi")) { - doi = value; - } else if (fieldName.equals("datafileCreateTime")) { - datafileCreateTime = value; - } else if (fieldName.equals("datafileModTime")) { - datafileModTime = value; - } else if (fieldName.equals("wrap")) { - wrap = (value != null - && value.toUpperCase().equals("TRUE")); - } else if (fieldName.equals("padding")) { - padding = (value != null - && value.toUpperCase().equals("TRUE")); - } else { - throw new BadRequestException("Form field " + fieldName - + "is not recognised"); - } - } else { - if (name == null) { - name = part.getSubmittedFileName(); + public Response putAsPost(@Context HttpServletRequest request) throws BadRequestException, NotFoundException, + InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException { + try { + String sessionId = null; + String name = null; + String datafileFormatId = null; + String datasetId = null; + String description = null; + String doi = null; + String datafileCreateTime = null; + String datafileModTime = null; + Response result = null; + boolean wrap = false; + boolean padding = false; + + // Parse the request + for (Part part : request.getParts()) { + String fieldName = part.getName(); + InputStream stream = part.getInputStream(); + if (part.getSubmittedFileName() == null) { + String value = new String(stream.readAllBytes(), StandardCharsets.UTF_8); + if (fieldName.equals("sessionId")) { + sessionId = value; + } else if (fieldName.equals("name")) { + name = value; + } else if (fieldName.equals("datafileFormatId")) { + datafileFormatId = value; + } else if (fieldName.equals("datasetId")) { + datasetId = value; + } else if (fieldName.equals("description")) { + description = value; + } else if (fieldName.equals("doi")) { + doi = value; + } else if (fieldName.equals("datafileCreateTime")) { + datafileCreateTime = value; + } else if (fieldName.equals("datafileModTime")) { + datafileModTime = value; + } else if (fieldName.equals("wrap")) { + wrap = (value != null && value.toUpperCase().equals("TRUE")); + } else if (fieldName.equals("padding")) { + padding = (value != null && value.toUpperCase().equals("TRUE")); + } else { + throw new BadRequestException("Form field " + fieldName + "is not recognised"); + } + } else { + if (name == null) { + name = part.getSubmittedFileName(); + } + var handler = new PutHandler(request.getRemoteAddr(), sessionId, stream, name, datafileFormatId, datasetId, description, + doi, datafileCreateTime, datafileModTime, wrap, padding); + result = handler.handle().getResponse(); + } } - var handler = new PutHandler(request.getRemoteAddr(), - sessionId, stream, name, datafileFormatId, - datasetId, description, doi, datafileCreateTime, - datafileModTime, wrap, padding); - result = handler.handle().getResponse(); + return result; + } catch (IOException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); + } catch (ServletException e) { + throw new BadRequestException("Multipart content expected"); } - } - return result; - } catch (IOException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); - } catch (ServletException e) { - throw new BadRequestException("Multipart content expected"); - } } /** @@ -839,12 +722,9 @@ public Response putAsPost(@Context HttpServletRequest request) * specified then investigationIds, datasetIds and datafileIds are not used. * * @title reset - * @param preparedId A valid preparedId returned by a call to - * prepareData - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds A comma separated list of investigation id - * values. + * @param preparedId A valid preparedId returned by a call to prepareData + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds A comma separated list of investigation id values. * @param datasetIds A comma separated list of data set id values. * @param datafileIds A comma separated list of data file id values. * @return a stream of json data. @@ -852,24 +732,18 @@ public Response putAsPost(@Context HttpServletRequest request) * @throws NotFoundException * @throws InternalException * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException + * @throws NotImplementedException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("reset") - public void reset(@Context HttpServletRequest request, - @FormParam(RequestIdNames.preparedId) String preparedId, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws BadRequestException, InternalException, NotFoundException, - InsufficientPrivilegesException, DataNotOnlineException, - NotImplementedException { - - var handler = new ResetHandler(request.getRemoteAddr(), preparedId, - sessionId, investigationIds, datasetIds, datafileIds); + public void reset(@Context HttpServletRequest request, @FormParam(RequestIdNames.preparedId) String preparedId, + @FormParam(RequestIdNames.sessionId) String sessionId, @FormParam("investigationIds") String investigationIds, + @FormParam("datasetIds") String datasetIds, @FormParam("datafileIds") String datafileIds) + throws BadRequestException, InternalException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException, NotImplementedException { + + var handler = new ResetHandler(request.getRemoteAddr(), preparedId, sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } @@ -880,53 +754,45 @@ public void reset(@Context HttpServletRequest request, * in use this has no effect. * * @title restore - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds If present, a comma separated list of - * investigation id values - * @param datasetIds If present, a comma separated list of data set id - * values or null - * @param datafileIds If present, a comma separated list of datafile id - * values. + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds If present, a comma separated list of investigation id values + * @param datasetIds If present, a comma separated list of data set id values or + * null + * @param datafileIds If present, a comma separated list of datafile id values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException * @throws InternalException * @throws NotFoundException - * @throws DataNotOnlineException + * @throws DataNotOnlineException * @statuscode 200 To indicate success */ @POST @Path("restore") @Consumes("application/x-www-form-urlencoded") - public void restore(@Context HttpServletRequest request, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, + public void restore(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new RestoreHandler(request.getRemoteAddr(), sessionId, - investigationIds, datasetIds, datafileIds); + var handler = new RestoreHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } + /** - * Write data specified by the investigationIds, datasetIds and datafileIds - * specified along with a sessionId to archive storage. If two level storage - * is not in use this has no effect. + * Write data specified by the investigationIds, datasetIds + * and datafileIds specified along with a sessionId to archive + * storage. If two level storage is not in use this has no + * effect. * * @title write - * @param sessionId A sessionId returned by a call to the icat - * server. - * @param investigationIds If present, a comma separated list of - * investigation id values - * @param datasetIds If present, a comma separated list of data set id - * values or null - * @param datafileIds If present, a comma separated list of datafile id - * values. + * @param sessionId A sessionId returned by a call to the icat server. + * @param investigationIds If present, a comma separated list of investigation id values + * @param datasetIds If present, a comma separated list of data set id values or + * null + * @param datafileIds If present, a comma separated list of datafile id values. * @throws NotImplementedException * @throws BadRequestException * @throws InsufficientPrivilegesException @@ -937,17 +803,13 @@ public void restore(@Context HttpServletRequest request, @POST @Path("write") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public void write(@Context HttpServletRequest request, - @FormParam(RequestIdNames.sessionId) String sessionId, - @FormParam("investigationIds") String investigationIds, - @FormParam("datasetIds") String datasetIds, - @FormParam("datafileIds") String datafileIds) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, + public void write(@Context HttpServletRequest request, @FormParam(RequestIdNames.sessionId) String sessionId, + @FormParam("investigationIds") String investigationIds, @FormParam("datasetIds") String datasetIds, + @FormParam("datafileIds") String datafileIds) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { - var handler = new WriteHandler(request.getRemoteAddr(), sessionId, - investigationIds, datasetIds, datafileIds); + var handler = new WriteHandler(request.getRemoteAddr(), sessionId, investigationIds, datasetIds, datafileIds); handler.handle(); } } diff --git a/src/main/java/org/icatproject/ids/Tidier.java b/src/main/java/org/icatproject/ids/Tidier.java index d9efa850..9d647de0 100644 --- a/src/main/java/org/icatproject/ids/Tidier.java +++ b/src/main/java/org/icatproject/ids/Tidier.java @@ -24,7 +24,6 @@ import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.IcatException_Exception; - import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.exceptions.InsufficientPrivilegesException; @@ -56,77 +55,57 @@ public void run() { if (twoLevel) { if (storageUnit == StorageUnit.DATASET) { - List dsInfos = mainStorage.getDatasetsToArchive( - stopArchivingLevel, startArchivingLevel); + List dsInfos = mainStorage.getDatasetsToArchive(stopArchivingLevel, + startArchivingLevel); for (DsInfo dsInfo : dsInfos) { StringBuilder sb = new StringBuilder( "SELECT ds FROM Dataset ds, ds.investigation inv, inv.facility fac WHERE"); boolean andNeeded = false; - andNeeded = addNumericConstraint(sb, "ds.id", - dsInfo.getDsId(), andNeeded); - andNeeded = addStringConstraint(sb, "ds.location", - dsInfo.getDsLocation(), andNeeded); - andNeeded = addStringConstraint(sb, "ds.name", - dsInfo.getDsName(), andNeeded); - - andNeeded = addNumericConstraint(sb, "inv.id", - dsInfo.getInvId(), andNeeded); - andNeeded = addStringConstraint(sb, "inv.name", - dsInfo.getInvName(), andNeeded); - andNeeded = addStringConstraint(sb, "inv.visitId", - dsInfo.getVisitId(), andNeeded); - - andNeeded = addStringConstraint(sb, "fac.name", - dsInfo.getFacilityName(), andNeeded); - andNeeded = addNumericConstraint(sb, "fac.id", - dsInfo.getFacilityId(), andNeeded); + andNeeded = addNumericConstraint(sb, "ds.id", dsInfo.getDsId(), andNeeded); + andNeeded = addStringConstraint(sb, "ds.location", dsInfo.getDsLocation(), andNeeded); + andNeeded = addStringConstraint(sb, "ds.name", dsInfo.getDsName(), andNeeded); + + andNeeded = addNumericConstraint(sb, "inv.id", dsInfo.getInvId(), andNeeded); + andNeeded = addStringConstraint(sb, "inv.name", dsInfo.getInvName(), andNeeded); + andNeeded = addStringConstraint(sb, "inv.visitId", dsInfo.getVisitId(), andNeeded); + + andNeeded = addStringConstraint(sb, "fac.name", dsInfo.getFacilityName(), andNeeded); + andNeeded = addNumericConstraint(sb, "fac.id", dsInfo.getFacilityId(), andNeeded); sb.append(" INCLUDE ds.investigation.facility"); try { int low = 0; while (true) { - String query = sb.toString() + " LIMIT " - + low + "," + tidyBlockSize; + String query = sb.toString() + " LIMIT " + low + "," + tidyBlockSize; List os = reader.search(query); - logger.debug(query + " returns " + os.size() - + " datasets"); + logger.debug(query + " returns " + os.size() + " datasets"); for (Object o : os) { - DatasetInfo dsInfoImpl = new DatasetInfo( - (Dataset) o); - logger.debug("Requesting archive of " - + dsInfoImpl - + " to recover main storage"); - fsm.queue(dsInfoImpl, - DeferredOp.ARCHIVE); + DatasetInfo dsInfoImpl = new DatasetInfo((Dataset) o); + logger.debug( + "Requesting archive of " + dsInfoImpl + " to recover main storage"); + fsm.queue(dsInfoImpl, DeferredOp.ARCHIVE); } if (os.size() < tidyBlockSize) { break; } low += tidyBlockSize; } - } catch (InternalException | IcatException_Exception - | InsufficientPrivilegesException e) { + } catch (InternalException | IcatException_Exception | InsufficientPrivilegesException e) { // Log it and carry on - logger.error( - e.getClass() + " " + e.getMessage()); + logger.error(e.getClass() + " " + e.getMessage()); } } } else if (storageUnit == StorageUnit.DATAFILE) { - List dfInfos = mainStorage - .getDatafilesToArchive(stopArchivingLevel, - startArchivingLevel); + List dfInfos = mainStorage.getDatafilesToArchive(stopArchivingLevel, + startArchivingLevel); for (DfInfo dfInfo : dfInfos) { - StringBuilder sb = new StringBuilder( - "SELECT df FROM Datafile df WHERE"); + StringBuilder sb = new StringBuilder("SELECT df FROM Datafile df WHERE"); boolean andNeeded = false; - andNeeded = addNumericConstraint(sb, "df.id", - dfInfo.getDfId(), andNeeded); - andNeeded = addStringConstraint(sb, "df.createId", - dfInfo.getCreateId(), andNeeded); - andNeeded = addStringConstraint(sb, "df.modId", - dfInfo.getModId(), andNeeded); + andNeeded = addNumericConstraint(sb, "df.id", dfInfo.getDfId(), andNeeded); + andNeeded = addStringConstraint(sb, "df.createId", dfInfo.getCreateId(), andNeeded); + andNeeded = addStringConstraint(sb, "df.modId", dfInfo.getModId(), andNeeded); if (key != null) { if (dfInfo.getDfLocation() != null) { if (andNeeded) { @@ -135,54 +114,42 @@ public void run() { sb.append(" "); andNeeded = true; } - sb.append("df.location" + " LIKE '" - + dfInfo.getDfLocation() + " %'"); + sb.append("df.location" + " LIKE '" + dfInfo.getDfLocation() + " %'"); } } else { - andNeeded = addStringConstraint(sb, - "df.location", + andNeeded = addStringConstraint(sb, "df.location", dfInfo.getDfLocation(), andNeeded); } - andNeeded = addStringConstraint(sb, "df.name", - dfInfo.getDfName(), andNeeded); + andNeeded = addStringConstraint(sb, "df.name", dfInfo.getDfName(), andNeeded); sb.append(" INCLUDE df.dataset"); try { int low = 0; while (true) { - String query = sb.toString() + " LIMIT " - + low + "," + tidyBlockSize; + String query = sb.toString() + " LIMIT " + low + "," + tidyBlockSize; List os = reader.search(query); - logger.debug(query + " returns " + os.size() - + " datafiles"); + logger.debug(query + " returns " + os.size() + " datafiles"); for (Object o : os) { Datafile df = (Datafile) o; - DatafileInfo dfInfoImpl = new DatafileInfo( - df.getId(), df.getName(), - - LocationHelper.getLocation( - df.getId(), - df.getLocation()), - df.getCreateId(), df.getModId(), - df.getDataset().getId()); - - logger.debug("Requesting archive of " - + dfInfoImpl - + " to recover main storage"); - fsm.queue(dfInfoImpl, - DeferredOp.ARCHIVE); + DatafileInfo dfInfoImpl = new DatafileInfo(df.getId(), df.getName(), + + LocationHelper.getLocation(df.getId(), df.getLocation()), df.getCreateId(), + df.getModId(), df.getDataset().getId()); + + + logger.debug( + "Requesting archive of " + dfInfoImpl + " to recover main storage"); + fsm.queue(dfInfoImpl, DeferredOp.ARCHIVE); } if (os.size() < tidyBlockSize) { break; } low += tidyBlockSize; } - } catch (InternalException | IcatException_Exception - | InsufficientPrivilegesException e) { + } catch (InternalException | IcatException_Exception | InsufficientPrivilegesException e) { // Log it and carry on - logger.error( - e.getClass() + " " + e.getMessage()); + logger.error(e.getClass() + " " + e.getMessage()); } } } @@ -198,8 +165,7 @@ public void run() { private final static Logger logger = LoggerFactory.getLogger(Tidier.class); - static boolean addStringConstraint(StringBuilder sb, String var, - String value, boolean andNeeded) { + static boolean addStringConstraint(StringBuilder sb, String var, String value, boolean andNeeded) { if (value != null) { if (andNeeded) { sb.append(" AND "); @@ -212,8 +178,7 @@ static boolean addStringConstraint(StringBuilder sb, String var, return andNeeded; } - static boolean addNumericConstraint(StringBuilder sb, String var, - Long value, boolean andNeeded) { + static boolean addNumericConstraint(StringBuilder sb, String var, Long value, boolean andNeeded) { if (value != null) { if (andNeeded) { sb.append(" AND "); @@ -226,8 +191,7 @@ static boolean addNumericConstraint(StringBuilder sb, String var, return andNeeded; } - static void cleanPreparedDir(Path preparedDir, int preparedCount) - throws IOException { + static void cleanPreparedDir(Path preparedDir, int preparedCount) throws IOException { Map dateMap = new HashMap<>(); File[] files = preparedDir.toFile().listFiles(); @@ -287,13 +251,11 @@ public void init() { try { PropertyHandler propertyHandler = PropertyHandler.getInstance(); - FiniteStateMachine.createInstance(reader, lockManager, - propertyHandler.getStorageUnit()); + FiniteStateMachine.createInstance(reader, lockManager, propertyHandler.getStorageUnit()); this.fsm = FiniteStateMachine.getInstance(); - this.fsm.init(); // if not yet initialized by IdsService do it now - - sizeCheckIntervalMillis = propertyHandler - .getSizeCheckIntervalMillis(); + this.fsm.init(); //if not yet initialized by IdsService do it now + + sizeCheckIntervalMillis = propertyHandler.getSizeCheckIntervalMillis(); preparedCount = propertyHandler.getPreparedCount(); preparedDir = propertyHandler.getCacheDir().resolve("prepared"); Files.createDirectories(preparedDir); @@ -311,8 +273,7 @@ public void init() { logger.info("Tidier started"); } catch (IOException e) { - throw new RuntimeException( - "Tidier reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException("Tidier reports " + e.getClass() + " " + e.getMessage()); } } diff --git a/src/main/java/org/icatproject/ids/enums/RequestType.java b/src/main/java/org/icatproject/ids/enums/RequestType.java index 1f4c0b77..a79f83e5 100644 --- a/src/main/java/org/icatproject/ids/enums/RequestType.java +++ b/src/main/java/org/icatproject/ids/enums/RequestType.java @@ -4,7 +4,20 @@ * This enum contains all defined types of requests to this server */ public enum RequestType { - GETDATA, ARCHIVE, GETICATURL, GETDATAFILEIDS, GETSERVICESTATUS, GETSIZE, - GETSTATUS, ISPREPARED, ISREADONLY, ISTWOLEVEL, PREPAREDATA, PUT, RESET, - RESTORE, WRITE, DELETE + GETDATA, + ARCHIVE, + GETICATURL, + GETDATAFILEIDS, + GETSERVICESTATUS, + GETSIZE, + GETSTATUS, + ISPREPARED, + ISREADONLY, + ISTWOLEVEL, + PREPAREDATA, + PUT, + RESET, + RESTORE, + WRITE, + DELETE } diff --git a/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java index 0c9567db..77920041 100644 --- a/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/IdsExceptionMapper.java @@ -9,9 +9,9 @@ import jakarta.ws.rs.ext.Provider; /** - * Capture any {@link org.icatproject.ids.exceptions.IdsException - * WebServiceException} thrown from {@link org.icatproject.ids.IdsService - * WebService} and generate the appropriate response code and message. + * Capture any {@link org.icatproject.ids.exceptions.IdsException WebServiceException} thrown from + * {@link org.icatproject.ids.IdsService WebService} and generate the appropriate response code and + * message. */ @Provider public class IdsExceptionMapper implements ExceptionMapper { @@ -23,7 +23,6 @@ public Response toResponse(IdsException e) { gen.writeStartObject().write("code", e.getClass().getSimpleName()) .write("message", e.getShortMessage()); gen.writeEnd().close(); - return Response.status(e.getHttpStatusCode()).entity(baos.toString()) - .build(); + return Response.status(e.getHttpStatusCode()).entity(baos.toString()).build(); } } diff --git a/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java index 266b2e8a..f9157c92 100644 --- a/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/NotFoundExceptionMapper.java @@ -13,11 +13,9 @@ import org.slf4j.LoggerFactory; @Provider -public class NotFoundExceptionMapper - implements ExceptionMapper { +public class NotFoundExceptionMapper implements ExceptionMapper { - private static Logger logger = LoggerFactory - .getLogger(NotFoundExceptionMapper.class); + private static Logger logger = LoggerFactory.getLogger(NotFoundExceptionMapper.class); @Override public Response toResponse(NotFoundException e) { @@ -25,10 +23,7 @@ public Response toResponse(NotFoundException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); gen.writeStartObject().write("code", "NOT_IMPLEMENTED") - .write("message", - "Operation not implemented by this IDS server.") - .writeEnd().close(); - return Response.status(Response.Status.NOT_IMPLEMENTED) - .entity(baos.toString()).build(); + .write("message", "Operation not implemented by this IDS server.").writeEnd().close(); + return Response.status(Response.Status.NOT_IMPLEMENTED).entity(baos.toString()).build(); } } diff --git a/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java b/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java index 55249b31..aa38ba0a 100644 --- a/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java +++ b/src/main/java/org/icatproject/ids/exceptions/RuntimeExceptionMapper.java @@ -14,11 +14,9 @@ import org.slf4j.LoggerFactory; @Provider -public class RuntimeExceptionMapper - implements ExceptionMapper { +public class RuntimeExceptionMapper implements ExceptionMapper { - private final static Logger logger = LoggerFactory - .getLogger(RuntimeExceptionMapper.class); + private final static Logger logger = LoggerFactory.getLogger(RuntimeExceptionMapper.class); @Override public Response toResponse(RuntimeException e) { @@ -29,9 +27,8 @@ public Response toResponse(RuntimeException e) { baos.reset(); JsonGenerator gen = Json.createGenerator(baos); gen.writeStartObject().write("code", "InternalException") - .write("message", e.getClass() + " " + e.getMessage()) - .writeEnd().close(); - return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR) - .entity(baos.toString()).build(); + .write("message", e.getClass() + " " + e.getMessage()).writeEnd().close(); + return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR).entity(baos.toString()) + .build(); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java index 78a88ae5..f115c8da 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachine.java @@ -18,18 +18,18 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.IcatReader; import org.icatproject.ids.services.LockManager; -import org.icatproject.ids.services.LockManager.LockInfo; import org.icatproject.ids.services.PropertyHandler; +import org.icatproject.ids.services.LockManager.LockInfo; public abstract class FiniteStateMachine { + private static FiniteStateMachine instance; private static Boolean inited = false; @@ -39,52 +39,40 @@ protected FiniteStateMachine(IcatReader reader, LockManager lockManager) { this.lockManager = lockManager; } - public static void createInstance(IcatReader reader, - LockManager lockManager, StorageUnit storageUnit) { + public static void createInstance(IcatReader reader, LockManager lockManager, StorageUnit storageUnit) { - if (instance == null) { - if (storageUnit == StorageUnit.DATAFILE) - instance = new FiniteStateMachineForStorageUnitDatafile(reader, - lockManager); - else if (storageUnit == StorageUnit.DATASET) - instance = new FiniteStateMachineForStorageUnitDataset(reader, - lockManager); + if(instance == null) { + if(storageUnit == StorageUnit.DATAFILE) + instance = new FiniteStateMachineForStorageUnitDatafile(reader, lockManager); + else if(storageUnit == StorageUnit.DATASET) + instance = new FiniteStateMachineForStorageUnitDataset(reader, lockManager); else - instance = new FiniteStateMachineForSingleLevelStorage(reader, - lockManager); + instance = new FiniteStateMachineForSingleLevelStorage(reader, lockManager); } } public static FiniteStateMachine getInstance() { - if (instance != null) { + if(instance != null) { return instance; } - // If this assert was executed: Instance of FiniteStateMachine is not - // created. At First createInstance() has to be called at least once. - throw new RuntimeException( - "Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once."); + // If this assert was executed: Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once. + throw new RuntimeException("Instance of FiniteStateMachine is not created. At First createInstance() has to be called at least once."); } - public abstract void queue(DataInfoBase dataInfo, DeferredOp deferredOp) - throws InternalException; - + public abstract void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException; protected abstract void scheduleTimer(); - protected abstract void addDataInfoJson(JsonGenerator gen); public enum RequestedState { - ARCHIVE_REQUESTED, DELETE_REQUESTED, RESTORE_REQUESTED, WRITE_REQUESTED, - WRITE_THEN_ARCHIVE_REQUESTED + ARCHIVE_REQUESTED, DELETE_REQUESTED, RESTORE_REQUESTED, WRITE_REQUESTED, WRITE_THEN_ARCHIVE_REQUESTED } - protected static Logger logger = LoggerFactory - .getLogger(FiniteStateMachine.class); + protected static Logger logger = LoggerFactory.getLogger(FiniteStateMachine.class); /* - * Note that the veriable processOpsDelayMillis is used to either delay all - * deferred datafile operations or to delay dataset writes, depending on the - * setting of storageUnit. + * Note that the veriable processOpsDelayMillis is used to either delay all deferred + * datafile operations or to delay dataset writes, depending on the setting of storageUnit. */ protected long processOpsDelayMillis; @@ -131,8 +119,7 @@ public Set getMaybeOffline() { } /** - * Find any DataFileInfo which are being restored or are queued for - * restoration + * Find any DataFileInfo which are being restored or are queued for restoration */ public Set getRestoring() { Map union; @@ -158,26 +145,23 @@ protected void addDataInfoJsonFromDeferredOpsQueue(JsonGenerator gen) { gen.writeStartArray("opsQueue"); for (Entry entry : union.entrySet()) { DataInfoBase item = entry.getKey(); - gen.writeStartObject().write("data", item.toString()) - .write("request", entry.getValue().name()).writeEnd(); + gen.writeStartObject().write("data", item.toString()).write("request", entry.getValue().name()) + .writeEnd(); } gen.writeEnd(); // end Array("opsQueue") } public String getServiceStatus() throws InternalException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos) - .writeStartObject()) { - + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { + this.addDataInfoJson(gen); Collection lockInfo = lockManager.getLockInfo(); gen.write("lockCount", lockInfo.size()); gen.writeStartArray("locks"); for (LockInfo li : lockInfo) { - gen.writeStartObject().write("id", li.id) - .write("type", li.type.name()).write("count", li.count) - .writeEnd(); + gen.writeStartObject().write("id", li.id).write("type", li.type.name()).write("count", li.count).writeEnd(); } gen.writeEnd(); // end Array("locks") @@ -192,25 +176,24 @@ public String getServiceStatus() throws InternalException { return baos.toString(); } + public void init() { try { synchronized (inited) { - if (!inited) { + if(!inited) { propertyHandler = PropertyHandler.getInstance(); - processQueueIntervalMillis = propertyHandler - .getProcessQueueIntervalSeconds() * 1000L; + processQueueIntervalMillis = propertyHandler.getProcessQueueIntervalSeconds() * 1000L; this.scheduleTimer(); - + markerDir = propertyHandler.getCacheDir().resolve("marker"); Files.createDirectories(markerDir); inited = true; } } } catch (IOException e) { - throw new RuntimeException("FiniteStateMachine reports " - + e.getClass() + " " + e.getMessage()); + throw new RuntimeException("FiniteStateMachine reports " + e.getClass() + " " + e.getMessage()); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java index bfa85337..7ffb349d 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForSingleLevelStorage.java @@ -1,33 +1,32 @@ package org.icatproject.ids.finiteStateMachine; -import jakarta.json.stream.JsonGenerator; - import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.IcatReader; import org.icatproject.ids.services.LockManager; -public class FiniteStateMachineForSingleLevelStorage - extends FiniteStateMachine { +import jakarta.json.stream.JsonGenerator; + +public class FiniteStateMachineForSingleLevelStorage extends FiniteStateMachine { - protected FiniteStateMachineForSingleLevelStorage(IcatReader reader, - LockManager lockManager) { + protected FiniteStateMachineForSingleLevelStorage(IcatReader reader, LockManager lockManager) { super(reader, lockManager); } + @Override - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) - throws InternalException { - throw new InternalException( - "### Operation is not permitted for single level storage"); + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { + throw new InternalException("### Operation is not permitted for single level storage"); } + @Override protected void scheduleTimer() { - // nothing to do here for single level storage + //nothing to do here for single level storage } + @Override protected void addDataInfoJson(JsonGenerator gen) { gen.writeStartArray("opsQueue").writeEnd(); diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java index 29113729..88543bb1 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDatafile.java @@ -11,16 +11,14 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.TimerTask; -import jakarta.json.stream.JsonGenerator; +import java.util.TimerTask; import org.icatproject.Dataset; - import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.exceptions.InternalException; -import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatafileInfo; +import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.services.IcatReader; @@ -32,43 +30,40 @@ import org.icatproject.ids.thread.DfRestorer; import org.icatproject.ids.thread.DfWriter; -public class FiniteStateMachineForStorageUnitDatafile - extends FiniteStateMachine { +import jakarta.json.stream.JsonGenerator; + +public class FiniteStateMachineForStorageUnitDatafile extends FiniteStateMachine { - protected FiniteStateMachineForStorageUnitDatafile(IcatReader icatReader, - LockManager lockManager) { + protected FiniteStateMachineForStorageUnitDatafile(IcatReader icatReader, LockManager lockManager) { super(icatReader, lockManager); } + @Override protected void scheduleTimer() { - processOpsDelayMillis = propertyHandler.getDelayDatafileOperations() - * 1000L; + processOpsDelayMillis = propertyHandler.getDelayDatafileOperations() * 1000L; timer.schedule(new DfProcessQueue(), processQueueIntervalMillis); - logger.info("DfProcessQueue scheduled to run in " - + processQueueIntervalMillis + " milliseconds"); + logger.info("DfProcessQueue scheduled to run in " + processQueueIntervalMillis + " milliseconds"); } + @Override protected void addDataInfoJson(JsonGenerator gen) { this.addDataInfoJsonFromDeferredOpsQueue(gen); } - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) - throws InternalException { + + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { var dfInfo = (DatafileInfo) dataInfo; - if (dfInfo == null) - throw new InternalException( - "DataInfoBase object could not be casted into a DataFileInfo. Did you handed over a DataSetInfo instead?"); + if(dfInfo == null) throw new InternalException("DataInfoBase object could not be casted into a DataFileInfo. Did you handed over a DataSetInfo instead?"); logger.info("Requesting " + deferredOp + " of datafile " + dfInfo); synchronized (deferredOpsQueue) { if (processOpsTime == null) { - processOpsTime = System.currentTimeMillis() - + processOpsDelayMillis; + processOpsTime = System.currentTimeMillis() + processOpsDelayMillis; final Date d = new Date(processOpsTime); logger.debug("Requesting delay operations till " + d); } @@ -77,75 +72,62 @@ public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) if (state == null) { if (deferredOp == DeferredOp.WRITE) { try { - Path marker = markerDir - .resolve(Long.toString(dfInfo.getId())); + Path marker = markerDir.resolve(Long.toString(dfInfo.getId())); Files.createFile(marker); logger.debug("Created marker " + marker); } catch (FileAlreadyExistsException e) { // Pass will ignore this } catch (IOException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } - deferredOpsQueue.put(dfInfo, - RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.WRITE_REQUESTED); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, - RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dfInfo, - RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.RESTORE_REQUESTED); } else if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, - RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); } } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.RESTORE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, - RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); } } else if (state == RequestedState.DELETE_REQUESTED) { // No way out } else if (state == RequestedState.RESTORE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { - deferredOpsQueue.put(dfInfo, - RequestedState.DELETE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.DELETE_REQUESTED); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, - RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dfInfo, - RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.DELETE) { deferredOpsQueue.remove(dfInfo); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dfInfo, - RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put(dfInfo, RequestedState.WRITE_REQUESTED); } } } } + private class DfProcessQueue extends TimerTask { @Override public void run() { try { synchronized (deferredOpsQueue) { - if (processOpsTime != null - && System.currentTimeMillis() > processOpsTime - && !deferredOpsQueue.isEmpty()) { + if (processOpsTime != null && System.currentTimeMillis() > processOpsTime && !deferredOpsQueue.isEmpty()) { processOpsTime = null; - logger.debug("deferredDfOpsQueue has " - + deferredOpsQueue.size() + " entries"); + logger.debug("deferredDfOpsQueue has " + deferredOpsQueue.size() + " entries"); List writes = new ArrayList<>(); List archives = new ArrayList<>(); List restores = new ArrayList<>(); @@ -156,27 +138,20 @@ public void run() { Map deleteLocks = new HashMap<>(); Map newOps = new HashMap<>(); - final Iterator> it = deferredOpsQueue - .entrySet().iterator(); + final Iterator> it = deferredOpsQueue.entrySet().iterator(); while (it.hasNext()) { - Entry opEntry = it - .next(); + Entry opEntry = it.next(); var dfInfo = (DatafileInfo) opEntry.getKey(); - if (dfInfo == null) - throw new RuntimeException( - "Could not cast DataInfoBase to DataFileInfo. Did you handed over another sub type?"); + if(dfInfo == null) throw new RuntimeException("Could not cast DataInfoBase to DataFileInfo. Did you handed over another sub type?"); Long dsId = dfInfo.getDsId(); DatasetInfo dsInfo; try { - Dataset ds = (Dataset) reader.get( - "Dataset ds INCLUDE ds.investigation.facility", - dsId); + Dataset ds = (Dataset) reader.get("Dataset ds INCLUDE ds.investigation.facility", dsId); dsInfo = new DatasetInfo(ds); } catch (Exception e) { - logger.error("Could not get dsInfo {}: {}.", - dsId, e.getMessage()); + logger.error("Could not get dsInfo {}: {}.", dsId, e.getMessage()); continue; } if (!dataInfoChanging.containsKey(dfInfo)) { @@ -185,20 +160,12 @@ public void run() { if (state == RequestedState.WRITE_REQUESTED) { if (!writeLocks.containsKey(dsId)) { try { - writeLocks.put(dsId, - lockManager.lock(dsInfo, - LockType.SHARED)); + writeLocks.put(dsId, lockManager.lock(dsInfo, LockType.SHARED)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " - + dsId - + ", hold back " - + state); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error("I/O exception " - + e.getMessage() - + " locking " + dsId); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } @@ -208,46 +175,28 @@ public void run() { } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (!writeLocks.containsKey(dsId)) { try { - writeLocks.put(dsId, - lockManager.lock(dsInfo, - LockType.SHARED)); + writeLocks.put(dsId, lockManager.lock(dsInfo, LockType.SHARED)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " - + dsId - + ", hold back " - + state); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error("I/O exception " - + e.getMessage() - + " locking " + dsId); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } it.remove(); - dataInfoChanging.put(dfInfo, - RequestedState.WRITE_REQUESTED); + dataInfoChanging.put(dfInfo, RequestedState.WRITE_REQUESTED); writes.add(dfInfo); - newOps.put(dfInfo, - RequestedState.ARCHIVE_REQUESTED); + newOps.put(dfInfo, RequestedState.ARCHIVE_REQUESTED); } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (!archiveLocks.containsKey(dsId)) { try { - archiveLocks.put(dsId, - lockManager.lock(dsInfo, - LockType.EXCLUSIVE)); + archiveLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " - + dsId - + ", hold back " - + state); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error("I/O exception " - + e.getMessage() - + " locking " + dsId); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } @@ -257,20 +206,12 @@ public void run() { } else if (state == RequestedState.RESTORE_REQUESTED) { if (!restoreLocks.containsKey(dsId)) { try { - restoreLocks.put(dsId, - lockManager.lock(dsInfo, - LockType.EXCLUSIVE)); + restoreLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " - + dsId - + ", hold back " - + state); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error("I/O exception " - + e.getMessage() - + " locking " + dsId); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } @@ -280,20 +221,12 @@ public void run() { } else if (state == RequestedState.DELETE_REQUESTED) { if (!deleteLocks.containsKey(dsId)) { try { - deleteLocks.put(dsId, - lockManager.lock(dsInfo, - LockType.EXCLUSIVE)); + deleteLocks.put(dsId, lockManager.lock(dsInfo, LockType.EXCLUSIVE)); } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " - + dsId - + ", hold back " - + state); + logger.debug("Could not acquire lock on " + dsId + ", hold back " + state); continue; } catch (IOException e) { - logger.error("I/O exception " - + e.getMessage() - + " locking " + dsId); + logger.error("I/O exception " + e.getMessage() + " locking " + dsId); continue; } } @@ -301,61 +234,41 @@ public void run() { dataInfoChanging.put(dfInfo, state); deletes.add(dfInfo); } else { - throw new AssertionError( - "Impossible state"); + throw new AssertionError("Impossible state"); } } } if (!newOps.isEmpty()) { deferredOpsQueue.putAll(newOps); - logger.debug( - "Adding {} operations to be scheduled next time round", - newOps.size()); + logger.debug("Adding {} operations to be scheduled next time round", newOps.size()); } if (!deferredOpsQueue.isEmpty()) { processOpsTime = 0L; } if (!writes.isEmpty()) { - logger.debug("Launch thread to process " - + writes.size() + " writes"); - Thread w = new Thread(new DfWriter(writes, - propertyHandler, - FiniteStateMachineForStorageUnitDatafile.this, - writeLocks.values())); + logger.debug("Launch thread to process " + writes.size() + " writes"); + Thread w = new Thread(new DfWriter(writes, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, writeLocks.values())); w.start(); } if (!archives.isEmpty()) { - logger.debug("Launch thread to process " - + archives.size() + " archives"); - Thread w = new Thread(new DfArchiver(archives, - propertyHandler, - FiniteStateMachineForStorageUnitDatafile.this, - archiveLocks.values())); + logger.debug("Launch thread to process " + archives.size() + " archives"); + Thread w = new Thread(new DfArchiver(archives, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, archiveLocks.values())); w.start(); } if (!restores.isEmpty()) { - logger.debug("Launch thread to process " - + restores.size() + " restores"); - Thread w = new Thread(new DfRestorer(restores, - propertyHandler, - FiniteStateMachineForStorageUnitDatafile.this, - restoreLocks.values())); + logger.debug("Launch thread to process " + restores.size() + " restores"); + Thread w = new Thread(new DfRestorer(restores, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, restoreLocks.values())); w.start(); } if (!deletes.isEmpty()) { - logger.debug("Launch thread to process " - + deletes.size() + " deletes"); - Thread w = new Thread(new DfDeleter(deletes, - propertyHandler, - FiniteStateMachineForStorageUnitDatafile.this, - deleteLocks.values())); + logger.debug("Launch thread to process " + deletes.size() + " deletes"); + Thread w = new Thread(new DfDeleter(deletes, propertyHandler, FiniteStateMachineForStorageUnitDatafile.this, deleteLocks.values())); w.start(); } } } } finally { - timer.schedule(new DfProcessQueue(), - processQueueIntervalMillis); + timer.schedule(new DfProcessQueue(), processQueueIntervalMillis); } } diff --git a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java index 6eeebfb7..cbe0a5cd 100644 --- a/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java +++ b/src/main/java/org/icatproject/ids/finiteStateMachine/FiniteStateMachineForStorageUnitDataset.java @@ -11,8 +11,6 @@ import java.util.Map.Entry; import java.util.TimerTask; -import jakarta.json.stream.JsonGenerator; - import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; @@ -26,22 +24,22 @@ import org.icatproject.ids.thread.DsRestorer; import org.icatproject.ids.thread.DsWriter; -public class FiniteStateMachineForStorageUnitDataset - extends FiniteStateMachine { +import jakarta.json.stream.JsonGenerator; + +public class FiniteStateMachineForStorageUnitDataset extends FiniteStateMachine { protected Map writeTimes = new HashMap<>(); - protected FiniteStateMachineForStorageUnitDataset(IcatReader reader, - LockManager lockManager) { + protected FiniteStateMachineForStorageUnitDataset(IcatReader reader, LockManager lockManager) { super(reader, lockManager); } + @Override protected void scheduleTimer() { processOpsDelayMillis = propertyHandler.getDelayDatasetWrites() * 1000L; timer.schedule(new DsProcessQueue(), processQueueIntervalMillis); - logger.info("DsProcessQueue scheduled to run in " - + processQueueIntervalMillis + " milliseconds"); + logger.info("DsProcessQueue scheduled to run in " + processQueueIntervalMillis + " milliseconds"); } @Override @@ -49,8 +47,9 @@ protected void addDataInfoJson(JsonGenerator gen) { this.addDataInfoJsonFromDeferredOpsQueue(gen); } - public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) - throws InternalException { + + public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) throws InternalException { + logger.info("Requesting " + deferredOp + " of dataset " + dataInfo); @@ -61,47 +60,41 @@ public void queue(DataInfoBase dataInfo, DeferredOp deferredOp) if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, - RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, - RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.RESTORE_REQUESTED); } } else if (state == RequestedState.ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); - deferredOpsQueue.put(dataInfo, - RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, - RequestedState.RESTORE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.RESTORE_REQUESTED); } } else if (state == RequestedState.RESTORE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { requestWrite(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, - RequestedState.ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { setDelay(dataInfo); } else if (deferredOp == DeferredOp.ARCHIVE) { - deferredOpsQueue.put(dataInfo, - RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.WRITE_THEN_ARCHIVE_REQUESTED); } } else if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (deferredOp == DeferredOp.WRITE) { setDelay(dataInfo); } else if (deferredOp == DeferredOp.RESTORE) { - deferredOpsQueue.put(dataInfo, - RequestedState.WRITE_REQUESTED); + deferredOpsQueue.put(dataInfo, RequestedState.WRITE_REQUESTED); } } } } + private void requestWrite(DataInfoBase dsInfo) throws InternalException { try { Path marker = markerDir.resolve(Long.toString(dsInfo.getId())); @@ -116,16 +109,16 @@ private void requestWrite(DataInfoBase dsInfo) throws InternalException { setDelay(dsInfo); } + private void setDelay(DataInfoBase dsInfo) { - writeTimes.put(dsInfo, - System.currentTimeMillis() + processOpsDelayMillis); + writeTimes.put(dsInfo, System.currentTimeMillis() + processOpsDelayMillis); if (logger.isDebugEnabled()) { final Date d = new Date(writeTimes.get(dsInfo)); - logger.debug("Requesting delay of writing of dataset " + dsInfo - + " till " + d); + logger.debug("Requesting delay of writing of dataset " + dsInfo + " till " + d); } } + private class DsProcessQueue extends TimerTask { @Override @@ -134,16 +127,13 @@ public void run() { synchronized (deferredOpsQueue) { final long now = System.currentTimeMillis(); Map newOps = new HashMap<>(); - final Iterator> it = deferredOpsQueue - .entrySet().iterator(); + final Iterator> it = deferredOpsQueue.entrySet().iterator(); while (it.hasNext()) { - final Entry opEntry = it - .next(); + final Entry opEntry = it.next(); final var dsInfo = (DatasetInfo) opEntry.getKey(); - if (dsInfo == null) - throw new RuntimeException( - "Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); + + if(dsInfo == null) throw new RuntimeException("Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); if (!dataInfoChanging.containsKey(dsInfo)) { final RequestedState state = opEntry.getValue(); @@ -151,81 +141,50 @@ public void run() { || state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { if (now > writeTimes.get(dsInfo)) { try { - Lock lock = lockManager.lock(dsInfo, - LockType.SHARED); - logger.debug("Will process " + dsInfo - + " with " + state); + Lock lock = lockManager.lock(dsInfo, LockType.SHARED); + logger.debug("Will process " + dsInfo + " with " + state); writeTimes.remove(dsInfo); - dataInfoChanging.put(dsInfo, - RequestedState.WRITE_REQUESTED); + dataInfoChanging.put(dsInfo, RequestedState.WRITE_REQUESTED); it.remove(); final Thread w = new Thread( - new DsWriter(dsInfo, - propertyHandler, - FiniteStateMachineForStorageUnitDataset.this, - reader, lock)); + new DsWriter(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, reader, lock)); w.start(); if (state == RequestedState.WRITE_THEN_ARCHIVE_REQUESTED) { - newOps.put(dsInfo, - RequestedState.ARCHIVE_REQUESTED); + newOps.put(dsInfo, RequestedState.ARCHIVE_REQUESTED); } } catch (AlreadyLockedException e) { - logger.debug( - "Could not acquire lock on " - + dsInfo - + ", hold back process with " - + state); + logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); } catch (IOException e) { - logger.error("I/O exception " - + e.getMessage() + " locking " - + dsInfo); + logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); } } } else if (state == RequestedState.ARCHIVE_REQUESTED) { try { - Lock lock = lockManager.lock(dsInfo, - LockType.EXCLUSIVE); + Lock lock = lockManager.lock(dsInfo, LockType.EXCLUSIVE); it.remove(); - logger.debug("Will process " + dsInfo - + " with " + state); + logger.debug("Will process " + dsInfo + " with " + state); dataInfoChanging.put(dsInfo, state); - final Thread w = new Thread(new DsArchiver( - dsInfo, propertyHandler, - FiniteStateMachineForStorageUnitDataset.this, - lock)); + final Thread w = new Thread( + new DsArchiver(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, lock)); w.start(); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " - + dsInfo - + ", hold back process with " - + state); + logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); } catch (IOException e) { - logger.error( - "I/O exception " + e.getMessage() - + " locking " + dsInfo); + logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); } } else if (state == RequestedState.RESTORE_REQUESTED) { try { - Lock lock = lockManager.lock(dsInfo, - LockType.EXCLUSIVE); - logger.debug("Will process " + dsInfo - + " with " + state); + Lock lock = lockManager.lock(dsInfo, LockType.EXCLUSIVE); + logger.debug("Will process " + dsInfo + " with " + state); dataInfoChanging.put(dsInfo, state); it.remove(); - final Thread w = new Thread(new DsRestorer( - dsInfo, propertyHandler, - FiniteStateMachineForStorageUnitDataset.this, - reader, lock)); + final Thread w = new Thread( + new DsRestorer(dsInfo, propertyHandler, FiniteStateMachineForStorageUnitDataset.this, reader, lock)); w.start(); } catch (AlreadyLockedException e) { - logger.debug("Could not acquire lock on " - + dsInfo - + ", hold back process with " - + state); + logger.debug("Could not acquire lock on " + dsInfo + ", hold back process with " + state); } catch (IOException e) { - logger.error( - "I/O exception " + e.getMessage() - + " locking " + dsInfo); + logger.error("I/O exception " + e.getMessage() + " locking " + dsInfo); } } } @@ -234,8 +193,7 @@ public void run() { } } finally { - timer.schedule(new DsProcessQueue(), - processQueueIntervalMillis); + timer.schedule(new DsProcessQueue(), processQueueIntervalMillis); } } diff --git a/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java b/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java index 8a29aec2..8c813be8 100644 --- a/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java +++ b/src/main/java/org/icatproject/ids/helpers/CORSResponseFilter.java @@ -11,8 +11,8 @@ @Provider public class CORSResponseFilter implements ContainerResponseFilter { - public void filter(ContainerRequestContext requestContext, - ContainerResponseContext responseContext) throws IOException { + public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) + throws IOException { MultivaluedMap headers = responseContext.getHeaders(); headers.add("Access-Control-Allow-Origin", "*"); diff --git a/src/main/java/org/icatproject/ids/helpers/Constants.java b/src/main/java/org/icatproject/ids/helpers/Constants.java index 298205bf..bc7d4aaa 100644 --- a/src/main/java/org/icatproject/ids/helpers/Constants.java +++ b/src/main/java/org/icatproject/ids/helpers/Constants.java @@ -9,8 +9,8 @@ public class Constants { static { - InputStream inputStream = Constants.class.getClassLoader() - .getResourceAsStream("app.properties"); + InputStream inputStream = Constants.class.getClassLoader().getResourceAsStream( + "app.properties"); Properties p = new Properties(); try { diff --git a/src/main/java/org/icatproject/ids/helpers/LocationHelper.java b/src/main/java/org/icatproject/ids/helpers/LocationHelper.java index b4149116..b3c1e8b5 100644 --- a/src/main/java/org/icatproject/ids/helpers/LocationHelper.java +++ b/src/main/java/org/icatproject/ids/helpers/LocationHelper.java @@ -2,11 +2,10 @@ import java.security.NoSuchAlgorithmException; -import org.icatproject.utils.IcatSecurity; - import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.services.ServiceProvider; +import org.icatproject.utils.IcatSecurity; public class LocationHelper { @@ -24,8 +23,8 @@ public static String getLocation(long dfid, String location) } } - public static String getLocationFromDigest(long id, String locationWithHash, - String key) + + public static String getLocationFromDigest(long id, String locationWithHash, String key) throws InternalException, InsufficientPrivilegesException { int i = locationWithHash.lastIndexOf(' '); try { @@ -33,13 +32,11 @@ public static String getLocationFromDigest(long id, String locationWithHash, String hash = locationWithHash.substring(i + 1); if (!hash.equals(IcatSecurity.digest(id, location, key))) { throw new InsufficientPrivilegesException( - "Location \"" + locationWithHash - + "\" does not contain a valid hash."); + "Location \"" + locationWithHash + "\" does not contain a valid hash."); } return location; } catch (IndexOutOfBoundsException e) { - throw new InsufficientPrivilegesException("Location \"" - + locationWithHash + "\" does not contain hash."); + throw new InsufficientPrivilegesException("Location \"" + locationWithHash + "\" does not contain hash."); } catch (NoSuchAlgorithmException e) { throw new InternalException(e.getMessage()); } diff --git a/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java b/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java index 208d39a5..ffaa34be 100644 --- a/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java +++ b/src/main/java/org/icatproject/ids/helpers/RangeOutputStream.java @@ -21,10 +21,10 @@ public class RangeOutputStream extends FilterOutputStream { * fixed number of bytes following some offset. * * @param os the stream to wrap - * @param min the number of bytes to skip. It may larger than the number - * of bytes in the stream. - * @param count restrict to transmit only this number of bytes. The value - * null indicates that all bytes after the offset should be + * @param min the number of bytes to skip. It may larger than the number of + * bytes in the stream. + * @param count restrict to transmit only this number of bytes. The value null + * indicates that all bytes after the offset should be * transmitted. */ public RangeOutputStream(OutputStream os, long min, Long count) { diff --git a/src/main/java/org/icatproject/ids/helpers/SO.java b/src/main/java/org/icatproject/ids/helpers/SO.java index 848fe6c4..58e1ee4a 100644 --- a/src/main/java/org/icatproject/ids/helpers/SO.java +++ b/src/main/java/org/icatproject/ids/helpers/SO.java @@ -10,18 +10,17 @@ import java.util.zip.ZipException; import java.util.zip.ZipOutputStream; -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; -import jakarta.ws.rs.core.StreamingOutput; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatafileInfo; +import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.ServiceProvider; +import org.icatproject.ids.services.LockManager.Lock; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; +import jakarta.ws.rs.core.StreamingOutput; public class SO implements StreamingOutput { @@ -39,10 +38,8 @@ public class SO implements StreamingOutput { private static final int BUFSIZ = 2048; private final static Logger logger = LoggerFactory.getLogger(SO.class); - public SO(Map dsInfos, Map dfInfos, - long offset, boolean zip, boolean compress, Lock lock, - Long transferId, String ip, long start, - ServiceProvider serviceProvider) { + public SO(Map dsInfos, Map dfInfos, long offset, boolean zip, boolean compress, + Lock lock, Long transferId, String ip, long start, ServiceProvider serviceProvider) { this.offset = offset; this.zip = zip; this.dsInfos = dsInfos; @@ -65,27 +62,21 @@ public void write(OutputStream output) throws IOException { } byte[] bytes = new byte[BUFSIZ]; if (zip) { - ZipOutputStream zos = new ZipOutputStream( - new BufferedOutputStream(output)); + ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(output)); if (!compress) { zos.setLevel(0); // Otherwise use default compression } - for (DataInfoBase dataInfo : dfInfos.values()) { + for ( DataInfoBase dataInfo : dfInfos.values()) { var dfInfo = (DatafileInfo) dataInfo; logger.debug("Adding " + dfInfo + " to zip"); transfer = dfInfo; - DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId()); - String entryName = this.serviceProvider.getPropertyHandler() - .getZipMapper() - .getFullEntryName((DatasetInfo) dsInfo, - (DatafileInfo) dfInfo); + DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId() ); + String entryName = this.serviceProvider.getPropertyHandler().getZipMapper().getFullEntryName((DatasetInfo)dsInfo, (DatafileInfo)dfInfo); InputStream stream = null; try { zos.putNextEntry(new ZipEntry(entryName)); - stream = this.serviceProvider.getMainStorage().get( - dfInfo.getLocation(), dfInfo.getCreateId(), - dfInfo.getModId()); + stream = this.serviceProvider.getMainStorage().get(dfInfo.getLocation(), dfInfo.getCreateId(), dfInfo.getModId()); int length; while ((length = stream.read(bytes)) >= 0) { zos.write(bytes, 0, length); @@ -100,11 +91,9 @@ public void write(OutputStream output) throws IOException { } zos.close(); } else { - DatafileInfo dfInfo = (DatafileInfo) dfInfos.values().iterator() - .next(); + DatafileInfo dfInfo = (DatafileInfo) dfInfos.values().iterator().next(); transfer = dfInfo; - InputStream stream = this.serviceProvider.getMainStorage().get( - dfInfo.getDfLocation(), dfInfo.getCreateId(), + InputStream stream = this.serviceProvider.getMainStorage().get(dfInfo.getDfLocation(), dfInfo.getCreateId(), dfInfo.getModId()); int length; while ((length = stream.read(bytes)) >= 0) { @@ -116,28 +105,23 @@ public void write(OutputStream output) throws IOException { if (transferId != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos) - .writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { gen.write("transferId", transferId); gen.writeEnd(); } - this.serviceProvider.getTransmitter().processMessage("getData", - ip, baos.toString(), start); + this.serviceProvider.getTransmitter().processMessage("getData", ip, baos.toString(), start); } } catch (IOException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos) - .writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { gen.write("transferId", transferId); gen.write("exceptionClass", e.getClass().toString()); gen.write("exceptionMessage", e.getMessage()); gen.writeEnd(); } - this.serviceProvider.getTransmitter().processMessage("getData", ip, - baos.toString(), start); - logger.error("Failed to stream " + transfer + " due to " - + e.getMessage()); + this.serviceProvider.getTransmitter().processMessage("getData", ip, baos.toString(), start); + logger.error("Failed to stream " + transfer + " due to " + e.getMessage()); throw e; } finally { lock.release(); diff --git a/src/main/java/org/icatproject/ids/helpers/ValueContainer.java b/src/main/java/org/icatproject/ids/helpers/ValueContainer.java index 2ebedf30..bb99efba 100644 --- a/src/main/java/org/icatproject/ids/helpers/ValueContainer.java +++ b/src/main/java/org/icatproject/ids/helpers/ValueContainer.java @@ -2,11 +2,11 @@ import java.io.InputStream; -import jakarta.ws.rs.core.Response; - import org.icatproject.ids.enums.ValueContainerType; import org.icatproject.ids.exceptions.InternalException; +import jakarta.ws.rs.core.Response; + /** * This class provides a container vor carrying different types of values */ @@ -22,16 +22,11 @@ private ValueContainer(Object value, ValueContainerType type) { /** * checks if the type of the contained value is the same as the typeToCheck - * * @param typeToCheck the type to be checked if the contained value is of * @throws InternalException if the types don't match an exception is thrown */ - private void checkType(ValueContainerType typeToCheck) - throws InternalException { - if (this.type != typeToCheck) - throw new InternalException( - "This ValueContainer ist not of the needed type " - + typeToCheck + " its type is " + this.type + "."); + private void checkType(ValueContainerType typeToCheck) throws InternalException { + if(this.type != typeToCheck) throw new InternalException("This ValueContainer ist not of the needed type " + typeToCheck + " its type is " + this.type + "."); } public static ValueContainer getInvalid() { @@ -52,7 +47,6 @@ private ValueContainer(Void value) { /** * Creates a ValueContainer of type int - * * @param value the value contained by the container */ public ValueContainer(int value) { @@ -61,7 +55,6 @@ public ValueContainer(int value) { /** * Creates a ValueContainer of type long - * * @param value the value contained by the container */ public ValueContainer(long value) { @@ -70,7 +63,6 @@ public ValueContainer(long value) { /** * Creates a ValueContainer of type String - * * @param value the value contained by the container */ public ValueContainer(String value) { @@ -79,7 +71,6 @@ public ValueContainer(String value) { /** * Creates a ValueContainer of type boolean - * * @param value the value contained by the container */ public ValueContainer(boolean value) { @@ -88,16 +79,14 @@ public ValueContainer(boolean value) { /** * Creates a ValueContainer of type Response - * * @param value the value contained by the container */ public ValueContainer(Response value) { this(value, ValueContainerType.RESPONSE); - } - + } + /** * Creates a ValueContainer of type InputStream - * * @param value the value contained by the container */ public ValueContainer(InputStream value) { @@ -106,7 +95,6 @@ public ValueContainer(InputStream value) { /** * Informs about the type of the contained value - * * @return */ public ValueContainerType getType() { @@ -115,10 +103,8 @@ public ValueContainerType getType() { /** * Tries to return the value of the type int. - * * @return - * @throws InternalException if the container has another type an exception - * will be thrown + * @throws InternalException if the container has another type an exception will be thrown */ public int getInt() throws InternalException { this.checkType(ValueContainerType.INT); @@ -127,10 +113,8 @@ public int getInt() throws InternalException { /** * Tries to return the value of the type long. - * * @return - * @throws InternalException if the container has another type an exception - * will be thrown + * @throws InternalException if the container has another type an exception will be thrown */ public long getLong() throws InternalException { this.checkType(ValueContainerType.LONG); @@ -139,10 +123,8 @@ public long getLong() throws InternalException { /** * Tries to return the value of the type boolean. - * * @return - * @throws InternalException if the container has another type an exception - * will be thrown + * @throws InternalException if the container has another type an exception will be thrown */ public boolean getBool() throws InternalException { this.checkType(ValueContainerType.BOOL); @@ -151,10 +133,8 @@ public boolean getBool() throws InternalException { /** * Tries to return the value of the type String. - * * @return - * @throws InternalException if the container has another type an exception - * will be thrown + * @throws InternalException if the container has another type an exception will be thrown */ public String getString() throws InternalException { this.checkType(ValueContainerType.STRING); @@ -163,10 +143,8 @@ public String getString() throws InternalException { /** * Tries to return the value of the type Response. - * * @return - * @throws InternalException if the container has another type an exception - * will be thrown + * @throws InternalException if the container has another type an exception will be thrown */ public Response getResponse() throws InternalException { this.checkType(ValueContainerType.RESPONSE); @@ -175,10 +153,8 @@ public Response getResponse() throws InternalException { /** * Tries to return the value of the type InputStream. - * * @return - * @throws InternalException if the container has another type an exception - * will be thrown + * @throws InternalException if the container has another type an exception will be thrown */ public InputStream getInputStream() throws InternalException { this.checkType(ValueContainerType.INPUTSTREAM); @@ -187,41 +163,27 @@ public InputStream getInputStream() throws InternalException { @Override public String toString() { - switch (this.type) { - case INVALID: - return "" + ValueContainerType.INVALID; - case VOID: - return "" + ValueContainerType.VOID; - case INT: - return "" + this.value; - case LONG: - return "" + this.value; - case BOOL: - return ((boolean) this.value ? "true" : "false"); - case STRING: - return (String) this.value; - case RESPONSE: - return "Response " + ((Response) this.value).toString(); - case INPUTSTREAM: - return "An InputStream which will be printed here to prevent it from closing (and maybe it is too long)."; - default: - throw new RuntimeException( - "Doesn't know how to make a String vom ValueContainer of type " - + this.type - + ". Please implement a new case is ValueContainer.toString()."); + switch(this.type) { + case INVALID: return ""+ValueContainerType.INVALID; + case VOID: return ""+ValueContainerType.VOID; + case INT: return ""+this.value; + case LONG: return ""+this.value; + case BOOL: return ((boolean)this.value ? "true" : "false"); + case STRING: return (String)this.value; + case RESPONSE: return "Response " + ((Response) this.value).toString(); + case INPUTSTREAM: return "An InputStream which will be printed here to prevent it from closing (and maybe it is too long)."; + default: + throw new RuntimeException("Doesn't know how to make a String vom ValueContainer of type " + this.type + ". Please implement a new case is ValueContainer.toString()."); } } - public boolean isNull() { - return this.isInvalid() && this.value == null; - } + public boolean isNull() { return this.isInvalid() && this.value == null; } - public boolean isInvalid() { - return this.type == ValueContainerType.INVALID; - } + public boolean isInvalid() { return this.type == ValueContainerType.INVALID; } + + public boolean isVoid() { return this.type == ValueContainerType.VOID; } + + - public boolean isVoid() { - return this.type == ValueContainerType.VOID; - } } diff --git a/src/main/java/org/icatproject/ids/models/DataInfoBase.java b/src/main/java/org/icatproject/ids/models/DataInfoBase.java index 57adf664..603a3e7e 100644 --- a/src/main/java/org/icatproject/ids/models/DataInfoBase.java +++ b/src/main/java/org/icatproject/ids/models/DataInfoBase.java @@ -9,7 +9,7 @@ public abstract class DataInfoBase { protected String name; protected String location; - protected DataInfoBase(long id, String name, String location) { + protected DataInfoBase(long id, String name, String location){ this.name = name; this.id = id; this.location = location; @@ -42,5 +42,5 @@ public boolean equals(Object obj) { } return this.id == ((DataInfoBase) obj).getId(); } - + } diff --git a/src/main/java/org/icatproject/ids/models/DataSelection.java b/src/main/java/org/icatproject/ids/models/DataSelection.java index ffb8aa77..1bc59344 100644 --- a/src/main/java/org/icatproject/ids/models/DataSelection.java +++ b/src/main/java/org/icatproject/ids/models/DataSelection.java @@ -15,10 +15,10 @@ public class DataSelection { protected Boolean compress; private long length; - public DataSelection(SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, - Boolean zip, Boolean compress) { + + + public DataSelection(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress) { this.dsInfos = dsInfos; this.dfInfos = dfInfos; @@ -31,10 +31,12 @@ public DataSelection(SortedMap dsInfos, this.compress = compress; } + public SortedMap getDsInfo() { return dsInfos; } + public SortedMap getDfInfo() { return dfInfos; } @@ -51,10 +53,11 @@ public long getLength() { return this.length; } + public boolean mustZip() { // if(this.zip == null) { - return dfids.size() > 1L || !dsids.isEmpty() || !invids.isEmpty() - || (dfids.isEmpty() && dsids.isEmpty() && invids.isEmpty()); + return dfids.size() > 1L || !dsids.isEmpty() || !invids.isEmpty() + || (dfids.isEmpty() && dsids.isEmpty() && invids.isEmpty()); // } // return this.zip; @@ -64,6 +67,7 @@ public boolean isSingleDataset() { return dfids.isEmpty() && dsids.size() == 1 && invids.isEmpty(); } + public Set getEmptyDatasets() { return emptyDatasets; } diff --git a/src/main/java/org/icatproject/ids/models/DatafileInfo.java b/src/main/java/org/icatproject/ids/models/DatafileInfo.java index 4fa20cfd..789cfa79 100644 --- a/src/main/java/org/icatproject/ids/models/DatafileInfo.java +++ b/src/main/java/org/icatproject/ids/models/DatafileInfo.java @@ -3,18 +3,16 @@ import org.icatproject.ids.plugin.DfInfo; /** - * Contains information about a Datafile. Replaces DsInfo in v3 May should - * implement DfInfo interface + * Contains information about a Datafile. Replaces DsInfo in v3 + * May should implement DfInfo interface */ -public class DatafileInfo extends DataInfoBase - implements Comparable, DfInfo { +public class DatafileInfo extends DataInfoBase implements Comparable, DfInfo { protected String createId; - protected String modId; + protected String modId; protected long datasId; - public DatafileInfo(Long id, String name, String location, String createId, - String modId, Long datasId) { + public DatafileInfo(Long id, String name, String location, String createId, String modId, Long datasId) { super(id, name, location); this.createId = createId; @@ -52,17 +50,9 @@ public int compareTo(DatafileInfo o) { // implementing DfInfo @Override - public Long getDfId() { - return this.getId(); - } - + public Long getDfId() { return this.getId(); } @Override - public String getDfLocation() { - return this.getLocation(); - } - + public String getDfLocation() { return this.getLocation(); } @Override - public String getDfName() { - return this.getName(); - } + public String getDfName() { return this.getName(); } } diff --git a/src/main/java/org/icatproject/ids/models/DatasetInfo.java b/src/main/java/org/icatproject/ids/models/DatasetInfo.java index 72c797a8..be4d9a77 100644 --- a/src/main/java/org/icatproject/ids/models/DatasetInfo.java +++ b/src/main/java/org/icatproject/ids/models/DatasetInfo.java @@ -3,13 +3,12 @@ import org.icatproject.Dataset; import org.icatproject.Facility; import org.icatproject.Investigation; - import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.plugin.DsInfo; /** - * Contains information about a Dataset. Replaces DsInfo in v3. May should - * implement DsInfo interface + * Contains information about a Dataset. Replaces DsInfo in v3. + * May should implement DsInfo interface */ public class DatasetInfo extends DataInfoBase implements DsInfo { @@ -19,14 +18,14 @@ public class DatasetInfo extends DataInfoBase implements DsInfo { protected String investigationName; protected String visitId; + public DatasetInfo(Dataset ds) throws InsufficientPrivilegesException { - super(ds.getId(), ds.getName(), ds.getLocation()); + super(ds.getId(), ds.getName(), ds.getLocation()); Investigation investigation = ds.getInvestigation(); if (investigation == null) { throw new InsufficientPrivilegesException( - "Probably not able to read Investigation for dataset id " - + ds.getId()); + "Probably not able to read Investigation for dataset id " + ds.getId()); } Facility facility = investigation.getFacility(); if (facility == null) { @@ -42,9 +41,8 @@ public DatasetInfo(Dataset ds) throws InsufficientPrivilegesException { this.facilityName = facility.getName(); } - public DatasetInfo(Long id, String name, String location, - Long investigationId, String investigationName, String visitId, - Long facilityId, String facilityName) { + + public DatasetInfo(Long id, String name, String location, Long investigationId, String investigationName, String visitId, Long facilityId, String facilityName) { super(id, name, location); this.facilityId = facilityId; @@ -56,8 +54,7 @@ public DatasetInfo(Long id, String name, String location, @Override public String toString() { - return this.investigationId + "/" + this.id + " (" + this.facilityName - + "/" + this.investigationName + "/" + this.visitId + "/" + return this.investigationId + "/" + this.id + " (" + this.facilityName + "/" + this.investigationName + "/" + this.visitId + "/" + this.name + ")"; } @@ -81,31 +78,18 @@ public String getVisitId() { return visitId; } + // implementing DsInfo @Override - public Long getDsId() { - return this.getId(); - } - + public Long getDsId() { return this.getId(); } @Override - public String getDsName() { - return this.getName(); - } - + public String getDsName() { return this.getName(); } @Override - public String getDsLocation() { - return this.getLocation(); - } - + public String getDsLocation() { return this.getLocation(); } @Override - public Long getInvId() { - return this.getInvestigationId(); - } - + public Long getInvId() { return this.getInvestigationId(); } @Override - public String getInvName() { - return this.getInvestigationName(); - } + public String getInvName() { return this.getInvestigationName(); } } diff --git a/src/main/java/org/icatproject/ids/models/Prepared.java b/src/main/java/org/icatproject/ids/models/Prepared.java index 0f5bcc21..5e04d830 100644 --- a/src/main/java/org/icatproject/ids/models/Prepared.java +++ b/src/main/java/org/icatproject/ids/models/Prepared.java @@ -8,6 +8,11 @@ import java.util.SortedMap; import java.util.TreeMap; +import org.icatproject.ids.exceptions.InternalException; +import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import jakarta.json.Json; import jakarta.json.JsonNumber; import jakarta.json.JsonObject; @@ -15,18 +20,12 @@ import jakarta.json.JsonValue; import jakarta.json.stream.JsonGenerator; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import org.icatproject.ids.exceptions.InternalException; -import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; - /* This is a POJO with only package access so don't make data private */ public class Prepared { - protected final static Logger logger = LoggerFactory - .getLogger(RequestHandlerBase.class); + protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); + public SortedMap dsInfos; public SortedMap dfInfos; public Set emptyDatasets; @@ -34,9 +33,7 @@ public class Prepared { public boolean zip; public boolean compress; - public Prepared(SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - long fileLength) { + public Prepared(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength) { this.dsInfos = dsInfos; this.dfInfos = dfInfos; this.emptyDatasets = emptyDatasets; @@ -45,17 +42,15 @@ public Prepared(SortedMap dsInfos, this.compress = false; } - public Prepared(SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - long fileLength, Boolean zip, Boolean compress) { + public Prepared(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength, Boolean zip, Boolean compress) { this(dsInfos, dfInfos, emptyDatasets, fileLength); this.zip = zip; this.compress = compress; } - public static void pack(OutputStream stream, boolean zip, boolean compress, - Map dsInfos, Map dfInfos, - Set emptyDatasets, long fileLength) { + + public static void pack(OutputStream stream, boolean zip, boolean compress, Map dsInfos, + Map dfInfos, Set emptyDatasets, long fileLength) { JsonGenerator gen = Json.createGenerator(stream); gen.writeStartObject(); gen.write("zip", zip); @@ -64,16 +59,13 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, gen.writeStartArray("dsInfo"); for (DataInfoBase dataInfo : dsInfos.values()) { - var dsInfo = (DatasetInfo) dataInfo; + var dsInfo = (DatasetInfo)dataInfo; logger.debug("dsInfo " + dsInfo); gen.writeStartObject().write("dsId", dsInfo.getId()) - .write("dsName", dsInfo.getDsName()) - .write("facilityId", dsInfo.getFacilityId()) - .write("facilityName", dsInfo.getFacilityName()) - .write("invId", dsInfo.getInvId()) - .write("invName", dsInfo.getInvName()) - .write("visitId", dsInfo.getVisitId()); + .write("dsName", dsInfo.getDsName()).write("facilityId", dsInfo.getFacilityId()) + .write("facilityName", dsInfo.getFacilityName()).write("invId", dsInfo.getInvId()) + .write("invName", dsInfo.getInvName()).write("visitId", dsInfo.getVisitId()); if (dsInfo.getDsLocation() != null) { gen.write("dsLocation", dsInfo.getDsLocation()); } else { @@ -85,12 +77,10 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, gen.writeStartArray("dfInfo"); for (DataInfoBase dataInfo : dfInfos.values()) { - var dfInfo = (DatafileInfo) dataInfo; + var dfInfo = (DatafileInfo)dataInfo; DataInfoBase dsInfo = dsInfos.get(dfInfo.getDsId()); - gen.writeStartObject().write("dsId", dsInfo.getId()) - .write("dfId", dfInfo.getId()) - .write("dfName", dfInfo.getDfName()) - .write("createId", dfInfo.getCreateId()) + gen.writeStartObject().write("dsId", dsInfo.getId()).write("dfId", dfInfo.getId()) + .write("dfName", dfInfo.getDfName()).write("createId", dfInfo.getCreateId()) .write("modId", dfInfo.getModId()); if (dfInfo.getDfLocation() != null) { gen.write("dfLocation", dfInfo.getDfLocation()); @@ -112,6 +102,7 @@ public static void pack(OutputStream stream, boolean zip, boolean compress, } + public static Prepared unpack(InputStream stream) throws InternalException { JsonObject pd; @@ -120,42 +111,35 @@ public static Prepared unpack(InputStream stream) throws InternalException { } var zip = pd.getBoolean("zip"); var compress = pd.getBoolean("compress"); - var fileLength = pd.containsKey("fileLength") ? pd.getInt("fileLength") - : 0; + var fileLength = pd.containsKey("fileLength") ? pd.getInt("fileLength") : 0; SortedMap dsInfos = new TreeMap<>(); SortedMap dfInfos = new TreeMap<>(); Set emptyDatasets = new HashSet<>(); for (JsonValue itemV : pd.getJsonArray("dfInfo")) { JsonObject item = (JsonObject) itemV; - String dfLocation = item.isNull("dfLocation") ? null - : item.getString("dfLocation"); + String dfLocation = item.isNull("dfLocation") ? null : item.getString("dfLocation"); long dfid = item.getJsonNumber("dfId").longValueExact(); - dfInfos.put(dfid, - new DatafileInfo(dfid, item.getString("dfName"), dfLocation, - item.getString("createId"), item.getString("modId"), - item.getJsonNumber("dsId").longValueExact())); + dfInfos.put(dfid, new DatafileInfo(dfid, item.getString("dfName"), + dfLocation, item.getString("createId"), item.getString("modId"), + item.getJsonNumber("dsId").longValueExact())); } for (JsonValue itemV : pd.getJsonArray("dsInfo")) { JsonObject item = (JsonObject) itemV; long dsId = item.getJsonNumber("dsId").longValueExact(); - String dsLocation = item.isNull("dsLocation") ? null - : item.getString("dsLocation"); - dsInfos.put(dsId, new DatasetInfo(dsId, item.getString("dsName"), - dsLocation, item.getJsonNumber("invId").longValueExact(), - item.getString("invName"), item.getString("visitId"), - item.getJsonNumber("facilityId").longValueExact(), - item.getString("facilityName"))); + String dsLocation = item.isNull("dsLocation") ? null : item.getString("dsLocation"); + dsInfos.put(dsId, new DatasetInfo(dsId, item.getString("dsName"), dsLocation, + item.getJsonNumber("invId").longValueExact(), item.getString("invName"), item.getString("visitId"), + item.getJsonNumber("facilityId").longValueExact(), item.getString("facilityName"))); } for (JsonValue itemV : pd.getJsonArray("emptyDs")) { emptyDatasets.add(((JsonNumber) itemV).longValueExact()); } - return new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength, zip, - compress); + return new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength, zip, compress); } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java index df19f640..032e8437 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/ArchiveHandler.java @@ -1,5 +1,6 @@ package org.icatproject.ids.requestHandlers; + import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; @@ -11,17 +12,13 @@ public class ArchiveHandler extends DataRequestHandler { - public ArchiveHandler(String ip, String sessionId, String investigationIds, - String datasetIds, String datafileIds) { - super(RequestType.ARCHIVE, ip, sessionId, investigationIds, datasetIds, - datafileIds); + public ArchiveHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.ARCHIVE, ip, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws NotImplementedException, InternalException { - + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws NotImplementedException, InternalException { + dataSelectionService.scheduleTasks(DeferredOp.ARCHIVE); return ValueContainer.getVoid(); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java index b952f2b6..a4cb44a9 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/DeleteHandler.java @@ -8,7 +8,6 @@ import org.icatproject.EntityBaseBean; import org.icatproject.IcatExceptionType; import org.icatproject.IcatException_Exception; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.enums.StorageUnit; @@ -22,57 +21,48 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataRequestHandler; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; + public class DeleteHandler extends DataRequestHandler { - public DeleteHandler(String ip, String sessionId, String investigationIds, - String datasetIds, String datafileIds) { - super(RequestType.DELETE, ip, sessionId, investigationIds, datasetIds, - datafileIds); + public DeleteHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.DELETE, ip, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { if (readOnly) { - throw new NotImplementedException( - "This operation has been configured to be unavailable"); + throw new NotImplementedException("This operation has been configured to be unavailable"); } var serviceProvider = ServiceProvider.getInstance(); // Do it - try (Lock lock = serviceProvider.getLockManager().lock( - dataSelectionService.getDsInfo().values(), - LockType.EXCLUSIVE)) { + try (Lock lock = serviceProvider.getLockManager().lock(dataSelectionService.getDsInfo().values(), LockType.EXCLUSIVE)) { if (storageUnit == StorageUnit.DATASET) { dataSelectionService.checkOnline(); } /* Now delete from ICAT */ List dfs = new ArrayList<>(); - for (DataInfoBase dfInfo : dataSelectionService.getDfInfo() - .values()) { + for (DataInfoBase dfInfo : dataSelectionService.getDfInfo().values()) { Datafile df = new Datafile(); df.setId(dfInfo.getId()); dfs.add(df); } try { - serviceProvider.getIcat() - .deleteMany(this.dataController.getOperationId(), dfs); + serviceProvider.getIcat().deleteMany(this.dataController.getOperationId(), dfs); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES - || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java index 79d59221..7c22c38f 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetDataFileIdsHandler.java @@ -2,9 +2,6 @@ import java.io.ByteArrayOutputStream; -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -19,33 +16,30 @@ import org.icatproject.ids.requestHandlers.base.PreparedDataController; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; + public class GetDataFileIdsHandler extends DataRequestHandler { - public GetDataFileIdsHandler(String ip, String preparedId, String sessionId, - String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETDATAFILEIDS, ip, preparedId, sessionId, - investigationIds, datasetIds, datafileIds); + public GetDataFileIdsHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETDATAFILEIDS, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos) - .writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { - if (this.dataController instanceof PreparedDataController) { + if(this.dataController instanceof PreparedDataController) { gen.write("zip", dataSelectionService.getZip()); gen.write("compress", dataSelectionService.getCompress()); } gen.writeStartArray("ids"); - for (DataInfoBase dfInfo : dataSelectionService.getDfInfo() - .values()) { + for (DataInfoBase dfInfo : dataSelectionService.getDfInfo().values()) { gen.write(dfInfo.getId()); } gen.writeEnd().writeEnd().close(); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java index e2b03865..8cdb64ce 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetDataHandler.java @@ -1,7 +1,5 @@ package org.icatproject.ids.requestHandlers; -import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH; - import java.io.IOException; import java.net.HttpURLConnection; import java.text.SimpleDateFormat; @@ -12,11 +10,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import jakarta.json.stream.JsonGenerator; -import jakarta.ws.rs.core.Response; - import org.icatproject.IcatException_Exception; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -31,11 +25,15 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataRequestHandler; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; +import jakarta.json.stream.JsonGenerator; +import jakarta.ws.rs.core.Response; +import static jakarta.ws.rs.core.HttpHeaders.CONTENT_LENGTH; + public class GetDataHandler extends DataRequestHandler { private Pattern rangeRe; @@ -47,48 +45,40 @@ public class GetDataHandler extends DataRequestHandler { String range; Long transferId; - public GetDataHandler(String ip, String preparedId, String sessionId, - String investigationIds, String datasetIds, String datafileIds, - Boolean compress, Boolean zip, String outname, String range) { - super(RequestType.GETDATA, ip, preparedId, sessionId, investigationIds, - datasetIds, datafileIds); + public GetDataHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds, Boolean compress, Boolean zip, String outname, String range) { + super(RequestType.GETDATA, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds ); - this.initializeAdditionallParameters(sessionId, compress, zip, outname, - range); + this.initializeAdditionallParameters(sessionId, compress, zip, outname, range); } - private void initializeAdditionallParameters(String sessionId, - Boolean compress, Boolean zip, String outname, String range) { - + private void initializeAdditionallParameters(String sessionId, Boolean compress, Boolean zip, String outname, String range) { + this.outname = outname; this.range = range; this.transferId = null; this.rangeRe = Pattern.compile("bytes=(\\d+)-"); - if (sessionId != null) { + if(sessionId != null) { this.compress = compress; this.zip = zip; } } + @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) throws InternalException, - NotImplementedException, BadRequestException, NotFoundException, - InsufficientPrivilegesException, DataNotOnlineException { + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws InternalException, NotImplementedException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException { - if (this.compress == null || this.zip == null) { + if(this.compress == null || this.zip == null) { this.zip = dataSelectionService.getZip(); this.compress = dataSelectionService.getCompress(); } long offset = 0; - if (range != null) { + if ( range != null) { Matcher m = rangeRe.matcher(range); if (!m.matches()) { - throw new BadRequestException( - "The range must match " + rangeRe.pattern()); + throw new BadRequestException("The range must match " + rangeRe.pattern()); } offset = Long.parseLong(m.group(1)); logger.debug("Range " + range + " -> offset " + offset); @@ -97,24 +87,20 @@ public ValueContainer handleDataRequest( return new ValueContainer(this.getData(dataSelectionService, offset)); } - private Response getData(DataSelectionService dataSelectionService, - final long offset) throws BadRequestException, NotFoundException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { - long start = System.currentTimeMillis(); + private Response getData(DataSelectionService dataSelectionService, final long offset) throws BadRequestException, + NotFoundException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - var length = this.zip ? OptionalLong.empty() - : dataSelectionService.getFileLength(); + long start = System.currentTimeMillis(); + + var length = this.zip ? OptionalLong.empty() : dataSelectionService.getFileLength(); - final boolean finalZip = this.dataController.mustZip(zip, - dataSelectionService); + final boolean finalZip = this.dataController.mustZip(zip, dataSelectionService); Lock lock = null; try { var serviceProvider = ServiceProvider.getInstance(); - lock = serviceProvider.getLockManager().lock( - dataSelectionService.getDsInfo().values(), LockType.SHARED); + lock = serviceProvider.getLockManager().lock(dataSelectionService.getDsInfo().values(), LockType.SHARED); if (twoLevel) { dataSelectionService.checkOnline(); @@ -125,16 +111,13 @@ private Response getData(DataSelectionService dataSelectionService, String name; if (outname == null) { if (finalZip) { - name = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss") - .format(new Date()) + ".zip"; + name = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()) + ".zip"; } else { - name = dataSelectionService.getDfInfo().values().iterator() - .next().getName(); + name = dataSelectionService.getDfInfo().values().iterator().next().getName(); } } else { if (finalZip) { - String ext = outname.substring(outname.lastIndexOf(".") + 1, - outname.length()); + String ext = outname.substring(outname.lastIndexOf(".") + 1, outname.length()); if ("zip".equals(ext)) { name = outname; } else { @@ -145,24 +128,17 @@ private Response getData(DataSelectionService dataSelectionService, } } - if (ServiceProvider.getInstance().getPropertyHandler().getLogSet() - .contains(this.getCallType())) { + if (ServiceProvider.getInstance().getPropertyHandler().getLogSet().contains(this.getCallType())) { transferId = transferIdCounter.getAndIncrement(); } - var response = Response - .status(offset == 0 ? HttpURLConnection.HTTP_OK - : HttpURLConnection.HTTP_PARTIAL) - .entity(new SO(dataSelectionService.getDsInfo(), - dataSelectionService.getDfInfo(), offset, finalZip, - compress, lock, transferId, ip, start, - serviceProvider)) - .header("Content-Disposition", - "attachment; filename=\"" + name + "\"") - .header("Accept-Ranges", "bytes"); - length.stream().map(l -> Math.max(0L, l - offset)) + var response = Response.status(offset == 0 ? HttpURLConnection.HTTP_OK : HttpURLConnection.HTTP_PARTIAL) + .entity(new SO(dataSelectionService.getDsInfo(), dataSelectionService.getDfInfo(), offset, finalZip, compress, lock, transferId, ip, start, serviceProvider)) + .header("Content-Disposition", "attachment; filename=\"" + name + "\"").header("Accept-Ranges", "bytes"); + length.stream() + .map(l -> Math.max(0L, l - offset)) .forEach(l -> response.header(CONTENT_LENGTH, l)); - + return response.build(); } catch (AlreadyLockedException e) { @@ -170,7 +146,7 @@ private Response getData(DataSelectionService dataSelectionService, throw new DataNotOnlineException("Data is busy"); } catch (IOException e) { // if (lock != null) { - // lock.release(); + // lock.release(); // } logger.error("I/O error " + e.getMessage()); throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -180,48 +156,38 @@ private Response getData(DataSelectionService dataSelectionService, } } + private void checkDatafilesPresent(Collection dfInfos) throws NotFoundException, InternalException { var serviceProvider = ServiceProvider.getInstance(); /* Check that datafiles have not been deleted before locking */ int n = 0; - StringBuffer sb = new StringBuffer( - "SELECT COUNT(df) from Datafile df WHERE (df.id in ("); + StringBuffer sb = new StringBuffer("SELECT COUNT(df) from Datafile df WHERE (df.id in ("); for (DataInfoBase dfInfo : dfInfos) { if (n != 0) { sb.append(','); } sb.append(dfInfo.getId()); - if (++n == serviceProvider.getPropertyHandler() - .getMaxIdsInQuery()) { + if (++n == serviceProvider.getPropertyHandler().getMaxIdsInQuery()) { try { - if (((Long) serviceProvider.getIcatReader() - .search(sb.append("))").toString()).get(0)) - .intValue() != n) { - throw new NotFoundException( - "One of the data files requested has been deleted"); + if (((Long) serviceProvider.getIcatReader().search(sb.append("))").toString()).get(0)).intValue() != n) { + throw new NotFoundException("One of the data files requested has been deleted"); } n = 0; - sb = new StringBuffer( - "SELECT COUNT(df) from Datafile df WHERE (df.id in ("); + sb = new StringBuffer("SELECT COUNT(df) from Datafile df WHERE (df.id in ("); } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } } } if (n != 0) { try { - if (((Long) serviceProvider.getIcatReader() - .search(sb.append("))").toString()).get(0)) - .intValue() != n) { - throw new NotFoundException( - "One of the datafiles requested has been deleted"); + if (((Long) serviceProvider.getIcatReader().search(sb.append("))").toString()).get(0)).intValue() != n) { + throw new NotFoundException("One of the datafiles requested has been deleted"); } } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } } @@ -234,8 +200,7 @@ public CallType getCallType() { @Override public void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - if (transferId == null) - transferId = -1L; + if(transferId == null) transferId = -1L; gen.write("transferId", transferId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java index 70942624..94a78919 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetIcatUrlHandler.java @@ -12,6 +12,7 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; import org.icatproject.ids.services.ServiceProvider; + public class GetIcatUrlHandler extends RequestHandlerBase { public GetIcatUrlHandler(String ip) { @@ -19,12 +20,11 @@ public GetIcatUrlHandler(String ip) { } @Override - public ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { - var propertyHandler = ServiceProvider.getInstance() - .getPropertyHandler(); + var propertyHandler = ServiceProvider.getInstance().getPropertyHandler(); return new ValueContainer(propertyHandler.getIcatUrl()); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java index 04909b0b..713c3603 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetServiceStatusHandler.java @@ -2,11 +2,8 @@ import java.util.Set; -import jakarta.json.stream.JsonGenerator; - import org.icatproject.IcatExceptionType; import org.icatproject.IcatException_Exception; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -19,6 +16,8 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; import org.icatproject.ids.services.ServiceProvider; +import jakarta.json.stream.JsonGenerator; + public class GetServiceStatusHandler extends RequestHandlerBase { private Set rootUserNames; @@ -29,27 +28,24 @@ public GetServiceStatusHandler(String ip, String sessionId) { super(RequestType.GETSERVICESTATUS, ip); this.sessionId = sessionId; - rootUserNames = ServiceProvider.getInstance().getPropertyHandler() - .getRootUserNames(); + rootUserNames = ServiceProvider.getInstance().getPropertyHandler().getRootUserNames(); this.serviceProvider = ServiceProvider.getInstance(); } @Override - public ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { try { String uname = serviceProvider.getIcat().getUserName(sessionId); if (!rootUserNames.contains(uname)) { - throw new InsufficientPrivilegesException(uname - + " is not included in the ids rootUserNames set."); + throw new InsufficientPrivilegesException(uname + " is not included in the ids rootUserNames set."); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); if (type == IcatExceptionType.SESSION) { - throw new InsufficientPrivilegesException( - e.getClass() + " " + e.getMessage()); + throw new InsufficientPrivilegesException(e.getClass() + " " + e.getMessage()); } throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -58,8 +54,7 @@ public ValueContainer handleRequest() throws BadRequestException, } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { gen.write("userName", serviceProvider.getIcat().getUserName(sessionId)); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java index 8f4c145f..9da9787c 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/GetStatusHandler.java @@ -19,27 +19,21 @@ public class GetStatusHandler extends DataRequestHandler { - public GetStatusHandler(String ip, String preparedId, String sessionId, - String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETSTATUS, ip, preparedId, sessionId, - investigationIds, datasetIds, datafileIds); + public GetStatusHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETSTATUS, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { Status status = Status.ONLINE; var serviceProvider = ServiceProvider.getInstance(); Set restoring = serviceProvider.getFsm().getRestoring(); - Set maybeOffline = serviceProvider.getFsm() - .getMaybeOffline(); - for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos() - .values()) { + Set maybeOffline = serviceProvider.getFsm().getMaybeOffline(); + for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (restoring.contains(dataInfo)) { status = Status.RESTORING; diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java index 53660e58..6f2deba7 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsPreparedHandler.java @@ -34,23 +34,17 @@ class PreparedStatus { private Map preparedStatusMap = new ConcurrentHashMap<>(); @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { // Do it boolean prepared = true; - PreparedStatus status = preparedStatusMap.computeIfAbsent( - this.dataController.getOperationId(), - k -> new PreparedStatus()); + PreparedStatus status = preparedStatusMap.computeIfAbsent(this.dataController.getOperationId(), k -> new PreparedStatus()); if (!status.lock.tryLock()) { - logger.debug( - "Lock held for evaluation of isPrepared for preparedId {}", - this.dataController.getOperationId()); + logger.debug("Lock held for evaluation of isPrepared for preparedId {}", this.dataController.getOperationId()); return new ValueContainer(false); } try { @@ -60,23 +54,19 @@ public ValueContainer handleDataRequest( try { future.get(); } catch (ExecutionException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (InterruptedException e) { // Ignore } finally { status.future = null; } } else { - logger.debug( - "Background process still running for preparedId {}", - this.dataController.getOperationId()); + logger.debug("Background process still running for preparedId {}", this.dataController.getOperationId()); return new ValueContainer(false); } } - prepared = dataSelectionService - .isPrepared(this.dataController.getOperationId()); + prepared = dataSelectionService.isPrepared(this.dataController.getOperationId()); return new ValueContainer(prepared); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java index 15c93a26..a7acf20e 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsReadOnlyHandler.java @@ -12,16 +12,16 @@ import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; public class IsReadOnlyHandler extends RequestHandlerBase { - + public IsReadOnlyHandler(String ip) { super(RequestType.ISREADONLY, ip); } @Override - public ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { return new ValueContainer(this.readOnly); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java index 1ffd2326..f9cc3be1 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/IsTwoLevelHandler.java @@ -18,10 +18,10 @@ public IsTwoLevelHandler(String ip) { } @Override - public ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { - + public ValueContainer handleRequest() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { + return new ValueContainer(twoLevel); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java index dc7edad6..625c4ac2 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/PrepareDataHandler.java @@ -6,8 +6,6 @@ import java.nio.file.Files; import java.util.UUID; -import jakarta.json.stream.JsonGenerator; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestIdNames; import org.icatproject.ids.enums.RequestType; @@ -22,31 +20,28 @@ import org.icatproject.ids.requestHandlers.base.DataRequestHandler; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; +import jakarta.json.stream.JsonGenerator; + public class PrepareDataHandler extends DataRequestHandler { Boolean compress; Boolean zip; String preparedId; - public PrepareDataHandler(String ip, String sessionId, - String investigationIds, String datasetIds, String datafileIds, - Boolean compress, Boolean zip) { - super(RequestType.PREPAREDATA, ip, sessionId, investigationIds, - datasetIds, datafileIds); + public PrepareDataHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds, Boolean compress, Boolean zip) { + super(RequestType.PREPAREDATA,ip, sessionId, investigationIds, datasetIds, datafileIds); this.zip = zip; this.compress = compress; } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException { + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { preparedId = UUID.randomUUID().toString(); - + dataSelectionService.restoreDataInfos(); if (dataSelectionService.mustZip()) { @@ -54,14 +49,15 @@ public ValueContainer handleDataRequest( } logger.debug("Writing to " + preparedDir.resolve(preparedId)); - try (OutputStream stream = new BufferedOutputStream( - Files.newOutputStream(preparedDir.resolve(preparedId)))) { - Prepared.pack(stream, zip, compress, - dataSelectionService.getDsInfo(), - dataSelectionService.getDfInfo(), - dataSelectionService.getEmptyDatasets(), - dataSelectionService.getFileLength().isEmpty() ? 0 - : dataSelectionService.getFileLength().getAsLong()); + try (OutputStream stream = new BufferedOutputStream(Files.newOutputStream(preparedDir.resolve(preparedId)))) { + Prepared.pack( stream, + zip, + compress, + dataSelectionService.getDsInfo(), + dataSelectionService.getDfInfo(), + dataSelectionService.getEmptyDatasets(), + dataSelectionService.getFileLength().isEmpty() ? 0 : dataSelectionService.getFileLength().getAsLong() + ); } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -72,14 +68,13 @@ public ValueContainer handleDataRequest( } @Override - public String addCustomParametersToLogString() { + public String addCustomParametersToLogString() { return "zip='" + this.zip + "'' compress='" + compress + "'"; } @Override public void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - if (preparedId == null) - preparedId = ""; + if(preparedId == null) preparedId = ""; gen.write(RequestIdNames.preparedId, preparedId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java index 7574ca25..3e9bfbd9 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/PutHandler.java @@ -13,18 +13,12 @@ import java.util.zip.CRC32; import javax.xml.datatype.DatatypeFactory; -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; -import jakarta.ws.rs.core.Response; - import org.icatproject.Datafile; import org.icatproject.DatafileFormat; import org.icatproject.Dataset; import org.icatproject.ICAT; import org.icatproject.IcatExceptionType; import org.icatproject.IcatException_Exception; -import org.icatproject.utils.IcatSecurity; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestIdNames; @@ -39,17 +33,22 @@ import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.helpers.CheckedWithSizeInputStream; import org.icatproject.ids.helpers.ValueContainer; -import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatafileInfo; +import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataControllerBase; import org.icatproject.ids.requestHandlers.base.RequestHandlerBase; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; import org.icatproject.ids.services.dataSelectionService.DataSelectionServiceFactory; +import org.icatproject.utils.IcatSecurity; + +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; +import jakarta.ws.rs.core.Response; public class PutHandler extends RequestHandlerBase { @@ -80,10 +79,9 @@ public class PutHandler extends RequestHandlerBase { paddedPrefix += "*/window.name='"; } - public PutHandler(String ip, String sessionId, InputStream body, - String name, String datafileFormatIdString, String datasetIdString, - String description, String doi, String datafileCreateTimeString, - String datafileModTimeString, boolean wrap, boolean padding) { + public PutHandler( String ip, String sessionId, InputStream body, String name, String datafileFormatIdString, + String datasetIdString, String description, String doi, String datafileCreateTimeString, + String datafileModTimeString, boolean wrap, boolean padding) { super(RequestType.PUT, ip); this.sessionId = sessionId; @@ -107,61 +105,52 @@ public PutHandler(String ip, String sessionId, InputStream body, } catch (Throwable e) { logger.error("Won't start ", e); - throw new RuntimeException("PutHandler reports " + e.getClass() - + " " + e.getMessage()); + throw new RuntimeException("PutHandler reports " + e.getClass() + " " + e.getMessage()); } } @Override - public ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handleRequest() + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, + DataNotOnlineException, NotImplementedException { try { if (readOnly) { - throw new NotImplementedException( - "This operation has been configured to be unavailable"); + throw new NotImplementedException("This operation has been configured to be unavailable"); } - DataControllerBase.validateUUID(RequestIdNames.sessionId, - sessionId); + DataControllerBase.validateUUID(RequestIdNames.sessionId, sessionId); if (name == null) { throw new BadRequestException("The name parameter must be set"); } if (datafileFormatIdString == null) { - throw new BadRequestException( - "The datafileFormatId parameter must be set"); + throw new BadRequestException("The datafileFormatId parameter must be set"); } long datafileFormatId; try { datafileFormatId = Long.parseLong(datafileFormatIdString); } catch (NumberFormatException e) { - throw new BadRequestException( - "The datafileFormatId parameter must be numeric"); + throw new BadRequestException("The datafileFormatId parameter must be numeric"); } if (datasetIdString == null) { - throw new BadRequestException( - "The datasetId parameter must be set"); + throw new BadRequestException("The datasetId parameter must be set"); } long datasetId; try { datasetId = Long.parseLong(datasetIdString); } catch (NumberFormatException e) { - throw new BadRequestException( - "The datasetId parameter must be numeric"); + throw new BadRequestException("The datasetId parameter must be numeric"); } Long datafileCreateTime = null; if (datafileCreateTimeString != null) { try { - datafileCreateTime = Long - .parseLong(datafileCreateTimeString); + datafileCreateTime = Long.parseLong(datafileCreateTimeString); } catch (NumberFormatException e) { - throw new BadRequestException( - "The datafileCreateTime parameter must be numeric"); + throw new BadRequestException("The datafileCreateTime parameter must be numeric"); } } @@ -170,20 +159,17 @@ public ValueContainer handleRequest() throws BadRequestException, try { datafileModTime = Long.parseLong(datafileModTimeString); } catch (NumberFormatException e) { - throw new BadRequestException( - "The datafileModTime parameter must be numeric"); + throw new BadRequestException("The datafileModTime parameter must be numeric"); } } // Do it Dataset ds; try { - ds = (Dataset) serviceProvider.getIcat().get(sessionId, - "Dataset INCLUDE Investigation, Facility", datasetId); + ds = (Dataset) serviceProvider.getIcat().get(sessionId, "Dataset INCLUDE Investigation, Facility", datasetId); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES - || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -193,74 +179,59 @@ public ValueContainer handleRequest() throws BadRequestException, } DatasetInfo dsInfo = new DatasetInfo(ds); - try (Lock lock = serviceProvider.getLockManager().lock(dsInfo, - LockType.SHARED)) { + try (Lock lock = serviceProvider.getLockManager().lock(dsInfo, LockType.SHARED)) { if (storageUnit == StorageUnit.DATASET) { var dfInfos = new TreeMap(); Set emptyDatasets = new HashSet<>(); try { - List counts = serviceProvider.getIcat().search( - sessionId, "COUNT(Datafile) <-> Dataset [id=" - + dsInfo.getId() + "]"); + List counts = serviceProvider.getIcat().search(sessionId, + "COUNT(Datafile) <-> Dataset [id=" + dsInfo.getId() + "]"); if ((Long) counts.get(0) == 0) { emptyDatasets.add(dsInfo.getId()); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES - || type == IcatExceptionType.SESSION) { - throw new InsufficientPrivilegesException( - e.getMessage()); + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { + throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } - throw new InternalException( - type + " " + e.getMessage()); + throw new InternalException(type + " " + e.getMessage()); } var dsInfos = new TreeMap(); dsInfos.put(dsInfo.getId(), dsInfo); - DataSelectionService dataSelectionService = DataSelectionServiceFactory - .getService(dsInfos, dfInfos, emptyDatasets, 0, - this.getRequestType()); + DataSelectionService dataSelectionService = DataSelectionServiceFactory.getService(dsInfos, dfInfos, emptyDatasets, 0, this.getRequestType()); dataSelectionService.checkOnline(); } CRC32 crc = new CRC32(); - CheckedWithSizeInputStream is = new CheckedWithSizeInputStream( - body, crc); + CheckedWithSizeInputStream is = new CheckedWithSizeInputStream(body, crc); String location; try { - location = serviceProvider.getMainStorage().put(dsInfo, - name, is); + location = serviceProvider.getMainStorage().put(dsInfo, name, is); } catch (IllegalArgumentException e) { - throw new BadRequestException( - "Illegal filename or dataset: " + e.getMessage()); + throw new BadRequestException("Illegal filename or dataset: " + e.getMessage()); } is.close(); long checksum = crc.getValue(); long size = is.getSize(); - + try { - dfId = registerDatafile(sessionId, name, datafileFormatId, - location, checksum, size, ds, description, doi, - datafileCreateTime, datafileModTime); - } catch (InsufficientPrivilegesException | NotFoundException - | InternalException | BadRequestException e) { - logger.debug("Problem with registration " + e.getClass() - + " " + e.getMessage() + dfId = registerDatafile(sessionId, name, datafileFormatId, location, checksum, size, ds, + description, doi, datafileCreateTime, datafileModTime); + } catch (InsufficientPrivilegesException | NotFoundException | InternalException + | BadRequestException e) { + logger.debug("Problem with registration " + e.getClass() + " " + e.getMessage() + " datafile will now be deleted"); String userId = null; try { - userId = serviceProvider.getIcat() - .getUserName(sessionId); + userId = serviceProvider.getIcat().getUserName(sessionId); } catch (IcatException_Exception e1) { - logger.error("Unable to get user name for session " - + sessionId + " so mainStorage.delete of " + logger.error("Unable to get user name for session " + sessionId + " so mainStorage.delete of " + location + " may fail"); } - serviceProvider.getMainStorage().delete(location, userId, - userId); + serviceProvider.getMainStorage().delete(location, userId, userId); throw e; } @@ -269,80 +240,62 @@ public ValueContainer handleRequest() throws BadRequestException, } else if (storageUnit == StorageUnit.DATAFILE) { Datafile df; try { - df = (Datafile) serviceProvider.getIcatReader() - .get("Datafile", dfId); + df = (Datafile) serviceProvider.getIcatReader().get("Datafile", dfId); } catch (IcatException_Exception e) { - throw new InternalException(e.getFaultInfo().getType() - + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } - serviceProvider.getFsm() - .queue(new DatafileInfo(dfId, name, location, - df.getCreateId(), df.getModId(), - dsInfo.getId()), DeferredOp.WRITE); + serviceProvider.getFsm().queue(new DatafileInfo(dfId, name, location, df.getCreateId(), df.getModId(), dsInfo.getId()), + DeferredOp.WRITE); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Json.createGenerator(baos).writeStartObject().write("id", dfId) - .write("checksum", checksum) - .write("location", - location.replace("\\", "\\\\").replace("'", - "\\'")) - .write("size", size).writeEnd().close(); - String resp = wrap ? prefix + baos.toString() + suffix - : baos.toString(); - - return new ValueContainer( - Response.status(HttpURLConnection.HTTP_CREATED) - .entity(resp).build()); + Json.createGenerator(baos).writeStartObject().write("id", dfId).write("checksum", checksum) + .write("location", location.replace("\\", "\\\\").replace("'", "\\'")).write("size", size) + .writeEnd().close(); + String resp = wrap ? prefix + baos.toString() + suffix : baos.toString(); + + return new ValueContainer(Response.status(HttpURLConnection.HTTP_CREATED).entity(resp).build()); } catch (AlreadyLockedException e) { logger.debug("Could not acquire lock, put failed"); throw new DataNotOnlineException("Data is busy"); } catch (IOException e) { - logger.error("I/O exception " + e.getMessage() + " putting " - + name + " to Dataset with id " + datasetIdString); - throw new InternalException( - e.getClass() + " " + e.getMessage()); + logger.error("I/O exception " + e.getMessage() + " putting " + name + " to Dataset with id " + + datasetIdString); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IdsException e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator gen = Json.createGenerator(baos); - gen.writeStartObject().write("code", e.getClass().getSimpleName()) - .write("message", e.getShortMessage()); + gen.writeStartObject().write("code", e.getClass().getSimpleName()).write("message", e.getShortMessage()); gen.writeEnd().close(); if (wrap) { String pre = padding ? paddedPrefix : prefix; - return new ValueContainer(Response - .status(e.getHttpStatusCode()).entity(pre - + baos.toString().replace("'", "\\'") + suffix) + return new ValueContainer(Response.status(e.getHttpStatusCode()).entity(pre + baos.toString().replace("'", "\\'") + suffix) .build()); } else { - return new ValueContainer(Response.status(e.getHttpStatusCode()) - .entity(baos.toString()).build()); + return new ValueContainer(Response.status(e.getHttpStatusCode()).entity(baos.toString()).build()); } } } - private Long registerDatafile(String sessionId, String name, - long datafileFormatId, String location, long checksum, long size, - Dataset dataset, String description, String doi, - Long datafileCreateTime, Long datafileModTime) - throws InsufficientPrivilegesException, NotFoundException, - InternalException, BadRequestException { + private Long registerDatafile(String sessionId, String name, long datafileFormatId, String location, long checksum, + long size, Dataset dataset, String description, String doi, Long datafileCreateTime, Long datafileModTime) + throws InsufficientPrivilegesException, NotFoundException, InternalException, BadRequestException { + + var serviceProvider = ServiceProvider.getInstance(); ICAT icat = serviceProvider.getIcat(); - + final Datafile df = new Datafile(); DatafileFormat format; try { - format = (DatafileFormat) icat.get(sessionId, "DatafileFormat", - datafileFormatId); + format = (DatafileFormat) icat.get(sessionId, "DatafileFormat", datafileFormatId); } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES - || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { @@ -362,14 +315,12 @@ private Long registerDatafile(String sessionId, String name, if (datafileCreateTime != null) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTimeInMillis(datafileCreateTime); - df.setDatafileCreateTime( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); + df.setDatafileCreateTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); } if (datafileModTime != null) { GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTimeInMillis(datafileModTime); - df.setDatafileModTime( - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); + df.setDatafileModTime(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar)); } try { long dfId = icat.create(sessionId, df); @@ -377,18 +328,15 @@ private Long registerDatafile(String sessionId, String name, String key = serviceProvider.getPropertyHandler().getKey(); if (key != null) { - df.setLocation(location + " " - + IcatSecurity.digest(dfId, location, key)); + df.setLocation(location + " " + IcatSecurity.digest(dfId, location, key)); icat.update(sessionId, df); } - logger.debug("Registered datafile for dataset {} for {}", - dataset.getId(), name + " at " + location); + logger.debug("Registered datafile for dataset {} for {}", dataset.getId(), name + " at " + location); return dfId; } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES - || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } if (type == IcatExceptionType.VALIDATION) { @@ -402,15 +350,12 @@ private Long registerDatafile(String sessionId, String name, public String addParametersToLogString() { return "name='" + name + "' " + "datafileFormatId='" - + datafileFormatIdString + "' " + "datasetId='" - + datasetIdString + "' " + "description='" + description + "' " - + "doi='" + doi + "' " + "datafileCreateTime='" - + datafileCreateTimeString + "' " + "datafileModTime='" - + datafileModTimeString + "'"; + + datafileFormatIdString + "' " + "datasetId='" + datasetIdString + "' " + "description='" + + description + "' " + "doi='" + doi + "' " + "datafileCreateTime='" + datafileCreateTimeString + + "' " + "datafileModTime='" + datafileModTimeString + "'"; } - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { gen.write("userName", serviceProvider.getIcat().getUserName(sessionId)); gen.write("datafileId", dfId); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java index b8f6e667..2fff2bb3 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/ResetHandler.java @@ -15,24 +15,20 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; + public class ResetHandler extends DataRequestHandler { - public ResetHandler(String ip, String preparedId, String sessionId, - String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.RESET, ip, preparedId, sessionId, investigationIds, - datasetIds, datafileIds); + public ResetHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.RESET, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { FiniteStateMachine fsm = ServiceProvider.getInstance().getFsm(); - for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos() - .values()) { + for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { fsm.recordSuccess(dataInfo.getId()); } @@ -43,5 +39,7 @@ public ValueContainer handleDataRequest( public CallType getCallType() { return CallType.MIGRATE; } + + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java index 3f3c1e81..77e0c7e5 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/RestoreHandler.java @@ -15,20 +15,16 @@ public class RestoreHandler extends DataRequestHandler { - public RestoreHandler(String ip, String sessionId, String investigationIds, - String datasetIds, String datafileIds) { - super(RequestType.RESTORE, ip, sessionId, investigationIds, datasetIds, - datafileIds); + public RestoreHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.RESTORE, ip, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - dataSelectionService.scheduleTasks(DeferredOp.RESTORE); + dataSelectionService.scheduleTasks(DeferredOp.RESTORE); return ValueContainer.getVoid(); } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java index 59d564ad..c6f994f4 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/WriteHandler.java @@ -16,48 +16,40 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.plugin.AlreadyLockedException; import org.icatproject.ids.requestHandlers.base.DataRequestHandler; +import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.LockManager.LockType; -import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; public class WriteHandler extends DataRequestHandler { - public WriteHandler(String ip, String sessionId, String investigationIds, - String datasetIds, String datafileIds) { - super(RequestType.WRITE, ip, sessionId, investigationIds, datasetIds, - datafileIds); + public WriteHandler(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.WRITE, ip, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { var serviceProvider = ServiceProvider.getInstance(); if (!serviceProvider.getPropertyHandler().getEnableWrite()) { - throw new NotImplementedException( - "This operation has been configured to be unavailable"); + throw new NotImplementedException("This operation has been configured to be unavailable"); } Map dsInfos = dataSelectionService.getDsInfo(); - try (Lock lock = serviceProvider.getLockManager().lock(dsInfos.values(), - LockType.SHARED)) { + try (Lock lock = serviceProvider.getLockManager().lock(dsInfos.values(), LockType.SHARED)) { if (twoLevel) { boolean maybeOffline = false; - for (DataInfoBase dataInfo : dataSelectionService - .getPrimaryDataInfos().values()) { + for (DataInfoBase dataInfo : dataSelectionService.getPrimaryDataInfos().values()) { if (!dataSelectionService.existsInMainStorage(dataInfo)) { maybeOffline = true; } } if (maybeOffline) { - throw new DataNotOnlineException( - "Requested data is not online, write request refused"); + throw new DataNotOnlineException("Requested data is not online, write request refused"); } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java b/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java index 25d6eeab..1291e98e 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/DataControllerBase.java @@ -2,10 +2,7 @@ import java.util.regex.Pattern; -import jakarta.json.stream.JsonGenerator; - import org.icatproject.IcatException_Exception; - import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; import org.icatproject.ids.exceptions.InsufficientPrivilegesException; @@ -15,64 +12,52 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; +import jakarta.json.stream.JsonGenerator; + public abstract class DataControllerBase { /** * matches standard UUID format of 8-4-4-4-12 hexadecimal digits */ - public static final Pattern uuidRegExp = Pattern.compile( - "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); + public static final Pattern uuidRegExp = Pattern + .compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); - public abstract DataSelectionService provideDataSelectionService( - RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, - InsufficientPrivilegesException, NotImplementedException; + public abstract DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException; public abstract void validateUUID() throws BadRequestException; public abstract String addParametersToLogString(); - public abstract void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException; + public abstract void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException; - public abstract boolean mustZip(boolean zip, - DataSelectionService dataSelectionService); + public abstract boolean mustZip(boolean zip, DataSelectionService dataSelectionService); public abstract String getOperationId(); /** - * returns the current sessionId. If it's null or not defined it should be - * created - * + * returns the current sessionId. If it's null or not defined it should be created * @return - * @throws InternalException + * @throws InternalException */ public abstract String forceGetSessionId() throws InternalException; /** * Provides a validity check for UUIDs - * - * @param thing You can give here a name of the prameter or whatever has - * been checked here (to provide a qualified error message if - * needed). - * @param id The String which has to be checked if it is a valid UUID + * @param thing You can give here a name of the prameter or whatever has been checked here (to provide a qualified error message if needed). + * @param id The String which has to be checked if it is a valid UUID * @throws BadRequestException */ - public static void validateUUID(String thing, String id) - throws BadRequestException { + public static void validateUUID(String thing, String id) throws BadRequestException { if (id == null || !uuidRegExp.matcher(id).matches()) - throw new BadRequestException("The " + thing + " parameter '" + id - + "' is not a valid UUID"); + throw new BadRequestException("The " + thing + " parameter '" + id + "' is not a valid UUID"); } protected String createSessionId() throws InternalException { String sessionId; try { - sessionId = ServiceProvider.getInstance().getIcatReader() - .getSessionId(); + sessionId = ServiceProvider.getInstance().getIcatReader().getSessionId(); } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } return sessionId; } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java index 691bcee8..72576fa5 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/DataRequestHandler.java @@ -1,9 +1,6 @@ package org.icatproject.ids.requestHandlers.base; -import jakarta.json.stream.JsonGenerator; - import org.icatproject.IcatException_Exception; - import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; import org.icatproject.ids.exceptions.DataNotOnlineException; @@ -14,34 +11,31 @@ import org.icatproject.ids.helpers.ValueContainer; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; +import jakarta.json.stream.JsonGenerator; + + public abstract class DataRequestHandler extends RequestHandlerBase { protected DataControllerBase dataController; - protected DataRequestHandler(RequestType requestType, String ip, - String sessionId, String investigationIds, String datasetIds, - String datafileIds) { + + protected DataRequestHandler(RequestType requestType, String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { super(requestType, ip); - this.dataController = new UnpreparedDataController(sessionId, - investigationIds, datasetIds, datafileIds); + this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); } - protected DataRequestHandler(RequestType requestType, String ip, - String preparedId) { + protected DataRequestHandler(RequestType requestType, String ip, String preparedId) { super(requestType, ip); this.dataController = new PreparedDataController(preparedId); } - protected DataRequestHandler(RequestType requestType, String ip, - String preparedId, String sessionId, String investigationIds, - String datasetIds, String datafileIds) { + protected DataRequestHandler(RequestType requestType, String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { super(requestType, ip); - if (sessionId != null) { - this.dataController = new UnpreparedDataController(sessionId, - investigationIds, datasetIds, datafileIds); + if(sessionId != null) { + this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); } else { this.dataController = new PreparedDataController(preparedId); } @@ -49,14 +43,11 @@ protected DataRequestHandler(RequestType requestType, String ip, } @Override - protected ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { + protected ValueContainer handleRequest() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { this.dataController.validateUUID(); - DataSelectionService dataSelectionService = this.dataController - .provideDataSelectionService(this.requestType); + DataSelectionService dataSelectionService = this.dataController.provideDataSelectionService(this.requestType); ValueContainer result = this.handleDataRequest(dataSelectionService); return result; @@ -64,38 +55,26 @@ protected ValueContainer handleRequest() throws BadRequestException, } @Override - protected void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { this.dataController.addParametersToTransmitterJSON(gen); this.addCustomParametersToTransmitterJSON(gen); } - protected abstract ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws NotImplementedException, InternalException, - BadRequestException, NotFoundException, - InsufficientPrivilegesException, DataNotOnlineException; + protected abstract ValueContainer handleDataRequest(DataSelectionService dataSelectionService) throws NotImplementedException, InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, DataNotOnlineException; @Override protected String addParametersToLogString() { - return this.dataController.addParametersToLogString() + " " - + this.addCustomParametersToLogString(); + return this.dataController.addParametersToLogString() + " " + this.addCustomParametersToLogString(); } /** - * Override this method in your concrete DataRequestHandler to add custom - * parameters to the JSON which will be transmitted. - * + * Override this method in your concrete DataRequestHandler to add custom parameters to the JSON which will be transmitted. * @param gen */ - protected void addCustomParametersToTransmitterJSON(JsonGenerator gen) { - } + protected void addCustomParametersToTransmitterJSON(JsonGenerator gen) {} /** - * Override this method in your concrete DataRequestHandler to add custom - * parameters to the log output. + * Override this method in your concrete DataRequestHandler to add custom parameters to the log output. */ - protected String addCustomParametersToLogString() { - return ""; - } + protected String addCustomParametersToLogString() { return "";} } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java b/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java index b1eb3c54..f9fe6519 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/PreparedDataController.java @@ -5,13 +5,7 @@ import java.nio.file.Files; import java.nio.file.NoSuchFileException; -import jakarta.json.stream.JsonGenerator; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.icatproject.IcatException_Exception; - import org.icatproject.ids.enums.RequestIdNames; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -23,11 +17,14 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; import org.icatproject.ids.services.dataSelectionService.DataSelectionServiceFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import jakarta.json.stream.JsonGenerator; public class PreparedDataController extends DataControllerBase { - protected final static Logger logger = LoggerFactory - .getLogger(RequestHandlerBase.class); + protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); String preparedId; @@ -37,21 +34,16 @@ public PreparedDataController(String preparedId) { } @Override - public DataSelectionService provideDataSelectionService( - RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, - InsufficientPrivilegesException, NotImplementedException { - - var preparedDir = ServiceProvider.getInstance().getPropertyHandler() - .getCacheDir().resolve("prepared"); + public DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, + BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + + var preparedDir = ServiceProvider.getInstance().getPropertyHandler().getCacheDir().resolve("prepared"); Prepared prepared; - try (InputStream stream = Files - .newInputStream(preparedDir.resolve(preparedId))) { + try (InputStream stream = Files.newInputStream(preparedDir.resolve(preparedId))) { prepared = Prepared.unpack(stream); } catch (NoSuchFileException e) { - throw new NotFoundException( - "The preparedId " + preparedId + " is not known"); + throw new NotFoundException("The preparedId " + preparedId + " is not known"); } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } @@ -70,14 +62,12 @@ public String addParametersToLogString() { } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { gen.write(RequestIdNames.preparedId, this.preparedId); } @Override - public boolean mustZip(boolean zip, - DataSelectionService dataSelectionService) { + public boolean mustZip(boolean zip, DataSelectionService dataSelectionService) { return zip; } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java b/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java index ba583b93..9524b7a0 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/RequestHandlerBase.java @@ -4,14 +4,13 @@ import java.nio.file.Path; import java.util.regex.Pattern; -import jakarta.json.Json; -import jakarta.json.stream.JsonGenerator; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.icatproject.IcatException_Exception; +import jakarta.json.Json; +import jakarta.json.stream.JsonGenerator; +import org.icatproject.IcatException_Exception; import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.enums.StorageUnit; @@ -24,15 +23,14 @@ import org.icatproject.ids.helpers.ValueContainer; import org.icatproject.ids.services.ServiceProvider; + /** - * This base class represents all common properties and methods which are needed - * by each request handler. Request handlers schould be added to the internal - * request handler list in RequestHandlerService, to be able to be called. + * This base class represents all common properties and methods which are needed by each request handler. + * Request handlers schould be added to the internal request handler list in RequestHandlerService, to be able to be called. */ public abstract class RequestHandlerBase { - protected final static Logger logger = LoggerFactory - .getLogger(RequestHandlerBase.class); + protected final static Logger logger = LoggerFactory.getLogger(RequestHandlerBase.class); protected Path preparedDir; protected boolean twoLevel; protected StorageUnit storageUnit; @@ -43,10 +41,11 @@ public abstract class RequestHandlerBase { /** * matches standard UUID format of 8-4-4-4-12 hexadecimal digits */ - public static final Pattern uuidRegExp = Pattern.compile( - "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); + public static final Pattern uuidRegExp = Pattern + .compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"); + - protected RequestHandlerBase(RequestType requestType, String ip) { + protected RequestHandlerBase(RequestType requestType, String ip ) { this.requestType = requestType; this.ip = ip; @@ -59,9 +58,9 @@ protected RequestHandlerBase(RequestType requestType, String ip) { this.readOnly = propertyHandler.getReadOnly(); } + /** * Informs about the RequestType the handler ist providing a handling for. - * * @return */ public RequestType getRequestType() { @@ -69,9 +68,7 @@ public RequestType getRequestType() { } /** - * The core method of each request handler. It has to be overwritten in the - * concrete implementation to provide an individual request handling - * + * The core method of each request handler. It has to be overwritten in the concrete implementation to provide an individual request handling * @return A ValueContainer with an indiviadual result type. * @throws BadRequestException * @throws InternalException @@ -80,15 +77,11 @@ public RequestType getRequestType() { * @throws DataNotOnlineException * @throws NotImplementedException */ - public ValueContainer handle() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { + public ValueContainer handle() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { // some preprocessing long start = System.currentTimeMillis(); - logger.info("New webservice request: " - + this.requestType.toString().toLowerCase() + " " - + this.addParametersToLogString()); + logger.info("New webservice request: " + this.requestType.toString().toLowerCase() + " " + this.addParametersToLogString()); // Do it ValueContainer result = this.handleRequest(); @@ -100,56 +93,44 @@ public ValueContainer handle() throws BadRequestException, } + private void transmit(long start) throws BadRequestException { - if (ServiceProvider.getInstance().getLogSet() - .contains(this.getCallType())) { + if (ServiceProvider.getInstance().getLogSet().contains(this.getCallType())) { try { String body = this.provideTransmissionBody(); - ServiceProvider.getInstance().getTransmitter() - .processMessage("archive", ip, body, start); + ServiceProvider.getInstance().getTransmitter().processMessage("archive", ip, body, start); } catch (IcatException_Exception e) { - logger.error("Failed to prepare jms message " + e.getClass() - + " " + e.getMessage()); + logger.error("Failed to prepare jms message " + e.getClass() + " " + e.getMessage()); } } } - public String provideTransmissionBody() - throws BadRequestException, IcatException_Exception { + public String provideTransmissionBody() throws BadRequestException, IcatException_Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (JsonGenerator gen = Json.createGenerator(baos) - .writeStartObject()) { + try (JsonGenerator gen = Json.createGenerator(baos).writeStartObject()) { this.addParametersToTransmitterJSON(gen); gen.writeEnd(); } return baos.toString(); } + /** - * Override to add additional parameters to the log output for the current - * request - * + * Override to add additional parameters to the log output for the current request * @return */ - protected String addParametersToLogString() { - return ""; - } + protected String addParametersToLogString() { return ""; } /** * Override to add additional parameters to the transmitter JSON - * * @param gen * @throws IcatException_Exception * @throws BadRequestException */ - protected void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { - } + protected void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException {} /** - * The core method of each request handler. It has to be overwritten in the - * concrete implementation to provide an individual request handling - * + * The core method of each request handler. It has to be overwritten in the concrete implementation to provide an individual request handling * @return A ValueContainer with an indiviadual result type. * @throws BadRequestException * @throws InternalException @@ -158,15 +139,10 @@ protected void addParametersToTransmitterJSON(JsonGenerator gen) * @throws DataNotOnlineException * @throws NotImplementedException */ - protected abstract ValueContainer handleRequest() - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, - DataNotOnlineException, NotImplementedException; + protected abstract ValueContainer handleRequest() throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException; /** - * each handler should provide its own CallType which is needed to create - * the Transmitter message - * + * each handler should provide its own CallType which is needed to create the Transmitter message * @return the Calltype of the request */ protected abstract CallType getCallType(); diff --git a/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java b/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java index f3dac573..57668b02 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/base/UnpreparedDataController.java @@ -1,9 +1,6 @@ package org.icatproject.ids.requestHandlers.base; -import jakarta.json.stream.JsonGenerator; - import org.icatproject.IcatException_Exception; - import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; import org.icatproject.ids.exceptions.InsufficientPrivilegesException; @@ -14,6 +11,8 @@ import org.icatproject.ids.services.dataSelectionService.DataSelectionService; import org.icatproject.ids.services.dataSelectionService.DataSelectionServiceFactory; +import jakarta.json.stream.JsonGenerator; + public class UnpreparedDataController extends DataControllerBase { public String sessionId; @@ -21,8 +20,7 @@ public class UnpreparedDataController extends DataControllerBase { public String datasetIds; public String datafileIds; - public UnpreparedDataController(String sessionId, String investigationIds, - String datasetIds, String datafileIds) { + public UnpreparedDataController(String sessionId, String investigationIds, String datasetIds, String datafileIds) { this.sessionId = sessionId; this.investigationIds = investigationIds; this.datasetIds = datasetIds; @@ -37,48 +35,41 @@ public void validateUUID() throws BadRequestException { @Override public String addParametersToLogString() { return "investigationIds='" + investigationIds + "' " + "datasetIds='" - + datasetIds + "' " + "datafileIds='" + datafileIds + "'"; + + datasetIds + "' " + "datafileIds='" + datafileIds + "'"; } @Override - public DataSelectionService provideDataSelectionService( - RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, - InsufficientPrivilegesException, NotImplementedException { - return DataSelectionServiceFactory.getService(sessionId, - investigationIds, datasetIds, datafileIds, requestType); + public DataSelectionService provideDataSelectionService(RequestType requestType) throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + return DataSelectionServiceFactory.getService(sessionId, investigationIds, datasetIds, datafileIds, requestType); } @Override - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { - gen.write("userName", - ServiceProvider.getInstance().getIcat().getUserName(sessionId)); + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { + gen.write("userName", ServiceProvider.getInstance().getIcat().getUserName(sessionId)); addIds(gen, investigationIds, datasetIds, datafileIds); } - protected void addIds(JsonGenerator gen, String investigationIds, - String datasetIds, String datafileIds) throws BadRequestException { + + + protected void addIds(JsonGenerator gen, String investigationIds, String datasetIds, String datafileIds) + throws BadRequestException { if (investigationIds != null) { gen.writeStartArray("investigationIds"); - for (long invid : DataSelectionService - .getValidIds("investigationIds", investigationIds)) { + for (long invid : DataSelectionService.getValidIds("investigationIds", investigationIds)) { gen.write(invid); } gen.writeEnd(); } if (datasetIds != null) { gen.writeStartArray("datasetIds"); - for (long invid : DataSelectionService.getValidIds("datasetIds", - datasetIds)) { + for (long invid : DataSelectionService.getValidIds("datasetIds", datasetIds)) { gen.write(invid); } gen.writeEnd(); } if (datafileIds != null) { gen.writeStartArray("datafileIds"); - for (long invid : DataSelectionService.getValidIds("datafileIds", - datafileIds)) { + for (long invid : DataSelectionService.getValidIds("datafileIds", datafileIds)) { gen.write(invid); } gen.writeEnd(); @@ -86,8 +77,7 @@ protected void addIds(JsonGenerator gen, String investigationIds, } @Override - public boolean mustZip(boolean zip, - DataSelectionService dataSelectionService) { + public boolean mustZip(boolean zip, DataSelectionService dataSelectionService) { return zip ? true : dataSelectionService.mustZip(); } @@ -101,7 +91,7 @@ public String forceGetSessionId() throws InternalException { if (this.sessionId == null) { this.sessionId = this.createSessionId(); } - + return this.sessionId; } } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java index 1b360bc3..0b4aa315 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandler.java @@ -3,7 +3,6 @@ import java.util.Map; import org.icatproject.IcatException_Exception; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -20,32 +19,25 @@ public class GetSizeHandler extends DataRequestHandler { - public GetSizeHandler(String ip, String preparedId, String sessionId, - String investigationIds, String datasetIds, String datafileIds) { - super(RequestType.GETSIZE, ip, preparedId, sessionId, investigationIds, - datasetIds, datafileIds); + public GetSizeHandler(String ip, String preparedId, String sessionId, String investigationIds, String datasetIds, String datafileIds) { + super(RequestType.GETSIZE, ip, preparedId, sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleDataRequest( - DataSelectionService dataSelectionService) - throws BadRequestException, InternalException, - InsufficientPrivilegesException, NotFoundException, + public ValueContainer handleDataRequest(DataSelectionService dataSelectionService) + throws BadRequestException, InternalException, InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { - + logger.debug("Slow computation for normal case"); long size = 0; - size = this.updateSizeFromDataInfoIds(size, - dataSelectionService.getDfInfo(), - this.dataController.forceGetSessionId()); + size = this.updateSizeFromDataInfoIds(size, dataSelectionService.getDfInfo(), this.dataController.forceGetSessionId()); return new ValueContainer(size); } - protected long updateSizeFromDataInfoIds(long size, - Map dataInfos, String sessionId) - throws InternalException { + + protected long updateSizeFromDataInfoIds(long size, Map dataInfos, String sessionId) throws InternalException { StringBuilder sb = new StringBuilder(); int n = 0; for (DataInfoBase dataInfo : dataInfos.values()) { @@ -66,13 +58,11 @@ protected long updateSizeFromDataInfoIds(long size, return size; } - private long getSizeFor(String sessionId, StringBuilder sb) - throws InternalException { - String query = "SELECT SUM(df.fileSize) from Datafile df WHERE df.id IN (" - + sb.toString() + ") AND df.location IS NOT NULL"; + + private long getSizeFor(String sessionId, StringBuilder sb) throws InternalException { + String query = "SELECT SUM(df.fileSize) from Datafile df WHERE df.id IN (" + sb.toString() + ") AND df.location IS NOT NULL"; try { - return (Long) ServiceProvider.getInstance().getIcat() - .search(sessionId, query).get(0); + return (Long) ServiceProvider.getInstance().getIcat().search(sessionId, query).get(0); } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (IndexOutOfBoundsException e) { @@ -84,5 +74,5 @@ private long getSizeFor(String sessionId, StringBuilder sb) public CallType getCallType() { return CallType.INFO; } - + } diff --git a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java index 3fab7574..6b57cd9b 100644 --- a/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java +++ b/src/main/java/org/icatproject/ids/requestHandlers/getSizeHandlers/GetSizeHandlerForFastProcessing.java @@ -2,11 +2,8 @@ import java.util.List; -import jakarta.json.stream.JsonGenerator; - import org.icatproject.Datafile; import org.icatproject.IcatException_Exception; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; @@ -21,62 +18,50 @@ import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.dataSelectionService.DataSelectionService; +import jakarta.json.stream.JsonGenerator; + public class GetSizeHandlerForFastProcessing extends RequestHandlerBase { UnpreparedDataController dataController; - public GetSizeHandlerForFastProcessing(String ip, String sessionId, - String investigationIds, String datasetIds, String datafileIds) { + public GetSizeHandlerForFastProcessing(String ip, String sessionId, String investigationIds, String datasetIds, String datafileIds) { super(RequestType.GETSIZE, ip); - this.dataController = new UnpreparedDataController(sessionId, - investigationIds, datasetIds, datafileIds); + this.dataController = new UnpreparedDataController(sessionId, investigationIds, datasetIds, datafileIds); } @Override - public ValueContainer handleRequest() throws BadRequestException, - InternalException, InsufficientPrivilegesException, - NotFoundException, DataNotOnlineException, NotImplementedException { - + public ValueContainer handleRequest() throws BadRequestException, InternalException, + InsufficientPrivilegesException, NotFoundException, DataNotOnlineException, NotImplementedException { + this.dataController.validateUUID(); var serviceProvider = ServiceProvider.getInstance(); - List dfids = DataSelectionService.getValidIds("datafileIds", - dataController.datafileIds); - List dsids = DataSelectionService.getValidIds("datasetIds", - dataController.datasetIds); - List invids = DataSelectionService.getValidIds("investigationIds", - dataController.investigationIds); + List dfids = DataSelectionService.getValidIds("datafileIds", dataController.datafileIds); + List dsids = DataSelectionService.getValidIds("datasetIds", dataController.datasetIds); + List invids = DataSelectionService.getValidIds("investigationIds", dataController.investigationIds); + if (dfids.size() + dsids.size() + invids.size() == 1) { long size = 0; - size = getSizeFor(dataController.getOperationId(), invids, - "df.dataset.investigation.id") - + getSizeFor(dataController.getOperationId(), dsids, - "df.dataset.id") - + getSizeFor(dataController.getOperationId(), dfids, - "df.id"); + size = getSizeFor(dataController.getOperationId(), invids, "df.dataset.investigation.id") + + getSizeFor(dataController.getOperationId(), dsids, "df.dataset.id") + + getSizeFor(dataController.getOperationId(), dfids, "df.id"); logger.debug("Fast computation for simple case"); if (size == 0) { try { if (dfids.size() != 0) { - Datafile datafile = (Datafile) serviceProvider.getIcat() - .get(dataController.getOperationId(), - "Datafile", dfids.get(0)); + Datafile datafile = (Datafile) serviceProvider.getIcat().get(dataController.getOperationId(), "Datafile", dfids.get(0)); if (datafile.getLocation() == null) { throw new NotFoundException("Datafile not found"); } } if (dsids.size() != 0) { - serviceProvider.getIcat().get( - dataController.getOperationId(), "Dataset", - dsids.get(0)); + serviceProvider.getIcat().get(dataController.getOperationId(), "Dataset", dsids.get(0)); } if (invids.size() != 0) { - serviceProvider.getIcat().get( - dataController.getOperationId(), - "Investigation", invids.get(0)); + serviceProvider.getIcat().get(dataController.getOperationId(), "Investigation", invids.get(0)); } } catch (IcatException_Exception e) { throw new NotFoundException(e.getMessage()); @@ -85,10 +70,10 @@ public ValueContainer handleRequest() throws BadRequestException, return new ValueContainer(size); } + + return ValueContainer.getInvalid(); //is case of fast computation is not the right way. - return ValueContainer.getInvalid(); // is case of fast computation is - // not the right way. - + } @Override @@ -96,17 +81,13 @@ public CallType getCallType() { return CallType.INFO; } - public String addParametersToLogString() { - return this.dataController.addParametersToLogString(); - } + public String addParametersToLogString() { return this.dataController.addParametersToLogString(); } - public void addParametersToTransmitterJSON(JsonGenerator gen) - throws IcatException_Exception, BadRequestException { + public void addParametersToTransmitterJSON(JsonGenerator gen) throws IcatException_Exception, BadRequestException { this.dataController.addParametersToTransmitterJSON(gen); } - private long getSizeFor(String sessionId, List ids, String where) - throws InternalException { + private long getSizeFor(String sessionId, List ids, String where) throws InternalException { long size = 0; if (ids != null) { @@ -131,19 +112,16 @@ private long getSizeFor(String sessionId, List ids, String where) return size; } - private long evalSizeFor(String sessionId, String where, StringBuilder sb) - throws InternalException { - String query = "SELECT SUM(df.fileSize) from Datafile df WHERE " + where - + " IN (" + sb.toString() + ") AND df.location IS NOT NULL"; + private long evalSizeFor(String sessionId, String where, StringBuilder sb) throws InternalException { + String query = "SELECT SUM(df.fileSize) from Datafile df WHERE " + where + " IN (" + sb.toString() + ") AND df.location IS NOT NULL"; logger.debug("icat query for size: {}", query); try { - return (Long) ServiceProvider.getInstance().getIcat() - .search(sessionId, query).get(0); + return (Long) ServiceProvider.getInstance().getIcat().search(sessionId, query).get(0); } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (IndexOutOfBoundsException e) { return 0L; } } - + } diff --git a/src/main/java/org/icatproject/ids/services/ICATGetter.java b/src/main/java/org/icatproject/ids/services/ICATGetter.java index d62bd126..4073c3ce 100644 --- a/src/main/java/org/icatproject/ids/services/ICATGetter.java +++ b/src/main/java/org/icatproject/ids/services/ICATGetter.java @@ -17,21 +17,19 @@ */ public class ICATGetter { - private static String[] suffices = new String[] { "ICATService/ICAT?wsdl", - "icat/ICAT?wsdl" }; + private static String[] suffices = new String[]{"ICATService/ICAT?wsdl", "icat/ICAT?wsdl"}; /** * Provide access to an ICAT SOAP web service with the basic URL string * provided. This exists to hide the differences between containers. * * @param urlString the url of the machine to be contacted. If the url has a - * non-empty file part it is used unchanged, otherwise - * suffices are tried suitable for Glassfish and WildFly. + * non-empty file part it is used unchanged, otherwise suffices + * are tried suitable for Glassfish and WildFly. * @return an ICAT * @throws IcatException_Exception if something is wrong */ - public static ICAT getService(String urlString) - throws IcatException_Exception { + public static ICAT getService(String urlString) throws IcatException_Exception { if (urlString == null) { throwSessionException("Argument to constructor must not be null"); @@ -49,16 +47,14 @@ public static ICAT getService(String urlString) for (String suffix : suffices) { String icatUrlWsdl = urlString + "/" + suffix; try { - icatService = new ICATService(new URL(icatUrlWsdl)) - .getICATPort(); + icatService = new ICATService(new URL(icatUrlWsdl)).getICATPort(); icatService.getApiVersion(); return icatService; } catch (MalformedURLException e) { throwSessionException("Invalid URL"); } catch (WebServiceException e) { Throwable cause = e.getCause(); - if (cause != null - && cause.getMessage().contains("security")) { + if (cause != null && cause.getMessage().contains("security")) { throwSessionException(cause.getMessage()); } } catch (Exception e) { @@ -90,8 +86,7 @@ public static String getCleanUrl(String urlString) { for (String suffix : suffices) { suffix = "/" + suffix; if (urlString.endsWith(suffix)) { - return urlString.substring(0, - urlString.length() - suffix.length()); + return urlString.substring(0, urlString.length() - suffix.length()); } } if (urlString.endsWith("/")) { @@ -100,8 +95,7 @@ public static String getCleanUrl(String urlString) { return urlString; } - private static void throwSessionException(String msg) - throws IcatException_Exception { + private static void throwSessionException(String msg) throws IcatException_Exception { IcatException except = new IcatException(); except.setMessage(msg); except.setType(IcatExceptionType.SESSION); diff --git a/src/main/java/org/icatproject/ids/services/IcatReader.java b/src/main/java/org/icatproject/ids/services/IcatReader.java index ab184adf..9f5adbb8 100644 --- a/src/main/java/org/icatproject/ids/services/IcatReader.java +++ b/src/main/java/org/icatproject/ids/services/IcatReader.java @@ -22,8 +22,7 @@ public class IcatReader { private String sessionId; - private final static Logger logger = LoggerFactory - .getLogger(IcatReader.class); + private final static Logger logger = LoggerFactory.getLogger(IcatReader.class); private PropertyHandler propertyHandler; @@ -32,8 +31,7 @@ public IcatReader() { } - // primarily required for testing to make it possible to use a mocked - // PropertyHandler + // primarily required for testing to make it possible to use a mocked PropertyHandler public IcatReader(PropertyHandler propertyHandler) { this.propertyHandler = propertyHandler; init(); @@ -45,8 +43,7 @@ private void init() { login(); logger.info("Reader started"); } catch (Exception e) { - throw new RuntimeException( - "Reader reports " + e.getClass() + " " + e.getMessage()); + throw new RuntimeException("Reader reports " + e.getClass() + " " + e.getMessage()); } } @@ -70,8 +67,7 @@ private void login() throws IcatException_Exception { } } - public EntityBaseBean get(String query, long id) - throws IcatException_Exception { + public EntityBaseBean get(String query, long id) throws IcatException_Exception { try { return icat.get(sessionId, query, id); } catch (IcatException_Exception e) { diff --git a/src/main/java/org/icatproject/ids/services/LockManager.java b/src/main/java/org/icatproject/ids/services/LockManager.java index 59d6fdcb..49beba05 100644 --- a/src/main/java/org/icatproject/ids/services/LockManager.java +++ b/src/main/java/org/icatproject/ids/services/LockManager.java @@ -11,7 +11,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; @@ -93,9 +92,7 @@ public void release() { try { storageLock.close(); } catch (Exception e) { - logger.error( - "Error while closing lock on {} in the storage plugin: {}.", - id, e.getMessage()); + logger.error("Error while closing lock on {} in the storage plugin: {}.", id, e.getMessage()); } } logger.debug("Released a lock on {}.", id); @@ -134,8 +131,7 @@ private void init() { logger.debug("LockManager initialized."); } - public Lock lock(DatasetInfo ds, LockType type) - throws AlreadyLockedException, IOException { + public Lock lock(DatasetInfo ds, LockType type) throws AlreadyLockedException, IOException { Long id = ds.getDsId(); assert id != null; synchronized (lockMap) { @@ -143,8 +139,7 @@ public Lock lock(DatasetInfo ds, LockType type) if (le == null) { le = new LockEntry(id, type); } else { - if (type == LockType.EXCLUSIVE - || le.type == LockType.EXCLUSIVE) { + if (type == LockType.EXCLUSIVE || le.type == LockType.EXCLUSIVE) { throw new AlreadyLockedException(); } } @@ -161,16 +156,12 @@ public Lock lock(DatasetInfo ds, LockType type) } } - public Lock lock(Collection datasets, LockType type) - throws AlreadyLockedException, IOException, InternalException { + public Lock lock(Collection datasets, LockType type) throws AlreadyLockedException, IOException, InternalException { LockCollection locks = new LockCollection(); try { for (DataInfoBase dataInfo : datasets) { DatasetInfo ds = (DatasetInfo) dataInfo; - if (ds == null) - throw new InternalException("Could not cast " - + dataInfo.getClass() - + " to DataSetInfo. Did you handed over another sub type of DataInfoBase? "); + if(ds == null) throw new InternalException("Could not cast " + dataInfo.getClass() + " to DataSetInfo. Did you handed over another sub type of DataInfoBase? "); locks.add(lock(ds, type)); } } catch (AlreadyLockedException | IOException e) { diff --git a/src/main/java/org/icatproject/ids/services/PropertyHandler.java b/src/main/java/org/icatproject/ids/services/PropertyHandler.java index ac5e611c..e3cbe354 100644 --- a/src/main/java/org/icatproject/ids/services/PropertyHandler.java +++ b/src/main/java/org/icatproject/ids/services/PropertyHandler.java @@ -22,14 +22,13 @@ import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; -import org.icatproject.utils.CheckedProperties; -import org.icatproject.utils.CheckedProperties.CheckedPropertyException; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.plugin.ArchiveStorageInterface; import org.icatproject.ids.plugin.MainStorageInterface; import org.icatproject.ids.plugin.ZipMapperInterface; +import org.icatproject.utils.CheckedProperties; +import org.icatproject.utils.CheckedProperties.CheckedPropertyException; /* * Load the properties specified in the properties file ids.properties. @@ -37,8 +36,7 @@ public class PropertyHandler { private static PropertyHandler instance = null; - private static final Logger logger = LoggerFactory - .getLogger(PropertyHandler.class); + private static final Logger logger = LoggerFactory.getLogger(PropertyHandler.class); public synchronized static PropertyHandler getInstance() { if (instance == null) { @@ -98,16 +96,12 @@ private PropertyHandler() { } preparedCount = props.getPositiveInt("preparedCount"); - processQueueIntervalSeconds = props - .getPositiveLong("processQueueIntervalSeconds"); - rootUserNames = new HashSet<>(Arrays.asList( - props.getString("rootUserNames").trim().split("\\s+"))); + processQueueIntervalSeconds = props.getPositiveLong("processQueueIntervalSeconds"); + rootUserNames = new HashSet<>(Arrays.asList(props.getString("rootUserNames").trim().split("\\s+"))); - reader = Arrays - .asList(props.getString("reader").trim().split("\\s+")); + reader = Arrays.asList(props.getString("reader").trim().split("\\s+")); if (reader.size() % 2 != 1) { - throw new IllegalStateException( - "reader must have an odd number of words"); + throw new IllegalStateException("reader must have an odd number of words"); } readOnly = props.getBoolean("readOnly", false); @@ -117,8 +111,7 @@ private PropertyHandler() { enableWrite = !readOnly; } - sizeCheckIntervalMillis = props - .getPositiveInt("sizeCheckIntervalSeconds") * 1000L; + sizeCheckIntervalMillis = props.getPositiveInt("sizeCheckIntervalSeconds") * 1000L; if (props.has("key")) { key = props.getString("key"); @@ -130,17 +123,14 @@ private PropertyHandler() { zipMapper = klass.getConstructor().newInstance(); logger.debug("ZipMapper initialised"); - } catch (ClassNotFoundException | InstantiationException - | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException - | SecurityException e) { + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException | SecurityException e) { abort(e.getClass() + " " + e.getMessage()); } // Now get simple properties to pass to the plugins Properties simpleProps = new Properties(); - try (InputStream is = getClass().getClassLoader() - .getResourceAsStream("run.properties")) { + try (InputStream is = getClass().getClassLoader().getResourceAsStream("run.properties")) { simpleProps.load(is); } catch (IOException e) { abort(e.getClass() + " " + e.getMessage()); @@ -149,67 +139,54 @@ private PropertyHandler() { try { Class klass = (Class) Class .forName(props.getString("plugin.main.class")); - mainStorage = klass.getConstructor(Properties.class) - .newInstance(simpleProps); + mainStorage = klass.getConstructor(Properties.class).newInstance(simpleProps); logger.debug("mainStorage initialised"); - } catch (ClassNotFoundException | InstantiationException - | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException - | SecurityException e) { + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException | SecurityException e) { logger.error("Plugin failed...", e); abort(e.getClass() + " " + e.getMessage()); } if (!props.has("plugin.archive.class")) { - logger.info( - "Property plugin.archive.class not set, single storage enabled."); + logger.info("Property plugin.archive.class not set, single storage enabled."); } else { try { Class klass = (Class) Class .forName(props.getString("plugin.archive.class")); - archiveStorage = klass.getConstructor(Properties.class) - .newInstance(simpleProps); + archiveStorage = klass.getConstructor(Properties.class).newInstance(simpleProps); logger.debug("archiveStorage initialised"); - } catch (ClassNotFoundException | InstantiationException - | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | NoSuchMethodException - | SecurityException e) { + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException + | IllegalArgumentException | InvocationTargetException | NoSuchMethodException + | SecurityException e) { logger.error("Plugin failed...", e); abort(e.getClass() + " " + e.getMessage()); } - startArchivingLevel = props - .getPositiveLong("startArchivingLevel1024bytes") * 1024; - stopArchivingLevel = props - .getPositiveLong("stopArchivingLevel1024bytes") * 1024; + startArchivingLevel = props.getPositiveLong("startArchivingLevel1024bytes") * 1024; + stopArchivingLevel = props.getPositiveLong("stopArchivingLevel1024bytes") * 1024; if (stopArchivingLevel >= startArchivingLevel) { abort("startArchivingLevel1024bytes must be greater than stopArchivingLevel1024bytes"); } try { String storageUnitName = props.getString("storageUnit"); - storageUnit = StorageUnit - .valueOf(storageUnitName.toUpperCase()); + storageUnit = StorageUnit.valueOf(storageUnitName.toUpperCase()); } catch (IllegalArgumentException e) { List vs = new ArrayList<>(); for (StorageUnit s : StorageUnit.values()) { vs.add(s.name()); } - abort("storageUnit value " + props.getString("storageUnit") - + " must be taken from " + vs); + abort("storageUnit value " + props.getString("storageUnit") + " must be taken from " + vs); } if (storageUnit == StorageUnit.DATASET) { - delayDatasetWrites = props - .getPositiveLong("delayDatasetWritesSeconds"); + delayDatasetWrites = props.getPositiveLong("delayDatasetWritesSeconds"); } else if (storageUnit == StorageUnit.DATAFILE) { - delayDatafileOperations = props - .getPositiveLong("delayDatafileOperationsSeconds"); + delayDatafileOperations = props.getPositiveLong("delayDatafileOperationsSeconds"); } tidyBlockSize = props.getPositiveInt("tidyBlockSize"); } try { - cacheDir = props.getFile("cache.dir").getCanonicalFile() - .toPath(); + cacheDir = props.getFile("cache.dir").getCanonicalFile().toPath(); } catch (IOException e) { abort("IOException " + e.getMessage()); } @@ -220,31 +197,25 @@ private PropertyHandler() { maxIdsInQuery = props.getPositiveInt("maxIdsInQuery"); /* JMS stuff */ - jmsTopicConnectionFactory = props.getString( - "jms.topicConnectionFactory", + jmsTopicConnectionFactory = props.getString("jms.topicConnectionFactory", "java:comp/DefaultJMSConnectionFactory"); /* Call logging categories */ if (props.has("log.list")) { - for (String callTypeString : props.getString("log.list") - .split("\\s+")) { + for (String callTypeString : props.getString("log.list").split("\\s+")) { try { - logSet.add( - CallType.valueOf(callTypeString.toUpperCase())); + logSet.add(CallType.valueOf(callTypeString.toUpperCase())); } catch (IllegalArgumentException e) { - abort("Value " + callTypeString - + " in log.list must be chosen from " + abort("Value " + callTypeString + " in log.list must be chosen from " + Arrays.asList(CallType.values())); } } logger.info("log.list: " + logSet); } else { - logger.info( - "'log.list' entry not present so no JMS call logging will be performed"); + logger.info("'log.list' entry not present so no JMS call logging will be performed"); } - useReaderForPerformance = props - .getBoolean("useReaderForPerformance", false); + useReaderForPerformance = props.getBoolean("useReaderForPerformance", false); } catch (CheckedPropertyException e) { abort(e.getMessage()); @@ -276,8 +247,7 @@ public synchronized ICAT getIcatService() { try { icatService = ICATGetter.getService(icatUrl); } catch (IcatException_Exception e) { - String msg = "Problem finding ICAT API version at " + icatUrl - + ": " + e.getFaultInfo().getType() + " " + String msg = "Problem finding ICAT API version at " + icatUrl + ": " + e.getFaultInfo().getType() + " " + e.getMessage(); logger.error(msg); try { @@ -315,19 +285,15 @@ public synchronized int getMaxEntities() { // will produce an error message. while (maxEntities == null) { try { - org.icatproject.icat.client.ICAT ricat = new org.icatproject.icat.client.ICAT( - icatUrl); + org.icatproject.icat.client.ICAT ricat = new org.icatproject.icat.client.ICAT(icatUrl); try (JsonReader parser = Json - .createReader(new ByteArrayInputStream( - ricat.getProperties().getBytes()))) { + .createReader(new ByteArrayInputStream(ricat.getProperties().getBytes()))) { maxEntities = parser.readObject().getInt("maxEntities"); - logger.info( - "maxEntities from the ICAT.server {} version {} is {}", - icatUrl, ricat.getVersion(), maxEntities); + logger.info("maxEntities from the ICAT.server {} version {} is {}", icatUrl, ricat.getVersion(), + maxEntities); } catch (Exception e) { - String msg = "Problem finding 1 ICAT API version " - + e.getClass() + " " + e.getMessage(); + String msg = "Problem finding 1 ICAT API version " + e.getClass() + " " + e.getMessage(); logger.error(msg); try { Thread.sleep(10000); @@ -336,8 +302,7 @@ public synchronized int getMaxEntities() { } } } catch (URISyntaxException e) { - String msg = "Problem finding 2 ICAT API version " - + e.getClass() + " " + e.getMessage(); + String msg = "Problem finding 2 ICAT API version " + e.getClass() + " " + e.getMessage(); logger.error(msg); throw new IllegalStateException(msg); } diff --git a/src/main/java/org/icatproject/ids/services/ServiceProvider.java b/src/main/java/org/icatproject/ids/services/ServiceProvider.java index bc999f65..dc179ad5 100644 --- a/src/main/java/org/icatproject/ids/services/ServiceProvider.java +++ b/src/main/java/org/icatproject/ids/services/ServiceProvider.java @@ -3,15 +3,13 @@ import java.util.Set; import org.icatproject.ICAT; - import org.icatproject.ids.enums.CallType; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.plugin.MainStorageInterface; /** - * This class provides multiple services to the developer Maybe it is just for - * the redesign for version 3 and will later be replaced with dependency - * injection, when it will be more clear where which service is used. + * This class provides multiple services to the developer + * Maybe it is just for the redesign for version 3 and will later be replaced with dependency injection, when it will be more clear where which service is used. */ public class ServiceProvider { @@ -23,9 +21,7 @@ public class ServiceProvider { private IcatReader icatReader; private PropertyHandler propertyHandler; - private ServiceProvider(PropertyHandler propertyHandler, - Transmitter transmitter, FiniteStateMachine fsm, - LockManager lockManager, IcatReader reader) { + private ServiceProvider(PropertyHandler propertyHandler, Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { this.transmitter = transmitter; this.fsm = fsm; this.lockManager = lockManager; @@ -34,37 +30,27 @@ private ServiceProvider(PropertyHandler propertyHandler, } /** - * At first, the ServiceProvider has to be created. Do not call - * getInstance() before you have called createInstande() - * + * At first, the ServiceProvider has to be created. Do not call getInstance() before you have called createInstande() * @param transmitter * @param fsm * @param lockManager * @param reader */ - public static void createInstance(Transmitter transmitter, - FiniteStateMachine fsm, LockManager lockManager, - IcatReader reader) { + public static void createInstance(Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { - createInstance(PropertyHandler.getInstance(), transmitter, fsm, - lockManager, reader); + createInstance(PropertyHandler.getInstance(), transmitter, fsm, lockManager, reader); } - public static void createInstance(PropertyHandler propertyHandler, - Transmitter transmitter, FiniteStateMachine fsm, - LockManager lockManager, IcatReader reader) { + public static void createInstance(PropertyHandler propertyHandler, Transmitter transmitter, FiniteStateMachine fsm, LockManager lockManager, IcatReader reader) { - if (instance != null) - return; + if(instance != null) return; - instance = new ServiceProvider(propertyHandler, transmitter, fsm, - lockManager, reader); + instance = new ServiceProvider(propertyHandler, transmitter, fsm, lockManager, reader); } public static ServiceProvider getInstance() { - if (instance == null) { - throw new RuntimeException( - "ServiceProvider is not yet instantiated, please call createInstance at first."); + if(instance == null) { + throw new RuntimeException("ServiceProvider is not yet instantiated, please call createInstance at first."); } return instance; } @@ -101,4 +87,5 @@ public Set getLogSet() { return PropertyHandler.getInstance().getLogSet(); } + } diff --git a/src/main/java/org/icatproject/ids/services/Transmitter.java b/src/main/java/org/icatproject/ids/services/Transmitter.java index 267e4940..996522cb 100644 --- a/src/main/java/org/icatproject/ids/services/Transmitter.java +++ b/src/main/java/org/icatproject/ids/services/Transmitter.java @@ -59,15 +59,12 @@ private void exit() { } } - public void processMessage(String operation, String ip, String body, - long startMillis) { - try (Session jmsSession = topicConnection.createSession(false, - Session.AUTO_ACKNOWLEDGE)) { + public void processMessage(String operation, String ip, String body, long startMillis) { + try (Session jmsSession = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE)) { TextMessage jmsg = jmsSession.createTextMessage(body); jmsg.setStringProperty("operation", operation); jmsg.setStringProperty("ip", ip); - jmsg.setLongProperty("millis", - System.currentTimeMillis() - startMillis); + jmsg.setLongProperty("millis", System.currentTimeMillis() - startMillis); jmsg.setLongProperty("start", startMillis); MessageProducer jmsProducer = jmsSession.createProducer(topic); jmsProducer.send(jmsg); diff --git a/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java b/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java index ed5c2031..5f9b372b 100644 --- a/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java +++ b/src/main/java/org/icatproject/ids/services/UnfinishedWorkService.java @@ -5,63 +5,53 @@ import java.nio.file.Files; import java.nio.file.Path; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.IcatExceptionType; import org.icatproject.IcatException_Exception; - import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.helpers.LocationHelper; -import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatafileInfo; +import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** - * This class is to handle not finished work. It could be redesigned with sub - * classes depending on StorageUnit, but it isn't worth it i guess, just for one - * abstract method (loadDataInfo()). + * This class is to handle not finished work. + * It could be redesigned with sub classes depending on StorageUnit, but it isn't worth it i guess, just for one abstract method (loadDataInfo()). */ public class UnfinishedWorkService { - protected final static Logger logger = LoggerFactory - .getLogger(UnfinishedWorkService.class); - public UnfinishedWorkService() { + protected final static Logger logger = LoggerFactory.getLogger(UnfinishedWorkService.class); + public UnfinishedWorkService() { + } - public void restartUnfinishedWork(Path markerDir, String key) - throws InternalException { + public void restartUnfinishedWork(Path markerDir, String key) throws InternalException { try { var serviceProvider = ServiceProvider.getInstance(); - StorageUnit storageUnit = serviceProvider.getPropertyHandler() - .getStorageUnit(); + StorageUnit storageUnit = serviceProvider.getPropertyHandler().getStorageUnit(); for (File file : markerDir.toFile().listFiles()) { - if (storageUnit == null) - break; - - long id = Long - .parseLong(file.toPath().getFileName().toString()); + if(storageUnit == null) break; + + long id = Long.parseLong(file.toPath().getFileName().toString()); try { DataInfoBase dataInfo = this.loadDataInfo(storageUnit, id); serviceProvider.getFsm().queue(dataInfo, DeferredOp.WRITE); - logger.info("Queued " + storageUnit.toString().toLowerCase() - + " with id " + id + " " + dataInfo + logger.info("Queued " + storageUnit.toString().toLowerCase() + " with id " + id + " " + dataInfo + " to be written as it was not written out previously by IDS"); } catch (IcatException_Exception e) { - if (e.getFaultInfo() - .getType() == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { - logger.warn(storageUnit.toString().toLowerCase() - + " with id " + id + if (e.getFaultInfo().getType() == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { + logger.warn( storageUnit.toString().toLowerCase() + " with id " + id + " was not written out by IDS and now no longer known to ICAT"); Files.delete(file.toPath()); } else { @@ -74,6 +64,7 @@ public void restartUnfinishedWork(Path markerDir, String key) } } + public void cleanPreparedDir(Path preparedDir) { for (File file : preparedDir.toFile().listFiles()) { Path path = file.toPath(); @@ -89,8 +80,7 @@ public void cleanPreparedDir(Path preparedDir) { } thisSize += Files.size(path); Files.delete(path); - logger.debug("Deleted " + path + " to reclaim " + thisSize - + " bytes"); + logger.debug("Deleted " + path + " to reclaim " + thisSize + " bytes"); } catch (IOException e) { logger.debug("Failed to delete " + path + e.getMessage()); } @@ -98,37 +88,31 @@ public void cleanPreparedDir(Path preparedDir) { } } + public void cleanDatasetCache(Path datasetDir) { for (File dsFile : datasetDir.toFile().listFiles()) { Path path = dsFile.toPath(); try { long thisSize = Files.size(path); Files.delete(path); - logger.debug("Deleted " + path + " to reclaim " + thisSize - + " bytes"); + logger.debug("Deleted " + path + " to reclaim " + thisSize + " bytes"); } catch (IOException e) { - logger.debug("Failed to delete " + path + " " + e.getClass() - + " " + e.getMessage()); + logger.debug("Failed to delete " + path + " " + e.getClass() + " " + e.getMessage()); } } } - private DataInfoBase loadDataInfo(StorageUnit storageUnit, long id) - throws IcatException_Exception, InsufficientPrivilegesException, - InternalException { - if (storageUnit == StorageUnit.DATASET) { - Dataset ds = (Dataset) ServiceProvider.getInstance().getIcatReader() - .get("Dataset ds INCLUDE ds.investigation.facility", id); + + private DataInfoBase loadDataInfo(StorageUnit storageUnit, long id) throws IcatException_Exception, InsufficientPrivilegesException, InternalException { + if(storageUnit == StorageUnit.DATASET) { + Dataset ds = (Dataset) ServiceProvider.getInstance().getIcatReader().get("Dataset ds INCLUDE ds.investigation.facility", id); return new DatasetInfo(ds); } - if (storageUnit == StorageUnit.DATAFILE) { - Datafile df = (Datafile) ServiceProvider.getInstance() - .getIcatReader().get("Datafile ds INCLUDE ds.dataset", id); - String location = LocationHelper.getLocation(df.getId(), - df.getLocation()); - return new DatafileInfo(id, df.getName(), location, - df.getCreateId(), df.getModId(), df.getDataset().getId()); + if(storageUnit == StorageUnit.DATAFILE) { + Datafile df = (Datafile) ServiceProvider.getInstance().getIcatReader().get("Datafile ds INCLUDE ds.dataset", id); + String location = LocationHelper.getLocation(df.getId(), df.getLocation()); + return new DatafileInfo(id, df.getName(), location, df.getCreateId(), df.getModId(), df.getDataset().getId()); } return null; diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java index c0a5f1cf..650e410f 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionService.java @@ -16,34 +16,30 @@ import java.util.concurrent.Future; import java.util.concurrent.locks.ReentrantLock; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.icatproject.IcatException_Exception; - import org.icatproject.ids.enums.DeferredOp; import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.exceptions.BadRequestException; import org.icatproject.ids.exceptions.DataNotOnlineException; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.exceptions.NotImplementedException; +import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DataSelection; -import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.services.ServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class DataSelectionService { - protected final static Logger logger = LoggerFactory - .getLogger(DataSelectionService.class); + protected final static Logger logger = LoggerFactory.getLogger(DataSelectionService.class); protected DataSelection dataSelection; protected RequestType requestType; + protected static ExecutorService threadPool; - static { - threadPool = Executors.newCachedThreadPool(); - } + static { threadPool = Executors.newCachedThreadPool(); } private Map preparedStatusMap = new ConcurrentHashMap<>(); @@ -54,54 +50,41 @@ class PreparedStatus { } - protected DataSelectionService(SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, - Boolean zip, Boolean compress, RequestType requestType) { + protected DataSelectionService(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, + List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { - this.dataSelection = new DataSelection(dsInfos, dfInfos, emptyDatasets, - invids2, dsids, dfids, length, zip, compress); + this.dataSelection = new DataSelection(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress); this.requestType = requestType; } - public abstract boolean isPrepared(String preparedId) - throws InternalException; + public abstract boolean isPrepared(String preparedId) throws InternalException; /** - * To get the DataInfos that is currently worked with, depending on - * StorageUnit - * + * To get the DataInfos that is currently worked with, depending on StorageUnit * @return */ public abstract SortedMap getPrimaryDataInfos(); - public abstract boolean existsInMainStorage(DataInfoBase dataInfo) - throws InternalException; + public abstract boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException; - public abstract void queueDelete() - throws NotImplementedException, InternalException; + public abstract void queueDelete() throws NotImplementedException, InternalException; - public abstract void scheduleTasks(DeferredOp operation) - throws NotImplementedException, InternalException; + public abstract void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException; - // TODO: maybe implementing this method here whould be the better way, than - // making it abstract. But we will miss the NotImplementedException for - // SingleLevelStorage in that case. + //TODO: maybe implementing this method here whould be the better way, than making it abstract. But we will miss the NotImplementedException for SingleLevelStorage in that case. // should we change the tests and the behavior of the ids server here - // public void scheduleTasks(DeferredOp operation) throws - // NotImplementedException, InternalException { - // for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { // in - // case of SingleLevelStorage an empty map is returned and nothing will - // happen (even not an exception). - // ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); - // } + // public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { + // for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { // in case of SingleLevelStorage an empty map is returned and nothing will happen (even not an exception). + // ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); + // } // } public SortedMap getDsInfo() { return this.dataSelection.getDsInfo(); } + public SortedMap getDfInfo() { return this.dataSelection.getDfInfo(); } @@ -118,6 +101,7 @@ public long getLength() { return this.dataSelection.getLength(); } + public boolean mustZip() { return this.dataSelection.mustZip(); } @@ -126,22 +110,21 @@ public boolean isSingleDataset() { return this.dataSelection.isSingleDataset(); } + public Set getEmptyDatasets() { return this.dataSelection.getEmptyDatasets(); } + /** - * tries to extract a list of ids from a comma separated id string. No - * spaces or leading 0's. Also accepts null. - * - * @param thing the name of the id list - for better error message - * @param idList a String which shoald contain long numbers seperated by - * commas + * tries to extract a list of ids from a comma separated id string. No spaces or leading 0's. Also + * accepts null. + * @param thing the name of the id list - for better error message + * @param idList a String which shoald contain long numbers seperated by commas * @return list of long numbers - the extracted id values * @throws BadRequestException */ - public static List getValidIds(String thing, String idList) - throws BadRequestException { + public static List getValidIds(String thing, String idList) throws BadRequestException { List result; if (idList == null) { @@ -153,8 +136,7 @@ public static List getValidIds(String thing, String idList) try { result.add(Long.parseLong(id)); } catch (NumberFormatException e) { - throw new BadRequestException("The " + thing - + " parameter '" + idList + "' is not a valid " + throw new BadRequestException("The " + thing + " parameter '" + idList + "' is not a valid " + "string representation of a comma separated list of longs"); } } @@ -162,8 +144,8 @@ public static List getValidIds(String thing, String idList) return result; } - private boolean restoreIfOffline(DataInfoBase dataInfo) - throws InternalException { + + private boolean restoreIfOffline(DataInfoBase dataInfo) throws InternalException { boolean maybeOffline = false; var serviceProvider = ServiceProvider.getInstance(); if (serviceProvider.getFsm().getMaybeOffline().contains(dataInfo)) { @@ -175,7 +157,8 @@ private boolean restoreIfOffline(DataInfoBase dataInfo) return maybeOffline; } - public void checkOnline() throws InternalException, DataNotOnlineException { + + public void checkOnline()throws InternalException, DataNotOnlineException { boolean maybeOffline = false; for (DataInfoBase dfInfo : this.getPrimaryDataInfos().values()) { @@ -189,59 +172,51 @@ public void checkOnline() throws InternalException, DataNotOnlineException { } } + public void restoreDataInfos() { var dataInfos = this.getPrimaryDataInfos().values(); - if (!dataInfos.isEmpty()) { + if(!dataInfos.isEmpty()) { for (DataInfoBase dataInfo : dataInfos) { - ServiceProvider.getInstance().getFsm() - .recordSuccess(dataInfo.getId()); + ServiceProvider.getInstance().getFsm().recordSuccess(dataInfo.getId()); } threadPool.submit(new RestoreDataInfoTask(dataInfos, this, false)); } } + public OptionalLong getFileLength() { - if (this.dataSelection.getDfInfo().isEmpty() - || this.dataSelection.mustZip()) { + if (this.dataSelection.getDfInfo().isEmpty() || this.dataSelection.mustZip()) { return OptionalLong.empty(); } return OptionalLong.of(this.dataSelection.getLength()); } - protected boolean areDataInfosPrepared(String preparedId) - throws InternalException { + + protected boolean areDataInfosPrepared(String preparedId) throws InternalException { boolean prepared = true; var serviceProvider = ServiceProvider.getInstance(); - PreparedStatus status = preparedStatusMap.computeIfAbsent(preparedId, - k -> new PreparedStatus()); + PreparedStatus status = preparedStatusMap.computeIfAbsent(preparedId, k -> new PreparedStatus()); - Collection toCheck = status.fromElement == null - ? this.getPrimaryDataInfos().values() - : this.getPrimaryDataInfos().tailMap(status.fromElement) - .values(); + Collection toCheck = status.fromElement == null ? this.getPrimaryDataInfos().values() + : this.getPrimaryDataInfos().tailMap(status.fromElement).values(); logger.debug("Will check online status of {} entries", toCheck.size()); for (DataInfoBase dataInfo : toCheck) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (this.restoreIfOffline(dataInfo)) { prepared = false; status.fromElement = dataInfo.getId(); - toCheck = this.getPrimaryDataInfos().tailMap(status.fromElement) - .values(); - logger.debug("Will check in background status of {} entries", - toCheck.size()); - status.future = threadPool - .submit(new RestoreDataInfoTask(toCheck, this, true)); + toCheck = this.getPrimaryDataInfos().tailMap(status.fromElement).values(); + logger.debug("Will check in background status of {} entries", toCheck.size()); + status.future = threadPool.submit(new RestoreDataInfoTask(toCheck, this, true)); break; } } if (prepared) { toCheck = status.fromElement == null ? Collections.emptySet() - : this.getPrimaryDataInfos().headMap(status.fromElement) - .values(); - logger.debug("Will check finally online status of {} entries", - toCheck.size()); + : this.getPrimaryDataInfos().headMap(status.fromElement).values(); + logger.debug("Will check finally online status of {} entries", toCheck.size()); for (DataInfoBase dataInfo : toCheck) { serviceProvider.getFsm().checkFailure(dataInfo.getId()); if (this.restoreIfOffline(dataInfo)) { @@ -253,52 +228,47 @@ protected boolean areDataInfosPrepared(String preparedId) return prepared; } + public void delete() throws InternalException, NotImplementedException { var serviceProvider = ServiceProvider.getInstance(); /* - * Delete the local copy directly rather than queueing it as it has been - * removed from ICAT so will not be accessible to any subsequent IDS - * calls. + * Delete the local copy directly rather than queueing it as it has + * been removed from ICAT so will not be accessible to any + * subsequent IDS calls. */ for (DataInfoBase dataInfo : this.dataSelection.getDfInfo().values()) { var dfInfo = (DatafileInfo) dataInfo; String location = dataInfo.getLocation(); try { - if ((long) serviceProvider.getIcatReader().search( - "SELECT COUNT(df) FROM Datafile df WHERE df.location LIKE '" - + location.replaceAll("'", "''") + "%'") + if ((long) serviceProvider.getIcatReader() + .search("SELECT COUNT(df) FROM Datafile df WHERE df.location LIKE '" + location.replaceAll("'", "''") + "%'") .get(0) == 0) { if (serviceProvider.getMainStorage().exists(location)) { - logger.debug("Delete physical file " + location - + " from main storage"); - serviceProvider.getMainStorage().delete(location, - dfInfo.getCreateId(), dfInfo.getModId()); + logger.debug("Delete physical file " + location + " from main storage"); + serviceProvider.getMainStorage().delete(location, dfInfo.getCreateId(), dfInfo.getModId()); } } } catch (IcatException_Exception e) { - throw new InternalException( - e.getFaultInfo().getType() + " " + e.getMessage()); + throw new InternalException(e.getFaultInfo().getType() + " " + e.getMessage()); } catch (IOException e) { - logger.error( - "I/O error " + e.getMessage() + " deleting " + dfInfo); - throw new InternalException( - e.getClass() + " " + e.getMessage()); + logger.error("I/O error " + e.getMessage() + " deleting " + dfInfo); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } this.queueDelete(); } + private class RestoreDataInfoTask implements Callable { private Collection dataInfos; private DataSelectionService dataselection; private boolean checkFailure; - public RestoreDataInfoTask(Collection dataInfos, - DataSelectionService dataSelection, boolean checkFailure) { + public RestoreDataInfoTask(Collection dataInfos, DataSelectionService dataSelection, boolean checkFailure) { this.dataInfos = dataInfos; this.dataselection = dataSelection; this.checkFailure = checkFailure; @@ -307,14 +277,13 @@ public RestoreDataInfoTask(Collection dataInfos, @Override public Void call() throws Exception { for (DataInfoBase dataInfo : dataInfos) { - if (checkFailure) - ServiceProvider.getInstance().getFsm() - .checkFailure(dataInfo.getId()); + if(checkFailure) + ServiceProvider.getInstance().getFsm().checkFailure(dataInfo.getId()); dataselection.restoreIfOffline(dataInfo); } return null; } - + } } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java index e05f76aa..25ff3ccb 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceFactory.java @@ -11,13 +11,6 @@ import java.util.SortedMap; import java.util.TreeMap; -import jakarta.json.Json; -import jakarta.json.JsonArray; -import jakarta.json.JsonValue; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.icatproject.Datafile; import org.icatproject.Dataset; import org.icatproject.ICAT; @@ -25,7 +18,6 @@ import org.icatproject.IcatException_Exception; import org.icatproject.icat.client.IcatException; import org.icatproject.icat.client.Session; - import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.exceptions.BadRequestException; @@ -34,18 +26,23 @@ import org.icatproject.ids.exceptions.NotFoundException; import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.helpers.LocationHelper; -import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatafileInfo; +import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.models.Prepared; import org.icatproject.ids.services.IcatReader; import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.ServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import jakarta.json.Json; +import jakarta.json.JsonArray; +import jakarta.json.JsonValue; public class DataSelectionServiceFactory { - private final static Logger logger = LoggerFactory - .getLogger(DataSelectionServiceFactory.class); + private final static Logger logger = LoggerFactory.getLogger(DataSelectionServiceFactory.class); private static DataSelectionServiceFactory instance = null; @@ -60,41 +57,30 @@ public enum Returns { DATASETS, DATASETS_AND_DATAFILES, DATAFILES } - public static DataSelectionServiceFactory getInstance() - throws InternalException { + public static DataSelectionServiceFactory getInstance() throws InternalException { if (instance == null) { var serviceProvider = ServiceProvider.getInstance(); - instance = new DataSelectionServiceFactory( - serviceProvider.getPropertyHandler(), - serviceProvider.getIcatReader()); + instance = new DataSelectionServiceFactory(serviceProvider.getPropertyHandler(), serviceProvider.getIcatReader()); } return instance; } - public static DataSelectionServiceFactory getInstanceOnlyForTesting( - PropertyHandler propertyHandler, IcatReader reader) - throws InternalException { + public static DataSelectionServiceFactory getInstanceOnlyForTesting(PropertyHandler propertyHandler, IcatReader reader) throws InternalException { if (instance == null) { instance = new DataSelectionServiceFactory(propertyHandler, reader); } return instance; } - public static DataSelectionService getService(String userSessionId, - String investigationIds, String datasetIds, String datafileIds, - RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, - InsufficientPrivilegesException, NotImplementedException { + public static DataSelectionService getService(String userSessionId, String investigationIds, String datasetIds, String datafileIds, RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { - return DataSelectionServiceFactory.getInstance().getSelectionService( - userSessionId, investigationIds, datasetIds, datafileIds, - requestType); + return DataSelectionServiceFactory.getInstance().getSelectionService(userSessionId, investigationIds, datasetIds, datafileIds, requestType); } + /** - * created a DataSelectionBase object from already created data infos. - * That's why no infos must be loaded from ICAT. - * + * created a DataSelectionBase object from already created data infos. That's why no infos must be loaded from ICAT. * @param dsInfos * @param dfInfos * @param emptyDatasets @@ -103,41 +89,33 @@ public static DataSelectionService getService(String userSessionId, * @return * @throws InternalException */ - public static DataSelectionService getService( - SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - long fileLength, RequestType requestType) throws InternalException { + public static DataSelectionService getService(SortedMap dsInfos, SortedMap dfInfos, Set emptyDatasets, long fileLength, RequestType requestType) throws InternalException { - var prepared = new Prepared(dsInfos, dfInfos, emptyDatasets, - fileLength); + var prepared = new Prepared(dsInfos, dfInfos, emptyDatasets, fileLength); return DataSelectionServiceFactory.getService(prepared, requestType); } - public static DataSelectionService getService(Prepared prepared, - RequestType requestType) throws InternalException { + public static DataSelectionService getService(Prepared prepared, RequestType requestType) throws InternalException { List dsids = new ArrayList(prepared.dsInfos.keySet()); List dfids = new ArrayList(); List invIds = new ArrayList(); var dataFileInfos = new HashMap(); - for (DataInfoBase dfInfo : prepared.dfInfos.values()) { + for(DataInfoBase dfInfo: prepared.dfInfos.values()) { dfids.add(dfInfo.getId()); dataFileInfos.put(dfInfo.getId(), dfInfo); dfids.add(dfInfo.getId()); } - for (DataInfoBase dsInfo : prepared.dsInfos.values()) { + for(DataInfoBase dsInfo : prepared.dsInfos.values()) { dsids.add(dsInfo.getId()); - invIds.add(((DatasetInfo) dsInfo).getInvId()); + invIds.add( ((DatasetInfo)dsInfo).getInvId() ); } - return DataSelectionServiceFactory.getInstance().createSelectionService( - prepared.dsInfos, prepared.dfInfos, prepared.emptyDatasets, - invIds, dsids, dfids, prepared.fileLength, prepared.zip, - prepared.compress, requestType); + return DataSelectionServiceFactory.getInstance().createSelectionService(prepared.dsInfos, prepared.dfInfos, prepared.emptyDatasets, invIds, dsids, dfids, prepared.fileLength, prepared.zip, prepared.compress, requestType); } - private DataSelectionServiceFactory(PropertyHandler propertyHandler, - IcatReader reader) throws InternalException { + private DataSelectionServiceFactory(PropertyHandler propertyHandler, IcatReader reader) throws InternalException + { logger.info("### Constructing..."); this.propertyHandler = propertyHandler; this.icat = propertyHandler.getIcatService(); @@ -150,66 +128,47 @@ private DataSelectionServiceFactory(PropertyHandler propertyHandler, logger.info("### Constructing finished"); } - private DataSelectionService createSelectionService( - SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - List invids, List dsids, List dfids, long length, - Boolean zip, Boolean compress, RequestType requestType) - throws InternalException { + private DataSelectionService createSelectionService(SortedMap dsInfos, SortedMap dfInfos, + Set emptyDatasets, List invids, List dsids, + List dfids, long length, Boolean zip, Boolean compress, + RequestType requestType) throws InternalException { StorageUnit storageUnit = this.propertyHandler.getStorageUnit(); - if (storageUnit == null) - return new DataSelectionServiceForSingleLevelStorage(dsInfos, - dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, - compress, requestType); + if(storageUnit == null ) + return new DataSelectionServiceForSingleLevelStorage(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); else if (storageUnit == StorageUnit.DATAFILE) - return new DataSelectionServiceForStorageUnitDatafile(dsInfos, - dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, - compress, requestType); + return new DataSelectionServiceForStorageUnitDatafile(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); - else if (storageUnit == StorageUnit.DATASET) - return new DataSelectionServiceForStorageUnitDataset(dsInfos, - dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, - compress, requestType); + else if(storageUnit == StorageUnit.DATASET) + return new DataSelectionServiceForStorageUnitDataset(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, zip, compress, requestType); - else - throw new InternalException("StorageUnit " + storageUnit - + " unknown. Maybe you forgot to handle a new StorageUnit here?"); + else throw new InternalException("StorageUnit " + storageUnit + " unknown. Maybe you forgot to handle a new StorageUnit here?"); } - public DataSelectionService getSelectionService(String userSessionId, - String investigationIds, String datasetIds, String datafileIds, - RequestType requestType) - throws InternalException, BadRequestException, NotFoundException, - InsufficientPrivilegesException, NotImplementedException { - + public DataSelectionService getSelectionService( String userSessionId, String investigationIds, String datasetIds, String datafileIds, RequestType requestType) + throws InternalException, BadRequestException, NotFoundException, InsufficientPrivilegesException, NotImplementedException { + List dfids = getValidIds("datafileIds", datafileIds); List dsids = getValidIds("datasetIds", datasetIds); List invids = getValidIds("investigationIds", investigationIds); Returns returns = this.getReturns(requestType); - boolean dfWanted = returns == Returns.DATASETS_AND_DATAFILES - || returns == Returns.DATAFILES; - boolean dsWanted = returns == Returns.DATASETS_AND_DATAFILES - || returns == Returns.DATASETS; + boolean dfWanted = returns == Returns.DATASETS_AND_DATAFILES || returns == Returns.DATAFILES; + boolean dsWanted = returns == Returns.DATASETS_AND_DATAFILES || returns == Returns.DATASETS; Session userRestSession = restIcat.getSession(userSessionId); // by default use the user's REST ICAT session Session restSessionToUse = userRestSession; - + try { - logger.debug("useReaderForPerformance = {}", - propertyHandler.getUseReaderForPerformance()); + logger.debug("useReaderForPerformance = {}", propertyHandler.getUseReaderForPerformance()); if (propertyHandler.getUseReaderForPerformance()) { - // if this is set, use a REST session for the reader account - // where possible - // to improve performance due to the final database queries - // being simpler - restSessionToUse = restIcat - .getSession(this.icatReader.getSessionId()); + // if this is set, use a REST session for the reader account where possible + // to improve performance due to the final database queries being simpler + restSessionToUse = restIcat.getSession(this.icatReader.getSessionId()); } } catch (IcatException_Exception e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -217,23 +176,20 @@ public DataSelectionService getSelectionService(String userSessionId, logger.debug("dfids: {} dsids: {} invids: {}", dfids, dsids, invids); - return prepareFromIds(dfWanted, dsWanted, dfids, dsids, invids, - userSessionId, restSessionToUse, userRestSession, requestType); + return prepareFromIds(dfWanted, dsWanted, dfids, dsids, invids, userSessionId, restSessionToUse, userRestSession, requestType); } - private DataSelectionService prepareFromIds(boolean dfWanted, - boolean dsWanted, List dfids, List dsids, - List invids, String userSessionId, Session restSessionToUse, - Session userRestSession, RequestType requestType) - throws NotFoundException, InsufficientPrivilegesException, - InternalException, BadRequestException { + + + private DataSelectionService prepareFromIds(boolean dfWanted, boolean dsWanted, List dfids, List dsids, List invids, String userSessionId, Session restSessionToUse, Session userRestSession, RequestType requestType) + throws NotFoundException, InsufficientPrivilegesException, InternalException, BadRequestException { var dsInfos = new TreeMap(); var emptyDatasets = new HashSet(); var dfInfos = new TreeMap(); long length = 0; - if (dfWanted) { // redundant ? + if (dfWanted) { //redundant ? dfInfos = new TreeMap(); } @@ -241,22 +197,18 @@ private DataSelectionService prepareFromIds(boolean dfWanted, for (Long dfid : dfids) { List dss = icat.search(userSessionId, - "SELECT ds FROM Dataset ds JOIN ds.datafiles df WHERE df.id = " - + dfid + "SELECT ds FROM Dataset ds JOIN ds.datafiles df WHERE df.id = " + dfid + " AND df.location IS NOT NULL INCLUDE ds.investigation.facility"); if (dss.size() == 1) { Dataset ds = (Dataset) dss.get(0); long dsid = ds.getId(); dsInfos.put(dsid, new DatasetInfo(ds)); if (dfWanted) { - Datafile df = (Datafile) icat.get(userSessionId, - "Datafile", dfid); + Datafile df = (Datafile) icat.get(userSessionId, "Datafile", dfid); length += df.getFileSize(); - String location = LocationHelper.getLocation(dfid, - df.getLocation()); - dfInfos.put(df.getId(), - new DatafileInfo(dfid, df.getName(), location, - df.getCreateId(), df.getModId(), dsid)); + String location = LocationHelper.getLocation(dfid, df.getLocation()); + dfInfos.put( df.getId(), + new DatafileInfo(dfid, df.getName(), location, df.getCreateId(), df.getModId(), dsid)); } } else { // Next line may reveal a permissions problem @@ -266,18 +218,13 @@ private DataSelectionService prepareFromIds(boolean dfWanted, } for (Long dsid : dsids) { - Dataset ds = (Dataset) icat.get(userSessionId, - "Dataset ds INCLUDE ds.investigation.facility", dsid); + Dataset ds = (Dataset) icat.get(userSessionId, "Dataset ds INCLUDE ds.investigation.facility", dsid); dsInfos.put(dsid, new DatasetInfo(ds)); - // dataset access for the user has been checked so the REST - // session for the - // reader account can be used if the IDS setting to allow this - // is enabled + // dataset access for the user has been checked so the REST session for the + // reader account can be used if the IDS setting to allow this is enabled String query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid + " AND df.location IS NOT NULL"; - JsonArray result = Json - .createReader(new ByteArrayInputStream( - restSessionToUse.search(query).getBytes())) + JsonArray result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())) .readArray().getJsonArray(0); if (result.getJsonNumber(2).longValueExact() == 0) { // Count 0 emptyDatasets.add(dsid); @@ -289,38 +236,31 @@ private DataSelectionService prepareFromIds(boolean dfWanted, for (Long invid : invids) { String query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + invid; - JsonArray result = Json - .createReader(new ByteArrayInputStream( - userRestSession.search(query).getBytes())) + JsonArray result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())) .readArray().getJsonArray(0); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, - userRestSession, restSessionToUse, result); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUse, result); } } catch (IcatException_Exception e) { IcatExceptionType type = e.getFaultInfo().getType(); - if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES - || type == IcatExceptionType.SESSION) { + if (type == IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } else if (type == IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } else { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IcatException e) { - org.icatproject.icat.client.IcatException.IcatExceptionType type = e - .getType(); + org.icatproject.icat.client.IcatException.IcatExceptionType type = e.getType(); if (type == org.icatproject.icat.client.IcatException.IcatExceptionType.INSUFFICIENT_PRIVILEGES || type == org.icatproject.icat.client.IcatException.IcatExceptionType.SESSION) { throw new InsufficientPrivilegesException(e.getMessage()); } else if (type == org.icatproject.icat.client.IcatException.IcatExceptionType.NO_SUCH_OBJECT_FOUND) { throw new NotFoundException(e.getMessage()); } else { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } /* @@ -332,17 +272,16 @@ private DataSelectionService prepareFromIds(boolean dfWanted, emptyDatasets = null; } - return this.createSelectionService(dsInfos, dfInfos, emptyDatasets, - invids, dsids, dfids, length, false, false, requestType); + return this.createSelectionService(dsInfos, dfInfos, emptyDatasets, invids, dsids, dfids, length, false, false, requestType); } + /** * Checks to see if the investigation, dataset or datafile id list is a * valid comma separated list of longs. No spaces or leading 0's. Also * accepts null. */ - public static List getValidIds(String thing, String idList) - throws BadRequestException { + public static List getValidIds(String thing, String idList) throws BadRequestException { List result; if (idList == null) { @@ -354,8 +293,7 @@ public static List getValidIds(String thing, String idList) try { result.add(Long.parseLong(id)); } catch (NumberFormatException e) { - throw new BadRequestException("The " + thing - + " parameter '" + idList + "' is not a valid " + throw new BadRequestException("The " + thing + " parameter '" + idList + "' is not a valid " + "string representation of a comma separated list of longs"); } } @@ -363,13 +301,10 @@ public static List getValidIds(String thing, String idList) return result; } - private void manyDfs(Map dfInfos, long dsid, - Session restSessionToUse, JsonArray result) throws IcatException, - InsufficientPrivilegesException, InternalException { - // dataset access for the user has been checked so the REST session for - // the - // reader account can be used if the IDS setting to allow this is - // enabled + private void manyDfs(Map dfInfos, long dsid, Session restSessionToUse, JsonArray result) + throws IcatException, InsufficientPrivilegesException, InternalException { + // dataset access for the user has been checked so the REST session for the + // reader account can be used if the IDS setting to allow this is enabled long min = result.getJsonNumber(0).longValueExact(); long max = result.getJsonNumber(1).longValueExact(); long count = result.getJsonNumber(2).longValueExact(); @@ -377,52 +312,33 @@ private void manyDfs(Map dfInfos, long dsid, if (count != 0) { if (count <= maxEntities) { String query = "SELECT df.id, df.name, df.location, df.createId, df.modId FROM Datafile df WHERE df.dataset.id = " - + dsid - + " AND df.location IS NOT NULL AND df.id BETWEEN " - + min + " AND " + max; - result = Json - .createReader(new ByteArrayInputStream( - restSessionToUse.search(query).getBytes())) - .readArray(); + + dsid + " AND df.location IS NOT NULL AND df.id BETWEEN " + min + " AND " + max; + result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray(); for (JsonValue tupV : result) { JsonArray tup = (JsonArray) tupV; long dfid = tup.getJsonNumber(0).longValueExact(); - String location = LocationHelper.getLocation(dfid, - tup.getString(2, null)); + String location = LocationHelper.getLocation(dfid, tup.getString(2, null)); dfInfos.put(dfid, - new DatafileInfo(dfid, tup.getString(1), location, - tup.getString(3), tup.getString(4), dsid)); + new DatafileInfo(dfid, tup.getString(1), location, tup.getString(3), tup.getString(4), dsid)); } } else { long half = (min + max) / 2; String query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " - + dsid - + " AND df.location IS NOT NULL AND df.id BETWEEN " - + min + " AND " + half; - result = Json - .createReader(new ByteArrayInputStream( - restSessionToUse.search(query).getBytes())) - .readArray().getJsonArray(0); + + dsid + " AND df.location IS NOT NULL AND df.id BETWEEN " + min + " AND " + half; + result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray() + .getJsonArray(0); manyDfs(dfInfos, dsid, restSessionToUse, result); - query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " - + dsid - + " AND df.location IS NOT NULL AND df.id BETWEEN " - + (half + 1) + " AND " + max; - result = Json - .createReader(new ByteArrayInputStream( - restSessionToUse.search(query).getBytes())) - .readArray().getJsonArray(0); + query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid + + " AND df.location IS NOT NULL AND df.id BETWEEN " + (half + 1) + " AND " + max; + result = Json.createReader(new ByteArrayInputStream(restSessionToUse.search(query).getBytes())).readArray() + .getJsonArray(0); manyDfs(dfInfos, dsid, restSessionToUse, result); } } } - private void manyDss(Map dsInfos, - HashSet emptyDatasets, Map dfInfos, - Long invid, boolean dfWanted, Session userRestSession, - Session restSessionToUseForDfs, JsonArray result) - throws IcatException, InsufficientPrivilegesException, - InternalException { + private void manyDss(Map dsInfos, HashSet emptyDatasets, Map dfInfos, Long invid, boolean dfWanted, Session userRestSession, Session restSessionToUseForDfs, JsonArray result) + throws IcatException, InsufficientPrivilegesException, InternalException { long min = result.getJsonNumber(0).longValueExact(); long max = result.getJsonNumber(1).longValueExact(); long count = result.getJsonNumber(2).longValueExact(); @@ -431,10 +347,7 @@ private void manyDss(Map dsInfos, if (count <= maxEntities) { String query = "SELECT inv.name, inv.visitId, inv.facility.id, inv.facility.name FROM Investigation inv WHERE inv.id = " + invid; - result = Json - .createReader(new ByteArrayInputStream( - userRestSession.search(query).getBytes())) - .readArray(); + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); if (result.size() == 0) { return; } @@ -444,25 +357,18 @@ private void manyDss(Map dsInfos, long facilityId = result.getJsonNumber(2).longValueExact(); String facilityName = result.getString(3); - query = "SELECT ds.id, ds.name, ds.location FROM Dataset ds WHERE ds.investigation.id = " - + invid + " AND ds.id BETWEEN " + min + " AND " + max; - result = Json - .createReader(new ByteArrayInputStream( - userRestSession.search(query).getBytes())) - .readArray(); + query = "SELECT ds.id, ds.name, ds.location FROM Dataset ds WHERE ds.investigation.id = " + invid + + " AND ds.id BETWEEN " + min + " AND " + max; + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); for (JsonValue tupV : result) { JsonArray tup = (JsonArray) tupV; long dsid = tup.getJsonNumber(0).longValueExact(); - dsInfos.put(dsid, - new DatasetInfo(dsid, tup.getString(1), - tup.getString(2, null), invid, invName, - visitId, facilityId, facilityName)); + dsInfos.put(dsid, new DatasetInfo(dsid, tup.getString(1), tup.getString(2, null), invid, invName, + visitId, facilityId, facilityName)); query = "SELECT min(df.id), max(df.id), count(df.id) FROM Datafile df WHERE df.dataset.id = " + dsid + " AND df.location IS NOT NULL"; - result = Json - .createReader(new ByteArrayInputStream( - userRestSession.search(query).getBytes())) + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())) .readArray().getJsonArray(0); if (result.getJsonNumber(2).longValueExact() == 0) { emptyDatasets.add(dsid); @@ -475,21 +381,13 @@ private void manyDss(Map dsInfos, long half = (min + max) / 2; String query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " + invid + " AND ds.id BETWEEN " + min + " AND " + half; - result = Json - .createReader(new ByteArrayInputStream( - userRestSession.search(query).getBytes())) - .readArray(); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, - userRestSession, restSessionToUseForDfs, result); + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray(); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUseForDfs, result); query = "SELECT min(ds.id), max(ds.id), count(ds.id) FROM Dataset ds WHERE ds.investigation.id = " - + invid + " AND ds.id BETWEEN " + half + 1 + " AND " - + max; - result = Json - .createReader(new ByteArrayInputStream( - userRestSession.search(query).getBytes())) - .readArray().getJsonArray(0); - manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, - userRestSession, restSessionToUseForDfs, result); + + invid + " AND ds.id BETWEEN " + half + 1 + " AND " + max; + result = Json.createReader(new ByteArrayInputStream(userRestSession.search(query).getBytes())).readArray() + .getJsonArray(0); + manyDss(dsInfos, emptyDatasets, dfInfos, invid, dfWanted, userRestSession, restSessionToUseForDfs, result); } } @@ -500,60 +398,42 @@ private void createRequestTypeToReturnsMapping() throws InternalException { this.requestTypeToReturnsMapping = new HashMap(); StorageUnit storageUnit = this.propertyHandler.getStorageUnit(); - this.requestTypeToReturnsMapping.put(RequestType.DELETE, - Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETDATAFILEIDS, - Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETSIZE, - Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.PREPAREDATA, - Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.RESET, - Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.WRITE, - Returns.DATASETS_AND_DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.GETDATA, - Returns.DATASETS_AND_DATAFILES); - - if (storageUnit == null) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, - Returns.DATASETS); - } else if (storageUnit == StorageUnit.DATAFILE) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, - Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.RESTORE, - Returns.DATAFILES); - this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, - Returns.DATAFILES); - } else if (storageUnit == StorageUnit.DATASET) { - this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, - Returns.DATASETS); - this.requestTypeToReturnsMapping.put(RequestType.RESTORE, - Returns.DATASETS); - this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, - Returns.DATASETS); + this.requestTypeToReturnsMapping.put(RequestType.DELETE, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETDATAFILEIDS, Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETSIZE, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.PREPAREDATA, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.RESET, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.WRITE, Returns.DATASETS_AND_DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.GETDATA, Returns.DATASETS_AND_DATAFILES); + + if(storageUnit == null ) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATASETS); + } + else if (storageUnit == StorageUnit.DATAFILE) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.RESTORE, Returns.DATAFILES); + this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, Returns.DATAFILES); } + else if(storageUnit == StorageUnit.DATASET) { + this.requestTypeToReturnsMapping.put(RequestType.GETSTATUS, Returns.DATASETS); + this.requestTypeToReturnsMapping.put(RequestType.RESTORE, Returns.DATASETS); + this.requestTypeToReturnsMapping.put(RequestType.ARCHIVE, Returns.DATASETS); + } + - else - throw new InternalException("StorageUnit " + storageUnit - + " unknown. Maybe you forgot to handle a new StorageUnit here?"); + else throw new InternalException("StorageUnit " + storageUnit + " unknown. Maybe you forgot to handle a new StorageUnit here?"); } - private Returns getReturns(RequestType requestType) - throws NotImplementedException { + private Returns getReturns(RequestType requestType) throws NotImplementedException { - if (this.requestTypeToReturnsMapping.containsKey(requestType)) + if(this.requestTypeToReturnsMapping.containsKey(requestType)) return this.requestTypeToReturnsMapping.get(requestType); + // is this needed here? - // if(this.propertyHandler.getStorageUnit() == null) throw new - // NotImplementedException("This operation is unavailable for single - // level storage"); - - throw new NotImplementedException("There is to mapping for RequestType." - + requestType + " and StorageUnit." - + this.propertyHandler.getStorageUnit() - + " defined. Did you forgot to register it in createRequestTypeToReturnsMapping()?"); + //if(this.propertyHandler.getStorageUnit() == null) throw new NotImplementedException("This operation is unavailable for single level storage"); + + throw new NotImplementedException("There is to mapping for RequestType." + requestType + " and StorageUnit." + this.propertyHandler.getStorageUnit() + " defined. Did you forgot to register it in createRequestTypeToReturnsMapping()?"); } } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java index c1a6a8a5..b7915daa 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForSingleLevelStorage.java @@ -11,49 +11,47 @@ import org.icatproject.ids.exceptions.NotImplementedException; import org.icatproject.ids.models.DataInfoBase; -public class DataSelectionServiceForSingleLevelStorage - extends DataSelectionService { +public class DataSelectionServiceForSingleLevelStorage extends DataSelectionService { - protected DataSelectionServiceForSingleLevelStorage( - SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, - Boolean zip, Boolean compress, RequestType requestType) { - - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, - zip, compress, requestType); + protected DataSelectionServiceForSingleLevelStorage(SortedMap dsInfos, SortedMap dfInfos, + Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { + + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); } + @Override public SortedMap getPrimaryDataInfos() { return new TreeMap(); } - @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) - throws InternalException { - throw new InternalException( - "This operation is unavailable for single level storage"); + @Override + public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { + + throw new InternalException("This operation is unavailable for single level storage"); } + @Override public boolean isPrepared(String preparedId) throws InternalException { return true; } + @Override - public void queueDelete() - throws NotImplementedException, InternalException { - // nothing todo for single level storage + public void queueDelete() throws NotImplementedException, InternalException { + //nothing todo for single level storage } + @Override - public void scheduleTasks(DeferredOp operation) - throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { - throw new NotImplementedException( - "This operation is unavailable for single level storage"); + throw new NotImplementedException("This operation is unavailable for single level storage"); } + + + } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java index 18da82fb..f744fa8f 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDatafile.java @@ -11,49 +11,44 @@ import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.services.ServiceProvider; -public class DataSelectionServiceForStorageUnitDatafile - extends DataSelectionService { +public class DataSelectionServiceForStorageUnitDatafile extends DataSelectionService { - protected DataSelectionServiceForStorageUnitDatafile( - SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, - Boolean zip, Boolean compress, RequestType requestType) { + protected DataSelectionServiceForStorageUnitDatafile(SortedMap dsInfos, SortedMap dfInfos, + Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, - zip, compress, requestType); + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); } + @Override public SortedMap getPrimaryDataInfos() { return this.dataSelection.getDfInfo(); } @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) - throws InternalException { - return ServiceProvider.getInstance().getMainStorage() - .exists(dataInfo.getLocation()); + public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { + return ServiceProvider.getInstance().getMainStorage().exists(dataInfo.getLocation()); } + @Override public boolean isPrepared(String preparedId) throws InternalException { return areDataInfosPrepared(preparedId); } + @Override - public void queueDelete() - throws NotImplementedException, InternalException { + public void queueDelete() throws NotImplementedException, InternalException { this.scheduleTasks(DeferredOp.DELETE); } + @Override - public void scheduleTasks(DeferredOp operation) - throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); } } - + } diff --git a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java index d75a4920..ff1b5c65 100644 --- a/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java +++ b/src/main/java/org/icatproject/ids/services/dataSelectionService/DataSelectionServiceForStorageUnitDataset.java @@ -12,56 +12,49 @@ import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.services.ServiceProvider; -public class DataSelectionServiceForStorageUnitDataset - extends DataSelectionService { +public class DataSelectionServiceForStorageUnitDataset extends DataSelectionService { - protected DataSelectionServiceForStorageUnitDataset( - SortedMap dsInfos, - SortedMap dfInfos, Set emptyDatasets, - List invids2, List dsids, List dfids, long length, - Boolean zip, Boolean compress, RequestType requestType) { + protected DataSelectionServiceForStorageUnitDataset(SortedMap dsInfos, SortedMap dfInfos, + Set emptyDatasets, List invids2, List dsids, List dfids, long length, Boolean zip, Boolean compress, RequestType requestType) { - super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, - zip, compress, requestType); + super(dsInfos, dfInfos, emptyDatasets, invids2, dsids, dfids, length, zip, compress, requestType); } + @Override public SortedMap getPrimaryDataInfos() { return this.dataSelection.getDsInfo(); } @Override - public boolean existsInMainStorage(DataInfoBase dataInfo) - throws InternalException { + public boolean existsInMainStorage(DataInfoBase dataInfo) throws InternalException { var dsInfo = (DatasetInfo) dataInfo; - if (dsInfo == null) - throw new InternalException( - "Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); - - return this.dataSelection.getEmptyDatasets().contains(dataInfo.getId()) - || ServiceProvider.getInstance().getMainStorage() - .exists(dsInfo); + if(dsInfo == null) throw new InternalException("Could not cast DataInfoBase to DataSetInfo. Did you handed over another sub type?"); + + return this.dataSelection.getEmptyDatasets().contains(dataInfo.getId()) || ServiceProvider.getInstance().getMainStorage().exists(dsInfo); } + @Override public boolean isPrepared(String preparedId) throws InternalException { return this.areDataInfosPrepared(preparedId); } + @Override - public void queueDelete() - throws NotImplementedException, InternalException { + public void queueDelete() throws NotImplementedException, InternalException { this.scheduleTasks(DeferredOp.WRITE); } + @Override - public void scheduleTasks(DeferredOp operation) - throws NotImplementedException, InternalException { + public void scheduleTasks(DeferredOp operation) throws NotImplementedException, InternalException { for (DataInfoBase dataInfo : this.getPrimaryDataInfos().values()) { ServiceProvider.getInstance().getFsm().queue(dataInfo, operation); } } + } diff --git a/src/main/java/org/icatproject/ids/thread/DfArchiver.java b/src/main/java/org/icatproject/ids/thread/DfArchiver.java index d1286741..15df979f 100644 --- a/src/main/java/org/icatproject/ids/thread/DfArchiver.java +++ b/src/main/java/org/icatproject/ids/thread/DfArchiver.java @@ -7,19 +7,17 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; +import org.icatproject.ids.services.LockManager.Lock; /* * Removes datafiles from the fast storage (doesn't write them to archive storage) */ public class DfArchiver implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DfArchiver.class); + private final static Logger logger = LoggerFactory.getLogger(DfArchiver.class); private MainStorageInterface mainStorageInterface; private FiniteStateMachine fsm; @@ -27,9 +25,7 @@ public class DfArchiver implements Runnable { private Path markerDir; private Collection locks; - public DfArchiver(List dfInfos, - PropertyHandler propertyHandler, FiniteStateMachine fsm, - Collection locks) { + public DfArchiver(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { this.dfInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -42,19 +38,16 @@ public void run() { try { for (DatafileInfo dfInfo : dfInfos) { try { - if (Files.exists(markerDir - .resolve(Long.toString(dfInfo.getDfId())))) { + if (Files.exists(markerDir.resolve(Long.toString(dfInfo.getDfId())))) { logger.error("Archive of " + dfInfo + " not carried out because a write to secondary storage operation failed previously"); } else { String dfLocation = dfInfo.getDfLocation(); - mainStorageInterface.delete(dfLocation, - dfInfo.getCreateId(), dfInfo.getModId()); + mainStorageInterface.delete(dfLocation, dfInfo.getCreateId(), dfInfo.getModId()); logger.debug("Archive of " + dfInfo + " completed"); } } catch (Exception e) { - logger.error("Archive of " + dfInfo + " failed due to " - + e.getClass() + " " + e.getMessage()); + logger.error("Archive of " + dfInfo + " failed due to " + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dfInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DfDeleter.java b/src/main/java/org/icatproject/ids/thread/DfDeleter.java index 5f1dd0cc..e0b4dab6 100644 --- a/src/main/java/org/icatproject/ids/thread/DfDeleter.java +++ b/src/main/java/org/icatproject/ids/thread/DfDeleter.java @@ -5,29 +5,25 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.ArchiveStorageInterface; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; +import org.icatproject.ids.services.LockManager.Lock; /** * Delete datafiles from archive */ public class DfDeleter implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DfDeleter.class); + private final static Logger logger = LoggerFactory.getLogger(DfDeleter.class); private FiniteStateMachine fsm; private ArchiveStorageInterface archiveStorageInterface; private List dfInfos; private Collection locks; - public DfDeleter(List dfInfos, - PropertyHandler propertyHandler, FiniteStateMachine fsm, - Collection locks) { + public DfDeleter(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { this.dfInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -43,8 +39,7 @@ public void run() { archiveStorageInterface.delete(dfLocation); logger.debug("Delete of " + dfInfo + " completed"); } catch (Exception e) { - logger.error("Delete of " + dfInfo + " failed due to " - + e.getClass() + " " + e.getMessage()); + logger.error("Delete of " + dfInfo + " failed due to " + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dfInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DfRestorer.java b/src/main/java/org/icatproject/ids/thread/DfRestorer.java index 3c38baa6..a97327b5 100644 --- a/src/main/java/org/icatproject/ids/thread/DfRestorer.java +++ b/src/main/java/org/icatproject/ids/thread/DfRestorer.java @@ -8,22 +8,20 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.ArchiveStorageInterface; import org.icatproject.ids.plugin.DfInfo; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; +import org.icatproject.ids.services.LockManager.Lock; /* * Restores datafiles from the slow to the fast storage. */ public class DfRestorer implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DfRestorer.class); + private final static Logger logger = LoggerFactory.getLogger(DfRestorer.class); private MainStorageInterface mainStorageInterface; private ArchiveStorageInterface archiveStorageInterface; @@ -31,9 +29,7 @@ public class DfRestorer implements Runnable { private List dataFileInfos; private Collection locks; - public DfRestorer(List dfInfos, - PropertyHandler propertyHandler, FiniteStateMachine fsm, - Collection locks) { + public DfRestorer(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { this.dataFileInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -65,14 +61,14 @@ public void run() { } } - // TODO: This is additional conversion caused by the redesign :-( + //TODO: This is additional conversion caused by the redesign :-( List dfInfos = new ArrayList<>(); - for (DfInfo dfInfo : this.dataFileInfos) { + for(DfInfo dfInfo : this.dataFileInfos) { dfInfos.add(dfInfo); } - Set failures = archiveStorageInterface - .restore(mainStorageInterface, dfInfos); + + Set failures = archiveStorageInterface.restore(mainStorageInterface, dfInfos); for (DatafileInfo dfInfo : dataFileInfos) { if (failures.contains(dfInfo)) { fsm.recordFailure(dfInfo.getDfId()); @@ -85,8 +81,7 @@ public void run() { } } catch (Exception e) { for (DatafileInfo dfInfo : dataFileInfos) { - logger.error("Restore of " + dfInfo + " failed " + e.getClass() - + " " + e.getMessage()); + logger.error("Restore of " + dfInfo + " failed " + e.getClass() + " " + e.getMessage()); fsm.removeFromChanging(dfInfo); } } finally { diff --git a/src/main/java/org/icatproject/ids/thread/DfWriter.java b/src/main/java/org/icatproject/ids/thread/DfWriter.java index 82ee3052..09881acc 100644 --- a/src/main/java/org/icatproject/ids/thread/DfWriter.java +++ b/src/main/java/org/icatproject/ids/thread/DfWriter.java @@ -8,21 +8,19 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatafileInfo; import org.icatproject.ids.plugin.ArchiveStorageInterface; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; +import org.icatproject.ids.services.LockManager.Lock; /** * Copies datafiles from main to archive */ public class DfWriter implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DfWriter.class); + private final static Logger logger = LoggerFactory.getLogger(DfWriter.class); private FiniteStateMachine fsm; private MainStorageInterface mainStorageInterface; @@ -31,8 +29,7 @@ public class DfWriter implements Runnable { private List dataFileInfos; private Collection locks; - public DfWriter(List dfInfos, PropertyHandler propertyHandler, - FiniteStateMachine fsm, Collection locks) { + public DfWriter(List dfInfos, PropertyHandler propertyHandler, FiniteStateMachine fsm, Collection locks) { this.dataFileInfos = dfInfos; this.fsm = fsm; this.locks = locks; @@ -46,17 +43,14 @@ public void run() { try { for (DatafileInfo dataFileInfo : dataFileInfos) { String dfLocation = dataFileInfo.getDfLocation(); - try (InputStream is = mainStorageInterface.get(dfLocation, - dataFileInfo.getCreateId(), dataFileInfo.getModId())) { + try (InputStream is = mainStorageInterface.get(dfLocation, dataFileInfo.getCreateId(), dataFileInfo.getModId())) { archiveStorageInterface.put(is, dfLocation); - Path marker = markerDir - .resolve(Long.toString(dataFileInfo.getDfId())); + Path marker = markerDir.resolve(Long.toString(dataFileInfo.getDfId())); Files.deleteIfExists(marker); logger.debug("Removed marker " + marker); logger.debug("Write of " + dataFileInfo + " completed"); } catch (Exception e) { - logger.error("Write of " + dataFileInfo + " failed due to " - + e.getClass() + " " + e.getMessage()); + logger.error("Write of " + dataFileInfo + " failed due to " + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dataFileInfo); } diff --git a/src/main/java/org/icatproject/ids/thread/DsArchiver.java b/src/main/java/org/icatproject/ids/thread/DsArchiver.java index 02c84314..81c2c664 100644 --- a/src/main/java/org/icatproject/ids/thread/DsArchiver.java +++ b/src/main/java/org/icatproject/ids/thread/DsArchiver.java @@ -5,19 +5,17 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.plugin.MainStorageInterface; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; +import org.icatproject.ids.services.LockManager.Lock; /* * Removes datasets from the fast storage (doesn't write them to slow storage) */ public class DsArchiver implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DsArchiver.class); + private final static Logger logger = LoggerFactory.getLogger(DsArchiver.class); private DatasetInfo dsInfo; private MainStorageInterface mainStorageInterface; @@ -25,8 +23,7 @@ public class DsArchiver implements Runnable { private Path markerDir; private Lock lock; - public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, - FiniteStateMachine fsm, Lock lock) { + public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; mainStorageInterface = propertyHandler.getMainStorage(); @@ -37,8 +34,7 @@ public DsArchiver(DatasetInfo dsInfo, PropertyHandler propertyHandler, @Override public void run() { try { - if (Files.exists( - markerDir.resolve(Long.toString(dsInfo.getDsId())))) { + if (Files.exists(markerDir.resolve(Long.toString(dsInfo.getDsId())))) { logger.error("Archive of " + dsInfo + " not carried out because a write to secondary storage operation failed previously"); } else { @@ -46,8 +42,7 @@ public void run() { logger.debug("Archive of " + dsInfo + " completed"); } } catch (Exception e) { - logger.error("Archive of " + dsInfo + " failed due to " - + e.getMessage()); + logger.error("Archive of " + dsInfo + " failed due to " + e.getMessage()); } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/main/java/org/icatproject/ids/thread/DsRestorer.java b/src/main/java/org/icatproject/ids/thread/DsRestorer.java index dc142398..20c7b49a 100644 --- a/src/main/java/org/icatproject/ids/thread/DsRestorer.java +++ b/src/main/java/org/icatproject/ids/thread/DsRestorer.java @@ -16,7 +16,6 @@ import org.icatproject.Datafile; import org.icatproject.Dataset; - import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.helpers.LocationHelper; import org.icatproject.ids.models.DatasetInfo; @@ -24,16 +23,15 @@ import org.icatproject.ids.plugin.MainStorageInterface; import org.icatproject.ids.plugin.ZipMapperInterface; import org.icatproject.ids.services.IcatReader; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; +import org.icatproject.ids.services.LockManager.Lock; /* * Restores datafiles from the slow to the fast storage. */ public class DsRestorer implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DsRestorer.class); + private final static Logger logger = LoggerFactory.getLogger(DsRestorer.class); private DatasetInfo dsInfo; @@ -48,8 +46,7 @@ public class DsRestorer implements Runnable { private ZipMapperInterface zipMapper; private Lock lock; - public DsRestorer(DatasetInfo dsInfo, PropertyHandler propertyHandler, - FiniteStateMachine fsm, IcatReader reader, Lock lock) { + public DsRestorer(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, IcatReader reader, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; zipMapper = propertyHandler.getZipMapper(); @@ -74,48 +71,39 @@ public void run() { long size = 0; int n = 0; - List datafiles = ((Dataset) reader - .get("Dataset INCLUDE Datafile", dsInfo.getDsId())) + List datafiles = ((Dataset) reader.get("Dataset INCLUDE Datafile", dsInfo.getDsId())) .getDatafiles(); - Map nameToLocalMap = new HashMap<>( - datafiles.size()); + Map nameToLocalMap = new HashMap<>(datafiles.size()); for (Datafile datafile : datafiles) { if (datafile.getLocation() == null) { continue; } - nameToLocalMap.put(datafile.getName(), LocationHelper - .getLocation(datafile.getId(), datafile.getLocation())); + nameToLocalMap.put(datafile.getName(), LocationHelper.getLocation(datafile.getId(), datafile.getLocation())); size += datafile.getFileSize(); n++; } - logger.debug("Restoring dataset " + dsInfo.getInvId() + "/" - + dsInfo.getDsId() + " with " + n + " files of total size " - + size); + logger.debug("Restoring dataset " + dsInfo.getInvId() + "/" + dsInfo.getDsId() + " with " + n + + " files of total size " + size); // Get the file into the dataset cache - Path datasetCachePath = Files.createTempFile(datasetCache, null, - null); + Path datasetCachePath = Files.createTempFile(datasetCache, null, null); archiveStorageInterface.get(dsInfo, datasetCachePath); // Now split file and store it locally - logger.debug("Unpacking dataset " + dsInfo.getInvId() + "/" - + dsInfo.getDsId() + " with " + n + " files of total size " - + size); - ZipInputStream zis = new ZipInputStream( - Files.newInputStream(datasetCachePath)); + logger.debug("Unpacking dataset " + dsInfo.getInvId() + "/" + dsInfo.getDsId() + " with " + n + + " files of total size " + size); + ZipInputStream zis = new ZipInputStream(Files.newInputStream(datasetCachePath)); ZipEntry ze = zis.getNextEntry(); Set seen = new HashSet<>(); while (ze != null) { String dfName = zipMapper.getFileName(ze.getName()); if (seen.contains(dfName)) { - throw new RuntimeException("Corrupt archive for " + dsInfo - + ": duplicate entry " + dfName); + throw new RuntimeException("Corrupt archive for " + dsInfo + ": duplicate entry " + dfName); } String location = nameToLocalMap.get(dfName); if (location == null) { - throw new RuntimeException("Corrupt archive for " + dsInfo - + ": spurious entry " + dfName); + throw new RuntimeException("Corrupt archive for " + dsInfo + ": spurious entry " + dfName); } mainStorageInterface.put(zis, location); ze = zis.getNextEntry(); @@ -123,16 +111,14 @@ public void run() { } zis.close(); if (!seen.equals(nameToLocalMap.keySet())) { - throw new RuntimeException( - "Corrupt archive for " + dsInfo + ": missing entries"); + throw new RuntimeException("Corrupt archive for " + dsInfo + ": missing entries"); } Files.delete(datasetCachePath); fsm.recordSuccess(dsInfo.getDsId()); logger.debug("Restore of " + dsInfo + " completed"); } catch (Exception e) { fsm.recordFailure(dsInfo.getDsId()); - logger.error("Restore of " + dsInfo + " failed due to " - + e.getClass() + " " + e.getMessage()); + logger.error("Restore of " + dsInfo + " failed due to " + e.getClass() + " " + e.getMessage()); try { mainStorageInterface.delete(dsInfo); } catch (IOException e2) { diff --git a/src/main/java/org/icatproject/ids/thread/DsWriter.java b/src/main/java/org/icatproject/ids/thread/DsWriter.java index 472dfab0..e5c1843b 100644 --- a/src/main/java/org/icatproject/ids/thread/DsWriter.java +++ b/src/main/java/org/icatproject/ids/thread/DsWriter.java @@ -14,7 +14,6 @@ import org.icatproject.Datafile; import org.icatproject.Dataset; - import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.helpers.LocationHelper; import org.icatproject.ids.models.DatafileInfo; @@ -23,16 +22,15 @@ import org.icatproject.ids.plugin.MainStorageInterface; import org.icatproject.ids.plugin.ZipMapperInterface; import org.icatproject.ids.services.IcatReader; -import org.icatproject.ids.services.LockManager.Lock; import org.icatproject.ids.services.PropertyHandler; +import org.icatproject.ids.services.LockManager.Lock; /** * Copies dataset from main to archive */ public class DsWriter implements Runnable { - private final static Logger logger = LoggerFactory - .getLogger(DsWriter.class); + private final static Logger logger = LoggerFactory.getLogger(DsWriter.class); private static final int BUFSIZ = 1024; private DatasetInfo dsInfo; @@ -45,8 +43,7 @@ public class DsWriter implements Runnable { private ZipMapperInterface zipMapper; private Lock lock; - public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, - FiniteStateMachine fsm, IcatReader reader, Lock lock) { + public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, FiniteStateMachine fsm, IcatReader reader, Lock lock) { this.dsInfo = dsInfo; this.fsm = fsm; this.zipMapper = propertyHandler.getZipMapper(); @@ -62,35 +59,27 @@ public DsWriter(DatasetInfo dsInfo, PropertyHandler propertyHandler, public void run() { try { if (!mainStorageInterface.exists(dsInfo)) { - logger.info("No files present in main storage for " + dsInfo - + " - will delete archive"); + logger.info("No files present in main storage for " + dsInfo + " - will delete archive"); archiveStorageInterface.delete(dsInfo); } else { - Path datasetCachePath = Files.createTempFile(datasetCache, null, - null); + Path datasetCachePath = Files.createTempFile(datasetCache, null, null); logger.debug("Creating " + datasetCachePath); - List datafiles = ((Dataset) reader - .get("Dataset INCLUDE Datafile", dsInfo.getDsId())) + List datafiles = ((Dataset) reader.get("Dataset INCLUDE Datafile", dsInfo.getDsId())) .getDatafiles(); - ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream( - datasetCachePath, StandardOpenOption.CREATE)); + ZipOutputStream zos = new ZipOutputStream( + Files.newOutputStream(datasetCachePath, StandardOpenOption.CREATE)); for (Datafile datafile : datafiles) { if (datafile.getLocation() == null) { continue; } - String location = LocationHelper.getLocation( - datafile.getId(), datafile.getLocation()); + String location = LocationHelper.getLocation(datafile.getId(), datafile.getLocation()); InputStream is = null; try { - zos.putNextEntry( - new ZipEntry(zipMapper.getFullEntryName(dsInfo, - new DatafileInfo(datafile.getId(), - datafile.getName(), location, - datafile.getCreateId(), - datafile.getModId(), 0L)))); - is = mainStorageInterface.get(location, - datafile.getCreateId(), datafile.getModId()); + zos.putNextEntry(new ZipEntry( + zipMapper.getFullEntryName(dsInfo, new DatafileInfo(datafile.getId(), datafile.getName(), + location, datafile.getCreateId(), datafile.getModId(), 0L)))); + is = mainStorageInterface.get(location, datafile.getCreateId(), datafile.getModId()); int bytesRead = 0; byte[] buffer = new byte[BUFSIZ]; while ((bytesRead = is.read(buffer)) > 0) { @@ -116,8 +105,7 @@ public void run() { logger.debug("Removed marker " + marker); logger.debug("Write of " + dsInfo + " completed"); } catch (Exception e) { - logger.error("Write of " + dsInfo + " failed due to " + e.getClass() - + " " + e.getMessage()); + logger.error("Write of " + dsInfo + " failed due to " + e.getClass() + " " + e.getMessage()); } finally { fsm.removeFromChanging(dsInfo); lock.release(); diff --git a/src/test/java/org/icatproject/ids/DataSelectionDevTest.java b/src/test/java/org/icatproject/ids/DataSelectionDevTest.java index 1dbc55eb..f0ba796e 100644 --- a/src/test/java/org/icatproject/ids/DataSelectionDevTest.java +++ b/src/test/java/org/icatproject/ids/DataSelectionDevTest.java @@ -1,8 +1,5 @@ package org.icatproject.ids; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; - import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.net.URISyntaxException; @@ -13,17 +10,18 @@ import jakarta.json.Json; import jakarta.json.JsonReader; +import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import static org.mockito.Mockito.when; import org.mockito.junit.MockitoJUnitRunner; import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; import org.icatproject.icat.client.IcatException; - import org.icatproject.ids.enums.RequestType; import org.icatproject.ids.services.ICATGetter; import org.icatproject.ids.services.IcatReader; @@ -33,8 +31,8 @@ /** * This test was created to fix issue #115 and was run against the Diamond - * pre-production ICAT using specifically selected usernames of users known to - * have access to a lot of data. + * pre-production ICAT using specifically selected usernames of users known + * to have access to a lot of data. * * As well as monitoring the time taken to create the DataSelection, detailed * monitoring of the eclipselink SQL logging was done to confirm that the @@ -76,26 +74,21 @@ public class DataSelectionDevTest { @Before public void setup() throws Exception { testProps = new Properties(); - testProps.load(new FileInputStream( - "src/test/resources/DataSelectionDevTest.properties")); + testProps.load(new FileInputStream("src/test/resources/DataSelectionDevTest.properties")); // set up the SOAP and REST ICAT clients icatUrl = ICATGetter.getCleanUrl(testProps.getProperty("icat.url")); icatService = ICATGetter.getService(icatUrl); restIcat = new org.icatproject.icat.client.ICAT(icatUrl); - // get session IDs for an end user and the reader user (with read-all - // permissions) + // get session IDs for an end user and the reader user (with read-all permissions) String userCredsString = testProps.getProperty("login.user"); userSessionId = TestUtils.login(icatService, userCredsString); System.out.println("userSessionId = " + userSessionId); - List readerCreds = Arrays.asList( - testProps.getProperty("login.reader").trim().split("\\s+")); + List readerCreds = Arrays.asList(testProps.getProperty("login.reader").trim().split("\\s+")); this.readerCreds = readerCreds; investigationIds = testProps.getProperty("investigation.ids"); datasetIds = testProps.getProperty("dataset.ids"); datafileIds = testProps.getProperty("datafile.ids"); - useReaderForPerformance = testProps - .getProperty("useReaderForPerformance") - .equalsIgnoreCase("true"); + useReaderForPerformance = testProps.getProperty("useReaderForPerformance").equalsIgnoreCase("true"); // set up a mocked version of the PropertyHandler setupPropertyHandler(); icatReader = new IcatReader(mockedPropertyHandler); @@ -103,32 +96,24 @@ public void setup() throws Exception { private void setupPropertyHandler() throws URISyntaxException, IcatException_Exception, IcatException { - JsonReader parser = Json.createReader( - new ByteArrayInputStream(restIcat.getProperties().getBytes())); + JsonReader parser = Json.createReader(new ByteArrayInputStream(restIcat.getProperties().getBytes())); maxEntities = parser.readObject().getInt("maxEntities"); when(mockedPropertyHandler.getMaxEntities()).thenReturn(maxEntities); when(mockedPropertyHandler.getIcatService()).thenReturn(icatService); when(mockedPropertyHandler.getRestIcat()).thenReturn(restIcat); when(mockedPropertyHandler.getReader()).thenReturn(readerCreds); - when(mockedPropertyHandler.getUseReaderForPerformance()) - .thenReturn(useReaderForPerformance); + when(mockedPropertyHandler.getUseReaderForPerformance()).thenReturn(useReaderForPerformance); } @Ignore("Test requires a specific ICAT setup to produce meaningful results. See class javadoc comment.") @Test public void testCreateDataSelection() throws Exception { long startMs = System.currentTimeMillis(); - var dataSelectionFactory = DataSelectionServiceFactory - .getInstanceOnlyForTesting(mockedPropertyHandler, icatReader); - DataSelectionService dataSelectionService = dataSelectionFactory - .getSelectionService(userSessionId, investigationIds, - datasetIds, datafileIds, RequestType.GETSIZE); - System.out.println("Creating DataSelection took " - + (System.currentTimeMillis() - startMs) + " ms"); - System.out.println( - "DsInfo size: " + dataSelectionService.getDsInfo().size()); - System.out.println( - "DfInfo size: " + dataSelectionService.getDfInfo().size()); + var dataSelectionFactory = DataSelectionServiceFactory.getInstanceOnlyForTesting(mockedPropertyHandler, icatReader); + DataSelectionService dataSelectionService = dataSelectionFactory.getSelectionService(userSessionId,investigationIds, datasetIds, datafileIds, RequestType.GETSIZE); + System.out.println("Creating DataSelection took " + (System.currentTimeMillis() - startMs) + " ms"); + System.out.println("DsInfo size: " + dataSelectionService.getDsInfo().size()); + System.out.println("DfInfo size: " + dataSelectionService.getDfInfo().size()); // there must be at least one Datafile in the DataSelection assertTrue("message", dataSelectionService.getDfInfo().size() > 0); } diff --git a/src/test/java/org/icatproject/ids/DigestTest.java b/src/test/java/org/icatproject/ids/DigestTest.java index 1e3d4199..735e6195 100644 --- a/src/test/java/org/icatproject/ids/DigestTest.java +++ b/src/test/java/org/icatproject/ids/DigestTest.java @@ -3,13 +3,11 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; - import org.junit.Test; -import org.icatproject.utils.IcatSecurity; - import org.icatproject.ids.exceptions.InsufficientPrivilegesException; import org.icatproject.ids.helpers.LocationHelper; +import org.icatproject.utils.IcatSecurity; public class DigestTest { @@ -18,18 +16,15 @@ public class DigestTest { @Test public void testCheck() throws Exception { - String a = location + " " - + IcatSecurity.digest(1234567L, location, key); + String a = location + " " + IcatSecurity.digest(1234567L, location, key); - assertEquals(location, - LocationHelper.getLocationFromDigest(1234567L, a, key)); + assertEquals(location, LocationHelper.getLocationFromDigest(1234567L, a, key)); try { LocationHelper.getLocationFromDigest(1234568L, a, key); fail(); } catch (InsufficientPrivilegesException e) { - assertTrue( - e.getMessage().contains("does not contain a valid hash")); + assertTrue(e.getMessage().contains("does not contain a valid hash")); } try { diff --git a/src/test/java/org/icatproject/ids/PreparePackingTest.java b/src/test/java/org/icatproject/ids/PreparePackingTest.java index 4aee2710..6b2a857e 100644 --- a/src/test/java/org/icatproject/ids/PreparePackingTest.java +++ b/src/test/java/org/icatproject/ids/PreparePackingTest.java @@ -1,11 +1,5 @@ package org.icatproject.ids; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -17,12 +11,17 @@ import java.util.Map.Entry; import java.util.Set; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; -import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatafileInfo; +import org.icatproject.ids.models.DataInfoBase; import org.icatproject.ids.models.DatasetInfo; import org.icatproject.ids.models.Prepared; +import org.junit.Test; public class PreparePackingTest { @@ -38,24 +37,21 @@ public void packAndUnpack() throws Exception { long dsid2 = 18L; long invId = 15L; long facilityId = 45L; - dfInfos.put(5L, new DatafileInfo(5L, "dfName", "dfLocation", "createId", - "modId", dsid1)); + dfInfos.put(5L, new DatafileInfo(5L, "dfName", "dfLocation", "createId", "modId", dsid1)); - dfInfos.put(51L, new DatafileInfo(51L, "dfName2", null, "createId", - "modId", dsid1)); + dfInfos.put(51L, new DatafileInfo(51L, "dfName2", null, "createId", "modId", dsid1)); - dsInfos.put(dsid1, new DatasetInfo(dsid1, "dsName", "dsLocation", invId, - "invName", "visitId", facilityId, "facilityName")); + dsInfos.put(dsid1, new DatasetInfo(dsid1, "dsName", "dsLocation", invId, "invName", + "visitId", facilityId, "facilityName")); - dsInfos.put(dsid2, new DatasetInfo(dsid2, "dsName2", null, invId, - "invName", "visitId", facilityId, "facilityName")); + dsInfos.put(dsid2, new DatasetInfo(dsid2, "dsName2", null, invId, "invName", "visitId", + facilityId, "facilityName")); emptyDatasets.add(dsid2); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (OutputStream stream = new BufferedOutputStream(baos)) { - Prepared.pack(stream, zip, compress, dsInfos, dfInfos, - emptyDatasets, 51); + Prepared.pack(stream, zip, compress, dsInfos, dfInfos, emptyDatasets, 51); } System.out.println(baos.toString()); InputStream stream = new ByteArrayInputStream(baos.toByteArray()); diff --git a/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java b/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java index ae06d898..e6960866 100644 --- a/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java +++ b/src/test/java/org/icatproject/ids/RangeOutputStreamTest.java @@ -1,13 +1,12 @@ package org.icatproject.ids; -import static org.junit.Assert.assertEquals; - import java.io.ByteArrayOutputStream; import java.io.OutputStream; -import org.junit.Test; +import static org.junit.Assert.assertEquals; import org.icatproject.ids.helpers.RangeOutputStream; +import org.junit.Test; public class RangeOutputStreamTest { @@ -50,8 +49,7 @@ public void t3() throws Exception { os.flush(); } os.close(); - assertEquals("m is " + m, "ABCDEFGHIJKLMONPQRSTUVWXYZ", - baos.toString()); + assertEquals("m is " + m, "ABCDEFGHIJKLMONPQRSTUVWXYZ", baos.toString()); } } @@ -70,6 +68,7 @@ public void t4() throws Exception { } } + @Test public void t5() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); diff --git a/src/test/java/org/icatproject/ids/TestICATGetter.java b/src/test/java/org/icatproject/ids/TestICATGetter.java index 7470024f..8bfa99c6 100644 --- a/src/test/java/org/icatproject/ids/TestICATGetter.java +++ b/src/test/java/org/icatproject/ids/TestICATGetter.java @@ -1,31 +1,26 @@ package org.icatproject.ids; import static org.junit.Assert.assertEquals; - import org.junit.Test; import org.icatproject.ICAT; - import org.icatproject.ids.services.ICATGetter; public class TestICATGetter { @Test public void testGFUrl() throws Exception { - assertEquals("http://localhost", ICATGetter - .getCleanUrl("http://localhost/ICATService/ICAT?wsdl")); + assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost/ICATService/ICAT?wsdl")); } @Test public void testSlash() throws Exception { - assertEquals("http://localhost", - ICATGetter.getCleanUrl("http://localhost/")); + assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost/")); } @Test public void testClean() throws Exception { - assertEquals("http://localhost", - ICATGetter.getCleanUrl("http://localhost")); + assertEquals("http://localhost", ICATGetter.getCleanUrl("http://localhost")); } @Test diff --git a/src/test/java/org/icatproject/ids/TestUtils.java b/src/test/java/org/icatproject/ids/TestUtils.java index 7b313f42..cbd62530 100644 --- a/src/test/java/org/icatproject/ids/TestUtils.java +++ b/src/test/java/org/icatproject/ids/TestUtils.java @@ -12,16 +12,15 @@ public class TestUtils { /** * Do an ICAT login and get the session ID using a list of credentials of - * the format (as found in the properties files): "db username READER - * password READERpass" + * the format (as found in the properties files): + * "db username READER password READERpass" * * @param icatService the ICAT to log in to * @param credsString a String of credentials in the format described above * @return an ICAT session ID * @throws IcatException_Exception if the login fails */ - public static String login(ICAT icatService, String credsString) - throws IcatException_Exception { + public static String login(ICAT icatService, String credsString) throws IcatException_Exception { List creds = Arrays.asList(credsString.trim().split("\\s+")); Credentials credentials = new Credentials(); List entries = credentials.getEntry(); diff --git a/src/test/java/org/icatproject/ids/TestValueContainer.java b/src/test/java/org/icatproject/ids/TestValueContainer.java index f592c7c5..bc3daf98 100644 --- a/src/test/java/org/icatproject/ids/TestValueContainer.java +++ b/src/test/java/org/icatproject/ids/TestValueContainer.java @@ -10,17 +10,17 @@ import java.nio.charset.StandardCharsets; import java.util.stream.Collectors; -import org.junit.Test; - import org.icatproject.ids.enums.ValueContainerType; import org.icatproject.ids.exceptions.InternalException; import org.icatproject.ids.helpers.ValueContainer; +import org.junit.Test; + public class TestValueContainer { @Test public void testInvalidValueContainer() throws Exception { - + var vc = ValueContainer.getInvalid(); assertFalse(vc.isVoid()); assertTrue(vc.getType() == ValueContainerType.INVALID); @@ -28,7 +28,7 @@ public void testInvalidValueContainer() throws Exception { @Test public void testVoidValueContainer() throws Exception { - + var vc = ValueContainer.getVoid(); assertTrue(vc.isVoid()); assertTrue(vc.getType() == ValueContainerType.VOID); @@ -37,7 +37,7 @@ public void testVoidValueContainer() throws Exception { @Test(expected = InternalException.class) public void testIntValueContainer() throws Exception { - + var vc = new ValueContainer(1); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -50,7 +50,7 @@ public void testIntValueContainer() throws Exception { @Test(expected = InternalException.class) public void testLongValueContainer() throws Exception { - + var vc = new ValueContainer(1L); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -63,7 +63,7 @@ public void testLongValueContainer() throws Exception { @Test(expected = InternalException.class) public void testBoolValueContainer() throws Exception { - + var vc = new ValueContainer(true); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -76,7 +76,7 @@ public void testBoolValueContainer() throws Exception { @Test(expected = InternalException.class) public void testStringValueContainer() throws Exception { - + var vc = new ValueContainer("test"); assertFalse(vc.isVoid()); assertFalse(vc.isInvalid()); @@ -89,7 +89,7 @@ public void testStringValueContainer() throws Exception { @Test(expected = InternalException.class) public void testInputStreamValueContainer() throws Exception { - + String s = "test InputStream"; var inputStream = new ByteArrayInputStream(s.getBytes()); var vc = new ValueContainer(inputStream); @@ -98,9 +98,8 @@ public void testInputStreamValueContainer() throws Exception { assertFalse(vc.isNull()); assertEquals(vc.getInputStream(), inputStream); - - var ISReader = new InputStreamReader(vc.getInputStream(), - StandardCharsets.UTF_8); + + var ISReader = new InputStreamReader(vc.getInputStream(), StandardCharsets.UTF_8); var BReader = new BufferedReader(ISReader); String textFromStream = BReader.lines().collect(Collectors.joining()); BReader.close(); diff --git a/src/test/java/org/icatproject/ids/TestZipping.java b/src/test/java/org/icatproject/ids/TestZipping.java index 34f92125..56dce770 100644 --- a/src/test/java/org/icatproject/ids/TestZipping.java +++ b/src/test/java/org/icatproject/ids/TestZipping.java @@ -1,7 +1,5 @@ package org.icatproject.ids; -import static org.junit.Assert.assertEquals; - import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -13,6 +11,7 @@ import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; +import static org.junit.Assert.assertEquals; import org.junit.Test; public class TestZipping { @@ -20,14 +19,12 @@ public class TestZipping { @Test public void testDuplicates() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ZipOutputStream zos = new ZipOutputStream( - new BufferedOutputStream(baos)); + ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos)); // zos.setLevel(0); List in = new ArrayList<>(); - for (String entryName : Arrays.asList("abcd/qa", "abcd/qw", "abcd/qw", - "abcd/qb", "abcd/qc", "abcd/qw")) { + for (String entryName : Arrays.asList("abcd/qa", "abcd/qw", "abcd/qw", "abcd/qb", "abcd/qc", "abcd/qw")) { try { zos.putNextEntry(new ZipEntry(entryName)); @@ -45,8 +42,7 @@ public void testDuplicates() throws Exception { assertEquals(4, in.size()); - ByteArrayInputStream bais = new ByteArrayInputStream( - baos.toByteArray()); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ZipInputStream zis = new ZipInputStream(bais); int off = 0; diff --git a/src/test/java/org/icatproject/ids/TidierTest.java b/src/test/java/org/icatproject/ids/TidierTest.java index b4a5096a..8f2762c6 100644 --- a/src/test/java/org/icatproject/ids/TidierTest.java +++ b/src/test/java/org/icatproject/ids/TidierTest.java @@ -1,14 +1,13 @@ package org.icatproject.ids; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.io.ByteArrayInputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -86,16 +85,13 @@ public void testCleanPreparedDir() throws Exception { @Test public void testAddStringConstraint() throws Exception { StringBuilder sb1 = new StringBuilder(); - Tidier.addStringConstraint(sb1, "df.location", "/path/to/normal/file", - false); + Tidier.addStringConstraint(sb1, "df.location", "/path/to/normal/file", false); assertEquals(" df.location = '/path/to/normal/file'", sb1.toString()); /* Fix error where a file path contains an apostrophe */ StringBuilder sb2 = new StringBuilder(); - Tidier.addStringConstraint(sb2, "df.location", - "/path/to/Person's Files/myscript.py", false); - assertEquals(" df.location = '/path/to/Person''s Files/myscript.py'", - sb2.toString()); + Tidier.addStringConstraint(sb2, "df.location", "/path/to/Person's Files/myscript.py", false); + assertEquals(" df.location = '/path/to/Person''s Files/myscript.py'", sb2.toString()); } @Test diff --git a/src/test/java/org/icatproject/ids/TransmittingTest.java b/src/test/java/org/icatproject/ids/TransmittingTest.java index 205822de..7e2e986c 100644 --- a/src/test/java/org/icatproject/ids/TransmittingTest.java +++ b/src/test/java/org/icatproject/ids/TransmittingTest.java @@ -7,15 +7,9 @@ import java.net.URISyntaxException; import java.nio.file.Paths; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - import org.icatproject.ICAT; import org.icatproject.IcatException_Exception; import org.icatproject.icat.client.IcatException; - import org.icatproject.ids.enums.StorageUnit; import org.icatproject.ids.finiteStateMachine.FiniteStateMachine; import org.icatproject.ids.plugin.ArchiveStorageInterface; @@ -41,11 +35,15 @@ import org.icatproject.ids.services.PropertyHandler; import org.icatproject.ids.services.ServiceProvider; import org.icatproject.ids.services.Transmitter; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + /** - * This test was created when the building of the transmission body was made - * more comon and generic. It should ensure the right structure of the - * containing json. + * This test was created when the building of the transmission body was made more comon and generic. + * It should ensure the right structure of the containing json. */ @RunWith(MockitoJUnitRunner.class) public class TransmittingTest { @@ -60,8 +58,7 @@ public class TransmittingTest { private LockManager mockedLockManager; @Mock private IcatReader mockedReader; - @Mock - ArchiveStorageInterface mockedArchiveStorage; + @Mock ArchiveStorageInterface mockedArchiveStorage; @Mock private ICAT mockedIcat; @@ -88,101 +85,81 @@ private void setup() } @Test - public void testTransmitterBodyForArchiveRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForArchiveRequest_shouldBeOk() throws Exception { + setup(); - var handler = new ArchiveHandler(ip, sessionId, investigationIds, - datasetIds, datafileIds); + var handler = new ArchiveHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetSizeRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetSizeRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetSizeHandler(ip, preparedId, null, null, null, - null); + var handler = new GetSizeHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetSizeHandler(ip, null, sessionId, investigationIds, - datasetIds, datafileIds); + handler = new GetSizeHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetSizeFastRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetSizeFastRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetSizeHandlerForFastProcessing(ip, sessionId, - investigationIds, datasetIds, datafileIds); + var handler = new GetSizeHandlerForFastProcessing(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForDeleteRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForDeleteRequest_shouldBeOk() throws Exception { + setup(); - var handler = new DeleteHandler(ip, sessionId, investigationIds, - datasetIds, datafileIds); + var handler = new DeleteHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetDataFileIdsRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetDataFileIdsRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetDataFileIdsHandler(ip, preparedId, null, null, - null, null); + var handler = new GetDataFileIdsHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetDataFileIdsHandler("192.168.17.1", null, sessionId, - investigationIds, datasetIds, datafileIds); + handler = new GetDataFileIdsHandler("192.168.17.1", null, sessionId, investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForGetDataRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetDataRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetDataHandler(ip, preparedId, null, null, null, null, - false, false, "", ""); + var handler = new GetDataHandler(ip, preparedId, null, null, null, null, false, false, "", ""); String body = handler.provideTransmissionBody(); - assertEquals( - "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"transferId\":-1}", - body); + assertEquals("{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\",\"transferId\":-1}", body); - handler = new GetDataHandler(ip, null, sessionId, investigationIds, - datasetIds, datafileIds, false, false, "", ""); + handler = new GetDataHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds, false, false, "", ""); body = handler.provideTransmissionBody(); - assertEquals( - "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"transferId\":-1}", - body); + assertEquals("{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"transferId\":-1}", body); } @Test - public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() throws Exception { + setup(); var handler = new GetIcatUrlHandler(ip); @@ -191,9 +168,8 @@ public void testTransmitterBodyForGetIcatUrlRequest_shouldBeOk() } @Test - public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() throws Exception { + setup(); var handler = new GetServiceStatusHandler(ip, sessionId); @@ -202,39 +178,32 @@ public void testTransmitterBodyForGetServiceStatusRequest_shouldBeOk() } @Test - public void testTransmitterBodyForGetStatusRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForGetStatusRequest_shouldBeOk() throws Exception { + setup(); - var handler = new GetStatusHandler(ip, preparedId, null, null, null, - null); + var handler = new GetStatusHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new GetStatusHandler(ip, null, sessionId, investigationIds, - datasetIds, datafileIds); + handler = new GetStatusHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForIsPreparedRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForIsPreparedRequest_shouldBeOk() throws Exception { + setup(); var handler = new IsPreparedHandler(ip, preparedId); String body = handler.provideTransmissionBody(); - assertEquals( - "{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}", - body); + assertEquals("{\"preparedId\":\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\"}", body); } @Test - public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() throws Exception { + setup(); var handler = new IsReadOnlyHandler(ip); @@ -243,9 +212,8 @@ public void testTransmitterBodyForIsReadOnlyRequest_shouldBeOk() } @Test - public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() throws Exception { + setup(); var handler = new IsTwoLevelHandler(ip); @@ -254,69 +222,56 @@ public void testTransmitterBodyForIsTwoLevelRequest_shouldBeOk() } @Test - public void testTransmitterBodyForPrepareDataRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForPrepareDataRequest_shouldBeOk() throws Exception { + setup(); - var handler = new PrepareDataHandler(ip, sessionId, investigationIds, - datasetIds, datafileIds, false, false); + var handler = new PrepareDataHandler(ip, sessionId, investigationIds, datasetIds, datafileIds, false, false); String body = handler.provideTransmissionBody(); - assertEquals( - "{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"preparedId\":\"\"}", - body); + assertEquals("{\"userName\":\"TestUser\",\"investigationIds\":[1,2,3],\"datasetIds\":[4,5,6],\"datafileIds\":[7,8,9],\"preparedId\":\"\"}", body); } @Test public void testTransmitterBodyForPutRequest_shouldBeOk() throws Exception { - + setup(); - var handler = new PutHandler(ip, sessionId, - new ByteArrayInputStream("".getBytes()), "someName", - datafileIds, datasetIds, "someDescription", "someDOI", - "simeCreateTimeString", "someModTimeString", false, false); + var handler = new PutHandler(ip, sessionId, new ByteArrayInputStream( "".getBytes() ), "someName", datafileIds , datasetIds, "someDescription", "someDOI", "simeCreateTimeString", "someModTimeString", false, false ); String body = handler.provideTransmissionBody(); assertEquals("{\"userName\":\"TestUser\",\"datafileId\":-1}", body); } @Test - public void testTransmitterBodyForResetRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForResetRequest_shouldBeOk() throws Exception { + setup(); var handler = new ResetHandler(ip, preparedId, null, null, null, null); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForPreparedId, body); - handler = new ResetHandler(ip, null, sessionId, investigationIds, - datasetIds, datafileIds); + handler = new ResetHandler(ip, null, sessionId, investigationIds, datasetIds, datafileIds); body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForRestoreRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForRestoreRequest_shouldBeOk() throws Exception { + setup(); - var handler = new RestoreHandler(ip, sessionId, investigationIds, - datasetIds, datafileIds); + var handler = new RestoreHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } @Test - public void testTransmitterBodyForWriteRequest_shouldBeOk() - throws Exception { - + public void testTransmitterBodyForWriteRequest_shouldBeOk() throws Exception { + setup(); - var handler = new WriteHandler(ip, sessionId, investigationIds, - datasetIds, datafileIds); + var handler = new WriteHandler(ip, sessionId, investigationIds, datasetIds, datafileIds); String body = handler.provideTransmissionBody(); assertEquals(defaultTransmissionBodyForSessionId, body); } diff --git a/src/test/java/org/icatproject/ids/ValidaterTest.java b/src/test/java/org/icatproject/ids/ValidaterTest.java index 2e9b05ec..6dda09ef 100644 --- a/src/test/java/org/icatproject/ids/ValidaterTest.java +++ b/src/test/java/org/icatproject/ids/ValidaterTest.java @@ -2,7 +2,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; - import org.junit.Test; import org.icatproject.ids.exceptions.BadRequestException; @@ -33,3 +32,4 @@ private void testValidUUID(boolean b, String id) { } } + diff --git a/src/test/java/org/icatproject/ids/integration/BaseTest.java b/src/test/java/org/icatproject/ids/integration/BaseTest.java index 27cee18a..4f0a23d4 100644 --- a/src/test/java/org/icatproject/ids/integration/BaseTest.java +++ b/src/test/java/org/icatproject/ids/integration/BaseTest.java @@ -1,10 +1,5 @@ package org.icatproject.ids.integration; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -35,6 +30,10 @@ import jakarta.json.Json; import jakarta.json.JsonReader; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import org.junit.Before; import org.icatproject.Datafile; @@ -48,7 +47,6 @@ import org.icatproject.InvestigationType; import org.icatproject.icat.client.ICAT; import org.icatproject.icat.client.Session; - import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; import org.icatproject.ids.integration.util.client.TestingClient; @@ -62,15 +60,13 @@ public class BaseTest { public class DeleteVisitor extends SimpleFileVisitor { @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) - throws IOException { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override - public FileVisitResult postVisitDirectory(Path dir, IOException e) - throws IOException { + public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; @@ -84,21 +80,15 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) private static long timestamp = System.currentTimeMillis(); - protected Path getDirOnFastStorage(Long dsId) - throws IcatException_Exception { - Dataset icatDs = (Dataset) icatWS.get(sessionId, - "Dataset INCLUDE Investigation", dsId); - return setup.getStorageDir() - .resolve(Long.toString(icatDs.getInvestigation().getId())) + protected Path getDirOnFastStorage(Long dsId) throws IcatException_Exception { + Dataset icatDs = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Investigation", dsId); + return setup.getStorageDir().resolve(Long.toString(icatDs.getInvestigation().getId())) .resolve(Long.toString(icatDs.getId())); } - protected Path getFileOnArchiveStorage(Long dsId) - throws IcatException_Exception { - Dataset icatDs = (Dataset) icatWS.get(sessionId, - "Dataset INCLUDE Investigation", dsId); - return setup.getStorageArchiveDir() - .resolve(Long.toString(icatDs.getInvestigation().getId())) + protected Path getFileOnArchiveStorage(Long dsId) throws IcatException_Exception { + Dataset icatDs = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Investigation", dsId); + return setup.getStorageArchiveDir().resolve(Long.toString(icatDs.getInvestigation().getId())) .resolve(Long.toString(icatDs.getId())); } @@ -127,8 +117,7 @@ public void before() throws Exception { testingClient = new TestingClient(setup.getIdsUrl()); sessionId = setup.getRootSessionId(); waitForIds(); - populateStorage(setup.isTwoLevel(), setup.getStorageUnit(), - setup.getKey()); + populateStorage(setup.isTwoLevel(), setup.getStorageUnit(), setup.getKey()); } protected void checkAbsent(Path file) { @@ -169,8 +158,7 @@ protected void logTime(String msg) { long now = System.currentTimeMillis(); if (msg != null) { if (time != 0) { - System.out - .println(msg + " took " + (now - time) / 1000. + "s."); + System.out.println(msg + " took " + (now - time) / 1000. + "s."); } else { System.out.println(msg); } @@ -178,8 +166,7 @@ protected void logTime(String msg) { time = now; } - protected void checkZipFile(Path file, List ids, int compressedSize) - throws IOException { + protected void checkZipFile(Path file, List ids, int compressedSize) throws IOException { checkZipStream(Files.newInputStream(file), ids, compressedSize, 0); } @@ -218,9 +205,8 @@ protected byte[] getOutput(InputStream stream) throws IOException { } } - private void populateStorage(boolean twoLevel, String storageUnit, - String key) throws IOException, IcatException_Exception, - NoSuchAlgorithmException { + private void populateStorage(boolean twoLevel, String storageUnit, String key) + throws IOException, IcatException_Exception, NoSuchAlgorithmException { clearStorage(); long timestamp = System.currentTimeMillis(); @@ -247,8 +233,7 @@ private void populateStorage(boolean twoLevel, String storageUnit, supportedDatafileFormat.setFacility(fac); supportedDatafileFormat.setName("test_format"); supportedDatafileFormat.setVersion("42.0.0"); - supportedDatafileFormat - .setId(icatWS.create(sessionId, supportedDatafileFormat)); + supportedDatafileFormat.setId(icatWS.create(sessionId, supportedDatafileFormat)); InvestigationType invType = new InvestigationType(); invType.setName("Not null"); @@ -292,33 +277,25 @@ private void populateStorage(boolean twoLevel, String storageUnit, df1.setName("a/df1_" + timestamp); df1.setLocation(ds1Loc + UUID.randomUUID()); df1.setDataset(ds1); - writeToFile(df1, - "df1 test content very compressible very compressible", - key); + writeToFile(df1, "df1 test content very compressible very compressible", key); Datafile df2 = new Datafile(); df2.setName("df2_" + timestamp); df2.setLocation(ds1Loc + UUID.randomUUID()); df2.setDataset(ds1); - writeToFile(df2, - "df2 test content very compressible very compressible", - key); + writeToFile(df2, "df2 test content very compressible very compressible", key); Datafile df3 = new Datafile(); df3.setName("df3_" + timestamp); df3.setLocation(ds2Loc + UUID.randomUUID()); df3.setDataset(ds2); - writeToFile(df3, - "df3 test content very compressible very compressible", - key); + writeToFile(df3, "df3 test content very compressible very compressible", key); Datafile df4 = new Datafile(); df4.setName("df4_" + timestamp); df4.setLocation(ds2Loc + "Person's file"); df4.setDataset(ds2); - writeToFile(df4, - "df4 test content very compressible very compressible", - key); + writeToFile(df4, "df4 test content very compressible very compressible", key); datasetIds.add(ds1.getId()); datasetIds.add(ds2.getId()); @@ -334,16 +311,14 @@ private void populateStorage(boolean twoLevel, String storageUnit, moveDatasetToArchive(storageUnit, ds2, ds2Loc, fac, inv, key); } - newFileLocation = setup.getUpdownDir() - .resolve("new_file_" + timestamp); + newFileLocation = setup.getUpdownDir().resolve("new_file_" + timestamp); Files.createDirectories(newFileLocation.getParent()); byte[] bytes = "new_file test content".getBytes(); OutputStream out = Files.newOutputStream(newFileLocation); out.write(bytes); out.close(); } catch (IllegalAccessError e) { - System.err.println( - "Could not prepare ICAT db for testing: " + e.getMessage()); + System.err.println("Could not prepare ICAT db for testing: " + e.getMessage()); e.printStackTrace(); throw e; } @@ -356,8 +331,8 @@ private void populateStorage(boolean twoLevel, String storageUnit, protected Map ids = new HashMap<>(); protected Map paths = new HashMap<>(); - protected void checkZipStream(InputStream stream, List datafileIdsIn, - long compressedSize, int numLeft) throws IOException { + protected void checkZipStream(InputStream stream, List datafileIdsIn, long compressedSize, int numLeft) + throws IOException { ZipInputStream zis = new ZipInputStream(stream); ZipEntry ze = zis.getNextEntry(); List idsNeeded = new ArrayList<>(datafileIdsIn); @@ -386,8 +361,7 @@ protected void checkStream(InputStream stream, long id) throws IOException { boolean found = false; for (Entry e : ids.entrySet()) { if (id == e.getValue()) { - assertEquals(contents.get(e.getKey()), - new String(getOutput(stream))); + assertEquals(contents.get(e.getKey()), new String(getOutput(stream))); found = true; break; } @@ -396,8 +370,7 @@ protected void checkStream(InputStream stream, long id) throws IOException { } protected void writeToFile(Datafile df, String content, String key) - throws IOException, IcatException_Exception, - NoSuchAlgorithmException { + throws IOException, IcatException_Exception, NoSuchAlgorithmException { Path path = setup.getStorageDir().resolve(df.getLocation()); Files.createDirectories(path.getParent()); byte[] bytes = content.getBytes(); @@ -423,16 +396,16 @@ protected void writeToFile(Datafile df, String content, String key) Dataset ds = df.getDataset(); Investigation inv = ds.getInvestigation(); Facility fac = inv.getFacility(); - paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" - + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" - + ds.getName() + "/" + df.getName(), df.getLocation()); + paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + URLEncoder.encode(inv.getVisitId(), "UTF-8") + + "/" + ds.getName() + "/" + df.getName(), df.getLocation()); } - private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', - '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + private static final char[] HEX_CHARS = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' + }; - private String digest(Long id, String location, String key) - throws NoSuchAlgorithmException { + private String digest(Long id, String location, String key) throws NoSuchAlgorithmException { byte[] pattern = (id + location + key).getBytes(); MessageDigest digest = null; digest = MessageDigest.getInstance("SHA-256"); @@ -447,19 +420,16 @@ private String digest(Long id, String location, String key) return new String(hexChars); } - private void moveDatasetToArchive(String storageUnit, Dataset ds, - String dsLoc, Facility fac, Investigation inv, String key) - throws IOException, IcatException_Exception { - ds = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Datafile", - ds.getId()); + private void moveDatasetToArchive(String storageUnit, Dataset ds, String dsLoc, Facility fac, Investigation inv, + String key) throws IOException, IcatException_Exception { + ds = (Dataset) icatWS.get(sessionId, "Dataset INCLUDE Datafile", ds.getId()); Path top = setup.getStorageDir(); if (storageUnit.equals("DATASET")) { Path zipFile = setup.getStorageArchiveDir().resolve(dsLoc); Files.createDirectories(zipFile.getParent()); - ZipOutputStream zos = new ZipOutputStream( - Files.newOutputStream(zipFile)); + ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile)); zos.setLevel(0); for (Datafile df : ds.getDatafiles()) { @@ -467,15 +437,12 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, if (key == null) { file = top.resolve(df.getLocation()); } else { - file = top.resolve(getLocationFromDigest(df.getId(), - df.getLocation())); + file = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); } InputStream fis = Files.newInputStream(file); - zos.putNextEntry(new ZipEntry( - "ids/" + fac.getName() + "/" + inv.getName() + "/" - + URLEncoder.encode(inv.getVisitId(), "UTF-8") - + "/" + ds.getName() + "/" + df.getName())); + zos.putNextEntry(new ZipEntry("ids/" + fac.getName() + "/" + inv.getName() + "/" + + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" + ds.getName() + "/" + df.getName())); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { @@ -494,11 +461,9 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, if (key == null) { p = top.resolve(df.getLocation()); } else { - p = top.resolve(getLocationFromDigest(df.getId(), - df.getLocation())); + p = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); } - Files.move(p, archive.resolve(p.getFileName()), - StandardCopyOption.REPLACE_EXISTING); + Files.move(p, archive.resolve(p.getFileName()), StandardCopyOption.REPLACE_EXISTING); } } for (Datafile df : ds.getDatafiles()) { @@ -506,8 +471,7 @@ private void moveDatasetToArchive(String storageUnit, Dataset ds, if (key == null) { file = top.resolve(df.getLocation()); } else { - file = top.resolve( - getLocationFromDigest(df.getId(), df.getLocation())); + file = top.resolve(getLocationFromDigest(df.getId(), df.getLocation())); } Files.deleteIfExists(file); Path parent = file.getParent(); @@ -544,10 +508,8 @@ protected void raceTest() throws Exception { } logTime("File created"); - Investigation inv = (Investigation) icatWS - .search(sessionId, "Investigation").get(0); - DatasetType dst = (DatasetType) icatWS.search(sessionId, "DatasetType") - .get(0); + Investigation inv = (Investigation) icatWS.search(sessionId, "Investigation").get(0); + DatasetType dst = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); Dataset ds = new Dataset(); ds.setName("Big ds"); @@ -557,21 +519,17 @@ protected void raceTest() throws Exception { ds.setId(dsid); for (int i = 0; i < 30; i++) { - testingClient.put(sessionId, Files.newInputStream(path), - "uploaded_file" + i, dsid, supportedDatafileFormat.getId(), - "A rather splendid datafile", 201); + testingClient.put(sessionId, Files.newInputStream(path), "uploaded_file" + i, dsid, + supportedDatafileFormat.getId(), "A rather splendid datafile", 201); } - testingClient.archive(sessionId, new DataSelection().addDataset(dsid), - 204); + testingClient.archive(sessionId, new DataSelection().addDataset(dsid), 204); logTime("Put and archive calls"); waitForIds(300); logTime("Archive complete"); - testingClient.restore(sessionId, new DataSelection().addDataset(dsid), - 204); - while (testingClient.getStatus(sessionId, - new DataSelection().addDataset(dsid), 200) != Status.ONLINE) { + testingClient.restore(sessionId, new DataSelection().addDataset(dsid), 204); + while (testingClient.getStatus(sessionId, new DataSelection().addDataset(dsid), 200) != Status.ONLINE) { Thread.sleep(1000); } logTime("Marked online"); @@ -580,8 +538,7 @@ protected void raceTest() throws Exception { assertTrue(stat.getOpItems().toString(), stat.getOpItems().isEmpty()); - testingClient.delete(sessionId, new DataSelection().addDataset(dsid), - 204); + testingClient.delete(sessionId, new DataSelection().addDataset(dsid), 204); icatWS.delete(sessionId, ds); waitForIds(300); @@ -594,20 +551,16 @@ protected void raceTest() throws Exception { public void getDatafileIdsTest() throws Exception { List ids = testingClient.getDatafileIds(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), - 200); + new DataSelection().addDataset(datasetIds.get(0)).addDatafile(datafileIds.get(0)), 200); assertEquals(2, ids.size()); assertTrue(ids.contains(datafileIds.get(0))); assertTrue(ids.contains(datafileIds.get(1))); - ids = testingClient.getDatafileIds(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), 200); + ids = testingClient.getDatafileIds(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 200); assertEquals(1, ids.size()); assertTrue(ids.contains(datafileIds.get(0))); - ids = testingClient.getDatafileIds(sessionId, - new DataSelection().addInvestigation(investigationId), 200); + ids = testingClient.getDatafileIds(sessionId, new DataSelection().addInvestigation(investigationId), 200); assertEquals(4, ids.size()); for (Long id : datafileIds) { assertTrue(ids.contains(id)); @@ -618,31 +571,24 @@ public void getDatafileIdsTest() throws Exception { public void bigDataSelectionTest() throws Exception { String icatUrl = testingClient.getIcatUrl(200).toExternalForm(); ICAT restIcat = new org.icatproject.icat.client.ICAT(icatUrl); - try (JsonReader parser = Json.createReader(new ByteArrayInputStream( - restIcat.getProperties().getBytes()))) { - assertEquals( - "maxEntities must have a fixed value in the icat.server for test to be useful", - 20, parser.readObject().getInt("maxEntities")); + try (JsonReader parser = Json.createReader(new ByteArrayInputStream(restIcat.getProperties().getBytes()))) { + assertEquals("maxEntities must have a fixed value in the icat.server for test to be useful", 20, + parser.readObject().getInt("maxEntities")); } - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); List idList = new ArrayList<>(); for (int i = 0; i < 45; i++) { - Long dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp + i, datasetIds.get(0), - supportedDatafileFormat.getId(), + Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp + i, datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", 201); idList.add(dfid); } waitForIds(); List idList2 = testingClient.getDatafileIds(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), - 200); + new DataSelection().addDataset(datasetIds.get(0)).addDatafile(datafileIds.get(0)), 200); assertEquals(47, idList2.size()); for (Long id : idList) { assertTrue(idList2.contains(id)); @@ -659,96 +605,69 @@ public void bigDataSelectionTest() throws Exception { assertTrue(idList2.contains(idList.get(i))); } - idList2 = testingClient.getDatafileIds(sessionId, - new DataSelection().addInvestigation(investigationId), 200); + idList2 = testingClient.getDatafileIds(sessionId, new DataSelection().addInvestigation(investigationId), 200); assertEquals(49, idList2.size()); } public void cloningTest() throws Exception { - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); - assertEquals(104, testingClient.getSize(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 200)); + assertEquals(104, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 200)); Session s = icat.getSession(sessionId); Map m = new HashMap<>(); m.put("name", "newOne"); long dfid = datafileIds.get(0); long ndfid = s.cloneEntity("Datafile", dfid, m); - Datafile df = (Datafile) icatWS.get(sessionId, - "Datafile df INCLUDE df.dataset.investigation.facility", ndfid); + Datafile df = (Datafile) icatWS.get(sessionId, "Datafile df INCLUDE df.dataset.investigation.facility", ndfid); Dataset ds = df.getDataset(); Investigation inv = ds.getInvestigation(); Facility fac = inv.getFacility(); - paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" - + URLEncoder.encode(inv.getVisitId(), "UTF-8") + "/" - + ds.getName() + "/" + df.getName(), df.getLocation()); + paths.put("ids/" + fac.getName() + "/" + inv.getName() + "/" + URLEncoder.encode(inv.getVisitId(), "UTF-8") + + "/" + ds.getName() + "/" + df.getName(), df.getLocation()); crcs.put(df.getLocation(), df.getChecksum()); fsizes.put(df.getLocation(), df.getFileSize()); - contents.put(df.getLocation(), - "df1 test content very compressible very compressible"); + contents.put(df.getLocation(), "df1 test content very compressible very compressible"); ids.put(df.getLocation(), df.getId()); - assertEquals(156, testingClient.getSize(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 200)); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, - 200)) { + assertEquals(156, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 200)); + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, 200)) { List dfids = new ArrayList<>(datafileIds.subList(0, 2)); dfids.add(ndfid); checkZipStream(stream, dfids, 57, 0); } long dsid = datasetIds.get(0); assertEquals(2, getDirOnFastStorage(dsid).toFile().list().length); - assertEquals("[3]", s.search( - "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " - + dsid)); - testingClient.delete(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), 204); - assertEquals(2, - getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); - assertEquals("[2]", s.search( - "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " - + dsid)); - testingClient.delete(sessionId, - new DataSelection().addDatafile(datafileIds.get(1)), 204); - assertEquals(1, - getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); - assertEquals("[1]", s.search( - "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " - + dsid)); - testingClient.delete(sessionId, new DataSelection().addDatafile(ndfid), - 204); + assertEquals("[3]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); + assertEquals(2, getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); + assertEquals("[2]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(1)), 204); + assertEquals(1, getDirOnFastStorage(datasetIds.get(0)).toFile().list().length); + assertEquals("[1]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); + testingClient.delete(sessionId, new DataSelection().addDatafile(ndfid), 204); assertFalse(getDirOnFastStorage(datasetIds.get(0)).toFile().exists()); - assertEquals("[0]", s.search( - "SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " - + dsid)); + assertEquals("[0]", s.search("SELECT COUNT(df) from Datafile df WHERE df.dataset.id = " + dsid)); } public void reliabilityTest() throws Exception { - testingClient.restore(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), - 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); waitForIds(); - Long dfid1 = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + Long dfid1 = testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file2_" + timestamp, + datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", 201); - testingClient.archive(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), - 204); + testingClient.archive(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures().isEmpty()); setup.setReliability(0.); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(dfid1), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(dfid1), Flag.NONE, + 200); try { while (!testingClient.isPrepared(preparedId, null)) { @@ -757,49 +676,38 @@ public void reliabilityTest() throws Exception { } catch (Exception e) { System.out.println(e); assertEquals("Restore failed", e.getMessage()); - Set failures = testingClient.getServiceStatus(sessionId, 200) - .getFailures(); + Set failures = testingClient.getServiceStatus(sessionId, 200).getFailures(); assertEquals(1, failures.size()); setup.setReliability(1.); testingClient.reset(preparedId, 204); - assertTrue(testingClient.getServiceStatus(sessionId, 200) - .getFailures().isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getFailures().isEmpty()); while (!testingClient.isPrepared(preparedId, null)) { Thread.sleep(1000); } } setup.setReliability(0.); - Long dfid2 = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "An even better datafile", - "7.1.3", new Date(420000), new Date(42000), 201); + Long dfid2 = testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file3_" + timestamp, + datasetIds.get(0), supportedDatafileFormat.getId(), "An even better datafile", "7.1.3", + new Date(420000), new Date(42000), 201); waitForIds(); System.out.println(testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(dfid2), - 200)); + new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 200)); setup.setReliability(1.); - testingClient.restore(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDatafile(dfid2), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 204); waitForIds(); System.out.println(testingClient.getStatus(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(dfid2), - 200)); + new DataSelection().addDataset(datasetIds.get(0)).addDatafile(dfid2), 200)); } protected void reliabilityTest2() throws Exception { - DataSelection dsel = new DataSelection() - .addDatafile(datafileIds.get(0)); + DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(0)); testingClient.archive(sessionId, dsel, 204); waitForIds(); setup.setReliability(0.); - String preparedId = testingClient.prepareData(sessionId, dsel, - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, dsel, Flag.NONE, 200); try { while (!testingClient.isPrepared(preparedId, null)) { @@ -824,8 +732,7 @@ protected void reliabilityTest2() throws Exception { } protected void reliabilityTest3() throws Exception { - DataSelection dsel = new DataSelection() - .addDatafile(datafileIds.get(0)); + DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(0)); testingClient.archive(sessionId, dsel, 204); waitForIds(); @@ -833,8 +740,7 @@ protected void reliabilityTest3() throws Exception { testingClient.restore(sessionId, dsel, 204); try { - while (testingClient.getStatus(sessionId, dsel, - null) != Status.ONLINE) { + while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { Thread.sleep(1000); } fail("Should throw an error"); @@ -845,8 +751,7 @@ protected void reliabilityTest3() throws Exception { setup.setReliability(1.); testingClient.restore(sessionId, dsel, 204); try { - while (testingClient.getStatus(sessionId, dsel, - null) != Status.ONLINE) { + while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { Thread.sleep(1000); } } catch (Exception e) { @@ -855,8 +760,7 @@ protected void reliabilityTest3() throws Exception { testingClient.reset(sessionId, dsel, 204); testingClient.restore(sessionId, dsel, 204); - while (testingClient.getStatus(sessionId, dsel, - null) != Status.ONLINE) { + while (testingClient.getStatus(sessionId, dsel, null) != Status.ONLINE) { Thread.sleep(1000); } } @@ -866,11 +770,9 @@ protected void isPreparedTest() throws Exception { int numDs = 30; int numDf = 19; - Investigation inv = (Investigation) icatWS - .search(sessionId, "Investigation INCLUDE Facility").get(0); + Investigation inv = (Investigation) icatWS.search(sessionId, "Investigation INCLUDE Facility").get(0); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS - .search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); Facility fac = inv.getFacility(); String key = setup.getKey(); @@ -909,8 +811,7 @@ protected void isPreparedTest() throws Exception { logTime("Put calls"); - String preparedId = testingClient.prepareData(sessionId, dsel, Flag.ZIP, - 200); + String preparedId = testingClient.prepareData(sessionId, dsel, Flag.ZIP, 200); logTime("Data prepared"); while (!testingClient.isPrepared(preparedId, 200)) { @@ -928,8 +829,7 @@ protected void isPreparedTest() throws Exception { logTime("Read " + l + " bytes"); } - testingClient.archive(sessionId, new DataSelection().addDataset(dsid), - 204); + testingClient.archive(sessionId, new DataSelection().addDataset(dsid), 204); waitForIds(300); logTime(null); diff --git a/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java index 2daa2b7e..d6cc867a 100644 --- a/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/BogusDatafileTest.java @@ -1,17 +1,15 @@ package org.icatproject.ids.integration.one; -import static org.junit.Assert.assertEquals; - import java.io.InputStream; import java.util.Collections; +import static org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; import org.icatproject.Dataset; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; @@ -40,24 +38,21 @@ public static void setup() throws Exception { public void createBogusFiles() throws Exception { long timestamp = System.currentTimeMillis(); - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -72,11 +67,9 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -85,8 +78,7 @@ public void getEmptyDataset() throws Exception { @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); } @@ -94,11 +86,9 @@ public void getSizeEmptyDataset() throws Exception { @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } @@ -107,8 +97,7 @@ public void getNonEmptyDataset() throws Exception { @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); } @@ -116,11 +105,9 @@ public void getSizeNonEmptyDataset() throws Exception { @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, 404)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -129,8 +116,7 @@ public void getBogusFile() throws Exception { @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); } diff --git a/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java index 7314e6d1..5f5b4881 100644 --- a/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/DeleteTest.java @@ -1,13 +1,12 @@ package org.icatproject.ids.integration.one; +import java.nio.file.Files; +import java.nio.file.Path; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; - -import java.nio.file.Files; -import java.nio.file.Path; - import org.junit.BeforeClass; import org.junit.Test; @@ -27,12 +26,9 @@ public static void setup() throws Exception { @Test public void deleteDatafileTest() throws Exception { - DataSelection dsel = new DataSelection() - .addDatafile(datafileIds.get(3)); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, dsel, 200)); - testingClient.delete(sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), 204); + DataSelection dsel = new DataSelection().addDatafile(datafileIds.get(3)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); try { testingClient.getStatus(sessionId, dsel, 404); fail(); @@ -48,12 +44,10 @@ public void deleteDatafileTest() throws Exception { public void deleteDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); DataSelection dsel = new DataSelection().addDataset(datasetIds.get(1)); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, dsel, 200)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); assertTrue(Files.exists(dirOnFastStorage)); testingClient.delete(sessionId, dsel, 204); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, dsel, 200)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, dsel, 200)); assertFalse(Files.exists(dirOnFastStorage)); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java index f7340ec2..16026ef5 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetDataExplicitTest.java @@ -1,15 +1,13 @@ package org.icatproject.ids.integration.one; -import static org.junit.Assert.assertEquals; - import java.io.InputStream; import java.util.Arrays; +import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -31,13 +29,11 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", - datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, - new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -51,15 +47,12 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", - datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection() - .addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, - Flag.NONE, 200); + DataSelection selection = new DataSelection().addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -71,74 +64,59 @@ public void getSizePreparedId() throws Exception { @Test public void getSizes1() throws Exception { - assertEquals(208L, testingClient.getSize(sessionId, - new DataSelection().addDatafiles(datafileIds), 200)); - assertEquals(208L, testingClient.getSize(sessionId, - new DataSelection().addDatasets(datasetIds), 200)); - assertEquals(208L, testingClient.getSize(sessionId, - new DataSelection().addInvestigation(investigationId), 200)); + assertEquals(208L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(208L, testingClient.getSize(sessionId, new DataSelection().addDatasets(datasetIds), 200)); assertEquals(208L, - testingClient.getSize(sessionId, - new DataSelection().addInvestigation(investigationId) - .addDatafiles(datafileIds), - 200)); - assertEquals(0L, testingClient.getSize(sessionId, - new DataSelection().addDataset(datasetIds.get(2)), 200)); + testingClient.getSize(sessionId, new DataSelection().addInvestigation(investigationId), 200)); + assertEquals(208L, testingClient.getSize(sessionId, + new DataSelection().addInvestigation(investigationId).addDatafiles(datafileIds), 200)); + assertEquals(0L, testingClient.getSize(sessionId, new DataSelection().addDataset(datasetIds.get(2)), 200)); } @Test(expected = NotFoundException.class) public void getSizesBad() throws Exception { - testingClient.getSize(sessionId, new DataSelection().addDatafile(563L), - 404); + testingClient.getSize(sessionId, new DataSelection().addDatafile(563L), 404); } @Test(expected = NotFoundException.class) public void getSizesBad2() throws Exception { - testingClient.getSize(sessionId, - new DataSelection().addDatafile(563L).addDatafile(564L), 404); + testingClient.getSize(sessionId, new DataSelection().addDatafile(563L).addDatafile(564L), 404); } @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0, - 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0, 400)) { } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - 403)) { + try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { } } @Test public void correctBehaviourTestNone() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 0, 200)) { checkStream(stream, datafileIds.get(0)); } } @Test public void correctBehaviourTestCompress() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.COMPRESS, 0, - 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.COMPRESS, 0, 200)) { checkZipStream(stream, datafileIds, 36L, 0); } - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), Flag.COMPRESS, 0, 200)) { checkStream(stream, datafileIds.get(0)); } @@ -146,29 +124,25 @@ public void correctBehaviourTestCompress() throws Exception { @Test public void correctBehaviourTestZip() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.ZIP, 0, - 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.ZIP, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP, - 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } @Test public void correctBehaviourTestZipAndCompress() throws Exception { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.ZIP_AND_COMPRESS, 0, 200)) { checkZipStream(stream, datafileIds, 36L, 0); } - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP_AND_COMPRESS, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 36L, 0); } @@ -177,31 +151,25 @@ public void correctBehaviourTestZipAndCompress() throws Exception { @Test public void correctBehaviourInvestigation() throws Exception { try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addInvestigation(investigationId), - Flag.NONE, 0, 200)) { + new DataSelection().addInvestigation(investigationId), Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP, - 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } @Test public void correctBehaviourInvestigations() throws Exception { - try (InputStream stream = testingClient - .getData(sessionId, - new DataSelection().addInvestigations( - Arrays.asList(investigationId)), - Flag.NONE, 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, + new DataSelection().addInvestigations(Arrays.asList(investigationId)), Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57L, 0); } - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.ZIP, - 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.ZIP, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 1), 57L, 0); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java index 997a11cf..9d844536 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetDataForPreparedIdTest.java @@ -1,9 +1,8 @@ package org.icatproject.ids.integration.one; -import static org.junit.Assert.assertEquals; - import java.io.InputStream; +import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; @@ -26,17 +25,15 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, - 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); do { Thread.sleep(500); @@ -48,8 +45,7 @@ public void badOffsetFormatTest() throws Exception { @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient - .getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { } } @@ -59,15 +55,13 @@ public void correctBehaviourNoOffsetTest() throws Exception { for (Flag flag : Flag.values()) { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), flag, - 200); + new DataSelection().addDatafile(datafileIds.get(0)), flag, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - try (InputStream stream = testingClient.getData(preparedId, 0, - 200)) { + try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { if (flag == Flag.NONE || flag == Flag.COMPRESS) { checkStream(stream, datafileIds.get(0)); } else if (flag == Flag.ZIP) { @@ -80,10 +74,9 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() - throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -97,9 +90,8 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -110,13 +102,10 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() - throws Exception { + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -133,8 +122,7 @@ public void correctBehaviourWithOffsetTest() throws Exception { for (Flag flag : Flag.values()) { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), flag, - 200); + new DataSelection().addDatafile(datafileIds.get(0)), flag, 200); do { Thread.sleep(1000); @@ -142,16 +130,13 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the file twice, with and without an offset byte[] out = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] outOffset = getOutput( - testingClient.getData(preparedId, goodOffset, 206)); + byte[] outOffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the // offset - System.out - .println(flag + ": " + out.length + " " + outOffset.length); + System.out.println(flag + ": " + out.length + " " + outOffset.length); for (int i = 0; i < outOffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) outOffset[i], - (byte) out[i + goodOffset]); + assertEquals("Byte offset: " + i, (byte) outOffset[i], (byte) out[i + goodOffset]); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java index f2a82f32..d5db81b7 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetStatusExplicitTest.java @@ -1,9 +1,8 @@ package org.icatproject.ids.integration.one; -import static org.junit.Assert.assertEquals; - import java.util.Arrays; +import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; @@ -41,20 +40,17 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, new DataSelection() - .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(sessionId, new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTestAnon() throws Exception { - testingClient.getStatus(null, new DataSelection() - .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(null, new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - testingClient.getStatus(setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), 403); + testingClient.getStatus(setup.getForbiddenSessionId(), new DataSelection().addDatafiles(datafileIds), 403); } @Test @@ -63,8 +59,7 @@ public void correctBehaviourTest() throws Exception { Status status; do { Thread.sleep(1000); - status = testingClient.getStatus(sessionId, - new DataSelection().addDatafiles(datafileIds), 200); + status = testingClient.getStatus(sessionId, new DataSelection().addDatafiles(datafileIds), 200); System.out.println("*" + status + "*"); } while (status != Status.ONLINE); @@ -72,10 +67,8 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, - Flag.NONE, 200); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } diff --git a/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java index b0c72fb6..b870eba0 100644 --- a/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/GetStatusForPreparedIdTest.java @@ -33,20 +33,22 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafiles( - Arrays.asList(1L, 2L, 3L, 99999L)), Flag.NONE, 404); + testingClient + .prepareData(sessionId, + new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), + Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, - new DataSelection().addDatafile(99999L), Flag.NONE, 404); + testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, + 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, - new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient + .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) diff --git a/src/test/java/org/icatproject/ids/integration/one/MiscTest.java b/src/test/java/org/icatproject/ids/integration/one/MiscTest.java index d5eaffe7..3baf012c 100644 --- a/src/test/java/org/icatproject/ids/integration/one/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/MiscTest.java @@ -1,10 +1,9 @@ package org.icatproject.ids.integration.one; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.io.InputStream; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -42,20 +41,17 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); @@ -64,15 +60,13 @@ public void correctBehaviourNoOffsetTest() throws Exception { @Test(expected = NotImplementedException.class) public void restoreNotAvailableTest() throws Exception { - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); waitForIds(); } @Test(expected = NotImplementedException.class) public void archiveNotAvailableTest() throws Exception { - testingClient.archive(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); waitForIds(); } diff --git a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java index cf1a064b..1943ae01 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java @@ -29,8 +29,7 @@ public static void beforeClass() throws Exception { int n = 100000; start = System.currentTimeMillis(); - try (OutputStream f = Files.newOutputStream(infile, - StandardOpenOption.CREATE)) { + try (OutputStream f = Files.newOutputStream(infile, StandardOpenOption.CREATE)) { for (int i = 0; i < n; i++) { f.write(junk); } @@ -51,13 +50,12 @@ public static void afterClass() throws Exception { public void putOneFileTest() throws Exception { byte[] junk = new byte[1000]; start = System.currentTimeMillis(); - Long dfid = testingClient.put(sessionId, Files.newInputStream(infile), - "big_" + timestamp, datasetIds.get(0), + Long dfid = testingClient.put(sessionId, Files.newInputStream(infile), "big_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), "A big datafile", 201); ts("store file (put)"); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(dfid), Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(dfid), Flag.NONE, 0, + null)) { boolean first = true; while (stream.read(junk) > 0) { if (first) { @@ -79,15 +77,14 @@ private static void ts(String msg) { public void putAsPostOneFileTest() throws Exception { byte[] junk = new byte[1000]; start = System.currentTimeMillis(); - Long dfid = testingClient.putAsPost(sessionId, - Files.newInputStream(infile), "big2_" + timestamp, - datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", null, null, null, true, 201); + Long dfid = testingClient.putAsPost(sessionId, Files.newInputStream(infile), "big2_" + timestamp, + datasetIds.get(0), supportedDatafileFormat.getId(), "A rather splendid datafile", null, null, null, + true, 201); ts("store file (post)"); int ntot = 0; - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(dfid), Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(dfid), Flag.NONE, 0, + null)) { boolean first = true; int n; diff --git a/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java index 6e56832a..acdf7be9 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PrepareDataTest.java @@ -1,12 +1,11 @@ package org.icatproject.ids.integration.one; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.nio.file.Files; import java.util.List; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -27,30 +26,27 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 400); } @Test(expected = BadRequestException.class) public void noIdsTest() throws Exception { - testingClient.prepareData("bad sessionId format", new DataSelection(), - Flag.NONE, 400); + testingClient.prepareData("bad sessionId format", new DataSelection(), Flag.NONE, 400); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 403); + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); System.out.println(preparedId); assertNotNull(preparedId); } @@ -58,9 +54,8 @@ public void correctBehaviourTest() throws Exception { @Test public void prepareDataset() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -71,17 +66,14 @@ public void prepareDataset() throws Exception { Thread.sleep(1000); } - assertTrue( - Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareTwoDatasets() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -93,16 +85,14 @@ public void prepareTwoDatasets() throws Exception { Thread.sleep(1000); } - assertTrue( - Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareDatafile() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -112,17 +102,14 @@ public void prepareDatafile() throws Exception { Thread.sleep(1000); } - assertTrue( - Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } @Test public void prepareDatafileAndItsDataset() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -133,8 +120,7 @@ public void prepareDatafileAndItsDataset() throws Exception { Thread.sleep(1000); } - assertTrue( - Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); + assertTrue(Files.exists(setup.getPreparedCacheDir().resolve(preparedId))); } } diff --git a/src/test/java/org/icatproject/ids/integration/one/PutTest.java b/src/test/java/org/icatproject/ids/integration/one/PutTest.java index 278383d8..0a6dec0a 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PutTest.java @@ -1,20 +1,18 @@ package org.icatproject.ids.integration.one; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - import java.nio.file.Files; import java.nio.file.Path; import java.util.Date; import java.util.GregorianCalendar; import javax.xml.datatype.DatatypeFactory; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -40,11 +38,9 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - Long dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); Datafile df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("A rather splendid datafile", df.getDescription()); @@ -53,11 +49,9 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileModTime()); assertTrue(Files.exists(dirOnFastStorage)); - dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "An even better datafile", - "7.1.3", new Date(420000), new Date(42000), 201); + dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -78,11 +72,9 @@ public void putAsPostOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - Long dfid = testingClient.putAsPost(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "A rather splendid datafile", - null, null, null, true, 201); + Long dfid = testingClient.putAsPost(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", null, null, null, true, 201); Datafile df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("A rather splendid datafile", df.getDescription()); @@ -91,11 +83,9 @@ public void putAsPostOneFileTest() throws Exception { assertNull(df.getDatafileModTime()); assertTrue(Files.exists(dirOnFastStorage)); - dfid = testingClient.putAsPost(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "An even better datafile", - "7.1.3", new Date(420000), new Date(42000), false, 201); + dfid = testingClient.putAsPost(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "An even better datafile", "7.1.3", new Date(420000), new Date(42000), false, 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); diff --git a/src/test/java/org/icatproject/ids/integration/one/WriteTest.java b/src/test/java/org/icatproject/ids/integration/one/WriteTest.java index 58891e59..9a7550a3 100644 --- a/src/test/java/org/icatproject/ids/integration/one/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/WriteTest.java @@ -18,8 +18,7 @@ public static void setup() throws Exception { @Test(expected = NotImplementedException.class) public void writeNotAvailableTest() throws Exception { - testingClient.write(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 501); + testingClient.write(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 501); } } diff --git a/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java b/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java index 56c39c26..d5bf557e 100644 --- a/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/ArchiveTest.java @@ -1,12 +1,11 @@ package org.icatproject.ids.integration.two; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -31,15 +30,13 @@ public void restoreThenArchiveDataset() throws Exception { assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); diff --git a/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java index 90dce2b6..3ff9adf1 100644 --- a/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/BogusDatafileTest.java @@ -1,9 +1,5 @@ package org.icatproject.ids.integration.two; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.io.File; import java.io.InputStream; import java.nio.file.Files; @@ -12,13 +8,15 @@ import java.util.HashSet; import java.util.Set; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; import org.icatproject.Dataset; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; @@ -49,24 +47,21 @@ public static void setup() throws Exception { @Before public void createBogusFiles() throws Exception { - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -82,11 +77,9 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -95,8 +88,7 @@ public void getEmptyDataset() throws Exception { @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); } @@ -104,17 +96,14 @@ public void getSizeEmptyDataset() throws Exception { @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); testingClient.restore(sessionId, selection, 204); waitForIds(); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, selection, null)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, null)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } @@ -123,8 +112,7 @@ public void getNonEmptyDataset() throws Exception { @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); } @@ -132,17 +120,14 @@ public void getSizeNonEmptyDataset() throws Exception { @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.restore(sessionId, selection, 404); waitForIds(); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, selection, 404)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, 404)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, 404)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -151,17 +136,15 @@ public void getBogusFile() throws Exception { @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); } /* - * Try the full cycle: upload a new file into a dataset having a bogus file, - * which triggers a write of the dataset to archive storage, archive the - * dataset, and restore it. Each step must deal gracefully with the bogus - * file in the dataset. + * Try the full cycle: upload a new file into a dataset having a bogus file, which triggers a write of the + * dataset to archive storage, archive the dataset, and restore it. Each step must deal gracefully with + * the bogus file in the dataset. */ @Test public void putWriteArchiveRestore() throws Exception { @@ -174,11 +157,9 @@ public void putWriteArchiveRestore() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfulId = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + Long dfulId = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); Datafile dful = (Datafile) icatWS.get(sessionId, "Datafile", dfulId); testingClient.archive(sessionId, selection, 204); waitForIds(); @@ -191,16 +172,13 @@ public void putWriteArchiveRestore() throws Exception { File[] filesList = dirOnFastStorage.toFile().listFiles(); assertEquals(3, filesList.length); Set locations = new HashSet<>(); - Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", - datafileIds.get(0)); + Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(0)); locations.add(getLocationFromDigest(df1.getId(), df1.getLocation())); - Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", - datafileIds.get(1)); + Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(1)); locations.add(getLocationFromDigest(df2.getId(), df2.getLocation())); locations.add(getLocationFromDigest(dful.getId(), dful.getLocation())); for (File file : filesList) { - String location = setup.getStorageDir().relativize(file.toPath()) - .toString(); + String location = setup.getStorageDir().relativize(file.toPath()).toString(); assertTrue(locations.contains(location)); } diff --git a/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java index da6b435f..d8179493 100644 --- a/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/DeleteTest.java @@ -20,8 +20,7 @@ public static void setup() throws Exception { @Test(expected = DataNotOnlineException.class) public void deleteFromUnrestoredDataset() throws Exception { - testingClient.delete(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 503); + testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 503); } @Test @@ -30,13 +29,11 @@ public void deleteDatafileFromRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); - testingClient.delete(sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), 204); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); waitForIds(); checkZipFile(fileOnArchiveStorage, datafileIds.subList(2, 3), 36); @@ -47,14 +44,12 @@ public void deleteRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); checkPresent(fileOnArchiveStorage); - testingClient.delete(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkAbsent(dirOnFastStorage); checkAbsent(fileOnArchiveStorage); diff --git a/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java index 30e012b3..1a3970a0 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetDataExplicitTest.java @@ -1,15 +1,13 @@ package org.icatproject.ids.integration.two; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - import java.io.InputStream; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -33,17 +31,14 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, - 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - 403)) { + try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { } } @@ -52,13 +47,11 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", - datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, - new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -72,15 +65,12 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", - datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection() - .addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, - Flag.NONE, 200); + DataSelection selection = new DataSelection().addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -93,18 +83,16 @@ public void getSizePreparedId() throws Exception { @Test public void correctBehaviourTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - 503)) { + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.NONE, + 0, 503)) { fail("Should have thrown exception"); } catch (IdsException e) { assertEquals(DataNotOnlineException.class, e.getClass()); } while (true) { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - null)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds, 57, 0); break; } catch (IdsException e) { @@ -117,18 +105,16 @@ public void correctBehaviourTest() throws Exception { @Test public void gettingDatafileRestoresItsDatasetTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(2)), Flag.NONE, - 0, null)) { + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)), + Flag.NONE, 0, null)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), Flag.NONE, - 0, 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), + Flag.NONE, 0, 200)) { checkStream(stream, datafileIds.get(3)); } @@ -137,9 +123,8 @@ public void gettingDatafileRestoresItsDatasetTest() throws Exception { @Test public void gettingDatasetUsesCacheTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, - null)) { + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, null)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -147,32 +132,26 @@ public void gettingDatasetUsesCacheTest() throws Exception { waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, - 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } } @Test - public void gettingDatafileAndDatasetShouldRestoreBothDatasetsTest() - throws Exception { - - try (InputStream z = testingClient - .getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(2)) - .addDataset(datasetIds.get(0)), - Flag.NONE, 0, 503)) { + public void gettingDatafileAndDatasetShouldRestoreBothDatasetsTest() throws Exception { + + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)) + .addDataset(datasetIds.get(0)), Flag.NONE, 0, 503)) { fail("Should throw exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatasets(datasetIds), Flag.NONE, 0, - 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatasets(datasetIds), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds, 57, 0); } } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java index 8e5311fa..97f66d5a 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetDataForPreparedIdTest.java @@ -1,9 +1,8 @@ package org.icatproject.ids.integration.two; -import static org.junit.Assert.assertEquals; - import java.io.InputStream; +import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; @@ -26,17 +25,15 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, - 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -48,16 +45,14 @@ public void badOffsetFormatTest() throws Exception { @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient - .getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { } } @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -69,10 +64,9 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() - throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -87,9 +81,8 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -101,13 +94,10 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() - throws Exception { + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), - Flag.NONE, 200); + new DataSelection().addDataset(datasetIds.get(0)).addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -121,9 +111,8 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() @Test public void correctBehaviourWithOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -131,14 +120,12 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the zip file twice, with and without an offset byte[] zip = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] zipoffset = getOutput( - testingClient.getData(preparedId, goodOffset, 206)); + byte[] zipoffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the offset System.out.println(zip.length + " " + zipoffset.length); for (int i = 0; i < zipoffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) zipoffset[i], - (byte) zip[i + goodOffset]); + assertEquals("Byte offset: " + i, (byte) zipoffset[i], (byte) zip[i + goodOffset]); } } diff --git a/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java index 4207e6c5..8ecd4429 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetStatusExplicitTest.java @@ -1,9 +1,8 @@ package org.icatproject.ids.integration.two; -import static org.junit.Assert.assertEquals; - import java.util.Arrays; +import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; @@ -35,8 +34,8 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, new DataSelection() - .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(sessionId, + new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) @@ -56,10 +55,8 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, - Flag.NONE, 200); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } @@ -67,8 +64,7 @@ public void getStatusPreparedIdTest() throws Exception { testingClient.archive(sessionId, selection, 204); waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); - // verify that getStatus() as opposed to isPrepared() does not - // implicitly trigger a restore. + // verify that getStatus() as opposed to isPrepared() does not implicitly trigger a restore. waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); } @@ -76,8 +72,7 @@ public void getStatusPreparedIdTest() throws Exception { @Test public void restoringDatafileRestoresItsDatasetTest() throws Exception { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java index e6fa086c..c7daebea 100644 --- a/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/GetStatusForPreparedIdTest.java @@ -33,20 +33,22 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafiles( - Arrays.asList(1L, 2L, 3L, 99999L)), Flag.NONE, 404); + testingClient + .prepareData(sessionId, + new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), + Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, - new DataSelection().addDatafile(99999L), Flag.NONE, 404); + testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, + 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, - new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient + .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) diff --git a/src/test/java/org/icatproject/ids/integration/two/MiscTest.java b/src/test/java/org/icatproject/ids/integration/two/MiscTest.java index e7707492..fa5751d8 100644 --- a/src/test/java/org/icatproject/ids/integration/two/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/MiscTest.java @@ -1,10 +1,9 @@ package org.icatproject.ids.integration.two; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.io.InputStream; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -41,23 +40,19 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); - assertFalse(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertFalse(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); diff --git a/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java index b7cf5608..891da989 100644 --- a/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/PrepareDataTest.java @@ -1,12 +1,11 @@ package org.icatproject.ids.integration.two; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.nio.file.Path; import java.util.List; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -29,9 +28,8 @@ public static void setup() throws Exception { public void prepareArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -51,10 +49,8 @@ public void prepareTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -74,9 +70,8 @@ public void prepareTwoArchivedDatasets() throws Exception { public void prepareArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -93,11 +88,8 @@ public void prepareArchivedDatafile() throws Exception { public void prepareArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient - .prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -113,29 +105,26 @@ public void prepareArchivedDatafileAndItsDataset() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 400); } @Test public void noIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, - 200); + testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, 200); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 403); + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); assertNotNull(preparedId); } @@ -143,16 +132,14 @@ public void correctBehaviourTest() throws Exception { public void prepareRestoredDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -168,19 +155,16 @@ public void prepareTwoRestoredDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), - 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage1); checkPresent(dirOnFastStorage2); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -195,16 +179,14 @@ public void prepareRestoredDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -219,16 +201,13 @@ public void prepareRestoredDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection() - .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), - 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/two/PutTest.java b/src/test/java/org/icatproject/ids/integration/two/PutTest.java index 23f44369..3ca3f8dd 100644 --- a/src/test/java/org/icatproject/ids/integration/two/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/PutTest.java @@ -1,21 +1,19 @@ package org.icatproject.ids.integration.two; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - import java.nio.file.Files; import java.nio.file.Path; import java.util.Date; import java.util.GregorianCalendar; import javax.xml.datatype.DatatypeFactory; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataNotOnlineException; @@ -34,9 +32,8 @@ public static void setup() throws Exception { @Test(expected = DataNotOnlineException.class) public void putToUnrestoredDataset() throws Exception { - testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file1_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), null, 503); + testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file1_" + + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), null, 503); } @Test @@ -44,18 +41,15 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); waitForIds(); @@ -65,11 +59,9 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileCreateTime()); assertNull(df.getDatafileModTime()); - dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "An even better datafile", - "7.1.3", new Date(420000), new Date(42000), 201); + dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -87,8 +79,7 @@ public void putOneFileTest() throws Exception { assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java b/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java index 89a868fa..5122232e 100644 --- a/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/RestoreErrorsTest.java @@ -5,9 +5,6 @@ * in archive storage having unexpected content. */ -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -15,6 +12,8 @@ import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import org.junit.BeforeClass; import org.junit.Test; @@ -33,9 +32,9 @@ public static void setup() throws Exception { } /* - * Note that we cannot test for DUPLICATE_ENTRY here, because - * ZipOutputStream() won't allow us to create such a defective ZIP file. But - * that doesn't mean that this error cannot occur. + * Note that we cannot test for DUPLICATE_ENTRY here, because ZipOutputStream() won't allow + * us to create such a defective ZIP file. But that doesn't mean that this error cannot + * occur. */ private enum Defect { NONE, MISSING_ENTRY, SPURIOUS_ENTRY, DUPLICATE_ENTRY @@ -44,10 +43,8 @@ private enum Defect { private void cloneZip(Path archivepath, Defect defect) throws IOException { Path savepath = archivepath.getParent().resolve(".sav"); Files.move(archivepath, savepath); - try (ZipOutputStream zipout = new ZipOutputStream( - Files.newOutputStream(archivepath))) { - try (ZipInputStream zipin = new ZipInputStream( - Files.newInputStream(savepath))) { + try (ZipOutputStream zipout = new ZipOutputStream(Files.newOutputStream(archivepath))) { + try (ZipInputStream zipin = new ZipInputStream(Files.newInputStream(savepath))) { ZipEntry entry = zipin.getNextEntry(); boolean first = true; String entryName = ""; diff --git a/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java b/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java index 0efa59fb..2755fc9e 100644 --- a/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/RestoreTest.java @@ -22,8 +22,7 @@ public void restoreArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); @@ -35,9 +34,8 @@ public void restoreArchivedDataset() throws Exception { public void restoreTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), - 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage1); @@ -49,8 +47,7 @@ public void restoreArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); @@ -62,9 +59,8 @@ public void restoreArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection() - .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), - 204); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/two/WriteTest.java b/src/test/java/org/icatproject/ids/integration/two/WriteTest.java index 0da7c9f8..c9367a43 100644 --- a/src/test/java/org/icatproject/ids/integration/two/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/two/WriteTest.java @@ -12,7 +12,6 @@ import org.icatproject.Dataset; import org.icatproject.DatasetType; import org.icatproject.Investigation; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -29,8 +28,8 @@ public static void setup() throws Exception { } /** - * In principle, it's always possible to do a write call on existing - * datasets, but it will have no visible effect. + * In principle, it's always possible to do a write call on + * existing datasets, but it will have no visible effect. */ @Test public void restoreThenWriteDataset() throws Exception { @@ -48,19 +47,18 @@ public void restoreThenWriteDataset() throws Exception { } /** - * Create a dataset in ICAT, store the files in main storage, then do a - * write call to IDS to get the dataset written to archive storage. + * Create a dataset in ICAT, store the files in main storage, + * then do a write call to IDS to get the dataset written to + * archive storage. */ @Test public void storeThenWrite() throws Exception { long timestamp = System.currentTimeMillis(); - Investigation inv = (Investigation) icatWS.get(sessionId, - "Investigation INCLUDE Facility", investigationId); + Investigation inv = (Investigation) icatWS.get(sessionId, "Investigation INCLUDE Facility", investigationId); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS - .search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); Dataset ds = new Dataset(); ds.setName("dsWrite_" + timestamp); @@ -74,8 +72,7 @@ public void storeThenWrite() throws Exception { df.setName("dfWrite_" + timestamp); df.setLocation(dsLoc + UUID.randomUUID()); df.setDataset(ds); - writeToFile(df, "some really boring datafile test content", - setup.getKey()); + writeToFile(df, "some really boring datafile test content", setup.getKey()); Long dsId = ds.getId(); Path dirOnFastStorage = getDirOnFastStorage(dsId); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java b/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java index ab073d89..75557414 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/ArchiveTest.java @@ -1,12 +1,11 @@ package org.icatproject.ids.integration.twodf; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -31,15 +30,13 @@ public void restoreThenArchiveDataset() throws Exception { assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertFalse(Files.exists(dirOnFastStorage)); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java b/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java index 17f56ce2..670a071a 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/BogusDatafileTest.java @@ -1,9 +1,5 @@ package org.icatproject.ids.integration.twodf; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.io.File; import java.io.InputStream; import java.nio.file.Files; @@ -12,13 +8,15 @@ import java.util.HashSet; import java.util.Set; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; import org.icatproject.Dataset; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; @@ -49,24 +47,21 @@ public static void setup() throws Exception { @Before public void createBogusFiles() throws Exception { - Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(0)); + Dataset ds1 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(0)); Datafile dfb1 = new Datafile(); dfb1.setName("dfbogus1_" + timestamp); dfb1.setFileSize(42L); dfb1.setDataset(ds1); dfb1.setId(icatWS.create(sessionId, dfb1)); - Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(1)); + Dataset ds2 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(1)); Datafile dfb2 = new Datafile(); dfb2.setName("dfbogus2_" + timestamp); dfb2.setFileSize(42L); dfb2.setDataset(ds2); dfb2.setId(icatWS.create(sessionId, dfb2)); - Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", - datasetIds.get(2)); + Dataset ds3 = (Dataset) icatWS.get(sessionId, "Dataset", datasetIds.get(2)); Datafile dfb3 = new Datafile(); dfb3.setName("dfbogus3_" + timestamp); dfb3.setFileSize(42L); @@ -82,11 +77,9 @@ public void createBogusFiles() throws Exception { @Test public void getEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -95,8 +88,7 @@ public void getEmptyDataset() throws Exception { @Test public void getSizeEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(2)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(2)); assertEquals(0L, testingClient.getSize(sessionId, selection, 200)); } @@ -104,17 +96,14 @@ public void getSizeEmptyDataset() throws Exception { @Test public void getNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); testingClient.restore(sessionId, selection, 204); waitForIds(); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, selection, null)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, null)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, null)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } @@ -123,8 +112,7 @@ public void getNonEmptyDataset() throws Exception { @Test public void getSizeNonEmptyDataset() throws Exception { - DataSelection selection = new DataSelection() - .addDataset(datasetIds.get(0)); + DataSelection selection = new DataSelection().addDataset(datasetIds.get(0)); assertEquals(104L, testingClient.getSize(sessionId, selection, 200)); } @@ -132,17 +120,14 @@ public void getSizeNonEmptyDataset() throws Exception { @Test(expected = NotFoundException.class) public void getBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.restore(sessionId, selection, 404); waitForIds(); - assertEquals(Status.ONLINE, - testingClient.getStatus(sessionId, selection, 404)); + assertEquals(Status.ONLINE, testingClient.getStatus(sessionId, selection, 404)); - try (InputStream stream = testingClient.getData(sessionId, selection, - Flag.NONE, 0, 404)) { + try (InputStream stream = testingClient.getData(sessionId, selection, Flag.NONE, 0, 404)) { checkZipStream(stream, Collections.emptyList(), 57, 0); } @@ -151,17 +136,15 @@ public void getBogusFile() throws Exception { @Test(expected = NotFoundException.class) public void getSizeBogusFile() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(5)); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(5)); testingClient.getSize(sessionId, selection, 404); } /* - * Try the full cycle: upload a new file into a dataset having a bogus file, - * which triggers a write of the datafile to archive storage, archive the - * dataset, and restore it. Each step must deal gracefully with the bogus - * file in the dataset. + * Try the full cycle: upload a new file into a dataset having a bogus file, which triggers a write of the + * datafile to archive storage, archive the dataset, and restore it. Each step must deal gracefully with + * the bogus file in the dataset. */ @Test public void putWriteArchiveRestore() throws Exception { @@ -174,11 +157,9 @@ public void putWriteArchiveRestore() throws Exception { waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfulId = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + Long dfulId = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); Datafile dful = (Datafile) icatWS.get(sessionId, "Datafile", dfulId); testingClient.archive(sessionId, selection, 204); waitForIds(); @@ -191,16 +172,13 @@ public void putWriteArchiveRestore() throws Exception { File[] filesList = dirOnFastStorage.toFile().listFiles(); assertEquals(3, filesList.length); Set locations = new HashSet<>(); - Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", - datafileIds.get(0)); + Datafile df1 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(0)); locations.add(getLocationFromDigest(df1.getId(), df1.getLocation())); - Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", - datafileIds.get(1)); + Datafile df2 = (Datafile) icatWS.get(sessionId, "Datafile", datafileIds.get(1)); locations.add(getLocationFromDigest(df2.getId(), df2.getLocation())); locations.add(getLocationFromDigest(dful.getId(), dful.getLocation())); for (File file : filesList) { - String location = setup.getStorageDir().relativize(file.toPath()) - .toString(); + String location = setup.getStorageDir().relativize(file.toPath()).toString(); assertTrue(locations.contains(location)); } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java b/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java index e92431e7..063102fb 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/DeleteTest.java @@ -19,8 +19,7 @@ public static void setup() throws Exception { @Test public void deleteFromUnrestoredDataset() throws Exception { - testingClient.delete(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); } @Test @@ -28,13 +27,11 @@ public void deleteDatafileFromRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); - testingClient.delete(sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), 204); + testingClient.delete(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), 204); waitForIds(); } @@ -43,14 +40,12 @@ public void deleteRestoredDatasetTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(1)); Path fileOnArchiveStorage = getFileOnArchiveStorage(datasetIds.get(1)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage); checkPresent(fileOnArchiveStorage); - testingClient.delete(sessionId, - new DataSelection().addDataset(datasetIds.get(1)), 204); + testingClient.delete(sessionId, new DataSelection().addDataset(datasetIds.get(1)), 204); waitForIds(); checkAbsent(dirOnFastStorage); checkAbsent(fileOnArchiveStorage); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java index e5734be0..b6d9e772 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetDataExplicitTest.java @@ -1,15 +1,13 @@ package org.icatproject.ids.integration.twodf; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - import java.io.InputStream; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -33,17 +31,14 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, - 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { } } @Test(expected = InsufficientPrivilegesException.class) public void forbiddenTest() throws Exception { - try (InputStream z = testingClient.getData( - setup.getForbiddenSessionId(), - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - 403)) { + try (InputStream z = testingClient.getData(setup.getForbiddenSessionId(), + new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, 403)) { } } @@ -52,13 +47,11 @@ public void getSizes() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", - datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - assertEquals(209L, testingClient.getSize(sessionId, - new DataSelection().addDatafiles(datafileIds), 200)); + assertEquals(209L, testingClient.getSize(sessionId, new DataSelection().addDatafiles(datafileIds), 200)); } finally { if (df != null) { df.setFileSize(size); @@ -72,15 +65,12 @@ public void getSizePreparedId() throws Exception { Datafile df = null; Long size = 0L; try { - df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", - datafileIds.get(0)); + df = (Datafile) icatWS.get(sessionId, "Datafile INCLUDE 1", datafileIds.get(0)); size = df.getFileSize(); df.setFileSize(size + 1); icatWS.update(sessionId, df); - DataSelection selection = new DataSelection() - .addDatafiles(datafileIds); - String preparedId = testingClient.prepareData(sessionId, selection, - Flag.NONE, 200); + DataSelection selection = new DataSelection().addDatafiles(datafileIds); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); assertEquals(209L, testingClient.getSize(preparedId, 200)); } finally { if (df != null) { @@ -93,9 +83,8 @@ public void getSizePreparedId() throws Exception { @Test public void correctBehaviourTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - 503)) { + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), Flag.NONE, + 0, 503)) { fail("Should have thrown exception"); } catch (IdsException e) { @@ -103,9 +92,8 @@ public void correctBehaviourTest() throws Exception { } while (true) { - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 0, - null)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 0, null)) { checkZipStream(stream, datafileIds, 57, 0); break; } catch (IdsException e) { @@ -118,18 +106,16 @@ public void correctBehaviourTest() throws Exception { @Test public void gettingDatafileDoesNotRestoreItsDatasetTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(2)), Flag.NONE, - 0, 503)) { + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)), + Flag.NONE, 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(3)), Flag.NONE, - 0, 503)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(3)), + Flag.NONE, 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -140,9 +126,8 @@ public void gettingDatafileDoesNotRestoreItsDatasetTest() throws Exception { @Test public void gettingDatasetUsesCacheTest() throws Exception { - try (InputStream z = testingClient.getData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, - 503)) { + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well @@ -150,32 +135,26 @@ public void gettingDatasetUsesCacheTest() throws Exception { waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 0, - 200)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 0, 200)) { checkZipStream(stream, datafileIds.subList(0, 2), 57, 0); } } @Test - public void gettingDatafileAndDatasetShouldNotRestoreBothDatasetsTest() - throws Exception { - - try (InputStream z = testingClient - .getData(sessionId, - new DataSelection().addDatafile(datafileIds.get(2)) - .addDataset(datasetIds.get(0)), - Flag.NONE, 0, 503)) { + public void gettingDatafileAndDatasetShouldNotRestoreBothDatasetsTest() throws Exception { + + try (InputStream z = testingClient.getData(sessionId, new DataSelection().addDatafile(datafileIds.get(2)) + .addDataset(datasetIds.get(0)), Flag.NONE, 0, 503)) { fail("Should throw exception"); } catch (DataNotOnlineException e) { // All is well } waitForIds(); - try (InputStream stream = testingClient.getData(sessionId, - new DataSelection().addDatasets(datasetIds), Flag.NONE, 0, - 503)) { + try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatasets(datasetIds), + Flag.NONE, 0, 503)) { fail("Should have thrown an exception"); } catch (DataNotOnlineException e) { // All is well diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java index 14cc376c..c0fa6eef 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetDataForPreparedIdTest.java @@ -1,9 +1,8 @@ package org.icatproject.ids.integration.twodf; -import static org.junit.Assert.assertEquals; - import java.io.InputStream; +import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; @@ -26,17 +25,15 @@ public static void setup() throws Exception { @Test(expected = BadRequestException.class) public void badPreparedIdFormatTest() throws Exception { - try (InputStream z = testingClient.getData("bad preparedId format", 0L, - 400)) { + try (InputStream z = testingClient.getData("bad preparedId format", 0L, 400)) { } } @Test(expected = BadRequestException.class) public void badOffsetFormatTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -48,16 +45,14 @@ public void badOffsetFormatTest() throws Exception { @Test(expected = NotFoundException.class) public void nonExistentPreparedIdTest() throws Exception { - try (InputStream z = testingClient - .getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { + try (InputStream z = testingClient.getData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", 0L, 404)) { } } @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -69,10 +64,9 @@ public void correctBehaviourNoOffsetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetMultipleDatafilesTest() - throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + public void correctBehaviourNoOffsetMultipleDatafilesTest() throws Exception { + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -87,9 +81,8 @@ public void correctBehaviourNoOffsetMultipleDatafilesTest() @Test public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -101,13 +94,10 @@ public void correctBehaviourNoOffsetWithDatasetTest() throws Exception { } @Test - public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() - throws Exception { + public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafiles(datafileIds), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDatafiles(datafileIds), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -121,9 +111,8 @@ public void correctBehaviourNoOffsetWithDatasetAndDatafileTest() @Test public void correctBehaviourWithOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -131,14 +120,12 @@ public void correctBehaviourWithOffsetTest() throws Exception { // request the zip file twice, with and without an offset byte[] zip = getOutput(testingClient.getData(preparedId, 0, 200)); - byte[] zipoffset = getOutput( - testingClient.getData(preparedId, goodOffset, 206)); + byte[] zipoffset = getOutput(testingClient.getData(preparedId, goodOffset, 206)); // compare the two zip files byte by byte taking into account the offset System.out.println(zip.length + " " + zipoffset.length); for (int i = 0; i < zipoffset.length; i++) { - assertEquals("Byte offset: " + i, (byte) zipoffset[i], - (byte) zip[i + goodOffset]); + assertEquals("Byte offset: " + i, (byte) zipoffset[i], (byte) zip[i + goodOffset]); } } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java index d8893903..ed6bed60 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusExplicitTest.java @@ -1,9 +1,8 @@ package org.icatproject.ids.integration.twodf; -import static org.junit.Assert.assertEquals; - import java.util.Arrays; +import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; @@ -35,8 +34,8 @@ public void notFoundPreparedId() throws Exception { @Test(expected = NotFoundException.class) public void notFoundDatafileIdsTest() throws Exception { - testingClient.getStatus(sessionId, new DataSelection() - .addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); + testingClient.getStatus(sessionId, + new DataSelection().addDatasets(Arrays.asList(1L, 2L, 3L, 9999999L)), 404); } @Test(expected = InsufficientPrivilegesException.class) @@ -56,10 +55,8 @@ public void correctBehaviourTest() throws Exception { @Test public void getStatusPreparedIdTest() throws Exception { - DataSelection selection = new DataSelection() - .addDatafile(datafileIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, selection, - Flag.NONE, 200); + DataSelection selection = new DataSelection().addDatafile(datafileIds.get(0)); + String preparedId = testingClient.prepareData(sessionId, selection, Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); } @@ -67,18 +64,15 @@ public void getStatusPreparedIdTest() throws Exception { testingClient.archive(sessionId, selection, 204); waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); - // verify that getStatus() as opposed to isPrepared() does not - // implicitly trigger a restore. + // verify that getStatus() as opposed to isPrepared() does not implicitly trigger a restore. waitForIds(); assertEquals(testingClient.getStatus(preparedId, 200), Status.ARCHIVED); } @Test - public void restoringDatafileDoesNotRestoresItsDatasetTest() - throws Exception { + public void restoringDatafileDoesNotRestoresItsDatasetTest() throws Exception { String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java index 6f7c04ae..7889492e 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/GetStatusForPreparedIdTest.java @@ -33,20 +33,22 @@ public void nonExistingPreparedIdTest() throws Exception { @Test(expected = NotFoundException.class) public void notFoundIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection().addDatafiles( - Arrays.asList(1L, 2L, 3L, 99999L)), Flag.NONE, 404); + testingClient + .prepareData(sessionId, + new DataSelection().addDatafiles(Arrays.asList(1L, 2L, 3L, 99999L)), + Flag.NONE, 404); } @Test(expected = NotFoundException.class) public void notFoundSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, - new DataSelection().addDatafile(99999L), Flag.NONE, 404); + testingClient.prepareData(sessionId, new DataSelection().addDatafile(99999L), Flag.NONE, + 404); } @Test(expected = NotFoundException.class) public void notFoundDatasetSingleIdTest() throws Exception { - testingClient.prepareData(sessionId, - new DataSelection().addDataset(99999L), Flag.NONE, 404); + testingClient + .prepareData(sessionId, new DataSelection().addDataset(99999L), Flag.NONE, 404); } @Test(expected = InsufficientPrivilegesException.class) diff --git a/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java b/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java index de551964..011de0ad 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/MiscTest.java @@ -1,10 +1,9 @@ package org.icatproject.ids.integration.twodf; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.io.InputStream; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -41,13 +40,11 @@ public void unprivSessionIdTest() throws Exception { @Test public void correctBehaviourNoOffsetTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); var status = testingClient.getServiceStatus(sessionId, 200); - // System.out.println("### twodf.MiscTest.correctBehaviourNoOffsetTest - - // status: " + status.toString()); + //System.out.println("### twodf.MiscTest.correctBehaviourNoOffsetTest - status: " + status.toString()); assertFalse(status.getOpItems().isEmpty()); @@ -55,12 +52,10 @@ public void correctBehaviourNoOffsetTest() throws Exception { Thread.sleep(1000); } - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); waitForIds(); - assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems() - .isEmpty()); + assertTrue(testingClient.getServiceStatus(sessionId, 200).getOpItems().isEmpty()); try (InputStream stream = testingClient.getData(preparedId, 0, 200)) { checkStream(stream, datafileIds.get(0)); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java b/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java index b339647d..386a7fff 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/PrepareDataTest.java @@ -1,12 +1,11 @@ package org.icatproject.ids.integration.twodf; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.nio.file.Path; import java.util.List; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -29,9 +28,8 @@ public static void setup() throws Exception { public void prepareArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -51,10 +49,8 @@ public void prepareTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(4, ids.size()); @@ -74,9 +70,8 @@ public void prepareTwoArchivedDatasets() throws Exception { public void prepareArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(1, ids.size()); @@ -93,11 +88,8 @@ public void prepareArchivedDatafile() throws Exception { public void prepareArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - String preparedId = testingClient - .prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDatafile(datafileIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDatafile(datafileIds.get(0)), Flag.NONE, 200); List ids = testingClient.getDatafileIds(preparedId, 200); assertEquals(2, ids.size()); @@ -113,29 +105,26 @@ public void prepareArchivedDatafileAndItsDataset() throws Exception { @Test(expected = BadRequestException.class) public void badSessionIdFormatTest() throws Exception { - testingClient.prepareData("bad sessionId format", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, + testingClient.prepareData("bad sessionId format", new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 400); } @Test public void noIdsTest() throws Exception { - testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, - 200); + testingClient.prepareData(sessionId, new DataSelection(), Flag.NONE, 200); } @Test(expected = InsufficientPrivilegesException.class) public void nonExistingSessionIdTest() throws Exception { testingClient.prepareData("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 403); + new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, 403); } @Test public void correctBehaviourTest() throws Exception { - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafiles(datafileIds), Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafiles(datafileIds), + Flag.NONE, 200); assertNotNull(preparedId); } @@ -143,16 +132,14 @@ public void correctBehaviourTest() throws Exception { public void prepareRestoredDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -168,19 +155,16 @@ public void prepareTwoRestoredDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), - 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage1); checkPresent(dirOnFastStorage2); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDataset(datasetIds.get(0)) - .addDataset(datasetIds.get(1)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDataset(datasetIds.get(0)) + .addDataset(datasetIds.get(1)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -195,16 +179,14 @@ public void prepareRestoredDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), Flag.NONE, - 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), + Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); @@ -219,16 +201,13 @@ public void prepareRestoredDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection() - .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), - 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); - String preparedId = testingClient.prepareData(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)) - .addDataset(datasetIds.get(0)), - Flag.NONE, 200); + String preparedId = testingClient.prepareData(sessionId, new DataSelection().addDatafile(datafileIds.get(0)) + .addDataset(datasetIds.get(0)), Flag.NONE, 200); while (!testingClient.isPrepared(preparedId, 200)) { Thread.sleep(1000); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java b/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java index 4854589f..b960cd6e 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/PutTest.java @@ -1,21 +1,19 @@ package org.icatproject.ids.integration.twodf; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - import java.nio.file.Files; import java.nio.file.Path; import java.util.Date; import java.util.GregorianCalendar; import javax.xml.datatype.DatatypeFactory; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; import org.icatproject.Datafile; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.DataSelection; @@ -33,9 +31,8 @@ public static void setup() throws Exception { @Test // Works fine for datafile storage public void putToUnrestoredDataset() throws Exception { - testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file1_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), null, 201); + testingClient.put(sessionId, Files.newInputStream(newFileLocation), "uploaded_file1_" + + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), null, 201); } @Test @@ -43,18 +40,15 @@ public void putOneFileTest() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); assertFalse(Files.exists(dirOnFastStorage)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); assertTrue(Files.exists(dirOnFastStorage)); - Long dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + Long dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); waitForIds(); @@ -64,11 +58,9 @@ public void putOneFileTest() throws Exception { assertNull(df.getDatafileCreateTime()); assertNull(df.getDatafileModTime()); - dfid = testingClient.put(sessionId, - Files.newInputStream(newFileLocation), - "uploaded_file3_" + timestamp, datasetIds.get(0), - supportedDatafileFormat.getId(), "An even better datafile", - "7.1.3", new Date(420000), new Date(42000), 201); + dfid = testingClient.put(sessionId, Files.newInputStream(newFileLocation), + "uploaded_file3_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), + "An even better datafile", "7.1.3", new Date(420000), new Date(42000), 201); df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); assertEquals("An even better datafile", df.getDescription()); assertEquals("7.1.3", df.getDoi()); @@ -86,8 +78,7 @@ public void putOneFileTest() throws Exception { assertTrue(Files.exists(dirOnFastStorage)); - testingClient.archive(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.archive(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java b/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java index 1e6b34e1..baf7be8d 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/QueueTest.java @@ -1,10 +1,9 @@ package org.icatproject.ids.integration.twodf; -import static org.junit.Assert.assertTrue; - import java.nio.file.Files; import java.nio.file.Path; +import static org.junit.Assert.assertTrue; import org.junit.BeforeClass; import org.junit.Test; @@ -23,8 +22,9 @@ public static void setup() throws Exception { } /* - * Arrange for multiple different operations, requiring conflicting locks on - * the same dataset to be processed at the same time. This triggers Bug #82. + * Arrange for multiple different operations, requiring conflicting + * locks on the same dataset to be processed at the same time. This + * triggers Bug #82. */ @Test public void multiOperationTest() throws Exception { @@ -37,9 +37,8 @@ public void multiOperationTest() throws Exception { assertTrue(Files.exists(dirOnFastStorage)); testingClient.put(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file_" + timestamp, dsId, - supportedDatafileFormat.getId(), "A rather splendid datafile", - 201); + "uploaded_file_" + timestamp, dsId, supportedDatafileFormat.getId(), + "A rather splendid datafile", 201); testingClient.archive(sessionId, selection, 204); waitForIds(); } diff --git a/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java b/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java index 4f792af5..7125b9a4 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/RestoreTest.java @@ -20,8 +20,7 @@ public static void setup() throws Exception { @Test public void restoreArchivedDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDataset(datasetIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDataset(datasetIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); } @@ -30,9 +29,8 @@ public void restoreArchivedDataset() throws Exception { public void restoreTwoArchivedDatasets() throws Exception { Path dirOnFastStorage1 = getDirOnFastStorage(datasetIds.get(0)); Path dirOnFastStorage2 = getDirOnFastStorage(datasetIds.get(1)); - testingClient.restore(sessionId, new DataSelection() - .addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), - 204); + testingClient.restore(sessionId, + new DataSelection().addDataset(datasetIds.get(0)).addDataset(datasetIds.get(1)), 204); waitForIds(); checkPresent(dirOnFastStorage1); @@ -44,8 +42,7 @@ public void restoreArchivedDatafile() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, - new DataSelection().addDatafile(datafileIds.get(0)), 204); + testingClient.restore(sessionId, new DataSelection().addDatafile(datafileIds.get(0)), 204); waitForIds(); checkPresent(dirOnFastStorage); @@ -57,9 +54,8 @@ public void restoreArchivedDatafileAndItsDataset() throws Exception { Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - testingClient.restore(sessionId, new DataSelection() - .addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), - 204); + testingClient.restore(sessionId, + new DataSelection().addDatafile(datafileIds.get(0)).addDataset(datasetIds.get(0)), 204); waitForIds(); diff --git a/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java b/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java index 9b66996d..876af417 100644 --- a/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java +++ b/src/test/java/org/icatproject/ids/integration/twodf/WriteTest.java @@ -11,7 +11,6 @@ import org.icatproject.Dataset; import org.icatproject.DatasetType; import org.icatproject.Investigation; - import org.icatproject.ids.integration.BaseTest; import org.icatproject.ids.integration.util.Setup; import org.icatproject.ids.integration.util.client.BadRequestException; @@ -28,8 +27,8 @@ public static void setup() throws Exception { } /** - * In principle, it's always possible to do a write call on existing - * datasets, but it will have no visible effect. + * In principle, it's always possible to do a write call on + * existing datasets, but it will have no visible effect. */ @Test public void restoreThenWriteDataset() throws Exception { @@ -47,19 +46,18 @@ public void restoreThenWriteDataset() throws Exception { } /** - * Create a dataset in ICAT, store the files in main storage, then do a - * write call to IDS to get the dataset written to archive storage. + * Create a dataset in ICAT, store the files in main storage, + * then do a write call to IDS to get the dataset written to + * archive storage. */ @Test public void storeThenWrite() throws Exception { long timestamp = System.currentTimeMillis(); - Investigation inv = (Investigation) icatWS.get(sessionId, - "Investigation INCLUDE Facility", investigationId); + Investigation inv = (Investigation) icatWS.get(sessionId, "Investigation INCLUDE Facility", investigationId); String invLoc = inv.getId() + "/"; - DatasetType dsType = (DatasetType) icatWS - .search(sessionId, "DatasetType").get(0); + DatasetType dsType = (DatasetType) icatWS.search(sessionId, "DatasetType").get(0); Dataset ds = new Dataset(); ds.setName("dsWrite_" + timestamp); @@ -73,8 +71,7 @@ public void storeThenWrite() throws Exception { df.setName("dfWrite_" + timestamp); df.setLocation(dsLoc + UUID.randomUUID()); df.setDataset(ds); - writeToFile(df, "some really boring datafile test content", - setup.getKey()); + writeToFile(df, "some really boring datafile test content", setup.getKey()); Long dsId = ds.getId(); Path dirOnFastStorage = getDirOnFastStorage(dsId); diff --git a/src/test/java/org/icatproject/ids/integration/util/Setup.java b/src/test/java/org/icatproject/ids/integration/util/Setup.java index 8273355b..2392805d 100644 --- a/src/test/java/org/icatproject/ids/integration/util/Setup.java +++ b/src/test/java/org/icatproject/ids/integration/util/Setup.java @@ -10,11 +10,10 @@ import java.util.Properties; import org.icatproject.ICAT; -import org.icatproject.utils.CheckedProperties; -import org.icatproject.utils.ShellCommand; - import org.icatproject.ids.TestUtils; import org.icatproject.ids.services.ICATGetter; +import org.icatproject.utils.CheckedProperties; +import org.icatproject.utils.ShellCommand; /* * Setup the test environment for the IDS. This is done by reading property @@ -58,13 +57,11 @@ public Setup(String runPropertyFile) throws Exception { // Start by reading the test properties Properties testProps = new Properties(); - InputStream is = Setup.class.getClassLoader() - .getResourceAsStream("test.properties"); + InputStream is = Setup.class.getClassLoader().getResourceAsStream("test.properties"); try { testProps.load(is); } catch (Exception e) { - System.err.println("Problem loading test.properties: " - + e.getClass() + " " + e.getMessage()); + System.err.println("Problem loading test.properties: " + e.getClass() + " " + e.getMessage()); } setReliability(1.); @@ -75,18 +72,16 @@ public Setup(String runPropertyFile) throws Exception { String containerHome = System.getProperty("containerHome"); if (containerHome == null) { - System.err.println( - "containerHome is not defined as a system property"); + System.err.println("containerHome is not defined as a system property"); } long time = System.currentTimeMillis(); - ShellCommand sc = new ShellCommand("src/test/scripts/prepare_test.py", - "src/test/resources/" + runPropertyFile, home.toString(), - containerHome, serverUrl); + ShellCommand sc = new ShellCommand("src/test/scripts/prepare_test.py", "src/test/resources/" + runPropertyFile, + home.toString(), containerHome, serverUrl); System.out.println(sc.getStdout() + " " + sc.getStderr()); - System.out.println("Setting up " + runPropertyFile + " took " - + (System.currentTimeMillis() - time) / 1000. + "seconds"); + System.out.println( + "Setting up " + runPropertyFile + " took " + (System.currentTimeMillis() - time) / 1000. + "seconds"); // Having set up the ids.properties file read it find other things CheckedProperties runProperties = new CheckedProperties(); @@ -97,10 +92,8 @@ public Setup(String runPropertyFile) throws Exception { updownDir = home.resolve(testProps.getProperty("updownDir")); icatUrl = runProperties.getURL("icat.url"); ICAT icat = ICATGetter.getService(icatUrl.toString()); - rootSessionId = TestUtils.login(icat, - testProps.getProperty("login.root")); - forbiddenSessionId = TestUtils.login(icat, - testProps.getProperty("login.unauthorized")); + rootSessionId = TestUtils.login(icat, testProps.getProperty("login.root")); + forbiddenSessionId = TestUtils.login(icat, testProps.getProperty("login.unauthorized")); storageDir = runProperties.getPath("plugin.main.dir"); diff --git a/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java b/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java index 577d29f5..6527c191 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/HttpInputStream.java @@ -17,8 +17,7 @@ public class HttpInputStream extends FilterInputStream { * @param httpclient the CloseableHttpClient to close * @param response the response to use and ultimately close */ - public HttpInputStream(CloseableHttpClient httpclient, - CloseableHttpResponse response) + public HttpInputStream(CloseableHttpClient httpclient, CloseableHttpResponse response) throws IllegalStateException, IOException { super(response.getEntity().getContent()); this.response = response; diff --git a/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java b/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java index 89f4d391..7bb22543 100644 --- a/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java +++ b/src/test/java/org/icatproject/ids/integration/util/client/TestingClient.java @@ -1,7 +1,5 @@ package org.icatproject.ids.integration.util.client; -import static org.junit.Assert.fail; - import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; @@ -48,9 +46,10 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; - import org.icatproject.ids.enums.RequestIdNames; +import static org.junit.Assert.fail; + public class TestingClient { public enum Flag { @@ -108,14 +107,11 @@ void storeFailure(Long id) { public String toString() { String result = "lockCount: " + this.lockCount + ", "; result += "lockedIDs: " + lockedDs.size() + "\n"; - for (long lockedId : lockedDs) - result += "\t" + lockedId + "\n"; + for(long lockedId : lockedDs) result += "\t" + lockedId + "\n"; result += "opItems: " + opItems.size() + "\n"; - for (String key : opItems.keySet()) - result += "\t" + key + ": " + opItems.get(key) + "\n"; + for(String key : opItems.keySet()) result += "\t" + key + ": " + opItems.get(key) + "\n"; result += "failures: " + failures.size() + "\n"; - for (long fail : failures) - result += " " + fail; + for(long fail : failures) result += " " + fail; result += "\n"; return result; @@ -133,8 +129,7 @@ public enum Status { public TestingClient(URL idsUrl) { try { - idsUri = new URI(idsUrl.getProtocol(), null, idsUrl.getHost(), - idsUrl.getPort(), null, null, null); + idsUri = new URI(idsUrl.getProtocol(), null, idsUrl.getHost(), idsUrl.getPort(), null, null, null); basePath = idsUrl.getPath(); } catch (URISyntaxException e) { throw new RuntimeException(e); @@ -142,28 +137,22 @@ public TestingClient(URL idsUrl) { } - public void archive(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, - NotFoundException { + public void archive(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, + BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { URI uri = getUri(getUriBuilder("archive")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(formparams)); - try (CloseableHttpResponse response = httpclient - .execute(httpPost)) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -171,10 +160,8 @@ public void archive(String sessionId, DataSelection data, Integer sc) } private void checkStatus(HttpResponse response, Integer sc) - throws InternalException, BadRequestException, - DataNotOnlineException, ParseException, IOException, - InsufficientPrivilegesException, NotImplementedException, - InsufficientStorageException, NotFoundException { + throws InternalException, BadRequestException, DataNotOnlineException, ParseException, IOException, + InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException { StatusLine status = response.getStatusLine(); if (status == null) { throw new InternalException("Status line returned is empty"); @@ -201,8 +188,7 @@ private void checkStatus(HttpResponse response, Integer sc) } String code; String message; - try (JsonReader jsonReader = Json - .createReader(new StringReader(error))) { + try (JsonReader jsonReader = Json.createReader(new StringReader(error))) { JsonObject json = jsonReader.readObject(); code = json.getString("code"); message = json.getString("message"); @@ -241,8 +227,7 @@ private void checkStatus(HttpResponse response, Integer sc) } public void delete(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("delete"); @@ -254,56 +239,47 @@ public void delete(String sessionId, DataSelection data, Integer sc) try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpDelete httpDelete = new HttpDelete(uri); - try (CloseableHttpResponse response = httpclient - .execute(httpDelete)) { + try (CloseableHttpResponse response = httpclient.execute(httpDelete)) { expectNothing(response, sc); } catch (InsufficientStorageException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - private void expectNothing(CloseableHttpResponse response, Integer sc) - throws InternalException, BadRequestException, - DataNotOnlineException, ParseException, - InsufficientPrivilegesException, NotImplementedException, - InsufficientStorageException, NotFoundException, IOException { + private void expectNothing(CloseableHttpResponse response, Integer sc) throws InternalException, + BadRequestException, DataNotOnlineException, ParseException, InsufficientPrivilegesException, + NotImplementedException, InsufficientStorageException, NotFoundException, IOException { checkStatus(response, sc); HttpEntity entity = response.getEntity(); if (entity != null) { if (!EntityUtils.toString(entity).isEmpty()) { - throw new InternalException( - "No http entity expected in response"); + throw new InternalException("No http entity expected in response"); } } } - public URL getIcatUrl(int sc) - throws InternalException, ParseException, NotImplementedException { + public URL getIcatUrl(int sc) throws InternalException, ParseException, NotImplementedException { URI uri = getUri(getUriBuilder("getIcatUrl")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return new URL(getString(response, sc)); - } catch (IOException | InsufficientStorageException - | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public InputStream getData(String sessionId, DataSelection data, Flag flags, - long offset, Integer sc) throws NotImplementedException, - BadRequestException, InsufficientPrivilegesException, - NotFoundException, InternalException, DataNotOnlineException { + public InputStream getData(String sessionId, DataSelection data, Flag flags, long offset, Integer sc) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, + InternalException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("getData"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -354,8 +330,7 @@ public InputStream getData(String sessionId, DataSelection data, Flag flags, } public InputStream getData(String preparedId, long offset, Integer sc) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException, DataNotOnlineException { URIBuilder uriBuilder = getUriBuilder("getData"); @@ -397,8 +372,7 @@ public InputStream getData(String preparedId, long offset, Integer sc) } public ServiceStatus getServiceStatus(String sessionId, Integer sc) - throws InternalException, InsufficientPrivilegesException, - NotImplementedException { + throws InternalException, InsufficientPrivilegesException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getServiceStatus"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -410,8 +384,7 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); String result = getString(response, sc); - try (JsonReader jsonReader = Json - .createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { ServiceStatus serviceStatus = new ServiceStatus(); JsonObject rootNode = jsonReader.readObject(); for (JsonValue on : rootNode.getJsonArray("opsQueue")) { @@ -421,8 +394,7 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) } serviceStatus.setLockedCount(rootNode.getInt("lockCount")); for (JsonValue lock : rootNode.getJsonArray("locks")) { - Long dsId = ((JsonObject) lock).getJsonNumber("id") - .longValueExact(); + Long dsId = ((JsonObject) lock).getJsonNumber("id").longValueExact(); serviceStatus.storeLockedDs(dsId); } for (JsonValue num : rootNode.getJsonArray("failures")) { @@ -431,25 +403,21 @@ public ServiceStatus getServiceStatus(String sessionId, Integer sc) } return serviceStatus; } catch (JsonException e) { - throw new InternalException("TestingClient " + e.getClass() - + " " + e.getMessage() + " from " + result); + throw new InternalException( + "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); } - } catch (InsufficientStorageException | DataNotOnlineException - | InternalException | BadRequestException - | NotFoundException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException | InternalException | BadRequestException + | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public long getSize(String sessionId, DataSelection data, int sc) - throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, - NotImplementedException { + public long getSize(String sessionId, DataSelection data, int sc) throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getSize"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); @@ -463,19 +431,16 @@ public long getSize(String sessionId, DataSelection data, int sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Long.parseLong(getString(response, sc)); - } catch (IOException | InsufficientStorageException - | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException | DataNotOnlineException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public long getSize(String preparedId, int sc) throws BadRequestException, - NotFoundException, InsufficientPrivilegesException, - InternalException, NotImplementedException { + public long getSize(String preparedId, int sc) throws BadRequestException, NotFoundException, + InsufficientPrivilegesException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getSize"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); @@ -485,20 +450,16 @@ public long getSize(String preparedId, int sc) throws BadRequestException, HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { return Long.parseLong(getString(response, sc)); - } catch (IOException | InsufficientStorageException - | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException | DataNotOnlineException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public Status getStatus(String sessionId, DataSelection data, Integer sc) - throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, - NotImplementedException { + public Status getStatus(String sessionId, DataSelection data, Integer sc) throws BadRequestException, + NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getStatus"); if (sessionId != null) { @@ -516,8 +477,7 @@ public Status getStatus(String sessionId, DataSelection data, Integer sc) checkResponseConformity(response); return Status.valueOf(getString(response, sc)); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -525,10 +485,8 @@ public Status getStatus(String sessionId, DataSelection data, Integer sc) } - public Status getStatus(String preparedId, Integer sc) - throws BadRequestException, NotFoundException, - InsufficientPrivilegesException, InternalException, - NotImplementedException { + public Status getStatus(String preparedId, Integer sc) throws BadRequestException, + NotFoundException, InsufficientPrivilegesException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("getStatus"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); @@ -540,8 +498,7 @@ public Status getStatus(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { return Status.valueOf(getString(response, sc)); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -549,10 +506,8 @@ public Status getStatus(String preparedId, Integer sc) } - private String getString(CloseableHttpResponse response, Integer sc) - throws InternalException, BadRequestException, - DataNotOnlineException, ParseException, - InsufficientPrivilegesException, NotImplementedException, + private String getString(CloseableHttpResponse response, Integer sc) throws InternalException, BadRequestException, + DataNotOnlineException, ParseException, InsufficientPrivilegesException, NotImplementedException, InsufficientStorageException, NotFoundException, IOException { checkStatus(response, sc); HttpEntity entity = response.getEntity(); @@ -575,8 +530,7 @@ private URIBuilder getUriBuilder(String path) { } public boolean isPrepared(String preparedId, Integer sc) - throws BadRequestException, NotFoundException, InternalException, - NotImplementedException { + throws BadRequestException, NotFoundException, InternalException, NotImplementedException { URIBuilder uriBuilder = getUriBuilder("isPrepared"); uriBuilder.setParameter(RequestIdNames.preparedId, preparedId); @@ -588,56 +542,47 @@ public boolean isPrepared(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (InsufficientStorageException | DataNotOnlineException - | InsufficientPrivilegesException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public boolean isReadOnly(int sc) - throws InternalException, NotImplementedException { + public boolean isReadOnly(int sc) throws InternalException, NotImplementedException { URI uri = getUri(getUriBuilder("isReadOnly")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (IOException | InsufficientStorageException - | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public boolean isTwoLevel(int sc) - throws InternalException, NotImplementedException { + public boolean isTwoLevel(int sc) throws InternalException, NotImplementedException { URI uri = getUri(getUriBuilder("isTwoLevel")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); return Boolean.parseBoolean(getString(response, sc)); - } catch (IOException | InsufficientStorageException - | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void ping(Integer sc) throws InternalException, NotFoundException, - NotImplementedException { + public void ping(Integer sc) throws InternalException, NotFoundException, NotImplementedException { URI uri = getUri(getUriBuilder("ping")); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { @@ -646,32 +591,26 @@ public void ping(Integer sc) throws InternalException, NotFoundException, checkResponseConformity(response); String result = getString(response, sc); if (!result.equals("IdsOK")) { - throw new NotFoundException( - "Server gave invalid response: " + result); + throw new NotFoundException("Server gave invalid response: " + result); } - } catch (IOException | InsufficientStorageException - | DataNotOnlineException | BadRequestException - | InsufficientPrivilegesException | NotFoundException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (IOException | InsufficientStorageException | DataNotOnlineException | BadRequestException + | InsufficientPrivilegesException | NotFoundException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public String prepareData(String sessionId, DataSelection data, Flag flags, - Integer sc) throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, NotFoundException, + public String prepareData(String sessionId, DataSelection data, Flag flags, Integer sc) + throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, NotFoundException, InternalException { URI uri = getUri(getUriBuilder("prepareData")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } if (flags == Flag.ZIP || flags == Flag.ZIP_AND_COMPRESS) { formparams.add(new BasicNameValuePair("zip", "true")); @@ -683,13 +622,11 @@ public String prepareData(String sessionId, DataSelection data, Flag flags, HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient - .execute(httpPost)) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { checkResponseConformity(response); return getString(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -697,23 +634,17 @@ public String prepareData(String sessionId, DataSelection data, Flag flags, } - public Long put(String sessionId, InputStream inputStream, String name, - long datasetId, long datafileFormatId, String description, - Integer sc) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, NotImplementedException, - DataNotOnlineException, InsufficientStorageException { - return put(sessionId, inputStream, name, datasetId, datafileFormatId, - description, null, null, null, sc); + public Long put(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, + String description, Integer sc) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, + NotImplementedException, DataNotOnlineException, InsufficientStorageException { + return put(sessionId, inputStream, name, datasetId, datafileFormatId, description, null, null, null, sc); } - public Long put(String sessionId, InputStream inputStream, String name, - long datasetId, long datafileFormatId, String description, - String doi, Date datafileCreateTime, Date datafileModTime, - Integer sc) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, NotImplementedException, - DataNotOnlineException, InsufficientStorageException { + public Long put(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, + String description, String doi, Date datafileCreateTime, Date datafileModTime, Integer sc) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, + NotImplementedException, DataNotOnlineException, InsufficientStorageException { if (inputStream == null) { throw new BadRequestException("Input stream is null"); @@ -721,10 +652,8 @@ public Long put(String sessionId, InputStream inputStream, String name, CRC32 crc = new CRC32(); inputStream = new CheckedInputStream(inputStream, crc); URIBuilder uriBuilder = getUriBuilder("put"); - uriBuilder.setParameter(RequestIdNames.sessionId, sessionId) - .setParameter("name", name) - .setParameter("datafileFormatId", - Long.toString(datafileFormatId)) + uriBuilder.setParameter(RequestIdNames.sessionId, sessionId).setParameter("name", name) + .setParameter("datafileFormatId", Long.toString(datafileFormatId)) .setParameter("datasetId", Long.toString(datasetId)); if (description != null) { uriBuilder.setParameter("description", description); @@ -733,49 +662,39 @@ public Long put(String sessionId, InputStream inputStream, String name, uriBuilder.setParameter("doi", doi); } if (datafileCreateTime != null) { - uriBuilder.setParameter("datafileCreateTime", - Long.toString(datafileCreateTime.getTime())); + uriBuilder.setParameter("datafileCreateTime", Long.toString(datafileCreateTime.getTime())); } if (datafileModTime != null) { - uriBuilder.setParameter("datafileModTime", - Long.toString(datafileModTime.getTime())); + uriBuilder.setParameter("datafileModTime", Long.toString(datafileModTime.getTime())); } URI uri = getUri(uriBuilder); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPut httpPut = new HttpPut(uri); - httpPut.setEntity(new InputStreamEntity(inputStream, - ContentType.APPLICATION_OCTET_STREAM)); + httpPut.setEntity(new InputStreamEntity(inputStream, ContentType.APPLICATION_OCTET_STREAM)); try (CloseableHttpResponse response = httpclient.execute(httpPut)) { String result = getString(response, sc); checkResponseConformity(response); - try (JsonReader jsonReader = Json - .createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); - if (rootNode.getJsonNumber("checksum").longValueExact() != crc - .getValue()) { - throw new InternalException( - "Error uploading - the checksum was not as expected"); + if (rootNode.getJsonNumber("checksum").longValueExact() != crc.getValue()) { + throw new InternalException("Error uploading - the checksum was not as expected"); } return rootNode.getJsonNumber("id").longValueExact(); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (NumberFormatException e) { - throw new InternalException( - "Web service call did not return a valid Long value"); + throw new InternalException("Web service call did not return a valid Long value"); } } - public Long putAsPost(String sessionId, InputStream inputStream, - String name, long datasetId, long datafileFormatId, - String description, String doi, Date datafileCreateTime, - Date datafileModTime, boolean wrap, Integer sc) - throws BadRequestException, NotFoundException, InternalException, - InsufficientPrivilegesException, NotImplementedException, - DataNotOnlineException, InsufficientStorageException { + public Long putAsPost(String sessionId, InputStream inputStream, String name, long datasetId, long datafileFormatId, + String description, String doi, Date datafileCreateTime, Date datafileModTime, boolean wrap, Integer sc) + throws BadRequestException, NotFoundException, InternalException, InsufficientPrivilegesException, + NotImplementedException, DataNotOnlineException, InsufficientStorageException { if (inputStream == null) { throw new BadRequestException("Input stream is null"); @@ -784,40 +703,29 @@ public Long putAsPost(String sessionId, InputStream inputStream, inputStream = new CheckedInputStream(inputStream, crc); URI uri = getUri(getUriBuilder("put")); - MultipartEntityBuilder reqEntityBuilder = MultipartEntityBuilder - .create() - .addPart(RequestIdNames.sessionId, - new StringBody(sessionId, ContentType.TEXT_PLAIN)) - .addPart("datafileFormatId", - new StringBody(Long.toString(datafileFormatId), - ContentType.TEXT_PLAIN)) + MultipartEntityBuilder reqEntityBuilder = MultipartEntityBuilder.create() + .addPart(RequestIdNames.sessionId, new StringBody(sessionId, ContentType.TEXT_PLAIN)) + .addPart("datafileFormatId", new StringBody(Long.toString(datafileFormatId), ContentType.TEXT_PLAIN)) .addPart("name", new StringBody(name, ContentType.TEXT_PLAIN)) - .addPart("datasetId", new StringBody(Long.toString(datasetId), - ContentType.TEXT_PLAIN)); + .addPart("datasetId", new StringBody(Long.toString(datasetId), ContentType.TEXT_PLAIN)); if (description != null) { - reqEntityBuilder.addPart("description", - new StringBody(description, ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart("description", new StringBody(description, ContentType.TEXT_PLAIN)); } if (doi != null) { - reqEntityBuilder.addPart("doi", - new StringBody(doi, ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart("doi", new StringBody(doi, ContentType.TEXT_PLAIN)); } if (datafileCreateTime != null) { reqEntityBuilder.addPart("datafileCreateTime", - new StringBody(Long.toString(datafileCreateTime.getTime()), - ContentType.TEXT_PLAIN)); + new StringBody(Long.toString(datafileCreateTime.getTime()), ContentType.TEXT_PLAIN)); } if (datafileModTime != null) { reqEntityBuilder.addPart("datafileModTime", - new StringBody(Long.toString(datafileModTime.getTime()), - ContentType.TEXT_PLAIN)); + new StringBody(Long.toString(datafileModTime.getTime()), ContentType.TEXT_PLAIN)); } if (wrap) { - reqEntityBuilder.addPart("wrap", - new StringBody("true", ContentType.TEXT_PLAIN)); + reqEntityBuilder.addPart("wrap", new StringBody("true", ContentType.TEXT_PLAIN)); } - InputStreamBody body = new InputStreamBody( - new BufferedInputStream(inputStream), + InputStreamBody body = new InputStreamBody(new BufferedInputStream(inputStream), ContentType.APPLICATION_OCTET_STREAM, "unreliable"); HttpEntity entity = reqEntityBuilder.addPart("file", body).build(); @@ -831,51 +739,40 @@ public Long putAsPost(String sessionId, InputStream inputStream, String prefix = ""; if (result.startsWith(prefix)) { - result = result.substring(prefix.length(), - result.length() - suffix.length()); + result = result.substring(prefix.length(), result.length() - suffix.length()); } - try (JsonReader jsonReader = Json - .createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); - if (rootNode.getJsonNumber("checksum").longValueExact() != crc - .getValue()) { - throw new InternalException( - "Error uploading - the checksum was not as expected"); + if (rootNode.getJsonNumber("checksum").longValueExact() != crc.getValue()) { + throw new InternalException("Error uploading - the checksum was not as expected"); } return rootNode.getJsonNumber("id").longValueExact(); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } catch (NumberFormatException e) { - throw new InternalException( - "Web service call did not return a valid Long value"); + throw new InternalException("Web service call did not return a valid Long value"); } } - public void restore(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, - NotFoundException { + public void restore(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, + BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { URI uri = getUri(getUriBuilder("restore")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient - .execute(httpPost)) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); @@ -895,8 +792,7 @@ public List getDatafileIds(String preparedId, Integer sc) try (CloseableHttpResponse response = httpclient.execute(httpGet)) { String result = getString(response, sc); checkResponseConformity(response); - try (JsonReader jsonReader = Json - .createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); List ids = new ArrayList<>(); @@ -906,24 +802,21 @@ public List getDatafileIds(String preparedId, Integer sc) } return ids; } catch (JsonException e) { - throw new InternalException("TestingClient " + e.getClass() - + " " + e.getMessage() + " from " + result); + throw new InternalException( + "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); } - } catch (InsufficientStorageException | DataNotOnlineException - | InsufficientPrivilegesException - | NotImplementedException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException + | NotImplementedException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public List getDatafileIds(String sessionId, DataSelection data, - Integer sc) throws InternalException, BadRequestException, - ParseException, NotFoundException { + public List getDatafileIds(String sessionId, DataSelection data, Integer sc) + throws InternalException, BadRequestException, ParseException, NotFoundException { URIBuilder uriBuilder = getUriBuilder("getDatafileIds"); uriBuilder.setParameter(RequestIdNames.sessionId, sessionId); for (Entry entry : data.getParameters().entrySet()) { @@ -937,8 +830,7 @@ public List getDatafileIds(String sessionId, DataSelection data, try (CloseableHttpResponse response = httpclient.execute(httpGet)) { checkResponseConformity(response); String result = getString(response, sc); - try (JsonReader jsonReader = Json - .createReader(new StringReader(result))) { + try (JsonReader jsonReader = Json.createReader(new StringReader(result))) { JsonObject rootNode = jsonReader.readObject(); List ids = new ArrayList<>(); @@ -948,122 +840,97 @@ public List getDatafileIds(String sessionId, DataSelection data, } return ids; } catch (JsonException e) { - throw new InternalException("TestingClient " + e.getClass() - + " " + e.getMessage() + " from " + result); + throw new InternalException( + "TestingClient " + e.getClass() + " " + e.getMessage() + " from " + result); } - } catch (InsufficientStorageException | DataNotOnlineException - | InsufficientPrivilegesException - | NotImplementedException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + } catch (InsufficientStorageException | DataNotOnlineException | InsufficientPrivilegesException + | NotImplementedException e) { + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void reset(String preparedId, Integer sc) - throws InternalException, BadRequestException, ParseException, - InsufficientPrivilegesException, NotImplementedException, - NotFoundException { + public void reset(String preparedId, Integer sc) throws InternalException, BadRequestException, ParseException, + InsufficientPrivilegesException, NotImplementedException, NotFoundException { URI uri = getUri(getUriBuilder("reset")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.preparedId, preparedId)); + formparams.add(new BasicNameValuePair(RequestIdNames.preparedId, preparedId)); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient - .execute(httpPost)) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void reset(String sessionId, DataSelection data, int sc) - throws InternalException, BadRequestException, ParseException, - InsufficientPrivilegesException, NotImplementedException, - NotFoundException { + public void reset(String sessionId, DataSelection data, int sc) throws InternalException, BadRequestException, + ParseException, InsufficientPrivilegesException, NotImplementedException, NotFoundException { URI uri = getUri(getUriBuilder("reset")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpEntity entity = new UrlEncodedFormEntity(formparams); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); - try (CloseableHttpResponse response = httpclient - .execute(httpPost)) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException | DataNotOnlineException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - public void write(String sessionId, DataSelection data, Integer sc) - throws NotImplementedException, BadRequestException, - InsufficientPrivilegesException, InternalException, - NotFoundException, DataNotOnlineException { + public void write(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, + BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException, + DataNotOnlineException { URI uri = getUri(getUriBuilder("write")); List formparams = new ArrayList<>(); - formparams.add( - new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); + formparams.add(new BasicNameValuePair(RequestIdNames.sessionId, sessionId)); for (Entry entry : data.getParameters().entrySet()) { - formparams.add( - new BasicNameValuePair(entry.getKey(), entry.getValue())); + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(formparams)); - try (CloseableHttpResponse response = httpclient - .execute(httpPost)) { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { expectNothing(response, sc); } catch (InsufficientStorageException e) { - throw new InternalException( - e.getClass() + " " + e.getMessage()); + throw new InternalException(e.getClass() + " " + e.getMessage()); } } catch (IOException e) { throw new InternalException(e.getClass() + " " + e.getMessage()); } } - private void checkResponseConformity(HttpResponse response) - throws InternalException { + private void checkResponseConformity(HttpResponse response) throws InternalException { StatusLine status = response.getStatusLine(); var statusCode = status.getStatusCode(); - if (statusCode == 200 && response.getEntity() != null) { - // we have a status of 200 and a payload. Check for header - // conformity - Boolean chunked = response - .getFirstHeader("Transfer-Encoding") != null - ? response.getFirstHeader("Transfer-Encoding") - .getValue().contains("chunked") - : false; - - if ((response.getFirstHeader("Content-Length") == null && !chunked) - || (response.getFirstHeader("Content-Length") != null - && chunked)) { - - throw new InternalException( - "Responses with payload should either contain the Content-Length header or the 'Transfer-Encoding: chunked' header. It needs exactly one of them."); + if(statusCode == 200 && response.getEntity() != null) { + //we have a status of 200 and a payload. Check for header conformity + Boolean chunked = response.getFirstHeader("Transfer-Encoding") != null ? response.getFirstHeader("Transfer-Encoding").getValue().contains("chunked") + : false; + + if ( (response.getFirstHeader("Content-Length") == null && !chunked) + || (response.getFirstHeader("Content-Length") != null && chunked ) ) { + + throw new InternalException("Responses with payload should either contain the Content-Length header or the 'Transfer-Encoding: chunked' header. It needs exactly one of them."); } } } From fcd044cc89f5d357f9b772302a532b84e188d411 Mon Sep 17 00:00:00 2001 From: Marcus Lewerenz <152180836+LewerenzM@users.noreply.github.com> Date: Wed, 4 Sep 2024 09:43:37 +0200 Subject: [PATCH 19/19] formatter: no line splitting --- eclipse-formatter.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eclipse-formatter.xml b/eclipse-formatter.xml index 8c3a01e3..eb9ce20b 100644 --- a/eclipse-formatter.xml +++ b/eclipse-formatter.xml @@ -247,7 +247,7 @@ - + @@ -397,7 +397,7 @@ - +