private void forbiddenResponse(Response response) { response.status(HttpResponseStatus.FORBIDDEN.code()); response.send(); }
public static void sendFile(Context context, Path file, BasicFileAttributes attributes) { Date date = new Date(attributes.lastModifiedTime().toMillis()); context.lastModified(date, () -> { final String ifNoneMatch = context.getRequest().getHeaders().get(HttpHeaderNames.IF_NONE_MATCH); Response response = context.getResponse(); if (ifNoneMatch != null && ifNoneMatch.trim().equals("*")) { response.status(NOT_MODIFIED.code()).send(); return; } response.contentTypeIfNotSet(() -> context.get(MimeTypes.class).getContentType(file.getFileName().toString())); response.getHeaders().set(HttpHeaderConstants.CONTENT_LENGTH, Long.toString(attributes.size())); try { response.sendFile(file); } catch (Exception e) { throw Exceptions.uncheck(e); } }); }
@Override public void render(Context ctx, CharSequence charSequence) { ctx.getResponse().send(charSequence.toString()); }
public void send(CharSequence contentType, byte[] bytes) { contentType(contentType).send(bytes); }
private void onErrorHandlerError(ServerErrorHandler serverErrorHandler, Throwable original, Throwable errorHandlerThrowable) { String msg = "Throwable thrown by error handler " + serverErrorHandler + " while handling throwable\n" + "Original throwable: " + getStackTraceAsString(original) + "\n" + "Error handler throwable: " + getStackTraceAsString(errorHandlerThrowable); LOGGER.error(msg); Response response = requestConstants.response.status(500); if (getServerConfig().isDevelopment()) { response.send(msg); } else { response.send(); } }
public void send(String text) { ByteBuf byteBuf = ByteBufUtil.encodeString(byteBufAllocator, CharBuffer.wrap(text), CharsetUtil.UTF_8); contentTypeIfNotSet(HttpHeaderConstants.PLAIN_TEXT_UTF8).send(byteBuf); }
@Override public void error(Context context, Throwable throwable) throws Exception { if (LOGGER.isWarnEnabled()) { LOGGER.warn(getMsg(ServerErrorHandler.class, "server error", context) + "\n", throwable); } context.getResponse().status(500).send(); }
@Override public void error(Context context, int statusCode) throws Exception { if (LOGGER.isWarnEnabled()) { LOGGER.warn(getMsg(ClientErrorHandler.class, "client error", context)); } context.getResponse().status(statusCode).send(); }
public void handle(Context context) throws Exception { Handler handler = reloadHandler.create(); if (handler == null) { context.getResponse().send("script file does not exist:" + script.toAbsolutePath()); } else { handler.handle(context); } }
@Override public void lastModified(Instant lastModified, Runnable runnable) { Instant ifModifiedSince = requestConstants.request.getHeaders().getInstant(IF_MODIFIED_SINCE); if (ifModifiedSince != null) { // Normalise to second resolution ifModifiedSince = Instant.ofEpochSecond(ifModifiedSince.getEpochSecond()); lastModified = Instant.ofEpochSecond(lastModified.getEpochSecond()); if (!lastModified.isAfter(ifModifiedSince)) { requestConstants.response.status(Status.NOT_MODIFIED).send(); return; } } requestConstants.response.getHeaders().setDate(HttpHeaderConstants.LAST_MODIFIED, lastModified); runnable.run(); }
public void sendResponse() { int statusCode = response.getStatus().getCode(); if (statusCode >= 400) { context.clientError(statusCode); } else { response.send(MediaType.TEXT_HTML, responseContent); } }
protected void sendResponseData(Context ctx, ResponseData r) { ctx.getResponse() .status(r.status) .contentType(r.contentType) .send(r.text); }
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") @Override public void render(Context ctx, HealthCheckResults healthCheckResults) throws Exception { ByteBuf buffer = byteBufAllocator.buffer(); try (Writer writer = new OutputStreamWriter(new BufferedOutputStream(new ByteBufOutputStream(buffer)))) { healthCheckResults.writeTo(writer); } catch (Exception e) { buffer.release(); throw e; } ctx.getResponse() .contentTypeIfNotSet(HttpHeaderConstants.PLAIN_TEXT_UTF8) .status(healthCheckResults.isUnhealthy() ? 503 : 200) .send(buffer); } }
@Override public void render(Context context) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(out, CharsetUtil.encoder(getEncoding())); MarkupBuilder markupBuilder = new MarkupBuilder(writer); ClosureUtil.configureDelegateFirst(markupBuilder, markupBuilder, getDefinition()); context.getResponse().contentType(getContentType()).send(out.toByteArray()); } }
@Override public void render(Context context, JsonRender object) throws Exception { ObjectWriter writer = object.getObjectWriter(); if (writer == null) { writer = defaultObjectWriter; } ByteBuf buffer = context.get(ByteBufAllocator.class).buffer(); OutputStream outputStream = new ByteBufOutputStream(buffer); try { writer.writeValue(outputStream, object.getObject()); } catch (JsonProcessingException e) { buffer.release(); context.error(e); return; } context.getResponse() .contentTypeIfNotSet(HttpHeaderConstants.JSON) .send(buffer); }
@Override public void redirect(Context context, int code, Object to) { String stringValue; if (to instanceof URI) { stringValue = ((URI) to).toASCIIString(); } else { stringValue = to.toString(); } context.getResponse().status(code); String normalizedLocation = generateRedirectLocation(context, context.getRequest(), stringValue); context.getResponse().getHeaders().set(HttpHeaderConstants.LOCATION, normalizedLocation); context.getResponse().send(); }
@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"); } }); } }
public void render(final Context ctx, final TextTemplate template) throws Exception { engine.renderTemplate(template.getId(), template.getModel()) .then(byteBuf -> { String type = template.getType(); if (type == null) { type = ctx.get(MimeTypes.class).getContentType(template.getId()); } ctx.getResponse().contentType(type).send(byteBuf); }); } }
public void handle(Context context) { ratpack.http.HttpMethod requestMethod = context.getRequest().getMethod(); if (requestMethod == method || requestMethod.name(method.getName())) { context.next(); } else if (requestMethod.isOptions()) { Response response = context.getResponse(); response.getHeaders().add(HttpHeaderConstants.ALLOW, method); response.status(200).send(); } else { context.clientError(405); } } }