use of ratpack.http.Request in project ratpack by ratpack.
the class FileHandler method currentUriWithTrailingSlash.
private String currentUriWithTrailingSlash(Context context) {
Request request = context.getRequest();
String redirectUri = "/" + request.getPath() + "/";
String query = request.getQuery();
if (!query.isEmpty()) {
redirectUri += "?" + query;
}
return redirectUri;
}
use of ratpack.http.Request in project ratpack by ratpack.
the class RatpackPac4j method initiateAuthentication.
private static void initiateAuthentication(Context ctx, Class<? extends Client<?, ?>> clientType) {
Request request = ctx.getRequest();
Clients clients = ctx.get(Clients.class);
Client<?, ?> client = clients.findClient(clientType);
RatpackWebContext.from(ctx, false).then(webContext -> {
webContext.getSession().set(Pac4jSessionKeys.REQUESTED_URL, request.getUri());
try {
client.redirect(webContext, true);
} catch (Exception e) {
if (e instanceof RequiresHttpAction) {
webContext.sendResponse((RequiresHttpAction) e);
return;
} else {
ctx.error(new TechnicalException("Failed to redirect", e));
}
}
webContext.sendResponse();
});
}
use of ratpack.http.Request in project ratpack by ratpack.
the class WebSocketEngine method connect.
@SuppressWarnings("deprecation")
public static <T> void connect(final Context context, String path, int maxLength, final WebSocketHandler<T> handler) {
PublicAddress publicAddress = context.get(PublicAddress.class);
URI address = publicAddress.get(context);
URI httpPath = address.resolve(path);
URI wsPath;
try {
wsPath = new URI("ws", httpPath.getUserInfo(), httpPath.getHost(), httpPath.getPort(), httpPath.getPath(), httpPath.getQuery(), httpPath.getFragment());
} catch (URISyntaxException e) {
throw uncheck(e);
}
WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(wsPath.toString(), null, false, maxLength);
Request request = context.getRequest();
HttpMethod method = valueOf(request.getMethod().getName());
FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri());
nettyRequest.headers().add(SEC_WEBSOCKET_VERSION, request.getHeaders().get(SEC_WEBSOCKET_VERSION));
nettyRequest.headers().add(SEC_WEBSOCKET_KEY, request.getHeaders().get(SEC_WEBSOCKET_KEY));
final WebSocketServerHandshaker handshaker = factory.newHandshaker(nettyRequest);
final DirectChannelAccess directChannelAccess = context.getDirectChannelAccess();
final Channel channel = directChannelAccess.getChannel();
if (!channel.config().isAutoRead()) {
channel.config().setAutoRead(true);
}
handshaker.handshake(channel, nettyRequest).addListener(new HandshakeFutureListener<>(context, handshaker, handler));
}
use of ratpack.http.Request in project ratpack by ratpack.
the class InferringPublicAddress method get.
@Override
public URI get() {
Request request = Execution.current().maybeGet(Request.class).orElseThrow(() -> new IllegalStateException("Inferring the public address is only supported during a request execution."));
String scheme = determineScheme(request);
String host;
int port;
HostAndPort forwardedHostData = getForwardedHostData(request);
if (forwardedHostData != null) {
host = forwardedHostData.getHost();
port = forwardedHostData.getPortOrDefault(-1);
} else {
URI absoluteRequestURI = getAbsoluteRequestUri(request);
if (absoluteRequestURI != null) {
host = absoluteRequestURI.getHost();
port = absoluteRequestURI.getPort();
} else {
HostAndPort hostData = getHostData(request);
if (hostData != null) {
host = hostData.getHost();
port = hostData.getPortOrDefault(-1);
} else {
HostAndPort localAddress = request.getLocalAddress();
host = localAddress.getHost();
port = ProtocolUtil.isDefaultPortForScheme(localAddress.getPort(), scheme) ? -1 : localAddress.getPort();
}
}
}
try {
return new URI(scheme, null, host, port, null, null, null);
} catch (URISyntaxException ex) {
throw Exceptions.uncheck(ex);
}
}
use of ratpack.http.Request in project ratpack by ratpack.
the class DefaultBlockingExecTimingInterceptor method intercept.
@Override
public void intercept(Execution execution, ExecType type, Block executionSegment) throws Exception {
if (type == ExecType.BLOCKING) {
Optional<Request> requestOpt = execution.maybeGet(Request.class);
if (requestOpt.isPresent()) {
Request request = requestOpt.get();
String tag = buildBlockingTimerTag(request.getPath(), request.getMethod().getName());
Timer.Context timer = metricRegistry.timer(tag).time();
try {
executionSegment.execute();
} finally {
timer.stop();
}
return;
}
}
executionSegment.execute();
}
Aggregations