Search in sources :

Example 1 with ActionResult

use of jodd.madvoc.result.ActionResult in project jodd by oblac.

the class ResultsManager method register.

/**
	 * Registers new action result instance. If action result of the same class is already
	 * registered, registration will be skipped. If result for the same result type or
	 * same target class exist, it will be replaced! However, default Jodd results will
	 * <i>never</i> replace other results. After the registration, results are initialized.
	 */
protected ActionResult register(ActionResult result) {
    Class<? extends ActionResult> actionResultClass = result.getClass();
    // check existing
    ActionResult existingResult = allResults.get(actionResultClass);
    if (existingResult != null) {
        if (log.isDebugEnabled()) {
            log.debug("ActionResult already registered: " + actionResultClass);
        }
        return existingResult;
    }
    // + string hook
    String resultName = result.getResultName();
    if (resultName != null) {
        existingResult = stringResults.get(resultName);
        if (existingResult != null) {
            // the same result name exist
            if (!resultMayReplaceExistingOne(actionResultClass)) {
                if (log.isDebugEnabled()) {
                    log.debug("ActionResult already registered: " + actionResultClass);
                }
                return existingResult;
            }
            // allow only one action result per result type
            allResults.remove(existingResult.getClass());
        }
        if (log.isInfoEnabled()) {
            log.debug("ActionResult registered: " + resultName + " -> " + actionResultClass);
        }
        stringResults.put(resultName, result);
    }
    // + type result
    Class resultValueType = result.getResultValueType();
    if (resultValueType != null && resultValueType != String.class) {
        existingResult = typeResults.get(resultValueType);
        if (existingResult != null) {
            if (!resultMayReplaceExistingOne(actionResultClass)) {
                if (log.isDebugEnabled()) {
                    log.debug("ActionResult already registered: " + actionResultClass);
                }
                return existingResult;
            }
            // allow only one action result per result type
            allResults.remove(existingResult.getClass());
        }
        if (log.isInfoEnabled()) {
            log.debug("ActionResult registered: " + resultValueType + " -> " + actionResultClass);
        }
        typeResults.put(resultValueType, result);
    }
    if (log.isInfoEnabled()) {
        log.debug("ActionResult registered: " + actionResultClass);
    }
    allResults.put(actionResultClass, result);
    // + init
    initializeResult(result);
    return result;
}
Also used : ActionResult(jodd.madvoc.result.ActionResult)

Example 2 with ActionResult

use of jodd.madvoc.result.ActionResult in project jodd by oblac.

the class ResultsManager method lookup.

/**
	 * Lookups for {@link jodd.madvoc.result.ActionResult action result handler}
	 * based on current {@link jodd.madvoc.ActionRequest action request} and action method
	 * result object. Lookup performs the following in given order:
	 * <ul>
	 *     <li>if result object is <code>null</code>, check if {@link jodd.madvoc.result.Result} is used</li>
	 *     <li>check result definition in <code>@Action</code> annotation</li>
	 *     <li>if result is not a <code>String</code>, check if it is annotated with {@link jodd.madvoc.meta.RenderWith} annotation</li>
	 *     <li>if result is not a <code>String</code>, find ActionResult for matching result object type</li>
	 *     <li>if action result still not found, call <code>toString</code> on result object and parse it</li>
	 * </ul>
	 */
public ActionResult lookup(ActionRequest actionRequest, Object resultObject) {
    ActionResult actionResult = null;
    // + special class: result
    if (resultObject == null) {
        Result result = actionRequest.getResult();
        if (result != null) {
            // read Result, if used; if not, values will be null
            Class<? extends ActionResult> actionResultClass = result.getActionResult();
            resultObject = result.getResultValue();
            if (resultObject == null) {
                resultObject = result.value();
            }
            if (actionResultClass != null) {
                actionResult = lookupAndRegisterIfMissing(actionResultClass);
            }
        }
    }
    if (actionResult == null) {
        // + still not found, read @Action value
        ActionConfig actionConfig = actionRequest.getActionConfig();
        Class<? extends ActionResult> actionResultClass = actionConfig.getActionResult();
        if (actionResultClass != null) {
            actionResult = lookupAndRegisterIfMissing(actionResultClass);
        }
    }
    if (actionResult == null && resultObject != null) {
        Class resultType = resultObject.getClass();
        if (resultType != String.class) {
            // + still not found, read @RenderWith value if exist
            RenderWith renderWith = resultObject.getClass().getAnnotation(RenderWith.class);
            if (renderWith != null) {
                actionResult = lookupAndRegisterIfMissing(renderWith.value());
            }
            if (actionResult == null) {
                // + still not found, lookup for type
                actionResult = typeResults.get(resultObject.getClass());
            }
        }
    }
    if (actionResult == null) {
        // + still not found, toString()
        ActionResult defaultActionResult = lookupAndRegisterIfMissing(madvocConfig.getDefaultActionResult());
        if (stringResults.isEmpty()) {
            // no string results registered, carry on with the defaults.
            actionResult = defaultActionResult;
        } else {
            String resultValue = resultObject != null ? resultObject.toString() : null;
            String resultName = null;
            // first check result value
            if (resultValue != null) {
                int columnIndex = resultValue.indexOf(':');
                if (columnIndex != -1) {
                    resultName = resultValue.substring(0, columnIndex);
                    resultValue = resultValue.substring(columnIndex + 1);
                }
            }
            if (resultName != null) {
                actionResult = stringResults.get(resultName);
            } else {
                actionResult = defaultActionResult;
            }
            if (actionResult.getResultName() != null) {
                // only when action result is string result
                try {
                    Class targetClass = actionResult.getResultValueType();
                    if (targetClass == null || targetClass == String.class) {
                        resultObject = resultValue;
                    } else {
                        resultObject = TypeConverterManager.convertType(resultValue, targetClass);
                    }
                } catch (Exception ex) {
                    resultObject = resultValue;
                }
            }
        }
    }
    // set action result object into action request!
    actionRequest.setActionResult(resultObject);
    return actionResult;
}
Also used : ActionConfig(jodd.madvoc.ActionConfig) RenderWith(jodd.madvoc.meta.RenderWith) ActionResult(jodd.madvoc.result.ActionResult) MadvocException(jodd.madvoc.MadvocException) ActionResult(jodd.madvoc.result.ActionResult) Result(jodd.madvoc.result.Result)

