Search in sources :

Example 26 with SEPASecurityException

use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.

the class SPUNaive method postUpdateInternalProcessing.

@Override
public Notification postUpdateInternalProcessing(UpdateResponse res) throws SEPAProcessingException {
    logger.log(Level.getLevel("spu"), "@postUpdateInternalProcessing");
    Response ret = null;
    // Query the SPARQL processing service
    try {
        logger.log(Level.getLevel("spu"), "Query endpoint");
        ret = manager.processQuery(subscribe);
    } catch (SEPASecurityException | IOException e) {
        if (logger.isTraceEnabled())
            e.printStackTrace();
        logger.log(Level.getLevel("spu"), "SEPASecurityException " + e.getMessage());
        throw new SEPAProcessingException("postUpdateInternalProcessing exception " + e.getMessage());
    }
    if (ret.isError()) {
        logger.log(Level.getLevel("spu"), "SEPAProcessingException " + ret);
        throw new SEPAProcessingException("postUpdateInternalProcessing exception " + ret.toString());
    }
    // Current and previous bindings
    BindingsResults results = ((QueryResponse) ret).getBindingsResults();
    BindingsResults currentBindings = new BindingsResults(results);
    // Initialize the results with the current bindings
    BindingsResults added = new BindingsResults(results.getVariables(), null);
    BindingsResults removed = new BindingsResults(results.getVariables(), null);
    // Create empty bindings if null
    if (lastBindings == null)
        lastBindings = new BindingsResults(null, null);
    logger.trace("Current bindings: " + currentBindings);
    logger.trace("Last bindings: " + lastBindings);
    // Find removed bindings
    long start = System.nanoTime();
    for (Bindings solution : lastBindings.getBindings()) {
        if (!results.contains(solution) && !solution.isEmpty())
            removed.add(solution);
        else
            results.remove(solution);
    }
    long stop = System.nanoTime();
    logger.trace("Removed bindings: " + removed + " found in " + (stop - start) + " ns");
    // Find added bindings
    start = System.nanoTime();
    for (Bindings solution : results.getBindings()) {
        if (!lastBindings.contains(solution) && !solution.isEmpty())
            added.add(solution);
    }
    stop = System.nanoTime();
    logger.trace("Added bindings: " + added + " found in " + (stop - start) + " ns");
    // Update the last bindings with the current ones
    lastBindings = currentBindings;
    // Send notification (or end processing indication)
    if (!added.isEmpty() || !removed.isEmpty()) {
        logger.log(Level.getLevel("spu"), "Send notification");
        return new Notification(getSPUID(), new ARBindingsResults(added, removed));
    }
    logger.log(Level.getLevel("spu"), "Nothing to be notified");
    return null;
}
Also used : Response(it.unibo.arces.wot.sepa.commons.response.Response) SubscribeResponse(it.unibo.arces.wot.sepa.commons.response.SubscribeResponse) UpdateResponse(it.unibo.arces.wot.sepa.commons.response.UpdateResponse) QueryResponse(it.unibo.arces.wot.sepa.commons.response.QueryResponse) ARBindingsResults(it.unibo.arces.wot.sepa.commons.sparql.ARBindingsResults) BindingsResults(it.unibo.arces.wot.sepa.commons.sparql.BindingsResults) ARBindingsResults(it.unibo.arces.wot.sepa.commons.sparql.ARBindingsResults) QueryResponse(it.unibo.arces.wot.sepa.commons.response.QueryResponse) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) IOException(java.io.IOException) Bindings(it.unibo.arces.wot.sepa.commons.sparql.Bindings) SEPAProcessingException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAProcessingException) Notification(it.unibo.arces.wot.sepa.commons.response.Notification)

Example 27 with SEPASecurityException

use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.

the class SPARQL11Handler method handle.

@Override
public void handle(HttpRequest request, HttpAsyncExchange httpExchange, HttpContext context) throws HttpException, IOException {
    logger.log(Level.getLevel("http"), "@handle " + request + " " + context);
    // CORS
    if (!corsHandling(httpExchange))
        return;
    // Authorize
    ClientAuthorization oauth = null;
    try {
        oauth = authorize(httpExchange.getRequest());
    } catch (SEPASecurityException e1) {
        HttpUtilities.sendFailureResponse(httpExchange, new ErrorResponse(HttpStatus.SC_UNAUTHORIZED, "oauth_exception", e1.getMessage()));
        jmx.authorizingFailed();
        return;
    }
    if (!oauth.isAuthorized()) {
        logger.log(Level.getLevel("oauth"), "*** NOT AUTHORIZED *** " + oauth.getDescription());
        HttpUtilities.sendFailureResponse(httpExchange, new ErrorResponse(HttpStatus.SC_UNAUTHORIZED, oauth.getError(), oauth.getDescription()));
        jmx.authorizingFailed();
        return;
    }
    InternalUQRequest sepaRequest = null;
    try {
        // Parsing SPARQL 1.1 request and attach a token
        sepaRequest = parse(httpExchange, oauth);
    } catch (SPARQL11ProtocolException e) {
        logger.log(Level.getLevel("http"), "Parsing failed: " + httpExchange.getRequest());
        HttpUtilities.sendFailureResponse(httpExchange, new ErrorResponse(e.getCode(), "SPARQL11ProtocolException", "Parsing failed: " + e.getMessage()));
        jmx.parsingFailed();
        return;
    }
    // Schedule request
    Timings.log(sepaRequest);
    ScheduledRequest req = scheduler.schedule(sepaRequest, new SPARQL11ResponseHandler(httpExchange, jmx));
    if (req == null) {
        logger.error("Out of tokens");
        HttpUtilities.sendFailureResponse(httpExchange, new ErrorResponse(429, "too_many_requests", "Too many pending requests"));
        jmx.outOfTokens();
    }
}
Also used : ScheduledRequest(it.unibo.arces.wot.sepa.engine.scheduling.ScheduledRequest) ClientAuthorization(it.unibo.arces.wot.sepa.commons.security.ClientAuthorization) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) InternalUQRequest(it.unibo.arces.wot.sepa.engine.scheduling.InternalUQRequest) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse)

