Search in sources :

Example 1 with SimpleHash

use of freemarker.template.SimpleHash in project jforum2 by rafaelsteil.

the class InstallServlet method service.

/** 
	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
	 */
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    try {
        String encoding = SystemGlobals.getValue(ConfigKeys.ENCODING);
        req.setCharacterEncoding(encoding);
        // Request
        RequestContext request = new WebRequestContext(req);
        ResponseContext response = new WebResponseContext(res);
        request.setCharacterEncoding(encoding);
        JForumExecutionContext ex = JForumExecutionContext.get();
        ForumContext forumContext = new JForumContext(request.getContextPath(), SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION), request, response, false);
        ex.setForumContext(forumContext);
        // Assigns the information to user's thread 
        JForumExecutionContext.set(ex);
        // Context
        SimpleHash context = JForumExecutionContext.getTemplateContext();
        context.put("contextPath", req.getContextPath());
        context.put("serverName", req.getServerName());
        context.put("templateName", "default");
        context.put("serverPort", Integer.toString(req.getServerPort()));
        context.put("I18n", I18n.getInstance());
        context.put("encoding", encoding);
        context.put("extension", SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
        context.put("JForumContext", forumContext);
        context.put("version", SystemGlobals.getValue(ConfigKeys.VERSION));
        if (SystemGlobals.getBoolValue(ConfigKeys.INSTALLED)) {
            JForumExecutionContext.setRedirect(request.getContextPath() + "/forums/list" + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
        } else {
            // Module and Action
            String moduleClass = ModulesRepository.getModuleClass(request.getModule());
            context.put("moduleName", request.getModule());
            context.put("action", request.getAction());
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), encoding));
            try {
                if (moduleClass != null) {
                    // Here we go, baby
                    Command c = (Command) Class.forName(moduleClass).newInstance();
                    Template template = c.process(request, response, context);
                    if (JForumExecutionContext.getRedirectTo() == null) {
                        response.setContentType("text/html; charset=" + encoding);
                        template.process(context, out);
                        out.flush();
                    }
                }
            } catch (Exception e) {
                response.setContentType("text/html; charset=" + encoding);
                if (out != null) {
                    new ExceptionWriter().handleExceptionData(e, out, request);
                } else {
                    new ExceptionWriter().handleExceptionData(e, new BufferedWriter(new OutputStreamWriter(response.getOutputStream())), request);
                }
            }
        }
        String redirectTo = JForumExecutionContext.getRedirectTo();
        if (redirectTo != null) {
            response.sendRedirect(response.encodeRedirectURL(redirectTo));
        }
    } finally {
        JForumExecutionContext.finish();
    }
}
Also used : ExceptionWriter(net.jforum.exceptions.ExceptionWriter) JForumContext(net.jforum.context.JForumContext) ForumContext(net.jforum.context.ForumContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Template(freemarker.template.Template) WebResponseContext(net.jforum.context.web.WebResponseContext) WebRequestContext(net.jforum.context.web.WebRequestContext) ResponseContext(net.jforum.context.ResponseContext) WebResponseContext(net.jforum.context.web.WebResponseContext) SimpleHash(freemarker.template.SimpleHash) OutputStreamWriter(java.io.OutputStreamWriter) RequestContext(net.jforum.context.RequestContext) WebRequestContext(net.jforum.context.web.WebRequestContext) JForumContext(net.jforum.context.JForumContext)

Example 2 with SimpleHash

use of freemarker.template.SimpleHash in project jforum2 by rafaelsteil.

the class JForum method service.

/**
	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    Writer out = null;
    JForumContext forumContext = null;
    RequestContext request = null;
    ResponseContext response = null;
    String encoding = SystemGlobals.getValue(ConfigKeys.ENCODING);
    try {
        // Initializes the execution context
        JForumExecutionContext ex = JForumExecutionContext.get();
        request = new WebRequestContext(req);
        response = new WebResponseContext(res);
        this.checkDatabaseStatus();
        forumContext = new JForumContext(request.getContextPath(), SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION), request, response);
        ex.setForumContext(forumContext);
        JForumExecutionContext.set(ex);
        // Setup stuff
        SimpleHash context = JForumExecutionContext.getTemplateContext();
        ControllerUtils utils = new ControllerUtils();
        utils.refreshSession();
        context.put("logged", SessionFacade.isLogged());
        // Process security data
        SecurityRepository.load(SessionFacade.getUserSession().getUserId());
        utils.prepareTemplateContext(context, forumContext);
        String module = request.getModule();
        // Gets the module class name
        String moduleClass = module != null ? ModulesRepository.getModuleClass(module) : null;
        if (moduleClass == null) {
            // Module not found, send 404 not found response
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        } else {
            boolean shouldBan = this.shouldBan(request.getRemoteAddr());
            if (!shouldBan) {
                context.put("moduleName", module);
                context.put("action", request.getAction());
            } else {
                moduleClass = ModulesRepository.getModuleClass("forums");
                context.put("moduleName", "forums");
                ((WebRequestContext) request).changeAction("banned");
            }
            if (shouldBan && SystemGlobals.getBoolValue(ConfigKeys.BANLIST_SEND_403FORBIDDEN)) {
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
            } else {
                context.put("language", I18n.getUserLanguage());
                context.put("session", SessionFacade.getUserSession());
                context.put("request", req);
                context.put("response", response);
                out = this.processCommand(out, request, response, encoding, context, moduleClass);
            }
        }
    } catch (Exception e) {
        this.handleException(out, response, encoding, e, request);
    } finally {
        this.handleFinally(out, forumContext, response);
    }
}
Also used : WebResponseContext(net.jforum.context.web.WebResponseContext) WebRequestContext(net.jforum.context.web.WebRequestContext) ResponseContext(net.jforum.context.ResponseContext) WebResponseContext(net.jforum.context.web.WebResponseContext) SimpleHash(freemarker.template.SimpleHash) WebRequestContext(net.jforum.context.web.WebRequestContext) RequestContext(net.jforum.context.RequestContext) OutputStreamWriter(java.io.OutputStreamWriter) ExceptionWriter(net.jforum.exceptions.ExceptionWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) ServletException(javax.servlet.ServletException) ForumStartupException(net.jforum.exceptions.ForumStartupException) IOException(java.io.IOException) JForumContext(net.jforum.context.JForumContext)

Example 3 with SimpleHash

use of freemarker.template.SimpleHash in project jforum2 by rafaelsteil.

the class POPPostAction method insertMessage.

/**
	 * Calls {@link PostAction#insertSave()}
	 * @param m the mail message
	 * @param user the user who's sent the message
	 */
private void insertMessage(POPMessage m, User user) {
    this.addDataToRequest(m, user);
    PostAction postAction = new PostAction(JForumExecutionContext.getRequest(), new SimpleHash());
    postAction.insertSave();
}
Also used : SimpleHash(freemarker.template.SimpleHash) PostAction(net.jforum.view.forum.PostAction)

Example 4 with SimpleHash

use of freemarker.template.SimpleHash in project jforum2 by rafaelsteil.

the class TopicsCommon method topicListingBase.

/**
	 * Common properties to be used when showing topic data
	 */
public static void topicListingBase() {
    SimpleHash context = JForumExecutionContext.getTemplateContext();
    // Topic Types
    context.put("TOPIC_ANNOUNCE", new Integer(Topic.TYPE_ANNOUNCE));
    context.put("TOPIC_STICKY", new Integer(Topic.TYPE_STICKY));
    context.put("TOPIC_NORMAL", new Integer(Topic.TYPE_NORMAL));
    // Topic Status
    context.put("STATUS_LOCKED", new Integer(Topic.STATUS_LOCKED));
    context.put("STATUS_UNLOCKED", new Integer(Topic.STATUS_UNLOCKED));
    // Moderation
    PermissionControl pc = SecurityRepository.get(SessionFacade.getUserSession().getUserId());
    context.put("moderator", pc.canAccess(SecurityConstants.PERM_MODERATION));
    context.put("can_remove_posts", pc.canAccess(SecurityConstants.PERM_MODERATION_POST_REMOVE));
    context.put("can_move_topics", pc.canAccess(SecurityConstants.PERM_MODERATION_TOPIC_MOVE));
    context.put("can_lockUnlock_topics", pc.canAccess(SecurityConstants.PERM_MODERATION_TOPIC_LOCK_UNLOCK));
    context.put("rssEnabled", SystemGlobals.getBoolValue(ConfigKeys.RSS_ENABLED));
}
Also used : PermissionControl(net.jforum.security.PermissionControl) SimpleHash(freemarker.template.SimpleHash)

Example 5 with SimpleHash

use of freemarker.template.SimpleHash in project jforum2 by rafaelsteil.

the class ExceptionWriter method handleExceptionData.

public void handleExceptionData(Throwable t, Writer w, RequestContext request) {
    StringWriter strWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(strWriter);
    t.printStackTrace(writer);
    String currentUrl = this.extractCurrentUrl(request);
    writer.write(currentUrl);
    writer.close();
    try {
        logger.error(strWriter);
        String message = "";
        Throwable cause = t.getCause();
        while (cause != null) {
            message = cause.toString();
            cause = cause.getCause();
        }
        if (message == null || message.equals("")) {
            message = t.getMessage();
        }
        if (message == null || message.equals("")) {
            message = t.toString();
        }
        boolean canViewStackTrace = !SystemGlobals.getBoolValue(ConfigKeys.STACKTRACE_MODERATORS_ONLY) || (SessionFacade.isLogged() && SessionFacade.getUserSession().isModerator());
        String filter = "[<>]";
        String stackTrace = canViewStackTrace ? strWriter.toString() : "Only moderators can view stack trace.";
        stackTrace = stackTrace.replaceAll(filter, "");
        message = message.replaceAll(filter, "");
        SimpleHash templateContext = JForumExecutionContext.getTemplateContext();
        templateContext.put("stackTrace", stackTrace);
        templateContext.put("message", message);
        Template template = JForumExecutionContext.templateConfig().getTemplate("exception.html");
        template.process(templateContext, w);
    } catch (Exception e) {
        strWriter = new StringWriter();
        writer = new PrintWriter(strWriter);
        e.printStackTrace(writer);
        writer.close();
        logger.error(strWriter);
    }
}
Also used : StringWriter(java.io.StringWriter) SimpleHash(freemarker.template.SimpleHash) PrintWriter(java.io.PrintWriter) Template(freemarker.template.Template)

Aggregations

SimpleHash (freemarker.template.SimpleHash)18 Template (freemarker.template.Template)5 IOException (java.io.IOException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 Writer (java.io.Writer)3 Locale (java.util.Locale)3 BufferedWriter (java.io.BufferedWriter)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ServletException (javax.servlet.ServletException)2 JForumContext (net.jforum.context.JForumContext)2 RequestContext (net.jforum.context.RequestContext)2 ResponseContext (net.jforum.context.ResponseContext)2 WebRequestContext (net.jforum.context.web.WebRequestContext)2 WebResponseContext (net.jforum.context.web.WebResponseContext)2 ExceptionWriter (net.jforum.exceptions.ExceptionWriter)2