xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexerUtil.java (revision c6f0939b1c668e9f8e1e276424439c3106b3a029)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
22  */
23 package org.opengrok.indexer.index;
24 
25 import org.opengrok.indexer.configuration.RuntimeEnvironment;
26 
27 import jakarta.ws.rs.ProcessingException;
28 import jakarta.ws.rs.WebApplicationException;
29 import jakarta.ws.rs.client.ClientBuilder;
30 import jakarta.ws.rs.client.Entity;
31 import jakarta.ws.rs.client.Invocation;
32 import jakarta.ws.rs.client.ResponseProcessingException;
33 import jakarta.ws.rs.core.HttpHeaders;
34 import jakarta.ws.rs.core.MultivaluedHashMap;
35 import jakarta.ws.rs.core.MultivaluedMap;
36 import jakarta.ws.rs.core.Response;
37 
38 public class IndexerUtil {
39 
IndexerUtil()40     private IndexerUtil() {
41     }
42 
43     /**
44      * @return map of HTTP headers to use when making API requests to the web application
45      */
getWebAppHeaders()46     public static MultivaluedMap<String, Object> getWebAppHeaders() {
47         MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
48         String token = null;
49         if ((token = RuntimeEnvironment.getInstance().getIndexerAuthenticationToken()) != null) {
50             headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + token);
51         }
52 
53         return headers;
54     }
55 
56     /**
57      * Enable projects in the remote host application.
58      * <p>
59      * NOTE: performs a check if the projects are already enabled,
60      * before making the change request
61      *
62      * @param host the url to the remote host
63      * @throws ResponseProcessingException in case processing of a received HTTP response fails
64      * @throws ProcessingException         in case the request processing or subsequent I/O operation fails
65      * @throws WebApplicationException     in case the response status code of the response returned by the server is not successful
66      */
enableProjects(final String host)67     public static void enableProjects(final String host) throws
68             ResponseProcessingException,
69             ProcessingException,
70             WebApplicationException {
71         final Invocation.Builder request = ClientBuilder.newClient()
72                                                         .target(host)
73                                                         .path("api")
74                                                         .path("v1")
75                                                         .path("configuration")
76                                                         .path("projectsEnabled")
77                                                         .request()
78                                                         .headers(getWebAppHeaders());
79         final String enabled = request.get(String.class);
80         if (!Boolean.parseBoolean(enabled)) {
81             final Response r = request.put(Entity.text(Boolean.TRUE.toString()));
82             if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
83                 throw new WebApplicationException(String.format("Unable to enable projects: %s", r.getStatusInfo().getReasonPhrase()), r.getStatus());
84             }
85         }
86     }
87 }
88