Search in sources :

Example 6 with ActionServlet

use of org.apache.struts.action.ActionServlet in project sonarqube by SonarSource.

the class CreateAction method getAction.

/* :TODO The Action class' dependency on having its "servlet" property set
     * requires this API-dependent subclass of AbstractCreateAction.
     */
protected synchronized Action getAction(ActionContext context, String type, ActionConfig actionConfig) throws Exception {
    ModuleConfig moduleConfig = actionConfig.getModuleConfig();
    String actionsKey = Constants.ACTIONS_KEY + moduleConfig.getPrefix();
    Map actions = (Map) context.getApplicationScope().get(actionsKey);
    if (actions == null) {
        actions = new HashMap();
        context.getApplicationScope().put(actionsKey, actions);
    }
    Action action = null;
    synchronized (actions) {
        action = (Action) actions.get(type);
        if (action == null) {
            action = createAction(context, type);
            actions.put(type, action);
        }
    }
    if (action.getServlet() == null) {
        ServletActionContext saContext = (ServletActionContext) context;
        ActionServlet actionServlet = saContext.getActionServlet();
        action.setServlet(actionServlet);
    }
    return (action);
}
Also used : Action(org.apache.struts.action.Action) HashMap(java.util.HashMap) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) ModuleConfig(org.apache.struts.config.ModuleConfig) Map(java.util.Map) HashMap(java.util.HashMap) ActionServlet(org.apache.struts.action.ActionServlet)

Example 7 with ActionServlet

use of org.apache.struts.action.ActionServlet in project sonarqube by SonarSource.

the class PerformForward method perform.

// ------------------------------------------------------- Protected Methods
/**
     * <p>Perform the appropriate processing on the specified
     * <code>ForwardConfig</code>.</p>
     *
     * @param context       The context for this request
     * @param forwardConfig The forward to be performed
     */
protected void perform(ActionContext context, ForwardConfig forwardConfig) throws Exception {
    ServletActionContext sacontext = (ServletActionContext) context;
    String uri = forwardConfig.getPath();
    if (uri == null) {
        ActionServlet servlet = sacontext.getActionServlet();
        MessageResources resources = servlet.getInternal();
        throw new IllegalArgumentException(resources.getMessage("forwardPathNull"));
    }
    HttpServletRequest request = sacontext.getRequest();
    ServletContext servletContext = sacontext.getContext();
    HttpServletResponse response = sacontext.getResponse();
    // If the forward can be unaliased into an action, then use the path of the action
    String actionIdPath = RequestUtils.actionIdURL(forwardConfig, sacontext.getRequest(), sacontext.getActionServlet());
    if (actionIdPath != null) {
        uri = actionIdPath;
        ForwardConfig actionIdForwardConfig = new ForwardConfig(forwardConfig);
        actionIdForwardConfig.setPath(actionIdPath);
        forwardConfig = actionIdForwardConfig;
    }
    if (uri.startsWith("/")) {
        uri = resolveModuleRelativePath(forwardConfig, servletContext, request);
    }
    if (response.isCommitted() && !forwardConfig.getRedirect()) {
        handleAsInclude(uri, servletContext, request, response);
    } else if (forwardConfig.getRedirect()) {
        handleAsRedirect(uri, request, response);
    } else {
        handleAsForward(uri, servletContext, request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MessageResources(org.apache.struts.util.MessageResources) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) ForwardConfig(org.apache.struts.config.ForwardConfig) ActionServlet(org.apache.struts.action.ActionServlet)

Example 8 with ActionServlet

use of org.apache.struts.action.ActionServlet in project sonarqube by SonarSource.

the class FormBeanConfig method createActionForm.

/**
     * <p>Create and return an <code>ActionForm</code> instance appropriate to
     * the information in this <code>FormBeanConfig</code>.</p>
     * <p><b>NOTE:</b> If the given <code>ActionContext</code> is not of type
     * <code>ServletActionContext</code> (or a subclass), then the form which
     * is returned will have a null <code>servlet</code> property.  Some of
     * the subclasses of <code>ActionForm</code> included in Struts will later
     * throw a <code>NullPointerException</code> in this case. </p> <p>TODO:
     * Find a way to control this direct dependency on the Servlet API.</p>
     *
     * @param context The ActionContext.
     * @return ActionForm instance
     * @throws IllegalAccessException if the Class or the appropriate
     *                                constructor is not accessible
     * @throws InstantiationException if this Class represents an abstract
     *                                class, an array class, a primitive type,
     *                                or void; or if instantiation fails for
     *                                some other reason
     */
public ActionForm createActionForm(ActionContext context) throws IllegalAccessException, InstantiationException {
    ActionServlet actionServlet = null;
    if (context instanceof ServletActionContext) {
        ServletActionContext saContext = (ServletActionContext) context;
        actionServlet = saContext.getActionServlet();
    }
    return createActionForm(actionServlet);
}
Also used : ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) ActionServlet(org.apache.struts.action.ActionServlet)

Example 9 with ActionServlet

use of org.apache.struts.action.ActionServlet 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 10 with ActionServlet

use of org.apache.struts.action.ActionServlet in project sonar-java by SonarSource.

the class PerformForward method perform.

// ------------------------------------------------------- Protected Methods
/**
 * <p>Perform the appropriate processing on the specified
 * <code>ForwardConfig</code>.</p>
 *
 * @param context       The context for this request
 * @param forwardConfig The forward to be performed
 */
protected void perform(ActionContext context, ForwardConfig forwardConfig) throws Exception {
    ServletActionContext sacontext = (ServletActionContext) context;
    String uri = forwardConfig.getPath();
    if (uri == null) {
        ActionServlet servlet = sacontext.getActionServlet();
        MessageResources resources = servlet.getInternal();
        throw new IllegalArgumentException(resources.getMessage("forwardPathNull"));
    }
    HttpServletRequest request = sacontext.getRequest();
    ServletContext servletContext = sacontext.getContext();
    HttpServletResponse response = sacontext.getResponse();
    // If the forward can be unaliased into an action, then use the path of the action
    String actionIdPath = RequestUtils.actionIdURL(forwardConfig, sacontext.getRequest(), sacontext.getActionServlet());
    if (actionIdPath != null) {
        uri = actionIdPath;
        ForwardConfig actionIdForwardConfig = new ForwardConfig(forwardConfig);
        actionIdForwardConfig.setPath(actionIdPath);
        forwardConfig = actionIdForwardConfig;
    }
    if (uri.startsWith("/")) {
        uri = resolveModuleRelativePath(forwardConfig, servletContext, request);
    }
    if (response.isCommitted() && !forwardConfig.getRedirect()) {
        handleAsInclude(uri, servletContext, request, response);
    } else if (forwardConfig.getRedirect()) {
        handleAsRedirect(uri, request, response);
    } else {
        handleAsForward(uri, servletContext, request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MessageResources(org.apache.struts.util.MessageResources) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) ForwardConfig(org.apache.struts.config.ForwardConfig) ActionServlet(org.apache.struts.action.ActionServlet)

Aggregations

ActionServlet (org.apache.struts.action.ActionServlet)10 ServletActionContext (org.apache.struts.chain.contexts.ServletActionContext)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 ForwardConfig (org.apache.struts.config.ForwardConfig)4 ModuleConfig (org.apache.struts.config.ModuleConfig)4 MessageResources (org.apache.struts.util.MessageResources)4 MalformedURLException (java.net.MalformedURLException)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 ServletContext (javax.servlet.ServletContext)2 Action (org.apache.struts.action.Action)2