/**
 * Copyright (C) 2015 Born Informatik AG (www.born.ch)
 *
 * 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 org.wte4j.ui.server.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.support.WebApplicationContextUtils;
import org.wte4j.User;
import org.wte4j.ui.server.services.ServiceContext;

public class SessionContextFilter implements Filter {

	private ServiceContext serviceContext;

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {

		if (request instanceof HttpServletRequest) {
			HttpServletRequest httpRequest = (HttpServletRequest) request;

			if (httpRequest.getUserPrincipal() != null) {
				String userName = httpRequest.getUserPrincipal().getName();
				if (userName != null) {
					serviceContext.setUser(new User(userName, userName));
				}
				serviceContext.setLocale(httpRequest.getLocale());
			}

		}

		filterChain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		serviceContext = WebApplicationContextUtils.
				getRequiredWebApplicationContext(filterConfig.getServletContext()).
				getBean(ServiceContext.class);
	}

}

@@ -1150,16 +1150,76 @@
 lter %7B%0A%0A
+%09private static final String ANONYMOUS_USER = %22anonymous%22;%0A%0A
 %09private
@@ -1250,16 +1250,16 @@
 ontext;%0A
-
 %0A%09@Overr
@@ -1555,16 +1555,70 @@
 request;
+%0A%09%09%09serviceContext.setLocale(httpRequest.getLocale());
 %0A%0A%09%09%09if 
@@ -1728,37 +1728,8 @@
 ();%0A
-%09%09%09%09if (userName != null) %7B%0A%09
 %09%09%09%09
@@ -1785,18 +1785,27 @@
 e));%0A%09%09%09
-%09
 %7D
+%0A%09%09%09else %7B
 %0A%09%09%09%09ser
@@ -1823,37 +1823,52 @@
 .set
-Locale(httpRequest.getLocale(
+User(new User(ANONYMOUS_USER, ANONYMOUS_USER
 ));%0A

set a anonymous user if not loged in