Search in sources :

Example 31 with MultipartHttpServletRequest

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

the class CommonsMultipartResolverTests method withServletContextAndFilter.

@Test
public void withServletContextAndFilter() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.registerSingleton("filterMultipartResolver", MockCommonsMultipartResolver.class, new MutablePropertyValues());
    wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
    wac.refresh();
    wac.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    CommonsMultipartResolver resolver = new CommonsMultipartResolver(wac.getServletContext());
    assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));
    MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
    filterConfig.addInitParameter("class", "notWritable");
    filterConfig.addInitParameter("unknownParam", "someValue");
    final MultipartFilter filter = new MultipartFilter();
    filter.init(filterConfig);
    final List<MultipartFile> files = new ArrayList<>();
    final FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletRequest;
            files.addAll(request.getFileMap().values());
        }
    };
    FilterChain filterChain2 = new PassThroughFilterChain(filter, filterChain);
    MockHttpServletRequest originalRequest = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    originalRequest.setMethod("POST");
    originalRequest.setContentType("multipart/form-data");
    originalRequest.addHeader("Content-type", "multipart/form-data");
    filter.doFilter(originalRequest, response, filterChain2);
    CommonsMultipartFile file1 = (CommonsMultipartFile) files.get(0);
    CommonsMultipartFile file2 = (CommonsMultipartFile) files.get(1);
    assertTrue(((MockFileItem) file1.getFileItem()).deleted);
    assertTrue(((MockFileItem) file2.getFileItem()).deleted);
}
Also used : MultipartFilter(org.springframework.web.multipart.support.MultipartFilter) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) ArrayList(java.util.ArrayList) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) MultipartFile(org.springframework.web.multipart.MultipartFile) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) Test(org.junit.Test)

Example 32 with MultipartHttpServletRequest

use of org.springframework.web.multipart.MultipartHttpServletRequest in project Activiti by Activiti.

the class ExecutionVariableResource method updateVariable.

