Search in sources :

Example 36 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class DefaultActionInvocation method invoke.

/**
 * @throws ConfigurationException If no result can be found with the returned code
 */
public String invoke() throws Exception {
    if (executed) {
        throw new IllegalStateException("Action has already executed");
    }
    if (asyncManager == null || !asyncManager.hasAsyncActionResult()) {
        if (interceptors.hasNext()) {
            final InterceptorMapping interceptorMapping = interceptors.next();
            Interceptor interceptor = interceptorMapping.getInterceptor();
            if (interceptor instanceof WithLazyParams) {
                interceptor = lazyParamInjector.injectParams(interceptor, interceptorMapping.getParams(), invocationContext);
            }
            resultCode = interceptor.intercept(DefaultActionInvocation.this);
        } else {
            resultCode = invokeActionOnly();
        }
    } else {
        Object asyncActionResult = asyncManager.getAsyncActionResult();
        if (asyncActionResult instanceof Throwable) {
            throw new Exception((Throwable) asyncActionResult);
        }
        asyncAction = null;
        resultCode = saveResult(proxy.getConfig(), asyncActionResult);
    }
    if (asyncManager == null || asyncAction == null) {
        // return above and flow through again
        if (!executed) {
            if (preResultListeners != null) {
                LOG.trace("Executing PreResultListeners for result [{}]", result);
                for (Object preResultListener : preResultListeners) {
                    PreResultListener listener = (PreResultListener) preResultListener;
                    listener.beforeResult(this, resultCode);
                }
            }
            // now execute the result, if we're supposed to
            if (proxy.getExecuteResult()) {
                executeResult();
            }
            executed = true;
        }
    } else {
        asyncManager.invokeAsyncAction(asyncAction);
    }
    return resultCode;
}
Also used : WithLazyParams(com.opensymphony.xwork2.interceptor.WithLazyParams) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PreResultListener(com.opensymphony.xwork2.interceptor.PreResultListener) Interceptor(com.opensymphony.xwork2.interceptor.Interceptor) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) NoSuchPropertyException(ognl.NoSuchPropertyException) StrutsException(org.apache.struts2.StrutsException) MethodFailedException(ognl.MethodFailedException)

Example 37 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class TagModelTest method testUnwrapMap.

public void testUnwrapMap() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    OgnlValueStack stack = (OgnlValueStack) ActionContext.getContext().getValueStack();
    Map params = new LinkedHashMap();
    // Try to test out the commons Freemarker's Template Model
    // TemplateBooleanModel
    params.put("property1", new TemplateBooleanModel() {

        public boolean getAsBoolean() throws TemplateModelException {
            return true;
        }
    });
    params.put("property2", new TemplateBooleanModel() {

        public boolean getAsBoolean() throws TemplateModelException {
            return false;
        }
    });
    // TemplateScalarModel
    params.put("property3", new TemplateScalarModel() {

        public String getAsString() throws TemplateModelException {
            return "toby";
        }
    });
    params.put("property4", new TemplateScalarModel() {

        public String getAsString() throws TemplateModelException {
            return "phil";
        }
    });
    // TemplateNumberModel
    params.put("property5", new TemplateNumberModel() {

        public Number getAsNumber() throws TemplateModelException {
            return new Integer("10");
        }
    });
    params.put("property6", new TemplateNumberModel() {

        public Number getAsNumber() throws TemplateModelException {
            return new Float("1.1");
        }
    });
    // TemplateHashModel
    params.put("property7", new TemplateHashModel() {

        public TemplateModel get(String arg0) throws TemplateModelException {
            return null;
        }

        public boolean isEmpty() throws TemplateModelException {
            return true;
        }
    });
    // TemplateSequenceModel
    params.put("property8", new TemplateSequenceModel() {

        public TemplateModel get(int arg0) throws TemplateModelException {
            return null;
        }

        public int size() throws TemplateModelException {
            return 0;
        }
    });
    // TemplateCollectionModel
    params.put("property9", new TemplateCollectionModel() {

        public TemplateModelIterator iterator() throws TemplateModelException {
            return new TemplateModelIterator() {

                private Iterator i;

                {
                    i = Collections.EMPTY_LIST.iterator();
                }

                public boolean hasNext() throws TemplateModelException {
                    return i.hasNext();
                }

                public TemplateModel next() throws TemplateModelException {
                    return (TemplateModel) i.next();
                }
            };
        }
    });
    // AdapterTemplateModel
    params.put("property10", new AdapterTemplateModel() {

        public Object getAdaptedObject(Class arg0) {
            return ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT;
        }
    });
    // WrapperTemplateModel
    params.put("property11", new WrapperTemplateModel() {

        public Object getWrappedObject() {
            return WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT;
        }
    });
    TagModel tagModel = new TagModel(stack, request, response) {

        protected Component getBean() {
            return null;
        }
    };
    Map result = tagModel.unwrapParameters(params);
    assertNotNull(result);
    assertEquals(result.size(), 11);
    assertEquals(result.get("property1"), Boolean.TRUE);
    assertEquals(result.get("property2"), Boolean.FALSE);
    assertEquals(result.get("property3"), "toby");
    assertEquals(result.get("property4"), "phil");
    assertEquals(result.get("property5"), new Integer(10));
    assertEquals(result.get("property6"), new Float(1.1));
    assertNotNull(result.get("property7"));
    assertTrue(result.get("property7") instanceof Map);
    assertNotNull(result.get("property8"));
    assertTrue(result.get("property8") instanceof Collection);
    assertNotNull(result.get("property9"));
    assertTrue(result.get("property9") instanceof Collection);
    assertEquals(result.get("property10"), ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT);
    assertEquals(result.get("property11"), WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT);
}
Also used : OgnlValueStack(com.opensymphony.xwork2.ognl.OgnlValueStack) TemplateModelException(freemarker.template.TemplateModelException) TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateModelIterator(freemarker.template.TemplateModelIterator) WrapperTemplateModel(freemarker.ext.util.WrapperTemplateModel) AdapterTemplateModel(freemarker.template.AdapterTemplateModel) TemplateModel(freemarker.template.TemplateModel) LinkedHashMap(java.util.LinkedHashMap) TemplateHashModel(freemarker.template.TemplateHashModel) Iterator(java.util.Iterator) TemplateModelIterator(freemarker.template.TemplateModelIterator) TemplateCollectionModel(freemarker.template.TemplateCollectionModel) WrapperTemplateModel(freemarker.ext.util.WrapperTemplateModel) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) TemplateBooleanModel(freemarker.template.TemplateBooleanModel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TemplateNumberModel(freemarker.template.TemplateNumberModel) AdapterTemplateModel(freemarker.template.AdapterTemplateModel) Collection(java.util.Collection) TemplateScalarModel(freemarker.template.TemplateScalarModel) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 38 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class I18n method start.

public boolean start(Writer writer) {
    boolean result = super.start(writer);
    try {
        String name = this.findString(this.name, "name", "Resource bundle name is required. Example: foo or foo_en");
        ResourceBundle bundle = defaultTextProvider.getTexts(name);
        if (bundle == null) {
            LocaleProvider localeProvider = localeProviderFactory.createLocaleProvider();
            bundle = localizedTextProvider.findResourceBundle(name, localeProvider.getLocale());
        }
        if (bundle != null) {
            textProvider = textProviderFactory.createInstance(bundle);
            getStack().push(textProvider);
            pushed = true;
        }
    } catch (Exception e) {
        throw new StrutsException("Could not find the bundle " + name, e);
    }
    return result;
}
Also used : StrutsException(org.apache.struts2.StrutsException) LocaleProvider(com.opensymphony.xwork2.LocaleProvider) ResourceBundle(java.util.ResourceBundle) StrutsException(org.apache.struts2.StrutsException)

Example 39 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class Debug method start.

public boolean start(Writer writer) {
    boolean result = super.start(writer);
    if (showDebug()) {
        ValueStack stack = getStack();
        Iterator iter = stack.getRoot().iterator();
        List stackValues = new ArrayList(stack.getRoot().size());
        while (iter.hasNext()) {
            Object o = iter.next();
            Map values;
            try {
                values = reflectionProvider.getBeanMap(o);
            } catch (Exception e) {
                throw new StrutsException("Caught an exception while getting the property values of " + o, e);
            }
            stackValues.add(new DebugMapEntry(o.getClass().getName(), values));
        }
        addParameter("stackValues", stackValues);
    }
    return result;
}
Also used : StrutsException(org.apache.struts2.StrutsException) ValueStack(com.opensymphony.xwork2.util.ValueStack) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) StrutsException(org.apache.struts2.StrutsException)

