Search in sources :

Example 1 with QueryRequest

use of it.unibo.arces.wot.sepa.commons.request.QueryRequest 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 2 with QueryRequest

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

the class SEPATest method queryTest.

protected static boolean queryTest(String sparql, String utf8, boolean secure) {
    QueryRequest query = new QueryRequest(sparql);
    if (!secure)
        logger.debug(query.toString());
    else
        logger.debug("SECURE " + query.toString());
    Response response;
    if (!secure)
        response = client.query(query);
    else
        response = client.secureQuery(query);
    logger.debug(response.toString());
    if (response.isQueryResponse() && utf8 != null) {
        QueryResponse queryResponse = (QueryResponse) response;
        List<Bindings> results = queryResponse.getBindingsResults().getBindings();
        if (results.size() == 1) {
            Bindings bindings = results.get(0);
            if (bindings.isLiteral("o")) {
                String value = bindings.getBindingValue("o");
                if (value.equals(utf8))
                    return true;
            }
        }
        return false;
    }
    return response.isQueryResponse();
}
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) QueryRequest(it.unibo.arces.wot.sepa.commons.request.QueryRequest) QueryResponse(it.unibo.arces.wot.sepa.commons.response.QueryResponse) Bindings(it.unibo.arces.wot.sepa.commons.sparql.Bindings)

Example 3 with QueryRequest

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

the class QueryHandler method parse.

@Override
protected Request parse(HttpAsyncExchange exchange) {
    switch(exchange.getRequest().getRequestLine().getMethod().toUpperCase()) {
        case "GET":
            logger.debug("query via GET");
            if (exchange.getRequest().getRequestLine().getUri().contains("query=")) {
                HttpUtilities.sendFailureResponse(exchange, HttpStatus.SC_BAD_REQUEST, "query is null");
                return null;
            }
            String[] query = exchange.getRequest().getRequestLine().getUri().split("&");
            for (String param : query) {
                String[] value = param.split("=");
                if (value[0].equals("query")) {
                    String sparql = "";
                    try {
                        sparql = URLDecoder.decode(value[1], "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        HttpUtilities.sendFailureResponse(exchange, HttpStatus.SC_BAD_REQUEST, e.getMessage());
                        return null;
                    }
                    return new QueryRequest(sparql);
                }
            }
            HttpUtilities.sendFailureResponse(exchange, HttpStatus.SC_BAD_REQUEST, "Wrong format: " + exchange.getRequest().getRequestLine());
            return null;
        case "POST":
            String body = null;
            HttpEntity entity = ((HttpEntityEnclosingRequest) exchange.getRequest()).getEntity();
            try {
                body = EntityUtils.toString(entity, Charset.forName("UTF-8"));
            } catch (ParseException | IOException e) {
                body = e.getLocalizedMessage();
            }
            Header[] headers = exchange.getRequest().getHeaders("Content-Type");
            if (headers.length != 1) {
                logger.error("Content-Type is missing");
                HttpUtilities.sendFailureResponse(exchange, HttpStatus.SC_BAD_REQUEST, "Content-Type is missing");
                return null;
            }
            if (headers[0].getValue().equals("application/sparql-query")) {
                logger.debug("query via POST directly");
                return new QueryRequest(body);
            } else if (headers[0].getValue().equals("application/x-www-form-urlencoded")) {
                String decodedBody;
                try {
                    decodedBody = URLDecoder.decode(body, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    logger.error(e.getMessage());
                    HttpUtilities.sendFailureResponse(exchange, HttpStatus.SC_BAD_REQUEST, e.getMessage());
                    return null;
                }
                String[] parameters = decodedBody.split("&");
                for (String param : parameters) {
                    String[] value = param.split("=");
                    if (value[0].equals("query")) {
                        logger.debug("query via URL-encoded");
                        return new QueryRequest(value[1]);
                    }
                }
            }
            logger.error("Request MUST conform to SPARQL 1.1 Protocol (https://www.w3.org/TR/sparql11-protocol/)");
            HttpUtilities.sendFailureResponse(exchange, HttpStatus.SC_NOT_FOUND, "Request MUST conform to SPARQL 1.1 Protocol (https://www.w3.org/TR/sparql11-protocol/)");
            return null;
    }
    logger.error("UNSUPPORTED METHOD: " + exchange.getRequest().getRequestLine().getMethod().toUpperCase());
    HttpUtilities.sendFailureResponse(exchange, HttpStatus.SC_NOT_FOUND, "Unsupported method: " + exchange.getRequest().getRequestLine().getMethod().toUpperCase());
    return null;
}
Also used : QueryRequest(it.unibo.arces.wot.sepa.commons.request.QueryRequest) HttpEntity(org.apache.http.HttpEntity) Header(org.apache.http.Header) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParseException(org.apache.http.ParseException) IOException(java.io.IOException)

Example 4 with QueryRequest

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

the class QueryProcessorTest method main.

public static void main(String[] args) throws SEPAProtocolException, SEPAPropertiesException {
    SPARQL11Properties properties = new SPARQL11Properties("endpoint.jpar");
    System.out.println(properties.toString());
    processor = new QueryProcessor(properties, null);
    while (true) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(processor.process(new QueryRequest("select ?s ?p ?o where {?s ?p ?o}"), 0));
    }
}
Also used : SPARQL11Properties(it.unibo.arces.wot.sepa.commons.protocol.SPARQL11Properties) QueryRequest(it.unibo.arces.wot.sepa.commons.request.QueryRequest) QueryProcessor(it.unibo.arces.wot.sepa.engine.processing.QueryProcessor)

Example 5 with QueryRequest

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

the class SchedulerTest method main.

public static void main(String[] args) throws SEPAPropertiesException {
    EngineProperties properties = new EngineProperties("engine.jpar");
    System.out.println(properties.toString());
    scheduler = new Scheduler(properties, null);
    Handler handler = new SchedulerTest().new Handler();
    while (true) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        UpdateRequest update = new UpdateRequest("PREFIX test:<http://sepa/test#> delete {?s ?p ?o} insert {test:s test:p \"" + Math.random() + "\"} where {OPTIONAL{?s ?p ?o}}");
        scheduler.schedule(update, handler);
        QueryRequest query = new QueryRequest("select * where {?s ?p ?o}");
        scheduler.schedule(query, handler);
    }
}
Also used : EngineProperties(it.unibo.arces.wot.sepa.engine.core.EngineProperties) QueryRequest(it.unibo.arces.wot.sepa.commons.request.QueryRequest) UpdateRequest(it.unibo.arces.wot.sepa.commons.request.UpdateRequest) Scheduler(it.unibo.arces.wot.sepa.engine.scheduling.Scheduler) ResponseHandler(it.unibo.arces.wot.sepa.engine.core.ResponseHandler)

