Search in sources :

Example 1 with BaseResponse

use of datawave.webservice.result.BaseResponse 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)

Example 2 with BaseResponse

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

the class CompositeQueryLogic method getTransformer.

/**
 * Method used to check that the configuration is correct and to get the response class by QueryExecutorBean.listQueryLogic()
 */
@Override
public synchronized QueryLogicTransformer getTransformer(Query settings) {
    ResultsPage emptyList = new ResultsPage();
    Class<? extends BaseResponse> responseClass = null;
    List<QueryLogicTransformer> delegates = new ArrayList<>();
    for (BaseQueryLogic<?> logic : queryLogics) {
        QueryLogicTransformer t = logic.getTransformer(settings);
        delegates.add(t);
        BaseResponse refResponse = t.createResponse(emptyList);
        if (null == responseClass) {
            responseClass = refResponse.getClass();
        } else {
            if (!responseClass.equals(refResponse.getClass())) {
                throw new RuntimeException("All query logics must use transformers that return the same object type");
            }
        }
    }
    if (null == this.transformer) {
        this.transformer = new CompositeQueryLogicTransformer(delegates);
    }
    return this.transformer;
}
Also used : BaseResponse(datawave.webservice.result.BaseResponse) ArrayList(java.util.ArrayList) ResultsPage(datawave.webservice.query.cache.ResultsPage) QueryLogicTransformer(datawave.webservice.query.logic.QueryLogicTransformer)

Example 3 with BaseResponse

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

the class QueryLogic method getResponseClass.

default String getResponseClass(Query query) throws QueryException {
    try {
        QueryLogicTransformer t = this.getTransformer(query);
        BaseResponse refResponse = t.createResponse(new ResultsPage());
        return refResponse.getClass().getCanonicalName();
    } catch (RuntimeException e) {
        throw new QueryException(DatawaveErrorCode.QUERY_TRANSFORM_ERROR);
    }
}
Also used : BaseResponse(datawave.webservice.result.BaseResponse) QueryException(datawave.webservice.query.exception.QueryException) ResultsPage(datawave.webservice.query.cache.ResultsPage)

Example 4 with BaseResponse

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

the class ResponseInterceptor method filter.

@Override
public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException {
    if (null == ORIGIN) {
        ORIGIN = System.getProperty("cluster.name") + "/" + System.getProperty("jboss.host.name");
    }
    if (response.getEntity() instanceof BaseResponse) {
        BaseResponse br = (BaseResponse) response.getEntity();
        response.getHeaders().add(Constants.OPERATION_TIME, br.getOperationTimeMS());
    }
    if (response.getEntity() instanceof BaseQueryResponse) {
        BaseQueryResponse bqr = (BaseQueryResponse) response.getEntity();
        response.getHeaders().add(Constants.PAGE_NUMBER, bqr.getPageNumber());
        response.getHeaders().add(Constants.IS_LAST_PAGE, !bqr.getHasResults());
        response.getHeaders().add(Constants.PARTIAL_RESULTS, bqr.isPartialResults());
    }
    try {
        response.getHeaders().add(Constants.RESPONSE_ORIGIN, ORIGIN);
    } catch (UnsupportedOperationException e) {
    // this only happens when there's an error earlier in the chain
    }
}
Also used : BaseResponse(datawave.webservice.result.BaseResponse) BaseQueryResponse(datawave.webservice.result.BaseQueryResponse)

Example 5 with BaseResponse

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

the class ResponseInterceptor method invoke.

@AroundInvoke
public Object invoke(InvocationContext ctx) throws Exception {
    long start = System.currentTimeMillis();
    Object r = ctx.proceed();
    boolean isResponseObject = r instanceof Response;
    boolean isBaseResponseObject = r instanceof BaseResponse;
    // If response type is not BaseResponse or subclass, then move on
    if (!isResponseObject && !isBaseResponseObject)
        return r;
    // Invoke the method
    if (isResponseObject) {
        Response result = (Response) r;
        long end = System.currentTimeMillis();
        result.getHeaders().add(Constants.OPERATION_TIME, (end - start));
        if (result.getEntity() instanceof BaseResponse)
            ((BaseResponse) result.getEntity()).setOperationTimeMS((end - start));
        return result;
    } else {
        BaseResponse result;
        try {
            result = (BaseResponse) r;
        } catch (NoResultsException e) {
            e.setStartTime(start);
            e.setEndTime(System.currentTimeMillis());
            throw e;
        }
        long end = System.currentTimeMillis();
        result.setOperationTimeMS((end - start));
        return result;
    }
}
Also used : BaseQueryResponse(datawave.webservice.result.BaseQueryResponse) Response(javax.ws.rs.core.Response) BaseResponse(datawave.webservice.result.BaseResponse) NoResultsException(datawave.webservice.common.exception.NoResultsException) BaseResponse(datawave.webservice.result.BaseResponse) AroundInvoke(javax.interceptor.AroundInvoke)

Aggregations

BaseResponse (datawave.webservice.result.BaseResponse)5 ResultsPage (datawave.webservice.query.cache.ResultsPage)3 NoResultsException (datawave.webservice.common.exception.NoResultsException)2 QueryException (datawave.webservice.query.exception.QueryException)2 QueryLogicTransformer (datawave.webservice.query.logic.QueryLogicTransformer)2 BaseQueryResponse (datawave.webservice.result.BaseQueryResponse)2 Timed (com.codahale.metrics.annotation.Timed)1 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)1 BadRequestException (datawave.webservice.common.exception.BadRequestException)1 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)1 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)1 Query (datawave.webservice.query.Query)1 QueryImpl (datawave.webservice.query.QueryImpl)1 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)1 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)1 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)1 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)1 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)1 VoidResponse (datawave.webservice.result.VoidResponse)1 Message (io.protostuff.Message)1