Skip to content

Commit 5df56d4

Browse files
committed
Added Servlet 3.0 implementation of DispatcherTypeProvider
1 parent a6f14bc commit 5df56d4

4 files changed

Lines changed: 127 additions & 10 deletions

File tree

‎impl-servlet-3.0/pom.xml‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45
<parent>
56
<groupId>org.ocpsoft.rewrite</groupId>
@@ -15,14 +16,6 @@
1516
<groupId>org.ocpsoft.rewrite</groupId>
1617
<artifactId>rewrite-api</artifactId>
1718
</dependency>
18-
<dependency>
19-
<groupId>org.ocpsoft.rewrite</groupId>
20-
<artifactId>rewrite-api-el</artifactId>
21-
</dependency>
22-
<dependency>
23-
<groupId>org.ocpsoft.rewrite</groupId>
24-
<artifactId>rewrite-impl</artifactId>
25-
</dependency>
2619
<dependency>
2720
<groupId>org.ocpsoft.rewrite</groupId>
2821
<artifactId>rewrite-api-servlet</artifactId>
@@ -34,7 +27,9 @@
3427
</dependency>
3528
<dependency>
3629
<groupId>org.ocpsoft.rewrite</groupId>
37-
<artifactId>rewrite-addressbuilder</artifactId>
30+
<artifactId>rewrite-test-harness</artifactId>
31+
<scope>test</scope>
3832
</dependency>
3933
</dependencies>
34+
4035
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
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.ocpsoft.rewrite.servlet.impl;
17+
18+
import javax.servlet.ServletContext;
19+
import javax.servlet.ServletRequest;
20+
21+
import org.ocpsoft.rewrite.servlet.DispatcherType;
22+
import org.ocpsoft.rewrite.servlet.spi.DispatcherTypeProvider;
23+
24+
/**
25+
* Implementation of {@link DispatcherTypeProvider} for Servlet 3.0 which supports
26+
* <code>HttpServletRequest.getDispatcherType()</code>
27+
*
28+
* @author Christian Kaltepoth
29+
*/
30+
public class Servlet3DispatcherTypeProvider implements DispatcherTypeProvider
31+
{
32+
33+
@Override
34+
public int priority()
35+
{
36+
return 10;
37+
}
38+
39+
@Override
40+
public DispatcherType getDispatcherType(ServletRequest request, ServletContext context)
41+
{
42+
if (context.getMajorVersion() >= 3) {
43+
return DispatcherType.valueOf(request.getDispatcherType().name());
44+
}
45+
return null;
46+
}
47+
48+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.ocpsoft.rewrite.servlet.impl.Servlet3DispatcherTypeProvider
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
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.ocpsoft.rewrite.servlet.impl;
17+
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNull;
20+
import static org.mockito.Mockito.mock;
21+
import static org.mockito.Mockito.when;
22+
23+
import javax.servlet.ServletContext;
24+
import javax.servlet.http.HttpServletRequest;
25+
26+
import org.junit.Test;
27+
import org.ocpsoft.rewrite.servlet.DispatcherType;
28+
import org.ocpsoft.rewrite.servlet.spi.DispatcherTypeProvider;
29+
30+
public class Servlet3DispatcherTypeProviderTest
31+
{
32+
33+
@Test
34+
public void testShouldReturnCorrectDispatcherTypeForServlet3()
35+
{
36+
37+
// GIVEN a Servlet 3.0 container
38+
ServletContext servletContext = mock(ServletContext.class);
39+
when(servletContext.getMajorVersion()).thenReturn(3);
40+
41+
// AND a forwarded request
42+
HttpServletRequest request = mock(HttpServletRequest.class);
43+
when(request.getDispatcherType()).thenReturn(javax.servlet.DispatcherType.FORWARD);
44+
45+
// WHEN the provider is asked for the DispatcherType
46+
DispatcherTypeProvider provider = new Servlet3DispatcherTypeProvider();
47+
DispatcherType dispatcherType = provider.getDispatcherType(request, servletContext);
48+
49+
// THEN it should return FORWARD
50+
assertEquals(DispatcherType.FORWARD, dispatcherType);
51+
52+
}
53+
54+
@Test
55+
public void testShouldReturnNullForServlet25()
56+
{
57+
58+
// GIVEN a Servlet 2.5 container
59+
ServletContext servletContext = mock(ServletContext.class);
60+
when(servletContext.getMajorVersion()).thenReturn(2);
61+
HttpServletRequest request = mock(HttpServletRequest.class);
62+
when(request.getDispatcherType()).thenThrow(new IllegalStateException("Call not allowed"));
63+
64+
// WHEN the provider is asked for the DispatcherType
65+
DispatcherTypeProvider provider = new Servlet3DispatcherTypeProvider();
66+
DispatcherType dispatcherType = provider.getDispatcherType(request, servletContext);
67+
68+
// THEN it should return null
69+
assertNull(dispatcherType);
70+
71+
}
72+
73+
}

0 commit comments

Comments
 (0)