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