Search in sources :

Example 1 with QueryExecException

use of org.apache.jena.query.QueryExecException in project jena by apache.

the class Service method setAnyTimeouts.

/**
     * Modified from QueryExecutionBase
     * 
     * @see org.apache.jena.sparql.engine.QueryExecutionBase
     */
private static void setAnyTimeouts(HttpQuery query, Context context) {
    if (context.isDefined(queryTimeout)) {
        Object obj = context.get(queryTimeout);
        if (obj instanceof Number) {
            int x = ((Number) obj).intValue();
            query.setConnectTimeout(x);
        } else if (obj instanceof String) {
            try {
                String str = obj.toString();
                if (str.contains(",")) {
                    String[] a = str.split(",");
                    int x1 = Integer.parseInt(a[0]);
                    int x2 = Integer.parseInt(a[1]);
                    query.setConnectTimeout(x1);
                    query.setReadTimeout(x2);
                } else {
                    int x = Integer.parseInt(str);
                    query.setConnectTimeout(x);
                }
            } catch (NumberFormatException ex) {
                throw new QueryExecException("Can't interpret string for timeout: " + obj);
            }
        } else {
            throw new QueryExecException("Can't interpret timeout: " + obj);
        }
    }
}
Also used : QueryExecException(org.apache.jena.query.QueryExecException)

Example 2 with QueryExecException

use of org.apache.jena.query.QueryExecException in project jena by apache.

the class Service method getServiceParamsFromContext.

