Search in sources :

Example 1 with WebContextBuilder

use of org.directwebremoting.WebContextFactory.WebContextBuilder in project ma-core-public by infiniteautomation.

the class StartupUtil method outOfContainerDestroy.

/**
 * Clean up the current thread when {@link #outOfContainerInit()} has been
 * called.
 * @param container The container created by {@link #outOfContainerInit()}.
 */
public void outOfContainerDestroy(Container container) {
    ServletLoggingOutput.unsetExecutionContext();
    WebContextBuilder webContextBuilder = (WebContextBuilder) container.getBean(WebContextBuilder.class.getName());
    if (webContextBuilder != null) {
        webContextBuilder.unset();
    }
}
Also used : WebContextBuilder(org.directwebremoting.WebContextFactory.WebContextBuilder)

Example 2 with WebContextBuilder

use of org.directwebremoting.WebContextFactory.WebContextBuilder in project ma-core-public by infiniteautomation.

the class DwrConvertTag method doStartTag.

@Override
public int doStartTag() throws JspException {
    ServletContext servletContext = pageContext.getServletContext();
    Container container = (Container) servletContext.getAttribute("DwrContainer");
    if (container == null)
        throw new JspException("Can't find 'DwrContainer' in servlet context");
    ConverterManager converterManager = (ConverterManager) container.getBean(ConverterManager.class.getName());
    final ScriptBuffer scriptBuffer = new ScriptBuffer();
    scriptBuffer.appendScript("return ");
    scriptBuffer.appendData(obj);
    WebContextBuilder webContextBuilder = (WebContextBuilder) servletContext.getAttribute(WebContextBuilder.class.getName());
    try {
        webContextBuilder.set((HttpServletRequest) pageContext.getRequest(), (HttpServletResponse) pageContext.getResponse(), null, servletContext, container);
        JspWriter out = pageContext.getOut();
        out.write("function() {");
        out.write(ScriptBufferUtil.createOutput(scriptBuffer, converterManager));
        out.write(";}()");
    } catch (IOException e) {
        throw new JspException("Error writing tag content", e);
    } catch (MarshallException e) {
        throw new JspException("Error marshalling object data", e);
    } finally {
        webContextBuilder.unset();
    }
    return EVAL_PAGE;
}
Also used : JspException(javax.servlet.jsp.JspException) Container(org.directwebremoting.Container) ConverterManager(org.directwebremoting.extend.ConverterManager) MarshallException(org.directwebremoting.extend.MarshallException) ServletContext(javax.servlet.ServletContext) WebContextBuilder(org.directwebremoting.WebContextFactory.WebContextBuilder) IOException(java.io.IOException) JspWriter(javax.servlet.jsp.JspWriter) ScriptBuffer(org.directwebremoting.ScriptBuffer)

Example 3 with WebContextBuilder

use of org.directwebremoting.WebContextFactory.WebContextBuilder in project ma-core-public by infiniteautomation.

the class StartupUtil method outOfContainerInit.

/**
 * A way to setup DWR outside of any Containers.
 * This method can also serve as a template for in container code wanting
 * to get DWR setup. Callers of this method should clean up after themselves
 * by calling {@link #outOfContainerDestroy(Container)}
 * @return A new initialized container.
 * @throws ServletException If the setup fails.
 */
public Container outOfContainerInit() throws ServletException {
    try {
        ServletConfig servletConfig = new FakeServletConfig("test", new FakeServletContext());
        ServletContext servletContext = servletConfig.getServletContext();
        StartupUtil.setupLogging(servletConfig, null);
        StartupUtil.logStartup(servletConfig);
        DefaultContainer container = ContainerUtil.createDefaultContainer(servletConfig);
        ContainerUtil.setupDefaultContainer(container, servletConfig);
        WebContextBuilder webContextBuilder = StartupUtil.initWebContext(servletConfig, servletContext, container);
        StartupUtil.initServerContext(servletConfig, servletContext, container);
        ContainerUtil.prepareForWebContextFilter(servletContext, servletConfig, container, webContextBuilder, null);
        ContainerUtil.configureContainerFully(container, servletConfig);
        ContainerUtil.publishContainer(container, servletConfig);
        return container;
    } catch (ServletException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new ServletException(ex);
    }
}
Also used : ServletException(javax.servlet.ServletException) FakeServletContext(org.directwebremoting.util.FakeServletContext) ServletConfig(javax.servlet.ServletConfig) FakeServletConfig(org.directwebremoting.util.FakeServletConfig) FakeServletContext(org.directwebremoting.util.FakeServletContext) ServletContext(javax.servlet.ServletContext) WebContextBuilder(org.directwebremoting.WebContextFactory.WebContextBuilder) ServletException(javax.servlet.ServletException) FakeServletConfig(org.directwebremoting.util.FakeServletConfig)

