http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/apache/unomi/rest/RESTActionType.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/apache/unomi/rest/RESTActionType.java b/rest/src/main/java/org/apache/unomi/rest/RESTActionType.java new file mode 100644 index 0000000..5e73a0f --- /dev/null +++ b/rest/src/main/java/org/apache/unomi/rest/RESTActionType.java @@ -0,0 +1,74 @@ +/* + * 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; + +import org.apache.unomi.api.actions.ActionType; + +import java.util.Collection; +import java.util.List; + +/** + * A representation of an {@link ActionType} better suited for definitions. + */ +public class RESTActionType { + private String id; + private String name; + private String description; + private Collection<String> tags; + private List<RESTParameter> parameters; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Collection<String> getTags() { + return tags; + } + + public void setTags(Collection<String> tags) { + this.tags = tags; + } + + public List<RESTParameter> getParameters() { + return parameters; + } + + public void setParameters(List<RESTParameter> parameters) { + this.parameters = parameters; + } +}
http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/apache/unomi/rest/RESTConditionType.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/apache/unomi/rest/RESTConditionType.java b/rest/src/main/java/org/apache/unomi/rest/RESTConditionType.java new file mode 100644 index 0000000..e37156f --- /dev/null +++ b/rest/src/main/java/org/apache/unomi/rest/RESTConditionType.java @@ -0,0 +1,79 @@ +/* + * 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; + +import org.apache.unomi.api.conditions.ConditionType; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.TreeSet; + +/** + * A representation of a {@link ConditionType} better suited for definitions. + */ +public class RESTConditionType { + private String id; + private String name; + private String description; + private Collection<String> tags = new TreeSet<String>(); + private List<RESTParameter> parameters = new ArrayList<RESTParameter>(); + + public RESTConditionType() { + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Collection<String> getTags() { + return tags; + } + + public void setTags(Collection<String> tags) { + this.tags = tags; + } + + public List<RESTParameter> getParameters() { + return parameters; + } + + public void setParameters(List<RESTParameter> parameters) { + this.parameters = parameters; + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/apache/unomi/rest/RESTParameter.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/apache/unomi/rest/RESTParameter.java b/rest/src/main/java/org/apache/unomi/rest/RESTParameter.java new file mode 100644 index 0000000..0cf9cde --- /dev/null +++ b/rest/src/main/java/org/apache/unomi/rest/RESTParameter.java @@ -0,0 +1,76 @@ +/* + * 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; + +import org.apache.unomi.api.Parameter; +import org.apache.unomi.api.conditions.initializers.ChoiceListValue; + +import java.util.ArrayList; +import java.util.List; + +/** + * A representation of a {@link Parameter} better suited for definitions. + */ +public class RESTParameter { + private String id; + private String type; + private boolean multivalued = false; + private String defaultValue = null; + private List<ChoiceListValue> choiceListValues = new ArrayList<ChoiceListValue>(); + + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public boolean isMultivalued() { + return multivalued; + } + + public void setMultivalued(boolean multivalued) { + this.multivalued = multivalued; + } + + public String getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + + public List<ChoiceListValue> getChoiceListValues() { + return choiceListValues; + } + + public void setChoiceListValues(List<ChoiceListValue> choiceListValues) { + this.choiceListValues = choiceListValues; + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/apache/unomi/rest/RESTTag.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/apache/unomi/rest/RESTTag.java b/rest/src/main/java/org/apache/unomi/rest/RESTTag.java new file mode 100644 index 0000000..07cf11c --- /dev/null +++ b/rest/src/main/java/org/apache/unomi/rest/RESTTag.java @@ -0,0 +1,84 @@ +/* + * 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; + +import org.apache.unomi.api.Tag; + +import java.util.Collection; +import java.util.TreeSet; + +/** + * A representation of a {@link Tag} better suited for definitions. + */ +public class RESTTag { + private String id; + private String name; + private String description; + private String parentId; + private double rank = 0.0; + private Collection<RESTTag> subTags = new TreeSet<RESTTag>(); + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getParentId() { + return parentId; + } + + public void setParentId(String parentId) { + this.parentId = parentId; + } + + public double getRank() { + return rank; + } + + public void setRank(double rank) { + this.rank = rank; + } + + public Collection<RESTTag> getSubTags() { + return subTags; + } + + public void setSubTags(Collection<RESTTag> subTags) { + this.subTags = subTags; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/apache/unomi/rest/RESTValueType.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/apache/unomi/rest/RESTValueType.java b/rest/src/main/java/org/apache/unomi/rest/RESTValueType.java new file mode 100644 index 0000000..95edadf --- /dev/null +++ b/rest/src/main/java/org/apache/unomi/rest/RESTValueType.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.rest; + +import org.apache.unomi.api.ValueType; + +import java.util.Collection; + +/** + * A representation of a {@link ValueType} better suited for definitions. + */ +public class RESTValueType { + + private String id; + private String name; + private String description; + private Collection<RESTTag> tags; + + public RESTValueType() { + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Collection<RESTTag> getTags() { + return tags; + } + + public void setTags(Collection<RESTTag> tags) { + this.tags = tags; + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/apache/unomi/rest/ResourceBundleHelper.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/apache/unomi/rest/ResourceBundleHelper.java b/rest/src/main/java/org/apache/unomi/rest/ResourceBundleHelper.java new file mode 100644 index 0000000..760a27e --- /dev/null +++ b/rest/src/main/java/org/apache/unomi/rest/ResourceBundleHelper.java @@ -0,0 +1,109 @@ +/* + * 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; + +import org.apache.unomi.api.PluginType; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.wiring.BundleWiring; + +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; +import java.util.regex.Pattern; + +public class ResourceBundleHelper { + + private static final Pattern COMMA = Pattern.compile(",", Pattern.LITERAL); + + private static final String RESOURCE_BUNDLE = "messages"; + + private BundleContext bundleContext; + + private ResourceBundle getBundle(String lang, Bundle bundle, ClassLoader loader) { + Locale locale = getLocale(lang); + try { + ResourceBundle resourceBundle = ResourceBundle.getBundle(RESOURCE_BUNDLE, locale, loader); + if (resourceBundle != null && locale.equals(resourceBundle.getLocale())) { + return resourceBundle; + } + } catch (MissingResourceException e) { + // continue with next language + } + + if (locale.getCountry().length() > 0) { + // try the locale without the country + return getBundle(locale.getLanguage(), bundle, loader); + } + + return null; + } + + private Locale getLocale(String lang) { + int i = lang.indexOf(';'); + if (i > -1) { + lang = lang.substring(0, i); + } + return Locale.forLanguageTag(lang); + } + + public ResourceBundle getResourceBundle(PluginType object, String language) { + ResourceBundle resourceBundle = null; + + Bundle bundle = bundleContext.getBundle(object.getPluginId()); + ClassLoader loader = bundle.adapt(BundleWiring.class).getClassLoader(); + + if (language != null) { + if (language.indexOf(',') != -1) { + String[] langs = COMMA.split(language); + for (String lang : langs) { + resourceBundle = getBundle(lang, bundle, loader); + if (resourceBundle != null) { + break; + } + } + } else { + resourceBundle = getBundle(language, bundle, loader); + } + } + if (resourceBundle == null) { + try { + return ResourceBundle.getBundle(RESOURCE_BUNDLE, Locale.ENGLISH, loader); + } catch (MissingResourceException e) { + // ignore + } + } + + return resourceBundle; + } + + public String getResourceBundleValue(ResourceBundle bundle, String nameKey) { + try { + if (bundle != null) { + return bundle.getString(nameKey); + } + } catch (MissingResourceException e) { + // Continue + } + return "???" + nameKey + "???"; + } + + public void setBundleContext(BundleContext bundleContext) { + this.bundleContext = bundleContext; + } +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/apache/unomi/rest/RulesServiceEndPoint.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/apache/unomi/rest/RulesServiceEndPoint.java b/rest/src/main/java/org/apache/unomi/rest/RulesServiceEndPoint.java new file mode 100644 index 0000000..c26b209 --- /dev/null +++ b/rest/src/main/java/org/apache/unomi/rest/RulesServiceEndPoint.java @@ -0,0 +1,126 @@ +/* + * 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; + +import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing; +import org.apache.unomi.api.Metadata; +import org.apache.unomi.api.PartialList; +import org.apache.unomi.api.query.Query; +import org.apache.unomi.api.rules.Rule; +import org.apache.unomi.api.services.RulesService; + +import javax.jws.WebMethod; +import javax.jws.WebService; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import java.util.Set; + +/** + * A JAX-RS endpoint to manage {@link Rule}s. + */ +@WebService +@Produces(MediaType.APPLICATION_JSON) +@CrossOriginResourceSharing( + allowAllOrigins = true, + allowCredentials = true +) +public class RulesServiceEndPoint { + + private RulesService rulesService; + + public RulesServiceEndPoint() { + System.out.println("Initializing rule service endpoint..."); + } + + @WebMethod(exclude=true) + public void setRulesService(RulesService rulesService) { + this.rulesService = rulesService; + } + + /** + * Retrieves the metadata for all known rules. + * + * @return the Set of known metadata + */ + @GET + @Path("/") + public Set<Metadata> getRuleMetadatas() { + return rulesService.getRuleMetadatas(); + } + + /** + * Persists the specified rule to the context server. + * + * @param rule the rule to be persisted + */ + @POST + @Path("/") + public void setRule(Rule rule) { + rulesService.setRule(rule); + } + + /** + * Retrieves rule metadatas for rules matching the specified {@link Query}. + * + * @param query the query the rules which metadata we want to retrieve must match + * @return a {@link PartialList} of rules metadata for the rules matching the specified query + */ + @POST + @Path("/query") + public PartialList<Metadata> getRuleMetadatas(Query query) { + return rulesService.getRuleMetadatas(query); + } + + /** + * Retrieves the rule identified by the specified identifier. + * + * @param ruleId the identifier of the rule we want to retrieve + * @return the rule identified by the specified identifier or {@code null} if no such rule exists. + */ + @GET + @Path("/{ruleId}") + public Rule getRule( @PathParam("ruleId") String ruleId) { + return rulesService.getRule(ruleId); + } + + /** + * Deletes the rule identified by the specified identifier. + * + * @param ruleId the identifier of the rule we want to delete + */ + @DELETE + @Path("/{ruleId}") + public void removeRule(@PathParam("ruleId") String ruleId) { + rulesService.removeRule(ruleId); + } + + /** + * TODO: remove + * + * @deprecated not needed anymore + */ + @GET + @Path("/resetQueries") + public void resetQueries() { + for (Metadata metadata : rulesService.getRuleMetadatas()) { + Rule r = rulesService.getRule(metadata.getId()); + rulesService.setRule(r); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/apache/unomi/rest/ScoringServiceEndPoint.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/apache/unomi/rest/ScoringServiceEndPoint.java b/rest/src/main/java/org/apache/unomi/rest/ScoringServiceEndPoint.java new file mode 100644 index 0000000..f585323 --- /dev/null +++ b/rest/src/main/java/org/apache/unomi/rest/ScoringServiceEndPoint.java @@ -0,0 +1,143 @@ +/* + * 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; + +import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing; +import org.apache.unomi.api.Item; +import org.apache.unomi.api.Metadata; +import org.apache.unomi.api.PartialList; +import org.apache.unomi.api.query.Query; +import org.apache.unomi.api.segments.Scoring; +import org.apache.unomi.api.services.SegmentService; + +import javax.jws.WebMethod; +import javax.jws.WebService; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import java.util.List; + +/** + * A JAX-RS endpoint to manage {@link Scoring}s + */ +@WebService +@Produces(MediaType.APPLICATION_JSON) +@CrossOriginResourceSharing( + allowAllOrigins = true, + allowCredentials = true +) +public class ScoringServiceEndPoint { + + private SegmentService segmentService; + + public ScoringServiceEndPoint() { + System.out.println("Initializing scoring service endpoint..."); + } + + @WebMethod(exclude = true) + public void setSegmentService(SegmentService segmentService) { + this.segmentService = segmentService; + } + + /** + * Retrieves the set of all scoring metadata. + * + * @return the set of all scoring metadata + */ + @GET + @Path("/") + public List<Metadata> getScoringMetadatas() { + return segmentService.getScoringMetadatas(0, 50, null).getList(); + } + + /** + * Retrieves the set of scoring metadata for scorings matching the specified query. + * + * @param query the query the scorings must match for their metadata to be retrieved + * @return the set of scoring metadata for scorings matching the specified query + */ + @POST + @Path("/query") + public PartialList<Metadata> getScoringMetadatas(Query query) { + return segmentService.getScoringMetadatas(query); + } + + /** + * Retrieves the scoring identified by the specified identifier. + * + * @param scoringId the identifier of the scoring to be retrieved + * @return the scoring identified by the specified identifier or {@code null} if no such scoring exists + */ + @GET + @Path("/{scoringID}") + public Scoring getScoringDefinition(@PathParam("scoringID") String scoringId) { + return segmentService.getScoringDefinition(scoringId); + } + + /** + * Persists the specified scoring in the context server. + * + * @param scoring the scoring to be persisted + */ + @POST + @Path("/") + public void setScoringDefinition(Scoring scoring) { + segmentService.setScoringDefinition(scoring); + } + + /** + * Creates a scoring with the specified scope, identifier, name and description from form-encoded data. + * + * @param scope the scope for the new scoring + * @param scoringId the identifier for the new scoring + * @param scoringName the name of the new scoring + * @param scoringDescription the description of the new scoring + * @see Item Item's description for a discussion of scope + */ + @PUT + @Path("/{scope}/{scoringID}") + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + public void createScoringDefinition(@PathParam("scope") String scope, @PathParam("scoringID") String scoringId, @FormParam("scoringName") String scoringName, @FormParam("scoringDescription") String scoringDescription) { + segmentService.createScoringDefinition(scope, scoringId, scoringName, scoringDescription); + } + + /** + * Deletes the scoring identified by the specified identifier from the context server. + * + * @param scoringId the identifier of the scoring to be deleted + */ + @DELETE + @Path("/{scoringID}") + public void removeScoringDefinition(@PathParam("scoringID") String scoringId) { + segmentService.removeScoringDefinition(scoringId); + } + + /** + * TODO: remove + * + * @deprecated not needed anymore + */ + @GET + @Path("/resetQueries") + public void resetQueries() { + for (Metadata metadata : segmentService.getScoringMetadatas(0, 50, null).getList()) { + Scoring s = segmentService.getScoringDefinition(metadata.getId()); + segmentService.setScoringDefinition(s); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/apache/unomi/rest/SegmentServiceEndPoint.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/apache/unomi/rest/SegmentServiceEndPoint.java b/rest/src/main/java/org/apache/unomi/rest/SegmentServiceEndPoint.java new file mode 100644 index 0000000..39081c6 --- /dev/null +++ b/rest/src/main/java/org/apache/unomi/rest/SegmentServiceEndPoint.java @@ -0,0 +1,191 @@ +/* + * 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; + +import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing; +import org.apache.unomi.api.Metadata; +import org.apache.unomi.api.PartialList; +import org.apache.unomi.api.Profile; +import org.apache.unomi.api.query.Query; +import org.apache.unomi.api.segments.Segment; +import org.apache.unomi.api.services.SegmentService; + +import javax.jws.WebMethod; +import javax.jws.WebService; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import java.util.List; + +/** + * A JAX-RS endpoint to manage {@link Segment}s. + */ +@WebService +@Produces(MediaType.APPLICATION_JSON) +@CrossOriginResourceSharing( + allowAllOrigins = true, + allowCredentials = true +) +public class SegmentServiceEndPoint { + + private SegmentService segmentService; + + public SegmentServiceEndPoint() { + System.out.println("Initializing segment service endpoint..."); + } + + @WebMethod(exclude=true) + public void setSegmentService(SegmentService segmentService) { + this.segmentService = segmentService; + } + + /** + * Retrieves a list of profiles matching the conditions defined by the segment identified by the specified identifier, 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 segmentId the identifier of the segment for which we want to retrieve matching profiles + * @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 profiles matching the specified segment + */ + @GET + @Path("/{segmentID}/match") + public PartialList<Profile> getMatchingIndividuals(@PathParam("segmentID") String segmentId, @QueryParam("offset") @DefaultValue("0") int offset, @QueryParam("size") @DefaultValue("50") int size, @QueryParam("sort") String sortBy) { + return segmentService.getMatchingIndividuals(segmentId, offset, size, sortBy); + } + + /** + * Retrieves the number of profiles matching the conditions defined by the segment identified by the specified identifier. + * + * @param segmentId the identifier of the segment for which we want to retrieve matching profiles + * @return the number of profiles matching the conditions defined by the segment identified by the specified identifier + */ + @GET + @Path("/{segmentID}/count") + public long getMatchingIndividualsCount(@PathParam("segmentID") String segmentId) { + return segmentService.getMatchingIndividualsCount(segmentId); + } + + /** + * Determines whether the specified profile is part of the segment identified by the specified identifier. + * + * @param profile the profile we want to check + * @param segmentId the identifier of the segment against which we want to check the profile + * @return {@code true} if the specified profile is in the specified segment, {@code false} otherwise + */ + @GET + @Path("/{segmentID}/match/{profile}") + public Boolean isProfileInSegment(@PathParam("profile") Profile profile, @PathParam("segmentID") String segmentId) { + return segmentService.isProfileInSegment(profile, segmentId); + } + + /** + * Retrieves the 50 first segment metadatas. + * + * @return a List of the 50 first segment metadata + */ + @GET + @Path("/") + public List<Metadata> getSegmentMetadatas() { + return segmentService.getSegmentMetadatas(0, 50, null).getList(); + } + + /** + * Retrieves the list of segment metadata of segments depending on the segment identified by the specified identifier. A segment is depending on another one if it includes + * that segment as part of its condition for profile matching. + * + * TODO: rename? + * + * @param segmentId the identifier of the segment which impact we want to evaluate + * @return a list of metadata of segments depending on the specified segment + */ + @GET + @Path("/{segmentID}/impacted") + public List<Metadata> getSegmentImpacted(@PathParam("segmentID") String segmentId) { + return segmentService.getImpactedSegmentMetadata(segmentId); + } + + /** + * Persists the specified segment in the context server. + * + * @param segment the segment to be persisted + */ + @POST + @Path("/") + public void setSegmentDefinition(Segment segment) { + segmentService.setSegmentDefinition(segment); + } + + /** + * Retrieves the metadata for segments matching the specified {@link Query}. + * + * @param query the query that the segments must match for their metadata to be retrieved + * @return a {@link PartialList} of segment metadata + */ + @POST + @Path("/query") + public PartialList<Metadata> getListMetadatas(Query query) { + return segmentService.getSegmentMetadatas(query); + } + + /** + * Retrieves the segment identified by the specified identifier. + * + * @param segmentId the identifier of the segment to be retrieved + * @return the segment identified by the specified identifier or {@code null} if no such segment exists + */ + @GET + @Path("/{segmentID}") + public Segment getSegmentDefinition(@PathParam("segmentID") String segmentId) { + return segmentService.getSegmentDefinition(segmentId); + } + + /** + * Removes the segment definition identified by the specified identifier. We can specify that we want the operation to be validated beforehand so that we can + * know if any other segment that might use the segment we're trying to delete as a condition might be impacted. If {@code validate} is set to {@code false}, no + * validation is performed. If set to {@code true}, we will first check if any segment depends on the one we're trying to delete and if so we will not delete the + * segment but rather return the list of the metadata of the impacted segments. If no dependents are found, then we properly delete the segment. + * + * @param segmentId the identifier of the segment we want to delete + * @param validate whether or not to perform validation + * @return a list of impacted segment metadata if any or an empty list if no such impacted segments are found or validation was skipped + */ + @DELETE + @Path("/{segmentID}") + public List<Metadata> removeSegmentDefinition(@PathParam("segmentID") String segmentId, @QueryParam("validate") boolean validate) { + return segmentService.removeSegmentDefinition(segmentId, validate); + } + + /** + * TODO: remove + * + * @deprecated not needed anymore + */ + @GET + @Path("/resetQueries") + public void resetQueries() { + for (Metadata metadata : segmentService.getSegmentMetadatas(0, 50, null).getList()) { + Segment s = segmentService.getSegmentDefinition(metadata.getId()); + segmentService.setSegmentDefinition(s); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/oasis_open/contextserver/rest/CampaignsServiceEndPoint.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/oasis_open/contextserver/rest/CampaignsServiceEndPoint.java b/rest/src/main/java/org/oasis_open/contextserver/rest/CampaignsServiceEndPoint.java deleted file mode 100644 index 4757488..0000000 --- a/rest/src/main/java/org/oasis_open/contextserver/rest/CampaignsServiceEndPoint.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * 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.oasis_open.contextserver.rest; - -import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing; -import org.oasis_open.contextserver.api.Metadata; -import org.oasis_open.contextserver.api.PartialList; -import org.oasis_open.contextserver.api.campaigns.Campaign; -import org.oasis_open.contextserver.api.campaigns.CampaignDetail; -import org.oasis_open.contextserver.api.campaigns.events.CampaignEvent; -import org.oasis_open.contextserver.api.query.Query; -import org.oasis_open.contextserver.api.services.GoalsService; - -import javax.jws.WebMethod; -import javax.jws.WebService; -import javax.ws.rs.*; -import javax.ws.rs.core.MediaType; -import java.util.Set; - -/** - * A JAX-RS endpoint to manage {@link Campaign}s and related information. - */ -@WebService -@Produces(MediaType.APPLICATION_JSON) -@CrossOriginResourceSharing( - allowAllOrigins = true, - allowCredentials = true -) -public class CampaignsServiceEndPoint { - - private GoalsService goalsService; - - public CampaignsServiceEndPoint() { - System.out.println("Initializing campaigns service endpoint..."); - } - - @WebMethod(exclude=true) - public void setGoalsService(GoalsService goalsService) { - this.goalsService = goalsService; - } - - /** - * Retrieves the set of Metadata associated with existing campaigns. - * - * @return the set of Metadata associated with existing campaigns - */ - @GET - @Path("/") - public Set<Metadata> getCampaignMetadatas() { - return goalsService.getCampaignMetadatas(); - } - - /** - * Saves the specified campaign in the context server and creates associated {@link org.oasis_open.contextserver.api.rules.Rule}s if the campaign is enabled. - * - * @param campaign the Campaign to be saved - */ - @POST - @Path("/") - public void setCampaignDefinition(Campaign campaign) { - goalsService.setCampaign(campaign); - } - - /** - * Retrieves the set of Metadata associated with existing campaign matching the specified {@link Query} - * - * @param query the Query used to filter the campagins which metadata we want to retrieve - * @return the set of Metadata associated with existing campaigns matching the specified {@link Query} - */ - @POST - @Path("/query") - public Set<Metadata> getCampaignMetadatas(Query query) { - return goalsService.getCampaignMetadatas(query); - } - - /** - * Retrieves campaign details for campaigns matching the specified query. - * - * @param query the query specifying which campaigns to retrieve - * @return a {@link PartialList} of campaign details for the campaigns matching the specified query - */ - @POST - @Path("/query/detailed") - public PartialList<CampaignDetail> getCampaignDetails(Query query) { - return goalsService.getCampaignDetails(query); - } - - /** - * Retrieves the {@link CampaignDetail} associated with the campaign identified with the specified identifier - * - * @param campaignID the identifier of the campaign for which we want to retrieve the details - * @return the CampaignDetail for the campaign identified by the specified identifier or {@code null} if no such campaign exists - */ - @GET - @Path("/{campaignID}/detailed") - public CampaignDetail getCampaignDetail(@PathParam("campaignID") String campaignID) { - return goalsService.getCampaignDetail(campaignID); - } - - /** - * Retrieves the campaign identified by the specified identifier - * - * @param campaignID the identifier of the campaign we want to retrieve - * @return the campaign associated with the specified identifier or {@code null} if no such campaign exists - */ - @GET - @Path("/{campaignID}") - public Campaign getCampaignDefinition(@PathParam("campaignID") String campaignID) { - return goalsService.getCampaign(campaignID); - } - - /** - * Removes the campaign associated with the specified identifier, also removing associated rules if needed. - * - * @param campaignID the identifier of the campaign to be removed - */ - @DELETE - @Path("/{campaignID}") - public void removeCampaignDefinition(@PathParam("campaignID") String campaignID) { - goalsService.removeCampaign(campaignID); - } - - /** - * Saves the specified campaign event in the context server. - * - * @param campaignEvent the CampaignEvent to be saved - */ - @POST - @Path("/event") - public void setCampaignEventDefinition(CampaignEvent campaignEvent) { - goalsService.setCampaignEvent(campaignEvent); - } - - /** - * Removes the campaign event associated with the specified identifier. - * - * @param campaignEventID the identifier of the campaign event to be removed - */ - @DELETE - @Path("/event/{eventId}") - public void removeCampaignEventDefinition(@PathParam("eventId") String campaignEventID) { - goalsService.removeCampaignEvent(campaignEventID); - } - - /** - * Retrieves {@link CampaignEvent}s matching the specified query. - * - * @param query the Query specifying which CampaignEvents to retrieve - * @return a {@link PartialList} of campaign events matching the specified query - */ - @POST - @Path("/events/query") - public PartialList<CampaignEvent> getCampaignEvents(Query query) { - return goalsService.getEvents(query); - } -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/oasis_open/contextserver/rest/ClusterServiceEndPoint.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/oasis_open/contextserver/rest/ClusterServiceEndPoint.java b/rest/src/main/java/org/oasis_open/contextserver/rest/ClusterServiceEndPoint.java deleted file mode 100644 index 958ed05..0000000 --- a/rest/src/main/java/org/oasis_open/contextserver/rest/ClusterServiceEndPoint.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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.oasis_open.contextserver.rest; - -import org.apache.cxf.jaxrs.ext.MessageContext; -import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing; -import org.oasis_open.contextserver.api.ClusterNode; -import org.oasis_open.contextserver.api.services.ClusterService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.jws.WebMethod; -import javax.jws.WebService; -import javax.ws.rs.*; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.List; - -/** - * A JAX-RS endpoint to access information about the context server's cluster. - */ -@WebService -@Produces(MediaType.APPLICATION_JSON) -@CrossOriginResourceSharing( - allowAllOrigins = true, - allowCredentials = true -) -public class ClusterServiceEndPoint { - private static final Logger logger = LoggerFactory.getLogger(ClusterServiceEndPoint.class.getName()); - - @Context - private MessageContext messageContext; - - private ClusterService clusterService; - - public ClusterServiceEndPoint() { - System.out.println("Initializing cluster service endpoint..."); - } - - @WebMethod(exclude = true) - public void setClusterService(ClusterService clusterService) { - this.clusterService = clusterService; - } - - @WebMethod(exclude = true) - public void setMessageContext(MessageContext messageContext) { - this.messageContext = messageContext; - } - - /** - * Retrieves the list of available nodes for this context server instance. - * - * @return a list of {@link ClusterNode} - */ - @GET - @Path("/") - public List<ClusterNode> getClusterNodes() { - return clusterService.getClusterNodes(); - } - - /** - * Removes all data before the specified date from the context server. - * - * @param date the Date before which all data needs to be removed - */ - @GET - @Path("/purge/{date}") - public void purge(@PathParam("date") String date) { - try { - clusterService.purge(new SimpleDateFormat("yyyy-MM-dd").parse(date)); - } catch (ParseException e) { - logger.error("Cannot purge",e); - } - } - - /** - * Removes all data associated with the provided scope. - * - * @param scope the scope for which we want to remove data - */ - @DELETE - @Path("{scope}") - public void deleteScopedData(@PathParam("scope") String scope) { - clusterService.purge(scope); - } -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/oasis_open/contextserver/rest/DefinitionsServiceEndPoint.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/oasis_open/contextserver/rest/DefinitionsServiceEndPoint.java b/rest/src/main/java/org/oasis_open/contextserver/rest/DefinitionsServiceEndPoint.java deleted file mode 100644 index 73210c0..0000000 --- a/rest/src/main/java/org/oasis_open/contextserver/rest/DefinitionsServiceEndPoint.java +++ /dev/null @@ -1,252 +0,0 @@ -/* - * 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.oasis_open.contextserver.rest; - -import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing; -import org.oasis_open.contextserver.api.PluginType; -import org.oasis_open.contextserver.api.PropertyMergeStrategyType; -import org.oasis_open.contextserver.api.ValueType; -import org.oasis_open.contextserver.api.actions.ActionType; -import org.oasis_open.contextserver.api.conditions.ConditionType; -import org.oasis_open.contextserver.api.services.DefinitionsService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.jws.WebMethod; -import javax.jws.WebService; -import javax.ws.rs.*; -import javax.ws.rs.core.MediaType; -import java.util.*; - -/** - * A JAX-RS endpoint to retrieve definition information about core context server entities such as conditions, actions and values. - */ -@WebService -@Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8") -@CrossOriginResourceSharing( - allowAllOrigins = true, - allowCredentials = true -) -public class DefinitionsServiceEndPoint { - private static final Logger logger = LoggerFactory.getLogger(DefinitionsServiceEndPoint.class.getName()); - - private DefinitionsService definitionsService; - private LocalizationHelper localizationHelper; - - @WebMethod(exclude = true) - public void setDefinitionsService(DefinitionsService definitionsService) { - this.definitionsService = definitionsService; - } - - @WebMethod(exclude = true) - public void setLocalizationHelper(LocalizationHelper localizationHelper) { - this.localizationHelper = localizationHelper; - } - - /** - * Retrieves all known tags localized using the specified language. - * - * @param language the language to use to localize - * @return the set of all known tags - */ - @GET - @Path("/tags") - public Collection<RESTTag> getAllTags(@HeaderParam("Accept-Language") String language) { - return localizationHelper.generateTags(definitionsService.getAllTags(), language); - } - - /** - * Retrieves the set of all root tags from which all other tags are derived via sub-tags localized using the specified language. - * - * @param language the language to use to localize. - * @return the set of all root tags - */ - @GET - @Path("/rootTags") - public Collection<RESTTag> getRootTags(@HeaderParam("Accept-Language") String language) { - return localizationHelper.generateTags(definitionsService.getRootTags(), language); - } - - /** - * Retrieves the tag with the specified identifier localized using the specified language. - * - * @param language the language to use to localize. - * @param tag the identifier of the tag to retrieve - * @param filterHidden {@code true} if hidden sub-tags should be filtered out, {@code false} otherwise - * @return the tag with the specified identifier - */ - @GET - @Path("/tags/{tagId}") - public RESTTag getTag(@PathParam("tagId") String tag, @QueryParam("filterHidden") @DefaultValue("false") boolean filterHidden, @HeaderParam("Accept-Language") String language) { - return localizationHelper.generateTag(definitionsService.getTag(tag), language, filterHidden); - } - - /** - * Retrieves all condition types localized using the specified language. - * - * @param language the language to use to localize. - * @return a Collection of all collection types - */ - @GET - @Path("/conditions") - public Collection<RESTConditionType> getAllConditionTypes(@HeaderParam("Accept-Language") String language) { - Collection<ConditionType> conditionTypes = definitionsService.getAllConditionTypes(); - return localizationHelper.generateConditions(conditionTypes, language); - } - - /** - * Retrieves the set of condition types with the specified tags also retrieving condition types from sub-tags if so specified localized using the specified language. - * - * @param language the language to use to localize. - * @param tags a comma-separated list of tag identifiers - * @param recursive {@code true} if we want to also include condition types marked by sub-tags of the specified tag - * @return the set of condition types with the specified tag (and its sub-tags, if specified) - */ - @GET - @Path("/conditions/tags/{tagId}") - public Collection<RESTConditionType> getConditionTypesByTag(@PathParam("tagId") String tags, @QueryParam("recursive") @DefaultValue("false") boolean recursive, @HeaderParam("Accept-Language") String language) { - String[] tagsArray = tags.split(","); - Set<ConditionType> results = new LinkedHashSet<>(); - for (String s : tagsArray) { - results.addAll(definitionsService.getConditionTypesByTag(definitionsService.getTag(s), recursive)); - } - return localizationHelper.generateConditions(results, language); - } - - /** - * Retrieves the condition type associated with the specified identifier localized using the specified language. - * - * @param language the language to use to localize. - * @param id the identifier of the condition type to retrieve - * @return the condition type associated with the specified identifier or {@code null} if no such condition type exists - */ - @GET - @Path("/conditions/{conditionId}") - public RESTConditionType getConditionType(@PathParam("conditionId") String id, @HeaderParam("Accept-Language") String language) { - ConditionType conditionType = definitionsService.getConditionType(id); - return localizationHelper.generateCondition(conditionType, language); - } - - /** - * Retrieves all known action types localized using the specified language. - * - * @param language the language to use to localize. - * @return all known action types - */ - @GET - @Path("/actions") - public Collection<RESTActionType> getAllActionTypes(@HeaderParam("Accept-Language") String language) { - Collection<ActionType> actionTypes = definitionsService.getAllActionTypes(); - return localizationHelper.generateActions(actionTypes, language); - } - - /** - * Retrieves the set of action types with the specified tags also retrieving action types from sub-tags if so specified localized using the specified language. - * - * @param language the language to use to localize. - * @param tags the tag marking the action types we want to retrieve - * @param recursive {@code true} if we want to also include action types marked by sub-tags of the specified tag - * @return the set of action types with the specified tag (and its sub-tags, if specified) - */ - @GET - @Path("/actions/tags/{tagId}") - public Collection<RESTActionType> getActionTypeByTag(@PathParam("tagId") String tags, @QueryParam("recursive") @DefaultValue("false") boolean recursive, @HeaderParam("Accept-Language") String language) { - String[] tagsArray = tags.split(","); - Set<ActionType> results = new LinkedHashSet<>(); - for (String s : tagsArray) { - results.addAll(definitionsService.getActionTypeByTag(definitionsService.getTag(s), recursive)); - } - return localizationHelper.generateActions(results, language); - } - - /** - * Retrieves the action type associated with the specified identifier localized using the specified language. - * - * @param language the language to use to localize. - * @param id the identifier of the action type to retrieve - * @return the action type associated with the specified identifier or {@code null} if no such action type exists - */ - @GET - @Path("/actions/{actionId}") - public RESTActionType getActionType(@PathParam("actionId") String id, @HeaderParam("Accept-Language") String language) { - ActionType actionType = definitionsService.getActionType(id); - return localizationHelper.generateAction(actionType, language); - } - - /** - * Retrieves all known value types localized using the specified language. - * - * @param language the language to use to localize. - * @return all known value types - */ - @GET - @Path("/values") - public Collection<RESTValueType> getAllValueTypes(@HeaderParam("Accept-Language") String language) { - return localizationHelper.generateValueTypes(definitionsService.getAllValueTypes(), language); - } - - /** - * Retrieves the set of value types with the specified tags also retrieving value types from sub-tags if so specified localized using the specified language. - * - * @param language the language to use to localize. - * @param tags the tag marking the value types we want to retrieve - * @param recursive {@code true} if we want to also include value types marked by sub-tags of the specified tag - * @return the set of value types with the specified tag (and its sub-tags, if specified) - */ - @GET - @Path("/values/tags/{tagId}") - public Collection<RESTValueType> getValueTypeByTag(@PathParam("tagId") String tags, @QueryParam("recursive") @DefaultValue("false") boolean recursive, @HeaderParam("Accept-Language") String language) { - String[] tagsArray = tags.split(","); - Set<ValueType> results = new LinkedHashSet<>(); - for (String s : tagsArray) { - results.addAll(definitionsService.getValueTypeByTag(definitionsService.getTag(s), recursive)); - } - return localizationHelper.generateValueTypes(results, language); - } - - /** - * Retrieves the value type associated with the specified identifier localized using the specified language. - * - * @param language the language to use to localize. - * @param id the identifier of the value type to retrieve - * @return the value type associated with the specified identifier or {@code null} if no such value type exists - */ - @GET - @Path("/values/{valueTypeId}") - public RESTValueType getValueType(@PathParam("valueTypeId") String id, @HeaderParam("Accept-Language") String language) { - ValueType valueType = definitionsService.getValueType(id); - return localizationHelper.generateValueType(valueType, language); - } - - /** - * Retrieves a Map of plugin identifier to a list of plugin types defined by that particular plugin. - * - * @return a Map of plugin identifier to a list of plugin types defined by that particular plugin - */ - @GET - @Path("/typesByPlugin") - public Map<Long, List<PluginType>> getTypesByPlugin() { - return definitionsService.getTypesByPlugin(); - } - - @WebMethod(exclude = true) - public PropertyMergeStrategyType getPropertyMergeStrategyType(String id) { - return definitionsService.getPropertyMergeStrategyType(id); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/oasis_open/contextserver/rest/GoalsServiceEndPoint.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/oasis_open/contextserver/rest/GoalsServiceEndPoint.java b/rest/src/main/java/org/oasis_open/contextserver/rest/GoalsServiceEndPoint.java deleted file mode 100644 index 5984188..0000000 --- a/rest/src/main/java/org/oasis_open/contextserver/rest/GoalsServiceEndPoint.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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.oasis_open.contextserver.rest; - -import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing; -import org.oasis_open.contextserver.api.Metadata; -import org.oasis_open.contextserver.api.goals.Goal; -import org.oasis_open.contextserver.api.goals.GoalReport; -import org.oasis_open.contextserver.api.query.AggregateQuery; -import org.oasis_open.contextserver.api.query.Query; -import org.oasis_open.contextserver.api.services.GoalsService; - -import javax.jws.WebMethod; -import javax.jws.WebService; -import javax.ws.rs.*; -import javax.ws.rs.core.MediaType; -import java.util.Set; - -/** - * A JAX-RS endpoint to manage {@link Goal}s and related information. - */ -@WebService -@Produces(MediaType.APPLICATION_JSON) -@CrossOriginResourceSharing( - allowAllOrigins = true, - allowCredentials = true -) -public class GoalsServiceEndPoint { - - private GoalsService goalsService; - - @WebMethod(exclude = true) - public void setGoalsService(GoalsService goalsService) { - this.goalsService = goalsService; - } - - /** - * Retrieves the set of Metadata associated with existing goals. - * - * @return the set of Metadata associated with existing goals - */ - @GET - @Path("/") - public Set<Metadata> getGoalMetadatas() { - return goalsService.getGoalMetadatas(); - } - - - /** - * Saves the specified goal in the context server and creates associated {@link org.oasis_open.contextserver.api.rules.Rule}s if the goal is enabled. - * - * @param goal the Goal to be saved - */ - @POST - @Path("/") - public void setGoal(Goal goal) { - goalsService.setGoal(goal); - } - - /** - * Retrieves the set of Metadata associated with existing goals matching the specified {@link Query} - * - * @param query the Query used to filter the Goals which metadata we want to retrieve - * @return the set of Metadata associated with existing goals matching the specified {@link Query} - */ - @POST - @Path("/query") - public Set<Metadata> getGoalMetadatas(Query query) { - return goalsService.getGoalMetadatas(query); - } - - /** - * Retrieves the goal associated with the specified identifier. - * - * @param goalId the identifier of the goal to retrieve - * @return the goal associated with the specified identifier or {@code null} if no such goal exists - */ - @GET - @Path("/{goalId}") - public Goal getGoal(@PathParam("goalId") String goalId) { - return goalsService.getGoal(goalId); - } - - /** - * Removes the goal associated with the specified identifier, also removing associated rules if needed. - * - * @param goalId the identifier of the goal to be removed - */ - @DELETE - @Path("/{goalId}") - public void removeGoal(@PathParam("goalId") String goalId) { - goalsService.removeGoal(goalId); - } - - /** - * Retrieves the report for the goal identified with the specified identifier. - * - * @param goalId the identifier of the goal which report we want to retrieve - * @return the report for the specified goal - */ - @GET - @Path("/{goalID}/report") - public GoalReport getGoalReport(@PathParam("goalID") String goalId) { - return goalsService.getGoalReport(goalId); - } - - /** - * Retrieves the report for the goal identified with the specified identifier, considering only elements determined by the specified {@link AggregateQuery}. - * - * @param goalId the identifier of the goal which report we want to retrieve - * @param query an {@link AggregateQuery} to further specify which elements of the report we want - * @return the report for the specified goal and query - */ - @POST - @Path("/{goalID}/report") - public GoalReport getGoalReport(@PathParam("goalID") String goalId, AggregateQuery query) { - return goalsService.getGoalReport(goalId, query); - } -} http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/dc1d1520/rest/src/main/java/org/oasis_open/contextserver/rest/LocalizationHelper.java ---------------------------------------------------------------------- diff --git a/rest/src/main/java/org/oasis_open/contextserver/rest/LocalizationHelper.java b/rest/src/main/java/org/oasis_open/contextserver/rest/LocalizationHelper.java deleted file mode 100644 index 3ba9bb0..0000000 --- a/rest/src/main/java/org/oasis_open/contextserver/rest/LocalizationHelper.java +++ /dev/null @@ -1,279 +0,0 @@ -/* - * 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.oasis_open.contextserver.rest; - -import org.oasis_open.contextserver.api.Parameter; -import org.oasis_open.contextserver.api.PluginType; -import org.oasis_open.contextserver.api.Tag; -import org.oasis_open.contextserver.api.ValueType; -import org.oasis_open.contextserver.api.actions.ActionType; -import org.oasis_open.contextserver.api.conditions.ConditionType; -import org.oasis_open.contextserver.api.conditions.initializers.ChoiceListInitializer; -import org.oasis_open.contextserver.api.conditions.initializers.ChoiceListValue; -import org.oasis_open.contextserver.api.conditions.initializers.I18nSupport; -import org.osgi.framework.BundleContext; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceReference; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.ResourceBundle; - -/** - * A helper class to provide localized versions of context server entities. - */ -public class LocalizationHelper { - - private static final Logger logger = LoggerFactory.getLogger(LocalizationHelper.class.getName()); - - private BundleContext bundleContext; - private ResourceBundleHelper resourceBundleHelper; - - /** - * Creates {@link RESTConditionType}s, localized using the specified language, based on the specified {@link ConditionType}s. - * - * @param conditionTypes the {@link ConditionType}s to be localized - * @param language the language to use to localize {@link ConditionType}s - * @return a collection of {@link RESTConditionType}s based on the specified {@link ConditionType}s and localized using the specified language - */ - public Collection<RESTConditionType> generateConditions(Collection<ConditionType> conditionTypes, String language) { - List<RESTConditionType> result = new ArrayList<RESTConditionType>(); - if (conditionTypes == null) { - return result; - } - for (ConditionType conditionType : conditionTypes) { - result.add(generateCondition(conditionType, language)); - } - return result; - } - - /** - * Creates {@link RESTActionType}s, localized using the specified language, based on the specified {@link ActionType}s. - * - * @param actionTypes the {@link ActionType}s to be localized - * @param language the language to use to localize {@link ActionType}s - * @return a collection of {@link RESTActionType}s based on the specified {@link ActionType}s and localized using the specified language - */ - public Collection<RESTActionType> generateActions(Collection<ActionType> actionTypes, String language) { - List<RESTActionType> result = new ArrayList<RESTActionType>(); - if (actionTypes == null) { - return result; - } - for (ActionType actionType : actionTypes) { - result.add(generateAction(actionType, language)); - } - return result; - } - - /** - * Creates a {@link RESTConditionType} based on the specified {@link ConditionType} and localized using the specified language. - * - * @param conditionType the {@link ConditionType} to be localized - * @param language the language to use to localize {@link ConditionType} - * @return a {@link RESTConditionType} based on the specified {@link ConditionType} and localized using the specified language - */ - public RESTConditionType generateCondition(ConditionType conditionType, String language) { - RESTConditionType result = new RESTConditionType(); - result.setId(conditionType.getId()); - - ResourceBundle bundle = resourceBundleHelper.getResourceBundle(conditionType, language); - result.setName(resourceBundleHelper.getResourceBundleValue(bundle, conditionType.getNameKey())); - result.setDescription(resourceBundleHelper.getResourceBundleValue(bundle, conditionType.getDescriptionKey())); - - result.setTags(conditionType.getTagIDs()); - - for (Parameter parameter : conditionType.getParameters()) { - result.getParameters().add(generateParameter(parameter, bundle)); - } - - return result; - } - - /** - * Creates a {@link RESTActionType} based on the specified {@link ActionType} and localized using the specified language. - * - * @param actionType the {@link ActionType} to be localized - * @param language the language to use to localize {@link ActionType} - * @return a {@link RESTActionType} based on the specified {@link ActionType} and localized using the specified language - */ - public RESTActionType generateAction(ActionType actionType, String language) { - RESTActionType result = new RESTActionType(); - result.setId(actionType.getId()); - - ResourceBundle bundle = resourceBundleHelper.getResourceBundle(actionType, language); - result.setName(resourceBundleHelper.getResourceBundleValue(bundle, actionType.getNameKey())); - result.setDescription(resourceBundleHelper.getResourceBundleValue(bundle, actionType.getDescriptionKey())); - - result.setTags(actionType.getTagIds()); - - List<RESTParameter> parameters = new ArrayList<RESTParameter>(); - for (Parameter parameter : actionType.getParameters()) { - parameters.add(generateParameter(parameter, bundle)); - } - result.setParameters(parameters); - - return result; - } - - /** - * Creates a {@link RESTParameter} based on the specified {@link Parameter} and localized using the specified {@link ResourceBundle}. - * - * @param parameter the {@link Parameter} to be localized - * @param bundle the {@link ResourceBundle} used to localize the {@link Parameter}'s choice list values if needed - * @return a {@link RESTParameter} based on the specified {@link ActionType} and localized using the specified {@link ResourceBundle} - */ - public RESTParameter generateParameter(Parameter parameter, ResourceBundle bundle) { - RESTParameter result = new RESTParameter(); - result.setId(parameter.getId()); - result.setDefaultValue(parameter.getDefaultValue()); - result.setMultivalued(parameter.isMultivalued()); - result.setType(parameter.getType()); - - localizeChoiceListValues(bundle, result.getChoiceListValues(), parameter.getChoiceListInitializerFilter()); - - return result; - } - - public void localizeChoiceListValues(ResourceBundle bundle, List<ChoiceListValue> result, String choiceListInitializerFilter) { - if (choiceListInitializerFilter != null && choiceListInitializerFilter.length() > 0) { - try { - Collection<ServiceReference<ChoiceListInitializer>> matchingChoiceListInitializerReferences = bundleContext.getServiceReferences(ChoiceListInitializer.class, choiceListInitializerFilter); - for (ServiceReference<ChoiceListInitializer> choiceListInitializerReference : matchingChoiceListInitializerReferences) { - ChoiceListInitializer choiceListInitializer = bundleContext.getService(choiceListInitializerReference); - List<ChoiceListValue> options = choiceListInitializer.getValues(bundle.getLocale()); - if (choiceListInitializer instanceof I18nSupport) { - for (ChoiceListValue value : options) { - if (value instanceof PluginType) { - result.add(value.localizedCopy(resourceBundleHelper.getResourceBundleValue(resourceBundleHelper.getResourceBundle((PluginType) value, bundle.getLocale().getLanguage()), value.getName()))); - } else { - result.add(value.localizedCopy(resourceBundleHelper.getResourceBundleValue(bundle, value.getName()))); - } - } - } else { - result.addAll(options); - } - } - } catch (InvalidSyntaxException e) { - logger.error("Invalid filter", e); - } - } - } - - /** - * Creates {@link RESTValueType}s, localized using the specified language, based on the specified {@link ValueType}s. - * - * @param valueTypes the {@link ValueType}s to be localized - * @param language the language to use to localize {@link ValueType}s - * @return a collection of {@link RESTValueType}s based on the specified {@link ValueType}s and localized using the specified language - */ - public Collection<RESTValueType> generateValueTypes(Collection<ValueType> valueTypes, String language) { - List<RESTValueType> result = new ArrayList<RESTValueType>(); - if (valueTypes == null) { - return result; - } - for (ValueType valueType : valueTypes) { - result.add(generateValueType(valueType, language)); - } - return result; - } - - /** - * Creates a {@link RESTValueType} based on the specified {@link ValueType} and localized using the specified language. - * - * @param valueType the {@link ValueType} to be localized - * @param language the language to use to localize {@link ValueType} - * @return a {@link RESTValueType} based on the specified {@link ValueType} and localized using the specified language - */ - public RESTValueType generateValueType(ValueType valueType, String language) { - RESTValueType result = new RESTValueType(); - result.setId(valueType.getId()); - - ResourceBundle bundle = resourceBundleHelper.getResourceBundle(valueType, language); - result.setName(resourceBundleHelper.getResourceBundleValue(bundle, valueType.getNameKey())); - result.setDescription(resourceBundleHelper.getResourceBundleValue(bundle, valueType.getDescriptionKey())); - result.setTags(generateTags(valueType.getTags(), language)); - return result; - } - - /** - * Same as generateTages(tags, language, false). - */ - public Collection<RESTTag> generateTags(Collection<Tag> tags, String language) { - return generateTags(tags, language, false); - } - - /** - * Creates {@link RESTTag}s, localized using the specified language, based on the specified {@link Tag}s. - * - * @param tags the {@link Tag}s to be localized - * @param language the language to use to localize {@link Tag}s - * @param filterHidden {@code true} to filter out hidden tags, {@code false} otherwise - * @return a collection of {@link RESTTag}s based on the specified {@link Tag}s and localized using the specified language - */ - public Collection<RESTTag> generateTags(Collection<Tag> tags, String language, boolean filterHidden) { - List<RESTTag> result = new ArrayList<RESTTag>(); - for (Tag tag : tags) { - RESTTag subTag = generateTag(tag, language, filterHidden); - if (subTag != null) { - result.add(subTag); - } - } - return result; - } - - /** - * Same as generateTag(tag, language, false). - */ - public RESTTag generateTag(Tag tag, String language) { - return generateTag(tag, language, false); - } - - /** - * Creates a {@link RESTTag}, localized using the specified language, based on the specified {@link Tag}. - * - * @param tag the {@link Tag} to be localized - * @param language the language to use to localize the {@link Tag} - * @param filterHidden {@code true} to filter out hidden sub-tags, {@code false} otherwise - * @return a {@link RESTTag} based on the specified {@link Tag} and localized using the specified language - */ - public RESTTag generateTag(Tag tag, String language, boolean filterHidden) { - if (filterHidden && tag.isHidden()) { - return null; - } - RESTTag result = new RESTTag(); - result.setId(tag.getId()); - ResourceBundle bundle = resourceBundleHelper.getResourceBundle(tag, language); - result.setName(resourceBundleHelper.getResourceBundleValue(bundle, tag.getNameKey())); - result.setDescription(resourceBundleHelper.getResourceBundleValue(bundle, tag.getDescriptionKey())); - result.setParentId(tag.getParentId()); - result.setRank(tag.getRank()); - result.setSubTags(generateTags(tag.getSubTags(), language, filterHidden)); - return result; - } - - public void setBundleContext(BundleContext bundleContext) { - this.bundleContext = bundleContext; - } - - public void setResourceBundleHelper(ResourceBundleHelper resourceBundleHelper) { - this.resourceBundleHelper = resourceBundleHelper; - } -}
