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.

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