Search in sources :

Example 1 with ActionDefinition

use of play.mvc.Router.ActionDefinition in project Japid by branaway.

the class JapidPlayAdapter method jsRoute.

/**
	 * create a js object that contains both a reverse lookup method and the action method of the current action
	 * @param name
	 * @param args
	 * @return
	 */
public static String jsRoute(String name, String... args) {
    final ActionDefinition action = urlMapper.lookupActionDefinition(name, args);
    StringBuffer sb = new StringBuffer();
    sb.append("{");
    if (action.args.isEmpty()) {
        sb.append("url: function() { return '" + action.url.replace("&", "&") + "'; },");
    } else {
        sb.append("url: function(args) { var pattern = '" + action.url.replace("&", "&") + "'; for (var key in args) { pattern = pattern.replace(':'+key, args[key]); } return pattern; },");
    }
    sb.append("method: '" + action.method + "'");
    sb.append("}");
    return sb.toString();
}
Also used : ActionDefinition(play.mvc.Router.ActionDefinition)

Example 2 with ActionDefinition

use of play.mvc.Router.ActionDefinition in project Japid by branaway.

the class ActionBridge method invokeMethod.

/**
	 * this is really to do the reverse url lookup
	 * 
	 * @param actionString
	 * @param param
	 * @return
	 */
public ActionDefinition invokeMethod(String actionString, Object param) {
    try {
        // forms: Controller.action, action, package.Controller.action
        String action = actionString;
        //			String methodName = actionString;
        if (actionString.indexOf(".") > 0) {
        //				int lastIndexOf = actionString.lastIndexOf('.');
        ////				methodName = actionString.substring(lastIndexOf + 1);
        //				controllerName = actionString.substring(0, lastIndexOf);
        //				// fell spec with controller name
        } else {
            Request req = Request.current();
            if (req != null) {
                action = req.controller + "." + actionString;
            }
        }
        try {
            Map<String, Object> args = new HashMap<String, Object>();
            Method actionMethod = (Method) ActionInvoker.getActionMethod(action)[1];
            //				String[] names = (String[]) actionMethod
            //						.getDeclaringClass()
            //						.getDeclaredField("$" + actionMethod.getName() + computeMethodHash(actionMethod.getParameterTypes())).get(null);
            String[] names = Java.parameterNames(actionMethod);
            if (param instanceof Object[]) {
                Object[] arrayParam = (Object[]) param;
                // error. we must warn him.
                if (names.length < arrayParam.length) {
                    throw new NoRouteFoundException(action, null);
                }
                Annotation[] annos = actionMethod.getAnnotations();
                for (int i = 0; i < arrayParam.length; i++) {
                    Object arrayParamElem = arrayParam[i];
                    if (arrayParamElem instanceof Router.ActionDefinition && arrayParamElem != null) {
                        Unbinder.unBind(args, arrayParamElem.toString(), i < names.length ? names[i] : "", annos);
                    } else if (isSimpleParam(actionMethod.getParameterTypes()[i])) {
                        if (arrayParamElem != null) {
                            Unbinder.unBind(args, arrayParamElem.toString(), i < names.length ? names[i] : "", annos);
                        }
                    } else {
                        Unbinder.unBind(args, arrayParamElem, i < names.length ? names[i] : "", annos);
                    }
                }
            }
            Router.ActionDefinition def = Router.reverse(action, args);
            if (absolute) {
                def.absolute();
            }
            // if (template.template.name.endsWith(".html") ||
            // template.template.name.endsWith(".xml")) {
            def.url = def.url.replace("&", "&amp;");
            // }
            return def;
        } catch (ActionNotFoundException e) {
            //				throw new NoRouteFoundException(action, null);
            throw new ReverseRouteException(action);
        }
    } catch (Exception e) {
        if (e instanceof PlayException) {
            throw (PlayException) e;
        }
        if (e instanceof JapidRuntimeException) {
            throw (JapidRuntimeException) e;
        }
        throw new UnexpectedException(e);
    }
}
Also used : JapidRuntimeException(cn.bran.japid.exceptions.JapidRuntimeException) UnexpectedException(play.exceptions.UnexpectedException) HashMap(java.util.HashMap) Request(play.mvc.Http.Request) Router(play.mvc.Router) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) PlayException(play.exceptions.PlayException) JapidRuntimeException(cn.bran.japid.exceptions.JapidRuntimeException) NoRouteFoundException(play.exceptions.NoRouteFoundException) ActionNotFoundException(play.exceptions.ActionNotFoundException) UnexpectedException(play.exceptions.UnexpectedException) ReverseRouteException(cn.bran.play.exceptions.ReverseRouteException) ActionNotFoundException(play.exceptions.ActionNotFoundException) NoRouteFoundException(play.exceptions.NoRouteFoundException) ActionDefinition(play.mvc.Router.ActionDefinition) PlayException(play.exceptions.PlayException) ReverseRouteException(cn.bran.play.exceptions.ReverseRouteException)

Aggregations

ActionDefinition (play.mvc.Router.ActionDefinition)2 JapidRuntimeException (cn.bran.japid.exceptions.JapidRuntimeException)1 ReverseRouteException (cn.bran.play.exceptions.ReverseRouteException)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 ActionNotFoundException (play.exceptions.ActionNotFoundException)1 NoRouteFoundException (play.exceptions.NoRouteFoundException)1 PlayException (play.exceptions.PlayException)1 UnexpectedException (play.exceptions.UnexpectedException)1 Request (play.mvc.Http.Request)1 Router (play.mvc.Router)1