Search in sources :

Example 61 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testCreateQuery_userNotInAllowedDNs.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testCreateQuery_userNotInAllowedDNs() throws Exception {
    // Set local test input
    String queryLogicName = "queryLogicName";
    String query = "query";
    String queryName = "queryName";
    String queryVisibility = "A&B";
    long currentTime = System.currentTimeMillis();
    Date beginDate = new Date(currentTime - 5000);
    Date endDate = new Date(currentTime - 1000);
    String queryAuthorizations = "AUTH_1";
    Date expirationDate = new Date(currentTime + 9999);
    int pagesize = 1000;
    int pageTimeout = -1;
    Long maxResultsOverride = null;
    QueryPersistence persistenceMode = QueryPersistence.PERSISTENT;
    String parameters = null;
    boolean trace = false;
    // Set expectations
    MultivaluedMap<String, String> queryParameters = new MultivaluedMapImpl<>();
    queryParameters.putAll(QueryParametersImpl.paramsToMap(queryLogicName, query, queryName, queryVisibility, beginDate, endDate, queryAuthorizations, expirationDate, pagesize, pageTimeout, maxResultsOverride, persistenceMode, parameters, trace));
    ColumnVisibilitySecurityMarking marking = new ColumnVisibilitySecurityMarking();
    marking.validate(queryParameters);
    QueryParameters qp = new QueryParametersImpl();
    qp.validate(queryParameters);
    MultivaluedMap<String, String> op = new MultivaluedMapImpl<>();
    op.putAll(qp.getUnknownParameters(queryParameters));
    op.putSingle(PrivateAuditConstants.LOGIC_CLASS, queryLogicName);
    op.putSingle(PrivateAuditConstants.COLUMN_VISIBILITY, queryVisibility);
    expect(this.queryLogicFactory.getQueryLogic(queryLogicName, this.principal)).andReturn((QueryLogic) this.queryLogic1);
    expect(this.context.getCallerPrincipal()).andReturn(this.principal).anyTimes();
    queryLogic1.validate(queryParameters);
    expect(this.principal.getName()).andReturn("userName Full");
    expect(this.principal.getShortName()).andReturn("userName");
    expect(this.principal.getUserDN()).andReturn(SubjectIssuerDNPair.of("userDN"));
    expect(this.principal.getDNs()).andReturn(new String[] { "userDN" });
    expect(this.principal.getProxyServers()).andReturn(new ArrayList<>(0)).anyTimes();
    expect(this.queryLogic1.containsDNWithAccess(Collections.singletonList("userDN"))).andReturn(false);
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    setInternalState(subject, EJBContext.class, context);
    setInternalState(subject, QueryLogicFactory.class, queryLogicFactory);
    setInternalState(subject, QueryExpirationConfiguration.class, queryExpirationConf);
    setInternalState(subject, SecurityMarking.class, new ColumnVisibilitySecurityMarking());
    setInternalState(subject, QueryParameters.class, new QueryParametersImpl());
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    Throwable result1 = null;
    try {
        subject.createQuery(queryLogicName, queryParameters);
    } catch (DatawaveWebApplicationException e) {
        result1 = e.getCause();
    }
    PowerMock.verifyAll();
    // Verify results
    assertTrue("QueryException expected to have been thrown", result1 instanceof QueryException);
    assertEquals("Thrown exception expected to have been due to access denied", "401", ((QueryException) result1).getErrorCode());
    assertEquals("Thrown exception expected to detail reason for access denial", "None of the DNs used have access to this query logic: [userDN]", result1.getMessage());
}
Also used : ArrayList(java.util.ArrayList) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) QueryParameters(datawave.webservice.query.QueryParameters) QueryParametersImpl(datawave.webservice.query.QueryParametersImpl) Date(java.util.Date) ColumnVisibilitySecurityMarking(datawave.marking.ColumnVisibilitySecurityMarking) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) QueryPersistence(datawave.webservice.query.QueryPersistence) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 62 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class JexlNodeFactory method getLiteral.

private static ASTNumberLiteral getLiteral(Number fieldValue) {
    ASTNumberLiteral literal = new ASTNumberLiteral(ParserTreeConstants.JJTNUMBERLITERAL);
    literal.image = fieldValue.toString();
    if (NATURAL_NUMBERS.contains(fieldValue.getClass())) {
        literal.setNatural(fieldValue.toString());
    } else if (REAL_NUMBERS.contains(fieldValue.getClass())) {
        literal.setReal(fieldValue.toString());
    } else {
        QueryException qe = new QueryException(DatawaveErrorCode.ASTNUMBERLITERAL_TYPE_ASCERTAIN_ERROR, MessageFormat.format("{0}", literal));
        throw new DatawaveFatalQueryException(qe);
    }
    return literal;
}
Also used : DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) QueryException(datawave.webservice.query.exception.QueryException) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) ASTNumberLiteral(org.apache.commons.jexl2.parser.ASTNumberLiteral)

Example 63 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class BaseIndexExpansionVisitor method expand.

/**
 * The expand method is the entrypoint which should be called to run index expansion on a given Jexl tree.
 *
 * @param script
 *            the Jexl tree to expand, not null
 * @param <T>
 *            the Jexl node type
 * @return a rebuilt Jexl tree with applicable fields/terms expanded
 */
