ratpack.handling.Context Java Examples
The following examples show how to use
ratpack.handling.Context.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserHandler.java From tutorials with MIT License | 6 votes |
@Override public void handle(Context context) throws Exception { context.parse(Map.class) .then(payload -> { Map<String, Object> parameters = (Map<String, Object>) payload.get("parameters"); ExecutionResult executionResult = graphql.execute(payload.get(SchemaUtils.QUERY) .toString(), null, this, parameters); Map<String, Object> result = new LinkedHashMap<>(); if (executionResult.getErrors() .isEmpty()) { result.put(SchemaUtils.DATA, executionResult.getData()); } else { result.put(SchemaUtils.ERRORS, executionResult.getErrors()); LOGGER.warning("Errors: " + executionResult.getErrors()); } context.render(json(result)); }); }
Example #2
Source File: RedirectHandler.java From tutorials with MIT License | 5 votes |
@Override public void handle(Context ctx) throws Exception { HttpClient client = ctx.get(HttpClient.class); URI uri = URI.create("http://localhost:5050/employee/1"); Promise<ReceivedResponse> responsePromise = client.get(uri); responsePromise.map(response -> response.getBody() .getText() .toUpperCase()) .then(responseText -> ctx.getResponse() .send(responseText)); }
Example #3
Source File: DefaultServerTracingHandler.java From ratpack-zipkin with Apache License 2.0 | 5 votes |
@Override public void handle(Context ctx) throws Exception { ServerRequest request = new ServerRequestImpl(ctx.getRequest()); final Span span = handler.handleReceive(extractor, request); ctx.getResponse().beforeSend(response -> { ServerResponse serverResponse = new ServerResponseImpl(response, request, ctx.getPathBinding()); handler.handleSend(serverResponse, null, span); }); //place the Span in scope so that downstream code (e.g. Ratpack handlers //further on in the chain) can see the Span. try (Tracer.SpanInScope scope = tracing.tracer().withSpanInScope(span)) { ctx.next(); } }
Example #4
Source File: DefaultServerTracingHandler.java From ratpack-zipkin with Apache License 2.0 | 5 votes |
@Override public String getUrl() { PublicAddress publicAddress = request.get(Context.class).get(PublicAddress.class); return publicAddress.builder() .path(request.getPath()) .params(request.getQueryParams()) .build().toString(); }
Example #5
Source File: EmployeeHandler.java From tutorials with MIT License | 5 votes |
@Override public void handle(Context ctx) throws Exception { EmployeeRepository repository = ctx.get(EmployeeRepository.class); Long id = Long.valueOf(ctx.getPathTokens() .get("id")); Promise<Employee> employeePromise = repository.findEmployeeById(id); employeePromise.map(employee -> employee.getName()) .then(name -> ctx.getResponse() .send(name)); }
Example #6
Source File: RpcProxyHandler.java From consensusj with Apache License 2.0 | 5 votes |
@Override public void handle(Context ctx) { ctx.parse(fromJson(JsonRpcRequest.class)).then(rpcReq -> { if (allowedMethods.contains(rpcReq.getMethod())) { rpcClient.call(rpcReq).then(rpcResponse -> ctx.render(json(rpcResponse))); } else { // Should we send a JsonRpcResponse here? ctx.getResponse().status(403).send("JSON-RPC method not allowed by proxy"); } }); }
Example #7
Source File: RateLimiterHandler.java From resilience4j with Apache License 2.0 | 5 votes |
@Override public void handle(Context ctx) throws Exception { boolean permission = rateLimiter.acquirePermission(); if (Thread.interrupted()) { throw new IllegalStateException("Thread was interrupted during permission wait"); } if (!permission) { Throwable t = createRequestNotPermitted(rateLimiter); ctx.error(t); } else { ctx.next(); } }
Example #8
Source File: BaseWorldHandler.java From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License | 5 votes |
protected int parseQueryCount(Context ctx) { String textValue = ctx.getRequest().getQueryParams().get("queries"); if (textValue == null) { return 1; } int parsedValue; try { parsedValue = Integer.parseInt(textValue); } catch (NumberFormatException e) { return 1; } return Math.min(500, Math.max(1, parsedValue)); }
Example #9
Source File: HeaderHandler.java From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void handle(Context ctx) { MutableHeaders headers = ctx.getResponse().getHeaders(); headers.set(HttpHeaderNames.DATE, dateHelper.getDate()); headers.set(SERVER, RATPACK); ctx.next(); }
Example #10
Source File: FortuneHandler.java From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License | 5 votes |
public void handle(Context ctx, DbRepository repository) { repository.fortunes().then(fortunes -> { fortunes.add(new Fortune(0, "Additional fortune added at request time.")); fortunes.sort(Comparator.comparing(fortune -> fortune.message)); ctx.render(Template.handlebarsTemplate("fortunes", Collections.singletonMap("fortunes", fortunes), "text/html; charset=UTF-8")); }); }
Example #11
Source File: RequestValidatorFilter.java From tutorials with MIT License | 4 votes |
@Override public void handle(Context ctx) throws Exception { MutableHeaders headers = ctx.getResponse().getHeaders(); headers.set("Access-Control-Allow-Origin", "*"); ctx.next(); }
Example #12
Source File: ratpack.java From jbang with MIT License | 4 votes |
void renderName(Context ctx) { var name = ctx.getPathTokens().get("name"); System.out.println(String.format("Hello %s", name)); ctx.render(String.format("[renderName] Hello <%s>!\n", name)); }
Example #13
Source File: FooHandler.java From tutorials with MIT License | 4 votes |
@Override public void handle(Context ctx) throws Exception { ctx.getResponse() .send("Hello Foo!"); }
Example #14
Source File: PlainTextHandler.java From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public void handle(Context ctx) { ctx.getResponse().send(MESSAGE); }
Example #15
Source File: JsonHandler.java From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public void handle(Context ctx) { ctx.render(json(Collections.singletonMap(MESSAGE, HELLO))); }
Example #16
Source File: QueryHandler.java From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License | 4 votes |
public void handle(Context ctx, DbRepository repository) { int queries = parseQueryCount(ctx); repository.getWorlds(getNumbers(queries)).then(result -> ctx.render(json(result))); }
Example #17
Source File: UpdateHandler.java From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License | 4 votes |
public void handle(Context ctx, DbRepository repository) { int queries = parseQueryCount(ctx); repository.findAndUpdateWorlds(getNumbers(queries), getNumbers(queries)).then(result -> ctx.render(json(result))); }
Example #18
Source File: DbHandler.java From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License | 4 votes |
public void handle(Context ctx, DbRepository repository) { repository.getWorld(randomWorldNumber()).then(result -> ctx.render(json(result))); }
Example #19
Source File: ChainStatusHandler.java From consensusj with Apache License 2.0 | 4 votes |
@Override public void handle(Context ctx) { JsonRpcRequest rpcReq = new JsonRpcRequest("getblockchaininfo"); rpcClient.call(rpcReq).then(rpcResponse -> ctx.render(json(rpcResponse))); }
Example #20
Source File: GenerateHandler.java From consensusj with Apache License 2.0 | 4 votes |
@Override public void handle(Context ctx) { JsonRpcRequest rpcReq = new JsonRpcRequest("setgenerate", Arrays.asList(true, 1)); rpcClient.call(rpcReq).then(rpcResponse -> ctx.render(json(rpcResponse))); }
Example #21
Source File: RatpackITest.java From java-specialagent with Apache License 2.0 | 4 votes |
public static void main(final String[] args) throws Exception { final RatpackServer server = RatpackServer.start(new Action<RatpackServerSpec>() { @Override public void execute(final RatpackServerSpec ratpackServerSpec) { ratpackServerSpec.handlers(new Action<Chain>() { @Override public void execute(final Chain chain) { chain.get(new Handler() { @Override public void handle(final Context context) { TestUtil.checkActiveSpan(); context.render("Test"); } }); } }); } }); final HttpClient client = HttpClient.of(new Action<HttpClientSpec>() { @Override public void execute(final HttpClientSpec httpClientSpec) { httpClientSpec .poolSize(10) .maxContentLength(ServerConfig.DEFAULT_MAX_CONTENT_LENGTH) .readTimeout(Duration.of(60, ChronoUnit.SECONDS)) .byteBufAllocator(PooledByteBufAllocator.DEFAULT); } }); try (final ExecHarness harness = ExecHarness.harness()) { final ExecResult<ReceivedResponse> result = harness.yield(new Function<Execution,Promise<ReceivedResponse>>() { @Override public Promise<ReceivedResponse> apply(final Execution execution) { return client.get(URI.create("http://localhost:5050")); } }); final int statusCode = result.getValue().getStatusCode(); if (200 != statusCode) throw new AssertionError("ERROR: response: " + statusCode); } server.stop(); TestUtil.checkSpan(new ComponentSpanCount("netty", 2, true)); }
Example #22
Source File: ratpack.java From jbang with MIT License | 4 votes |
void renderWorld(Context ctx) { var message = "Hello World!"; System.out.println(message); ctx.render(String.format("[renderWorld] %s\n", message)); }