use of datawave.annotation.GenerateQuerySessionId in project datawave by NationalSecurityAgency.
the class CachedResultsBean method loadAndCreate.
/**
* @param queryParameters
*
* @return datawave.webservice.result.CachedResultsResponse
* @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
*
* @HTTP 200 success
* @HTTP 500 internal server error
*/
@POST
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml" })
@javax.ws.rs.Path("/{queryId}/loadAndCreate")
@Interceptors(RequiredInterceptor.class)
@GenerateQuerySessionId(cookieBasePath = "/DataWave/CachedResults/")
@Timed(name = "dw.cachedr.loadAndCreate", absolute = true)
public CachedResultsResponse loadAndCreate(@Required("queryId") @PathParam("queryId") String queryId, MultivaluedMap<String, String> queryParameters) {
CreateQuerySessionIDFilter.QUERY_ID.set(null);
String newQueryId = queryParameters.getFirst("newQueryId");
Preconditions.checkNotNull(newQueryId, "newQueryId cannot be null");
Preconditions.checkNotNull(queryId, "queryId cannot be null");
queryParameters.putSingle(CachedResultsParameters.QUERY_ID, queryId);
String alias = queryParameters.getFirst(CachedResultsParameters.ALIAS);
// Find out who/what called this method
Principal p = ctx.getCallerPrincipal();
String owner = getOwnerFromPrincipal(p);
GenericResponse<String> r = null;
try {
r = load(queryId, alias);
} catch (DatawaveWebApplicationException e) {
if (e instanceof NoResultsException == false) {
if (e.getCause() instanceof QueryCanceledException) {
try {
persistByQueryId(newQueryId, alias, owner, CachedRunningQuery.Status.CANCELED, "query canceled", true);
} catch (IOException e1) {
log.error("Error persisting state to CachedResults store", e1);
}
throw e;
} else {
String statusMessage = e.getCause().getMessage();
if (null == statusMessage) {
statusMessage = e.getClass().getName();
}
try {
persistByQueryId(newQueryId, alias, owner, CachedRunningQuery.Status.ERROR, statusMessage, true);
} catch (IOException e1) {
log.error("Error persisting state to CachedResults store", e1);
}
throw e;
}
} else if (e.getResponse().getEntity() == null) {
// NoResultsException can't contain the response object, otherwise we'll return invalid HTML. So, instead, pull the ID from the
// NoResultsException and make a new GenericResponse here.
r = new GenericResponse<>();
r.setResult(((NoResultsException) e).getId());
}
if (e.getResponse().getEntity() instanceof GenericResponse<?>) {
@SuppressWarnings("unchecked") GenericResponse<String> gr = (GenericResponse<String>) e.getResponse().getEntity();
r = gr;
} else if (r == null) {
throw e;
}
} catch (RuntimeException e) {
log.error(e.getMessage(), e);
throw e;
}
String view = r.getResult();
// pagesize validated in create
CreateQuerySessionIDFilter.QUERY_ID.set(newQueryId);
queryParameters.remove(CachedResultsParameters.QUERY_ID);
queryParameters.remove(CachedResultsParameters.VIEW);
queryParameters.putSingle(CachedResultsParameters.VIEW, view);
CachedResultsResponse response = create(newQueryId, queryParameters);
try {
persistByQueryId(newQueryId, alias, owner, CachedRunningQuery.Status.AVAILABLE, "", true);
} catch (IOException e) {
QueryException qe = new QueryException(DatawaveErrorCode.CACHE_PERSISTANCE_ERROR, e);
response.addException(qe);
throw new DatawaveWebApplicationException(e, response);
}
return response;
}
use of datawave.annotation.GenerateQuerySessionId in project datawave by NationalSecurityAgency.
the class CreateQuerySessionIDFilter method filter.
@Override
public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException {
ResourceMethodInvoker method = (ResourceMethodInvoker) request.getProperty(ResourceMethodInvoker.class.getName());
GenerateQuerySessionId annotation = FindAnnotation.findAnnotation(method.getMethodAnnotations(), GenerateQuerySessionId.class);
String path = annotation.cookieBasePath();
String id = "";
String cookieValue = generateCookieValue();
boolean setCookie = true;
switch(response.getStatusInfo().getFamily()) {
case SERVER_ERROR:
case CLIENT_ERROR:
// If we're sending an error response, then there's no need to set a cookie since
// there's no query "session" to stick to this server.
setCookie = false;
QUERY_ID.set(null);
break;
default:
if (StringUtils.isEmpty(QUERY_ID.get())) {
log.error(method.getResourceClass() + "." + method.getMethod().getName() + " did not set QUERY_ID threadlocal.");
} else {
id = QUERY_ID.get();
QUERY_ID.set(null);
}
break;
}
if (setCookie) {
response.getHeaders().add(HttpHeaderNames.SET_COOKIE, new NewCookie(Constants.QUERY_COOKIE_NAME, cookieValue, path + id, null, null, NewCookie.DEFAULT_MAX_AGE, false));
}
}
use of datawave.annotation.GenerateQuerySessionId in project datawave by NationalSecurityAgency.
the class CreateQuerySessionIDFilterTest method setUp.
@Before
public void setUp() throws Exception {
annotation = new GenerateQuerySessionId() {
@Override
public String cookieBasePath() {
return "/test/path/";
}
@Override
public Class<? extends Annotation> annotationType() {
return GenerateQuerySessionId.class;
}
};
request = new ResponseContainerRequestContext(MockHttpRequest.post("/mock"));
request.setProperty(ResourceMethodInvoker.class.getName(), method);
response = new ContainerResponseContextImpl(request.getHttpRequest(), new MockHttpResponse(), new BuiltResponse());
filter = new CreateQuerySessionIDFilter();
}
Aggregations