Search in sources :

Example 1 with ActionMapping

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

the class ValidateActionForm method validate.

// ------------------------------------------------------- Protected Methods
/**
     * <p>Call the <code>validate()</code> method of the specified form bean,
     * and return the resulting <code>ActionErrors</code> object.</p>
     *
     * @param context    The context for this request
     * @param actionForm The form bean for this request
     */
protected ActionErrors validate(ActionContext context, ActionConfig actionConfig, ActionForm actionForm) {
    ServletActionContext saContext = (ServletActionContext) context;
    ActionErrors errors = (actionForm.validate((ActionMapping) actionConfig, saContext.getRequest()));
    // Special handling for multipart request
    if ((errors != null) && !errors.isEmpty()) {
        if (actionForm.getMultipartRequestHandler() != null) {
            if (log.isTraceEnabled()) {
                log.trace("  Rolling back multipart request");
            }
            actionForm.getMultipartRequestHandler().rollback();
        }
    }
    return errors;
}
Also used : ActionMapping(org.apache.struts.action.ActionMapping) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) ActionErrors(org.apache.struts.action.ActionErrors)

Example 2 with ActionMapping

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

the class TestRequestUtils method testCreateActionForm1a.

// ----------------------------------------------------- createActionForm()
// Default module -- No ActionForm should be created
public void testCreateActionForm1a() {
    request.setPathElements("/myapp", "/noform.do", null, null);
    ActionMapping mapping = (ActionMapping) moduleConfig.findActionConfig("/noform");
    assertNotNull("Found /noform mapping", mapping);
    ActionForm form = RequestUtils.createActionForm(request, mapping, moduleConfig, null);
    assertNull("No ActionForm returned", form);
}
Also used : ActionMapping(org.apache.struts.action.ActionMapping) ActionForm(org.apache.struts.action.ActionForm) DynaActionForm(org.apache.struts.action.DynaActionForm)

Example 3 with ActionMapping

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

the class TestRequestUtils method testCreateActionForm4a.

