/* * This file is part of gms-client * Copyright (C) 2021 Istituto Nazionale di Astrofisica * SPDX-License-Identifier: GPL-3.0-or-later */ package it.inaf.ia2.gms.client.call; import it.inaf.ia2.client.BaseCall; import it.inaf.ia2.gms.client.GmsClient; import java.net.URLEncoder; import java.net.http.HttpRequest; import java.net.http.HttpResponse.BodyHandlers; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class ListGroupsCall extends BaseCall { public ListGroupsCall(GmsClient client) { super(client); } /** * Returns the list of the groups in a given parent group (if the user has * the privileges to see that information). The parent group name is removed * by the service. */ public List listGroups(String parentGroup, boolean recursive) { List groups = new ArrayList<>(); if (parentGroup == null) { parentGroup = ""; } String uri = "groups?parent=" + URLEncoder.encode(parentGroup, StandardCharsets.UTF_8) + "&recursive=" + recursive; HttpRequest groupsRequest = client.newRequest(uri) .header("Accept", "text/plain") .GET() .build(); return client.call(groupsRequest, BodyHandlers.ofInputStream(), 200, inputStream -> { try ( Scanner scan = new Scanner(inputStream)) { while (scan.hasNextLine()) { String line = scan.nextLine(); if (!line.isEmpty()) { groups.add(line); } } } return groups; }); } }