|
22 | 22 | */
|
23 | 23 | package org.opengrok.indexer.index;
|
24 | 24 |
|
| 25 | +import javax.ws.rs.ProcessingException; |
| 26 | +import javax.ws.rs.WebApplicationException; |
25 | 27 | import javax.ws.rs.client.ClientBuilder;
|
26 | 28 | 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; |
27 | 32 |
|
28 | 33 | class IndexerUtil {
|
29 | 34 |
|
30 | 35 | private IndexerUtil() {
|
31 | 36 | }
|
32 | 37 |
|
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 | + } |
42 | 67 | }
|
43 |
| - |
44 | 68 | }
|
0 commit comments