11a2bb6b6SAdam Hornáček /* 21a2bb6b6SAdam Hornáček * CDDL HEADER START 31a2bb6b6SAdam Hornáček * 41a2bb6b6SAdam Hornáček * The contents of this file are subject to the terms of the 51a2bb6b6SAdam Hornáček * Common Development and Distribution License (the "License"). 61a2bb6b6SAdam Hornáček * You may not use this file except in compliance with the License. 71a2bb6b6SAdam Hornáček * 81a2bb6b6SAdam Hornáček * See LICENSE.txt included in this distribution for the specific 91a2bb6b6SAdam Hornáček * language governing permissions and limitations under the License. 101a2bb6b6SAdam Hornáček * 111a2bb6b6SAdam Hornáček * When distributing Covered Code, include this CDDL HEADER in each 121a2bb6b6SAdam Hornáček * file and include the License file at LICENSE.txt. 131a2bb6b6SAdam Hornáček * If applicable, add the following below this CDDL HEADER, with the 141a2bb6b6SAdam Hornáček * fields enclosed by brackets "[]" replaced with your own identifying 151a2bb6b6SAdam Hornáček * information: Portions Copyright [yyyy] [name of copyright owner] 161a2bb6b6SAdam Hornáček * 171a2bb6b6SAdam Hornáček * CDDL HEADER END 181a2bb6b6SAdam Hornáček */ 191a2bb6b6SAdam Hornáček 201a2bb6b6SAdam Hornáček /* 21*c6f0939bSAdam Hornacek * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. 221a2bb6b6SAdam Hornáček */ 231a2bb6b6SAdam Hornáček package org.opengrok.indexer.index; 241a2bb6b6SAdam Hornáček 258d34347cSVladimir Kotal import org.opengrok.indexer.configuration.RuntimeEnvironment; 268d34347cSVladimir Kotal 27aa6abf42SAdam Hornacek import jakarta.ws.rs.ProcessingException; 28aa6abf42SAdam Hornacek import jakarta.ws.rs.WebApplicationException; 29aa6abf42SAdam Hornacek import jakarta.ws.rs.client.ClientBuilder; 30aa6abf42SAdam Hornacek import jakarta.ws.rs.client.Entity; 31aa6abf42SAdam Hornacek import jakarta.ws.rs.client.Invocation; 32aa6abf42SAdam Hornacek import jakarta.ws.rs.client.ResponseProcessingException; 33aa6abf42SAdam Hornacek import jakarta.ws.rs.core.HttpHeaders; 34aa6abf42SAdam Hornacek import jakarta.ws.rs.core.MultivaluedHashMap; 35aa6abf42SAdam Hornacek import jakarta.ws.rs.core.MultivaluedMap; 36aa6abf42SAdam Hornacek import jakarta.ws.rs.core.Response; 371a2bb6b6SAdam Hornáček 388d34347cSVladimir Kotal public class IndexerUtil { 391a2bb6b6SAdam Hornáček IndexerUtil()401a2bb6b6SAdam Hornáček private IndexerUtil() { 411a2bb6b6SAdam Hornáček } 421a2bb6b6SAdam Hornáček 435fc870e4SKryštof Tulinger /** 448d34347cSVladimir Kotal * @return map of HTTP headers to use when making API requests to the web application 458d34347cSVladimir Kotal */ getWebAppHeaders()46f1e34e10SVladimir Kotal public static MultivaluedMap<String, Object> getWebAppHeaders() { 478d34347cSVladimir Kotal MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(); 488d34347cSVladimir Kotal String token = null; 498d34347cSVladimir Kotal if ((token = RuntimeEnvironment.getInstance().getIndexerAuthenticationToken()) != null) { 508d34347cSVladimir Kotal headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + token); 518d34347cSVladimir Kotal } 528d34347cSVladimir Kotal 538d34347cSVladimir Kotal return headers; 548d34347cSVladimir Kotal } 558d34347cSVladimir Kotal 568d34347cSVladimir Kotal /** 575fc870e4SKryštof Tulinger * Enable projects in the remote host application. 585fc870e4SKryštof Tulinger * <p> 595fc870e4SKryštof Tulinger * NOTE: performs a check if the projects are already enabled, 605fc870e4SKryštof Tulinger * before making the change request 615fc870e4SKryštof Tulinger * 625fc870e4SKryštof Tulinger * @param host the url to the remote host 635fc870e4SKryštof Tulinger * @throws ResponseProcessingException in case processing of a received HTTP response fails 645fc870e4SKryštof Tulinger * @throws ProcessingException in case the request processing or subsequent I/O operation fails 655fc870e4SKryštof Tulinger * @throws WebApplicationException in case the response status code of the response returned by the server is not successful 665fc870e4SKryštof Tulinger */ enableProjects(final String host)675fc870e4SKryštof Tulinger public static void enableProjects(final String host) throws 685fc870e4SKryštof Tulinger ResponseProcessingException, 695fc870e4SKryštof Tulinger ProcessingException, 705fc870e4SKryštof Tulinger WebApplicationException { 715fc870e4SKryštof Tulinger final Invocation.Builder request = ClientBuilder.newClient() 721a2bb6b6SAdam Hornáček .target(host) 731a2bb6b6SAdam Hornáček .path("api") 741a2bb6b6SAdam Hornáček .path("v1") 751a2bb6b6SAdam Hornáček .path("configuration") 761a2bb6b6SAdam Hornáček .path("projectsEnabled") 778d34347cSVladimir Kotal .request() 78f1e34e10SVladimir Kotal .headers(getWebAppHeaders()); 795fc870e4SKryštof Tulinger final String enabled = request.get(String.class); 80*c6f0939bSAdam Hornacek if (!Boolean.parseBoolean(enabled)) { 815fc870e4SKryštof Tulinger final Response r = request.put(Entity.text(Boolean.TRUE.toString())); 825fc870e4SKryštof Tulinger if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { 835fc870e4SKryštof Tulinger throw new WebApplicationException(String.format("Unable to enable projects: %s", r.getStatusInfo().getReasonPhrase()), r.getStatus()); 841a2bb6b6SAdam Hornáček } 855fc870e4SKryštof Tulinger } 865fc870e4SKryštof Tulinger } 871a2bb6b6SAdam Hornáček } 88