/*
 * $Id$ $Revision:
 * 1.1 $ $Date$
 * 
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package wicket.protocol.http;

import javax.servlet.http.HttpServletRequest;

import wicket.Application;
import wicket.Session;
import wicket.WicketRuntimeException;

/**
 * Session subclass for HTTP protocol which holds an underlying WebSession
 * object and provides access to that object via getHttpServletSession. A method
 * which abstracts session invalidation is also provided via invalidate().
 * 
 * @author Jonathan Locke
 */
public class WebSession extends Session
{
	/** Serial Version ID */
	private static final long serialVersionUID = -7738551549126761943L;

	/** The underlying WebSession object */
	private transient javax.servlet.http.HttpSession httpSession;

	/**
	 * Gets session from request, creating a new one if it doesn't already exist
	 * 
	 * @param application
	 *            The application object
	 * @param request
	 *            The http request object
	 * @return The session object
	 */
	static WebSession getSession(final Application application, final HttpServletRequest request)
	{
		// Get session, creating if it doesn't exist
		final javax.servlet.http.HttpSession httpServletSession = request.getSession(true);

		// The request session object is unique per web application, but wicket
		// requires it
		// to be unique per servlet. That is, there must be a 1..n relationship
		// between
		// HTTP sessions (JSESSIONID) and Wicket applications.
		final String sessionAttributeName = "session" + request.getServletPath();

		// Get Session abstraction from httpSession attribute
		WebSession httpSession = (WebSession)httpServletSession.getAttribute(sessionAttributeName);

		if (httpSession == null)
		{
			// Create session using session factory
			final Session session = application.getSessionFactory().newSession();
			if (session instanceof WebSession)
			{
				httpSession = (WebSession)session;
			}
			else
			{
				throw new WicketRuntimeException(
						"Session created by a WebApplication session factory must be a subclass of WebSession");
			}

			// Save servlet session in there
			httpSession.httpSession = httpServletSession;

			// Set the client Locale for this session
			httpSession.setLocale(request.getLocale());

			// Attach to httpSession
			httpServletSession.setAttribute(sessionAttributeName, httpSession);
		}
		else
		{
			// Reattach http servlet session
			httpSession.httpSession = httpServletSession;

			// In a clustered environment the session is not replicated
			// if it is not dirty. If we just read the http session object
			// and manipulate that then the http servlet session never gets
			// flagged as being dirty. We therefore need to force a
			// change on the http servlet session to ensure clustering
			// occurs.
			httpServletSession.setAttribute(sessionAttributeName, httpSession);
		}

		// Set the current session to the session we just retrieved
		Session.set(httpSession);

		return httpSession;
	}

	/**
	 * Constructor
	 * 
	 * @param application
	 *            The application
	 */
	protected WebSession(final Application application)
	{
		super(application);
	}

	/**
	 * @return The underlying WebSession object
	 */
	public javax.servlet.http.HttpSession getHttpSession()
	{
		return httpSession;
	}

	/**
	 * Invalidates this session
	 */
	public void invalidate()
	{
		try
		{
			httpSession.invalidate();
		}
		catch (IllegalStateException e)
		{
			; // ignore
		}
	}
}

@@ -1918,21 +1918,16 @@
 uires it
-%0A%09%09//
  to be u
@@ -1975,16 +1975,22 @@
  a 1..n 
+%0A%09%09// 
 relation
@@ -1997,21 +1997,16 @@
 ship
-%0A%09%09//
  between
 %0A%09%09/
@@ -2001,21 +2001,16 @@
  between
-%0A%09%09//
  HTTP se
@@ -2196,28 +2196,27 @@
 %09WebSession 
-http
+web
 Session = (W
@@ -2287,20 +2287,19 @@
 %0A%0A%09%09if (
-http
+web
 Session 
@@ -2474,20 +2474,19 @@
 %09%09%7B%0A%09%09%09%09
-http
+web
 Session 
@@ -2669,94 +2669,8 @@
 %09%7D%0A%0A
-%09%09%09// Save servlet session in there%0A%09%09%09httpSession.httpSession = httpServletSession;%0A%0A
 %09%09%09/
@@ -2705,36 +2705,35 @@
 this session%0A%09%09%09
-http
+web
 Session.setLocal
@@ -2756,18 +2756,23 @@
 ale());%0A
-%0A
 %09
+%09%7D%0A%09%09%0A
 %09%09// Att
@@ -2779,116 +2779,11 @@
 ach 
-to httpSession%0A%09%09%09httpServletSession.setAttribute(sessionAttributeName, httpSession);%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09// R
+/ r
 eatt
@@ -2809,21 +2809,19 @@
 ssion%0A%09%09
-%09http
+web
 Session.
@@ -2857,17 +2857,16 @@
 ion;%0A%0A%09%09
-%09
 // In a 
@@ -2917,17 +2917,16 @@
 licated%0A
-%09
 %09%09// if 
@@ -2984,17 +2984,16 @@
 bject%0A%09%09
-%09
 // and m
@@ -3048,17 +3048,16 @@
 er gets%0A
-%09
 %09%09// fla
@@ -3108,17 +3108,16 @@
 rce a%0A%09%09
-%09
 // chang
@@ -3169,19 +3169,30 @@
 ering%0A%09%09
-%09
 //
+ replication
  occurs.
@@ -3192,17 +3192,16 @@
 occurs.%0A
-%09
 %09%09httpSe
@@ -3248,20 +3248,19 @@
 teName, 
-http
+web
 Session)
@@ -3260,20 +3260,16 @@
 ession);
-%0A%09%09%7D
 %0A%0A%09%09// S
@@ -3338,20 +3338,19 @@
 ion.set(
-http
+web
 Session)
@@ -3353,36 +3353,35 @@
 ion);%0A%0A%09%09return 
-http
+web
 Session;%0A%09%7D%0A%0A%09/*

Removed repeated code and renamed a confusing variable name.