From 7beb081a3076c6d748b452887625d66bac11b09c Mon Sep 17 00:00:00 2001 From: Stefan Wiedemann Date: Fri, 21 Jun 2024 13:40:58 +0200 Subject: [PATCH] propepr wrapping --- .../java/org/fiware/odrl/BundleResource.java | 2 +- .../java/org/fiware/odrl/PolicyResource.java | 8 ++++---- .../org/fiware/odrl/mapping/EntityMapper.java | 17 ++++++++++------- .../org/fiware/odrl/persistence/Policy.java | 13 +++++++++++++ .../fiware/odrl/persistence/PolicyEntity.java | 4 ++-- .../java/org/fiware/odrl/rego/OdrlPolicy.java | 7 +------ .../java/org/fiware/odrl/rego/RegoPolicy.java | 7 +------ 7 files changed, 32 insertions(+), 26 deletions(-) create mode 100644 src/main/java/org/fiware/odrl/persistence/Policy.java diff --git a/src/main/java/org/fiware/odrl/BundleResource.java b/src/main/java/org/fiware/odrl/BundleResource.java index a182463..782d8ed 100644 --- a/src/main/java/org/fiware/odrl/BundleResource.java +++ b/src/main/java/org/fiware/odrl/BundleResource.java @@ -87,7 +87,7 @@ public Response getPolicies() { String mainPolicy = getMainPolicy(policies); var toZip = policies.entrySet().stream() - .collect(Collectors.toMap(e -> String.format("policy.%s", e.getKey()), e -> e.getValue().rego().getPolicy(), (e1, e2) -> e1)); + .collect(Collectors.toMap(e -> String.format("policy.%s", e.getKey()), e -> e.getValue().rego().policy(), (e1, e2) -> e1)); toZip.put("policy.main", mainPolicy); try { return Response.ok(zipMap(toZip, "rego", objectMapper.writeValueAsString(getManifest(toZip)))).build(); diff --git a/src/main/java/org/fiware/odrl/PolicyResource.java b/src/main/java/org/fiware/odrl/PolicyResource.java index 95dc34b..a36d50d 100644 --- a/src/main/java/org/fiware/odrl/PolicyResource.java +++ b/src/main/java/org/fiware/odrl/PolicyResource.java @@ -78,8 +78,8 @@ public Response getPolicies(Integer page, Integer pageSize) { .stream() .map(policyEntry -> new Policy() .id(policyEntry.getKey()) - .odrl(policyEntry.getValue().odrl().getPolicy()) - .rego(policyEntry.getValue().rego().getPolicy())).toList(); + .odrl(policyEntry.getValue().odrl().policy()) + .rego(policyEntry.getValue().rego().policy())).toList(); return Response.ok(policyList).build(); } @@ -91,8 +91,8 @@ public Response getPolicyById(String id) { .getPolicy(id) .map(pw -> new Policy() .id(id) - .odrl(pw.odrl().getPolicy()) - .rego(pw.rego().getPolicy())) + .odrl(pw.odrl().policy()) + .rego(pw.rego().policy())) .map(Response::ok) .map(Response.ResponseBuilder::build) .orElse(Response.status(Response.Status.NOT_FOUND).build()); diff --git a/src/main/java/org/fiware/odrl/mapping/EntityMapper.java b/src/main/java/org/fiware/odrl/mapping/EntityMapper.java index 8575d1a..35767ed 100644 --- a/src/main/java/org/fiware/odrl/mapping/EntityMapper.java +++ b/src/main/java/org/fiware/odrl/mapping/EntityMapper.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; +import org.fiware.odrl.persistence.Policy; import org.fiware.odrl.persistence.PolicyEntity; import org.fiware.odrl.rego.OdrlPolicy; import org.fiware.odrl.rego.PolicyWrapper; @@ -19,18 +20,20 @@ public class EntityMapper { private ObjectMapper objectMapper; public PolicyEntity map(String id, PolicyWrapper policyWrapper) { + Policy rego = new Policy(); + rego.setPolicy(policyWrapper.rego().policy()); + Policy odrl = new Policy(); + odrl.setPolicy(policyWrapper.odrl().policy()); PolicyEntity policy = new PolicyEntity(); policy.setPolicyId(id); - policy.setRego(policyWrapper.rego()); - policy.setOdrl(policyWrapper.odrl()); + policy.setRego(rego); + policy.setOdrl(odrl); return policy; } public PolicyWrapper map(PolicyEntity policyEntity) { - try { - return new PolicyWrapper(new OdrlPolicy(objectMapper.writeValueAsString(policyEntity.getOdrl())), new RegoPolicy(objectMapper.writeValueAsString(policyEntity.getOdrl()))); - } catch (JsonProcessingException e) { - throw new RuntimeException("Was not able to read the policy from the db.", e); - } + return new PolicyWrapper( + new OdrlPolicy(policyEntity.getOdrl().getPolicy()), + new RegoPolicy(policyEntity.getRego().getPolicy())); } } diff --git a/src/main/java/org/fiware/odrl/persistence/Policy.java b/src/main/java/org/fiware/odrl/persistence/Policy.java new file mode 100644 index 0000000..256bb12 --- /dev/null +++ b/src/main/java/org/fiware/odrl/persistence/Policy.java @@ -0,0 +1,13 @@ +package org.fiware.odrl.persistence; + +import io.quarkus.runtime.annotations.RegisterForReflection; +import lombok.Data; + +/** + * @author Stefan Wiedemann + */ +@RegisterForReflection +@Data +public class Policy { + private String policy; +} diff --git a/src/main/java/org/fiware/odrl/persistence/PolicyEntity.java b/src/main/java/org/fiware/odrl/persistence/PolicyEntity.java index 8ebf019..b1c9fdd 100644 --- a/src/main/java/org/fiware/odrl/persistence/PolicyEntity.java +++ b/src/main/java/org/fiware/odrl/persistence/PolicyEntity.java @@ -23,10 +23,10 @@ public class PolicyEntity extends PanacheEntity { private String policyId; @JdbcTypeCode(SqlTypes.JSON) - private Object odrl; + private Policy odrl; @JdbcTypeCode(SqlTypes.JSON) - private Object rego; + private Policy rego; public static Optional findByPolicyId(String policyId) { return Optional.ofNullable(find("policyId", policyId).firstResult()); diff --git a/src/main/java/org/fiware/odrl/rego/OdrlPolicy.java b/src/main/java/org/fiware/odrl/rego/OdrlPolicy.java index f0ebee7..cd5343b 100644 --- a/src/main/java/org/fiware/odrl/rego/OdrlPolicy.java +++ b/src/main/java/org/fiware/odrl/rego/OdrlPolicy.java @@ -1,15 +1,10 @@ package org.fiware.odrl.rego; import io.quarkus.runtime.annotations.RegisterForReflection; -import lombok.AllArgsConstructor; -import lombok.Data; /** * @author Stefan Wiedemann */ @RegisterForReflection -@Data -@AllArgsConstructor -public class OdrlPolicy { - private String policy; +public record OdrlPolicy(String policy) { } diff --git a/src/main/java/org/fiware/odrl/rego/RegoPolicy.java b/src/main/java/org/fiware/odrl/rego/RegoPolicy.java index 8a3d1c2..719d404 100644 --- a/src/main/java/org/fiware/odrl/rego/RegoPolicy.java +++ b/src/main/java/org/fiware/odrl/rego/RegoPolicy.java @@ -1,15 +1,10 @@ package org.fiware.odrl.rego; import io.quarkus.runtime.annotations.RegisterForReflection; -import lombok.AllArgsConstructor; -import lombok.Data; /** * @author Stefan Wiedemann */ @RegisterForReflection -@Data -@AllArgsConstructor -public class RegoPolicy { - private String policy; +public record RegoPolicy(String policy) { }