Search in sources :

Example 1 with ForumContext

use of net.jforum.context.ForumContext in project jforum2 by rafaelsteil.

the class OnlineUsersTest method setUp.

protected void setUp() throws Exception {
    new SessionFacade().setCacheEngine(new DefaultCacheEngine());
    RequestContext requestContext = new WebRequestContext(new FakeHttpRequest());
    ResponseContext responseContext = new WebResponseContext(new FakeHttpResponse());
    ForumContext forumContext = new JForumContext(requestContext.getContextPath(), SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION), requestContext, responseContext, false);
    JForumExecutionContext ex = JForumExecutionContext.get();
    ex.setForumContext(forumContext);
    JForumExecutionContext.set(ex);
    SystemGlobals.setValue(ConfigKeys.ANONYMOUS_USER_ID, Integer.toString(ANONYMOUS));
}
Also used : WebResponseContext(net.jforum.context.web.WebResponseContext) WebRequestContext(net.jforum.context.web.WebRequestContext) FakeHttpResponse(net.jforum.http.FakeHttpResponse) ResponseContext(net.jforum.context.ResponseContext) WebResponseContext(net.jforum.context.web.WebResponseContext) DefaultCacheEngine(net.jforum.cache.DefaultCacheEngine) JForumContext(net.jforum.context.JForumContext) ForumContext(net.jforum.context.ForumContext) RequestContext(net.jforum.context.RequestContext) WebRequestContext(net.jforum.context.web.WebRequestContext) FakeHttpRequest(net.jforum.http.FakeHttpRequest) JForumContext(net.jforum.context.JForumContext)

Example 2 with ForumContext

use of net.jforum.context.ForumContext 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)

Aggregations

ForumContext (net.jforum.context.ForumContext)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 SimpleHash (freemarker.template.SimpleHash)1 Template (freemarker.template.Template)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ServletException (javax.servlet.ServletException)1 DefaultCacheEngine (net.jforum.cache.DefaultCacheEngine)1 ExceptionWriter (net.jforum.exceptions.ExceptionWriter)1 FakeHttpRequest (net.jforum.http.FakeHttpRequest)1 FakeHttpResponse (net.jforum.http.FakeHttpResponse)1