Search in sources :

Example 1 with UploadValidationFunction

use of com.synopsys.integration.alert.common.descriptor.config.field.validation.UploadValidationFunction in project hub-alert by blackducksoftware.

the class AbstractUploadAction method writeFile.

private ActionResponse<Void> writeFile(Resource fileResource) {
    try {
        String targetFilename = target.getFilename();
        String tempFilename = "temp_" + targetFilename;
        Optional<UploadValidationFunction> validationFunction = target.getValidationFunction();
        if (validationFunction.isPresent()) {
            writeFile(tempFilename, fileResource);
            File tempFileToValidate = filePersistenceUtil.createUploadsFile(tempFilename);
            ValidationResult validationResult = validationFunction.get().apply(tempFileToValidate);
            filePersistenceUtil.delete(tempFileToValidate);
            if (validationResult.hasErrors()) {
                return new ActionResponse<>(HttpStatus.BAD_REQUEST, validationResult.combineErrorMessages());
            }
        }
        writeFile(targetFilename, fileResource);
    } catch (IOException ex) {
        logger.error("Error uploading file - file: {}, context: {}, descriptor: {} ", target.getFilename(), target.getContext(), target.getDescriptorKey().getUniversalKey());
        logger.error("Error uploading file caused by: ", ex);
        return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, "Error uploading file to server.");
    }
    return new ActionResponse<>(HttpStatus.NO_CONTENT);
}
Also used : UploadValidationFunction(com.synopsys.integration.alert.common.descriptor.config.field.validation.UploadValidationFunction) IOException(java.io.IOException) ValidationResult(com.synopsys.integration.alert.common.descriptor.config.field.validation.ValidationResult) File(java.io.File) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse)

Aggregations

ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)1 UploadValidationFunction (com.synopsys.integration.alert.common.descriptor.config.field.validation.UploadValidationFunction)1 ValidationResult (com.synopsys.integration.alert.common.descriptor.config.field.validation.ValidationResult)1 File (java.io.File)1 IOException (java.io.IOException)1