Skip to content

Commit 5fc870e

Browse files
tulinkryVladimir Kotal
authored andcommitted
checking if projects are enabled before enabling them (#2571)
fixes #2563
1 parent 0c03167 commit 5fc870e

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ public static void main(String argv[]) {
307307
try {
308308
IndexerUtil.enableProjects(webappURI);
309309
} catch (Exception e) {
310-
LOGGER.log(Level.SEVERE, "Mis-configuration of webapp webappURI", e);
311-
System.err.println("Couldn't notify the webapp: " + e.getLocalizedMessage());
310+
LOGGER.log(Level.SEVERE, String.format("Couldn't notify the webapp on %s.", webappURI), e);
311+
System.err.println(String.format("Couldn't notify the webapp on %s: %s.", webappURI, e.getLocalizedMessage()));
312312
}
313313
}
314314

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,47 @@
2222
*/
2323
package org.opengrok.indexer.index;
2424

25+
import javax.ws.rs.ProcessingException;
26+
import javax.ws.rs.WebApplicationException;
2527
import javax.ws.rs.client.ClientBuilder;
2628
import javax.ws.rs.client.Entity;
29+
import javax.ws.rs.client.Invocation;
30+
import javax.ws.rs.client.ResponseProcessingException;
31+
import javax.ws.rs.core.Response;
2732

2833
class IndexerUtil {
2934

3035
private IndexerUtil() {
3136
}
3237

33-
static void enableProjects(final String host) {
34-
ClientBuilder.newClient()
35-
.target(host)
36-
.path("api")
37-
.path("v1")
38-
.path("configuration")
39-
.path("projectsEnabled")
40-
.request()
41-
.put(Entity.text(Boolean.TRUE.toString()));
38+
/**
39+
* Enable projects in the remote host application.
40+
* <p>
41+
* NOTE: performs a check if the projects are already enabled,
42+
* before making the change request
43+
*
44+
* @param host the url to the remote host
45+
* @throws ResponseProcessingException in case processing of a received HTTP response fails
46+
* @throws ProcessingException in case the request processing or subsequent I/O operation fails
47+
* @throws WebApplicationException in case the response status code of the response returned by the server is not successful
48+
*/
49+
public static void enableProjects(final String host) throws
50+
ResponseProcessingException,
51+
ProcessingException,
52+
WebApplicationException {
53+
final Invocation.Builder request = ClientBuilder.newClient()
54+
.target(host)
55+
.path("api")
56+
.path("v1")
57+
.path("configuration")
58+
.path("projectsEnabled")
59+
.request();
60+
final String enabled = request.get(String.class);
61+
if (enabled == null || !Boolean.valueOf(enabled)) {
62+
final Response r = request.put(Entity.text(Boolean.TRUE.toString()));
63+
if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
64+
throw new WebApplicationException(String.format("Unable to enable projects: %s", r.getStatusInfo().getReasonPhrase()), r.getStatus());
65+
}
66+
}
4267
}
43-
4468
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.opengrok.indexer.index;
2+
3+
import javax.ws.rs.ProcessingException;
4+
import org.junit.Test;
5+
6+
public class IndexerUtilTest {
7+
@Test(expected = ProcessingException.class)
8+
public void testEnableProjectsInvalidUrl() {
9+
IndexerUtil.enableProjects("http://non-existent.server.com:123");
10+
}
11+
}

0 commit comments

Comments
 (0)