use of it.unibo.arces.wot.sepa.engine.scheduling.InternalUpdateRequest in project SEPA by arces-wot.
the class Processor method processUpdate.
public synchronized Response processUpdate(InternalUpdateRequest update) {
InternalUpdateRequest preRequest = update;
if (spuManager.doUpdateARQuadsExtraction(update)) {
try {
preRequest = ARQuadsAlgorithm.extractARQuads(update, queryProcessor);
} catch (SEPAProcessingException | SPARQL11ProtocolException | SEPASparqlParsingException e) {
return new ErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "update_processing", e.getMessage());
}
}
// PRE-UPDATE processing
spuManager.subscriptionsProcessingPreUpdate(preRequest);
// Endpoint UPDATE
Response ret;
try {
ret = updateEndpoint(preRequest);
} catch (SEPASecurityException | IOException e) {
return new ErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "sparql11endpoint", e.getMessage());
}
// STOP processing?
if (ret.isError()) {
logger.error("*** UPDATE ENDPOINT PROCESSING FAILED *** " + ret);
spuManager.abortSubscriptionsProcessing();
return ret;
}
// POST-UPDATE processing
spuManager.subscriptionsProcessingPostUpdate(ret);
return ret;
}
use of it.unibo.arces.wot.sepa.engine.scheduling.InternalUpdateRequest in project SEPA by arces-wot.
the class SPARQL11Handler method parse.
protected InternalUQRequest parse(HttpAsyncExchange exchange, ClientAuthorization auth) {
URI uri;
try {
uri = new URI(exchange.getRequest().getRequestLine().getUri());
} catch (URISyntaxException e1) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, e1.getMessage());
}
String requestUri = exchange.getRequest().getRequestLine().getUri();
if (exchange.getRequest().getRequestLine().getMethod().toUpperCase().equals("GET")) {
/**
* <a href="https://www.w3.org/TR/sparql11-protocol/"> SPARQL 1.1 Protocol</a>
*
* <pre>
*
* HTTP Method Query String Parameters Request | Content Type Request Message Body
* ----------------------------------------------------------------------------------------------------------------------------------------
* query via GET | GET | query (exactly 1) | None | None | default-graph-uri (0 or more) named-graph-uri (0 or more)
*
* 2.1.4 Specifying an RDF Dataset
*
* A SPARQL query is executed against an RDF Dataset. The RDF Dataset for a
* query may be specified either via the default-graph-uri and named-graph-uri
* parameters in the SPARQL Protocol or in the SPARQL query string using the
* FROM and FROM NAMED keywords.
*
* If different RDF Datasets are specified in both the protocol request and the
* SPARQL query string, then the SPARQL service must execute the query using the
* RDF Dataset given in the protocol request.
*
* Note that a service may reject a query with HTTP response code 400 if the
* service does not allow protocol clients to specify the RDF Dataset. If an RDF
* Dataset is not specified in either the protocol request or the SPARQL query
* string, then implementations may execute the query against an
* implementation-defined default RDF dataset.
*
* </pre>
*/
if (!uri.getPath().equals(queryPath))
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, "GET method available for query only. Wrong path: " + uri.getPath() + " expecting: " + queryPath);
try {
if (requestUri.indexOf('?') == -1) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, "Wrong request uri: ? not found in " + requestUri);
}
String queryParameters = requestUri.substring(requestUri.indexOf('?') + 1);
Map<String, Set<String>> params = HttpUtilities.splitQuery(queryParameters);
if (params.get("query") == null) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, "Wrong request uri: 'query=' not found in " + queryParameters);
}
String sparql = params.get("query").iterator().next();
Set<String> graphUri = params.get("default-graph-uri");
Set<String> namedGraphUri = params.get("named-graph-uri");
Header[] headers = exchange.getRequest().getHeaders("Accept");
if (headers.length != 1)
return new InternalQueryRequest(sparql, graphUri, namedGraphUri, auth);
else
return new InternalQueryRequest(sparql, graphUri, namedGraphUri, auth, headers[0].getValue());
} catch (SEPASparqlParsingException | UnsupportedEncodingException e) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
} else if (exchange.getRequest().getRequestLine().getMethod().toUpperCase().equals("POST")) {
/**
* <a href="https://www.w3.org/TR/sparql11-protocol/"> SPARQL 1.1 Protocol</a>
*
* *
*
* <pre>
* HTTP Method Query String Parameters Request Content Type Request Message Body
*----------------------------------------------------------------------------------------------------------------------------------------
* update via URL-encoded POST| POST None application/x-www-form-urlencoded URL-encoded, ampersand-separated query parameters.
* | update (exactly 1)
* | using-graph-uri (0 or more)
* | using-named-graph-uri (0 or more)
*----------------------------------------------------------------------------------------------------------------------------------------
* update via POST directly | POST using-graph-uri (0 or more) application/sparql-update Unencoded SPARQL update request string
* using-named-graph-uri (0 or more)
* ----------------------------------------------------------------------------------------------------------------------------------------
* query via URL-encoded POST | POST None application/x-www-form-urlencoded URL-encoded, ampersand-separated query parameters.
* | query (exactly 1)
* | default-graph-uri (0 or more)
* | named-graph-uri (0 or more)
*----------------------------------------------------------------------------------------------------------------------------------------
* query via POST directly | POST default-graph-uri (0 or more)
* | named-graph-uri (0 or more) application/sparql-query Unencoded SPARQL query string
* </pre>
*/
Header[] headers = exchange.getRequest().getHeaders("Content-Type");
if (headers.length != 1) {
logger.log(Level.getLevel("http"), "Content-Type is missing or multiple");
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, "Content-Type is missing or multiple");
}
HttpEntity entity = ((HttpEntityEnclosingRequest) exchange.getRequest()).getEntity();
String body;
try {
body = EntityUtils.toString(entity, Charset.forName("UTF-8"));
} catch (ParseException | IOException e) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
String defGraph = "default-graph-uri";
String namedGraph = "named-graph-uri";
String sparql = null;
Set<String> default_graph_uri = null;
Set<String> named_graph_uri = null;
if (headers[0].getValue().equals("application/x-www-form-urlencoded")) {
String decodedBody;
try {
decodedBody = URLDecoder.decode(body, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
try {
Map<String, Set<String>> params = HttpUtilities.splitQuery(decodedBody);
if (params.containsKey("query")) {
if (!uri.getPath().equals(queryPath))
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, "Wrong path: " + uri.getPath() + " was expecting: " + queryPath);
params = HttpUtilities.splitQuery(decodedBody);
sparql = params.get("query").iterator().next();
default_graph_uri = params.get(defGraph);
named_graph_uri = params.get(namedGraph);
headers = exchange.getRequest().getHeaders("Accept");
if (headers.length != 1)
try {
return new InternalQueryRequest(sparql, default_graph_uri, named_graph_uri, auth);
} catch (SEPASparqlParsingException e) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
else
try {
return new InternalQueryRequest(sparql, default_graph_uri, named_graph_uri, auth, headers[0].getValue());
} catch (SEPASparqlParsingException e) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
} else if (params.containsKey("update")) {
if (!uri.getPath().equals(updatePath))
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, "Wrong path: " + uri.getPath() + " was expecting: " + updatePath);
defGraph = "using-graph-uri";
namedGraph = "using-named-graph-uri";
params = HttpUtilities.splitQuery(decodedBody);
sparql = params.get("update").iterator().next();
default_graph_uri = params.get(defGraph);
named_graph_uri = params.get(namedGraph);
try {
return new InternalUpdateRequest(sparql, default_graph_uri, named_graph_uri, auth);
} catch (SPARQL11ProtocolException | SEPASparqlParsingException e) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
}
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, "Query or update query parameter not found");
} catch (UnsupportedEncodingException e) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
} else if (headers[0].getValue().equals("application/sparql-query")) {
if (!uri.getPath().equals(queryPath))
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, "Wrong path: " + uri.getPath() + " was expecting: " + queryPath);
if (requestUri.indexOf('?') != -1) {
String queryParameters = requestUri.substring(requestUri.indexOf('?') + 1);
Map<String, Set<String>> params;
try {
params = HttpUtilities.splitQuery(queryParameters);
default_graph_uri = params.get(defGraph);
named_graph_uri = params.get(namedGraph);
} catch (UnsupportedEncodingException e) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
}
sparql = body;
headers = exchange.getRequest().getHeaders("Accept");
if (headers.length != 1)
try {
return new InternalQueryRequest(sparql, default_graph_uri, named_graph_uri, auth);
} catch (SEPASparqlParsingException e) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
else
try {
return new InternalQueryRequest(sparql, default_graph_uri, named_graph_uri, auth, headers[0].getValue());
} catch (SEPASparqlParsingException e) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
} else if (headers[0].getValue().equals("application/sparql-update")) {
if (!uri.getPath().equals(updatePath))
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, "Wrong path: " + uri.getPath() + " was expecting: " + updatePath);
defGraph = "using-graph-uri";
namedGraph = "using-named-graph-uri";
if (requestUri.indexOf('?') != -1) {
String queryParameters = requestUri.substring(requestUri.indexOf('?') + 1);
Map<String, Set<String>> params;
try {
params = HttpUtilities.splitQuery(queryParameters);
default_graph_uri = params.get(defGraph);
named_graph_uri = params.get(namedGraph);
} catch (UnsupportedEncodingException e) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
}
sparql = body;
try {
return new InternalUpdateRequest(sparql, default_graph_uri, named_graph_uri, auth);
} catch (SPARQL11ProtocolException | SEPASparqlParsingException e) {
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
}
}
throw new SPARQL11ProtocolException(HttpStatus.SC_BAD_REQUEST, "Unsupported HTTP method: " + exchange.getRequest().getRequestLine().getMethod().toUpperCase());
}
use of it.unibo.arces.wot.sepa.engine.scheduling.InternalUpdateRequest in project SEPA by arces-wot.
the class UpdateProcessor method process.
public Response process(InternalUpdateRequest req) throws SEPASecurityException, IOException {
// ENDPOINT UPDATE (set timeout and set retry = 0)
UpdateRequest request = new UpdateRequest(properties.getUpdateMethod(), properties.getProtocolScheme(), properties.getHost(), properties.getPort(), properties.getUpdatePath(), req.getSparql(), req.getDefaultGraphUri(), req.getNamedGraphUri(), req.getBasicAuthorizationHeader(), UpdateProcessorBeans.getTimeout(), 0);
logger.trace(request);
Response ret;
int n = 0;
do {
long start = Timings.getTime();
SPARQLEndpoint endpoint;
if (properties.getProtocolScheme().equals("jena-api") && properties.getHost().equals("in-memory"))
endpoint = new JenaInMemoryEndpoint();
else
endpoint = new RemoteEndpoint();
ret = endpoint.update(request);
endpoint.close();
long stop = Timings.getTime();
UpdateProcessorBeans.timings(start, stop);
logger.trace("Response: " + ret.toString());
Timings.log("UPDATE_PROCESSING_TIME", start, stop);
n++;
if (ret.isTimeoutError()) {
UpdateProcessorBeans.timedOutRequest();
logger.error("*TIMEOUT* (" + n + "/" + UpdateProcessorBeans.getTimeoutNRetry() + ") " + req);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
logger.warn("Failed to sleep...");
}
}
} while (ret.isTimeoutError() && n < UpdateProcessorBeans.getTimeoutNRetry());
if (ret.isTimeoutError()) {
logger.error("*** REQUEST ABORTED *** " + request);
UpdateProcessorBeans.abortedRequest();
}
return ret;
}
use of it.unibo.arces.wot.sepa.engine.scheduling.InternalUpdateRequest in project SEPA by arces-wot.
the class UpdateProcessingThread method run.
public void run() {
while (processor.isRunning()) {
ScheduledRequest request;
try {
logger.trace("Wait for update requests...");
request = processor.waitUpdateRequest();
} catch (InterruptedException e) {
return;
}
// Update request
InternalUpdateRequest update = (InternalUpdateRequest) request.getRequest();
// Notify update (not reliable)
if (!processor.isUpdateReliable()) {
logger.trace("Notify client of update processing (not reliable)");
processor.addResponse(request.getToken(), new UpdateResponse("Processing: " + update));
}
// Process update
logger.trace("Start processing update...");
Response ret = processor.processUpdate(update);
logger.trace("Update processing COMPLETED");
// Notify update result
if (processor.isUpdateReliable()) {
logger.trace("Notify client of update processing (reliable)");
processor.addResponse(request.getToken(), ret);
}
}
}
Aggregations