#2140 fix - Many changes related with cahce and JPA cycling
relationships
| .. | .. |
|---|
| 22 | 22 | import com.fasterxml.jackson.annotation.JsonIgnore; |
|---|
| 23 | 23 | import com.fasterxml.jackson.annotation.JsonInclude; |
|---|
| 24 | 24 | import com.fasterxml.jackson.annotation.JsonInclude.Include; |
|---|
| 25 | +import com.fasterxml.jackson.annotation.JsonManagedReference; |
|---|
| 25 | 26 | import com.fasterxml.jackson.annotation.JsonProperty; |
|---|
| 26 | 27 | |
|---|
| 27 | 28 | /** |
|---|
| .. | .. |
|---|
| 56 | 57 | @JsonProperty("creation_timestamp") |
|---|
| 57 | 58 | private Date creationTimestamp; |
|---|
| 58 | 59 | |
|---|
| 59 | | - @JsonIgnore |
|---|
| 60 | 60 | // We don't include the referenced entities to limit the size of each row at |
|---|
| 61 | | - // the listing |
|---|
| 61 | + // // the listing |
|---|
| 62 | + @JsonIgnore |
|---|
| 62 | 63 | @OneToMany(fetch = FetchType.LAZY, mappedBy = "application") |
|---|
| 63 | 64 | private Set<LicenseType> licenseTypes; |
|---|
| 64 | 65 | |
|---|
| 65 | 66 | @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "application") |
|---|
| 67 | + @JsonManagedReference |
|---|
| 66 | 68 | private Set<ApplicationMetadata> metadata; |
|---|
| 67 | 69 | |
|---|
| 68 | 70 | public Integer getId() { |
|---|
| .. | .. |
|---|
| 90 | 92 | } |
|---|
| 91 | 93 | |
|---|
| 92 | 94 | public Date getCreationTimestamp() { |
|---|
| 93 | | - LOG.info("APP {} TS: {}", this.id, this.creationTimestamp); |
|---|
| 94 | 95 | return creationTimestamp; |
|---|
| 95 | 96 | } |
|---|
| 96 | 97 | |
|---|
| .. | .. |
|---|
| 98 | 99 | this.creationTimestamp = creationTimestamp; |
|---|
| 99 | 100 | } |
|---|
| 100 | 101 | |
|---|
| 101 | | - public Set<LicenseType> getLicenseTypes() { |
|---|
| 102 | | - return licenseTypes; |
|---|
| 103 | | - } |
|---|
| 104 | | - |
|---|
| 105 | | - public void setLicenseTypes(Set<LicenseType> licenseTypes) { |
|---|
| 106 | | - this.licenseTypes = licenseTypes; |
|---|
| 107 | | - } |
|---|
| 108 | | - |
|---|
| 109 | 102 | @JsonProperty("metadata") |
|---|
| 110 | 103 | public Set<ApplicationMetadata> getApplicationMetadata() { |
|---|
| 104 | + LOG.info("Getting metadata from app: {}", metadata); |
|---|
| 111 | 105 | return metadata; |
|---|
| 112 | 106 | } |
|---|
| 113 | 107 | |
|---|
| .. | .. |
|---|
| 137 | 131 | public void setLicenseFilename(String licenseFilename) { |
|---|
| 138 | 132 | this.licenseFilename = licenseFilename; |
|---|
| 139 | 133 | } |
|---|
| 134 | + |
|---|
| 135 | + public Set<LicenseType> getLicenseTypes() { |
|---|
| 136 | + LOG.info("Getting list license types!!!!"); |
|---|
| 137 | + return licenseTypes; |
|---|
| 138 | + } |
|---|
| 139 | + |
|---|
| 140 | + public void setLicenseTypes(Set<LicenseType> licenseTypes) { |
|---|
| 141 | + this.licenseTypes = licenseTypes; |
|---|
| 142 | + } |
|---|
| 140 | 143 | } |
|---|
| .. | .. |
|---|
| 16 | 16 | import org.apache.logging.log4j.Logger; |
|---|
| 17 | 17 | |
|---|
| 18 | 18 | import com.fasterxml.jackson.annotation.JsonAutoDetect; |
|---|
| 19 | | -import com.fasterxml.jackson.annotation.JsonIgnore; |
|---|
| 19 | +import com.fasterxml.jackson.annotation.JsonBackReference; |
|---|
| 20 | 20 | import com.fasterxml.jackson.annotation.JsonInclude; |
|---|
| 21 | 21 | import com.fasterxml.jackson.annotation.JsonInclude.Include; |
|---|
| 22 | 22 | import com.fasterxml.jackson.annotation.JsonProperty; |
|---|
| .. | .. |
|---|
| 38 | 38 | |
|---|
| 39 | 39 | private static final long serialVersionUID = 1L; |
|---|
| 40 | 40 | |
|---|
| 41 | | - @JsonIgnore |
|---|
| 42 | 41 | @Id |
|---|
| 43 | 42 | @ManyToOne |
|---|
| 44 | 43 | @JoinColumn(name = "application_id") |
|---|
| 44 | + @JsonBackReference |
|---|
| 45 | 45 | private Application application; |
|---|
| 46 | 46 | |
|---|
| 47 | 47 | @Id |
|---|
| .. | .. |
|---|
| 65 | 65 | } |
|---|
| 66 | 66 | |
|---|
| 67 | 67 | public Application getApplication() { |
|---|
| 68 | + LOG.info("Getting application from app metadata: {}", application); |
|---|
| 68 | 69 | return application; |
|---|
| 69 | 70 | } |
|---|
| 70 | 71 | |
|---|
| .. | .. |
|---|
| 73 | 74 | } |
|---|
| 74 | 75 | |
|---|
| 75 | 76 | public Date getCreationTimestamp() { |
|---|
| 76 | | - LOG.info("APP_MD (app: {}) {} TS: {}", this.application.getId(), this.key, this.creationTimestamp); |
|---|
| 77 | 77 | return creationTimestamp; |
|---|
| 78 | 78 | } |
|---|
| 79 | 79 | |
|---|
| 80 | 80 | public void setCreationTimestamp(Date creationTimestamp) { |
|---|
| 81 | 81 | this.creationTimestamp = creationTimestamp; |
|---|
| 82 | | - } |
|---|
| 83 | | - |
|---|
| 84 | | - @JsonProperty("application_id") |
|---|
| 85 | | - public Integer getApplicationId() { |
|---|
| 86 | | - return application == null ? null : application.getId(); |
|---|
| 87 | | - } |
|---|
| 88 | | - |
|---|
| 89 | | - @JsonProperty("application_id") |
|---|
| 90 | | - public void setApplicationId(Integer idApplication) { |
|---|
| 91 | | - if (idApplication == null) { |
|---|
| 92 | | - application = null; |
|---|
| 93 | | - } else { |
|---|
| 94 | | - application = new Application(); |
|---|
| 95 | | - application.setId(idApplication); |
|---|
| 96 | | - } |
|---|
| 97 | 82 | } |
|---|
| 98 | 83 | |
|---|
| 99 | 84 | public String getValue() { |
|---|
| .. | .. |
|---|
| 129 | 114 | @Override |
|---|
| 130 | 115 | public String toString() { |
|---|
| 131 | 116 | |
|---|
| 132 | | - return String.format("ApplicationMetadata (%s - %s)", this.application == null ? null : application.getId(), this.key); |
|---|
| 117 | + return String.format("ApplicationMetadata (%s)", this.key); |
|---|
| 133 | 118 | } |
|---|
| 134 | 119 | |
|---|
| 135 | 120 | } |
|---|
| .. | .. |
|---|
| 25 | 25 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|---|
| 26 | 26 | import com.fasterxml.jackson.annotation.JsonInclude; |
|---|
| 27 | 27 | import com.fasterxml.jackson.annotation.JsonInclude.Include; |
|---|
| 28 | +import com.fasterxml.jackson.annotation.JsonManagedReference; |
|---|
| 28 | 29 | import com.fasterxml.jackson.annotation.JsonProperty; |
|---|
| 29 | 30 | |
|---|
| 30 | 31 | /** |
|---|
| .. | .. |
|---|
| 59 | 60 | private Date creationTimestamp; |
|---|
| 60 | 61 | |
|---|
| 61 | 62 | @JsonIgnore |
|---|
| 62 | | - @ManyToOne |
|---|
| 63 | + @ManyToOne(fetch = FetchType.LAZY) |
|---|
| 63 | 64 | @JoinColumn(name = "application_id") |
|---|
| 64 | 65 | private Application application; |
|---|
| 65 | 66 | |
|---|
| 66 | 67 | @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "licenseType") |
|---|
| 68 | + @JsonManagedReference |
|---|
| 67 | 69 | private Set<LicenseTypeMetadata> metadata; |
|---|
| 68 | 70 | |
|---|
| 69 | 71 | public Set<LicenseTypeMetadata> getMetadata() { |
|---|
| .. | .. |
|---|
| 107 | 109 | } |
|---|
| 108 | 110 | |
|---|
| 109 | 111 | public Application getApplication() { |
|---|
| 112 | + LOG.info("Getting APP from LicType"); |
|---|
| 110 | 113 | return application; |
|---|
| 111 | 114 | } |
|---|
| 112 | 115 | |
|---|
| .. | .. |
|---|
| 12 | 12 | import javax.persistence.Table; |
|---|
| 13 | 13 | |
|---|
| 14 | 14 | import com.fasterxml.jackson.annotation.JsonAutoDetect; |
|---|
| 15 | +import com.fasterxml.jackson.annotation.JsonBackReference; |
|---|
| 15 | 16 | import com.fasterxml.jackson.annotation.JsonIgnore; |
|---|
| 16 | 17 | import com.fasterxml.jackson.annotation.JsonInclude; |
|---|
| 17 | 18 | import com.fasterxml.jackson.annotation.JsonInclude.Include; |
|---|
| 18 | | -import com.fasterxml.jackson.annotation.JsonProperty; |
|---|
| 19 | 19 | |
|---|
| 20 | 20 | /** |
|---|
| 21 | 21 | * Entity implementation class for Entity: licensetype_metadata |
|---|
| .. | .. |
|---|
| 32 | 32 | |
|---|
| 33 | 33 | private static final long serialVersionUID = 1L; |
|---|
| 34 | 34 | |
|---|
| 35 | | - @Id |
|---|
| 36 | 35 | @JsonIgnore |
|---|
| 36 | + @Id |
|---|
| 37 | 37 | @ManyToOne |
|---|
| 38 | 38 | @JoinColumn(name = "license_type_id") |
|---|
| 39 | + @JsonBackReference |
|---|
| 39 | 40 | private LicenseType licenseType; |
|---|
| 40 | 41 | |
|---|
| 41 | 42 | @Id |
|---|
| .. | .. |
|---|
| 45 | 46 | private String value; |
|---|
| 46 | 47 | |
|---|
| 47 | 48 | private boolean mandatory; |
|---|
| 48 | | - |
|---|
| 49 | | - @JsonProperty("licensetype_id") |
|---|
| 50 | | - public Integer getLicenseTypeId() { |
|---|
| 51 | | - return licenseType == null ? null : licenseType.getId(); |
|---|
| 52 | | - } |
|---|
| 53 | | - |
|---|
| 54 | | - @JsonProperty("licensetype_id") |
|---|
| 55 | | - public void setLicenseTypeId(Integer idLicenseType) { |
|---|
| 56 | | - if (idLicenseType == null) { |
|---|
| 57 | | - licenseType = null; |
|---|
| 58 | | - } else { |
|---|
| 59 | | - licenseType = new LicenseType(); |
|---|
| 60 | | - licenseType.setId(idLicenseType); |
|---|
| 61 | | - } |
|---|
| 62 | | - } |
|---|
| 63 | 49 | |
|---|
| 64 | 50 | public LicenseType getLicenseType() { |
|---|
| 65 | 51 | return licenseType; |
|---|
| .. | .. |
|---|
| 1 | 1 | package net.curisit.securis.db; |
|---|
| 2 | 2 | |
|---|
| 3 | 3 | import java.io.Serializable; |
|---|
| 4 | | -import java.util.ArrayList; |
|---|
| 5 | 4 | import java.util.Date; |
|---|
| 5 | +import java.util.HashSet; |
|---|
| 6 | 6 | import java.util.List; |
|---|
| 7 | 7 | import java.util.Set; |
|---|
| 8 | 8 | |
|---|
| .. | .. |
|---|
| 72 | 72 | inverseJoinColumns = { |
|---|
| 73 | 73 | @JoinColumn(name = "username", referencedColumnName = "username") |
|---|
| 74 | 74 | }) |
|---|
| 75 | | - private List<User> users; |
|---|
| 75 | + private Set<User> users; |
|---|
| 76 | 76 | |
|---|
| 77 | 77 | @JsonIgnore |
|---|
| 78 | 78 | // We don't include the users to limit the size of each row a the listing |
|---|
| .. | .. |
|---|
| 125 | 125 | this.creationTimestamp = creationTimestamp; |
|---|
| 126 | 126 | } |
|---|
| 127 | 127 | |
|---|
| 128 | | - public List<User> getUsers() { |
|---|
| 128 | + public Set<User> getUsers() { |
|---|
| 129 | 129 | return users; |
|---|
| 130 | 130 | } |
|---|
| 131 | 131 | |
|---|
| 132 | | - public void setUsers(List<User> users) { |
|---|
| 132 | + public void setUsers(Set<User> users) { |
|---|
| 133 | 133 | this.users = users; |
|---|
| 134 | 134 | } |
|---|
| 135 | 135 | |
|---|
| .. | .. |
|---|
| 166 | 166 | |
|---|
| 167 | 167 | @JsonProperty("users_ids") |
|---|
| 168 | 168 | public void setUsersIds(List<String> usersIds) { |
|---|
| 169 | | - users = new ArrayList<>(); |
|---|
| 169 | + users = new HashSet<>(); |
|---|
| 170 | 170 | if (usersIds != null) { |
|---|
| 171 | 171 | for (String userid : usersIds) { |
|---|
| 172 | 172 | User u = new User(); |
|---|
| .. | .. |
|---|
| 177 | 177 | } |
|---|
| 178 | 178 | |
|---|
| 179 | 179 | @JsonProperty("users_ids") |
|---|
| 180 | | - public List<String> getUsersIds() { |
|---|
| 180 | + public Set<String> getUsersIds() { |
|---|
| 181 | 181 | if (users == null) { |
|---|
| 182 | 182 | return null; |
|---|
| 183 | 183 | } |
|---|
| 184 | | - List<String> ids = new ArrayList<>(); |
|---|
| 184 | + Set<String> ids = new HashSet<>(); |
|---|
| 185 | 185 | for (User user : users) { |
|---|
| 186 | 186 | ids.add(user.getUsername()); |
|---|
| 187 | 187 | } |
|---|
| .. | .. |
|---|
| 26 | 26 | |
|---|
| 27 | 27 | import net.curisit.integrity.commons.Utils; |
|---|
| 28 | 28 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 29 | +import net.curisit.securis.SeCurisException; |
|---|
| 29 | 30 | import net.curisit.securis.db.Application; |
|---|
| 30 | 31 | import net.curisit.securis.db.ApplicationMetadata; |
|---|
| 31 | 32 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| 32 | 33 | import net.curisit.securis.security.Securable; |
|---|
| 34 | +import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 35 | +import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; |
|---|
| 36 | +import net.curisit.securis.utils.JsonUtils; |
|---|
| 33 | 37 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 34 | 38 | |
|---|
| 35 | 39 | import org.apache.logging.log4j.LogManager; |
|---|
| .. | .. |
|---|
| 71 | 75 | LOG.info("Getting applications list "); |
|---|
| 72 | 76 | |
|---|
| 73 | 77 | EntityManager em = emProvider.get(); |
|---|
| 78 | + em.clear(); |
|---|
| 74 | 79 | TypedQuery<Application> q = em.createNamedQuery("list-applications", Application.class); |
|---|
| 75 | 80 | List<Application> list = q.getResultList(); |
|---|
| 76 | 81 | |
|---|
| .. | .. |
|---|
| 80 | 85 | /** |
|---|
| 81 | 86 | * |
|---|
| 82 | 87 | * @return the server version in format majorVersion.minorVersion |
|---|
| 88 | + * @throws SeCurisServiceException |
|---|
| 83 | 89 | */ |
|---|
| 84 | 90 | @GET |
|---|
| 85 | 91 | @Path("/{appid}") |
|---|
| .. | .. |
|---|
| 87 | 93 | MediaType.APPLICATION_JSON |
|---|
| 88 | 94 | }) |
|---|
| 89 | 95 | @Securable |
|---|
| 90 | | - public Response get(@PathParam("appid") String appid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 96 | + public Response get(@PathParam("appid") String appid) throws SeCurisServiceException { |
|---|
| 91 | 97 | LOG.info("Getting application data for id: {}: ", appid); |
|---|
| 92 | 98 | if (appid == null || "".equals(appid)) { |
|---|
| 93 | 99 | LOG.error("Application ID is mandatory"); |
|---|
| .. | .. |
|---|
| 95 | 101 | } |
|---|
| 96 | 102 | |
|---|
| 97 | 103 | EntityManager em = emProvider.get(); |
|---|
| 98 | | - Application app = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 104 | + em.clear(); |
|---|
| 105 | + |
|---|
| 106 | + Application app = null; |
|---|
| 107 | + try { |
|---|
| 108 | + LOG.info("READY to GET app: {}", appid); |
|---|
| 109 | + app = em.find(Application.class, Integer.parseInt(appid)); |
|---|
| 110 | + } catch (Exception e) { |
|---|
| 111 | + LOG.info("ERROR GETTING app: {}", e); |
|---|
| 112 | + } |
|---|
| 99 | 113 | if (app == null) { |
|---|
| 100 | 114 | LOG.error("Application with id {} not found in DB", appid); |
|---|
| 115 | + throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Application not found with ID: " + appid); |
|---|
| 116 | + } |
|---|
| 101 | 117 | |
|---|
| 102 | | - return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid) |
|---|
| 103 | | - .build(); |
|---|
| 118 | + try { |
|---|
| 119 | + LOG.info("JSON for APP:\n\n\n"); |
|---|
| 120 | + LOG.info(JsonUtils.toJSON(app)); |
|---|
| 121 | + } catch (SeCurisException e) { |
|---|
| 122 | + LOG.info("ERROR {}", e); |
|---|
| 123 | + |
|---|
| 124 | + } catch (Exception e) { |
|---|
| 125 | + LOG.info("ERROR??? {}", e); |
|---|
| 104 | 126 | } |
|---|
| 105 | 127 | return Response.ok(app).build(); |
|---|
| 106 | 128 | } |
|---|
| .. | .. |
|---|
| 199 | 221 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application not found with ID: " + appid) |
|---|
| 200 | 222 | .build(); |
|---|
| 201 | 223 | } |
|---|
| 202 | | - |
|---|
| 203 | | - if (app.getLicenseTypes() != null && !app.getLicenseTypes().isEmpty()) { |
|---|
| 204 | | - return Response |
|---|
| 205 | | - .status(Status.FORBIDDEN) |
|---|
| 206 | | - .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, |
|---|
| 207 | | - "Application can not be deleted becasue has assigned one or more License types, ID: " + appid).build(); |
|---|
| 208 | | - } |
|---|
| 209 | | - |
|---|
| 224 | + /* |
|---|
| 225 | + * if (app.getLicenseTypes() != null && |
|---|
| 226 | + * !app.getLicenseTypes().isEmpty()) { throw new |
|---|
| 227 | + * SeCurisServiceException(ErrorCodes.NOT_FOUND, |
|---|
| 228 | + * "Application can not be deleted becasue has assigned one or more License types, ID: " |
|---|
| 229 | + * + appid); } |
|---|
| 230 | + */ |
|---|
| 210 | 231 | em.remove(app); |
|---|
| 211 | 232 | return Response.ok(Utils.createMap("success", true, "id", appid)).build(); |
|---|
| 212 | 233 | } |
|---|
| .. | .. |
|---|
| 27 | 27 | import org.apache.logging.log4j.LogManager; |
|---|
| 28 | 28 | import org.apache.logging.log4j.Logger; |
|---|
| 29 | 29 | |
|---|
| 30 | +import com.google.inject.persist.Transactional; |
|---|
| 31 | + |
|---|
| 30 | 32 | /** |
|---|
| 31 | 33 | * Basic services for login and basic app wrkflow |
|---|
| 32 | 34 | * |
|---|
| .. | .. |
|---|
| 98 | 100 | @Produces({ |
|---|
| 99 | 101 | MediaType.APPLICATION_JSON |
|---|
| 100 | 102 | }) |
|---|
| 103 | + @Transactional |
|---|
| 101 | 104 | public Response check(@HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token, @QueryParam("token") String token2) { |
|---|
| 102 | 105 | if (token == null) { |
|---|
| 103 | 106 | token = token2; |
|---|
| .. | .. |
|---|
| 107 | 107 | LOG.info("Getting licenses list "); |
|---|
| 108 | 108 | |
|---|
| 109 | 109 | EntityManager em = emProvider.get(); |
|---|
| 110 | + em.clear(); |
|---|
| 110 | 111 | |
|---|
| 111 | 112 | if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 112 | 113 | Pack pack = em.find(Pack.class, packId); |
|---|
| .. | .. |
|---|
| 141 | 142 | LOG.info("Getting organization data for id: {}: ", licId); |
|---|
| 142 | 143 | |
|---|
| 143 | 144 | EntityManager em = emProvider.get(); |
|---|
| 145 | + em.clear(); |
|---|
| 144 | 146 | License lic = getCurrentLicense(licId, bsc, em); |
|---|
| 145 | 147 | return Response.ok(lic).build(); |
|---|
| 146 | 148 | } |
|---|
| .. | .. |
|---|
| 156 | 158 | @Produces({ |
|---|
| 157 | 159 | MediaType.APPLICATION_OCTET_STREAM |
|---|
| 158 | 160 | }) |
|---|
| 161 | + @Transactional |
|---|
| 159 | 162 | public Response download(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) throws SeCurisServiceException { |
|---|
| 160 | 163 | |
|---|
| 161 | 164 | EntityManager em = emProvider.get(); |
|---|
| .. | .. |
|---|
| 32 | 32 | import net.curisit.securis.db.LicenseTypeMetadata; |
|---|
| 33 | 33 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| 34 | 34 | import net.curisit.securis.security.Securable; |
|---|
| 35 | +import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 36 | +import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; |
|---|
| 35 | 37 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 36 | 38 | |
|---|
| 37 | 39 | import org.apache.logging.log4j.LogManager; |
|---|
| .. | .. |
|---|
| 73 | 75 | LOG.info("Getting license types list "); |
|---|
| 74 | 76 | |
|---|
| 75 | 77 | EntityManager em = emProvider.get(); |
|---|
| 78 | + em.clear(); |
|---|
| 76 | 79 | TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class); |
|---|
| 77 | 80 | List<LicenseType> list = q.getResultList(); |
|---|
| 78 | 81 | |
|---|
| .. | .. |
|---|
| 82 | 85 | /** |
|---|
| 83 | 86 | * |
|---|
| 84 | 87 | * @return the server version in format majorVersion.minorVersion |
|---|
| 88 | + * @throws SeCurisServiceException |
|---|
| 85 | 89 | */ |
|---|
| 86 | 90 | @GET |
|---|
| 87 | 91 | @Path("/{ltid}") |
|---|
| .. | .. |
|---|
| 89 | 93 | MediaType.APPLICATION_JSON |
|---|
| 90 | 94 | }) |
|---|
| 91 | 95 | @Securable |
|---|
| 92 | | - public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) { |
|---|
| 96 | + public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) throws SeCurisServiceException { |
|---|
| 93 | 97 | LOG.info("Getting license type data for id: {}: ", ltid); |
|---|
| 94 | 98 | if (ltid == null || "".equals(ltid)) { |
|---|
| 95 | 99 | LOG.error("LicenseType ID is mandatory"); |
|---|
| .. | .. |
|---|
| 97 | 101 | } |
|---|
| 98 | 102 | |
|---|
| 99 | 103 | EntityManager em = emProvider.get(); |
|---|
| 104 | + em.clear(); |
|---|
| 100 | 105 | LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid)); |
|---|
| 101 | 106 | if (lt == null) { |
|---|
| 102 | 107 | LOG.error("LicenseType with id {} not found in DB", ltid); |
|---|
| 103 | | - return Response.status(Status.NOT_FOUND).build(); |
|---|
| 108 | + throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "LicenseType was not found in DB"); |
|---|
| 104 | 109 | } |
|---|
| 105 | 110 | return Response.ok(lt).build(); |
|---|
| 106 | 111 | } |
|---|
| .. | .. |
|---|
| 1 | 1 | package net.curisit.securis.services; |
|---|
| 2 | 2 | |
|---|
| 3 | | -import java.util.ArrayList; |
|---|
| 4 | 3 | import java.util.Date; |
|---|
| 4 | +import java.util.HashSet; |
|---|
| 5 | 5 | import java.util.List; |
|---|
| 6 | +import java.util.Set; |
|---|
| 6 | 7 | |
|---|
| 7 | 8 | import javax.annotation.security.RolesAllowed; |
|---|
| 8 | 9 | import javax.inject.Inject; |
|---|
| .. | .. |
|---|
| 35 | 36 | |
|---|
| 36 | 37 | import org.apache.logging.log4j.LogManager; |
|---|
| 37 | 38 | import org.apache.logging.log4j.Logger; |
|---|
| 38 | | -import org.jboss.resteasy.spi.ResteasyProviderFactory; |
|---|
| 39 | 39 | |
|---|
| 40 | 40 | import com.google.inject.persist.Transactional; |
|---|
| 41 | 41 | |
|---|
| .. | .. |
|---|
| 69 | 69 | public Response index(@Context BasicSecurityContext bsc) { |
|---|
| 70 | 70 | LOG.info("Getting organizations list "); |
|---|
| 71 | 71 | |
|---|
| 72 | | - BasicSecurityContext bsc2 = ResteasyProviderFactory.getContextData(BasicSecurityContext.class); |
|---|
| 73 | | - LOG.debug("securityContext ROL_ADMIN?: {}", bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)); |
|---|
| 74 | 72 | EntityManager em = emProvider.get(); |
|---|
| 73 | + em.clear(); |
|---|
| 75 | 74 | TypedQuery<Organization> q; |
|---|
| 76 | 75 | if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 77 | 76 | LOG.info("GEtting all orgs for user: " + bsc.getUserPrincipal()); |
|---|
| .. | .. |
|---|
| 103 | 102 | } |
|---|
| 104 | 103 | |
|---|
| 105 | 104 | EntityManager em = emProvider.get(); |
|---|
| 105 | + em.clear(); |
|---|
| 106 | 106 | Organization org = em.find(Organization.class, Integer.parseInt(orgid)); |
|---|
| 107 | 107 | if (org == null) { |
|---|
| 108 | 108 | LOG.error("Organization with id {} not found in DB", orgid); |
|---|
| .. | .. |
|---|
| 141 | 141 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 142 | 142 | } |
|---|
| 143 | 143 | |
|---|
| 144 | | - List<User> users = null; |
|---|
| 145 | | - List<String> usersIds = org.getUsersIds(); |
|---|
| 144 | + Set<User> users = null; |
|---|
| 145 | + Set<String> usersIds = org.getUsersIds(); |
|---|
| 146 | 146 | if (usersIds != null && !usersIds.isEmpty()) { |
|---|
| 147 | | - users = new ArrayList<>(); |
|---|
| 147 | + users = new HashSet<>(); |
|---|
| 148 | 148 | for (String username : usersIds) { |
|---|
| 149 | 149 | User user = em.find(User.class, username); |
|---|
| 150 | 150 | if (user == null) { |
|---|
| .. | .. |
|---|
| 176 | 176 | org.setParentOrganization(parentOrg); |
|---|
| 177 | 177 | } |
|---|
| 178 | 178 | |
|---|
| 179 | | - private void setOrgUsers(Organization org, List<String> usersIds, EntityManager em) throws SeCurisException { |
|---|
| 180 | | - List<User> users = null; |
|---|
| 179 | + private void setOrgUsers(Organization org, Set<String> usersIds, EntityManager em) throws SeCurisException { |
|---|
| 180 | + Set<User> users = null; |
|---|
| 181 | 181 | if (usersIds != null && !usersIds.isEmpty()) { |
|---|
| 182 | | - users = new ArrayList<>(); |
|---|
| 182 | + users = new HashSet<>(); |
|---|
| 183 | 183 | for (String username : usersIds) { |
|---|
| 184 | 184 | User user = em.find(User.class, username); |
|---|
| 185 | 185 | if (user == null) { |
|---|
| .. | .. |
|---|
| 40 | 40 | import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 41 | 41 | import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; |
|---|
| 42 | 42 | import net.curisit.securis.services.helpers.LicenseHelper; |
|---|
| 43 | | -import net.curisit.securis.services.helpers.UserHelper; |
|---|
| 44 | 43 | import net.curisit.securis.utils.LicUtils; |
|---|
| 45 | 44 | import net.curisit.securis.utils.TokenHelper; |
|---|
| 46 | 45 | |
|---|
| .. | .. |
|---|
| 67 | 66 | Provider<EntityManager> emProvider; |
|---|
| 68 | 67 | |
|---|
| 69 | 68 | @Inject |
|---|
| 70 | | - private UserHelper userHelper; |
|---|
| 71 | | - |
|---|
| 72 | | - @Inject |
|---|
| 73 | 69 | private LicenseHelper licenseHelper; |
|---|
| 74 | 70 | |
|---|
| 75 | 71 | /** |
|---|
| .. | .. |
|---|
| 86 | 82 | LOG.info("Getting packs list "); |
|---|
| 87 | 83 | |
|---|
| 88 | 84 | EntityManager em = emProvider.get(); |
|---|
| 85 | + em.clear(); |
|---|
| 89 | 86 | |
|---|
| 90 | 87 | TypedQuery<Pack> q; |
|---|
| 91 | 88 | if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| .. | .. |
|---|
| 127 | 124 | } |
|---|
| 128 | 125 | |
|---|
| 129 | 126 | EntityManager em = emProvider.get(); |
|---|
| 127 | + em.clear(); |
|---|
| 130 | 128 | Pack pack = em.find(Pack.class, packId); |
|---|
| 131 | 129 | if (pack == null) { |
|---|
| 132 | 130 | LOG.error("Pack with id {} not found in DB", packId); |
|---|
| .. | .. |
|---|
| 76 | 76 | LOG.info("Getting users list "); |
|---|
| 77 | 77 | |
|---|
| 78 | 78 | EntityManager em = emProvider.get(); |
|---|
| 79 | + em.clear(); |
|---|
| 79 | 80 | TypedQuery<User> q = em.createNamedQuery("list-users", User.class); |
|---|
| 80 | 81 | |
|---|
| 81 | 82 | List<User> list = q.getResultList(); |
|---|
| .. | .. |
|---|
| 102 | 103 | } |
|---|
| 103 | 104 | |
|---|
| 104 | 105 | EntityManager em = emProvider.get(); |
|---|
| 106 | + em.clear(); |
|---|
| 105 | 107 | User lt = em.find(User.class, uid); |
|---|
| 106 | 108 | if (lt == null) { |
|---|
| 107 | 109 | LOG.error("User with id {} not found in DB", uid); |
|---|
| .. | .. |
|---|
| 136 | 138 | if (user.getPassword() != null && !"".equals(user.getPassword())) { |
|---|
| 137 | 139 | user.setPassword(Utils.sha256(user.getPassword())); |
|---|
| 138 | 140 | } else { |
|---|
| 139 | | - return Response.status(DefaultExceptionHandler.DEFAULT_APP_ERROR_STATUS_CODE).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "User password is mandatory").build(); |
|---|
| 141 | + return Response.status(DefaultExceptionHandler.DEFAULT_APP_ERROR_STATUS_CODE) |
|---|
| 142 | + .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "User password is mandatory").build(); |
|---|
| 140 | 143 | } |
|---|
| 141 | 144 | user.setModificationTimestamp(new Date()); |
|---|
| 142 | 145 | user.setLastLogin(null); |
|---|
| .. | .. |
|---|
| 197 | 200 | currentUser.setPassword(Utils.sha256(user.getPassword())); |
|---|
| 198 | 201 | } else { |
|---|
| 199 | 202 | // Password has not been modified |
|---|
| 200 | | - //return Response.status(DefaultExceptionHandler.DEFAULT_APP_ERROR_STATUS_CODE).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "User password is mandatory").build(); |
|---|
| 203 | + // return |
|---|
| 204 | + // Response.status(DefaultExceptionHandler.DEFAULT_APP_ERROR_STATUS_CODE).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, |
|---|
| 205 | + // "User password is mandatory").build(); |
|---|
| 201 | 206 | } |
|---|
| 202 | 207 | |
|---|
| 203 | 208 | currentUser.setLastLogin(user.getLastLogin()); |
|---|
| .. | .. |
|---|
| 238 | 243 | LOG.info("user: {}, pass: {}", username, password); |
|---|
| 239 | 244 | LOG.info("is user in role: {} == {} ? ", "advance", request.isUserInRole("advance")); |
|---|
| 240 | 245 | LOG.info("is user in role: {} == {} ? ", "admin", request.isUserInRole("admin")); |
|---|
| 241 | | - |
|---|
| 246 | + |
|---|
| 242 | 247 | EntityManager em = emProvider.get(); |
|---|
| 243 | 248 | User user = em.find(User.class, username); |
|---|
| 244 | 249 | if (user == null) { |
|---|
| .. | .. |
|---|
| 246 | 251 | return Response.status(Status.UNAUTHORIZED).build(); |
|---|
| 247 | 252 | } |
|---|
| 248 | 253 | String securedPassword = Utils.sha256(password); |
|---|
| 249 | | - |
|---|
| 254 | + |
|---|
| 250 | 255 | if (securedPassword == null || !securedPassword.equals(user.getPassword())) { |
|---|
| 251 | 256 | // TODO: Code to test exception handling |
|---|
| 252 | 257 | return Response.status(Status.UNAUTHORIZED).build(); |
|---|
| .. | .. |
|---|
| 256 | 261 | try { |
|---|
| 257 | 262 | em.persist(user); |
|---|
| 258 | 263 | em.getTransaction().commit(); |
|---|
| 259 | | - } catch(PersistenceException ex) { |
|---|
| 264 | + } catch (PersistenceException ex) { |
|---|
| 260 | 265 | LOG.error("Error updating last login date for user: {}", username); |
|---|
| 261 | 266 | LOG.error(ex); |
|---|
| 262 | 267 | em.getTransaction().rollback(); |
|---|
| .. | .. |
|---|
| 17 | 17 | <property name="hibernate.connection.datasource" value="java:comp/env/jdbc/SeCurisDS" /> |
|---|
| 18 | 18 | |
|---|
| 19 | 19 | <property name="hibernate.cache.use_second_level_cache" value="false" /> |
|---|
| 20 | | - <property name="hibernate.show_sql" value="false" /> |
|---|
| 20 | + <property name="hibernate.show_sql" value="true" /> |
|---|
| 21 | 21 | |
|---|
| 22 | | - <!-- <property name="hibernate.format_sql" value="true"/> |
|---|
| 23 | | - --> |
|---|
| 22 | + <property name="hibernate.format_sql" value="false"/> |
|---|
| 24 | 23 | </properties> |
|---|
| 25 | 24 | |
|---|
| 26 | 25 | </persistence-unit> |
|---|
| .. | .. |
|---|
| 69 | 69 | |
|---|
| 70 | 70 | $scope.selectCatalog = _changeCatalog; |
|---|
| 71 | 71 | |
|---|
| 72 | | - $scope.edit = function(data) { |
|---|
| 73 | | - $scope.showForm = true; |
|---|
| 74 | | - $scope.isNew = false; |
|---|
| 75 | | - // Next line is a workaround due to some issues with values with ID == 0 |
|---|
| 72 | + $scope._loadFormu = function(elementData) { |
|---|
| 76 | 73 | $('select').val(null); |
|---|
| 77 | 74 | $scope.formu = {} |
|---|
| 78 | 75 | var fields = Catalogs.getMetadata().fields; |
|---|
| .. | .. |
|---|
| 83 | 80 | // next lines are a workaround to avoid an issue where we try to show a form with "select" fields (if select field value doesn't change |
|---|
| 84 | 81 | $scope.formu[field.name] = null; |
|---|
| 85 | 82 | setTimeout(function() { |
|---|
| 86 | | - $scope.formu[field.name] = data[field.name]; |
|---|
| 83 | + $scope.formu[field.name] = elementData[field.name]; |
|---|
| 87 | 84 | $scope.$apply(); |
|---|
| 88 | 85 | }, 0); |
|---|
| 89 | 86 | } else { |
|---|
| 90 | | - if (!field.listingOnly) $scope.formu[field.name] = data[field.name] || null; |
|---|
| 87 | + if (!field.listingOnly) $scope.formu[field.name] = elementData[field.name] || null; |
|---|
| 91 | 88 | } |
|---|
| 92 | 89 | }) |
|---|
| 90 | + } |
|---|
| 91 | + |
|---|
| 92 | + $scope.edit = function(data) { |
|---|
| 93 | + $scope.showForm = true; |
|---|
| 94 | + $scope.isNew = false; |
|---|
| 95 | + $scope._loadFormu(data); |
|---|
| 96 | + Catalogs.get(data[Catalogs.getPk()], function(eleData) { |
|---|
| 97 | + $scope._loadFormu(eleData); |
|---|
| 98 | + }); |
|---|
| 99 | + // Next line is a workaround due to some issues with values with ID == 0 |
|---|
| 93 | 100 | setTimeout(function() { |
|---|
| 94 | 101 | $('#'+Catalogs.getFFF()).focus(); |
|---|
| 95 | 102 | }, 0); |
|---|
| .. | .. |
|---|
| 18 | 18 | function($rootScope, $http, $resource, $q) { |
|---|
| 19 | 19 | var resources = { |
|---|
| 20 | 20 | application : $resource( |
|---|
| 21 | | - 'application/:appId', { |
|---|
| 22 | | - appId : '@id' |
|---|
| 21 | + 'application/:id', { |
|---|
| 22 | + id : '@id' |
|---|
| 23 | 23 | }), |
|---|
| 24 | | - user : $resource('user/:userId', { |
|---|
| 25 | | - userId : '@username' |
|---|
| 24 | + user : $resource('user/:id', { |
|---|
| 25 | + id : '@username' |
|---|
| 26 | 26 | }), |
|---|
| 27 | 27 | organization : $resource( |
|---|
| 28 | | - 'organization/:orgId', { |
|---|
| 29 | | - orgId : '@id' |
|---|
| 28 | + 'organization/:id', { |
|---|
| 29 | + id : '@id' |
|---|
| 30 | 30 | }), |
|---|
| 31 | 31 | licensetype : $resource( |
|---|
| 32 | | - 'licensetype/:licenseTypeId', { |
|---|
| 33 | | - licenseTypeId : '@id' |
|---|
| 32 | + 'licensetype/:id', { |
|---|
| 33 | + id : '@id' |
|---|
| 34 | 34 | }) |
|---|
| 35 | 35 | } |
|---|
| 36 | 36 | |
|---|
| .. | .. |
|---|
| 68 | 68 | return resources[res]; |
|---|
| 69 | 69 | } |
|---|
| 70 | 70 | this.getPk = function(catalogMetadata) { |
|---|
| 71 | | - if (!catalogMetadata) |
|---|
| 71 | + if (!catalogMetadata) { |
|---|
| 72 | 72 | catalogMetadata = _current; |
|---|
| 73 | + } |
|---|
| 73 | 74 | |
|---|
| 74 | | - for (var i = 0; i < catalogMetadata.fields.length; i++) |
|---|
| 75 | | - if (catalogMetadata.fields[i].pk) |
|---|
| 75 | + for (var i = 0; i < catalogMetadata.fields.length; i++) { |
|---|
| 76 | + if (catalogMetadata.fields[i].pk) { |
|---|
| 76 | 77 | return catalogMetadata.fields[i].name; |
|---|
| 78 | + } |
|---|
| 79 | + } |
|---|
| 77 | 80 | |
|---|
| 78 | 81 | return null; |
|---|
| 79 | 82 | } |
|---|
| .. | .. |
|---|
| 167 | 170 | } |
|---|
| 168 | 171 | |
|---|
| 169 | 172 | this.save = function(data) { |
|---|
| 170 | | - if (!_current) |
|---|
| 173 | + if (!_current) { |
|---|
| 171 | 174 | throw new Error('There is no current catalog selected'); |
|---|
| 172 | | - |
|---|
| 175 | + } |
|---|
| 173 | 176 | var resource = this.getResource(); |
|---|
| 174 | 177 | return resource.save(data, _success, _fail); |
|---|
| 178 | + } |
|---|
| 179 | + this.get = function(id, _onsuccess, _onfail) { |
|---|
| 180 | + if (!_current) { |
|---|
| 181 | + throw new Error('There is no current catalog selected'); |
|---|
| 182 | + } |
|---|
| 183 | + var resource = this.getResource(); |
|---|
| 184 | + return resource.get({id: id}, _onsuccess, _onfail); |
|---|
| 175 | 185 | } |
|---|
| 176 | 186 | this.remove = function(data) { |
|---|
| 177 | 187 | return this.getResource().remove({}, data, |
|---|
| 178 | 188 | _success, _fail) |
|---|
| 179 | 189 | } |
|---|
| 180 | 190 | this.query = function() { |
|---|
| 181 | | - return this.getResource().query({}, |
|---|
| 182 | | - _success, _fail); |
|---|
| 191 | + var list = this.getResource().query(); |
|---|
| 192 | + list.$promise.then(_success, _fail); |
|---|
| 193 | + return list; |
|---|
| 183 | 194 | } |
|---|
| 184 | 195 | this.refreshRef = function(refs, res, |
|---|
| 185 | 196 | preloadedData) { |
|---|