Aggregations

QueryRequest (it.unibo.arces.wot.sepa.commons.request.QueryRequest)5 UpdateRequest (it.unibo.arces.wot.sepa.commons.request.UpdateRequest)2 Response (it.unibo.arces.wot.sepa.commons.response.Response)2 SPARQL11Properties (it.unibo.arces.wot.sepa.commons.protocol.SPARQL11Properties)1 Request (it.unibo.arces.wot.sepa.commons.request.Request)1 SubscribeRequest (it.unibo.arces.wot.sepa.commons.request.SubscribeRequest)1 UnsubscribeRequest (it.unibo.arces.wot.sepa.commons.request.UnsubscribeRequest)1 ErrorResponse (it.unibo.arces.wot.sepa.commons.response.ErrorResponse)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 Bindings (it.unibo.arces.wot.sepa.commons.sparql.Bindings)1 EngineProperties (it.unibo.arces.wot.sepa.engine.core.EngineProperties)1 ResponseHandler (it.unibo.arces.wot.sepa.engine.core.ResponseHandler)1 QueryProcessor (it.unibo.arces.wot.sepa.engine.processing.QueryProcessor)1 ScheduledRequest (it.unibo.arces.wot.sepa.engine.scheduling.ScheduledRequest)1 Scheduler (it.unibo.arces.wot.sepa.engine.scheduling.Scheduler)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Header (org.apache.http.Header)1