Example 4 with WebContextBuilder

use of org.directwebremoting.WebContextFactory.WebContextBuilder in project ma-core-public by infiniteautomation.

the class StartupUtil method initWebContext.

/**
 * Get the {@link WebContextFactory.WebContextBuilder} out of the
 * {@link Container}, configure it (call WebContextBuilder#set()) and use it
 * to configure the {@link WebContextFactory}.
 * @param servletConfig The servlet configuration
 * @param servletContext The servlet context
 * @param servlet The servlet that we are running under
 * @param container The container to save in the ServletContext
 * @return a new WebContextBuilder
 */
public static WebContextBuilder initWebContext(ServletConfig servletConfig, ServletContext servletContext, Container container) {
    WebContextBuilder webContextBuilder = (WebContextBuilder) container.getBean(WebContextBuilder.class.getName());
    WebContextFactory.setWebContextBuilder(webContextBuilder);
    webContextBuilder.set(null, null, servletConfig, servletContext, container);
    return webContextBuilder;
}
Also used : WebContextBuilder(org.directwebremoting.WebContextFactory.WebContextBuilder)

Example 5 with WebContextBuilder

use of org.directwebremoting.WebContextFactory.WebContextBuilder in project ma-core-public by infiniteautomation.

the class DwrWebContextFilter method doFilter.

/* (non-Javadoc)
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    ServletContext servletContext = filterConfig.getServletContext();
    Container container = (Container) servletContext.getAttribute(Container.class.getName());
    if (container == null) {
        log.error("DwrWebContextFilter can not find ServletContext attribute for the DWR Container. Is DwrServlet configured in this web-application?");
    }
    ServletConfig servletConfig = (ServletConfig) servletContext.getAttribute(ServletConfig.class.getName());
    if (servletConfig == null) {
        log.error("DwrWebContextFilter can not find ServletContext attribute for the ServletConfig.");
    }
    WebContextBuilder webContextBuilder = (WebContextBuilder) servletContext.getAttribute(WebContextBuilder.class.getName());
    if (webContextBuilder == null) {
        log.error("DwrWebContextFilter can not find ServletContext attribute for the WebContextBuilder. WebContext will not be available.");
    } else {
        try {
            webContextBuilder.set((HttpServletRequest) request, (HttpServletResponse) response, servletConfig, servletContext, container);
            // It is totally legitimate for a servlet to be unavailable
            // (e.g. Spring DwrController)
            HttpServlet servlet = (HttpServlet) servletContext.getAttribute(HttpServlet.class.getName());
            if (servlet != null) {
                ServletLoggingOutput.setExecutionContext(servlet);
            }
            chain.doFilter(request, response);
        } finally {
            webContextBuilder.unset();
            ServletLoggingOutput.unsetExecutionContext();
        }
    }
}
Also used : Container(org.directwebremoting.Container) HttpServlet(javax.servlet.http.HttpServlet) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) WebContextBuilder(org.directwebremoting.WebContextFactory.WebContextBuilder)

Aggregations

WebContextBuilder (org.directwebremoting.WebContextFactory.WebContextBuilder)5 ServletContext (javax.servlet.ServletContext)3 ServletConfig (javax.servlet.ServletConfig)2 Container (org.directwebremoting.Container)2 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 HttpServlet (javax.servlet.http.HttpServlet)1 JspException (javax.servlet.jsp.JspException)1 JspWriter (javax.servlet.jsp.JspWriter)1 ScriptBuffer (org.directwebremoting.ScriptBuffer)1 ConverterManager (org.directwebremoting.extend.ConverterManager)1 MarshallException (org.directwebremoting.extend.MarshallException)1 FakeServletConfig (org.directwebremoting.util.FakeServletConfig)1 FakeServletContext (org.directwebremoting.util.FakeServletContext)1