io.micronaut.http.HttpStatus Java Examples

The following examples show how to use io.micronaut.http.HttpStatus. 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: AbstractMicronautLambdaRuntime.java    From micronaut-aws with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param handlerResponse Handler response object
 * @return If the handlerResponseType and the responseType are identical just returns the supplied object. However,
 * if the response type is of type {@link APIGatewayProxyResponseEvent} it attempts to serialized the handler response
 * as a JSON String and set it in the response body. If the object cannot be serialized, a 400 response is returned
 */
@Nullable
protected ResponseType createResponse(HandlerResponseType handlerResponse) {
    if (handlerResponseType == responseType) {
        logn(LogLevel.TRACE, "HandlerResponseType and ResponseType are identical");
        return (ResponseType) handlerResponse;

    } else if (responseType == APIGatewayProxyResponseEvent.class) {
        logn(LogLevel.TRACE, "response type is APIGatewayProxyResponseEvent");
        String json = serializeAsJsonString(handlerResponse);
        if (json != null) {
            return (ResponseType) respond(HttpStatus.OK, json, MediaType.APPLICATION_JSON);
        }
        return (ResponseType) respond(HttpStatus.BAD_REQUEST,
                "Could not serialize " + handlerResponse.toString() + " as json",
                MediaType.TEXT_PLAIN);
    }
    return null;
}
 
Example #3
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 #4
Source File: MicronautAwsProxyResponse.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 * The default constructor.
 * @param request The request
 * @param latch The latch to indicate request completion
 * @param environment The {@link MicronautLambdaContainerContext}
 */
MicronautAwsProxyResponse(
        AwsProxyRequest request,
        CountDownLatch latch,
        MicronautLambdaContainerContext environment) {
    this.responseEncodeLatch = latch;
    this.request = request;
    this.response.setMultiValueHeaders(multiValueHeaders);
    this.handler = environment;
    this.status = HttpStatus.OK;
    this.response.setStatusCode(HttpStatus.OK.getCode());
}
 
Example #5
Source File: AbstractMicronautLambdaRuntime.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param status HTTP Status of the response
 * @param body Body of the response
 * @param contentType HTTP Header Content-Type value
 * @return a {@link APIGatewayProxyResponseEvent} populated with the supplied status, body and content type
 */
protected APIGatewayProxyResponseEvent respond(HttpStatus status, String body, String contentType) {
    APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
    Map<String, String> headers = new HashMap<>();
    headers.put(HttpHeaders.CONTENT_TYPE, contentType);
    response.setHeaders(headers);
    response.setBody(body);
    response.setStatusCode(status.getCode());
    logn(LogLevel.TRACE, "response: " + status.getCode() + " content type: " + headers.get(HttpHeaders.CONTENT_TYPE) + " message " + body);
    return response;
}
 
Example #6
Source File: LoginTest.java    From micronaut-microservices-poc with Apache License 2.0 5 votes vote down vote up
@Test
public void cantLoginWithInvalidCredentials() {
    try {
        UsernamePasswordCredentials upc = new UsernamePasswordCredentials("jimmy.solid","secret111");
        HttpRequest loginRequest = HttpRequest.POST("/login", upc);
        HttpResponse<BearerAccessRefreshToken> rsp = httpClient.toBlocking().exchange(loginRequest, BearerAccessRefreshToken.class);
        fail();
    } catch (HttpClientResponseException ex) {
        assertThat(ex.getStatus().getCode()).isEqualTo(HttpStatus.UNAUTHORIZED.getCode());
    }
    
}
 
Example #7
Source File: ResponseStatusAnnotationMapper.java    From micronaut-spring with Apache License 2.0 5 votes vote down vote up
@Override
protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    try {
        final String code = annotation.getValue(String.class).orElse(annotation.get("code", String.class).orElse(null));
        final HttpStatus status = HttpStatus.valueOf(code);

        return Collections.singletonList(
                AnnotationValue.builder(Status.class).value(status).build()
        );
    } catch (IllegalArgumentException e) {
        // ignore
    }
    return Collections.emptyList();
}
 
Example #8
Source File: HelloController.java    From micronaut-microservices-poc with Apache License 2.0 4 votes vote down vote up
@Get
public HttpStatus index() {
    return HttpStatus.OK;
}
 
Example #9
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 #10
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 #11
Source File: FeaturesClient.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
@Post("/{name}")
HttpStatus saveFeature(String name, FeaturesEndpoint.Feature feature);
 
Example #12
Source File: FeaturesClient.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
@Delete("/{name}")
HttpStatus deleteFeature(String name);
 
Example #13
Source File: HelloControllerTest.java    From micronaut-microservices-poc with Apache License 2.0 4 votes vote down vote up
@Test
public void testIndex() {
    assertEquals(HttpStatus.OK, client.index());
}
 
Example #14
Source File: HelloTestClient.java    From micronaut-microservices-poc with Apache License 2.0 4 votes vote down vote up
@Get
HttpStatus index();
 
Example #15
Source File: GoogleHttpResponse.java    From micronaut-gcp with Apache License 2.0 4 votes vote down vote up
/**
 * @return The HTTP status
 */
default HttpStatus getStatus() {
    return HttpStatus.valueOf(getStatusCode());
}
 
Example #16
Source File: MicronautAwsProxyResponse.java    From micronaut-aws with Apache License 2.0 4 votes vote down vote up
@Override
public HttpStatus getStatus() {
    return status;
}
 
Example #17
Source File: PetController.java    From micronaut-gcp with Apache License 2.0 4 votes vote down vote up
@Delete("/{name}")
@Status(HttpStatus.NO_CONTENT)
void deletePet(String name) {
    petService.deletePet(name);
}
 
Example #18
Source File: PetController.java    From micronaut-gcp with Apache License 2.0 4 votes vote down vote up
@Post(processes = APPLICATION_JSON)
@Status(HttpStatus.CREATED)
Pet savePet(String name, int age) {
    return petService.savePet(new Pet(name, age));
}