Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QueryCriteriaValidator to search
Adds an extendable point to allow custom validation of search queries. Signed-off-by: David Schwilk <david.schwilk@bosch.io>
- Loading branch information
1 parent
29d9c20
commit 059b1d2
Showing
11 changed files
with
437 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...itto/services/thingsearch/persistence/query/validation/DefaultQueryCriteriaValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.services.thingsearch.persistence.query.validation; | ||
|
||
import java.util.Set; | ||
|
||
import org.eclipse.ditto.model.base.headers.DittoHeaders; | ||
import org.eclipse.ditto.model.query.criteria.Criteria; | ||
import org.eclipse.ditto.model.query.filter.QueryFilterCriteriaFactory; | ||
import org.eclipse.ditto.signals.commands.thingsearch.query.ThingSearchQueryCommand; | ||
|
||
/** | ||
* Default {@link org.eclipse.ditto.services.thingsearch.persistence.query.validation.QueryCriteriaValidator}, | ||
* who parses QueryCriteria without additional validation | ||
*/ | ||
public class DefaultQueryCriteriaValidator extends QueryCriteriaValidator { | ||
|
||
/** | ||
* Instantiate this provider. Called by reflection. | ||
*/ | ||
@SuppressWarnings("unused") | ||
public DefaultQueryCriteriaValidator() { | ||
// Nothing to initialize | ||
} | ||
|
||
@Override | ||
public Criteria parseCriteria(final ThingSearchQueryCommand<?> command, final QueryFilterCriteriaFactory factory) { | ||
|
||
final DittoHeaders headers = command.getDittoHeaders(); | ||
final Set<String> namespaces = command.getNamespaces().orElse(null); | ||
final String filter = command.getFilter().orElse(null); | ||
if (namespaces == null) { | ||
return factory.filterCriteria(filter, command.getDittoHeaders()); | ||
} else { | ||
return factory.filterCriteriaRestrictedByNamespaces(filter, headers, namespaces); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...lipse/ditto/services/thingsearch/persistence/query/validation/QueryCriteriaValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (c) 2020 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.services.thingsearch.persistence.query.validation; | ||
|
||
import org.eclipse.ditto.model.query.criteria.Criteria; | ||
import org.eclipse.ditto.model.query.filter.QueryFilterCriteriaFactory; | ||
import org.eclipse.ditto.services.thingsearch.common.config.DittoSearchConfig; | ||
import org.eclipse.ditto.services.thingsearch.common.config.SearchConfig; | ||
import org.eclipse.ditto.services.utils.akka.AkkaClassLoader; | ||
import org.eclipse.ditto.services.utils.config.DefaultScopedConfig; | ||
import org.eclipse.ditto.signals.commands.thingsearch.query.ThingSearchQueryCommand; | ||
|
||
import akka.actor.AbstractExtensionId; | ||
import akka.actor.ActorSystem; | ||
import akka.actor.ExtendedActorSystem; | ||
import akka.actor.Extension; | ||
|
||
/** | ||
* Search Query Validator to be loaded by reflection. | ||
* Can be used as an extension point to use custom validation of search queries. | ||
* Implementations MUST have a public constructor. | ||
*/ | ||
public abstract class QueryCriteriaValidator implements Extension { | ||
|
||
/** | ||
* Gets the criteria of a {@link org.eclipse.ditto.signals.commands.thingsearch.query.ThingSearchQueryCommand} and | ||
* validates it. | ||
* | ||
* May throw an exception depending on the implementation in the used QueryCriteriaValidator. | ||
* | ||
* @return the criteria of the query command. | ||
*/ | ||
public abstract Criteria parseCriteria(final ThingSearchQueryCommand<?> command, | ||
final QueryFilterCriteriaFactory factory); | ||
|
||
/** | ||
* Load a {@code QueryCriteriaValidator} dynamically according to the search configuration. | ||
* | ||
* @param actorSystem The actor system in which to load the validator. | ||
* @return The validator. | ||
*/ | ||
public static QueryCriteriaValidator get(final ActorSystem actorSystem) { | ||
return ExtensionId.INSTANCE.get(actorSystem); | ||
} | ||
|
||
/** | ||
* ID of the actor system extension to validate the {@code QueryCriteriaValidator}. | ||
*/ | ||
private static final class ExtensionId extends AbstractExtensionId<QueryCriteriaValidator> { | ||
|
||
private static final ExtensionId INSTANCE = new ExtensionId(); | ||
|
||
@Override | ||
public QueryCriteriaValidator createExtension(final ExtendedActorSystem system) { | ||
final SearchConfig searchConfig = | ||
DittoSearchConfig.of(DefaultScopedConfig.dittoScoped( | ||
system.settings().config())); | ||
return AkkaClassLoader.instantiate(system, QueryCriteriaValidator.class, | ||
searchConfig.getQueryValidator()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
services/thingsearch/starter/src/main/resources/things-search.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.