Skip to content

Commit 6630f04

Browse files
author
Vladimir Kotal
authored
add API endpoint for include files reload (#2325)
fixes #2323
1 parent 15e1bc3 commit 6630f04

File tree

3 files changed

+101
-6
lines changed

3 files changed

+101
-6
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,15 @@ public boolean isLastEditedDisplayMode() {
294294
public String getIncludeRootPath() {
295295
return threadConfig.get().getIncludeRoot();
296296
}
297-
297+
298+
/**
299+
* Set include root path
300+
* @param includeRoot path
301+
*/
302+
public void setIncludeRoot(String includeRoot) {
303+
threadConfig.get().setIncludeRoot(getCanonicalPath(includeRoot));
304+
}
305+
298306
/**
299307
* Get the path to the where the index database is stored
300308
*
@@ -1500,11 +1508,15 @@ public void setConfiguration(Configuration configuration, List<String> subFileLi
15001508
reloadIncludeFiles(configuration);
15011509
}
15021510

1503-
private void reloadIncludeFiles(Configuration configuration1) {
1504-
configuration1.getBodyIncludeFileContent(true);
1505-
configuration1.getHeaderIncludeFileContent(true);
1506-
configuration1.getFooterIncludeFileContent(true);
1507-
configuration1.getForbiddenIncludeFileContent(true);
1511+
/**
1512+
* Reload the content of all include files.
1513+
* @param configuration configuration
1514+
*/
1515+
public void reloadIncludeFiles(Configuration configuration) {
1516+
configuration.getBodyIncludeFileContent(true);
1517+
configuration.getHeaderIncludeFileContent(true);
1518+
configuration.getFooterIncludeFileContent(true);
1519+
configuration.getForbiddenIncludeFileContent(true);
15081520
}
15091521

15101522
public Configuration getConfiguration() {

opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/SystemController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ public void refresh(final String project) {
4848
suggester.refresh(project);
4949
}
5050

51+
@PUT
52+
@Path("/includes/reload")
53+
public void reloadIncludes() {
54+
env.reloadIncludeFiles(env.getConfiguration());
55+
}
5156
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.opengrok.web.api.v1.controller;
2+
3+
4+
import org.glassfish.jersey.test.JerseyTest;
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.opengrok.indexer.configuration.Configuration;
9+
import org.opengrok.indexer.configuration.RuntimeEnvironment;
10+
import org.opengrok.indexer.util.IOUtils;
11+
import org.opengrok.web.api.v1.RestApp;
12+
13+
import javax.ws.rs.client.Entity;
14+
import javax.ws.rs.core.Application;
15+
import javax.ws.rs.core.Response;
16+
17+
import java.io.File;
18+
import java.io.IOException;
19+
import java.io.PrintWriter;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
22+
23+
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.assertNotEquals;
25+
26+
public class SystemControllerTest extends JerseyTest {
27+
28+
private RuntimeEnvironment env = RuntimeEnvironment.getInstance();
29+
30+
@Override
31+
protected Application configure() {
32+
return new RestApp();
33+
}
34+
35+
/**
36+
* This method tests include files reload by testing it for one specific file out of the whole set.
37+
* @throws IOException
38+
*/
39+
@Test
40+
public void testIncludeReload() throws IOException {
41+
// Set new include root directory.
42+
Path includeRootPath = Files.createTempDirectory("systemControllerTest");
43+
File includeRoot = includeRootPath.toFile();
44+
env.setIncludeRoot(includeRoot.getAbsolutePath());
45+
assertEquals(includeRoot.getCanonicalPath(), env.getIncludeRootPath());
46+
47+
// Create footer include file.
48+
File footerFile = new File(includeRoot, Configuration.FOOTER_INCLUDE_FILE);
49+
String content = "foo";
50+
try (PrintWriter out = new PrintWriter(footerFile)) {
51+
out.println(content);
52+
}
53+
54+
// Sanity check that getFooterIncludeFileContent() works since the test depends on it.
55+
String before = env.getConfiguration().getFooterIncludeFileContent(false);
56+
assertEquals(content, before.trim());
57+
58+
// Modify the contents of the file.
59+
content = content + "bar";
60+
try (PrintWriter out = new PrintWriter(footerFile)) {
61+
out.println(content);
62+
}
63+
64+
// Reload the contents via API call.
65+
Response r = target("system")
66+
.path("includes").path("reload")
67+
.request().put(Entity.text(""));
68+
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), r.getStatus());
69+
70+
// Check that the content was reloaded.
71+
String after = env.getConfiguration().getFooterIncludeFileContent(false);
72+
assertNotEquals(before, after);
73+
assertEquals(content, after.trim());
74+
75+
// Cleanup
76+
IOUtils.removeRecursive(includeRootPath);
77+
}
78+
}

0 commit comments

Comments
 (0)