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.

92 lines
3.4 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.regions.Regions;
  6. import com.amazonaws.services.s3.AmazonS3;
  7. import com.amazonaws.services.s3.AmazonS3ClientBuilder;
  8. @Path(TCPTimeCounterRESTService.webContextPath)
  9. public class TCPTimeCounterRESTService {
  10. static final String webContextPath = "/counter";
  11. static final String bucketName = "vertsys-counter";
  12. static final String bucketRegion = "eu-central-1";
  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.setRegion(com.amazonaws.regions.Region.getRegion(Regions.EU_CENTRAL_1));
  22. s3Client.putObject(bucketName, bucketRegion, 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>HS Fulda - TCPTimeCounter REST Service</h2>"+
  36. "<p><b>HTML-Output:</b> " + getCounter() + "</p></body>"+
  37. "<form method=POST action=\"/counter\">" +
  38. "<input type=\"hidden\" name=\"input\" value=\"1\">"+
  39. "<input type=\"submit\" value=\"Increment\"></form></body></html>";
  40. }
  41. @GET
  42. @Produces(MediaType.APPLICATION_JSON)
  43. public String getCounterJson() {
  44. return "{\n \"type\": \"JSON\",\n \"output\": \"" + getCounter() + "\"\n}";
  45. }
  46. @POST
  47. @Consumes(MediaType.TEXT_PLAIN)
  48. @Produces(MediaType.TEXT_PLAIN)
  49. public String incrCounterPlain(@FormParam("input") String input) {
  50. Long currentCounterValue = getCounter();
  51. currentCounterValue = currentCounterValue + (new Long(input)).longValue();
  52. setCounter(currentCounterValue);
  53. return "Plain-Text: TCPTimeCounter counter increased by " + input + " to " + currentCounterValue;
  54. }
  55. @POST
  56. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  57. @Produces(MediaType.TEXT_HTML)
  58. public String incrCounterHtml(@FormParam("input") String input) {
  59. Long currentCounterValue = getCounter();
  60. currentCounterValue = currentCounterValue + (new Long(input)).longValue();
  61. setCounter(currentCounterValue);
  62. return "<html><head><title>TCPTimeCounter REST Service</title>" +
  63. "<meta http-equiv=\"refresh\" content=\"5\"/></head><body>" +
  64. "<h2>HS Fulda - TCPTimeCounter REST Service</h2>"+
  65. "<p><b>HTML-Output:</b> " + getCounter() + "</p></body>"+
  66. "<form method=POST action=\"/counter\">" +
  67. "<input type=\"hidden\" name=\"input\" value=\"1\">"+
  68. "<input type=\"submit\" value=\"Increment\"></form></body></html>";
  69. }
  70. @POST
  71. @Consumes(MediaType.TEXT_PLAIN)
  72. @Produces(MediaType.APPLICATION_JSON)
  73. public String incrCounterJson(@QueryParam("input") String input) {
  74. Long currentCounterValue = getCounter();
  75. currentCounterValue = currentCounterValue + (new Long(input)).longValue();
  76. setCounter(currentCounterValue);
  77. return "{\n \"type\": \"JSON\",\n \"output\": \"Plain-Text: TCPTimeCounter counter increased by " + input
  78. + " to " + currentCounterValue + "\"\n}";
  79. }
  80. }