From 7095ae5aaf9d2b9588ae2ff4238a5212b36ac10c Mon Sep 17 00:00:00 2001 From: Rolf Krahl Date: Wed, 31 Jul 2024 14:20:58 +0200 Subject: [PATCH 1/3] Drop putAsPost call --- .../java/org/icatproject/ids/IdsService.java | 84 ------------------- .../ids/integration/one/PerformanceTest.java | 26 ------ .../ids/integration/one/PutTest.java | 34 -------- .../util/client/TestingClient.java | 65 -------------- 4 files changed, 209 deletions(-) diff --git a/src/main/java/org/icatproject/ids/IdsService.java b/src/main/java/org/icatproject/ids/IdsService.java index daf957bc..30d69957 100644 --- a/src/main/java/org/icatproject/ids/IdsService.java +++ b/src/main/java/org/icatproject/ids/IdsService.java @@ -521,90 +521,6 @@ public Response put(@Context HttpServletRequest request, InputStream body, datafileModTime, false, false, request.getRemoteAddr()); } - /** - * This is an alternative to using PUT on the put resource. All the same - * arguments appear as form fields. In addition there are two boolean fields - * wrap and padding which should be set to true as a CORS work around. These - * two fields will be removed shortly as they are only required by the old - * (GWT based) topcat. - * - * @title putAsPost - * @param request - * @return a json object with attributes of "id", "checksum", "location" and - * "size"; - * @throws BadRequestException - * @throws NotFoundException - * @throws InternalException - * @throws InsufficientPrivilegesException - * @throws NotImplementedException - * @throws DataNotOnlineException - * @statuscode 201 When object successfully created - */ - @POST - @Path("put") - @Consumes(MediaType.MULTIPART_FORM_DATA) - @Produces(MediaType.APPLICATION_JSON) - @Deprecated - 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(); - } - result = idsBean.put(stream, sessionId, name, datafileFormatId, datasetId, description, doi, - datafileCreateTime, datafileModTime, wrap, padding, request.getRemoteAddr()); - } - } - return result; - } catch (IOException e) { - throw new InternalException(e.getClass() + " " + e.getMessage()); - } catch (ServletException e) { - throw new BadRequestException("Multipart content expected"); - } - } - /** * Reset a particular set of datafiles or datasets so that they can be * queried again. diff --git a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java index 1943ae01..31297bf3 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PerformanceTest.java @@ -72,30 +72,4 @@ private static void ts(String msg) { System.out.println("Time to " + msg + ": " + (end - start) + "ms."); start = end; } - - @Test - 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); - ts("store file (post)"); - - int ntot = 0; - try (InputStream stream = testingClient.getData(sessionId, new DataSelection().addDatafile(dfid), Flag.NONE, 0, - null)) { - boolean first = true; - int n; - - while ((n = stream.read(junk)) > 0) { - ntot += n; - if (first) { - ts("get first 1000 bytes"); - first = false; - } - } - } - ts("get last byte of " + ntot); - } } diff --git a/src/test/java/org/icatproject/ids/integration/one/PutTest.java b/src/test/java/org/icatproject/ids/integration/one/PutTest.java index 0a6dec0a..df66057a 100644 --- a/src/test/java/org/icatproject/ids/integration/one/PutTest.java +++ b/src/test/java/org/icatproject/ids/integration/one/PutTest.java @@ -66,38 +66,4 @@ public void putOneFileTest() throws Exception { df.getDatafileModTime()); } - - @Test - public void putAsPostOneFileTest() throws Exception { - - Path dirOnFastStorage = getDirOnFastStorage(datasetIds.get(0)); - - Long dfid = testingClient.putAsPost(sessionId, Files.newInputStream(newFileLocation), - "uploaded_file2_" + timestamp, datasetIds.get(0), supportedDatafileFormat.getId(), - "A rather splendid datafile", null, null, null, true, 201); - - Datafile df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); - assertEquals("A rather splendid datafile", df.getDescription()); - assertNull("A doi", df.getDoi()); - assertNull(df.getDatafileCreateTime()); - 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); - df = (Datafile) icatWS.get(sessionId, "Datafile", dfid); - assertEquals("An even better datafile", df.getDescription()); - assertEquals("7.1.3", df.getDoi()); - - DatatypeFactory datatypeFactory = DatatypeFactory.newInstance(); - GregorianCalendar gregorianCalendar = new GregorianCalendar(); - gregorianCalendar.setTime(new Date(420000)); - assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileCreateTime()); - gregorianCalendar.setTime(new Date(42000)); - assertEquals(datatypeFactory.newXMLGregorianCalendar(gregorianCalendar), - df.getDatafileModTime()); - - } } 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 2f826866..4f335494 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 @@ -700,71 +700,6 @@ public Long put(String sessionId, InputStream inputStream, String name, long dat } - 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"); - } - CRC32 crc = new CRC32(); - inputStream = new CheckedInputStream(inputStream, crc); - URI uri = getUri(getUriBuilder("put")); - - MultipartEntityBuilder reqEntityBuilder = MultipartEntityBuilder.create() - .addPart("sessionId", new StringBody(sessionId, ContentType.TEXT_PLAIN)) - .addPart("datafileFormatId", new StringBody(Long.toString(datafileFormatId), ContentType.TEXT_PLAIN)) - .addPart("name", new StringBody(name, ContentType.TEXT_PLAIN)) - .addPart("datasetId", new StringBody(Long.toString(datasetId), ContentType.TEXT_PLAIN)); - if (description != null) { - reqEntityBuilder.addPart("description", new StringBody(description, ContentType.TEXT_PLAIN)); - } - if (doi != null) { - reqEntityBuilder.addPart("doi", new StringBody(doi, ContentType.TEXT_PLAIN)); - } - if (datafileCreateTime != null) { - reqEntityBuilder.addPart("datafileCreateTime", - new StringBody(Long.toString(datafileCreateTime.getTime()), ContentType.TEXT_PLAIN)); - } - if (datafileModTime != null) { - reqEntityBuilder.addPart("datafileModTime", - new StringBody(Long.toString(datafileModTime.getTime()), ContentType.TEXT_PLAIN)); - } - if (wrap) { - reqEntityBuilder.addPart("wrap", new StringBody("true", ContentType.TEXT_PLAIN)); - } - InputStreamBody body = new InputStreamBody(new BufferedInputStream(inputStream), - ContentType.APPLICATION_OCTET_STREAM, "unreliable"); - HttpEntity entity = reqEntityBuilder.addPart("file", body).build(); - - CloseableHttpClient httpclient = HttpClients.createDefault(); - HttpPost httpPost = new HttpPost(uri); - httpPost.setEntity(entity); - - try (CloseableHttpResponse response = httpclient.execute(httpPost)) { - checkResponseConformity(response); - String result = getString(response, sc); - String prefix = ""; - if (result.startsWith(prefix)) { - result = result.substring(prefix.length(), result.length() - suffix.length()); - } - 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"); - } - 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"); - } - - } - public void restore(String sessionId, DataSelection data, Integer sc) throws NotImplementedException, BadRequestException, InsufficientPrivilegesException, InternalException, NotFoundException { From bc29357e71122990b6419f6c97941a855f050e7c Mon Sep 17 00:00:00 2001 From: Rolf Krahl Date: Wed, 31 Jul 2024 13:50:16 +0200 Subject: [PATCH 2/3] Drop arguments wrap and padding from method IdsBean.put() --- .../java/org/icatproject/ids/IdsBean.java | 23 +++---------------- .../java/org/icatproject/ids/IdsService.java | 2 +- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/icatproject/ids/IdsBean.java b/src/main/java/org/icatproject/ids/IdsBean.java index 59b49fa7..0c0be3ad 100644 --- a/src/main/java/org/icatproject/ids/IdsBean.java +++ b/src/main/java/org/icatproject/ids/IdsBean.java @@ -275,9 +275,6 @@ public void write(OutputStream output) throws IOException { private static String key; private final static Logger logger = LoggerFactory.getLogger(IdsBean.class); - private static String paddedPrefix; - private static final String prefix = ""; /** * matches standard UUID format of 8-4-4-4-12 hexadecimal digits @@ -285,14 +282,6 @@ public void write(OutputStream output) throws IOException { 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}$"); - static { - paddedPrefix = "