| .. | .. |
|---|
| 16 | 16 | import javax.persistence.ManyToOne; |
|---|
| 17 | 17 | import javax.persistence.NamedQueries; |
|---|
| 18 | 18 | import javax.persistence.NamedQuery; |
|---|
| 19 | +import javax.persistence.NonUniqueResultException; |
|---|
| 19 | 20 | import javax.persistence.OneToMany; |
|---|
| 20 | 21 | import javax.persistence.Table; |
|---|
| 21 | 22 | import javax.persistence.TypedQuery; |
|---|
| 22 | 23 | |
|---|
| 23 | 24 | import net.curisit.integrity.commons.Utils; |
|---|
| 25 | +import net.curisit.securis.services.exception.SeCurisServiceException; |
|---|
| 26 | +import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes; |
|---|
| 27 | + |
|---|
| 28 | +import org.apache.logging.log4j.LogManager; |
|---|
| 29 | +import org.apache.logging.log4j.Logger; |
|---|
| 24 | 30 | |
|---|
| 25 | 31 | import com.fasterxml.jackson.annotation.JsonAutoDetect; |
|---|
| 26 | 32 | import com.fasterxml.jackson.annotation.JsonIgnore; |
|---|
| .. | .. |
|---|
| 39 | 45 | @JsonIgnoreProperties(ignoreUnknown = true) |
|---|
| 40 | 46 | @NamedQueries({ |
|---|
| 41 | 47 | @NamedQuery(name = "list-licenses-by-pack", query = "SELECT l FROM License l where l.pack.id = :packId"), |
|---|
| 48 | + @NamedQuery(name = "list-licenses-by-req-data", query = "SELECT l FROM License l where l.reqDataHash = :hash"), |
|---|
| 42 | 49 | @NamedQuery(name = "list-active-licenses-by-req-data", query = "SELECT l FROM License l where l.reqDataHash = :hash and l.status in ('AC', 'PA')") |
|---|
| 43 | 50 | }) |
|---|
| 44 | 51 | public class License implements Serializable { |
|---|
| 52 | + private static final long serialVersionUID = 2700310404904877227L; |
|---|
| 45 | 53 | |
|---|
| 46 | | - private static final long serialVersionUID = 1L; |
|---|
| 54 | + private static final Logger LOG = LogManager.getLogger(License.class); |
|---|
| 47 | 55 | |
|---|
| 48 | 56 | @Id |
|---|
| 49 | 57 | @GeneratedValue |
|---|
| .. | .. |
|---|
| 63 | 71 | |
|---|
| 64 | 72 | @JsonIgnore |
|---|
| 65 | 73 | @ManyToOne |
|---|
| 66 | | - @JoinColumn(name = "canceled_by") |
|---|
| 67 | | - private User canceledBy; |
|---|
| 74 | + @JoinColumn(name = "cancelled_by") |
|---|
| 75 | + private User cancelledBy; |
|---|
| 68 | 76 | |
|---|
| 69 | 77 | private LicenseStatus status; |
|---|
| 70 | 78 | |
|---|
| .. | .. |
|---|
| 78 | 86 | @JsonProperty("request_data") |
|---|
| 79 | 87 | private String requestData; |
|---|
| 80 | 88 | |
|---|
| 89 | + /** |
|---|
| 90 | + * request data hash is automatically set when we use |
|---|
| 91 | + * {@link License#setRequestData(String)} method |
|---|
| 92 | + */ |
|---|
| 81 | 93 | @Column(name = "request_data_hash") |
|---|
| 82 | 94 | @JsonIgnore |
|---|
| 83 | 95 | private String reqDataHash; |
|---|
| .. | .. |
|---|
| 158 | 170 | } |
|---|
| 159 | 171 | } |
|---|
| 160 | 172 | |
|---|
| 161 | | - @JsonProperty("canceled_by_id") |
|---|
| 162 | | - public String getCanceledById() { |
|---|
| 163 | | - return canceledBy == null ? null : canceledBy.getUsername(); |
|---|
| 173 | + @JsonProperty("cancelled_by_id") |
|---|
| 174 | + public String getCancelledById() { |
|---|
| 175 | + return cancelledBy == null ? null : cancelledBy.getUsername(); |
|---|
| 164 | 176 | } |
|---|
| 165 | 177 | |
|---|
| 166 | | - @JsonProperty("canceled_by_id") |
|---|
| 167 | | - public void setCanceledById(String username) { |
|---|
| 178 | + @JsonProperty("cancelled_by_id") |
|---|
| 179 | + public void setCancelledById(String username) { |
|---|
| 168 | 180 | if (username == null) { |
|---|
| 169 | | - canceledBy = null; |
|---|
| 181 | + cancelledBy = null; |
|---|
| 170 | 182 | } else { |
|---|
| 171 | | - canceledBy = new User(); |
|---|
| 172 | | - canceledBy.setUsername(username); |
|---|
| 183 | + cancelledBy = new User(); |
|---|
| 184 | + cancelledBy.setUsername(username); |
|---|
| 173 | 185 | } |
|---|
| 174 | 186 | } |
|---|
| 175 | 187 | |
|---|
| .. | .. |
|---|
| 229 | 241 | this.id = id; |
|---|
| 230 | 242 | } |
|---|
| 231 | 243 | |
|---|
| 232 | | - public User getCanceledBy() { |
|---|
| 233 | | - return canceledBy; |
|---|
| 244 | + public User getCancelledBy() { |
|---|
| 245 | + return cancelledBy; |
|---|
| 234 | 246 | } |
|---|
| 235 | 247 | |
|---|
| 236 | | - public void setCanceledBy(User canceledBy) { |
|---|
| 237 | | - this.canceledBy = canceledBy; |
|---|
| 248 | + public void setCancelledBy(User cancelledBy) { |
|---|
| 249 | + this.cancelledBy = cancelledBy; |
|---|
| 238 | 250 | } |
|---|
| 239 | 251 | |
|---|
| 240 | 252 | public Date getLastAccessTimestamp() { |
|---|
| .. | .. |
|---|
| 251 | 263 | |
|---|
| 252 | 264 | public void setRequestData(String requestData) { |
|---|
| 253 | 265 | this.requestData = requestData; |
|---|
| 266 | + this.reqDataHash = BlockedRequest.generateHash(this.requestData); |
|---|
| 254 | 267 | } |
|---|
| 255 | 268 | |
|---|
| 256 | 269 | public String getLicenseData() { |
|---|
| .. | .. |
|---|
| 283 | 296 | |
|---|
| 284 | 297 | public void setExpirationDate(Date expirationDate) { |
|---|
| 285 | 298 | this.expirationDate = expirationDate; |
|---|
| 299 | + } |
|---|
| 300 | + |
|---|
| 301 | + public String getReqDataHash() { |
|---|
| 302 | + return reqDataHash; |
|---|
| 286 | 303 | } |
|---|
| 287 | 304 | |
|---|
| 288 | 305 | public static class Action { |
|---|
| .. | .. |
|---|
| 323 | 340 | } |
|---|
| 324 | 341 | } |
|---|
| 325 | 342 | |
|---|
| 326 | | - public License findLicenseByRequestData(String requestData, EntityManager em) { |
|---|
| 343 | + public static License findLicenseByRequestData(String requestData, EntityManager em) throws SeCurisServiceException { |
|---|
| 327 | 344 | TypedQuery<License> query = em.createNamedQuery("list-active-licenses-by-req-data", License.class); |
|---|
| 328 | 345 | query.setParameter("hash", BlockedRequest.generateHash(requestData)); |
|---|
| 329 | | - return null; |
|---|
| 346 | + try { |
|---|
| 347 | + return query.getSingleResult(); |
|---|
| 348 | + } catch (NonUniqueResultException e) { |
|---|
| 349 | + LOG.error("There are more than 1 active license for request data: {}\nHash: {}", requestData, BlockedRequest.generateHash(requestData)); |
|---|
| 350 | + throw new SeCurisServiceException(ErrorCodes.DUPLICATED_REQUEST_DATA, "There are more than 1 active license for request data hash: " |
|---|
| 351 | + + BlockedRequest.generateHash(requestData)); |
|---|
| 352 | + } |
|---|
| 330 | 353 | } |
|---|
| 331 | 354 | |
|---|
| 332 | | - public String getReqDataHash() { |
|---|
| 333 | | - return reqDataHash; |
|---|
| 334 | | - } |
|---|
| 335 | | - |
|---|
| 336 | | - public void setReqDataHash(String reqDataHash) { |
|---|
| 337 | | - this.reqDataHash = reqDataHash; |
|---|
| 338 | | - } |
|---|
| 339 | 355 | } |
|---|