Search in sources :

Example 1 with InternalQueryRequest

use of it.unibo.arces.wot.sepa.engine.scheduling.InternalQueryRequest in project SEPA by arces-wot.

the class QueryProcessor method process.

public Response process(InternalQueryRequest req) throws SEPASecurityException, IOException {
    // Build the request
    QueryRequest request;
    request = new QueryRequest(properties.getQueryMethod(), properties.getProtocolScheme(), properties.getHost(), properties.getPort(), properties.getQueryPath(), req.getSparql(), req.getDefaultGraphUri(), req.getNamedGraphUri(), req.getBasicAuthorizationHeader(), req.getInternetMediaType(), QueryProcessorBeans.getTimeout(), 0);
    int n = 0;
    Response ret;
    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.query(request);
        endpoint.close();
        long stop = Timings.getTime();
        UpdateProcessorBeans.timings(start, stop);
        logger.trace("Response: " + ret.toString());
        Timings.log("QUERY_PROCESSING_TIME", start, stop);
        n++;
        if (ret.isTimeoutError()) {
            QueryProcessorBeans.timedOutRequest();
            logger.error("*** TIMEOUT *** (" + n + "/" + QueryProcessorBeans.getTimeoutNRetry() + ") " + req);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                logger.warn("Failed to sleep...");
            }
        }
    } while (ret.isTimeoutError() && n < QueryProcessorBeans.getTimeoutNRetry());
    // Request ABORTED
    if (ret.isTimeoutError()) {
        logger.error("*** REQUEST ABORTED *** " + request);
        QueryProcessorBeans.abortedRequest();
    }
    return ret;
}
Also used : Response(it.unibo.arces.wot.sepa.commons.response.Response) QueryRequest(it.unibo.arces.wot.sepa.commons.request.QueryRequest) InternalQueryRequest(it.unibo.arces.wot.sepa.engine.scheduling.InternalQueryRequest) JenaInMemoryEndpoint(it.unibo.arces.wot.sepa.engine.processing.endpoint.JenaInMemoryEndpoint) RemoteEndpoint(it.unibo.arces.wot.sepa.engine.processing.endpoint.RemoteEndpoint) SPARQLEndpoint(it.unibo.arces.wot.sepa.engine.processing.endpoint.SPARQLEndpoint) JenaInMemoryEndpoint(it.unibo.arces.wot.sepa.engine.processing.endpoint.JenaInMemoryEndpoint) RemoteEndpoint(it.unibo.arces.wot.sepa.engine.processing.endpoint.RemoteEndpoint) SPARQLEndpoint(it.unibo.arces.wot.sepa.engine.processing.endpoint.SPARQLEndpoint)

Example 2 with InternalQueryRequest

use of it.unibo.arces.wot.sepa.engine.scheduling.InternalQueryRequest 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());
}
Also used : Set(java.util.Set) HttpEntity(org.apache.http.HttpEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) InternalQueryRequest(it.unibo.arces.wot.sepa.engine.scheduling.InternalQueryRequest) Header(org.apache.http.Header) SEPASparqlParsingException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASparqlParsingException) Map(java.util.Map) InternalUpdateRequest(it.unibo.arces.wot.sepa.engine.scheduling.InternalUpdateRequest)

Example 3 with InternalQueryRequest

use of it.unibo.arces.wot.sepa.engine.scheduling.InternalQueryRequest in project SEPA by arces-wot.

the class QueryProcessingThread method run.

public void run() {
    while (processor.isRunning()) {
        ScheduledRequest request;
        try {
            request = processor.waitQueryRequest();
        } catch (InterruptedException e) {
            return;
        }
        InternalQueryRequest query = (InternalQueryRequest) request.getRequest();
        Response ret;
        try {
            ret = processor.processQuery(query);
        } catch (SEPASecurityException | IOException e) {
            logger.error(e.getMessage());
            if (logger.isTraceEnabled())
                e.printStackTrace();
            ret = new ErrorResponse(401, "SEPASecurityException", e.getMessage());
        }
        processor.addResponse(request.getToken(), ret);
    }
}
Also used : Response(it.unibo.arces.wot.sepa.commons.response.Response) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse) ScheduledRequest(it.unibo.arces.wot.sepa.engine.scheduling.ScheduledRequest) SEPASecurityException(it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException) IOException(java.io.IOException) InternalQueryRequest(it.unibo.arces.wot.sepa.engine.scheduling.InternalQueryRequest) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse)

Aggregations

InternalQueryRequest (it.unibo.arces.wot.sepa.engine.scheduling.InternalQueryRequest)3 Response (it.unibo.arces.wot.sepa.commons.response.Response)2 SEPASecurityException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)1 SEPASparqlParsingException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASparqlParsingException)1 QueryRequest (it.unibo.arces.wot.sepa.commons.request.QueryRequest)1 ErrorResponse (it.unibo.arces.wot.sepa.commons.response.ErrorResponse)1 JenaInMemoryEndpoint (it.unibo.arces.wot.sepa.engine.processing.endpoint.JenaInMemoryEndpoint)1 RemoteEndpoint (it.unibo.arces.wot.sepa.engine.processing.endpoint.RemoteEndpoint)1 SPARQLEndpoint (it.unibo.arces.wot.sepa.engine.processing.endpoint.SPARQLEndpoint)1 InternalUpdateRequest (it.unibo.arces.wot.sepa.engine.scheduling.InternalUpdateRequest)1 ScheduledRequest (it.unibo.arces.wot.sepa.engine.scheduling.ScheduledRequest)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Map (java.util.Map)1 Set (java.util.Set)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1