Search in sources :

Example 1 with SPARQL11SEProtocol

use of it.unibo.arces.wot.sepa.api.SPARQL11SEProtocol in project SEPA by arces-wot.

the class SEPATest method main.

public static void main(String[] args) {
    boolean ret = false;
    try {
        properties = new SPARQL11SEProperties("client.jpar");
    } catch (SEPAPropertiesException e2) {
        logger.fatal("JSAP exception: " + e2.getMessage());
        System.exit(1);
    }
    try {
        client = new SPARQL11SEProtocol(properties, handler);
    } catch (SEPAProtocolException e2) {
        logger.fatal(e2.getLocalizedMessage());
        System.exit(1);
    }
    logger.warn("**********************************************************");
    logger.warn("***     SPARQL 1.1 SE Protocol Service test suite      ***");
    logger.warn("**********************************************************");
    logger.warn("***   WARNING: the RDF store content will be ERASED    ***");
    logger.warn("***         Do you want to continue (yes/no)?          ***");
    logger.warn("**********************************************************");
    logger.warn("SPARQL 1.1 SE Protocol Service parameters: " + client.toString());
    Scanner scanner = new Scanner(System.in);
    // "\\z" means end of input
    scanner.useDelimiter("\\n");
    String input = scanner.next();
    if (!input.equals("yes")) {
        scanner.close();
        logger.info("Bye bye! :-)");
        System.exit(0);
    }
    logger.warn("**********************************************************");
    logger.warn("***                Are you sure (yes/no)?              ***");
    logger.warn("**********************************************************");
    input = scanner.next();
    if (!input.equals("yes")) {
        scanner.close();
        logger.info("Bye bye! :-)");
        System.exit(0);
    }
    scanner.close();
    // UPDATE
    ret = updateTest("prefix test:<http://www.vaimee.com/test#> delete {?s ?p ?o} insert {test:Sub test:Pred \"測試\"} where {?s ?p ?o}", false);
    results.addResult("Update", ret);
    if (ret)
        logger.info("Update PASSED");
    else
        logger.error("Update FAILED");
    // QUERY
    ret = queryTest("select ?o where {?s ?p ?o}", "測試", false);
    results.addResult("Query", ret);
    if (ret)
        logger.info("Query PASSED");
    else
        logger.error("Query FAILED");
    // SUBSCRIBE
    ret = subscribeTest("select ?o where {?s ?p ?o}", false);
    results.addResult("Subscribe - request", ret);
    if (ret)
        logger.info("Subscribe PASSED");
    else
        logger.error("Subscribe FAILED");
    // PING
    ret = waitPing();
    results.addResult("Subscribe - ping", ret);
    if (ret)
        logger.info("Ping received PASSED");
    else
        logger.error("Ping recevied FAILED");
    // TRIGGER A NOTIFICATION
    ret = updateTest("prefix test:<http://www.vaimee.com/test#> delete {?s ?p ?o} insert {test:Sub test:Pred \"ვაიმეე\"} where {?s ?p ?o}", false);
    results.addResult("Subscribe - triggering", ret);
    if (ret)
        logger.info("Triggering update PASSED");
    else
        logger.error("Triggering update FAILED");
    // WAIT NOTIFICATION
    ret = waitNotification();
    results.addResult("Subscribe - notification", ret);
    if (ret)
        logger.info("Notification PASSED");
    else
        logger.error("Notification FAILED");
    // UNSUBSCRIBE
    ret = unsubscribeTest(spuid, false);
    results.addResult("Unsubscribe - request", ret);
    if (ret)
        logger.info("Unsubscribe PASSED");
    else
        logger.error("Unsubscribe FAILED");
    // PING
    ret = !waitPing();
    results.addResult("Unsubscribe - ping", ret);
    if (ret)
        logger.info("Ping not received PASSED");
    else
        logger.error("Ping not recevied FAILED");
    // **********************
    // Enable security
    // **********************
    logger.debug("Switch to secure mode");
    // REGISTRATION (registration not allowed)
    ret = !registrationTest("RegisterMePlease");
    results.addResult("Registration not allowed", ret);
    if (ret)
        logger.info("Registration not allowed PASSED");
    else
        logger.error("Registration not allowed FAILED");
    // REGISTRATION
    ret = registrationTest("SEPATest");
    results.addResult("Registration", ret);
    if (ret)
        logger.info("Registration PASSED");
    else
        logger.error("Registration FAILED");
    // REQUEST ACCESS TOKEN
    ret = requestAccessTokenTest();
    results.addResult("Access token", ret);
    if (ret)
        logger.info("Access token PASSED");
    else
        logger.error("Access token FAILED");
    // REQUEST ACCESS TOKEN (not expired);
    try {
        if (!properties.isTokenExpired())
            ret = !requestAccessTokenTest();
        else
            ret = false;
    } catch (SEPASecurityException e2) {
        logger.error(e2.getMessage());
        ret = false;
    }
    results.addResult("Access token not expired", ret);
    if (ret)
        logger.info("Access token (not expired) PASSED");
    else
        logger.error("Access token (not expired) FAILED");
    // REQUEST ACCESS TOKEN (expired);
    try {
        logger.debug("Waiting token expiring in " + properties.getExpiringSeconds() + " + 2 seconds...");
    } catch (SEPASecurityException e2) {
        logger.error(e2.getMessage());
    }
    try {
        Thread.sleep((properties.getExpiringSeconds() + 2) * 1000);
    } catch (InterruptedException | SEPASecurityException e) {
        logger.error(e.getMessage());
    }
    try {
        if (properties.isTokenExpired())
            ret = requestAccessTokenTest();
        else
            ret = false;
    } catch (SEPASecurityException e2) {
        logger.error(e2.getMessage());
        ret = false;
    }
    results.addResult("Access token expired", ret);
    if (ret)
        logger.info("Access token (expired) PASSED");
    else
        logger.error("Access token (expired) FAILED");
    // SECURE UPDATE
    try {
        logger.debug("Waiting token expiring in " + properties.getExpiringSeconds() + " + 2 seconds...");
    } catch (SEPASecurityException e2) {
        logger.error(e2.getMessage());
    }
    try {
        Thread.sleep((properties.getExpiringSeconds() + 2) * 1000);
    } catch (InterruptedException | SEPASecurityException e) {
        logger.error(e.getMessage());
    }
    try {
        if (properties.isTokenExpired())
            requestAccessTokenTest();
    } catch (SEPASecurityException e13) {
        logger.error(e13.getMessage());
    }
    ret = updateTest("prefix test:<http://wot.arces.unibo.it/test#> delete {?s ?p ?o} insert {test:Sub test:Pred \"ვაიმეე\"} where {?s ?p ?o}", true);
    results.addResult("Secure update ", ret);
    if (ret)
        logger.info("Secure update PASSED");
    else
        logger.error("Secure update FAILED");
    // SECURE UPDATE (expired token)
    try {
        logger.debug("Waiting token expiring in " + properties.getExpiringSeconds() + " + 2 seconds...");
    } catch (SEPASecurityException e12) {
        logger.error(e12.getMessage());
    }
    try {
        Thread.sleep((properties.getExpiringSeconds() + 2) * 1000);
    } catch (InterruptedException | SEPASecurityException e) {
        logger.error(e.getMessage());
    }
    ret = !updateTest("prefix test:<http://wot.arces.unibo.it/test#> delete {?s ?p ?o} insert {test:Sub test:Pred \"vaimee!\"} where {?s ?p ?o}", true);
    results.addResult("Secure update (expired)", ret);
    if (ret)
        logger.info("Secure update (expired) PASSED");
    else
        logger.error("Secure update (expired) FAILED");
    // SECURE QUERY
    try {
        logger.debug("Waiting token expiring in " + properties.getExpiringSeconds() + " + 2 seconds...");
    } catch (SEPASecurityException e11) {
        logger.error(e11.getMessage());
    }
    try {
        Thread.sleep((properties.getExpiringSeconds() + 2) * 1000);
    } catch (InterruptedException | SEPASecurityException e) {
        logger.error(e.getMessage());
    }
    try {
        if (properties.isTokenExpired())
            requestAccessTokenTest();
    } catch (SEPASecurityException e10) {
        logger.error(e10.getMessage());
    }
    ret = queryTest("select ?o where {?s ?p ?o}", "ვაიმეე", true);
    results.addResult("Secure query", ret);
    if (ret)
        logger.info("Secure query PASSED");
    else
        logger.error("Secure query FAILED");
    // SECURE QUERY (expired token)
    try {
        logger.debug("Waiting token expiring in " + properties.getExpiringSeconds() + " + 2 seconds...");
    } catch (SEPASecurityException e9) {
        logger.error(e9.getMessage());
    }
    try {
        Thread.sleep((properties.getExpiringSeconds() + 2) * 1000);
    } catch (InterruptedException | SEPASecurityException e) {
        logger.error(e.getMessage());
    }
    ret = !queryTest("select ?o where {?s ?p ?o}", "ვაიმეე", true);
    results.addResult("Secure query (expired)", ret);
    if (ret)
        logger.info("Secure query (expired) PASSED");
    else
        logger.error("Secure query (expired) FAILED");
    // SECURE SUBSCRIBE
    try {
        logger.debug("Waiting token expiring in " + properties.getExpiringSeconds() + " + 2 seconds...");
    } catch (SEPASecurityException e8) {
        logger.error(e8.getMessage());
    }
    try {
        Thread.sleep((properties.getExpiringSeconds() + 2) * 1000);
    } catch (InterruptedException | SEPASecurityException e) {
        logger.error(e.getMessage());
    }
    try {
        if (properties.isTokenExpired())
            requestAccessTokenTest();
    } catch (SEPASecurityException e7) {
        logger.error(e7.getMessage());
    }
    ret = subscribeTest("select ?o where {?s ?p ?o}", true);
    results.addResult("Secure subscribe - request", ret);
    if (ret)
        logger.info("Secure subscribe PASSED");
    else
        logger.error("Secure subscribe FAILED");
    // PING
    ret = waitPing();
    results.addResult("Secure subscribe - ping", ret);
    if (ret)
        logger.info("Secure ping received PASSED");
    else
        logger.error("Secure ping recevied FAILED");
    // TRIGGER A NOTIFICATION
    try {
        logger.debug("Waiting token expiring in " + properties.getExpiringSeconds() + " + 2 seconds...");
    } catch (SEPASecurityException e6) {
        logger.error(e6.getMessage());
    }
    try {
        Thread.sleep((properties.getExpiringSeconds() + 2) * 1000);
    } catch (InterruptedException | SEPASecurityException e) {
        logger.error(e.getMessage());
    }
    try {
        if (properties.isTokenExpired())
            requestAccessTokenTest();
    } catch (SEPASecurityException e5) {
        logger.error(e5.getMessage());
    }
    ret = updateTest("prefix test:<http://wot.arces.unibo.it/test#> delete {?s ?p ?o} insert {test:Sub test:Pred \"卢卡\"} where {?s ?p ?o}", true);
    results.addResult("Secure subscribe - triggering", ret);
    if (ret)
        logger.info("Secure triggering update PASSED");
    else
        logger.error("Secure triggering update FAILED");
    // NOTIFICATION
    ret = waitNotification();
    results.addResult("Secure subscribe - notification", ret);
    if (ret)
        logger.info("Secure subscribe - notification PASSED");
    else
        logger.error("Secure subscribe - notification FAILED");
    // SECURE UNSUBSCRIBE (expired)
    try {
        logger.debug("Wait token expiring in " + properties.getExpiringSeconds() + " + 2 seconds...");
    } catch (SEPASecurityException e4) {
        logger.error(e4.getMessage());
    }
    try {
        Thread.sleep((properties.getExpiringSeconds() + 2) * 1000);
    } catch (InterruptedException | SEPASecurityException e) {
        logger.error(e.getMessage());
    }
    ret = !unsubscribeTest(spuid, true);
    results.addResult("Secure unsubscribe (expired) - request", ret);
    if (ret)
        logger.info("Secure unsubscribe (expired) - request PASSED");
    else
        logger.error("Secure unsubscribe (expired) - request FAILED");
    // UNSUBSCRIBE
    try {
        logger.debug("Waiting token expiring in " + properties.getExpiringSeconds() + " + 2 seconds...");
    } catch (SEPASecurityException e3) {
        logger.error(e3.getMessage());
    }
    try {
        Thread.sleep((properties.getExpiringSeconds() + 2) * 1000);
    } catch (InterruptedException | SEPASecurityException e) {
        logger.error(e.getMessage());
    }
    try {
        if (properties.isTokenExpired())
            requestAccessTokenTest();
    } catch (SEPASecurityException e2) {
        logger.error(e2.getMessage());
    }
    ret = unsubscribeTest(spuid, true);
    results.addResult("Secure unsubscribe - request", ret);
    if (ret)
        logger.info("Secure unsubscribe - request PASSED");
    else
        logger.error("Secure unsubscribe - request FAILED");
    // WAITING PING
    ret = !waitPing();
    results.addResult("Secure unsubscribe - ping", ret);
    if (ret)
        logger.info("Secure unsubscribe - ping PASSED");
    else
        logger.error("Secure unsubscribe - ping FAILED");
    // SECURE SUBSCRIBE (expired)
    try {
        logger.info("Wait token expiring in " + properties.getExpiringSeconds() + " + 2 seconds...");
    } catch (SEPASecurityException e1) {
        logger.error(e1.getMessage());
    }
    try {
        Thread.sleep((properties.getExpiringSeconds() + 2) * 1000);
    } catch (InterruptedException | SEPASecurityException e) {
        logger.error(e.getMessage());
    }
    ret = !subscribeTest("select ?o where {?s ?p ?o}", true);
    results.addResult("Secure subscribe (expired) - request", ret);
    if (ret)
        logger.info("Secure subscribe (expired) - request PASSED");
    else
        logger.error("Secure subscribe (expired) - request FAILED");
    results.print();
    System.exit(0);
}
Also used : SPARQL11SEProtocol(it.unibo.arces.wot.sepa.api.SPARQL11SEProtocol) SEPAProtocolException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException) Scanner(java.util.Scanner) SEPAPropertiesException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException) SPARQL11SEProperties(it.unibo.arces.wot.sepa.api.SPARQL11SEProperties) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)

