Search in sources :

Example 81 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolveMultipartFile.

@Test
public void resolveMultipartFile() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
    request.addFile(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    boolean condition = result instanceof MultipartFile;
    assertThat(condition).isTrue();
    assertThat(result).as("Invalid result").isEqualTo(expected);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 82 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolveMultipartFileNotAnnot.

@Test
public void resolveMultipartFileNotAnnot() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("multipartFileNotAnnot", "Hello World".getBytes());
    request.addFile(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    boolean condition = result instanceof MultipartFile;
    assertThat(condition).isTrue();
    assertThat(result).as("Invalid result").isEqualTo(expected);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 83 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class StandardMultipartHttpServletRequestTests method filename.

@Test
public void filename() throws Exception {
    String disposition = "form-data; name=\"file\"; filename=\"myFile.txt\"";
    StandardMultipartHttpServletRequest request = requestWithPart("file", disposition, "");
    MultipartFile multipartFile = request.getFile("file");
    assertThat(multipartFile).isNotNull();
    assertThat(multipartFile.getOriginalFilename()).isEqualTo("myFile.txt");
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) Test(org.junit.jupiter.api.Test)

Example 84 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project goci by EBISPOT.

the class DiseaseTraitUploadController method updloadDiseaseTraits.

@PostMapping("/uploads")
public Object updloadDiseaseTraits(@Valid FileUploadRequest fileUploadRequest, BindingResult result) {
    if (result.hasErrors()) {
        throw new FileValidationException(result);
    }
    MultipartFile multipartFile = fileUploadRequest.getMultipartFile();
    List<DiseaseTrait> diseaseTraits = DiseaseTraitDtoAssembler.disassemble(multipartFile);
    diseaseTraits = diseaseTraitService.createDiseaseTraits(diseaseTraits);
    log.info("{} {} were created", diseaseTraits.size(), EntityType.DISEASE_TRAIT);
    return new ResponseEntity<>(DiseaseTraitDtoAssembler.assemble(diseaseTraits), HttpStatus.CREATED);
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ResponseEntity(org.springframework.http.ResponseEntity) DiseaseTrait(uk.ac.ebi.spot.goci.model.DiseaseTrait) FileValidationException(uk.ac.ebi.spot.goci.curation.exception.FileValidationException)

Example 85 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project goci by EBISPOT.

the class AssociationUploadService method upload.

@Transactional(readOnly = false)
public List<AssociationUploadErrorView> upload(MultipartFile file, Study study, SecureUser user) throws IOException, EnsemblMappingException {
    int s = study.getEvents().size();
    if (s > 0) {
        getLog().trace("Study " + study.getId().toString() + " has " + s + " events");
    }
    // File errors will contain any validation errors and be returned to controller if any are found
    List<AssociationUploadErrorView> fileErrors = new ArrayList<>();
    String originalFilename = file.getOriginalFilename();
    getLog().info("Uploading file: ".concat(originalFilename));
    // Upload file
    try {
        uploadFile(file, study.getId());
        // Send file, including path, to SNP batch loader process
        File uploadedFile = studyFileService.getFileFromFileName(study.getId(), originalFilename);
        ValidationSummary validationSummary = associationFileUploadService.processAndValidateAssociationFile(uploadedFile, "full");
        if (validationSummary != null) {
            // Check if we have any row errors
            long rowErrorCount = validationSummary.getRowValidationSummaries().parallelStream().filter(rowValidationSummary -> !rowValidationSummary.getErrors().isEmpty()).count();
            // Errors found
            if (rowErrorCount > 0) {
                studyFileService.deleteFile(study.getId(), originalFilename);
                getLog().error("Row errors found in file: " + originalFilename);
                validationSummary.getRowValidationSummaries().forEach(rowValidationSummary -> fileErrors.addAll(processRowError(rowValidationSummary)));
            } else {
                // Determine if we have any errors rather than warnings
                // Errors prevent saving association
                List<ValidationError> allAssociationsErrors = new ArrayList<>();
                validationSummary.getAssociationSummaries().forEach(associationSummary -> allAssociationsErrors.addAll(associationSummary.getErrors()));
                long associationErrorCount = allAssociationsErrors.parallelStream().filter(validationError -> !validationError.getWarning()).count();
                if (associationErrorCount > 0) {
                    studyFileService.deleteFile(study.getId(), originalFilename);
                    getLog().error("Association errors found in file: " + originalFilename);
                    validationSummary.getAssociationSummaries().forEach(associationSummary -> fileErrors.addAll(processAssociationError(associationSummary)));
                } else {
                    Integer numberOfAssociations = validationSummary.getAssociationSummaries().size();
                    String description = numberOfAssociations.toString().concat(" associations created from upload of '").concat(originalFilename).concat("'");
                    createBatchUploadEvent(study, description, user);
                    saveAssociations(validationSummary.getAssociationSummaries(), study, user);
                }
            }
        }
        return fileErrors;
    } catch (IOException e) {
        throw new IOException(e);
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ValidationSummary(uk.ac.ebi.spot.goci.model.ValidationSummary) ArrayList(java.util.ArrayList) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) StudyRepository(uk.ac.ebi.spot.goci.repository.StudyRepository) AssociationFileUploadService(uk.ac.ebi.spot.goci.service.AssociationFileUploadService) SecureUser(uk.ac.ebi.spot.goci.model.SecureUser) RowValidationSummary(uk.ac.ebi.spot.goci.model.RowValidationSummary) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Collection(java.util.Collection) EnsemblMappingException(uk.ac.ebi.spot.goci.exception.EnsemblMappingException) ValidationError(uk.ac.ebi.spot.goci.model.ValidationError) IOException(java.io.IOException) AssociationUploadErrorView(uk.ac.ebi.spot.goci.curation.model.AssociationUploadErrorView) File(java.io.File) TrackingOperationService(uk.ac.ebi.spot.goci.service.TrackingOperationService) List(java.util.List) Association(uk.ac.ebi.spot.goci.model.Association) MultipartFile(org.springframework.web.multipart.MultipartFile) Study(uk.ac.ebi.spot.goci.model.Study) AssociationSummary(uk.ac.ebi.spot.goci.model.AssociationSummary) Transactional(org.springframework.transaction.annotation.Transactional) ValidationSummary(uk.ac.ebi.spot.goci.model.ValidationSummary) RowValidationSummary(uk.ac.ebi.spot.goci.model.RowValidationSummary) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AssociationUploadErrorView(uk.ac.ebi.spot.goci.curation.model.AssociationUploadErrorView) ValidationError(uk.ac.ebi.spot.goci.model.ValidationError) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

MultipartFile (org.springframework.web.multipart.MultipartFile)272 File (java.io.File)151 IOException (java.io.IOException)71 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)71 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)53 Test (org.junit.Test)41 FileInputStream (java.io.FileInputStream)37 ArrayList (java.util.ArrayList)30 JobConfig4DB (com.vip.saturn.job.console.mybatis.entity.JobConfig4DB)28 Test (org.junit.jupiter.api.Test)27 InputStream (java.io.InputStream)25 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)24 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 FileOutputStream (java.io.FileOutputStream)18 Date (java.util.Date)18 List (java.util.List)18 PostMapping (org.springframework.web.bind.annotation.PostMapping)17 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)17 Path (java.nio.file.Path)15