Java Code Examples for com.linecorp.armeria.server.ServerBuilder#annotatedService()

The following examples show how to use com.linecorp.armeria.server.ServerBuilder#annotatedService() . 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: DropwizardArmeriaApplication.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(Bootstrap<DropwizardArmeriaConfiguration> bootstrap) {
    final ArmeriaBundle<DropwizardArmeriaConfiguration> bundle =
            new ArmeriaBundle<DropwizardArmeriaConfiguration>() {
        @Override
        public void configure(ServerBuilder builder) {
            builder.service("/", (ctx, res) -> HttpResponse.of(MediaType.HTML_UTF_8, "<h2>It works!</h2>"));
            builder.service("/armeria", (ctx, res) -> HttpResponse.of("Hello, Armeria!"));

            builder.annotatedService(new HelloService());

            // You can also bind asynchronous RPC services such as Thrift and gRPC:
            // builder.service(THttpService.of(...));
            // builder.service(GrpcService.builder()...build());
        }
    };
    bootstrap.addBundle(bundle);
}
 
Example 2
Source File: AnnotatedServiceAccessModifierTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.annotatedService("/anonymous", new Object() {
        @Get("/public")
        @LoggingDecorator
        @ResponseConverter(UnformattedStringConverterFunction.class)
        public String public0() {
            return "hello";
        }

        @Get("/package")
        String package0() {
            return "hello";
        }

        @Get("/private")
        private String private0() {
            return "hello";
        }
    });

    sb.annotatedService("/named", new AccessModifierTest());
}
 
Example 3
Source File: HttpServiceLogNameTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.accessLogWriter(AccessLogWriter.combined(), true);
    sb.service("/no-default", new MyHttpService());
    sb.service("/no-default/:id", new MyHttpService().decorate(LoggingService.newDecorator()));
    sb.route()
      .get("/users/:id")
      .defaultServiceName("userService")
      .defaultLogName("profile")
      .build((ctx, req) -> HttpResponse.of(HttpStatus.OK));

    sb.annotatedService("/annotated", new MyAnnotatedService());
    sb.decorator((delegate, ctx, req) -> {
       capturedCtx = ctx;
       return delegate.serve(ctx, req);
    });
}
 
Example 4
Source File: AnnotatedServiceRequestLogNameTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.annotatedService(new FooService());
    sb.annotatedService("/serviceName", new BarService());
    sb.annotatedService("/decorated", new BarService(), service -> {
        return service.decorate((delegate, ctx, req) -> {
            ctx.logBuilder().name("DecoratedService", ctx.method().name());
            return delegate.serve(ctx, req);
        });
    });

    sb.decorator((delegate, ctx, req) -> {
        logs.add(ctx.log());
        return delegate.serve(ctx, req);
    });

    // The annotated service will not be invoked at all for '/fail_early'.
    sb.routeDecorator().path("/fail_early")
      .build((delegate, ctx, req) -> {
          throw HttpStatusException.of(500);
      });
}
 
Example 5
Source File: AnnotatedServiceAnnotationAliasTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.annotatedService(new Object() {
        @Post("/hello")
        @MyPostServiceSpecifications
        public String hello(MyRequest myRequest) {
            return myRequest.name;
        }

        @Get("/exception1")
        @MyGetServiceSpecifications
        public String exception1() {
            throw new IllegalArgumentException("Anticipated!");
        }

        @Get("/exception2")
        @MyGetServiceSpecifications
        public String exception2() {
            throw new IllegalStateException("Anticipated!");
        }
    });
}
 
Example 6
Source File: AnnotatedServiceExceptionHandlerTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.annotatedService("/1", new MyService1(),
                        LoggingService.newDecorator());

    sb.annotatedService("/2", new MyService2(),
                        LoggingService.newDecorator());

    sb.annotatedService("/3", new MyService3(),
                        LoggingService.newDecorator());

    sb.annotatedService("/4", new MyService4(),
                        LoggingService.newDecorator());

    sb.annotatedService("/5", new MyService5());

    sb.requestTimeoutMillis(500L);
}
 
Example 7
Source File: AnnotatedServiceDecorationTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.annotatedService("/1", new MyDecorationService1());
    sb.annotatedService("/2", new MyDecorationService2());
    sb.annotatedService("/3", new MyDecorationService3());
    sb.annotatedService("/4", new MyDecorationService4());
}
 
