| .. | .. |
|---|
| 1 | +/* |
|---|
| 2 | + * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved. |
|---|
| 3 | + */ |
|---|
| 1 | 4 | package net.curisit.securis.services; |
|---|
| 2 | 5 | |
|---|
| 3 | 6 | import java.util.Date; |
|---|
| .. | .. |
|---|
| 44 | 47 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 45 | 48 | |
|---|
| 46 | 49 | /** |
|---|
| 47 | | - * LicenseType resource, this service will provide methods to create, modify and |
|---|
| 48 | | - * delete license types |
|---|
| 49 | | - * |
|---|
| 50 | | - * @author roberto <roberto.sanchez@curisit.net> |
|---|
| 50 | + * LicenseTypeResource |
|---|
| 51 | + * <p> |
|---|
| 52 | + * CRUD for license types. Non-admin queries are scoped to the applications |
|---|
| 53 | + * accessible by the caller. Metadata changes are reconciled and, when keys |
|---|
| 54 | + * change, can be propagated to dependent entities via {@link MetadataHelper}. |
|---|
| 55 | + * |
|---|
| 56 | + * @author JRA |
|---|
| 57 | + * Last reviewed by JRA on Oct 5, 2025. |
|---|
| 51 | 58 | */ |
|---|
| 52 | 59 | @Path("/licensetype") |
|---|
| 53 | 60 | public class LicenseTypeResource { |
|---|
| 54 | 61 | |
|---|
| 55 | 62 | private static final Logger LOG = LogManager.getLogger(LicenseTypeResource.class); |
|---|
| 56 | 63 | |
|---|
| 57 | | - @Inject |
|---|
| 58 | | - TokenHelper tokenHelper; |
|---|
| 64 | + @Inject TokenHelper tokenHelper; |
|---|
| 65 | + @Inject MetadataHelper metadataHelper; |
|---|
| 59 | 66 | |
|---|
| 60 | | - @Inject |
|---|
| 61 | | - MetadataHelper metadataHelper; |
|---|
| 67 | + @Context EntityManager em; |
|---|
| 62 | 68 | |
|---|
| 63 | | - @Context |
|---|
| 64 | | - EntityManager em; |
|---|
| 65 | | - |
|---|
| 66 | | - public LicenseTypeResource() { |
|---|
| 67 | | - } |
|---|
| 69 | + public LicenseTypeResource() { } |
|---|
| 68 | 70 | |
|---|
| 69 | 71 | /** |
|---|
| 70 | | - * |
|---|
| 71 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 72 | + * index |
|---|
| 73 | + * <p> |
|---|
| 74 | + * List license types. Non-admin users get only types for their allowed apps. |
|---|
| 75 | + * |
|---|
| 76 | + * @param bsc security context. |
|---|
| 77 | + * @return 200 OK with list (possibly empty). |
|---|
| 72 | 78 | */ |
|---|
| 73 | 79 | @GET |
|---|
| 74 | 80 | @Path("/") |
|---|
| .. | .. |
|---|
| 76 | 82 | @Securable |
|---|
| 77 | 83 | public Response index(@Context BasicSecurityContext bsc) { |
|---|
| 78 | 84 | LOG.info("Getting license types list "); |
|---|
| 79 | | - |
|---|
| 80 | | - // EntityManager em = emProvider.get(); |
|---|
| 81 | 85 | em.clear(); |
|---|
| 82 | 86 | TypedQuery<LicenseType> q; |
|---|
| 83 | 87 | if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| .. | .. |
|---|
| 87 | 91 | return Response.ok().build(); |
|---|
| 88 | 92 | } |
|---|
| 89 | 93 | q = em.createNamedQuery("list-license_types-by_apps-id", LicenseType.class); |
|---|
| 90 | | - |
|---|
| 91 | 94 | q.setParameter("list_ids", bsc.getApplicationsIds()); |
|---|
| 92 | 95 | } |
|---|
| 93 | 96 | List<LicenseType> list = q.getResultList(); |
|---|
| 94 | | - |
|---|
| 95 | 97 | return Response.ok(list).build(); |
|---|
| 96 | 98 | } |
|---|
| 97 | 99 | |
|---|
| 98 | 100 | /** |
|---|
| 99 | | - * |
|---|
| 100 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 101 | | - * @throws SeCurisServiceException |
|---|
| 101 | + * get |
|---|
| 102 | + * <p> |
|---|
| 103 | + * Fetch a license type by id. |
|---|
| 104 | + * |
|---|
| 105 | + * @param ltid LicenseType id (string form). |
|---|
| 106 | + * @param token (unused) header token. |
|---|
| 107 | + * @return 200 OK with the entity. |
|---|
| 108 | + * @throws SeCurisServiceException 404 if not found. |
|---|
| 102 | 109 | */ |
|---|
| 103 | 110 | @GET |
|---|
| 104 | 111 | @Path("/{ltid}") |
|---|
| .. | .. |
|---|
| 111 | 118 | return Response.status(Status.NOT_FOUND).build(); |
|---|
| 112 | 119 | } |
|---|
| 113 | 120 | |
|---|
| 114 | | - // EntityManager em = emProvider.get(); |
|---|
| 115 | 121 | em.clear(); |
|---|
| 116 | 122 | LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 117 | 123 | if (lt == null) { |
|---|
| .. | .. |
|---|
| 121 | 127 | return Response.ok(lt).build(); |
|---|
| 122 | 128 | } |
|---|
| 123 | 129 | |
|---|
| 130 | + /** |
|---|
| 131 | + * create |
|---|
| 132 | + * <p> |
|---|
| 133 | + * Create a new license type. Requires ADMIN. Sets application reference, |
|---|
| 134 | + * persists metadata entries and stamps creation time. |
|---|
| 135 | + * |
|---|
| 136 | + * @param lt Payload. |
|---|
| 137 | + * @param token (unused) token header. |
|---|
| 138 | + * @return 200 OK with created entity, or 404 if app missing. |
|---|
| 139 | + */ |
|---|
| 124 | 140 | @POST |
|---|
| 125 | 141 | @Path("/") |
|---|
| 126 | 142 | @Consumes(MediaType.APPLICATION_JSON) |
|---|
| .. | .. |
|---|
| 130 | 146 | @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 131 | 147 | public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 132 | 148 | LOG.info("Creating new license type"); |
|---|
| 133 | | - // EntityManager em = emProvider.get(); |
|---|
| 134 | 149 | |
|---|
| 135 | 150 | try { |
|---|
| 136 | 151 | setApplication(lt, lt.getApplicationId(), em); |
|---|
| .. | .. |
|---|
| 158 | 173 | return Response.ok(lt).build(); |
|---|
| 159 | 174 | } |
|---|
| 160 | 175 | |
|---|
| 161 | | - private Set<String> getMdKeys(Set<LicenseTypeMetadata> mds) { |
|---|
| 162 | | - Set<String> ids = new HashSet<String>(); |
|---|
| 163 | | - if (mds != null) { |
|---|
| 164 | | - for (LicenseTypeMetadata md : mds) { |
|---|
| 165 | | - ids.add(md.getKey()); |
|---|
| 166 | | - } |
|---|
| 167 | | - } |
|---|
| 168 | | - return ids; |
|---|
| 169 | | - } |
|---|
| 170 | | - |
|---|
| 176 | + /** |
|---|
| 177 | + * modify |
|---|
| 178 | + * <p> |
|---|
| 179 | + * Update an existing license type. Reconciles metadata: |
|---|
| 180 | + * removes keys not present in the new set; merges existing; persists new ones. |
|---|
| 181 | + * If keys changed, {@link MetadataHelper#propagateMetadata} is invoked. |
|---|
| 182 | + * |
|---|
| 183 | + * @param lt New values. |
|---|
| 184 | + * @param ltid LicenseType id. |
|---|
| 185 | + * @param token (unused) token. |
|---|
| 186 | + * @return 200 OK with updated entity; 404 if not found or app missing. |
|---|
| 187 | + */ |
|---|
| 171 | 188 | @PUT |
|---|
| 172 | 189 | @POST |
|---|
| 173 | 190 | @Path("/{ltid}") |
|---|
| .. | .. |
|---|
| 178 | 195 | @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 179 | 196 | public Response modify(LicenseType lt, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 180 | 197 | LOG.info("Modifying license type with id: {}", ltid); |
|---|
| 181 | | - // EntityManager em = emProvider.get(); |
|---|
| 182 | 198 | LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 183 | 199 | if (currentlt == null) { |
|---|
| 184 | 200 | LOG.error("LicenseType with id {} not found in DB", ltid); |
|---|
| .. | .. |
|---|
| 230 | 246 | return Response.ok(currentlt).build(); |
|---|
| 231 | 247 | } |
|---|
| 232 | 248 | |
|---|
| 233 | | - private void setApplication(LicenseType licType, Integer applicationId, EntityManager em) throws SeCurisException { |
|---|
| 234 | | - Application app = null; |
|---|
| 235 | | - if (applicationId != null) { |
|---|
| 236 | | - app = em.find(Application.class, applicationId); |
|---|
| 237 | | - if (app == null) { |
|---|
| 238 | | - LOG.error("LicenseType application with id {} not found in DB", applicationId); |
|---|
| 239 | | - |
|---|
| 240 | | - throw new SecurityException("License type's app not found with ID: " + applicationId); |
|---|
| 241 | | - } |
|---|
| 242 | | - } |
|---|
| 243 | | - licType.setApplication(app); |
|---|
| 244 | | - } |
|---|
| 245 | | - |
|---|
| 249 | + /** |
|---|
| 250 | + * delete |
|---|
| 251 | + * <p> |
|---|
| 252 | + * Delete a license type by id. Requires ADMIN. |
|---|
| 253 | + * |
|---|
| 254 | + * @param ltid LicenseType id. |
|---|
| 255 | + * @param req request (unused). |
|---|
| 256 | + * @return 200 OK on success; 404 if not found. |
|---|
| 257 | + */ |
|---|
| 246 | 258 | @DELETE |
|---|
| 247 | 259 | @Path("/{ltid}") |
|---|
| 248 | 260 | @EnsureTransaction |
|---|
| 249 | 261 | @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 250 | 262 | @Securable(roles = Rol.ADMIN) |
|---|
| 251 | 263 | @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 252 | | - public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) { |
|---|
| 264 | + public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest req) { |
|---|
| 253 | 265 | LOG.info("Deleting app with id: {}", ltid); |
|---|
| 254 | | - // EntityManager em = emProvider.get(); |
|---|
| 255 | 266 | LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 256 | 267 | if (app == null) { |
|---|
| 257 | 268 | LOG.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid); |
|---|
| .. | .. |
|---|
| 262 | 273 | return Response.ok(Utils.createMap("success", true, "id", ltid)).build(); |
|---|
| 263 | 274 | } |
|---|
| 264 | 275 | |
|---|
| 276 | + // --------------------------------------------------------------------- |
|---|
| 277 | + // Helpers |
|---|
| 278 | + // --------------------------------------------------------------------- |
|---|
| 279 | + |
|---|
| 280 | + private Set<String> getMdKeys(Set<LicenseTypeMetadata> mds) { |
|---|
| 281 | + Set<String> ids = new HashSet<String>(); |
|---|
| 282 | + if (mds != null) { |
|---|
| 283 | + for (LicenseTypeMetadata md : mds) { |
|---|
| 284 | + ids.add(md.getKey()); |
|---|
| 285 | + } |
|---|
| 286 | + } |
|---|
| 287 | + return ids; |
|---|
| 288 | + } |
|---|
| 289 | + |
|---|
| 290 | + /** |
|---|
| 291 | + * setApplication<p> |
|---|
| 292 | + * Resolve and set the application of a license type. |
|---|
| 293 | + * |
|---|
| 294 | + * @param licType target LicenseType. |
|---|
| 295 | + * @param applicationId id of the application (nullable). |
|---|
| 296 | + * @param em entity manager. |
|---|
| 297 | + * @throws SeCurisException if id provided but not found. |
|---|
| 298 | + */ |
|---|
| 299 | + private void setApplication(LicenseType licType, Integer applicationId, EntityManager em) throws SeCurisException { |
|---|
| 300 | + Application app = null; |
|---|
| 301 | + if (applicationId != null) { |
|---|
| 302 | + app = em.find(Application.class, applicationId); |
|---|
| 303 | + if (app == null) { |
|---|
| 304 | + LOG.error("LicenseType application with id {} not found in DB", applicationId); |
|---|
| 305 | + throw new SecurityException("License type's app not found with ID: " + applicationId); |
|---|
| 306 | + } |
|---|
| 307 | + } |
|---|
| 308 | + licType.setApplication(app); |
|---|
| 309 | + } |
|---|
| 265 | 310 | } |
|---|
| 311 | + |
|---|