UNOMI-89 : Add an endpoint/Service for userLists metadatas
Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/22f297c0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/22f297c0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/22f297c0 Branch: refs/heads/feature-UNOMI-5-KARAF4 Commit: 22f297c0399ff53bf887293be31c1374ab7b429f Parents: 2f68791 Author: Abdelkader Midani <amid...@apache.org> Authored: Mon Mar 27 14:04:33 2017 +0200 Committer: Abdelkader Midani <amid...@apache.org> Committed: Mon Mar 27 14:04:33 2017 +0200 ---------------------------------------------------------------------- .../org/apache/unomi/api/lists/UserList.java | 55 ++++++++++++++ .../unomi/api/services/UserListService.java | 49 +++++++++++++ .../conditions/profileUserListCondition.json | 33 +++++++++ .../unomi/rest/UserListServiceEndPoint.java | 75 ++++++++++++++++++++ .../resources/OSGI-INF/blueprint/blueprint.xml | 17 +++++ .../services/services/AbstractServiceImpl.java | 68 ++++++++++++++++++ .../services/services/SegmentServiceImpl.java | 36 +--------- .../services/services/UserListServiceImpl.java | 67 +++++++++++++++++ .../resources/OSGI-INF/blueprint/blueprint.xml | 8 +++ 9 files changed, 373 insertions(+), 35 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/22f297c0/api/src/main/java/org/apache/unomi/api/lists/UserList.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/unomi/api/lists/UserList.java b/api/src/main/java/org/apache/unomi/api/lists/UserList.java new file mode 100644 index 0000000..d463a58 --- /dev/null +++ b/api/src/main/java/org/apache/unomi/api/lists/UserList.java @@ -0,0 +1,55 @@ +/* + * 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.api.lists; + +import org.apache.unomi.api.Item; +import org.apache.unomi.api.Metadata; +import org.apache.unomi.api.MetadataItem; + +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Created by amidani on 24/03/2017. + */ +@XmlRootElement +public class UserList extends MetadataItem{ + + /** + * The Segment ITEM_TYPE. + * + * @see Item for a discussion of ITEM_TYPE + */ + public static final String ITEM_TYPE = "userList"; + + private static final long serialVersionUID = -1384533444875433496L; + + /** + * Instantiates a new UserList. + */ + public UserList() {} + + /** + * Instantiates a new UserList with the specified metadata. + * + * @param metadata the metadata + */ + public UserList(Metadata metadata) { + super(metadata); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/22f297c0/api/src/main/java/org/apache/unomi/api/services/UserListService.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/unomi/api/services/UserListService.java b/api/src/main/java/org/apache/unomi/api/services/UserListService.java new file mode 100644 index 0000000..04d109a --- /dev/null +++ b/api/src/main/java/org/apache/unomi/api/services/UserListService.java @@ -0,0 +1,49 @@ +/* + * 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.api.services; + +import org.apache.unomi.api.Metadata; +import org.apache.unomi.api.PartialList; +import org.apache.unomi.api.lists.UserList; + +import java.util.List; + +/** + * Created by amidani on 24/03/2017. + */ +public interface UserListService { + /** + * + * @return a {@link List} of {@link UserList} + */ + List<UserList> getAllUserLists(); + + /** + * Retrieves user lists metadatas, ordered according to the specified {@code sortBy} String and and paged: only {@code size} of them are retrieved, starting with the {@code + * offset}-th one. + * + * @param offset zero or a positive integer specifying the position of the first element in the total ordered collection of matching elements + * @param size a positive integer specifying how many matching elements should be retrieved or {@code -1} if all of them should be retrieved + * @param sortBy an optional ({@code null} if no sorting is required) String of comma ({@code ,}) separated property names on which ordering should be performed, ordering elements according to the property order in the + * String, considering each in turn and moving on to the next one in case of equality of all preceding ones. Each property name is optionally followed by + * a column ({@code :}) and an order specifier: {@code asc} or {@code desc}. + * @return a {@link PartialList} of {@link UserList} metadata + */ + public PartialList<Metadata> getUserListMetadatas(int offset, int size, String sortBy); + +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/22f297c0/plugins/baseplugin/src/main/resources/META-INF/cxs/conditions/profileUserListCondition.json ---------------------------------------------------------------------- diff --git a/plugins/baseplugin/src/main/resources/META-INF/cxs/conditions/profileUserListCondition.json b/plugins/baseplugin/src/main/resources/META-INF/cxs/conditions/profileUserListCondition.json new file mode 100644 index 0000000..221f5f9 --- /dev/null +++ b/plugins/baseplugin/src/main/resources/META-INF/cxs/conditions/profileUserListCondition.json @@ -0,0 +1,33 @@ +{ + "metadata": { + "id": "profileUserListCondition", + "name": "profileUserListCondition", + "description": "", + "tags": [ + "aggregated", + "profileCondition" + ], + "readOnly": true + }, + "parentCondition": { + "type": "profilePropertyCondition", + "parameterValues": { + "propertyName": "systemProperties.lists", + "propertyValues": "parameter::lists", + "comparisonOperator": "parameter::matchType" + } + }, + + "parameters": [ + { + "id": "lists", + "type": "string", + "multivalued": true + }, + { + "id": "matchType", + "type": "string", + "multivalued": false + } + ] +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/22f297c0/rest/src/main/java/org/apache/unomi/rest/UserListServiceEndPoint.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/apache/unomi/rest/UserListServiceEndPoint.java b/rest/src/main/java/org/apache/unomi/rest/UserListServiceEndPoint.java new file mode 100644 index 0000000..ee27411 --- /dev/null +++ b/rest/src/main/java/org/apache/unomi/rest/UserListServiceEndPoint.java @@ -0,0 +1,75 @@ +/* + * 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; + +/** + * Created by amidani on 24/03/2017. + */ + +import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing; +import org.apache.unomi.api.Metadata; +import org.apache.unomi.api.lists.UserList; +import org.apache.unomi.api.services.UserListService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.jws.WebMethod; +import javax.jws.WebService; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import java.util.List; + +/** + * A JAX-RS endpoint to manage {@link UserList}s. + */ +@WebService +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +@CrossOriginResourceSharing( + allowAllOrigins = true, + allowCredentials = true +) +public class UserListServiceEndPoint { + + private static final Logger logger = LoggerFactory.getLogger(UserListServiceEndPoint.class.getName()); + + private UserListService userListService; + + public UserListServiceEndPoint() { + logger.info("Initializing user lists service endpoint..."); + } + + @WebMethod(exclude=true) + public void setUserListService(UserListService userListService) { + this.userListService = userListService; + } + + /** + * Retrieves the 50 first {@link UserList} metadatas. + * + * @return a List of the 50 first {@link UserList} metadata + */ + @GET + @Path("/") + public List<Metadata> getUserListsMetadatas() { + return userListService.getUserListMetadatas(0, 50, null).getList(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/22f297c0/rest/src/main/resources/OSGI-INF/blueprint/blueprint.xml ---------------------------------------------------------------------- diff --git a/rest/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/rest/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 0c81d6e..6c43256 100644 --- a/rest/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/rest/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -70,6 +70,18 @@ </jaxrs:serviceBeans> </jaxrs:server> + <jaxrs:server address="/userList" id="restUserListService"> + <jaxrs:providers> + <ref component-id="jaxb-provider"/> + <ref component-id="cors-filter"/> + <ref component-id="jaas-filter"/> + </jaxrs:providers> + + <jaxrs:serviceBeans> + <ref component-id="userListServiceEndPoint"/> + </jaxrs:serviceBeans> + </jaxrs:server> + <jaxrs:server address="/scoring" id="restScoringService"> <jaxrs:providers> <ref component-id="jaxb-provider"/> @@ -167,6 +179,7 @@ </jaxrs:server> <reference id="segmentService" interface="org.apache.unomi.api.services.SegmentService"/> + <reference id="userListService" interface="org.apache.unomi.api.services.UserListService"/> <reference id="definitionsService" interface="org.apache.unomi.api.services.DefinitionsService"/> <reference id="goalsService" interface="org.apache.unomi.api.services.GoalsService"/> <reference id="rulesService" interface="org.apache.unomi.api.services.RulesService"/> @@ -179,6 +192,10 @@ <property name="segmentService" ref="segmentService"/> </bean> + <bean id="userListServiceEndPoint" class="org.apache.unomi.rest.UserListServiceEndPoint"> + <property name="userListService" ref="userListService"/> + </bean> + <bean id="scoringServiceEndPoint" class="org.apache.unomi.rest.ScoringServiceEndPoint"> <property name="segmentService" ref="segmentService"/> </bean> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/22f297c0/services/src/main/java/org/apache/unomi/services/services/AbstractServiceImpl.java ---------------------------------------------------------------------- diff --git a/services/src/main/java/org/apache/unomi/services/services/AbstractServiceImpl.java b/services/src/main/java/org/apache/unomi/services/services/AbstractServiceImpl.java new file mode 100644 index 0000000..c9f967d --- /dev/null +++ b/services/src/main/java/org/apache/unomi/services/services/AbstractServiceImpl.java @@ -0,0 +1,68 @@ +/* + * 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.services.services; + +import org.apache.unomi.api.Metadata; +import org.apache.unomi.api.MetadataItem; +import org.apache.unomi.api.PartialList; +import org.apache.unomi.api.query.Query; +import org.apache.unomi.api.services.DefinitionsService; +import org.apache.unomi.persistence.spi.PersistenceService; + +import java.util.LinkedList; +import java.util.List; + +/** + * Created by amidani on 24/03/2017. + */ +public abstract class AbstractServiceImpl { + + PersistenceService persistenceService; + + DefinitionsService definitionsService; + + public void setPersistenceService(PersistenceService persistenceService) { + this.persistenceService = persistenceService; + } + + public void setDefinitionsService(DefinitionsService definitionsService) { + this.definitionsService = definitionsService; + } + + <T extends MetadataItem> PartialList<Metadata> getMetadatas(int offset, int size, String sortBy, Class<T> clazz) { + PartialList<T> items = persistenceService.getAllItems(clazz, offset, size, sortBy); + List<Metadata> details = new LinkedList<>(); + for (T definition : items.getList()) { + details.add(definition.getMetadata()); + } + return new PartialList<>(details, items.getOffset(), items.getPageSize(), items.getTotalSize()); + } + + <T extends MetadataItem> PartialList<Metadata> getMetadatas(Query query, Class<T> clazz) { + if (query.isForceRefresh()) { + persistenceService.refresh(); + } + definitionsService.resolveConditionType(query.getCondition()); + PartialList<T> items = persistenceService.query(query.getCondition(), query.getSortby(), clazz, query.getOffset(), query.getLimit()); + List<Metadata> details = new LinkedList<>(); + for (T definition : items.getList()) { + details.add(definition.getMetadata()); + } + return new PartialList<>(details, items.getOffset(), items.getPageSize(), items.getTotalSize()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/22f297c0/services/src/main/java/org/apache/unomi/services/services/SegmentServiceImpl.java ---------------------------------------------------------------------- diff --git a/services/src/main/java/org/apache/unomi/services/services/SegmentServiceImpl.java b/services/src/main/java/org/apache/unomi/services/services/SegmentServiceImpl.java index 4367b4d..979c3cb 100644 --- a/services/src/main/java/org/apache/unomi/services/services/SegmentServiceImpl.java +++ b/services/src/main/java/org/apache/unomi/services/services/SegmentServiceImpl.java @@ -45,16 +45,12 @@ import java.net.URL; import java.security.MessageDigest; import java.util.*; -public class SegmentServiceImpl implements SegmentService, SynchronousBundleListener { +public class SegmentServiceImpl extends AbstractServiceImpl implements SegmentService, SynchronousBundleListener { private static final Logger logger = LoggerFactory.getLogger(SegmentServiceImpl.class.getName()); private BundleContext bundleContext; - private PersistenceService persistenceService; - - private DefinitionsService definitionsService; - private EventService eventService; private RulesService rulesService; @@ -105,14 +101,6 @@ public class SegmentServiceImpl implements SegmentService, SynchronousBundleList this.bundleContext = bundleContext; } - public void setPersistenceService(PersistenceService persistenceService) { - this.persistenceService = persistenceService; - } - - public void setDefinitionsService(DefinitionsService definitionsService) { - this.definitionsService = definitionsService; - } - public void setEventService(EventService eventService) { this.eventService = eventService; } @@ -1025,26 +1013,4 @@ public class SegmentServiceImpl implements SegmentService, SynchronousBundleList this.taskExecutionPeriod = taskExecutionPeriod; } - private <T extends MetadataItem> PartialList<Metadata> getMetadatas(int offset, int size, String sortBy, Class<T> clazz) { - PartialList<T> items = persistenceService.getAllItems(clazz, offset, size, sortBy); - List<Metadata> details = new LinkedList<>(); - for (T definition : items.getList()) { - details.add(definition.getMetadata()); - } - return new PartialList<>(details, items.getOffset(), items.getPageSize(), items.getTotalSize()); - } - - private <T extends MetadataItem> PartialList<Metadata> getMetadatas(Query query, Class<T> clazz) { - if (query.isForceRefresh()) { - persistenceService.refresh(); - } - definitionsService.resolveConditionType(query.getCondition()); - PartialList<T> items = persistenceService.query(query.getCondition(), query.getSortby(), clazz, query.getOffset(), query.getLimit()); - List<Metadata> details = new LinkedList<>(); - for (T definition : items.getList()) { - details.add(definition.getMetadata()); - } - return new PartialList<>(details, items.getOffset(), items.getPageSize(), items.getTotalSize()); - } - } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/22f297c0/services/src/main/java/org/apache/unomi/services/services/UserListServiceImpl.java ---------------------------------------------------------------------- diff --git a/services/src/main/java/org/apache/unomi/services/services/UserListServiceImpl.java b/services/src/main/java/org/apache/unomi/services/services/UserListServiceImpl.java new file mode 100644 index 0000000..e3156ea --- /dev/null +++ b/services/src/main/java/org/apache/unomi/services/services/UserListServiceImpl.java @@ -0,0 +1,67 @@ +/* + * 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.services.services; + +import org.apache.unomi.api.Metadata; +import org.apache.unomi.api.PartialList; +import org.apache.unomi.api.lists.UserList; +import org.apache.unomi.api.services.UserListService; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleEvent; +import org.osgi.framework.SynchronousBundleListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +/** + * Created by amidani on 24/03/2017. + */ +public class UserListServiceImpl extends AbstractServiceImpl implements UserListService, SynchronousBundleListener { + + private static final Logger logger = LoggerFactory.getLogger(UserListServiceImpl.class.getName()); + + private BundleContext bundleContext; + + public void setBundleContext(BundleContext bundleContext) { + this.bundleContext = bundleContext; + } + + public void postConstruct() { + logger.debug("postConstruct {" + bundleContext.getBundle() + "}"); + bundleContext.addBundleListener(this); + logger.info("User list service initialized."); + } + + public void preDestroy() { + bundleContext.removeBundleListener(this); + logger.info("User list service shutdown."); + } + + public List<UserList> getAllUserLists() { + return persistenceService.getAllItems(UserList.class); + } + + public PartialList<Metadata> getUserListMetadatas(int offset, int size, String sortBy) { + return getMetadatas(offset, size, sortBy, UserList.class); + } + + + @Override + public void bundleChanged(BundleEvent bundleEvent) { } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/22f297c0/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml ---------------------------------------------------------------------- diff --git a/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 23af746..c5dfdd6 100644 --- a/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -127,6 +127,14 @@ </bean> <service id="segmentService" ref="segmentServiceImpl" auto-export="interfaces"/> + <bean id="userListServiceImpl" class="org.apache.unomi.services.services.UserListServiceImpl" + init-method="postConstruct" destroy-method="preDestroy"> + <property name="persistenceService" ref="persistenceService"/> + <property name="definitionsService" ref="definitionsServiceImpl"/> + <property name="bundleContext" ref="blueprintBundleContext"/> + </bean> + <service id="userListService" ref="userListServiceImpl" auto-export="interfaces"/> + <bean id="profileServiceImpl" class="org.apache.unomi.services.services.ProfileServiceImpl" init-method="postConstruct" destroy-method="preDestroy"> <property name="persistenceService" ref="persistenceService"/>