Skip to content
This repository was archived by the owner on Dec 15, 2018. It is now read-only.

Commit a4b93d2

Browse files
dmaidaniukchkal
authored andcommitted
Add support for a default file extension for view names (#204)
1 parent f83ee29 commit a4b93d2

File tree

14 files changed

+617
-3
lines changed

14 files changed

+617
-3
lines changed

core/src/main/java/org/mvcspec/ozark/OzarkConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,12 @@ public CsrfTokenStrategy getCsrfTokenStrategy() {
6161

6262
}
6363

64+
public String getDefaultViewFileExtension() {
65+
Object value = config.getProperty(Properties.DEFAULT_VIEW_FILE_EXTENSION);
66+
if (value instanceof String) {
67+
return (String) value;
68+
}
69+
return null;
70+
}
71+
6472
}

core/src/main/java/org/mvcspec/ozark/Properties.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ public interface Properties {
3535
*/
3636
String CSRF_TOKEN_STRATEGY = "org.mvcspec.ozark.csrfTokenStrategy";
3737

38+
/**
39+
* Property for defining default file extension for usage in views
40+
*/
41+
String DEFAULT_VIEW_FILE_EXTENSION = "org.mvcspec.ozark.defaultViewFileExtension";
42+
3843
}

core/src/main/java/org/mvcspec/ozark/core/ViewResponseFilter.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.mvcspec.ozark.core;
1717

18+
import org.mvcspec.ozark.OzarkConfig;
1819
import org.mvcspec.ozark.event.AfterControllerEventImpl;
1920
import org.mvcspec.ozark.event.ControllerRedirectEventImpl;
2021

