Search in sources :

Example 1 with CachedResultsResponse

use of datawave.webservice.result.CachedResultsResponse in project datawave by NationalSecurityAgency.

the class CachedResultsBean method reset.

/**
 * Re-allocate resources for these cached results and reset paging to the beginning
 *
 * @param queryId
 *            user defined id for this query
 *
 * @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
 *
 * @HTTP 200 success
 * @HTTP 500 internal server error
 */
@PUT
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml" })
@javax.ws.rs.Path("/{queryId}/reset")
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
@GenerateQuerySessionId(cookieBasePath = "/DataWave/CachedResults/")
@Timed(name = "dw.cachedr.close", absolute = true)
public CachedResultsResponse reset(@PathParam("queryId") @Required("queryId") String queryId) {
    CreateQuerySessionIDFilter.QUERY_ID.set(null);
    CachedResultsResponse response = new CachedResultsResponse();
    // 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);
            }
            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() == true) {
                    crq.closeConnection(log);
                }
                Connection connection = ds.getConnection();
                String logicName = crq.getQueryLogicName();
                QueryLogic<?> queryLogic = queryFactory.getQueryLogic(logicName, p);
                crq.activate(connection, queryLogic);
                response.setQueryId(crq.getQueryId());
                response.setOriginalQueryId(crq.getOriginalQueryId());
                response.setViewName(crq.getView());
                response.setAlias(crq.getAlias());
                response.setTotalRows(crq.getTotalRows());
            }
            CreateQuerySessionIDFilter.QUERY_ID.set(queryId);
            return response;
        } 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) {
        response.addMessage(e.getMessage());
        QueryException qe = new QueryException(DatawaveErrorCode.RESET_CALL_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) 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) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) Connection(java.sql.Connection) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) IOException(java.io.IOException) CachedResultsResponse(datawave.webservice.result.CachedResultsResponse) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) 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) GenerateQuerySessionId(datawave.annotation.GenerateQuerySessionId) Interceptors(javax.interceptor.Interceptors) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) PUT(javax.ws.rs.PUT)

Example 2 with CachedResultsResponse

use of datawave.webservice.result.CachedResultsResponse 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 3 with CachedResultsResponse

use of datawave.webservice.result.CachedResultsResponse in project datawave by NationalSecurityAgency.

the class CachedResultsBean method setAlias.

