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 7ec5b997..861dabb6 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 @@ -20,7 +20,9 @@ import java.io.*; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; +import java.nio.file.Path; import java.util.Map; import java.util.Objects; import java.util.Set; @@ -136,10 +138,12 @@ public SPARQL11Properties(String args[]) { override(args); } - public SPARQL11Properties(String jsapFile,String[] args) throws SEPAPropertiesException { + public SPARQL11Properties(String uri,String[] args) throws SEPAPropertiesException { this(); - Reader in = getReaderFromUrl(jsapFile); + Path path = Path.of(uri); + + Reader in = getReaderFromUri(uri); parseJSAP(in); try { in.close(); @@ -149,13 +153,13 @@ public SPARQL11Properties(String jsapFile,String[] args) throws SEPAPropertiesEx override(args); - filename = jsapFile; + filename = uri; } public SPARQL11Properties(String jsapFile) throws SEPAPropertiesException { this(); - Reader in = getReaderFromUrl(jsapFile); + Reader in = getReaderFromUri(jsapFile); parseJSAP(in); try { in.close(); @@ -191,13 +195,13 @@ public SPARQL11Properties(Reader in) 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)); + Logging.logger.trace("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]); + Logging.logger.trace("Argument "+args[i]+" : "+args[i+1]); setParameter(args[i], args[i+1]); } } @@ -208,7 +212,6 @@ private void parseJSAP(Reader in) throws SEPAPropertiesException { jsap = new Gson().fromJson(in, SPARQL11Properties.class); } catch (JsonSyntaxException | JsonIOException e) { Logging.logger.error(e.getMessage()); - e.printStackTrace(); throw new SEPAPropertiesException(e); } @@ -227,28 +230,23 @@ private void parseJSAP(Reader in) throws SEPAPropertiesException { * 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; + protected Reader getReaderFromUri(String uri) throws SEPAPropertiesException { + Reader in; - Logging.logger.info("Get reader from URL: "+url); + Logging.logger.info("Get stream reader from URI: "+uri); try { - jsap = new URL(url); + in = new BufferedReader( + new InputStreamReader(URI.create(uri).toURL().openStream())); + } catch (IOException | IllegalArgumentException e) { + Logging.logger.warn("Failed to get input stream: "+e.getMessage()); 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()))); - } + Logging.logger.info("Get file reader from URI: "+uri); + in = new FileReader(Path.of(uri).toFile()); + } catch (FileNotFoundException ex) { + Logging.logger.warn("Failed to get file reader: "+ex.getMessage()); + Logging.logger.info("Get resource from URI: "+uri); + in = new InputStreamReader(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream(uri))); } - } catch (MalformedURLException e) { - Logging.logger.error(e.getMessage()); - throw new SEPAPropertiesException(e); } return in; 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 4c94c7cd..11adc036 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 @@ -112,7 +112,7 @@ public SPARQL11SEProperties(Reader in,String[] args) throws SEPAPropertiesExcept public SPARQL11SEProperties(String propertiesFile,String[] args) throws SEPAPropertiesException { super(propertiesFile); - Reader in = getReaderFromUrl(propertiesFile); + Reader in = getReaderFromUri(propertiesFile); parseJSAP(in); try { in.close(); @@ -126,7 +126,7 @@ public SPARQL11SEProperties(String propertiesFile,String[] args) throws SEPAProp public SPARQL11SEProperties(String propertiesFile) throws SEPAPropertiesException { super(propertiesFile); - Reader in = getReaderFromUrl(propertiesFile); + Reader in = getReaderFromUri(propertiesFile); parseJSAP(in); try { in.close(); diff --git a/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/security/SSLManager.java b/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/security/SSLManager.java index 5114248c..72a635e9 100644 --- a/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/security/SSLManager.java +++ b/client-api/src/main/java/it/unibo/arces/wot/sepa/commons/security/SSLManager.java @@ -199,7 +199,7 @@ public SSLContext getSSLContextFromJKS(String jksName, String jksPassword) throw sslContext = SSLContexts .custom() .loadKeyMaterial(new File(jksName), jksPassword.toCharArray(), jksPassword.toCharArray()) - .setProtocol("TLS") + .useProtocol("TLS") .build(); } catch (NoSuchAlgorithmException e) { Logging.logger.error("getSSLContextFromJKS jksName:"+jksName+" jksPassword:"+jksPassword+" error:"+e.getMessage()); 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 49139902..e30f6e7d 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 @@ -21,6 +21,8 @@ import java.io.*; import java.net.URI; import java.net.URISyntaxException; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; import java.util.*; import java.util.Map.Entry; @@ -199,58 +201,14 @@ private void defaultNamespaces() { namespaces.put("xsd", "http://www.w3.org/2001/XMLSchema#"); } - 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); - - defaultNamespaces(); - - buildSPARQLPrefixes(); - } - - public JSAP(Reader in,String[] args) throws SEPAPropertiesException, SEPASecurityException { - super(in); - - load(in, true); - - override(args); - - defaultNamespaces(); - - buildSPARQLPrefixes(); + public JSAP(String uri) throws SEPAPropertiesException, SEPASecurityException { + this(uri,null); } - public JSAP(String url,String[] args) throws SEPAPropertiesException, SEPASecurityException { - super(url); + public JSAP(String uri,String[] args) throws SEPAPropertiesException, SEPASecurityException { + super(uri); - Reader in = getReaderFromUrl(url); - load(in, true); - try { - in.close(); - } catch (IOException e) { - throw new SEPAPropertiesException(e); - } + load(uri, true); override(args); @@ -259,31 +217,31 @@ public JSAP(String url,String[] args) throws SEPAPropertiesException, SEPASecuri buildSPARQLPrefixes(); } - private void load(Reader in, boolean replace) throws SEPAPropertiesException { - read(in, replace); - try { - in.close(); - } catch (IOException e) { - throw new SEPAPropertiesException(e); - } + private void load(String uri, boolean replace) throws SEPAPropertiesException, SEPASecurityException { + read(uri, replace); - ArrayList files = new ArrayList<>(); + //Include + ArrayList uriList = new ArrayList<>(); if (include != null) { for (JsonElement element : include) - files.add(element.getAsString()); + uriList.add(element.getAsString()); include = new JsonArray(); } - for (String url : files) { - Reader rd = getReaderFromUrl(url); - load(rd,replace); + for (String child : uriList) { + Path path; try { - rd.close(); - } catch (IOException e) { - throw new SEPAPropertiesException(e); + path = Path.of(child); + } + catch (InvalidPathException e) { + load(child,false); + continue; } + + if(path.isAbsolute()) load(child,false); + else load (Path.of(uri).getParent().toString()+File.separator+child,false); } } @@ -312,13 +270,10 @@ public void read(Reader in, boolean 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 { - Reader in = getReaderFromUrl(filename); + public void read(String uri, boolean replace) throws SEPAPropertiesException { + Reader in = getReaderFromUri(uri); read(in,replace); try { in.close(); @@ -331,7 +286,7 @@ public void read(String filename) throws SEPAPropertiesException, SEPASecurityEx read(filename,true); } - private JSAP merge(JSAP temp) { + private void merge(JSAP temp) { host = (temp.host != null ? temp.host : this.host); if (sparql11protocol != null) sparql11protocol.merge(temp.sparql11protocol); @@ -345,8 +300,6 @@ private JSAP merge(JSAP temp) { namespaces = mergeNamespaces(namespaces, temp.namespaces); queries = mergeQueries(queries, temp.queries); updates = mergeUpdates(updates, temp.updates); - - return this; } private JsonObject mergeExtended(JsonObject extended,JsonObject temp) {