Search in sources :

Example 1 with FileBrowserRequest

use of org.entando.entando.web.filebrowser.model.FileBrowserRequest in project entando-core by entando.

the class FileBrowserValidator method validate.

@Override
public void validate(Object target, Errors errors) {
    FileBrowserRequest request = (FileBrowserRequest) target;
    String path = request.getPath();
    if (path.endsWith("/")) {
        if (request instanceof FileBrowserFileRequest) {
            errors.rejectValue("path", ERRCODE_INVALID_PATH, new String[] { path }, "fileBrowser.filename.invalidPath");
            return;
        } else {
            path = path.substring(0, path.length() - 1);
        }
    }
    if (!path.contains("/")) {
        return;
    }
    try {
        String directory = path.substring(0, path.lastIndexOf("/"));
        if (!this.getStorageManager().exists(directory, request.isProtectedFolder())) {
            throw new ResourceNotFoundException(FileBrowserValidator.ERRCODE_RESOURCE_DOES_NOT_EXIST, "parent folder", path);
        }
        if (request instanceof FileBrowserFileRequest) {
            FileBrowserFileRequest fileRequest = (FileBrowserFileRequest) target;
            String extractedFileName = path.substring(path.lastIndexOf("/") + 1, path.length());
            if (!extractedFileName.equalsIgnoreCase(fileRequest.getFilename())) {
                errors.rejectValue("filename", ERRCODE_FILENAME_MISMATCH, new String[] { fileRequest.getFilename(), extractedFileName }, "fileBrowser.filename.body.mismatch");
                throw new ValidationConflictException((BindingResult) errors);
            }
        }
    } catch (ValidationConflictException vce) {
        throw vce;
    } catch (ResourceNotFoundException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Error checking path {} , protected {} ", path, request.isProtectedFolder(), e);
        throw new RestServerError("error checking path", e);
    }
}
Also used : FileBrowserRequest(org.entando.entando.web.filebrowser.model.FileBrowserRequest) RestServerError(org.entando.entando.aps.system.exception.RestServerError) FileBrowserFileRequest(org.entando.entando.web.filebrowser.model.FileBrowserFileRequest) ResourceNotFoundException(org.entando.entando.aps.system.exception.ResourceNotFoundException) ResourceNotFoundException(org.entando.entando.aps.system.exception.ResourceNotFoundException) ValidationConflictException(org.entando.entando.web.common.exceptions.ValidationConflictException) ValidationConflictException(org.entando.entando.web.common.exceptions.ValidationConflictException)

Aggregations

ResourceNotFoundException (org.entando.entando.aps.system.exception.ResourceNotFoundException)1 RestServerError (org.entando.entando.aps.system.exception.RestServerError)1 ValidationConflictException (org.entando.entando.web.common.exceptions.ValidationConflictException)1 FileBrowserFileRequest (org.entando.entando.web.filebrowser.model.FileBrowserFileRequest)1 FileBrowserRequest (org.entando.entando.web.filebrowser.model.FileBrowserRequest)1