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 */
2323package org .opengrok .indexer .index ;
2424
2525import 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 ;
2629import org .opengrok .indexer .configuration .RuntimeEnvironment ;
2730
2831import jakarta .ws .rs .ProcessingException ;
3538import jakarta .ws .rs .core .MultivaluedHashMap ;
3639import jakarta .ws .rs .core .MultivaluedMap ;
3740import 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 ;
3946import 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
4152public 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}
0 commit comments