Search in sources :

Example 11 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class QueryExecutorBean method planQuery.

/**
 * @param queryLogicName
 * @param queryParameters
 * @return
 */
@POST
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff" })
@Path("/{logicName}/plan")
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
@Timed(name = "dw.query.planQuery", absolute = true)
public GenericResponse<String> planQuery(@Required("logicName") @PathParam("logicName") String queryLogicName, MultivaluedMap<String, String> queryParameters) {
    QueryData qd = validateQuery(queryLogicName, queryParameters, null);
    GenericResponse<String> response = new GenericResponse<>();
    Query q = null;
    Connector connection = null;
    AccumuloConnectionFactory.Priority priority;
    try {
        // Default hasResults to true.
        response.setHasResults(true);
        // by default we will expand the fields but not the values.
        boolean expandFields = true;
        boolean expandValues = false;
        if (queryParameters.containsKey(EXPAND_FIELDS)) {
            expandFields = Boolean.valueOf(queryParameters.getFirst(EXPAND_FIELDS));
        }
        if (queryParameters.containsKey(EXPAND_VALUES)) {
            expandValues = Boolean.valueOf(queryParameters.getFirst(EXPAND_VALUES));
        }
        AuditType auditType = qd.logic.getAuditType(null);
        try {
            MultivaluedMap<String, String> optionalQueryParameters = new MultivaluedMapImpl<>();
            optionalQueryParameters.putAll(qp.getUnknownParameters(queryParameters));
            q = persister.create(qd.userDn, qd.dnList, marking, queryLogicName, qp, optionalQueryParameters);
            auditType = qd.logic.getAuditType(q);
        } finally {
            queryParameters.add(PrivateAuditConstants.AUDIT_TYPE, auditType.name());
            // on audit if needed, and we are using the index to expand the values
            if (expandValues && !auditType.equals(AuditType.NONE)) {
                // audit the query before its executed.
                try {
                    try {
                        List<String> selectors = qd.logic.getSelectors(q);
                        if (selectors != null && !selectors.isEmpty()) {
                            queryParameters.put(PrivateAuditConstants.SELECTORS, selectors);
                        }
                    } catch (Exception e) {
                        log.error("Error accessing query selector", e);
                    }
                    // if the user didn't set an audit id, use the query id
                    if (!queryParameters.containsKey(AuditParameters.AUDIT_ID)) {
                        queryParameters.putSingle(AuditParameters.AUDIT_ID, q.getId().toString());
                    }
                    auditor.audit(queryParameters);
                } catch (IllegalArgumentException e) {
                    log.error("Error validating audit parameters", e);
                    BadRequestQueryException qe = new BadRequestQueryException(DatawaveErrorCode.MISSING_REQUIRED_PARAMETER, e);
                    response.addException(qe);
                    throw new BadRequestException(qe, response);
                } catch (Exception e) {
                    log.error("Error auditing query", e);
                    QueryException qe = new QueryException(DatawaveErrorCode.QUERY_AUDITING_ERROR, e);
                    response.addException(qe);
                    throw qe;
                }
            }
        }
        priority = qd.logic.getConnectionPriority();
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        addQueryToTrackingMap(trackingMap, q);
        accumuloConnectionRequestBean.requestBegin(q.getId().toString());
        try {
            connection = connectionFactory.getConnection(qd.logic.getConnPoolName(), priority, trackingMap);
        } finally {
            accumuloConnectionRequestBean.requestEnd(q.getId().toString());
        }
        Set<Authorizations> calculatedAuths = AuthorizationsUtil.getDowngradedAuthorizations(qp.getAuths(), qd.p);
        String plan = qd.logic.getPlan(connection, q, calculatedAuths, expandFields, expandValues);
        response.setResult(plan);
        return response;
    } catch (Throwable t) {
        response.setHasResults(false);
        /*
             * Allow web services to throw their own WebApplicationExceptions
             */
        if (t instanceof Error && !(t instanceof TokenMgrError)) {
            log.error(t.getMessage(), t);
            throw (Error) t;
        } else if (t instanceof WebApplicationException) {
            log.error(t.getMessage(), t);
            throw ((WebApplicationException) t);
        } else {
            log.error(t.getMessage(), t);
            QueryException qe = new QueryException(DatawaveErrorCode.QUERY_PLAN_ERROR, t);
            response.addException(qe.getBottomQueryException());
            int statusCode = qe.getBottomQueryException().getStatusCode();
            throw new DatawaveWebApplicationException(qe, response, statusCode);
        }
    } finally {
        if (connection != null) {
            try {
                connectionFactory.returnConnection(connection);
            } catch (Exception e) {
                log.error("Failed to close connection for " + q.getId(), e);
            }
        }
        // close the logic on exception
        try {
            if (null != qd.logic) {
                qd.logic.close();
            }
        } catch (Exception e) {
            log.error("Exception occured while closing query logic; may be innocuous if scanners were running.", e);
        }
        if (null != connection) {
            try {
                connectionFactory.returnConnection(connection);
            } catch (Exception e) {
                log.error("Error returning connection on failed create", e);
            }
        }
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Query(datawave.webservice.query.Query) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) WebApplicationException(javax.ws.rs.WebApplicationException) AccumuloConnectionFactory(datawave.webservice.common.connection.AccumuloConnectionFactory) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) Authorizations(org.apache.accumulo.core.security.Authorizations) GenericResponse(datawave.webservice.result.GenericResponse) AuditType(datawave.webservice.common.audit.Auditor.AuditType) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) TokenMgrError(org.apache.commons.jexl2.parser.TokenMgrError) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) TokenMgrError(org.apache.commons.jexl2.parser.TokenMgrError) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) CancellationException(java.util.concurrent.CancellationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) WebApplicationException(javax.ws.rs.WebApplicationException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) IOException(java.io.IOException) QueryException(datawave.webservice.query.exception.QueryException) BadRequestException(datawave.webservice.common.exception.BadRequestException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) JAXBException(javax.xml.bind.JAXBException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) NoResultsException(datawave.webservice.common.exception.NoResultsException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RollbackException(javax.transaction.RollbackException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) BadRequestException(datawave.webservice.common.exception.BadRequestException) Path(javax.ws.rs.Path) Interceptors(javax.interceptor.Interceptors) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Example 12 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class QueryExecutorBean method defineQuery.

/**
 * @param queryLogicName
 * @param queryParameters
 * @return
 */
@POST
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff" })
@Path("/{logicName}/define")
@GZIP
@GenerateQuerySessionId(cookieBasePath = "/DataWave/Query/")
@EnrichQueryMetrics(methodType = MethodType.CREATE)
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
@Timed(name = "dw.query.defineQuery", absolute = true)
public GenericResponse<String> defineQuery(@Required("logicName") @PathParam("logicName") String queryLogicName, MultivaluedMap<String, String> queryParameters, @Context HttpHeaders httpHeaders) {
    CreateQuerySessionIDFilter.QUERY_ID.set(null);
    QueryData qd = validateQuery(queryLogicName, queryParameters, httpHeaders);
    GenericResponse<String> response = new GenericResponse<>();
    // We need to put a disconnected RunningQuery instance into the cache. Otherwise TRANSIENT queries
    // will not exist when reset is called.
    Span defineSpan = null;
    RunningQuery rq;
    try {
        MultivaluedMap<String, String> optionalQueryParameters = new MultivaluedMapImpl<>();
        optionalQueryParameters.putAll(qp.getUnknownParameters(queryParameters));
        Query q = persister.create(qd.userDn, qd.dnList, marking, queryLogicName, qp, optionalQueryParameters);
        response.setResult(q.getId().toString());
        // If we're supposed to trace this query, then turn tracing on and set information about the query
        // onto the span so that it is saved in the trace table.
        TInfo traceInfo = null;
        boolean shouldTraceQuery = shouldTraceQuery(qp.getQuery(), qd.userid, false);
        if (shouldTraceQuery) {
            Span span = Trace.on("query:" + q.getId());
            log.debug("Tracing query " + q.getId() + " [" + qp.getQuery() + "] on trace ID " + Long.toHexString(span.traceId()));
            for (Entry<String, List<String>> param : queryParameters.entrySet()) {
                span.data(param.getKey(), param.getValue().get(0));
            }
            traceInfo = Tracer.traceInfo();
            defineSpan = Trace.start("query:define");
        }
        AccumuloConnectionFactory.Priority priority = qd.logic.getConnectionPriority();
        rq = new RunningQuery(metrics, null, priority, qd.logic, q, qp.getAuths(), qd.p, new RunningQueryTimingImpl(queryExpirationConf, qp.getPageTimeout()), this.executor, this.predictor, this.metricFactory);
        rq.setActiveCall(true);
        rq.getMetric().setProxyServers(qd.proxyServers);
        rq.setTraceInfo(traceInfo);
        queryCache.put(q.getId().toString(), rq);
        rq.setActiveCall(false);
        CreateQuerySessionIDFilter.QUERY_ID.set(q.getId().toString());
        return response;
    } catch (DatawaveWebApplicationException e) {
        throw e;
    } catch (Exception e) {
        log.error("Error accessing optional query parameters", e);
        QueryException qe = new QueryException(DatawaveErrorCode.RUNNING_QUERY_CACHE_ERROR, e);
        response.addException(qe.getBottomQueryException());
        int statusCode = qe.getBottomQueryException().getStatusCode();
        throw new DatawaveWebApplicationException(qe, response, statusCode);
    } finally {
        if (null != defineSpan) {
            // couple milliseconds just to ensure we get something saved.
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
            // ignore
            }
            defineSpan.stop();
        // TODO: not sure this makes any sense anymore in Accumulo 1.8.1
        // if (null != defineSpan.parent()) {
        // // Stop the main query span since we're done working with it on this thread.
        // // We'll continue it later.
        // defineSpan.parent().stop();
        // }
        }
    }
}
Also used : TInfo(org.apache.accumulo.core.trace.thrift.TInfo) Query(datawave.webservice.query.Query) GenericResponse(datawave.webservice.result.GenericResponse) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) Span(org.apache.accumulo.core.trace.Span) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) CancellationException(java.util.concurrent.CancellationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) WebApplicationException(javax.ws.rs.WebApplicationException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) IOException(java.io.IOException) QueryException(datawave.webservice.query.exception.QueryException) BadRequestException(datawave.webservice.common.exception.BadRequestException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) JAXBException(javax.xml.bind.JAXBException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) NoResultsException(datawave.webservice.common.exception.NoResultsException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RollbackException(javax.transaction.RollbackException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) AccumuloConnectionFactory(datawave.webservice.common.connection.AccumuloConnectionFactory) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) ArrayList(java.util.ArrayList) List(java.util.List) RunningQueryTimingImpl(datawave.webservice.query.cache.RunningQueryTimingImpl) Path(javax.ws.rs.Path) GenerateQuerySessionId(datawave.annotation.GenerateQuerySessionId) Interceptors(javax.interceptor.Interceptors) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GZIP(org.jboss.resteasy.annotations.GZIP) EnrichQueryMetrics(datawave.webservice.query.annotation.EnrichQueryMetrics)

