org.jooby.Request.require()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(343)

本文整理了Java中org.jooby.Request.require方法的一些代码示例,展示了Request.require的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Request.require方法的具体详情如下:
包路径:org.jooby.Request
类名称:Request
方法名:require

Request.require介绍

暂无

代码示例

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. public <T> T require(final Class<T> type) {
  3. return req.require(type);
  4. }

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. public <T> T require(final Key<T> key) {
  3. return req.require(key);
  4. }

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. public <T> T require(final TypeLiteral<T> type) {
  3. return req.require(type);
  4. }

代码示例来源:origin: jooby-project/jooby

  1. @SuppressWarnings("rawtypes")
  2. @Override
  3. public void handle(final Request req, final Response rsp) throws Exception {
  4. CommonProfile user = req.require(CommonProfile.class);
  5. Config config = req.require(Config.class);
  6. WebContext ctx = req.require(WebContext.class);
  7. AuthorizationChecker authorizationChecker = req.require(AuthorizationChecker.class);
  8. Map<String, Authorizer> authorizers = config.getAuthorizers();
  9. log.debug("checking access for: {}", user);
  10. if (!authorizationChecker.isAuthorized(ctx, Arrays.asList(user), this.authorizer, authorizers)) {
  11. log.debug("forbidden: {}", user);
  12. throw new Err(Status.FORBIDDEN);
  13. }
  14. log.debug("authorized: {}", user);
  15. }

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. public void handle(final Request req, final Response rsp) throws Throwable {
  3. MetricRegistry registry = req.require(MetricRegistry.class);
  4. Counter counter = registry.counter("request.actives");
  5. Timer.Context timer = registry.timer("request").time();
  6. counter.inc();
  7. rsp.complete((ereq, ersp, x) -> {
  8. timer.stop();
  9. counter.dec();
  10. Meter meter = registry.meter("responses." + rsp.status().orElse(Status.OK).value());
  11. meter.mark();
  12. });
  13. }

代码示例来源:origin: jooby-project/jooby

  1. @SuppressWarnings("rawtypes")
  2. private Object newBean(final Request req, final Response rsp, final Route.Chain chain,
  3. final Map<String, Mutant> params, final TypeLiteral type) throws Throwable {
  4. BeanPlan form = forms.get(type);
  5. if (form == null) {
  6. form = new BeanPlan(req.require(ParameterNameProvider.class), type);
  7. forms.put(type, form);
  8. }
  9. return form.newBean(p -> value(p, req, rsp, chain), params.keySet());
  10. }

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. public Object handle(final Request req) throws Throwable {
  3. Set<Route.Definition> routes = req.require(Route.KEY);
  4. WebPageProvider provider = req.require(name, WebPageProvider.class);
  5. List<WebPage> pages = routes.stream()
  6. .filter(filter)
  7. .flatMap(r -> provider.apply(r).stream())
  8. .peek(page -> {
  9. String name = page.getName();
  10. if (name.startsWith("/")) {
  11. name = name.substring(1);
  12. }
  13. page.setName(name);
  14. }).collect(Collectors.toList());
  15. return Results.ok(gen.apply(pages)).type(MediaType.xml);
  16. }

代码示例来源:origin: jooby-project/jooby

  1. static Provider profileProvider(AtomicReference<Registry> registry, Class profile,
  2. Function<Request, UserProfile> unauthenticated) {
  3. return () -> {
  4. Request req = registry.get().require(Request.class);
  5. ProfileManager pm = req.require(ProfileManager.class);
  6. Object result = pm.getAll(req.ifSession().isPresent()).stream()
  7. .filter(profile::isInstance)
  8. .findFirst()
  9. .orElse(null);
  10. if (result == null) {
  11. if (unauthenticated == null) {
  12. throw new Err(Status.FORBIDDEN, "Not found: " + profile.getSimpleName());
  13. }
  14. result = unauthenticated.apply(req);
  15. }
  16. return result;
  17. };
  18. }
  19. }

代码示例来源:origin: jooby-project/jooby

  1. public Object invoke(final Request req, final Response rsp, Route.Chain chain) {
  2. return Try.apply(() -> {
  3. Object target = req.require(implementingClass);
  4. List<RequestParam> parameters = provider.parameters(handler);
  5. Object[] args = new Object[parameters.size()];
  6. for (int i = 0; i < parameters.size(); i++) {
  7. args[i] = parameters.get(i).value(req, rsp, chain);
  8. }
  9. final Object result = handler.invoke(target, args);
  10. return result;
  11. }).unwrap(InvocationTargetException.class)
  12. .get();
  13. }
  14. }

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. public void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {
  3. Optional<String> origin = req.header("Origin").toOptional();
  4. Cors cors = this.cors.orElseGet(() -> req.require(Cors.class));
  5. if (cors.enabled() && origin.isPresent()) {
  6. cors(cors, req, rsp, origin.get());
  7. }
  8. chain.next(req, rsp);
  9. }

