Search in sources :

Example 1 with MultipartRequestHandler

use of org.apache.struts.upload.MultipartRequestHandler in project sonarqube by SonarSource.

the class RequestUtils method getMultipartHandler.

/**
     * <p>Try to locate a multipart request handler for this request. First,
     * look for a mapping-specific handler stored for us under an attribute.
     * If one is not present, use the global multipart handler, if there is
     * one.</p>
     *
     * @param request The HTTP request for which the multipart handler should
     *                be found.
     * @return the multipart handler to use, or null if none is found.
     * @throws ServletException if any exception is thrown while attempting to
     *                          locate the multipart handler.
     */
private static MultipartRequestHandler getMultipartHandler(HttpServletRequest request) throws ServletException {
    MultipartRequestHandler multipartHandler = null;
    String multipartClass = (String) request.getAttribute(Globals.MULTIPART_KEY);
    request.removeAttribute(Globals.MULTIPART_KEY);
    // Try to initialize the mapping specific request handler
    if (multipartClass != null) {
        try {
            multipartHandler = (MultipartRequestHandler) applicationInstance(multipartClass);
        } catch (ClassNotFoundException cnfe) {
            log.error("MultipartRequestHandler class \"" + multipartClass + "\" in mapping class not found, " + "defaulting to global multipart class");
        } catch (InstantiationException ie) {
            log.error("InstantiationException when instantiating " + "MultipartRequestHandler \"" + multipartClass + "\", " + "defaulting to global multipart class, exception: " + ie.getMessage());
        } catch (IllegalAccessException iae) {
            log.error("IllegalAccessException when instantiating " + "MultipartRequestHandler \"" + multipartClass + "\", " + "defaulting to global multipart class, exception: " + iae.getMessage());
        }
        if (multipartHandler != null) {
            return multipartHandler;
        }
    }
    ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request);
    multipartClass = moduleConfig.getControllerConfig().getMultipartClass();
    // Try to initialize the global request handler
    if (multipartClass != null) {
        try {
            multipartHandler = (MultipartRequestHandler) applicationInstance(multipartClass);
        } catch (ClassNotFoundException cnfe) {
            throw new ServletException("Cannot find multipart class \"" + multipartClass + "\"" + ", exception: " + cnfe.getMessage());
        } catch (InstantiationException ie) {
            throw new ServletException("InstantiationException when instantiating " + "multipart class \"" + multipartClass + "\", exception: " + ie.getMessage());
        } catch (IllegalAccessException iae) {
            throw new ServletException("IllegalAccessException when instantiating " + "multipart class \"" + multipartClass + "\", exception: " + iae.getMessage());
        }
        if (multipartHandler != null) {
            return multipartHandler;
        }
    }
    return multipartHandler;
}
Also used : ServletException(javax.servlet.ServletException) ModuleConfig(org.apache.struts.config.ModuleConfig) MultipartRequestHandler(org.apache.struts.upload.MultipartRequestHandler)

Example 2 with MultipartRequestHandler

use of org.apache.struts.upload.MultipartRequestHandler in project sonarqube by SonarSource.

the class RequestUtils method populate.

/**
     * <p>Populate the properties of the specified JavaBean from the specified
     * HTTP request, based on matching each parameter name (plus an optional
     * prefix and/or suffix) against the corresponding JavaBeans "property
     * setter" methods in the bean's class. Suitable conversion is done for
     * argument types as described under <code>setProperties</code>.</p>
     *
     * <p>If you specify a non-null <code>prefix</code> and a non-null
     * <code>suffix</code>, the parameter name must match
     * <strong>both</strong> conditions for its value(s) to be used in
     * populating bean properties. If the request's content type is
     * "multipart/form-data" and the method is "POST", the
     * <code>HttpServletRequest</code> object will be wrapped in a
     * <code>MultipartRequestWrapper</code object.</p>
     *
     * @param bean    The JavaBean whose properties are to be set
     * @param prefix  The prefix (if any) to be prepend to bean property names
     *                when looking for matching parameters
     * @param suffix  The suffix (if any) to be appended to bean property
     *                names when looking for matching parameters
     * @param request The HTTP request whose parameters are to be used to
     *                populate bean properties
     * @throws ServletException if an exception is thrown while setting
     *                          property values
     */
