/*
 * $Id$
 * $Revision$ $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 java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpSession;

import wicket.Application;
import wicket.Request;
import wicket.RequestCycle;
import wicket.session.ISessionStore;

/**
 * Default web implementation of {@link wicket.session.ISessionStore} that uses
 * the {@link javax.servlet.http.HttpSession} to store its attributes.
 * 
 * @author Eelco Hillenius
 */
public class HttpSessionStore implements ISessionStore
{
	/**
	 * the prefix for storing variables in the actual session.
	 */
	private String sessionAttributePrefix;

	/** cached http session object. */
	private HttpSession httpSession = null;

	/**
	 * Construct.
	 */
	public HttpSessionStore()
	{
		// sanity check
		Application application = Application.get();
		if (!(application instanceof WebApplication))
		{
			throw new IllegalStateException(getClass().getName()
					+ " can only operate in the context of web applications");
		}
	}

	/**
	 * @see wicket.session.ISessionStore#getId()
	 */
	public String getId()
	{
		HttpSession httpSession = getHttpSession();
		if (httpSession != null)
		{
			return httpSession.getId();
		}
		return String.valueOf(hashCode());
	}

	/**
	 * @see wicket.session.ISessionStore#invalidate()
	 */
	public void invalidate()
	{
		HttpSession httpSession = getHttpSession();
		if (httpSession != null)
		{
			try
			{
				httpSession.invalidate();
			}
			catch (IllegalStateException e)
			{
				// Ignore
			}
		}
	}

	/**
	 * @see wicket.session.ISessionStore#setAttribute(java.lang.String,
	 *      java.lang.Object)
	 */
	public void setAttribute(String name, Object value)
	{
		HttpSession httpSession = getHttpSession();
		if (httpSession != null)
		{
			httpSession.setAttribute(getSessionAttributePrefix() + name, value);
		}
	}

	/**
	 * @see wicket.session.ISessionStore#getAttribute(java.lang.String)
	 */
	public Object getAttribute(String name)
	{
		HttpSession httpSession = getHttpSession();
		if (httpSession != null)
		{
			return httpSession.getAttribute(getSessionAttributePrefix() + name);
		}
		return null;
	}

	/**
	 * @see wicket.session.ISessionStore#removeAttribute(java.lang.String)
	 */
	public void removeAttribute(String name)
	{
		HttpSession httpSession = getHttpSession();
		if (httpSession != null)
		{
			httpSession.removeAttribute(getSessionAttributePrefix() + name);
		}
	}

	/**
	 * Gets the session attribute prefix.
	 * 
	 * @return the session attribute prefix
	 */
	private String getSessionAttributePrefix()
	{
		if (sessionAttributePrefix == null)
		{
			WebApplication application = (WebApplication)Application.get();
			WebRequestCycle cycle = (WebRequestCycle)RequestCycle.get();
			String servletContextName = application.getSessionAttributePrefix(cycle.getWebRequest());
			sessionAttributePrefix = "wicket:" + servletContextName + ":";
		}
		return sessionAttributePrefix;
	}

	/**
	 * @see wicket.session.ISessionStore#getAttributeNames()
	 */
	public List getAttributeNames()
	{
		List list = new ArrayList();
		HttpSession httpSession = getHttpSession();
		if (httpSession != null)
		{
			final Enumeration names = httpSession.getAttributeNames();
			final String prefix = getSessionAttributePrefix();
			while (names.hasMoreElements())
			{
				final String name = (String)names.nextElement();
				if (name.startsWith(prefix))
				{
					list.add(name.substring(prefix.length()));
				}
			}
		}
		return list;
	}

	/**
	 * Gets the underlying HttpSession object or null.
	 * <p>
	 * WARNING: it is a bad idea to depend on the http session object directly.
	 * Please use the classes and methods that are exposed by Wicket instead.
	 * Send an email to the mailing list in case it is not clear how to do
	 * things or you think you miss funcionality which causes you to depend on
	 * this directly.
	 * </p>
	 * 
	 * @return The underlying HttpSession object (null if not created yet).
	 */
	protected final HttpSession getHttpSession()
	{
		if (httpSession == null)
		{
			httpSession = createHttpSession();
		}
		return httpSession;
	}

	/**
	 * Gets the prefix for storing variables in the actual session (typically
	 * {@link HttpSession} for this application instance.
	 * 
	 * @param request
	 *            the request
	 * 
	 * @return the prefix for storing variables in the actual session
	 */
	protected final String getSessionAttributePrefix(final WebRequest request)
	{
		return getSessionAttributePrefix();
	}

	/**
	 * Create the http session.
	 * 
	 * @return The http session
	 */
	private final HttpSession createHttpSession()
	{
		RequestCycle requestCycle = RequestCycle.get();
		if (requestCycle != null)
		{
			Request request = requestCycle.getRequest();
			if (request instanceof WebRequest)
			{
				WebRequest webRequest = (WebRequest)request;
				if (webRequest != null)
				{
					return webRequest.getHttpServletRequest().getSession(true);
				}
			}
		}
		return null;
	}
}

@@ -3405,33 +3405,30 @@
 %0A%09%09%09
-String servletContextName
+sessionAttributePrefix
  = a
@@ -3492,74 +3492,8 @@
 ));%0A
-%09%09%09sessionAttributePrefix = %22wicket:%22 + servletContextName + %22:%22;%0A
 %09%09%7D%0A

fix for double session entries one under key: wicket:wicket:servlet:session and one wicket:servlet:session