Skip to content

Commit d0c24b9

Browse files
committed
Move the ObjectifyFilter into ObjectifyService
Offer a Filter and FilterJavax depending on which API version we're using
1 parent a4037f1 commit d0c24b9

5 files changed

Lines changed: 123 additions & 52 deletions

File tree

pom.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,16 @@
183183

184184
<dependency>
185185
<groupId>javax.servlet</groupId>
186-
<artifactId>servlet-api</artifactId>
187-
<version>2.5</version>
186+
<artifactId>javax.servlet-api</artifactId>
187+
<version>4.0.1</version>
188+
<type>jar</type>
189+
<scope>provided</scope>
190+
</dependency>
191+
192+
<dependency>
193+
<groupId>jakarta.servlet</groupId>
194+
<artifactId>jakarta.servlet-api</artifactId>
195+
<version>6.0.0</version>
188196
<scope>provided</scope>
189197
</dependency>
190198

src/main/java/com/googlecode/objectify/ObjectifyFilter.java

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,31 @@
33

44
package com.googlecode.objectify;
55

6-
import com.googlecode.objectify.util.AbstractFilter;
76
import com.googlecode.objectify.util.Closeable;
7+
8+
import javax.servlet.Filter;
89
import javax.servlet.FilterChain;
10+
import javax.servlet.FilterConfig;
911
import javax.servlet.ServletException;
1012
import javax.servlet.ServletRequest;
1113
import javax.servlet.ServletResponse;
1214
import java.io.IOException;
1315

1416
/**
15-
* Configure this filter to use Objectify in your application. It works in concert with {@code ObjectifyService}
16-
* to provide the correct {@code Objectify} instance when {@code ObjectifyService.ofy()} is called.
17-
*
18-
* In your web.xml:
19-
*<pre>
20-
* &lt;filter&gt;
21-
* &lt;filter-name&gt;ObjectifyFilter&lt;/filter-name&gt;
22-
* &lt;filter-class&gt;com.googlecode.objectify.ObjectifyFilter&lt;/filter-class&gt;
23-
* &lt;/filter&gt;
24-
* &lt;filter-mapping&gt;
25-
* &lt;filter-name&gt;ObjectifyFilter&lt;/filter-name&gt;
26-
* &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
27-
* &lt;/filter-mapping&gt;
28-
*</pre>
29-
*
30-
* Or, if you use Guice:
31-
*
32-
*<pre>
33-
* filter("/*").through(ObjectifyFilter.class);
34-
*</pre>
35-
*
36-
* <p>If you use the Objectify outside of the context of a request (say, using the remote
37-
* API or from a unit test), then you should use the ObjectifyService.run() method.</p>
38-
*
39-
* @author Jeff Schnitzer
17+
* @deprecated use {@code ObjectifyService.Filter} or {@code ObjectifyService.FilterJavax} instead
4018
*/
41-
public class ObjectifyFilter extends AbstractFilter
19+
@Deprecated
20+
public class ObjectifyFilter implements Filter
4221
{
43-
/** */
4422
@Override
4523
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
46-
4724
try (Closeable closeable = ObjectifyService.begin()) {
4825
chain.doFilter(request, response);
4926
}
5027
}
28+
29+
@Override
30+
public void init(final FilterConfig filterConfig) throws ServletException {}
31+
@Override
32+
public void destroy() {}
5133
}

src/main/java/com/googlecode/objectify/ObjectifyService.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55

66
import com.google.common.base.Preconditions;
77
import com.googlecode.objectify.util.Closeable;
8+
import jakarta.servlet.FilterChain;
9+
import jakarta.servlet.FilterConfig;
10+
import jakarta.servlet.ServletException;
11+
import jakarta.servlet.ServletRequest;
12+
import jakarta.servlet.ServletResponse;
13+
14+
import java.io.IOException;
815

