play.server.Server Java Examples

The following examples show how to use play.server.Server. 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: SslPlayHandler.java    From restcommander with Apache License 2.0 6 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    // We have to redirect to https://, as it was targeting http://
    // Redirect to the root as we don't know the url at that point
    if (e.getCause() instanceof SSLException) {
        Logger.debug(e.getCause(), "");
        InetSocketAddress inet = ((InetSocketAddress) ctx.getAttachment());
        ctx.getPipeline().remove("ssl");
        HttpResponse nettyResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.TEMPORARY_REDIRECT);
        nettyResponse.setHeader(LOCATION, "https://" + inet.getHostName() + ":" + Server.httpsPort + "/");
        ChannelFuture writeFuture = ctx.getChannel().write(nettyResponse);
        writeFuture.addListener(ChannelFutureListener.CLOSE);
    } else {
        Logger.error(e.getCause(), "");
        e.getChannel().close();
    }
}
 
Example #4
Source File: ImageSparkTransformServer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public void runMain(String[] args) throws Exception {
    JCommander jcmdr = new JCommander(this);

    try {
        jcmdr.parse(args);
    } catch (ParameterException e) {
        //User provides invalid input -> print the usage info
        jcmdr.usage();
        if (jsonPath == null)
            System.err.println("Json path parameter is missing.");
        try {
            Thread.sleep(500);
        } catch (Exception e2) {
        }
        System.exit(1);
    }

    if (jsonPath != null) {
        String json = FileUtils.readFileToString(new File(jsonPath));
        ImageTransformProcess transformProcess = ImageTransformProcess.fromJson(json);
        transform = new ImageSparkTransform(transformProcess);
    } else {
        log.warn("Server started with no json for transform process. Please ensure you specify a transform process via sending a post request with raw json"
                + "to /transformprocess");
    }

    server = Server.forRouter(Mode.PROD, port, this::createRouter);
}
 
Example #5
Source File: StatusServerTests.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void runStatusServer() {
    Server server = StatusServer.startServer(new InMemoryStatusStorage(), 65236);
    server.stop();
}
 
Example #6
Source File: CSVSparkTransformServer.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public void runMain(String[] args) throws Exception {
    JCommander jcmdr = new JCommander(this);

    try {
        jcmdr.parse(args);
    } catch (ParameterException e) {
        //User provides invalid input -> print the usage info
        jcmdr.usage();
        if (jsonPath == null)
            System.err.println("Json path parameter is missing.");
        try {
            Thread.sleep(500);
        } catch (Exception e2) {
        }
        System.exit(1);
    }

    if (jsonPath != null) {
        String json = FileUtils.readFileToString(new File(jsonPath));
        TransformProcess transformProcess = TransformProcess.fromJson(json);
        transform = new CSVSparkTransform(transformProcess);
    } else {
        log.warn("Server started with no json for transform process. Please ensure you specify a transform process via sending a post request with raw json"
                + "to /transformprocess");
    }

    //Set play secret key, if required
    //http://www.playframework.com/documentation/latest/ApplicationSecret
    String crypto = System.getProperty("play.crypto.secret");
    if (crypto == null || "changeme".equals(crypto) || "".equals(crypto) ) {
        byte[] newCrypto = new byte[1024];

        new Random().nextBytes(newCrypto);

        String base64 = Base64.getEncoder().encodeToString(newCrypto);
        System.setProperty("play.crypto.secret", base64);
    }


    server = Server.forRouter(Mode.PROD, port, this::createRouter);
}
 
Example #7
Source File: StatusServerTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000L)
public void runStatusServer() {
    Server server = StatusServer.startServer(new InMemoryStatusStorage(), 65236);
    server.stop();
}
 
Example #8
Source File: StatusServer.java    From deeplearning4j with Apache License 2.0 2 votes vote down vote up
/**
 * Start a server based on the given subscriber.
 * Note that for the port to start the server on, you should
 * set the statusServerPortField on the subscriber
 * either manually or via command line. The
 * server defaults to port 9000.
 *
 * The end points are:
 * /opType: returns the opType information (master/slave)
 * /started: if it's a master node, it returns master:started/stopped and responder:started/stopped
 * /connectioninfo: See the SlaveConnectionInfo and MasterConnectionInfo classes for fields.
 * /ids: the list of ids for all of the subscribers
 * @param statusStorage the subscriber to base
 *                   the status server on
 * @return the started server
 */
public static Server startServer(StatusStorage statusStorage, int statusServerPort) {
    log.info("Starting server on port " + statusServerPort);
    return Server.forRouter(Mode.PROD, statusServerPort, builtInComponents -> createRouter(statusStorage, builtInComponents));
}