Search in sources :

Example 11 with ModuleConfig

use of org.apache.struts.config.ModuleConfig in project sonarqube by SonarSource.

the class TagUtils method getActionMappingURL.

/**
     * Return the form action converted into a server-relative URL.
     */
public String getActionMappingURL(String action, String module, PageContext pageContext, boolean contextRelative) {
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    String contextPath = request.getContextPath();
    StringBuffer value = new StringBuffer();
    //  in case of non-root context, otherwise length==1 (the slash)
    if (contextPath.length() > 1) {
        value.append(contextPath);
    }
    ModuleConfig moduleConfig = getModuleConfig(module, pageContext);
    if ((moduleConfig != null) && (!contextRelative)) {
        value.append(moduleConfig.getPrefix());
    }
    // Use our servlet mapping, if one is specified
    String servletMapping = (String) pageContext.getAttribute(Globals.SERVLET_KEY, PageContext.APPLICATION_SCOPE);
    if (servletMapping != null) {
        String queryString = null;
        int question = action.indexOf("?");
        if (question >= 0) {
            queryString = action.substring(question);
        }
        String actionMapping = getActionMappingName(action);
        if (servletMapping.startsWith("*.")) {
            value.append(actionMapping);
            value.append(servletMapping.substring(1));
        } else if (servletMapping.endsWith("/*")) {
            value.append(servletMapping.substring(0, servletMapping.length() - 2));
            value.append(actionMapping);
        } else if (servletMapping.equals("/")) {
            value.append(actionMapping);
        }
        if (queryString != null) {
            value.append(queryString);
        }
    } else // Otherwise, assume extension mapping is in use and extension is
    // already included in the action property
    {
        if (!action.startsWith("/")) {
            value.append("/");
        }
        value.append(action);
    }
    return value.toString();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ModuleConfig(org.apache.struts.config.ModuleConfig)

Example 12 with ModuleConfig

use of org.apache.struts.config.ModuleConfig in project sonarqube by SonarSource.

the class TagUtils method computeURLWithCharEncoding.

/**
     * Compute a hyperlink URL based on the <code>forward</code>,
     * <code>href</code>, <code>action</code> or <code>page</code> parameter
     * that is not null. The returned URL will have already been passed to
     * <code>response.encodeURL()</code> for adding a session identifier.
     *
     * @param pageContext      PageContext for the tag making this call
     * @param forward          Logical forward name for which to look up the
     *                         context-relative URI (if specified)
     * @param href             URL to be utilized unmodified (if specified)
     * @param page             Module-relative page for which a URL should be
     *                         created (if specified)
     * @param action           Logical action name for which to look up the
     *                         context-relative URI (if specified)
     * @param params           Map of parameters to be dynamically included
     *                         (if any)
     * @param anchor           Anchor to be dynamically included (if any)
     * @param redirect         Is this URL for a <code>response.sendRedirect()</code>?
     * @param encodeSeparator  This is only checked if redirect is set to
     *                         false (never encoded for a redirect).  If true,
     *                         query string parameter separators are encoded
     *                         as &gt;amp;, else &amp; is used.
     * @param useLocalEncoding If set to true, urlencoding is done on the
     *                         bytes of character encoding from
     *                         ServletResponse#getCharacterEncoding. Use UTF-8
     *                         otherwise.
     * @return URL with session identifier
     * @throws java.net.MalformedURLException if a URL cannot be created for
     *                                        the specified parameters
     */
public String computeURLWithCharEncoding(PageContext pageContext, String forward, String href, String page, String action, String module, Map params, String anchor, boolean redirect, boolean encodeSeparator, boolean useLocalEncoding) throws MalformedURLException {
    String charEncoding = "UTF-8";
    if (useLocalEncoding) {
        charEncoding = pageContext.getResponse().getCharacterEncoding();
    }
    // TODO All the computeURL() methods need refactoring!
    // Validate that exactly one specifier was included
    int n = 0;
    if (forward != null) {
        n++;
    }
    if (href != null) {
        n++;
    }
    if (page != null) {
        n++;
    }
    if (action != null) {
        n++;
    }
    if (n != 1) {
        throw new MalformedURLException(messages.getMessage("computeURL.specifier"));
    }
    // Look up the module configuration for this request
    ModuleConfig moduleConfig = getModuleConfig(module, pageContext);
    // Calculate the appropriate URL
    StringBuffer url = new StringBuffer();
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    if (forward != null) {
        ForwardConfig forwardConfig = moduleConfig.findForwardConfig(forward);
        if (forwardConfig == null) {
            throw new MalformedURLException(messages.getMessage("computeURL.forward", forward));
        }
        if (forwardConfig.getPath().startsWith("/")) {
            url.append(request.getContextPath());
            url.append(RequestUtils.forwardURL(request, forwardConfig, moduleConfig));
        } else {
            url.append(forwardConfig.getPath());
        }
    } else if (href != null) {
        url.append(href);
    } else if (action != null) {
        ActionServlet servlet = (ActionServlet) pageContext.getServletContext().getAttribute(Globals.ACTION_SERVLET_KEY);
        String actionIdPath = RequestUtils.actionIdURL(action, moduleConfig, servlet);
        if (actionIdPath != null) {
            action = actionIdPath;
            url.append(request.getContextPath());
            url.append(actionIdPath);
        } else {
            url.append(instance.getActionMappingURL(action, module, pageContext, false));
        }
    } else /* if (page != null) */
    {
        url.append(request.getContextPath());
        url.append(this.pageURL(request, page, moduleConfig));
    }
    // Add anchor if requested (replacing any existing anchor)
    if (anchor != null) {
        String temp = url.toString();
        int hash = temp.indexOf('#');
        if (hash >= 0) {
            url.setLength(hash);
        }
        url.append('#');
        url.append(this.encodeURL(anchor, charEncoding));
    }
    // Add dynamic parameters if requested
    if ((params != null) && (params.size() > 0)) {
        // Save any existing anchor
        String temp = url.toString();
        int hash = temp.indexOf('#');
        if (hash >= 0) {
            anchor = temp.substring(hash + 1);
            url.setLength(hash);
            temp = url.toString();
        } else {
            anchor = null;
        }
        // Define the parameter separator
        String separator = null;
        if (redirect) {
            separator = "&";
        } else if (encodeSeparator) {
            separator = "&amp;";
        } else {
            separator = "&";
        }
        // Add the required request parameters
        boolean question = temp.indexOf('?') >= 0;
        Iterator keys = params.keySet().iterator();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            Object value = params.get(key);
            if (value == null) {
                if (!question) {
                    url.append('?');
                    question = true;
                } else {
                    url.append(separator);
                }
                url.append(this.encodeURL(key, charEncoding));
                // Interpret null as "no value"
                url.append('=');
            } else if (value instanceof String) {
                if (!question) {
                    url.append('?');
                    question = true;
                } else {
                    url.append(separator);
                }
                url.append(this.encodeURL(key, charEncoding));
                url.append('=');
                url.append(this.encodeURL((String) value, charEncoding));
            } else if (value instanceof String[]) {
                String[] values = (String[]) value;
                for (int i = 0; i < values.length; i++) {
                    if (!question) {
                        url.append('?');
                        question = true;
                    } else {
                        url.append(separator);
                    }
                    url.append(this.encodeURL(key, charEncoding));
                    url.append('=');
                    url.append(this.encodeURL(values[i], charEncoding));
                }
            } else /* Convert other objects to a string */
            {
                if (!question) {
                    url.append('?');
                    question = true;
                } else {
                    url.append(separator);
                }
                url.append(this.encodeURL(key, charEncoding));
                url.append('=');
                url.append(this.encodeURL(value.toString(), charEncoding));
            }
        }
        // Re-add the saved anchor (if any)
        if (anchor != null) {
            url.append('#');
            url.append(this.encodeURL(anchor, charEncoding));
        }
    }
    // but only if url is not an external URL
    if ((href == null) && (pageContext.getSession() != null)) {
        HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
        if (redirect) {
            return (response.encodeRedirectURL(url.toString()));
        }
        return (response.encodeURL(url.toString()));
    }
    return (url.toString());
}
Also used : MalformedURLException(java.net.MalformedURLException) ModuleConfig(org.apache.struts.config.ModuleConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) ActionServlet(org.apache.struts.action.ActionServlet) HttpServletRequest(javax.servlet.http.HttpServletRequest) Iterator(java.util.Iterator) ForwardConfig(org.apache.struts.config.ForwardConfig)

