Search in sources :

Example 6 with GenerateQuerySessionId

use of datawave.annotation.GenerateQuerySessionId in project datawave by NationalSecurityAgency.

the class QueryExecutorBean method reset.

/**
 * Resets the query named by {@code id}. If the query is not alive, meaning that the current session has expired (due to either timeout, or server failure),
 * then this will reload the query and start it over. If the query is alive, it closes it and starts the query over.
 *
 * @param id
 *            the ID of the query to reload/reset
 * @return an empty response
 *
 * @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
 * @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
 *
 * @HTTP 200 success
 * @HTTP 400 invalid or missing parameter
 * @HTTP 500 internal server error
 */
@PUT
@POST
@Path("/{id}/reset")
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff" })
@GZIP
@GenerateQuerySessionId(cookieBasePath = "/DataWave/Query/")
@Interceptors({ ResponseInterceptor.class, RequiredInterceptor.class })
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@Override
@Timed(name = "dw.query.reset", absolute = true)
public VoidResponse reset(@Required("id") @PathParam("id") String id) {
    CreateQuerySessionIDFilter.QUERY_ID.set(null);
    VoidResponse response = new VoidResponse();
    AccumuloConnectionFactory.Priority priority;
    Connector connection = null;
    RunningQuery query = null;
    Span span = null;
    try {
        ctx.getUserTransaction().begin();
        query = getQueryById(id);
        // If we're tracing this query, then continue the trace for the reset call.
        TInfo traceInfo = query.getTraceInfo();
        if (traceInfo != null) {
            span = Trace.trace(traceInfo, "query:reset");
        }
        // The lock should be released at the end of the method call.
        if (!queryCache.lock(id))
            throw new QueryException(DatawaveErrorCode.QUERY_LOCKED_ERROR);
        // restarting the query, so we should re-audit ().
        if (query.getConnection() != null) {
            query.closeConnection(connectionFactory);
        } else {
            AuditType auditType = query.getLogic().getAuditType(query.getSettings());
            MultivaluedMap<String, String> queryParameters = new MultivaluedMapImpl<>();
            queryParameters.putAll(query.getSettings().toMap());
            queryParameters.putSingle(PrivateAuditConstants.AUDIT_TYPE, auditType.name());
            queryParameters.putSingle(PrivateAuditConstants.LOGIC_CLASS, query.getLogic().getLogicName());
            queryParameters.putSingle(PrivateAuditConstants.USER_DN, query.getSettings().getUserDN());
            queryParameters.putSingle(PrivateAuditConstants.COLUMN_VISIBILITY, query.getSettings().getColumnVisibility());
            if (!auditType.equals(AuditType.NONE)) {
                try {
                    try {
                        List<String> selectors = query.getLogic().getSelectors(query.getSettings());
                        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, id);
                    }
                    auditor.audit(queryParameters);
                } catch (IllegalArgumentException 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;
                }
            }
        }
        // Allocate the connection for this query so we are ready to go when
        // they call next.
        priority = query.getConnectionPriority();
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        addQueryToTrackingMap(trackingMap, query.getSettings());
        accumuloConnectionRequestBean.requestBegin(id);
        try {
            connection = connectionFactory.getConnection(query.getLogic().getConnPoolName(), priority, trackingMap);
        } finally {
            accumuloConnectionRequestBean.requestEnd(id);
        }
        query.setConnection(connection);
        response.addMessage(id + " reset.");
        CreateQuerySessionIDFilter.QUERY_ID.set(id);
        return response;
    } catch (InterruptedException e) {
        if (query != null) {
            query.getMetric().setLifecycle(QueryMetric.Lifecycle.CANCELLED);
        }
        log.info("Query " + id + " canceled on request");
        QueryException qe = new QueryException(DatawaveErrorCode.QUERY_CANCELED, e);
        response.addException(qe.getBottomQueryException());
        int statusCode = qe.getBottomQueryException().getStatusCode();
        throw new DatawaveWebApplicationException(qe, response, statusCode);
    } catch (Exception e) {
        log.error("Exception caught on resetting query", e);
        try {
            if (null != connection) {
                /*
                     * if the query exists, we need to make sure the connection isn't set on it because the "proper" work flow is to close and/or cancel the
                     * query after a failure. we don't want to purge it from the query cache, so setting the connector to null avoids having the connector
                     * returned multiple times to the connector pool.
                     */
                if (query != null) {
                    query.setConnection(null);
                }
                connectionFactory.returnConnection(connection);
            }
        } catch (Exception e2) {
            log.error("Error returning connection on failed reset", e2);
        }
        QueryException qe = new QueryException(DatawaveErrorCode.QUERY_RESET_ERROR, e);
        response.addException(qe.getBottomQueryException());
        int statusCode = qe.getBottomQueryException().getStatusCode();
        throw new DatawaveWebApplicationException(qe, response, statusCode);
    } finally {
        queryCache.unlock(id);
        try {
            if (ctx.getUserTransaction().getStatus() != Status.STATUS_NO_TRANSACTION) {
                // no reason to commit if transaction not started, ie Query not found exception
                ctx.getUserTransaction().commit();
            }
        } catch (Exception e) {
            QueryException qe = new QueryException(DatawaveErrorCode.QUERY_TRANSACTION_ERROR, e);
            response.addException(qe.getBottomQueryException());
            throw new DatawaveWebApplicationException(qe, response);
        } finally {
            // Stop timing on this trace, if any
            if (span != null) {
                span.stop();
            }
        }
    }
}
Also used : TInfo(org.apache.accumulo.core.trace.thrift.TInfo) Connector(org.apache.accumulo.core.client.Connector) AuditType(datawave.webservice.common.audit.Auditor.AuditType) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) 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) VoidResponse(datawave.webservice.result.VoidResponse) BadRequestException(datawave.webservice.common.exception.BadRequestException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) Path(javax.ws.rs.Path) GenerateQuerySessionId(datawave.annotation.GenerateQuerySessionId) 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) PUT(javax.ws.rs.PUT)

