package com.cognifide.slice.core.internal.filter;

/*
 * #%L
 * Slice - Core
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2012 Cognifide Limited
 * %%
 * 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.
 * #L%
 */


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 org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.osgi.framework.Constants;

import com.cognifide.slice.api.context.Context;
import com.cognifide.slice.api.context.ContextProvider;
import com.cognifide.slice.core.internal.context.SliceContextFactory;

// @formatter:off
/**
 * @author Witold Szczerba
 * @author Rafał Malinowski
 * @class ContextRequstFilter
 * 
 * This filter is run for on each request. Each time it stores a Context instance with ServletRequest and
 * ServletResponse values in per-thread variable. This Context is later accessible by getContext() and can be
 * used in Injector instances that are used during this request.
 */
@Component(immediate = true)
@Service
@Properties({
		@Property(name = Constants.SERVICE_DESCRIPTION, value = "Filter that is injected into request chain to provide access to request, resource and response."),
		@Property(name = Constants.SERVICE_VENDOR, value = "Cognifide"),
		@Property(name = Constants.SERVICE_RANKING, intValue = ContextRequstFilter.RANKING),
		@Property(name = "filter.scope", value = "request") })
// @formatter:on
public class ContextRequstFilter implements Filter, ContextProvider {

	public static final int RANKING = -800;

	private final ThreadLocal<Context> contexts = new ThreadLocal<Context>();

	@Override
	public void init(final FilterConfig filterConfig) throws ServletException {
	}

	/**
	 * Update Context instance for current thread to use current request and response values.
	 */
	@Override
	public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
			throws IOException, ServletException {
		final Context previous = contexts.get();

		try {
			final Context context = (new SliceContextFactory()).getServletRequestContext(request, response);
			contexts.set(context);
			chain.doFilter(request, response);
		} finally {
			contexts.set(previous);
		}
	}

	@Override
	public void destroy() {
	}

	/**
	 * @returns Context instance for current thread
	 * 
	 * Return Context instance for current thread. It contains most current ServletRequest and ServletResponse
	 * instances.
	 */
	@Override
	public Context getContext() {
		return contexts.get();
	}

}

@@ -46,19 +46,23 @@
 ter;
-%0A
+%0D%0A%0D
 %0A/*
+%0D
 %0A * #%25L
+%0D
 %0A * 
@@ -73,16 +73,17 @@
 e - Core
+%0D
 %0A * $Id:
@@ -83,16 +83,17 @@
  * $Id:$
+%0D
 %0A * $Hea
@@ -98,22 +98,24 @@
 eadURL:$
+%0D
 %0A * %25%25
+%0D
 %0A * Copy
@@ -146,22 +146,24 @@
  Limited
+%0D
 %0A * %25%25
+%0D
 %0A * Lice
@@ -221,16 +221,17 @@
 cense%22);
+%0D
 %0A * you 
@@ -286,24 +286,25 @@
 the License.
+%0D
 %0A * You may 
@@ -334,20 +334,22 @@
 cense at
+%0D
 %0A * 
+%0D
 %0A *     
@@ -391,20 +391,22 @@
 ENSE-2.0
+%0D
 %0A * 
+%0D
 %0A * Unle
@@ -468,16 +468,17 @@
 software
+%0D
 %0A * dist
@@ -538,16 +538,17 @@
 %22 BASIS,
+%0D
 %0A * WITH
@@ -615,16 +615,17 @@
 implied.
+%0D
 %0A * See 
@@ -687,16 +687,17 @@
 ions and
+%0D
 %0A * limi
@@ -726,20 +726,24 @@
 nse.
+%0D
 %0A * #L%25
+%0D
 %0A */
-%0A
+%0D%0A%0D
 %0A%0D%0Ai
@@ -2236,16 +2236,17 @@
 value = 
+%7B
 %22request
@@ -2246,16 +2246,27 @@
 request%22
+,%22forward%22%7D
 ) %7D)%0D%0A//

Update slice-core/src/main/java/com/cognifide/slice/core/internal/filter/ContextRequstFilter.java