Java tutorial
package pl.psi.licensing.boundary; import com.google.common.base.Charsets; import com.google.common.io.CharStreams; import pl.psi.licensing.boundary.responses.LicensingSystemResponse; import pl.psi.licensing.license.monitoring.LicenseService; import pl.psi.licensing.license.monitoring.exception.LicenseServiceException; import javax.inject.Inject; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * Created by mblaszyk on 2016-07-22. */ @Path("/admin_all") public class GeneralAdministrationEndpoint { @Inject LicenseService licenseService; /** * Endpoint returning active license */ @GET @Path("/getLicenseInfo") @Produces(MediaType.APPLICATION_JSON) public Response getLicenseInfo() { try { return Response.ok(licenseService.getLicenseInfo()).build(); } catch (LicenseServiceException e) { return Response.status(Response.Status.NOT_FOUND).build(); } } /** * Endpoint giving info on all active sessions */ @GET @Path("/allActiveSessionInfo") @Produces(MediaType.APPLICATION_JSON) public Response getAllActiveSessions() { return Response.ok(licenseService.getAllActiveSessionsInfo()).build(); } /** * Endpoint terminating all active sessions */ @DELETE @Path("/terminateAllSession") @Produces(MediaType.APPLICATION_JSON) public Response terminateAllSessions() { licenseService.terminateAllSessions(); return Response.noContent().build(); } /** * Endpoint for uploading a new license */ @POST @Path("/uploadNewLicense") @Consumes(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.APPLICATION_JSON) public Response uploadNewLicense(InputStream license) { String licenseText = ""; try { licenseText = CharStreams.toString(new InputStreamReader(license, Charsets.UTF_8)); } catch (IOException e) { e.printStackTrace(); } LicensingSystemResponse resp = licenseService.introduceNewLicense(licenseText); return Response.ok(resp).build(); } }