Example 7 with GenerateQuerySessionId

use of datawave.annotation.GenerateQuerySessionId in project datawave by NationalSecurityAgency.

the class QueryExecutorBean method createQueryAndNextAsync.

@POST
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff" })
@Path("/{logicName}/async/createAndNext")
@GZIP
@GenerateQuerySessionId(cookieBasePath = "/DataWave/Query/")
@EnrichQueryMetrics(methodType = MethodType.CREATE_AND_NEXT)
@Interceptors({ ResponseInterceptor.class, RequiredInterceptor.class })
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@Asynchronous
@Timed(name = "dw.query.createAndNextAsync", absolute = true)
public void createQueryAndNextAsync(@Required("logicName") @PathParam("logicName") String logicName, MultivaluedMap<String, String> queryParameters, @Suspended AsyncResponse asyncResponse) {
    try {
        BaseQueryResponse response = createQueryAndNext(logicName, queryParameters);
        asyncResponse.resume(response);
    } catch (Throwable t) {
        asyncResponse.resume(t);
    }
}
Also used : BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) Path(javax.ws.rs.Path) GenerateQuerySessionId(datawave.annotation.GenerateQuerySessionId) Asynchronous(javax.ejb.Asynchronous) 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) EnrichQueryMetrics(datawave.webservice.query.annotation.EnrichQueryMetrics)

Example 8 with GenerateQuerySessionId

use of datawave.annotation.GenerateQuerySessionId in project datawave by NationalSecurityAgency.

the class BasicQueryBean method showQueryWizardStep3.

/**
 * Display the query plan and link to basic query results for a simple query web UI in the quickstart
 *
 * @HTTP 200 Success
 * @return datawave.webservice.result.QueryWizardStep3Response
 * @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 X-OperationTimeInMS time spent on the server performing the operation, does not account for network or result serialization
 */
@POST
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "text/html" })
@Path("/{logicName}/showQueryWizardStep3")
@GenerateQuerySessionId(cookieBasePath = "/DataWave/BasicQuery/")
@Interceptors({ ResponseInterceptor.class })
@Timed(name = "dw.query.showQueryWizardStep3", absolute = true)
public QueryWizardStep3Response showQueryWizardStep3(@Required("logicName") @PathParam("logicName") String logicName, MultivaluedMap<String, String> queryParameters, @Context HttpHeaders httpHeaders) {
    CreateQuerySessionIDFilter.QUERY_ID.set(null);
    GenericResponse<String> createResponse;
    QueryWizardStep3Response queryWizardStep3Response = new QueryWizardStep3Response();
    try {
        createResponse = queryExecutor.createQuery(logicName, queryParameters, httpHeaders);
    } catch (Exception e) {
        queryWizardStep3Response.setErrorMessage(e.getMessage());
        return queryWizardStep3Response;
    }
    String queryId = createResponse.getResult();
    CreateQuerySessionIDFilter.QUERY_ID.set(queryId);
    queryWizardStep3Response.setQueryId(queryId);
    BaseQueryLogic logic = getQueryLogic(logicName);
    if (logic != null && !(NO_PLAN_REQUIRED.contains(logic.getClass().getName()))) {
        GenericResponse<String> planResponse;
        try {
            planResponse = queryExecutor.plan(queryId);
        } catch (Exception e) {
            queryWizardStep3Response.setErrorMessage(e.getMessage());
            return queryWizardStep3Response;
        }
        queryWizardStep3Response.setQueryPlan(planResponse.getResult());
    } else
        queryWizardStep3Response.setQueryPlan("No plan required for this query");
    return queryWizardStep3Response;
}
Also used : QueryWizardStep3Response(datawave.webservice.result.QueryWizardStep3Response) QueryException(datawave.webservice.query.exception.QueryException) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) 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)

