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] 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."); } } }