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

The following examples show how to use com.linecorp.armeria.common.HttpMethod#GET . 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: AnnotatedDocServiceTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
private static void addFooMethodInfo(Map<Class<?>, Set<MethodInfo>> methodInfos) {
    final EndpointInfo endpoint = EndpointInfo.builder("*", "exact:/service/foo")
                                              .availableMimeTypes(MediaType.JSON_UTF_8)
                                              .build();
    final List<FieldInfo> fieldInfos = ImmutableList.of(
            FieldInfo.builder("header", INT).requirement(REQUIRED)
                     .location(FieldLocation.HEADER)
                     .docString("header parameter").build(),
            FieldInfo.builder("query", LONG).requirement(REQUIRED)
                     .location(QUERY)
                     .docString("query parameter").build());
    final MethodInfo methodInfo = new MethodInfo(
            "foo", TypeSignature.ofBase("T"), fieldInfos, ImmutableList.of(),
            ImmutableList.of(endpoint), HttpMethod.GET, "foo method");
    methodInfos.computeIfAbsent(MyService.class, unused -> new HashSet<>()).add(methodInfo);
}
 
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: MethodInfoTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static MethodInfo newMethodInfo(List<String> examplePaths, List<String> exampleQueries) {
    return new MethodInfo("foo", TypeSignature.ofBase("T"),
                          /* parameters */ ImmutableList.of(), /* exceptionSignatures */ ImmutableList.of(),
                          /* endpoints */ ImmutableList.of(), /* exampleHeaders */ ImmutableList.of(),
                          /* exampleRequests */ ImmutableList.of(),
                          examplePaths, exampleQueries,
                          HttpMethod.GET, null);
}
 
Example 4
Source File: AnnotatedDocServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void addIntsMethodInfo(Map<Class<?>, Set<MethodInfo>> methodInfos) {
    final EndpointInfo endpoint = EndpointInfo.builder("*", "exact:/service/ints")
                                              .availableMimeTypes(MediaType.JSON_UTF_8)
                                              .build();
    final List<FieldInfo> fieldInfos = ImmutableList.of(
            FieldInfo.builder("ints", TypeSignature.ofList(INT)).requirement(REQUIRED)
                     .location(QUERY).build());
    final MethodInfo methodInfo = new MethodInfo(
            "ints", TypeSignature.ofList(INT),
            fieldInfos, ImmutableList.of(),
            ImmutableList.of(endpoint), HttpMethod.GET, null);
    methodInfos.computeIfAbsent(MyService.class, unused -> new HashSet<>()).add(methodInfo);
}
 
Example 5
Source File: AnnotatedDocServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void addPathParamsMethodInfo(Map<Class<?>, Set<MethodInfo>> methodInfos) {
    final EndpointInfo endpoint = EndpointInfo.builder("*", "/service/hello1/{hello2}/hello3/{hello4}")
                                              .availableMimeTypes(MediaType.JSON_UTF_8)
                                              .build();
    final List<FieldInfo> fieldInfos = ImmutableList.of(
            FieldInfo.builder("hello2", STRING).requirement(REQUIRED).location(PATH).build(),
            FieldInfo.builder("hello4", STRING).requirement(REQUIRED).location(PATH).build());
    final MethodInfo methodInfo = new MethodInfo(
            "pathParams", STRING, fieldInfos, ImmutableList.of(),
            ImmutableList.of(endpoint), HttpMethod.GET, null);
    methodInfos.computeIfAbsent(MyService.class, unused -> new HashSet<>()).add(methodInfo);
}
 
Example 6
Source File: AnnotatedDocServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void addPathParamsWithQueriesMethodInfo(Map<Class<?>, Set<MethodInfo>> methodInfos) {
    final EndpointInfo endpoint = EndpointInfo.builder("*", "/service/hello1/{hello2}")
                                              .availableMimeTypes(MediaType.JSON_UTF_8)
                                              .build();
    final List<FieldInfo> fieldInfos = ImmutableList.of(
            FieldInfo.builder("hello2", STRING).requirement(REQUIRED).location(PATH).build(),
            FieldInfo.builder("hello3", STRING).requirement(REQUIRED).location(QUERY).build());
    final MethodInfo methodInfo = new MethodInfo(
            "pathParamsWithQueries", STRING, fieldInfos, ImmutableList.of(),
            ImmutableList.of(endpoint), HttpMethod.GET, null);
    methodInfos.computeIfAbsent(MyService.class, unused -> new HashSet<>()).add(methodInfo);
}
 