Example 8
Source File: AnnotatedServiceBlockingTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.annotatedService("/myEvenLoop", new MyEventLoopAnnotatedService(),
                        LoggingService.newDecorator());
    sb.annotatedService("/myBlocking", new MyBlockingAnnotatedService(),
                        LoggingService.newDecorator());
    sb.blockingTaskExecutor(executor, true);
}
 
Example 9
Source File: ContentPreviewerTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
void build(ServerBuilder sb) {
    sb.decorator(ContentPreviewingService.newDecorator(contentPreviewerFactory(10)));
    sb.decorator(delegate -> (ctx, req) -> {
        if (waitingFuture != null) {
            ctx.log().whenComplete().thenAccept(waitingFuture::complete);
        }
        return delegate.serve(ctx, req);
    });
    sb.annotatedService("/example", new Object() {
        @Get("/get")
        public String get() {
            return "test";
        }

        @Get("/get-unicode")
        public HttpResponse getUnicode() {
            return HttpResponse.of(MediaType.ANY_TEXT_TYPE, "안녕");
        }

        @Get("/get-audio")
        public HttpResponse getBinary() {
            return HttpResponse.of(HttpStatus.OK, MediaType.BASIC_AUDIO, new byte[] { 1, 2, 3, 4 });
        }

        @Get("/get-json")
        public HttpResponse getJson() {
            return HttpResponse.of(MediaType.JSON, "{\"value\":1}");
        }

        @Get("/get-longstring")
        public String getLongString() {
            return Strings.repeat("a", 10000);
        }

        @Post("/post")
        public String post(String requestContent) {
            return "abcdefghijkmnopqrstu";
        }
    });
}
 
Example 10
Source File: MatchesHeaderTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.annotatedService(new Object() {
        @Get("/matches")
        @MatchesHeader("my-header=my-value")
        public String matches() {
            return "my-header=my-value";
        }

        @Get("/matches")
        public String fallback1() {
            return "fallback";
        }

        @Get("/doesNotMatch")
        @MatchesHeader("my-header!=my-value")
        public String doesNotMatch() {
            return "my-header!=my-value";
        }

        @Get("/doesNotMatch")
        public String fallback2() {
            return "fallback";
        }

        @Get("/contains")
        @MatchesHeader("my-header")
        public String contains() {
            return "my-header";
        }

        @Get("/contains")
        public String fallback3() {
            return "fallback";
        }

        @Get("/doesNotContain")
        @MatchesHeader("!my-header")
        public String doesNotContain() {
            return "!my-header";
        }

        @Get("/doesNotContain")
        public String fallback4() {
            return "fallback";
        }

        @Get("/multiple")
        @MatchesHeader("my-header=my-value")
        @MatchesHeader("!your-header")
        public String multiple() {
            return "multiple";
        }

        @Get("/multiple")
        public String fallback5() {
            return "fallback";
        }
    });
}
 
Example 11
Source File: MatchesParamTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.annotatedService(new Object() {
        @Get("/matches")
        @MatchesParam("my-param=my-value")
        public String matches() {
            return "my-param=my-value";
        }

        @Get("/matches")
        public String fallback1() {
            return "fallback";
        }

        @Get("/doesNotMatch")
        @MatchesParam("my-param!=my-value")
        public String doesNotMatch() {
            return "my-param!=my-value";
        }

        @Get("/doesNotMatch")
        public String fallback2() {
            return "fallback";
        }

        @Get("/contains")
        @MatchesParam("my-param")
        public String contains() {
            return "my-param";
        }

        @Get("/contains")
        public String fallback3() {
            return "fallback";
        }

        @Get("/doesNotContain")
        @MatchesParam("!my-param")
        public String doesNotContain() {
            return "!my-param";
        }

        @Get("/doesNotContain")
        public String fallback4() {
            return "fallback";
        }

        @Get("/matches/percentEncoded")
        @MatchesParam("my-param=/")
        public String matchesPercentEncoded1() {
            return "my-param=/";
        }

        @Get("/matches/percentEncoded")
        @MatchesParam("my-param=%2F")
        public String matchesPercentEncoded2() {
            return "my-param=%2F";
        }
    });
}
 
Example 12
Source File: Server.java    From rpc-benchmark with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) throws Exception {
	ServerBuilder sb = new ServerBuilder();

	// Configure an HTTP port.
	sb.http(new InetSocketAddress("benchmark-server", 8080));

	// Using an annotated service object:
	sb.annotatedService(new ArmeriaUserServiceServerImpl());

	sb.build().start().join();

}