Search in sources :

Example 1 with RequestController

use of fi.internetix.smvc.controllers.RequestController in project pyramus by otavanopisto.

the class PyramusServletContextListener method loadPlugins.

@SuppressWarnings("unchecked")
private void loadPlugins() {
    try {
        PluginRepositoryDAO pluginRepositoryDAO = DAOFactory.getInstance().getPluginRepositoryDAO();
        List<PluginRepository> pluginRepositories = pluginRepositoryDAO.listAll();
        PluginManager pluginManager = PluginManager.initialize(getClass().getClassLoader(), pluginRepositories);
        PluginDAO pluginDAO = DAOFactory.getInstance().getPluginDAO();
        List<Plugin> enabledPlugins = pluginDAO.listByEnabled(Boolean.TRUE);
        for (Plugin plugin : enabledPlugins) {
            try {
                pluginManager.loadPlugin(plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion());
            } catch (Exception e) {
                Logging.logException("Failed to load plugin: " + plugin.getGroupId() + "." + plugin.getArtifactId() + ":" + plugin.getVersion(), e);
            }
        }
        pluginManager.registerPlugins();
        // Load additional request mappings from plugins
        List<PluginDescriptor> plugins = pluginManager.getPlugins();
        for (PluginDescriptor plugin : plugins) {
            Map<String, Class<?>> pageRequestControllers = plugin.getPageRequestControllers();
            if (pageRequestControllers != null) {
                for (Map.Entry<String, Class<?>> entry : pageRequestControllers.entrySet()) {
                    String className = entry.getValue().getName();
                    Class<? extends RequestController> pageController = (Class<? extends RequestController>) Class.forName(className, false, pluginManager.getPluginsClassLoader());
                    RequestController oldController = RequestControllerMapper.getRequestController(entry.getKey() + ".page");
                    if (oldController != null) {
                        // Save masked controllers for extending existing functionality by calling
                        // the masked controller's .process()
                        RequestControllerMapper.mapController(entry.getKey(), ".page.masked", oldController);
                    }
                    RequestControllerMapper.mapController(entry.getKey(), ".page", pageController.newInstance());
                }
            }
            Map<String, Class<?>> jsonRequestControllers = plugin.getJSONRequestControllers();
            if (jsonRequestControllers != null) {
                for (Map.Entry<String, Class<?>> entry : jsonRequestControllers.entrySet()) {
                    String className = entry.getValue().getName();
                    Class<? extends RequestController> pageController = (Class<? extends RequestController>) Class.forName(className, false, pluginManager.getPluginsClassLoader());
                    RequestController oldController = RequestControllerMapper.getRequestController(entry.getKey() + ".json");
                    if (oldController != null) {
                        // Save masked controllers for extending existing functionality by calling
                        // the masked controller's .process()
                        RequestControllerMapper.mapController(entry.getKey(), ".json.masked", oldController);
                    }
                    RequestControllerMapper.mapController(entry.getKey(), ".json", pageController.newInstance());
                }
            }
            Map<String, Class<?>> binaryRequestControllers = plugin.getBinaryRequestControllers();
            if (binaryRequestControllers != null) {
                for (Map.Entry<String, Class<?>> entry : binaryRequestControllers.entrySet()) {
                    String className = entry.getValue().getName();
                    Class<? extends RequestController> pageController = (Class<? extends RequestController>) Class.forName(className, false, pluginManager.getPluginsClassLoader());
                    RequestController oldController = RequestControllerMapper.getRequestController(entry.getKey() + ".binary");
                    if (oldController != null) {
                        // Save masked controllers for extending existing functionality by calling
                        // the masked controller's .process()
                        RequestControllerMapper.mapController(entry.getKey(), ".binary.masked", oldController);
                    }
                    RequestControllerMapper.mapController(entry.getKey(), ".binary", pageController.newInstance());
                }
            }
        }
    } catch (Exception e) {
        Logging.logException("Plugins loading failed", e);
    }
    for (Webhook webhook : webhookDAO.listAll()) {
        webhooks.addWebhook(webhook.getUrl(), webhook.getSecret());
    }
    ;
}
Also used : PluginRepositoryDAO(fi.otavanopisto.pyramus.dao.plugins.PluginRepositoryDAO) PluginDAO(fi.otavanopisto.pyramus.dao.plugins.PluginDAO) PluginRepository(fi.otavanopisto.pyramus.domainmodel.plugins.PluginRepository) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PluginManager(fi.otavanopisto.pyramus.plugin.PluginManager) PluginDescriptor(fi.otavanopisto.pyramus.plugin.PluginDescriptor) RequestController(fi.internetix.smvc.controllers.RequestController) Webhook(fi.otavanopisto.pyramus.domainmodel.webhooks.Webhook) Map(java.util.Map) Plugin(fi.otavanopisto.pyramus.domainmodel.plugins.Plugin)

