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;
// }
// }
}
}
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();
}
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;
}
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));
}
}
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);
}
}
Aggregations