Search in sources :

Example 1 with UpdateRequest

use of it.unibo.arces.wot.sepa.commons.request.UpdateRequest in project SEPA by arces-wot.

the class SPARQL11SEProtocol method executeSPARQL11SEPrimitive.

protected Response executeSPARQL11SEPrimitive(SPARQL11SEPrimitive op, Object request) {
    // Create the HTTPS request
    URI uri;
    String path = null;
    int port = 0;
    // Headers and body
    String contentType = null;
    ByteArrayEntity body = null;
    String accept = null;
    String authorization = null;
    switch(op) {
        case SUBSCRIBE:
            SubscribeRequest subscribe = (SubscribeRequest) request;
            return wsClient.subscribe(subscribe.getSPARQL());
        case UNSUBSCRIBE:
            UnsubscribeRequest unsubscribe = (UnsubscribeRequest) request;
            return wsClient.unsubscribe(unsubscribe.getSubscribeUUID());
        // }
        default:
            break;
    }
    switch(op) {
        case REGISTER:
            path = properties.getRegisterPath();
            port = properties.getHttpsPort();
            accept = "application/json";
            contentType = "application/json";
            String identity = (String) request;
            try {
                body = new ByteArrayEntity(new RegistrationRequest(identity).toString().getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                return new ErrorResponse(500, e.getMessage());
            }
            break;
        case REQUESTTOKEN:
            String basic;
            try {
                basic = properties.getBasicAuthorization();
            } catch (SEPASecurityException e2) {
                return new ErrorResponse(500, e2.getMessage());
            }
            if (basic == null)
                return new ErrorResponse(0, 401, "Basic authorization in null. Register first");
            path = properties.getTokenRequestPath();
            port = properties.getHttpsPort();
            authorization = "Basic " + basic;
            contentType = "application/json";
            accept = "application/json";
            break;
        case SECUREUPDATE:
            path = properties.getSecurePath() + properties.getUpdatePath();
            port = properties.getHttpsPort();
            accept = "text/plain";
            contentType = "application/x-www-form-urlencoded";
            try {
                authorization = "Bearer " + properties.getAccessToken();
            } catch (SEPASecurityException e2) {
                return new ErrorResponse(500, e2.getMessage());
            }
            String encodedContent;
            try {
                encodedContent = URLEncoder.encode(((UpdateRequest) request).getSPARQL(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                return new ErrorResponse(500, e.getMessage());
            }
            body = new ByteArrayEntity(("update=" + encodedContent).getBytes());
            body.setContentType(contentType);
            break;
        case SECUREQUERY:
            path = properties.getSecurePath() + properties.getQueryPath();
            port = properties.getHttpsPort();
            accept = "application/sparql-results+json";
            contentType = "application/sparql-query";
            try {
                authorization = "Bearer " + properties.getAccessToken();
            } catch (SEPASecurityException e2) {
                return new ErrorResponse(500, e2.getMessage());
            }
            try {
                body = new ByteArrayEntity(((QueryRequest) request).getSPARQL().getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                return new ErrorResponse(500, e.getMessage());
            }
            break;
        default:
            break;
    }
    // POST request
    try {
        uri = new URI("https", null, properties.getHost(), port, path, null, null);
    } catch (URISyntaxException e1) {
        return new ErrorResponse(500, e1.getMessage());
    }
    HttpUriRequest httpRequest = new HttpPost(uri);
    if (contentType != null)
        httpRequest.setHeader("Content-Type", contentType);
    if (accept != null)
        httpRequest.setHeader("Accept", accept);
    if (authorization != null)
        httpRequest.setHeader("Authorization", authorization);
    if (body != null)
        ((HttpPost) httpRequest).setEntity(body);
    logger.debug("Request: " + httpRequest);
    // HTTP request execution
    CloseableHttpClient httpclient;
    try {
        httpclient = new SSLSecurityManager("TLSv1", "sepa.jks", "sepa2017", "sepa2017").getSSLHttpClient();
    } catch (KeyManagementException | UnrecoverableKeyException | NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException e) {
        return new ErrorResponse(500, e.getMessage());
    }
    CloseableHttpResponse response = null;
    String jsonResponse = null;
    try {
        long timing = System.nanoTime();
        try {
            response = httpclient.execute(httpRequest);
        } catch (IOException e) {
            return new ErrorResponse(500, e.getMessage());
        }
        timing = System.nanoTime() - timing;
        logger.debug("Response: " + response);
        if (op.equals(SPARQL11SEPrimitive.REGISTER))
            logger.debug("REGISTER " + timing / 1000000 + " ms");
        else if (op.equals(SPARQL11SEPrimitive.REQUESTTOKEN))
            logger.debug("TOKEN " + timing / 1000000 + " ms");
        else if (op.equals(SPARQL11SEPrimitive.SECUREQUERY))
            logger.debug("SECURE_QUERY " + timing / 1000000 + " ms");
        else if (op.equals(SPARQL11SEPrimitive.SECUREUPDATE))
            logger.debug("SECURE_UPDATE " + timing / 1000000 + " ms");
        HttpEntity entity = response.getEntity();
        try {
            jsonResponse = EntityUtils.toString(entity, Charset.forName("UTF-8"));
        } catch (ParseException | IOException e) {
            return new ErrorResponse(500, e.getMessage());
        }
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            return new ErrorResponse(500, e.getMessage());
        }
    } finally {
        try {
            response.close();
        } catch (IOException e) {
            return new ErrorResponse(500, e.getMessage());
        }
    }
    // Parsing the response
    try {
        return parseSPARQL11SEResponse(jsonResponse, op);
    } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
        return new ErrorResponse(500, e.getMessage());
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) CertificateException(java.security.cert.CertificateException) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) URI(java.net.URI) RegistrationRequest(it.unibo.arces.wot.sepa.commons.request.RegistrationRequest) KeyManagementException(java.security.KeyManagementException) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) UnrecoverableKeyException(java.security.UnrecoverableKeyException) SubscribeRequest(it.unibo.arces.wot.sepa.commons.request.SubscribeRequest) SSLSecurityManager(it.unibo.arces.wot.sepa.commons.protocol.SSLSecurityManager) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) UpdateRequest(it.unibo.arces.wot.sepa.commons.request.UpdateRequest) UnsubscribeRequest(it.unibo.arces.wot.sepa.commons.request.UnsubscribeRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse) JsonParseException(com.google.gson.JsonParseException) ParseException(org.apache.http.ParseException)

