1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.office.viewer.script

File DefaultOfficeViewerScriptService.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

4
28
6
1
196
102
9
0.32
4.67
6
1.5

Classes

Class Line # Actions
DefaultOfficeViewerScriptService 58 28 0% 9 4
0.894736889.5%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.office.viewer.script;
21   
22    import java.util.Collections;
23    import java.util.Map;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.artofsolving.jodconverter.document.DocumentFormat;
30    import org.slf4j.Logger;
31    import org.xwiki.bridge.DocumentAccessBridge;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.context.Execution;
35    import org.xwiki.model.reference.AttachmentReference;
36    import org.xwiki.model.reference.DocumentReference;
37    import org.xwiki.office.viewer.OfficeViewer;
38    import org.xwiki.office.viewer.OfficeViewerScriptService;
39    import org.xwiki.officeimporter.converter.OfficeConverter;
40    import org.xwiki.officeimporter.server.OfficeServer;
41    import org.xwiki.rendering.block.XDOM;
42    import org.xwiki.rendering.renderer.BlockRenderer;
43    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
44    import org.xwiki.rendering.renderer.printer.WikiPrinter;
45    import org.xwiki.rendering.syntax.Syntax;
46    import org.xwiki.rendering.transformation.TransformationContext;
47    import org.xwiki.rendering.transformation.TransformationManager;
48   
49    /**
50    * Default implementation of {@link OfficeViewerScriptService}.
51    *
52    * @since 2.5M2
53    * @version $Id: 0a81aa23de705f4f4a680efa5600362f5c506f9b $
54    */
55    @Component
56    @Named("officeviewer")
57    @Singleton
 
58    public class DefaultOfficeViewerScriptService implements OfficeViewerScriptService
59    {
60    /**
61    * The key used to save on the execution context the exception caught during office document view.
62    */
63    private static final String OFFICE_VIEW_EXCEPTION = "officeView.caughtException";
64   
65    /**
66    * The component used to view office documents.
67    */
68    @Inject
69    private OfficeViewer officeViewer;
70   
71    /**
72    * The component used to retrieve the office document converter, which knows the supported media types.
73    *
74    * @see #isMimeTypeSupported(String)
75    */
76    @Inject
77    private OfficeServer officeServer;
78   
79    /**
80    * Used to lookup various {@link BlockRenderer} implementations based on the output syntax.
81    */
82    @Inject
83    private ComponentManager componentManager;
84   
85    /**
86    * Reference to the current execution context, used to save the exception caught during office document view.
87    */
88    @Inject
89    private Execution execution;
90   
91    /**
92    * The component used to check access rights on the document holding the office attachment to be viewed.
93    */
94    @Inject
95    private DocumentAccessBridge documentAccessBridge;
96   
97    /**
98    * The component used to perform the XDOM transformations.
99    */
100    @Inject
101    private TransformationManager transformationManager;
102   
103    /**
104    * The logger to log.
105    */
106    @Inject
107    private Logger logger;
108   
 
109  1 toggle @Override
110    public Exception getCaughtException()
111    {
112  1 return (Exception) this.execution.getContext().getProperty(OFFICE_VIEW_EXCEPTION);
113    }
114   
 
115  2 toggle @Override
116    public String view(AttachmentReference attachmentReference)
117    {
118  2 Map<String, String> parameters = Collections.emptyMap();
119  2 return view(attachmentReference, parameters);
120    }
121   
 
122  2 toggle @Override
123    public String view(AttachmentReference attachmentReference, Map<String, String> parameters)
124    {
125    // Clear previous caught exception.
126  2 this.execution.getContext().removeProperty(OFFICE_VIEW_EXCEPTION);
127  2 try {
128  2 DocumentReference documentReference = attachmentReference.getDocumentReference();
129    // Check whether current user has view rights on the document containing the attachment.
130  1 if (!this.documentAccessBridge.isDocumentViewable(documentReference)) {
131  0 throw new RuntimeException("Inadequate privileges.");
132    }
133   
134    // Create the view and render the result.
135  1 Syntax fromSyntax = this.documentAccessBridge.getDocument(documentReference).getSyntax();
136  1 Syntax toSyntax = Syntax.XHTML_1_0;
137  1 return render(this.officeViewer.createView(attachmentReference, parameters), fromSyntax, toSyntax);
138    } catch (Exception e) {
139    // Save caught exception.
140  1 this.execution.getContext().setProperty(OFFICE_VIEW_EXCEPTION, e);
141  1 this.logger.error("Failed to view office document: " + attachmentReference, e);
142  1 return null;
143    }
144    }
145   
 
146  8 toggle @Override
147    public boolean isMimeTypeSupported(String mimeType)
148    {
149  8 return isConversionSupported(mimeType, "text/html");
150    }
151   
152    /**
153    * Use this method to check if the unidirectional conversion from a document format (input media type) to another
154    * document format (output media type) is supported by this converter.
155    *
156    * @param inputMediaType the media type of the input document
157    * @param outputMediaType the media type of the output document
158    * @return {@code true} if a document can be converted from the input media type to the output media type,
159    * {@code false} otherwise
160    */
 
161  8 toggle private boolean isConversionSupported(String inputMediaType, String outputMediaType)
162    {
163  8 OfficeConverter converter = this.officeServer.getConverter();
164  8 if (converter != null) {
165  8 DocumentFormat inputFormat = converter.getFormatRegistry().getFormatByMediaType(inputMediaType);
166  8 DocumentFormat outputFormat = converter.getFormatRegistry().getFormatByMediaType(outputMediaType);
167  8 return inputFormat != null && outputFormat != null
168    && outputFormat.getStoreProperties(inputFormat.getInputFamily()) != null;
169    } else {
170  0 return false;
171    }
172    }
173   
174    /**
175    * Renders the given XDOM into specified syntax.
176    *
177    * @param xdom the {@link XDOM} to be rendered
178    * @param fromSyntax the syntax for which to perform the transformations
179    * @param toSyntax expected output syntax
180    * @return string holding the result of rendering
181    * @throws Exception if an error occurs during rendering
182    */
 
183  1 toggle private String render(XDOM xdom, Syntax fromSyntax, Syntax toSyntax) throws Exception
184    {
185    // Perform the transformations. This is required for office presentations which use the gallery macro to display
186    // the slide images.
187  1 TransformationContext context = new TransformationContext(xdom, fromSyntax);
188  1 context.setTargetSyntax(toSyntax);
189  1 this.transformationManager.performTransformations(xdom, context);
190   
191  1 WikiPrinter printer = new DefaultWikiPrinter();
192  1 BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, toSyntax.toIdString());
193  1 renderer.render(xdom, printer);
194  1 return printer.toString();
195    }
196    }