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("&", "&");
// }
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);
}
}
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);
}
}
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);
}
}
Aggregations