Skip to content

Commit 8bf9039

Browse files
committed
avoid marking project as indexed in the webapp if not known
fixes #4799
1 parent e34493b commit 8bf9039

File tree

3 files changed

+81
-37
lines changed

3 files changed

+81
-37
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexDatabase.java

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.nio.file.Paths;
3939
import java.util.ArrayList;
4040
import java.util.Arrays;
41+
import java.util.Collection;
4142
import java.util.Comparator;
4243
import java.util.Date;
4344
import java.util.HashMap;
@@ -423,44 +424,21 @@ private void markProjectIndexed(Project project) {
423424
return;
424425
}
425426

426-
// Also need to store the correct value in configuration
427-
// when indexer writes it to a file.
427+
// Also need to store the correct value in configuration when indexer writes it to a file.
428428
project.setIndexed(true);
429429

430430
if (env.getConfigURI() == null) {
431431
return;
432432
}
433433

434-
Response response;
435-
try {
436-
response = ClientBuilder.newBuilder().connectTimeout(env.getConnectTimeout(), TimeUnit.SECONDS).build()
437-
.target(env.getConfigURI())
438-
.path("api")
439-
.path("v1")
440-
.path("projects")
441-
.path(Util.uriEncode(project.getName()))
442-
.path("indexed")
443-
.request()
444-
.headers(getWebAppHeaders())
445-
.put(Entity.text(""));
446-
} catch (RuntimeException e) {
447-
LOGGER.log(Level.WARNING, String.format("Could not notify the webapp that project %s was indexed",
448-
project), e);
434+
// If this project is not known to the webapp yet, there is no point in setting its indexed property.
435+
Collection<String> webappProjects = IndexerUtil.getProjects(env.getConfigURI());
436+
if (!webappProjects.contains(project.getName())) {
437+
LOGGER.log(Level.FINEST, "Project {0} is not known to the webapp", project);
449438
return;
450439
}
451440

452-
if (response.getStatus() == Response.Status.ACCEPTED.getStatusCode()) {
453-
try {
454-
response = waitForAsyncApi(response);
455-
} catch (InterruptedException e) {
456-
LOGGER.log(Level.WARNING, "interrupted while waiting for API response", e);
457-
}
458-
}
459-
460-
if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
461-
LOGGER.log(Level.WARNING, "Could not notify the webapp that project {0} was indexed: {1}",
462-
new Object[] {project, response});
463-
}
441+
IndexerUtil.markProjectIndexed(env.getConfigURI(), project);
464442
}
465443

