Search in sources :

Example 21 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class LookupUUIDUtil method validateLookupCriteria.

private AbstractUUIDLookupCriteria validateLookupCriteria(final AbstractUUIDLookupCriteria criteria, boolean validateUUIDTerms) {
    // Initialize the validated logic name, which is only necessary for UUID lookup and
    // OK to be a null value when paging through content results.
    String logicName = null;
    // Conditionally validate UUID type/value pairs
    if (validateUUIDTerms) {
        // Get the unvalidated LUCENE query for UUID lookup
        final String unvalidatedQuery = criteria.getRawQueryString();
        // Initialize the counter for validating against the maximum number of allowed UUIDs
        int uuidPairCount = 0;
        int eventTypeCountForContentLookup = 0;
        // Reformat the query into a tokenizable series of UUID type/value pairs
        String tokenizablePairs;
        if (null != unvalidatedQuery) {
            tokenizablePairs = unvalidatedQuery;
            // Replace grouping characters with whitespace
            tokenizablePairs = tokenizablePairs.replaceAll(REGEX_GROUPING_CHARS, SPACE);
            // Remove most, but not all, non-word characters
            tokenizablePairs = tokenizablePairs.replaceAll(REGEX_NONWORD_CHARS, EMPTY_STRING);
            // Remove OR operators
            tokenizablePairs = tokenizablePairs.replaceAll(REGEX_OR_OPERATOR, SPACE);
        } else {
            tokenizablePairs = EMPTY_STRING;
        }
        // Validate each UUID type and value
        final String[] uuidTypeValuePairs = tokenizablePairs.split(REGEX_WHITESPACE_CHARS);
        for (final String potentialUUIDTerm : uuidTypeValuePairs) {
            // Validate the "potential" UUID term. It's potential because it could be an OR operator
            // or some other query syntax that would be validated with more scrutiny once the query
            // executor is invoked.
            final UUIDType uuidType = this.validateUUIDTerm(potentialUUIDTerm.trim(), logicName);
            if (null != uuidType) {
                // Assign the query logic name if undefined
                if (null == logicName) {
                    logicName = uuidType.getDefinedView();
                }
                // Increment the UUID type/value count
                uuidPairCount++;
                // Increment the counter for specialized "event" UUID types in the case of content lookups
                if (criteria.isContentLookup() && EVENT_TYPE_NAME.equals(uuidType.getFieldName())) {
                    eventTypeCountForContentLookup++;
                }
            }
        }
        // Validate at least one UUID was specified in the query string
        if (null == logicName) {
            final String message = "Undefined UUID types not supported with the LuceneToJexlUUIDQueryParser";
            final GenericResponse<String> errorReponse = new GenericResponse<>();
            errorReponse.addMessage(message);
            throw new DatawaveWebApplicationException(new IllegalArgumentException(message), errorReponse);
        }
        // Validate the number of specified UUIDs did not exceed the upper limit, if any
        if ((this.maxAllowedBatchLookupUUIDs > 0) && (uuidPairCount > this.maxAllowedBatchLookupUUIDs)) {
            final String message = "The " + uuidPairCount + " specified UUIDs exceed the maximum number of " + this.maxAllowedBatchLookupUUIDs + " allowed for a given lookup request";
            final GenericResponse<String> errorReponse = new GenericResponse<>();
            errorReponse.addMessage(message);
            throw new DatawaveWebApplicationException(new IllegalArgumentException(message), errorReponse);
        }
        // Set the flag if we know we're dealing with an all-event UID lookup that has not exceeded the max page size
        if ((eventTypeCountForContentLookup > 0) && (uuidPairCount == eventTypeCountForContentLookup) && (uuidPairCount <= Integer.parseInt(criteria.getQueryParameters().getFirst(QueryParameters.QUERY_PAGESIZE)))) {
            criteria.setAllEventLookup(true);
        }
    }
    // Set the query logic
    criteria.getQueryParameters().put(QueryParameters.QUERY_LOGIC_NAME, Collections.singletonList(logicName));
    List<String> paramList = criteria.getQueryParameters().remove(QueryParameters.QUERY_PARAMS);
    String params = null;
    if (paramList != null && !paramList.isEmpty()) {
        params = paramList.get(0);
    }
    // Add Lucene syntax to the parameters, except during a call for next content
    if (!(criteria instanceof NextContentCriteria)) {
        params = params + PARAM_LUCENE_QUERY_SYNTAX;
    }
    // Conditionally add content.lookup syntax to parameters to indicate content lookup during "next" calls
    if (criteria.isContentLookup() && !criteria.isAllEventLookup()) {
        params = params + ';' + PARAM_CONTENT_LOOKUP + ':' + true;
    }
    criteria.getQueryParameters().putSingle(QueryParameters.QUERY_PARAMS, params);
    // All is well, so return the validated criteria
    return criteria;
}
Also used : GenericResponse(datawave.webservice.result.GenericResponse) UUIDType(datawave.query.data.UUIDType) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException)

