Search in sources :

Example 6 with DocumentRenderer

use of org.apache.maven.doxia.siterenderer.DocumentRenderer in project maven-plugins by apache.

the class DoxiaFilter method doFilter.

/**
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) servletRequest;
    // ----------------------------------------------------------------------
    // Handle the servlet path
    // ----------------------------------------------------------------------
    String path = req.getServletPath();
    // welcome file
    if (path.endsWith("/")) {
        path += "index.html";
    }
    // Remove the /
    path = path.substring(1);
    // Handle locale request
    SiteRenderingContext context;
    Map<String, DocumentRenderer> documents;
    SiteRenderingContext generatedSiteContext;
    String localeWanted = null;
    for (Locale locale : localesList) {
        if (path.startsWith(locale.getLanguage() + "/")) {
            localeWanted = locale.toString();
            path = path.substring(locale.getLanguage().length() + 1);
        }
    }
    if (localeWanted == null) {
        DoxiaBean defaultDoxiaBean = i18nDoxiaContexts.get("default");
        if (defaultDoxiaBean == null) {
            throw new ServletException("No doxia bean found for the default locale");
        }
        context = defaultDoxiaBean.getContext();
        documents = defaultDoxiaBean.getDocuments();
        generatedSiteContext = defaultDoxiaBean.getGeneratedSiteContext();
    } else {
        DoxiaBean i18nDoxiaBean = i18nDoxiaContexts.get(localeWanted);
        if (i18nDoxiaBean == null) {
            throw new ServletException("No doxia bean found for the locale " + localeWanted);
        }
        context = i18nDoxiaBean.getContext();
        documents = i18nDoxiaBean.getDocuments();
        generatedSiteContext = i18nDoxiaBean.getGeneratedSiteContext();
    }
    // ----------------------------------------------------------------------
    if (documents.containsKey(path)) {
        try {
            DocumentRenderer renderer = documents.get(path);
            renderer.renderDocument(servletResponse.getWriter(), siteRenderer, context);
            if (renderer instanceof ReportDocumentRenderer) {
                ReportDocumentRenderer reportDocumentRenderer = (ReportDocumentRenderer) renderer;
                if (reportDocumentRenderer.isExternalReport()) {
                    try {
                        filterChain.doFilter(servletRequest, servletResponse);
                    } catch (Exception e) {
                        throw new ServletException(e);
                    }
                }
            }
            return;
        } catch (RendererException e) {
            throw new ServletException(e);
        }
    } else if (generatedSiteContext != null) {
        try {
            Map<String, DocumentRenderer> locateDocuments = siteRenderer.locateDocumentFiles(generatedSiteContext);
            if (locateDocuments.containsKey(path)) {
                DocumentRenderer renderer = locateDocuments.get(path);
                renderer.renderDocument(servletResponse.getWriter(), siteRenderer, generatedSiteContext);
                return;
            }
        } catch (RendererException e) {
            throw new ServletException(e);
        }
    }
    filterChain.doFilter(servletRequest, servletResponse);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Locale(java.util.Locale) ServletException(javax.servlet.ServletException) RendererException(org.apache.maven.doxia.siterenderer.RendererException) SiteRenderingContext(org.apache.maven.doxia.siterenderer.SiteRenderingContext) ReportDocumentRenderer(org.apache.maven.plugins.site.render.ReportDocumentRenderer) DocumentRenderer(org.apache.maven.doxia.siterenderer.DocumentRenderer) ReportDocumentRenderer(org.apache.maven.plugins.site.render.ReportDocumentRenderer) Map(java.util.Map) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) RendererException(org.apache.maven.doxia.siterenderer.RendererException)

Aggregations

DocumentRenderer (org.apache.maven.doxia.siterenderer.DocumentRenderer)6 SiteRenderingContext (org.apache.maven.doxia.siterenderer.SiteRenderingContext)5 ArrayList (java.util.ArrayList)3 MavenReport (org.apache.maven.reporting.MavenReport)3 MavenReportExecution (org.apache.maven.reporting.exec.MavenReportExecution)3 File (java.io.File)2 IOException (java.io.IOException)2 Locale (java.util.Locale)2 Map (java.util.Map)2 DoxiaDocumentRenderer (org.apache.maven.doxia.siterenderer.DoxiaDocumentRenderer)2 RenderingContext (org.apache.maven.doxia.siterenderer.RenderingContext)2 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1