916
/**
1017
* <p>Most applications connect to a single datastore. To make your life easy, we offer this
@@ -156,4 +163,90 @@ public static <T> Ref<T> ref(final Key<T> key) {
156163
public static <T> Ref<T> ref(final T value) {
157164
return factory().ref(value);
158165
}
166+
167+
/**
168+
* <p>This version is for the newer jakarta.servlet.* API. If you are using the older javax.servlet.*, use {@code FilterJavax}.</p>
169+
*
170+
* <p>Configure this filter to use Objectify in your application. It works in concert with {@code ObjectifyService}
171+
* to provide the correct {@code Objectify} instance when {@code ObjectifyService.ofy()} is called.</p>
172+
*
173+
* <p>In your web.xml:</p>
174+
*<pre>
175+
* &lt;filter&gt;
176+
* &lt;filter-name&gt;ObjectifyFilter&lt;/filter-name&gt;
177+
* &lt;filter-class&gt;com.googlecode.objectify.ObjectifyService$Filter&lt;/filter-class&gt;
178+
* &lt;/filter&gt;
179+
* &lt;filter-mapping&gt;
180+
* &lt;filter-name&gt;ObjectifyFilter&lt;/filter-name&gt;
181+
* &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
182+
* &lt;/filter-mapping&gt;
183+
*</pre>
184+
*
185+
* <p>Or, if you use Guice:</p>
186+
*
187+
*<pre>
188+
* filter("/*").through(ObjectifyService.Filter.class);
189+
*</pre>
190+
*
191+
* <p>If you use the Objectify outside of the context of a request (say, using the remote
192+
* API or from a unit test), then you should use the ObjectifyService.run() method.</p>
193+
*
194+
* @author Jeff Schnitzer
195+
*/
196+
public static class Filter implements jakarta.servlet.Filter {
197+
@Override
198+
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
199+
try (Closeable closeable = ObjectifyService.begin()) {
200+
chain.doFilter(request, response);
201+
}
202+
}
203+
204+
@Override
205+
public void init(final FilterConfig filterConfig) throws ServletException {}
206+
@Override
207+
public void destroy() {}
208+
}
209+
210+
/**
211+
* <p>This version is for the older javax.servlet.* API. If you are using the newer jakarta.servlet.*, use {@code ObjectifyFilter}.</p>
212+
*
213+
* <p>Configure this filter to use Objectify in your application. It works in concert with {@code ObjectifyService}
214+
* to provide the correct {@code Objectify} instance when {@code ObjectifyService.ofy()} is called.</p>
215+
*
216+
* <p>In your web.xml:</p>
217+
*<pre>
218+
* &lt;filter&gt;
219+
* &lt;filter-name&gt;ObjectifyFilter&lt;/filter-name&gt;
220+
* &lt;filter-class&gt;com.googlecode.objectify.ObjectifyService$FilterJavax&lt;/filter-class&gt;
221+
* &lt;/filter&gt;
222+
* &lt;filter-mapping&gt;
223+
* &lt;filter-name&gt;ObjectifyFilter&lt;/filter-name&gt;
224+
* &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
225+
* &lt;/filter-mapping&gt;
226+
*</pre>
227+
*
228+
* <p>Or, if you use Guice:</p>
229+
*
230+
*<pre>
231+
* filter("/*").through(ObjectifyService.FilterJavax.class);
232+
*</pre>
233+
*
234+
* <p>If you use the Objectify outside of the context of a request (say, using the remote
235+
* API or from a unit test), then you should use the ObjectifyService.run() method.</p>
236+
*
237+
* @author Jeff Schnitzer
238+
*/
239+
public static class FilterJavax implements javax.servlet.Filter {
240+
@Override
241+
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) throws IOException, javax.servlet.ServletException {
242+
try (Closeable closeable = ObjectifyService.begin()) {
243+
chain.doFilter(request, response);
244+
}
245+
}
246+
247+
@Override
248+
public void init(final javax.servlet.FilterConfig filterConfig) throws javax.servlet.ServletException {}
249+
@Override
250+
public void destroy() {}
251+
}
159252
}

src/main/java/com/googlecode/objectify/cache/AsyncCacheFilter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.googlecode.objectify.cache;
22

3-
import com.googlecode.objectify.util.AbstractFilter;
3+
import javax.servlet.Filter;
44
import javax.servlet.FilterChain;
5+
import javax.servlet.FilterConfig;
56
import javax.servlet.ServletException;
67
import javax.servlet.ServletRequest;
78
import javax.servlet.ServletResponse;
@@ -33,15 +34,15 @@
3334
* filter("/*").through(AsyncCacheFilter.class);
3435
*</pre>
3536
*
36-
* <p>Note that you do not need to configure this filter if you use the {@code ObjectifyFilter}.</p>
37+
* <p>Note that you do not need to configure this filter if you are using Objectify.</p>
3738
*
3839
* <p>If you use the CachingAsyncDatastoreService outside of the context of a request (say, using the remote
3940
* API or from a unit test), then you should call {@code AsyncCacheFilter.complete()} after every operation
4041
* that you consider a "request". For example, after each test.</p>
4142
*
4243
* @author Jeff Schnitzer <jeff@infohazard.org>
4344
*/
44-
public class AsyncCacheFilter extends AbstractFilter
45+
public class AsyncCacheFilter implements Filter
4546
{
4647
@Override
4748
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
@@ -58,4 +59,9 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
5859
public static void complete() {
5960
PendingFutures.completeAllPendingFutures();
6061
}
62+
63+
@Override
64+
public void init(final FilterConfig filterConfig) throws ServletException {}
65+
@Override
66+
public void destroy() {}
6167
}

src/main/java/com/googlecode/objectify/util/AbstractFilter.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)