Search in sources :

Example 91 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class Request method doGetSession.

// ------------------------------------------------------ Protected Methods
protected Session doGetSession(boolean create) {
    // There cannot be a session if no context has been assigned yet
    Context context = getContext();
    if (context == null) {
        return (null);
    }
    // Return the current session if it exists and is valid
    if ((session != null) && !session.isValid()) {
        session = null;
    }
    if (session != null) {
        return (session);
    }
    // Return the requested session if it exists and is valid
    Manager manager = context.getManager();
    if (manager == null) {
        // Sessions are not supported
        return (null);
    }
    if (requestedSessionId != null) {
        try {
            session = manager.findSession(requestedSessionId);
        } catch (IOException e) {
            session = null;
        }
        if ((session != null) && !session.isValid()) {
            session = null;
        }
        if (session != null) {
            session.access();
            return (session);
        }
    }
    // Create a new session if requested and the response is not committed
    if (!create) {
        return (null);
    }
    if (response != null && context.getServletContext().getEffectiveSessionTrackingModes().contains(SessionTrackingMode.COOKIE) && response.getResponse().isCommitted()) {
        throw new IllegalStateException(sm.getString("coyoteRequest.sessionCreateCommitted"));
    }
    // Re-use session IDs provided by the client in very limited
    // circumstances.
    String sessionId = getRequestedSessionId();
    if (requestedSessionSSL) {
    // If the session ID has been obtained from the SSL handshake then
    // use it.
    } else if (("/".equals(context.getSessionCookiePath()) && isRequestedSessionIdFromCookie())) {
        /* This is the common(ish) use case: using the same session ID with
             * multiple web applications on the same host. Typically this is
             * used by Portlet implementations. It only works if sessions are
             * tracked via cookies. The cookie must have a path of "/" else it
             * won't be provided for requests to all web applications.
             *
             * Any session ID provided by the client should be for a session
             * that already exists somewhere on the host. Check if the context
             * is configured for this to be confirmed.
             */
        if (context.getValidateClientProvidedNewSessionId()) {
            boolean found = false;
            for (Container container : getHost().findChildren()) {
                Manager m = ((Context) container).getManager();
                if (m != null) {
                    try {
                        if (m.findSession(sessionId) != null) {
                            found = true;
                            break;
                        }
                    } catch (IOException e) {
                    // Ignore. Problems with this manager will be
                    // handled elsewhere.
                    }
                }
            }
            if (!found) {
                sessionId = null;
            }
        }
    } else {
        sessionId = null;
    }
    session = manager.createSession(sessionId);
    // Creating a new session cookie based on that session
    if (session != null && context.getServletContext().getEffectiveSessionTrackingModes().contains(SessionTrackingMode.COOKIE)) {
        Cookie cookie = ApplicationSessionCookieConfig.createSessionCookie(context, session.getIdInternal(), isSecure());
        response.addSessionCookieInternal(cookie);
    }
    if (session == null) {
        return null;
    }
    session.access();
    return session;
}
Also used : ServletRequestContext(org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext) AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) ServerCookie(org.apache.tomcat.util.http.ServerCookie) Cookie(javax.servlet.http.Cookie) Container(org.apache.catalina.Container) IOException(java.io.IOException) StringManager(org.apache.tomcat.util.res.StringManager) Manager(org.apache.catalina.Manager) InstanceManager(org.apache.tomcat.InstanceManager)

Example 92 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class Request method login.

/**
     * {@inheritDoc}
     */
@Override
public void login(String username, String password) throws ServletException {
    if (getAuthType() != null || getRemoteUser() != null || getUserPrincipal() != null) {
        throw new ServletException(sm.getString("coyoteRequest.alreadyAuthenticated"));
    }
    Context context = getContext();
    if (context.getAuthenticator() == null) {
        throw new ServletException("no authenticator");
    }
    context.getAuthenticator().login(username, password, this);
}
Also used : ServletException(javax.servlet.ServletException) ServletRequestContext(org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext) AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext)

Example 93 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class Request method notifyAttributeRemoved.

/**
     * Notify interested listeners that attribute has been removed.
     *
     * @param name Attribute name
     * @param value Attribute value
     */
private void notifyAttributeRemoved(String name, Object value) {
    Context context = getContext();
    Object[] listeners = context.getApplicationEventListeners();
    if ((listeners == null) || (listeners.length == 0)) {
        return;
    }
    ServletRequestAttributeEvent event = new ServletRequestAttributeEvent(context.getServletContext(), getRequest(), name, value);
    for (int i = 0; i < listeners.length; i++) {
        if (!(listeners[i] instanceof ServletRequestAttributeListener)) {
            continue;
        }
        ServletRequestAttributeListener listener = (ServletRequestAttributeListener) listeners[i];
        try {
            listener.attributeRemoved(event);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            // Error valve will pick this exception up and display it to user
            attributes.put(RequestDispatcher.ERROR_EXCEPTION, t);
            context.getLogger().error(sm.getString("coyoteRequest.attributeEvent"), t);
        }
    }
}
Also used : ServletRequestContext(org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext) AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) ServletRequestAttributeEvent(javax.servlet.ServletRequestAttributeEvent)

Example 94 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class WebappLoader method setContext.

@Override
public void setContext(Context context) {
    if (this.context == context) {
        return;
    }
    if (getState().isAvailable()) {
        throw new IllegalStateException(sm.getString("webappLoader.setContext.ise"));
    }
    // Deregister from the old Context (if any)
    if (this.context != null) {
        this.context.removePropertyChangeListener(this);
    }
    // Process this property change
    Context oldContext = this.context;
    this.context = context;
    support.firePropertyChange("context", oldContext, this.context);
    // Register with the new Container (if any)
    if (this.context != null) {
        setReloadable(this.context.getReloadable());
        this.context.addPropertyChangeListener(this);
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext)

Example 95 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class ContextMBean method findApplicationParameters.

/**
     * Return the set of application parameters for this application.
     * @return a string array with a representation of each parameter
     * @throws MBeanException propagated from the managed resource access
     */
public String[] findApplicationParameters() throws MBeanException {
    Context context = doGetManagedResource();
    ApplicationParameter[] params = context.findApplicationParameters();
    String[] stringParams = new String[params.length];
    for (int counter = 0; counter < params.length; counter++) {
        stringParams[counter] = params[counter].toString();
    }
    return stringParams;
}
Also used : Context(org.apache.catalina.Context) ApplicationParameter(org.apache.tomcat.util.descriptor.web.ApplicationParameter) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Aggregations

Context (org.apache.catalina.Context)376 Tomcat (org.apache.catalina.startup.Tomcat)212 Test (org.junit.Test)180 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)127 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)96 File (java.io.File)77 ServletContext (javax.servlet.ServletContext)74 AsyncContext (javax.servlet.AsyncContext)73 StandardContext (org.apache.catalina.core.StandardContext)65 Wrapper (org.apache.catalina.Wrapper)53 IOException (java.io.IOException)40 TesterContext (org.apache.tomcat.unittest.TesterContext)39 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)37 URI (java.net.URI)33 WebSocketContainer (javax.websocket.WebSocketContainer)32 Session (javax.websocket.Session)31 Host (org.apache.catalina.Host)30 Container (org.apache.catalina.Container)26 ArrayList (java.util.ArrayList)25 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24