/* Beispiel angelehnt an http://www.torsten-horn.de/techdocs/jee-rest.htm */ package verteiltesysteme.aws; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; @Path(TCPTimeCounterRESTService.webContextPath) public class TCPTimeCounterRESTService { static final String webContextPath = "/counter"; static final String bucketName = "vertsys-counter"; static final String bucketRegion = "eu-central-1"; private Long getCounter() { // Verbindung zu S3 AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); return new Long(s3Client.getObjectAsString(bucketName, bucketRegion)); } private boolean setCounter(Long counter) { // Verbindung zu S3 AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); s3Client.setRegion(com.amazonaws.regions.Region.getRegion(Regions.EU_CENTRAL_1)); s3Client.putObject(bucketName, bucketRegion, new Long(counter).toString()); return true; } @GET @Produces(MediaType.TEXT_PLAIN) public String getCounterPlain() { return "Plain-Text: " + getCounter(); } @GET @Produces(MediaType.TEXT_HTML) public String getCounterHtml() { return "TCPTimeCounter REST Service" + "" + "

HS Fulda - TCPTimeCounter REST Service

"+ "

HTML-Output: " + getCounter() + "

"+ "
" + ""+ "
"; } @GET @Produces(MediaType.APPLICATION_JSON) public String getCounterJson() { return "{\n \"type\": \"JSON\",\n \"output\": \"" + getCounter() + "\"\n}"; } @POST @Consumes(MediaType.TEXT_PLAIN) @Produces(MediaType.TEXT_PLAIN) public String incrCounterPlain(@FormParam("input") String input) { Long currentCounterValue = getCounter(); currentCounterValue = currentCounterValue + (new Long(input)).longValue(); setCounter(currentCounterValue); return "Plain-Text: TCPTimeCounter counter increased by " + input + " to " + currentCounterValue; } @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.TEXT_HTML) public String incrCounterHtml(@FormParam("input") String input) { Long currentCounterValue = getCounter(); currentCounterValue = currentCounterValue + (new Long(input)).longValue(); setCounter(currentCounterValue); return "TCPTimeCounter REST Service" + "" + "

HS Fulda - TCPTimeCounter REST Service

"+ "

HTML-Output: " + getCounter() + "

"+ "
" + ""+ "
"; } @POST @Consumes(MediaType.TEXT_PLAIN) @Produces(MediaType.APPLICATION_JSON) public String incrCounterJson(@QueryParam("input") String input) { Long currentCounterValue = getCounter(); currentCounterValue = currentCounterValue + (new Long(input)).longValue(); setCounter(currentCounterValue); return "{\n \"type\": \"JSON\",\n \"output\": \"Plain-Text: TCPTimeCounter counter increased by " + input + " to " + currentCounterValue + "\"\n}"; } }