Search in sources :

Example 11 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class CachedResultsBean method getRows.

/**
 * Returns a set of rows based on the given starting and end positions. The response object type is dynamic, see the listQueryLogic operation to determine
 * what the response type object will be.
 *
 * @param queryId
 *            CachedResults queryId
 * @param rowBegin
 *            first row to be returned
 * @param rowEnd
 *            last row to be returned
 * @return datawave.webservice.result.BaseQueryResponse
 * @RequestHeader X-ProxiedEntitiesChain use when proxying request for user by specifying a chain of DNs of the identities to proxy
 * @RequestHeader X-ProxiedIssuersChain required when using X-ProxiedEntitiesChain, specify one issuer DN per subject DN listed in X-ProxiedEntitiesChain
 * @RequestHeader query-session-id session id value used for load balancing purposes. query-session-id can be placed in the request in a Cookie header or as
 *                a query parameter
 * @ResponseHeader X-OperationTimeInMS time spent on the server performing the operation, does not account for network or result serialization
 * @ResponseHeader X-Partial-Results true if the page contains less than the requested number of results
 *
 * @HTTP 200 success
 * @HTTP 401 caller is not authorized to run the query
 * @HTTP 412 if the query is not active
 * @HTTP 500 internal server error
 */
@GET
@javax.ws.rs.Path("/{queryId}/getRows")
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf" })
@GZIP
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
@Timed(name = "dw.cachedr.getRows", absolute = true)
public BaseQueryResponse getRows(@PathParam("queryId") @Required("queryId") String queryId, @QueryParam("rowBegin") @DefaultValue("1") Integer rowBegin, @QueryParam("rowEnd") Integer rowEnd) {
    BaseQueryResponse response = responseObjectFactory.getEventQueryResponse();
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String owner = getOwnerFromPrincipal(p);
    try {
        if (rowBegin < 1) {
            throw new BadRequestQueryException(DatawaveErrorCode.ROW_BEGIN_LESS_THAN_1);
        }
        if (rowEnd != null && rowEnd < rowBegin) {
            throw new BadRequestQueryException(DatawaveErrorCode.ROW_END_LESS_THAN_ROW_BEGIN);
        }
        // If there is a this.maxPageSize set, then we should honor it here. Otherwise, we use Integer.MAX_VALUE
        int maxPageSize = cachedResultsConfiguration.getMaxPageSize();
        if (rowEnd == null) {
            if (maxPageSize > 0) {
                rowEnd = (rowBegin + maxPageSize) - 1;
            } else {
                rowEnd = Integer.MAX_VALUE;
            }
        }
        int pagesize = (rowEnd - rowBegin) + 1;
        if (maxPageSize > 0 && pagesize > maxPageSize) {
            throw new QueryException(DatawaveErrorCode.TOO_MANY_ROWS_REQUESTED, MessageFormat.format("Size must be less than or equal to: {0}", maxPageSize));
        }
        CachedRunningQuery crq = null;
        try {
            // Get the CachedRunningQuery object from the cache
            try {
                crq = retrieve(queryId, owner);
            } catch (IOException e) {
                throw new PreConditionFailedQueryException(DatawaveErrorCode.CACHED_RESULTS_IMPORT_ERROR, e);
            }
            if (null == crq)
                throw new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_NOT_CACHED);
            response = crq.getQueryLogic().getResponseObjectFactory().getEventQueryResponse();
            if (!crq.getUser().equals(owner)) {
                throw new UnauthorizedQueryException(DatawaveErrorCode.QUERY_OWNER_MISMATCH, MessageFormat.format("{0} != {1}", crq.getUser(), owner));
            }
            synchronized (crq) {
                if (crq.isActivated() == false) {
                    Connection connection = ds.getConnection();
                    String logicName = crq.getQueryLogicName();
                    QueryLogic<?> queryLogic = queryFactory.getQueryLogic(logicName, p);
                    crq.activate(connection, queryLogic);
                }
                try {
                    ResultsPage resultList = crq.getRows(rowBegin, rowEnd, cachedResultsConfiguration.getPageByteTrigger());
                    response = crq.getTransformer().createResponse(resultList);
                    Status status;
                    if (!resultList.getResults().isEmpty()) {
                        response.setHasResults(true);
                        status = Status.OK;
                    } else {
                        response.setHasResults(false);
                        status = Status.NO_CONTENT;
                    }
                    response.setLogicName(crq.getQueryLogic().getLogicName());
                    response.setQueryId(crq.getQueryId());
                    if (response instanceof TotalResultsAware) {
                        ((TotalResultsAware) response).setTotalResults(crq.getTotalRows());
                    }
                    if (status == Status.NO_CONTENT) {
                        throw new NoResultsQueryException(DatawaveErrorCode.NO_CONTENT_STATUS);
                    }
                    crq.getMetric().setLifecycle(QueryMetric.Lifecycle.RESULTS);
                    return response;
                } catch (SQLException e) {
                    throw new QueryException();
                } catch (NoResultsQueryException e) {
                    throw e;
                } catch (RuntimeException e) {
                    log.error(e.getMessage(), e);
                    throw e;
                } finally {
                    crq.closeConnection(log);
                }
            }
        } finally {
            // Push metrics
            try {
                if (null != crq && crq.getQueryLogic().getCollectQueryMetrics() == true) {
                    metrics.updateMetric(crq.getMetric());
                }
            } catch (Exception e1) {
                log.error("Error updating metrics", e1);
            }
        }
    } catch (Exception e) {
        DatawaveErrorCode dec;
        if (e instanceof NoResultsQueryException) {
            dec = DatawaveErrorCode.NO_CONTENT_STATUS;
        } else {
            dec = DatawaveErrorCode.CACHED_QUERY_TRANSACTION_ERROR;
        }
        QueryException qe = new QueryException(dec, e);
        log.error(qe);
        response.addException(qe.getBottomQueryException());
        int statusCode = qe.getBottomQueryException().getStatusCode();
        throw new DatawaveWebApplicationException(qe, response, statusCode);
    }
}
Also used : FileStatus(org.apache.hadoop.fs.FileStatus) Status(javax.ws.rs.core.Response.Status) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultsPage(datawave.webservice.query.cache.ResultsPage) IOException(java.io.IOException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) EJBException(javax.ejb.EJBException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) BatchUpdateException(java.sql.BatchUpdateException) SQLException(java.sql.SQLException) IOException(java.io.IOException) QueryCanceledQueryException(datawave.webservice.query.exception.QueryCanceledQueryException) QueryException(datawave.webservice.query.exception.QueryException) QueryCanceledException(datawave.webservice.common.exception.QueryCanceledException) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) NotFoundException(datawave.webservice.common.exception.NotFoundException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) NoResultsException(datawave.webservice.common.exception.NoResultsException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) MalformedURLException(java.net.MalformedURLException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryCanceledQueryException(datawave.webservice.query.exception.QueryCanceledQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) TotalResultsAware(datawave.webservice.result.TotalResultsAware) DatawaveErrorCode(datawave.webservice.query.exception.DatawaveErrorCode) BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Interceptors(javax.interceptor.Interceptors) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 12 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class CachedResultsBean method previous.

/**
 * Returns the previous page of results to the caller. The response object type is dynamic, see the listQueryLogic operation to determine what the response
 * type object will be.
 *
 * @param queryId
 *            user defined id for this query
 * @return previous page of results
 *
 * @return datawave.webservice.result.BaseQueryResponse
 * @RequestHeader X-ProxiedEntitiesChain use when proxying request for user by specifying a chain of DNs of the identities to proxy
 * @RequestHeader X-ProxiedIssuersChain required when using X-ProxiedEntitiesChain, specify one issuer DN per subject DN listed in X-ProxiedEntitiesChain
 * @RequestHeader query-session-id session id value used for load balancing purposes. query-session-id can be placed in the request in a Cookie header or as
 *                a query parameter
 * @ResponseHeader X-OperationTimeInMS time spent on the server performing the operation, does not account for network or result serialization
 * @ResponseHeader X-query-page-number page number returned by this call
 * @ResponseHeader X-query-last-page if true then there are no more pages for this query, caller should call close()
 * @ResponseHeader X-Partial-Results true if the page contains less than the requested number of results
 *
 * @HTTP 200 success
 * @HTTP 401 caller is not authorized to run the query
 * @HTTP 412 if the query is not active
 * @HTTP 500 internal server error
 */
@GET
@javax.ws.rs.Path("/{queryId}/previous")
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf" })
@GZIP
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
@Timed(name = "dw.cachedr.previous", absolute = true)
public BaseQueryResponse previous(@PathParam("queryId") @Required("queryId") String queryId) {
    BaseQueryResponse response = responseObjectFactory.getEventQueryResponse();
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String owner = getOwnerFromPrincipal(p);
    try {
        CachedRunningQuery crq = null;
        try {
            // Get the CachedRunningQuery object from the cache
            try {
                crq = retrieve(queryId, owner);
            } catch (IOException e) {
                throw new PreConditionFailedQueryException(DatawaveErrorCode.CACHED_RESULTS_IMPORT_ERROR, e);
            }
            if (null == crq)
                throw new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_NOT_CACHED);
            if (!crq.getUser().equals(owner)) {
                throw new UnauthorizedQueryException(DatawaveErrorCode.QUERY_OWNER_MISMATCH, MessageFormat.format("{0} != {1}", crq.getUser(), owner));
            }
            synchronized (crq) {
                if (crq.isActivated() == false) {
                    if (crq.getShouldAutoActivate()) {
                        Connection connection = ds.getConnection();
                        String logicName = crq.getQueryLogicName();
                        QueryLogic<?> queryLogic = queryFactory.getQueryLogic(logicName, p);
                        crq.activate(connection, queryLogic);
                    } else {
                        throw new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_TIMEOUT_FOR_RESOURCES);
                    }
                }
                try {
                    ResultsPage resultList = crq.previous(cachedResultsConfiguration.getPageByteTrigger());
                    long pageNum = crq.getLastPageNumber();
                    response = crq.getTransformer().createResponse(resultList);
                    Status status = null;
                    if (!resultList.getResults().isEmpty()) {
                        response.setHasResults(true);
                        status = Status.OK;
                    } else {
                        response.setHasResults(false);
                        status = Status.NO_CONTENT;
                    }
                    response.setPageNumber(pageNum);
                    response.setLogicName(crq.getQueryLogic().getLogicName());
                    response.setQueryId(crq.getQueryId());
                    if (response instanceof TotalResultsAware) {
                        ((TotalResultsAware) response).setTotalResults(crq.getTotalRows());
                    }
                    if (status == Status.NO_CONTENT) {
                        throw new NoResultsException(null);
                    }
                    crq.getMetric().setLifecycle(QueryMetric.Lifecycle.RESULTS);
                    return response;
                } catch (SQLException e) {
                    throw new QueryException(DatawaveErrorCode.CACHED_QUERY_SQL_ERROR, e);
                }
            }
        } finally {
            // Push metrics
            if (null != crq && crq.getQueryLogic().getCollectQueryMetrics() == true) {
                try {
                    metrics.updateMetric(crq.getMetric());
                } catch (Exception e1) {
                    log.error("Error updating metrics", e1);
                }
            }
        }
    } catch (Exception e) {
        QueryException qe = null;
        if (e instanceof NoResultsException) {
            qe = new QueryException(DatawaveErrorCode.NO_CONTENT_STATUS, e);
        } else {
            qe = new QueryException("Error calling previous.", e, "500-42");
            log.error(qe);
        }
        response.addException(qe.getBottomQueryException());
        int statusCode = qe.getBottomQueryException().getStatusCode();
        throw new DatawaveWebApplicationException(qe, response, statusCode);
    }
}
Also used : FileStatus(org.apache.hadoop.fs.FileStatus) Status(javax.ws.rs.core.Response.Status) NoResultsException(datawave.webservice.common.exception.NoResultsException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultsPage(datawave.webservice.query.cache.ResultsPage) IOException(java.io.IOException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) EJBException(javax.ejb.EJBException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) BatchUpdateException(java.sql.BatchUpdateException) SQLException(java.sql.SQLException) IOException(java.io.IOException) QueryCanceledQueryException(datawave.webservice.query.exception.QueryCanceledQueryException) QueryException(datawave.webservice.query.exception.QueryException) QueryCanceledException(datawave.webservice.common.exception.QueryCanceledException) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) NotFoundException(datawave.webservice.common.exception.NotFoundException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) NoResultsException(datawave.webservice.common.exception.NoResultsException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) MalformedURLException(java.net.MalformedURLException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryCanceledQueryException(datawave.webservice.query.exception.QueryCanceledQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) TotalResultsAware(datawave.webservice.result.TotalResultsAware) BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Interceptors(javax.interceptor.Interceptors) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 13 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class CachedResultsBean method cancelLoad.

/**
 * Cancel the load process.
 *
 * @param originalQueryId
 *
 * @return datawave.webservice.result.VoidResponse
 * @RequestHeader X-ProxiedEntitiesChain use when proxying request for user by specifying a chain of DNs of the identities to proxy
 * @RequestHeader X-ProxiedIssuersChain required when using X-ProxiedEntitiesChain, specify one issuer DN per subject DN listed in X-ProxiedEntitiesChain
 * @RequestHeader query-session-id session id value used for load balancing purposes. query-session-id can be placed in the request in a Cookie header or as
 *                a query parameter
 * @ResponseHeader X-OperationTimeInMS time spent on the server performing the operation, does not account for network or result serialization
 *
 * @HTTP 200 success
 * @HTTP 401 caller is not authorized to cancel the query
 */
@PUT
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff" })
@javax.ws.rs.Path("/{queryId}/cancel")
@GZIP
@ClearQuerySessionId
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
@Timed(name = "dw.cachedr.cancel", absolute = true)
public VoidResponse cancelLoad(@PathParam("queryId") @Required("queryId") String originalQueryId) {
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String owner = getOwnerFromPrincipal(p);
    VoidResponse response = new VoidResponse();
    try {
        // check if query is even loading
        RunningQuery query = CachedResultsBean.loadingQueryMap.get(originalQueryId);
        if (query == null) {
            NotFoundQueryException e = new NotFoundQueryException(DatawaveErrorCode.NO_QUERY_OBJECT_MATCH);
            throw new NotFoundException(e, response);
        } else {
            if (query.getSettings().getOwner().equals(owner)) {
                accumuloConnectionRequestBean.cancelConnectionRequest(originalQueryId);
                query.cancel();
                response.addMessage("CachedResults load canceled.");
            } else {
                UnauthorizedQueryException e = new UnauthorizedQueryException(DatawaveErrorCode.QUERY_OWNER_MISMATCH, MessageFormat.format("{0} != {1}", query.getSettings().getOwner(), owner));
                throw new UnauthorizedException(e, response);
            }
        }
        return response;
    } catch (DatawaveWebApplicationException e) {
        throw e;
    } catch (Exception e) {
        QueryException qe = new QueryException(DatawaveErrorCode.CANCELLATION_ERROR, e, MessageFormat.format("query_id: {0}", originalQueryId));
        log.error(qe);
        response.addException(qe.getBottomQueryException());
        int statusCode = qe.getBottomQueryException().getStatusCode();
        throw new DatawaveWebApplicationException(qe, response, statusCode);
    }
}
Also used : RunningQuery(datawave.webservice.query.runner.RunningQuery) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryCanceledQueryException(datawave.webservice.query.exception.QueryCanceledQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) VoidResponse(datawave.webservice.result.VoidResponse) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) NotFoundException(datawave.webservice.common.exception.NotFoundException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) EJBException(javax.ejb.EJBException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) BatchUpdateException(java.sql.BatchUpdateException) SQLException(java.sql.SQLException) IOException(java.io.IOException) QueryCanceledQueryException(datawave.webservice.query.exception.QueryCanceledQueryException) QueryException(datawave.webservice.query.exception.QueryException) QueryCanceledException(datawave.webservice.common.exception.QueryCanceledException) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) NotFoundException(datawave.webservice.common.exception.NotFoundException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) NoResultsException(datawave.webservice.common.exception.NoResultsException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) MalformedURLException(java.net.MalformedURLException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) Interceptors(javax.interceptor.Interceptors) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ClearQuerySessionId(datawave.annotation.ClearQuerySessionId) GZIP(org.jboss.resteasy.annotations.GZIP) PUT(javax.ws.rs.PUT)

