Skip to content

Commit f2a0080

Browse files
author
Vladimir Kotal
authored
configurable directory for web app include files (#2169)
fixes #2095
1 parent 37f743e commit f2a0080

File tree

8 files changed

+179
-18
lines changed

8 files changed

+179
-18
lines changed

src/org/opensolaris/opengrok/configuration/Configuration.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ public final class Configuration {
143143
private Set<Group> groups;
144144
private String sourceRoot;
145145
private String dataRoot;
146+
/**
147+
* directory with include files for web application (header, footer, etc.)
148+
*/
149+
private String includeRoot;
146150
private List<RepositoryInfo> repositories;
147151
/**
148152
* @deprecated This is kept around so not to break object deserialization
@@ -804,6 +808,18 @@ public void setDataRoot(String dataRoot) {
804808
this.dataRoot = dataRoot;
805809
}
806810

811+
/**
812+
* If {@link includeRoot} is not set, {@link dataRoot} will be returned.
813+
* @return web include root directory
814+
*/
815+
public String getIncludeRoot() {
816+
return includeRoot != null ? includeRoot : dataRoot;
817+
}
818+
819+
public void setIncludeRoot(String newRoot) {
820+
this.includeRoot = newRoot;
821+
}
822+
807823
public List<RepositoryInfo> getRepositories() {
808824
return repositories;
809825
}
@@ -1175,13 +1191,14 @@ private static String getFileContent(File file) {
11751191
/**
11761192
* Get the contents of the footer include file.
11771193
*
1194+
* @param force if true, reload even if already set
11781195
* @return an empty string if it could not be read successfully, the
11791196
* contents of the file otherwise.
11801197
* @see #FOOTER_INCLUDE_FILE
11811198
*/
1182-
public String getFooterIncludeFileContent() {
1183-
if (footer == null) {
1184-
footer = getFileContent(new File(getDataRoot(), FOOTER_INCLUDE_FILE));
1199+
public String getFooterIncludeFileContent(boolean force) {
1200+
if (footer == null || force) {
1201+
footer = getFileContent(new File(getIncludeRoot(), FOOTER_INCLUDE_FILE));
11851202
}
11861203
return footer;
11871204
}
@@ -1197,17 +1214,18 @@ public String getFooterIncludeFileContent() {
11971214
/**
11981215
* Get the contents of the header include file.
11991216
*
1217+
* @param force if true, reload even if already set
12001218
* @return an empty string if it could not be read successfully, the
12011219
* contents of the file otherwise.
12021220
* @see #HEADER_INCLUDE_FILE
12031221
*/
1204-
public String getHeaderIncludeFileContent() {
1205-
if (header == null) {
1206-
header = getFileContent(new File(getDataRoot(), HEADER_INCLUDE_FILE));
1222+
public String getHeaderIncludeFileContent(boolean force) {
1223+
if (header == null || force) {
1224+
header = getFileContent(new File(getIncludeRoot(), HEADER_INCLUDE_FILE));
12071225
}
12081226
return header;
12091227
}
1210-
1228+
12111229
/**
12121230
* The name of the file relative to the <var>DATA_ROOT</var>, which should
12131231
* be included into the body of web app's "Home" page.
@@ -1219,13 +1237,14 @@ public String getHeaderIncludeFileContent() {
12191237
/**
12201238
* Get the contents of the body include file.
12211239
*
1240+
* @param force if true, reload even if already set
12221241
* @return an empty string if it could not be read successfully, the
12231242
* contents of the file otherwise.
12241243
* @see Configuration#BODY_INCLUDE_FILE
12251244
*/
1226-
public String getBodyIncludeFileContent() {
1227-
if (body == null) {
1228-
body = getFileContent(new File(getDataRoot(), BODY_INCLUDE_FILE));
1245+
public String getBodyIncludeFileContent(boolean force) {
1246+
if (body == null || force) {
1247+
body = getFileContent(new File(getIncludeRoot(), BODY_INCLUDE_FILE));
12291248
}
12301249
return body;
12311250
}
@@ -1243,13 +1262,14 @@ public String getBodyIncludeFileContent() {
12431262
* Get the contents of the page for forbidden error page (403 Forbidden)
12441263
* include file.
12451264
*
1265+
* @param force if true, reload even if already set
12461266
* @return an empty string if it could not be read successfully, the
12471267
* contents of the file otherwise.
12481268
* @see Configuration#E_FORBIDDEN_INCLUDE_FILE
12491269
*/
1250-
public String getForbiddenIncludeFileContent() {
1251-
if (eforbidden_content == null) {
1252-
eforbidden_content = getFileContent(new File(getDataRoot(), E_FORBIDDEN_INCLUDE_FILE));
1270+
public String getForbiddenIncludeFileContent(boolean force) {
1271+
if (eforbidden_content == null || force) {
1272+
eforbidden_content = getFileContent(new File(getIncludeRoot(), E_FORBIDDEN_INCLUDE_FILE));
12531273
}
12541274
return eforbidden_content;
12551275
}

src/org/opensolaris/opengrok/configuration/RuntimeEnvironment.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,15 @@ public boolean isLastEditedDisplayMode() {
317317
return threadConfig.get().isLastEditedDisplayMode();
318318
}
319319

320+
/**
321+
* Get the path to the where the web application includes are stored
322+
*
323+
* @return the path to the web application include files
324+
*/
325+
public String getIncludeRootPath() {
326+
return threadConfig.get().getIncludeRoot();
327+
}
328+
320329
/**
321330
* Get the path to the where the index database is stored
322331
*
@@ -1552,6 +1561,15 @@ public void setConfiguration(Configuration configuration, List<String> subFileLi
15521561
// The invalidation of repositories above might have excluded some
15531562
// repositories in HistoryGuru so the configuration needs to reflect that.
15541563
configuration.setRepositories(new ArrayList<>(histGuru.getRepositories()));
1564+
1565+
reloadIncludeFiles(configuration);
1566+
}
1567+
1568+
private void reloadIncludeFiles(Configuration configuration1) {
1569+
configuration1.getBodyIncludeFileContent(true);
1570+
configuration1.getHeaderIncludeFileContent(true);
1571+
configuration1.getFooterIncludeFileContent(true);
1572+
configuration1.getForbiddenIncludeFileContent(true);
15551573
}
15561574

15571575
public Configuration getConfiguration() {

src/org/opensolaris/opengrok/web/AuthorizationFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) thr
7575
config.getEnv().getStatistics().addRequestTime("requests_forbidden",
7676
System.currentTimeMillis() - processTime);
7777

78-
if (!config.getEnv().getConfiguration().getForbiddenIncludeFileContent().isEmpty()) {
78+
if (!config.getEnv().getConfiguration().getForbiddenIncludeFileContent(false).isEmpty()) {
7979
sr.getRequestDispatcher("/eforbidden").forward(sr, sr1);
8080
return;
8181
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opensolaris.opengrok.configuration;
24+
25+
import java.io.BufferedWriter;
26+
import java.io.File;
27+
import java.io.FileWriter;
28+
import java.io.IOException;
29+
import java.nio.file.Files;
30+
import java.nio.file.Path;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
34+
import static org.junit.Assert.assertEquals;
35+
36+
/**
37+
* Test include file functionality for web application.
38+
*
39+
* @author Vladimir Kotal
40+
*/
41+
public class IncludeFilesTest {
42+
static Path includeRoot;
43+
static final String CONTENT_1 = "foo";
44+
static final String CONTENT_2 = "bar";
45+
static RuntimeEnvironment env = RuntimeEnvironment.getInstance();
46+
static final String LINE_SEP = System.lineSeparator();
47+
48+
@BeforeClass
49+
public static void setUpClass() throws IOException {
50+
includeRoot = Files.createTempDirectory("include_root");
51+
env.getConfiguration().setIncludeRoot(includeRoot.toString());
52+
}
53+
54+
private void writeStringToFile(File file, String str) throws IOException {
55+
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
56+
bw.write(str);
57+
}
58+
}
59+
60+
@Test
61+
public void testGetHeaderIncludeFileContent() throws IOException {
62+
File file = new File(includeRoot.toFile(), Configuration.HEADER_INCLUDE_FILE);
63+
writeStringToFile(file, CONTENT_1);
64+
assertEquals(CONTENT_1 + LINE_SEP,
65+
env.getConfiguration().getHeaderIncludeFileContent(false));
66+
writeStringToFile(file, CONTENT_2);
67+
assertEquals(CONTENT_2 + LINE_SEP,
68+
env.getConfiguration().getHeaderIncludeFileContent(true));
69+
}
70+
71+
@Test
72+
public void testGetBodyIncludeFileContent() throws IOException {
73+
File file = new File(includeRoot.toFile(), Configuration.BODY_INCLUDE_FILE);
74+
writeStringToFile(file, CONTENT_1);
75+
assertEquals(CONTENT_1 + LINE_SEP,
76+
env.getConfiguration().getBodyIncludeFileContent(false));
77+
writeStringToFile(file, CONTENT_2);
78+
assertEquals(CONTENT_2 + LINE_SEP,
79+
env.getConfiguration().getBodyIncludeFileContent(true));
80+
}
81+
82+
@Test
83+
public void testGetFooterIncludeFileContent() throws IOException {
84+
File file = new File(includeRoot.toFile(), Configuration.FOOTER_INCLUDE_FILE);
85+
writeStringToFile(file, CONTENT_1);
86+
assertEquals(CONTENT_1 + LINE_SEP,
87+
env.getConfiguration().getFooterIncludeFileContent(false));
88+
writeStringToFile(file, CONTENT_2);
89+
assertEquals(CONTENT_2 + LINE_SEP,
90+
env.getConfiguration().getFooterIncludeFileContent(true));
91+
}
92+
93+
@Test
94+
public void testGetForbiddenIncludeFileContent() throws IOException {
95+
File file = new File(includeRoot.toFile(), Configuration.E_FORBIDDEN_INCLUDE_FILE);
96+
writeStringToFile(file, CONTENT_1);
97+
assertEquals(CONTENT_1 + LINE_SEP,
98+
env.getConfiguration().getForbiddenIncludeFileContent(false));
99+
writeStringToFile(file, CONTENT_2);
100+
assertEquals(CONTENT_2 + LINE_SEP,
101+
env.getConfiguration().getForbiddenIncludeFileContent(true));
102+
}
103+
}

test/org/opensolaris/opengrok/configuration/RuntimeEnvironmentTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* CDDL HEADER END
1818
*/
1919

20-
/*
20+
/*
2121
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
2323
*/
@@ -119,6 +119,26 @@ public void testDataRoot() throws IOException {
119119
assertEquals(path, instance.getDataRootFile().getCanonicalPath());
120120
}
121121

122+
@Test
123+
public void testIncludeRoot() throws IOException {
124+
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
125+
assertNull(instance.getIncludeRootPath());
126+
127+
// set data root
128+
File f = File.createTempFile("dataroot", null);
129+
String path = f.getCanonicalPath();
130+
instance.setDataRoot(path);
131+
132+
// verify they are the same
133+
assertEquals(instance.getDataRootPath(), instance.getIncludeRootPath());
134+
135+
// set include root
136+
f = File.createTempFile("includeroot", null);
137+
path = f.getCanonicalPath();
138+
instance.getConfiguration().setIncludeRoot(path);
139+
assertEquals(path, instance.getIncludeRootPath());
140+
}
141+
122142
@Test
123143
public void testSourceRoot() throws IOException {
124144
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();

web/foot.jspf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ org.opensolaris.opengrok.web.Prefix"
4545
<% if(dateForLastIndexRun != null) { %>
4646
<p>Last Index update <%= dateForLastIndexRun %></p>
4747
<%}%>
48-
<%= cfg.getEnv().getConfiguration().getFooterIncludeFileContent() %>
48+
<%= cfg.getEnv().getConfiguration().getFooterIncludeFileContent(false) %>
4949
<%
5050
if (needAddDiv.contains(cfg.getPrefix())) {
5151
%></div><% // #content

web/index.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ include file="menu.jspf"
5858
%></div>
5959
</div>
6060
<div id="results">
61-
<%= PageConfig.get(request).getEnv().getConfiguration().getBodyIncludeFileContent() %>
61+
<%= PageConfig.get(request).getEnv().getConfiguration().getBodyIncludeFileContent(false) %>
6262
<% if (PageConfig.get(request).getEnv().getDisplayRepositories()) { %><%@
6363
6464
include file="repos.jspf"

web/pageheader.jspf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
2424
{
2525
PageConfig cfg = PageConfig.get(request);
2626
%>
27-
<%= cfg.getEnv().getConfiguration().getHeaderIncludeFileContent() %>
27+
<%= cfg.getEnv().getConfiguration().getHeaderIncludeFileContent(false) %>
2828
<%
2929
}
3030
/* ---------------------- pageheader.jspf end --------------------- */

0 commit comments

Comments
 (0)