Beispiel #1
0
    def do_query(self, request, resource, scope, query):
        if scope not in { 'graph', 'subtree' }:
            return MyResponse("400 Bad Request\nUnsupported scope '%s'." % scope,
                              status="400 Bad Request",
                              request=request)

        resource.force_state_refresh()
        if scope == 'subtree':
            graph = PrefixConjunctiveView(resource.uri, resource.service.store)
        else:
            # scope == 'graph'
            graph = resource.get_state()

        result = graph.query(query, base=resource.uri)

        if result.graph is not None:
            ctype = serfmt = (
                request.accept.best_match(self.CONSTRUCT_CTYPES)
                or "text/turtle"
            )
        elif result.askAnswer is not None:
            ctype = request.accept.best_match(self.ASK_CTYPES)
            if ctype is None:
                ctype = "application/sparql-results+json"
            serfmt = self.SELECT_CTYPES[ctype]
        else:
            ctype = request.accept.best_match(self.SELECT_CTYPES)
            if ctype is None:
                ctype = "application/sparql-results+json"
            serfmt = self.SELECT_CTYPES[ctype]
        try:
            return MyResponse(result.serialize(format=serfmt),
                              status="200 Ok",
                              content_type=ctype,
                              request=request)
        except Exception, ex:
            if ex.message.startswith(
                    "You performed a query operation requiring a dataset"):
                status = "403 Forbidden"
                return MyResponse("%s\n%s"
                                  % (status, ex.message),
                                  status=status,
                                  request=request)
            else:
                raise
Beispiel #2
0
    def do_query(self, request, resource, scope, query):
        if scope not in {'graph', 'subtree'}:
            return MyResponse("400 Bad Request\nUnsupported scope '%s'." %
                              scope,
                              status="400 Bad Request",
                              request=request)

        resource.force_state_refresh()
        if scope == 'subtree':
            graph = PrefixConjunctiveView(resource.uri, resource.service.store)
        else:
            # scope == 'graph'
            graph = resource.get_state()

        result = graph.query(query, base=resource.uri)

        if result.graph is not None:
            ctype = serfmt = (request.accept.best_match(self.CONSTRUCT_CTYPES)
                              or "text/turtle")
        elif result.askAnswer is not None:
            ctype = request.accept.best_match(self.ASK_CTYPES)
            if ctype is None:
                ctype = "application/sparql-results+json"
            serfmt = self.SELECT_CTYPES[ctype]
        else:
            ctype = request.accept.best_match(self.SELECT_CTYPES)
            if ctype is None:
                ctype = "application/sparql-results+json"
            serfmt = self.SELECT_CTYPES[ctype]
        try:
            return MyResponse(result.serialize(format=serfmt),
                              status="200 Ok",
                              content_type=ctype,
                              request=request)
        except Exception, ex:
            if ex.message.startswith(
                    "You performed a query operation requiring a dataset"):
                status = "403 Forbidden"
                return MyResponse("%s\n%s" % (status, ex.message),
                                  status=status,
                                  request=request)
            else:
                raise
Beispiel #3
0
    def compute_obsels(self, computed_trace, from_scratch=False):
        """I implement :meth:`.interface.IMethod.compute_obsels`.
        """
        diag = Diagnosis("sparql.compute_obsels")

        source = computed_trace.source_traces[0]
        parameters = computed_trace.parameters_as_dict
        parameters["__destination__"] = computed_trace.uri
        parameters["__source__"] = source.uri

        scope = parameters.get('scope', 'trace')
        try:
            if scope == 'base':
                data = PrefixConjunctiveView(source.base.uri,
                                             source.service.store)
            else:
                # scope == 'trace'
                data = source.obsel_collection.get_state({"refresh":"no"})
            sparql = parameters["sparql"] % parameters
            result = data.query(sparql, base=source.obsel_collection.uri).graph
            replace_obsels(computed_trace, result, ("inherit" in parameters))
        except Exception, exc:
            LOG.warn(traceback.format_exc())
            diag.append(unicode(exc))
