Search in sources :

Example 16 with ServletActionContext

use of org.apache.struts.chain.contexts.ServletActionContext in project sonarqube by SonarSource.

the class SelectAction method getPath.

// ------------------------------------------------------- Protected Methods
protected String getPath(ActionContext context) {
    ServletActionContext saContext = (ServletActionContext) context;
    HttpServletRequest request = saContext.getRequest();
    String path = null;
    boolean extension = false;
    // For prefix matching, match on the path info
    path = (String) request.getAttribute(Constants.INCLUDE_PATH_INFO);
    if ((path == null) || (path.length() == 0)) {
        path = request.getPathInfo();
    }
    // For extension matching, match on the servlet path
    if ((path == null) || (path.length() == 0)) {
        path = (String) request.getAttribute(Constants.INCLUDE_SERVLET_PATH);
        if ((path == null) || (path.length() == 0)) {
            path = request.getServletPath();
        }
        if ((path == null) || (path.length() == 0)) {
            throw new IllegalArgumentException("No path information in request");
        }
        extension = true;
    }
    // Strip the module prefix and extension (if any)
    ModuleConfig moduleConfig = saContext.getModuleConfig();
    String prefix = moduleConfig.getPrefix();
    if (!path.startsWith(prefix)) {
        throw new IllegalArgumentException("Path does not start with '" + prefix + "'");
    }
    path = path.substring(prefix.length());
    if (extension) {
        int slash = path.lastIndexOf("/");
        int period = path.lastIndexOf(".");
        if ((period >= 0) && (period > slash)) {
            path = path.substring(0, period);
        }
    }
    return (path);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) ModuleConfig(org.apache.struts.config.ModuleConfig)

Example 17 with ServletActionContext

use of org.apache.struts.chain.contexts.ServletActionContext in project sonarqube by SonarSource.

the class SelectLocale method getLocale.

// ------------------------------------------------------- Protected Methods
/**
     * <p>Return the <code>Locale</code> to be used for this request.</p>
     *
     * @param context The <code>Context</code> for this request
     */
protected Locale getLocale(ActionContext context) {
    ServletActionContext saContext = (ServletActionContext) context;
    // Has a Locale already been selected?
    HttpSession session = saContext.getRequest().getSession();
    Locale locale = (Locale) session.getAttribute(Globals.LOCALE_KEY);
    if (locale != null) {
        return (locale);
    }
    // Select and cache the Locale to be used
    locale = saContext.getRequest().getLocale();
    if (locale == null) {
        locale = Locale.getDefault();
    }
    session.setAttribute(Globals.LOCALE_KEY, locale);
    return (locale);
}
Also used : AbstractSelectLocale(org.apache.struts.chain.commands.AbstractSelectLocale) Locale(java.util.Locale) HttpSession(javax.servlet.http.HttpSession) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext)

Example 18 with ServletActionContext

use of org.apache.struts.chain.contexts.ServletActionContext in project sonarqube by SonarSource.

the class SelectModule method getPrefix.

// ------------------------------------------------------- Protected Methods
protected String getPrefix(ActionContext context) {
    // Identify the URI from which we will match a module prefix
    ServletActionContext sacontext = (ServletActionContext) context;
    HttpServletRequest request = sacontext.getRequest();
    String uri = (String) request.getAttribute(Constants.INCLUDE_SERVLET_PATH);
    if (uri == null) {
        uri = request.getServletPath();
    }
    if (uri == null) {
        throw new IllegalArgumentException("No path information in request");
    }
    // Identify the module prefix for the current module
    // Initialize to default prefix
    String prefix = "";
    String[] prefixes = (String[]) sacontext.getApplicationScope().get(Globals.MODULE_PREFIXES_KEY);
    int lastSlash = 0;
    while (prefix.equals("") && ((lastSlash = uri.lastIndexOf("/")) > 0)) {
        uri = uri.substring(0, lastSlash);
        for (int i = 0; i < prefixes.length; i++) {
            if (uri.equals(prefixes[i])) {
                prefix = prefixes[i];
                break;
            }
        }
    }
    return (prefix);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext)

Example 19 with ServletActionContext

use of org.apache.struts.chain.contexts.ServletActionContext in project sonarqube by SonarSource.

the class TestWrappingLookupCommand method testWrapContextSubclass.

public void testWrapContextSubclass() throws Exception {
    WrappingLookupCommand command = new WrappingLookupCommand();
    command.setWrapperClassName(ServletActionContext.class.getName());
    Context testContext = new ServletWebContext();
    Context wrapped = command.getContext(testContext);
    assertNotNull(wrapped);
    assertTrue(wrapped instanceof ServletActionContext);
}
Also used : ServletWebContext(org.apache.commons.chain.web.servlet.ServletWebContext) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) Context(org.apache.commons.chain.Context) ServletWebContext(org.apache.commons.chain.web.servlet.ServletWebContext) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext)

Example 20 with ServletActionContext

use of org.apache.struts.chain.contexts.ServletActionContext in project sonarqube by SonarSource.

the class TestAuthorizeAction method setUp.

/* setUp method for test case */
protected void setUp() throws Exception {
    this.request = new MockHttpServletRequest();
    this.principal = new MockPrincipal("Mr. Macri", new String[] { "administrator" });
    this.request.setUserPrincipal(principal);
    MockServletConfig servletConfig = new MockServletConfig();
    MockServletContext servletContext = new MockServletContext();
    MockActionServlet servlet = new MockActionServlet(servletContext, servletConfig);
    servlet.initInternal();
    this.saContext = new ServletActionContext(servletContext, request, new MockHttpServletResponse());
    this.saContext.setActionServlet(servlet);
    this.command = new AuthorizeAction();
}
Also used : MockActionServlet(org.apache.struts.mock.MockActionServlet) MockHttpServletRequest(org.apache.struts.mock.MockHttpServletRequest) MockPrincipal(org.apache.struts.mock.MockPrincipal) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) MockServletConfig(org.apache.struts.mock.MockServletConfig) MockServletContext(org.apache.struts.mock.MockServletContext) MockHttpServletResponse(org.apache.struts.mock.MockHttpServletResponse)

Aggregations

ServletActionContext (org.apache.struts.chain.contexts.ServletActionContext)22 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 ActionServlet (org.apache.struts.action.ActionServlet)4 MockActionServlet (org.apache.struts.mock.MockActionServlet)3 MockHttpServletRequest (org.apache.struts.mock.MockHttpServletRequest)3 MockHttpServletResponse (org.apache.struts.mock.MockHttpServletResponse)3 MockServletConfig (org.apache.struts.mock.MockServletConfig)3 MockServletContext (org.apache.struts.mock.MockServletContext)3 Map (java.util.Map)2 ActionForm (org.apache.struts.action.ActionForm)2 ActionMapping (org.apache.struts.action.ActionMapping)2 ModuleConfig (org.apache.struts.config.ModuleConfig)2 MockPrincipal (org.apache.struts.mock.MockPrincipal)2 MessageResources (org.apache.struts.util.MessageResources)2 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletContext (javax.servlet.ServletContext)1 HttpSession (javax.servlet.http.HttpSession)1