Roberto Sánchez
2013-12-23 d7ee99d10fc17ca29511b2f1e551fcd1dd1c2c8e
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
package net.curisit.securis.services;
import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
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.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import net.curisit.integrity.commons.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * User resource
 * 
 * @author roberto <roberto.sanchez@curisit.net>
 */
@Path("/user")
public class UserResource {
   // private LicenseHelper licenseHelper = InjectorFactory.getInjector().getInstance(LicenseHelper.class);
   private static final Logger log = LoggerFactory.getLogger(UserResource.class);
   public UserResource() {
   }
   /**
    * 
    * @return the server version in format majorVersion.minorVersion
    */
   @GET
   @Path("/")
   @Produces(
       { MediaType.TEXT_PLAIN })
   public Response index(@Context HttpServletRequest request) {
       return Response.ok("User resource").build();
   }
   @POST
   @Path("/login")
   @Produces(
       { MediaType.APPLICATION_JSON })
   public Response login(@FormParam("username") String user, @FormParam("password") String password, @Context HttpServletRequest request) {
       log.info("index session: " + request.getSession());
       log.info("user: {}, pass: {}", user, password);
       log.info("is user in role: {} == {} ? ", "advance", request.isUserInRole("advance"));
       request.getSession().setAttribute("username", user);
       if ("no".equals(password))
           return Response.status(Status.FORBIDDEN).build();
       return Response.ok(Utils.createMap("name", "Pepito", "username", user)).build();
   }
   /**
    * @return the version of the three entities that can be synchronized (Users, DataSet and Settings)
    */
   @GET
   @Path("/{username}")
   @Produces(
       { MediaType.APPLICATION_JSON })
   @RolesAllowed("advance")
   public Response main(@PathParam("username") String username) {
       return Response.ok().entity(Utils.createMap("name", "Pepito", "username", username)).build();
   }
   @GET
   @Path("/logout")
   @Produces(
       { MediaType.APPLICATION_JSON })
   public Response logout(@Context HttpServletRequest request) {
       request.getSession().invalidate();
       return Response.ok().build();
   }
   //
   // private <T> ServiceResponse<T> buildErrorResponse(ServiceResponse<T> response, String msgErrorCode) {
   // response.setSuccess(false);
   // response.setErrorMessage(localManager.getString(msgErrorCode));
   // response.setErrorMessageCode(msgErrorCode);
   // return response;
   // }
   //
   // private Date calculateCaducation() {
   // Integer licenseExpiration = systemParams.getParamAsInt(SystemParams.Keys.CONFIG_SERVER_LICENSE_EXPIRATION);
   // if (licenseExpiration == null)
   // licenseExpiration = DEFAULT_LICENSE_EXPIRATION;
   // return Utils.addDays(new Date(), licenseExpiration);
   // }
   //
   // private boolean validateLicense(String license) {
   // BasicApplication ba = basicApplicationDao.findByLicense(license);
   // return (ba != null);
   // }
   //
   // private boolean validateVersion(int minorVersion, int majorVersion) {
   // return (versionManager.getMajorVersion() == majorVersion);
   // }
   //
   // private BasicApplication findBasicApp(String license) {
   // BasicApplication ba = basicApplicationDao.findByLicense(license);
   // return ba;
   // }
   //
   // private License generateLicense() {
   // // TODO complete all field of the license
   // License license = new License();
   // license.setCustomerCode(systemParams.getParam(SystemParams.Keys.CONFIG_COMMON_CUSTOMER_CODE));
   // license.setCSCode(systemParams.getParam(SystemParams.Keys.CONFIG_COMMON_CS_CODE));
   // license.setCRCLogo("00000000");
   // license.setExpirationDate(calculateCaducation());
   // license.setInstallCode(codeGenerator.generateInstalationNumber());
   // return license;
   // }
}