package net.curisit.securis.services; import java.io.IOException; import java.net.URI; import java.text.MessageFormat; import javax.inject.Named; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jboss.resteasy.annotations.providers.multipart.MultipartForm; import org.jboss.resteasy.annotations.providers.multipart.PartType; // The Java class will be hosted at the URI path "/myresource" @Path("/test") public class LicenseServices { private static final Logger LOG = LogManager.getLogger(LicenseServices.class); private static final int DEFAULT_LICENSE_EXPIRATION = 365; private static final String LICENSE_STRING = "CurisIntegrity Config Server v{0}.{1}"; @com.google.inject.Inject @Named("base-uri") private URI uri; public LicenseServices() { } /** * * @return the server version in format majorVersion.minorVersion */ @GET @Produces({ MediaType.TEXT_HTML }) public Response index() { try { String index = IOUtils.toString(this.getClass().getResourceAsStream("/static/index.html")); return Response.ok().entity(index).build(); } catch (IOException e) { LOG.error("Error getting index.html", e); } return Response.ok().entity(MessageFormat.format(LICENSE_STRING, 0, 1)).build(); } @GET @Path("/dummy") @Produces({ MediaType.TEXT_PLAIN }) public Response dummy(@Context HttpServletRequest request) { LOG.info("Request: " + request.getPathInfo()); return Response.ok().entity((uri == null)).build(); } /** * @return the version of the three entities that can be synchronized * (Users, DataSet and Settings) */ @POST @Path("/upload1") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces({ MediaType.APPLICATION_JSON }) public Response testFile1(@MultipartForm FileUploadForm mfdi) { LOG.info("FORM: texto: {}, file: {}", mfdi.getTexto(), new String(mfdi.getFile())); return Response.ok("OK").build(); } // @GET // @Path("/current/{license}") // @Produces({ // MediaType.APPLICATION_JSON // }) // public ServiceResponse testFile(@PathParam("license") String license, // @DefaultValue("-1") @QueryParam("minorVersion") int minorVersion, @DefaultValue("-1") @QueryParam("majorVersion") int majorVersion) { // // LOG.info("Called 'current' service with license: {}", license); // ServiceResponse response = new ServiceResponse(); // // return response; // } public static class FileUploadForm { @FormParam("file1") @PartType("application/octet-stream") private byte[] file; @FormParam("texto1") @PartType("text/plain") private String texto; public FileUploadForm() { } public byte[] getFile() { return file; } public void setFile(byte[] file) { this.file = file; } public void setTexto(final String texto) { this.texto = texto; } public String getTexto() { return texto; } } }