Java Code Examples for com.linecorp.armeria.common.HttpMethod#POST

The following examples show how to use com.linecorp.armeria.common.HttpMethod#POST . 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: RestfulJsonResponseConverter.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Override
public HttpResponse convertResponse(ServiceRequestContext ctx, ResponseHeaders headers,
                                    @Nullable Object resObj,
                                    HttpHeaders trailingHeaders) throws Exception {
    try {
        final HttpRequest request = RequestContext.current().request();
        final HttpData httpData =
                resObj != null &&
                resObj.getClass() == Object.class ? EMPTY_RESULT
                                                  : HttpData.wrap(Jackson.writeValueAsBytes(resObj));

        final ResponseHeadersBuilder builder = headers.toBuilder();
        if (HttpMethod.POST == request.method()) {
            builder.status(HttpStatus.CREATED);
        }
        if (builder.contentType() == null) {
            builder.contentType(MediaType.JSON_UTF_8);
        }
        return HttpResponse.of(builder.build(), httpData, trailingHeaders);
    } catch (JsonProcessingException e) {
        return HttpApiUtil.newResponse(ctx, HttpStatus.INTERNAL_SERVER_ERROR, e);
    }
}
 
Example 2
Source File: ArmeriaSdkHttpClient.java    From curiostack with MIT License 5 votes vote down vote up
private static HttpMethod convert(SdkHttpMethod method) {
  switch (method) {
    case GET:
      return HttpMethod.GET;
    case POST:
      return HttpMethod.POST;
    case PUT:
      return HttpMethod.PUT;
    case DELETE:
      return HttpMethod.DELETE;
    case HEAD:
      return HttpMethod.HEAD;
    case PATCH:
      return HttpMethod.PATCH;
    case OPTIONS:
      return HttpMethod.OPTIONS;
    default:
      try {
        return HttpMethod.valueOf(method.name());
      } catch (IllegalArgumentException unused) {
        throw new IllegalArgumentException(
            "Unknown SdkHttpMethod: "
                + method
                + ". Cannot convert to an Armeria request. This could only practically happen if "
                + "the HTTP standard has new methods added and is very unlikely.");
      }
  }
}
 
Example 3
Source File: GrpcDocServicePlugin.java    From armeria with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static MethodInfo newMethodInfo(MethodDescriptor method, ServiceEntry service) {
    final Set<EndpointInfo> methodEndpoints =
            service.endpointInfos.stream()
                                 .map(e -> {
                                     final EndpointInfoBuilder builder = EndpointInfo.builder(
                                             e.hostnamePattern(), e.pathMapping() + method.getName());
                                     if (e.fragment() != null) {
                                         builder.fragment(e.fragment());
                                     }
                                     if (e.defaultMimeType() != null) {
                                         builder.defaultMimeType(e.defaultMimeType());
                                     }
                                     return builder.availableMimeTypes(e.availableMimeTypes()).build();
                                 })
                                 .collect(toImmutableSet());
    return new MethodInfo(
            method.getName(),
            namedMessageSignature(method.getOutputType()),
            // gRPC methods always take a single request parameter of message type.
            ImmutableList.of(FieldInfo.builder("request", namedMessageSignature(method.getInputType()))
                                      .requirement(FieldRequirement.REQUIRED).build()),
            /* exceptionTypeSignatures */ ImmutableList.of(),
            methodEndpoints,
            /* exampleHttpHeaders */ ImmutableList.of(),
            defaultExamples(method),
            /* examplePaths */ ImmutableList.of(),
            /* exampleQueries */ ImmutableList.of(),
            HttpMethod.POST,
            /* docString */ null);
}
 
Example 4
Source File: THttpService.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
    if (req.method() != HttpMethod.POST) {
        return HttpResponse.of(HttpStatus.METHOD_NOT_ALLOWED);
    }

    final SerializationFormat serializationFormat = determineSerializationFormat(req);
    if (serializationFormat == null) {
        return HttpResponse.of(HttpStatus.UNSUPPORTED_MEDIA_TYPE,
                               MediaType.PLAIN_TEXT_UTF_8, PROTOCOL_NOT_SUPPORTED);
    }

    if (!validateAcceptHeaders(req, serializationFormat)) {
        return HttpResponse.of(HttpStatus.NOT_ACCEPTABLE,
                               MediaType.PLAIN_TEXT_UTF_8, ACCEPT_THRIFT_PROTOCOL_MUST_MATCH_CONTENT_TYPE);
    }

    final CompletableFuture<HttpResponse> responseFuture = new CompletableFuture<>();
    final HttpResponse res = HttpResponse.from(responseFuture);
    ctx.logBuilder().serializationFormat(serializationFormat);
    ctx.logBuilder().defer(RequestLogProperty.REQUEST_CONTENT);
    PooledHttpRequest.of(req).aggregateWithPooledObjects(
            ctx.eventLoop(), ctx.alloc()).handle((aReq, cause) -> {
        if (cause != null) {
            final HttpResponse errorRes;
            if (ctx.config().verboseResponses()) {
                errorRes = HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR,
                                           MediaType.PLAIN_TEXT_UTF_8,
                                           Exceptions.traceText(cause));
            } else {
                errorRes = HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR);
            }
            responseFuture.complete(errorRes);
            return null;
        }
        decodeAndInvoke(ctx, aReq, serializationFormat, responseFuture);
        return null;
    }).exceptionally(CompletionActions::log);
    return res;
}
 
Example 5
Source File: MethodInfo.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance.
 */
public MethodInfo(String name,
                  TypeSignature returnTypeSignature,
                  Iterable<FieldInfo> parameters,
                  Iterable<TypeSignature> exceptionTypeSignatures,
                  Iterable<EndpointInfo> endpoints) {
    this(name, returnTypeSignature, parameters, exceptionTypeSignatures, endpoints, HttpMethod.POST, null);
}
 
Example 6
Source File: DefaultClientRequestContextTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static DefaultClientRequestContext newContext() {
    final DefaultClientRequestContext ctx = new DefaultClientRequestContext(
            mock(EventLoop.class), NoopMeterRegistry.get(), SessionProtocol.H2C,
            RequestId.random(), HttpMethod.POST, "/foo", null, null,
            ClientOptions.of(),
            HttpRequest.of(RequestHeaders.of(
                    HttpMethod.POST, "/foo",
                    HttpHeaderNames.SCHEME, "http",
                    HttpHeaderNames.AUTHORITY, "example.com:8080")),
            null, System.nanoTime(), SystemInfo.currentTimeMicros());
    ctx.init(Endpoint.of("example.com", 8080));
    return ctx;
}
 
Example 7
Source File: AnnotatedDocServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void addJsonMethodInfo(Map<Class<?>, Set<MethodInfo>> methodInfos) {
    final EndpointInfo endpoint1 = EndpointInfo.builder("*", "exact:/service/json")
                                               .availableMimeTypes(MediaType.JSON_UTF_8)
                                               .build();
    final MethodInfo methodInfo1 = new MethodInfo(
            "json", STRING, ImmutableList.of(), ImmutableList.of(),
            ImmutableList.of(endpoint1), HttpMethod.POST, null);
    final MethodInfo methodInfo2 = new MethodInfo(
            "json", STRING, ImmutableList.of(), ImmutableList.of(),
            ImmutableList.of(endpoint1), HttpMethod.PUT, null);
    final Set<MethodInfo> methods = methodInfos.computeIfAbsent(MyService.class, unused -> new HashSet<>());
    methods.add(methodInfo1);
    methods.add(methodInfo2);
}