Search in sources :

Example 1 with DynaActionForm

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

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

the class TestCopyFormToContext method testLookupByNameAndSessionScope.

public void testLookupByNameAndSessionScope() throws Exception {
    CopyFormToContext command = new CopyFormToContext();
    String formName = "bar";
    command.setFormName(formName);
    command.setScope("session");
    command.setToKey(POST_EXECUTION_CONTEXT_KEY);
    assertNull(context.get(POST_EXECUTION_CONTEXT_KEY));
    assertNull(context.getRequestScope().get(formName));
    assertNull(context.getSessionScope().get(formName));
    command.execute(context);
    assertNotNull(context.get(POST_EXECUTION_CONTEXT_KEY));
    assertNull(context.getRequestScope().get(formName));
    assertNotNull(context.getSessionScope().get(formName));
    assertSame(context.get(POST_EXECUTION_CONTEXT_KEY), context.getSessionScope().get(formName));
    ActionForm theForm = (ActionForm) context.get(POST_EXECUTION_CONTEXT_KEY);
    assertTrue(theForm instanceof DynaActionForm);
    DynaActionForm dForm = (DynaActionForm) theForm;
    assertEquals("test", dForm.get("property"));
}
Also used : DynaActionForm(org.apache.struts.action.DynaActionForm) ActionForm(org.apache.struts.action.ActionForm) DynaActionForm(org.apache.struts.action.DynaActionForm)

Example 3 with DynaActionForm

use of org.apache.struts.action.DynaActionForm in project iaf by ibissource.

the class ExecuteJdbcQueryExecute method executeSub.

public ActionForward executeSub(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    // Initialize action
    initAction(request);
    DynaActionForm executeJdbcQueryExecuteForm = (DynaActionForm) form;
    String form_jmsRealm = (String) executeJdbcQueryExecuteForm.get("jmsRealm");
    String form_queryType = (String) executeJdbcQueryExecuteForm.get("queryType");
    String form_resultType = (String) executeJdbcQueryExecuteForm.get("resultType");
    String form_query = (String) executeJdbcQueryExecuteForm.get("query");
    DirectQuerySender qs;
    String result = "";
    try {
        qs = (DirectQuerySender) ibisManager.getIbisContext().createBeanAutowireByName(DirectQuerySender.class);
        try {
            qs.setName("QuerySender");
            qs.setJmsRealm(form_jmsRealm);
            qs.setQueryType(form_queryType);
            qs.setBlobSmartGet(true);
            qs.configure(true);
            qs.open();
            result = qs.sendMessage("dummy", form_query);
            if ("csv".equalsIgnoreCase(form_resultType)) {
                URL url = ClassUtils.getResourceURL(this, DB2XML_XSLT);
                if (url != null) {
                    Transformer t = XmlUtils.createTransformer(url);
                    result = XmlUtils.transformXml(t, result);
                }
            }
        } catch (Throwable t) {
            error("error occured on executing jdbc query", t);
        } finally {
            qs.close();
        }
    } catch (Exception e) {
        error("error occured on creating or closing connection", e);
    }
    StoreFormData(form_query, result, executeJdbcQueryExecuteForm);
    if (!errors.isEmpty()) {
        saveErrors(request, errors);
        return (new ActionForward(mapping.getInput()));
    }
    // Successfull: store cookie
    String cookieValue = "";
    cookieValue += "jmsRealm=\"" + form_jmsRealm + "\"";
    // separator
    cookieValue += " ";
    cookieValue += "queryType=\"" + form_queryType + "\"";
    // separator
    cookieValue += " ";
    cookieValue += "resultType=\"" + form_resultType + "\"";
    // separator
    cookieValue += " ";
    // TODO: fix workaround to avoid http error 500 on WAS (line separators in query cookie)
    String fq = Misc.replace(form_query, System.getProperty("line.separator"), " ");
    cookieValue += "query=\"" + fq + "\"";
    Cookie execJdbcCookie = new Cookie(AppConstants.getInstance().getProperty("WEB_EXECJDBCCOOKIE_NAME"), cookieValue);
    execJdbcCookie.setMaxAge(Integer.MAX_VALUE);
    log.debug("Store cookie for " + request.getServletPath() + " cookieName[" + AppConstants.getInstance().getProperty("WEB_EXECJDBCCOOKIE_NAME") + "] " + " cookieValue[" + new StringTagger(cookieValue).toString() + "]");
    try {
        response.addCookie(execJdbcCookie);
    } catch (Throwable e) {
        log.warn("unable to add cookie to request. cookie value [" + execJdbcCookie.getValue() + "]");
    }
    log.debug("forward to success");
    return (mapping.findForward("success"));
}
Also used : Cookie(javax.servlet.http.Cookie) DynaActionForm(org.apache.struts.action.DynaActionForm) Transformer(javax.xml.transform.Transformer) DirectQuerySender(nl.nn.adapterframework.jdbc.DirectQuerySender) StringTagger(nl.nn.adapterframework.util.StringTagger) URL(java.net.URL) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ActionForward(org.apache.struts.action.ActionForward)

Example 4 with DynaActionForm

use of org.apache.struts.action.DynaActionForm in project iaf by ibissource.