Example 22 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class MapReduceBean method ooziesubmit.

/**
 * Execute a Oozie workflow with the given workFlow name and runtime parameters
 *
 * @param queryParameters
 * @return
 */
@POST
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff" })
@javax.ws.rs.Path("/ooziesubmit")
@GZIP
public GenericResponse<String> ooziesubmit(MultivaluedMap<String, String> queryParameters) {
    GenericResponse<String> response = new GenericResponse<>();
    String workFlow = queryParameters.getFirst(OozieJobConstants.WORKFLOW_PARAM);
    if (StringUtils.isBlank(workFlow)) {
        throw new BadRequestException(new IllegalArgumentException(OozieJobConstants.WORKFLOW_PARAM + " parameter missing"), response);
    }
    String parameters = queryParameters.getFirst(OozieJobConstants.PARAMETERS);
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String sid = null;
    String userDn = p.getName();
    DatawavePrincipal datawavePrincipal = null;
    if (p instanceof DatawavePrincipal) {
        datawavePrincipal = (DatawavePrincipal) p;
        sid = datawavePrincipal.getShortName();
    } else {
        QueryException qe = new QueryException(DatawaveErrorCode.UNEXPECTED_PRINCIPAL_ERROR, MessageFormat.format("Class: {0}", p.getClass().getName()));
        response.addException(qe);
        throw new DatawaveWebApplicationException(qe, response);
    }
    OozieJobConfiguration job;
    try {
        MapReduceJobConfiguration mrConfig = this.mapReduceConfiguration.getConfiguration(workFlow);
        if (mrConfig instanceof OozieJobConfiguration) {
            job = (OozieJobConfiguration) mrConfig;
        } else {
            throw new IllegalArgumentException(workFlow + " not an Oozie job configuration");
        }
    } catch (IllegalArgumentException e) {
        BadRequestQueryException qe = new BadRequestQueryException(DatawaveErrorCode.JOB_CONFIGURATION_ERROR, e);
        response.addException(qe);
        throw new BadRequestException(qe, response);
    }
    if (job instanceof NeedCallerDetails) {
        ((NeedCallerDetails) job).setUserSid(sid);
        ((NeedCallerDetails) job).setPrincipal(p);
    }
    // Ensure that the user has the required roles and has passed the required auths
    if (null != job.getRequiredRoles() || null != job.getRequiredAuths()) {
        try {
            canRunJob(datawavePrincipal, queryParameters, job.getRequiredRoles(), job.getRequiredAuths());
        } catch (UnauthorizedQueryException qe) {
            // user does not have all of the required roles or did not pass the required auths
            response.addException(qe);
            throw new UnauthorizedException(qe, response);
        }
    }
    String id = sid + "_" + UUID.randomUUID();
    OozieClient oozieClient = null;
    Properties oozieConf = null;
    try {
        oozieClient = new OozieClient((String) job.getJobConfigurationProperties().get(OozieJobConstants.OOZIE_CLIENT_PROP));
        oozieConf = oozieClient.createConfiguration();
        job.initializeOozieConfiguration(id, oozieConf, queryParameters);
        job.validateWorkflowParameter(oozieConf, mapReduceConfiguration);
    } catch (QueryException qe) {
        log.error(qe.getMessage(), qe);
        response.addException(qe);
        throw new DatawaveWebApplicationException(qe, response);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        response.addException(new QueryException(e.getMessage(), e));
        throw new DatawaveWebApplicationException(e, response);
    } finally {
        // audit query here
        Auditor.AuditType auditType = job.getAuditType();
        log.trace("Audit type is: " + auditType.name());
        if (!auditType.equals(Auditor.AuditType.NONE)) {
            try {
                marking.validate(queryParameters);
                PrivateAuditConstants.stripPrivateParameters(queryParameters);
                queryParameters.putSingle(PrivateAuditConstants.USER_DN, userDn);
                queryParameters.putSingle(PrivateAuditConstants.COLUMN_VISIBILITY, marking.toColumnVisibilityString());
                queryParameters.putSingle(PrivateAuditConstants.AUDIT_TYPE, auditType.name());
                List<String> selectors = job.getSelectors(queryParameters, oozieConf);
                if (selectors != null && !selectors.isEmpty()) {
                    queryParameters.put(PrivateAuditConstants.SELECTORS, selectors);
                }
                // 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) {
                log.error("Error validating audit parameters", 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);
                response.addMessage("Error auditing query - " + e.getMessage());
                throw new BadRequestException(e, response);
            }
        }
    }
    // Submit the Oozie workflow.
    try {
        String jobID = null;
        try {
            jobID = oozieClient.run(oozieConf);
        } catch (Exception e) {
            throw new QueryException(DatawaveErrorCode.OOZIE_JOB_START_ERROR, e);
        }
        try {
            String jobResultstDir = oozieConf.getProperty(OozieJobConstants.OUT_DIR_PROP) + "/" + id;
            response.setResult(id);
            Path baseDir = new Path(this.mapReduceConfiguration.getMapReduceBaseDirectory());
            // Create a directory path for this job
            Path jobDir = new Path(baseDir, id);
            mapReduceState.create(id, job.getHdfsUri(), job.getJobTracker(), jobDir.toString(), jobID, jobResultstDir, parameters, workFlow);
        } catch (Exception e) {
            QueryException qe = new QueryException(DatawaveErrorCode.MAPREDUCE_STATE_PERSISTENCE_ERROR, e);
            response.addException(qe.getBottomQueryException());
            try {
                oozieClient.kill(jobID);
                // if we successfully kill the job, throw the original exception
                throw qe;
            } catch (Exception e2) {
                // throw the exception from killing the job
                throw new QueryException(DatawaveErrorCode.MAPREDUCE_JOB_KILL_ERROR, e2);
            }
        }
    } catch (QueryException qe) {
        log.error(qe.getMessage(), qe);
        response.addException(qe);
        throw new DatawaveWebApplicationException(qe, response);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        QueryException qe = new QueryException(DatawaveErrorCode.UNKNOWN_SERVER_ERROR, e.getMessage());
        response.addException(qe);
        throw new DatawaveWebApplicationException(qe, response);
    }
    return response;
}
Also used : Path(org.apache.hadoop.fs.Path) GenericResponse(datawave.webservice.result.GenericResponse) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) NeedCallerDetails(datawave.webservice.mr.configuration.NeedCallerDetails) Properties(java.util.Properties) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) WebApplicationException(javax.ws.rs.WebApplicationException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) IOException(java.io.IOException) QueryException(datawave.webservice.query.exception.QueryException) BadRequestException(datawave.webservice.common.exception.BadRequestException) NotFoundException(datawave.webservice.common.exception.NotFoundException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) Auditor(datawave.webservice.common.audit.Auditor) OozieClient(org.apache.oozie.client.OozieClient) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) MapReduceJobConfiguration(datawave.webservice.mr.configuration.MapReduceJobConfiguration) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) BadRequestException(datawave.webservice.common.exception.BadRequestException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) OozieJobConfiguration(datawave.webservice.mr.configuration.OozieJobConfiguration) Principal(java.security.Principal) ServerPrincipal(datawave.security.system.ServerPrincipal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 23 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testCreateQueryAndNext_InvalidExpirationDate.

@Test
public void testCreateQueryAndNext_InvalidExpirationDate() 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 - 500);
    int pagesize = 1;
    int pageTimeout = -1;
    Long maxResultsOverride = null;
    QueryPersistence persistenceMode = QueryPersistence.PERSISTENT;
    String parameters = null;
    boolean trace = false;
    MultivaluedMap<String, String> p = new MultivaluedMapImpl<>();
    p.putAll(QueryParametersImpl.paramsToMap(queryLogicName, query, queryName, queryVisibility, beginDate, endDate, queryAuthorizations, expirationDate, pagesize, pageTimeout, maxResultsOverride, persistenceMode, parameters, trace));
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    setInternalState(subject, QueryParameters.class, new QueryParametersImpl());
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    Throwable result1 = null;
    try {
        subject.createQueryAndNext(queryLogicName, p);
    } catch (DatawaveWebApplicationException e) {
        result1 = e;
    }
    PowerMock.verifyAll();
    // Verify results
    assertTrue("BadRequestException expected to have been thrown", result1 instanceof BadRequestException);
    assertEquals("Thrown exception expected to have been due to invalid expiration date", "400-3", ((QueryException) result1.getCause()).getErrorCode());
}
Also used : MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) QueryParametersImpl(datawave.webservice.query.QueryParametersImpl) Date(java.util.Date) QueryPersistence(datawave.webservice.query.QueryPersistence) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) BadRequestException(datawave.webservice.common.exception.BadRequestException) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 24 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testReset_PreexistingRunningQueryWithCloseConnectionException.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testReset_PreexistingRunningQueryWithCloseConnectionException() throws Exception {
    // Set local test input
    String authorization = "AUTH_1";
    String queryName = "queryName";
    String userName = "userName";
    String userSid = "sid";
    // Set expectations
    expect(this.context.getUserTransaction()).andReturn(this.transaction).anyTimes();
    this.transaction.begin();
    expect(this.transaction.getStatus()).andReturn(Status.STATUS_ACTIVE).anyTimes();
    expect(this.context.getCallerPrincipal()).andReturn(this.principal);
    expect(this.principal.getName()).andReturn(userName);
    expect(this.principal.getShortName()).andReturn(userSid);
    expect(this.principal.getAuthorizations()).andReturn((Collection) Arrays.asList(Arrays.asList(authorization)));
    expect(this.cache.get(queryName)).andReturn(this.runningQuery);
    expect(this.runningQuery.getSettings()).andReturn(this.query);
    expect(this.query.getOwner()).andReturn(userSid);
    expect(this.runningQuery.getTraceInfo()).andReturn(this.traceInfo);
    expect(this.cache.lock(queryName)).andReturn(true);
    expect(this.runningQuery.getConnection()).andReturn(this.connector);
    this.runningQuery.closeConnection(this.connectionFactory);
    PowerMock.expectLastCall().andThrow(new IOException("INTENTIONALLY THROWN 1ST-LEVEL TEST EXCEPTION"));
    cache.unlock(queryName);
    this.transaction.commit();
    PowerMock.expectLastCall().andThrow(new IllegalStateException("INTENTIONALLY THROWN 3RD-LEVEL TEST EXCEPTION"));
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    setInternalState(subject, EJBContext.class, context);
    setInternalState(subject, AccumuloConnectionFactory.class, connectionFactory);
    setInternalState(subject, ResponseObjectFactory.class, responseObjectFactory);
    setInternalState(subject, QueryCache.class, cache);
    setInternalState(subject, ClosedQueryCache.class, closedCache);
    setInternalState(subject, Persister.class, persister);
    setInternalState(subject, QueryLogicFactory.class, queryLogicFactory);
    setInternalState(subject, QueryExpirationConfiguration.class, queryExpirationConf);
    setInternalState(subject, AuditBean.class, auditor);
    setInternalState(subject, QueryMetricsBean.class, metrics);
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    Throwable result1 = null;
    try {
        subject.reset(queryName);
    } catch (DatawaveWebApplicationException e) {
        result1 = e.getCause();
    }
    PowerMock.verifyAll();
    // Verify results
    assertTrue("Query exception expected to have been thrown due to locking problem", result1 instanceof QueryException);
}
Also used : NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) IOException(java.io.IOException) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 25 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testDuplicateQuery_BadRequestException.

