Java源码示例:ratpack.http.Request

示例1
public static void main(String... args) throws Exception {
    HttpVerificationService httpVerificationService = new HttpVerificationService();
    Module appModule = new AppModule();
    SmartApp smartApp = SmartApp.of(Guice.smartapp(bindings -> bindings.module(appModule)));
    RatpackServer.start(server -> {
        ObjectMapper objectMapper = DefaultConfigDataBuilder.newDefaultObjectMapper()
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        server
            .registry(ratpack.guice.Guice.registry(bindingsSpec -> {
                bindingsSpec.module(appModule);
                bindingsSpec.bindInstance(ObjectMapper.class, objectMapper);
            }))
            .handlers(chain -> chain
                .get(ctx -> ctx.getResponse()
                    .status(Status.FORBIDDEN)
                    .send("This app only functions as a SmartThings Automation webhook endpoint app"))
                .post("smartapp", ctx -> {
                    ctx.parse(ExecutionRequest.class).then(executionRequest -> {
                        Request request = ctx.getRequest();
                        Headers headers = request.getHeaders();
                        Map<String, String> headersMap = headers.getNames().stream()
                                .collect(Collectors.toMap(name -> name, name -> headers.get(name)));
                        String method = request.getMethod().getName();
                        if (executionRequest.getLifecycle() != AppLifecycle.PING
                                && !httpVerificationService.verify(method, request.getUri(), headersMap)) {
                            ctx.clientError(401);
                        } else {
                            ctx.render(json(smartApp.execute(executionRequest)));
                        }
                    });
                })
            );
        }
    );
}
 
示例2
public static void main(String... args) throws Exception {
  ObjectMapper mapper = new ObjectMapper();
  ratpack.server.RatpackServer.start(
      server -> {
        server.handlers(
            chain -> {
              chain.post(
                  "",
                  ctx -> {
                    Request request = ctx.getRequest();
                    request
                        .getBody()
                        .then(
                            data -> {
                              SimpleRequest simpleRequest =
                                  mapper.readValue(data.getInputStream(), SimpleRequest.class);
                              SimpleResponse simpleResponse = new SimpleResponse();
                              simpleResponse.setMessage(simpleRequest.getMessage());
                              String s = mapper.writeValueAsString(simpleResponse);
                              Response response = ctx.getResponse();
                              response
                                  .contentTypeIfNotSet("application/javascript")
                                  .status(200)
                                  .send(s);
                            });
                  });
            });
      });
}
 
示例3
private ServerRequestImpl(final Request request) {
  this.request = request;
}