Skip to content

Commit 7b595b4

Browse files
authored
Merge pull request #133 from cip4/jakarta2
Jakarta2 - better resource fowarding
2 parents 645b350 + 76f53d4 commit 7b595b4

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/main/java/org/cip4/jdfutility/server/JettyServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ public void runServer() throws Exception
305305
final Sequence handlers = createHandlerList();
306306
final Sequence handlerbase = createBaseCollection(handlers);
307307
server.setHandler(handlerbase);
308+
server.setDefaultHandler(new RedirectHandler());
308309
server.start();
309310
log.info("completed starting new server: " + toString());
310311
}

src/main/java/org/cip4/jdfutility/server/MyResourceHandler.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class MyResourceHandler extends ResourceHandler
6161
public MyResourceHandler(final String strip, final String home)
6262
{
6363
super();
64-
this.strip = StringUtil.getNonEmpty(strip);
64+
this.strip = strip == null ? null : StringUtil.token(StringUtil.getNonEmpty(strip.toLowerCase()), 0, "/");
6565
this.home = home;
6666
whiteList = new HashSet<>();
6767
}
@@ -70,19 +70,23 @@ public MyResourceHandler(final String strip, final String home)
7070

7171
String update(String url)
7272
{
73-
74-
if (strip != null && url.startsWith(strip) && (url.length() == strip.length() || url.charAt(strip.length()) == '/'))
73+
final String urlLow = url.toLowerCase();
74+
int pos = StringUtil.posOfToken(urlLow, strip, "/", 0);
75+
if (pos >= 0)
7576
{
76-
url = StringUtil.removeToken(url, 0, "/");
77+
pos = urlLow.indexOf(strip);
78+
url = url.substring(0, pos) + url.substring(pos + StringUtil.length(strip) + 1);
7779
}
80+
7881
if ("".equals(url) || "/".equals(url))
7982
{
8083
return home;
8184
}
8285
else if (!whiteList.isEmpty())
8386
{
8487
final String base = StringUtil.token(url, 0, "/");
85-
if (!whiteList.contains(base.toLowerCase()))
88+
final String base2 = StringUtil.token(url, 2, "/"); // http://host:port/root
89+
if (!whiteList.contains(base.toLowerCase()) && (base2 == null || !whiteList.contains(base2.toLowerCase())))
8690
{
8791
return null;
8892
}
@@ -110,6 +114,9 @@ public String toString()
110114
@Override
111115
public boolean handle(final Request request, final Response response, final Callback callback) throws Exception
112116
{
117+
final String context = request.getContext().getContextPath();
118+
if (StringUtil.isEmpty(context))
119+
return false;
113120
final HttpURI uri = request.getHttpURI();
114121
final String uriString = update(uri.asString());
115122
if (uriString == null)

src/test/java/org/cip4/jdfutility/server/JettyServerTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ public void testResHandler() throws InterruptedException
170170
{
171171
final MyResourceHandler rh = new MyResourceHandler("foo", "dummy");
172172
assertEquals("nix", rh.update("nix"));
173+
assertEquals("nix", rh.update("foo/nix"));
174+
assertEquals("http://localhost/bar/nix", rh.update("http://localhost/bar/foo/nix"));
173175
assertEquals("dummy", rh.update(""));
176+
assertEquals("dummy", rh.update("/"));
174177
}
175178

176179
@Test
@@ -181,6 +184,9 @@ public void testResHandlerWL() throws InterruptedException
181184
assertEquals(null, rh.update("foo/nix"));
182185
assertEquals("boo", rh.update("foo/boo"));
183186
assertEquals("boo", rh.update("boo"));
187+
assertEquals("http://localhost/boo/nix", rh.update("http://localhost/foo/boo/nix"));
188+
assertEquals("http://localhost/BOO/nix", rh.update("http://localhost/foo/BOO/nix"));
189+
assertEquals("http://localhost/BOO/nix", rh.update("http://localhost/FOO/BOO/nix"));
184190
}
185191

186192
@Test

0 commit comments

Comments
 (0)