Example 14 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class AtomServiceBean method getCategories.

/**
 * @return Atom Categories document that lists category names
 */
@GET
@GZIP
@Produces("application/atomcat+xml")
@Path("/categories")
public Categories getCategories() {
    Principal p = ctx.getCallerPrincipal();
    Set<Authorizations> auths = new HashSet<>();
    if (p instanceof DatawavePrincipal) {
        DatawavePrincipal dp = (DatawavePrincipal) p;
        for (Collection<String> cbAuths : dp.getAuthorizations()) auths.add(new Authorizations(cbAuths.toArray(new String[cbAuths.size()])));
    }
    Categories result;
    Connector connection = null;
    try {
        result = abdera.newCategories();
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        connection = connectionFactory.getConnection(poolName, Priority.NORMAL, trackingMap);
        try (Scanner scanner = ScannerHelper.createScanner(connection, tableName + "Categories", auths)) {
            Map<String, String> props = new HashMap<>();
            props.put(MatchingKeySkippingIterator.ROW_DELIMITER_OPTION, "\0");
            props.put(MatchingKeySkippingIterator.NUM_SCANS_STRING_NAME, "5");
            IteratorSetting setting = new IteratorSetting(30, MatchingKeySkippingIterator.class, props);
            scanner.addScanIterator(setting);
            for (Map.Entry<Key, Value> entry : scanner) {
                String collectionName = entry.getKey().getRow().toString();
                result.addCategory(collectionName);
            }
        }
        if (result.getCategories().isEmpty())
            throw new NoResultsException(null);
        else
            return result;
    } catch (WebApplicationException web) {
        throw web;
    } catch (Exception e) {
        VoidResponse response = new VoidResponse();
        QueryException qe = new QueryException(DatawaveErrorCode.COLLECTION_ERROR, e);
        log.error(qe);
        response.addException(qe.getBottomQueryException());
        throw new DatawaveWebApplicationException(qe, response);
    } finally {
        if (null != connection) {
            try {
                connectionFactory.returnConnection(connection);
            } catch (Exception e) {
                log.error("Error returning connection to factory", e);
            }
        }
    }
}
Also used : NoResultsException(datawave.webservice.common.exception.NoResultsException) Connector(org.apache.accumulo.core.client.Connector) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) WebApplicationException(javax.ws.rs.WebApplicationException) Categories(org.apache.abdera.model.Categories) HashMap(java.util.HashMap) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) WebApplicationException(javax.ws.rs.WebApplicationException) NoResultsException(datawave.webservice.common.exception.NoResultsException) QueryException(datawave.webservice.query.exception.QueryException) QueryException(datawave.webservice.query.exception.QueryException) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) VoidResponse(datawave.webservice.result.VoidResponse) DefaultValue(javax.ws.rs.DefaultValue) Value(org.apache.accumulo.core.data.Value) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) Map(java.util.Map) HashMap(java.util.HashMap) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Key(org.apache.accumulo.core.data.Key) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 15 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class IteratorBuildingVisitor method visit.

