Search in sources :

Example 1 with OfficeExporterURLFactory

use of com.xpn.xwiki.internal.export.OfficeExporterURLFactory in project xwiki-platform by xwiki.

the class ExportAction method export.

private String export(String format, XWikiContext context) throws XWikiException, IOException {
    // We currently use the PDF export infrastructure but we have to redesign the export code.
    XWikiURLFactory urlFactory = new OfficeExporterURLFactory();
    PdfExport exporter = new OfficeExporter();
    // Check if the office exporter supports the specified format.
    ExportType exportType = ((OfficeExporter) exporter).getExportType(format);
    // Note 2: we don't use the office server for PDF exports since it doesn't work OOB. Instead we use FOP.
    if ("pdf".equalsIgnoreCase(format)) {
        // The export format is PDF or the office converter can't be used (either it doesn't support the specified
        // format or the office server is not started).
        urlFactory = new PdfURLFactory();
        exporter = new PdfExportImpl();
        exportType = ExportType.PDF;
    } else if (exportType == null) {
        context.put("message", "core.export.formatUnknown");
        return "exception";
    }
    urlFactory.init(context);
    context.setURLFactory(urlFactory);
    handleRevision(context);
    XWikiDocument doc = context.getDoc();
    context.getResponse().setContentType(exportType.getMimeType());
    // Compute the name of the export. Since it's gong to be saved on the user's file system it needs to be a valid
    // File name. Thus we use the "path" serializer but replace the "/" separator by "_" since we're not computing
    // a directory hierarchy but a file name
    EntityReferenceSerializer<String> serializer = Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "path");
    String filename = serializer.serialize(doc.getDocumentReference()).replaceAll("/", "_");
    // Make sure we don't go over 255 chars since several filesystems don't support filename longer than that!
    filename = StringUtils.abbreviateMiddle(filename, "__", 255);
    context.getResponse().addHeader("Content-disposition", String.format("inline; filename=%s.%s", filename, exportType.getExtension()));
    exporter.export(doc, context.getResponse().getOutputStream(), exportType, context);
    return null;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) OfficeExporter(com.xpn.xwiki.internal.export.OfficeExporter) PdfExport(com.xpn.xwiki.pdf.api.PdfExport) ExportType(com.xpn.xwiki.pdf.api.PdfExport.ExportType) OfficeExporterURLFactory(com.xpn.xwiki.internal.export.OfficeExporterURLFactory) PdfURLFactory(com.xpn.xwiki.pdf.impl.PdfURLFactory) PdfExportImpl(com.xpn.xwiki.pdf.impl.PdfExportImpl)

Aggregations

XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 OfficeExporter (com.xpn.xwiki.internal.export.OfficeExporter)1 OfficeExporterURLFactory (com.xpn.xwiki.internal.export.OfficeExporterURLFactory)1 PdfExport (com.xpn.xwiki.pdf.api.PdfExport)1 ExportType (com.xpn.xwiki.pdf.api.PdfExport.ExportType)1 PdfExportImpl (com.xpn.xwiki.pdf.impl.PdfExportImpl)1 PdfURLFactory (com.xpn.xwiki.pdf.impl.PdfURLFactory)1