Skip to content

Commit

Permalink
Stash: update featureItems endpoint PoC WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
GPortas committed Dec 16, 2024
1 parent e3b5795 commit d796959
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
56 changes: 53 additions & 3 deletions src/main/java/edu/harvard/iq/dataverse/api/Dataverses.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import edu.harvard.iq.dataverse.util.BundleUtil;
import edu.harvard.iq.dataverse.util.ConstraintViolationUtil;
import edu.harvard.iq.dataverse.util.FileUtil;
import edu.harvard.iq.dataverse.util.StringUtil;
import static edu.harvard.iq.dataverse.util.StringUtil.nonEmpty;
import static edu.harvard.iq.dataverse.util.json.JsonPrinter.*;
Expand All @@ -33,7 +34,9 @@
import edu.harvard.iq.dataverse.util.json.JsonPrinter;
import edu.harvard.iq.dataverse.util.json.JsonUtil;

import java.io.StringReader;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -59,15 +62,16 @@
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;

import java.io.IOException;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.stream.Collectors;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.StreamingOutput;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

import javax.xml.stream.XMLStreamException;

/**
Expand Down Expand Up @@ -1729,4 +1733,50 @@ public Response getUserPermissionsOnDataverse(@Context ContainerRequestContext c
jsonObjectBuilder.add("canDeleteDataverse", permissionService.userOn(requestUser, dataverse).has(Permission.DeleteDataverse));
return ok(jsonObjectBuilder);
}

@PUT
@AuthRequired
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("{identifier}/featuredItems")
public Response updateFeaturedItems(@Context ContainerRequestContext crc,
@PathParam("identifier") String dvIdtf,
@FormDataParam("title") String title,
@FormDataParam("content") String content,
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {
Dataverse dataverse;
try {
dataverse = findDataverseOrDie(dvIdtf);
} catch (WrappedResponse wr) {
return wr.getResponse();
}
try {
String fileName = contentDispositionHeader.getFileName();
File uploadedFile = new File(createTempDir(dataverse), fileName);
if (!uploadedFile.exists()) {
uploadedFile.createNewFile();
}
File file = FileUtil.inputStreamToFile(fileInputStream);
if (file.length() > systemConfig.getUploadLogoSizeLimit()) {
return error(Response.Status.BAD_REQUEST, "File is larger than maximum size: " + systemConfig.getUploadLogoSizeLimit() + ".");
}
Files.copy(fileInputStream, uploadedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
return ok("");
}

private File createTempDir(Dataverse editDv) {
try {
// Create the temporary space if not yet existing (will silently ignore preexisting)
// Note that the docroot directory is checked within ConfigCheckService for presence and write access.
java.nio.file.Path tempRoot = java.nio.file.Path.of(JvmSettings.DOCROOT_DIRECTORY.lookup(), "featuredItems");
Files.createDirectories(tempRoot);

return Files.createTempDirectory(tempRoot, editDv.getId().toString()).toFile();
} catch (IOException e) {
throw new RuntimeException("Error creating temp directory", e); // improve error handling
}
}
}
1 change: 1 addition & 0 deletions src/main/java/propertyFiles/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2790,6 +2790,7 @@ dataverses.api.create.dataset.error.mustIncludeAuthorName=Please provide author
dataverses.api.validate.json.succeeded=The Dataset JSON provided is valid for this Dataverse Collection.
dataverses.api.validate.json.failed=The Dataset JSON provided failed validation with the following error:
dataverses.api.validate.json.exception=Validation failed with following exception:
dataverses.api.update.featured.items.error.onlyImageFilesAllowed=Invalid file type. Only image files are allowed.

#Access.java
access.api.allowRequests.failure.noDataset=Could not find Dataset with id: {0}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/DataversesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1559,4 +1559,17 @@ public void testGetUserPermissionsOnDataverse() {
Response getUserPermissionsOnDataverseInvalidIdResponse = UtilIT.getUserPermissionsOnDataverse("testInvalidAlias", apiToken);
getUserPermissionsOnDataverseInvalidIdResponse.then().assertThat().statusCode(NOT_FOUND.getStatusCode());
}

@Test
public void testUpdateFeaturedItems() {
Response createUserResponse = UtilIT.createRandomUser();
String apiToken = UtilIT.getApiTokenFromResponse(createUserResponse);
Response createDataverseResponse = UtilIT.createRandomDataverse(apiToken);
createDataverseResponse.then().assertThat().statusCode(CREATED.getStatusCode());
String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse);

String pathToTestFile = "src/test/resources/images/coffeeshop.png";
Response updateFeatureItemsResponse = UtilIT.updateFeaturedItems(dataverseAlias, apiToken, "test", "test", pathToTestFile);
updateFeatureItemsResponse.prettyPrint();
}
}
10 changes: 10 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -4305,4 +4305,14 @@ static Response deleteDatasetTypes(long doomed, String apiToken) {
.delete("/api/datasets/datasetTypes/" + doomed);
}

static Response updateFeaturedItems(String dataverseAlias, String apiToken, String title, String content, String pathToFile) {
return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.contentType(ContentType.MULTIPART)
.multiPart("title", title)
.multiPart("content", content)
.multiPart("file", new File(pathToFile))
.when()
.put("/api/dataverses/" + dataverseAlias + "/featuredItems");
}
}

0 comments on commit d796959

Please sign in to comment.