use of ratpack.server.PublicAddress in project ratpack by ratpack.
the class Pac4jAuthenticator method createClients.
private Promise<Clients> createClients(Context ctx, PathBinding pathBinding) throws Exception {
String boundTo = pathBinding.getBoundTo();
PublicAddress publicAddress = ctx.get(PublicAddress.class);
String absoluteCallbackUrl = publicAddress.get(b -> b.maybeEncodedPath(boundTo).maybeEncodedPath(path)).toASCIIString();
Iterable<? extends Client<?, ?>> result = clientsProvider.get(ctx);
@SuppressWarnings("rawtypes") List<Client> clients;
if (result instanceof List) {
clients = Types.cast(result);
} else {
clients = ImmutableList.copyOf(result);
}
return Promise.value(new Clients(absoluteCallbackUrl, clients));
}
use of ratpack.server.PublicAddress 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.server.PublicAddress in project ratpack by ratpack.
the class DefaultRedirector method generateRedirectLocation.
private String generateRedirectLocation(Context ctx, Request request, String path) {
//Rules
//1. Given absolute URL use it
//1a. Protocol Relative URL given starting of // we use the protocol from the request
//2. Given Starting Slash prepend public facing domain:port if provided if not use base URL of request
//3. Given relative URL prepend public facing domain:port plus parent path of request URL otherwise full parent path
PublicAddress publicAddress = ctx.get(PublicAddress.class);
String generatedPath;
if (ABSOLUTE_PATTERN.matcher(path).matches()) {
//Rule 1 - Path is absolute
generatedPath = path;
} else {
URI host = publicAddress.get();
if (path.startsWith("//")) {
//Rule 1a - Protocol relative url
generatedPath = host.getScheme() + ":" + path;
} else {
if (path.charAt(0) == '/') {
//Rule 2 - Starting Slash
generatedPath = host.toString() + path;
} else {
//Rule 3
generatedPath = host.toString() + getParentPath(request.getUri()) + path;
}
}
}
return generatedPath;
}
Aggregations