1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
package org.apache.tiles.request.servlet.wildcard; |
23 | |
|
24 | |
import java.io.IOException; |
25 | |
import java.net.URL; |
26 | |
import java.util.ArrayList; |
27 | |
import java.util.Collection; |
28 | |
import java.util.Collections; |
29 | |
import java.util.Locale; |
30 | |
|
31 | |
import javax.servlet.ServletContext; |
32 | |
|
33 | |
import org.apache.tiles.request.ApplicationResource; |
34 | |
import org.apache.tiles.request.locale.URLApplicationResource; |
35 | |
import org.apache.tiles.request.servlet.ServletApplicationContext; |
36 | |
import org.springframework.core.io.Resource; |
37 | |
import org.springframework.core.io.support.ResourcePatternResolver; |
38 | |
import org.springframework.web.context.support.ServletContextResourcePatternResolver; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public class WildcardServletApplicationContext extends ServletApplicationContext { |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
protected ResourcePatternResolver resolver; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public WildcardServletApplicationContext(ServletContext servletContext) { |
59 | 1 | super(servletContext); |
60 | 1 | resolver = new ServletContextResourcePatternResolver(servletContext); |
61 | 1 | } |
62 | |
|
63 | |
|
64 | |
@Override |
65 | |
public ApplicationResource getResource(String localePath) { |
66 | 2 | ApplicationResource retValue = null; |
67 | 2 | Collection<ApplicationResource> urlSet = getResources(localePath); |
68 | 2 | if (urlSet != null && !urlSet.isEmpty()) { |
69 | 2 | retValue = urlSet.iterator().next(); |
70 | |
} |
71 | 2 | return retValue; |
72 | |
} |
73 | |
|
74 | |
|
75 | |
@Override |
76 | |
public ApplicationResource getResource(ApplicationResource base, Locale locale) { |
77 | 0 | ApplicationResource retValue = null; |
78 | 0 | Collection<ApplicationResource> urlSet = getResources(base.getLocalePath(locale)); |
79 | 0 | if (urlSet != null && !urlSet.isEmpty()) { |
80 | 0 | retValue = urlSet.iterator().next(); |
81 | |
} |
82 | 0 | return retValue; |
83 | |
} |
84 | |
|
85 | |
|
86 | |
@Override |
87 | |
public Collection<ApplicationResource> getResources(String path) { |
88 | |
Resource[] resources; |
89 | |
try { |
90 | 4 | resources = resolver.getResources(path); |
91 | 0 | } catch (IOException e) { |
92 | 0 | return Collections.<ApplicationResource> emptyList(); |
93 | 4 | } |
94 | 4 | Collection<ApplicationResource> resourceList = new ArrayList<ApplicationResource>(); |
95 | 4 | if (resources != null && resources.length > 0) { |
96 | 10 | for (int i = 0; i < resources.length; i++) { |
97 | |
URL url; |
98 | |
try { |
99 | 6 | url = resources[i].getURL(); |
100 | 6 | resourceList.add(new URLApplicationResource(url.toExternalForm(), url)); |
101 | 0 | } catch (IOException e) { |
102 | |
|
103 | 0 | throw new IllegalArgumentException("no URL for " + resources[i].toString(), e); |
104 | 6 | } |
105 | |
} |
106 | |
} |
107 | 4 | return resourceList; |
108 | |
} |
109 | |
} |