Search in sources :

Example 6 with VoidResponse

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

the class ModelBean method deleteMapping.

private VoidResponse deleteMapping(datawave.webservice.model.Model model, String modelTableName, boolean reloadCache) {
    VoidResponse response = new VoidResponse();
    Connector connector = null;
    BatchWriter writer = null;
    String tableName = this.checkModelTableName(modelTableName);
    try {
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        connector = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.LOW, trackingMap);
        writer = connector.createBatchWriter(tableName, new BatchWriterConfig().setMaxLatency(BATCH_WRITER_MAX_LATENCY, TimeUnit.MILLISECONDS).setMaxMemory(BATCH_WRITER_MAX_MEMORY).setMaxWriteThreads(BATCH_WRITER_MAX_THREADS));
        for (FieldMapping mapping : model.getFields()) {
            Mutation m = ModelKeyParser.createDeleteMutation(mapping, model.getName());
            writer.addMutation(m);
        }
    } catch (Exception e) {
        log.error("Could not delete mapping.", e);
        QueryException qe = new QueryException(DatawaveErrorCode.MAPPING_DELETION_ERROR, e);
        response.addException(qe.getBottomQueryException());
        throw new DatawaveWebApplicationException(qe, response);
    } finally {
        if (null != writer) {
            try {
                writer.close();
            } catch (MutationsRejectedException e1) {
                QueryException qe = new QueryException(DatawaveErrorCode.WRITER_CLOSE_ERROR, e1);
                log.error(qe);
                response.addException(qe);
                throw new DatawaveWebApplicationException(qe, response);
            }
        }
        if (null != connector) {
            try {
                connectionFactory.returnConnection(connector);
            } catch (Exception e) {
                log.error("Error returning connection to factory", e);
            }
        }
    }
    if (reloadCache)
        cache.reloadCache(tableName);
    return response;
}
Also used : Connector(org.apache.accumulo.core.client.Connector) QueryException(datawave.webservice.query.exception.QueryException) VoidResponse(datawave.webservice.result.VoidResponse) FieldMapping(datawave.webservice.model.FieldMapping) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) NotFoundException(datawave.webservice.common.exception.NotFoundException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) QueryException(datawave.webservice.query.exception.QueryException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Example 7 with VoidResponse

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

the class ModificationCacheBean method reloadMutableFieldCache.

/**
 * @return datawave.webservice.result.VoidResponse
 * @RequestHeader X-ProxiedEntitiesChain use when proxying request for user
 * @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 202 if asynch is true - see Location response header for the job URI location
 * @HTTP 400 invalid or missing parameter
 * @HTTP 500 internal server error
 */
@GET
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff" })
@Path("/reloadCache")
@GZIP
@JmxManaged
public VoidResponse reloadMutableFieldCache() {
    this.clearCache();
    log.trace("cleared cache");
    final VoidResponse resp = new VoidResponse();
    Connector con = null;
    BatchScanner s = null;
    try {
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        log.trace("getting mutable list from table " + this.modificationConfiguration.getTableName());
        log.trace("modificationConfiguration.getPoolName() = " + modificationConfiguration.getPoolName());
        con = connectionFactory.getConnection(modificationConfiguration.getPoolName(), Priority.ADMIN, trackingMap);
        log.trace("got connection");
        s = ScannerHelper.createBatchScanner(con, this.modificationConfiguration.getTableName(), Collections.singleton(con.securityOperations().getUserAuthorizations(con.whoami())), 8);
        s.setRanges(Collections.singleton(new Range()));
        s.fetchColumnFamily(MODIFICATION_COLUMN);
        for (Entry<Key, Value> e : s) {
            // Field name is in the row and datatype is in the colq.
            String datatype = e.getKey().getColumnQualifier().toString();
            log.trace("datatype = " + datatype);
            String fieldName = e.getKey().getRow().toString();
            log.trace("fieldname = " + fieldName);
            if (null == cache.get(datatype))
                cache.put(datatype, new HashSet<>());
            cache.get(datatype).add(fieldName);
        }
        log.trace("cache size = " + cache.size());
        for (Entry<String, Set<String>> e : cache.entrySet()) {
            log.trace("datatype = " + e.getKey() + ", fieldcount = " + e.getValue().size());
        }
    } catch (Exception e) {
        log.error("Error during initialization of ModificationCacheBean", e);
        throw new EJBException("Error during initialization of ModificationCacheBean", e);
    } finally {
        if (null != s)
            s.close();
        try {
            connectionFactory.returnConnection(con);
        } catch (Exception e) {
            log.error("Error returning connection to pool", e);
        }
    }
    return resp;
}
Also used : Connector(org.apache.accumulo.core.client.Connector) HashSet(java.util.HashSet) Set(java.util.Set) BatchScanner(org.apache.accumulo.core.client.BatchScanner) Range(org.apache.accumulo.core.data.Range) EJBException(javax.ejb.EJBException) VoidResponse(datawave.webservice.result.VoidResponse) Value(org.apache.accumulo.core.data.Value) EJBException(javax.ejb.EJBException) Key(org.apache.accumulo.core.data.Key) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) JmxManaged(org.apache.deltaspike.core.api.jmx.JmxManaged) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 8 with VoidResponse

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