@@ -96,6 +97,9 @@ public class ViewResponseFilter implements ContainerResponseFilter {
9697
@Inject
9798
private Messages messages;
9899

100+
@Inject
101+
private OzarkConfig ozarkConfig;
102+
99103
@Override
100104
public void filter(ContainerRequestContext requestContext,
101105
ContainerResponseContext responseContext) throws IOException {
@@ -125,7 +129,7 @@ public void filter(ContainerRequestContext requestContext,
125129
if (contentType == null) {
126130
contentType = MediaType.TEXT_HTML_TYPE; // default
127131
}
128-
responseContext.setEntity(new Viewable(an.value()), null, contentType);
132+
responseContext.setEntity(new Viewable(appendExtensionIfRequired(an.value())), null, contentType);
129133
// If the entity is null the status will be set to 204 by Jersey. For void methods we need to
130134
// set the status to 200 unless no other status was set by e.g. throwing an Exception.
131135

@@ -138,7 +142,7 @@ public void filter(ContainerRequestContext requestContext,
138142
throw new ServerErrorException(messages.get("VoidControllerNoView", resourceInfo.getResourceMethod()), INTERNAL_SERVER_ERROR);
139143
}
140144
} else if (entityType != Viewable.class) {
141-
final String view = entity.toString();
145+
final String view = appendExtensionIfRequired(entity.toString());
142146
if (view == null) {
143147
throw new ServerErrorException(messages.get("EntityToStringNull", resourceInfo.getResourceMethod()), INTERNAL_SERVER_ERROR);
144148
}
@@ -148,7 +152,7 @@ public void filter(ContainerRequestContext requestContext,
148152
// Redirect logic, entity must be a Viewable if not null
149153
entity = responseContext.getEntity();
150154
if (entity != null) {
151-
final String view = ((Viewable) entity).getView();
155+
final String view = appendExtensionIfRequired(((Viewable) entity).getView());
152156
final String uri = uriInfo.getBaseUri() + noStartingSlash(noPrefix(view, REDIRECT));
153157
if (view.startsWith(REDIRECT)) {
154158
responseContext.setStatusInfo(SEE_OTHER);
@@ -173,6 +177,26 @@ public void filter(ContainerRequestContext requestContext,
173177
}
174178
}
175179

180+
private String appendExtensionIfRequired(String viewName) {
181+
return appendExtensionIfRequired(viewName, ozarkConfig.getDefaultViewFileExtension());
182+
}
183+
184+
/*
185+
* Append to view name default extension if one available and applicable.
186+
*/
187+
static String appendExtensionIfRequired(String viewName, String defaultExtension) {
188+
if (viewName == null || viewName.startsWith(REDIRECT)
189+
|| defaultExtension == null || defaultExtension.length() == 0) {
190+
return viewName;
191+
}
192+
193+
String resultView = viewName;
194+
if (!viewName.contains(".")) {
195+
resultView += "." + defaultExtension;
196+
}
197+
return resultView;
198+
}
199+
176200
private static MediaType selectVariant(Request request, ResourceInfo resourceInfo) {
177201

178202
Produces produces = resourceInfo.getResourceMethod().getAnnotation(Produces.class);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright © 2017 Ivar Grimstad ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mvcspec.ozark.core;
17+
18+
import org.junit.Test;
19+
import org.junit.runner.RunWith;
20+
import org.junit.runners.Parameterized;
21+
import org.junit.runners.Parameterized.Parameters;
22+
23+
import java.util.Arrays;
24+
import java.util.Collection;
25+
26+
import static org.junit.Assert.assertEquals;
27+
28+
/**
29+
* Test for ViewResponseFilter
30+
*
31+
* @author Dmytro Maidaniuk
32+
*/
33+
@RunWith(value = Parameterized.class)
34+
public class ViewResponseFilterTest {
35+
36+
private String viewName;
37+
38+
private String defaultExtension;
39+
40+
private String expectedViewName;
41+
42+
public ViewResponseFilterTest(String viewName,
43+
String defaultExtension,
44+
String expectedViewName) {
45+
this.viewName = viewName;
46+
this.defaultExtension = defaultExtension;
47+
this.expectedViewName = expectedViewName;
48+
}
49+
50+
@Parameters
51+
public static Collection<Object[]> data() {
52+
return Arrays.asList(new Object[][] {
53+
{"main.jsp", "jsp", "main.jsp"},
54+
{"main", "jsp", "main.jsp"},
55+
{"main", null, "main"},
56+
{"main", "", "main"},
57+
{"redirect:some.jsp", "jsp", "redirect:some.jsp"},
58+
{"react:some", "jsp", "react:some.jsp"},
59+
{"main.", "jsp", "main."}
60+
});
61+
}
62+
63+
@Test
64+
public void testAppendExtensionIfRequired() {
65+
String actualViewName = ViewResponseFilter.appendExtensionIfRequired(viewName, defaultExtension);
66+
assertEquals(expectedViewName, actualViewName);
67+
}
68+
69+
}

test/def-ext/pom.xml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright © 2017 Ivar Grimstad ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
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, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>org.mvc-spec.ozark.test</groupId>
23+
<artifactId>ozark-parent-test</artifactId>
24+
<version>1.0.0-SNAPSHOT</version>
25+
</parent>
26+
<artifactId>def-ext</artifactId>
27+
<packaging>war</packaging>
28+
<name>Ozark Default Extension Tests</name>
29+
<build>
30+
<finalName>test-def-ext</finalName>
31+
</build>
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.mvc-spec.ozark</groupId>
35+
<artifactId>ozark-core</artifactId>
36+
<version>1.0.0-SNAPSHOT</version>
37+
</dependency>
38+
</dependencies>
39+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright © 2017 Ivar Grimstad ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mvcspec.ozark.test.mvc;
17+
18+
import javax.mvc.Controller;
19+
import javax.ws.rs.GET;
20+
import javax.ws.rs.Path;
21+
22+
/**
23+
* DefaultExtensionController test.
24+
*
25+
* @author Dmytro Maidaniuk
26+
*/
27+
@Path("def-ext")
28+
@Controller
29+
public class DefaultExtensionController {
30+
31+
@GET
32+
public String get() {
33+
return "extension";
34+
}
35+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright © 2017 Ivar Grimstad ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mvcspec.ozark.test.mvc;
17+
18+
import javax.ws.rs.ApplicationPath;
19+
import javax.ws.rs.core.Application;
20+
import java.util.HashMap;
21+
import java.util.HashSet;
22+
import java.util.Map;
23+
import java.util.Set;
24+
25+
/**
26+
* Class MyApplication.
27+
*
28+
* @author Santiago Pericas-Geertsen
29+
*/
30+
@ApplicationPath("resources")
31+
public class MyApplication extends Application {
32+
33+
@Override
34+
public Set<Class<?>> getClasses() {
35+
final Set<Class<?>> set = new HashSet<>();
36+
set.add(DefaultExtensionController.class);
37+
return set;
38+
}
39+
40+
@Override
41+
public Map<String, Object> getProperties() {
42+
final Map<String, Object> map = new HashMap<>();
43+
map.put("myproperty", true);
44+
map.put("org.mvcspec.ozark.defaultViewFileExtension", "jsp");
45+
return map;
46+
}
47+
}

0 commit comments

Comments
 (0)