Example 2 with UpdateRequest

use of it.unibo.arces.wot.sepa.commons.request.UpdateRequest in project SEPA by arces-wot.

the class Aggregator method update.

public final Response update(Bindings forcedBindings) {
    if (protocolClient == null || sparqlUpdate == null) {
        logger.fatal("Aggregator not initialized");
        return new ErrorResponse(-1, 400, "Aggregator not initialized");
    }
    String sparql = prefixes() + replaceBindings(sparqlUpdate, forcedBindings);
    logger.debug("<UPDATE> " + SPARQL_ID + " ==> " + sparql);
    return protocolClient.update(new UpdateRequest(sparql));
}
Also used : UpdateRequest(it.unibo.arces.wot.sepa.commons.request.UpdateRequest) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse)

Example 3 with UpdateRequest

use of it.unibo.arces.wot.sepa.commons.request.UpdateRequest in project SEPA by arces-wot.

the class Processor method run.

@Override
public void run() {
    while (true) {
        // WAIT NEW REQUEST
        ScheduledRequest scheduledRequest;
        try {
            scheduledRequest = queue.waitRequest();
        } catch (InterruptedException e1) {
            return;
        }
        Request request = scheduledRequest.getRequest();
        if (request.isUpdateRequest()) {
            logger.info("Update request #" + request.getToken());
            logger.debug(request);
            // Process update request
            Response ret = updateProcessor.process((UpdateRequest) request, ProcessorBeans.getUpdateTimeout());
            // // Notify update result
            // setChanged();
            // notifyObservers(ret);
            queue.addResponse(ret);
            if (ret.isUpdateResponse()) {
                // updateProcessing = true;
                spuManager.process((UpdateResponse) ret);
                try {
                    updateProcessingQueue.waitUpdateEOP();
                } catch (InterruptedException e1) {
                    return;
                }
            // while (updateProcessing) {
            // // Wait for SPUs processing end
            // synchronized (updateProcessor) {
            // try {
            // updateProcessor.wait();
            // } catch (InterruptedException e) {
            // return;
            // }
            // }
            // }
            }
        } else if (request.isQueryRequest()) {
            logger.info("Query request #" + request.getToken());
            logger.debug(request);
            Thread queryProcessing = new Thread() {

                public void run() {
                    Response ret = queryProcessor.process((QueryRequest) request, ProcessorBeans.getQueryTimeout());
                    // setChanged();
                    // notifyObservers(ret);
                    queue.addResponse(ret);
                }
            };
            queryProcessing.setName("SEPA Query Processing Thread-" + request.getToken());
            queryProcessing.start();
        } else if (request.isSubscribeRequest()) {
            logger.info("Subscribe request #" + request.getToken());
            logger.debug(request);
            Response ret = spuManager.subscribe((SubscribeRequest) request, (EventHandler) scheduledRequest.getHandler());
            // setChanged();
            // notifyObservers(ret);
            queue.addResponse(ret);
        } else if (request.isUnsubscribeRequest()) {
            logger.info("Unsubscribe request #" + request.getToken());
            logger.debug(request);
            Response ret = spuManager.unsubscribe((UnsubscribeRequest) request);
            // setChanged();
            // notifyObservers(ret);
            queue.addResponse(ret);
        }
    // 
    // UpdateRequest request;
    // while ((request = updateRequestQueue.poll()) != null) {
    // logger.debug("New request: " + request);
    // 
    // // Process update request
    // Response ret = updateProcessor.process(request,
    // ProcessorBeans.getUpdateTimeout());
    // 
    // // Notify update result
    // setChanged();
    // notifyObservers(ret);
    // 
    // if (ret.isUpdateResponse()) {
    // updateProcessing = true;
    // 
    // spuManager.process((UpdateResponse) ret);
    // 
    // while (updateProcessing) {
    // // Wait for SPUs processing end
    // synchronized (updateProcessor) {
    // try {
    // updateProcessor.wait();
    // } catch (InterruptedException e) {
    // return;
    // }
    // }
    // }
    // }
    // }
    // 
    // synchronized (updateRequestQueue) {
    // try {
    // updateRequestQueue.wait();
    // } catch (InterruptedException e) {
    // logger.error(e.getMessage());
    // return;
    // }
    // }
    }
}
Also used : Response(it.unibo.arces.wot.sepa.commons.response.Response) UpdateResponse(it.unibo.arces.wot.sepa.commons.response.UpdateResponse) ScheduledRequest(it.unibo.arces.wot.sepa.engine.scheduling.ScheduledRequest) QueryRequest(it.unibo.arces.wot.sepa.commons.request.QueryRequest) UnsubscribeRequest(it.unibo.arces.wot.sepa.commons.request.UnsubscribeRequest) Request(it.unibo.arces.wot.sepa.commons.request.Request) UpdateRequest(it.unibo.arces.wot.sepa.commons.request.UpdateRequest) QueryRequest(it.unibo.arces.wot.sepa.commons.request.QueryRequest) SubscribeRequest(it.unibo.arces.wot.sepa.commons.request.SubscribeRequest) UnsubscribeRequest(it.unibo.arces.wot.sepa.commons.request.UnsubscribeRequest) ScheduledRequest(it.unibo.arces.wot.sepa.engine.scheduling.ScheduledRequest)

