001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.
018 */
019package org.apache.wiki.api.spi;
020
021import org.apache.wiki.api.core.Engine;
022import org.apache.wiki.api.core.Session;
023
024import javax.servlet.http.HttpServletRequest;
025
026
027/**
028 * SPI used to locate and provide {@link Session} instances.
029 */
030public class SessionDSL {
031
032    private final SessionSPI sessionSPI;
033
034    SessionDSL( final SessionSPI sessionSPI ) {
035        this.sessionSPI = sessionSPI;
036    }
037
038    /**
039     * Removes the wiki session associated with the user's HTTP request from the cache of wiki sessions, typically as part of a logout process.
040     *
041     * @param engine the wiki engine
042     * @param request the users's HTTP request
043     */
044    public void remove( final Engine engine, final HttpServletRequest request ) {
045        sessionSPI.remove( engine, request );
046    }
047
048    /**
049     * <p>Returns the Session object associated with the current HTTP request. If not found, one is created.
050     * This method is guaranteed to always return a Session, although the authentication status is unpredictable until the user
051     * attempts to log in. If the servlet request parameter is <code>null</code>, a synthetic {@link #guest(Engine)} is
052     * returned.</p>
053     * <p>When a session is created, this method attaches a WikiEventListener to the GroupManager, UserManager and AuthenticationManager,
054     * so that changes to users, groups, logins, etc. are detected automatically.</p>
055     *
056     * @param engine the engine
057     * @param request the servlet request object
058     * @return the existing (or newly created) session
059     */
060    public Session find( final Engine engine, final HttpServletRequest request ) {
061        return sessionSPI.find( engine, request );
062    }
063
064    /**
065     * Creates a new "guest" session containing a single user Principal {@code org.apache.wiki.auth.WikiPrincipal#GUEST}, plus the role
066     * principals {@code Role#ALL} and {@code Role#ANONYMOUS}. This method also adds the session as a listener for GroupManager,
067     * AuthenticationManager and UserManager events.
068     *
069     * @param engine the wiki engine
070     * @return the guest wiki session
071     */
072    public Session guest( final Engine engine ) {
073        return sessionSPI.guest( engine );
074    }
075
076}