// Default module -- Dynamic ActionForm with initializers
public void testCreateActionForm4a() {
    // Retrieve an appropriately configured DynaActionForm instance
    request.setPathElements("/myapp", "/dynamic0.do", null, null);
    ActionMapping mapping = (ActionMapping) moduleConfig.findActionConfig("/dynamic0");
    assertNotNull("Found /dynamic0 mapping", mapping);
    assertNotNull("Mapping has non-null name", mapping.getName());
    assertEquals("Mapping has correct name", "dynamic0", mapping.getName());
    assertNotNull("AppConfig has form bean " + mapping.getName(), moduleConfig.findFormBeanConfig(mapping.getName()));
    ActionForm form = RequestUtils.createActionForm(request, mapping, moduleConfig, null);
    assertNotNull("ActionForm returned", form);
    assertTrue("ActionForm of correct type", form instanceof DynaActionForm);
    // Validate the property values
    DynaActionForm dform = (DynaActionForm) form;
    Boolean booleanProperty = (Boolean) dform.get("booleanProperty");
    assertTrue("booleanProperty is true", booleanProperty.booleanValue());
    String stringProperty = (String) dform.get("stringProperty");
    assertEquals("stringProperty is correct", "String Property", stringProperty);
    Object value = null;
    value = dform.get("intArray1");
    assertNotNull("intArray1 exists", value);
    assertTrue("intArray1 is int[]", value instanceof int[]);
    int[] intArray1 = (int[]) value;
    assertEquals("intArray1 length is correct", 3, intArray1.length);
    assertEquals("intArray1[0] value is correct", 1, intArray1[0]);
    assertEquals("intArray1[1] value is correct", 2, intArray1[1]);
    assertEquals("intArray1[2] value is correct", 3, intArray1[2]);
    value = dform.get("intArray2");
    assertNotNull("intArray2 exists", value);
    assertTrue("intArray2 is int[]", value instanceof int[]);
    int[] intArray2 = (int[]) value;
    assertEquals("intArray2 length is correct", 5, intArray2.length);
    assertEquals("intArray2[0] value is correct", 0, intArray2[0]);
    assertEquals("intArray2[1] value is correct", 0, intArray2[1]);
    assertEquals("intArray2[2] value is correct", 0, intArray2[2]);
    assertEquals("intArray2[3] value is correct", 0, intArray2[3]);
    assertEquals("intArray2[4] value is correct", 0, intArray2[4]);
    value = dform.get("principal");
    assertNotNull("principal exists", value);
    assertTrue("principal is MockPrincipal", value instanceof MockPrincipal);
    value = dform.get("stringArray1");
    assertNotNull("stringArray1 exists", value);
    assertTrue("stringArray1 is int[]", value instanceof String[]);
    String[] stringArray1 = (String[]) value;
    assertEquals("stringArray1 length is correct", 3, stringArray1.length);
    assertEquals("stringArray1[0] value is correct", "aaa", stringArray1[0]);
    assertEquals("stringArray1[1] value is correct", "bbb", stringArray1[1]);
    assertEquals("stringArray1[2] value is correct", "ccc", stringArray1[2]);
    value = dform.get("stringArray2");
    assertNotNull("stringArray2 exists", value);
    assertTrue("stringArray2 is int[]", value instanceof String[]);
    String[] stringArray2 = (String[]) value;
    assertEquals("stringArray2 length is correct", 3, stringArray2.length);
    assertEquals("stringArray2[0] value is correct", new String(), stringArray2[0]);
    assertEquals("stringArray2[1] value is correct", new String(), stringArray2[1]);
    assertEquals("stringArray2[2] value is correct", new String(), stringArray2[2]);
    // Different form beans should get different property value instances
    Object value1 = null;
    DynaActionForm dform1 = (DynaActionForm) RequestUtils.createActionForm(request, mapping, moduleConfig, null);
    value = dform.get("principal");
    value1 = dform1.get("principal");
    assertEquals("Different form beans get equal instance values", value, value1);
    assertTrue("Different form beans get different instances 1", value != value1);
    value = dform.get("stringArray1");
    value1 = dform1.get("stringArray1");
    assertTrue("Different form beans get different instances 2", value != value1);
    dform1.set("stringProperty", "Different stringProperty value");
    value = dform.get("stringProperty");
    value1 = dform1.get("stringProperty");
    assertTrue("Different form beans get different instances 3", value != value1);
}
Also used : ActionMapping(org.apache.struts.action.ActionMapping) DynaActionForm(org.apache.struts.action.DynaActionForm) ActionForm(org.apache.struts.action.ActionForm) DynaActionForm(org.apache.struts.action.DynaActionForm) MockPrincipal(org.apache.struts.mock.MockPrincipal)

Example 4 with ActionMapping

use of org.apache.struts.action.ActionMapping 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)

Example 5 with ActionMapping

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

the class TestActionConfigMatcher method testCheckMultipleSubstitutions.

public void testCheckMultipleSubstitutions() {
    ActionMapping[] mapping = new ActionMapping[1];
    mapping[0] = new ActionMapping();
    mapping[0].setPath("/foo*");
    mapping[0].setName("name,{1}-{1}");
    ActionConfigMatcher matcher = new ActionConfigMatcher(mapping);
    ActionConfig m = matcher.match("/fooBar");
    assertTrue("Name hasn't been replaced correctly: " + m.getName(), "name,Bar-Bar".equals(m.getName()));
}
Also used : ActionMapping(org.apache.struts.action.ActionMapping)

Aggregations

ActionMapping (org.apache.struts.action.ActionMapping)32 ActionForm (org.apache.struts.action.ActionForm)18 DynaActionForm (org.apache.struts.action.DynaActionForm)14 ActionForward (org.apache.struts.action.ActionForward)6 MockFormBean (org.apache.struts.mock.MockFormBean)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 ActionFormBean (org.apache.struts.action.ActionFormBean)4 ServletActionContext (org.apache.struts.chain.contexts.ServletActionContext)4 FormPropertyConfig (org.apache.struts.config.FormPropertyConfig)4 ForwardConfig (org.apache.struts.config.ForwardConfig)4 ModuleConfigFactory (org.apache.struts.config.ModuleConfigFactory)4 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 ServletContext (javax.servlet.ServletContext)2 ServletException (javax.servlet.ServletException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 JspException (javax.servlet.jsp.JspException)2 Field (org.apache.commons.validator.Field)2