package com.zyeeda.framework.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tapestry5.ioc.Registry;
import org.openid4java.OpenIDException;
import org.openid4java.consumer.ConsumerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zyeeda.framework.openid.OpenIdConsumer;
import com.zyeeda.framework.template.TemplateService;
import com.zyeeda.framework.utils.IocUtils;

public class OpenIdConsumerServlet extends HttpServlet {

	private static final long serialVersionUID = 6393634729613708155L;
	
	private static final Logger logger = LoggerFactory.getLogger(OpenIdConsumerServlet.class);
	
	private static final String OPENID_CONSUMER_KEY = "openid.consumer";
	//private static final String IDENTIFIER_KEY = "openid.identifier";
	private static final String REDIRECT_TO_URL_KEY = "redirect.to.url";
	private static final String RETURN_TO_URL_KEY = "return.to.url";
	private static final String PUBLIC_IDENTIFIER_KEY = "public.identifier";
	
	private String publicIdentifier;
	private OpenIdConsumer consumer;
	
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		
		this.publicIdentifier = config.getInitParameter(PUBLIC_IDENTIFIER_KEY);
		
		this.consumer = (OpenIdConsumer) this.getServletContext().getAttribute(OPENID_CONSUMER_KEY);
		if (this.consumer == null) {
			try {
				this.consumer = new OpenIdConsumer();
				this.consumer.setRedirectUrl(config.getInitParameter(REDIRECT_TO_URL_KEY));
				this.consumer.setReturnToUrl(config.getInitParameter(RETURN_TO_URL_KEY));
				this.getServletContext().setAttribute(OPENID_CONSUMER_KEY, this.consumer);
			} catch (ConsumerException e) {
				throw new ServletException(e);
			}
		}
		logger.info("Initialized OpenID Consumer Servlet.");
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("servlet path info = {}", request.getPathInfo());
		
		if ("/redirect".equals(request.getPathInfo())) {
			Map<String, Object> args = new HashMap<String, Object>();
			args.put("message", request.getAttribute("message"));

			Registry registry = IocUtils.getRegistry(this.getServletContext());
			TemplateService tplSvc = registry.getService(TemplateService.class);
			tplSvc.paint(request.getServletPath() + "/redirect.ftl", response.getWriter(), args);
			return;
		}
		
		if ("/verify".equals(request.getServletPath())) {
			logger.info("Verify OpenId auth response.");
			return;
		}
		
		try {
			this.consumer.authRequest(this.publicIdentifier, request, response);
		} catch (OpenIDException e) {
			throw new ServletException(e);
		}		
	}

}

@@ -62,58 +62,8 @@
 on;%0D
-%0Aimport java.util.HashMap;%0D%0Aimport java.util.Map;%0D
 %0A%0D%0Ai
@@ -279,51 +279,8 @@
 %0D%0A%0D%0A
-import org.apache.tapestry5.ioc.Registry;%0D%0A
 impo
@@ -425,24 +425,24 @@
 Factory;%0D%0A%0D%0A
+
 import com.z
@@ -484,108 +484,8 @@
 er;%0D
-%0Aimport com.zyeeda.framework.template.TemplateService;%0D%0Aimport com.zyeeda.framework.utils.IocUtils;%0D
 %0A%0D%0Ap
@@ -2072,438 +2072,8 @@
 %09%09%0D%0A
-%09%09if (%22/redirect%22.equals(request.getPathInfo())) %7B%0D%0A%09%09%09Map%3CString, Object%3E args = new HashMap%3CString, Object%3E();%0D%0A%09%09%09args.put(%22message%22, request.getAttribute(%22message%22));%0D%0A%0D%0A%09%09%09Registry registry = IocUtils.getRegistry(this.getServletContext());%0D%0A%09%09%09TemplateService tplSvc = registry.getService(TemplateService.class);%0D%0A%09%09%09tplSvc.paint(request.getServletPath() + %22/redirect.ftl%22, response.getWriter(), args);%0D%0A%09%09%09return;%0D%0A%09%09%7D%0D%0A%09%09%0D%0A
 %09%09if

Replace /redirect servlet path with jsp openid_redirect.jsp