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.

88 lines
3.2 KiB

  1. /* Beispiel angelehnt an http://www.torsten-horn.de/techdocs/jee-rest.htm */
  2. package verteiltesysteme.aws;
  3. import javax.ws.rs.*;
  4. import javax.ws.rs.core.MediaType;
  5. import com.amazonaws.services.s3.AmazonS3;
  6. import com.amazonaws.services.s3.AmazonS3ClientBuilder;
  7. @Path(TCPTimeCounterRESTService.webContextPath)
  8. public class TCPTimeCounterRESTService {
  9. static final String webContextPath = "/counter";
  10. static final String bucketName = "vertsys-counter";
  11. static final String bucketRegion = "eu-central-1";
  12. static final String dnsNameELB = "VertSys-ELB1-b88aad9416b2929b.elb.eu-central-1.amazonaws.com";
  13. private Long getCounter() {
  14. // Verbindung zu S3
  15. AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
  16. return new Long(s3Client.getObjectAsString(bucketName, bucketRegion));
  17. }
  18. private boolean setCounter(Long counter) {
  19. // Verbindung zu S3
  20. AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
  21. s3Client.putObject(bucketName, bucketRegion, new Long(counter).toString());
  22. return true;
  23. }
  24. @GET
  25. @Produces(MediaType.TEXT_PLAIN)
  26. public String getCounterPlain() {
  27. return "Plain-Text: " + getCounter();
  28. }
  29. @GET
  30. @Produces(MediaType.TEXT_HTML)
  31. public String getCounterHtml() {
  32. return "<html><head><title>TCPTimeCounter REST Service</title>" +
  33. "<meta http-equiv=\"refresh\" content=\"5\"/></head><body>" +
  34. "<h2>Verteilte Systeme HS Fulda - TCPTimeCounter REST Service</h2>"+
  35. "<p><b>HTML-Output:</b> " + getCounter() + "</p></body></html>";
  36. }
  37. @GET
  38. @Produces(MediaType.APPLICATION_JSON)
  39. public String getCounterJson() {
  40. return "{\n \"type\": \"JSON\",\n \"output\": \"" + getCounter() + "\"\n}";
  41. }
  42. @POST
  43. @Consumes(MediaType.TEXT_PLAIN)
  44. @Produces(MediaType.TEXT_PLAIN)
  45. public String incrCounterPlain(@FormParam("input") String input) {
  46. Long currentCounterValue = getCounter();
  47. currentCounterValue = currentCounterValue + (new Long(input)).longValue();
  48. setCounter(currentCounterValue);
  49. return "Plain-Text: TCPTimeCounter counter increased by " + input + " to " + currentCounterValue;
  50. }
  51. @POST
  52. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  53. @Produces(MediaType.TEXT_HTML)
  54. public String incrCounterHtml(@FormParam("input") String input) {
  55. Long currentCounterValue = getCounter();
  56. currentCounterValue = currentCounterValue + (new Long(input)).longValue();
  57. setCounter(currentCounterValue);
  58. return "<html><head><title>TCPTimeCounter REST Service</title></head></body>" +
  59. "<h2>Verteilte Systeme HS Fulda - TCPTimeCounter REST Service</h2>" +
  60. "<p><b>HTML-Output:</b> counter increased by " +
  61. input + " to " + currentCounterValue + "</p>" +
  62. "<form method=POST action=\"http://" + dnsNameELB + ":36042/counter\">" +
  63. "<input name=\"input\" value=\"\"></form></body></html>";
  64. }
  65. @POST
  66. @Consumes(MediaType.TEXT_PLAIN)
  67. @Produces(MediaType.APPLICATION_JSON)
  68. public String incrCounterJson(@QueryParam("input") String input) {
  69. Long currentCounterValue = getCounter();
  70. currentCounterValue = currentCounterValue + (new Long(input)).longValue();
  71. setCounter(currentCounterValue);
  72. return "{\n \"type\": \"JSON\",\n \"output\": \"Plain-Text: TCPTimeCounter counter increased by " + input
  73. + " to " + currentCounterValue + "\"\n}";
  74. }
  75. }