466444
private static List<Repository> getRepositoriesForProject(Project project) {

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexerUtil.java

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.index;
2424

2525
import jakarta.ws.rs.client.Client;
26+
import jakarta.ws.rs.core.GenericType;
27+
import jakarta.ws.rs.core.MediaType;
28+
import org.opengrok.indexer.configuration.Project;
2629
import org.opengrok.indexer.configuration.RuntimeEnvironment;
2730

2831
import jakarta.ws.rs.ProcessingException;
@@ -35,11 +38,21 @@
3538
import jakarta.ws.rs.core.MultivaluedHashMap;
3639
import jakarta.ws.rs.core.MultivaluedMap;
3740
import jakarta.ws.rs.core.Response;
41+
import org.opengrok.indexer.logger.LoggerFactory;
42+
import org.opengrok.indexer.web.Util;
3843

44+
import java.util.Collection;
45+
import java.util.List;
3946
import java.util.concurrent.TimeUnit;
47+
import java.util.logging.Level;
48+
import java.util.logging.Logger;
49+
50+
import static org.opengrok.indexer.web.ApiUtils.waitForAsyncApi;
4051

4152
public class IndexerUtil {
4253

54+
private static final Logger LOGGER = LoggerFactory.getLogger(IndexerUtil.class);
55+
4356
private IndexerUtil() {
4457
}
4558

@@ -57,24 +70,24 @@ public static MultivaluedMap<String, Object> getWebAppHeaders() {
5770
}
5871

5972
/**
60-
* Enable projects in the remote host application.
73+
* Enable projects in the remote application.
6174
* <p>
6275
* NOTE: performs a check if the projects are already enabled,
6376
* before making the change request
6477
*
65-
* @param host the url to the remote host
78+
* @param webappUri the url to the remote web application
6679
* @throws ResponseProcessingException in case processing of a received HTTP response fails
6780
* @throws ProcessingException in case the request processing or subsequent I/O operation fails
6881
* @throws WebApplicationException in case the response status code of the response returned by the server is not successful
6982
*/
70-
public static void enableProjects(final String host) throws
83+
public static void enableProjects(final String webappUri) throws
7184
ResponseProcessingException,
7285
ProcessingException,
7386
WebApplicationException {
7487

7588
try (Client client = ClientBuilder.newBuilder().
7689
connectTimeout(RuntimeEnvironment.getInstance().getConnectTimeout(), TimeUnit.SECONDS).build()) {
77-
final Invocation.Builder request = client.target(host)
90+
final Invocation.Builder request = client.target(webappUri)
7891
.path("api")
7992
.path("v1")
8093
.path("configuration")
@@ -92,4 +105,58 @@ public static void enableProjects(final String host) throws
92105
}
93106
}
94107
}
108+
109+
/**
110+
* Mark project as indexed via API call. Assumes the project is already known to the webapp.
111+
* @param webappUri URI for the webapp
112+
* @param project project to mark as indexed
113+
*/
114+
public static void markProjectIndexed(String webappUri, Project project) {
115+
Response response;
116+
try (Client client = ClientBuilder.newBuilder().
117+
connectTimeout(RuntimeEnvironment.getInstance().getConnectTimeout(), TimeUnit.SECONDS).build()) {
118+
response = client.target(webappUri)
119+
.path("api")
120+
.path("v1")
121+
.path("projects")
122+
.path(Util.uriEncode(project.getName()))
123+
.path("indexed")
124+
.request()
125+
.headers(getWebAppHeaders())
126+
.put(Entity.text(""));
127+
128+
if (response.getStatus() == Response.Status.ACCEPTED.getStatusCode()) {
129+
try {
130+
response = waitForAsyncApi(response);
131+
} catch (InterruptedException e) {
132+
LOGGER.log(Level.WARNING, "interrupted while waiting for API response", e);
133+
}
134+
}
135+
136+
if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
137+
LOGGER.log(Level.WARNING, "Could not notify the webapp that project {0} was indexed: {1}",
138+
new Object[] {project, response});
139+
}
140+
} catch (RuntimeException e) {
141+
LOGGER.log(Level.WARNING, String.format("Could not notify the webapp that project %s was indexed",
142+
project), e);
143+
}
144+
}
145+
146+
/**
147+
* @param webappUri URI for the webapp
148+
* @return list of projects known to the webapp
149+
*/
150+
public static Collection<String> getProjects(String webappUri) {
151+
try (Client client = ClientBuilder.newBuilder().
152+
connectTimeout(RuntimeEnvironment.getInstance().getConnectTimeout(), TimeUnit.SECONDS).build()) {
153+
final Invocation.Builder request = client.target(webappUri)
154+
.path("api")
155+
.path("v1")
156+
.path("projects")
157+
.request(MediaType.APPLICATION_JSON)
158+
.headers(getWebAppHeaders());
159+
return request.get(new GenericType<List<String>>(){});
160+
}
161+
}
95162
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public Response markIndexed(@Context HttpServletRequest request, @PathParam("pro
358358
Repository repo = getRepository(ri, CommandTimeoutType.RESTFUL);
359359

360360
if (repo != null && repo.getCurrentVersion() != null &&
361-
repo.getCurrentVersion().length() > 0) {
361+
!repo.getCurrentVersion().isEmpty()) {
362362
// getRepository() always creates fresh instance
363363
// of the Repository object so there is no need
364364
// to call setCurrentVersion() on it.
@@ -420,8 +420,7 @@ public Object get(@PathParam("project") String projectName, @PathParam("field")
420420

421421
Project project = env.getProjects().get(projectName);
422422
if (project == null) {
423-
throw new WebApplicationException(
424-
"cannot find project '" + projectName + "' to get a property", Response.Status.BAD_REQUEST);
423+
throw new NotFoundException(String.format("cannot find project '%s' to get a property", projectName));
425424
}
426425
return ClassUtil.getFieldValue(project, field);
427426
}

0 commit comments

Comments
 (0)