18
18
*/
19
19
20
20
/*
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.
22
22
*/
23
23
package org .opengrok .indexer .index ;
24
24
25
25
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 ;
26
29
import org .opengrok .indexer .configuration .RuntimeEnvironment ;
27
30
28
31
import jakarta .ws .rs .ProcessingException ;
35
38
import jakarta .ws .rs .core .MultivaluedHashMap ;
36
39
import jakarta .ws .rs .core .MultivaluedMap ;
37
40
import jakarta .ws .rs .core .Response ;
41
+ import org .opengrok .indexer .logger .LoggerFactory ;
42
+ import org .opengrok .indexer .web .Util ;
38
43
44
+ import java .util .Collection ;
45
+ import java .util .List ;
39
46
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 ;
40
51
41
52
public class IndexerUtil {
42
53
54
+ private static final Logger LOGGER = LoggerFactory .getLogger (IndexerUtil .class );
55
+
43
56
private IndexerUtil () {
44
57
}
45
58
@@ -57,24 +70,24 @@ public static MultivaluedMap<String, Object> getWebAppHeaders() {
57
70
}
58
71
59
72
/**
60
- * Enable projects in the remote host application.
73
+ * Enable projects in the remote application.
61
74
* <p>
62
75
* NOTE: performs a check if the projects are already enabled,
63
76
* before making the change request
64
77
*
65
- * @param host the url to the remote host
78
+ * @param webappUri the url to the remote web application
66
79
* @throws ResponseProcessingException in case processing of a received HTTP response fails
67
80
* @throws ProcessingException in case the request processing or subsequent I/O operation fails
68
81
* @throws WebApplicationException in case the response status code of the response returned by the server is not successful
69
82
*/
70
- public static void enableProjects (final String host ) throws
83
+ public static void enableProjects (final String webappUri ) throws
71
84
ResponseProcessingException ,
72
85
ProcessingException ,
73
86
WebApplicationException {
74
87
75
88
try (Client client = ClientBuilder .newBuilder ().
76
89
connectTimeout (RuntimeEnvironment .getInstance ().getConnectTimeout (), TimeUnit .SECONDS ).build ()) {
77
- final Invocation .Builder request = client .target (host )
90
+ final Invocation .Builder request = client .target (webappUri )
78
91
.path ("api" )
79
92
.path ("v1" )
80
93
.path ("configuration" )
@@ -92,4 +105,58 @@ public static void enableProjects(final String host) throws
92
105
}
93
106
}
94
107
}
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
+ }
95
162
}
0 commit comments