package it.inaf.ia2.gms.client; import it.inaf.ia2.gms.client.model.Group; import it.inaf.ia2.gms.client.model.Member; import it.inaf.ia2.gms.client.model.Permission; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; public class GmsClient { private static final String WS = "ws"; private final String baseUrl; private final String authHeader; private RestTemplate restTemplate; public GmsClient(String baseUrl, String clientId, String clientSecret) { this.baseUrl = baseUrl; String auth = clientId + ":" + clientSecret; byte[] encodedAuth = Base64.getEncoder().encode( auth.getBytes(StandardCharsets.UTF_8)); authHeader = "Basic " + new String(encodedAuth); restTemplate = new RestTemplate(); } /** * For testing purpose. */ protected void setRestTemplate(RestTemplate restTemplate) { this.restTemplate = restTemplate; } public Group addGroup(List names) { String url = UriComponentsBuilder.fromHttpUrl(baseUrl) .pathSegment(WS, "group") .toUriString(); HttpEntity> httpEntity = getEntity(names); return restTemplate.exchange(url, HttpMethod.POST, httpEntity, Group.class).getBody(); } public void removeGroup(List names) { String url = UriComponentsBuilder.fromHttpUrl(baseUrl) .pathSegment(WS, "group") .queryParam("names", names.toArray()) .toUriString(); restTemplate.exchange(url, HttpMethod.DELETE, getEntity(), Void.class); } public Member addMember(List names, String userId) { String url = UriComponentsBuilder.fromHttpUrl(baseUrl) .pathSegment(WS, "member") .toUriString(); Map params = new HashMap<>(); params.put("names", names); params.put("userId", userId); HttpEntity> httpEntity = getEntity(params); return restTemplate.exchange(url, HttpMethod.POST, httpEntity, Member.class).getBody(); } public void removeMember(List names, String userId) { String url = UriComponentsBuilder.fromHttpUrl(baseUrl) .pathSegment(WS, "member") .queryParam("names", names.toArray()) .queryParam("userId", userId) .toUriString(); restTemplate.exchange(url, HttpMethod.DELETE, getEntity(), Void.class); } public Permission addPermission(List names, String userId, String permission) { String url = UriComponentsBuilder.fromHttpUrl(baseUrl) .pathSegment(WS, "permission") .toUriString(); Map params = new HashMap<>(); params.put("names", names); params.put("userId", userId); params.put("permission", permission); HttpEntity> httpEntity = getEntity(params); return restTemplate.exchange(url, HttpMethod.POST, httpEntity, Permission.class).getBody(); } public void removePermission(List names, String userId) { String url = UriComponentsBuilder.fromHttpUrl(baseUrl) .pathSegment(WS, "permission") .queryParam("names", names.toArray()) .queryParam("userId", userId) .toUriString(); restTemplate.exchange(url, HttpMethod.DELETE, getEntity(), Void.class); } public void prepareToJoin(String fromUserId, String toUserId) { String url = UriComponentsBuilder.fromHttpUrl(baseUrl) .pathSegment(WS, "prepare-join") .toUriString(); Map params = new HashMap<>(); params.put("fromUserId", fromUserId); params.put("toUserId", toUserId); HttpEntity> httpEntity = getEntity(params); restTemplate.exchange(url, HttpMethod.POST, httpEntity, Void.class); } private HttpEntity getEntity() { return new HttpEntity<>(getHeaders()); } private HttpEntity getEntity(T body) { return new HttpEntity<>(body, getHeaders()); } private HttpHeaders getHeaders() { return new HttpHeaders() { { set("Authorization", authHeader); } }; } }