diff options
Diffstat (limited to 'id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/entrypoints/DispatcherServlet.java')
-rw-r--r-- | id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/entrypoints/DispatcherServlet.java | 322 |
1 files changed, 196 insertions, 126 deletions
diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/entrypoints/DispatcherServlet.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/entrypoints/DispatcherServlet.java index 48f44f97b..72ade4f25 100644 --- a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/entrypoints/DispatcherServlet.java +++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/entrypoints/DispatcherServlet.java @@ -1,103 +1,95 @@ package at.gv.egovernment.moa.id.entrypoints; import java.io.IOException; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; import javax.servlet.ServletConfig; import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import at.gv.egovernment.moa.id.MOAIDException; import at.gv.egovernment.moa.id.auth.MOAIDAuthInitializer; +import at.gv.egovernment.moa.id.auth.WrongParametersException; +import at.gv.egovernment.moa.id.auth.servlet.AuthServlet; +import at.gv.egovernment.moa.id.moduls.AuthenticationManager; +import at.gv.egovernment.moa.id.moduls.IAction; import at.gv.egovernment.moa.id.moduls.IModulInfo; +import at.gv.egovernment.moa.id.moduls.IRequest; import at.gv.egovernment.moa.id.moduls.ModulStorage; -import at.gv.egovernment.moa.id.moduls.ServletInfo; -import at.gv.egovernment.moa.id.moduls.ServletType; +import at.gv.egovernment.moa.id.moduls.NoPassivAuthenticationException; +import at.gv.egovernment.moa.id.moduls.RequestStorage; import at.gv.egovernment.moa.id.util.MOAIDMessageProvider; import at.gv.egovernment.moa.logging.Logger; -public class DispatcherServlet extends HttpServlet { +public class DispatcherServlet extends AuthServlet { /** * */ private static final long serialVersionUID = 1L; - public static final String PARAM_TARGET_PATH = "mod"; - public static final String PARAM_TARGET_PROTOCOL = "action"; - public static final String PARAM_DISPATCHER_TARGETS = "DispatcherTargets"; - public static final String PARAM_DISPATCHER_TYPE = "DispatcherType"; - public static final String PARAM_DISPATCHER_TYPE_UNAUTH = "UNAUTH"; - public static final String PARAM_DISPATCHER_TYPE_AUTH = "AUTH"; - public static String SYSTEM_NEWLINE = System.getProperty("line.separator"); - - private HashMap<String, HashMap<String, HttpServlet>> endpointMap = new HashMap<String, HashMap<String, HttpServlet>>(); - - private void registerModule(IModulInfo modulInfo) { - - HashMap<String, HttpServlet> tempMap = new HashMap<String, HttpServlet>(); - - try { - - String path = modulInfo.getPath(); - - if (path == null) { - throw new Exception(String.format( - "%s does not return a valid target path!", - new Object[] { modulInfo.getClass().getName() })); - } - - Logger.debug("Registering: " + modulInfo.getName() + " under " - + path); - - List<ServletInfo> servletInfos = modulInfo.getServlets(); - - Iterator<ServletInfo> servletInfoIterator = servletInfos.iterator(); - - while (servletInfoIterator.hasNext()) { - - ServletInfo servletInfo = servletInfoIterator.next(); - - if (servletInfo.getType() == ServletType.UNAUTH) { - HttpServlet servlet = servletInfo.getServletInstance(); - String target = servletInfo.getTarget(); - - if (target == null) { - throw new Exception( - String.format( - "%s does not return a valid target identifier!", - new Object[] { servlet.getClass() - .getName() })); - } - - if (tempMap.containsKey(target)) { - throw new Exception(String.format( - "%s tried to overwrite %s/%s", new Object[] { - servlet.getClass().getName(), path, - target })); - } - - tempMap.put(target, servlet); - Logger.info("Registered Servlet class: " - + servlet.getClass().getName() + " OK"); - } - - } - - // when there was no error we register all servlets into the real - // endpoint map ... - if (!tempMap.isEmpty()) { - endpointMap.put(path, tempMap); - } - } catch (Throwable e) { - Logger.error("Registering Modul class: " - + modulInfo.getClass().getName() + " FAILED!!", e); - } - } + public static final String PARAM_TARGET_MODULE = "mod"; + public static final String PARAM_TARGET_ACTION = "action"; + /* + * public static final String PARAM_DISPATCHER_TARGETS = + * "DispatcherTargets"; public static final String PARAM_DISPATCHER_TYPE = + * "DispatcherType"; public static final String PARAM_DISPATCHER_TYPE_UNAUTH + * = "UNAUTH"; public static final String PARAM_DISPATCHER_TYPE_AUTH = + * "AUTH"; public static String SYSTEM_NEWLINE = + * System.getProperty("line.separator"); + */ + /* + * private HashMap<String, HashMap<String, HttpServlet>> endpointMap = new + * HashMap<String, HashMap<String, HttpServlet>>(); + * + * private void registerModule(IModulInfo modulInfo) { + * + * HashMap<String, HttpServlet> tempMap = new HashMap<String, + * HttpServlet>(); + * + * try { + * + * String path = modulInfo.getPath(); + * + * if (path == null) { throw new Exception(String.format( + * "%s does not return a valid target path!", new Object[] { + * modulInfo.getClass().getName() })); } + * + * Logger.debug("Registering: " + modulInfo.getName() + " under " + path); + * + * List<ServletInfo> servletInfos = modulInfo.getServlets(); + * + * Iterator<ServletInfo> servletInfoIterator = servletInfos.iterator(); + * + * while (servletInfoIterator.hasNext()) { + * + * ServletInfo servletInfo = servletInfoIterator.next(); + * + * if (servletInfo.getType() == ServletType.UNAUTH) { HttpServlet servlet = + * servletInfo.getServletInstance(); String target = + * servletInfo.getTarget(); + * + * if (target == null) { throw new Exception( String.format( + * "%s does not return a valid target identifier!", new Object[] { + * servlet.getClass() .getName() })); } + * + * if (tempMap.containsKey(target)) { throw new Exception(String.format( + * "%s tried to overwrite %s/%s", new Object[] { + * servlet.getClass().getName(), path, target })); } + * + * tempMap.put(target, servlet); Logger.info("Registered Servlet class: " + + * servlet.getClass().getName() + " OK"); } + * + * } + * + * // when there was no error we register all servlets into the real // + * endpoint map ... if (!tempMap.isEmpty()) { endpointMap.put(path, + * tempMap); } } catch (Throwable e) { + * Logger.error("Registering Modul class: " + modulInfo.getClass().getName() + * + " FAILED!!", e); } } + */ @Override public void init(ServletConfig config) throws ServletException { try { @@ -113,69 +105,147 @@ public class DispatcherServlet extends HttpServlet { } Logger.info("Dispatcher Servlet initialization"); - List<IModulInfo> modules = ModulStorage.getAllModules(); - Iterator<IModulInfo> it = modules.iterator(); - while (it.hasNext()) { - IModulInfo info = it.next(); - String targetClass = info.getClass().getName(); - try { - registerModule(info); - } catch (Throwable e) { - Logger.error("Registering Class " + targetClass + " FAILED!!", - e); - } - } + /* + * List<IModulInfo> modules = ModulStorage.getAllModules(); + * Iterator<IModulInfo> it = modules.iterator(); while (it.hasNext()) { + * IModulInfo info = it.next(); String targetClass = + * info.getClass().getName(); try { registerModule(info); } catch + * (Throwable e) { Logger.error("Registering Class " + targetClass + + * " FAILED!!", e); } } + */ } protected void processRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - Object pathObject = req.getParameter(PARAM_TARGET_PATH); - String path = null; - if (pathObject != null && (pathObject instanceof String)) { - path = (String) pathObject; - } + try { + Object moduleObject = req.getParameter(PARAM_TARGET_MODULE); + String module = null; + if (moduleObject != null && (moduleObject instanceof String)) { + module = (String) moduleObject; + } - if (path == null) { - path = (String) req.getAttribute(PARAM_TARGET_PATH); - } + if (module == null) { + module = (String) req.getAttribute(PARAM_TARGET_MODULE); + } - Object protocolObject = req.getParameter(PARAM_TARGET_PROTOCOL); - String protocol = null; - if (protocolObject != null && (protocolObject instanceof String)) { - protocol = (String) protocolObject; - } + Object actionObject = req.getParameter(PARAM_TARGET_ACTION); + String action = null; + if (actionObject != null && (actionObject instanceof String)) { + action = (String) actionObject; + } - if (protocol == null) { - protocol = req.getParameter(PARAM_TARGET_PROTOCOL); - } + if (action == null) { + action = req.getParameter(PARAM_TARGET_ACTION); + } + + Logger.debug("dispatching to " + module + " protocol " + action); + + IModulInfo info = ModulStorage.getModuleByPath(module); + + if (info == null) { + resp.sendError(HttpServletResponse.SC_NOT_FOUND); + Logger.error("Protocol " + module + " has no module registered"); + return; + } + + IAction moduleAction = info.getAction(action); + + if (moduleAction == null) { + resp.sendError(HttpServletResponse.SC_NOT_FOUND); + Logger.error("Action " + action + " is not available!"); + return; + } - Logger.debug("dispatching to " + path + " protocol " + protocol); - - if (path != null && protocol != null && endpointMap.containsKey(path)) { - HashMap<String, HttpServlet> pathMap = endpointMap.get(path); - Logger.debug("found path"); - if (pathMap.containsKey(protocol)) { - Logger.debug("found protocol"); - try { - HttpServlet servlet = (HttpServlet) pathMap.get(protocol); - String forward = servlet.getClass().getName(); - Logger.info("Forwarding to Servlet: " + forward); - getServletContext().getNamedDispatcher(forward).forward( - req, resp); + HttpSession httpSession = req.getSession(); + + try { + IRequest protocolRequest = RequestStorage + .getPendingRequest(httpSession); + + if (protocolRequest != null) { + // check if pending request is same protocol and action + if (!protocolRequest.requestedModule().equals(module) + || !protocolRequest.requestedAction() + .equals(action)) { + resp.sendError(HttpServletResponse.SC_CONFLICT); + Logger.error("Different Request is pending in this session!"); + return; + } + } + + if (protocolRequest == null) { + protocolRequest = info.preProcess(req, resp, action); + if(protocolRequest != null) { + protocolRequest.setAction(action); + protocolRequest.setModule(module); + } + } + + if (protocolRequest == null) { + resp.sendError(HttpServletResponse.SC_BAD_REQUEST); + Logger.error("Failed to generate a valid protocol request!"); return; - } catch (Throwable e) { - Logger.error("Failed to process request!", e); - IModulInfo info = ModulStorage.getModuleByPath(path); - if(info != null) { - if(info.generateErrorMessage(e, req, resp)) { + } + + RequestStorage.setPendingRequest(httpSession, protocolRequest); + + if (moduleAction.needAuthentication(protocolRequest, req, resp)) { + if (protocolRequest.isPassiv() + && protocolRequest.forceAuth()) { + // conflict! + throw new NoPassivAuthenticationException(); + } + + if (protocolRequest.forceAuth()) { + if (!AuthenticationManager.tryPerformAuthentication( + req, resp)) { + AuthenticationManager.doAuthentication(req, resp, + protocolRequest); + return; + } + } else if (protocolRequest.isPassiv()) { + if (AuthenticationManager.tryPerformAuthentication(req, + resp) + || AuthenticationManager.isAuthenticated(req, + resp)) { + // Passive authentication ok! + } else { + throw new NoPassivAuthenticationException(); + } + } else { + if (AuthenticationManager.tryPerformAuthentication(req, + resp) + || AuthenticationManager.isAuthenticated(req, + resp)) { + // Is authenticated .. proceed + } else { + // Start authentication! + AuthenticationManager.doAuthentication(req, resp, + protocolRequest); return; } } - resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } + + moduleAction.processRequest(protocolRequest, req, resp); + + RequestStorage.removePendingRequest(httpSession); + + } catch (Throwable e) { + // Try handle module specific, if not possible rethrow + if (!info.generateErrorMessage(e, req, resp)) { + throw e; } } + } catch (WrongParametersException ex) { + handleWrongParameters(ex, req, resp); + } catch (MOAIDException ex) { + handleError(null, ex, req, resp); + } catch (Throwable e) { + e.printStackTrace(); + resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } - resp.sendError(HttpServletResponse.SC_NOT_FOUND); + } @Override |