the class LogHandler method executeSub.

public ActionForward executeSub(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    // Initialize action
    initAction(request);
    String commandIssuedBy = " remoteHost [" + request.getRemoteHost() + "]";
    commandIssuedBy += " remoteAddress [" + request.getRemoteAddr() + "]";
    commandIssuedBy += " remoteUser [" + request.getRemoteUser() + "]";
    Logger lg = LogUtil.getRootLogger();
    DynaActionForm logForm = (DynaActionForm) form;
    String form_logLevel = (String) logForm.get("logLevel");
    boolean form_logIntermediaryResults = false;
    if (null != logForm.get("logIntermediaryResults")) {
        form_logIntermediaryResults = ((Boolean) logForm.get("logIntermediaryResults")).booleanValue();
    }
    log.warn("*** logintermediary results=" + form_logIntermediaryResults);
    String logIntermediaryResults = "false";
    if (form_logIntermediaryResults)
        logIntermediaryResults = "true";
    int form_lengthLogRecords = ((Integer) logForm.get("lengthLogRecords")).intValue();
    Level level = Level.toLevel(form_logLevel);
    Appender appender = lg.getAppender("appwrap");
    IbisAppenderWrapper iaw = null;
    if (appender != null && appender instanceof IbisAppenderWrapper) {
        iaw = (IbisAppenderWrapper) appender;
    }
    String msg = ("LogLevel changed from [" + lg.getLevel() + "] to [" + level + "], logIntermediaryResults from [" + AppConstants.getInstance().getProperty("log.logIntermediaryResults") + "] to [" + "" + form_logIntermediaryResults + "]  and logMaxMessageLength from [" + (iaw != null ? iaw.getMaxMessageLength() : -1) + "] to [" + "" + form_lengthLogRecords + "] by" + commandIssuedBy);
    log.warn(msg);
    secLog.info(msg);
    if (iaw != null) {
        iaw.setMaxMessageLength(form_lengthLogRecords);
    }
    AppConstants.getInstance().put("log.logIntermediaryResults", logIntermediaryResults);
    lg.setLevel(level);
    return (mapping.findForward("success"));
}
Also used : Appender(org.apache.log4j.Appender) DynaActionForm(org.apache.struts.action.DynaActionForm) Level(org.apache.log4j.Level) Logger(org.apache.log4j.Logger) IbisAppenderWrapper(nl.nn.adapterframework.extensions.log4j.IbisAppenderWrapper)

Example 5 with DynaActionForm

use of org.apache.struts.action.DynaActionForm in project iaf by ibissource.

the class SendJmsMessage method executeSub.

public ActionForward executeSub(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    // Initialize action
    initAction(request);
    DynaActionForm sendJmsMessageForm = getPersistentForm(mapping, form, request);
    Cookie[] cookies = request.getCookies();
    if (null != cookies) {
        for (int i = 0; i < cookies.length; i++) {
            Cookie aCookie = cookies[i];
            if (aCookie.getName().equals(AppConstants.getInstance().getProperty("WEB_JMSCOOKIE_NAME"))) {
                StringTagger cs = new StringTagger(aCookie.getValue());
                log.debug("restoring values from cookie: " + cs.toString());
                try {
                    sendJmsMessageForm.set("jmsRealm", cs.Value("jmsRealm"));
                    sendJmsMessageForm.set("destinationName", cs.Value("destinationName"));
                    sendJmsMessageForm.set("destinationType", cs.Value("destinationType"));
                } catch (Exception e) {
                    log.warn("could not restore Cookie value's");
                }
            }
        }
    }
    List jmsRealms = JmsRealmFactory.getInstance().getRegisteredRealmNamesAsList();
    if (jmsRealms.size() == 0)
        jmsRealms.add("no realms defined");
    sendJmsMessageForm.set("jmsRealms", jmsRealms);
    // Forward control to the specified success URI
    log.debug("forward to success");
    return (mapping.findForward("success"));
}
Also used : Cookie(javax.servlet.http.Cookie) DynaActionForm(org.apache.struts.action.DynaActionForm) StringTagger(nl.nn.adapterframework.util.StringTagger) List(java.util.List) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

DynaActionForm (org.apache.struts.action.DynaActionForm)20 ActionForm (org.apache.struts.action.ActionForm)8 ActionMapping (org.apache.struts.action.ActionMapping)6 IOException (java.io.IOException)5 ServletException (javax.servlet.ServletException)5 ActionForward (org.apache.struts.action.ActionForward)4 ArrayList (java.util.ArrayList)3 Cookie (javax.servlet.http.Cookie)3 StringTagger (nl.nn.adapterframework.util.StringTagger)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 IbisAppenderWrapper (nl.nn.adapterframework.extensions.log4j.IbisAppenderWrapper)2 XmlBuilder (nl.nn.adapterframework.util.XmlBuilder)2 Appender (org.apache.log4j.Appender)2 Logger (org.apache.log4j.Logger)2 MockPrincipal (org.apache.struts.mock.MockPrincipal)2 FormFile (org.apache.struts.upload.FormFile)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 PrintWriter (java.io.PrintWriter)1