package de.spieleck.pcs.http;
import de.spieleck.pcs.Constants;
import de.spieleck.pcs.conf.ConfigAdmin;
import de.spieleck.pcs.conf.ConfigUser;
import de.spieleck.pcs.view.View;
import de.spieleck.pcs.view.ViewException;
import de.spieleck.pcs.factory.DefaultFactory;
import de.spieleck.pcs.action.Action;
import de.spieleck.pcs.action.ActionResult;
import de.spieleck.pcs.action.ActionException;
import de.spieleck.pcs.factory.FactoryException;
import de.spieleck.pcs.factory.NoSuchKeyException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Hashtable;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
public class ControllerServlet extends HttpServlet implements ConfigUser, Constants {
protected static String description = "This servlet acts a Controller";
protected ControllerConfig controllerConfig;
public void init(ServletConfig config) throws ServletException {
super.init(config);
try{
controllerConfig = new XmlControllerConfig(
getInitParameter(CONTOLLER_XML_CONFIG_PARAM));
} catch(ControllerException e){
throw new ServletException("Could not create ControllerConfig", e);
}
View.setUseJTidy(controllerConfig.useJTidy());
ConfigAdmin.getInstance().addConfigUser(this);
}
public void destroy() {
}
protected ServiceInfo getService(HttpServletRequest request)
throws ControllerException {
Exception e = (Exception) request.getSession().
getAttribute(CONTROLLER_SESSION_EXCEPTION_PARAM_NAME);
if( e != null){
return controllerConfig.getServiceInfo(EXCEPTION_SERVICE_NAME);
}
String path = request.getPathInfo();
ServiceInfo service = null;
String serviceName = null;
if(path != null){
int end = path.indexOf('/', 2);
if(end > 0)
serviceName = path.substring(1, end);
else
serviceName = path.substring(1);
if(serviceName != null && serviceName.length() > 0)
service = lookupService(serviceName);
}
if (service == null) {
serviceName = request.getParameter(REQUEST_SERVICE_NAME_PARAM);
if(serviceName != null && serviceName.length() > 0)
service = lookupService(serviceName);
}
if(service == null)
throw new ControllerException("No suiting service found.");
return service;
}
private ServiceInfo lookupService(String name){
try{
return controllerConfig.getServiceInfo(name);
}
catch(Exception e){
return null;
}
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Action action = null;
try{
ServiceInfo service = getService(request);
action = service.getAction();
Hashtable h = HttpUtil.getParameters(request);
h.put(CONTEXT_REAL_PATH, getServletContext().getRealPath("/"));
Exception e = (Exception) request.getSession().getAttribute(
CONTROLLER_SESSION_EXCEPTION_PARAM_NAME);
if(e!= null){
h.put(CONTROLLER_SESSION_EXCEPTION_PARAM_NAME, e);
request.getSession().removeAttribute(
CONTROLLER_SESSION_EXCEPTION_PARAM_NAME);
}
ActionResult ar = action.perform(h);
display(request, response, ar, service);
}
catch(ActionException e){
e.printStackTrace();
handleException(request, response, e);
}
catch(Exception e){
e.printStackTrace();
throw new ServletException(e.getMessage());
}
}
protected void display(HttpServletRequest request,
HttpServletResponse response, ActionResult ar, ServiceInfo service)
throws IOException{
ar.getParameters().put("sessionid", request.getSession().getId());
response.setContentType("text/html");
OutputStream os = response.getOutputStream();
View view = new View(os);
try{
String stylesheet = null;
if(service.isDynamicView())
stylesheet = ar.getStyleSheet();
if(stylesheet == null ||stylesheet.length() == 0)
stylesheet = service.getStylesheet();
view.output(ar, controllerConfig.getStyleSheetAbsoluteFileName(
stylesheet));
view.cleanUp();
} catch(Exception ve){
ve.printStackTrace();
ve.printStackTrace(new java.io.PrintStream(os));
}
os.close();
}
protected void handleException(HttpServletRequest request,
HttpServletResponse response, Exception e) throws IOException,
ServletException{
request.getSession().setAttribute(
CONTROLLER_SESSION_EXCEPTION_PARAM_NAME, e);
processRequest(request, response);
}
protected boolean checkAuthorization(HttpServletRequest request){
return true;
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return description;
}
public void resetConfig() {
try{
init();
} catch(Exception e){
e.printStackTrace();
}
}
}