@RequestMapping(value = "/runtime/executions/{executionId}/variables/{variableName}", method = RequestMethod.PUT, produces = "application/json")
public RestVariable updateVariable(@PathVariable("executionId") String executionId, @PathVariable("variableName") String variableName, HttpServletRequest request) {
    Execution execution = getExecutionFromRequest(executionId);
    RestVariable result = null;
    if (request instanceof MultipartHttpServletRequest) {
        result = setBinaryVariable((MultipartHttpServletRequest) request, execution, RestResponseFactory.VARIABLE_EXECUTION, false);
        if (!result.getName().equals(variableName)) {
            throw new ActivitiIllegalArgumentException("Variable name in the body should be equal to the name used in the requested URL.");
        }
    } else {
        RestVariable restVariable = null;
        try {
            restVariable = objectMapper.readValue(request.getInputStream(), RestVariable.class);
        } catch (Exception e) {
            throw new ActivitiIllegalArgumentException("Error converting request body to RestVariable instance", e);
        }
        if (restVariable == null) {
            throw new ActivitiException("Invalid body was supplied");
        }
        if (!restVariable.getName().equals(variableName)) {
            throw new ActivitiIllegalArgumentException("Variable name in the body should be equal to the name used in the requested URL.");
        }
        result = setSimpleVariable(restVariable, execution, false);
    }
    return result;
}
Also used : RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) ActivitiException(org.activiti.engine.ActivitiException) Execution(org.activiti.engine.runtime.Execution) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ActivitiException(org.activiti.engine.ActivitiException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 33 with MultipartHttpServletRequest

use of org.springframework.web.multipart.MultipartHttpServletRequest in project Activiti by Activiti.

the class BaseVariableCollectionResource method createExecutionVariable.

protected Object createExecutionVariable(Execution execution, boolean override, int variableType, HttpServletRequest request, HttpServletResponse response) {
    Object result = null;
    if (request instanceof MultipartHttpServletRequest) {
        result = setBinaryVariable((MultipartHttpServletRequest) request, execution, variableType, true);
    } else {
        List<RestVariable> inputVariables = new ArrayList<RestVariable>();
        List<RestVariable> resultVariables = new ArrayList<RestVariable>();
        result = resultVariables;
        try {
            @SuppressWarnings("unchecked") List<Object> variableObjects = (List<Object>) objectMapper.readValue(request.getInputStream(), List.class);
            for (Object restObject : variableObjects) {
                RestVariable restVariable = objectMapper.convertValue(restObject, RestVariable.class);
                inputVariables.add(restVariable);
            }
        } catch (Exception e) {
            throw new ActivitiIllegalArgumentException("Failed to serialize to a RestVariable instance", e);
        }
        if (inputVariables == null || inputVariables.size() == 0) {
            throw new ActivitiIllegalArgumentException("Request didn't contain a list of variables to create.");
        }
        RestVariableScope sharedScope = null;
        RestVariableScope varScope = null;
        Map<String, Object> variablesToSet = new HashMap<String, Object>();
        for (RestVariable var : inputVariables) {
            // Validate if scopes match
            varScope = var.getVariableScope();
            if (var.getName() == null) {
                throw new ActivitiIllegalArgumentException("Variable name is required");
            }
            if (varScope == null) {
                varScope = RestVariableScope.LOCAL;
            }
            if (sharedScope == null) {
                sharedScope = varScope;
            }
            if (varScope != sharedScope) {
                throw new ActivitiIllegalArgumentException("Only allowed to update multiple variables in the same scope.");
            }
            if (!override && hasVariableOnScope(execution, var.getName(), varScope)) {
                throw new ActivitiConflictException("Variable '" + var.getName() + "' is already present on execution '" + execution.getId() + "'.");
            }
            Object actualVariableValue = restResponseFactory.getVariableValue(var);
            variablesToSet.put(var.getName(), actualVariableValue);
            resultVariables.add(restResponseFactory.createRestVariable(var.getName(), actualVariableValue, varScope, execution.getId(), variableType, false));
        }
        if (!variablesToSet.isEmpty()) {
            if (sharedScope == RestVariableScope.LOCAL) {
                runtimeService.setVariablesLocal(execution.getId(), variablesToSet);
            } else {
                if (execution.getParentId() != null) {
                    // Explicitly set on parent, setting non-local variables on execution itself will override local-variables if exists
                    runtimeService.setVariables(execution.getParentId(), variablesToSet);
                } else {
                    // Standalone task, no global variables possible
                    throw new ActivitiIllegalArgumentException("Cannot set global variables on execution '" + execution.getId() + "', task is not part of process.");
                }
            }
        }
    }
    response.setStatus(HttpStatus.CREATED.value());
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RestVariableScope(org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope) ActivitiConflictException(org.activiti.rest.exception.ActivitiConflictException) ActivitiConflictException(org.activiti.rest.exception.ActivitiConflictException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ArrayList(java.util.ArrayList) List(java.util.List) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Example 34 with MultipartHttpServletRequest

use of org.springframework.web.multipart.MultipartHttpServletRequest in project Activiti by Activiti.

the class ProcessInstanceVariableResource method updateVariable.

@RequestMapping(value = "/runtime/process-instances/{processInstanceId}/variables/{variableName}", method = RequestMethod.PUT, produces = "application/json")
public RestVariable updateVariable(@PathVariable("processInstanceId") String processInstanceId, @PathVariable("variableName") String variableName, HttpServletRequest request) {
    Execution execution = getProcessInstanceFromRequest(processInstanceId);
    RestVariable result = null;
    if (request instanceof MultipartHttpServletRequest) {
        result = setBinaryVariable((MultipartHttpServletRequest) request, execution, RestResponseFactory.VARIABLE_PROCESS, false);
        if (!result.getName().equals(variableName)) {
            throw new ActivitiIllegalArgumentException("Variable name in the body should be equal to the name used in the requested URL.");
        }
    } else {
        RestVariable restVariable = null;
        try {
            restVariable = objectMapper.readValue(request.getInputStream(), RestVariable.class);
        } catch (Exception e) {
            throw new ActivitiIllegalArgumentException("request body could not be transformed to a RestVariable instance.");
        }
        if (restVariable == null) {
            throw new ActivitiException("Invalid body was supplied");
        }
        if (!restVariable.getName().equals(variableName)) {
            throw new ActivitiIllegalArgumentException("Variable name in the body should be equal to the name used in the requested URL.");
        }
        result = setSimpleVariable(restVariable, execution, false);
    }
    return result;
}
Also used : RestVariable(org.activiti.rest.service.api.engine.variable.RestVariable) ActivitiException(org.activiti.engine.ActivitiException) Execution(org.activiti.engine.runtime.Execution) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) ActivitiException(org.activiti.engine.ActivitiException) ActivitiObjectNotFoundException(org.activiti.engine.ActivitiObjectNotFoundException) ActivitiIllegalArgumentException(org.activiti.engine.ActivitiIllegalArgumentException) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 35 with MultipartHttpServletRequest

use of org.springframework.web.multipart.MultipartHttpServletRequest in project portal by ixinportal.

the class AppController method isRightPhoto.

/**
 * 检查图片是否在20k以内,是否为jpg、png格式
 *
 * @param request
 * @return
 */
private boolean isRightPhoto(HttpServletRequest request) {
    // 20k
    long maxLogoSize = 80 * 1024;
    boolean ret = true;
    if (request instanceof MultipartHttpServletRequest) {
        Map<String, MultipartFile> multifiles = ((MultipartHttpServletRequest) request).getFileMap();
        // 扩展名格式
        String extName = "";
        String logoFile = "";
        for (String fileName : multifiles.keySet()) {
            MultipartFile logo = multifiles.get(fileName);
            logoFile = logo.getOriginalFilename();
            if (StringUtils.isNotBlank(logoFile) && logoFile.lastIndexOf(".") > 0)
                extName = logoFile.substring(logoFile.lastIndexOf(".") + 1).toLowerCase();
            // 2.文件名不为空,且格式不为png
            if (logo.getSize() > maxLogoSize || (StringUtils.isNotBlank(extName) && !"png".equals(extName) && !"jpg".equals(extName))) {
                ret = false;
                break;
            }
            extName = "";
        }
    }
    return ret;
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) 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