1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.vfs.internal

File VfsResourceReferenceHandler.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
17
3
1
124
73
7
0.41
5.67
3
2.33

Classes

Class Line # Actions
VfsResourceReferenceHandler 59 17 0% 7 1
0.9595%
 

Contributing tests

This file is covered by 2 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.vfs.internal;
21   
22    import java.io.InputStream;
23    import java.net.URI;
24    import java.nio.file.Files;
25    import java.nio.file.Path;
26    import java.util.Arrays;
27    import java.util.List;
28   
29    import javax.inject.Inject;
30    import javax.inject.Named;
31    import javax.inject.Provider;
32    import javax.inject.Singleton;
33   
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.component.manager.ComponentManager;
36    import org.xwiki.resource.ResourceReference;
37    import org.xwiki.resource.ResourceReferenceHandlerChain;
38    import org.xwiki.resource.ResourceReferenceHandlerException;
39    import org.xwiki.resource.ResourceReferenceSerializer;
40    import org.xwiki.resource.ResourceType;
41    import org.xwiki.resource.SerializeResourceReferenceException;
42    import org.xwiki.resource.UnsupportedResourceReferenceException;
43    import org.xwiki.vfs.VfsPermissionChecker;
44    import org.xwiki.vfs.VfsResourceReference;
45   
46    import net.java.truevfs.access.TPath;
47   
48    /**
49    * Handles VFS Resource References by outputting in the Container's response the resource pointed to in the archive
50    * file defined in the URL.
51    *
52    * @version $Id: ae754eac085d10fbd6f1e7c18192ccde5caccf4d $
53    * @see VfsResourceReferenceResolver for the URL format handled
54    * @since 7.4M2
55    */
56    @Component
57    @Named("vfs")
58    @Singleton
 
59    public class VfsResourceReferenceHandler extends AbstractContentResourceReferenceHandler
60    {
61    @Inject
62    @Named("context")
63    private Provider<ComponentManager> componentManagerProvider;
64   
65    @Inject
66    @Named("cascading")
67    private VfsPermissionChecker permissionChecker;
68   
69    @Inject
70    @Named("truevfs")
71    private ResourceReferenceSerializer<VfsResourceReference, URI> trueVfsResourceReferenceSerializer;
72   
 
73  63 toggle @Override
74    public List<ResourceType> getSupportedResourceReferences()
75    {
76  63 return Arrays.asList(VfsResourceReference.TYPE);
77    }
78   
 
79  3 toggle @Override
80    public void handle(ResourceReference resourceReference, ResourceReferenceHandlerChain chain)
81    throws ResourceReferenceHandlerException
82    {
83    // This code only handles VFS Resource References.
84  3 VfsResourceReference vfsResourceReference = (VfsResourceReference) resourceReference;
85   
86  3 try {
87    // Verify that the user has the permission for the specified VFS scheme and for the VFS URI
88  3 this.permissionChecker.checkPermission(vfsResourceReference);
89   
90    // Extract the asked resource from inside the zip and return its content for display.
91   
92    // We need to convert the VFS Resource Reference into a hierarchical URI supported by TrueVFS
93  2 URI trueVFSURI = convertResourceReference(vfsResourceReference);
94   
95    // We use TrueVFS. This line will automatically use the VFS Driver that matches the scheme passed in the URI
96  2 Path path = new TPath(trueVFSURI);
97  2 try (InputStream in = Files.newInputStream(path)) {
98  2 List<String> pathSegments = vfsResourceReference.getPathSegments();
99  2 serveResource(pathSegments.get(pathSegments.size() - 1), in);
100    }
101    } catch (Exception e) {
102  1 throw new ResourceReferenceHandlerException(
103    String.format("Failed to extract resource [%s]", vfsResourceReference), e);
104    }
105   
106    // Be a good citizen, continue the chain, in case some lower-priority Handler has something to do for this
107    // Resource Reference.
108  2 chain.handleNext(vfsResourceReference);
109    }
110   
 
111  2 toggle private URI convertResourceReference(VfsResourceReference reference) throws ResourceReferenceHandlerException
112    {
113  2 URI resultURI;
114   
115  2 try {
116  2 resultURI = this.trueVfsResourceReferenceSerializer.serialize(reference);
117    } catch (SerializeResourceReferenceException | UnsupportedResourceReferenceException e) {
118  0 throw new ResourceReferenceHandlerException(
119    String.format("Failed to convert VFS URI [%s] into a valid FS format", reference), e);
120    }
121   
122  2 return resultURI;
123    }
124    }