Example 7
Source File: AnnotatedDocServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void addRegexMethodInfo(Map<Class<?>, Set<MethodInfo>> methodInfos) {
    final EndpointInfo endpoint = EndpointInfo.builder("*", "regex:/(bar|baz)")
                                              .regexPathPrefix("prefix:/service/")
                                              .availableMimeTypes(MediaType.JSON_UTF_8)
                                              .build();
    final List<FieldInfo> fieldInfos = ImmutableList.of(
            FieldInfo.builder("myEnum", toTypeSignature(MyEnum.class))
                     .requirement(REQUIRED)
                     .location(QUERY)
                     .build());
    final MethodInfo methodInfo = new MethodInfo(
            "regex", TypeSignature.ofList(TypeSignature.ofList(STRING)), fieldInfos, ImmutableList.of(),
            ImmutableList.of(endpoint), HttpMethod.GET, null);
    methodInfos.computeIfAbsent(MyService.class, unused -> new HashSet<>()).add(methodInfo);
}
 
Example 8
Source File: AnnotatedDocServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void addPrefixMethodInfo(Map<Class<?>, Set<MethodInfo>> methodInfos) {
    final EndpointInfo endpoint = EndpointInfo.builder("*", "prefix:/service/prefix/")
                                              .availableMimeTypes(MediaType.JSON_UTF_8)
                                              .build();
    final MethodInfo methodInfo = new MethodInfo(
            "prefix", STRING, ImmutableList.of(), ImmutableList.of(),
            ImmutableList.of(endpoint), HttpMethod.GET, null);
    methodInfos.computeIfAbsent(MyService.class, unused -> new HashSet<>()).add(methodInfo);
}
 
Example 9
Source File: AnnotatedDocServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void addConsumesMethodInfo(Map<Class<?>, Set<MethodInfo>> methodInfos) {
    final EndpointInfo endpoint = EndpointInfo.builder("*", "exact:/service/consumes")
                                              .availableMimeTypes(MediaType.APPLICATION_BINARY,
                                                                  MediaType.JSON_UTF_8)
                                              .build();
    final MethodInfo methodInfo = new MethodInfo(
            "consumes", TypeSignature.ofContainer("BiFunction", TypeSignature.ofBase("JsonNode"),
                                                  TypeSignature.ofUnresolved(""), STRING),
            ImmutableList.of(), ImmutableList.of(), ImmutableList.of(endpoint), HttpMethod.GET, null);
    methodInfos.computeIfAbsent(MyService.class, unused -> new HashSet<>()).add(methodInfo);
}
 
Example 10
Source File: AnnotatedDocServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void addBeanMethodInfo(Map<Class<?>, Set<MethodInfo>> methodInfos) {
    final EndpointInfo endpoint = EndpointInfo.builder("*", "exact:/service/bean")
                                              .availableMimeTypes(MediaType.JSON_UTF_8)
                                              .build();
    final List<FieldInfo> fieldInfos = ImmutableList.of(compositeBean());
    final MethodInfo methodInfo = new MethodInfo(
            "bean", TypeSignature.ofBase("HttpResponse"), fieldInfos, ImmutableList.of(),
            ImmutableList.of(endpoint), HttpMethod.GET, null);
    methodInfos.computeIfAbsent(MyService.class, unused -> new HashSet<>()).add(methodInfo);
}
 
