From 206fc0d0d710942a626942762b0e2b64953b1d22 Mon Sep 17 00:00:00 2001 From: Luca Roffia Date: Tue, 31 Oct 2023 10:53:10 +0100 Subject: [PATCH] feat: changing JSAP to load from URL --- .../properties/SPARQL11Properties.java | 188 +++++++++++--- .../properties/SPARQL11SEProperties.java | 131 ++++++++-- .../commons/security/OAuthProperties.java | 110 +------- .../arces/wot/sepa/pattern/Aggregator.java | 4 +- .../arces/wot/sepa/pattern/Consumer.java | 4 +- .../it/unibo/arces/wot/sepa/pattern/JSAP.java | 237 ++++++++++-------- .../arces/wot/sepa/pattern/Producer.java | 4 +- .../arces/wot/sepa/ConfigurationProvider.java | 10 +- .../sepa/engine/core/EngineProperties.java | 15 +- 9 files changed, 420 insertions(+), 283 deletions(-) diff --git a/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/properties/SPARQL11Properties.java b/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/properties/SPARQL11Properties.java index 45d3b982..7ec5b997 100644 --- a/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/properties/SPARQL11Properties.java +++ b/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/properties/SPARQL11Properties.java @@ -19,6 +19,10 @@ package it.unibo.arces.wot.sepa.commons.properties; import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Map; +import java.util.Objects; import java.util.Set; import com.google.gson.Gson; @@ -83,13 +87,8 @@ public class SPARQL11Properties { protected String host; protected SPARQL11ProtocolProperties sparql11protocol; protected GraphsProperties graphs = null; - - /** The defaults file name. */ - protected String defaultsFileName = "endpoint.jpar"; - /** The properties file. */ - protected String propertiesFile = defaultsFileName; - + protected String filename = null; /** * The Enum SPARQLPrimitive (QUERY, UPDATE). @@ -123,57 +122,188 @@ public String getProtocolScheme() { } }; - + public SPARQL11Properties() { + this.host = "localhost"; + this.sparql11protocol = new SPARQL11ProtocolProperties(); + this.sparql11protocol.protocol = ProtocolScheme.http; + + override(null); + } + + public SPARQL11Properties(String args[]) { + this(); + + override(args); + } + + public SPARQL11Properties(String jsapFile,String[] args) throws SEPAPropertiesException { + this(); + + Reader in = getReaderFromUrl(jsapFile); + parseJSAP(in); + try { + in.close(); + } catch (IOException e) { + throw new SEPAPropertiesException(e); + } + + override(args); + + filename = jsapFile; + } + + public SPARQL11Properties(String jsapFile) throws SEPAPropertiesException { + this(); + + Reader in = getReaderFromUrl(jsapFile); + parseJSAP(in); + try { + in.close(); + } catch (IOException e) { + throw new SEPAPropertiesException(e); + } + + override(null); + + filename = jsapFile; + } + public SPARQL11Properties(String host,ProtocolScheme scheme) { this.host = host; this.sparql11protocol = new SPARQL11ProtocolProperties(); this.sparql11protocol.protocol = scheme; + + override(null); + } + + public SPARQL11Properties(Reader in,String[] args) throws SEPAPropertiesException { + this(); + + parseJSAP(in); + + override(args); + } + + public SPARQL11Properties(Reader in) throws SEPAPropertiesException { + this(in,null); } - public SPARQL11Properties(String jsapFile) throws SEPAPropertiesException { + protected void override(String[] args) { + Map envs = System.getenv(); + for(String var : envs.keySet()) { + Logging.logger.debug("Environmental variable "+var+" : "+envs.get(var)); + setParameter("-"+var, envs.get(var)); + } + + if (args != null) + for (int i = 0; i < args.length; i = i + 2) { + Logging.logger.debug("Argument "+args[i]+" : "+args[i+1]); + setParameter(args[i], args[i+1]); + } + } + + private void parseJSAP(Reader in) throws SEPAPropertiesException { SPARQL11Properties jsap = null; - if (jsapFile == null) throw new SEPAPropertiesException("File is null"); try { - FileReader reader = new FileReader(jsapFile); - jsap = new Gson().fromJson(reader, SPARQL11Properties.class); - } catch (JsonSyntaxException | JsonIOException | FileNotFoundException e) { + jsap = new Gson().fromJson(in, SPARQL11Properties.class); + } catch (JsonSyntaxException | JsonIOException e) { Logging.logger.error(e.getMessage()); e.printStackTrace(); throw new SEPAPropertiesException(e); } - + this.host = jsap.host; this.sparql11protocol = jsap.sparql11protocol; this.graphs = jsap.graphs; - - propertiesFile = jsapFile; } - public String getJSAPFilename() { - return propertiesFile; + /* + * Applications working with file paths and file URIs should take great care + * to use the appropriate methods to convert between the two. + * The Path.of(URI) factory method and the File.File(URI) constructor + * can be used to create Path or File objects from a file URI. + * Path.toUri() and File.toURI() can be used to create a URI from a file path, + * which can be converted to URL using URI.toURL(). Applications should never + * try to construct or parse a URL from the direct string representation + * of a File or Path instance + * */ + protected Reader getReaderFromUrl(String url) throws SEPAPropertiesException { + Reader in = null; + URL jsap = null; + + Logging.logger.info("Get reader from URL: "+url); + try { + jsap = new URL(url); + try { + in = new BufferedReader( + new InputStreamReader(jsap.openStream())); + } catch (IOException e) { + Logging.logger.warn("Failed to read input stream: "+e.getMessage()); + try { + in = new FileReader(jsap.getFile()); + } catch (FileNotFoundException ex) { + Logging.logger.warn("Failed to read file: "+e.getMessage()); + in = new InputStreamReader(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream(jsap.getFile()))); + } + } + } catch (MalformedURLException e) { + Logging.logger.error(e.getMessage()); + throw new SEPAPropertiesException(e); + } + + return in; + } + + protected void setParameter(String key,String value) { + switch (key) { + case "-host" : + this.host = value; + break; + case "-sparql11protocol.port": + this.sparql11protocol.port = Integer.valueOf(value); + break; + case "-sparql11protocol.host": + this.sparql11protocol.host = host; + break; + case "-sparql11protocol.protocol": + this.sparql11protocol.protocol = (value == "http" ? ProtocolScheme.http : ProtocolScheme.https); + break; + case "-sparql11protocol.update.method": + this.sparql11protocol.update.method = (value == "post" ? UpdateHTTPMethod.POST : UpdateHTTPMethod.URL_ENCODED_POST); + break; + case "-sparql11protocol.update.format": + this.sparql11protocol.update.format = (value == "json" ? UpdateResultsFormat.JSON : UpdateResultsFormat.HTML); + break; + case "-sparql11protocol.update.path": + this.sparql11protocol.update.path = value; + break; + case "-sparql11protocol.query.method": + this.sparql11protocol.query.method = (value == "get" ? QueryHTTPMethod.GET : (value == "post" ? QueryHTTPMethod.POST : QueryHTTPMethod.URL_ENCODED_POST)); + break; + case "-sparql11protocol.query.format": + this.sparql11protocol.query.format = (value == "json" ? QueryResultsFormat.JSON : (value == "xml" ? QueryResultsFormat.XML : QueryResultsFormat.CSV)); + break; + case "-sparql11protocol.query.path": + this.sparql11protocol.query.path = value; + break; + } } public String toString() { return new Gson().toJson(this); } - /** - * Store properties. - * - * @param propertiesFile the properties file - * @throws SEPAPropertiesException - * @throws IOException Signals that an I/O exception has occurred. - */ - protected void storeProperties(String propertiesFile) throws SEPAPropertiesException { + public void write() throws SEPAPropertiesException { FileWriter out; try { - out = new FileWriter(propertiesFile); + out = new FileWriter(filename); out.write(this.toString()); out.close(); - } catch (Exception e) { - throw new SEPAPropertiesException(e); + } catch (IOException e) { + if (Logging.logger.isTraceEnabled()) + e.printStackTrace(); + throw new SEPAPropertiesException(e.getMessage()); } - } /** diff --git a/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/properties/SPARQL11SEProperties.java b/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/properties/SPARQL11SEProperties.java index 2dbeafd3..4c94c7cd 100644 --- a/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/properties/SPARQL11SEProperties.java +++ b/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/properties/SPARQL11SEProperties.java @@ -18,8 +18,8 @@ package it.unibo.arces.wot.sepa.commons.properties; -import java.io.FileNotFoundException; -import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; import com.google.gson.Gson; import com.google.gson.JsonIOException; @@ -81,47 +81,124 @@ public enum SPARQL11SEPrimitive { // Members protected SPARQL11SEProtocolProperties sparql11seprotocol; + public SPARQL11SEProperties() { + sparql11seprotocol = new SPARQL11SEProtocolProperties(); + + override(null); + } + /** * Instantiates a new SPARQL 11 SE properties. * - * @param propertiesFile the properties file + * @param in where to read the JSAP from * @throws SEPAPropertiesException */ + public SPARQL11SEProperties(Reader in) throws SEPAPropertiesException { + super(in); + + parseJSAP(in); + + override(null); + } + + public SPARQL11SEProperties(Reader in,String[] args) throws SEPAPropertiesException { + super(in,args); + + parseJSAP(in); + + override(args); + } + + public SPARQL11SEProperties(String propertiesFile,String[] args) throws SEPAPropertiesException { + super(propertiesFile); + + Reader in = getReaderFromUrl(propertiesFile); + parseJSAP(in); + try { + in.close(); + } catch (IOException e) { + throw new SEPAPropertiesException(e); + } + + override(args); + } + public SPARQL11SEProperties(String propertiesFile) throws SEPAPropertiesException { super(propertiesFile); + Reader in = getReaderFromUrl(propertiesFile); + parseJSAP(in); + try { + in.close(); + } catch (IOException e) { + throw new SEPAPropertiesException(e); + } + + override(null); + } + + private void parseJSAP(Reader in) throws SEPAPropertiesException { SPARQL11SEProperties jsap; try { - jsap = new Gson().fromJson(new FileReader(propertiesFile), SPARQL11SEProperties.class); + jsap = new Gson().fromJson(in, SPARQL11SEProperties.class); sparql11seprotocol = jsap.sparql11seprotocol; - } catch (JsonSyntaxException | JsonIOException | FileNotFoundException e2) { + } catch (JsonSyntaxException | JsonIOException e2) { Logging.logger.error(e2.getMessage()); e2.printStackTrace(); throw new SEPAPropertiesException(e2); } + } -// try { -// jsap = new Gson().fromJson(new FileReader(propertiesFile), SPARQL11SEProperties.class); -// } catch (Exception e) { -// Logging.logger.warn("Create from file: " + propertiesFile); -// Logging.logger.warn(e.getMessage()); -// jsap = new SPARQL11SEProperties(); -// Logging.logger.warn("USING DEFAULTS. Edit \"" + defaultsFileName + "\" (if needed) and run again the broker"); -// try { -// jsap.storeProperties(defaultsFileName); -// } catch (SEPAPropertiesException e1) { -// Logging.logger.error(e1.getMessage()); -// } -// -// } - - - } - -// public SPARQL11SEProperties() { -// super(); -// sparql11seprotocol = new SPARQL11SEProtocol(); -// } + protected final void setParameter(String key,String value) { + switch (key) { + case "-host" : + this.host = value; + break; + case "-sparql11protocol.port": + this.sparql11protocol.port = Integer.valueOf(value); + break; + case "-sparql11protocol.host": + this.sparql11protocol.host = host; + break; + case "-sparql11protocol.protocol": + this.sparql11protocol.protocol = (value == "http" ? ProtocolScheme.http : ProtocolScheme.https); + break; + case "-sparql11protocol.update.method": + this.sparql11protocol.update.method = (value == "post" ? UpdateProperties.UpdateHTTPMethod.POST : UpdateProperties.UpdateHTTPMethod.URL_ENCODED_POST); + break; + case "-sparql11protocol.update.format": + this.sparql11protocol.update.format = (value == "json" ? UpdateProperties.UpdateResultsFormat.JSON : UpdateProperties.UpdateResultsFormat.HTML); + break; + case "-sparql11protocol.update.path": + this.sparql11protocol.update.path = value; + break; + case "-sparql11protocol.query.method": + this.sparql11protocol.query.method = (value == "get" ? QueryProperties.QueryHTTPMethod.GET : (value == "post" ? QueryProperties.QueryHTTPMethod.POST : QueryProperties.QueryHTTPMethod.URL_ENCODED_POST)); + break; + case "-sparql11protocol.query.format": + this.sparql11protocol.query.format = (value == "json" ? QueryProperties.QueryResultsFormat.JSON : (value == "xml" ? QueryProperties.QueryResultsFormat.XML : QueryProperties.QueryResultsFormat.CSV)); + break; + case "-sparql11protocol.query.path": + this.sparql11protocol.query.path = value; + break; + case "-sparql11seprotocol.host": + this.sparql11seprotocol.host = value; + break; + case "-sparql11seprotocol.protocol": + this.sparql11seprotocol.protocol = value; + break; + case "-sparql11seprotocol.reconnect": + this.sparql11seprotocol.reconnect = Boolean.valueOf(value); + break; + default: + if (key.startsWith("-sparql11seprotocol.availableProtocols")) { + String[] token = key.split("."); + if (token[3] == "path") this.sparql11seprotocol.availableProtocols.get(token[2]).path = value; + else if (token[3] == "port") this.sparql11seprotocol.availableProtocols.get(token[2]).port = Integer.valueOf(value); + else if (token[3] == "scheme") this.sparql11seprotocol.availableProtocols.get(token[2]).scheme = value; + } + } + } public String toString() { return new Gson().toJson(this); diff --git a/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/security/OAuthProperties.java b/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/security/OAuthProperties.java index bb55bc35..35c30673 100644 --- a/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/security/OAuthProperties.java +++ b/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/security/OAuthProperties.java @@ -19,17 +19,12 @@ package it.unibo.arces.wot.sepa.commons.security; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.Base64; import java.util.Date; -import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; @@ -37,6 +32,7 @@ import it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException; import it.unibo.arces.wot.sepa.commons.response.JWTResponse; import it.unibo.arces.wot.sepa.logging.Logging; +import it.unibo.arces.wot.sepa.pattern.JSAP; /** * The set of properties used for client authentication @@ -98,94 +94,18 @@ public enum OAUTH_PROVIDER{SEPA,KEYCLOAK}; public OAUTH_PROVIDER getProvider() { return provider; } - - public OAuthProperties(InputStream input,byte[] secret) throws SEPAPropertiesException { - InputStreamReader in = new InputStreamReader(input); - - jsap = new Gson().fromJson(in,JsonObject.class); - - try { - - if (secret != null) encryption = new Encryption(secret); - - if (jsap.has("oauth")) { - oauthJsonObject = jsap.getAsJsonObject("oauth"); - if (oauthJsonObject.has("enable")) enabled = oauthJsonObject.get("enable").getAsBoolean(); - if (oauthJsonObject.has("loadTrustMaterial")) { - jks = oauthJsonObject.get("loadTrustMaterial").getAsJsonObject().get("jks").getAsString(); - jksSecret = oauthJsonObject.get("loadTrustMaterial").getAsJsonObject().get("secret").getAsString(); - } - if (oauthJsonObject.has("ssl")) ssl = oauthJsonObject.get("ssl").getAsString(); - - if (enabled) { - if (oauthJsonObject.has("provider")) { - String p = oauthJsonObject.get("provider").getAsString(); - if (p.equals("keycloak")) provider = OAUTH_PROVIDER.KEYCLOAK; - else if (p.equals("sepa")) provider = OAUTH_PROVIDER.SEPA; - else throw new SEPASecurityException("Provider must have one of the following values: [sepa|keycloak]"); - } - - if (oauthJsonObject.has("authentication")) { - JsonObject auth = oauthJsonObject.getAsJsonObject("authentication"); - - if (auth.has("endpoint")) - tokenRequestURL = auth.get("endpoint").getAsString(); - if (auth.has("client_id")) - clientId = encryption.decrypt(auth.get("client_id").getAsString()); - if (auth.has("client_secret")) - clientSecret = encryption.decrypt(auth.get("client_secret").getAsString()); - if (auth.has("jwt")) - jwt = encryption.decrypt(auth.get("jwt").getAsString()); - if (auth.has("expires")) - expires = Long.decode(encryption.decrypt(auth.get("expires").getAsString())); - if (auth.has("type")) - type = encryption.decrypt(auth.get("type").getAsString()); - } - - // Initial access token registration - if (oauthJsonObject.has("registration")) { - JsonObject reg = oauthJsonObject.getAsJsonObject("registration"); - registrationURL = reg.get("endpoint").getAsString(); - - initialAccessToken = (reg.has("initialAccessToken") ? reg.get("initialAccessToken").getAsString() : null); - username = (reg.has("username") ? reg.get("username").getAsString() : null); - clientRegistrationId = (reg.has("client_id") ? reg.get("client_id").getAsString() : null); - } - } - } - } catch(Exception e) { - Logging.logger.error(e.getMessage()); - throw new SEPAPropertiesException(e.getMessage()); - } + public OAuthProperties(JSAP jsap) throws SEPAPropertiesException { + this(jsap,null); } - - public OAuthProperties(String jsapFileName, byte[] secret) - throws SEPAPropertiesException, SEPASecurityException { - propertiesFile = new File(jsapFileName); - - FileReader in; - try { - in = new FileReader(propertiesFile); - } catch (FileNotFoundException e) { - throw new SEPAPropertiesException("FileNotFoundException. " + e.getMessage()); - } - - jsap = new Gson().fromJson(in,JsonObject.class); - - try { - in.close(); - } catch (IOException e) { - throw new SEPAPropertiesException("IOException. " + e.getMessage()); - } - + public OAuthProperties(JSAP jsap,byte[] secret) throws SEPAPropertiesException { try { if (secret != null) encryption = new Encryption(secret); - if (jsap.has("oauth")) { - oauthJsonObject = jsap.getAsJsonObject("oauth"); - + if (jsap.isSecure()) { + //oauthJsonObject = jsap.getAsJsonObject("oauth"); + //TODO: to be adapted to the new JSAP structure! if (oauthJsonObject.has("enable")) enabled = oauthJsonObject.get("enable").getAsBoolean(); if (oauthJsonObject.has("loadTrustMaterial")) { jks = oauthJsonObject.get("loadTrustMaterial").getAsJsonObject().get("jks").getAsString(); @@ -233,22 +153,6 @@ public OAuthProperties(String jsapFileName, byte[] secret) Logging.logger.error(e.getMessage()); throw new SEPAPropertiesException(e.getMessage()); } - - } - - public OAuthProperties(String jsap) throws SEPAPropertiesException, SEPASecurityException { - this(jsap, null); - } - - public OAuthProperties(InputStream jsap) throws SEPAPropertiesException, SEPASecurityException { - this(jsap, null); - } - - public OAuthProperties() {} - - - public boolean isEnabled() { - return enabled; } public String getRegisterUrl() { diff --git a/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/Aggregator.java b/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/Aggregator.java index 6f71165a..b46f242f 100644 --- a/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/Aggregator.java +++ b/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/Aggregator.java @@ -44,8 +44,8 @@ public Aggregator(JSAP appProfile, String subscribeID, String updateID) } if (appProfile.getSPARQLUpdate(updateID) == null) { - Logging.logger.fatal("UPDATE ID " + updateID + " not found in " + appProfile.getFileName()); - throw new IllegalArgumentException("UPDATE ID " + updateID + " not found in " + appProfile.getFileName()); + Logging.logger.fatal("UPDATE ID " + updateID + " not found"); + throw new IllegalArgumentException("UPDATE ID " + updateID + " not found"); } updateId = updateID; diff --git a/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/Consumer.java b/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/Consumer.java index e2ee56b7..2efc6a03 100644 --- a/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/Consumer.java +++ b/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/Consumer.java @@ -55,9 +55,9 @@ public Consumer(JSAP appProfile, String subscribeID) } if (appProfile.getSPARQLQuery(subscribeID) == null) { - Logging.logger.fatal("SUBSCRIBE ID [" + subscribeID + "] not found in " + appProfile.getFileName()); + Logging.logger.fatal("SUBSCRIBE ID [" + subscribeID + "] not found"); throw new IllegalArgumentException( - "SUBSCRIBE ID [" + subscribeID + "] not found in " + appProfile.getFileName()); + "SUBSCRIBE ID [" + subscribeID + "] not found"); } subID = subscribeID; diff --git a/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/JSAP.java b/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/JSAP.java index b2857786..49139902 100644 --- a/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/JSAP.java +++ b/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/JSAP.java @@ -18,19 +18,11 @@ package it.unibo.arces.wot.sepa.pattern; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.Reader; +import java.io.*; import java.net.URI; import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; +import java.util.*; import java.util.Map.Entry; -import java.util.Set; import org.apache.commons.lang.StringEscapeUtils; @@ -181,9 +173,6 @@ public class JSAP extends SPARQL11SEProperties { private String prefixes = ""; // Members -// private String host = null; -// protected SPARQL11Properties sparql11protocol; -// protected SPARQL11SEProperties sparql11seprotocol; protected HashMap queries = null;// new HashMap(); protected HashMap updates = null;// new HashMap(); protected HashMap namespaces = new HashMap(); @@ -210,16 +199,138 @@ private void defaultNamespaces() { namespaces.put("xsd", "http://www.w3.org/2001/XMLSchema#"); } - public JSAP(String propertiesFile) throws SEPAPropertiesException, SEPASecurityException { - super(propertiesFile); + public JSAP(Reader in) throws SEPAPropertiesException, SEPASecurityException { + super(in); + + load(in, true); + + override(null); + + defaultNamespaces(); + + buildSPARQLPrefixes(); + } + + public JSAP(String url) throws SEPAPropertiesException, SEPASecurityException { + super(url); + + Reader in = getReaderFromUrl(url); + load(in, true); + try { + in.close(); + } catch (IOException e) { + throw new SEPAPropertiesException(e); + } + + override(null); - loadFromFile(propertiesFile, true); - defaultNamespaces(); buildSPARQLPrefixes(); } + public JSAP(Reader in,String[] args) throws SEPAPropertiesException, SEPASecurityException { + super(in); + + load(in, true); + + override(args); + + defaultNamespaces(); + + buildSPARQLPrefixes(); + } + + public JSAP(String url,String[] args) throws SEPAPropertiesException, SEPASecurityException { + super(url); + + Reader in = getReaderFromUrl(url); + load(in, true); + try { + in.close(); + } catch (IOException e) { + throw new SEPAPropertiesException(e); + } + + override(args); + + defaultNamespaces(); + + buildSPARQLPrefixes(); + } + + private void load(Reader in, boolean replace) throws SEPAPropertiesException { + read(in, replace); + try { + in.close(); + } catch (IOException e) { + throw new SEPAPropertiesException(e); + } + + ArrayList files = new ArrayList<>(); + + if (include != null) { + for (JsonElement element : include) + files.add(element.getAsString()); + + include = new JsonArray(); + } + + for (String url : files) { + Reader rd = getReaderFromUrl(url); + load(rd,replace); + try { + rd.close(); + } catch (IOException e) { + throw new SEPAPropertiesException(e); + } + } + + } + + public void read(Reader in, boolean replace) { + JSAP jsap = new Gson().fromJson(in, JSAP.class); + + if (replace) { + this.include = jsap.include; + this.host = jsap.host; + this.sparql11protocol = jsap.sparql11protocol; + this.sparql11seprotocol = jsap.sparql11seprotocol; + this.extended = jsap.extended; + this.graphs = jsap.graphs; + this.namespaces = jsap.namespaces; + this.queries = jsap.queries; + this.updates = jsap.updates; + } else { + merge(jsap); + } + + buildSPARQLPrefixes(); + } + + /** + * Parse the file and merge the content with the actual JSAP object. Primitive + * values are replaced if replace = true. + * + * @throws IOException + * @throws FileNotFoundException + * @throws SEPAPropertiesException + * @throws SEPASecurityException + */ + public void read(String filename, boolean replace) throws SEPAPropertiesException, SEPASecurityException { + Reader in = getReaderFromUrl(filename); + read(in,replace); + try { + in.close(); + } catch (IOException e) { + throw new SEPAPropertiesException(e); + } + } + + public void read(String filename) throws SEPAPropertiesException, SEPASecurityException { + read(filename,true); + } + private JSAP merge(JSAP temp) { host = (temp.host != null ? temp.host : this.host); @@ -301,94 +412,6 @@ public JsonObject getExtendedData() { return extended; } - private void __read(Reader in, boolean replace) { - JSAP jsap = new Gson().fromJson(in, JSAP.class); - - if (replace) { - this.include = jsap.include; - this.host = jsap.host; - this.sparql11protocol = jsap.sparql11protocol; - this.sparql11seprotocol = jsap.sparql11seprotocol; - this.extended = jsap.extended; - this.graphs = jsap.graphs; - this.namespaces = jsap.namespaces; - this.queries = jsap.queries; - this.updates = jsap.updates; - } else { - merge(jsap); - } - - buildSPARQLPrefixes(); - } - - private void loadFromFile(String uri, boolean replace) throws SEPAPropertiesException, SEPASecurityException { - read(uri, replace); - - ArrayList files = new ArrayList<>(); - - if (include != null) { - for (JsonElement element : include) - files.add(element.getAsString()); - - include = new JsonArray(); - } - - for (String file : files) { - String path = new File(uri).getParent(); - if (path == null) loadFromFile(file, false); - else loadFromFile(path + File.separator + file, false); - } - - } - - public void read(Reader input, boolean replace) throws SEPAPropertiesException, SEPASecurityException { - //__read(new InputStreamReader(input), replace); - __read(input, replace); - } - - /** - * Parse the file and merge the content with the actual JSAP object. Primitive - * values are replaced if replace = true. - * - * @throws IOException - * @throws FileNotFoundException - * @throws SEPAPropertiesException - * @throws SEPASecurityException - */ - public void read(String filename, boolean replace) throws SEPAPropertiesException, SEPASecurityException { - FileReader in; - try { - in = new FileReader(filename); - } catch (FileNotFoundException e) { - throw new SEPAPropertiesException(e.getMessage()); - } - - __read(in, replace); - - try { - in.close(); - } catch (IOException e) { - Logging.logger.error(e.getMessage()); - } - } - - public void write() throws SEPAPropertiesException { - write(getFileName()); - } - - public void write(String fileName) throws SEPAPropertiesException { - FileWriter out; - try { - out = new FileWriter(fileName); - out.write(this.toString()); - out.close(); - } catch (IOException e) { - if (Logging.logger.isTraceEnabled()) - e.printStackTrace(); - throw new SEPAPropertiesException(e.getMessage()); - } - } - // TODO: add the query as parameter and get just the namespaces required public String getPrefixes() { return prefixes; @@ -856,10 +879,6 @@ public ForcedBindings getQueryBindings(String id) throws IllegalArgumentExceptio return ret; } - public String getFileName() { - return propertiesFile; - } - public String toString() { return new Gson().toJson(this); } diff --git a/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/Producer.java b/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/Producer.java index 19a50341..b60ac264 100644 --- a/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/Producer.java +++ b/client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/Producer.java @@ -42,8 +42,8 @@ public Producer(JSAP appProfile, String updateID) super(appProfile); if (appProfile.getSPARQLUpdate(updateID) == null) { - Logging.logger.fatal("UPDATE ID [" + updateID + "] not found in " + appProfile.getFileName()); - throw new IllegalArgumentException("UPDATE ID [" + updateID + "] not found in " + appProfile.getFileName()); + Logging.logger.fatal("UPDATE ID [" + updateID + "] not found"); + throw new IllegalArgumentException("UPDATE ID [" + updateID + "] not found"); } SPARQL_ID = updateID; diff --git a/client-api/src/test/java/it/unibo/arces/wot/sepa/ConfigurationProvider.java b/client-api/src/test/java/it/unibo/arces/wot/sepa/ConfigurationProvider.java index bd8052b2..a7ae7d6f 100644 --- a/client-api/src/test/java/it/unibo/arces/wot/sepa/ConfigurationProvider.java +++ b/client-api/src/test/java/it/unibo/arces/wot/sepa/ConfigurationProvider.java @@ -15,6 +15,7 @@ import java.io.Closeable; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.UUID; @@ -50,7 +51,12 @@ public ConfigurationProvider() throws SEPAPropertiesException, SEPASecurityExcep Logging.logger.debug("Loading JSAP from: " + jsapPath); - appProfile = new JSAP(jsapPath); + try { + appProfile = new JSAP(jsapPath); + } catch (SEPAPropertiesException e) { + Logging.logger.error(e.getMessage()); + throw new RuntimeException(e); + } prefixes = appProfile.getPrefixes(); @@ -146,7 +152,7 @@ private ClientSecurityManager buildSecurityManager() throws SEPASecurityExceptio public String getClientId() throws SEPAPropertiesException, SEPASecurityException { if (appProfile.isSecure()) { - OAuthProperties oauth = new OAuthProperties(appProfile.getFileName()); + OAuthProperties oauth = new OAuthProperties(appProfile); if (oauth.getProvider().equals(OAUTH_PROVIDER.SEPA)) return "SEPATest"; else if (oauth.getProvider().equals(OAUTH_PROVIDER.KEYCLOAK)) diff --git a/engine/src/main/java/it/unibo/arces/wot/sepa/engine/core/EngineProperties.java b/engine/src/main/java/it/unibo/arces/wot/sepa/engine/core/EngineProperties.java index 82108a8b..aa1990f5 100644 --- a/engine/src/main/java/it/unibo/arces/wot/sepa/engine/core/EngineProperties.java +++ b/engine/src/main/java/it/unibo/arces/wot/sepa/engine/core/EngineProperties.java @@ -18,6 +18,7 @@ */ package it.unibo.arces.wot.sepa.engine.core; +import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; @@ -311,7 +312,7 @@ public EngineProperties(String[] args) throws SEPASecurityException { try { endpointProperties = new SPARQL11Properties(endpointJpar); - } catch (SEPAPropertiesException e) { + } catch (SEPAPropertiesException e) { //e.printStackTrace(); Logging.logger.error("Endpoint configuration file not found: "+endpointJpar+"USING DEFAULTS: Jena in memory"); endpointProperties = new SPARQL11Properties("in-memory",ProtocolScheme.jena_api); @@ -520,10 +521,10 @@ private void setParameter(String key,String value) { parameters.gates.paths.unsubscribe = value; break; - case "host": + case "-host": endpointProperties.setHost(value); break; - case "sparql11protocol.protocol": + case "-sparql11protocol.protocol": if (value.toLowerCase().equals("http")) endpointProperties.setProtocolScheme(ProtocolScheme.http); else if (value.toLowerCase().equals("https")) endpointProperties.setProtocolScheme(ProtocolScheme.https); else endpointProperties.setProtocolScheme(ProtocolScheme.jena_api); @@ -534,12 +535,12 @@ private void setParameter(String key,String value) { case "-sparql11protocol.query.path": endpointProperties.setQueryPath(value); break; - case "sparql11protocol.query.method": + case "-sparql11protocol.query.method": if (value.toLowerCase().equals("get")) endpointProperties.setQueryMethod(QueryHTTPMethod.GET); else if (value.toLowerCase().equals("post")) endpointProperties.setQueryMethod(QueryHTTPMethod.POST); else endpointProperties.setQueryMethod(QueryHTTPMethod.URL_ENCODED_POST); break; - case "sparql11protocol.query.format": + case "-sparql11protocol.query.format": if (value.toLowerCase().equals("json")) endpointProperties.setQueryAcceptHeader(QueryResultsFormat.JSON); else if (value.toLowerCase().equals("csv")) endpointProperties.setQueryAcceptHeader(QueryResultsFormat.CSV); else endpointProperties.setQueryAcceptHeader(QueryResultsFormat.XML); @@ -547,11 +548,11 @@ private void setParameter(String key,String value) { case "-sparql11protocol.update.path": endpointProperties.setUpdatePath(value); break; - case "sparql11protocol.update.method": + case "-sparql11protocol.update.method": if (value.toLowerCase().equals("post")) endpointProperties.setUpdateMethod(UpdateHTTPMethod.POST); else endpointProperties.setUpdateMethod(UpdateHTTPMethod.URL_ENCODED_POST); break; - case "sparql11protocol.update.format": + case "-sparql11protocol.update.format": if (value.toLowerCase().equals("json")) endpointProperties.setUpdateAcceptHeader(UpdateResultsFormat.JSON); else endpointProperties.setUpdateAcceptHeader(UpdateResultsFormat.HTML); break;