From 67037555120804860208a4c7ab24d4f9c12a2b96 Mon Sep 17 00:00:00 2001 From: Steve Ikeoka Date: Thu, 18 Apr 2024 10:12:54 -0700 Subject: [PATCH] [GWC-1233] Refactor inline JavaScript in the Seed Controller --- .../org/geowebcache/util/ServletUtils.java | 10 ++++++++++ .../geowebcache/rest/service/FormService.java | 4 ++-- .../org/geowebcache/rest/webresources/seed.js | 6 ++++++ .../rest/service/FormServiceTest.java | 20 ++++++++++++++++++- 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 geowebcache/rest/src/main/resources/org/geowebcache/rest/webresources/seed.js diff --git a/geowebcache/core/src/main/java/org/geowebcache/util/ServletUtils.java b/geowebcache/core/src/main/java/org/geowebcache/util/ServletUtils.java index 229d81657..53b4665c7 100644 --- a/geowebcache/core/src/main/java/org/geowebcache/util/ServletUtils.java +++ b/geowebcache/core/src/main/java/org/geowebcache/util/ServletUtils.java @@ -354,12 +354,22 @@ private static String[] URLDecode(String[] values, String encoding) { } public static String gwcHtmlHeader(String relBasePath, String pageTitle) { + return gwcHtmlHeader(relBasePath, pageTitle, null); + } + + public static String gwcHtmlHeader(String relBasePath, String pageTitle, String jsFile) { StringBuilder builder = new StringBuilder(); builder.append("\n"); builder.append("").append(pageTitle).append("\n"); builder.append("\n"); + if (jsFile != null) { + builder.append("\n"); + } builder.append("\n"); return builder.toString(); } diff --git a/geowebcache/rest/src/main/java/org/geowebcache/rest/service/FormService.java b/geowebcache/rest/src/main/java/org/geowebcache/rest/service/FormService.java index b8363bc33..d8afb564e 100644 --- a/geowebcache/rest/src/main/java/org/geowebcache/rest/service/FormService.java +++ b/geowebcache/rest/src/main/java/org/geowebcache/rest/service/FormService.java @@ -650,7 +650,7 @@ private void makeFormFooter(StringBuilder doc) { private void makeHeader(StringBuilder doc) { doc.append( "\n" - + ServletUtils.gwcHtmlHeader("../../", "GWC Seed Form") + + ServletUtils.gwcHtmlHeader("../../", "GWC Seed Form", "rest/web/seed.js") + "\n" + ServletUtils.gwcHtmlLogoLink("../../")); } @@ -824,7 +824,7 @@ private String makeKillallThreadsForm(TileLayer tl, boolean listAll) { .append(escapeHtml4(layerName)) .append("\" method=\"post\">\n"); doc.append("List "); - doc.append("\n"); doc.append("\n"); diff --git a/geowebcache/rest/src/main/resources/org/geowebcache/rest/webresources/seed.js b/geowebcache/rest/src/main/resources/org/geowebcache/rest/webresources/seed.js new file mode 100644 index 000000000..019874f5a --- /dev/null +++ b/geowebcache/rest/src/main/resources/org/geowebcache/rest/webresources/seed.js @@ -0,0 +1,6 @@ + +window.onload = function() { + document.getElementById('list').list.onchange = function() { + document.getElementById('list').submit(); + }; +}; diff --git a/geowebcache/rest/src/test/java/org/geowebcache/rest/service/FormServiceTest.java b/geowebcache/rest/src/test/java/org/geowebcache/rest/service/FormServiceTest.java index 33f3077ec..fd6ac6886 100644 --- a/geowebcache/rest/src/test/java/org/geowebcache/rest/service/FormServiceTest.java +++ b/geowebcache/rest/src/test/java/org/geowebcache/rest/service/FormServiceTest.java @@ -18,12 +18,12 @@ import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.verify; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasProperty; import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.util.Arrays; @@ -100,6 +100,24 @@ public void testEscaping() throws Exception { assertThat(body, containsString(escapedRegex)); } + @Test + public void testRemovedInlineJavaScript() throws Exception { + TileLayer tl = EasyMock.createMock("tl", TileLayer.class); + expect(breeder.findTileLayer("testLayer")).andReturn(tl); + expect(tl.getName()).andStubReturn("testLayer"); + expect(breeder.getRunningAndPendingTasks()).andReturn(Collections.emptyIterator()).times(2); + expect(tl.getGridSubsets()).andReturn(Collections.emptySet()).times(4); + expect(tl.getMimeTypes()).andReturn(Collections.emptyList()); + expect(tl.getParameterFilters()).andReturn(Collections.emptyList()); + replay(tl, breeder); + ResponseEntity response = service.handleGet(null, "testLayer"); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + String body = (String) response.getBody(); + assertThat(body, containsString("")); + assertThat(body, not(containsString(" onchange="))); + } + @Test public void testKill() { Map form = new HashMap<>();