Roberto Sánchez
2014-01-27 3a4f598d47254e07c62776324e775f39d595ff5f
securis/src/main/java/net/curisit/securis/services/LicenseResource.java
....@@ -27,6 +27,7 @@
2727 import net.curisit.integrity.exception.CurisException;
2828 import net.curisit.securis.DefaultExceptionHandler;
2929 import net.curisit.securis.db.License;
30
+import net.curisit.securis.db.LicenseHistory;
3031 import net.curisit.securis.db.Pack;
3132 import net.curisit.securis.db.User;
3233 import net.curisit.securis.security.BasicSecurityContext;
....@@ -119,6 +120,92 @@
119120 return Response.ok(lic).build();
120121 }
121122
123
+ /**
124
+ *
125
+ * @return The license file, only of license is active
126
+ */
127
+ @GET
128
+ @Path("/{licId}/download")
129
+ @Securable
130
+ @Produces(
131
+ { MediaType.APPLICATION_OCTET_STREAM })
132
+ public Response download(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) {
133
+ log.info("Getting license data for id: {}: ", licId);
134
+ if (licId == null || licId.equals("")) {
135
+ log.error("License ID is mandatory");
136
+ return Response.status(Status.NOT_FOUND).build();
137
+ }
138
+
139
+ EntityManager em = emProvider.get();
140
+ License lic = em.find(License.class, licId);
141
+ if (lic == null) {
142
+ log.error("License with id {} not found in DB", licId);
143
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License not found for ID: " + licId).build();
144
+ }
145
+ if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
146
+ if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) {
147
+ log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal());
148
+ return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to license data").build();
149
+ }
150
+ }
151
+ if (lic.getLicenseData() == null) {
152
+ log.error("License with id {} has not license file generated", licId, bsc.getUserPrincipal());
153
+ return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License has not contain data to generate license file").build();
154
+ }
155
+ if (lic.getStatus() != License.Status.ACTIVE) {
156
+ log.error("License with id {} is not active, so It can not downloaded", licId, bsc.getUserPrincipal());
157
+ return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License is not active, so It can not be downloaded").build();
158
+ }
159
+ return Response.ok(lic.getLicenseData()).build();
160
+ }
161
+
162
+ @PUT
163
+ @POST
164
+ @Path("/{licId}/activate")
165
+ @Securable
166
+ @Transactional
167
+ @Consumes(MediaType.APPLICATION_JSON)
168
+ @Produces(
169
+ { MediaType.APPLICATION_JSON })
170
+ public Response activate(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) {
171
+ log.info("Getting license data for id: {}: ", licId);
172
+ if (licId == null || licId.equals("")) {
173
+ log.error("License ID is mandatory");
174
+ return Response.status(Status.NOT_FOUND).build();
175
+ }
176
+
177
+ EntityManager em = emProvider.get();
178
+ License lic = em.find(License.class, licId);
179
+ if (lic == null) {
180
+ log.error("License with id {} not found in DB", licId);
181
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License not found for ID: " + licId).build();
182
+ }
183
+ if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
184
+ if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) {
185
+ log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal());
186
+ return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to license data").build();
187
+ }
188
+ }
189
+
190
+ User user = null;
191
+ try {
192
+ user = getUser(bsc.getUserPrincipal().getName(), em);
193
+ } catch (CurisException ex) {
194
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Current user not found in DB: " + bsc.getUserPrincipal()).build();
195
+ }
196
+
197
+ lic.setStatus(License.Status.ACTIVE);
198
+ lic.setModificationTimestamp(new Date());
199
+ em.persist(lic);
200
+ LicenseHistory lh = new LicenseHistory();
201
+ lh.setLicense(lic);
202
+ lh.setUser(user);
203
+ lh.setTimestamp(new Date());
204
+ lh.setAction(LicenseHistory.Actions.ACTIVATE);
205
+ em.persist(lh);
206
+ return Response.ok(lic).build();
207
+ }
208
+
122209 @POST
123210 @Path("/")
124211 @Consumes(MediaType.APPLICATION_JSON)
....@@ -145,9 +232,9 @@
145232 }
146233 }
147234
235
+ User createdBy = null;
148236 try {
149
- User createdBy = getUser(bsc.getUserPrincipal().getName(), em);
150
- lic.setCreatedBy(createdBy);
237
+ createdBy = getUser(bsc.getUserPrincipal().getName(), em);
151238 } catch (CurisException ex) {
152239 String createdByUsername = lic.getCreatedById();
153240 log.error("License created by user with id {} not found in DB", createdByUsername);
....@@ -155,11 +242,17 @@
155242 }
156243
157244 // ODO: Manage status if request data is set
158
- lic.setCanceledBy(null);
245
+ lic.setCreatedBy(createdBy);
159246 lic.setStatus(License.Status.CREATED);
160247 lic.setCreationTimestamp(new Date());
161248 lic.setModificationTimestamp(lic.getCreationTimestamp());
162249 em.persist(lic);
250
+ LicenseHistory lh = new LicenseHistory();
251
+ lh.setLicense(lic);
252
+ lh.setUser(createdBy);
253
+ lh.setTimestamp(new Date());
254
+ lh.setAction(LicenseHistory.Actions.CREATE);
255
+ em.persist(lh);
163256
164257 return Response.ok(lic).build();
165258 }