Example 2 with SPARQL11SEProtocol

use of it.unibo.arces.wot.sepa.api.SPARQL11SEProtocol 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 3 with SPARQL11SEProtocol

use of it.unibo.arces.wot.sepa.api.SPARQL11SEProtocol in project SEPA by arces-wot.

the class GenericClient method _subscribe.

/**
 * Subscribe.
 *
 * @param ID      the identifier of the subscribe within the JSAP
 * @param sparql  if specified it replaces the default SPARQL in the JSAP
 * @param forced  the forced
 * @param handler the handler
 * @param timeout the timeout
 * @throws SEPAProtocolException   the SEPA protocol exception
 * @throws SEPASecurityException   the SEPA security exception
 * @throws IOException             Signals that an I/O exception has occurred.
 * @throws SEPAPropertiesException the SEPA properties exception
 * @throws URISyntaxException      the URI syntax exception
 * @throws SEPABindingsException   the SEPA bindings exception
 * @throws InterruptedException
 */
private void _subscribe(String ID, String sparql, Bindings forced, String alias, long timeout, long nRretry) throws SEPAProtocolException, SEPASecurityException, SEPAPropertiesException, SEPABindingsException, InterruptedException {
    if (sparql == null)
        sparql = appProfile.getSPARQLQuery(ID);
    if (sparql == null)
        throw new SEPAProtocolException("SPARQL query not found " + ID);
    synchronized (subLock) {
        if (req != null)
            subLock.wait();
        if (appProfile.getSubscribeProtocol(ID).equals(SPARQL11SEProperties.SubscriptionProtocol.WS)) {
            url = "ws_" + appProfile.getSubscribeHost(ID) + "_" + appProfile.getSubscribePort(ID) + "_" + appProfile.getSubscribePath(ID);
        } else {
            url = "wss_" + appProfile.getSubscribeHost(ID) + "_" + appProfile.getSubscribePort(ID) + "_" + appProfile.getSubscribePath(ID);
        }
        if (activeClients.containsKey(url)) {
            subscription = activeClients.get(url);
        } else {
            protocol = new WebsocketSubscriptionProtocol(appProfile.getSubscribeHost(ID), appProfile.getSubscribePort(ID), appProfile.getSubscribePath(ID), this, sm);
            subscription = new SPARQL11SEProtocol(protocol);
        }
        req = new SubscribeRequest(appProfile.addPrefixesAndReplaceBindings(sparql, addDefaultDatatype(forced, ID, true)), alias, appProfile.getDefaultGraphURI(ID), appProfile.getNamedGraphURI(ID), (appProfile.isSecure() ? appProfile.getAuthenticationProperties().getBearerAuthorizationHeader() : null), timeout, nRretry);
        subscription.subscribe((SubscribeRequest) req);
    }
}
Also used : SPARQL11SEProtocol(it.unibo.arces.wot.sepa.api.SPARQL11SEProtocol) SEPAProtocolException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException) SubscribeRequest(it.unibo.arces.wot.sepa.commons.request.SubscribeRequest) WebsocketSubscriptionProtocol(it.unibo.arces.wot.sepa.api.protocols.websocket.WebsocketSubscriptionProtocol)

Aggregations

SPARQL11SEProtocol (it.unibo.arces.wot.sepa.api.SPARQL11SEProtocol)3 SEPAProtocolException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException)3 WebsocketSubscriptionProtocol (it.unibo.arces.wot.sepa.api.protocols.websocket.WebsocketSubscriptionProtocol)2 SEPAPropertiesException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAPropertiesException)2 SEPASecurityException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)2 SPARQL11SEProperties (it.unibo.arces.wot.sepa.api.SPARQL11SEProperties)1 SEPABindingsException (it.unibo.arces.wot.sepa.commons.exceptions.SEPABindingsException)1 SubscribeRequest (it.unibo.arces.wot.sepa.commons.request.SubscribeRequest)1 Scanner (java.util.Scanner)1