// Old way - retain but ARQ.httpRequestModifer preferred.
// This is to allow setting additional/optional query parameters on a per
// SERVICE level, see: JENA-195
/*package*/
static Params getServiceParamsFromContext(String serviceURI, Context context) throws QueryExecException {
    Params params = Params.create();
    Object obj = context.get(serviceParams);
    if (obj == null)
        return params;
    // Old style.
    try {
        @SuppressWarnings("unchecked") Map<String, Map<String, List<String>>> serviceParams = (Map<String, Map<String, List<String>>>) obj;
        if (serviceParams != null) {
            Map<String, List<String>> paramsMap = serviceParams.get(serviceURI);
            if (paramsMap != null) {
                for (String param : paramsMap.keySet()) {
                    if (HttpParams.pQuery.equals(param))
                        throw new QueryExecException("ARQ serviceParams overrides the 'query' SPARQL protocol parameter");
                    List<String> values = paramsMap.get(param);
                    for (String value : values) params.add(param, value);
                }
            }
        }
        return params;
    } catch (Throwable ex) {
        LOGGER.warn("Failed to process " + obj + " : context value of ARQ.serviceParams");
        return null;
    }
}
Also used : HttpParams(org.apache.jena.sparql.engine.http.HttpParams) List(java.util.List) QueryExecException(org.apache.jena.query.QueryExecException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with QueryExecException

use of org.apache.jena.query.QueryExecException in project jena by apache.

the class Service method exec.

public static QueryIterator exec(OpService op, Context context) {
    if (context == null)
        context = emptyContext;
    if (context != null && context.isFalse(httpServiceAllowed))
        throw new QueryExecException("SERVICE execution disabled");
    if (!op.getService().isURI())
        throw new QueryExecException("Service URI not bound: " + op.getService());
    boolean silent = op.getSilent();
    // [QExec] Add getSubOpUnmodified();
    if (!op.getService().isURI())
        throw new QueryExecException("Service URI not bound: " + op.getService());
    String serviceURL = op.getService().getURI();
    Op opRemote = op.getSubOp();
    Query query;
    if (false) {
        // ***** Interacts with substitution.
        Element el = op.getServiceElement().getElement();
        if (el instanceof ElementSubQuery)
            query = ((ElementSubQuery) el).getQuery();
        else {
            query = QueryFactory.create();
            query.setQueryPattern(el);
            query.setResultVars();
        }
    }
    // This relies on the observation that the query was originally correct,
    // so reversing the scope renaming is safe (it merely restores the
    // algebra expression).
    // 
    // Any variables that reappear should be internal ones that were hidden
    // by renaming in the first place.
    // 
    // Any substitution is also safe because it replaces variables by
    // values.
    // 
    // It is safer to rename/unrename than skipping SERVICE during rename
    // to avoid substituting hidden variables.
    Op opRestored = Rename.reverseVarRename(opRemote, true);
    query = OpAsQuery.asQuery(opRestored);
    // Transforming: Same object means "no change"
    boolean requiresRemapping = false;
    Map<Var, Var> varMapping = null;
    if (!opRestored.equals(opRemote)) {
        varMapping = new HashMap<>();
        Set<Var> originalVars = OpVars.visibleVars(op);
        Set<Var> remoteVars = OpVars.visibleVars(opRestored);
        for (Var v : originalVars) {
            if (v.getName().contains("/")) {
                // A variable which was scope renamed so has a different name
                String origName = v.getName().substring(v.getName().lastIndexOf('/') + 1);
                Var remoteVar = Var.alloc(origName);
                if (remoteVars.contains(remoteVar)) {
                    varMapping.put(remoteVar, v);
                    requiresRemapping = true;
                }
            } else {
                // A variable which does not have a different name
                if (remoteVars.contains(v))
                    varMapping.put(v, v);
            }
        }
    }
    // -- Setup
    // boolean withCompression = context.isTrueOrUndef(httpQueryCompression);
    long timeoutMillis = timeoutFromContext(context);
    // RegistryServiceModifier is applied by QueryExecHTTP
    Params serviceParams = getServiceParamsFromContext(serviceURL, context);
    HttpClient httpClient = chooseHttpClient(serviceURL, context);
    QuerySendMode querySendMode = chooseQuerySendMode(serviceURL, context, QuerySendMode.asGetWithLimitBody);
    // -- End setup
    // Build the execution
    QueryExecHTTP qExec = QueryExecHTTP.newBuilder().endpoint(serviceURL).timeout(timeoutMillis, TimeUnit.MILLISECONDS).query(query).params(serviceParams).context(context).httpClient(httpClient).sendMode(querySendMode).build();
    try {
        // Detach from the network stream.
        RowSet rowSet = qExec.select().materialize();
        QueryIterator qIter = QueryIterPlainWrapper.create(rowSet);
        if (requiresRemapping)
            qIter = QueryIter.map(qIter, varMapping);
        return qIter;
    } catch (HttpException ex) {
        throw QueryExceptionHTTP.rewrap(ex);
    }
}
Also used : Op(org.apache.jena.sparql.algebra.Op) ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) OpAsQuery(org.apache.jena.sparql.algebra.OpAsQuery) Query(org.apache.jena.query.Query) Var(org.apache.jena.sparql.core.Var) Element(org.apache.jena.sparql.syntax.Element) RowSet(org.apache.jena.sparql.exec.RowSet) HttpParams(org.apache.jena.sparql.engine.http.HttpParams) QueryExecException(org.apache.jena.query.QueryExecException) ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) HttpClient(java.net.http.HttpClient) RegistryHttpClient(org.apache.jena.http.RegistryHttpClient) HttpException(org.apache.jena.atlas.web.HttpException)

Example 4 with QueryExecException

use of org.apache.jena.query.QueryExecException in project jena by apache.

the class BindingComparator method compare.

// Compare bindings by iterating.
// Node comparsion is:
// Compare by
@Override
public int compare(Binding bind1, Binding bind2) {
    for (SortCondition sc : conditions) {
        if (sc.expression == null) {
            throw new QueryExecException("Broken sort condition");
        }
        NodeValue nv1 = null;
        NodeValue nv2 = null;
        try {
            nv1 = sc.expression.eval(bind1, env);
        } catch (VariableNotBoundException ex) {
        } catch (ExprEvalException ex) {
            Log.warn(this, ex.getMessage());
        }
        try {
            nv2 = sc.expression.eval(bind2, env);
        } catch (VariableNotBoundException ex) {
        } catch (ExprEvalException ex) {
            Log.warn(this, ex.getMessage());
        }
        Node n1 = NodeValue.toNode(nv1);
        Node n2 = NodeValue.toNode(nv2);
        int x = compareNodes(nv1, nv2, sc.direction);
        if (x != Expr.CMP_EQUAL) {
            return x;
        }
    }
    // Same by the SortConditions - now do any extra tests to make sure they are unique.
    return compareBindingsSyntactic(bind1, bind2);
// return 0 ;
}
Also used : SortCondition(org.apache.jena.query.SortCondition) NodeValue(org.apache.jena.sparql.expr.NodeValue) VariableNotBoundException(org.apache.jena.sparql.expr.VariableNotBoundException) Node(org.apache.jena.graph.Node) QueryExecException(org.apache.jena.query.QueryExecException) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 5 with QueryExecException

