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.

87 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 dnsNameELB = "VertSys-ELB1-b88aad9416b2929b.elb.eu-central-1.amazonaws.com";
  12. private Long getCounter() {
  13. // Verbindung zu S3
  14. AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
  15. String region = s3Client.getRegionName();
  16. return new Long(s3Client.getObjectAsString(bucketName, region));
  17. }
  18. private boolean setCounter(Long counter) {
  19. // Verbindung zu S3
  20. AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
  21. String region = s3Client.getRegionName();
  22. s3Client.putObject(bucketName, region, new Long(counter).toString());
  23. return true;
  24. }
  25. @GET
  26. @Produces(MediaType.TEXT_PLAIN)
  27. public String getCounterPlain() {
  28. return "Plain-Text: " + getCounter();
  29. }
  30. @GET
  31. @Produces(MediaType.TEXT_HTML)
  32. public String getCounterHtml() {
  33. return "<html><head><title>TCPTimeCounter REST Service</title>" +
  34. "<meta http-equiv=\"refresh\" content=\"5\"/></head><body>" +
  35. "<h2>Verteilte Systeme HS Fulda - TCPTimeCounter REST Service</h2>"+
  36. "<p><b>HTML-Output:</b> " + getCounter() + "</p></body></html>";
  37. }
  38. @GET
  39. @Produces(MediaType.APPLICATION_JSON)
  40. public String getCounterJson() {
  41. return "{\n \"type\": \"JSON\",\n \"output\": \"" + getCounter() + "\"\n}";
  42. }
  43. @POST
  44. @Consumes(MediaType.TEXT_PLAIN)
  45. @Produces(MediaType.TEXT_PLAIN)
  46. public String incrCounterPlain(@FormParam("input") String input) {
  47. Long currentCounterValue = getCounter();
  48. currentCounterValue = currentCounterValue + (new Long(input)).longValue();
  49. setCounter(currentCounterValue);
  50. return "Plain-Text: TCPTimeCounter counter increased by " + input + " to " + currentCounterValue;
  51. }
  52. @POST
  53. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  54. @Produces(MediaType.TEXT_HTML)
  55. public String incrCounterHtml(@FormParam("input") String input) {
  56. Long currentCounterValue = getCounter();
  57. currentCounterValue = currentCounterValue + (new Long(input)).longValue();
  58. setCounter(currentCounterValue);
  59. return "<html><head><title>TCPTimeCounter REST Service</title></head></body>" +
  60. "<h2>Verteilte Systeme HS Fulda - TCPTimeCounter REST Service</h2>" +
  61. "<p><b>HTML-Output:</b> counter increased by " +
  62. input + " to " + currentCounterValue + "</p>" +
  63. "<form method=POST action=\"http://" + dnsNameELB + ":36042/counter\">" +
  64. "<input name=\"input\" value=\"\"></form></body></html>";
  65. }
  66. @POST
  67. @Consumes(MediaType.TEXT_PLAIN)
  68. @Produces(MediaType.APPLICATION_JSON)
  69. public String incrCounterJson(@QueryParam("input") String input) {
  70. Long currentCounterValue = getCounter();
  71. currentCounterValue = currentCounterValue + (new Long(input)).longValue();
  72. setCounter(currentCounterValue);
  73. return "{\n \"type\": \"JSON\",\n \"output\": \"Plain-Text: TCPTimeCounter counter increased by " + input
  74. + " to " + currentCounterValue + "\"\n}";
  75. }
  76. }