use of datawave.webservice.result.BaseQueryResponse in project datawave by NationalSecurityAgency.
the class MutableMetadataHandler method findMatchingEventUuid.
/**
* Finds the event by creating a query by UUID from the keys and values in the runtime parameters.
*
* @param uuid
* @param uuidType
* @param userAuths
* @return Event
* @throws Exception
*/
protected EventBase<?, ?> findMatchingEventUuid(String uuid, String uuidType, Set<Authorizations> userAuths, ModificationOperation operation) throws Exception {
String field = operation.getFieldName();
String columnVisibility = operation.getColumnVisibility();
// query in format uuidType:uuid
StringBuilder query = new StringBuilder();
query.append(uuidType.toUpperCase()).append(":\"").append(uuid).append("\"");
// make the query only return the field to be modified and the UUIDType (avoids NoResultsException on an insert where the field has no values)
StringBuilder queryOptions = new StringBuilder();
queryOptions.append("query.syntax:LUCENE-UUID;raw.data.only:true");
if (field != null) {
queryOptions.append(";return.fields:").append(field.toUpperCase()).append(",").append(uuidType.toUpperCase());
}
String logicName = "LuceneUUIDEventQuery";
DefaultEvent e = null;
QueryExecutorBean queryService = this.getQueryService();
String id = null;
HashSet<String> auths = new HashSet<>();
for (Authorizations a : userAuths) auths.addAll(Arrays.asList(a.toString().split(",")));
Date expiration = new Date();
expiration = new Date(expiration.getTime() + (1000 * 60 * 60 * 24));
try {
MultivaluedMap<String, String> paramsMap = new MultivaluedMapImpl<>();
paramsMap.putAll(QueryParametersImpl.paramsToMap(logicName, query.toString(), "Query to find matching records for metadata modification", columnVisibility, new Date(0), new Date(), StringUtils.join(auths, ','), expiration, 2, -1, null, QueryPersistence.TRANSIENT, queryOptions.toString(), false));
GenericResponse<String> createResponse = queryService.createQuery(logicName, paramsMap);
id = createResponse.getResult();
BaseQueryResponse response = queryService.next(id);
if (response instanceof DefaultEventQueryResponse) {
DefaultEventQueryResponse eResponse = (DefaultEventQueryResponse) response;
if (eResponse.getEvents().size() > 1) {
throw new IllegalStateException("More than one event matched " + uuid + " (" + eResponse.getEvents().size() + " matched)");
}
if (eResponse.getEvents().isEmpty()) {
throw new IllegalStateException("No event matched " + uuid);
}
e = (DefaultEvent) eResponse.getEvents().get(0);
}
} catch (Exception ex) {
log.error(ex);
} finally {
if (id != null) {
queryService.close(id);
}
}
return e;
}
use of datawave.webservice.result.BaseQueryResponse in project datawave by NationalSecurityAgency.
the class GroupingTest method runTestQueryWithGrouping.
protected BaseQueryResponse runTestQueryWithGrouping(Map<String, Integer> expected, String querystr, Date startDate, Date endDate, Map<String, String> extraParms, Connector connector) throws Exception {
log.debug("runTestQueryWithGrouping");
QueryImpl settings = new QueryImpl();
settings.setBeginDate(startDate);
settings.setEndDate(endDate);
settings.setPagesize(Integer.MAX_VALUE);
settings.setQueryAuthorizations(auths.serialize());
settings.setQuery(querystr);
settings.setParameters(extraParms);
settings.setId(UUID.randomUUID());
log.debug("query: " + settings.getQuery());
log.debug("logic: " + settings.getQueryLogicName());
GenericQueryConfiguration config = logic.initialize(connector, settings, authSet);
logic.setupQuery(config);
DocumentTransformer transformer = (DocumentTransformer) (logic.getTransformer(settings));
TransformIterator iter = new DatawaveTransformIterator(logic.iterator(), transformer);
List<Object> eventList = new ArrayList<>();
while (iter.hasNext()) {
eventList.add(iter.next());
}
BaseQueryResponse response = transformer.createResponse(eventList);
// un-comment to look at the json output
ObjectMapper mapper = new ObjectMapper();
mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
mapper.writeValue(temporaryFolder.newFile(), response);
Assert.assertTrue(response instanceof DefaultEventQueryResponse);
DefaultEventQueryResponse eventQueryResponse = (DefaultEventQueryResponse) response;
Assert.assertEquals("Got the wrong number of events", expected.size(), (long) eventQueryResponse.getReturnedEvents());
for (EventBase event : eventQueryResponse.getEvents()) {
String firstKey = "";
String secondKey = "";
Integer value = null;
for (Object field : event.getFields()) {
FieldBase fieldBase = (FieldBase) field;
switch(fieldBase.getName()) {
case "COUNT":
value = Integer.valueOf(fieldBase.getValueString());
break;
case "GENDER":
case "GEN":
case "BIRTHDAY":
firstKey = fieldBase.getValueString();
break;
case "AGE":
case "AG":
case "RECORD":
secondKey = fieldBase.getValueString();
break;
}
}
log.debug("mapping is " + firstKey + "-" + secondKey + " count:" + value);
String key;
if (!firstKey.isEmpty() && !secondKey.isEmpty()) {
key = firstKey + "-" + secondKey;
} else if (!firstKey.isEmpty()) {
key = firstKey;
} else {
key = secondKey;
}
Assert.assertEquals(expected.get(key), value);
}
return response;
}
use of datawave.webservice.result.BaseQueryResponse in project datawave by NationalSecurityAgency.
the class FieldIndexCountQueryTransformer method createResponse.
@Override
public BaseQueryResponse createResponse(List<Object> resultList) {
EventQueryResponseBase response = responseObjectFactory.getEventQueryResponse();
List<EventBase> eventList = new ArrayList<>();
for (Object o : resultList) {
EventBase<?, ?> e = (EventBase<?, ?>) o;
eventList.add(e);
}
response.setFields(variableFieldList);
response.setEvents(eventList);
response.setReturnedEvents((long) eventList.size());
return (BaseQueryResponse) response;
}
use of datawave.webservice.result.BaseQueryResponse in project datawave by NationalSecurityAgency.
the class CachedResultsBean method getRows.
/**
* Returns a set of rows based on the given starting and end positions. The response object type is dynamic, see the listQueryLogic operation to determine
* what the response type object will be.
*
* @param queryId
* CachedResults queryId
* @param rowBegin
* first row to be returned
* @param rowEnd
* last row to be returned
* @return datawave.webservice.result.BaseQueryResponse
* @RequestHeader X-ProxiedEntitiesChain use when proxying request for user by specifying a chain of DNs of the identities to proxy
* @RequestHeader X-ProxiedIssuersChain required when using X-ProxiedEntitiesChain, specify one issuer DN per subject DN listed in X-ProxiedEntitiesChain
* @RequestHeader query-session-id session id value used for load balancing purposes. query-session-id can be placed in the request in a Cookie header or as
* a query parameter
* @ResponseHeader X-OperationTimeInMS time spent on the server performing the operation, does not account for network or result serialization
* @ResponseHeader X-Partial-Results true if the page contains less than the requested number of results
*
* @HTTP 200 success
* @HTTP 401 caller is not authorized to run the query
* @HTTP 412 if the query is not active
* @HTTP 500 internal server error
*/
@GET
@javax.ws.rs.Path("/{queryId}/getRows")
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf" })
@GZIP
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
@Timed(name = "dw.cachedr.getRows", absolute = true)
public BaseQueryResponse getRows(@PathParam("queryId") @Required("queryId") String queryId, @QueryParam("rowBegin") @DefaultValue("1") Integer rowBegin, @QueryParam("rowEnd") Integer rowEnd) {
BaseQueryResponse response = responseObjectFactory.getEventQueryResponse();
// Find out who/what called this method
Principal p = ctx.getCallerPrincipal();
String owner = getOwnerFromPrincipal(p);
try {
if (rowBegin < 1) {
throw new BadRequestQueryException(DatawaveErrorCode.ROW_BEGIN_LESS_THAN_1);
}
if (rowEnd != null && rowEnd < rowBegin) {
throw new BadRequestQueryException(DatawaveErrorCode.ROW_END_LESS_THAN_ROW_BEGIN);
}
// If there is a this.maxPageSize set, then we should honor it here. Otherwise, we use Integer.MAX_VALUE
int maxPageSize = cachedResultsConfiguration.getMaxPageSize();
if (rowEnd == null) {
if (maxPageSize > 0) {
rowEnd = (rowBegin + maxPageSize) - 1;
} else {
rowEnd = Integer.MAX_VALUE;
}
}
int pagesize = (rowEnd - rowBegin) + 1;
if (maxPageSize > 0 && pagesize > maxPageSize) {
throw new QueryException(DatawaveErrorCode.TOO_MANY_ROWS_REQUESTED, MessageFormat.format("Size must be less than or equal to: {0}", maxPageSize));
}
CachedRunningQuery crq = null;
try {
// Get the CachedRunningQuery object from the cache
try {
crq = retrieve(queryId, owner);
} catch (IOException e) {
throw new PreConditionFailedQueryException(DatawaveErrorCode.CACHED_RESULTS_IMPORT_ERROR, e);
}
if (null == crq)
throw new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_NOT_CACHED);
response = crq.getQueryLogic().getResponseObjectFactory().getEventQueryResponse();
if (!crq.getUser().equals(owner)) {
throw new UnauthorizedQueryException(DatawaveErrorCode.QUERY_OWNER_MISMATCH, MessageFormat.format("{0} != {1}", crq.getUser(), owner));
}
synchronized (crq) {
if (crq.isActivated() == false) {
Connection connection = ds.getConnection();
String logicName = crq.getQueryLogicName();
QueryLogic<?> queryLogic = queryFactory.getQueryLogic(logicName, p);
crq.activate(connection, queryLogic);
}
try {
ResultsPage resultList = crq.getRows(rowBegin, rowEnd, cachedResultsConfiguration.getPageByteTrigger());
response = crq.getTransformer().createResponse(resultList);
Status status;
if (!resultList.getResults().isEmpty()) {
response.setHasResults(true);
status = Status.OK;
} else {
response.setHasResults(false);
status = Status.NO_CONTENT;
}
response.setLogicName(crq.getQueryLogic().getLogicName());
response.setQueryId(crq.getQueryId());
if (response instanceof TotalResultsAware) {
((TotalResultsAware) response).setTotalResults(crq.getTotalRows());
}
if (status == Status.NO_CONTENT) {
throw new NoResultsQueryException(DatawaveErrorCode.NO_CONTENT_STATUS);
}
crq.getMetric().setLifecycle(QueryMetric.Lifecycle.RESULTS);
return response;
} catch (SQLException e) {
throw new QueryException();
} catch (NoResultsQueryException e) {
throw e;
} catch (RuntimeException e) {
log.error(e.getMessage(), e);
throw e;
} finally {
crq.closeConnection(log);
}
}
} finally {
// Push metrics
try {
if (null != crq && crq.getQueryLogic().getCollectQueryMetrics() == true) {
metrics.updateMetric(crq.getMetric());
}
} catch (Exception e1) {
log.error("Error updating metrics", e1);
}
}
} catch (Exception e) {
DatawaveErrorCode dec;
if (e instanceof NoResultsQueryException) {
dec = DatawaveErrorCode.NO_CONTENT_STATUS;
} else {
dec = DatawaveErrorCode.CACHED_QUERY_TRANSACTION_ERROR;
}
QueryException qe = new QueryException(dec, e);
log.error(qe);
response.addException(qe.getBottomQueryException());
int statusCode = qe.getBottomQueryException().getStatusCode();
throw new DatawaveWebApplicationException(qe, response, statusCode);
}
}
use of datawave.webservice.result.BaseQueryResponse in project datawave by NationalSecurityAgency.
the class CachedResultsBean method previous.
/**
* Returns the previous page of results to the caller. The response object type is dynamic, see the listQueryLogic operation to determine what the response
* type object will be.
*
* @param queryId
* user defined id for this query
* @return previous page of results
*
* @return datawave.webservice.result.BaseQueryResponse
* @RequestHeader X-ProxiedEntitiesChain use when proxying request for user by specifying a chain of DNs of the identities to proxy
* @RequestHeader X-ProxiedIssuersChain required when using X-ProxiedEntitiesChain, specify one issuer DN per subject DN listed in X-ProxiedEntitiesChain
* @RequestHeader query-session-id session id value used for load balancing purposes. query-session-id can be placed in the request in a Cookie header or as
* a query parameter
* @ResponseHeader X-OperationTimeInMS time spent on the server performing the operation, does not account for network or result serialization
* @ResponseHeader X-query-page-number page number returned by this call
* @ResponseHeader X-query-last-page if true then there are no more pages for this query, caller should call close()
* @ResponseHeader X-Partial-Results true if the page contains less than the requested number of results
*
* @HTTP 200 success
* @HTTP 401 caller is not authorized to run the query
* @HTTP 412 if the query is not active
* @HTTP 500 internal server error
*/
@GET
@javax.ws.rs.Path("/{queryId}/previous")
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf" })
@GZIP
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
@Timed(name = "dw.cachedr.previous", absolute = true)
public BaseQueryResponse previous(@PathParam("queryId") @Required("queryId") String queryId) {
BaseQueryResponse response = responseObjectFactory.getEventQueryResponse();
// Find out who/what called this method
Principal p = ctx.getCallerPrincipal();
String owner = getOwnerFromPrincipal(p);
try {
CachedRunningQuery crq = null;
try {
// Get the CachedRunningQuery object from the cache
try {
crq = retrieve(queryId, owner);
} catch (IOException e) {
throw new PreConditionFailedQueryException(DatawaveErrorCode.CACHED_RESULTS_IMPORT_ERROR, e);
}
if (null == crq)
throw new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_NOT_CACHED);
if (!crq.getUser().equals(owner)) {
throw new UnauthorizedQueryException(DatawaveErrorCode.QUERY_OWNER_MISMATCH, MessageFormat.format("{0} != {1}", crq.getUser(), owner));
}
synchronized (crq) {
if (crq.isActivated() == false) {
if (crq.getShouldAutoActivate()) {
Connection connection = ds.getConnection();
String logicName = crq.getQueryLogicName();
QueryLogic<?> queryLogic = queryFactory.getQueryLogic(logicName, p);
crq.activate(connection, queryLogic);
} else {
throw new PreConditionFailedQueryException(DatawaveErrorCode.QUERY_TIMEOUT_FOR_RESOURCES);
}
}
try {
ResultsPage resultList = crq.previous(cachedResultsConfiguration.getPageByteTrigger());
long pageNum = crq.getLastPageNumber();
response = crq.getTransformer().createResponse(resultList);
Status status = null;
if (!resultList.getResults().isEmpty()) {
response.setHasResults(true);
status = Status.OK;
} else {
response.setHasResults(false);
status = Status.NO_CONTENT;
}
response.setPageNumber(pageNum);
response.setLogicName(crq.getQueryLogic().getLogicName());
response.setQueryId(crq.getQueryId());
if (response instanceof TotalResultsAware) {
((TotalResultsAware) response).setTotalResults(crq.getTotalRows());
}
if (status == Status.NO_CONTENT) {
throw new NoResultsException(null);
}
crq.getMetric().setLifecycle(QueryMetric.Lifecycle.RESULTS);
return response;
} catch (SQLException e) {
throw new QueryException(DatawaveErrorCode.CACHED_QUERY_SQL_ERROR, e);
}
}
} finally {
// Push metrics
if (null != crq && crq.getQueryLogic().getCollectQueryMetrics() == true) {
try {
metrics.updateMetric(crq.getMetric());
} catch (Exception e1) {
log.error("Error updating metrics", e1);
}
}
}
} catch (Exception e) {
QueryException qe = null;
if (e instanceof NoResultsException) {
qe = new QueryException(DatawaveErrorCode.NO_CONTENT_STATUS, e);
} else {
qe = new QueryException("Error calling previous.", e, "500-42");
log.error(qe);
}
response.addException(qe.getBottomQueryException());
int statusCode = qe.getBottomQueryException().getStatusCode();
throw new DatawaveWebApplicationException(qe, response, statusCode);
}
}
Aggregations