Skip to content

Commit

Permalink
[GWC-1233] Refactor inline JavaScript in the Seed Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
sikeoka committed Apr 18, 2024
1 parent 360f6a1 commit 6703755
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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("<head>\n");
builder.append("<title>").append(pageTitle).append("</title>\n");
builder.append("<link rel=\"stylesheet\" href=\"")
.append(relBasePath)
.append("rest/web/gwc.css\" type=\"text/css\"/>\n");
if (jsFile != null) {
builder.append("<script src=\"")
.append(relBasePath)
.append(jsFile)
.append("\"></script>\n");
}
builder.append("</head>\n");
return builder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ private void makeFormFooter(StringBuilder doc) {
private void makeHeader(StringBuilder doc) {
doc.append(
"<html>\n"
+ ServletUtils.gwcHtmlHeader("../../", "GWC Seed Form")
+ ServletUtils.gwcHtmlHeader("../../", "GWC Seed Form", "rest/web/seed.js")
+ "<body>\n"
+ ServletUtils.gwcHtmlLogoLink("../../"));
}
Expand Down Expand Up @@ -824,7 +824,7 @@ private String makeKillallThreadsForm(TileLayer tl, boolean listAll) {
.append(escapeHtml4(layerName))
.append("\" method=\"post\">\n");
doc.append("List ");
doc.append("<select name=\"list\" onchange=\"this.form.submit();\">\n");
doc.append("<select name=\"list\">\n");
doc.append("<option value=\"layer\"")
.append(listAll ? "" : " selected")
.append(">this Layer tasks</option>\n");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

window.onload = function() {
document.getElementById('list').list.onchange = function() {
document.getElementById('list').submit();
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("<script src=\"../../rest/web/seed.js\"></script>"));
assertThat(body, not(containsString(" onchange=")));
}

@Test
public void testKill() {
Map<String, String> form = new HashMap<>();
Expand Down

0 comments on commit 6703755

Please sign in to comment.