/**
 * Set alias that this cached result can be retrieved by
 *
 * @param queryId
 *            user defined id for this query
 * @param alias
 *            additional name that this query can be retrieved by
 * @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
 *
 * @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}/setAlias")
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
@Timed(name = "dw.cachedr.setAlias", absolute = true)
public CachedResultsResponse setAlias(@PathParam("queryId") @Required("queryId") String queryId, @FormParam("alias") @Required("alias") String alias) {
    CachedResultsResponse response = new CachedResultsResponse();
    // 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 (alias != null) {
                    crq.setAlias(alias);
                    persist(crq, owner);
                }
                response = new CachedResultsResponse();
                response.setOriginalQueryId(crq.getOriginalQueryId());
                response.setQueryId(crq.getQueryId());
                response.setViewName(crq.getView());
                response.setAlias(crq.getAlias());
                response.setTotalRows(crq.getTotalRows());
            }
            return response;
        } 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 = new QueryException(DatawaveErrorCode.CACHED_QUERY_TRANSACTION_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) 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) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) IOException(java.io.IOException) CachedResultsResponse(datawave.webservice.result.CachedResultsResponse) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) 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) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Example 4 with CachedResultsResponse

use of datawave.webservice.result.CachedResultsResponse 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)

Example 5 with CachedResultsResponse

use of datawave.webservice.result.CachedResultsResponse in project datawave by NationalSecurityAgency.

the class CachedResultsBean method loadAndCreate.

/**
 * @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
 *
 * @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}/loadAndCreate")
@Interceptors(RequiredInterceptor.class)
@GenerateQuerySessionId(cookieBasePath = "/DataWave/CachedResults/")
@Timed(name = "dw.cachedr.loadAndCreate", absolute = true)
public CachedResultsResponse loadAndCreate(@Required("queryId") @PathParam("queryId") String queryId, MultivaluedMap<String, String> queryParameters) {
    CreateQuerySessionIDFilter.QUERY_ID.set(null);
    String newQueryId = queryParameters.getFirst("newQueryId");
    Preconditions.checkNotNull(newQueryId, "newQueryId cannot be null");
    Preconditions.checkNotNull(queryId, "queryId cannot be null");
    queryParameters.putSingle(CachedResultsParameters.QUERY_ID, queryId);
    String alias = queryParameters.getFirst(CachedResultsParameters.ALIAS);
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String owner = getOwnerFromPrincipal(p);
    GenericResponse<String> r = null;
    try {
        r = load(queryId, alias);
    } catch (DatawaveWebApplicationException e) {
        if (e instanceof NoResultsException == false) {
            if (e.getCause() instanceof QueryCanceledException) {
                try {
                    persistByQueryId(newQueryId, alias, owner, CachedRunningQuery.Status.CANCELED, "query canceled", true);
                } catch (IOException e1) {
                    log.error("Error persisting state to CachedResults store", e1);
                }
                throw e;
            } else {
                String statusMessage = e.getCause().getMessage();
                if (null == statusMessage) {
                    statusMessage = e.getClass().getName();
                }
                try {
                    persistByQueryId(newQueryId, alias, owner, CachedRunningQuery.Status.ERROR, statusMessage, true);
                } catch (IOException e1) {
                    log.error("Error persisting state to CachedResults store", e1);
                }
                throw e;
            }
        } else if (e.getResponse().getEntity() == null) {
            // NoResultsException can't contain the response object, otherwise we'll return invalid HTML. So, instead, pull the ID from the
            // NoResultsException and make a new GenericResponse here.
            r = new GenericResponse<>();
            r.setResult(((NoResultsException) e).getId());
        }
        if (e.getResponse().getEntity() instanceof GenericResponse<?>) {
            @SuppressWarnings("unchecked") GenericResponse<String> gr = (GenericResponse<String>) e.getResponse().getEntity();
            r = gr;
        } else if (r == null) {
            throw e;
        }
    } catch (RuntimeException e) {
        log.error(e.getMessage(), e);
        throw e;
    }
    String view = r.getResult();
    // pagesize validated in create
    CreateQuerySessionIDFilter.QUERY_ID.set(newQueryId);
    queryParameters.remove(CachedResultsParameters.QUERY_ID);
    queryParameters.remove(CachedResultsParameters.VIEW);
    queryParameters.putSingle(CachedResultsParameters.VIEW, view);
    CachedResultsResponse response = create(newQueryId, queryParameters);
    try {
        persistByQueryId(newQueryId, alias, owner, CachedRunningQuery.Status.AVAILABLE, "", true);
    } catch (IOException e) {
        QueryException qe = new QueryException(DatawaveErrorCode.CACHE_PERSISTANCE_ERROR, e);
        response.addException(qe);
        throw new DatawaveWebApplicationException(e, response);
    }
    return response;
}
Also used : NoResultsException(datawave.webservice.common.exception.NoResultsException) GenericResponse(datawave.webservice.result.GenericResponse) IOException(java.io.IOException) QueryCanceledException(datawave.webservice.common.exception.QueryCanceledException) 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

Timed (com.codahale.metrics.annotation.Timed)5 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)5 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)5 NoResultsException (datawave.webservice.common.exception.NoResultsException)5 QueryCanceledException (datawave.webservice.common.exception.QueryCanceledException)5 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)5 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)5 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)5 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)5 QueryCanceledQueryException (datawave.webservice.query.exception.QueryCanceledQueryException)5 QueryException (datawave.webservice.query.exception.QueryException)5 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)5 CachedResultsResponse (datawave.webservice.result.CachedResultsResponse)5 IOException (java.io.IOException)5 Principal (java.security.Principal)5 Interceptors (javax.interceptor.Interceptors)5 Produces (javax.ws.rs.Produces)5 GenerateQuerySessionId (datawave.annotation.GenerateQuerySessionId)4 NotFoundException (datawave.webservice.common.exception.NotFoundException)4 PreConditionFailedException (datawave.webservice.common.exception.PreConditionFailedException)4