Example 13 with ModuleConfig

use of org.apache.struts.config.ModuleConfig in project sonarqube by SonarSource.

the class ModuleUtils method selectModule.

/**
     * Select the module to which the specified request belongs, and add
     * corresponding request attributes to this request.
     *
     * @param prefix  The module prefix of the desired module
     * @param request The servlet request we are processing
     * @param context The ServletContext for this web application
     */
public void selectModule(String prefix, HttpServletRequest request, ServletContext context) {
    // Expose the resources for this module
    ModuleConfig config = getModuleConfig(prefix, context);
    if (config != null) {
        request.setAttribute(Globals.MODULE_KEY, config);
        MessageResourcesConfig[] mrConfig = config.findMessageResourcesConfigs();
        for (int i = 0; i < mrConfig.length; i++) {
            String key = mrConfig[i].getKey();
            MessageResources resources = (MessageResources) context.getAttribute(key + prefix);
            if (resources != null) {
                request.setAttribute(key, resources);
            } else {
                request.removeAttribute(key);
            }
        }
    } else {
        request.removeAttribute(Globals.MODULE_KEY);
    }
}
Also used : ModuleConfig(org.apache.struts.config.ModuleConfig) MessageResourcesConfig(org.apache.struts.config.MessageResourcesConfig)

