use of it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException in project SEPA by arces-wot.
the class SPARQL11SEProtocol method parseSPARQL11SEResponse.
protected Response parseSPARQL11SEResponse(String response, SPARQL11SEPrimitive op) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
if (response == null)
return new ErrorResponse(0, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Response is null");
JsonObject json = null;
try {
json = new JsonParser().parse(response).getAsJsonObject();
} catch (JsonParseException | IllegalStateException e) {
return new ErrorResponse(0, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Unknown response: " + response);
}
// Error response
if (json.get("code") != null)
if (json.get("code").getAsInt() >= 400)
return new ErrorResponse(0, json.get("code").getAsInt(), json.get("body").getAsString());
if (op == SPARQL11SEPrimitive.SECUREQUERY)
return new QueryResponse(json);
if (op == SPARQL11SEPrimitive.SECUREUPDATE)
return new UpdateResponse(response);
if (op == SPARQL11SEPrimitive.REGISTER) {
if (json.get("client_id") != null && json.get("client_secret") != null) {
try {
properties.setCredentials(json.get("client_id").getAsString(), json.get("client_secret").getAsString());
} catch (SEPASecurityException | SEPAPropertiesException e) {
return new ErrorResponse(-1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Failed to save credentials");
}
return new RegistrationResponse(json.get("client_id").getAsString(), json.get("client_secret").getAsString(), json.get("signature"));
}
return new ErrorResponse(-1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Credentials not found in registration response");
}
if (op == SPARQL11SEPrimitive.REQUESTTOKEN) {
if (json.get("access_token") != null && json.get("expires_in") != null && json.get("token_type") != null) {
int seconds = json.get("expires_in").getAsInt();
Date expires = new Date();
expires.setTime(expires.getTime() + (1000 * seconds));
try {
properties.setJWT(json.get("access_token").getAsString(), expires, json.get("token_type").getAsString());
} catch (SEPASecurityException | SEPAPropertiesException e) {
return new ErrorResponse(-1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Failed to save JWT");
}
return new JWTResponse(json.get("access_token").getAsString(), json.get("token_type").getAsString(), json.get("expires_in").getAsLong());
} else if (json.get("code") != null && json.get("body") != null)
return new ErrorResponse(0, json.get("code").getAsInt(), json.get("body").getAsString());
else if (json.get("code") != null)
return new ErrorResponse(0, json.get("code").getAsInt(), "");
return new ErrorResponse(0, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Response not recognized: " + json.toString());
}
return new ErrorResponse(0, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Response unknown: " + response);
}
use of it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException in project SEPA by arces-wot.
the class SPARQL11Properties method validate.
protected void validate() throws SEPAPropertiesException {
try {
parameters.get("host").getAsString();
// parameters.get("ports").getAsJsonObject().get("http").getAsInt();
parameters.get("paths").getAsJsonObject().get("update").getAsString();
parameters.get("paths").getAsJsonObject().get("query").getAsString();
parameters.get("methods").getAsJsonObject().get("update").getAsString();
parameters.get("methods").getAsJsonObject().get("query").getAsString();
parameters.get("formats").getAsJsonObject().get("update").getAsString();
parameters.get("formats").getAsJsonObject().get("query").getAsString();
} catch (Exception e) {
throw new SEPAPropertiesException(e);
}
}
use of it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException in project SEPA by arces-wot.
the class SPARQL11Properties method loadProperties.
private void loadProperties(File jsapFile) throws SEPAPropertiesException {
try (final FileReader in = new FileReader(jsapFile)) {
doc = new JsonParser().parse(in).getAsJsonObject();
if (doc.get("parameters") == null) {
logger.warn("parameters key is missing");
throw new SEPAPropertiesException(new Exception("parameters key is missing"));
}
parameters = doc.get("parameters").getAsJsonObject();
// Validate the JSON elements
validate();
this.propertiesFile = jsapFile;
} catch (Exception e) {
logger.warn(e.getMessage());
defaults();
try {
storeProperties(defaultsFileName);
} catch (Exception e1) {
throw new SEPAPropertiesException(e1);
}
logger.warn("USING DEFAULTS. Edit \"" + defaultsFileName + "\" and rename it to\"" + propertiesFile.getName() + "\"");
throw new SEPAPropertiesException(new Exception("USING DEFAULTS. Edit \"" + defaultsFileName + "\" and rename it to\"" + propertiesFile.getName() + "\""));
}
}
Aggregations