Skip to content

Commit 64d0542

Browse files
authored
Merge pull request #566 from apache/WW-5185-struts-resource
[WW-5185] Reintroduces proper implementation of Tiles resource to support wildcard matching of Tiles definitions
2 parents dc4e403 + 9a2996f commit 64d0542

File tree

4 files changed

+109
-31
lines changed

4 files changed

+109
-31
lines changed

apps/rest-showcase/src/main/webapp/WEB-INF/web.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
* under the License.
2020
*/
2121
-->
22-
<web-app id="starter" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
22+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
2323
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
24-
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
24+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
25+
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
26+
version="3.1">
2527

2628
<display-name>Struts 2 Rest Example</display-name>
2729

28-
2930
<!-- Filters -->
3031
<!-- START SNIPPET: filter -->
3132
<filter>

apps/showcase/src/main/webapp/WEB-INF/web.xml

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
3+
/*
34
* Licensed to the Apache Software Foundation (ASF) under one
45
* or more contributor license agreements. See the NOTICE file
56
* distributed with this work for additional information
@@ -16,20 +17,16 @@
1617
* KIND, either express or implied. See the License for the
1718
* specific language governing permissions and limitations
1819
* under the License.
20+
*/
1921
-->
20-
<web-app id="starter" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
22+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
2123
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22-
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
24+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
25+
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
26+
version="3.1">
2327

2428
<display-name>Struts Showcase Application</display-name>
2529

26-
<context-param>
27-
<param-name>org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG</param-name>
28-
<param-value>
29-
/WEB-INF/tiles.xml
30-
</param-value>
31-
</context-param>
32-
3330
<filter>
3431
<filter-name>async</filter-name>
3532
<filter-class>org.apache.struts2.showcase.async.AsyncFilter</filter-class>
@@ -46,11 +43,10 @@
4643
<filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
4744
</filter>
4845

49-
50-
<filter>
51-
<filter-name>sitemesh</filter-name>
52-
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
53-
</filter>
46+
<filter>
47+
<filter-name>sitemesh</filter-name>
48+
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
49+
</filter>
5450

5551
<filter-mapping>
5652
<filter-name>async</filter-name>
@@ -81,18 +77,18 @@
8177
org.apache.struts2.tiles.StrutsTilesListener
8278
</listener-class>
8379
</listener>
84-
80+
8581
<!-- Chat Example in Showcase -->
8682
<listener>
87-
<listener-class>
88-
org.apache.struts2.showcase.chat.ChatSessionListener
89-
</listener-class>
83+
<listener-class>
84+
org.apache.struts2.showcase.chat.ChatSessionListener
85+
</listener-class>
9086
</listener>
9187

9288
<listener>
9389
<listener-class>org.apache.struts2.dispatcher.listener.StrutsListener</listener-class>
9490
</listener>
95-
91+
9692
<!-- SNIPPET START: dwr -->
9793

9894
<servlet>
@@ -103,13 +99,13 @@
10399
<param-value>true</param-value>
104100
</init-param>
105101
</servlet>
106-
<servlet>
107-
<servlet-name>JspSupportServlet</servlet-name>
108-
<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
109-
<load-on-startup>1</load-on-startup>
110-
</servlet>
102+
<servlet>
103+
<servlet-name>JspSupportServlet</servlet-name>
104+
<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
105+
<load-on-startup>1</load-on-startup>
106+
</servlet>
111107

112-
<!-- Sitemesh Freemarker and Velocity Decorator Servlets. Shares configuration with Struts.-->
108+
<!-- Sitemesh Freemarker and Velocity Decorator Servlets. Shares configuration with Struts.-->
113109
<servlet>
114110
<servlet-name>sitemesh-freemarker</servlet-name>
115111
<servlet-class>org.apache.struts2.sitemesh.FreemarkerDecoratorServlet</servlet-class>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.struts2.tiles;
20+
21+
import org.apache.tiles.request.locale.PostfixedApplicationResource;
22+
import org.apache.tiles.request.locale.URLApplicationResource;
23+
24+
import java.io.File;
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.net.URI;
28+
import java.net.URL;
29+
30+
public class StrutsApplicationResource extends PostfixedApplicationResource {
31+
32+
private final URL url;
33+
private final File file;
34+
35+
/**
36+
* fixes WW-5011
37+
* @return file path for "file" protocol elsewhere url path as before to keep backward-compatibility
38+
* @see URLApplicationResource#getFile(URL)
39+
*/
40+
private static String getFilePath(URL url) {
41+
String path = url.getPath();
42+
if (!"file".equals(url.getProtocol())) {
43+
return path;
44+
}
45+
try {
46+
// fixes WW-5011 because includes ref in path - like URLApplicationResource#getFile(URL)
47+
path = (new URI(url.toExternalForm())).getSchemeSpecificPart();
48+
} catch (Exception e) {
49+
// fallback solution
50+
if (url.getRef() != null && !new File(path).exists()) {
51+
// it's like WW-5011
52+
path += "#" + url.getRef();
53+
}
54+
}
55+
56+
return path;
57+
}
58+
59+
public StrutsApplicationResource(URL url) {
60+
super(getFilePath(url));
61+
this.url = url;
62+
this.file = new File(getFilePath(url));
63+
}
64+
65+
@Override
66+
public InputStream getInputStream() throws IOException {
67+
return url.openStream();
68+
}
69+
70+
@Override
71+
public long getLastModified() throws IOException {
72+
if (file.exists()) {
73+
return file.lastModified();
74+
}
75+
return 0;
76+
}
77+
78+
@Override
79+
public String toString() {
80+
return "Resource " + getLocalePath() + " at " + url.toString();
81+
}
82+
}

plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public ApplicationResource getResource(ApplicationResource base, Locale locale)
102102
File localFile = new File(localePath);
103103
if (localFile.exists()) {
104104
try {
105-
return new URLApplicationResource(localePath, localFile.toURI().toURL());
105+
return new StrutsApplicationResource(localFile.toURI().toURL());
106106
} catch (MalformedURLException e) {
107107
LOG.warn("Cannot access [{}]", localePath, e);
108108
return null;
@@ -121,8 +121,7 @@ protected Set<ApplicationResource> findResources(String path) throws IOException
121121

122122
for (Map.Entry<String, URL> entry : matches.entrySet()) {
123123
if (pattern.matcher(entry.getKey()).matches()) {
124-
URL url = entry.getValue();
125-
resources.add(new URLApplicationResource(url.toExternalForm(), url));
124+
resources.add(new StrutsApplicationResource(entry.getValue()));
126125
}
127126
}
128127

0 commit comments

Comments
 (0)