Search in sources :

Example 6 with RendererException

use of org.apache.maven.doxia.siterenderer.RendererException 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)

Example 7 with RendererException

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

the class AbstractProjectInfoReport method execute.

@Override
public void execute() throws MojoExecutionException {
    if (!canGenerateReport()) {
        return;
    }
    // TODO: push to a helper? Could still be improved by taking more of the site information from the site plugin
    Writer writer = null;
    try {
        String filename = getOutputName() + ".html";
        DecorationModel model = new DecorationModel();
        model.setBody(new Body());
        Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put("outputEncoding", "UTF-8");
        attributes.put("project", project);
        Locale locale = Locale.getDefault();
        Artifact defaultSkin = siteTool.getDefaultSkinArtifact(localRepository, project.getRemoteArtifactRepositories());
        SiteRenderingContext siteContext = siteRenderer.createContextForSkin(defaultSkin.getFile(), attributes, model, getName(locale), locale);
        RenderingContext context = new RenderingContext(outputDirectory, filename);
        SiteRendererSink sink = new SiteRendererSink(context);
        generate(sink, null, locale);
        outputDirectory.mkdirs();
        writer = new OutputStreamWriter(new FileOutputStream(new File(outputDirectory, filename)), "UTF-8");
        siteRenderer.generateDocument(writer, sink, siteContext);
        siteRenderer.copyResources(siteContext, new File(project.getBasedir(), "src/site/resources"), outputDirectory);
        writer.close();
        writer = null;
    } catch (RendererException e) {
        throw new MojoExecutionException("An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
    } catch (IOException e) {
        throw new MojoExecutionException("An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
    } catch (SiteToolException e) {
        throw new MojoExecutionException("An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
    } catch (MavenReportException e) {
        throw new MojoExecutionException("An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e);
    } finally {
        IOUtil.close(writer);
    }
}
Also used : Locale(java.util.Locale) RenderingContext(org.apache.maven.doxia.siterenderer.RenderingContext) SiteRenderingContext(org.apache.maven.doxia.siterenderer.SiteRenderingContext) DecorationModel(org.apache.maven.doxia.site.decoration.DecorationModel) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact) SiteToolException(org.apache.maven.doxia.tools.SiteToolException) RendererException(org.apache.maven.doxia.siterenderer.RendererException) SiteRenderingContext(org.apache.maven.doxia.siterenderer.SiteRenderingContext) SiteRendererSink(org.apache.maven.doxia.siterenderer.sink.SiteRendererSink) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Body(org.apache.maven.doxia.site.decoration.Body) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) MavenReportException(org.apache.maven.reporting.MavenReportException)

Aggregations

RendererException (org.apache.maven.doxia.siterenderer.RendererException)7 IOException (java.io.IOException)6 File (java.io.File)5 Locale (java.util.Locale)5 SiteRenderingContext (org.apache.maven.doxia.siterenderer.SiteRenderingContext)5 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)5 DecorationModel (org.apache.maven.doxia.site.decoration.DecorationModel)4 Writer (java.io.Writer)3 HashMap (java.util.HashMap)3 SiteRendererSink (org.apache.maven.doxia.siterenderer.sink.SiteRendererSink)3 SiteToolException (org.apache.maven.doxia.tools.SiteToolException)3 MavenReportException (org.apache.maven.reporting.MavenReportException)3 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Map (java.util.Map)2 Artifact (org.apache.maven.artifact.Artifact)2 Body (org.apache.maven.doxia.site.decoration.Body)2 LinkedHashMap (java.util.LinkedHashMap)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1