@SuppressWarnings("unchecked") @Override public Map<String, ?> parseRequestBody(Req req, byte[] body) throws Exception { return Customization.of(req).objectMapper().readValue(body, Map.class); }
@Override public void render(Req req, Object value, OutputStream out) throws Exception { Customization.of(req).objectMapper().writeValue(out, value); }
@Override public Object getParamValue(Req req, Class<?> paramType, String paramName, Map<String, Object> properties) { return Customization.of(req).objectMapper().convertValue(properties, paramType); }
@SuppressWarnings("unchecked") @Override public Map<String, ?> parseRequestBody(Req req, byte[] body) throws Exception { return Customization.of(req).xmlMapper().readValue(body, Map.class); }
public static BasicConfig zone(Req req) { Customization custom = Customization.of(req); return zone(custom, req.zone()); }
@Override public void render(Req req, Object value, OutputStream out) throws Exception { Customization.of(req).xmlMapper().writeValue(out, value); }
private Set<String> userRoles(Req req, String username) { if (username != null) { try { return Customization.of(req).rolesProvider().getRolesForUser(req, username); } catch (Exception e) { throw U.rte(e); } } else { return Collections.emptySet(); } }
@Override public Object handleError(Req req, Resp resp, Throwable error) { resp.result(null); Customization custom = Customization.of(req); Object result = handleError(req, resp, error, custom); if (result instanceof Throwable) { Throwable errResult = (Throwable) result; return renderError(req, resp, errResult); } else { return result; } }
public static UserInfo login(Req req, String username, String password) { LoginProvider loginProvider = Customization.of(req).loginProvider(); U.must(loginProvider != null, "A login provider wasn't set!"); RolesProvider rolesProvider = Customization.of(req).rolesProvider(); U.must(rolesProvider != null, "A roles provider wasn't set!"); try { boolean success = loginProvider.login(req, username, password); if (success) { Set<String> roles = rolesProvider.getRolesForUser(req, username); return new UserInfo(username, roles); } } catch (Throwable e) { throw U.rte("Login error!", e); } return null; }
public static byte[] renderPage(ReqImpl req, String content) { PageDecorator pageDecorator = Customization.of(req).pageDecorator(); U.must(pageDecorator != null, "A page decorator wasn't configured!"); ReusableWritable out = Msc.locals().pageRenderingStream(); try { pageDecorator.renderPage(req, content, out); } catch (Exception e) { throw U.rte("Error while rendering page!", e); } return out.copy(); }
private void internalServerError(final Channel channel, final boolean isKeepAlive, final Req req) { MediaType contentType = req != null ? req.contentType() : HttpUtils.getDefaultContentType(); HttpResponseRenderer jsonRenderer = Customization.of(req).jsonResponseRenderer(); RespBody body = new RespBodyBytes(HttpUtils.responseToBytes(req, INTERNAL_SERVER_ERROR, contentType, jsonRenderer)); HttpIO.INSTANCE.respond(HttpUtils.maybe(req), channel, -1, -1, 500, isKeepAlive, contentType, body, null, null); }
private static HttpResponseRenderer mediaResponseRenderer(Resp resp) { Customization customization = Customization.of(resp.request()); if (resp.contentType().equals(MediaType.JSON)) { return customization.jsonResponseRenderer(); } else if (resp.contentType().equals(MediaType.XML_UTF_8)) { return customization.xmlResponseRenderer(); } else { // defaults to json return customization.jsonResponseRenderer(); } }
@SuppressWarnings("unchecked") @Override public <P> P openPersister(Ctx ctx) { Req req = ctx.exchange(); Customization custom = Customization.of(req); EntityManagerProvider entityManagerProvider = custom.entityManagerProvider(); if (entityManagerProvider != null) { try { return (P) entityManagerProvider.getEntityManager(req); } catch (Exception e) { throw U.rte("Error occurred in the EntityManager provider!", e); } } EntityManagerFactory emf; EntityManagerFactoryProvider entityManagerFactoryProvider = custom.entityManagerFactoryProvider(); if (entityManagerFactoryProvider != null) { try { emf = entityManagerFactoryProvider.getEntityManagerFactory(req); } catch (Exception e) { throw U.rte("Error occurred in the EntityManagerFactory provider!", e); } } else { throw U.rte("No EntityManagerProvider nor EntityManagerFactoryProvider was configured!"); } try { return (P) emf.createEntityManager(); } catch (Exception e) { throw U.rte("Error occurred while creating an EntityManager!", e); } }
HttpUtils.validateViewName(viewName); Customization custom = Customization.of(req);
void error(final Req req, final Throwable error, LogLevel logLevel) { try { logError(req, error, logLevel); Resp resp = req.response().code(500).result(null); Object result = Customization.of(req).errorHandler().handleError(req, resp, error); HttpUtils.resultOf(req, result); } catch (Exception e) { Log.error("An error occurred inside the error handler!", e); HttpUtils.resultToResponse(req, HttpUtils.getErrorInfo(req.response(), e)); } }
@SuppressWarnings("unchecked") @Override public Map<String, ?> parseRequestBody(Req req, byte[] body) throws Exception { return Customization.of(req).objectMapper().readValue(body, Map.class); }
@Override public void render(Req req, Object value, OutputStream out) throws Exception { Customization.of(req).xmlMapper().writeValue(out, value); }
@SuppressWarnings("unchecked") @Override public Map<String, ?> parseRequestBody(Req req, byte[] body) throws Exception { return Customization.of(req).xmlMapper().readValue(body, Map.class); }
public static byte[] renderPage(ReqImpl req, String content) { PageDecorator pageDecorator = Customization.of(req).pageDecorator(); U.must(pageDecorator != null, "A page decorator wasn't configured!"); ReusableWritable out = Msc.locals().pageRenderingStream(); try { pageDecorator.renderPage(req, content, out); } catch (Exception e) { throw U.rte("Error while rendering page!", e); } return out.copy(); }
private static HttpResponseRenderer mediaResponseRenderer(Resp resp) { Customization customization = Customization.of(resp.request()); if (resp.contentType().equals(MediaType.JSON)) { return customization.jsonResponseRenderer(); } else if (resp.contentType().equals(MediaType.XML_UTF_8)) { return customization.xmlResponseRenderer(); } else { // defaults to json return customization.jsonResponseRenderer(); } }