io.micronaut.http.MutableHttpResponse Java Examples

The following examples show how to use io.micronaut.http.MutableHttpResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SlackAppMicronautAdapter.java    From java-slack-sdk with MIT License 6 votes vote down vote up
public HttpResponse<String> toMicronautResponse(Response resp) {
    HttpStatus status = HttpStatus.valueOf(resp.getStatusCode());
    MutableHttpResponse<String> response = micronautResponseFactory.status(status);
    for (Map.Entry<String, List<String>> header : resp.getHeaders().entrySet()) {
        String name = header.getKey();
        for (String value : header.getValue()) {
            response.header(name, value);
        }
    }
    response.body(resp.getBody());
    response.contentType(resp.getContentType());
    if (resp.getBody() != null) {
        response.contentLength(resp.getBody().length());
    } else {
        response.contentLength(0);
    }
    return response;
}
 
Example #2
Source File: MicronautAwsProxyResponseFactory.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> MutableHttpResponse<T> ok(T body) {
    final HttpRequest<Object> req = ServerRequestContext.currentRequest().orElse(null);
    if (req instanceof MicronautAwsProxyRequest) {
        final MicronautAwsProxyResponse<T> response = (MicronautAwsProxyResponse<T>) ((MicronautAwsProxyRequest<Object>) req).getResponse();
        return response.status(HttpStatus.OK).body(body);
    } else {
        if (ALTERNATE != null) {
            return ALTERNATE.ok(body);
        } else {
            throw new InternalServerException("No request present");
        }
    }
}
 
Example #3
Source File: HelloWorldMicronautTest.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
@Get("/multi-cookie")
HttpResponse<String> multiCookie() {
    final MutableHttpResponse<String> response = HttpResponse.ok(BODY_TEXT_RESPONSE);
    response.cookie(Cookie.of(COOKIE_NAME, COOKIE_VALUE).domain(COOKIE_DOMAIN).path(COOKIE_PATH))
            .cookie(Cookie.of(COOKIE_NAME + "2", COOKIE_VALUE + "2").domain(COOKIE_DOMAIN).path(COOKIE_PATH));
    return response;
}
 
Example #4
Source File: UserController.java    From kyoko with MIT License 5 votes vote down vote up
@Get("/guilds/{id}")
Single<HttpResponse> guild(@RequestAttribute("session") Session session, long id) {
    if (!session.hasGuild(id)) {
        return Single.just(ErrorResponse.get(ErrorCode.UNKNOWN_GUILD));
    }

    return Flowable.fromFuture(Main.getCatnip().rest().guild()
            .getGuild(String.valueOf(id))
            .thenApply(KGuildExt::new)
            .toCompletableFuture())
            .singleOrError()
            .flatMap(guild -> Single.<MutableHttpResponse<?>>just(HttpResponse.ok(guild))
                    .onErrorReturn(err -> ErrorResponse.get(ErrorCode.UNKNOWN_GUILD)));
}
 
Example #5
Source File: HelloWorldMicronautTest.java    From micronaut-aws with Apache License 2.0 4 votes vote down vote up
@Get("/cookie")
HttpResponse<String> cookie() {
    final MutableHttpResponse<String> response = HttpResponse.ok(BODY_TEXT_RESPONSE);
    response.cookie(Cookie.of(COOKIE_NAME, COOKIE_VALUE).domain(COOKIE_DOMAIN).path(COOKIE_PATH));
    return response;
}
 
Example #6
Source File: ErrorResponse.java    From kyoko with MIT License 4 votes vote down vote up
public static MutableHttpResponse<?> get(HttpStatus status) {
    return HttpResponse.status(status).body(new Response("Unknown error", 0));
}
 
Example #7
Source File: ErrorResponse.java    From kyoko with MIT License 4 votes vote down vote up
public static MutableHttpResponse<?> get(HttpStatus status, String message, int code) {
    return HttpResponse.status(status).body(new Response(message, code));
}
 
Example #8
Source File: ErrorResponse.java    From kyoko with MIT License 4 votes vote down vote up
public static MutableHttpResponse<?> get(ErrorCode code) {
    return HttpResponse.status(code.getStatus()).body(new Response(code.getMessage(), code.getCode()));
}
 
Example #9
Source File: AuthFilter.java    From kyoko with MIT License 4 votes vote down vote up
@Override
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
    var auth = request.getHeaders().findFirst("authorization");
    return sessionManager.authorizeToken(auth)
            .doOnError(err -> logger.error("Auth error", err))
            .onErrorComplete()
            .toSingle(Session.EMPTY_SESSION)
            .flatMap(Session::updateSession)
            .flatMap(sessionManager::updateSession)
            .flatMap(session -> session == Session.EMPTY_SESSION
                    ? Single.just(ErrorResponse.get(ErrorCode.UNAUTHORIZED))
                    : Single.fromPublisher(chain.proceed(request.setAttribute("session", session))))
            .toFlowable();
}
 
Example #10
Source File: RateLimitFilter.java    From kyoko with MIT License 3 votes vote down vote up
@Override
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
    return null;
}