Example 2 with RequestController

use of fi.internetix.smvc.controllers.RequestController in project pyramus by otavanopisto.

the class BreadcrumbFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    try {
        if (request instanceof HttpServletRequest) {
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            String uri = httpRequest.getRequestURI();
            String ctxPath = httpRequest.getContextPath();
            String controllerName = uri.substring(ctxPath.length() + 1);
            // TODO: Needed??
            // if (StringUtils.isNotBlank(applicationPath)) {
            // controllerName = controllerName.substring(applicationPath.length());
            // }
            RequestController requestController = RequestControllerMapper.getRequestController(controllerName);
            if ((requestController instanceof PyramusViewController || requestController instanceof PyramusViewController2) && requestController instanceof Breadcrumbable) {
                BreadcrumbHandler breadcrumbHandler = getBreadcrumbHandler(httpRequest);
                if (request.getParameter("resetbreadcrumb") != null) {
                    breadcrumbHandler.clear();
                }
                if (requestController instanceof Breadcrumbable && "GET".equals(httpRequest.getMethod())) {
                    Breadcrumbable breadcrumbable = (Breadcrumbable) requestController;
                    breadcrumbHandler.process(httpRequest, breadcrumbable);
                }
            }
        }
    } finally {
        chain.doFilter(request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PyramusViewController(fi.otavanopisto.pyramus.framework.PyramusViewController) Breadcrumbable(fi.otavanopisto.pyramus.breadcrumbs.Breadcrumbable) RequestController(fi.internetix.smvc.controllers.RequestController) PyramusViewController2(fi.otavanopisto.pyramus.framework.PyramusViewController2) BreadcrumbHandler(fi.otavanopisto.pyramus.breadcrumbs.BreadcrumbHandler)

Example 3 with RequestController

use of fi.internetix.smvc.controllers.RequestController in project pyramus by otavanopisto.

the class Servlet method doService.

private void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    try {
        userTransaction.begin();
    } catch (Exception e) {
        Logging.logException(e);
        throw new ServletException(e);
    }
    RequestContext requestContext = null;
    RequestController requestController;
    RequestDispatchContext dispatchContext;
    if (requestDispatcher != null && requestDispatcher.canHandle(request, response)) {
        dispatchContext = requestDispatcher.getContext(request, response);
        requestController = dispatchContext.getRequestController();
    } else {
        String uri = request.getRequestURI();
        String ctxPath = request.getContextPath();
        String controllerName = uri.substring(ctxPath.length() + 1);
        if (StringUtils.isNotBlank(applicationPath)) {
            controllerName = controllerName.substring(applicationPath.length());
        }
        requestController = RequestControllerMapper.getRequestController(controllerName);
        dispatchContext = new RequestDispatchContext(requestController, new DefaultParameterHandlerImpl(request, decodeGETUtf));
    }
    int statusCode = StatusCode.OK;
    try {
        if (requestController == null) {
            requestContext = new PageRequestContext(dispatchContext, request, response, getServletContext(), errorJspPage);
            throw new PageNotFoundException(request.getLocale());
        } else if (requestController instanceof PageController) {
            requestContext = new PageRequestContext(dispatchContext, request, response, getServletContext(), errorJspPage);
        } else if (requestController instanceof JSONRequestController) {
            requestContext = new JSONRequestContext(dispatchContext, request, response, getServletContext());
        } else if (requestController instanceof BinaryRequestController) {
            requestContext = new BinaryRequestContext(dispatchContext, request, response, getServletContext());
        }
        // Let the controller authorize the request. Most common exceptions thrown include
        // LoginRequiredException and AccessDeniedException
        requestController.authorize(requestContext);
        if (requestController instanceof PageController) {
            ((PageController) requestController).process((PageRequestContext) requestContext);
        } else if (requestController instanceof JSONRequestController) {
            ((JSONRequestController) requestController).process((JSONRequestContext) requestContext);
        } else if (requestController instanceof BinaryRequestController) {
            ((BinaryRequestController) requestController).process((BinaryRequestContext) requestContext);
        }
    } catch (LoginRequiredException lre) {
        if (platformErrorListener != null)
            platformErrorListener.onLoginRequiredException(request, response, lre);
        Logging.logInfo("Login required for " + getCurrentUrl(request, true));
        if (requestController instanceof PageController) {
            HttpSession session = requestContext.getRequest().getSession(true);
            session.setAttribute("loginRedirectUrl", lre.getRedirectUrl());
            if (lre.getContextType() != null && lre.getContextId() != null) {
                session.setAttribute("loginContextType", lre.getContextType());
                session.setAttribute("loginContextId", lre.getContextId());
            }
            requestContext.setRedirectURL(loginUrl);
        } else {
            // TODO LoginRequiredException for requests other than pages?
            statusCode = lre.getStatusCode();
            requestContext.addMessage(Severity.WARNING, lre.getMessage());
        }
    } catch (PageNotFoundException pnfe) {
        if (platformErrorListener != null)
            platformErrorListener.onPageNotFoundException(request, response, pnfe);
        Logging.logInfo("404 - " + getCurrentUrl(request, true));
        statusCode = pnfe.getStatusCode();
        if (requestContext != null) {
            requestContext.getResponse().setStatus(HttpServletResponse.SC_NOT_FOUND);
            requestContext.addMessage(Severity.WARNING, pnfe.getMessage());
        } else {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    } catch (AccessDeniedException ade) {
        if (platformErrorListener != null)
            platformErrorListener.onAccessDeniedException(request, response, ade);
        Logging.logInfo("403 - " + getCurrentUrl(request, true) + " - " + requestContext.getLoggedUserId());
        statusCode = ade.getStatusCode();
        requestContext.getResponse().setStatus(HttpServletResponse.SC_FORBIDDEN);
        requestContext.addMessage(Severity.WARNING, ade.getMessage());
    } catch (InvalidLoginException ile) {
        Logging.logInfo("Invalid login credentials");
        statusCode = ile.getStatusCode();
        requestContext.addMessage(Severity.ERROR, ile.getMessage());
    } catch (AlreadyLoggedInException ile) {
        Logging.logInfo("Already logged in");
        statusCode = ile.getStatusCode();
        requestContext.addMessage(Severity.ERROR, ile.getMessage());
    } catch (SmvcRuntimeException pre) {
        if (platformErrorListener != null)
            platformErrorListener.onSmvcRuntimeException(request, response, pre);
        Logging.logException(pre);
        statusCode = pre.getStatusCode();
        requestContext.addMessage(Severity.ERROR, pre.getMessage());
    } catch (Exception e) {
        if (platformErrorListener != null)
            platformErrorListener.onUncontrolledException(request, response, e);
        // All other exceptions are considered to be fatal and unexpected, so the request
        // transaction is rolled back, the stack trace of the exception is printed out, and
        // an error view is shown
        Logging.logException(e);
        statusCode = StatusCode.UNDEFINED;
        requestContext.addMessage(Severity.CRITICAL, e.getMessage());
    } finally {
        try {
            // Pre-commit response
            requestContext.writePreCommitResponse(statusCode);
            if (statusCode == StatusCode.OK) {
                userTransaction.commit();
            } else {
                userTransaction.rollback();
            }
            // Post-commit response
            requestContext.writePostCommitResponse(statusCode);
        } catch (Exception e) {
            if (platformErrorListener != null)
                platformErrorListener.onTransactionCommitException(request, response, e);
            Logging.logException(e);
            throw new ServletException(e);
        }
    }
}
Also used : LoginRequiredException(fi.internetix.smvc.LoginRequiredException) AccessDeniedException(fi.internetix.smvc.AccessDeniedException) HttpSession(javax.servlet.http.HttpSession) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) AlreadyLoggedInException(fi.internetix.smvc.AlreadyLoggedInException) BinaryRequestContext(fi.internetix.smvc.controllers.BinaryRequestContext) LoginRequiredException(fi.internetix.smvc.LoginRequiredException) ServletException(javax.servlet.ServletException) AlreadyLoggedInException(fi.internetix.smvc.AlreadyLoggedInException) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) PageNotFoundException(fi.internetix.smvc.PageNotFoundException) InvalidLoginException(fi.internetix.smvc.InvalidLoginException) AccessDeniedException(fi.internetix.smvc.AccessDeniedException) ServletException(javax.servlet.ServletException) JSONRequestController(fi.internetix.smvc.controllers.JSONRequestController) PageNotFoundException(fi.internetix.smvc.PageNotFoundException) PageController(fi.internetix.smvc.controllers.PageController) BinaryRequestController(fi.internetix.smvc.controllers.BinaryRequestController) BinaryRequestController(fi.internetix.smvc.controllers.BinaryRequestController) RequestController(fi.internetix.smvc.controllers.RequestController) JSONRequestController(fi.internetix.smvc.controllers.JSONRequestController) InvalidLoginException(fi.internetix.smvc.InvalidLoginException) BinaryRequestContext(fi.internetix.smvc.controllers.BinaryRequestContext) JSONRequestContext(fi.internetix.smvc.controllers.JSONRequestContext) RequestContext(fi.internetix.smvc.controllers.RequestContext) PageRequestContext(fi.internetix.smvc.controllers.PageRequestContext) PageRequestContext(fi.internetix.smvc.controllers.PageRequestContext) JSONRequestContext(fi.internetix.smvc.controllers.JSONRequestContext)