public static void populate(Object bean, String prefix, String suffix, HttpServletRequest request) throws ServletException {
    // Build a list of relevant request parameters from this request
    HashMap properties = new HashMap();
    // Iterator of parameter names
    Enumeration names = null;
    // Map for multipart parameters
    Map multipartParameters = null;
    String contentType = request.getContentType();
    String method = request.getMethod();
    boolean isMultipart = false;
    if (bean instanceof ActionForm) {
        ((ActionForm) bean).setMultipartRequestHandler(null);
    }
    MultipartRequestHandler multipartHandler = null;
    if ((contentType != null) && (contentType.startsWith("multipart/form-data")) && (method.equalsIgnoreCase("POST"))) {
        // Get the ActionServletWrapper from the form bean
        ActionServletWrapper servlet;
        if (bean instanceof ActionForm) {
            servlet = ((ActionForm) bean).getServletWrapper();
        } else {
            throw new ServletException("bean that's supposed to be " + "populated from a multipart request is not of type " + "\"org.apache.struts.action.ActionForm\", but type " + "\"" + bean.getClass().getName() + "\"");
        }
        // Obtain a MultipartRequestHandler
        multipartHandler = getMultipartHandler(request);
        if (multipartHandler != null) {
            isMultipart = true;
            // Set servlet and mapping info
            servlet.setServletFor(multipartHandler);
            multipartHandler.setMapping((ActionMapping) request.getAttribute(Globals.MAPPING_KEY));
            // Initialize multipart request class handler
            multipartHandler.handleRequest(request);
            //stop here if the maximum length has been exceeded
            Boolean maxLengthExceeded = (Boolean) request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
            if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
                ((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
                return;
            }
            //retrieve form values and put into properties
            multipartParameters = getAllParametersForMultipartRequest(request, multipartHandler);
            names = Collections.enumeration(multipartParameters.keySet());
        }
    }
    if (!isMultipart) {
        names = request.getParameterNames();
    }
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        String stripped = name;
        if (prefix != null) {
            if (!stripped.startsWith(prefix)) {
                continue;
            }
            stripped = stripped.substring(prefix.length());
        }
        if (suffix != null) {
            if (!stripped.endsWith(suffix)) {
                continue;
            }
            stripped = stripped.substring(0, stripped.length() - suffix.length());
        }
        Object parameterValue = null;
        if (isMultipart) {
            parameterValue = multipartParameters.get(name);
            parameterValue = rationalizeMultipleFileProperty(bean, name, parameterValue);
        } else {
            parameterValue = request.getParameterValues(name);
        }
        // such as 'org.apache.struts.action.CANCEL'
        if (!(stripped.startsWith("org.apache.struts."))) {
            properties.put(stripped, parameterValue);
        }
    }
    // Set the corresponding properties of our bean
    try {
        BeanUtils.populate(bean, properties);
    } catch (Exception e) {
        throw new ServletException("BeanUtils.populate", e);
    } finally {
        if (multipartHandler != null) {
            // Set the multipart request handler for our ActionForm.
            // If the bean isn't an ActionForm, an exception would have been
            // thrown earlier, so it's safe to assume that our bean is
            // in fact an ActionForm.
            ((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) ActionForm(org.apache.struts.action.ActionForm) ActionServletWrapper(org.apache.struts.action.ActionServletWrapper) HashMap(java.util.HashMap) Map(java.util.Map) ServletException(javax.servlet.ServletException) MalformedURLException(java.net.MalformedURLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MultipartRequestHandler(org.apache.struts.upload.MultipartRequestHandler)

Example 3 with MultipartRequestHandler

use of org.apache.struts.upload.MultipartRequestHandler in project jaffa-framework by jaffa-projects.

the class CustomRequestProcessor method getMultipartHandler.

/**
 ********************************************************************************************
 */
/* The following methods are straight copies of the respective private methods of RequestUtils */
/**
 ********************************************************************************************
 */
/**
 * <p>Try to locate a multipart request handler for this request. First, look
 * for a mapping-specific handler stored for us under an attribute. If one
 * is not present, use the global multipart handler, if there is one.</p>
 *
 * @param request The HTTP request for which the multipart handler should
 *                be found.
 * @return the multipart handler to use, or null if none is
 *         found.
 *
 * @exception ServletException if any exception is thrown while attempting
 *                             to locate the multipart handler.
 */
private static MultipartRequestHandler getMultipartHandler(HttpServletRequest request) throws ServletException {
    MultipartRequestHandler multipartHandler = null;
    String multipartClass = (String) request.getAttribute(Globals.MULTIPART_KEY);
    request.removeAttribute(Globals.MULTIPART_KEY);
    // Try to initialize the mapping specific request handler
    if (multipartClass != null) {
        try {
            multipartHandler = (MultipartRequestHandler) RequestUtils.applicationInstance(multipartClass);
        } catch (ClassNotFoundException cnfe) {
            log.error("MultipartRequestHandler class \"" + multipartClass + "\" in mapping class not found, " + "defaulting to global multipart class");
        } catch (InstantiationException ie) {
            log.error("InstantiationException when instantiating " + "MultipartRequestHandler \"" + multipartClass + "\", " + "defaulting to global multipart class, exception: " + ie.getMessage());
        } catch (IllegalAccessException iae) {
            log.error("IllegalAccessException when instantiating " + "MultipartRequestHandler \"" + multipartClass + "\", " + "defaulting to global multipart class, exception: " + iae.getMessage());
        }
        if (multipartHandler != null) {
            return multipartHandler;
        }
    }
    ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request);
    multipartClass = moduleConfig.getControllerConfig().getMultipartClass();
    // Try to initialize the global request handler
    if (multipartClass != null) {
        try {
            multipartHandler = (MultipartRequestHandler) RequestUtils.applicationInstance(multipartClass);
        } catch (ClassNotFoundException cnfe) {
            throw new ServletException("Cannot find multipart class \"" + multipartClass + "\"" + ", exception: " + cnfe.getMessage());
        } catch (InstantiationException ie) {
            throw new ServletException("InstantiationException when instantiating " + "multipart class \"" + multipartClass + "\", exception: " + ie.getMessage());
        } catch (IllegalAccessException iae) {
            throw new ServletException("IllegalAccessException when instantiating " + "multipart class \"" + multipartClass + "\", exception: " + iae.getMessage());
        }
        if (multipartHandler != null) {
            return multipartHandler;
        }
    }
    return multipartHandler;
}
Also used : ServletException(javax.servlet.ServletException) ModuleConfig(org.apache.struts.config.ModuleConfig) MultipartRequestHandler(org.apache.struts.upload.MultipartRequestHandler)

Example 4 with MultipartRequestHandler

use of org.apache.struts.upload.MultipartRequestHandler in project jaffa-framework by jaffa-projects.

the class CustomRequestProcessor method customRequestUtilsPopulate.

/**
 * <p>Populate the properties of the specified JavaBean from the specified
 * HTTP request, based on matching each parameter name (plus an optional
 * prefix and/or suffix) against the corresponding JavaBeans "property
 * setter" methods in the bean's class. Suitable conversion is done for
 * argument types as described under <code>setProperties</code>.</p>
 *
 * <p>If you specify a non-null <code>prefix</code> and a non-null
 * <code>suffix</code>, the parameter name must match <strong>both</strong>
 * conditions for its value(s) to be used in populating bean properties.
 * If the request's content type is "multipart/form-data" and the
 * method is "POST", the <code>HttpServletRequest</code> object will be wrapped in
 * a <code>MultipartRequestWrapper</code object.</p>
 *
 * @param bean The JavaBean whose properties are to be set
 * @param prefix The prefix (if any) to be prepend to bean property
 *               names when looking for matching parameters
 * @param suffix The suffix (if any) to be appended to bean property
 *               names when looking for matching parameters
 * @param request The HTTP request whose parameters are to be used
 *                to populate bean properties
 *
 * @exception ServletException if an exception is thrown while setting
 *            property values
 */
protected static void customRequestUtilsPopulate(Object bean, String prefix, String suffix, HttpServletRequest request, ActionMapping mapping) throws ServletException {
    // Build a list of relevant request parameters from this request
    HashMap properties = new HashMap();
    // Iterator of parameter names
    Enumeration names = null;
    // Map for multipart parameters
    Map multipartParameters = null;
    String contentType = request.getContentType();
    String method = request.getMethod();
    boolean isMultipart = false;
    if ((contentType != null) && (contentType.startsWith("multipart/form-data")) && (method.equalsIgnoreCase("POST"))) {
        // Get the ActionServletWrapper from the form bean
        ActionServletWrapper servlet;
        if (bean instanceof ActionForm) {
            servlet = ((ActionForm) bean).getServletWrapper();
        } else {
            throw new ServletException("bean that's supposed to be " + "populated from a multipart request is not of type " + "\"org.apache.struts.action.ActionForm\", but type " + "\"" + bean.getClass().getName() + "\"");
        }
        // Obtain a MultipartRequestHandler
        MultipartRequestHandler multipartHandler = getMultipartHandler(request);
        // Set the multipart request handler for our ActionForm.
        // If the bean isn't an ActionForm, an exception would have been
        // thrown earlier, so it's safe to assume that our bean is
        // in fact an ActionForm.
        ((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
        if (multipartHandler != null) {
            isMultipart = true;
            // Set servlet and mapping info
            servlet.setServletFor(multipartHandler);
            multipartHandler.setMapping((ActionMapping) request.getAttribute(Globals.MAPPING_KEY));
            // Initialize multipart request class handler
            multipartHandler.handleRequest(request);
            // stop here if the maximum length has been exceeded
            Boolean maxLengthExceeded = (Boolean) request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
            if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
                // return;
                throw new ServletException("The size of the file being uploaded exceeds the maximum allowed " + ModuleUtils.getInstance().getModuleConfig(request).getControllerConfig().getMaxFileSize());
            }
            // retrieve form values and put into properties
            multipartParameters = getAllParametersForMultipartRequest(request, multipartHandler);
            names = Collections.enumeration(multipartParameters.keySet());
        }
    }
    if (!isMultipart) {
        names = request.getParameterNames();
    }
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        String stripped = name;
        if (prefix != null) {
            if (!stripped.startsWith(prefix)) {
                continue;
            }
            stripped = stripped.substring(prefix.length());
        }
        if (suffix != null) {
            if (!stripped.endsWith(suffix)) {
                continue;
            }
            stripped = stripped.substring(0, stripped.length() - suffix.length());
        }
        Object parameterValue = null;
        if (isMultipart) {
            parameterValue = multipartParameters.get(name);
        } else {
            parameterValue = request.getParameterValues(name);
        }
        // such as 'org.apache.struts.action.CANCEL'
        if (!(stripped.startsWith("org.apache.struts."))) {
            properties.put(stripped, parameterValue);
        }
    }
    // *** Jaffa-customization: Reset the bean ***
    try {
        if (log.isDebugEnabled())
            log.debug("Calling FormBean reset()");
        ((ActionForm) bean).reset(mapping, request);
    } catch (Exception e) {
        throw new ServletException("FormBean.reset", e);
    }
    // Set the corresponding properties of our bean
    try {
        BeanUtils.populate(bean, properties);
    } catch (Exception e) {
        throw new ServletException("BeanUtils.populate", e);
    }
}
Also used : ServletException(javax.servlet.ServletException) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) ActionForm(org.apache.struts.action.ActionForm) ActionServletWrapper(org.apache.struts.action.ActionServletWrapper) HashMap(java.util.HashMap) Map(java.util.Map) ServletException(javax.servlet.ServletException) MultipartRequestHandler(org.apache.struts.upload.MultipartRequestHandler)

Example 5 with MultipartRequestHandler

use of org.apache.struts.upload.MultipartRequestHandler in project sonar-java by SonarSource.

the class RequestUtils method populate.

/**
 * <p>Populate the properties of the specified JavaBean from the specified
 * HTTP request, based on matching each parameter name (plus an optional
 * prefix and/or suffix) against the corresponding JavaBeans "property
 * setter" methods in the bean's class. Suitable conversion is done for
 * argument types as described under <code>setProperties</code>.</p>
 *
 * <p>If you specify a non-null <code>prefix</code> and a non-null
 * <code>suffix</code>, the parameter name must match
 * <strong>both</strong> conditions for its value(s) to be used in
 * populating bean properties. If the request's content type is
 * "multipart/form-data" and the method is "POST", the
 * <code>HttpServletRequest</code> object will be wrapped in a
 * <code>MultipartRequestWrapper</code object.</p>
 *
 * @param bean    The JavaBean whose properties are to be set
 * @param prefix  The prefix (if any) to be prepend to bean property names
 *                when looking for matching parameters
 * @param suffix  The suffix (if any) to be appended to bean property
 *                names when looking for matching parameters
 * @param request The HTTP request whose parameters are to be used to
 *                populate bean properties
 * @throws ServletException if an exception is thrown while setting
 *                          property values
 */
public static void populate(Object bean, String prefix, String suffix, HttpServletRequest request) throws ServletException {
    // Build a list of relevant request parameters from this request
    HashMap properties = new HashMap();
    // Iterator of parameter names
    Enumeration names = null;
    // Map for multipart parameters
    Map multipartParameters = null;
    String contentType = request.getContentType();
    String method = request.getMethod();
    boolean isMultipart = false;
    if (bean instanceof ActionForm) {
        ((ActionForm) bean).setMultipartRequestHandler(null);
    }
    MultipartRequestHandler multipartHandler = null;
    if ((contentType != null) && (contentType.startsWith("multipart/form-data")) && (method.equalsIgnoreCase("POST"))) {
        // Get the ActionServletWrapper from the form bean
        ActionServletWrapper servlet;
        if (bean instanceof ActionForm) {
            servlet = ((ActionForm) bean).getServletWrapper();
        } else {
            throw new ServletException("bean that's supposed to be " + "populated from a multipart request is not of type " + "\"org.apache.struts.action.ActionForm\", but type " + "\"" + bean.getClass().getName() + "\"");
        }
        // Obtain a MultipartRequestHandler
        multipartHandler = getMultipartHandler(request);
        if (multipartHandler != null) {
            isMultipart = true;
            // Set servlet and mapping info
            servlet.setServletFor(multipartHandler);
            multipartHandler.setMapping((ActionMapping) request.getAttribute(Globals.MAPPING_KEY));
            // Initialize multipart request class handler
            multipartHandler.handleRequest(request);
            // stop here if the maximum length has been exceeded
            Boolean maxLengthExceeded = (Boolean) request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
            if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
                ((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
                return;
            }
            // retrieve form values and put into properties
            multipartParameters = getAllParametersForMultipartRequest(request, multipartHandler);
            names = Collections.enumeration(multipartParameters.keySet());
        }
    }
    if (!isMultipart) {
        names = request.getParameterNames();
    }
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        String stripped = name;
        if (prefix != null) {
            if (!stripped.startsWith(prefix)) {
                continue;
            }
            stripped = stripped.substring(prefix.length());
        }
        if (suffix != null) {
            if (!stripped.endsWith(suffix)) {
                continue;
            }
            stripped = stripped.substring(0, stripped.length() - suffix.length());
        }
        Object parameterValue = null;
        if (isMultipart) {
            parameterValue = multipartParameters.get(name);
            parameterValue = rationalizeMultipleFileProperty(bean, name, parameterValue);
        } else {
            parameterValue = request.getParameterValues(name);
        }
        // such as 'org.apache.struts.action.CANCEL'
        if (!(stripped.startsWith("org.apache.struts."))) {
            properties.put(stripped, parameterValue);
        }
    }
    // Set the corresponding properties of our bean
    try {
        BeanUtils.populate(bean, properties);
    } catch (Exception e) {
        throw new ServletException("BeanUtils.populate", e);
    } finally {
        if (multipartHandler != null) {
            // Set the multipart request handler for our ActionForm.
            // If the bean isn't an ActionForm, an exception would have been
            // thrown earlier, so it's safe to assume that our bean is
            // in fact an ActionForm.
            ((ActionForm) bean).setMultipartRequestHandler(multipartHandler);
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) ActionForm(org.apache.struts.action.ActionForm) ActionServletWrapper(org.apache.struts.action.ActionServletWrapper) HashMap(java.util.HashMap) Map(java.util.Map) ServletException(javax.servlet.ServletException) MalformedURLException(java.net.MalformedURLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MultipartRequestHandler(org.apache.struts.upload.MultipartRequestHandler)

Aggregations

ServletException (javax.servlet.ServletException)6 MultipartRequestHandler (org.apache.struts.upload.MultipartRequestHandler)6 Enumeration (java.util.Enumeration)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ActionForm (org.apache.struts.action.ActionForm)3 ActionServletWrapper (org.apache.struts.action.ActionServletWrapper)3 ModuleConfig (org.apache.struts.config.ModuleConfig)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MalformedURLException (java.net.MalformedURLException)2