@Test
public void testDuplicateQuery_BadRequestException() throws Exception {
    // Set local test input
    String queryLogicName = "queryLogicName";
    String query = "query";
    String newQueryName = "";
    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 = 10;
    int pageTimeout = -1;
    Long maxResultsOverride = null;
    QueryPersistence persistenceMode = QueryPersistence.PERSISTENT;
    String parameters = "invalidparam; valid:param";
    boolean trace = true;
    UUID queryId = UUID.randomUUID();
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    Exception result1 = null;
    try {
        subject.duplicateQuery(queryId.toString(), newQueryName, queryLogicName, query, queryVisibility, beginDate, endDate, queryAuthorizations, expirationDate, pagesize, pageTimeout, maxResultsOverride, persistenceMode, parameters, trace);
    } catch (DatawaveWebApplicationException e) {
        result1 = e;
    }
    PowerMock.verifyAll();
    assertNotNull("Expected a DatawaveWebApplicationException.", result1);
    assertEquals("Expected a Bad Request status code.", 400, ((DatawaveWebApplicationException) result1).getResponse().getStatus());
}
Also used : QueryPersistence(datawave.webservice.query.QueryPersistence) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) UUID(java.util.UUID) Date(java.util.Date) 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

DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)72 QueryException (datawave.webservice.query.exception.QueryException)64 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)48 IOException (java.io.IOException)43 Produces (javax.ws.rs.Produces)43 NoResultsException (datawave.webservice.common.exception.NoResultsException)39 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)37 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)37 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)36 GZIP (org.jboss.resteasy.annotations.GZIP)34 Interceptors (javax.interceptor.Interceptors)33 BadRequestException (datawave.webservice.common.exception.BadRequestException)32 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)32 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)31 Path (javax.ws.rs.Path)27 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)26 Principal (java.security.Principal)26 WebApplicationException (javax.ws.rs.WebApplicationException)25 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)24 HeuristicMixedException (javax.transaction.HeuristicMixedException)24