@SuppressWarnings("unchecked")
protected <T extends JexlNode> T expand(T script) {
    setupExecutor();
    try {
        if (null == config.getQueryFieldsDatatypes()) {
            QueryException qe = new QueryException(DatawaveErrorCode.DATATYPESFORINDEXFIELDS_MULTIMAP_MISSING);
            throw new DatawaveFatalQueryException(qe);
        }
        T rebuiltScript = (T) (script.jjtAccept(this, null));
        rebuildFutureJexlNodes();
        // handle the case where the root node was expanded
        if (rebuiltScript instanceof FutureJexlNode) {
            rebuiltScript = (T) ((FutureJexlNode) rebuiltScript).getRebuiltNode();
        }
        return rebuiltScript;
    } finally {
        shutdownExecutor();
    }
}
Also used : DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) QueryException(datawave.webservice.query.exception.QueryException) DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException)

Example 64 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave by NationalSecurityAgency.

the class ShardIndexQueryTableStaticMethods method getBoundedRangeRange.

/**
 * Get the accumulo range for a literal query range. Note that it is assumed that the column family set will include the fieldname.
 *
 * @param literalRange
 * @return
 * @throws IllegalRangeArgumentException
 */
public static Range getBoundedRangeRange(LiteralRange<?> literalRange) throws IllegalRangeArgumentException {
    String lower = literalRange.getLower().toString(), upper = literalRange.getUpper().toString();
    Key startKey = new Key(new Text(lower));
    Key endKey = new Key(new Text(literalRange.isUpperInclusive() ? upper + Constants.MAX_UNICODE_STRING : upper));
    Range range = null;
    try {
        range = new Range(startKey, literalRange.isLowerInclusive(), endKey, literalRange.isUpperInclusive());
    } catch (IllegalArgumentException e) {
        QueryException qe = new QueryException(DatawaveErrorCode.RANGE_CREATE_ERROR, e, MessageFormat.format("{0}", literalRange));
        log.debug(qe);
        throw new IllegalRangeArgumentException(qe);
    }
    return range;
}
Also used : DatawaveFatalQueryException(datawave.query.exceptions.DatawaveFatalQueryException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) DoNotPerformOptimizedQueryException(datawave.query.exceptions.DoNotPerformOptimizedQueryException) QueryException(datawave.webservice.query.exception.QueryException) Text(org.apache.hadoop.io.Text) LongRange(org.apache.commons.lang.math.LongRange) LiteralRange(datawave.query.jexl.LiteralRange) Range(org.apache.accumulo.core.data.Range) Key(org.apache.accumulo.core.data.Key) PartialKey(org.apache.accumulo.core.data.PartialKey) IllegalRangeArgumentException(datawave.query.exceptions.IllegalRangeArgumentException)

Example 65 with QueryException

use of datawave.webservice.query.exception.QueryException in project datawave-spring-boot-starter by NationalSecurityAgency.

the class ProtostuffHttpMessageConverterTest method testProtostuffResponse.

@Test
public void testProtostuffResponse() throws Exception {
    MvcResult mvcResult = mvc.perform(get("/vr").accept("application/x-protostuff")).andExpect(status().isOk()).andReturn();
    VoidResponse actual = new VoidResponse();
    ProtostuffIOUtil.mergeFrom(mvcResult.getResponse().getContentAsByteArray(), actual, actual.cachedSchema());
    QueryException qe = new QueryException(DatawaveErrorCode.BAD_RESPONSE_CLASS, "This is a test QueryException");
    ArrayList<String> expectedMessages = Lists.newArrayList("This is a test message", "This is another test message");
    ArrayList<QueryExceptionType> expectedExceptions = Lists.newArrayList(new QueryExceptionType(qe.getMessage(), "Exception with no cause caught", qe.getErrorCode()), new QueryExceptionType("This is the cause exception", "java.lang.Exception: This is the cause exception", null), new QueryExceptionType("this is a test exception", "Exception with no cause caught", null));
    assertTrue(actual.getHasResults());
    assertTrue(actual.getOperationTimeMS() >= 0);
    assertEquals(expectedMessages, actual.getMessages());
    assertEquals(expectedExceptions, actual.getExceptions());
}
Also used : QueryException(datawave.webservice.query.exception.QueryException) VoidResponse(datawave.webservice.result.VoidResponse) QueryExceptionType(datawave.webservice.query.exception.QueryExceptionType) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

QueryException (datawave.webservice.query.exception.QueryException)131 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)63 IOException (java.io.IOException)62 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)57 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)51 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)47 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)45 Produces (javax.ws.rs.Produces)44 NoResultsException (datawave.webservice.common.exception.NoResultsException)40 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)39 DatawaveFatalQueryException (datawave.query.exceptions.DatawaveFatalQueryException)36 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)36 Interceptors (javax.interceptor.Interceptors)36 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)34 GZIP (org.jboss.resteasy.annotations.GZIP)34 Principal (java.security.Principal)32 WebApplicationException (javax.ws.rs.WebApplicationException)31 BadRequestException (datawave.webservice.common.exception.BadRequestException)29 Path (javax.ws.rs.Path)28 Timed (com.codahale.metrics.annotation.Timed)26