Aggregations

RequestController (fi.internetix.smvc.controllers.RequestController)3 AccessDeniedException (fi.internetix.smvc.AccessDeniedException)1 AlreadyLoggedInException (fi.internetix.smvc.AlreadyLoggedInException)1 InvalidLoginException (fi.internetix.smvc.InvalidLoginException)1 LoginRequiredException (fi.internetix.smvc.LoginRequiredException)1 PageNotFoundException (fi.internetix.smvc.PageNotFoundException)1 SmvcRuntimeException (fi.internetix.smvc.SmvcRuntimeException)1 BinaryRequestContext (fi.internetix.smvc.controllers.BinaryRequestContext)1 BinaryRequestController (fi.internetix.smvc.controllers.BinaryRequestController)1 JSONRequestContext (fi.internetix.smvc.controllers.JSONRequestContext)1 JSONRequestController (fi.internetix.smvc.controllers.JSONRequestController)1 PageController (fi.internetix.smvc.controllers.PageController)1 PageRequestContext (fi.internetix.smvc.controllers.PageRequestContext)1 RequestContext (fi.internetix.smvc.controllers.RequestContext)1 BreadcrumbHandler (fi.otavanopisto.pyramus.breadcrumbs.BreadcrumbHandler)1 Breadcrumbable (fi.otavanopisto.pyramus.breadcrumbs.Breadcrumbable)1 PluginDAO (fi.otavanopisto.pyramus.dao.plugins.PluginDAO)1 PluginRepositoryDAO (fi.otavanopisto.pyramus.dao.plugins.PluginRepositoryDAO)1 Plugin (fi.otavanopisto.pyramus.domainmodel.plugins.Plugin)1 PluginRepository (fi.otavanopisto.pyramus.domainmodel.plugins.PluginRepository)1