You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by GitBox <gi...@apache.org> on 2022/04/25 14:07:39 UTC

[GitHub] [unomi] dgriffon commented on a diff in pull request #407: UNOMI-562

dgriffon commented on code in PR #407:
URL: https://github.com/apache/unomi/pull/407#discussion_r857667939


##########
rest/src/main/java/org/apache/unomi/rest/deserializers/ContextRequestDeserializer.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.unomi.rest.deserializers;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.apache.unomi.api.ContextRequest;
+import org.apache.unomi.api.Event;
+import org.apache.unomi.api.Profile;
+import org.apache.unomi.api.services.PersonalizationService;
+import org.apache.unomi.api.services.SchemaRegistry;
+
+import javax.ws.rs.BadRequestException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Custom deserializer for ContextRequest that do validate the object using JSon Schema

Review Comment:
   done



##########
rest/src/main/java/org/apache/unomi/rest/deserializers/ContextRequestDeserializer.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.unomi.rest.deserializers;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.apache.unomi.api.ContextRequest;
+import org.apache.unomi.api.Event;
+import org.apache.unomi.api.Profile;
+import org.apache.unomi.api.services.PersonalizationService;
+import org.apache.unomi.api.services.SchemaRegistry;
+
+import javax.ws.rs.BadRequestException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Custom deserializer for ContextRequest that do validate the object using JSon Schema
+ */
+public class ContextRequestDeserializer extends StdDeserializer<ContextRequest> {
+
+    private final SchemaRegistry schemaRegistry;
+
+    public ContextRequestDeserializer(SchemaRegistry schemaRegistry) {
+        this(null,  schemaRegistry);
+    }
+
+    public ContextRequestDeserializer(Class<ContextRequest> vc, SchemaRegistry schemaRegistry) {
+        super(vc);
+        this.schemaRegistry = schemaRegistry;
+    }
+
+    @Override
+    public ContextRequest deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException {
+        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
+        // Validate schema on it
+        if (schemaRegistry.isValid(node, "https://unomi.apache.org/schemas/json/contextrequest/1-0-0")) {
+            ContextRequest cr = new ContextRequest();
+            if (node.get("requireSegments") != null) {
+                cr.setRequireSegments(node.get("requireSegments").booleanValue());
+            }
+            final JsonNode requiredProfileProperties = node.get("requiredProfileProperties");
+            if (requiredProfileProperties instanceof ArrayNode) {
+                List<String> profileProperties = new ArrayList<>();
+                requiredProfileProperties.elements().forEachRemaining(el -> profileProperties.add(el.textValue()));
+                cr.setRequiredProfileProperties(profileProperties);
+            }
+            final JsonNode requiredSessionPropertiesNode = node.get("requiredSessionProperties");
+            if (requiredSessionPropertiesNode instanceof ArrayNode) {
+                List<String> requiredSessionProperties = new ArrayList<>();
+                requiredSessionPropertiesNode.elements().forEachRemaining(el -> requiredSessionProperties.add(el.textValue()));
+                cr.setRequiredProfileProperties(requiredSessionProperties);
+            }
+            if (node.get("requireScores") != null) {
+                cr.setRequireScores(node.get("requireScores").booleanValue());
+            }
+            final JsonNode eventsNode = node.get("events");
+            if (eventsNode instanceof ArrayNode) {
+                ArrayNode events = (ArrayNode) eventsNode;
+                List<Event> filteredEvents = new ArrayList<>();
+                for (JsonNode event : events) {
+                    if (schemaRegistry.isValid(event, "https://unomi.apache.org/schemas/json/events/" + event.get("eventType").textValue() + "/1-0-0")) {
+                        filteredEvents.add(jsonParser.getCodec().treeToValue(event, Event.class));
+                    }
+                }
+                cr.setEvents(filteredEvents);
+            }
+            final JsonNode filtersNode = node.get("filters");
+            if (filtersNode instanceof ArrayNode) {
+                ArrayNode filters = (ArrayNode) filtersNode;
+                List<PersonalizationService.PersonalizedContent> f = new ArrayList<>();
+                filters.elements().forEachRemaining(el -> {
+                    try {
+                        jsonParser.getCodec().treeToValue(el, PersonalizationService.PersonalizedContent.class);
+                    } catch (JsonProcessingException e) {
+                        // Unable to deserialize, ignore the entry
+                    }
+                });
+                cr.setFilters(f);
+            }
+            final JsonNode personalizationsNode = node.get("personalizations");
+            if (personalizationsNode instanceof ArrayNode) {
+                ArrayNode personalizations = (ArrayNode) personalizationsNode;
+                List<PersonalizationService.PersonalizationRequest> p = new ArrayList<>();
+                personalizations.elements().forEachRemaining(el -> {
+                    try {
+                        jsonParser.getCodec().treeToValue(el, PersonalizationService.PersonalizationRequest.class);

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@unomi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org