Search in sources :

Example 51 with ModuleConfig

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

the class TestRequestUtils method testSelectApplication2a.

// Map to the default module -- include
public void testSelectApplication2a() {
    request.setPathElements("/myapp", "/2/noform.do", null, null);
    request.setAttribute(RequestProcessor.INCLUDE_SERVLET_PATH, "/noform.do");
    ModuleUtils.getInstance().selectModule(request, context);
    ModuleConfig moduleConfig = (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
    assertNotNull("Selected an application", moduleConfig);
    assertEquals("Selected correct application", "", moduleConfig.getPrefix());
// FIXME - check application resources?
}
Also used : ModuleConfig(org.apache.struts.config.ModuleConfig)

Example 52 with ModuleConfig

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

the class TestTagUtils method testModuleConfig_getModuleConfig_PageContext.

// ----public ModuleConfig getModuleConfig(PageContext pageContext)
public void testModuleConfig_getModuleConfig_PageContext() {
    MockServletConfig mockServletConfig = new MockServletConfig();
    ModuleConfig moduleConfig = new ModuleConfigImpl("");
    MockServletContext mockServletContext = new MockServletContext();
    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
    mockServletConfig.setServletContext(mockServletContext);
    MockPageContext mockPageContext = new MockPageContext(mockServletConfig, mockHttpServletRequest, mockHttpServletResponse);
    ModuleConfig foundModuleConfig = null;
    try {
        foundModuleConfig = tagutils.getModuleConfig(mockPageContext);
        fail("Expected ModuleConfig to not be found");
    } catch (NullPointerException ignore) {
    // expected result
    }
    mockHttpServletRequest.setAttribute(Globals.MODULE_KEY, moduleConfig);
    mockPageContext.getServletContext().setAttribute(Globals.MODULE_KEY, mockPageContext);
    foundModuleConfig = tagutils.getModuleConfig(mockPageContext);
    assertNotNull(foundModuleConfig);
}
Also used : MockHttpServletRequest(org.apache.struts.mock.MockHttpServletRequest) MockServletConfig(org.apache.struts.mock.MockServletConfig) ModuleConfig(org.apache.struts.config.ModuleConfig) MockPageContext(org.apache.struts.mock.MockPageContext) ModuleConfigImpl(org.apache.struts.config.impl.ModuleConfigImpl) MockServletContext(org.apache.struts.mock.MockServletContext) MockHttpServletResponse(org.apache.struts.mock.MockHttpServletResponse)

Example 53 with ModuleConfig

use of org.apache.struts.config.ModuleConfig in project sonar-java by SonarSource.

the class Resources method getMessageResources.

/**
 * Retrieve <code>MessageResources</code> for the module and bundle.
 *
 * @param application the servlet context
 * @param request     the servlet request
 * @param bundle      the bundle key
 */
public static MessageResources getMessageResources(ServletContext application, HttpServletRequest request, String bundle) {
    if (bundle == null) {
        bundle = Globals.MESSAGES_KEY;
    }
    MessageResources resources = (MessageResources) request.getAttribute(bundle);
    if (resources == null) {
        ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, application);
        resources = (MessageResources) application.getAttribute(bundle + moduleConfig.getPrefix());
    }
    if (resources == null) {
        resources = (MessageResources) application.getAttribute(bundle);
    }
    if (resources == null) {
        throw new NullPointerException("No message resources found for bundle: " + bundle);
    }
    return resources;
}
Also used : MessageResources(org.apache.struts.util.MessageResources) ModuleConfig(org.apache.struts.config.ModuleConfig)

Example 54 with ModuleConfig

use of org.apache.struts.config.ModuleConfig in project sonar-java 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 55 with ModuleConfig

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

Aggregations

ModuleConfig (org.apache.struts.config.ModuleConfig)73 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 JspException (javax.servlet.jsp.JspException)10 Iterator (java.util.Iterator)8 ActionConfig (org.apache.struts.config.ActionConfig)8 ForwardConfig (org.apache.struts.config.ForwardConfig)8 MessageResources (org.apache.struts.util.MessageResources)8 ArrayList (java.util.ArrayList)6 ServletException (javax.servlet.ServletException)6 Enumeration (java.util.Enumeration)4 List (java.util.List)4 Locale (java.util.Locale)4 ActionServlet (org.apache.struts.action.ActionServlet)4 ServletActionContext (org.apache.struts.chain.contexts.ServletActionContext)4 UnavailableException (javax.servlet.UnavailableException)3 Digester (org.apache.commons.digester.Digester)3 ModuleConfigFactory (org.apache.struts.config.ModuleConfigFactory)3 MultipartRequestHandler (org.apache.struts.upload.MultipartRequestHandler)3 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2