Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
AbstractContentResourceReferenceHandler | 42 | 7 | 0% | 2 | 1 |
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.vfs.internal; | |
21 | ||
22 | import java.io.BufferedInputStream; | |
23 | import java.io.InputStream; | |
24 | ||
25 | import javax.inject.Inject; | |
26 | ||
27 | import org.apache.commons.io.IOUtils; | |
28 | import org.apache.tika.Tika; | |
29 | import org.xwiki.container.Container; | |
30 | import org.xwiki.container.Response; | |
31 | import org.xwiki.resource.AbstractResourceReferenceHandler; | |
32 | import org.xwiki.resource.ResourceReferenceHandlerException; | |
33 | import org.xwiki.resource.ResourceType; | |
34 | ||
35 | /** | |
36 | * Helper to implement {@link org.xwiki.resource.ResourceReferenceHandler} components that return content to the | |
37 | * Container's output stream. | |
38 | * | |
39 | * @version $Id: 86b5ebdc8451fb37a0c092bacf0a55cc114a6d1d $ | |
40 | * @since 7.4M2 | |
41 | */ | |
42 | public abstract class AbstractContentResourceReferenceHandler extends AbstractResourceReferenceHandler<ResourceType> | |
43 | { | |
44 | @Inject | |
45 | private Container container; | |
46 | ||
47 | /** | |
48 | * Used to determine the Content Type of the requested resource files. | |
49 | */ | |
50 | private Tika tika = new Tika(); | |
51 | ||
52 | 2 | protected void serveResource(String resourceName, InputStream resourceStream) |
53 | throws ResourceReferenceHandlerException | |
54 | { | |
55 | // Make sure the resource stream supports mark & reset which is needed in order be able to detect the | |
56 | // content type without affecting the stream (Tika may need to read a few bytes from the start of the | |
57 | // stream, in which case it will mark & reset the stream). | |
58 | // | |
59 | // Note that even though the stream returned by TrueVFS returns true for markSupported() in practice it | |
60 | // doesn't! Thus we need to wrap the stream to make it support mark and reset. | |
61 | 2 | InputStream markResetSupportingStream = new BufferedInputStream(resourceStream); |
62 | ||
63 | 2 | try { |
64 | 2 | Response response = this.container.getResponse(); |
65 | 2 | response.setContentType(this.tika.detect(markResetSupportingStream, resourceName)); |
66 | 2 | IOUtils.copy(markResetSupportingStream, response.getOutputStream()); |
67 | } catch (Exception e) { | |
68 | 0 | throw new ResourceReferenceHandlerException(String.format("Failed to read resource [%s]", resourceName), e); |
69 | } finally { | |
70 | 2 | IOUtils.closeQuietly(markResetSupportingStream); |
71 | } | |
72 | } | |
73 | } |