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

File AbstractResourceReferenceResolver.java

 

Coverage histogram

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

Code metrics

4
16
3
1
107
57
7
0.44
5.33
3
2.33

Classes

Class Line # Actions
AbstractResourceReferenceResolver 44 16 0% 7 2
0.913043591.3%
 

Contributing tests

This file is covered by 9 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.url.internal;
21   
22    import java.lang.reflect.Type;
23    import java.util.List;
24    import java.util.Map;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28   
29    import org.xwiki.component.manager.ComponentLookupException;
30    import org.xwiki.component.manager.ComponentManager;
31    import org.xwiki.component.util.DefaultParameterizedType;
32    import org.xwiki.resource.ResourceReference;
33    import org.xwiki.resource.ResourceReferenceResolver;
34    import org.xwiki.resource.ResourceType;
35    import org.xwiki.resource.UnsupportedResourceReferenceException;
36    import org.xwiki.url.ExtendedURL;
37   
38    /**
39    * Helper for implementers of {@link ResourceReferenceResolver}.
40    *
41    * @version $Id: 84e38b64c16b1aefde850de290b1e75120f2cf7f $
42    * @since 7.1M1
43    */
 
44    public abstract class AbstractResourceReferenceResolver implements ResourceReferenceResolver<ExtendedURL>
45    {
46    @Inject
47    @Named("context")
48    protected ComponentManager componentManager;
49   
50    /**
51    * Find the right Resolver for the passed Resource type and call it.
52    */
 
53  22820 toggle protected ResourceReferenceResolver<ExtendedURL> findResourceResolver(String hintPrefix, ResourceType type)
54    throws UnsupportedResourceReferenceException
55    {
56  22822 ResourceReferenceResolver<ExtendedURL> resolver;
57   
58  22820 Type roleType = new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class);
59  22818 String roleHint = computeResolverHint(hintPrefix, type.getId());
60   
61    // Step 1: Look for a Resolver specific to the scheme and specific to the Resource Type
62  22823 if (this.componentManager.hasComponent(roleType, roleHint)) {
63  21105 try {
64  21101 resolver = this.componentManager.getInstance(roleType, roleHint);
65    } catch (ComponentLookupException cle) {
66    // There's no Resolver registered for the passed Resource Type
67  0 throw new UnsupportedResourceReferenceException(String.format(
68    "Failed to lookup Resource Reference Resolver for hint [%s]", roleHint), cle);
69    }
70    } else {
71    // Step 2: If not found, look for a Resolver specific to the Resource Type but registered for all URL
72    // schemes
73  1716 try {
74  1716 resolver = this.componentManager.getInstance(roleType, type.getId());
75    } catch (ComponentLookupException cle) {
76    // There's no Resolver registered for the passed Resource Type
77  0 throw new UnsupportedResourceReferenceException(String.format(
78    "Failed to lookup Resource Reference Resolver for type [%s]", type), cle);
79    }
80    }
81   
82  22816 return resolver;
83    }
84   
 
85  22806 toggle protected String computeResolverHint(String hintPrefix, String type)
86    {
87  22808 return String.format("%s/%s", hintPrefix, type);
88    }
89   
90    /**
91    * Copies query string parameters from the passed {@link org.xwiki.url.ExtendedURL} to the passed
92    * {@link ResourceReference}.
93    *
94    * @param source the source URL from where to get the query string parameters
95    * @param target the {@link ResourceReference} into which to copy the query string parameters
96    */
 
97  22790 toggle protected void copyParameters(ExtendedURL source, ResourceReference target)
98    {
99  22809 for (Map.Entry<String, List<String>> entry : source.getParameters().entrySet()) {
100  14806 if (entry.getValue().isEmpty()) {
101  92 target.addParameter(entry.getKey(), null);
102    } else {
103  14705 target.addParameter(entry.getKey(), entry.getValue());
104    }
105    }
106    }
107    }