package at.gv.egovernment.moa.id.entrypoints; import java.io.IOException; import java.util.Iterator; import javax.servlet.ServletConfig; import javax.servlet.ServletException; 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.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 AuthServlet { /** * */ private static final long serialVersionUID = 1L; 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> endpointMap = new * HashMap>(); * * private void registerModule(IModulInfo modulInfo) { * * HashMap tempMap = new HashMap(); * * 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 servletInfos = modulInfo.getServlets(); * * Iterator 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 { super.init(config); MOAIDAuthInitializer.initialize(); Logger.info(MOAIDMessageProvider.getInstance().getMessage( "init.00", null)); } catch (Exception ex) { Logger.fatal( MOAIDMessageProvider.getInstance().getMessage("init.02", null), ex); throw new ServletException(ex); } Logger.info("Dispatcher Servlet initialization"); /* * List modules = ModulStorage.getAllModules(); * Iterator 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 { try { Object moduleObject = req.getParameter(PARAM_TARGET_MODULE); String module = null; if (moduleObject != null && (moduleObject instanceof String)) { module = (String) moduleObject; } if (module == null) { module = (String) req.getAttribute(PARAM_TARGET_MODULE); } Object actionObject = req.getParameter(PARAM_TARGET_ACTION); String action = null; if (actionObject != null && (actionObject instanceof String)) { action = (String) actionObject; } if (action == null) { action = req.getParameter(PARAM_TARGET_ACTION); } Logger.debug("dispatching to " + module + " protocol " + action); IModulInfo info = ModulStorage.getModuleByPath(module); IAction moduleAction = null; if (info == null) { Iterator modules = ModulStorage.getAllModules() .iterator(); while (modules.hasNext()) { info = modules.next(); moduleAction = info.canHandleRequest(req, resp); if (moduleAction != null) { action = moduleAction.getDefaultActionName(); module = info.getPath(); break; } info = null; } if (moduleAction == null) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); Logger.error("Protocol " + module + " has no module registered"); return; } } if (moduleAction == null) { moduleAction = info.getAction(action); if (moduleAction == null) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); Logger.error("Action " + action + " is not available!"); return; } } HttpSession httpSession = req.getSession(); IRequest protocolRequest = null; try { 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; } 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; } } } moduleAction.processRequest(protocolRequest, req, resp); RequestStorage.removePendingRequest(httpSession); AuthenticationManager.logout(req, resp); } catch (Throwable e) { e.printStackTrace(); // Try handle module specific, if not possible rethrow if (!info.generateErrorMessage(e, req, resp, protocolRequest)) { 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); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { processRequest(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { processRequest(req, resp); } }