Roberto Sánchez
2014-09-18 52ce72b22ef8d92a1f35b4993bcddaaa66d67350
securis/src/main/java/net/curisit/securis/services/PackResource.java
....@@ -5,6 +5,7 @@
55 import java.util.List;
66
77 import javax.annotation.security.RolesAllowed;
8
+import javax.crypto.SealedObject;
89 import javax.inject.Inject;
910 import javax.inject.Provider;
1011 import javax.persistence.EntityManager;
....@@ -24,6 +25,7 @@
2425
2526 import net.curisit.integrity.commons.Utils;
2627 import net.curisit.securis.DefaultExceptionHandler;
28
+import net.curisit.securis.SeCurisException;
2729 import net.curisit.securis.db.LicenseType;
2830 import net.curisit.securis.db.Organization;
2931 import net.curisit.securis.db.Pack;
....@@ -45,7 +47,7 @@
4547 @Path("/pack")
4648 public class PackResource {
4749
48
- private static final Logger log = LogManager.getLogger(PackResource.class);
50
+ private static final Logger LOG = LogManager.getLogger(PackResource.class);
4951
5052 @Inject
5153 TokenHelper tokenHelper;
....@@ -66,14 +68,14 @@
6668 @Produces(
6769 { MediaType.APPLICATION_JSON })
6870 public Response index(@Context BasicSecurityContext bsc) {
69
- log.info("Getting packs list ");
71
+ LOG.info("Getting packs list ");
7072
7173 EntityManager em = emProvider.get();
7274 // TypedQuery<Pack> q = em.createNamedQuery("list-packs-by-orgs", Pack.class);
7375
7476 TypedQuery<Pack> q;
7577 if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
76
- log.info("Getting all packs for user: " + bsc.getUserPrincipal());
78
+ LOG.info("Getting all packs for user: " + bsc.getUserPrincipal());
7779 q = em.createNamedQuery("list-packs", Pack.class);
7880 } else {
7981 q = em.createNamedQuery("list-packs-by-orgs", Pack.class);
....@@ -88,7 +90,7 @@
8890 }
8991
9092 private Response generateErrorUnathorizedAccess(Pack pack, Principal user) {
91
- log.error("Pack with id {} not accesible by user {}", pack, user);
93
+ LOG.error("Pack with id {} not accesible by user {}", pack, user);
9294 return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to pack").build();
9395 }
9496
....@@ -102,16 +104,16 @@
102104 @Produces(
103105 { MediaType.APPLICATION_JSON })
104106 public Response get(@PathParam("packId") Integer packId, @Context BasicSecurityContext bsc) {
105
- log.info("Getting pack data for id: {}: ", packId);
107
+ LOG.info("Getting pack data for id: {}: ", packId);
106108 if (packId == null || packId.equals("")) {
107
- log.error("Pack ID is mandatory");
109
+ LOG.error("Pack ID is mandatory");
108110 return Response.status(Status.NOT_FOUND).build();
109111 }
110112
111113 EntityManager em = emProvider.get();
112114 Pack pack = em.find(Pack.class, packId);
113115 if (pack == null) {
114
- log.error("Pack with id {} not found in DB", packId);
116
+ LOG.error("Pack with id {} not found in DB", packId);
115117 return Response.status(Status.NOT_FOUND).build();
116118 }
117119 if (bsc.isUserInRole(BasicSecurityContext.ROL_ADVANCE)) {
....@@ -131,35 +133,41 @@
131133 { MediaType.APPLICATION_JSON })
132134 @Transactional
133135 public Response create(Pack pack, @Context BasicSecurityContext bsc) {
134
- log.info("Creating new pack");
136
+ LOG.info("Creating new pack");
135137 EntityManager em = emProvider.get();
136138
137
- Organization org = null;
138
- if (pack.getOrgId() != null) {
139
- org = em.find(Organization.class, pack.getOrgId());
140
- if (org == null) {
141
- log.error("Organization pack with id {} not found in DB", pack.getOrgId());
142
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack organization not found with ID: " + pack.getOrgId()).build();
143
- }
139
+ try {
140
+ setPackOrganization(pack, pack.getOrgId(), em);
141
+ } catch (SeCurisException e) {
142
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
144143 }
145
- LicenseType lt = null;
146
- if (pack.getLicTypeId() != null) {
147
- lt = em.find(LicenseType.class, pack.getLicTypeId());
148
- if (lt == null) {
149
- log.error("Pack license type with id {} not found in DB", pack.getLicTypeId());
150
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build();
151
- }
144
+
145
+ try {
146
+ setPackLicenseType(pack, pack.getLicTypeId(), em);
147
+ } catch (SeCurisException e) {
148
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
152149 }
153150
154151 User user = em.find(User.class, bsc.getUserPrincipal().getName());
155152
156153 pack.setCreatedBy(user);
157
- pack.setLicenseType(lt);
158
- pack.setOrganization(org);
159154 pack.setCreationTimestamp(new Date());
160155 em.persist(pack);
161156
162157 return Response.ok(pack).build();
158
+ }
159
+
160
+ private void setPackLicenseType(Pack pack, Integer licTypeId, EntityManager em) throws SeCurisException {
161
+ LicenseType lt = null;
162
+ if (licTypeId != null) {
163
+ lt = em.find(LicenseType.class, pack.getLicTypeId());
164
+ if (lt == null) {
165
+ LOG.error("Pack license type with id {} not found in DB", licTypeId);
166
+ throw new SeCurisException("Pack license type not found with ID: " + licTypeId);
167
+// return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build();
168
+ }
169
+ }
170
+ pack.setLicenseType(lt);
163171 }
164172
165173 @PUT
....@@ -172,29 +180,23 @@
172180 @Produces(
173181 { MediaType.APPLICATION_JSON })
174182 public Response modify(Pack pack, @PathParam("packId") Integer packId) {
175
- log.info("Modifying pack with id: {}", packId);
183
+ LOG.info("Modifying pack with id: {}", packId);
176184 EntityManager em = emProvider.get();
177185 Pack currentPack = em.find(Pack.class, packId);
178186
179
- Organization org = null;
180
- if (pack.getOrgId() != null) {
181
- org = em.find(Organization.class, pack.getOrgId());
182
- if (org == null) {
183
- log.error("Organization pack with id {} not found in DB", pack.getOrgId());
184
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack organization not found with ID: " + pack.getOrgId()).build();
185
- }
187
+ try {
188
+ setPackOrganization(currentPack, pack.getOrgId(), em);
189
+ } catch (SeCurisException e) {
190
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
186191 }
187
- LicenseType lt = null;
188
- if (pack.getLicTypeId() != null) {
189
- lt = em.find(LicenseType.class, pack.getLicTypeId());
190
- if (lt == null) {
191
- log.error("Pack license type with id {} not found in DB", pack.getLicTypeId());
192
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack license type not found with ID: " + pack.getLicTypeId()).build();
193
- }
192
+
193
+ try {
194
+ setPackLicenseType(currentPack, pack.getLicTypeId(), em);
195
+ } catch (SeCurisException e) {
196
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
194197 }
198
+
195199 currentPack.setLicensePreactivation(pack.isLicensePreactivation());
196
- currentPack.setLicenseType(lt);
197
- currentPack.setOrganization(org);
198200 currentPack.setCode(pack.getCode());
199201 currentPack.setComments(pack.getComments());
200202 currentPack.setNumLicenses(pack.getNumLicenses());
....@@ -202,6 +204,18 @@
202204 em.persist(currentPack);
203205
204206 return Response.ok(pack).build();
207
+ }
208
+
209
+ private void setPackOrganization(Pack currentPack, Integer orgId, EntityManager em) throws SeCurisException {
210
+ Organization org = null;
211
+ if (orgId != null) {
212
+ org = em.find(Organization.class, orgId);
213
+ if (org == null) {
214
+ LOG.error("Organization pack with id {} not found in DB", orgId);
215
+ throw new SeCurisException("Pack organization not found with ID: " + orgId);
216
+ }
217
+ }
218
+ currentPack.setOrganization(org);
205219 }
206220
207221 @DELETE
....@@ -212,11 +226,11 @@
212226 @Produces(
213227 { MediaType.APPLICATION_JSON })
214228 public Response delete(@PathParam("packId") String packId) {
215
- log.info("Deleting pack with id: {}", packId);
229
+ LOG.info("Deleting pack with id: {}", packId);
216230 EntityManager em = emProvider.get();
217231 Pack org = em.find(Pack.class, Integer.parseInt(packId));
218232 if (org == null) {
219
- log.error("Pack with id {} can not be deleted, It was not found in DB", packId);
233
+ LOG.error("Pack with id {} can not be deleted, It was not found in DB", packId);
220234 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Pack was not found, ID: " + packId).build();
221235 }
222236