Example 9 with GenerateQuerySessionId

use of datawave.annotation.GenerateQuerySessionId in project datawave by NationalSecurityAgency.

the class CachedResultsBean method update.

/**
 * Update fields, conditions, grouping, or order for a CachedResults query. As a general rule, keep parens at least one space away from field names. Field
 * names also work with or without tick marks.
 *
 * @param queryId
 *            user defined id for this query
 * @param fields
 *            comma separated list of fields in the result set
 * @param conditions
 *            analogous to a SQL where clause
 * @param grouping
 *            comma separated list of fields to group by
 * @param order
 *            comma separated list of fields for ordering
 * @param pagesize
 *            size of returned pages
 *
 * @return datawave.webservice.result.CachedResultsResponse
 * @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 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
 *
 * @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
 */
@POST
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml" })
@javax.ws.rs.Path("/{queryId}/update")
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
@GenerateQuerySessionId(cookieBasePath = "/DataWave/CachedResults/")
@Timed(name = "dw.cachedr.update", absolute = true)
public CachedResultsResponse update(@PathParam("queryId") @Required("queryId") String queryId, @FormParam("fields") String fields, @FormParam("conditions") String conditions, @FormParam("grouping") String grouping, @FormParam("order") String order, @FormParam("pagesize") Integer pagesize) {
    CreateQuerySessionIDFilter.QUERY_ID.set(null);
    boolean updated = false;
    CachedResultsResponse response = new CachedResultsResponse();
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String owner = getOwnerFromPrincipal(p);
    try {
        CachedRunningQuery crq = null;
        try {
            crq = retrieve(queryId, owner);
            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));
            }
            if (pagesize == null || pagesize <= 0) {
                pagesize = cachedResultsConfiguration.getDefaultPageSize();
            }
            int maxPageSize = cachedResultsConfiguration.getMaxPageSize();
            if (maxPageSize > 0 && pagesize > maxPageSize) {
                throw new PreConditionFailedQueryException(DatawaveErrorCode.REQUESTED_PAGE_SIZE_TOO_LARGE, MessageFormat.format("{0} > {1}.", pagesize, cachedResultsConfiguration.getMaxPageSize()));
            }
            synchronized (crq) {
                if (crq.isActivated() == false) {
                    Connection connection = ds.getConnection();
                    String logicName = crq.getQueryLogicName();
                    if (logicName != null) {
                        QueryLogic<?> queryLogic = queryFactory.getQueryLogic(logicName, p);
                        crq.activate(connection, queryLogic);
                    } else {
                        DbUtils.closeQuietly(connection);
                    }
                }
                try {
                    if (fields == null && conditions == null && grouping == null && order == null) {
                    // don't do update
                    } else {
                        updated = crq.update(fields, conditions, grouping, order, pagesize);
                        persist(crq, owner);
                    }
                    response.setOriginalQueryId(crq.getOriginalQueryId());
                    response.setQueryId(crq.getQueryId());
                    response.setViewName(crq.getView());
                    response.setAlias(crq.getAlias());
                    response.setTotalRows(crq.getTotalRows());
                } finally {
                    // only close connection if the crq changed, because we expect additional actions
                    if (updated) {
                        crq.closeConnection(log);
                    }
                }
            }
        } finally {
            if (crq != null && crq.getQueryLogic() != null && crq.getQueryLogic().getCollectQueryMetrics() == true) {
                try {
                    metrics.updateMetric(crq.getMetric());
                } catch (Exception e1) {
                    log.error("Error updating metrics", e1);
                }
            }
        }
        CreateQuerySessionIDFilter.QUERY_ID.set(queryId);
        return response;
    } catch (Exception e) {
        QueryException qe = new QueryException(DatawaveErrorCode.CACHED_QUERY_UPDATE_ERROR, e);
        log.error(qe);
        response.addException(qe.getBottomQueryException());
        int statusCode = qe.getBottomQueryException().getStatusCode();
        throw new DatawaveWebApplicationException(qe, response, statusCode);
    }
}
Also used : PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) Connection(java.sql.Connection) 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) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) CachedResultsResponse(datawave.webservice.result.CachedResultsResponse) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) GenerateQuerySessionId(datawave.annotation.GenerateQuerySessionId) Interceptors(javax.interceptor.Interceptors) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Example 10 with GenerateQuerySessionId

