From 40311d9feb314ff292922a643361699994aaf1f1 Mon Sep 17 00:00:00 2001 From: valentinogiardino Date: Tue, 19 Nov 2024 11:46:32 -0300 Subject: [PATCH] #30669 add params validation --- .../v1/contentImport/ContentImportHelper.java | 18 +++---------- .../v1/contentImport/ContentImportParams.java | 25 +++++++++++++------ 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/dotCMS/src/main/java/com/dotcms/rest/api/v1/contentImport/ContentImportHelper.java b/dotCMS/src/main/java/com/dotcms/rest/api/v1/contentImport/ContentImportHelper.java index 6447441688eb..52411427eddb 100644 --- a/dotCMS/src/main/java/com/dotcms/rest/api/v1/contentImport/ContentImportHelper.java +++ b/dotCMS/src/main/java/com/dotcms/rest/api/v1/contentImport/ContentImportHelper.java @@ -111,10 +111,11 @@ public void onDestroy() { public String createJob( final boolean preview, final String queueName, - final com.dotcms.rest.api.v1.contentImport.ContentImportParams params, + final ContentImportParams params, final User user, final HttpServletRequest request) throws DotDataException, JsonProcessingException { + params.checkValid(); params.getForm().checkValid(); final Map jobParameters = createJobParameters(preview, params, user, request); @@ -154,7 +155,7 @@ private Map createJobParameters( */ private void addOptionalParameters( final com.dotcms.rest.api.v1.contentImport.ContentImportParams params, - final Map jobParameters) throws JsonProcessingException, DotDataException { + final Map jobParameters) throws JsonProcessingException { final com.dotcms.rest.api.v1.contentImport.ContentImportForm form = params.getForm(); @@ -182,12 +183,10 @@ private void addSiteInformation( * Processes the file upload and adds the necessary parameters to the job */ private void processFileUpload( - final com.dotcms.rest.api.v1.contentImport.ContentImportParams params, + final ContentImportParams params, final Map jobParameters, final HttpServletRequest request) throws DotDataException { - validateFileUpload(params); - try { final DotTempFile tempFile = APILocator.getTempFileAPI().createTempFile( params.getContentDisposition().getFileName(), @@ -201,13 +200,4 @@ private void processFileUpload( throw new DotDataException("Error processing file upload: " + e.getMessage()); } } - - /** - * Validates that the file upload parameters are present - */ - private void validateFileUpload(final com.dotcms.rest.api.v1.contentImport.ContentImportParams params) throws DotDataException { - if (params.getFileInputStream() == null || params.getContentDisposition() == null) { - throw new DotDataException("CSV file is required"); - } - } } \ No newline at end of file diff --git a/dotCMS/src/main/java/com/dotcms/rest/api/v1/contentImport/ContentImportParams.java b/dotCMS/src/main/java/com/dotcms/rest/api/v1/contentImport/ContentImportParams.java index 7f9f9738c62b..0b33eb8c8dae 100644 --- a/dotCMS/src/main/java/com/dotcms/rest/api/v1/contentImport/ContentImportParams.java +++ b/dotCMS/src/main/java/com/dotcms/rest/api/v1/contentImport/ContentImportParams.java @@ -1,9 +1,11 @@ package com.dotcms.rest.api.v1.contentImport; +import com.dotcms.repackage.javax.validation.ValidationException; +import com.dotcms.repackage.javax.validation.constraints.NotNull; import com.dotcms.rest.api.Validated; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.dotmarketing.exception.DotDataException; +import net.minidev.json.annotate.JsonIgnore; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import org.glassfish.jersey.media.multipart.FormDataParam; @@ -14,15 +16,18 @@ */ public class ContentImportParams extends Validated { + @NotNull(message = "The file is required.") @FormDataParam("file") private InputStream fileInputStream; + @JsonIgnore @FormDataParam("file") private FormDataContentDisposition contentDisposition; @FormDataParam("form") - private com.dotcms.rest.api.v1.contentImport.ContentImportForm form; + private ContentImportForm form; + @NotNull(message = "The form data is required.") @FormDataParam("form") private String jsonForm; @@ -46,7 +51,7 @@ public String getJsonForm() { return jsonForm; } - public void setForm(com.dotcms.rest.api.v1.contentImport.ContentImportForm form) { + public void setForm(ContentImportForm form) { this.form = form; } @@ -54,14 +59,10 @@ public void setForm(com.dotcms.rest.api.v1.contentImport.ContentImportForm form) * Gets the parsed form object, lazily parsing the JSON if needed * @return The ContentImportForm object */ - public com.dotcms.rest.api.v1.contentImport.ContentImportForm getForm() throws DotDataException, JsonProcessingException { + public ContentImportForm getForm() throws JsonProcessingException { if (null == form && (null != jsonForm && !jsonForm.isEmpty())) { form = new ObjectMapper().readValue(jsonForm, com.dotcms.rest.api.v1.contentImport.ContentImportForm.class); } - - if (form == null) { - throw new DotDataException("Import form parameters are required"); - } return form; } @@ -73,4 +74,12 @@ public String toString() { ", fileName=" + (contentDisposition != null ? contentDisposition.getFileName() : "null") + '}'; } + + @Override + public void checkValid() { + super.checkValid(); + if (contentDisposition == null || contentDisposition.getFileName() == null) { + throw new ValidationException("The file must have a valid file name."); + } + } } \ No newline at end of file