package org.frontcache.include.impl.f;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.collections4.MultiValuedMap;
import org.apache.http.client.HttpClient;
import org.frontcache.core.FCUtils;
import org.frontcache.core.FrontCacheException;
import org.frontcache.core.WebResponse;
import org.frontcache.include.IncludeProcessorFilter;

public class BotIncludeProcessorFilter implements IncludeProcessorFilter
{
	private Map<String, String> cache = new ConcurrentHashMap<String, String>();

	public String callInclude(String urlStr, MultiValuedMap<String, String> requestHeaders, HttpClient client) throws FrontCacheException
	{
		if (isBot(requestHeaders))
		{
			// work with cache			
			String content = cache.get(urlStr);
			if (null != content)
				return content;

			// recursive call to FCServlet
			WebResponse webResponse = FCUtils.dynamicCall(urlStr, requestHeaders, client);
			content = webResponse.getContent();
			
			cache.put(urlStr, content);
			
			return content;

		} else {
			// recursive call to FCServlet
			WebResponse webResponse = FCUtils.dynamicCall(urlStr, requestHeaders, client);
			return webResponse.getContent();
		}
		
	}
	
	private boolean isBot(MultiValuedMap<String, String> requestHeaders)
	{
		System.out.println(requestHeaders);
		return false;
	}
}
@@ -379,16 +379,74 @@
 ilter;%0A%0A
+/**%0A * %0A * TODO: make it configurable and optional%0A *%0A */%0A
 public c
@@ -591,16 +591,359 @@
 ng%3E();%0A%0A
+%09// TODO: move to config file%0A%09private String%5B%5D botUserAgentKeywords = new String%5B%5D %7B%0A%09%09%09%22Googlebot%22, %22msnbot%22, %22bingbot%22, %22YandexBot%22, %22YandexDirect%22, %22Baiduspider%22, %22Yahoo! Slurp%22,%0A%09%09%09%22majestic12%22, %22Mail.RU_Bot%22, %22EasouSpider%22, %22voilabot%22, %22AhrefsBot%22, %22orangebot%22, %22SemrushBot%22%7D;%0A%09%0A%0A%09%09%09%0A%09public BotIncludeProcessorFilter() %7B%0A%09%09super();%0A%09%7D%0A%0A
 %09public 
@@ -1105,16 +1105,52 @@
 s))%0A%09%09%7B%0A
+%09%09%09requestHeaders.remove(%22cookie%22);%0A
 %09%09%09// wo
@@ -1712,16 +1712,18 @@
 ers)%0A%09%7B%0A
+//
 %09%09System
@@ -1758,22 +1758,362 @@
 ;%0A%09%09
-return false;%0A%09%7D
+%0A%09%09if (null != requestHeaders.get(%22user-agent%22))%0A%09%09%7B%0A%09%09%09for (String userAgent : requestHeaders.get(%22user-agent%22))%0A%09%09%09%09if (isBot(userAgent))%0A%09%09%09%09%09return true;%0A%09%09%7D%0A%09%09return false;%0A%09%7D%0A%09%0A%09private boolean isBot(String userAgent)%0A%09%7B%0A%09%09for (String botKeyword : botUserAgentKeywords)%0A%09%09%09if (userAgent.contains(botKeyword))%0A%09%09%09%09return true;%0A%09%09%09%0A%09%09return false;%0A%09%7D%0A%09
 %0A%7D

include processor filters (e.g. to for requests with no session) #6