001/*
002 * GWTEventService
003 * Copyright (c) 2011 and beyond, strawbill UG (haftungsbeschr?nkt)
004 *
005 * This is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU Lesser General Public License as
007 * published by the Free Software Foundation; either version 3 of
008 * the License, or (at your option) any later version.
009 * Other licensing for GWTEventService may also be possible on request.
010 * Please view the license.txt of the project for more information.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022package de.novanic.eventservice.service.connection.id;
023
024import de.novanic.eventservice.client.config.ConfigurationException;
025
026import javax.servlet.http.HttpServletRequest;
027import java.util.Random;
028
029/**
030 * A {@link de.novanic.eventservice.service.connection.id.ConnectionIdGenerator} generates unique ids which are used to
031 * identify the connected clients / users.
032 *
033 * That implementation of {@link de.novanic.eventservice.service.connection.id.ConnectionIdGenerator} uses and extends the
034 * session ids to generate a unique connection / client id. That allows the user / client to use more than one browser
035 * instance for the same application (multiple session ids / session sharing).
036 *
037 * @author sstrohschein
038 *         <br>Date: 28.03.2010
039 *         <br>Time: 23:34:39
040 */
041public class SessionExtendedConnectionIdGenerator implements ConnectionIdGenerator
042{
043    private Random myRandomizer;
044
045    /**
046     * Creates a new instance of {@link de.novanic.eventservice.service.connection.id.SessionExtendedConnectionIdGenerator}
047     * and initializes the random number generation.
048     */
049    public SessionExtendedConnectionIdGenerator() {
050        myRandomizer = new Random();
051    }
052
053    /**
054     * Generates a new connection / client id.
055     * The {@link de.novanic.eventservice.service.connection.id.SessionExtendedConnectionIdGenerator} creates a new session with
056     * that call when no session is available. The session id will be extended with a random number to support multiple session ids / session sharing.
057     * @param aRequest request from the client
058     * @return unique connection / client id
059     */
060    public String generateConnectionId(HttpServletRequest aRequest) {
061        return aRequest.getSession(true).getId() + myRandomizer.nextInt();
062    }
063
064    /**
065     * Returns the previous generated connection / client id
066     * (see {@link de.novanic.eventservice.service.connection.id.ConnectionIdGenerator#generateConnectionId(javax.servlet.http.HttpServletRequest)})
067     * without generating a new connection / client id.
068     * @see de.novanic.eventservice.service.connection.id.ConnectionIdGenerator#generateConnectionId(javax.servlet.http.HttpServletRequest)
069     * @param aRequest request
070     * @return the previous generated connection / client id for the specific client
071     */
072    public String getConnectionId(HttpServletRequest aRequest) {
073        final String theConnectionId = aRequest.getParameter("id");
074        if(theConnectionId == null) {
075            throw new ConfigurationException("A client id was requested without generating a connection id first or the connection id was not transferred with the request!");
076        }
077        return theConnectionId;
078    }
079}