Skip to content

Commit

Permalink
Added: new InvalidCommandArgumentsException and using it in CreateDat…
Browse files Browse the repository at this point in the history
…averseFeaturedItemCommand
  • Loading branch information
GPortas committed Dec 26, 2024
1 parent 2c96f21 commit 9cc6117
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
import edu.harvard.iq.dataverse.dataset.DatasetTypeServiceBean;
import edu.harvard.iq.dataverse.engine.command.Command;
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
import edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException;
import edu.harvard.iq.dataverse.engine.command.exception.PermissionException;
import edu.harvard.iq.dataverse.engine.command.exception.*;
import edu.harvard.iq.dataverse.engine.command.impl.GetDraftDatasetVersionCommand;
import edu.harvard.iq.dataverse.engine.command.impl.GetLatestAccessibleDatasetVersionCommand;
import edu.harvard.iq.dataverse.engine.command.impl.GetLatestPublishedDatasetVersionCommand;
import edu.harvard.iq.dataverse.engine.command.impl.GetSpecificPublishedDatasetVersionCommand;
import edu.harvard.iq.dataverse.engine.command.exception.RateLimitCommandException;
import edu.harvard.iq.dataverse.externaltools.ExternalToolServiceBean;
import edu.harvard.iq.dataverse.license.LicenseServiceBean;
import edu.harvard.iq.dataverse.pidproviders.PidUtil;
Expand Down Expand Up @@ -635,6 +632,8 @@ protected <T> T execCommand( Command<T> cmd ) throws WrappedResponse {
throw new WrappedResponse(error(Response.Status.UNAUTHORIZED,
"User " + cmd.getRequest().getUser().getIdentifier() + " is not permitted to perform requested action.") );

} catch (InvalidCommandArgumentsException ex) {
throw new WrappedResponse(ex, error(Status.BAD_REQUEST, ex.getMessage()));
} catch (CommandException ex) {
Logger.getLogger(AbstractApiBean.class.getName()).log(Level.SEVERE, "Error while executing command " + cmd, ex);
throw new WrappedResponse(ex, error(Status.INTERNAL_SERVER_ERROR, ex.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package edu.harvard.iq.dataverse.engine.command.exception;

import edu.harvard.iq.dataverse.engine.command.Command;

/**
* Exception thrown when a {@link Command} is executed with invalid or malformed arguments.
* <p>
* This exception typically indicates that the input parameters provided to the command
* do not meet the required criteria (e.g., missing fields, invalid formats, or other
* constraints).
* </p>
* <p>
* Example scenarios:
* <ul>
* <li>A required argument is null or missing.</li>
* <li>An argument is in an invalid format (e.g., a malformed email address).</li>
* <li>Arguments violate business rules or constraints.</li>
* </ul>
*/
public class InvalidCommandArgumentsException extends CommandException {

public InvalidCommandArgumentsException(String message, Command aCommand) {
super(message, aCommand);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
import edu.harvard.iq.dataverse.engine.command.RequiredPermissions;
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
import edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException;
import edu.harvard.iq.dataverse.engine.command.exception.InvalidCommandArgumentsException;
import edu.harvard.iq.dataverse.settings.JvmSettings;
import edu.harvard.iq.dataverse.util.BundleUtil;
import edu.harvard.iq.dataverse.util.FileUtil;
Expand Down Expand Up @@ -44,7 +44,7 @@ public DataverseFeaturedItem execute(CommandContext ctxt) throws CommandExceptio
return ctxt.dataverseFeaturedItems().save(featuredItem);
}

private void setImageIfAvailable(DataverseFeaturedItem featuredItem) throws IllegalCommandException {
private void setImageIfAvailable(DataverseFeaturedItem featuredItem) throws InvalidCommandArgumentsException {
if (newDataverseFeaturedItemDTO.getImageFileName() != null) {
try {
prepareUploadedImageFile();
Expand All @@ -55,7 +55,7 @@ private void setImageIfAvailable(DataverseFeaturedItem featuredItem) throws Ille
}
}

private void prepareUploadedImageFile() throws IOException, IllegalCommandException {
private void prepareUploadedImageFile() throws IOException, InvalidCommandArgumentsException {
// Step 1: Create a temporary directory to store the uploaded image
Path tempDir = createTempDir();
File uploadedFile = new File(tempDir.toFile(), newDataverseFeaturedItemDTO.getImageFileName());
Expand All @@ -80,27 +80,27 @@ private Path createTempDir() throws IOException {
return tempRoot;
}

private void validateFile(File file) throws IOException, IllegalCommandException {
private void validateFile(File file) throws IOException, InvalidCommandArgumentsException {
validateFileType(file);
validateFileSize(file);
}

private void validateFileType(File file) throws IOException, IllegalCommandException {
private void validateFileType(File file) throws IOException, InvalidCommandArgumentsException {
Tika tika = new Tika();
String mimeType = tika.detect(file);
boolean isImageFile = mimeType != null && mimeType.startsWith("image/");
if (!isImageFile) {
throw new IllegalCommandException(
throw new InvalidCommandArgumentsException(
BundleUtil.getStringFromBundle("dataverse.create.featuredItem.error.invalidFileType"),
this
);
}
}

private void validateFileSize(File file) throws IllegalCommandException {
private void validateFileSize(File file) throws InvalidCommandArgumentsException {
Integer featuredItemsImageMaxSize = JvmSettings.FEATURED_ITEMS_IMAGE_MAXSIZE.lookup(Integer.class);
if (file.length() > featuredItemsImageMaxSize) {
throw new IllegalCommandException(BundleUtil.getStringFromBundle("dataverse.create.featuredItem.error.fileSizeExceedsLimit", List.of(featuredItemsImageMaxSize.toString())), this);
throw new InvalidCommandArgumentsException(BundleUtil.getStringFromBundle("dataverse.create.featuredItem.error.fileSizeExceedsLimit", List.of(featuredItemsImageMaxSize.toString())), this);
}
}
}

0 comments on commit 9cc6117

Please sign in to comment.