| .. | .. |
|---|
| 25 | 25 | import javax.ws.rs.core.Response; |
|---|
| 26 | 26 | import javax.ws.rs.core.Response.Status; |
|---|
| 27 | 27 | |
|---|
| 28 | +import org.apache.logging.log4j.LogManager; |
|---|
| 29 | +import org.apache.logging.log4j.Logger; |
|---|
| 30 | + |
|---|
| 28 | 31 | import net.curisit.integrity.commons.Utils; |
|---|
| 29 | 32 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 30 | 33 | import net.curisit.securis.db.Application; |
|---|
| .. | .. |
|---|
| 34 | 37 | import net.curisit.securis.security.Securable; |
|---|
| 35 | 38 | import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 36 | 39 | import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; |
|---|
| 40 | +import net.curisit.securis.services.helpers.MetadataHelper; |
|---|
| 37 | 41 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 38 | | - |
|---|
| 39 | | -import org.apache.logging.log4j.LogManager; |
|---|
| 40 | | -import org.apache.logging.log4j.Logger; |
|---|
| 41 | 42 | |
|---|
| 42 | 43 | /** |
|---|
| 43 | 44 | * Application resource, this service will provide methods to create, modify and |
|---|
| .. | .. |
|---|
| 48 | 49 | @Path("/application") |
|---|
| 49 | 50 | public class ApplicationResource { |
|---|
| 50 | 51 | |
|---|
| 51 | | - @Inject |
|---|
| 52 | | - TokenHelper tokenHelper; |
|---|
| 52 | + @Inject |
|---|
| 53 | + TokenHelper tokenHelper; |
|---|
| 53 | 54 | |
|---|
| 54 | | - @Context |
|---|
| 55 | | - EntityManager em; |
|---|
| 55 | + @Inject |
|---|
| 56 | + MetadataHelper metadataHelper; |
|---|
| 56 | 57 | |
|---|
| 57 | | - private static final Logger LOG = LogManager.getLogger(ApplicationResource.class); |
|---|
| 58 | + @Context |
|---|
| 59 | + EntityManager em; |
|---|
| 58 | 60 | |
|---|
| 59 | | - public ApplicationResource() { |
|---|
| 60 | | - } |
|---|
| 61 | + private static final Logger LOG = LogManager.getLogger(ApplicationResource.class); |
|---|
| 61 | 62 | |
|---|
| 62 | | - /** |
|---|
| 63 | | - * |
|---|
| 64 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 65 | | - */ |
|---|
| 66 | | - @GET |
|---|
| 67 | | - @Path("/") |
|---|
| 68 | | - @Produces({ |
|---|
| 69 | | - MediaType.APPLICATION_JSON |
|---|
| 70 | | - }) |
|---|
| 71 | | - @Securable |
|---|
| 72 | | - public Response index() { |
|---|
| 73 | | - LOG.info("Getting applications list "); |
|---|
| 63 | + public ApplicationResource() { |
|---|
| 64 | + } |
|---|
| 74 | 65 | |
|---|
| 75 | | - // EntityManager em = emProvider.get(); |
|---|
| 76 | | - em.clear(); |
|---|
| 77 | | - TypedQuery<Application> q = em.createNamedQuery("list-applications", Application.class); |
|---|
| 78 | | - List<Application> list = q.getResultList(); |
|---|
| 66 | + /** |
|---|
| 67 | + * |
|---|
| 68 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 69 | + */ |
|---|
| 70 | + @GET |
|---|
| 71 | + @Path("/") |
|---|
| 72 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 73 | + @Securable |
|---|
| 74 | + public Response index() { |
|---|
| 75 | + LOG.info("Getting applications list "); |
|---|
| 79 | 76 | |
|---|
| 80 | | - return Response.ok(list).build(); |
|---|
| 81 | | - } |
|---|
| 77 | + // EntityManager em = emProvider.get(); |
|---|
| 78 | + em.clear(); |
|---|
| 79 | + TypedQuery<Application> q = em.createNamedQuery("list-applications", Application.class); |
|---|
| 80 | + List<Application> list = q.getResultList(); |
|---|
| 82 | 81 | |
|---|
| 83 | | - /** |
|---|
| 84 | | - * |
|---|
| 85 | | - * @return the server version in format majorVersion.minorVersion |
|---|
| 86 | | - * @throws SeCurisServiceException |
|---|
| 87 | | - */ |
|---|
| 88 | | - @GET |
|---|
| 89 | | - @Path("/{appid}") |
|---|
| 90 | | - @Produces({ |
|---|
| 91 | | - MediaType.APPLICATION_JSON |
|---|
| 92 | | - }) |
|---|
| 93 | | - @Securable |
|---|
| 94 | | - public Response get(@PathParam("appid") String appid) throws SeCurisServiceException { |
|---|
| 95 | | - LOG.info("Getting application data for id: {}: ", appid); |
|---|
| 96 | | - if (appid == null || "".equals(appid)) { |
|---|
| 97 | | - LOG.error("Application ID is mandatory"); |
|---|
| 98 | | - return Response.status(Status.NOT_FOUND).build(); |
|---|
| 99 | | - } |
|---|
| 82 | + return Response.ok(list).build(); |
|---|
| 83 | + } |
|---|
| 100 | 84 | |
|---|
| 101 | | - em.clear(); |
|---|
| 85 | + /** |
|---|
| 86 | + * |
|---|
| 87 | + * @return the server version in format majorVersion.minorVersion |
|---|
| 88 | + * @throws SeCurisServiceException |
|---|
| 89 | + */ |
|---|
| 90 | + @GET |
|---|
| 91 | + @Path("/{appid}") |
|---|
| 92 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 93 | + @Securable |
|---|
| 94 | + public Response get(@PathParam("appid") String appid) throws SeCurisServiceException { |
|---|
| 95 | + LOG.info("Getting application data for id: {}: ", appid); |
|---|
| 96 | + if (appid == null || "".equals(appid)) { |
|---|
| 97 | + LOG.error("Application ID is mandatory"); |
|---|
| 98 | + return Response.status(Status.NOT_FOUND).build(); |
|---|
| 99 | + } |
|---|
| 102 | 100 | |
|---|
| 103 | | - Application app = null; |
|---|
| 104 | | - try { |
|---|
| 105 | | - LOG.info("READY to GET app: {}", appid); |
|---|
| 106 | | - app = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 107 | | - } catch (Exception e) { |
|---|
| 108 | | - LOG.info("ERROR GETTING app: {}", e); |
|---|
| 109 | | - } |
|---|
| 110 | | - if (app == null) { |
|---|
| 111 | | - LOG.error("Application with id {} not found in DB", appid); |
|---|
| 112 | | - throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Application not found with ID: " + appid); |
|---|
| 113 | | - } |
|---|
| 101 | + em.clear(); |
|---|
| 114 | 102 | |
|---|
| 115 | | - return Response.ok(app).build(); |
|---|
| 116 | | - } |
|---|
| 103 | + Application app = null; |
|---|
| 104 | + try { |
|---|
| 105 | + LOG.info("READY to GET app: {}", appid); |
|---|
| 106 | + app = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 107 | + } catch (Exception e) { |
|---|
| 108 | + LOG.info("ERROR GETTING app: {}", e); |
|---|
| 109 | + } |
|---|
| 110 | + if (app == null) { |
|---|
| 111 | + LOG.error("Application with id {} not found in DB", appid); |
|---|
| 112 | + throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Application not found with ID: " + appid); |
|---|
| 113 | + } |
|---|
| 117 | 114 | |
|---|
| 118 | | - @POST |
|---|
| 119 | | - @Path("/") |
|---|
| 120 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 121 | | - @Produces({ |
|---|
| 122 | | - MediaType.APPLICATION_JSON |
|---|
| 123 | | - }) |
|---|
| 124 | | - @EnsureTransaction |
|---|
| 125 | | - @Securable |
|---|
| 126 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 127 | | - public Response create(Application app, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 128 | | - LOG.info("Creating new application"); |
|---|
| 129 | | - // EntityManager em = emProvider.get(); |
|---|
| 130 | | - app.setCreationTimestamp(new Date()); |
|---|
| 131 | | - em.persist(app); |
|---|
| 115 | + return Response.ok(app).build(); |
|---|
| 116 | + } |
|---|
| 132 | 117 | |
|---|
| 133 | | - if (app.getApplicationMetadata() != null) { |
|---|
| 134 | | - for (ApplicationMetadata md : app.getApplicationMetadata()) { |
|---|
| 135 | | - md.setApplication(app); |
|---|
| 136 | | - md.setCreationTimestamp(new Date()); |
|---|
| 137 | | - em.persist(md); |
|---|
| 138 | | - } |
|---|
| 139 | | - } |
|---|
| 140 | | - LOG.info("Creating application ({}) with date: {}", app.getId(), app.getCreationTimestamp()); |
|---|
| 118 | + @POST |
|---|
| 119 | + @Path("/") |
|---|
| 120 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 121 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 122 | + @EnsureTransaction |
|---|
| 123 | + @Securable |
|---|
| 124 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 125 | + public Response create(Application app, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 126 | + LOG.info("Creating new application"); |
|---|
| 127 | + // EntityManager em = emProvider.get(); |
|---|
| 128 | + app.setCreationTimestamp(new Date()); |
|---|
| 129 | + em.persist(app); |
|---|
| 141 | 130 | |
|---|
| 142 | | - return Response.ok(app).build(); |
|---|
| 143 | | - } |
|---|
| 131 | + if (app.getApplicationMetadata() != null) { |
|---|
| 132 | + for (ApplicationMetadata md : app.getApplicationMetadata()) { |
|---|
| 133 | + md.setApplication(app); |
|---|
| 134 | + md.setCreationTimestamp(new Date()); |
|---|
| 135 | + em.persist(md); |
|---|
| 136 | + } |
|---|
| 137 | + } |
|---|
| 138 | + LOG.info("Creating application ({}) with date: {}", app.getId(), app.getCreationTimestamp()); |
|---|
| 144 | 139 | |
|---|
| 145 | | - @PUT |
|---|
| 146 | | - @POST |
|---|
| 147 | | - @Path("/{appid}") |
|---|
| 148 | | - @EnsureTransaction |
|---|
| 149 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 150 | | - @Produces({ |
|---|
| 151 | | - MediaType.APPLICATION_JSON |
|---|
| 152 | | - }) |
|---|
| 153 | | - @Securable |
|---|
| 154 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 155 | | - public Response modify(Application app, @PathParam("appid") String appid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 156 | | - LOG.info("Modifying application with id: {}", appid); |
|---|
| 157 | | - // EntityManager em = emProvider.get(); |
|---|
| 158 | | - Application currentapp = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 159 | | - if (currentapp == null) { |
|---|
| 160 | | - LOG.error("Application with id {} not found in DB", appid); |
|---|
| 161 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid) |
|---|
| 162 | | - .build(); |
|---|
| 163 | | - } |
|---|
| 164 | | - currentapp.setCode(app.getCode()); |
|---|
| 165 | | - currentapp.setName(app.getName()); |
|---|
| 166 | | - currentapp.setLicenseFilename(app.getLicenseFilename()); |
|---|
| 167 | | - currentapp.setDescription(app.getDescription()); |
|---|
| 140 | + return Response.ok(app).build(); |
|---|
| 141 | + } |
|---|
| 168 | 142 | |
|---|
| 169 | | - Set<ApplicationMetadata> newMD = app.getApplicationMetadata(); |
|---|
| 170 | | - Set<ApplicationMetadata> oldMD = currentapp.getApplicationMetadata(); |
|---|
| 171 | | - Map<String, ApplicationMetadata> directOldMD = getMapMD(oldMD); |
|---|
| 172 | | - Map<String, ApplicationMetadata> directNewMD = getMapMD(newMD); |
|---|
| 173 | | - for (ApplicationMetadata currentMd : oldMD) { |
|---|
| 174 | | - if (newMD == null || !directNewMD.containsKey(currentMd.getKey())) { |
|---|
| 175 | | - em.remove(currentMd); |
|---|
| 176 | | - } |
|---|
| 177 | | - } |
|---|
| 143 | + @PUT |
|---|
| 144 | + @POST |
|---|
| 145 | + @Path("/{appid}") |
|---|
| 146 | + @EnsureTransaction |
|---|
| 147 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 148 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 149 | + @Securable |
|---|
| 150 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 151 | + public Response modify(Application app, @PathParam("appid") String appid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 152 | + LOG.info("Modifying application with id: {}", appid); |
|---|
| 153 | + // EntityManager em = emProvider.get(); |
|---|
| 154 | + Application currentapp = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 155 | + if (currentapp == null) { |
|---|
| 156 | + LOG.error("Application with id {} not found in DB", appid); |
|---|
| 157 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid).build(); |
|---|
| 158 | + } |
|---|
| 159 | + currentapp.setCode(app.getCode()); |
|---|
| 160 | + currentapp.setName(app.getName()); |
|---|
| 161 | + currentapp.setLicenseFilename(app.getLicenseFilename()); |
|---|
| 162 | + currentapp.setDescription(app.getDescription()); |
|---|
| 178 | 163 | |
|---|
| 179 | | - if (newMD != null) { |
|---|
| 180 | | - for (ApplicationMetadata md : newMD) { |
|---|
| 181 | | - if (directOldMD.containsKey(md.getKey())) { |
|---|
| 182 | | - ApplicationMetadata amd = directOldMD.get(md.getKey()); |
|---|
| 183 | | - amd.setValue(md.getValue()); |
|---|
| 184 | | - amd.setMandatory(md.isMandatory()); |
|---|
| 185 | | - em.merge(amd); |
|---|
| 186 | | - } else { |
|---|
| 187 | | - md.setApplication(currentapp); |
|---|
| 188 | | - if (md.getCreationTimestamp() == null) { |
|---|
| 189 | | - md.setCreationTimestamp(app.getCreationTimestamp()); |
|---|
| 190 | | - } |
|---|
| 191 | | - em.persist(md); |
|---|
| 192 | | - } |
|---|
| 193 | | - } |
|---|
| 194 | | - } |
|---|
| 195 | | - currentapp.setApplicationMetadata(app.getApplicationMetadata()); |
|---|
| 196 | | - em.merge(currentapp); |
|---|
| 197 | | - return Response.ok(currentapp).build(); |
|---|
| 198 | | - } |
|---|
| 164 | + Set<ApplicationMetadata> newMD = app.getApplicationMetadata(); |
|---|
| 165 | + Set<ApplicationMetadata> oldMD = currentapp.getApplicationMetadata(); |
|---|
| 166 | + boolean metadataChanges = !metadataHelper.match(newMD, oldMD); |
|---|
| 167 | + if (metadataChanges) { |
|---|
| 168 | + Map<String, ApplicationMetadata> directOldMD = getMapMD(oldMD); |
|---|
| 169 | + Map<String, ApplicationMetadata> directNewMD = getMapMD(newMD); |
|---|
| 170 | + for (ApplicationMetadata currentMd : oldMD) { |
|---|
| 171 | + if (newMD == null || !directNewMD.containsKey(currentMd.getKey())) { |
|---|
| 172 | + em.remove(currentMd); |
|---|
| 173 | + } |
|---|
| 174 | + } |
|---|
| 199 | 175 | |
|---|
| 200 | | - private Map<String, ApplicationMetadata> getMapMD(Set<ApplicationMetadata> amd) { |
|---|
| 201 | | - Map<String, ApplicationMetadata> map = new HashMap<String, ApplicationMetadata>(); |
|---|
| 202 | | - if (amd != null) { |
|---|
| 203 | | - for (ApplicationMetadata applicationMetadata : amd) { |
|---|
| 204 | | - map.put(applicationMetadata.getKey(), applicationMetadata); |
|---|
| 205 | | - } |
|---|
| 206 | | - } |
|---|
| 207 | | - return map; |
|---|
| 208 | | - } |
|---|
| 176 | + if (newMD != null) { |
|---|
| 177 | + for (ApplicationMetadata md : newMD) { |
|---|
| 178 | + if (directOldMD.containsKey(md.getKey())) { |
|---|
| 179 | + em.merge(md); |
|---|
| 180 | + } else { |
|---|
| 181 | + md.setApplication(currentapp); |
|---|
| 182 | + if (md.getCreationTimestamp() == null) { |
|---|
| 183 | + md.setCreationTimestamp(app.getCreationTimestamp()); |
|---|
| 184 | + } |
|---|
| 185 | + em.persist(md); |
|---|
| 186 | + } |
|---|
| 187 | + } |
|---|
| 188 | + } |
|---|
| 189 | + currentapp.setApplicationMetadata(app.getApplicationMetadata()); |
|---|
| 190 | + } |
|---|
| 191 | + em.merge(currentapp); |
|---|
| 192 | + if (metadataChanges) { |
|---|
| 193 | + metadataHelper.propagateMetadata(em, currentapp); |
|---|
| 194 | + } |
|---|
| 195 | + return Response.ok(currentapp).build(); |
|---|
| 196 | + } |
|---|
| 209 | 197 | |
|---|
| 210 | | - @DELETE |
|---|
| 211 | | - @Path("/{appid}") |
|---|
| 212 | | - @EnsureTransaction |
|---|
| 213 | | - @Produces({ |
|---|
| 214 | | - MediaType.APPLICATION_JSON |
|---|
| 215 | | - }) |
|---|
| 216 | | - @Securable |
|---|
| 217 | | - @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 218 | | - public Response delete(@PathParam("appid") String appid, @Context HttpServletRequest request) { |
|---|
| 219 | | - LOG.info("Deleting app with id: {}", appid); |
|---|
| 220 | | - // EntityManager em = emProvider.get(); |
|---|
| 221 | | - Application app = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 222 | | - if (app == null) { |
|---|
| 223 | | - LOG.error("Application with id {} can not be deleted, It was not found in DB", appid); |
|---|
| 224 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid) |
|---|
| 225 | | - .build(); |
|---|
| 226 | | - } |
|---|
| 227 | | - /* |
|---|
| 228 | | - * if (app.getLicenseTypes() != null && |
|---|
| 229 | | - * !app.getLicenseTypes().isEmpty()) { throw new |
|---|
| 230 | | - * SeCurisServiceException(ErrorCodes.NOT_FOUND, |
|---|
| 231 | | - * "Application can not be deleted becasue has assigned one or more License types, ID: " |
|---|
| 232 | | - * + appid); } |
|---|
| 233 | | - */ |
|---|
| 234 | | - em.remove(app); |
|---|
| 235 | | - return Response.ok(Utils.createMap("success", true, "id", appid)).build(); |
|---|
| 236 | | - } |
|---|
| 198 | + private Map<String, ApplicationMetadata> getMapMD(Set<ApplicationMetadata> amd) { |
|---|
| 199 | + Map<String, ApplicationMetadata> map = new HashMap<String, ApplicationMetadata>(); |
|---|
| 200 | + if (amd != null) { |
|---|
| 201 | + for (ApplicationMetadata applicationMetadata : amd) { |
|---|
| 202 | + map.put(applicationMetadata.getKey(), applicationMetadata); |
|---|
| 203 | + } |
|---|
| 204 | + } |
|---|
| 205 | + return map; |
|---|
| 206 | + } |
|---|
| 207 | + |
|---|
| 208 | + @DELETE |
|---|
| 209 | + @Path("/{appid}") |
|---|
| 210 | + @EnsureTransaction |
|---|
| 211 | + @Produces({ MediaType.APPLICATION_JSON }) |
|---|
| 212 | + @Securable |
|---|
| 213 | + @RolesAllowed(BasicSecurityContext.ROL_ADMIN) |
|---|
| 214 | + public Response delete(@PathParam("appid") String appid, @Context HttpServletRequest request) { |
|---|
| 215 | + LOG.info("Deleting app with id: {}", appid); |
|---|
| 216 | + // EntityManager em = emProvider.get(); |
|---|
| 217 | + Application app = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 218 | + if (app == null) { |
|---|
| 219 | + LOG.error("Application with id {} can not be deleted, It was not found in DB", appid); |
|---|
| 220 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid).build(); |
|---|
| 221 | + } |
|---|
| 222 | + /* |
|---|
| 223 | + * if (app.getLicenseTypes() != null && |
|---|
| 224 | + * !app.getLicenseTypes().isEmpty()) { throw new |
|---|
| 225 | + * SeCurisServiceException(ErrorCodes.NOT_FOUND, |
|---|
| 226 | + * "Application can not be deleted becasue has assigned one or more License types, ID: " |
|---|
| 227 | + * + appid); } |
|---|
| 228 | + */ |
|---|
| 229 | + em.remove(app); |
|---|
| 230 | + return Response.ok(Utils.createMap("success", true, "id", appid)).build(); |
|---|
| 231 | + } |
|---|
| 237 | 232 | |
|---|
| 238 | 233 | } |
|---|