package com.sun.j2ee.blueprints.waf.controller.web.flow;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Enumeration;
import com.sun.j2ee.blueprints.waf.controller.web.util.WebKeys;
import com.sun.j2ee.blueprints.waf.controller.web.URLMappingsXmlDAO;
import com.sun.j2ee.blueprints.waf.controller.web.URLMapping;
import com.sun.j2ee.blueprints.waf.controller.web.flow.ScreenFlowData;
import com.sun.j2ee.blueprints.waf.controller.web.flow.FlowHandler;
import com.sun.j2ee.blueprints.waf.controller.web.flow.FlowHandlerException;
import com.sun.j2ee.blueprints.util.tracer.Debug;
public class ScreenFlowManager implements java.io.Serializable {
private HashMap screens;
private HashMap urlMappings;
private HashMap exceptionMappings;
private HashMap screenDefinitionMappings;
private String defaultScreen = "";
private ServletContext context;
public ScreenFlowManager() {
screens = new HashMap();
}
public void init(ServletContext context) {
this.context = context;
String requestMappingsURL = null;
try {
requestMappingsURL = context.getResource("/WEB-INF/mappings.xml").toString();
} catch (java.net.MalformedURLException ex) {
System.err.println("ScreenFlowManager: initializing ScreenFlowManager malformed URL exception: " + ex);
}
urlMappings = (HashMap)context.getAttribute(WebKeys.URL_MAPPINGS);
ScreenFlowData screenFlowData = URLMappingsXmlDAO.loadScreenFlowData(requestMappingsURL);
defaultScreen = screenFlowData.getDefaultScreen();
exceptionMappings = screenFlowData.getExceptionMappings();
}
private URLMapping getURLMapping(String urlPattern) {
if ((urlMappings != null) && urlMappings.containsKey(urlPattern)) {
return (URLMapping)urlMappings.get(urlPattern);
} else {
return null;
}
}
public String getExceptionScreen(String exceptionClassName) {
return (String)exceptionMappings.get(exceptionClassName);
}
public void forwardToNextScreen(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, FlowHandlerException, javax.servlet.ServletException {
String fullURL = request.getRequestURI();
String selectedURL = defaultScreen;
int lastPathSeparator = fullURL.lastIndexOf("/") + 1;
if (lastPathSeparator != -1) {
selectedURL = fullURL.substring(lastPathSeparator, fullURL.length());
}
String currentScreen = "";
URLMapping urlMapping = getURLMapping(selectedURL);
if (urlMapping != null) {
if (!urlMapping.useFlowHandler()) {
currentScreen = urlMapping.getScreen();
} else {
FlowHandler handler = null;
String flowHandlerString = urlMapping.getFlowHandler();
try {
handler = (FlowHandler)getClass().getClassLoader().loadClass(flowHandlerString).newInstance();
handler.doStart(request);
String flowResult = handler.processFlow(request);
handler.doEnd(request);
currentScreen = urlMapping.getResultScreen(flowResult);
if (currentScreen == null) currentScreen = flowResult;
} catch (Exception ex) {
System.err.println("ScreenFlowManager caught loading handler: " + ex);
}
}
}
if (currentScreen == null) {
System.err.println("ScreenFlowManager: Screen not found for " + selectedURL);
throw new RuntimeException("Screen not found for " + selectedURL);
}
context.getRequestDispatcher("/" + currentScreen).forward(request, response);
}
public String getExceptionScreen(Throwable e) {
Iterator it = exceptionMappings.keySet().iterator();
while (it.hasNext()) {
String exceptionName = (String)it.next();
Class targetExceptionClass = null;
try {
targetExceptionClass = this.getClass().getClassLoader().loadClass(exceptionName);
} catch (ClassNotFoundException cnfe) {
System.err.println("ScreenFlowManager: Could not load exception " + exceptionName);
}
System.err.println("Checking exception: " + exceptionName + " against " + e.getClass().getName());
if ((targetExceptionClass != null) &&
targetExceptionClass.isAssignableFrom(e.getClass())) {
System.err.println("ScreenFlowManager: found exception for " + exceptionName + " matches");;
return "/" + (String)exceptionMappings.get(exceptionName);
}
}
return null;
}
public void setDefaultScreen(String defaultScreen) {
this.defaultScreen = defaultScreen;
}
public String getCurrentScreen(HttpSession session) {
return (String)session.getAttribute(WebKeys.CURRENT_SCREEN);
}
}