Skip to content

Commit 78fccc0

Browse files
Servlet Tools.
1 parent 175a880 commit 78fccc0

File tree

7 files changed

+240
-1
lines changed

7 files changed

+240
-1
lines changed

pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,20 @@
6666
<version>1.0.1</version>
6767
</dependency>
6868

69-
<!-- Logging -->
69+
<!-- SLF4J Logging -->
7070
<dependency>
7171
<groupId>org.slf4j</groupId>
7272
<artifactId>slf4j-api</artifactId>
7373
<version>1.7.25</version>
7474
</dependency>
7575

76+
<!-- Commons Logging -->
77+
<dependency>
78+
<groupId>commons-logging</groupId>
79+
<artifactId>commons-logging</artifactId>
80+
<version>1.1.1</version>
81+
</dependency>
82+
7683
<!-- Caching API. -->
7784
<dependency>
7885
<groupId>javax.cache</groupId>
@@ -109,6 +116,7 @@
109116
<module>taskmaster-cache-ehcache</module>
110117
<module>taskmaster-core</module>
111118
<module>taskmaster-service-helper</module>
119+
<module>taskmaster-servlet-tools</module>
112120
</modules>
113121

114122
</project>

taskmaster-core/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
<version>${project.version}</version>
3434
</dependency>
3535

36+
<!-- TaskMaster Servlet Tools -->
37+
<dependency>
38+
<groupId>com.github.bordertech.taskmaster</groupId>
39+
<artifactId>taskmaster-servlet-tools</artifactId>
40+
<version>${project.version}</version>
41+
</dependency>
42+
3643
<!-- Didums Injection -->
3744
<dependency>
3845
<groupId>com.github.bordertech.didums</groupId>