Example 3 with ActionResult

use of jodd.madvoc.result.ActionResult in project jodd by oblac.

the class ResultsManagerTest method testDuplicateResults2.

@Test
public void testDuplicateResults2() {
    ResultsManager resultsManager = new ResultsManager() {

        @Override
        protected void initializeResult(ActionResult result) {
        }

        @Override
        protected boolean resultMayReplaceExistingOne(Class<? extends ActionResult> actionResultClass) {
            if (actionResultClass.getName().contains("Test")) {
                return true;
            }
            return super.resultMayReplaceExistingOne(actionResultClass);
        }
    };
    // register
    resultsManager.register(new MyRedirect1());
    // ignore
    resultsManager.register(new MyRedirect1());
    // ignore
    resultsManager.register(new ServletRedirectResult());
    assertNull(resultsManager.allResults.get(ServletRedirectResult.class));
    assertEquals(MyRedirect1.class, resultsManager.stringResults.get("redirect").getClass());
    // ignore
    resultsManager.register(new MyRedirect2());
    assertEquals(MyRedirect2.class, resultsManager.stringResults.get("redirect").getClass());
    assertEquals(1, resultsManager.allResults.size());
}
Also used : ServletRedirectResult(jodd.madvoc.result.ServletRedirectResult) ActionResult(jodd.madvoc.result.ActionResult) Test(org.junit.Test)

Example 4 with ActionResult

use of jodd.madvoc.result.ActionResult in project jodd by oblac.

the class ResultsManagerTest method testDuplicateResults1.

@Test
public void testDuplicateResults1() {
    ResultsManager resultsManager = new ResultsManager() {

        @Override
        protected void initializeResult(ActionResult result) {
        }

        @Override
        protected boolean resultMayReplaceExistingOne(Class<? extends ActionResult> actionResultClass) {
            if (actionResultClass.getName().contains("Test")) {
                return true;
            }
            return super.resultMayReplaceExistingOne(actionResultClass);
        }
    };
    // new
    resultsManager.register(new ServletRedirectResult());
    // ignore
    resultsManager.register(new ServletRedirectResult());
    // replace
    resultsManager.register(new MyRedirect1());
    assertNull(resultsManager.allResults.get(ServletRedirectResult.class));
    assertEquals(MyRedirect1.class, resultsManager.stringResults.get("redirect").getClass());
    // replace
    resultsManager.register(new MyRedirect2());
    assertEquals(MyRedirect2.class, resultsManager.stringResults.get("redirect").getClass());
    assertEquals(1, resultsManager.allResults.size());
}
Also used : ServletRedirectResult(jodd.madvoc.result.ServletRedirectResult) ActionResult(jodd.madvoc.result.ActionResult) Test(org.junit.Test)

Example 5 with ActionResult

use of jodd.madvoc.result.ActionResult in project jodd by oblac.

the class MadvocController method render.

// ---------------------------------------------------------------- render
/**
	 * Invokes a result after the action invocation.
	 * <p>
	 * Results may be objects that specify which action result will be used
	 * to render the result.
	 * <p>
	 * Result value may consist of two parts: type and value. Result type is optional and, if exists, it is separated
	 * by semi-colon from the value. If type is not specified
	 * then the default result type if still not defined. Result type defines which
	 * {@link ActionResult} should be used for rendering the value.
	 * <p>
	 * Result value is first checked against aliased values. Then, it is resolved and then passed
	 * to the founded {@link ActionResult}.
	 *
	 * @see ActionResult#render(jodd.madvoc.ActionRequest, Object)
	 */
@SuppressWarnings("unchecked")
public void render(ActionRequest actionRequest, Object resultObject) throws Exception {
    ActionResult actionResult = resultsManager.lookup(actionRequest, resultObject);
    if (actionResult == null) {
        throw new MadvocException("Action result not found");
    }
    if (madvocConfig.isPreventCaching()) {
        ServletUtil.preventCaching(actionRequest.getHttpServletResponse());
    }
    actionResult.render(actionRequest, actionRequest.getActionResult());
}
Also used : ActionResult(jodd.madvoc.result.ActionResult) MadvocException(jodd.madvoc.MadvocException)

Aggregations

ActionResult (jodd.madvoc.result.ActionResult)5 MadvocException (jodd.madvoc.MadvocException)2 ServletRedirectResult (jodd.madvoc.result.ServletRedirectResult)2 Test (org.junit.Test)2 ActionConfig (jodd.madvoc.ActionConfig)1 RenderWith (jodd.madvoc.meta.RenderWith)1 Result (jodd.madvoc.result.Result)1