Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.indexer.configuration;

Expand All @@ -27,22 +27,28 @@
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
import org.opengrok.indexer.logger.LoggerFactory;

public class IndexTimestamp {
private Date lastModified;

private static final Logger LOGGER = LoggerFactory.getLogger(IndexTimestamp.class);

@VisibleForTesting
public static final String TIMESTAMP_FILE_NAME = "timestamp";

/**
* Get the date of the last index update.
*
* @return the time of the last index update.
* @return the time of the last index update or {@code null}.
*/
public Date getDateForLastIndexRun() {
public @Nullable Date getDateForLastIndexRun() {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
if (lastModified == null) {
File timestamp = new File(env.getDataRootFile(), "timestamp");
File timestamp = new File(env.getDataRootFile(), TIMESTAMP_FILE_NAME);
if (timestamp.exists()) {
lastModified = new Date(timestamp.lastModified());
}
Expand Down
17 changes: 15 additions & 2 deletions opengrok-web/src/test/java/org/opengrok/web/PageConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.opengrok.indexer.authorization.AuthControlFlag;
import org.opengrok.indexer.authorization.AuthorizationFramework;
import org.opengrok.indexer.authorization.AuthorizationPlugin;
import org.opengrok.indexer.authorization.TestPlugin;
import org.opengrok.indexer.condition.EnabledForRepository;
import org.opengrok.indexer.configuration.IndexTimestamp;
import org.opengrok.indexer.configuration.Project;
import org.opengrok.indexer.configuration.RuntimeEnvironment;
import org.opengrok.indexer.history.Annotation;
Expand Down Expand Up @@ -655,8 +657,9 @@ public String getPathInfo() {
};
}

@Test
void testIsNotModifiedEtag() {
@ParameterizedTest
@ValueSource(booleans = {true, false})
void testIsNotModifiedEtag(boolean createTimestamp) throws IOException {
HttpServletRequest req = new DummyHttpServletRequest() {
@Override
public String getHeader(String name) {
Expand All @@ -672,6 +675,16 @@ public String getPathInfo() {
}
};

// The ETag value depends on the timestamp file.
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.refreshDateForLastIndexRun();
Path timestampPath = Path.of(env.getDataRootPath(), IndexTimestamp.TIMESTAMP_FILE_NAME);
Files.deleteIfExists(timestampPath);
if (createTimestamp) {
Files.createFile(timestampPath);
assertTrue(timestampPath.toFile().exists());
}

PageConfig cfg = PageConfig.get(req);
HttpServletResponse resp = mock(HttpServletResponse.class);
assertFalse(cfg.isNotModified(req, resp));
Expand Down
Loading