Search in sources :

Example 11 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class ForwardTag method doRedirect.

/**
     * Redirect to the given path converting exceptions to JspException.
     *
     * @param path The path to redirect to.
     * @throws JspException
     * @since Struts 1.2
     */
protected void doRedirect(String path) throws JspException {
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
    try {
        if (path.startsWith("/")) {
            path = request.getContextPath() + path;
        }
        response.sendRedirect(response.encodeRedirectURL(path));
    } catch (Exception e) {
        TagUtils.getInstance().saveException(pageContext, e);
        throw new JspException(messages.getMessage("forward.redirect", name, e.toString()));
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JspException(javax.servlet.jsp.JspException) HttpServletResponse(javax.servlet.http.HttpServletResponse) JspException(javax.servlet.jsp.JspException)

Example 12 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class MatchTag method condition.

/**
     * Evaluate the condition that is being tested by this particular tag, and
     * return <code>true</code> if the nested body content of this tag should
     * be evaluated, or <code>false</code> if it should be skipped. This
     * method must be implemented by concrete subclasses.
     *
     * @param desired Desired value for a true result
     * @throws JspException if a JSP exception occurs
     */
protected boolean condition(boolean desired) throws JspException {
    // Acquire the specified variable
    String variable = null;
    if (cookie != null) {
        Cookie[] cookies = ((HttpServletRequest) pageContext.getRequest()).getCookies();
        if (cookies == null) {
            cookies = new Cookie[0];
        }
        for (int i = 0; i < cookies.length; i++) {
            if (cookie.equals(cookies[i].getName())) {
                variable = cookies[i].getValue();
                break;
            }
        }
    } else if (header != null) {
        variable = ((HttpServletRequest) pageContext.getRequest()).getHeader(header);
    } else if (name != null) {
        Object value = TagUtils.getInstance().lookup(pageContext, name, property, scope);
        if (value != null) {
            variable = value.toString();
        }
    } else if (parameter != null) {
        variable = pageContext.getRequest().getParameter(parameter);
    } else {
        JspException e = new JspException(messages.getMessage("logic.selector"));
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    }
    if (variable == null) {
        JspException e = new JspException(messages.getMessage("logic.variable", value));
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    }
    // Perform the comparison requested by the location attribute
    boolean matched = false;
    if (location == null) {
        matched = (variable.indexOf(value) >= 0);
    } else if (location.equals("start")) {
        matched = variable.startsWith(value);
    } else if (location.equals("end")) {
        matched = variable.endsWith(value);
    } else {
        JspException e = new JspException(messages.getMessage("logic.location", location));
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    }
    // Return the final result
    return (matched == desired);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) JspException(javax.servlet.jsp.JspException)

Example 13 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class PresentTag method condition.

/**
     * Evaluate the condition that is being tested by this particular tag, and
     * return <code>true</code> if the nested body content of this tag should
     * be evaluated, or <code>false</code> if it should be skipped. This
     * method must be implemented by concrete subclasses.
     *
     * @param desired Desired outcome for a true result
     * @throws JspException if a JSP exception occurs
     */
protected boolean condition(boolean desired) throws JspException {
    // Evaluate the presence of the specified value
    boolean present = false;
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    if (cookie != null) {
        present = this.isCookiePresent(request);
    } else if (header != null) {
        String value = request.getHeader(header);
        present = (value != null);
    } else if (name != null) {
        present = this.isBeanPresent();
    } else if (parameter != null) {
        String value = request.getParameter(parameter);
        present = (value != null);
    } else if (role != null) {
        StringTokenizer st = new StringTokenizer(role, ROLE_DELIMITER, false);
        while (!present && st.hasMoreTokens()) {
            present = request.isUserInRole(st.nextToken());
        }
    } else if (user != null) {
        Principal principal = request.getUserPrincipal();
        present = (principal != null) && user.equals(principal.getName());
    } else {
        JspException e = new JspException(messages.getMessage("logic.selector"));
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    }
    return (present == desired);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JspException(javax.servlet.jsp.JspException) StringTokenizer(java.util.StringTokenizer) Principal(java.security.Principal)

Example 14 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class RedirectTag method generateRedirectURL.

/**
     * Calculate the url to redirect to.
     *
     * @throws JspException
     * @since Struts 1.2
     */
protected String generateRedirectURL() throws JspException {
    Map params = TagUtils.getInstance().computeParameters(pageContext, paramId, paramName, paramProperty, paramScope, name, property, scope, transaction);
    String url = null;
    try {
        url = TagUtils.getInstance().computeURLWithCharEncoding(pageContext, forward, href, page, action, module, params, anchor, true, useLocalEncoding);
    } catch (MalformedURLException e) {
        TagUtils.getInstance().saveException(pageContext, e);
        throw new JspException(messages.getMessage("redirect.url", e.toString()));
    }
    return url;
}
Also used : JspException(javax.servlet.jsp.JspException) MalformedURLException(java.net.MalformedURLException) Map(java.util.Map)

Example 15 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class JavascriptValidatorTag method createDynamicJavascript.

/**
     * Generates the dynamic JavaScript for the form.
     *
     * @param config
     * @param resources
     * @param locale
     * @param form
     */
private String createDynamicJavascript(ModuleConfig config, ValidatorResources resources, Locale locale, Form form) throws JspException {
    StringBuffer results = new StringBuffer();
    MessageResources messages = TagUtils.getInstance().retrieveMessageResources(pageContext, bundle, true);
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    ServletContext application = pageContext.getServletContext();
    List actions = this.createActionList(resources, form);
    final String methods = this.createMethods(actions, this.stopOnError(config));
    String formName = form.getName();
    jsFormName = formName;
    if (jsFormName.charAt(0) == '/') {
        String mappingName = TagUtils.getInstance().getActionMappingName(jsFormName);
        ActionMapping mapping = (ActionMapping) config.findActionConfig(mappingName);
        if (mapping == null) {
            JspException e = new JspException(messages.getMessage("formTag.mapping", mappingName));
            pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
            throw e;
        }
        jsFormName = mapping.getAttribute();
    }
    results.append(this.getJavascriptBegin(methods));
    for (Iterator i = actions.iterator(); i.hasNext(); ) {
        ValidatorAction va = (ValidatorAction) i.next();
        int jscriptVar = 0;
        String functionName = null;
        if ((va.getJsFunctionName() != null) && (va.getJsFunctionName().length() > 0)) {
            functionName = va.getJsFunctionName();
        } else {
            functionName = va.getName();
        }
        results.append("    function " + jsFormName + "_" + functionName + " () { \n");
        for (Iterator x = form.getFields().iterator(); x.hasNext(); ) {
            Field field = (Field) x.next();
            // retrieve from scope?))
            if (field.isIndexed() || (field.getPage() != page) || !field.isDependency(va.getName())) {
                continue;
            }
            String message = Resources.getMessage(application, request, messages, locale, va, field);
            message = (message != null) ? message : "";
            // prefix variable with 'a' to make it a legal identifier
            results.append("     this.a" + jscriptVar++ + " = new Array(\"" + field.getKey() + "\", \"" + escapeQuotes(message) + "\", ");
            results.append("new Function (\"varName\", \"");
            Map vars = field.getVars();
            // Loop through the field's variables.
            Iterator varsIterator = vars.keySet().iterator();
            while (varsIterator.hasNext()) {
                String varName = (String) varsIterator.next();
                Var var = (Var) vars.get(varName);
                String varValue = Resources.getVarValue(var, application, request, false);
                String jsType = var.getJsType();
                // fieldValue
                if (varName.startsWith("field")) {
                    continue;
                }
                String varValueEscaped = escapeJavascript(varValue);
                if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=" + varValueEscaped + "; ");
                } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                // So everyone using the latest format doesn't need to
                // change their xml files immediately.
                } else if ("mask".equalsIgnoreCase(varName)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                }
            }
            results.append(" return this[varName];\"));\n");
        }
        results.append("    } \n\n");
    }
    return results.toString();
}
Also used : ValidatorAction(org.apache.commons.validator.ValidatorAction) MessageResources(org.apache.struts.util.MessageResources) Var(org.apache.commons.validator.Var) HttpServletRequest(javax.servlet.http.HttpServletRequest) JspException(javax.servlet.jsp.JspException) Field(org.apache.commons.validator.Field) ActionMapping(org.apache.struts.action.ActionMapping) Iterator(java.util.Iterator) ServletContext(javax.servlet.ServletContext) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Aggregations

JspException (javax.servlet.jsp.JspException)151 IOException (java.io.IOException)34 Map (java.util.Map)30 HashMap (java.util.HashMap)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)21 Iterator (java.util.Iterator)20 JspWriter (javax.servlet.jsp.JspWriter)20 ActionMessages (org.apache.struts.action.ActionMessages)15 MockFormBean (org.apache.struts.mock.MockFormBean)15 InvocationTargetException (java.lang.reflect.InvocationTargetException)11 ActionMessage (org.apache.struts.action.ActionMessage)11 ArrayList (java.util.ArrayList)7 Collection (java.util.Collection)7 List (java.util.List)7 PageExpiredException (org.mifos.framework.exceptions.PageExpiredException)6 MalformedURLException (java.net.MalformedURLException)5 Locale (java.util.Locale)5 ModuleConfig (org.apache.struts.config.ModuleConfig)5 UserContext (org.mifos.security.util.UserContext)5 JspTag (javax.servlet.jsp.tagext.JspTag)4