Example 40 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class XmlConfigurationProviderExceptionMappingsTest method testActions.

public void testActions() throws ConfigurationException {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-exception-mappings.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    List<ExceptionMappingConfig> exceptionMappings = new ArrayList<>();
    HashMap<String, String> parameters = new HashMap<>();
    HashMap<String, ResultConfig> results = new HashMap<>();
    exceptionMappings.add(new ExceptionMappingConfig.Builder("spooky-result", "com.opensymphony.xwork2.SpookyException", "spooky-result").build());
    results.put("spooky-result", new ResultConfig.Builder("spooky-result", MockResult.class.getName()).build());
    Map<String, String> resultParams = new HashMap<>();
    resultParams.put("actionName", "bar.vm");
    results.put("specificLocationResult", new ResultConfig.Builder("specificLocationResult", ActionChainResult.class.getName()).addParams(resultParams).build());
    ActionConfig expectedAction = new ActionConfig.Builder("default", "Bar", SimpleAction.class.getName()).addParams(parameters).addResultConfigs(results).addExceptionMappings(exceptionMappings).build();
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map actionConfigs = pkg.getActionConfigs();
    // assertions
    assertEquals(1, actionConfigs.size());
    ActionConfig action = (ActionConfig) actionConfigs.get("Bar");
    assertEquals(expectedAction, action);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) MockResult(com.opensymphony.xwork2.mock.MockResult) HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) ArrayList(java.util.ArrayList) SimpleAction(com.opensymphony.xwork2.SimpleAction) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) ExceptionMappingConfig(com.opensymphony.xwork2.config.entities.ExceptionMappingConfig) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ActionSupport (com.opensymphony.xwork2.ActionSupport)63 Test (org.junit.Test)52 ActionProxy (com.opensymphony.xwork2.ActionProxy)51 List (java.util.List)49 ValueStack (com.opensymphony.xwork2.util.ValueStack)38 Result (edu.stanford.CVC4.Result)35 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)34 ActionContext (com.opensymphony.xwork2.ActionContext)33 Expr (edu.stanford.CVC4.Expr)32 SExpr (edu.stanford.CVC4.SExpr)32 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)31 ArrayList (java.util.ArrayList)31 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)30 HashMap (java.util.HashMap)29 Rational (edu.stanford.CVC4.Rational)25 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)22 Map (java.util.Map)21 ServletActionContext (org.apache.struts2.ServletActionContext)21 Result (com.opensymphony.xwork2.Result)18 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)18