Search in sources :

Example 1 with MessageBytes

use of org.glassfish.grizzly.http.util.MessageBytes in project Payara by payara.

the class StandardContext method getRequestDispatcher.

/**
 * Return a <code>RequestDispatcher</code> instance that acts as a
 * wrapper for the resource at the given path.  The path must begin
 * with a "/" and is interpreted as relative to the current context root.
 */
@Override
public RequestDispatcher getRequestDispatcher(String path) {
    // Validate the path argument
    if (path == null) {
        return null;
    }
    if (!path.startsWith("/") && !path.isEmpty()) {
        String msg = MessageFormat.format(rb.getString(LogFacade.INCORRECT_OR_NOT_EMPTY_PATH), path);
        throw new IllegalArgumentException(msg);
    }
    // Get query string
    String queryString = null;
    int pos = path.indexOf('?');
    if (pos >= 0) {
        queryString = path.substring(pos + 1);
        path = path.substring(0, pos);
    }
    path = RequestUtil.normalize(path);
    if (path == null)
        return (null);
    pos = path.length();
    // Use the thread local URI and mapping data
    DispatchData dd = dispatchData.get();
    if (dd == null) {
        dd = new DispatchData();
        dispatchData.set(dd);
    }
    MessageBytes uriMB = dd.uriMB;
    uriMB.recycle();
    // Retrieve the thread local mapping data
    MappingData mappingData = dd.mappingData;
    // Map the URI
    CharChunk uriCC = uriMB.getCharChunk();
    try {
        uriCC.append(getPath(), 0, getPath().length());
        /*
             * Ignore any trailing path params (separated by ';') for mapping
             * purposes
             */
        int semicolon = path.indexOf(';');
        if (pos >= 0 && semicolon > pos) {
            semicolon = -1;
        }
        uriCC.append(path, 0, semicolon > 0 ? semicolon : pos);
        getMapper().map(uriMB, mappingData);
        if (mappingData.wrapper == null) {
            return (null);
        }
        /*
             * Append any trailing path params (separated by ';') that were
             * ignored for mapping purposes, so that they're reflected in the
             * RequestDispatcher's requestURI
             */
        if (semicolon > 0) {
            uriCC.append(path, semicolon, pos - semicolon);
        }
    } catch (Exception e) {
        // Should never happen
        log.log(Level.WARNING, LogFacade.MAPPING_ERROR_EXCEPTION, e);
        return (null);
    }
    Wrapper wrapper = (Wrapper) mappingData.wrapper;
    String wrapperPath = mappingData.wrapperPath.toString();
    String pathInfo = mappingData.pathInfo.toString();
    HttpServletMapping mappingForDispatch = new MappingImpl(mappingData);
    mappingData.recycle();
    // Construct a RequestDispatcher to process this request
    return new ApplicationDispatcher(wrapper, mappingForDispatch, uriCC.toString(), wrapperPath, pathInfo, queryString, null);
}
Also used : Wrapper(org.apache.catalina.Wrapper) MessageBytes(org.glassfish.grizzly.http.util.MessageBytes) MappingData(org.glassfish.grizzly.http.server.util.MappingData) MappingImpl(org.apache.catalina.connector.MappingImpl) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint) CharChunk(org.glassfish.grizzly.http.util.CharChunk) LifecycleException(org.apache.catalina.LifecycleException) MalformedObjectNameException(javax.management.MalformedObjectNameException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) NamingException(javax.naming.NamingException) MBeanRegistrationException(javax.management.MBeanRegistrationException) MalformedURLException(java.net.MalformedURLException) HttpServletMapping(javax.servlet.http.HttpServletMapping)

Example 2 with MessageBytes

use of org.glassfish.grizzly.http.util.MessageBytes in project Payara by payara.

the class FormAuthenticator method authenticate.