Example 4 with UpdateRequest

use of it.unibo.arces.wot.sepa.commons.request.UpdateRequest in project SEPA by arces-wot.

the class Aggregator method update.

public final Response update(long timeout, long nRetry) throws SEPASecurityException, SEPAProtocolException, SEPAPropertiesException, SEPABindingsException {
    String authorizationHeader = null;
    if (isSecure())
        authorizationHeader = appProfile.getAuthenticationProperties().getBearerAuthorizationHeader();
    UpdateRequest req = new UpdateRequest(appProfile.getUpdateMethod(updateId), appProfile.getUpdateProtocolScheme(updateId), appProfile.getUpdateHost(updateId), appProfile.getUpdatePort(updateId), appProfile.getUpdatePath(updateId), appProfile.addPrefixesAndReplaceBindings(sparqlUpdate, addDefaultDatatype(updateForcedBindings, updateId, false)), appProfile.getUsingGraphURI(updateId), appProfile.getUsingNamedGraphURI(updateId), authorizationHeader, timeout, nRetry);
    logger.debug("UPDATE " + req);
    Response retResponse = sparql11.update(req);
    return retResponse;
}
Also used : Response(it.unibo.arces.wot.sepa.commons.response.Response) UpdateRequest(it.unibo.arces.wot.sepa.commons.request.UpdateRequest)

