aboutsummaryrefslogtreecommitdiff
path: root/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/entrypoints/AuthDispatcherServlet.java
diff options
context:
space:
mode:
Diffstat (limited to 'id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/entrypoints/AuthDispatcherServlet.java')
-rw-r--r--id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/entrypoints/AuthDispatcherServlet.java219
1 files changed, 219 insertions, 0 deletions
diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/entrypoints/AuthDispatcherServlet.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/entrypoints/AuthDispatcherServlet.java
new file mode 100644
index 000000000..e78d9345c
--- /dev/null
+++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/entrypoints/AuthDispatcherServlet.java
@@ -0,0 +1,219 @@
+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.auth.MOAIDAuthInitializer;
+import at.gv.egovernment.moa.id.moduls.AuthenticationManager;
+import at.gv.egovernment.moa.id.moduls.IModulInfo;
+import at.gv.egovernment.moa.id.moduls.ITargetConfiguration;
+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.util.MOAIDMessageProvider;
+import at.gv.egovernment.moa.logging.Logger;
+
+public class AuthDispatcherServlet extends HttpServlet {
+
+ /**
+ *
+ */
+ 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.AUTH) {
+ 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("Auth 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);
+ }
+ }
+ }
+
+ protected void processRequest(HttpServletRequest req,
+ HttpServletResponse resp) throws ServletException, IOException {
+ try {
+ Object pathObject = req.getAttribute(PARAM_TARGET_PATH);
+ String path = null;
+
+ HttpSession session = req.getSession();
+
+ if (pathObject != null && (pathObject instanceof String)) {
+ path = (String) pathObject;
+ }
+
+ if (path == null) {
+ path = (String) session.getAttribute(PARAM_TARGET_PATH);
+ }
+
+ Object protocolObject = req.getAttribute(PARAM_TARGET_PROTOCOL);
+ String protocol = null;
+ if (protocolObject != null && (protocolObject instanceof String)) {
+ protocol = (String) protocolObject;
+ }
+
+ if (protocol == null) {
+ protocol = (String) session.getAttribute(PARAM_TARGET_PROTOCOL);
+ }
+
+ Logger.debug("dispatching to " + path + " protocol " + protocol);
+
+ if (path != null && protocol != null
+ && endpointMap.containsKey(path)) {
+ IModulInfo info = ModulStorage.getModuleByPath(path);
+ if (info == null) {
+ resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+ Logger.error("Path " + path + " has no module registered");
+ return;
+ }
+
+ ITargetConfiguration configuration = info.preProcess(req, resp);
+
+ if (!AuthenticationManager.isAuthenticated(req, resp)) {
+
+ session.setAttribute(PARAM_TARGET_PATH, path);
+ session.setAttribute(PARAM_TARGET_PROTOCOL, protocol);
+
+ AuthenticationManager.doAuthentication(req, resp,
+ configuration);
+ return;
+ }
+
+ 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);
+ return;
+ } catch (Throwable e) {
+ e.printStackTrace();
+ resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ }
+ }
+ }
+ resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+ } 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);
+ }
+}