Skip to content

Commit

Permalink
#30669 add params validation
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinogiardino committed Nov 19, 2024
1 parent e3d982b commit 40311d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> jobParameters = createJobParameters(preview, params, user, request);
Expand Down Expand Up @@ -154,7 +155,7 @@ private Map<String, Object> createJobParameters(
*/
private void addOptionalParameters(
final com.dotcms.rest.api.v1.contentImport.ContentImportParams params,
final Map<String, Object> jobParameters) throws JsonProcessingException, DotDataException {
final Map<String, Object> jobParameters) throws JsonProcessingException {

final com.dotcms.rest.api.v1.contentImport.ContentImportForm form = params.getForm();

Expand Down Expand Up @@ -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<String, Object> jobParameters,
final HttpServletRequest request) throws DotDataException {

validateFileUpload(params);

try {
final DotTempFile tempFile = APILocator.getTempFileAPI().createTempFile(
params.getContentDisposition().getFileName(),
Expand All @@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;

Expand All @@ -46,22 +51,18 @@ public String getJsonForm() {
return jsonForm;
}

public void setForm(com.dotcms.rest.api.v1.contentImport.ContentImportForm form) {
public void setForm(ContentImportForm form) {
this.form = 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;
}

Expand All @@ -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.");
}
}
}

0 comments on commit 40311d9

Please sign in to comment.