Search in sources :

Example 1 with RequestWrapper

use of org.exist.http.servlets.RequestWrapper in project exist by eXist-db.

the class XMLDBAuthenticate method getSession.

/**
 * Get the HTTP Session. Create it if requested and it doesn't exist.
 *
 * @param createSession true to create a new session if one does not exist.
 *
 * @return the session if we could get or create it.
 */
private Optional<SessionWrapper> getSession(final boolean createSession) throws XPathException {
    final Optional<SessionWrapper> maybeExistingSession = Optional.ofNullable(context.getHttpContext()).map(XQueryContext.HttpContext::getSession);
    if (maybeExistingSession.isPresent() || !createSession) {
        if (!createSession) {
            return maybeExistingSession;
        }
        if (!maybeExistingSession.get().isInvalid()) {
            return maybeExistingSession;
        }
    }
    final RequestWrapper request = Optional.ofNullable(context.getHttpContext()).map(XQueryContext.HttpContext::getRequest).orElseThrow(() -> new XPathException(this, ErrorCodes.XPDY0002, "No request object found in the current XQuery context."));
    final Optional<SessionWrapper> newSession = Optional.ofNullable(request.getSession(true));
    newSession.ifPresent(session -> context.setHttpContext(context.getHttpContext().setSession(session)));
    return newSession;
}
Also used : RequestWrapper(org.exist.http.servlets.RequestWrapper) SessionWrapper(org.exist.http.servlets.SessionWrapper)

Example 2 with RequestWrapper

use of org.exist.http.servlets.RequestWrapper in project exist by eXist-db.

the class ProcessMonitor method getRequestURI.

/**
 * Try to figure out the HTTP request URI by which a query was called.
 * Request tracking is not enabled unless {@link #setTrackRequestURI(boolean)}
 * is called.
 *
 * @param watchdog XQuery WatchDog
 * @return HTTP request URI by which a query was called
 */
public static String getRequestURI(final XQueryWatchDog watchdog) {
    final Module[] modules = watchdog.getContext().getModules(RequestModule.NAMESPACE_URI);
    if (isEmpty(modules)) {
        return null;
    }
    final Optional<RequestWrapper> maybeRequest = Optional.ofNullable(watchdog.getContext()).map(XQueryContext::getHttpContext).map(XQueryContext.HttpContext::getRequest);
    if (!maybeRequest.isPresent()) {
        return null;
    }
    final RequestWrapper request = maybeRequest.get();
    final Object attr = request.getAttribute(XQueryURLRewrite.RQ_ATTR_REQUEST_URI);
    String uri;
    if (attr == null) {
        uri = request.getRequestURI();
    } else {
        uri = attr.toString();
    }
    String queryString = request.getQueryString();
    if (queryString != null) {
        uri += "?" + queryString;
    }
    return uri;
}
Also used : RequestWrapper(org.exist.http.servlets.RequestWrapper) Module(org.exist.xquery.Module) RequestModule(org.exist.xquery.functions.request.RequestModule)

Example 3 with RequestWrapper

use of org.exist.http.servlets.RequestWrapper in project exist by eXist-db.

the class SessionFunction method getOrCreateSession.

private static SessionWrapper getOrCreateSession(final Expression expr, final XQueryContext context, final Optional<SessionWrapper> session, final boolean sessionMustBeValid) throws XPathException {
    if (session.isPresent()) {
        final SessionWrapper existingSession = session.get();
        if (sessionMustBeValid) {
            if (existingSession.isInvalid()) {
                LOG.warn("Existing HTTP Session was invalid, creating a new HTTP Session");
            } else {
                return existingSession;
            }
        } else {
            return existingSession;
        }
    }
    final RequestWrapper request = Optional.ofNullable(context.getHttpContext()).map(XQueryContext.HttpContext::getRequest).orElseThrow(() -> new XPathException(expr, ErrorCodes.XPDY0002, "No request object found in the current XQuery context."));
    final SessionWrapper newSession = request.getSession(true);
    context.setHttpContext(context.getHttpContext().setSession(newSession));
    return newSession;
}
Also used : RequestWrapper(org.exist.http.servlets.RequestWrapper) SessionWrapper(org.exist.http.servlets.SessionWrapper)

Aggregations

RequestWrapper (org.exist.http.servlets.RequestWrapper)3 SessionWrapper (org.exist.http.servlets.SessionWrapper)2 Module (org.exist.xquery.Module)1 RequestModule (org.exist.xquery.functions.request.RequestModule)1