Example 13 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class QueryExecutorBean method execute.

/**
 * @param logicName
 * @param queryParameters
 *
 * @return {@code datawave.webservice.result.GenericResponse<String>}
 * @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
 * @ResponseHeader query-session-id this header and value will be in the Set-Cookie header, subsequent calls for this session will need to supply the
 *                 query-session-id header 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 204 success and no results
 * @HTTP 400 invalid or missing parameter
 * @HTTP 500 internal server error
 */
@POST
@Produces("*/*")
@Path("/{logicName}/execute")
@GZIP
@Interceptors({ ResponseInterceptor.class, RequiredInterceptor.class })
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@Override
@Timed(name = "dw.query.executeQuery", absolute = true)
public StreamingOutput execute(@PathParam("logicName") String logicName, MultivaluedMap<String, String> queryParameters, @Context HttpHeaders httpHeaders) {
    /**
     * This method captures the metrics on the query instead of doing it in the QueryMetricsEnrichmentInterceptor. The ExecuteStreamingOutputResponse class
     * is returned from this method and executed in the JAX-RS layer. It updates the metrics which are then updated on each call to the _next method.
     */
    Collection<String> proxyServers = null;
    Principal p = ctx.getCallerPrincipal();
    DatawavePrincipal dp;
    if (p instanceof DatawavePrincipal) {
        dp = (DatawavePrincipal) p;
        proxyServers = dp.getProxyServers();
    }
    final MediaType PB_MEDIA_TYPE = new MediaType("application", "x-protobuf");
    final MediaType YAML_MEDIA_TYPE = new MediaType("application", "x-yaml");
    final VoidResponse response = new VoidResponse();
    // HttpHeaders.getAcceptableMediaTypes returns a priority sorted list of acceptable response types.
    // Find the first one in the list that we support.
    MediaType responseType = null;
    for (MediaType type : httpHeaders.getAcceptableMediaTypes()) {
        if (type.equals(MediaType.APPLICATION_XML_TYPE) || type.equals(MediaType.APPLICATION_JSON_TYPE) || type.equals(PB_MEDIA_TYPE) || type.equals(YAML_MEDIA_TYPE)) {
            responseType = type;
            break;
        }
    }
    if (null == responseType) {
        QueryException qe = new QueryException(DatawaveErrorCode.UNSUPPORTED_MEDIA_TYPE);
        response.setHasResults(false);
        response.addException(qe);
        throw new DatawaveWebApplicationException(qe, response, MediaType.APPLICATION_XML_TYPE);
    }
    // reference query necessary to avoid NPEs in getting the Transformer and BaseResponse
    Query q = new QueryImpl();
    Date now = new Date();
    q.setBeginDate(now);
    q.setEndDate(now);
    q.setExpirationDate(now);
    q.setQuery("test");
    q.setQueryAuthorizations("ALL");
    ResultsPage emptyList = new ResultsPage();
    // Find the response class
    Class<?> responseClass;
    try {
        QueryLogic<?> l = queryLogicFactory.getQueryLogic(logicName, p);
        QueryLogicTransformer t = l.getTransformer(q);
        BaseResponse refResponse = t.createResponse(emptyList);
        responseClass = refResponse.getClass();
    } catch (Exception e) {
        QueryException qe = new QueryException(DatawaveErrorCode.QUERY_TRANSFORM_ERROR, e);
        log.error(qe, e);
        response.setHasResults(false);
        response.addException(qe.getBottomQueryException());
        int statusCode = qe.getBottomQueryException().getStatusCode();
        throw new DatawaveWebApplicationException(qe, response, statusCode, MediaType.APPLICATION_XML_TYPE);
    }
    SerializationType s;
    if (responseType.equals(MediaType.APPLICATION_XML_TYPE)) {
        s = SerializationType.XML;
    } else if (responseType.equals(MediaType.APPLICATION_JSON_TYPE)) {
        s = SerializationType.JSON;
    } else if (responseType.equals(PB_MEDIA_TYPE)) {
        if (!(Message.class.isAssignableFrom(responseClass))) {
            QueryException qe = new QueryException(DatawaveErrorCode.BAD_RESPONSE_CLASS, MessageFormat.format("Response  class: {0}", responseClass));
            response.setHasResults(false);
            response.addException(qe);
            throw new DatawaveWebApplicationException(qe, response, MediaType.APPLICATION_XML_TYPE);
        }
        s = SerializationType.PB;
    } else if (responseType.equals(YAML_MEDIA_TYPE)) {
        if (!(Message.class.isAssignableFrom(responseClass))) {
            QueryException qe = new QueryException(DatawaveErrorCode.BAD_RESPONSE_CLASS, MessageFormat.format("Response  class: {0}", responseClass));
            response.setHasResults(false);
            response.addException(qe);
            throw new DatawaveWebApplicationException(qe, response, MediaType.APPLICATION_XML_TYPE);
        }
        s = SerializationType.YAML;
    } else {
        QueryException qe = new QueryException(DatawaveErrorCode.INVALID_FORMAT, MessageFormat.format("format: {0}", responseType.toString()));
        response.setHasResults(false);
        response.addException(qe);
        throw new DatawaveWebApplicationException(qe, response, MediaType.APPLICATION_XML_TYPE);
    }
    long start = System.nanoTime();
    GenericResponse<String> createResponse = null;
    try {
        createResponse = this.createQuery(logicName, queryParameters, httpHeaders);
    } catch (Throwable t) {
        if (t instanceof DatawaveWebApplicationException) {
            QueryException qe = (QueryException) ((DatawaveWebApplicationException) t).getCause();
            response.setHasResults(false);
            response.addException(qe.getBottomQueryException());
            int statusCode = qe.getBottomQueryException().getStatusCode();
            throw new DatawaveWebApplicationException(qe, response, statusCode, MediaType.APPLICATION_XML_TYPE);
        } else {
            throw t;
        }
    }
    long createCallTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
    final String queryId = createResponse.getResult();
    // We created the query and put into cache, get the RunningQuery object
    final RunningQuery rq = queryCache.get(queryId);
    rq.getMetric().setCreateCallTime(createCallTime);
    final Collection<String> proxies = proxyServers;
    final SerializationType serializationType = s;
    final Class<?> queryResponseClass = responseClass;
    return new ExecuteStreamingOutputResponse(queryId, queryResponseClass, response, rq, serializationType, proxies);
}
Also used : Query(datawave.webservice.query.Query) Message(io.protostuff.Message) ResultsPage(datawave.webservice.query.cache.ResultsPage) QueryLogicTransformer(datawave.webservice.query.logic.QueryLogicTransformer) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Date(java.util.Date) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) CancellationException(java.util.concurrent.CancellationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) WebApplicationException(javax.ws.rs.WebApplicationException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) IOException(java.io.IOException) QueryException(datawave.webservice.query.exception.QueryException) BadRequestException(datawave.webservice.common.exception.BadRequestException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) JAXBException(javax.xml.bind.JAXBException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) NoResultsException(datawave.webservice.common.exception.NoResultsException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RollbackException(javax.transaction.RollbackException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) BaseResponse(datawave.webservice.result.BaseResponse) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) QueryImpl(datawave.webservice.query.QueryImpl) VoidResponse(datawave.webservice.result.VoidResponse) MediaType(javax.ws.rs.core.MediaType) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Path(javax.ws.rs.Path) Interceptors(javax.interceptor.Interceptors) TransactionAttribute(javax.ejb.TransactionAttribute) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 14 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class QueryExecutorBean method predictions.