// ------------------------------------------------------- Public Methods
/**
 * Authenticate the user making this request, based on the specified
 * login configuration.  Return <code>true</code> if any specified
 * constraint has been satisfied, or <code>false</code> if we have
 * created a response challenge already.
 *
 * @param request Request we are processing
 * @param response Response we are creating
 * @param config Login configuration describing how authentication
 * should be performed
 *
 * @exception IOException if an input/output error occurs
 */
@Override
public boolean authenticate(HttpRequest request, HttpResponse response, LoginConfig config) throws IOException {
    // References to objects we will need later
    HttpServletRequest hreq = (HttpServletRequest) request.getRequest();
    HttpServletResponse hres = (HttpServletResponse) response.getResponse();
    Session session = null;
    String contextPath = hreq.getContextPath();
    String requestURI = request.getDecodedRequestURI();
    // Is this the action request from the login page?
    boolean loginAction = requestURI.startsWith(contextPath) && requestURI.endsWith(Constants.FORM_ACTION);
    // Have we already authenticated someone?
    Principal principal = hreq.getUserPrincipal();
    // processing section of this method.
    if (principal != null && !loginAction) {
        if (log.isLoggable(Level.FINE))
            log.log(Level.FINE, "Already authenticated '" + principal.getName() + "'");
        String ssoId = (String) request.getNote(Constants.REQ_SSOID_NOTE);
        if (ssoId != null) {
            getSession(request, true);
        }
        return (true);
    }
    // processing section of this method.
    if (!cache && !loginAction) {
        session = getSession(request, true);
        if (log.isLoggable(Level.FINE))
            log.log(Level.FINE, "Checking for reauthenticate in session " + session);
        String username = (String) session.getNote(Constants.SESS_USERNAME_NOTE);
        char[] password = (char[]) session.getNote(Constants.SESS_PASSWORD_NOTE);
        if ((username != null) && (password != null)) {
            if (log.isLoggable(Level.FINE))
                log.log(Level.FINE, "Reauthenticating username '" + username + "'");
            principal = context.getRealm().authenticate(username, password);
            if (principal != null) {
                session.setNote(Constants.FORM_PRINCIPAL_NOTE, principal);
                if (!matchRequest(request)) {
                    register(request, response, principal, Constants.FORM_METHOD, username, password);
                    return (true);
                }
            }
            if (log.isLoggable(Level.FINE))
                log.log(Level.FINE, "Reauthentication failed, proceed normally");
        }
    }
    // authentication?  If so, forward the *original* request instead.
    if (matchRequest(request)) {
        session = getSession(request, true);
        if (log.isLoggable(Level.FINE)) {
            String msg = "Restore request from session '" + session.getIdInternal() + "'";
            log.log(Level.FINE, msg);
        }
        principal = (Principal) session.getNote(Constants.FORM_PRINCIPAL_NOTE);
        register(request, response, principal, Constants.FORM_METHOD, (String) session.getNote(Constants.SESS_USERNAME_NOTE), (char[]) session.getNote(Constants.SESS_PASSWORD_NOTE));
        String ssoId = (String) request.getNote(Constants.REQ_SSOID_NOTE);
        if (ssoId != null) {
            associate(ssoId, getSsoVersion(request), session);
        }
        if (restoreRequest(request, session)) {
            if (log.isLoggable(Level.FINE))
                log.log(Level.FINE, "Proceed to restored request");
            return (true);
        } else {
            if (log.isLoggable(Level.FINE))
                log.log(Level.FINE, "Restore of original request failed");
            hres.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return (false);
        }
    }
    // Acquire references to objects we will need to evaluate
    MessageBytes uriMB = MessageBytes.newInstance();
    CharChunk uriCC = uriMB.getCharChunk();
    uriCC.setLimit(-1);
    response.setContext(request.getContext());
    // No -- Save this request and redirect to the form login page
    if (!loginAction) {
        session = getSession(request, true);
        if (log.isLoggable(Level.FINE)) {
            String msg = "Save request in session '" + session.getIdInternal() + "'";
            log.log(Level.FINE, msg);
        }
        saveRequest(request, session);
        // START Apache bug 36136: Refactor the login and error page forward
        /*
            RequestDispatcher disp =
                context.getServletContext().getRequestDispatcher
                (config.getLoginPage());
            try {
                disp.forward(hreq, hres);
                response.finishResponse();
            } catch (Throwable t) {
                log.warn("Unexpected error forwarding to login page", t);
            }
            */
        forwardToLoginPage(request, response, config);
        return (false);
    }
    // Yes -- Validate the specified credentials and redirect
    // to the error page if they are not correct
    Realm realm = context.getRealm();
    String username = hreq.getParameter(Constants.FORM_USERNAME);
    String pwd = hreq.getParameter(Constants.FORM_PASSWORD);
    char[] password = ((pwd != null) ? pwd.toCharArray() : null);
    if (log.isLoggable(Level.FINE))
        log.log(Level.FINE, "Authenticating username '" + username + "'");
    principal = realm.authenticate(username, password);
    if (principal == null) {
        // START Apache bug 36136: Refactor the login and error page forward
        /*
            RequestDispatcher disp =
                context.getServletContext().getRequestDispatcher
                (config.getErrorPage());
            try {
                disp.forward(hreq, hres);
            } catch (Throwable t) {
                log.warn("Unexpected error forwarding to error page", t);
            }
            */
        forwardToErrorPage(request, response, config);
        return (false);
    }
    // Save the authenticated Principal in our session
    if (log.isLoggable(Level.FINE))
        log.log(Level.FINE, "Authentication of '" + username + "' was successful");
    if (session == null)
        session = getSession(request, true);
    session.setNote(Constants.FORM_PRINCIPAL_NOTE, principal);
    // If we are not caching, save the username and password as well
    if (!cache) {
        session.setNote(Constants.SESS_USERNAME_NOTE, username);
        session.setNote(Constants.SESS_PASSWORD_NOTE, password);
    }
    // Redirect the user to the original request URI (which will cause
    // the original request to be restored)
    requestURI = savedRequestURL(session);
    if (requestURI == null) {
        // requestURI will be null if the login form is submitted
        // directly, i.e., if there has not been any original request
        // that was stored away before the redirect to the login form was
        // issued. In this case, assume that the original request has been
        // for the context root, and have the welcome page mechanism take
        // care of it
        requestURI = hreq.getContextPath() + "/";
        register(request, response, principal, Constants.FORM_METHOD, (String) session.getNote(Constants.SESS_USERNAME_NOTE), (char[]) session.getNote(Constants.SESS_PASSWORD_NOTE));
        String ssoId = (String) request.getNote(Constants.REQ_SSOID_NOTE);
        if (ssoId != null) {
            associate(ssoId, getSsoVersion(request), session);
        }
    }
    if (log.isLoggable(Level.FINE)) {
        log.log(Level.FINE, "Redirecting to original '" + requestURI + "'");
    }
    hres.sendRedirect(hres.encodeRedirectURL(requestURI));
    return (false);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) MessageBytes(org.glassfish.grizzly.http.util.MessageBytes) Realm(org.apache.catalina.Realm) Principal(java.security.Principal) CharChunk(org.glassfish.grizzly.http.util.CharChunk) Session(org.apache.catalina.Session)

Aggregations

CharChunk (org.glassfish.grizzly.http.util.CharChunk)2 MessageBytes (org.glassfish.grizzly.http.util.MessageBytes)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 Principal (java.security.Principal)1 MBeanRegistrationException (javax.management.MBeanRegistrationException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 NamingException (javax.naming.NamingException)1 ServletException (javax.servlet.ServletException)1 HttpServletMapping (javax.servlet.http.HttpServletMapping)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 LifecycleException (org.apache.catalina.LifecycleException)1 Realm (org.apache.catalina.Realm)1 Session (org.apache.catalina.Session)1 Wrapper (org.apache.catalina.Wrapper)1 MappingImpl (org.apache.catalina.connector.MappingImpl)1 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)1 MappingData (org.glassfish.grizzly.http.server.util.MappingData)1