use of org.apache.jena.query.QueryExecException in project jena by apache.

the class QueryIterService method nextStage.

@Override
protected QueryIterator nextStage(Binding outerBinding) {
    boolean silent = opService.getSilent();
    ExecutionContext execCxt = getExecContext();
    Context cxt = execCxt.getContext();
    ServiceExecutorRegistry registry = ServiceExecutorRegistry.get(cxt);
    ServiceExecution svcExec = null;
    OpService substitutedOp = (OpService) QC.substitute(opService, outerBinding);
    try {
        // ---- Find handler
        if (registry != null) {
            for (ServiceExecutorFactory factory : registry.getFactories()) {
                // Internal consistency check
                if (factory == null) {
                    Log.warn(this, "SERVICE <" + opService.getService().toString() + ">: Null item in custom ServiceExecutionRegistry");
                    continue;
                }
                svcExec = factory.createExecutor(substitutedOp, opService, outerBinding, execCxt);
                if (svcExec != null)
                    break;
            }
        }
        // ---- Execute
        if (svcExec == null)
            throw new QueryExecException("No SERVICE handler");
        QueryIterator qIter = svcExec.exec();
        qIter = QueryIter.makeTracked(qIter, getExecContext());
        // There should be no variables in common because of the OpSubstitute.substitute
        return new QueryIterCommonParent(qIter, outerBinding, getExecContext());
    } catch (RuntimeException ex) {
        if (silent) {
            Log.warn(this, "SERVICE " + NodeFmtLib.strTTL(substitutedOp.getService()) + " : " + ex.getMessage());
            // Return the input
            return QueryIterSingleton.create(outerBinding, getExecContext());
        }
        throw ex;
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) ServiceExecutorFactory(org.apache.jena.sparql.service.ServiceExecutorFactory) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) ServiceExecution(org.apache.jena.sparql.service.ServiceExecution) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) QueryIterCommonParent(org.apache.jena.sparql.engine.iterator.QueryIterCommonParent) OpService(org.apache.jena.sparql.algebra.op.OpService) ServiceExecutorRegistry(org.apache.jena.sparql.service.ServiceExecutorRegistry) QueryExecException(org.apache.jena.query.QueryExecException)

Aggregations

QueryExecException (org.apache.jena.query.QueryExecException)16 Node (org.apache.jena.graph.Node)5 Var (org.apache.jena.sparql.core.Var)4 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)4 HashMap (java.util.HashMap)2 Query (org.apache.jena.query.Query)2 RDFNode (org.apache.jena.rdf.model.RDFNode)2 Op (org.apache.jena.sparql.algebra.Op)2 OpAsQuery (org.apache.jena.sparql.algebra.OpAsQuery)2 HttpParams (org.apache.jena.sparql.engine.http.HttpParams)2 Template (org.apache.jena.sparql.syntax.Template)2 InputStream (java.io.InputStream)1 HttpClient (java.net.http.HttpClient)1 List (java.util.List)1 Map (java.util.Map)1 IndentedLineBuffer (org.apache.jena.atlas.io.IndentedLineBuffer)1 JsonArray (org.apache.jena.atlas.json.JsonArray)1 JsonObject (org.apache.jena.atlas.json.JsonObject)1 JsonValue (org.apache.jena.atlas.json.JsonValue)1 HttpException (org.apache.jena.atlas.web.HttpException)1