Roberto Sánchez
2014-09-19 8d5386be38db25a2a41c3bf6c876adee21ca26cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import net.curisit.integrity.beans.ServerConfigVersions;
import net.curisit.integrity.beans.ServiceResponse;
import org.apache.commons.io.IOUtils;
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
import org.jboss.resteasy.annotations.providers.multipart.PartType;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
// 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<ServerConfigVersions> 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<ServerConfigVersions> response = new ServiceResponse<ServerConfigVersions>();
        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;
        }
    }
}