|
|
@ -0,0 +1,78 @@ |
|
|
|
/* 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.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"; |
|
|
|
|
|
|
|
private Long getCounter() { |
|
|
|
// Verbindung zu S3 |
|
|
|
AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); |
|
|
|
String region = s3Client.getRegionName(); |
|
|
|
return new Long(s3Client.getObjectAsString(bucketName, region)); |
|
|
|
} |
|
|
|
|
|
|
|
private boolean setCounter(Long counter) { |
|
|
|
// Verbindung zu S3 |
|
|
|
AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); |
|
|
|
String region = s3Client.getRegionName(); |
|
|
|
s3Client.putObject(bucketName, region, 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 "<html><head><title>TCPTimeCounter REST Service</title><meta http-equiv=\"refresh\" content=\"5\"/></head><body><h2>HTML: " + getCounter() + "</h2></body></html>"; |
|
|
|
} |
|
|
|
|
|
|
|
@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 "<html><title>TCPTimeCounter counter increased by " + input + " to " + currentCounterValue + "</h2></body></html>"; |
|
|
|
} |
|
|
|
|
|
|
|
@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}"; |
|
|
|
} |
|
|
|
|
|
|
|
} |