@Override
public Object visit(ASTAndNode and, Object data) {
    QueryPropertyMarker.Instance instance = QueryPropertyMarker.findInstance(and);
    if (instance.isType(ExceededOrThresholdMarkerJexlNode.class)) {
        JexlNode source = instance.getSource();
        // Ivarator to get the job done
        if (source instanceof ASTAndNode) {
            try {
                ivarateList(and, source, data);
            } catch (IOException ioe) {
                throw new DatawaveFatalQueryException(ioe);
            }
        } else {
            QueryException qe = new QueryException(DatawaveErrorCode.UNEXPECTED_SOURCE_NODE, MessageFormat.format("{0}", "Limited ExceededOrThresholdMarkerJexlNode"));
            throw new DatawaveFatalQueryException(qe);
        }
    } else if (data instanceof IndexRangeIteratorBuilder) {
        // index checking has already been done, otherwise we would not have
        // an "ExceededValueThresholdMarker"
        // hence the "IndexAgnostic" method can be used here
        LiteralRange range = JexlASTHelper.findRange().recursively().getRange(and);
        if (range == null) {
            QueryException qe = new QueryException(DatawaveErrorCode.MULTIPLE_RANGES_IN_EXPRESSION);
            throw new DatawaveFatalQueryException(qe);
        }
        ((IndexRangeIteratorBuilder) data).setRange(range);
    } else if (instance.isType(ExceededValueThresholdMarkerJexlNode.class)) {
        // if the parent is our ExceededValueThreshold marker, then use an
        // Ivarator to get the job done unless we don't have to
        JexlNode source = instance.getSource();
        String identifier = null;
        LiteralRange<?> range = null;
        boolean negatedLocal = false;
        if (source instanceof ASTAndNode) {
            range = buildLiteralRange(source, null);
            identifier = range.getFieldName();
        } else {
            if (source instanceof ASTNRNode || source instanceof ASTNotNode)
                negatedLocal = true;
            range = buildLiteralRange(source);
            identifier = JexlASTHelper.getIdentifier(source);
        }
        boolean negatedOverall = negatedLocal;
        if (data instanceof AbstractIteratorBuilder) {
            AbstractIteratorBuilder oib = (AbstractIteratorBuilder) data;
            if (oib.isInANot()) {
                negatedOverall = !negatedOverall;
            }
        }
        // or the field is index only but not in the term frequencies, then we must ivarate
        if (!limitLookup || !allowTermFrequencyLookup || (indexOnlyFields.contains(identifier) && !termFrequencyFields.contains(identifier))) {
            if (source instanceof ASTAndNode) {
                try {
                    List<ASTFunctionNode> functionNodes = JexlASTHelper.getFunctionNodes(source).stream().filter(node -> JexlFunctionArgumentDescriptorFactory.F.getArgumentDescriptor(node).allowIvaratorFiltering()).collect(Collectors.toList());
                    if (functionNodes.isEmpty()) {
                        ivarateRange(and, source, data);
                    } else {
                        ivarateFilter(and, source, data, functionNodes);
                    }
                } catch (IOException ioe) {
                    throw new DatawaveFatalQueryException("Unable to ivarate", ioe);
                }
            } else if (source instanceof ASTERNode || source instanceof ASTNRNode) {
                try {
                    ivarateRegex(and, source, data);
                } catch (IOException ioe) {
                    throw new DatawaveFatalQueryException("Unable to ivarate", ioe);
                }
            } else {
                QueryException qe = new QueryException(DatawaveErrorCode.UNEXPECTED_SOURCE_NODE, MessageFormat.format("{0}", "ExceededValueThresholdMarkerJexlNode"));
                throw new DatawaveFatalQueryException(qe);
            }
        } else {
            NestedIterator<Key> nested = null;
            if (termFrequencyFields.contains(identifier)) {
                nested = buildExceededFromTermFrequency(identifier, and, source, range, data);
            } else {
                /**
                 * This is okay since 1) We are doc specific 2) We are not index only or tf 3) Therefore, we must evaluate against the document for this
                 * expression 4) Return a stubbed range in case we have a disjunction that breaks the current doc.
                 */
                if (!limitOverride && !negatedOverall)
                    nested = createExceededCheck(identifier, range, and);
            }
            if (null != nested && null != data && data instanceof AbstractIteratorBuilder) {
                AbstractIteratorBuilder iterators = (AbstractIteratorBuilder) data;
                if (negatedLocal) {
                    iterators.addExclude(nested);
                } else {
                    iterators.addInclude(nested);
                }
            } else {
                if (isQueryFullySatisfied == true) {
                    log.warn("Determined that isQueryFullySatisfied should be false, but it was not preset to false in the SatisfactionVisitor");
                }
                return nested;
            }
        }
    } else if (null != data && data instanceof AndIteratorBuilder) {
        and.childrenAccept(this, data);
    } else {
        // Create an AndIterator and recursively add the children
        AbstractIteratorBuilder andItr = new AndIteratorBuilder();
        andItr.negateAsNeeded(data);
        and.childrenAccept(this, andItr);
        // If there is no parent
        if (data == null) {
            // Make this AndIterator the root node
            if (!andItr.includes().isEmpty()) {
                root = andItr.build();
            }
        } else {
            // Otherwise, add this AndIterator to its parent
            AbstractIteratorBuilder parent = (AbstractIteratorBuilder) data;
            if (!andItr.includes().isEmpty()) {
                parent.addInclude(andItr.build());
            }
        }
        if (log.isTraceEnabled()) {
            log.trace("ASTAndNode visit: pretty formatting of:\nparent.includes:" + formatIncludesOrExcludes(andItr.includes()) + "\nparent.excludes:" + formatIncludesOrExcludes(andItr.excludes()));
        }
    }
    return null;
}
Also used : Arrays(java.util.Arrays) Text(org.apache.hadoop.io.Text) ASTSizeMethod(org.apache.commons.jexl2.parser.ASTSizeMethod) AbstractIteratorBuilder(datawave.query.iterator.builder.AbstractIteratorBuilder) JavaRegexParseException(datawave.query.parser.JavaRegexAnalyzer.JavaRegexParseException) IvaratorCacheDir(datawave.query.iterator.ivarator.IvaratorCacheDir) ASTNRNode(org.apache.commons.jexl2.parser.ASTNRNode) Map(java.util.Map) ExceededValueThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode) AttributeFactory(datawave.query.attributes.AttributeFactory) JexlFunctionArgumentDescriptorFactory(datawave.query.jexl.functions.JexlFunctionArgumentDescriptorFactory) ExceededOrThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededOrThresholdMarkerJexlNode) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) ASTJexlScript(org.apache.commons.jexl2.parser.ASTJexlScript) TimeFilter(datawave.query.predicate.TimeFilter) Set(java.util.Set) IndexRangeIteratorBuilder(datawave.query.iterator.builder.IndexRangeIteratorBuilder) ASTNENode(org.apache.commons.jexl2.parser.ASTNENode) DefaultArithmetic(datawave.query.jexl.DefaultArithmetic) ASTOrNode(org.apache.commons.jexl2.parser.ASTOrNode) Predicate(com.google.common.base.Predicate) AndIteratorBuilder(datawave.query.iterator.builder.AndIteratorBuilder) JexlContext(org.apache.commons.jexl2.JexlContext) DatawaveFieldIndexListIteratorJexl(datawave.core.iterators.DatawaveFieldIndexListIteratorJexl) TermFrequencyAggregator(datawave.query.jexl.functions.TermFrequencyAggregator) FileSortedSet(datawave.query.util.sortedset.FileSortedSet) IteratorBuilder(datawave.query.iterator.builder.IteratorBuilder) ASTEQNode(org.apache.commons.jexl2.parser.ASTEQNode) NestedIterator(datawave.query.iterator.NestedIterator) JexlASTHelper(datawave.query.jexl.JexlASTHelper) IndexIteratorBuilder(datawave.query.iterator.builder.IndexIteratorBuilder) JexlArithmetic(org.apache.commons.jexl2.JexlArithmetic) ASTMethodNode(org.apache.commons.jexl2.parser.ASTMethodNode) IteratorToSortedKeyValueIterator(datawave.query.util.IteratorToSortedKeyValueIterator) JexlNode(org.apache.commons.jexl2.parser.JexlNode) SortedKeyValueIterator(org.apache.accumulo.core.iterators.SortedKeyValueIterator) TreeSet(java.util.TreeSet) SourceFactory(datawave.query.iterator.SourceFactory) ArrayList(java.util.ArrayList) NodeOperand(datawave.query.jexl.LiteralRange.NodeOperand) Lists(com.google.common.collect.Lists) LiteralRange(datawave.query.jexl.LiteralRange) Key(org.apache.accumulo.core.data.Key) IndexFilterIteratorBuilder(datawave.query.iterator.builder.IndexFilterIteratorBuilder) OrIteratorBuilder(datawave.query.iterator.builder.OrIteratorBuilder) Predicates(com.google.common.base.Predicates) EventFieldIterator(datawave.query.iterator.EventFieldIterator) IndexRegexIteratorBuilder(datawave.query.iterator.builder.IndexRegexIteratorBuilder) FileSystemCache(datawave.core.iterators.filesystem.FileSystemCache) FieldIndexAggregator(datawave.query.jexl.functions.FieldIndexAggregator) QueryPropertyMarker(datawave.query.jexl.nodes.QueryPropertyMarker) TermFrequencyIndexBuilder(datawave.query.iterator.builder.TermFrequencyIndexBuilder) IOException(java.io.IOException) IndexListIteratorBuilder(datawave.query.iterator.builder.IndexListIteratorBuilder) DatawaveErrorCode(datawave.webservice.query.exception.DatawaveErrorCode) QueryLock(datawave.core.iterators.querylock.QueryLock) OrIterator(datawave.query.iterator.logic.OrIterator) NegationBuilder(datawave.query.iterator.builder.NegationBuilder) Range(org.apache.accumulo.core.data.Range) JavaRegexAnalyzer(datawave.query.parser.JavaRegexAnalyzer) Filter(datawave.query.predicate.Filter) QueryException(datawave.webservice.query.exception.QueryException) ASTEvaluationOnly(org.apache.commons.jexl2.parser.ASTEvaluationOnly) FST(org.apache.lucene.util.fst.FST) PartialKey(org.apache.accumulo.core.data.PartialKey) QuerySpanCollector(datawave.query.iterator.profile.QuerySpanCollector) IvaratorBuilder(datawave.query.iterator.builder.IvaratorBuilder) ASTIdentifier(org.apache.commons.jexl2.parser.ASTIdentifier) IteratorEnvironment(org.apache.accumulo.core.iterators.IteratorEnvironment) Script(org.apache.commons.jexl2.Script) SortedSet(java.util.SortedSet) DatawaveJexlEngine(datawave.query.jexl.DatawaveJexlEngine) URISyntaxException(java.net.URISyntaxException) SourceManager(datawave.query.iterator.SourceManager) Logger(org.apache.log4j.Logger) ValueTuple(datawave.query.attributes.ValueTuple) EventDataQueryFilter(datawave.query.predicate.EventDataQueryFilter) ASTNotNode(org.apache.commons.jexl2.parser.ASTNotNode) ASTStringLiteral(org.apache.commons.jexl2.parser.ASTStringLiteral) Path(org.apache.hadoop.fs.Path) Value(org.apache.accumulo.core.data.Value) URI(java.net.URI) UniversalSet(datawave.util.UniversalSet) TypeMetadata(datawave.query.util.TypeMetadata) ASTDelayedPredicate(org.apache.commons.jexl2.parser.ASTDelayedPredicate) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) IdentifierOpLiteral(datawave.query.jexl.JexlASTHelper.IdentifierOpLiteral) Entry(java.util.Map.Entry) IvaratorCacheDirConfig(datawave.query.iterator.ivarator.IvaratorCacheDirConfig) EventDataQueryExpressionFilter(datawave.query.predicate.EventDataQueryExpressionFilter) JexlNodes.children(org.apache.commons.jexl2.parser.JexlNodes.children) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool) ASTReferenceExpression(org.apache.commons.jexl2.parser.ASTReferenceExpression) ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral) NoOpType(datawave.data.type.NoOpType) IdentityAggregator(datawave.query.jexl.functions.IdentityAggregator) MessageFormat(java.text.MessageFormat) HashSet(java.util.HashSet) ASTERNode(org.apache.commons.jexl2.parser.ASTERNode) DatawaveJexlContext(datawave.query.jexl.DatawaveJexlContext) ASTAndNode(org.apache.commons.jexl2.parser.ASTAndNode) NoSuchElementException(java.util.NoSuchElementException) ParserTreeConstants(org.apache.commons.jexl2.parser.ParserTreeConstants) MalformedURLException(java.net.MalformedURLException) Maps(com.google.common.collect.Maps) Constants(datawave.query.Constants) ArithmeticJexlEngines(datawave.query.jexl.ArithmeticJexlEngines) ASTFunctionNode(org.apache.commons.jexl2.parser.ASTFunctionNode) CompositeMetadata(datawave.query.composite.CompositeMetadata) EventFieldAggregator(datawave.query.jexl.functions.EventFieldAggregator) Collections(java.util.Collections) ChainableEventDataQueryFilter(datawave.query.predicate.ChainableEventDataQueryFilter) ASTReference(org.apache.commons.jexl2.parser.ASTReference) ASTNotNode(org.apache.commons.jexl2.parser.ASTNotNode) ASTERNode(org.apache.commons.jexl2.parser.ASTERNode) ASTNRNode(org.apache.commons.jexl2.parser.ASTNRNode) QueryPropertyMarker(datawave.query.jexl.nodes.QueryPropertyMarker) IOException(java.io.IOException) AndIteratorBuilder(datawave.query.iterator.builder.AndIteratorBuilder) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) QueryException(datawave.webservice.query.exception.QueryException) ASTFunctionNode(org.apache.commons.jexl2.parser.ASTFunctionNode) AbstractIteratorBuilder(datawave.query.iterator.builder.AbstractIteratorBuilder) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) ExceededValueThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededValueThresholdMarkerJexlNode) ExceededOrThresholdMarkerJexlNode(datawave.query.jexl.nodes.ExceededOrThresholdMarkerJexlNode) JexlNode(org.apache.commons.jexl2.parser.JexlNode) IndexRangeIteratorBuilder(datawave.query.iterator.builder.IndexRangeIteratorBuilder) LiteralRange(datawave.query.jexl.LiteralRange) Key(org.apache.accumulo.core.data.Key) PartialKey(org.apache.accumulo.core.data.PartialKey) ASTAndNode(org.apache.commons.jexl2.parser.ASTAndNode)

Aggregations

QueryException (datawave.webservice.query.exception.QueryException)131 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)63 IOException (java.io.IOException)62 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)57 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)51 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)47 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)45 Produces (javax.ws.rs.Produces)44 NoResultsException (datawave.webservice.common.exception.NoResultsException)40 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)39 DatawaveFatalQueryException (datawave.query.exceptions.DatawaveFatalQueryException)36 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)36 Interceptors (javax.interceptor.Interceptors)36 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)34 GZIP (org.jboss.resteasy.annotations.GZIP)34 Principal (java.security.Principal)32 WebApplicationException (javax.ws.rs.WebApplicationException)31 BadRequestException (datawave.webservice.common.exception.BadRequestException)29 Path (javax.ws.rs.Path)28 Timed (com.codahale.metrics.annotation.Timed)26