use of org.webpieces.ctx.api.RouterRequest in project webpieces by deanhiller.
the class ParamToObjectTranslatorImpl method translate.
private Object translate(RouterRequest req, Method method, ParamNode valuesToUse, Meta fieldMeta, Validation validator) {
Class<?> fieldClass = fieldMeta.getFieldClass();
ObjectStringConverter<?> converter = objectTranslator.getConverter(fieldClass);
if (converter != null) {
return convert(req, method, valuesToUse, fieldMeta, converter, validator);
} else if (fieldClass.isArray()) {
throw new UnsupportedOperationException("not done yet...let me know and I will do it=" + fieldMeta);
} else if (fieldClass.isEnum()) {
throw new UnsupportedOperationException("You need to install a " + ObjectStringConverter.class.getSimpleName() + " for this enum " + fieldMeta);
} else if (List.class.isAssignableFrom(fieldClass)) {
if (valuesToUse == null)
return new ArrayList<>();
else if (valuesToUse instanceof ArrayNode) {
List<ParamNode> paramNodes = ((ArrayNode) valuesToUse).getList();
return createList(req, method, fieldMeta, validator, paramNodes);
} else if (valuesToUse instanceof ValueNode) {
List<ParamNode> paramNodes = new ArrayList<>();
paramNodes.add(valuesToUse);
return createList(req, method, fieldMeta, validator, paramNodes);
}
throw new IllegalArgumentException("Found List on field or param=" + fieldMeta + " but did not find ArrayNode type");
} else if (valuesToUse instanceof ArrayNode) {
throw new IllegalArgumentException("Incoming array need a type List but instead found type=" + fieldClass + " on field=" + fieldMeta);
} else if (valuesToUse instanceof ValueNode) {
ValueNode v = (ValueNode) valuesToUse;
String fullName = v.getFullName();
throw new IllegalArgumentException("Could not convert incoming value=" + v.getValue() + " of key name=" + fullName + " field=" + fieldMeta);
} else if (valuesToUse == null) {
//validate if null is ok or not
fieldMeta.validateNullValue();
return null;
} else if (!(valuesToUse instanceof ParamTreeNode)) {
throw new IllegalStateException("Bug, must be missing a case. v=" + valuesToUse + " type to field=" + fieldMeta);
}
ParamTreeNode tree = (ParamTreeNode) valuesToUse;
EntityLookup pluginLookup = fetchPluginLoader(fieldClass);
Object bean = null;
if (pluginLookup != null) {
bean = pluginLookup.find(fieldMeta, tree, c -> createBean(c));
if (bean == null)
throw new IllegalStateException("plugin=" + pluginLookup.getClass() + " failed to create bean. This is a plugin bug");
} else
bean = createBean(fieldClass);
for (Map.Entry<String, ParamNode> entry : tree.entrySet()) {
String key = entry.getKey();
ParamNode value = entry.getValue();
Field field = findBeanFieldType(bean.getClass(), key, new ArrayList<>());
FieldMeta nextFieldMeta = new FieldMeta(field);
Object translatedValue = translate(req, method, value, nextFieldMeta, validator);
nextFieldMeta.setValueOnBean(bean, translatedValue);
}
return bean;
}
use of org.webpieces.ctx.api.RouterRequest in project webpieces by deanhiller.
the class ParamToObjectTranslatorImpl method createArgsImpl.
protected List<Object> createArgsImpl(Method method, RequestContext ctx, BodyContentBinder binder) {
RouterRequest req = ctx.getRequest();
Parameter[] paramMetas = method.getParameters();
Annotation[][] paramAnnotations = method.getParameterAnnotations();
ParamTreeNode paramTree = new ParamTreeNode();
//For multipart AND for query params such as ?var=xxx&var=yyy&var2=xxx AND for url path params /mypath/{var1}/account/{id}
//query params first
Map<String, String> queryParams = translate(req.queryParams);
treeCreator.createTree(paramTree, queryParams, FromEnum.QUERY_PARAM);
//next multi-part params
Map<String, String> multiPartParams = translate(req.multiPartFields);
treeCreator.createTree(paramTree, multiPartParams, FromEnum.FORM_MULTIPART);
//lastly path params
treeCreator.createTree(paramTree, ctx.getPathParams(), FromEnum.URL_PATH);
List<Object> result = new ArrayList<>();
for (int i = 0; i < paramMetas.length; i++) {
Parameter paramMeta = paramMetas[i];
Annotation[] annotations = paramAnnotations[i];
ParamMeta fieldMeta = new ParamMeta(method, paramMeta, annotations);
String name = fieldMeta.getName();
ParamNode paramNode = paramTree.get(name);
if (binder != null && isManagedBy(binder, fieldMeta)) {
Object bean = binder.unmarshal(fieldMeta.getFieldClass(), req.body.createByteArray());
result.add(bean);
} else {
Object arg = translate(req, method, paramNode, fieldMeta, ctx.getValidation());
result.add(arg);
}
}
return result;
}
use of org.webpieces.ctx.api.RouterRequest in project webpieces by deanhiller.
the class DevRoutingService method fetchNotFoundRoute.
public NotFoundInfo fetchNotFoundRoute(NotFoundException e, RouterRequest req) {
//Production app's notFound route TBD and used in iframe later
RouteMeta origMeta = routeLoader.fetchNotFoundRoute(req.domain);
if (req.queryParams.containsKey("webpiecesShowPage")) {
//This is actually a callback from the below code's iframe!!!
if (origMeta.getControllerInstance() == null) {
finder.loadControllerIntoMetaObject(origMeta, false);
finder.loadFiltersIntoMeta(origMeta, origMeta.getFilters(), false);
}
Service<MethodMeta, Action> svc = origMeta.getService222();
return new NotFoundInfo(origMeta, svc, req);
}
log.error("(Development only log message) Route not found!!! Either you(developer) typed the wrong url OR you have a bad route. Either way,\n" + " something needs a'fixin. req=" + req, e);
RouteImpl r = new RouteImpl("/org/webpieces/devrouter/impl/NotFoundController.notFound", RouteType.NOT_FOUND);
RouteModuleInfo info = new RouteModuleInfo("", null);
RouteMeta meta = new RouteMeta(r, origMeta.getInjector(), info, config.getUrlEncoding());
if (meta.getControllerInstance() == null) {
finder.loadControllerIntoMetaObject(meta, false);
meta.setService(serviceCreator.create());
}
String reason = "Your route was not found in routes table";
if (e != null)
reason = e.getMessage();
RouterRequest newRequest = new RouterRequest();
newRequest.putMultipart("webpiecesError", "Exception message=" + reason);
newRequest.putMultipart("url", req.relativePath);
return new NotFoundInfo(meta, meta.getService222(), newRequest);
}
use of org.webpieces.ctx.api.RouterRequest in project webpieces by deanhiller.
the class NotFoundController method notFound.
public Action notFound() {
RouterRequest request = Current.request();
String error = request.getSingleMultipart("webpiecesError");
String url = request.getSingleMultipart("url");
if (url.contains("?")) {
url += "&webpiecesShowPage=true";
} else {
url += "?webpiecesShowPage=true";
}
return Actions.renderThis("error", error, "url", url);
}
use of org.webpieces.ctx.api.RouterRequest in project webpieces by deanhiller.
the class ErrorTest method testNoMethod.
@Test
public void testNoMethod() {
log.info("starting");
String moduleFileContents = NoMethodRouterModules.class.getName();
RouterService server = ErrorCommonTest.createServer(true, moduleFileContents);
try {
server.start();
Assert.fail("Should have thrown exception on start since this is prod");
} catch (RuntimeException e) {
Assert.assertTrue(e.getMessage().contains("Cannot find 'public' method='thisMethodNotExist' on class="));
}
RouterRequest req = RequestCreation.createHttpRequest(HttpMethod.GET, "/something");
MockResponseStream mockResponseStream = new MockResponseStream();
server.incomingCompleteRequest(req, mockResponseStream);
Exception e = mockResponseStream.getOnlyException();
Assert.assertEquals(IllegalStateException.class, e.getClass());
Assert.assertTrue(e.getMessage().contains("start was not called by client or start threw"));
}
Aggregations