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.

90 lines
3.3 KiB

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