Search in sources :

Example 6 with MultipartRequestHandler

use of org.apache.struts.upload.MultipartRequestHandler in project sonar-java 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)

Aggregations

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