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.

77 lines
2.7 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. private Long getCounter() {
  12. // Verbindung zu S3
  13. AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
  14. String region = s3Client.getRegionName();
  15. return new Long(s3Client.getObjectAsString(bucketName, region));
  16. }
  17. private boolean setCounter(Long counter) {
  18. // Verbindung zu S3
  19. AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
  20. String region = s3Client.getRegionName();
  21. s3Client.putObject(bucketName, region, 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><meta http-equiv=\"refresh\" content=\"5\"/></head><body><h2>HTML: " + getCounter() + "</h2></body></html>";
  33. }
  34. @GET
  35. @Produces(MediaType.APPLICATION_JSON)
  36. public String getCounterJson() {
  37. return "{\n \"type\": \"JSON\",\n \"output\": \"" + getCounter() + "\"\n}";
  38. }
  39. @POST
  40. @Consumes(MediaType.TEXT_PLAIN)
  41. @Produces(MediaType.TEXT_PLAIN)
  42. public String incrCounterPlain(@FormParam("input") String input) {
  43. Long currentCounterValue = getCounter();
  44. currentCounterValue = currentCounterValue + (new Long(input)).longValue();
  45. setCounter(currentCounterValue);
  46. return "Plain-Text: TCPTimeCounter counter increased by " + input + " to " + currentCounterValue;
  47. }
  48. @POST
  49. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  50. @Produces(MediaType.TEXT_HTML)
  51. public String incrCounterHtml(@FormParam("input") String input) {
  52. Long currentCounterValue = getCounter();
  53. currentCounterValue = currentCounterValue + (new Long(input)).longValue();
  54. setCounter(currentCounterValue);
  55. return "<html><title>TCPTimeCounter counter increased by " + input + " to " + currentCounterValue + "</h2></body></html>";
  56. }
  57. @POST
  58. @Consumes(MediaType.TEXT_PLAIN)
  59. @Produces(MediaType.APPLICATION_JSON)
  60. public String incrCounterJson(@QueryParam("input") String input) {
  61. Long currentCounterValue = getCounter();
  62. currentCounterValue = currentCounterValue + (new Long(input)).longValue();
  63. setCounter(currentCounterValue);
  64. return "{\n \"type\": \"JSON\",\n \"output\": \"Plain-Text: TCPTimeCounter counter increased by " + input + " to " + currentCounterValue + "\"\n}";
  65. }
  66. }