/**
 * Pulls back the current predictions for a query.
 *
 * @param id
 *            - (@Required)
 *
 * @return GenericResponse containing predictions
 * @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 204 success and no results
 * @HTTP 404 if id not found
 * @HTTP 412 if the query is no longer alive, client should call {@link #reset(String)} and try again
 * @HTTP 500 internal server error
 */
@GET
@Path("/{id}/predictions")
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff" })
@GZIP
@Interceptors({ ResponseInterceptor.class, RequiredInterceptor.class })
@Override
@Timed(name = "dw.query.predictions", absolute = true)
public GenericResponse<String> predictions(@Required("id") @PathParam("id") String id) {
    // in case we don't make it to creating the response from the QueryLogic
    GenericResponse<String> response = new GenericResponse<>();
    Principal p = ctx.getCallerPrincipal();
    String userid = p.getName();
    if (p instanceof DatawavePrincipal) {
        DatawavePrincipal dp = (DatawavePrincipal) p;
        userid = dp.getShortName();
    }
    try {
        // Not calling getQueryById() here. We don't want to pull the persisted definition.
        RunningQuery query = queryCache.get(id);
        // an error.
        if (null == query || null == query.getConnection()) {
            // status code.
            if (null == query) {
                List<Query> queries = persister.findById(id);
                if (queries == null || queries.size() != 1) {
                    throw new NotFoundQueryException(DatawaveErrorCode.NO_QUERY_OBJECT_MATCH, MessageFormat.format("{0}", id));
                }
            }
            throw new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_TIMEOUT_OR_SERVER_ERROR, MessageFormat.format("id = {0}", id));
        } else {
            // Validate the query belongs to the caller
            if (!query.getSettings().getOwner().equals(userid)) {
                throw new UnauthorizedQueryException(DatawaveErrorCode.QUERY_OWNER_MISMATCH, MessageFormat.format("{0} != {1}", userid, query.getSettings().getOwner()));
            }
            // pull the predictions out of the query metric
            Set<Prediction> predictions = query.getMetric().getPredictions();
            if (predictions != null && !predictions.isEmpty()) {
                response.setResult(predictions.toString());
                response.setHasResults(true);
            }
        }
    } catch (Exception e) {
        log.error("Failed to get query predictions", e);
        QueryException qe = new QueryException(DatawaveErrorCode.QUERY_PREDICTIONS_ERROR, e, MessageFormat.format("query id: {0}", id));
        log.error(qe, e);
        response.addException(qe.getBottomQueryException());
        int statusCode = qe.getBottomQueryException().getStatusCode();
        throw new DatawaveWebApplicationException(qe, response, statusCode);
    }
    return response;
}
Also used : Query(datawave.webservice.query.Query) GenericResponse(datawave.webservice.result.GenericResponse) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) Prediction(datawave.microservice.querymetric.BaseQueryMetric.Prediction) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) CancellationException(java.util.concurrent.CancellationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) WebApplicationException(javax.ws.rs.WebApplicationException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) IOException(java.io.IOException) QueryException(datawave.webservice.query.exception.QueryException) BadRequestException(datawave.webservice.common.exception.BadRequestException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) JAXBException(javax.xml.bind.JAXBException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) NoResultsException(datawave.webservice.common.exception.NoResultsException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RollbackException(javax.transaction.RollbackException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Path(javax.ws.rs.Path) 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 15 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class QueryExecutorBean method createQuery.

/**
 * @param queryLogicName
 * @param queryParameters
 * @return
 */
@POST
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff" })
@Path("/{logicName}/create")
@GZIP
@GenerateQuerySessionId(cookieBasePath = "/DataWave/Query/")
@EnrichQueryMetrics(methodType = MethodType.CREATE)
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
@Timed(name = "dw.query.createQuery", absolute = true)
public GenericResponse<String> createQuery(@Required("logicName") @PathParam("logicName") String queryLogicName, MultivaluedMap<String, String> queryParameters, @Context HttpHeaders httpHeaders) {
    CreateQuerySessionIDFilter.QUERY_ID.set(null);
    QueryData qd = validateQuery(queryLogicName, queryParameters, httpHeaders);
    GenericResponse<String> response = new GenericResponse<>();
    Query q = null;
    Connector connection = null;
    AccumuloConnectionFactory.Priority priority;
    Span createSpan = null;
    RunningQuery rq = null;
    try {
        // Default hasResults to true. If a query logic is actually able to set this value,
        // then their value will overwrite this one. Otherwise, we return true so that
        // callers know they have to call next (even though next may not return results).
        response.setHasResults(true);
        AuditType auditType = qd.logic.getAuditType(null);
        try {
            MultivaluedMap<String, String> optionalQueryParameters = new MultivaluedMapImpl<>();
            optionalQueryParameters.putAll(qp.getUnknownParameters(queryParameters));
            q = persister.create(qd.userDn, qd.dnList, marking, queryLogicName, qp, optionalQueryParameters);
            auditType = qd.logic.getAuditType(q);
        } finally {
            queryParameters.add(PrivateAuditConstants.AUDIT_TYPE, auditType.name());
            if (!auditType.equals(AuditType.NONE)) {
                // audit the query before its executed.
                try {
                    try {
                        List<String> selectors = qd.logic.getSelectors(q);
                        if (selectors != null && !selectors.isEmpty()) {
                            queryParameters.put(PrivateAuditConstants.SELECTORS, selectors);
                        }
                    } catch (Exception e) {
                        log.error("Error accessing query selector", e);
                    }
                    // if the user didn't set an audit id, use the query id
                    if (!queryParameters.containsKey(AuditParameters.AUDIT_ID)) {
                        queryParameters.putSingle(AuditParameters.AUDIT_ID, q.getId().toString());
                    }
                    auditor.audit(queryParameters);
                } catch (IllegalArgumentException e) {
                    log.error("Error validating audit parameters", e);
                    BadRequestQueryException qe = new BadRequestQueryException(DatawaveErrorCode.MISSING_REQUIRED_PARAMETER, e);
                    response.addException(qe);
                    throw new BadRequestException(qe, response);
                } catch (Exception e) {
                    log.error("Error auditing query", e);
                    QueryException qe = new QueryException(DatawaveErrorCode.QUERY_AUDITING_ERROR, e);
                    response.addException(qe);
                    throw qe;
                }
            }
        }
        priority = qd.logic.getConnectionPriority();
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        addQueryToTrackingMap(trackingMap, q);
        accumuloConnectionRequestBean.requestBegin(q.getId().toString());
        try {
            connection = connectionFactory.getConnection(qd.logic.getConnPoolName(), priority, trackingMap);
        } finally {
            accumuloConnectionRequestBean.requestEnd(q.getId().toString());
        }
        // If we're supposed to trace this query, then turn tracing on and set information about the query
        // onto the span so that it is saved in the trace table.
        TInfo traceInfo = null;
        boolean shouldTraceQuery = shouldTraceQuery(qp.getQuery(), qd.userid, qp.isTrace());
        if (shouldTraceQuery) {
            Span span = Trace.on("query:" + q.getId());
            log.debug("Tracing query " + q.getId() + " [" + qp.getQuery() + "] on trace ID " + Long.toHexString(span.traceId()));
            for (Entry<String, List<String>> param : queryParameters.entrySet()) {
                span.data(param.getKey(), param.getValue().get(0));
            }
            traceInfo = Tracer.traceInfo();
            createSpan = Trace.start("query:create");
        }
        // hold on to a reference of the query logic so we cancel it if need be.
        qlCache.add(q.getId().toString(), qd.userid, qd.logic, connection);
        rq = new RunningQuery(metrics, null, priority, qd.logic, q, qp.getAuths(), qd.p, new RunningQueryTimingImpl(queryExpirationConf, qp.getPageTimeout()), this.executor, this.predictor, this.metricFactory);
        rq.setActiveCall(true);
        rq.setTraceInfo(traceInfo);
        rq.getMetric().setProxyServers(qd.proxyServers);
        rq.setConnection(connection);
        // Put in the cache by id. Don't put the cache in by name because multiple users may use the same name
        // and only the last one will be in the cache.
        queryCache.put(q.getId().toString(), rq);
        response.setResult(q.getId().toString());
        rq.setActiveCall(false);
        CreateQuerySessionIDFilter.QUERY_ID.set(q.getId().toString());
        return response;
    } catch (Throwable t) {
        response.setHasResults(false);
        if (rq != null) {
            rq.getMetric().setError(t);
        }
        // close the logic on exception
        try {
            if (null != qd.logic) {
                qd.logic.close();
            }
        } catch (Exception e) {
            log.error("Exception occured while closing query logic; may be innocuous if scanners were running.", e);
        }
        if (null != connection) {
            try {
                connectionFactory.returnConnection(connection);
            } catch (Exception e) {
                log.error("Error returning connection on failed create", e);
            }
        }
        try {
            if (null != q)
                persister.remove(q);
        } catch (Exception e) {
            response.addException(new QueryException(DatawaveErrorCode.DEPERSIST_ERROR, e).getBottomQueryException());
        }
        /*
             * Allow web services to throw their own WebApplicationExceptions
             */
        if (t instanceof Error && !(t instanceof TokenMgrError)) {
            log.error(t.getMessage(), t);
            throw (Error) t;
        } else if (t instanceof WebApplicationException) {
            log.error(t.getMessage(), t);
            throw ((WebApplicationException) t);
        } else if (t instanceof InterruptedException) {
            if (rq != null) {
                rq.getMetric().setLifecycle(QueryMetric.Lifecycle.CANCELLED);
            }
            log.info("Query " + q.getId() + " canceled on request");
            QueryException qe = new QueryException(DatawaveErrorCode.QUERY_CANCELED, t);
            response.addException(qe.getBottomQueryException());
            int statusCode = qe.getBottomQueryException().getStatusCode();
            throw new DatawaveWebApplicationException(qe, response, statusCode);
        } else {
            log.error(t.getMessage(), t);
            QueryException qe = new QueryException(DatawaveErrorCode.RUNNING_QUERY_CACHE_ERROR, t);
            response.addException(qe.getBottomQueryException());
            int statusCode = qe.getBottomQueryException().getStatusCode();
            throw new DatawaveWebApplicationException(qe, response, statusCode);
        }
    } finally {
        if (createSpan != null) {
            createSpan.stop();
        // TODO: not sure this makes any sense anymore in Accumulo 1.8.1
        // Stop the main query span since we're done working with it on this thread.
        // We'll continue it later.
        // createSpan.parent().stop();
        }
        if (null != q) {
            // - Remove the logic from the cache
            qlCache.poll(q.getId().toString());
        }
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Query(datawave.webservice.query.Query) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) WebApplicationException(javax.ws.rs.WebApplicationException) Span(org.apache.accumulo.core.trace.Span) AccumuloConnectionFactory(datawave.webservice.common.connection.AccumuloConnectionFactory) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) ArrayList(java.util.ArrayList) List(java.util.List) TInfo(org.apache.accumulo.core.trace.thrift.TInfo) GenericResponse(datawave.webservice.result.GenericResponse) AuditType(datawave.webservice.common.audit.Auditor.AuditType) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) TokenMgrError(org.apache.commons.jexl2.parser.TokenMgrError) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) TokenMgrError(org.apache.commons.jexl2.parser.TokenMgrError) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) CancellationException(java.util.concurrent.CancellationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) WebApplicationException(javax.ws.rs.WebApplicationException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) IOException(java.io.IOException) QueryException(datawave.webservice.query.exception.QueryException) BadRequestException(datawave.webservice.common.exception.BadRequestException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) JAXBException(javax.xml.bind.JAXBException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) NoResultsException(datawave.webservice.common.exception.NoResultsException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RollbackException(javax.transaction.RollbackException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) BadRequestException(datawave.webservice.common.exception.BadRequestException) RunningQueryTimingImpl(datawave.webservice.query.cache.RunningQueryTimingImpl) Path(javax.ws.rs.Path) GenerateQuerySessionId(datawave.annotation.GenerateQuerySessionId) Interceptors(javax.interceptor.Interceptors) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GZIP(org.jboss.resteasy.annotations.GZIP) EnrichQueryMetrics(datawave.webservice.query.annotation.EnrichQueryMetrics)

Aggregations

DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)72 QueryException (datawave.webservice.query.exception.QueryException)64 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)48 IOException (java.io.IOException)43 Produces (javax.ws.rs.Produces)43 NoResultsException (datawave.webservice.common.exception.NoResultsException)39 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)37 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)37 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)36 GZIP (org.jboss.resteasy.annotations.GZIP)34 Interceptors (javax.interceptor.Interceptors)33 BadRequestException (datawave.webservice.common.exception.BadRequestException)32 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)32 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)31 Path (javax.ws.rs.Path)27 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)26 Principal (java.security.Principal)26 WebApplicationException (javax.ws.rs.WebApplicationException)25 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)24 HeuristicMixedException (javax.transaction.HeuristicMixedException)24