Search in sources :

Example 1 with DefaultEventQueryResponse

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

the class TermFrequencyQueryTransformer method createResponse.

@Override
public BaseQueryResponse createResponse(List<Object> resultList) {
    DefaultEventQueryResponse response = new DefaultEventQueryResponse();
    List<EventBase> eventList = new ArrayList<>();
    for (Object o : resultList) {
        EventBase result = (EventBase) o;
        eventList.add(result);
    }
    response.setEvents(eventList);
    response.setReturnedEvents(new Long(eventList.size()));
    return response;
}
Also used : EventBase(datawave.webservice.query.result.event.EventBase) DefaultEventQueryResponse(datawave.webservice.result.DefaultEventQueryResponse) ArrayList(java.util.ArrayList)

Example 2 with DefaultEventQueryResponse

use of datawave.webservice.result.DefaultEventQueryResponse 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;
}
Also used : DefaultEvent(datawave.webservice.query.result.event.DefaultEvent) Authorizations(org.apache.accumulo.core.security.Authorizations) DefaultEventQueryResponse(datawave.webservice.result.DefaultEventQueryResponse) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) Date(java.util.Date) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) ExecutionException(java.util.concurrent.ExecutionException) QueryExecutorBean(datawave.webservice.query.runner.QueryExecutorBean) BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) HashSet(java.util.HashSet)

Example 3 with DefaultEventQueryResponse

use of datawave.webservice.result.DefaultEventQueryResponse 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;
}
Also used : EventBase(datawave.webservice.query.result.event.EventBase) DefaultEventQueryResponse(datawave.webservice.result.DefaultEventQueryResponse) TransformIterator(org.apache.commons.collections4.iterators.TransformIterator) DatawaveTransformIterator(datawave.webservice.query.iterator.DatawaveTransformIterator) DatawaveTransformIterator(datawave.webservice.query.iterator.DatawaveTransformIterator) ArrayList(java.util.ArrayList) GenericQueryConfiguration(datawave.webservice.query.configuration.GenericQueryConfiguration) QueryImpl(datawave.webservice.query.QueryImpl) BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) FieldBase(datawave.webservice.query.result.event.FieldBase) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with DefaultEventQueryResponse

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

the class ContentQueryTransformer method createResponse.

@Override
public BaseQueryResponse createResponse(List<Object> resultList) {
    DefaultEventQueryResponse response = new DefaultEventQueryResponse();
    List<EventBase> eventList = new ArrayList<>();
    for (Object o : resultList) {
        DefaultEvent result = (DefaultEvent) o;
        eventList.add(result);
    }
    response.setEvents(eventList);
    response.setReturnedEvents((long) eventList.size());
    return response;
}
Also used : EventBase(datawave.webservice.query.result.event.EventBase) DefaultEvent(datawave.webservice.query.result.event.DefaultEvent) DefaultEventQueryResponse(datawave.webservice.result.DefaultEventQueryResponse) ArrayList(java.util.ArrayList)

Example 5 with DefaultEventQueryResponse

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

the class ExtendedQueryExecutorBeanTest method testNext_UserNotOwner.

@Test
public void testNext_UserNotOwner() throws Exception {
    // Set local test input
    String userName = "userName";
    String userSid = "userSid";
    String otherSid = "otherSid";
    UUID queryId = UUID.randomUUID();
    // Set expectations
    expect(this.context.getCallerPrincipal()).andReturn(this.principal).anyTimes();
    expect(this.principal.getName()).andReturn(userName);
    expect(this.principal.getShortName()).andReturn(otherSid);
    expect(this.principal.getProxyServers()).andReturn(new ArrayList<>(0));
    expect(this.context.getUserTransaction()).andReturn(this.transaction).anyTimes();
    this.transaction.begin();
    expect(this.cache.get(queryId.toString())).andReturn(this.runningQuery);
    expect(this.cache.lock(queryId.toString())).andReturn(true);
    expect(this.responseObjectFactory.getEventQueryResponse()).andReturn(new DefaultEventQueryResponse());
    expect(this.runningQuery.getConnection()).andReturn(this.connector);
    expect(this.runningQuery.getSettings()).andReturn(this.query);
    expect(this.query.getOwner()).andReturn(userSid);
    expect(this.runningQuery.getSettings()).andReturn(this.query);
    expect(this.query.getOwner()).andReturn(userSid);
    cache.unlock(queryId.toString());
    expect(this.transaction.getStatus()).andReturn(Status.STATUS_PREPARING).times(2);
    this.transaction.setRollbackOnly();
    this.transaction.commit();
    this.runningQuery.setActiveCall(false);
    expect(this.runningQuery.getLogic()).andReturn((QueryLogic) this.queryLogic1);
    expect(this.queryLogic1.getCollectQueryMetrics()).andReturn(true);
    expect(this.runningQuery.getMetric()).andReturn(this.queryMetric).times(2);
    expectLastCall();
    this.queryMetric.setError(isA(Throwable.class));
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    setInternalState(subject, EJBContext.class, context);
    setInternalState(subject, QueryCache.class, cache);
    setInternalState(subject, ClosedQueryCache.class, closedCache);
    setInternalState(subject, ResponseObjectFactory.class, responseObjectFactory);
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    Exception result1 = null;
    try {
        subject.next(queryId.toString());
    } catch (DatawaveWebApplicationException e) {
        result1 = e;
        assertTrue(e.getCause() instanceof QueryException);
        assertEquals("401-1", ((QueryException) e.getCause().getCause()).getErrorCode());
    }
    PowerMock.verifyAll();
    // Verify results
    assertNotNull("Expected a DatawaveWebApplicationException to be thrown due an unchecked exception", result1);
}
Also used : NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) DefaultEventQueryResponse(datawave.webservice.result.DefaultEventQueryResponse) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) UUID(java.util.UUID) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) JMSRuntimeException(javax.jms.JMSRuntimeException) HeuristicMixedException(javax.transaction.HeuristicMixedException) 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) NoResultsException(datawave.webservice.common.exception.NoResultsException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

DefaultEventQueryResponse (datawave.webservice.result.DefaultEventQueryResponse)13 ArrayList (java.util.ArrayList)8 QueryMetricFactoryImpl (datawave.microservice.querymetric.QueryMetricFactoryImpl)7 UUID (java.util.UUID)7 Test (org.junit.Test)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 BaseQueryResponse (datawave.webservice.result.BaseQueryResponse)6 EventBase (datawave.webservice.query.result.event.EventBase)5 HashSet (java.util.HashSet)5 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)4 Date (java.util.Date)4 Set (java.util.Set)4 HeuristicMixedException (javax.transaction.HeuristicMixedException)4 MultivaluedMapImpl (org.jboss.resteasy.specimpl.MultivaluedMapImpl)4 ColumnVisibilitySecurityMarking (datawave.marking.ColumnVisibilitySecurityMarking)3 QueryMetric (datawave.microservice.querymetric.QueryMetric)3 SubjectIssuerDNPair (datawave.security.authorization.SubjectIssuerDNPair)3 BadRequestException (datawave.webservice.common.exception.BadRequestException)3 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)3 NoResultsException (datawave.webservice.common.exception.NoResultsException)3