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

File StandardExtendedURLResourceTypeResolver.java

 

Coverage histogram

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

Code metrics

0
30
4
1
156
96
5
0.17
7.5
4
1.25

Classes

Class Line # Actions
StandardExtendedURLResourceTypeResolver 60 30 0% 5 1
0.970588297.1%
 

Contributing tests

This file is covered by 1 test. .

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.standard;
21   
22    import java.util.Map;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
30    import org.xwiki.component.descriptor.DefaultComponentDependency;
31    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.component.manager.ComponentRepositoryException;
34    import org.xwiki.component.phase.Initializable;
35    import org.xwiki.component.phase.InitializationException;
36    import org.xwiki.component.util.DefaultParameterizedType;
37    import org.xwiki.model.reference.EntityReference;
38    import org.xwiki.model.reference.EntityReferenceResolver;
39    import org.xwiki.resource.CreateResourceTypeException;
40    import org.xwiki.resource.ResourceReferenceResolver;
41    import org.xwiki.resource.ResourceType;
42    import org.xwiki.resource.entity.EntityResourceReference;
43    import org.xwiki.resource.internal.entity.EntityResourceActionLister;
44    import org.xwiki.url.ExtendedURL;
45    import org.xwiki.url.internal.AbstractExtendedURLResourceTypeResolver;
46    import org.xwiki.url.internal.standard.entity.BinEntityResourceReferenceResolver;
47    import org.xwiki.url.internal.standard.entity.WikiEntityResourceReferenceResolver;
48   
49    /**
50    * Extracts the {@link ResourceType} from a passed {@link ExtendedURL}, using the {@code standard} URL scheme format.
51    * In that format the Resource Type is the path segment in the URL just after the Context Path one (e.g.
52    * {@code bin} in {@code http://<server>/xwiki/bin/view/Space/Page}.
53    *
54    * @version $Id: 4786639d2525a2426121ecfc6e4fc1c40983eb0e $
55    * @since 7.1M1
56    */
57    @Component
58    @Named("standard")
59    @Singleton
 
60    public class StandardExtendedURLResourceTypeResolver extends AbstractExtendedURLResourceTypeResolver implements
61    Initializable
62    {
63    private static final String HINT = "standard";
64   
65    @Inject
66    private ComponentManager rootComponentManager;
67   
68    /**
69    * Used to know if the wiki is in path-based configuration or not.
70    */
71    @Inject
72    private StandardURLConfiguration configuration;
73   
 
74  33 toggle @Override
75    public void initialize() throws InitializationException
76    {
77    // Note that we initialize the 2 Resolver in this component and not in
78    // StandardExtendedURLResourceReferenceResolver because this class is called before
79    // StandardExtendedURLResourceReferenceResolver and it checks if the Resolvers are registered when performing
80    // its resolve. Thus the 2 Resolvers need to be registered *before* this class's resolve() is called.
81   
82    // Step 1: Dynamically register a WikiEntityResourceReferenceResolver since the user can choose the Resource
83    // type name when specifying an Entity Resource Reference in path-based multiwiki, see
84    // StandardURLConfiguration#getWikiPathPrefix()
85  33 registerEntityResourceReferenceResolver(this.configuration.getWikiPathPrefix(),
86    WikiEntityResourceReferenceResolver.class, "path");
87   
88    // Step 2: Dynamically register a BinEntityResourceReferenceResolver. Note that we use the
89    // {@link EntityResourceReference#TYPE} as the hint since the Standard ExtendedURL Resource Type Resolver will
90    // have converted from {@code this.configuration.getEntityPathPrefix()} to {@link EntityResourceReference#TYPE}
91    // already.
92  33 registerEntityResourceReferenceResolver(EntityResourceReference.TYPE.getId(),
93    BinEntityResourceReferenceResolver.class, "domain");
94    }
95   
 
96  35230 toggle @Override
97    public ResourceType resolve(ExtendedURL extendedURL, Map<String, Object> parameters)
98    throws CreateResourceTypeException
99    {
100  35230 return resolve(HINT, extendedURL, parameters);
101    }
102   
 
103  66 toggle private void registerEntityResourceReferenceResolver(String registrationHint,
104    Class<? extends ResourceReferenceResolver<ExtendedURL>> registrationImplementation,
105    String wikiExtractorHint) throws InitializationException
106    {
107  66 DefaultComponentDescriptor<ResourceReferenceResolver<ExtendedURL>> resolverDescriptor =
108    new DefaultComponentDescriptor<>();
109  66 resolverDescriptor.setImplementation(registrationImplementation);
110  66 resolverDescriptor.setInstantiationStrategy(ComponentInstantiationStrategy.SINGLETON);
111  66 String hint = computeHint(registrationHint);
112  66 resolverDescriptor.setRoleHint(hint);
113  66 resolverDescriptor.setRoleType(
114    new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class));
115   
116    // Register dependencies
117   
118  66 DefaultComponentDependency<WikiReferenceExtractor> wikiReferenceExtractorDependency =
119    new DefaultComponentDependency<>();
120  66 wikiReferenceExtractorDependency.setRoleType(WikiReferenceExtractor.class);
121  66 wikiReferenceExtractorDependency.setRoleHint(wikiExtractorHint);
122  66 wikiReferenceExtractorDependency.setName("wikiExtractor");
123  66 resolverDescriptor.addComponentDependency(wikiReferenceExtractorDependency);
124   
125  66 DefaultComponentDependency<EntityReferenceResolver<EntityReference>> entityReferenceResolverDependency =
126    new DefaultComponentDependency<>();
127  66 entityReferenceResolverDependency.setRoleType(new DefaultParameterizedType(null, EntityReferenceResolver.class,
128    EntityReference.class));
129  66 entityReferenceResolverDependency.setName("defaultReferenceEntityReferenceResolver");
130  66 resolverDescriptor.addComponentDependency(entityReferenceResolverDependency);
131   
132  66 DefaultComponentDependency<StandardURLConfiguration> standardURLConfigurationDependency =
133    new DefaultComponentDependency<>();
134  66 standardURLConfigurationDependency.setRoleType(StandardURLConfiguration.class);
135  66 standardURLConfigurationDependency.setName("configuration");
136  66 resolverDescriptor.addComponentDependency(standardURLConfigurationDependency);
137   
138  66 DefaultComponentDependency<EntityResourceActionLister> entityResourceActionListerDependency =
139    new DefaultComponentDependency<>();
140  66 entityResourceActionListerDependency.setRoleType(EntityResourceActionLister.class);
141  66 entityResourceActionListerDependency.setName("entityResourceActionLister");
142  66 resolverDescriptor.addComponentDependency(entityResourceActionListerDependency);
143   
144  66 try {
145  66 this.rootComponentManager.registerComponent(resolverDescriptor);
146    } catch (ComponentRepositoryException e) {
147  0 throw new InitializationException(String.format(
148    "Failed to dynamically register Resource Reference Resolver for hint [%s]", hint), e);
149    }
150    }
151   
 
152  66 toggle private String computeHint(String type)
153    {
154  66 return String.format("%s/%s", HINT, type);
155    }
156    }