Roberto Sánchez
2014-01-27 c3d9abdd3cd55a12d6509ade288648c2408baeb5
securis/src/main/java/net/curisit/securis/services/LicenseResource.java
....@@ -22,9 +22,7 @@
2222 import javax.ws.rs.core.Response;
2323 import javax.ws.rs.core.Response.Status;
2424
25
-import net.curisit.integrity.commons.JsonUtils;
2625 import net.curisit.integrity.commons.Utils;
27
-import net.curisit.integrity.exception.CurisException;
2826 import net.curisit.securis.DefaultExceptionHandler;
2927 import net.curisit.securis.db.License;
3028 import net.curisit.securis.db.LicenseHistory;
....@@ -32,6 +30,7 @@
3230 import net.curisit.securis.db.User;
3331 import net.curisit.securis.security.BasicSecurityContext;
3432 import net.curisit.securis.security.Securable;
33
+import net.curisit.securis.services.exception.SeCurisServiceException;
3534 import net.curisit.securis.utils.TokenHelper;
3635
3736 import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
....@@ -92,69 +91,43 @@
9291 /**
9392 *
9493 * @return the server version in format majorVersion.minorVersion
94
+ * @throws SeCurisServiceException
9595 */
9696 @GET
9797 @Path("/{licId}")
9898 @Securable
9999 @Produces(
100100 { MediaType.APPLICATION_JSON })
101
- public Response get(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) {
101
+ public Response get(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
102102 log.info("Getting organization data for id: {}: ", licId);
103
- if (licId == null || licId.equals("")) {
104
- log.error("License ID is mandatory");
105
- return Response.status(Status.NOT_FOUND).build();
106
- }
107103
108104 EntityManager em = emProvider.get();
109
- License lic = em.find(License.class, licId);
110
- if (lic == null) {
111
- log.error("License with id {} not found in DB", licId);
112
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License not found for ID: " + licId).build();
113
- }
114
- if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
115
- if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) {
116
- log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal());
117
- return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to license data").build();
118
- }
119
- }
105
+ License lic = getCurrentLicense(licId, bsc, em);
120106 return Response.ok(lic).build();
121107 }
122108
123109 /**
124110 *
125111 * @return The license file, only of license is active
112
+ * @throws SeCurisServiceException
126113 */
127114 @GET
128115 @Path("/{licId}/download")
129116 @Securable
130117 @Produces(
131118 { 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
- }
119
+ public Response download(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
138120
139121 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
- }
122
+ License lic = getCurrentLicense(licId, bsc, em);
123
+
151124 if (lic.getLicenseData() == null) {
152125 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();
126
+ throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License has not contain data to generate license file");
154127 }
155128 if (lic.getStatus() != License.Status.ACTIVE) {
156129 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();
130
+ throw new SeCurisServiceException(Status.FORBIDDEN.getStatusCode(), "License is not active, so It can not be downloaded");
158131 }
159132 return Response.ok(lic.getLicenseData()).build();
160133 }
....@@ -167,42 +140,60 @@
167140 @Consumes(MediaType.APPLICATION_JSON)
168141 @Produces(
169142 { 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
- }
143
+ public Response activate(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
176144
177145 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
- }
146
+ License lic = getCurrentLicense(licId, bsc, em);
189147
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
- }
148
+ User user = getUser(bsc.getUserPrincipal().getName(), em);
196149
197150 lic.setStatus(License.Status.ACTIVE);
198151 lic.setModificationTimestamp(new Date());
199152 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);
153
+ em.persist(createLicenseHistoryAction(lic, user, LicenseHistory.Actions.ACTIVATE));
154
+ return Response.ok(lic).build();
155
+ }
156
+
157
+ @PUT
158
+ @POST
159
+ @Path("/{licId}/send")
160
+ @Securable
161
+ @Transactional
162
+ @Consumes(MediaType.APPLICATION_JSON)
163
+ @Produces(
164
+ { MediaType.APPLICATION_JSON })
165
+ public Response send(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
166
+
167
+ EntityManager em = emProvider.get();
168
+ License lic = getCurrentLicense(licId, bsc, em);
169
+
170
+ User user = getUser(bsc.getUserPrincipal().getName(), em);
171
+ // TODO: Send mail with lic file
172
+ lic.setModificationTimestamp(new Date());
173
+ em.persist(lic);
174
+ em.persist(createLicenseHistoryAction(lic, user, LicenseHistory.Actions.SEND, "Email sent to: " + lic.getEmail()));
175
+ return Response.ok(lic).build();
176
+ }
177
+
178
+ @PUT
179
+ @POST
180
+ @Path("/{licId}/cancel")
181
+ @Securable
182
+ @Transactional
183
+ @Consumes(MediaType.APPLICATION_JSON)
184
+ @Produces(
185
+ { MediaType.APPLICATION_JSON })
186
+ public Response cancel(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
187
+
188
+ EntityManager em = emProvider.get();
189
+ License lic = getCurrentLicense(licId, bsc, em);
190
+
191
+ User user = getUser(bsc.getUserPrincipal().getName(), em);
192
+
193
+ lic.setStatus(License.Status.CANCELED);
194
+ lic.setModificationTimestamp(new Date());
195
+ em.persist(lic);
196
+ em.persist(createLicenseHistoryAction(lic, user, LicenseHistory.Actions.CANCEL));
206197 return Response.ok(lic).build();
207198 }
208199
....@@ -213,7 +204,7 @@
213204 @Produces(
214205 { MediaType.APPLICATION_JSON })
215206 @Transactional
216
- public Response create(License lic, @Context BasicSecurityContext bsc) {
207
+ public Response create(License lic, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
217208 log.info("Creating new license from create()");
218209 EntityManager em = emProvider.get();
219210 Pack pack = null;
....@@ -232,14 +223,7 @@
232223 }
233224 }
234225
235
- User createdBy = null;
236
- try {
237
- createdBy = getUser(bsc.getUserPrincipal().getName(), em);
238
- } catch (CurisException ex) {
239
- String createdByUsername = lic.getCreatedById();
240
- log.error("License created by user with id {} not found in DB", createdByUsername);
241
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License's created by user not found with ID: " + createdByUsername).build();
242
- }
226
+ User createdBy = getUser(bsc.getUserPrincipal().getName(), em);
243227
244228 // ODO: Manage status if request data is set
245229 lic.setCreatedBy(createdBy);
....@@ -247,12 +231,7 @@
247231 lic.setCreationTimestamp(new Date());
248232 lic.setModificationTimestamp(lic.getCreationTimestamp());
249233 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);
234
+ em.persist(createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE));
256235
257236 return Response.ok(lic).build();
258237 }
....@@ -264,7 +243,7 @@
264243 @Produces(
265244 { MediaType.APPLICATION_JSON })
266245 @Transactional
267
- public Response createWithFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException {
246
+ public Response createWithFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException {
268247 License lic = new License();
269248 lic.setCode(mpfdi.getFormDataPart("code", String.class, null));
270249 lic.setRequestData(mpfdi.getFormDataPart("request_data", String.class, null));
....@@ -272,25 +251,8 @@
272251 lic.setFullName(mpfdi.getFormDataPart("full_name", String.class, null));
273252 lic.setEmail(mpfdi.getFormDataPart("email", String.class, null));
274253 lic.setComments(mpfdi.getFormDataPart("comments", String.class, null));
275
- try {
276
- log.info("File content: {}", lic.getRequestData());
277
- log.info("License read from multipart: {}", JsonUtils.toJSON(lic));
278
- } catch (CurisException e) {
279
- // TODO Auto-generated catch block
280
- e.printStackTrace();
281
- }
282
- return create(lic, bsc);
283
- }
284254
285
- private User getUser(String username, EntityManager em) throws CurisException {
286
- User user = null;
287
- if (username != null) {
288
- user = em.find(User.class, username);
289
- if (user == null) {
290
- throw new CurisException("User not found");
291
- }
292
- }
293
- return user;
255
+ return create(lic, bsc);
294256 }
295257
296258 @PUT
....@@ -301,57 +263,13 @@
301263 @Consumes(MediaType.APPLICATION_JSON)
302264 @Produces(
303265 { MediaType.APPLICATION_JSON })
304
- public Response modify(License lic, @PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) {
266
+ public Response modify(License lic, @PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
305267 log.info("Modifying organization with id: {}", licId);
268
+
306269 EntityManager em = emProvider.get();
307270
308
- // Pack pack = null;
309
- // if (lic.getPackId() != null) {
310
- // pack = em.find(Pack.class, lic.getPackId());
311
- // if (pack == null) {
312
- // log.error("License pack with id {} not found in DB", lic.getPackId());
313
- // return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License's pack not found with ID: " + lic.getPackId()).build();
314
- // } else {
315
- // if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
316
- // if (!bsc.getOrganizationsIds().contains(pack.getOrganization().getId())) {
317
- // log.error("License for pack with id {} can not be modified by user {}", pack.getId(), bsc.getUserPrincipal());
318
- // return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized action on pack license").build();
319
- // }
320
- // }
321
- // }
322
- // }
323
- User createdBy = null;
324
- try {
325
- createdBy = getUser(lic.getCreatedById(), em);
326
- } catch (CurisException ex) {
327
- String createdByUsername = lic.getCreatedById();
328
- log.error("License created by user with id {} not found in DB", createdByUsername);
329
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License's created by user not found with ID: " + createdByUsername).build();
330
- }
271
+ License currentLicense = getCurrentLicense(licId, bsc, em);
331272
332
- User canceledBy = null;
333
- try {
334
- canceledBy = getUser(lic.getCanceledById(), em);
335
- } catch (CurisException ex) {
336
- String canceledByUsername = lic.getCreatedById();
337
- log.error("License canceled by user with id {} not found in DB", canceledByUsername);
338
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License's canceled by user not found with ID: " + canceledByUsername).build();
339
- }
340
- License currentLicense = em.find(License.class, lic.getId());
341
- if (currentLicense == null) {
342
- log.error("License with id {} not found in DB", licId);
343
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License not found for ID: " + licId).build();
344
- }
345
- if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
346
- if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) {
347
- log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal());
348
- return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to license data").build();
349
- }
350
- }
351
- // TODO: set status based in current one and dates ? use custom actions ?
352
- currentLicense.setCreatedBy(createdBy);
353
- currentLicense.setCanceledBy(canceledBy);
354
- // currentLicense.setPack(pack);
355273 currentLicense.setCode(lic.getCode());
356274 currentLicense.setFullName(lic.getFullName());
357275 currentLicense.setEmail(lic.getEmail());
....@@ -368,21 +286,10 @@
368286 @Securable
369287 @Produces(
370288 { MediaType.APPLICATION_JSON })
371
- public Response delete(@PathParam("licId") String licId, @Context BasicSecurityContext bsc) {
289
+ public Response delete(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException {
372290 log.info("Deleting license with id: {}", licId);
373291 EntityManager em = emProvider.get();
374
- License lic = em.find(License.class, Integer.parseInt(licId));
375
- if (lic == null) {
376
- log.error("License with id {} can not be deleted, It was not found in DB", licId);
377
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License was not found, ID: " + licId).build();
378
- }
379
-
380
- if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
381
- if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) {
382
- log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal());
383
- return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to license data").build();
384
- }
385
- }
292
+ License lic = getCurrentLicense(licId, bsc, em);
386293
387294 if (lic.getStatus() != License.Status.CANCELED || lic.getStatus() != License.Status.CREATED) {
388295 log.error("License {} can not be deleted with status {}", lic.getCode(), lic.getStatus());
....@@ -393,4 +300,48 @@
393300 return Response.ok(Utils.createMap("success", true, "id", licId)).build();
394301 }
395302
303
+ private License getCurrentLicense(Integer licId, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
304
+ if (licId == null || licId.equals("")) {
305
+ log.error("License ID is mandatory");
306
+ throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "Missing license ID");
307
+ }
308
+
309
+ License lic = em.find(License.class, licId);
310
+ if (lic == null) {
311
+ log.error("License with id {} not found in DB", licId);
312
+ throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "License not found for ID: " + licId);
313
+ }
314
+ if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
315
+ if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) {
316
+ log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal());
317
+ throw new SeCurisServiceException(Status.UNAUTHORIZED.getStatusCode(), "Unathorized access to license data");
318
+ }
319
+ }
320
+ return lic;
321
+ }
322
+
323
+ private User getUser(String username, EntityManager em) throws SeCurisServiceException {
324
+ User user = null;
325
+ if (username != null) {
326
+ user = em.find(User.class, username);
327
+ if (user == null) {
328
+ throw new SeCurisServiceException(Status.NOT_FOUND.getStatusCode(), "User not found with username: " + username);
329
+ }
330
+ }
331
+ return user;
332
+ }
333
+
334
+ private LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) {
335
+ LicenseHistory lh = new LicenseHistory();
336
+ lh.setLicense(lic);
337
+ lh.setUser(user);
338
+ lh.setTimestamp(new Date());
339
+ lh.setAction(action);
340
+ lh.setComments(comments);
341
+ return lh;
342
+ }
343
+
344
+ private LicenseHistory createLicenseHistoryAction(License lic, User user, String action) {
345
+ return createLicenseHistoryAction(lic, user, action, null);
346
+ }
396347 }