use of datawave.annotation.GenerateQuerySessionId in project datawave by NationalSecurityAgency.

the class CachedResultsBean method create.

/**
 * @param queryParameters
 *
 * @return datawave.webservice.result.CachedResultsResponse
 * @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
 *
 *                 'view' is a required parameter, however the caller may not know the view name. In this case, the caller may substitute the alias name
 *                 they created for the view. the retrieve call may retrieve using the alias, however other calls that operate on the actual view may not
 *                 substitute the alias (it is not the name of the table/view!) see comments inline below
 *
 * @HTTP 200 success
 * @HTTP 500 internal server error
 */
@POST
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml" })
@javax.ws.rs.Path("/{queryId}/create")
@Interceptors(RequiredInterceptor.class)
@GenerateQuerySessionId(cookieBasePath = "/DataWave/CachedResults/")
@Timed(name = "dw.cachedr.create", absolute = true)
public CachedResultsResponse create(@Required("queryId") @PathParam("queryId") String queryId, MultivaluedMap<String, String> queryParameters) {
    CreateQuerySessionIDFilter.QUERY_ID.set(null);
    queryParameters.putSingle(CachedResultsParameters.QUERY_ID, queryId);
    cp.clear();
    cp.validate(queryParameters);
    CachedResultsResponse response = new CachedResultsResponse();
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String owner = getOwnerFromPrincipal(p);
    CachedRunningQuery crq = null;
    Connection con = null;
    try {
        con = ds.getConnection();
        // the caller may have used the alias name for the view.
        CachedRunningQuery loadCrq = retrieve(cp.getView(), owner);
        if (loadCrq == null) {
            throw new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_NOT_CACHED);
        }
        if (!loadCrq.getUser().equals(owner)) {
            throw new UnauthorizedQueryException(DatawaveErrorCode.QUERY_OWNER_MISMATCH, MessageFormat.format("{0} != {1}", loadCrq.getUser(), owner));
        }
        if (cp.getPagesize() <= 0) {
            cp.setPagesize(cachedResultsConfiguration.getDefaultPageSize());
        }
        int maxPageSize = cachedResultsConfiguration.getMaxPageSize();
        if (maxPageSize > 0 && cp.getPagesize() > maxPageSize) {
            throw new PreConditionFailedQueryException(DatawaveErrorCode.REQUESTED_PAGE_SIZE_TOO_LARGE, MessageFormat.format("{0} > {1}.", cp.getPagesize(), maxPageSize));
        }
        QueryLogic<?> queryLogic = loadCrq.getQueryLogic();
        String originalQueryId = loadCrq.getOriginalQueryId();
        Query query = loadCrq.getQuery();
        String table = loadCrq.getTableName();
        Set<String> fixedFields = null;
        if (!StringUtils.isEmpty(cp.getFixedFields())) {
            fixedFields = new HashSet<>();
            for (String field : cp.getFixedFields().split(",")) {
                fixedFields.add(field.trim());
            }
        }
        // this needs the real view name, so use the value from loadCrq instead of cp.getView() (because cp.getView may return the alias instead)
        crq = new CachedRunningQuery(con, query, queryLogic, cp.getQueryId(), cp.getAlias(), owner, loadCrq.getView(), cp.getFields(), cp.getConditions(), cp.getGrouping(), cp.getOrder(), cp.getPagesize(), loadCrq.getVariableFields(), fixedFields, metricFactory);
        crq.setStatus(CachedRunningQuery.Status.CREATING);
        crq.setOriginalQueryId(originalQueryId);
        crq.setTableName(table);
        persist(crq, owner);
        // see above comment about using loadCrq.getView() instead of cp.getView()
        CachedRunningQuery.removeFromDatabase(loadCrq.getView());
        crq.getMetric().setLifecycle(QueryMetric.Lifecycle.DEFINED);
        // see above comment about using loadCrq.getView() instead of cp.getView()
        String sqlQuery = crq.generateSql(loadCrq.getView(), cp.getFields(), cp.getConditions(), cp.getGrouping(), cp.getOrder(), owner, con);
        // Store the CachedRunningQuery in the cache under the user-supplied alias
        if (cp.getAlias() != null) {
            response.setAlias(cp.getAlias());
        }
        AuditType auditType = queryLogic.getAuditType(query);
        if (!auditType.equals(AuditType.NONE)) {
            // if auditType > AuditType.NONE, audit passively
            auditType = AuditType.PASSIVE;
            StringBuilder auditMessage = new StringBuilder();
            auditMessage.append("User running secondary query on cached results of original query,");
            auditMessage.append(" original query: ").append(query.getQuery());
            auditMessage.append(", secondary query: ").append(sqlQuery);
            MultivaluedMap<String, String> params = new MultivaluedMapImpl<>();
            params.putAll(query.toMap());
            marking.validate(params);
            PrivateAuditConstants.stripPrivateParameters(queryParameters);
            params.putSingle(PrivateAuditConstants.COLUMN_VISIBILITY, marking.toColumnVisibilityString());
            params.putSingle(PrivateAuditConstants.AUDIT_TYPE, auditType.name());
            params.putSingle(PrivateAuditConstants.USER_DN, query.getUserDN());
            params.putSingle(PrivateAuditConstants.LOGIC_CLASS, crq.getQueryLogic().getLogicName());
            params.remove(QueryParameters.QUERY_STRING);
            params.putSingle(QueryParameters.QUERY_STRING, auditMessage.toString());
            params.putAll(queryParameters);
            // if the user didn't set an audit id, use the query id
            if (!params.containsKey(AuditParameters.AUDIT_ID)) {
                params.putSingle(AuditParameters.AUDIT_ID, queryId);
            }
            auditor.audit(params);
        }
        response.setOriginalQueryId(originalQueryId);
        response.setQueryId(cp.getQueryId());
        response.setViewName(loadCrq.getView());
        response.setTotalRows(crq.getTotalRows());
        crq.setStatus(CachedRunningQuery.Status.AVAILABLE);
        persist(crq, owner);
        CreateQuerySessionIDFilter.QUERY_ID.set(cp.getQueryId());
        return response;
    } catch (Exception e) {
        if (crq != null) {
            crq.getMetric().setError(e);
        }
        String statusMessage = e.getMessage();
        if (null == statusMessage) {
            statusMessage = e.getClass().getName();
        }
        try {
            persistByQueryId(cp.getQueryId(), cp.getAlias(), owner, CachedRunningQuery.Status.ERROR, statusMessage, true);
        } catch (IOException e1) {
            response.addException(new QueryException(DatawaveErrorCode.CACHED_QUERY_PERSISTANCE_ERROR, e1));
        }
        QueryException qe = new QueryException(DatawaveErrorCode.CACHED_QUERY_SET_ERROR, e);
        log.error(qe);
        response.addException(qe.getBottomQueryException());
        throw new DatawaveWebApplicationException(qe, response);
    } finally {
        crq.closeConnection(log);
        // Push metrics
        if (crq != null && crq.getQueryLogic().getCollectQueryMetrics() == true) {
            try {
                metrics.updateMetric(crq.getMetric());
            } catch (Exception e1) {
                log.error(e1.getMessage(), e1);
            }
        }
    }
}
Also used : Query(datawave.webservice.query.Query) RunningQuery(datawave.webservice.query.runner.RunningQuery) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) AuditType(datawave.webservice.common.audit.Auditor.AuditType) Connection(java.sql.Connection) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) 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) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) CachedResultsResponse(datawave.webservice.result.CachedResultsResponse) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) GenerateQuerySessionId(datawave.annotation.GenerateQuerySessionId) Interceptors(javax.interceptor.Interceptors) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

GenerateQuerySessionId (datawave.annotation.GenerateQuerySessionId)13 Timed (com.codahale.metrics.annotation.Timed)11 Interceptors (javax.interceptor.Interceptors)11 Produces (javax.ws.rs.Produces)11 POST (javax.ws.rs.POST)10 QueryException (datawave.webservice.query.exception.QueryException)8 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)7 NoResultsException (datawave.webservice.common.exception.NoResultsException)7 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)7 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)7 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)7 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)7 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)7 IOException (java.io.IOException)7 Path (javax.ws.rs.Path)7 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)6 GZIP (org.jboss.resteasy.annotations.GZIP)6 BadRequestException (datawave.webservice.common.exception.BadRequestException)5 MultivaluedMapImpl (org.jboss.resteasy.specimpl.MultivaluedMapImpl)4 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)3