Java Code Examples for com.linecorp.armeria.common.HttpStatus#valueOf()

The following examples show how to use com.linecorp.armeria.common.HttpStatus#valueOf() . 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: HttpStatusException.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static HttpStatusException of0(HttpStatus status, @Nullable Throwable cause) {
    final boolean sampled = Flags.verboseExceptionSampler().isSampled(HttpStatusException.class);
    if (sampled || cause != null) {
        return new HttpStatusException(status, sampled, cause);
    }

    final int statusCode = status.code();
    if (statusCode >= 0 && statusCode < 1000) {
        return EXCEPTIONS[statusCode];
    } else {
        return new HttpStatusException(HttpStatus.valueOf(statusCode), false, null);
    }
}
 
Example 2
Source File: AnnotatedServiceFactory.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static HttpStatus defaultResponseStatus(Method method) {
    final StatusCode statusCodeAnnotation = AnnotationUtil.findFirst(method, StatusCode.class);
    if (statusCodeAnnotation == null) {
        // Set a default HTTP status code for a response depending on the return type of the method.
        final Class<?> returnType = method.getReturnType();
        return returnType == Void.class ||
               returnType == void.class ? HttpStatus.NO_CONTENT : HttpStatus.OK;
    }

    final int statusCode = statusCodeAnnotation.value();
    checkArgument(statusCode >= 0,
                  "invalid HTTP status code: %s (expected: >= 0)", statusCode);
    return HttpStatus.valueOf(statusCode);
}
 
Example 3
Source File: HttpStreamReader.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public void onNext(HttpObject obj) {
    if (cancelled) {
        return;
    }
    if (obj instanceof HttpHeaders) {
        // Only clients will see headers from a stream. It doesn't hurt to share this logic between server
        // and client though as everything else is identical.
        final HttpHeaders headers = (HttpHeaders) obj;

        if (!sawLeadingHeaders) {
            final String statusText = headers.get(HttpHeaderNames.STATUS);
            if (statusText == null) {
                // Not allowed to have empty leading headers, kill the stream hard.
                transportStatusListener.transportReportStatus(
                        Status.INTERNAL.withDescription("Missing HTTP status code"));
                return;
            }

            if (ArmeriaHttpUtil.isInformational(statusText)) {
                // Skip informational headers.
                return;
            }

            sawLeadingHeaders = true;

            final HttpStatus status = HttpStatus.valueOf(statusText);
            if (!status.equals(HttpStatus.OK)) {
                transportStatusListener.transportReportStatus(
                        GrpcStatus.httpStatusToGrpcStatus(status.code()));
                return;
            }
        }

        final String grpcStatus = headers.get(GrpcHeaderNames.GRPC_STATUS);
        if (grpcStatus != null) {
            GrpcStatus.reportStatus(headers, this, transportStatusListener);
        }

        // Headers without grpc-status are the leading headers of a non-failing response, prepare to receive
        // messages.
        final String grpcEncoding = headers.get(GrpcHeaderNames.GRPC_ENCODING);
        if (grpcEncoding != null) {
            final Decompressor decompressor = decompressorRegistry.lookupDecompressor(grpcEncoding);
            if (decompressor == null) {
                transportStatusListener.transportReportStatus(Status.INTERNAL.withDescription(
                        "Can't find decompressor for " + grpcEncoding));
                return;
            }
            try {
                deframer.decompressor(ForwardingDecompressor.forGrpc(decompressor));
            } catch (Throwable t) {
                transportStatusListener.transportReportStatus(GrpcStatus.fromThrowable(t));
                return;
            }
        }
        requestHttpFrame();
        return;
    }
    final HttpData data = (HttpData) obj;
    try {
        deframer.deframe(data, false);
    } catch (Throwable cause) {
        try {
            transportStatusListener.transportReportStatus(GrpcStatus.fromThrowable(cause));
            return;
        } finally {
            deframer.close();
        }
    }
    requestHttpFrame();
}
 
Example 4
Source File: AccessLogFormats.java    From armeria with Apache License 2.0 4 votes vote down vote up
Builder addHttpStatus(String text) {
    final HttpStatus s = HttpStatus.valueOf(Integer.parseInt(text));
    statusSet.add(s);
    isEmpty = false;
    return this;
}