Search in sources :

Example 41 with MultipartHttpServletRequest

use of org.springframework.web.multipart.MultipartHttpServletRequest in project plumdo-work by wengwh.

the class ProcessDefinitionImportResource method createProcessDefinition.

@PostMapping(value = "/process-definitions/import", name = "流程定义导入")
@ResponseStatus(value = HttpStatus.CREATED)
public ProcessDefinitionResponse createProcessDefinition(@RequestParam(value = "tenantId", required = false) String tenantId, HttpServletRequest request) {
    if (!(request instanceof MultipartHttpServletRequest)) {
        exceptionFactory.throwIllegalArgument(ErrorConstant.REQUEST_NOT_MULTIPART);
    }
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    if (multipartRequest.getFileMap().size() == 0) {
        exceptionFactory.throwIllegalArgument(ErrorConstant.MULTIPART_CONTENT_EMPTY);
    }
    MultipartFile file = multipartRequest.getFileMap().values().iterator().next();
    String fileName = file.getOriginalFilename();
    if (ObjectUtils.isEmpty(fileName) || !(fileName.endsWith(".bpmn20.xml") || fileName.endsWith(".bpmn") || fileName.toLowerCase().endsWith(".bar") || fileName.toLowerCase().endsWith(".zip"))) {
        exceptionFactory.throwIllegalArgument(ErrorConstant.FILE_NOT_BPMN, fileName);
    }
    try {
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
        if (fileName.endsWith(".bpmn20.xml") || fileName.endsWith(".bpmn")) {
            deploymentBuilder.addInputStream(fileName, file.getInputStream());
        } else if (fileName.toLowerCase().endsWith(".bar") || fileName.toLowerCase().endsWith(".zip")) {
            deploymentBuilder.addZipInputStream(new ZipInputStream(file.getInputStream()));
        }
        deploymentBuilder.name(fileName);
        if (tenantId != null) {
            deploymentBuilder.tenantId(tenantId);
        }
        String deploymentId = deploymentBuilder.deploy().getId();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
        return restResponseFactory.createProcessDefinitionResponse(processDefinition);
    } catch (Exception e) {
        logger.error("导入流程文件异常", e);
        exceptionFactory.throwDefinedException(ErrorConstant.DEFINITION_IMPORT_FILE_ERROR, fileName, e.getMessage());
        return null;
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ZipInputStream(java.util.zip.ZipInputStream) ProcessDefinition(org.flowable.engine.repository.ProcessDefinition) DeploymentBuilder(org.flowable.engine.repository.DeploymentBuilder) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Example 42 with MultipartHttpServletRequest

use of org.springframework.web.multipart.MultipartHttpServletRequest in project ma-core-public by infiniteautomation.

the class FileUploadController method prepareResponse.

/**
 * @param request
 * @param response
 * @param model
 * @throws IOException
 * @throws JsonException
 * @throws FileUploadException
 */
private FileUploadView prepareResponse(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) throws IOException, JsonException, FileUploadException {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    String dataType = multipartRequest.getParameter("dataType");
    String uploadType = multipartRequest.getParameter("uploadType");
    List<Object> fileInfo = new ArrayList<Object>();
    Translations translations = ControllerUtils.getTranslations(request);
    Iterator<String> itr = multipartRequest.getFileNames();
    while (itr.hasNext()) {
        MultipartFile file = multipartRequest.getFile(itr.next());
        if (!file.isEmpty()) {
            Map<String, Object> info = new HashMap<String, Object>();
            info.put("filename", file.getOriginalFilename());
            info.put("dataType", dataType);
            parseFile(file.getInputStream(), info, translations, request);
            fileInfo.add(info);
        }
    }
    model.put("fileInfo", fileInfo);
    boolean iframe = false;
    boolean html5 = false;
    boolean flash = false;
    if (uploadType.equals("iframe")) {
        iframe = true;
        response.setContentType("text/html");
    } else if (uploadType.equals("html5")) {
        html5 = true;
        response.setContentType("application/json");
    } else if (uploadType.equals("flash")) {
        flash = true;
        response.setContentType("text/plain");
    }
    if (iframe || html5) {
        return new FileUploadView(iframe);
    } else if (flash) {
        // TODO handle Flash
        throw new ShouldNeverHappenException("Flash upload not supported.");
    } else {
        throw new ShouldNeverHappenException("Invalid file upload type.");
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MultipartFile(org.springframework.web.multipart.MultipartFile) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) Translations(com.serotonin.m2m2.i18n.Translations) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Example 43 with MultipartHttpServletRequest

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

the class DispatcherServletTests method existingMultipartRequest.

@Test
public void existingMultipartRequest() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
    request.addPreferredLocale(Locale.CANADA);
    request.addUserRole("role1");
    MockHttpServletResponse response = new MockHttpServletResponse();
    ComplexWebApplicationContext.MockMultipartResolver multipartResolver = (ComplexWebApplicationContext.MockMultipartResolver) complexDispatcherServlet.getWebApplicationContext().getBean("multipartResolver");
    MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
    complexDispatcherServlet.service(multipartRequest, response);
    multipartResolver.cleanupMultipart(multipartRequest);
    assertThat(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE)).isNull();
    assertThat(request.getAttribute("cleanedUp")).isNotNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 44 with MultipartHttpServletRequest

use of org.springframework.web.multipart.MultipartHttpServletRequest in project grails-core by grails.

the class DefaultUrlMappingInfo method getResolvedRequest.

private MultipartHttpServletRequest getResolvedRequest(HttpServletRequest request, MultipartResolver resolver) {
    MultipartHttpServletRequest resolvedMultipartRequest = (MultipartHttpServletRequest) request.getAttribute(MultipartHttpServletRequest.class.getName());
    if (resolvedMultipartRequest == null) {
        resolvedMultipartRequest = resolver.resolveMultipart(request);
        request.setAttribute(MultipartHttpServletRequest.class.getName(), resolvedMultipartRequest);
    }
    return resolvedMultipartRequest;
}
Also used : MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Aggregations

MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)44 MultipartFile (org.springframework.web.multipart.MultipartFile)29 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14 File (java.io.File)11 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)10 ActivitiException (org.activiti.engine.ActivitiException)8 PostMapping (org.springframework.web.bind.annotation.PostMapping)8 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)6 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)6 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)6 HashMap (java.util.HashMap)5 RestVariable (org.activiti.rest.service.api.engine.variable.RestVariable)5 DataGridException (com.emc.metalnx.core.domain.exceptions.DataGridException)2 OptUploadFileRespDto (com.paascloud.provider.model.dto.oss.OptUploadFileRespDto)2 ApiOperation (io.swagger.annotations.ApiOperation)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStreamReader (java.io.InputStreamReader)2 Date (java.util.Date)2 List (java.util.List)2