Example usage for com.liferay.portal.kernel.servlet PortletServlet PortletServlet

List of usage examples for com.liferay.portal.kernel.servlet PortletServlet PortletServlet

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.servlet PortletServlet PortletServlet.

Prototype

PortletServlet

Source Link

Usage

From source file:com.liferay.mvc.freemarker.internal.FreeMarkerMVCContextHelper.java

License:Open Source License

public static void addPortletJSPTaglibSupport(FreeMarkerContext freeMarkerContext,
        PortletRequest portletRequest, PortletResponse portletResponse, Set<String> templateRegistry)
        throws Exception {

    HttpServletRequest request = PortalUtil.getHttpServletRequest(portletRequest);
    HttpServletResponse response = PortalUtil.getHttpServletResponse(portletResponse);

    FreeMarkerVariablesUtil.insertVariables(freeMarkerContext, request);

    Portlet portlet = (Portlet) request.getAttribute(WebKeys.RENDER_PORTLET);

    String servletContextName = portlet.getPortletApp().getServletContextName();

    final ServletContext servletContext = ServletContextPool.get(servletContextName);

    Object templateRegistryAttribute = servletContext.getAttribute(TEMPLATE_REGISTRY_ATTRIBUTE);

    if ((templateRegistry != null) && (templateRegistryAttribute == null)) {
        servletContext.setAttribute(TEMPLATE_REGISTRY_ATTRIBUTE, templateRegistry);
    }//  w  w w .  jav  a2  s  . com

    freeMarkerContext.put("fullTemplatesPath",
            StringPool.SLASH + servletContextName + FreeMarkerTemplateLoader.SERVLET_SEPARATOR);

    TemplateHashModel taglibsFactory = FreeMarkerTaglibFactoryUtil.createTaglibFactory(servletContext);

    freeMarkerContext.put("PortletJspTagLibs", taglibsFactory);

    ServletConfig servletConfig = (ServletConfig) portletRequest
            .getAttribute(PortletServlet.PORTLET_SERVLET_CONFIG);

    final PortletServlet portletServlet = new PortletServlet();

    portletServlet.init(servletConfig);

    ServletContextHashModel servletContextHashModel = new ServletContextHashModel(portletServlet,
            ObjectWrapper.DEFAULT_WRAPPER);

    freeMarkerContext.put("Application", servletContextHashModel);

    HttpRequestHashModel httpRequestHashModel = new HttpRequestHashModel(request, response,
            ObjectWrapper.DEFAULT_WRAPPER);

    freeMarkerContext.put("Request", httpRequestHashModel);
}