代码示例来源:origin: jooby-project/jooby

  1. @Override public void handle(Request req, Response rsp) {
  2. WebContext context = req.require(WebContext.class);
  3. String redirectTo = req.<String>ifGet("pac4j.logout.redirectTo").orElse(defaultUrl);
  4. conf.getLogoutLogic()
  5. .perform(context, conf, conf.getHttpActionAdapter(), redirectTo, logoutUrlPattern,
  6. localLogout, destroySession, centralLogout);
  7. }
  8. }

代码示例来源:origin: jooby-project/jooby

  1. @Override public void handle(Request req, Response rsp) {
  2. WebContext context = req.require(WebContext.class);
  3. pac4j.getCallbackLogic()
  4. .perform(context, pac4j, pac4j.getHttpActionAdapter(), defaultUrl, multiProfile,
  5. renewSession);
  6. }
  7. }

代码示例来源:origin: jooby-project/jooby

  1. @SuppressWarnings("unchecked")
  2. @Override
  3. public void handle(final Request req, final Response rsp) throws Throwable {
  4. // DON'T create a session for JWT/param/header auth (a.k.a stateless)
  5. Optional<Session> ifSession = req.ifSession();
  6. if (ifSession.isPresent()) {
  7. Session session = ifSession.get();
  8. Optional<String> profileId = session.unset(Auth.ID).toOptional();
  9. if (profileId.isPresent()) {
  10. Optional<CommonProfile> profile = req.require(AuthStore.class).unset(profileId.get());
  11. log.debug("logout {}", profile);
  12. session.destroy();
  13. }
  14. } else {
  15. log.debug("nothing to logout from session");
  16. }
  17. String redirectTo = req.<String> ifGet("auth.logout.redirectTo").orElse(this.redirectTo);
  18. rsp.redirect(redirectTo);
  19. }

代码示例来源:origin: jooby-project/jooby

  1. protected void handshake(final Request req, final Runnable handler) throws Exception {
  2. this.injector = req.require(Injector.class);
  3. this.renderers = ImmutableList.copyOf(injector.getInstance(Renderer.KEY));
  4. this.produces = req.route().produces();
  5. this.locals = req.attributes();
  6. this.lastEventId = req.header("Last-Event-ID");
  7. this.locale = req.locale();
  8. handshake(handler);
  9. }

代码示例来源:origin: jooby-project/jooby

  1. @Override public void handle(Request req, Response rsp) throws Throwable {
  2. PrometheusMeterRegistry registry = req.require(PrometheusMeterRegistry.class);
  3. String response = registry.scrape();
  4. rsp.type(MediaType.plain)
  5. .send(Results.ok(response));
  6. }
  7. }

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. public void handle(final Request req, final Response rsp, final Err ex) throws Throwable {
  3. log.error("execution of: {}{} resulted in exception\nRoute:\n{}\n\nStacktrace:",
  4. req.method(), req.path(), req.route().print(6), ex);
  5. Config conf = req.require(Config.class);
  6. boolean stackstrace = Try.apply(() -> conf.getBoolean("err.stacktrace"))
  7. .orElse(req.require(Env.class).name().equals("dev"));
  8. rsp.send(
  9. Results
  10. .when(MediaType.html, () -> Results.html(VIEW).put("err", ex.toMap(stackstrace)))
  11. .when(MediaType.all, () -> ex.toMap(stackstrace)));
  12. }

代码示例来源:origin: jooby-project/jooby

  1. @Override public void handle(Request req, Response rsp) {
  2. TimedSupport timed = TimedSupport.create(req.route());
  3. if (timed != null) {
  4. MeterRegistry registry = req.require(MeterRegistry.class);
  5. TimedSupport.Sample sample = timed.start(registry);
  6. rsp.complete((request, response, cause) -> sample.stop());
  7. }
  8. }
  9. }

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. protected void send(final Request req, final Response rsp, final Asset asset) throws Throwable {
  3. Env env = req.require(Env.class);
  4. CharSequence text = process(env, text(asset.stream()));
  5. rsp.type(asset.type())
  6. .send(text);
  7. }

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. public void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {
  3. RootUnitOfWork uow = (RootUnitOfWork) req.require(UnitOfWork.class);
  4. // start transaction
  5. uow.begin();
  6. rsp.after(after(uow));
  7. rsp.complete(complete(uow));
  8. // move next
  9. chain.next(req, rsp);
  10. }

代码示例来源:origin: jooby-project/jooby

  1. static void install(final Env env, final Config conf) {
  2. String path = conf.getString("crash.httpshell.path");
  3. Router router = env.router();
  4. router.get(path + "/{cmd:.*}", router.promise("direct", (req, deferred) -> {
  5. MediaType type = req.accepts(MediaType.json)
  6. .map(it -> MediaType.json)
  7. .orElse(MediaType.html);
  8. PluginContext ctx = req.require(PluginContext.class);
  9. ShellFactory shellFactory = ctx.getPlugin(ShellFactory.class);
  10. Shell shell = shellFactory.create(null);
  11. String cmd = req.param("cmd").value().replaceAll("/", " ");
  12. ShellProcess process = shell.createProcess(cmd);
  13. ShellProcessContext spc = new SimpleProcessContext(
  14. result -> deferred.resolve(result.type(type)));
  15. process.execute(spc);
  16. }));
  17. }

相关文章