Search in sources :

Example 1 with UnexpectedException

use of play.exceptions.UnexpectedException 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)

Example 2 with UnexpectedException

use of play.exceptions.UnexpectedException in project play-cookbook by spinscale.

the class Query method getIdValueFromIndex.

private Object getIdValueFromIndex(Class<?> clazz, String indexValue) {
    java.lang.reflect.Field field = getIdField(clazz);
    Class<?> parameter = field.getType();
    try {
        return Binder.directBind(indexValue, parameter);
    } catch (Exception e) {
        throw new UnexpectedException("Could not convert the ID from index to corresponding type", e);
    }
}
Also used : UnexpectedException(play.exceptions.UnexpectedException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) UnexpectedException(play.exceptions.UnexpectedException)

Example 3 with UnexpectedException

use of play.exceptions.UnexpectedException in project play-cookbook by spinscale.

the class FeedResult method apply.

public void apply(Request request, Response response) {
    try {
        SyndFeed feed = new SyndFeedImpl();
        feed.setAuthor(Play.configuration.getProperty("rss.author"));
        feed.setTitle(Play.configuration.getProperty("rss.title"));
        feed.setDescription(Play.configuration.getProperty("rss.description"));
        feed.setLink(getFeedLink());
        List<SyndEntry> entries = new ArrayList<SyndEntry>();
        for (Post post : posts) {
            String url = createUrl("Application.showPost", "id", post.id.toString());
            SyndEntry entry = createEntry(post.title, url, post.content, post.createdAt);
            entries.add(entry);
        }
        feed.setEntries(entries);
        feed.setFeedType(getFeedType());
        setContentType(response);
        SyndFeedOutput output = new SyndFeedOutput();
        String rss = output.outputString(feed);
        response.out.write(rss.getBytes("utf-8"));
    } catch (Exception e) {
        throw new UnexpectedException(e);
    }
}
Also used : SyndFeed(com.sun.syndication.feed.synd.SyndFeed) UnexpectedException(play.exceptions.UnexpectedException) SyndEntry(com.sun.syndication.feed.synd.SyndEntry) Post(models.Post) ArrayList(java.util.ArrayList) SyndFeedImpl(com.sun.syndication.feed.synd.SyndFeedImpl) SyndFeedOutput(com.sun.syndication.io.SyndFeedOutput) UnexpectedException(play.exceptions.UnexpectedException)

Aggregations

UnexpectedException (play.exceptions.UnexpectedException)3 JapidRuntimeException (cn.bran.japid.exceptions.JapidRuntimeException)1 ReverseRouteException (cn.bran.play.exceptions.ReverseRouteException)1 SyndEntry (com.sun.syndication.feed.synd.SyndEntry)1 SyndFeed (com.sun.syndication.feed.synd.SyndFeed)1 SyndFeedImpl (com.sun.syndication.feed.synd.SyndFeedImpl)1 SyndFeedOutput (com.sun.syndication.io.SyndFeedOutput)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Post (models.Post)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 ActionNotFoundException (play.exceptions.ActionNotFoundException)1 NoRouteFoundException (play.exceptions.NoRouteFoundException)1 PlayException (play.exceptions.PlayException)1 Request (play.mvc.Http.Request)1 Router (play.mvc.Router)1 ActionDefinition (play.mvc.Router.ActionDefinition)1