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);
}
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;
}
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);
}
}
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
}
}
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;
}
}
Aggregations