Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Upd] Add better authentication for RDF4jModule #143

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package cz.cvut.spipes.engine;

import cz.cvut.spipes.util.CoreConfigProperies;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.QuerySolutionMap;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.ResourceFactory;

import java.util.Map;

public class ExecutionContextFactory {

Expand All @@ -11,21 +17,21 @@ public class ExecutionContextFactory {
public static ExecutionContext createEmptyContext() {
ExecutionContextImpl context = new ExecutionContextImpl();
context.setDefaultModel(ModelFactory.createDefaultModel());
context.setVariablesBinding(new VariablesBinding());
context.setVariablesBinding(extendByConfigurationVariables(new VariablesBinding()));
return context;
}

public static ExecutionContext createContext(Model defaultModel, VariablesBinding variablesBinding) {
ExecutionContextImpl context = new ExecutionContextImpl();
context.setDefaultModel(defaultModel);
context.setVariablesBinding(variablesBinding);
context.setVariablesBinding(extendByConfigurationVariables(variablesBinding));
return context;
}

public static ExecutionContext createContext(VariablesBinding variablesBinding) {
ExecutionContextImpl context = new ExecutionContextImpl();
context.setDefaultModel(ModelFactory.createDefaultModel());
context.setVariablesBinding(variablesBinding);
context.setVariablesBinding(extendByConfigurationVariables(variablesBinding));
return context;
}

Expand All @@ -34,8 +40,22 @@ public static ExecutionContext createContext(VariablesBinding variablesBinding)
public static ExecutionContext createContext(Model defaultModel) {
ExecutionContextImpl context = new ExecutionContextImpl();
context.setDefaultModel(defaultModel);
context.setVariablesBinding(new VariablesBinding());
context.setVariablesBinding(extendByConfigurationVariables(new VariablesBinding()));
return context;
}

private static VariablesBinding extendByConfigurationVariables(VariablesBinding variable) {
Map<String,String> map = CoreConfigProperies.getConfigurationVariables();
variable.extendConsistently(new VariablesBinding(transform(map)));
return variable;
}

private static QuerySolution transform(final Map<String,String> parameters) {
final QuerySolutionMap querySolution = new QuerySolutionMap();

parameters.entrySet().forEach(
e -> querySolution.add(e.getKey(), ResourceFactory.createPlainLiteral(e.getValue()))
);
return querySolution;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,14 @@ public void load(final InputStream is, final String lang) throws IOException {

@Override
public String toString() {
return binding.asMap().toString();
return getVariablesWithoutConfigurationOnes().toString();
}

public String toTruncatedString() {
return binding.asMap().entrySet().stream()
.map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), getTruncatedValue(e.getValue().toString()))).
Map<String,String> map = getVariablesWithoutConfigurationOnes();

return map.entrySet().stream()
.map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), getTruncatedValue(e.getValue()))).
collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)).toString();
}

Expand All @@ -194,4 +196,10 @@ private static String getTruncatedValue(@NotNull String value) {
}
return value;
}

private Map<String, String> getVariablesWithoutConfigurationOnes(){
return binding.asMap().entrySet()
.stream().filter(entry -> !entry.getKey().startsWith("variable.assignment"))
.collect(Collectors.toMap(Map.Entry::getKey, o -> o.getValue().toString(), (x, y) -> y, HashMap::new));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class CoreConfigProperies {
private static final String CONFIG_FILE = "config-core.properties";
Expand Down Expand Up @@ -38,6 +41,14 @@ public class CoreConfigProperies {
}
}

public static Map<String,String> getConfigurationVariables(){
Matthew-Kulich marked this conversation as resolved.
Show resolved Hide resolved
Map<String, String> map = new HashMap<>((Map) prop);
return map.entrySet()
.stream()
.filter(entry -> (entry.getKey()).startsWith("variable.assignment"))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> y, HashMap::new));
}

public static String get(String name) {
String value = prop.getProperty(name);
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ ExecutionContext executeSelf() {
VariablesBinding variablesBinding = getExecutionContext().getVariablesBinding();

String username = Optional
.ofNullable(variablesBinding.getNode("p-username"))
.ofNullable(variablesBinding.getNode("variable.assignment.rdf4jUsername"))
Matthew-Kulich marked this conversation as resolved.
Show resolved Hide resolved
.map(RDFNode::toString)
.orElse(null);

String password = Optional
.ofNullable(variablesBinding.getNode("p-password"))
.ofNullable(variablesBinding.getNode("variable.assignment.rdf4jPassword"))
.map(RDFNode::toString)
.orElse(null);

Expand Down