Search in sources :

Example 1 with PublicAddress

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));
}
Also used : Types(ratpack.util.Types) Context(ratpack.handling.Context) RatpackPac4j(ratpack.pac4j.RatpackPac4j) Exceptions.uncheck(ratpack.util.Exceptions.uncheck) Promise(ratpack.exec.Promise) PublicAddress(ratpack.server.PublicAddress) Blocking(ratpack.exec.Blocking) RequiresHttpAction(org.pac4j.core.exception.RequiresHttpAction) WebContext(org.pac4j.core.context.WebContext) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Clients(org.pac4j.core.client.Clients) Client(org.pac4j.core.client.Client) Handler(ratpack.handling.Handler) Registry(ratpack.registry.Registry) Optional(java.util.Optional) PathBinding(ratpack.path.PathBinding) TechnicalException(org.pac4j.core.exception.TechnicalException) UserProfile(org.pac4j.core.profile.UserProfile) SessionData(ratpack.session.SessionData) Credentials(org.pac4j.core.credentials.Credentials) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PublicAddress(ratpack.server.PublicAddress) Client(org.pac4j.core.client.Client) Clients(org.pac4j.core.client.Clients)

Example 2 with PublicAddress

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));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DirectChannelAccess(ratpack.handling.direct.DirectChannelAccess) Channel(io.netty.channel.Channel) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Request(ratpack.http.Request) PublicAddress(ratpack.server.PublicAddress) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 3 with PublicAddress

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;
}
Also used : PublicAddress(ratpack.server.PublicAddress) URI(java.net.URI)

Aggregations

PublicAddress (ratpack.server.PublicAddress)3 URI (java.net.URI)2 ImmutableList (com.google.common.collect.ImmutableList)1 Channel (io.netty.channel.Channel)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 URISyntaxException (java.net.URISyntaxException)1 List (java.util.List)1 Optional (java.util.Optional)1 Client (org.pac4j.core.client.Client)1 Clients (org.pac4j.core.client.Clients)1 WebContext (org.pac4j.core.context.WebContext)1 Credentials (org.pac4j.core.credentials.Credentials)1 RequiresHttpAction (org.pac4j.core.exception.RequiresHttpAction)1 TechnicalException (org.pac4j.core.exception.TechnicalException)1 UserProfile (org.pac4j.core.profile.UserProfile)1 Blocking (ratpack.exec.Blocking)1 Promise (ratpack.exec.Promise)1 Context (ratpack.handling.Context)1