Skip to content

Commit

Permalink
added data source support - crypto support
Browse files Browse the repository at this point in the history
  • Loading branch information
dprzybyl committed Oct 12, 2023
1 parent 33f6bac commit 30b7626
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ public interface DataSource {

String getName();

ApmType determine(ResourceResolver resolver, List<ApmType> parameters);
ApmType determine(ResourceResolver resolver, List<Object> parameters);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
*/
package com.cognifide.apm.core.grammar.datasource;

import com.cognifide.apm.core.crypto.DecryptionService;
import com.cognifide.apm.core.grammar.ApmType;
import com.cognifide.apm.core.grammar.argument.ArgumentResolverException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand All @@ -33,6 +35,9 @@
@Component(service = DataSourceInvoker.class)
public class DataSourceInvoker {

@Reference
private DecryptionService decryptionService;

private final Map<String, DataSource> dataSources = new HashMap<>();

@Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE, service = DataSource.class)
Expand All @@ -47,7 +52,10 @@ protected final void unbindDataSource(DataSource dataSource) {
public ApmType determine(String name, ResourceResolver resolver, List<ApmType> parameters) {
DataSource dataSource = dataSources.get(name.toUpperCase());
try {
return dataSource == null ? null : dataSource.determine(resolver, parameters);
List<Object> decryptedParameters = parameters.stream()
.map(parameter -> parameter.getArgument(decryptionService))
.collect(Collectors.toList());
return dataSource == null ? null : dataSource.determine(resolver, decryptedParameters);
} catch (Exception e) {
throw new ArgumentResolverException(String.format("%s data source: %s", name.toUpperCase(), e.getMessage()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public String getName() {
}

@Override
public ApmType determine(ResourceResolver resolver, List<ApmType> parameters) {
String value = parameters.get(0).getString();
String regex = parameters.get(1).getString();
public ApmType determine(ResourceResolver resolver, List<Object> parameters) {
String value = (String) parameters.get(0);
String regex = (String) parameters.get(1);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(value);
List<ApmType> list = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public String getName() {
}

@Override
public ApmType determine(ResourceResolver resolver, List<ApmType> parameters) {
String value = parameters.get(0).getString();
public ApmType determine(ResourceResolver resolver, List<Object> parameters) {
String value = (String) parameters.get(0);
return new ApmString(value.toLowerCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public String getName() {
}

@Override
public ApmType determine(ResourceResolver resolver, List<ApmType> parameters) {
String path = parameters.get(0).getString();
String regex = parameters.get(1).getString();
public ApmType determine(ResourceResolver resolver, List<Object> parameters) {
String path = (String) parameters.get(0);
String regex = (String) parameters.get(1);
Pattern pattern = Pattern.compile(regex);
Resource root = resolver.getResource(path);
List<ApmMap> values = StreamSupport.stream(root.getChildren().spliterator(), false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.day.cq.wcm.api.NameConstants;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -51,8 +52,8 @@ public String getName() {
}

@Override
public ApmType determine(ResourceResolver resolver, List<ApmType> parameters) {
String rootPath = parameters.get(0).getString();
public ApmType determine(ResourceResolver resolver, List<Object> parameters) {
String rootPath = (String) parameters.get(0);
List<Config> configs = determineConfigs(parameters);
Resource root = resolver.getResource(rootPath);
return traverseTree(root, 0, configs);
Expand Down Expand Up @@ -88,11 +89,10 @@ private ApmType traverseTree(Resource root, int depth, List<Config> configs) {
return new ApmList(list);
}

private List<Config> determineConfigs(List<ApmType> parameters) {
return parameters.get(1)
.getList()
private List<Config> determineConfigs(List<Object> parameters) {
return ((List<Object>) parameters.get(1))
.stream()
.map(ApmType::getMap)
.map(item -> (Map<String, Object>) item)
.map(Config::new)
.collect(Collectors.toList());
}
Expand All @@ -109,22 +109,18 @@ private static class Config {

List<String> paramNames;

Config(Map<String, ApmType> map) {
String regex = map.getOrDefault("regex", new ApmEmpty()).getString();
Config(Map<String, Object> map) {
String regex = (String) map.get("regex");
if (StringUtils.isNotEmpty(regex)) {
pattern = Pattern.compile(regex);
}
String excludeRegex = map.getOrDefault("excludeRegex", new ApmEmpty()).getString();
String excludeRegex = (String) map.get("excludeRegex");
if (StringUtils.isNotEmpty(excludeRegex)) {
excludePattern = Pattern.compile(excludeRegex);
}
template = map.getOrDefault("template", new ApmEmpty()).getString();
resourceType = map.getOrDefault("resourceType", new ApmEmpty()).getString();
paramNames = map.getOrDefault("paramNames", new ApmList(new ArrayList<>()))
.getList()
.stream()
.map(ApmType::getString)
.collect(Collectors.toList());
template = (String) map.get("template");
resourceType = (String) map.get("resourceType");
paramNames = (List<String>) map.getOrDefault("paramNames", Collections.emptyList());
if (StringUtils.countMatches(regex, "(") != paramNames.size()) {
throw new IllegalArgumentException("Number of paramNames must match number of regex groups");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public String getName() {
}

@Override
public ApmType determine(ResourceResolver resolver, List<ApmType> parameters) {
String value = parameters.get(0).getString();
public ApmType determine(ResourceResolver resolver, List<Object> parameters) {
String value = (String) parameters.get(0);
return new ApmString(value.toUpperCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public String getName() {
}

@Override
public ApmType determine(ResourceResolver resolver, List<ApmType> parameters) {
String path = parameters.get(0).getString();
public ApmType determine(ResourceResolver resolver, List<Object> parameters) {
String path = (String) parameters.get(0);
ValueMap valueMap = resolver.getResource(path).getValueMap();
Map<String, ApmType> map = new HashMap<>();
for (Map.Entry<String, Object> entry : valueMap.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ package com.cognifide.apm.core.grammar
import com.cognifide.apm.api.scripts.Script
import com.cognifide.apm.api.services.ScriptFinder
import com.cognifide.apm.api.status.Status
import com.cognifide.apm.core.crypto.DecryptionService
import com.cognifide.apm.core.grammar.datasource.DataSource
import com.cognifide.apm.core.grammar.datasource.DataSourceInvoker
import com.cognifide.apm.core.progress.ProgressImpl
import org.apache.commons.io.IOUtils
import org.apache.commons.lang3.reflect.FieldUtils
import org.apache.sling.api.resource.ResourceResolver
import spock.lang.Specification

Expand Down Expand Up @@ -242,6 +244,7 @@ class ScriptRunnerTest extends Specification {

private static DataSourceInvoker createDataSourceInvoker() {
def dataSourceInvoker = new DataSourceInvoker()
FieldUtils.writeField(dataSourceInvoker, "decryptionService", new DecryptionService(), true)
def bindDataSource = DataSourceInvoker.class.getDeclaredMethod("bindDataSource", DataSource.class)
bindDataSource.setAccessible(true)
bindDataSource.invoke(dataSourceInvoker, new DataSource() {
Expand All @@ -251,8 +254,8 @@ class ScriptRunnerTest extends Specification {
}

@Override
ApmType determine(ResourceResolver resolver, List<ApmType> parameters) {
return parameters.get(0)
ApmType determine(ResourceResolver resolver, List<Object> parameters) {
return new ApmString(parameters.get(0))
}
})
bindDataSource.invoke(dataSourceInvoker, new DataSource() {
Expand All @@ -262,8 +265,8 @@ class ScriptRunnerTest extends Specification {
}

@Override
ApmType determine(ResourceResolver resolver, List<ApmType> parameters) {
return new ApmString(parameters.get(0).getString().toUpperCase())
ApmType determine(ResourceResolver resolver, List<Object> parameters) {
return new ApmString(parameters.get(0).toUpperCase())
}
})
return dataSourceInvoker
Expand Down

0 comments on commit 30b7626

Please sign in to comment.