Roberto Sánchez
2013-12-17 9dee3b104cccba41cab0f4369bec444485da7014
#333 feature - Added Guice support to jersey framework
1 files added
2 files modified
changed files
securis/pom.xml patch | view | blame | history
securis/src/main/java/net/curisit/securis/MainApp.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/LicenseServices.java patch | view | blame | history
securis/pom.xml
....@@ -26,6 +26,11 @@
2626 <artifactId>commons</artifactId>
2727 <version>0.0.1-SNAPSHOT</version>
2828 </dependency>
29
+ <dependency>
30
+ <groupId>com.sun.jersey.contribs</groupId>
31
+ <artifactId>jersey-guice</artifactId>
32
+ <version>1.18</version>
33
+ </dependency>
2934 </dependencies>
3035 <build>
3136 <plugins>
securis/src/main/java/net/curisit/securis/MainApp.java
....@@ -1,18 +1,55 @@
11 package net.curisit.securis;
22
3
+import java.io.IOException;
4
+import java.net.URI;
5
+import java.util.Arrays;
6
+
7
+import javax.inject.Inject;
8
+import javax.inject.Named;
9
+
10
+import net.curisit.securis.ioc.SecurisModule;
11
+
312 import org.glassfish.grizzly.http.server.HttpServer;
413 import org.slf4j.Logger;
514 import org.slf4j.LoggerFactory;
15
+
16
+import com.google.inject.Guice;
17
+import com.google.inject.Injector;
18
+import com.google.inject.Key;
19
+import com.google.inject.name.Names;
20
+import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
21
+import com.sun.jersey.api.core.PackagesResourceConfig;
22
+import com.sun.jersey.api.core.ResourceConfig;
23
+import com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory;
24
+import com.sun.jersey.guice.spi.container.GuiceComponentProviderFactory;
625
726 public class MainApp {
827
928 private static final Logger log = LoggerFactory.getLogger(MainApp.class);
1029
11
- private HttpServer mHttpServer;
30
+ private static HttpServer mHttpServer;
31
+ private static Injector injector = null;
1232
13
- public static void main(String[] args) {
33
+ @Inject
34
+ @Named("base-uri")
35
+ private URI uri;
36
+
37
+ public static void main(String[] args) throws IOException, InterruptedException {
1438 log.info("SeCuris init...");
1539
40
+ injector = Guice.createInjector(Arrays.asList(new SecurisModule()));
41
+ mHttpServer = startServer(injector.getInstance(Key.get(URI.class, Names.named("base-uri"))));
42
+ while (true) {
43
+ Thread.currentThread().sleep(100);
44
+ }
45
+ }
46
+
47
+ private static HttpServer startServer(URI uri) throws IOException {
48
+ System.out.println("Starting grizzly2...");
49
+ ResourceConfig rc = new PackagesResourceConfig("net.curisit.securis.services", "org.codehaus.jackson.jaxrs");
50
+ IoCComponentProviderFactory ioc = new GuiceComponentProviderFactory(rc, injector);
51
+
52
+ return GrizzlyServerFactory.createHttpServer(uri, rc, ioc);
1653 }
1754
1855 }
securis/src/main/java/net/curisit/securis/services/LicenseServices.java
....@@ -0,0 +1,115 @@
1
+package net.curisit.securis.services;
2
+
3
+import java.net.URI;
4
+import java.text.MessageFormat;
5
+
6
+import javax.inject.Inject;
7
+import javax.inject.Named;
8
+import javax.ws.rs.DefaultValue;
9
+import javax.ws.rs.GET;
10
+import javax.ws.rs.Path;
11
+import javax.ws.rs.PathParam;
12
+import javax.ws.rs.Produces;
13
+import javax.ws.rs.QueryParam;
14
+import javax.ws.rs.core.MediaType;
15
+import javax.ws.rs.core.Response;
16
+
17
+import net.curisit.integrity.beans.ServerConfigVersions;
18
+import net.curisit.integrity.beans.ServiceResponse;
19
+
20
+import org.slf4j.Logger;
21
+import org.slf4j.LoggerFactory;
22
+
23
+// The Java class will be hosted at the URI path "/myresource"
24
+@Path("/license")
25
+public class LicenseServices {
26
+
27
+ // private LicenseHelper licenseHelper = InjectorFactory.getInjector().getInstance(LicenseHelper.class);
28
+ private static final Logger log = LoggerFactory.getLogger(LicenseServices.class);
29
+
30
+ private static final int DEFAULT_LICENSE_EXPIRATION = 3650; // 10 years;
31
+ private static final String LICENSE_STRING = "CurisIntegrity Config Server v{0}.{1}";
32
+
33
+ @Inject
34
+ @Named("base-uri")
35
+ private URI uri;
36
+
37
+ @Inject
38
+ public LicenseServices() {
39
+ }
40
+
41
+ /**
42
+ *
43
+ * @return the server version in format majorVersion.minorVersion
44
+ */
45
+ @GET
46
+ @Path("/")
47
+ @Produces(
48
+ { MediaType.TEXT_PLAIN })
49
+ public Response currentVersion() {
50
+ return Response.ok().entity(MessageFormat.format(LICENSE_STRING, 0, 1)).build();
51
+ }
52
+
53
+ @GET
54
+ @Path("/dummy")
55
+ @Produces(
56
+ { MediaType.TEXT_PLAIN })
57
+ public Response dummy() {
58
+ return Response.ok().entity(uri.toString()).build();
59
+ }
60
+
61
+ /**
62
+ * @return the version of the three entities that can be synchronized (Users, DataSet and Settings)
63
+ */
64
+ @GET
65
+ @Path("/current/{license}")
66
+ @Produces(
67
+ { MediaType.APPLICATION_JSON })
68
+ public ServiceResponse<ServerConfigVersions> status(@PathParam("license") String license, @DefaultValue("-1") @QueryParam("minorVersion") int minorVersion, @DefaultValue("-1") @QueryParam("majorVersion") int majorVersion) {
69
+
70
+ log.info("Called 'current' service with license: {}", license);
71
+ ServiceResponse<ServerConfigVersions> response = new ServiceResponse<ServerConfigVersions>();
72
+
73
+ return response;
74
+ }
75
+ //
76
+ // private <T> ServiceResponse<T> buildErrorResponse(ServiceResponse<T> response, String msgErrorCode) {
77
+ // response.setSuccess(false);
78
+ // response.setErrorMessage(localManager.getString(msgErrorCode));
79
+ // response.setErrorMessageCode(msgErrorCode);
80
+ // return response;
81
+ // }
82
+ //
83
+ // private Date calculateCaducation() {
84
+ // Integer licenseExpiration = systemParams.getParamAsInt(SystemParams.Keys.CONFIG_SERVER_LICENSE_EXPIRATION);
85
+ // if (licenseExpiration == null)
86
+ // licenseExpiration = DEFAULT_LICENSE_EXPIRATION;
87
+ // return Utils.addDays(new Date(), licenseExpiration);
88
+ // }
89
+ //
90
+ // private boolean validateLicense(String license) {
91
+ // BasicApplication ba = basicApplicationDao.findByLicense(license);
92
+ // return (ba != null);
93
+ // }
94
+ //
95
+ // private boolean validateVersion(int minorVersion, int majorVersion) {
96
+ // return (versionManager.getMajorVersion() == majorVersion);
97
+ // }
98
+ //
99
+ // private BasicApplication findBasicApp(String license) {
100
+ // BasicApplication ba = basicApplicationDao.findByLicense(license);
101
+ // return ba;
102
+ // }
103
+ //
104
+ // private License generateLicense() {
105
+ // // TODO complete all field of the license
106
+ // License license = new License();
107
+ // license.setCustomerCode(systemParams.getParam(SystemParams.Keys.CONFIG_COMMON_CUSTOMER_CODE));
108
+ // license.setCSCode(systemParams.getParam(SystemParams.Keys.CONFIG_COMMON_CS_CODE));
109
+ // license.setCRCLogo("00000000");
110
+ // license.setExpirationDate(calculateCaducation());
111
+ // license.setInstallCode(codeGenerator.generateInstalationNumber());
112
+ // return license;
113
+ // }
114
+
115
+}