Example 28 with SEPASecurityException

use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.

the class SSLManager method getSSLHttpClient.

public CloseableHttpClient getSSLHttpClient(String jksName, String jksPassword) throws SEPASecurityException {
    // Trust own CA and all self-signed certificates and allow the specified
    // protocols
    LayeredConnectionSocketFactory sslsf = null;
    try {
        SSLContext ctx = SSLContexts.custom().loadTrustMaterial(new File(jksName), jksPassword.toCharArray(), new TrustSelfSignedStrategy()).build();
        sslsf = new SSLConnectionSocketFactory(ctx, protocols, null, this);
    } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException e) {
        logger.error(e.getMessage());
        if (logger.isTraceEnabled())
            e.printStackTrace();
        throw new SEPASecurityException(e.getMessage());
    }
    HttpClientBuilder clientFactory = HttpClients.custom().setSSLSocketFactory(sslsf);
    return clientFactory.build();
}
Also used : LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeyManagementException(java.security.KeyManagementException) File(java.io.File) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Example 29 with SEPASecurityException

use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.

the class Consumer method onBrokenConnection.

@Override
public void onBrokenConnection(ErrorResponse errorResponse) {
    logger.warn("onBrokenConnection");
    subscribed = false;
    // Auto reconnection mechanism
    if (appProfile.reconnect()) {
        try {
            protocol = new WebsocketSubscriptionProtocol(appProfile.getSubscribeHost(subID), appProfile.getSubscribePort(subID), appProfile.getSubscribePath(subID), this, sm);
            client = new SPARQL11SEProtocol(protocol, sm);
        } catch (SEPASecurityException | SEPAProtocolException e1) {
            logger.error(e1.getMessage());
            return;
        }
        while (!subscribed) {
            try {
                subscribe(TIMEOUT, NRETRY);
            } catch (SEPASecurityException | SEPAPropertiesException | SEPAProtocolException | SEPABindingsException e) {
                logger.error(e.getMessage());
                if (logger.isTraceEnabled())
                    e.printStackTrace();
            }
            try {
                synchronized (client) {
                    client.wait(TIMEOUT);
                }
            } catch (InterruptedException e) {
                logger.error(e.getMessage());
                if (logger.isTraceEnabled())
                    e.printStackTrace();
            }
        }
    }
}
Also used : SPARQL11SEProtocol(it.unibo.arces.wot.sepa.api.SPARQL11SEProtocol) SEPAProtocolException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException) SEPABindingsException(it.unibo.arces.wot.sepa.commons.exceptions.SEPABindingsException) SEPAPropertiesException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) WebsocketSubscriptionProtocol(it.unibo.arces.wot.sepa.api.protocols.websocket.WebsocketSubscriptionProtocol)

Example 30 with SEPASecurityException

use of it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException in project SEPA by arces-wot.

the class JKSUtil method getRSAKey.

public static RSAKey getRSAKey(String keystore, String storepass, String keyalias, String keypass) throws SEPASecurityException {
    KeyStore jks;
    try {
        jks = KeyStore.getInstance("JKS");
        jks.load(new FileInputStream(keystore), storepass.toCharArray());
        logger.debug(jks);
        return RSAKey.load(jks, keyalias, keypass.toCharArray());
    } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | JOSEException e) {
        throw new SEPASecurityException(e.getMessage());
    }
}
Also used : SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) JOSEException(com.nimbusds.jose.JOSEException) FileInputStream(java.io.FileInputStream)

Aggregations

SEPASecurityException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)69 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)29 IOException (java.io.IOException)20 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)18 ErrorResponse (it.unibo.arces.wot.sepa.commons.response.ErrorResponse)15 Response (it.unibo.arces.wot.sepa.commons.response.Response)12 SEPAPropertiesException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException)11 SEPAProtocolException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException)10 JsonObject (com.google.gson.JsonObject)7 JsonParser (com.google.gson.JsonParser)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)7 Modification (org.apache.directory.api.ldap.model.entry.Modification)7 SEPABindingsException (it.unibo.arces.wot.sepa.commons.exceptions.SEPABindingsException)5 Credentials (it.unibo.arces.wot.sepa.commons.security.Credentials)5 HttpEntity (org.apache.http.HttpEntity)5 JOSEException (com.nimbusds.jose.JOSEException)4 SignedJWT (com.nimbusds.jwt.SignedJWT)4 JWTResponse (it.unibo.arces.wot.sepa.commons.response.JWTResponse)4 ParseException (java.text.ParseException)4