Example 11
Source File: AnnotatedDocServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void addMultiMethodInfo(Map<Class<?>, Set<MethodInfo>> methodInfos) {
    final EndpointInfo endpoint1 = EndpointInfo.builder("*", "exact:/service/multi")
                                               .availableMimeTypes(MediaType.JSON_UTF_8)
                                               .build();
    final EndpointInfo endpoint2 = EndpointInfo.builder("*", "prefix:/service/multi2/")
                                               .availableMimeTypes(MediaType.JSON_UTF_8)
                                               .build();
    final MethodInfo methodInfo = new MethodInfo(
            "multi", TypeSignature.ofBase("HttpResponse"), ImmutableList.of(), ImmutableList.of(),
            ImmutableList.of(endpoint1, endpoint2), HttpMethod.GET, null);
    methodInfos.computeIfAbsent(MyService.class, unused -> new HashSet<>()).add(methodInfo);
}
 
Example 12
Source File: AnnotatedDocServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void addPeriodMethodInfo(Map<Class<?>, Set<MethodInfo>> methodInfos) {
    final EndpointInfo endpoint = EndpointInfo.builder("*", "exact:/service/period")
                                              .availableMimeTypes(MediaType.JSON_UTF_8)
                                              .build();
    final List<FieldInfo> fieldInfos = ImmutableList.of(
            FieldInfo.builder("period", TypeSignature.ofBase("Period"))
                     .requirement(REQUIRED).location(QUERY).build());
    final MethodInfo methodInfo = new MethodInfo(
            "period", TypeSignature.ofBase("HttpResponse"), fieldInfos, ImmutableList.of(),
            ImmutableList.of(endpoint), HttpMethod.GET, null);
    methodInfos.computeIfAbsent(MyService.class, unused -> new HashSet<>()).add(methodInfo);
}
 
Example 13
Source File: AbstractHttpFile.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public HttpService asService() {
    return (ctx, req) -> {
        final HttpMethod method = ctx.method();
        if (method != HttpMethod.GET && method != HttpMethod.HEAD) {
            return HttpResponse.of(HttpStatus.METHOD_NOT_ALLOWED);
        }

        return HttpResponse.from(readAttributes(ctx.blockingTaskExecutor()).thenApply(attrs -> {
            if (attrs == null) {
                return HttpResponse.of(HttpStatus.NOT_FOUND);
            }

            // See https://tools.ietf.org/html/rfc7232#section-6 for more information
            // about how conditional requests are handled.

            // Handle 'if-none-match' header.
            final RequestHeaders reqHeaders = req.headers();
            final String etag = generateEntityTag(attrs);
            final String ifNoneMatch = reqHeaders.get(HttpHeaderNames.IF_NONE_MATCH);
            if (etag != null && ifNoneMatch != null) {
                if ("*".equals(ifNoneMatch) || entityTagMatches(etag, ifNoneMatch)) {
                    return newNotModified(attrs, etag);
                }
            }

            // Handle 'if-modified-since' header, only if 'if-none-match' does not exist.
            if (ifNoneMatch == null) {
                try {
                    final Long ifModifiedSince =
                            reqHeaders.getTimeMillis(HttpHeaderNames.IF_MODIFIED_SINCE);
                    if (ifModifiedSince != null) {
                        // HTTP-date does not have subsecond-precision; add 999ms to it.
                        final long ifModifiedSinceMillis = LongMath.saturatedAdd(ifModifiedSince, 999);
                        if (attrs.lastModifiedMillis() <= ifModifiedSinceMillis) {
                            return newNotModified(attrs, etag);
                        }
                    }
                } catch (Exception ignore) {
                    // Malformed date.
                }
            }

            // Precondition did not match. Handle as usual.
            switch (ctx.method()) {
                case HEAD:
                    final ResponseHeaders resHeaders = readHeaders(attrs);
                    if (resHeaders != null) {
                        return HttpResponse.of(resHeaders);
                    }
                    break;
                case GET:
                    final HttpResponse res = read(ctx.blockingTaskExecutor(), ctx.alloc(), attrs);
                    if (res != null) {
                        return res;
                    }
                    break;
                default:
                    throw new Error(); // Never reaches here.
            }

            // readHeaders() or read() returned null above.
            return HttpResponse.of(HttpStatus.NOT_FOUND);
        }));
    };
}