the class QueryExecutorBean method close.

private VoidResponse close(String id, Principal principal) {
    VoidResponse response = new VoidResponse();
    try {
        boolean connectionRequestCanceled = accumuloConnectionRequestBean.cancelConnectionRequest(id, principal);
        Pair<QueryLogic<?>, Connector> tuple = qlCache.pollIfOwnedBy(id, ((DatawavePrincipal) principal).getShortName());
        if (tuple == null) {
            try {
                RunningQuery query = getQueryById(id, principal);
                close(query);
            } catch (Exception e) {
                log.debug("Failed to close " + id + ", checking if closed previously");
                // if this is a query that is in the closed query cache, then we have already successfully closed this query so ignore
                if (!closedQueryCache.exists(id)) {
                    log.debug("Failed to close " + id + ", checking if connection request was canceled");
                    // if connection request was canceled, then the call was successful even if a RunningQuery was not found
                    if (!connectionRequestCanceled) {
                        log.error("Failed to close " + id, e);
                        throw e;
                    }
                }
            }
            response.addMessage(id + " closed.");
        } else {
            QueryLogic<?> logic = tuple.getFirst();
            try {
                logic.close();
            } catch (Exception e) {
                log.error("Exception occurred while closing query logic; may be innocuous if scanners were running.", e);
            }
            connectionFactory.returnConnection(tuple.getSecond());
            response.addMessage(id + " closed before create completed.");
        }
        // no longer need to remember this query
        closedQueryCache.remove(id);
        return response;
    } catch (DatawaveWebApplicationException e) {
        throw e;
    } catch (Exception e) {
        QueryException qe = new QueryException(DatawaveErrorCode.CLOSE_ERROR, e, MessageFormat.format("query_id: {0}", id));
        log.error(qe, e);
        response.addException(qe.getBottomQueryException());
        int statusCode = qe.getBottomQueryException().getStatusCode();
        throw new DatawaveWebApplicationException(qe, response, statusCode);
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) VoidResponse(datawave.webservice.result.VoidResponse) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) QueryLogic(datawave.webservice.query.logic.QueryLogic) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) CancellationException(java.util.concurrent.CancellationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) WebApplicationException(javax.ws.rs.WebApplicationException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) 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) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) JAXBException(javax.xml.bind.JAXBException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) NoResultsException(datawave.webservice.common.exception.NoResultsException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RollbackException(javax.transaction.RollbackException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException)

Example 9 with VoidResponse

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

the class QueryExecutorBean method disableTracing.

/**
 * <strong>JBossAdministrator or Administrator credentials required.</strong> Disables tracing that was previously enabled using the
 * {@link #enableTracing(String, String)} method.
 *
 * @param queryRegex
 *            (optional) the query regular expression defining queries to disable tracing
 * @param user
 *            (optional) the user name for which to disable query tracing
 * @return datawave.webservice.result.VoidResponse
 *
 * @HTTP 200 success
 * @HTTP 400 if neither queryRegex nor user are specified
 * @HTTP 401 if the user does not have Administrative credentials
 */
@GET
@Path("/disableTracing")
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff" })
@RolesAllowed({ "Administrator", "JBossAdministrator" })
@Override
public VoidResponse disableTracing(@QueryParam("queryRegex") String queryRegex, @QueryParam("user") String user) {
    VoidResponse response = new VoidResponse();
    if (queryRegex == null && user == null) {
        BadRequestQueryException qe = new BadRequestQueryException(DatawaveErrorCode.QUERY_REGEX_OR_USER_REQUIRED);
        response.addException(qe);
        throw new BadRequestException(qe, response);
    } else if (queryRegex == null) {
        traceInfos.removeAll(user);
        response.addMessage("All query tracing for " + user + " is disabled.  Per-query tracing is still possible.");
    } else {
        traceInfos.remove(user, PatternWrapper.wrap(queryRegex));
        response.addMessage("Queries for user " + user + " matching " + queryRegex + " have been disabled. Per-query tracing is still possible.");
    }
    // Put updated map back in the cache
    queryTraceCache.put("traceInfos", traceInfos);
    return response;
}
Also used : BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) VoidResponse(datawave.webservice.result.VoidResponse) BadRequestException(datawave.webservice.common.exception.BadRequestException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 10 with VoidResponse

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

the class QueryExecutorBean method execute.

/**
 * @param logicName
 * @param queryParameters
 *
 * @return {@code datawave.webservice.result.GenericResponse<String>}
 * @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
 * @ResponseHeader X-Partial-Results true if the page contains less than the requested number of results
 *
 * @HTTP 200 success
 * @HTTP 204 success and no results
 * @HTTP 400 invalid or missing parameter
 * @HTTP 500 internal server error
 */
@POST
@Produces("*/*")
@Path("/{logicName}/execute")
@GZIP
@Interceptors({ ResponseInterceptor.class, RequiredInterceptor.class })
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@Override
@Timed(name = "dw.query.executeQuery", absolute = true)
public StreamingOutput execute(@PathParam("logicName") String logicName, MultivaluedMap<String, String> queryParameters, @Context HttpHeaders httpHeaders) {
    /**
     * This method captures the metrics on the query instead of doing it in the QueryMetricsEnrichmentInterceptor. The ExecuteStreamingOutputResponse class
     * is returned from this method and executed in the JAX-RS layer. It updates the metrics which are then updated on each call to the _next method.
     */
    Collection<String> proxyServers = null;
    Principal p = ctx.getCallerPrincipal();
    DatawavePrincipal dp;
    if (p instanceof DatawavePrincipal) {
        dp = (DatawavePrincipal) p;
        proxyServers = dp.getProxyServers();
    }
    final MediaType PB_MEDIA_TYPE = new MediaType("application", "x-protobuf");
    final MediaType YAML_MEDIA_TYPE = new MediaType("application", "x-yaml");
    final VoidResponse response = new VoidResponse();
    // HttpHeaders.getAcceptableMediaTypes returns a priority sorted list of acceptable response types.
    // Find the first one in the list that we support.
    MediaType responseType = null;
    for (MediaType type : httpHeaders.getAcceptableMediaTypes()) {
        if (type.equals(MediaType.APPLICATION_XML_TYPE) || type.equals(MediaType.APPLICATION_JSON_TYPE) || type.equals(PB_MEDIA_TYPE) || type.equals(YAML_MEDIA_TYPE)) {
            responseType = type;
            break;
        }
    }
    if (null == responseType) {
        QueryException qe = new QueryException(DatawaveErrorCode.UNSUPPORTED_MEDIA_TYPE);
        response.setHasResults(false);
        response.addException(qe);
        throw new DatawaveWebApplicationException(qe, response, MediaType.APPLICATION_XML_TYPE);
    }
    // reference query necessary to avoid NPEs in getting the Transformer and BaseResponse
    Query q = new QueryImpl();
    Date now = new Date();
    q.setBeginDate(now);
    q.setEndDate(now);
    q.setExpirationDate(now);
    q.setQuery("test");
    q.setQueryAuthorizations("ALL");
    ResultsPage emptyList = new ResultsPage();
    // Find the response class
    Class<?> responseClass;
    try {
        QueryLogic<?> l = queryLogicFactory.getQueryLogic(logicName, p);
        QueryLogicTransformer t = l.getTransformer(q);
        BaseResponse refResponse = t.createResponse(emptyList);
        responseClass = refResponse.getClass();
    } catch (Exception e) {
        QueryException qe = new QueryException(DatawaveErrorCode.QUERY_TRANSFORM_ERROR, e);
        log.error(qe, e);
        response.setHasResults(false);
        response.addException(qe.getBottomQueryException());
        int statusCode = qe.getBottomQueryException().getStatusCode();
        throw new DatawaveWebApplicationException(qe, response, statusCode, MediaType.APPLICATION_XML_TYPE);
    }
    SerializationType s;
    if (responseType.equals(MediaType.APPLICATION_XML_TYPE)) {
        s = SerializationType.XML;
    } else if (responseType.equals(MediaType.APPLICATION_JSON_TYPE)) {
        s = SerializationType.JSON;
    } else if (responseType.equals(PB_MEDIA_TYPE)) {
        if (!(Message.class.isAssignableFrom(responseClass))) {
            QueryException qe = new QueryException(DatawaveErrorCode.BAD_RESPONSE_CLASS, MessageFormat.format("Response  class: {0}", responseClass));
            response.setHasResults(false);
            response.addException(qe);
            throw new DatawaveWebApplicationException(qe, response, MediaType.APPLICATION_XML_TYPE);
        }
        s = SerializationType.PB;
    } else if (responseType.equals(YAML_MEDIA_TYPE)) {
        if (!(Message.class.isAssignableFrom(responseClass))) {
            QueryException qe = new QueryException(DatawaveErrorCode.BAD_RESPONSE_CLASS, MessageFormat.format("Response  class: {0}", responseClass));
            response.setHasResults(false);
            response.addException(qe);
            throw new DatawaveWebApplicationException(qe, response, MediaType.APPLICATION_XML_TYPE);
        }
        s = SerializationType.YAML;
    } else {
        QueryException qe = new QueryException(DatawaveErrorCode.INVALID_FORMAT, MessageFormat.format("format: {0}", responseType.toString()));
        response.setHasResults(false);
        response.addException(qe);
        throw new DatawaveWebApplicationException(qe, response, MediaType.APPLICATION_XML_TYPE);
    }
    long start = System.nanoTime();
    GenericResponse<String> createResponse = null;
    try {
        createResponse = this.createQuery(logicName, queryParameters, httpHeaders);
    } catch (Throwable t) {
        if (t instanceof DatawaveWebApplicationException) {
            QueryException qe = (QueryException) ((DatawaveWebApplicationException) t).getCause();
            response.setHasResults(false);
            response.addException(qe.getBottomQueryException());
            int statusCode = qe.getBottomQueryException().getStatusCode();
            throw new DatawaveWebApplicationException(qe, response, statusCode, MediaType.APPLICATION_XML_TYPE);
        } else {
            throw t;
        }
    }
    long createCallTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
    final String queryId = createResponse.getResult();
    // We created the query and put into cache, get the RunningQuery object
    final RunningQuery rq = queryCache.get(queryId);
    rq.getMetric().setCreateCallTime(createCallTime);
    final Collection<String> proxies = proxyServers;
    final SerializationType serializationType = s;
    final Class<?> queryResponseClass = responseClass;
    return new ExecuteStreamingOutputResponse(queryId, queryResponseClass, response, rq, serializationType, proxies);
}
Also used : Query(datawave.webservice.query.Query) Message(io.protostuff.Message) ResultsPage(datawave.webservice.query.cache.ResultsPage) QueryLogicTransformer(datawave.webservice.query.logic.QueryLogicTransformer) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Date(java.util.Date) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) CancellationException(java.util.concurrent.CancellationException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) WebApplicationException(javax.ws.rs.WebApplicationException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) 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) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) JAXBException(javax.xml.bind.JAXBException) UnauthorizedException(datawave.webservice.common.exception.UnauthorizedException) NoResultsException(datawave.webservice.common.exception.NoResultsException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RollbackException(javax.transaction.RollbackException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) BaseResponse(datawave.webservice.result.BaseResponse) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) QueryImpl(datawave.webservice.query.QueryImpl) VoidResponse(datawave.webservice.result.VoidResponse) MediaType(javax.ws.rs.core.MediaType) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Path(javax.ws.rs.Path) Interceptors(javax.interceptor.Interceptors) TransactionAttribute(javax.ejb.TransactionAttribute) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GZIP(org.jboss.resteasy.annotations.GZIP)

Aggregations

VoidResponse (datawave.webservice.result.VoidResponse)51 QueryException (datawave.webservice.query.exception.QueryException)25 Produces (javax.ws.rs.Produces)25 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)21 Path (javax.ws.rs.Path)21 GZIP (org.jboss.resteasy.annotations.GZIP)21 Interceptors (javax.interceptor.Interceptors)17 BadRequestException (datawave.webservice.common.exception.BadRequestException)15 NoResultsException (datawave.webservice.common.exception.NoResultsException)15 Test (org.junit.Test)15 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)14 IOException (java.io.IOException)13 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)12 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)12 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)12 Connector (org.apache.accumulo.core.client.Connector)12 QueryMetricFactoryImpl (datawave.microservice.querymetric.QueryMetricFactoryImpl)11 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)11 GET (javax.ws.rs.GET)11 WebApplicationException (javax.ws.rs.WebApplicationException)11