You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.8 KiB

  1. /* Beispiel angelehnt an http://www.torsten-horn.de/techdocs/jee-rest.htm */
  2. package verteiltesysteme.aws;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.PrintStream;
  6. import java.net.URI;
  7. import org.glassfish.grizzly.http.server.ErrorPageGenerator;
  8. import org.glassfish.grizzly.http.server.HttpServer;
  9. import org.glassfish.grizzly.http.server.Request;
  10. import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
  11. import org.glassfish.jersey.server.ResourceConfig;
  12. public class TCPTimeCounterRESTServer {
  13. public static void main(String[] args) throws IOException, InterruptedException {
  14. String baseUrl = (args.length > 0) ? args[0] : "http://0.0.0.0:36042";
  15. final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(baseUrl),
  16. new ResourceConfig(TCPTimeCounterRESTService.class), false);
  17. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
  18. @Override
  19. public void run() {
  20. server.shutdownNow();
  21. }
  22. }));
  23. ErrorPageGenerator epg = new ErrorPageGenerator() {
  24. @Override
  25. public String generate(Request request, int status, String reasonPhrase, String description,
  26. Throwable exception) {
  27. StringBuilder sb = new StringBuilder();
  28. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  29. PrintStream ps = new PrintStream(baos);
  30. exception.printStackTrace(ps);
  31. ps.close();
  32. sb.append(new String(baos.toByteArray()));
  33. System.out.println(sb.toString());
  34. return sb.toString();
  35. }
  36. };
  37. server.getServerConfiguration().setDefaultErrorPageGenerator(epg);
  38. server.start();
  39. System.out.println("Grizzly-HTTP-Server gestartet");
  40. System.out.println("Stoppen des Grizzly-HTTP-Servers mit: Strg+C\n");
  41. System.out.println("RESTful Web Service URL: " + baseUrl + TCPTimeCounterRESTService.webContextPath);
  42. Thread.currentThread().join();
  43. }
  44. }