Example 5 with UpdateRequest

use of it.unibo.arces.wot.sepa.commons.request.UpdateRequest in project SEPA by arces-wot.

the class SEPATest method updateTest.

protected static boolean updateTest(String sparql, boolean secure) {
    notificationReceived = false;
    UpdateRequest update = new UpdateRequest(sparql);
    if (!secure)
        logger.debug(update.toString());
    else
        logger.debug("SECURE " + update.toString());
    Response response;
    if (secure)
        response = client.secureUpdate(update);
    else
        response = client.update(update);
    logger.debug(response.toString());
    return response.isUpdateResponse();
}
Also used : Response(it.unibo.arces.wot.sepa.commons.response.Response) SubscribeResponse(it.unibo.arces.wot.sepa.commons.response.SubscribeResponse) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse) QueryResponse(it.unibo.arces.wot.sepa.commons.response.QueryResponse) UpdateRequest(it.unibo.arces.wot.sepa.commons.request.UpdateRequest)

Aggregations

UpdateRequest (it.unibo.arces.wot.sepa.commons.request.UpdateRequest)11 Response (it.unibo.arces.wot.sepa.commons.response.Response)5 ErrorResponse (it.unibo.arces.wot.sepa.commons.response.ErrorResponse)4 QueryRequest (it.unibo.arces.wot.sepa.commons.request.QueryRequest)2 SubscribeRequest (it.unibo.arces.wot.sepa.commons.request.SubscribeRequest)2 UnsubscribeRequest (it.unibo.arces.wot.sepa.commons.request.UnsubscribeRequest)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HttpEntity (org.apache.http.HttpEntity)2 ParseException (org.apache.http.ParseException)2 JsonParseException (com.google.gson.JsonParseException)1 SEPASecurityException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)1 SPARQL11Properties (it.unibo.arces.wot.sepa.commons.protocol.SPARQL11Properties)1 SSLSecurityManager (it.unibo.arces.wot.sepa.commons.protocol.SSLSecurityManager)1 RegistrationRequest (it.unibo.arces.wot.sepa.commons.request.RegistrationRequest)1 Request (it.unibo.arces.wot.sepa.commons.request.Request)1 QueryResponse (it.unibo.arces.wot.sepa.commons.response.QueryResponse)1 SubscribeResponse (it.unibo.arces.wot.sepa.commons.response.SubscribeResponse)1 UpdateResponse (it.unibo.arces.wot.sepa.commons.response.UpdateResponse)1 EngineProperties (it.unibo.arces.wot.sepa.engine.core.EngineProperties)1