Search in sources :

Example 6 with SEPAPropertiesException

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);
}
Also used : SEPAPropertiesException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException) JsonObject(com.google.gson.JsonObject) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse) UpdateResponse(it.unibo.arces.wot.sepa.commons.response.UpdateResponse) QueryResponse(it.unibo.arces.wot.sepa.commons.response.QueryResponse) RegistrationResponse(it.unibo.arces.wot.sepa.commons.response.RegistrationResponse) JsonParser(com.google.gson.JsonParser) JWTResponse(it.unibo.arces.wot.sepa.commons.response.JWTResponse)

Example 7 with SEPAPropertiesException

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);
    }
}
Also used : SEPAPropertiesException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException) SEPAPropertiesException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException)

Example 8 with SEPAPropertiesException

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() + "\""));
    }
}
Also used : SEPAPropertiesException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException) SEPAPropertiesException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException) JsonParser(com.google.gson.JsonParser)

Aggregations

SEPAPropertiesException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException)8 SEPASecurityException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)4 JsonParser (com.google.gson.JsonParser)2 SEPAProtocolException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 SPARQL11SEProperties (it.unibo.arces.wot.sepa.api.SPARQL11SEProperties)1 SPARQL11SEProtocol (it.unibo.arces.wot.sepa.api.SPARQL11SEProtocol)1 ErrorResponse (it.unibo.arces.wot.sepa.commons.response.ErrorResponse)1 JWTResponse (it.unibo.arces.wot.sepa.commons.response.JWTResponse)1 QueryResponse (it.unibo.arces.wot.sepa.commons.response.QueryResponse)1 RegistrationResponse (it.unibo.arces.wot.sepa.commons.response.RegistrationResponse)1 UpdateResponse (it.unibo.arces.wot.sepa.commons.response.UpdateResponse)1 ApplicationProfile (it.unibo.arces.wot.sepa.pattern.ApplicationProfile)1 FileInputStream (java.io.FileInputStream)1 Date (java.util.Date)1 NoSuchElementException (java.util.NoSuchElementException)1 Scanner (java.util.Scanner)1