Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
AbstractExtendedURLResourceTypeResolver | 46 | 12 | 0% | 6 | 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: 8f80d33735cbd634a5238faa7ec13367d621d397 $ | |
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 | 35180 | protected ResourceType resolve(String hintPrefix, ExtendedURL extendedURL, Map<String, Object> parameters) |
65 | throws CreateResourceTypeException | |
66 | { | |
67 | 35200 | ResourceType resourceType; |
68 | ||
69 | // Find the Resource Type, which is the first segment in the ExtendedURL, except for GTW resources which | |
70 | // use a ".gwtrpc" suffix. | |
71 | // TODO: Right now we don't have any specific Resolver for GWT resources and we just consider them as Entity | |
72 | // Resources. | |
73 | // | |
74 | // Note that we need to remove the type from the ExtendedURL instance since it's passed to the specific | |
75 | // resolvers and they shouldn't be aware of where it was located since they need to be able to resolve the | |
76 | // rest of the URL independently of the URL scheme, in case they wish to have a single URL syntax for all URL | |
77 | // schemes. | |
78 | // | |
79 | // Examples: | |
80 | // - scheme 1: /<type>/something | |
81 | // - scheme 2: /something?type=<type> | |
82 | // | |
83 | // The specific resolver for type <type> needs to be passed an ExtendedURL independent of the type, in this | |
84 | // case, "/something" for both examples. | |
85 | // | |
86 | // However since we also want this code to work when short URLs are enabled, we only remove the segment part | |
87 | // if a Resource type has been identified (see below) and if not, we assume the URL is pointing to an Entity | |
88 | // Resource. | |
89 | 35133 | List<String> segments = extendedURL.getSegments(); |
90 | ||
91 | // Special handling for GWT resources | |
92 | 35182 | if (segments.size() > 0 && segments.get(segments.size() - 1).endsWith(".gwtrpc")) |
93 | { | |
94 | 7 | return EntityResourceReference.TYPE; |
95 | } | |
96 | ||
97 | 35172 | resourceType = this.defaultStringResourceTypeResolver.resolve(segments.get(0), Collections |
98 | .<String, Object>emptyMap()); | |
99 | ||
100 | // First, find out if an ExtendedURL Resource Resolver exists, only for this URL scheme. | |
101 | // Second, if not found, try to locate a URL Resolver registered for all URL schemes | |
102 | 35217 | if (this.componentManager.hasComponent(new DefaultParameterizedType(null, |
103 | ResourceReferenceResolver.class, ExtendedURL.class), computeHint(hintPrefix, resourceType.getId()))) | |
104 | { | |
105 | 29917 | extendedURL.getSegments().remove(0); |
106 | 5293 | } else if (this.componentManager.hasComponent(new DefaultParameterizedType(null, |
107 | ResourceReferenceResolver.class, ExtendedURL.class), resourceType.getId())) | |
108 | { | |
109 | 1716 | extendedURL.getSegments().remove(0); |
110 | } else { | |
111 | // No specific Resource Type Resolver has been found. In order to support short URLs (ie. without the | |
112 | // "bin" or "wiki" part specified), we assume the URL is pointing to an Entity Resource Reference. | |
113 | // Since the "wiki" type was not selected, we're assuming that the we'll use the "bin" entity resolver. | |
114 | 3579 | resourceType = EntityResourceReference.TYPE; |
115 | } | |
116 | ||
117 | 35190 | return resourceType; |
118 | } | |
119 | ||
120 | 35160 | private String computeHint(String hintPrefix, String type) |
121 | { | |
122 | 35180 | return String.format("%s/%s", hintPrefix, type); |
123 | } | |
124 | } |