Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
lroffia committed Sep 11, 2023
2 parents 6e3237b + 67eaeda commit d0d7e67
Show file tree
Hide file tree
Showing 72 changed files with 2,374 additions and 2,519 deletions.
25 changes: 12 additions & 13 deletions client-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
</parent>
<artifactId>client-api</artifactId>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</build>
<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-surefire-plugin</artifactId>-->
<!-- <version>3.1.2</version>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
Expand All @@ -34,19 +34,19 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.6</version>
<version>4.4.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.26</version>
</dependency>
<!-- 1.12 -->
<!-- 2.0.2 -->
<dependency>
<groupId>org.glassfish.tyrus.bundles</groupId>
<artifactId>tyrus-standalone-client</artifactId>
<version>2.0.2</version>
<version>2.1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
Expand All @@ -55,7 +55,6 @@
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.nimbusds/nimbus-jose-jwt -->

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

package it.unibo.arces.wot.sepa.api;

import com.google.gson.JsonElement;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;

import it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException;
import it.unibo.arces.wot.sepa.commons.protocol.SPARQL11Properties;
Expand All @@ -32,7 +35,9 @@
*
* <pre>
"sparql11seprotocol": {
"host" : "override" (optional)
"protocol": "ws",
"reconnect" : true, (optional),
"availableProtocols": {
"ws": {
"port": 9000,
Expand All @@ -47,6 +52,7 @@
*/
public class SPARQL11SEProperties extends SPARQL11Properties {
/**
*
* The primitives introduced by the SPARQL 1.1 SE Protocol are:
*
* SECUREUPDATE,SECUREQUERY,SUBSCRIBE,SECURESUBSCRIBE,UNSUBSCRIBE,SECUREUNSUBSCRIBE,REGISTER,REQUESTTOKEN
Expand Down Expand Up @@ -74,148 +80,124 @@ public enum SPARQL11SEPrimitive {
SECUREQUERY
}

public enum SubscriptionProtocol {
WS, WSS
// Members
protected SPARQL11SEProtocol sparql11seprotocol;

public static class SubscriptionProtocol {
public String path = null;
public int port = -1;
public String scheme = null;
}

protected static class SPARQL11SEProtocol {
public String protocol = null;
public HashMap<String,SubscriptionProtocol> availableProtocols = null;
public String host = null;
public boolean reconnect = true;

public SPARQL11SEProtocol merge(SPARQL11SEProtocol temp) {
if (temp != null) {
protocol = (temp.protocol != null ? temp.protocol : protocol);
host = (temp.host != null ? temp.host : host);
reconnect = temp.reconnect;
availableProtocols = (temp.availableProtocols != null ? temp.availableProtocols : availableProtocols);
}
return this;
}

public int getPort() {
return availableProtocols.get(protocol).port;
}

public String getPath() {
return availableProtocols.get(protocol).path;
}

public SubscriptionProtocol getSubscriptionProtocol() {
return availableProtocols.get(protocol);
}
}

/**
* Instantiates a new SPARQL 11 SE properties.
*
* @param propertiesFile
* the properties file
* @param propertiesFile the properties file
* @throws SEPAPropertiesException
*/
public SPARQL11SEProperties(String propertiesFile,boolean validate) throws SEPAPropertiesException {
super(propertiesFile,validate);
}

public SPARQL11SEProperties(String propertiesFile) throws SEPAPropertiesException {
super(propertiesFile,false);
}
super(propertiesFile);

public SPARQL11SEProperties() {
super();
}

public String toString() {
return jsap.toString();
}

/**
* <pre>
"sparql11seprotocol": {
"protocol": "ws",
"availableProtocols": {
"ws": {
"port": 9000,
"path": "/subscribe"
},
"wss": {
"port": 9443,
"path": "/secure/subscribe"
}
SPARQL11SEProperties jsap;
try {
jsap = new Gson().fromJson(new FileReader(propertiesFile), SPARQL11SEProperties.class);
sparql11seprotocol = jsap.sparql11seprotocol;
} catch (JsonSyntaxException | JsonIOException | FileNotFoundException e2) {
Logging.logger.error(e2.getMessage());
e2.printStackTrace();
throw new SEPAPropertiesException(e2);
}
* </pre>
*/

@Override
protected void defaults() {
super.defaults();

JsonObject sparql11seprotocol = new JsonObject();
sparql11seprotocol.add("protocol", new JsonPrimitive("ws"));

JsonObject availableProtocols = new JsonObject();
JsonObject ws = new JsonObject();
JsonObject wss = new JsonObject();
ws.add("port", new JsonPrimitive(9000));
ws.add("path", new JsonPrimitive("/subscribe"));
availableProtocols.add("ws", ws);
ws.add("port", new JsonPrimitive(9443));
ws.add("path", new JsonPrimitive("/subscribe"));
availableProtocols.add("wss", wss);
sparql11seprotocol.add("availableProtocols", availableProtocols);

jsap.add("sparql11seprotocol", sparql11seprotocol);

// 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());
// }
//
// }


}

@Override
protected void validate() throws SEPAPropertiesException {
super.validate();
// public SPARQL11SEProperties() {
// super();
// sparql11seprotocol = new SPARQL11SEProtocol();
// }

try {
JsonElement sparql11seprotocol = jsap.get("sparql11seprotocol");
String protocol = sparql11seprotocol.getAsJsonObject().get("protocol").getAsString();

jsap.get("sparql11seprotocol").getAsJsonObject().get("availableProtocols").getAsJsonObject().get(protocol);

switch (protocol) {
case "ws":
jsap.get("sparql11seprotocol").getAsJsonObject().get("availableProtocols").getAsJsonObject()
.get(protocol).getAsJsonObject().get("port").getAsInt();
jsap.get("sparql11seprotocol").getAsJsonObject().get("availableProtocols").getAsJsonObject()
.get(protocol).getAsJsonObject().get("path").getAsString();

break;
case "wss":
jsap.get("sparql11seprotocol").getAsJsonObject().get("availableProtocols").getAsJsonObject()
.get(protocol).getAsJsonObject().get("port").getAsInt();
jsap.get("sparql11seprotocol").getAsJsonObject().get("availableProtocols").getAsJsonObject()
.get(protocol).getAsJsonObject().get("path").getAsString();
break;
}
} catch (Exception e) {
throw new SEPAPropertiesException(e);
}
public String toString() {
return new Gson().toJson(this);
}

public String getSubscribeHost() {
try {
return jsap.get("sparql11seprotocol").getAsJsonObject().get("host").getAsString();
}
catch(Exception e) {
return super.getHost();
}
return (sparql11seprotocol.host != null ? sparql11seprotocol.host : super.host);
}

public void setHost(String host) {
jsap.get("sparql11seprotocol").getAsJsonObject().add("host",new JsonPrimitive(host));
sparql11seprotocol.host = host;
}

public String getSubscribePath() {
try {
return jsap.get("sparql11seprotocol").getAsJsonObject().get("availableProtocols").getAsJsonObject()
.get(jsap.get("sparql11seprotocol").getAsJsonObject().get("protocol").getAsString()).getAsJsonObject().get("path").getAsString();

} catch (Exception e) {
Logging.logger.error(e.getMessage());
return null;
}
return sparql11seprotocol.availableProtocols.get(sparql11seprotocol.protocol).path;
}

public void setSubscribePath(String path) {
jsap.get("sparql11seprotocol").getAsJsonObject().get("availableProtocols").getAsJsonObject()
.get(jsap.get("sparql11seprotocol").getAsJsonObject().get("protocol").getAsString()).getAsJsonObject().add("path",new JsonPrimitive(path));
sparql11seprotocol.availableProtocols.get(sparql11seprotocol.protocol).path = path;
}

public int getSubscribePort() {
try {
return jsap.get("sparql11seprotocol").getAsJsonObject().get("availableProtocols").getAsJsonObject()
.get(jsap.get("sparql11seprotocol").getAsJsonObject().get("protocol").getAsString()).getAsJsonObject().get("port").getAsInt();

} catch (Exception e) {
Logging.logger.error(e.getMessage());
return -1;
}
return sparql11seprotocol.availableProtocols.get(sparql11seprotocol.protocol).port;
}

public void setSubscribePort(int port) {
jsap.get("sparql11seprotocol").getAsJsonObject().get("availableProtocols").getAsJsonObject()
.get(jsap.get("sparql11seprotocol").getAsJsonObject().get("protocol").getAsString()).getAsJsonObject().add("port",new JsonPrimitive(port));
sparql11seprotocol.availableProtocols.get(sparql11seprotocol.protocol).port = port;
}

public SubscriptionProtocol getSubscriptionProtocol() {
if(jsap.get("sparql11seprotocol").getAsJsonObject().get("protocol").getAsString().toUpperCase().equals("WSS")) return SubscriptionProtocol.WSS;
return SubscriptionProtocol.WS;
return sparql11seprotocol.availableProtocols.get(sparql11seprotocol.protocol);
}

public boolean getReconnect() {
return sparql11seprotocol.reconnect;
}

public void setSubscriptionProtocol(String scheme) {
sparql11seprotocol.protocol = scheme;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public SPARQL11SEProtocol(SubscriptionProtocol protocol,ClientSecurityManager sm
* @throws SEPASecurityException
*/
public void subscribe(SubscribeRequest request) throws SEPAProtocolException, SEPASecurityException {
Logging.logger.debug("SUBSCRIBE: "+request.toString());
Logging.logger.trace("SUBSCRIBE: "+request.toString());

subscriptionProtocol.subscribe(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
import org.glassfish.tyrus.client.ClientProperties;
import org.glassfish.tyrus.client.SslEngineConfigurator;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;

import it.unibo.arces.wot.sepa.api.ISubscriptionHandler;
import it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException;
Expand Down Expand Up @@ -85,7 +85,7 @@ public void close() throws IOException {
}

public void onOpen(Session session, EndpointConfig config) {
Logging.logger.debug("@onOpen session: " + session.getId());
Logging.logger.trace("@onOpen session: " + session.getId());

this.session = session;

Expand All @@ -97,7 +97,7 @@ public void onMessage(String message) {
// Parse message
JsonObject jsonMessage = null;
try {
jsonMessage = JsonParser.parseString(message).getAsJsonObject();
jsonMessage = new Gson().fromJson(message, JsonObject.class);
} catch (Exception e) {
Logging.logger.error("Exception on parsing message: " + message + " exception: " + e.getMessage());
return;
Expand Down Expand Up @@ -177,7 +177,7 @@ public void onError(Session session, Throwable thr) {
ErrorResponse error = null;
try{
msg = msg.substring(0, msg.lastIndexOf('}')+1);
JsonObject err = JsonParser.parseString(msg).getAsJsonObject();
JsonObject err = new Gson().fromJson(msg, JsonObject.class);
error = new ErrorResponse(err.get("status_code").getAsInt(), err.get("error").getAsString(), err.get("error_description").getAsString());
}
catch(JsonParseException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public WebsocketSubscriptionProtocol(String scheme, String host, int port, Strin

mutex = new Object();

if (!scheme.equals("ws") && scheme.equals("wss"))
if (!scheme.equals("ws") && !scheme.equals("wss"))
throw new SEPAProtocolException("Scheme must be 'ws' or 'wss'");

if (port == -1)
Expand Down
Loading

0 comments on commit d0d7e67

Please sign in to comment.