play.routing.RoutingDsl Java Examples

The following examples show how to use play.routing.RoutingDsl. 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: PlayTest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Test
public void test(final MockTracer tracer) throws IOException {
  final Server server = Server.forRouter(components -> RoutingDsl.fromComponents(components)
    .GET("/hello/:to")
    .routeTo(request -> {
      assertNotNull(tracer.activeSpan());
      return ok("Hello");
    })
    .build());

  final URL url = new URL("http://localhost:" + server.httpPort() + "/hello/world");
  final HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.setRequestMethod("GET");
  assertEquals(200, connection.getResponseCode());

  server.stop();

  assertEquals(1, tracer.finishedSpans().size());
  assertEquals(PlayAgentIntercept.COMPONENT_NAME, tracer.finishedSpans().get(0).tags().get(Tags.COMPONENT.getKey()));
}
 
Example #2
Source File: PlayITest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] args) throws IOException {
  final Server server = Server.forRouter(components -> RoutingDsl.fromComponents(components)
    .GET("/hello/:to")
    .routeTo(request -> {
      TestUtil.checkActiveSpan();
      return ok("Hello");
    })
    .build());

  final URL url = new URL("http://localhost:" + server.httpPort() + "/hello/world");
  final HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.setRequestMethod("GET");
  final int responseCode = connection.getResponseCode();
  server.stop();

  if (200 != responseCode)
    throw new AssertionError("ERROR: response: " + responseCode);

  TestUtil.checkSpan(true, new ComponentSpanCount("play", 1), new ComponentSpanCount("http-url-connection", 1));
}
 
Example #3
Source File: HttpAuthenticationFilterTest.java    From commercetools-sunrise-java with Apache License 2.0 5 votes vote down vote up
private TestServer testServer(final boolean isAuthEnabled) {
    final Router router = new RoutingDsl()
            .GET(URI).routeTo(() -> ok())
            .build();
    final Application app = new GuiceApplicationBuilder()
            .configure("play.http.filters", "com.commercetools.sunrise.httpauth.basic.BasicHttpAuthenticationFilters")
            .overrides(
                    bind(HttpAuthentication.class).toInstance(httpAuthentication(isAuthEnabled)),
                    bind(play.api.routing.Router.class).toInstance(router.asScala()))
            .build();
    return Helpers.testServer(app);
}
 
Example #4
Source File: BasicHttpAuthenticationFilterTest.java    From commercetools-sunrise-java with Apache License 2.0 5 votes vote down vote up
@Override
protected Application provideApplication() {
    final Router router = new RoutingDsl()
            .GET(URI).routeTo(() -> ok())
            .build();
    return new GuiceApplicationBuilder()
            .configure("play.http.filters", "com.commercetools.sunrise.httpauth.basic.BasicHttpAuthenticationFilters")
            .overrides(
                    bind(HttpAuthentication.class).toInstance(new BasicHttpAuthentication(REALM, USERNAME + ":" + PASSWORD)),
                    bind(play.api.routing.Router.class).toInstance(router.asScala()))
            .build();
}
 
Example #5
Source File: StatusServer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
protected static Router createRouter(StatusStorage statusStorage, BuiltInComponents builtInComponents){
    RoutingDsl dsl = RoutingDsl.fromComponents(builtInComponents);
    dsl.GET("/ids/").routingTo(request -> ok(toJson(statusStorage.ids())));
    dsl.GET("/state/:id").routingTo((request, id) -> ok(toJson(statusStorage.getState(Integer.parseInt(id.toString())))));
    dsl.GET("/opType/:id").routingTo((request, id) -> ok(toJson(ServerTypeJson.builder()
            .type(statusStorage.getState(Integer.parseInt(id.toString())).serverType()))));
    dsl.GET("/started/:id").routingTo((request, id) -> {
        boolean isMaster = statusStorage.getState(Integer.parseInt(id.toString())).isMaster();
        if(isMaster){
            return ok(toJson(MasterStatus.builder().master(statusStorage.getState(Integer.parseInt(id.toString())).getServerState())
                    //note here that a responder is id + 1
                    .responder(statusStorage.getState(Integer.parseInt(id.toString()) + 1).getServerState())
                    .responderN(statusStorage.getState(Integer.parseInt(id.toString())).getTotalUpdates())
                    .build()));
        } else {
            return ok(toJson(SlaveStatus.builder().slave(statusStorage.getState(Integer.parseInt(id.toString())).serverType()).build()));
        }
    });
    dsl.GET("/connectioninfo/:id").routingTo((request, id) -> ok(toJson(statusStorage.getState(Integer.parseInt(id.toString())).getConnectionInfo())));

    dsl.POST("/updatestatus/:id").routingTo((request, id) -> {
        SubscriberState subscriberState = Json.fromJson(request.body().asJson(), SubscriberState.class);
        statusStorage.updateState(subscriberState);
        return ok(toJson(subscriberState));
    });

    return dsl.build();
}