Search in sources :

Example 1 with MultipartRequestWrapper

use of jodd.servlet.upload.MultipartRequestWrapper in project jodd by oblac.

the class ServletConfigInterceptor method intercept.

/**
	 * {@inheritDoc}
	 */
public Object intercept(ActionRequest actionRequest) throws Exception {
    HttpServletRequest servletRequest = actionRequest.getHttpServletRequest();
    // detect multipart request
    if (ServletUtil.isMultipartRequest(servletRequest)) {
        servletRequest = new MultipartRequestWrapper(servletRequest, madvocConfig.getFileUploadFactory(), madvocConfig.getEncoding());
        actionRequest.setHttpServletRequest(servletRequest);
    }
    // do it
    inject(actionRequest);
    Object result = actionRequest.invoke();
    outject(actionRequest);
    return result;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MultipartRequestWrapper(jodd.servlet.upload.MultipartRequestWrapper)

Example 2 with MultipartRequestWrapper

use of jodd.servlet.upload.MultipartRequestWrapper in project jodd by oblac.

the class RequestScopeInjector method injectUploadedFiles.

/**
	 * Inject uploaded files from multipart request parameters.
	 */
protected void injectUploadedFiles(Target[] targets, ScopeData[] injectData, HttpServletRequest servletRequest) {
    if (!(servletRequest instanceof MultipartRequestWrapper)) {
        return;
    }
    MultipartRequestWrapper multipartRequest = (MultipartRequestWrapper) servletRequest;
    if (!multipartRequest.isMultipart()) {
        return;
    }
    Enumeration paramNames = multipartRequest.getFileParameterNames();
    while (paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        if (servletRequest.getAttribute(paramName) != null) {
            continue;
        }
        for (int i = 0; i < targets.length; i++) {
            Target target = targets[i];
            if (injectData[i] == null) {
                continue;
            }
            ScopeData.In[] scopes = injectData[i].in;
            if (scopes == null) {
                continue;
            }
            for (ScopeData.In in : scopes) {
                String name = getMatchedPropertyName(in, paramName);
                if (name != null) {
                    FileUpload[] paramValues = multipartRequest.getFiles(paramName);
                    if (ignoreInvalidUploadFiles) {
                        for (int j = 0; j < paramValues.length; j++) {
                            FileUpload paramValue = paramValues[j];
                            if ((!paramValue.isValid()) || (!paramValue.isUploaded())) {
                                paramValues[j] = null;
                            }
                        }
                    }
                    Object value = (paramValues.length == 1 ? paramValues[0] : paramValues);
                    setTargetProperty(target, name, value);
                }
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) MultipartRequestWrapper(jodd.servlet.upload.MultipartRequestWrapper) ScopeData(jodd.madvoc.ScopeData) FileUpload(jodd.upload.FileUpload)

Example 3 with MultipartRequestWrapper

use of jodd.servlet.upload.MultipartRequestWrapper in project jodd by oblac.

the class ServletUtil method copyParamsToAttributes.

// ---------------------------------------------------------------- copy
/**
	 * Copies all request parameters to attributes.
	 */
public static void copyParamsToAttributes(HttpServletRequest servletRequest, boolean trimParams, boolean treatEmptyParamsAsNull, boolean ignoreEmptyRequestParams) {
    Enumeration paramNames = servletRequest.getParameterNames();
    while (paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        if (servletRequest.getAttribute(paramName) != null) {
            continue;
        }
        String[] paramValues = servletRequest.getParameterValues(paramName);
        paramValues = prepareParameters(paramValues, trimParams, treatEmptyParamsAsNull, ignoreEmptyRequestParams);
        if (paramValues == null) {
            continue;
        }
        servletRequest.setAttribute(paramName, paramValues.length == 1 ? paramValues[0] : paramValues);
    }
    // multipart
    if (!(servletRequest instanceof MultipartRequestWrapper)) {
        return;
    }
    MultipartRequestWrapper multipartRequest = (MultipartRequestWrapper) servletRequest;
    if (!multipartRequest.isMultipart()) {
        return;
    }
    paramNames = multipartRequest.getFileParameterNames();
    while (paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        if (servletRequest.getAttribute(paramName) != null) {
            continue;
        }
        FileUpload[] paramValues = multipartRequest.getFiles(paramName);
        servletRequest.setAttribute(paramName, paramValues.length == 1 ? paramValues[0] : paramValues);
    }
}
Also used : Enumeration(java.util.Enumeration) MultipartRequestWrapper(jodd.servlet.upload.MultipartRequestWrapper) FileUpload(jodd.upload.FileUpload)

Aggregations

MultipartRequestWrapper (jodd.servlet.upload.MultipartRequestWrapper)3 Enumeration (java.util.Enumeration)2 FileUpload (jodd.upload.FileUpload)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ScopeData (jodd.madvoc.ScopeData)1