Commit 0922b032 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

API endpoints refactoring

parent 69bd8b4e
......@@ -2,9 +2,11 @@ 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.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
public class AddMemberCall extends BaseCall<GmsClient> {
......@@ -14,15 +16,18 @@ public class AddMemberCall extends BaseCall<GmsClient> {
public boolean addMember(String completeGroupName, String userId) {
String endpoint = "ws/jwt/membership";
if (completeGroupName != null && !completeGroupName.isBlank()) {
endpoint += "/" + completeGroupName;
String endpoint = "membership";
if (completeGroupName == null) {
completeGroupName = "";
}
HttpRequest groupsRequest = client.newRequest(endpoint)
.header("Accept", "text/plain")
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(BodyPublishers.ofString("user_id=" + userId))
.POST(BodyPublishers.ofString(
"group=" + URLEncoder.encode(completeGroupName, StandardCharsets.UTF_8)
+ "&user_id=" + userId))
.build();
return client.call(groupsRequest, BodyHandlers.ofInputStream(), 200, res -> true);
......
......@@ -3,10 +3,12 @@ package it.inaf.ia2.gms.client.call;
import it.inaf.ia2.client.BaseCall;
import it.inaf.ia2.gms.client.GmsClient;
import it.inaf.ia2.gms.client.model.Permission;
import java.net.URLEncoder;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
public class AddPermissionCall extends BaseCall<GmsClient> {
......@@ -16,13 +18,15 @@ public class AddPermissionCall extends BaseCall<GmsClient> {
public String addPermission(String completeGroupName, String userId, Permission permission) {
String endpoint = "ws/jwt/permission";
if (completeGroupName != null && !completeGroupName.isBlank()) {
endpoint += "/" + completeGroupName;
String endpoint = "permission";
if (completeGroupName == null) {
completeGroupName = "";
}
BodyPublisher requestBody = BodyPublishers.ofString(
"user_id=" + userId + "&permission=" + permission);
"group=" + URLEncoder.encode(completeGroupName, StandardCharsets.UTF_8)
+ "&user_id=" + userId + "&permission=" + permission);
HttpRequest groupsRequest = client.newRequest(endpoint)
.header("Accept", "text/plain")
......
......@@ -2,8 +2,10 @@ 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;
public class DeleteGroupCall extends BaseCall<GmsClient> {
......@@ -13,7 +15,8 @@ public class DeleteGroupCall extends BaseCall<GmsClient> {
public boolean deleteGroup(String completeGroupName) {
HttpRequest groupsRequest = client.newRequest("ws/jwt/" + completeGroupName)
HttpRequest groupsRequest = client.newRequest("group?name="
+ URLEncoder.encode(completeGroupName, StandardCharsets.UTF_8))
.header("Accept", "text/plain")
.DELETE()
.build();
......
......@@ -4,8 +4,10 @@ import it.inaf.ia2.client.BaseCall;
import it.inaf.ia2.gms.client.GmsClient;
import it.inaf.ia2.gms.client.model.GroupPermission;
import it.inaf.ia2.gms.client.model.Permission;
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;
......@@ -16,12 +18,11 @@ public class GetGroupPermissionsCall extends BaseCall<GmsClient> {
super(client);
}
public List<GroupPermission> getGroupPermissions(String groupId) {
public List<GroupPermission> getGroupPermissions(String groupName) {
List<GroupPermission> groupPermissions = new ArrayList<>();
String endpoint = "ws/jwt/permission";
endpoint += "/" + groupId;
String endpoint = "permission?group=" + URLEncoder.encode(groupName, StandardCharsets.UTF_8);
HttpRequest groupsRequest = client.newRequest(endpoint)
.header("Accept", "text/plain")
......
......@@ -2,8 +2,10 @@ 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;
......@@ -48,15 +50,17 @@ public class GetUserGroupsCall extends BaseCall<GmsClient> {
});
}
public List<String> getUserGroups(String userId, String prefix) {
public List<String> getUserGroups(String userId, String groupName) {
List<String> groups = new ArrayList<>();
String endpoint = "ws/jwt/membership";
if (prefix != null && !prefix.isBlank()) {
endpoint += "/" + prefix;
if (groupName == null) {
groupName = "";
}
endpoint += "?user_id=" + userId;
String endpoint = "membership?group="
+ URLEncoder.encode(groupName, StandardCharsets.UTF_8)
+ "&user_id=" + userId;
HttpRequest groupsRequest = client.newRequest(endpoint)
.header("Accept", "text/plain")
......
......@@ -20,8 +20,7 @@ public class GetUserPermissionsCall extends BaseCall<GmsClient> {
List<UserPermission> userPermissions = new ArrayList<>();
String endpoint = "ws/jwt/permission";
endpoint += "?user_id=" + userId;
String endpoint = "permission?user_id=" + userId;
HttpRequest groupsRequest = client.newRequest(endpoint)
.header("Accept", "text/plain")
......
......@@ -2,8 +2,10 @@ 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;
......@@ -16,18 +18,20 @@ public class ListGroupsCall extends BaseCall<GmsClient> {
/**
* Returns the list of the groups in a given parent group (if the user has
* the privileges to see that information). The prefix is removed by the
* service.
* the privileges to see that information). The parent group name is removed
* by the service.
*/
public List<String> listGroups(String prefix, boolean recursive) {
public List<String> listGroups(String parentGroup, boolean recursive) {
List<String> groups = new ArrayList<>();
String uri = "list";
if (prefix != null && !prefix.isBlank()) {
uri += "/" + prefix;
if (parentGroup == null) {
parentGroup = "";
}
uri += "?recursive=" + recursive;
String uri = "groups?parent="
+ URLEncoder.encode(parentGroup, StandardCharsets.UTF_8)
+ "&recursive=" + recursive;
HttpRequest groupsRequest = client.newRequest(uri)
.header("Accept", "text/plain")
......
......@@ -2,8 +2,10 @@ 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;
public class RemoveMemberCall extends BaseCall<GmsClient> {
......@@ -13,11 +15,13 @@ public class RemoveMemberCall extends BaseCall<GmsClient> {
public boolean removeMember(String completeGroupName, String userId) {
String endpoint = "ws/jwt/membership";
if (completeGroupName != null && !completeGroupName.isBlank()) {
endpoint += "/" + completeGroupName;
if (completeGroupName == null) {
completeGroupName = "";
}
endpoint += "?user_id=" + userId;
String endpoint = "membership?group="
+ URLEncoder.encode(completeGroupName, StandardCharsets.UTF_8)
+ "&user_id=" + userId;
HttpRequest groupsRequest = client.newRequest(endpoint)
.header("Accept", "text/plain")
......
......@@ -2,8 +2,10 @@ 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;
public class RemovePermissionCall extends BaseCall<GmsClient> {
......@@ -13,11 +15,13 @@ public class RemovePermissionCall extends BaseCall<GmsClient> {
public boolean removePermission(String completeGroupName, String userId) {
String endpoint = "ws/jwt/permission";
if (completeGroupName != null && !completeGroupName.isBlank()) {
endpoint += "/" + completeGroupName;
if (completeGroupName == null) {
completeGroupName = "";
}
endpoint += "?user_id=" + userId;
String endpoint = "permission?group="
+ URLEncoder.encode(completeGroupName, StandardCharsets.UTF_8)
+ "&user_id=" + userId;
HttpRequest groupsRequest = client.newRequest(endpoint)
.header("Accept", "text/plain")
......
......@@ -3,8 +3,10 @@ package it.inaf.ia2.gms.client.call;
import it.inaf.ia2.client.BaseCall;
import it.inaf.ia2.gms.client.GmsClient;
import it.inaf.ia2.gms.client.model.Permission;
import java.net.URLEncoder;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
public class SetPermissionCall extends BaseCall<GmsClient> {
......@@ -14,15 +16,15 @@ public class SetPermissionCall extends BaseCall<GmsClient> {
public String setPermission(String completeGroupName, String userId, Permission permission) {
String endpoint = "ws/jwt/permission";
if (completeGroupName != null && !completeGroupName.isBlank()) {
endpoint += "/" + completeGroupName;
if (completeGroupName == null) {
completeGroupName = "";
}
HttpRequest.BodyPublisher requestBody = HttpRequest.BodyPublishers.ofString(
"user_id=" + userId + "&permission=" + permission);
"group=" + URLEncoder.encode(completeGroupName, StandardCharsets.UTF_8)
+ "&user_id=" + userId + "&permission=" + permission);
HttpRequest groupsRequest = client.newRequest(endpoint)
HttpRequest groupsRequest = client.newRequest("permission")
.header("Accept", "text/plain")
.header("Content-Type", "application/x-www-form-urlencoded")
.PUT(requestBody)
......
......@@ -28,6 +28,6 @@ public class AddMemberTest extends BaseGmsClientTest {
when(httpClient.sendAsync(any(), any())).thenReturn(response);
gmsClient.addMember("LBT.INAF", "user");
verify(httpClient, times(1)).sendAsync(endpointEq("POST", "ws/jwt/membership/LBT.INAF"), any());
verify(httpClient, times(1)).sendAsync(endpointEq("POST", "membership"), any());
}
}
......@@ -29,6 +29,6 @@ public class AddPermissionTest extends BaseGmsClientTest {
when(httpClient.sendAsync(any(), any())).thenReturn(response);
gmsClient.addPermission("LBT.INAF", "user", Permission.ADMIN);
verify(httpClient, times(1)).sendAsync(endpointEq("POST", "ws/jwt/permission/LBT.INAF"), any());
verify(httpClient, times(1)).sendAsync(endpointEq("POST", "permission"), any());
}
}
......@@ -28,6 +28,6 @@ public class CreateGroupTest extends BaseGmsClientTest {
when(httpClient.sendAsync(any(), any())).thenReturn(response);
gmsClient.createGroup("LBT.INAF", false);
verify(httpClient, times(1)).sendAsync(endpointEq("POST", "ws/jwt/LBT.INAF"), any());
verify(httpClient, times(1)).sendAsync(endpointEq("POST", "group"), any());
}
}
......@@ -28,6 +28,6 @@ public class DeleteGroupTest extends BaseGmsClientTest {
when(httpClient.sendAsync(any(), any())).thenReturn(response);
gmsClient.deleteGroup("LBT.INAF");
verify(httpClient, times(1)).sendAsync(endpointEq("DELETE", "ws/jwt/LBT.INAF"), any());
verify(httpClient, times(1)).sendAsync(endpointEq("DELETE", "group?name=LBT.INAF"), any());
}
}
......@@ -50,9 +50,9 @@ public class GetUserGroupsTest extends BaseGmsClientTest {
CompletableFuture response = CompletableFuture.completedFuture(getMockedStreamResponse(200, body));
when(httpClient.sendAsync(any(), any())).thenReturn(response);
List<String> groups = gmsClient.listGroups("LBT.", false);
List<String> groups = gmsClient.listGroups("LBT", false);
verify(httpClient, times(1)).sendAsync(endpointEq("GET", "list/LBT.?recursive=false"), any());
verify(httpClient, times(1)).sendAsync(endpointEq("GET", "groups?parent=LBT&recursive=false"), any());
assertEquals(2, groups.size());
assertEquals("INAF", groups.get(0));
......
......@@ -28,6 +28,6 @@ public class RemoveMemberTest extends BaseGmsClientTest {
when(httpClient.sendAsync(any(), any())).thenReturn(response);
gmsClient.removeMember("LBT.INAF", "user");
verify(httpClient, times(1)).sendAsync(endpointEq("DELETE", "ws/jwt/membership/LBT.INAF?user_id=user"), any());
verify(httpClient, times(1)).sendAsync(endpointEq("DELETE", "membership?group=LBT.INAF&user_id=user"), any());
}
}
......@@ -28,6 +28,6 @@ public class RemovePermissionTest extends BaseGmsClientTest {
when(httpClient.sendAsync(any(), any())).thenReturn(response);
gmsClient.removePermission("LBT.INAF", "user");
verify(httpClient, times(1)).sendAsync(endpointEq("DELETE", "ws/jwt/permission/LBT.INAF?user_id=user"), any());
verify(httpClient, times(1)).sendAsync(endpointEq("DELETE", "permission?group=LBT.INAF&user_id=user"), any());
}
}
......@@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.security.Principal;
import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
......@@ -146,20 +147,33 @@ public class GroupsController {
return groupsTreeBuilder.listSubGroups(parentGroup, request, servletRequest.getUserPrincipal().getName());
}
@GetMapping(value = "/groups", produces = MediaType.TEXT_PLAIN_VALUE)
public void listChildGroups(@RequestParam("parent") Optional<String> groupNames,
@RequestParam(value = "recursive", defaultValue = "false") boolean recursive,
Principal principal, HttpServletResponse response) throws IOException {
GroupEntity parentGroup = groupNameService.getGroupFromNames(groupNames);
List<GroupEntity> visibleSubgroups = groupsManager.getChildGroups(parentGroup, recursive);
try ( PrintWriter pw = new PrintWriter(response.getOutputStream())) {
for (String groupName : groupNameService.getGroupsNames(visibleSubgroups)) {
pw.println(groupNameService.getShortGroupName(groupName, groupNames));
}
}
}
/**
* Creates a group and its ancestors if they are missing. It doesn't fail if
* the last group already exists.
* the last group already exists. For CLI/API usage.
*/
@PostMapping(value = "/group", produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void createGroup(@RequestParam("name") String groupParam,
@RequestParam(value = "leaf", required = false, defaultValue = "false") boolean leaf,
HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpServletResponse response) throws IOException {
List<String> groupNames = groupNameService.extractGroupNames(groupParam);
String leafParam = request.getParameter("leaf");
//boolean leaf = leafParam == null ? false : Boolean.valueOf(leafParam);
GroupEntity group = groupsManager.getRoot();
for (int i = 0; i < groupNames.size(); i++) {
String name = groupNames.get(i);
......@@ -176,4 +190,11 @@ public class GroupsController {
pw.println(groupParam);
}
}
@DeleteMapping(value = "/group", produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<?> deleteGroup(@RequestParam("name") String groupParam) {
GroupEntity group = groupNameService.getGroupFromNames(Optional.of(groupParam));
groupsManager.deleteGroup(group.getId());
return ResponseEntity.noContent().build();
}
}
......@@ -200,13 +200,15 @@ public class JWTWebServiceController {
}
}
@Deprecated
@DeleteMapping(value = "/ws/jwt/{group:.+}", produces = MediaType.TEXT_PLAIN_VALUE)
public void deleteGroup(@PathVariable("group") String groupParam, HttpServletResponse response) {
GroupEntity group = groupNameService.getGroupFromNames(Optional.of(groupParam));
groupsDAO.deleteGroup(group);
groupsManager.deleteGroup(group.getId());
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
@Deprecated
@GetMapping(value = {"/ws/jwt/membership/{group:.+}", "/ws/jwt/membership"}, produces = MediaType.TEXT_PLAIN_VALUE)
public void getMembership(@PathVariable("group") Optional<String> groupNames, @RequestParam("user_id") String userId, HttpServletResponse response) throws IOException {
......@@ -221,6 +223,7 @@ public class JWTWebServiceController {
}
}
@Deprecated
@PostMapping(value = {"/ws/jwt/membership/{group:.+}", "/ws/jwt/membership"}, produces = MediaType.TEXT_PLAIN_VALUE)
public void addMember(@PathVariable("group") Optional<String> groupNames, HttpServletRequest request, HttpServletResponse response) throws IOException {
......@@ -235,6 +238,7 @@ public class JWTWebServiceController {
membershipManager.addMember(groupEntity, targetUserId);
}
@Deprecated
@DeleteMapping(value = {"/ws/jwt/membership/{group:.+}", "/ws/jwt/membership"}, produces = MediaType.TEXT_PLAIN_VALUE)
public void removeMember(@PathVariable("group") Optional<String> groupNames, @RequestParam("user_id") String userId,
HttpServletRequest request, HttpServletResponse response) throws IOException {
......@@ -245,6 +249,7 @@ public class JWTWebServiceController {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
@Deprecated
@GetMapping(value = {"/ws/jwt/permission/{group:.+}", "/ws/jwt/permission"}, produces = MediaType.TEXT_PLAIN_VALUE)
public void getUserPermission(@PathVariable("group") Optional<String> groupNames, @RequestParam("user_id") Optional<String> userId, HttpServletRequest request, HttpServletResponse response) throws IOException {
......@@ -265,18 +270,21 @@ public class JWTWebServiceController {
}
}
@Deprecated
@PostMapping(value = {"/ws/jwt/permission/{group:.+}", "/ws/jwt/permission/"}, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void addPermission(@PathVariable("group") Optional<String> groupNames, @RequestParam("user_id") String targetUserId, @RequestParam("permission") Permission permission) throws IOException {
GroupEntity groupEntity = groupNameService.getGroupFromNames(groupNames);
permissionsManager.addPermission(groupEntity, targetUserId, permission);
}
@Deprecated
@PutMapping(value = {"/ws/jwt/permission/{group:.+}", "/ws/jwt/permission/"}, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void setPermission(@PathVariable("group") Optional<String> groupNames, @RequestParam("user_id") String targetUserId, @RequestParam("permission") Permission permission) throws IOException {
GroupEntity groupEntity = groupNameService.getGroupFromNames(groupNames);
permissionsManager.createOrUpdatePermission(groupEntity, targetUserId, permission);
}
@Deprecated
@DeleteMapping(value = {"/ws/jwt/permission/{group:.+}", "/ws/jwt/permission/"}, produces = MediaType.TEXT_PLAIN_VALUE)
public void removePermission(@PathVariable("group") Optional<String> groupNames, @RequestParam("user_id") String userId,
HttpServletRequest request, HttpServletResponse response) throws IOException {
......
......@@ -8,10 +8,12 @@ import it.inaf.ia2.gms.model.request.PaginatedModelRequest;
import it.inaf.ia2.gms.model.request.RemoveMemberRequest;
import it.inaf.ia2.gms.model.request.TabRequest;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
import it.inaf.ia2.gms.service.GroupNameService;
import it.inaf.ia2.gms.service.GroupsService;
import it.inaf.ia2.rap.data.RapUser;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
......@@ -21,6 +23,7 @@ import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
......@@ -35,6 +38,9 @@ public class MembersController {
@Autowired
private PermissionsManager permissionsManager;
@Autowired
protected GroupNameService groupNameService;
@GetMapping(value = "/members", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<PaginatedData<RapUser>> getMembersTab(TabRequest request) {
......@@ -77,4 +83,22 @@ public class MembersController {
});
return new PaginatedData<>(members, request.getPaginatorPage(), request.getPaginatorPageSize());
}
@PostMapping(value = "/membership", produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void addMember(@RequestParam("group") Optional<String> groupNames,
@RequestParam("user_id") String targetUserId) {
GroupEntity groupEntity = groupNameService.getGroupFromNames(groupNames);
membershipManager.addMember(groupEntity, targetUserId);
}
@DeleteMapping(value = "/membership", produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<?> removeMember(@RequestParam("group") Optional<String> groupNames, @RequestParam("user_id") String userId) {
GroupEntity groupEntity = groupNameService.getGroupFromNames(groupNames);
membershipManager.removeMember(groupEntity, userId);
return ResponseEntity.noContent().build();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment