Skip to content

Commit e01d455

Browse files
committed
Fixed some warnings, improved test harness API, improved test to include oubound rewriting (still need to verify results,) fixed potential ISException on duplicate rewrites
1 parent 52e2722 commit e01d455

9 files changed

Lines changed: 123 additions & 28 deletions

File tree

‎api/src/main/java/com/ocpsoft/rewrite/services/ServiceLoader.java‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,27 @@ public class ServiceLoader<S> implements Iterable<S>
7272
* @return A new service loader
7373
*/
7474
@SuppressWarnings("rawtypes")
75-
public static <S> ServiceLoader/* TODO fix this generic */load(final Class<S> service)
75+
public static <S> ServiceLoader load(final Class<S> service)
76+
{
77+
return load(service, Thread.currentThread().getContextClassLoader());
78+
}
79+
80+
/**
81+
* Creates a new service loader for the given service type, using the current thread's context class loader.
82+
*
83+
* An invocation of this convenience method of the form
84+
*
85+
* {@code ServiceLoader.load(service)</code>}
86+
*
87+
* is equivalent to
88+
*
89+
* <code>ServiceLoader.load(service,
90+
* Thread.currentThread().getContextClassLoader())</code>
91+
*
92+
* @param service The interface or abstract class representing the service
93+
* @return A new service loader
94+
*/
95+
public static <S> ServiceLoader<S> loadTypesafe(final Class<S> service)
7696
{
7797
return load(service, Thread.currentThread().getContextClassLoader());
7898
}
@@ -185,8 +205,7 @@ private void loadServiceFile(final URL serviceFile)
185205
}
186206
catch (IOException e)
187207
{
188-
// FIXME: correct exception
189-
throw new RuntimeException("Could not read services file " + serviceFile);
208+
throw new RuntimeException("Could not read services file " + serviceFile, e);
190209
}
191210
finally
192211
{
@@ -198,7 +217,6 @@ private void loadServiceFile(final URL serviceFile)
198217
}
199218
catch (IOException e)
200219
{
201-
// FIXME: correct exception
202220
throw new RuntimeException("Could not close services file " + serviceFile, e);
203221
}
204222
}
@@ -246,7 +264,7 @@ private Class<? extends S> loadClass(final String serviceClassName)
246264
}
247265
catch (ClassCastException e)
248266
{
249-
throw new RuntimeException("ClassCastException: Service class [" + serviceClassName
267+
throw new ClassCastException("ClassCastException: Service class [" + serviceClassName
250268
+ "] did not implement the interface [" + expectedType.getName() + "]");
251269
}
252270
return serviceClass;

‎api/src/test/java/com/ocpsoft/rewrite/services/ServiceLoaderTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
public class ServiceLoaderTest
3131
{
3232
@Test
33+
@SuppressWarnings("unchecked")
3334
public void test()
3435
{
3536
ServiceLoader<TestService> services = ServiceLoader.load(TestService.class);

‎impl-servlet/src/main/java/com/ocpsoft/rewrite/servlet/RewriteFilter.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class RewriteFilter implements Filter
6060
@SuppressWarnings("unchecked")
6161
public void init(final FilterConfig filterConfig) throws ServletException
6262
{
63-
// TODO SPI pre filter init
63+
// TODO SPI pre filter init?
6464
log.info("RewriteFilter starting up...");
6565

6666
listeners = Iterators.asList(ServiceLoader.load(RewriteLifecycleListener.class));
@@ -82,7 +82,7 @@ public void init(final FilterConfig filterConfig) throws ServletException
8282
logLoadedServices(OutboundRewriteProducer.class, outbound);
8383

8484
log.info("RewriteFilter initialized.");
85-
// TODO SPI post filter init
85+
// TODO SPI post filter init?
8686
}
8787

8888
@Override
@@ -146,7 +146,8 @@ public InboundServletRewrite<ServletRequest, ServletResponse> createRewriteEvent
146146
{
147147
for (InboundRewriteProducer<ServletRequest, ServletResponse> producer : inbound)
148148
{
149-
InboundServletRewrite<ServletRequest, ServletResponse> event = producer.createInboundRewrite(request, response);
149+
InboundServletRewrite<ServletRequest, ServletResponse> event = producer
150+
.createInboundRewrite(request, response);
150151
if (event != null)
151152
return event;
152153
}
@@ -194,7 +195,7 @@ else if (event.getFlow().is(Flow.INCLUDE))
194195
@Override
195196
public void destroy()
196197
{
197-
// TODO SPI filter destroy
198+
// TODO SPI filter destroy?
198199
}
199200

200201
private <T> void logLoadedServices(final Class<T> type, final List<? extends T> services)

‎impl-servlet/src/main/java/com/ocpsoft/rewrite/servlet/http/event/HttpInboundServletRewrite.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.ocpsoft.rewrite.servlet.http.event;
1717

18+
import javax.servlet.ServletContext;
1819
import javax.servlet.http.HttpServletRequest;
1920
import javax.servlet.http.HttpServletResponse;
2021

@@ -46,13 +47,15 @@ public interface HttpInboundServletRewrite extends InboundServletRewrite<HttpSer
4647

4748
/**
4849
* If the {@link HttpServletResponse} is not already committed, issue a Temporary Redirect (302) and call
49-
* {@link #abort()}.
50+
* {@link #abort()}. This location must be include {@link #getContextPath()} if attempting to redirect within the
51+
* current {@link ServletContext}
5052
*/
5153
public void redirectTemporary(final String location);
5254

5355
/**
5456
* If the {@link HttpServletResponse} is not already committed, issue a Permanent Redirect (302) and call
55-
* {@link #abort()}.
57+
* {@link #abort()}. This location must be include {@link #getContextPath()} if attempting to redirect within the
58+
* current {@link ServletContext}
5659
*/
5760
public void redirectPermanent(final String location);
5861

‎integration-cdi/src/test/java/com/ocpsoft/rewrite/cdi/bridge/RewriteLifecycleEventObserver.java‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import javax.enterprise.context.RequestScoped;
2525
import javax.enterprise.event.Observes;
2626

27+
import com.ocpsoft.rewrite.servlet.event.RewriteBase.Flow;
2728
import com.ocpsoft.rewrite.servlet.http.event.HttpInboundServletRewrite;
29+
import com.ocpsoft.rewrite.servlet.http.event.HttpOutboundServletRewrite;
2830

2931
/**
3032
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
@@ -33,11 +35,24 @@
3335
@RequestScoped
3436
public class RewriteLifecycleEventObserver
3537
{
38+
public void rewriteInbound2(@Observes final HttpInboundServletRewrite event)
39+
{
40+
System.out.println("Inbound: " + event.getRequestURL());
41+
if (event.getRequestURL().equals("/redirect-301")) {
42+
event.redirectTemporary(event.getContextPath() + "/outbound");
43+
}
44+
}
45+
46+
public void rewriteOutbound(@Observes final HttpOutboundServletRewrite event)
47+
{
48+
if (event.getOutboundURL().equals(event.getContextPath() + "/outbound")) {
49+
event.setOutboundURL(event.getContextPath() + "/outbound-rewritten");
50+
}
51+
}
52+
3653
public void rewriteInbound(@Observes final HttpInboundServletRewrite event)
3754
{
38-
if (event.getRequestURL().equals("/page")) {
39-
System.out.println("Inbound: " + event.getRequestURL());
55+
if (!event.getFlow().is(Flow.HANDLED))
4056
event.sendStatusCode(200);
41-
}
4257
}
4358
}

‎integration-cdi/src/test/java/com/ocpsoft/rewrite/cdi/bridge/RewriteProviderBridgeTest.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323

2424
import junit.framework.Assert;
2525

26-
import org.apache.http.HttpResponse;
26+
import org.apache.http.client.methods.HttpGet;
2727
import org.jboss.arquillian.container.test.api.Deployment;
2828
import org.jboss.arquillian.junit.Arquillian;
2929
import org.jboss.shrinkwrap.api.spec.WebArchive;
3030
import org.junit.Test;
3131
import org.junit.runner.RunWith;
3232

3333
import com.ocpsoft.rewrite.cdi.CDIRoot;
34+
import com.ocpsoft.rewrite.test.HttpAction;
3435
import com.ocpsoft.rewrite.test.RewriteTestBase;
3536

3637
/**
@@ -54,14 +55,14 @@ public static WebArchive getDeployment()
5455
@Test
5556
public void testRewriteProviderBridgeAcceptsChanges()
5657
{
57-
HttpResponse response = request("/page");
58-
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
58+
HttpAction<HttpGet> action = get("/success");
59+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
5960
}
6061

6162
@Test
62-
public void testRewriteProviderBridgeIgnoresUnchangedEvent()
63+
public void testRewriteRedirect301()
6364
{
64-
HttpResponse response = request("/unchanged");
65-
Assert.assertEquals(404, response.getStatusLine().getStatusCode());
65+
HttpAction<HttpGet> action = get("/redirect-301");
66+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
6667
}
6768
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2011, Red Hat, Inc., and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* This is free software; you can redistribute it and/or modify it
8+
* under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation; either version 2.1 of
10+
* the License, or (at your option) any later version.
11+
*
12+
* This software is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this software; if not, write to the Free
19+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21+
*/
22+
package com.ocpsoft.rewrite.test;
23+
24+
import org.apache.http.HttpRequest;
25+
import org.apache.http.HttpResponse;
26+
import org.apache.http.client.HttpClient;
27+
28+
/**
29+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
30+
*
31+
*/
32+
public class HttpAction<T extends HttpRequest>
33+
{
34+
private final HttpClient client;
35+
private final T request;
36+
private final HttpResponse response;
37+
38+
public HttpAction(final HttpClient httpClient, final T httpGet, final HttpResponse response)
39+
{
40+
this.client = httpClient;
41+
this.request = httpGet;
42+
this.response = response;
43+
}
44+
45+
public HttpClient getClient()
46+
{
47+
return client;
48+
}
49+
50+
public T getRequest()
51+
{
52+
return request;
53+
}
54+
55+
public HttpResponse getResponse()
56+
{
57+
return response;
58+
}
59+
}

‎test-harness/src/main/java/com/ocpsoft/rewrite/test/RewriteTestBase.java‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,24 @@ private static Collection<GenericArchive> resolveDependencies(final String coord
9191
* <p>
9292
* E.g: A path of '/example' will be sent as '/rewrite-test/example'
9393
*/
94-
protected HttpResponse request(final String path)
94+
protected HttpAction<HttpGet> get(final String path)
9595
{
96-
DefaultHttpClient httpclient = new DefaultHttpClient();
96+
DefaultHttpClient httpClient = new DefaultHttpClient();
9797
try {
9898
String url = baseURL.toExternalForm();
9999
if (url.endsWith("/"))
100100
{
101101
url = url.substring(0, url.length() - 1);
102102
}
103103
url = url + path;
104-
HttpGet httpget = new HttpGet(url);
105-
106-
HttpResponse response = httpclient.execute(httpget);
104+
HttpGet httpGet = new HttpGet(url);
107105

106+
HttpResponse response = httpClient.execute(httpGet);
108107
HttpEntity entity = response.getEntity();
109108
if (entity != null)
110109
EntityUtils.consume(entity);
111110

112-
return response;
111+
return new HttpAction<HttpGet>(httpClient, httpGet, response);
113112
}
114113
catch (Exception e) {
115114
throw new RuntimeException(e);

‎test-harness/src/main/resources/jetty-log4j.xml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3-
42
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
53
<appender name="console" class="org.apache.log4j.ConsoleAppender">
64
<param name="Target" value="System.out" />

0 commit comments

Comments
 (0)