Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions src/main/java/org/icatproject/ids/IdsBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,24 +275,13 @@ 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 = "<html><script type=\"text/javascript\">window.name='";
private static final String suffix = "';</script></html>";

/**
* 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}$");

static {
paddedPrefix = "<html><script type=\"text/javascript\">/*";
for (int n = 1; n < 25; n++) {
paddedPrefix += " * \n";
}
paddedPrefix += "*/window.name='";
}

static void cleanDatasetCache(Path datasetDir) {
for (File dsFile : datasetDir.toFile().listFiles()) {
Path path = dsFile.toPath();
Expand Down Expand Up @@ -1789,7 +1778,7 @@ public String prepareData(String sessionId, String investigationIds, String data

public Response put(InputStream body, String sessionId, String name, String datafileFormatIdString,
String datasetIdString, String description, String doi, String datafileCreateTimeString,
String datafileModTimeString, boolean wrap, boolean padding, String ip)
String datafileModTimeString, String ip)
throws NotFoundException, DataNotOnlineException, BadRequestException, InsufficientPrivilegesException,
InternalException, NotImplementedException {

Expand Down Expand Up @@ -1937,7 +1926,7 @@ public Response put(InputStream body, String sessionId, String name, String data
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();
String resp = baos.toString();

if (logSet.contains(CallType.WRITE)) {
try {
Expand Down Expand Up @@ -1969,13 +1958,7 @@ public Response put(InputStream body, String sessionId, String name, String data
JsonGenerator gen = Json.createGenerator(baos);
gen.writeStartObject().write("code", e.getClass().getSimpleName()).write("message", e.getShortMessage());
gen.writeEnd().close();
if (wrap) {
String pre = padding ? paddedPrefix : prefix;
return Response.status(e.getHttpStatusCode()).entity(pre + baos.toString().replace("'", "\\'") + suffix)
.build();
} else {
return Response.status(e.getHttpStatusCode()).entity(baos.toString()).build();
}
return Response.status(e.getHttpStatusCode()).entity(baos.toString()).build();
}

}
Expand Down
86 changes: 1 addition & 85 deletions src/main/java/org/icatproject/ids/IdsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -518,91 +518,7 @@ public Response put(@Context HttpServletRequest request, InputStream body,
@QueryParam("datafileModTime") String datafileModTime) throws BadRequestException, NotFoundException,
InternalException, InsufficientPrivilegesException, NotImplementedException, DataNotOnlineException {
return idsBean.put(body, sessionId, name, datafileFormatId, datasetId, description, doi, datafileCreateTime,
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");
}
datafileModTime, request.getRemoteAddr());
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/site/xhtml/release-notes.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

<h1>IDS Server Release Notes</h1>

<h2>3.0.0 (not yet released)</h2>
<ul>
<li>#162, #168: Remove POST implementation of put, deprecated in 2.1.1.</li>
</ul>

<h2>2.1.1</h2>
<p>Bug fix release</p>
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
34 changes: 0 additions & 34 deletions src/test/java/org/icatproject/ids/integration/one/PutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<html><script type=\"text/javascript\">window.name='";
String suffix = "';</script></html>";
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 {

Expand Down