taskmaster-servlet-tools/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?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">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.github.bordertech.taskmaster</groupId>
6+
<name>taskmaster-servlet-tools</name>
7+
<artifactId>taskmaster-servlet-tools</artifactId>
8+
<version>1.0.2-SNAPSHOT</version>
9+
10+
<parent>
11+
<groupId>com.github.bordertech.taskmaster</groupId>
12+
<artifactId>taskmaster-parent</artifactId>
13+
<version>1.0.2-SNAPSHOT</version>
14+
<relativePath>../pom.xml</relativePath>
15+
</parent>
16+
17+
<packaging>jar</packaging>
18+
19+
<properties>
20+
<bt.qa.skip>false</bt.qa.skip>
21+
</properties>
22+
23+
<description>
24+
Task Master provides Servlet Tools such as filters and listeners.
25+
</description>
26+
27+
<dependencies>
28+
29+
<!-- Commons Logging -->
30+
<dependency>
31+
<groupId>commons-logging</groupId>
32+
<artifactId>commons-logging</artifactId>
33+
</dependency>
34+
35+
<!-- Servlet Interface -->
36+
<dependency>
37+
<groupId>javax.servlet</groupId>
38+
<artifactId>servlet-api</artifactId>
39+
<scope>provided</scope>
40+
</dependency>
41+
42+
</dependencies>
43+
</project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.github.bordertech.taskmaster.servlet.combo;
2+
3+
import java.io.IOException;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import javax.servlet.Filter;
8+
import javax.servlet.FilterChain;
9+
import javax.servlet.FilterConfig;
10+
import javax.servlet.ServletException;
11+
import javax.servlet.ServletRequest;
12+
import javax.servlet.ServletResponse;
13+
14+
/**
15+
* Combo Filter that allows a group of Filters to be defined in one class.
16+
* <p>
17+
* This allows multiple filters to be easily annotated in one class instead of multiple entries in a web.xml.
18+
* </p>
19+
*/
20+
public abstract class AbstractComboFilter implements Filter {
21+
22+
private final List<Filter> filters;
23+
24+
/**
25+
* @param filters the group of filters to combine into a single filter
26+
*/
27+
public AbstractComboFilter(final Filter... filters) {
28+
this(Arrays.asList(filters));
29+
}
30+
31+
/**
32+
* @param filters the group of filters to combine into a single filter
33+
*/
34+
public AbstractComboFilter(final List<Filter> filters) {
35+
this.filters = Collections.unmodifiableList(filters);
36+
}
37+
38+
/**
39+
* @return the list of filters to be combined
40+
*/
41+
public final List<Filter> getFilters() {
42+
return filters;
43+
}
44+
45+
@Override
46+
public void init(final FilterConfig config) throws ServletException {
47+
// Config each filter
48+
for (Filter filter : getFilters()) {
49+
filter.init(config);
50+
}
51+
}
52+
53+
@Override
54+
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
55+
throws IOException, ServletException {
56+
ComboFilterChain combo = new ComboFilterChain(chain, getFilters());
57+
combo.doFilter(request, response);
58+
}
59+
60+
@Override
61+
public void destroy() {
62+
// Destroy in reverse order
63+
for (int i = getFilters().size(); i-- > 0;) {
64+
Filter filter = getFilters().get(i);
65+
filter.destroy();
66+
}
67+
}
68+
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.github.bordertech.taskmaster.servlet.combo;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import javax.servlet.ServletContextEvent;
7+
import javax.servlet.ServletContextListener;
8+
import org.apache.commons.logging.Log;
9+
import org.apache.commons.logging.LogFactory;
10+
11+
/**
12+
* Combo ServletListener that allows a group of Listeners to be defined in one class.
13+
* <p>
14+
* This allows multiple ServletListeners to be easily annotated in one class instead of multiple entries in a web.xml.
15+
* </p>
16+
*/
17+
public abstract class AbstractComboServletListener implements ServletContextListener {
18+
19+
private static final Log LOGGER = LogFactory.getLog(AbstractComboServletListener.class);
20+
21+
private final List<ServletContextListener> listeners;
22+
23+
/**
24+
* @param listeners the group of listeners to combine into a single listener
25+
*/
26+
public AbstractComboServletListener(final ServletContextListener... listeners) {
27+
this(Arrays.asList(listeners));
28+
}
29+
30+
/**
31+
* @param listeners the group of listeners to combine into a single listener
32+
*/
33+
public AbstractComboServletListener(final List<ServletContextListener> listeners) {
34+
this.listeners = Collections.unmodifiableList(listeners);
35+
}
36+
37+
/**
38+
*
39+
* @return the group of listeners to combine
40+
*/
41+
public final List<ServletContextListener> getListeners() {
42+
return listeners;
43+
}
44+
45+
@Override
46+
public void contextInitialized(final ServletContextEvent sce) {
47+
for (ServletContextListener listener : getListeners()) {
48+
try {
49+
listener.contextInitialized(sce);
50+
} catch (Exception e) {
51+
LOGGER.error("Error calling initialized servlet context listener [" + listener.getClass().getName() + "]. "
52+
+ e.getMessage(), e);
53+
}
54+
}
55+
}
56+
57+
@Override
58+
public void contextDestroyed(final ServletContextEvent sce) {
59+
for (ServletContextListener listener : getListeners()) {
60+
try {
61+
listener.contextDestroyed(sce);
62+
} catch (Exception e) {
63+
LOGGER.error("Error calling destoryed servlet context listener [" + listener.getClass().getName() + "]. "
64+
+ e.getMessage(), e);
65+
}
66+
}
67+
}
68+
69+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.bordertech.taskmaster.servlet.combo;
2+
3+
import java.io.IOException;
4+
import java.util.List;
5+
import javax.servlet.Filter;
6+
import javax.servlet.FilterChain;
7+
import javax.servlet.ServletException;
8+
import javax.servlet.ServletRequest;
9+
import javax.servlet.ServletResponse;
10+
11+
/**
12+
* Process the combo filters and then the original chain.
13+
*/
14+
public class ComboFilterChain implements FilterChain {
15+
16+
private final FilterChain chain;
17+
private final List<Filter> combo;
18+
private int position = 0;
19+
20+
/**
21+
* @param chain the original servlet filter chain
22+
* @param comboFilters the list of combined filters to process
23+
*/
24+
public ComboFilterChain(final FilterChain chain, final List<Filter> comboFilters) {
25+
this.chain = chain;
26+
this.combo = comboFilters;
27+
}
28+
29+
@Override
30+
public void doFilter(final ServletRequest request, final ServletResponse response) throws IOException, ServletException {
31+
if (position == combo.size()) {
32+
chain.doFilter(request, response);
33+
} else {
34+
Filter nextFilter = combo.get(position++);
35+
nextFilter.doFilter(request, response, this);
36+
}
37+
}
38+
39+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Combine Servlet Filters and Listeners into a single class.
3+
*/
4+
package com.github.bordertech.taskmaster.servlet.combo;

0 commit comments

Comments
 (0)