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

File ExtendedURLTemporaryResourceReferenceResolver.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

4
18
2
1
98
60
5
0.28
9
2
2.5

Classes

Class Line # Actions
ExtendedURLTemporaryResourceReferenceResolver 52 18 0% 5 24
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.resource.temporary.internal;
21   
22    import java.util.List;
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.xwiki.component.annotation.Component;
30    import org.xwiki.model.EntityType;
31    import org.xwiki.model.reference.EntityReference;
32    import org.xwiki.model.reference.EntityReferenceResolver;
33    import org.xwiki.resource.CreateResourceReferenceException;
34    import org.xwiki.resource.ResourceType;
35    import org.xwiki.resource.UnsupportedResourceReferenceException;
36    import org.xwiki.resource.temporary.TemporaryResourceReference;
37    import org.xwiki.url.ExtendedURL;
38    import org.xwiki.url.internal.AbstractResourceReferenceResolver;
39   
40    /**
41    * Resolves a {@link TemporaryResourceReference} from an {@link ExtendedURL}. The following URL format is expected:
42    * {@code http://<server>/<context>/tmp/<module id>/<owning entity reference>/<module-dependent resource path>}.
43    *
44    * @version $Id: 740d6da8b3aa357bd921eb61fca910946ea479e8 $
45    * @since 7.4.6
46    * @since 8.2.2
47    * @since 8.3
48    */
49    @Component
50    @Named("standard/tmp")
51    @Singleton
 
52    public class ExtendedURLTemporaryResourceReferenceResolver extends AbstractResourceReferenceResolver
53    {
54    @Inject
55    @Named("url")
56    private EntityReferenceResolver<String> urlEntityReferenceResolver;
57   
 
58  0 toggle @Override
59    public TemporaryResourceReference resolve(ExtendedURL extendedURL, ResourceType resourceType,
60    Map<String, Object> parameters) throws CreateResourceReferenceException, UnsupportedResourceReferenceException
61    {
62  0 TemporaryResourceReference reference;
63  0 List<String> segments = extendedURL.getSegments();
64   
65  0 if (segments.size() < 3) {
66  0 throw new CreateResourceReferenceException(
67    String.format("Invalid temporary resource URL format [%s].", extendedURL.toString()));
68    } else {
69    // The first segment is the module id.
70  0 String moduleId = segments.get(0);
71    // The second segment is the serialized owning entity reference. This is used to check access rights.
72  0 EntityReference owningEntityReference = resolveEntityReference(segments.get(1));
73    // The other segments point to the resource path.
74  0 List<String> resourcePath = segments.subList(2, segments.size());
75  0 reference = new TemporaryResourceReference(moduleId, resourcePath, owningEntityReference);
76  0 copyParameters(extendedURL, reference);
77    }
78  0 return reference;
79    }
80   
 
81  0 toggle private EntityReference resolveEntityReference(String representation) throws CreateResourceReferenceException
82    {
83  0 int index = representation.indexOf(':');
84  0 if (index > 0) {
85  0 String entityTypeString = representation.substring(0, index);
86  0 try {
87  0 EntityType entityType = EntityType.valueOf(entityTypeString.toUpperCase());
88  0 return this.urlEntityReferenceResolver.resolve(representation.substring(index + 1), entityType);
89    } catch (Exception e) {
90  0 throw new CreateResourceReferenceException(
91    String.format("Unknown entity type [%s].", entityTypeString));
92    }
93    } else {
94  0 throw new CreateResourceReferenceException(
95    String.format("Entity type is missing from [%s].", representation));
96    }
97    }
98    }