Search in sources :

Example 11 with GenerateQuerySessionId

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;
}
Also used : NoResultsException(datawave.webservice.common.exception.NoResultsException) GenericResponse(datawave.webservice.result.GenericResponse) IOException(java.io.IOException) QueryCanceledException(datawave.webservice.common.exception.QueryCanceledException) PreConditionFailedQueryException(datawave.webservice.query.exception.PreConditionFailedQueryException) NotFoundQueryException(datawave.webservice.query.exception.NotFoundQueryException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryCanceledQueryException(datawave.webservice.query.exception.QueryCanceledQueryException) QueryException(datawave.webservice.query.exception.QueryException) UnauthorizedQueryException(datawave.webservice.query.exception.UnauthorizedQueryException) BadRequestQueryException(datawave.webservice.query.exception.BadRequestQueryException) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) CachedResultsResponse(datawave.webservice.result.CachedResultsResponse) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) GenerateQuerySessionId(datawave.annotation.GenerateQuerySessionId) Interceptors(javax.interceptor.Interceptors) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Example 12 with GenerateQuerySessionId

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));
    }
}
Also used : ResourceMethodInvoker(org.jboss.resteasy.core.ResourceMethodInvoker) GenerateQuerySessionId(datawave.annotation.GenerateQuerySessionId) NewCookie(javax.ws.rs.core.NewCookie)

Example 13 with GenerateQuerySessionId

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();
}
Also used : ResourceMethodInvoker(org.jboss.resteasy.core.ResourceMethodInvoker) GenerateQuerySessionId(datawave.annotation.GenerateQuerySessionId) ResponseContainerRequestContext(org.jboss.resteasy.core.interception.ResponseContainerRequestContext) ContainerResponseContextImpl(org.jboss.resteasy.core.interception.ContainerResponseContextImpl) BuiltResponse(org.jboss.resteasy.specimpl.BuiltResponse) Annotation(java.lang.annotation.Annotation) MockHttpResponse(org.jboss.resteasy.mock.MockHttpResponse) Before(org.junit.Before)

Aggregations

GenerateQuerySessionId (datawave.annotation.GenerateQuerySessionId)13 Timed (com.codahale.metrics.annotation.Timed)11 Interceptors (javax.interceptor.Interceptors)11 Produces (javax.ws.rs.Produces)11 POST (javax.ws.rs.POST)10 QueryException (datawave.webservice.query.exception.QueryException)8 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)7 NoResultsException (datawave.webservice.common.exception.NoResultsException)7 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)7 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)7 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)7 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)7 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)7 IOException (java.io.IOException)7 Path (javax.ws.rs.Path)7 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)6 GZIP (org.jboss.resteasy.annotations.GZIP)6 BadRequestException (datawave.webservice.common.exception.BadRequestException)5 MultivaluedMapImpl (org.jboss.resteasy.specimpl.MultivaluedMapImpl)4 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)3