Roberto Sánchez
2013-12-23 d7ee99d10fc17ca29511b2f1e551fcd1dd1c2c8e
securis/src/main/java/net/curisit/securis/services/BasicServices.java
....@@ -1,34 +1,29 @@
11 package net.curisit.securis.services;
22
3
-import java.io.IOException;
43 import java.net.URI;
4
+import java.util.Date;
55
6
-import javax.annotation.security.RolesAllowed;
76 import javax.servlet.http.HttpServletRequest;
8
-import javax.ws.rs.FormParam;
7
+import javax.servlet.http.HttpSession;
98 import javax.ws.rs.GET;
10
-import javax.ws.rs.POST;
119 import javax.ws.rs.Path;
1210 import javax.ws.rs.Produces;
1311 import javax.ws.rs.core.Context;
1412 import javax.ws.rs.core.MediaType;
1513 import javax.ws.rs.core.Response;
16
-import javax.ws.rs.core.Response.Status;
1714 import javax.ws.rs.core.UriBuilder;
1815
19
-import org.apache.commons.io.IOUtils;
2016 import org.slf4j.Logger;
2117 import org.slf4j.LoggerFactory;
2218
2319 /**
24
- * Basic services for login a nd basic app wrkflow
20
+ * Basic services for login and basic app wrkflow
2521 *
2622 * @author roberto <roberto.sanchez@curisit.net>
2723 */
2824 @Path("/")
2925 public class BasicServices {
3026
31
- // private LicenseHelper licenseHelper = InjectorFactory.getInjector().getInstance(LicenseHelper.class);
3227 private static final Logger log = LoggerFactory.getLogger(BasicServices.class);
3328
3429 public BasicServices() {
....@@ -39,115 +34,24 @@
3934 * @return the server version in format majorVersion.minorVersion
4035 */
4136 @GET
37
+ @Path("/index")
4238 @Produces(
4339 { MediaType.TEXT_HTML })
4440 public Response index(@Context HttpServletRequest request) {
4541 log.info("index session: " + request.getSession());
46
- URI uri = UriBuilder.fromUri("/login").build();
42
+ HttpSession session = request.getSession(false);
43
+ String page = session != null && session.getAttribute("user") != null ? "/main.html" : "/login.html";
44
+ URI uri = UriBuilder.fromUri(page).build();
4745 return Response.seeOther(uri).build();
4846 // return Response.ok().entity("License server").build();
4947 }
5048
5149 @GET
52
- @Path("/login")
50
+ @Path("/info")
5351 @Produces(
54
- { MediaType.TEXT_HTML })
55
- public Response login(@Context HttpServletRequest request) {
56
- log.info("index login: " + request.getSession());
57
- try {
58
- String index = IOUtils.toString(this.getClass().getResourceAsStream("/static/login.html"));
59
- return Response.ok().entity(index).build();
60
- } catch (IOException e) {
61
- // TODO Auto-generated catch block
62
- e.printStackTrace();
63
- }
64
- return Response.serverError().build();
52
+ { MediaType.TEXT_PLAIN })
53
+ public Response info(@Context HttpServletRequest request) {
54
+ return Response.ok().entity("License server running OK. Date: " + new Date()).build();
6555 }
66
-
67
- @POST
68
- @Path("/login")
69
- @Produces(
70
- { MediaType.TEXT_HTML })
71
- public Response login(@FormParam("user") String user, @FormParam("password") String password, @Context HttpServletRequest request) {
72
- log.info("index session: " + request.getSession());
73
- log.info("Request: " + request.getParameter("user"));
74
- log.info("is user in role: {} == {} ? ", "advance", request.isUserInRole("advance"));
75
- // log.info("user: {} == {} ? " + request.getParameter("user"), user);
76
- request.getSession().setAttribute("user", user);
77
- URI uri = UriBuilder.fromUri("/main").build();
78
- return Response.seeOther(uri).build();
79
- }
80
-
81
- /**
82
- * @return the version of the three entities that can be synchronized (Users, DataSet and Settings)
83
- */
84
- @GET
85
- @Path("/main")
86
- @Produces(
87
- { MediaType.TEXT_HTML })
88
- @RolesAllowed("advance")
89
- public Response main(@Context HttpServletRequest request) {
90
- try {
91
- log.info("Is user in role advance: {}", request.isUserInRole("advance"));
92
- String index = IOUtils.toString(this.getClass().getResourceAsStream("/static/main.html"));
93
- return Response.ok().entity(index).build();
94
- } catch (IOException e) {
95
- // TODO Auto-generated catch block
96
- e.printStackTrace();
97
- }
98
- return Response.status(Status.FORBIDDEN).build();
99
-
100
- }
101
-
102
- @GET
103
- @Path("/logout")
104
- @Produces(
105
- { MediaType.TEXT_HTML })
106
- public Response logout(@Context HttpServletRequest request) {
107
- request.getSession().setAttribute("user", null);
108
- URI uri = UriBuilder.fromUri("/login").build();
109
- return Response.seeOther(uri).build();
110
-
111
- }
112
-
113
- //
114
- // private <T> ServiceResponse<T> buildErrorResponse(ServiceResponse<T> response, String msgErrorCode) {
115
- // response.setSuccess(false);
116
- // response.setErrorMessage(localManager.getString(msgErrorCode));
117
- // response.setErrorMessageCode(msgErrorCode);
118
- // return response;
119
- // }
120
- //
121
- // private Date calculateCaducation() {
122
- // Integer licenseExpiration = systemParams.getParamAsInt(SystemParams.Keys.CONFIG_SERVER_LICENSE_EXPIRATION);
123
- // if (licenseExpiration == null)
124
- // licenseExpiration = DEFAULT_LICENSE_EXPIRATION;
125
- // return Utils.addDays(new Date(), licenseExpiration);
126
- // }
127
- //
128
- // private boolean validateLicense(String license) {
129
- // BasicApplication ba = basicApplicationDao.findByLicense(license);
130
- // return (ba != null);
131
- // }
132
- //
133
- // private boolean validateVersion(int minorVersion, int majorVersion) {
134
- // return (versionManager.getMajorVersion() == majorVersion);
135
- // }
136
- //
137
- // private BasicApplication findBasicApp(String license) {
138
- // BasicApplication ba = basicApplicationDao.findByLicense(license);
139
- // return ba;
140
- // }
141
- //
142
- // private License generateLicense() {
143
- // // TODO complete all field of the license
144
- // License license = new License();
145
- // license.setCustomerCode(systemParams.getParam(SystemParams.Keys.CONFIG_COMMON_CUSTOMER_CODE));
146
- // license.setCSCode(systemParams.getParam(SystemParams.Keys.CONFIG_COMMON_CS_CODE));
147
- // license.setCRCLogo("00000000");
148
- // license.setExpirationDate(calculateCaducation());
149
- // license.setInstallCode(codeGenerator.generateInstalationNumber());
150
- // return license;
151
- // }
15256
15357 }