Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
AbstractExtendedURLResourceTypeResolver | 46 | 10 | 0% | 4 | 0 |
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.util.Collections; | |
23 | import java.util.List; | |
24 | ||
25 | import java.util.Map; | |
26 | ||
27 | import javax.inject.Inject; | |
28 | import javax.inject.Named; | |
29 | ||
30 | import org.xwiki.component.manager.ComponentManager; | |
31 | import org.xwiki.component.util.DefaultParameterizedType; | |
32 | import org.xwiki.resource.CreateResourceTypeException; | |
33 | import org.xwiki.resource.ResourceReferenceResolver; | |
34 | import org.xwiki.resource.ResourceType; | |
35 | import org.xwiki.resource.ResourceTypeResolver; | |
36 | import org.xwiki.resource.entity.EntityResourceReference; | |
37 | import org.xwiki.url.ExtendedURL; | |
38 | ||
39 | /** | |
40 | * Helper class to implement {@link ResourceTypeResolver} which extract the Resource Type from the first path segment | |
41 | * after the Context Path one (eg {@code bin} in {@code http://<server>/xwiki/bin/view/Space/Page}). | |
42 | * | |
43 | * @version $Id: 47ad14892dd59c7baa48116923ddb6ac33ef6623 $ | |
44 | * @since 7.1M1 | |
45 | */ | |
46 | public abstract class AbstractExtendedURLResourceTypeResolver implements ResourceTypeResolver<ExtendedURL> | |
47 | { | |
48 | /** | |
49 | * Even though we use the Context Component Manager to locate Resource Reference Resolver, it may fall back on the | |
50 | * Root Component Manager since at this stage the Context may not have been defined yet. For example this code is | |
51 | * called by the RoutingFilter which executes before any Context has been set. | |
52 | */ | |
53 | @Inject | |
54 | @Named("context") | |
55 | private ComponentManager componentManager; | |
56 | ||
57 | /** | |
58 | *The Resource Type Resolver to use to convert the type as defined in the URL (String) into a Resource Type object. | |
59 | * For example for the "standard" URL scheme it would convert "bin" into {@link EntityResourceReference#TYPE}. | |
60 | */ | |
61 | @Inject | |
62 | private ResourceTypeResolver<String> defaultStringResourceTypeResolver; | |
63 | ||
64 | 122965 | protected ResourceType resolve(String hintPrefix, ExtendedURL extendedURL, Map<String, Object> parameters) |
65 | throws CreateResourceTypeException | |
66 | { | |
67 | 122973 | ResourceType resourceType; |
68 | ||
69 | // Find the Resource Type, which is the first segment in the ExtendedURL. | |
70 | // | |
71 | // Note that we need to remove the type from the ExtendedURL instance since it's passed to the specific | |
72 | // resolvers and they shouldn't be aware of where it was located since they need to be able to resolve the | |
73 | // rest of the URL independently of the URL scheme, in case they wish to have a single URL syntax for all URL | |
74 | // schemes. | |
75 | // | |
76 | // Examples: | |
77 | // - scheme 1: /<type>/something | |
78 | // - scheme 2: /something?type=<type> | |
79 | // | |
80 | // The specific resolver for type <type> needs to be passed an ExtendedURL independent of the type, in this | |
81 | // case, "/something" for both examples. | |
82 | // | |
83 | // However since we also want this code to work when short URLs are enabled, we only remove the segment part | |
84 | // if a Resource type has been identified (see below) and if not, we assume the URL is pointing to an Entity | |
85 | // Resource. | |
86 | 122950 | List<String> segments = extendedURL.getSegments(); |
87 | ||
88 | 122938 | resourceType = this.defaultStringResourceTypeResolver.resolve(segments.get(0), Collections |
89 | .<String, Object>emptyMap()); | |
90 | ||
91 | // First, find out if an ExtendedURL Resource Resolver exists, only for this URL scheme. | |
92 | // Second, if not found, try to locate a URL Resolver registered for all URL schemes | |
93 | 122991 | if (this.componentManager.hasComponent(new DefaultParameterizedType(null, |
94 | ResourceReferenceResolver.class, ExtendedURL.class), computeHint(hintPrefix, resourceType.getId()))) | |
95 | { | |
96 | 113976 | extendedURL.getSegments().remove(0); |
97 | 8989 | } else if (this.componentManager.hasComponent(new DefaultParameterizedType(null, |
98 | ResourceReferenceResolver.class, ExtendedURL.class), resourceType.getId())) | |
99 | { | |
100 | 5061 | extendedURL.getSegments().remove(0); |
101 | } else { | |
102 | // No specific Resource Type Resolver has been found. In order to support short URLs (ie. without the | |
103 | // "bin" or "wiki" part specified), we assume the URL is pointing to an Entity Resource Reference. | |
104 | // Since the "wiki" type was not selected, we're assuming that the we'll use the "bin" entity resolver. | |
105 | 3928 | resourceType = EntityResourceReference.TYPE; |
106 | } | |
107 | ||
108 | 122965 | return resourceType; |
109 | } | |
110 | ||
111 | 122916 | private String computeHint(String hintPrefix, String type) |
112 | { | |
113 | 122941 | return String.format("%s/%s", hintPrefix, type); |
114 | } | |
115 | } |