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

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

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

Request.require介绍

暂无

代码示例

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

static Provider profileProvider(AtomicReference<Registry> registry, Class profile,
   Function<Request, UserProfile> unauthenticated) {
  return () -> {
   Request req = registry.get().require(Request.class);
   ProfileManager pm = req.require(ProfileManager.class);
   Object result = pm.getAll(req.ifSession().isPresent()).stream()
     .filter(profile::isInstance)
     .findFirst()
     .orElse(null);
   if (result == null) {
    if (unauthenticated == null) {
     throw new Err(Status.FORBIDDEN, "Not found: " + profile.getSimpleName());
    }
    result = unauthenticated.apply(req);
   }
   return result;
  };
 }
}

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

public Object invoke(final Request req, final Response rsp, Route.Chain chain) {
  return Try.apply(() -> {
   Object target = req.require(implementingClass);

   List<RequestParam> parameters = provider.parameters(handler);
   Object[] args = new Object[parameters.size()];
   for (int i = 0; i < parameters.size(); i++) {
    args[i] = parameters.get(i).value(req, rsp, chain);
   }

   final Object result = handler.invoke(target, args);

   return result;
  }).unwrap(InvocationTargetException.class)
    .get();
 }
}

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

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

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

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

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

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

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

@SuppressWarnings("unchecked")
@Override
public void handle(final Request req, final Response rsp) throws Throwable {
 // DON'T create a session for JWT/param/header auth (a.k.a stateless)
 Optional<Session> ifSession = req.ifSession();
 if (ifSession.isPresent()) {
  Session session = ifSession.get();
  Optional<String> profileId = session.unset(Auth.ID).toOptional();
  if (profileId.isPresent()) {
   Optional<CommonProfile> profile = req.require(AuthStore.class).unset(profileId.get());
   log.debug("logout {}", profile);
   session.destroy();
  }
 } else {
  log.debug("nothing to logout from session");
 }
 String redirectTo = req.<String> ifGet("auth.logout.redirectTo").orElse(this.redirectTo);
 rsp.redirect(redirectTo);
}

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

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

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

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

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

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

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

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

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

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

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

@Override
public void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {
 RootUnitOfWork uow = (RootUnitOfWork) req.require(UnitOfWork.class);
 // start transaction
 uow.begin();
 rsp.after(after(uow));
 rsp.complete(complete(uow));
 // move next
 chain.next(req, rsp);
}

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

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

相关文章