Example 14 with ModuleConfig

use of org.apache.struts.config.ModuleConfig in project sonarqube by SonarSource.

the class ImageTag method src.

// ------------------------------------------------------ Protected Methods
/**
     * Return the base source URL that will be rendered in the
     * <code>src</code> property for this generated element, or
     * <code>null</code> if there is no such URL.
     *
     * @throws JspException if an error occurs
     */
protected String src() throws JspException {
    // Deal with a direct context-relative page that has been specified
    if (this.page != null) {
        if ((this.src != null) || (this.srcKey != null) || (this.pageKey != null)) {
            JspException e = new JspException(messages.getMessage("imgTag.src"));
            TagUtils.getInstance().saveException(pageContext, e);
            throw e;
        }
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        ModuleConfig config = ModuleUtils.getInstance().getModuleConfig(this.module, request, pageContext.getServletContext());
        String pageValue = this.page;
        if (config != null) {
            pageValue = TagUtils.getInstance().pageURL(request, this.page, config);
        }
        return (request.getContextPath() + pageValue);
    }
    // Deal with an indirect context-relative page that has been specified
    if (this.pageKey != null) {
        if ((this.src != null) || (this.srcKey != null)) {
            JspException e = new JspException(messages.getMessage("imgTag.src"));
            TagUtils.getInstance().saveException(pageContext, e);
            throw e;
        }
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        ModuleConfig config = ModuleUtils.getInstance().getModuleConfig(this.module, request, pageContext.getServletContext());
        String pageValue = TagUtils.getInstance().message(pageContext, getBundle(), getLocale(), this.pageKey);
        if (config != null) {
            pageValue = TagUtils.getInstance().pageURL(request, pageValue, config);
        }
        return (request.getContextPath() + pageValue);
    }
    // Deal with an absolute source that has been specified
    if (this.src != null) {
        if (this.srcKey != null) {
            JspException e = new JspException(messages.getMessage("imgTag.src"));
            TagUtils.getInstance().saveException(pageContext, e);
            throw e;
        }
        return (this.src);
    }
    // Deal with an indirect source that has been specified
    if (this.srcKey == null) {
        JspException e = new JspException(messages.getMessage("imgTag.src"));
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    }
    return TagUtils.getInstance().message(pageContext, getBundle(), getLocale(), this.srcKey);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JspException(javax.servlet.jsp.JspException) ModuleConfig(org.apache.struts.config.ModuleConfig)

Example 15 with ModuleConfig

use of org.apache.struts.config.ModuleConfig 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)

Aggregations

ModuleConfig (org.apache.struts.config.ModuleConfig)34 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 JspException (javax.servlet.jsp.JspException)5 Iterator (java.util.Iterator)4 ActionConfig (org.apache.struts.config.ActionConfig)4 ForwardConfig (org.apache.struts.config.ForwardConfig)4 MessageResources (org.apache.struts.util.MessageResources)4 ArrayList (java.util.ArrayList)3 Enumeration (java.util.Enumeration)2 List (java.util.List)2 Locale (java.util.Locale)2 ServletException (javax.servlet.ServletException)2 ActionServlet (org.apache.struts.action.ActionServlet)2 ServletActionContext (org.apache.struts.chain.contexts.ServletActionContext)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Map (java.util.Map)1 ServletContext (javax.servlet.ServletContext)1