Search in sources :

Example 1 with UriBuilder

use of io.micronaut.http.uri.UriBuilder in project micronaut-core by micronaut-projects.

the class DefaultHttpClient method resolveRedirectURI.

/**
 * @param parentRequest      The parent request
 * @param request            The redirect location request
 * @param <I>                The input type
 * @return A {@link Publisher} with the resolved URI
 */
protected <I> Publisher<URI> resolveRedirectURI(io.micronaut.http.HttpRequest<?> parentRequest, io.micronaut.http.HttpRequest<I> request) {
    URI requestURI = request.getUri();
    if (requestURI.getScheme() != null) {
        // if the request URI includes a scheme then it is fully qualified so use the direct server
        return Flux.just(requestURI);
    } else {
        if (parentRequest == null || parentRequest.getUri().getHost() == null) {
            return resolveURI(request, false);
        } else {
            URI parentURI = parentRequest.getUri();
            UriBuilder uriBuilder = UriBuilder.of(requestURI).scheme(parentURI.getScheme()).userInfo(parentURI.getUserInfo()).host(parentURI.getHost()).port(parentURI.getPort());
            return Flux.just(uriBuilder.build());
        }
    }
}
Also used : UriBuilder(io.micronaut.http.uri.UriBuilder) URI(java.net.URI)

Example 2 with UriBuilder

use of io.micronaut.http.uri.UriBuilder in project micronaut-core by micronaut-projects.

the class NettyClientHttpRequest method decodeParameters.

private NettyHttpParameters decodeParameters(URI uri) {
    QueryStringDecoder queryStringDecoder = createDecoder(uri);
    return new NettyHttpParameters(queryStringDecoder.parameters(), ConversionService.SHARED, (name, value) -> {
        UriBuilder newUri = UriBuilder.of(getUri());
        newUri.replaceQueryParam(name.toString(), value.toArray());
        this.uri(newUri.build());
    });
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) NettyHttpParameters(io.micronaut.http.netty.NettyHttpParameters) UriBuilder(io.micronaut.http.uri.UriBuilder)

Example 3 with UriBuilder

use of io.micronaut.http.uri.UriBuilder in project micronaut-core by micronaut-projects.

the class HttpToHttpsRedirectHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpRequest && ctx.pipeline().get(SslHandler.class) == null) {
        HttpRequest<?> request = (HttpRequest<?>) msg;
        UriBuilder uriBuilder = UriBuilder.of(hostResolver.resolve(request));
        uriBuilder.scheme("https");
        int port = sslConfiguration.getPort();
        if (port == 443) {
            uriBuilder.port(-1);
        } else {
            uriBuilder.port(port);
        }
        uriBuilder.path(request.getPath());
        MutableHttpResponse<?> response = HttpResponse.permanentRedirect(uriBuilder.build()).header(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
        io.netty.handler.codec.http.HttpResponse nettyResponse = NettyHttpResponseBuilder.toHttpResponse(response);
        ctx.writeAndFlush(nettyResponse);
    } else {
        ctx.fireChannelRead(msg);
    }
}
Also used : HttpRequest(io.micronaut.http.HttpRequest) UriBuilder(io.micronaut.http.uri.UriBuilder)

Example 4 with UriBuilder

use of io.micronaut.http.uri.UriBuilder in project micronaut-security by micronaut-projects.

the class DefaultAuthorizationRedirectHandler method expandedUri.

/**
 * @param baseUrl Base Url
 * @param queryParams Query Parameters
 * @return The Expanded URI
 */
protected String expandedUri(@NonNull String baseUrl, @NonNull Map<String, Object> queryParams) {
    UriBuilder builder = UriBuilder.of(baseUrl);
    queryParams.entrySet().stream().filter(entry -> entry.getValue() != null).forEach(entry -> builder.queryParam(entry.getKey(), entry.getValue()));
    return builder.toString();
}
Also used : Logger(org.slf4j.Logger) UriBuilder(io.micronaut.http.uri.UriBuilder) LoggerFactory(org.slf4j.LoggerFactory) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) Singleton(jakarta.inject.Singleton) HttpHeaders(io.micronaut.http.HttpHeaders) HashMap(java.util.HashMap) NonNull(io.micronaut.core.annotation.NonNull) StringUtils(io.micronaut.core.util.StringUtils) HttpStatus(io.micronaut.http.HttpStatus) Map(java.util.Map) HttpResponse(io.micronaut.http.HttpResponse) Optional(java.util.Optional) UriBuilder(io.micronaut.http.uri.UriBuilder)

Example 5 with UriBuilder

use of io.micronaut.http.uri.UriBuilder in project micronaut-starter by micronaut-projects.

the class GitHubRedirectService method constructOAuthRedirectUrl.

/**
 * Creates redirect URI to github oauth auhtorise in order to receive the user access code.
 *
 * @param requestInfo origin request info
 * @return URI to github oauth authorise
 */
protected URI constructOAuthRedirectUrl(RequestInfo requestInfo) {
    try {
        UriBuilder uriBuilder = UriBuilder.of(requestInfo.getServerURL()).path(requestInfo.getPath());
        requestInfo.getParameters().forEachValue(uriBuilder::queryParam);
        URI redirectUri = uriBuilder.build();
        return UriBuilder.of(githubOAuthUrl).queryParam("scope", "user,repo").queryParam("client_id", gitHubConfiguration.getClientId()).queryParam("redirect_uri", redirectUri.toString()).queryParam("state", UUID.randomUUID().toString()).build();
    } catch (Exception e) {
        String msg = "Failed to construct redirect URI using request " + requestInfo + " to GiHub OAuth: " + e.getMessage();
        LOG.error(msg, e);
        throw new RuntimeException(msg);
    }
}
Also used : UriBuilder(io.micronaut.http.uri.UriBuilder) URI(java.net.URI)

Aggregations

UriBuilder (io.micronaut.http.uri.UriBuilder)6 NonNull (io.micronaut.core.annotation.NonNull)2 URI (java.net.URI)2 StringUtils (io.micronaut.core.util.StringUtils)1 HttpHeaders (io.micronaut.http.HttpHeaders)1 HttpRequest (io.micronaut.http.HttpRequest)1 HttpResponse (io.micronaut.http.HttpResponse)1 HttpStatus (io.micronaut.http.HttpStatus)1 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)1 NettyHttpParameters (io.micronaut.http.netty.NettyHttpParameters)1 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)1 Singleton (jakarta.inject.Singleton)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1