Skip to content

Commit

Permalink
feat: new JSAP
Browse files Browse the repository at this point in the history
  • Loading branch information
lroffia committed Oct 31, 2023
1 parent 206fc0d commit e8bf89b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -191,13 +195,13 @@ public SPARQL11Properties(Reader in) throws SEPAPropertiesException {
protected void override(String[] args) {
Map<String, String> 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]);
}
}
Expand All @@ -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);
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
97 changes: 25 additions & 72 deletions client-api/src/main/java/it/unibo/arces/wot/sepa/pattern/JSAP.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand All @@ -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<String> files = new ArrayList<>();
//Include
ArrayList<String> 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);
}

}
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit e8bf89b

Please sign in to comment.