Skip to content

Commit 68c19b4

Browse files
AlexandrSergei4Vladimir Kotal
authored andcommitted
Add possibility to include text from http_header_include file into the HTTP header
fixes #2071
1 parent 3b65bef commit 68c19b4

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Configuration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
/*
2121
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017-2019, Chris Fraire <[email protected]>.
23+
* Portions Copyright (c) 2020, Aleksandr Kirillov <[email protected]>.
2324
*/
2425
package org.opengrok.indexer.configuration;
2526

@@ -1165,6 +1166,13 @@ public void setListDirsFirst(boolean flag) {
11651166
*/
11661167
public static final String E_FORBIDDEN_INCLUDE_FILE = "error_forbidden_include";
11671168

1169+
1170+
/**
1171+
* The name of the file relative to the <var>DATA_ROOT</var>, which should
1172+
* be included into the HTTP header of generated web pages.
1173+
*/
1174+
public static final String HTTP_HEADER_INCLUDE_FILE = "http_header_include";
1175+
11681176
/**
11691177
* @return path to the file holding compiled path descriptions for the web application
11701178
*/

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/IncludeFiles.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2020, Aleksandr Kirillov <[email protected]>.
2223
*/
2324

2425
package org.opengrok.indexer.configuration;
@@ -36,6 +37,7 @@ public void reloadIncludeFiles() {
3637
getHeaderIncludeFileContent(true);
3738
getFooterIncludeFileContent(true);
3839
getForbiddenIncludeFileContent(true);
40+
getHttpHeaderIncludeFileContent(true);
3941
}
4042

4143
private transient String footer = null;
@@ -110,4 +112,22 @@ public String getForbiddenIncludeFileContent(boolean force) {
110112
}
111113
return eforbidden_content;
112114
}
115+
116+
private transient String http_header = null;
117+
118+
/**
119+
* Get the contents of the HTTP header include file.
120+
*
121+
* @param force if true, reload even if already set
122+
* @return an empty string if it could not be read successfully, the
123+
* contents of the file otherwise.
124+
* @see Configuration#HTTP_HEADER_INCLUDE_FILE
125+
*/
126+
public String getHttpHeaderIncludeFileContent(boolean force) {
127+
if (http_header == null || force) {
128+
http_header = getFileContent(new File(RuntimeEnvironment.getInstance().getIncludeRootPath(),
129+
Configuration.HTTP_HEADER_INCLUDE_FILE));
130+
}
131+
return http_header;
132+
}
113133
}

opengrok-indexer/src/test/java/org/opengrok/indexer/configuration/IncludeFilesTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2020, Aleksandr Kirillov <[email protected]>.
2223
*/
2324
package org.opengrok.indexer.configuration;
2425

@@ -100,4 +101,15 @@ public void testGetForbiddenIncludeFileContent() throws IOException {
100101
assertEquals(CONTENT_2 + LINE_SEP,
101102
env.includeFiles.getForbiddenIncludeFileContent(true));
102103
}
104+
105+
@Test
106+
public void testGetHttpHeaderIncludeFileContent() throws IOException {
107+
File file = new File(includeRoot.toFile(), Configuration.HTTP_HEADER_INCLUDE_FILE);
108+
writeStringToFile(file, CONTENT_1);
109+
assertEquals(CONTENT_1 + LINE_SEP,
110+
env.includeFiles.getHttpHeaderIncludeFileContent(false));
111+
writeStringToFile(file, CONTENT_2);
112+
assertEquals(CONTENT_2 + LINE_SEP,
113+
env.includeFiles.getHttpHeaderIncludeFileContent(true));
114+
}
103115
}

opengrok-web/src/main/webapp/httpheader.jspf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ CDDL HEADER END
2121
Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
2222
Portions Copyright 2011 Jens Elkner.
2323
Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
24+
Portions Copyright (c) 2020, Aleksandr Kirillov <[email protected]>.
2425

2526
--%><%--
2627

@@ -98,6 +99,7 @@ if (cfg.getPrefix().equals(Prefix.HIST_L)) {
9899
document.domReady = [];
99100
/* ]]> */
100101
</script>
102+
<%= cfg.getEnv().getIncludeFiles().getHttpHeaderIncludeFileContent(false) %>
101103
<title><%=cfg.getTitle()%></title><%
102104
out.write(cfg.getHeaderData());
103105
%>

0 commit comments

Comments
 (0)