Beispiel #4
0
    def handle_sparql(self, request):
        """
        I handle a SPARQL request
        """
        resource = request.environ['rdfrest.resource']
        if request.method == "GET" \
        or request.content_type == "application/sparql-query":
            params = request.GET
        else:
            params = request.POST
        scope = params.getall("scope") or ['graph']
        default_graph_uri = params.getall("default-graph-uri")
        named_graph_uri = params.getall("named-graph-uri")

        if request.content_type != "application/sparql-query":
            lst = params.getall("query")
            if len(lst) == 0:
                # NB: not rejecting several queries, because some services
                # provide the same query several times (YASGUI)
                return MyResponse("400 Bad Request\nQuery not provided",
                                  status="400 Bad Request",
                                  request=request)
            query = lst[0]
        else:
            query = request.body

        if len(scope) > 1:
            return MyResponse("400 Bad Request\nMultiple values for 'scope'.",
                              status="400 Bad Request",
                              request=request)
        scope = scope[0]
        if scope not in {'graph', 'subtree'}:
            return MyResponse("400 Bad Request\nUnsupported scope '%s'." %
                              scope,
                              status="400 Bad Request",
                              request=request)

        # TODO LATER do something with default_graph_uri and named_graph_uri ?

        resource.force_state_refresh()
        if scope == 'subtree':
            graph = PrefixConjunctiveView(resource.uri, resource.service.store)
        else:
            # scope == 'graph'
            graph = resource.get_state()

        result = graph.query(query, base=resource.uri)

        if result.graph is not None:
            ctype = serfmt = (request.accept.best_match(self.CONSTRUCT_CTYPES)
                              or "text/turtle")
        elif result.askAnswer is not None:
            ctype = request.accept.best_match(self.ASK_CTYPES)
            if ctype is None:
                ctype = "application/sparql-results+json"
            serfmt = self.SELECT_CTYPES[ctype]
        else:
            ctype = request.accept.best_match(self.SELECT_CTYPES)
            if ctype is None:
                ctype = "application/sparql-results+json"
            serfmt = self.SELECT_CTYPES[ctype]
        try:
            return MyResponse(result.serialize(format=serfmt),
                              status="200 Ok",
                              content_type=ctype,
                              request=request)
        except Exception, ex:
            if ex.message.startswith(
                    "You performed a query operation requiring a dataset"):
                status = "403 Forbidden"
                return MyResponse("%s\n%s" % (status, ex.message),
                                  status=status,
                                  request=request)
            else:
                raise
Beispiel #5
0
    def handle_sparql(self, request):
        """
        I handle a SPARQL request
        """
        resource = request.environ['rdfrest.resource']
        if request.method == "GET" \
        or request.content_type == "application/sparql-query":
            params = request.GET
        else:
            params = request.POST
        scope = params.getall("scope") or ['graph']
        default_graph_uri = params.getall("default-graph-uri")
        named_graph_uri = params.getall("named-graph-uri")

        if request.content_type != "application/sparql-query":
            lst = params.getall("query")
            if len(lst) == 0:
                # NB: not rejecting several queries, because some services
                # provide the same query several times (YASGUI)
                return MyResponse("400 Bad Request\nQuery not provided",
                                  status="400 Bad Request",
                                  request=request)
            query = lst[0]
        else:
            query = request.body

        if len(scope) > 1:
            return MyResponse("400 Bad Request\nMultiple values for 'scope'.",
                              status="400 Bad Request",
                              request=request)
        scope = scope[0]
        if scope not in { 'graph', 'subtree' }:
            return MyResponse("400 Bad Request\nUnsupported scope '%s'." % scope,
                              status="400 Bad Request",
                              request=request)

        # TODO LATER do something with default_graph_uri and named_graph_uri ?

        resource.force_state_refresh()
        if scope == 'subtree':
            graph = PrefixConjunctiveView(resource.uri, resource.service.store)
        else:
            # scope == 'graph'
            graph = resource.get_state()

        result = graph.query(query, base=resource.uri)
        
        if result.graph is not None:
            ctype = serfmt = (
                request.accept.best_match(self.CONSTRUCT_CTYPES)
                or "text/turtle"
            )
        elif result.askAnswer is not None:
            ctype = request.accept.best_match(self.ASK_CTYPES)
            if ctype is None:
                ctype = "application/sparql-results+json"
            serfmt = self.SELECT_CTYPES[ctype]
        else:
            ctype = request.accept.best_match(self.SELECT_CTYPES)
            if ctype is None:
                ctype = "application/sparql-results+json"
            serfmt = self.SELECT_CTYPES[ctype]
        try:
            return MyResponse(result.serialize(format=serfmt),
                              status="200 Ok",
                              content_type=ctype,
                              request=request)
        except Exception, ex:
            if ex.message.startswith(
                    "You performed a query operation requiring a dataset"):
                status = "403 Forbidden"
                return MyResponse("%s\n%s"
                                  % (status, ex.message),
                                  status=status,
                                  request=request)
            else:
                raise