Skip to content

Commit

Permalink
Refactor: dataset version string identifiers extracted to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
GPortas committed Sep 20, 2023
1 parent b19fb82 commit b6ce32b
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 73 deletions.
5 changes: 5 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/api/ApiConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ private ApiConstants() {

// Authentication
public static final String CONTAINER_REQUEST_CONTEXT_USER = "user";

// Dataset
public static final String DS_VERSION_LATEST = ":latest";
public static final String DS_VERSION_DRAFT = ":draft";
public static final String DS_VERSION_LATEST_PUBLISHED = ":latest-published";
}
32 changes: 16 additions & 16 deletions src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import edu.harvard.iq.dataverse.util.json.JsonUtil;
import edu.harvard.iq.dataverse.search.IndexServiceBean;

import static edu.harvard.iq.dataverse.api.ApiConstants.*;
import static edu.harvard.iq.dataverse.util.json.JsonPrinter.*;
import static edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder.jsonObjectBuilder;
import edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder;
Expand Down Expand Up @@ -391,8 +392,8 @@ public Response destroyDataset(@Context ContainerRequestContext crc, @PathParam(
@AuthRequired
@Path("{id}/versions/{versionId}")
public Response deleteDraftVersion(@Context ContainerRequestContext crc, @PathParam("id") String id, @PathParam("versionId") String versionId ){
if ( ! ":draft".equals(versionId) ) {
return badRequest("Only the :draft version can be deleted");
if (!DS_VERSION_DRAFT.equals(versionId)) {
return badRequest("Only the " + DS_VERSION_DRAFT + " version can be deleted");
}

return response( req -> {
Expand Down Expand Up @@ -545,7 +546,7 @@ public Response getVersionFileCounts(@Context ContainerRequestContext crc, @Path
public Response getFileAccessFolderView(@Context ContainerRequestContext crc, @PathParam("id") String datasetId, @QueryParam("version") String versionId, @QueryParam("folder") String folderName, @QueryParam("original") Boolean originals, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) {

folderName = folderName == null ? "" : folderName;
versionId = versionId == null ? ":latest-published" : versionId;
versionId = versionId == null ? DS_VERSION_LATEST_PUBLISHED : versionId;

DatasetVersion version;
try {
Expand Down Expand Up @@ -620,8 +621,8 @@ public Response getVersionMetadataBlock(@Context ContainerRequestContext crc,
@AuthRequired
@Path("{id}/versions/{versionId}/linkset")
public Response getLinkset(@Context ContainerRequestContext crc, @PathParam("id") String datasetId, @PathParam("versionId") String versionId, @Context UriInfo uriInfo, @Context HttpHeaders headers) {
if ( ":draft".equals(versionId) ) {
return badRequest("Signposting is not supported on the :draft version");
if (DS_VERSION_DRAFT.equals(versionId)) {
return badRequest("Signposting is not supported on the " + DS_VERSION_DRAFT + " version");
}
User user = getRequestUser(crc);
return response(req -> {
Expand Down Expand Up @@ -706,10 +707,9 @@ public Response updateDatasetPIDMetadataAll(@Context ContainerRequestContext crc
@AuthRequired
@Path("{id}/versions/{versionId}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateDraftVersion(@Context ContainerRequestContext crc, String jsonBody, @PathParam("id") String id, @PathParam("versionId") String versionId){

if ( ! ":draft".equals(versionId) ) {
return error( Response.Status.BAD_REQUEST, "Only the :draft version can be updated");
public Response updateDraftVersion(@Context ContainerRequestContext crc, String jsonBody, @PathParam("id") String id, @PathParam("versionId") String versionId) {
if (!DS_VERSION_DRAFT.equals(versionId)) {
return error( Response.Status.BAD_REQUEST, "Only the " + DS_VERSION_DRAFT + " version can be updated");
}

try ( StringReader rdr = new StringReader(jsonBody) ) {
Expand Down Expand Up @@ -792,7 +792,7 @@ public Response getVersionJsonLDMetadata(@Context ContainerRequestContext crc, @
@Path("{id}/metadata")
@Produces("application/ld+json, application/json-ld")
public Response getVersionJsonLDMetadata(@Context ContainerRequestContext crc, @PathParam("id") String id, @Context UriInfo uriInfo, @Context HttpHeaders headers) {
return getVersionJsonLDMetadata(crc, id, ":draft", uriInfo, headers);
return getVersionJsonLDMetadata(crc, id, DS_VERSION_DRAFT, uriInfo, headers);
}

@PUT
Expand Down Expand Up @@ -1726,7 +1726,7 @@ public Response getCustomTermsTab(@PathParam("id") String id, @PathParam("versio
return error(Status.NOT_FOUND, "This Dataset has no custom license");
}
persistentId = getRequestParameter(":persistentId".substring(1));
if (versionId.equals(":draft")) {
if (versionId.equals(DS_VERSION_DRAFT)) {
versionId = "DRAFT";
}
} catch (WrappedResponse wrappedResponse) {
Expand Down Expand Up @@ -2687,11 +2687,11 @@ private void msgt(String m) {
public static <T> T handleVersion(String versionId, DsVersionHandler<T> hdl)
throws WrappedResponse {
switch (versionId) {
case ":latest":
case DS_VERSION_LATEST:
return hdl.handleLatest();
case ":draft":
case DS_VERSION_DRAFT:
return hdl.handleDraft();
case ":latest-published":
case DS_VERSION_LATEST_PUBLISHED:
return hdl.handleLatestPublished();
default:
try {
Expand Down Expand Up @@ -3928,8 +3928,8 @@ public Response getDatasetVersionCitation(@Context ContainerRequestContext crc,
@AuthRequired
@Path("{id}/versions/{versionId}/deaccession")
public Response deaccessionDataset(@Context ContainerRequestContext crc, @PathParam("id") String datasetId, @PathParam("versionId") String versionId, String jsonBody, @Context UriInfo uriInfo, @Context HttpHeaders headers) {
if (":draft".equals(versionId) || ":latest".equals(versionId)) {
return badRequest("Only :latest-published or a specific version can be deaccessioned");
if (DS_VERSION_DRAFT.equals(versionId) || DS_VERSION_LATEST.equals(versionId)) {
return badRequest("Only " + DS_VERSION_LATEST_PUBLISHED + " or a specific version can be deaccessioned");
}
return response(req -> {
DatasetVersion datasetVersion = getDatasetVersionOrDie(req, versionId, findDatasetOrDie(datasetId), uriInfo, headers, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddress;
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser;
import edu.harvard.iq.dataverse.dataaccess.DataAccess;

import static edu.harvard.iq.dataverse.api.ApiConstants.DS_VERSION_DRAFT;
import static edu.harvard.iq.dataverse.dataaccess.DataAccess.getStorageIO;
import edu.harvard.iq.dataverse.dataaccess.StorageIO;
import edu.harvard.iq.dataverse.dataaccess.ImageThumbConverter;
Expand Down Expand Up @@ -580,10 +582,10 @@ public static String getLicenseURI(DatasetVersion dsv) {
// Return the URI
// For standard licenses, just return the stored URI
return (license != null) ? license.getUri().toString()
// For custom terms, construct a URI with :draft or the version number in the URI
// For custom terms, construct a URI with draft version constant or the version number in the URI
: (dsv.getVersionState().name().equals("DRAFT")
? dsv.getDataverseSiteUrl()
+ "/api/datasets/:persistentId/versions/:draft/customlicense?persistentId="
+ "/api/datasets/:persistentId/versions/" + DS_VERSION_DRAFT + "/customlicense?persistentId="
+ dsv.getDataset().getGlobalId().asString()
: dsv.getDataverseSiteUrl() + "/api/datasets/:persistentId/versions/" + dsv.getVersionNumber()
+ "." + dsv.getMinorVersionNumber() + "/customlicense?persistentId="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import org.apache.commons.codec.binary.StringUtils;

import static edu.harvard.iq.dataverse.api.ApiConstants.DS_VERSION_LATEST;

/**
* Handles an operation on a specific file. Requires a file id in order to be
* instantiated. Applies logic based on an {@link ExternalTool} specification,
Expand Down Expand Up @@ -110,7 +112,7 @@ public String handleRequest(boolean preview) {
switch (externalTool.getScope()) {
case DATASET:
callback=SystemConfig.getDataverseSiteUrlStatic() + "/api/v1/datasets/"
+ dataset.getId() + "/versions/:latest/toolparams/" + externalTool.getId();
+ dataset.getId() + "/versions/" + DS_VERSION_LATEST + "/toolparams/" + externalTool.getId();
break;
case FILE:
callback= SystemConfig.getDataverseSiteUrlStatic() + "/api/v1/files/"
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import edu.harvard.iq.dataverse.dataset.DatasetThumbnail;
import edu.harvard.iq.dataverse.dataset.DatasetUtil;
import edu.harvard.iq.dataverse.datasetutility.FileExceedsMaxSizeException;

import static edu.harvard.iq.dataverse.api.ApiConstants.DS_VERSION_DRAFT;
import static edu.harvard.iq.dataverse.datasetutility.FileSizeChecker.bytesToHumanReadable;
import edu.harvard.iq.dataverse.ingest.IngestReport;
import edu.harvard.iq.dataverse.ingest.IngestServiceBean;
Expand Down Expand Up @@ -2152,7 +2154,7 @@ private static String getFileAccessUrl(FileMetadata fileMetadata, String apiLoca
private static String getFolderAccessUrl(DatasetVersion version, String currentFolder, String subFolder, String apiLocation, boolean originals) {
String datasetId = version.getDataset().getId().toString();
String versionTag = version.getFriendlyVersionNumber();
versionTag = versionTag.replace("DRAFT", ":draft");
versionTag = versionTag.replace("DRAFT", DS_VERSION_DRAFT);
if (!"".equals(currentFolder)) {
subFolder = currentFolder + "/" + subFolder;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/util/URLTokenUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import edu.harvard.iq.dataverse.GlobalId;
import edu.harvard.iq.dataverse.authorization.users.ApiToken;

import static edu.harvard.iq.dataverse.api.ApiConstants.DS_VERSION_DRAFT;

public class URLTokenUtil {

protected static final Logger logger = Logger.getLogger(URLTokenUtil.class.getCanonicalName());
Expand Down Expand Up @@ -177,8 +179,7 @@ private String getTokenValue(String value) {
}
}
if (("DRAFT").equals(versionString)) {
versionString = ":draft"; // send the token needed in api calls that can be substituted for a numeric
// version.
versionString = DS_VERSION_DRAFT; // send the token needed in api calls that can be substituted for a numeric version.
}
return versionString;
case FILE_METADATA_ID:
Expand Down
Loading

0 comments on commit b6ce32b

Please sign in to comment.