Use UriInfo and URIBuilder for generating URI for created responses

Jens Pelzetter 2020-06-06 16:53:37 +02:00
parent 10da2f6ef6
commit a72ec4e8d9
3 changed files with 92 additions and 75 deletions

View File

@ -53,6 +53,9 @@ import org.libreccm.security.User;
import java.util.stream.Collectors;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
@ -61,6 +64,9 @@ import java.util.stream.Collectors;
@Path("/groups")
public class GroupsApi {
@Context
private UriInfo uriInfo;
@Inject
private GroupManager groupManager;
@ -139,11 +145,12 @@ public class GroupsApi {
groupRepository.save(group);
return Response
.status(Response.Status.CREATED)
.contentLocation(
URI.create(String.format("/api/groups/%s", group.getName()))
)
.build();
.created(
uriInfo
.getRequestUriBuilder()
.path(group.getName())
.build()
).build();
}
@PUT
@ -209,13 +216,13 @@ public class GroupsApi {
@QueryParam("offset") @DefaultValue("0") final int offset
) {
final Group group = repository.findGroup(groupIdentifier);
return new ListView<>(
groupRepository
.findGroupMemberships(group, limit, offset)
.stream()
.map(GroupUserMembership::new)
.collect(Collectors.toList()),
.findGroupMemberships(group, limit, offset)
.stream()
.map(GroupUserMembership::new)
.collect(Collectors.toList()),
groupRepository.countGroupMemberships(group),
limit,
offset

View File

@ -58,34 +58,34 @@ import javax.ws.rs.core.UriInfo;
@RequestScoped
@Path("/roles")
public class RolesApi {
@Context
private UriInfo uriInfo;
@Inject
private CcmObjectRepository ccmObjectRepository;
@Inject
private IdentifierParser identifierExtractor;
@Inject
private PartyRepository partyRepository;
@Inject
private PermissionManager permissionManager;
@Inject
private PermissionRepository permissionRepository;
@Inject
private SecurityApiRepository repository;
@Inject
private RoleManager roleManager;
@Inject
private RoleRepository roleRepository;
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@ -98,7 +98,7 @@ public class RolesApi {
) {
final long count = roleRepository.countAll();
final List<Role> roles = roleRepository.findAll(limit, offset);
return new ListView<>(
roles.stream().map(RoleData::new).collect(Collectors.toList()),
count,
@ -106,7 +106,7 @@ public class RolesApi {
offset
);
}
@GET
@Path("/{roleIdentifier}")
@Produces(MediaType.APPLICATION_JSON)
@ -118,7 +118,7 @@ public class RolesApi {
) {
return new RoleData(repository.findRole(roleIdentifier));
}
@POST
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@ -129,14 +129,18 @@ public class RolesApi {
final Role role = new Role();
role.setName(roleData.getName());
role.setDescription(roleData.getDescription());
roleRepository.save(role);
return Response.created(
URI.create(String.format("/api/admin/roles/%s", role.getName()))
).build();
return Response
.created(
uriInfo
.getBaseUriBuilder()
.path(role.getName())
.build()
).build();
}
@PUT
@Path("/{roleIdentifier}")
@Consumes(MediaType.APPLICATION_JSON)
@ -148,20 +152,20 @@ public class RolesApi {
final RoleData roleData
) {
final Role role = repository.findRole(roleIdentifier);
if (roleData != null
&& roleData.getName() != null
&& !roleData.getName().equals(role.getName())) {
role.setName(roleData.getName());
}
roleRepository.save(role);
return Response
.ok(String.format("Role %s updated succesfully.", roleIdentifier))
.build();
}
@DELETE
@Path("/{roleIdentifier}")
@Consumes(MediaType.APPLICATION_JSON)
@ -177,7 +181,7 @@ public class RolesApi {
.ok(String.format("Role %s deleted successfully.", roleIdentifier))
.build();
}
@GET
@Path("/{roleIdentifier}/members")
@Produces(MediaType.APPLICATION_JSON)
@ -202,7 +206,7 @@ public class RolesApi {
offset
);
}
@PUT
@Path("/{roleIdentifier}/members/{partyIdentifier}")
@AuthorizationRequired
@ -214,9 +218,9 @@ public class RolesApi {
) {
final Role role = repository.findRole(groupIdentifier);
final Party party = repository.findParty(partyIdentifier);
roleManager.assignRoleToParty(role, party);
return Response
.ok(
String.format(
@ -226,7 +230,7 @@ public class RolesApi {
)
).build();
}
@DELETE
@Path("/{roleIdentifier}/members/{partyIdentifier}")
@AuthorizationRequired
@ -238,9 +242,9 @@ public class RolesApi {
) {
final Role role = repository.findRole(groupIdentifier);
final Party party = repository.findParty(partyIdentifier);
roleManager.removeRoleFromParty(role, party);
return Response
.ok(
String.format(
@ -251,7 +255,7 @@ public class RolesApi {
)
.build();
}
@GET
@Path("/{roleIdentifier}/permissions")
@Produces(MediaType.APPLICATION_JSON)
@ -275,7 +279,7 @@ public class RolesApi {
offset
);
}
@POST
@Path("/{roleIdentifier}/permissions")
@Consumes(MediaType.APPLICATION_JSON)
@ -288,7 +292,7 @@ public class RolesApi {
) {
final Role role = repository.findRole(roleIdentifier);
final String privilege = permissionData.getGrantedPrivilege();
final Permission permission;
if (permissionData.getObject() != null) {
final CcmObject object = ccmObjectRepository
@ -317,15 +321,15 @@ public class RolesApi {
permission = permissionManager.grantPrivilege(
privilege, role, object
);
return Response.created(
URI.create(
String.format(
"/api/admin/roles/%s/permissions/UUID-%s",
role.getName(),
permission.getUuid()
)
)
).build();
return Response
.created(
uriInfo
.getBaseUriBuilder()
.path(role.getName())
.path("permissions")
.path(String.format("UUID-%s", permission.getUuid()))
.build()
).build();
}
} else {
if (permissionRepository.existsPermission(privilege, role)) {
@ -339,20 +343,20 @@ public class RolesApi {
).build();
} else {
permission = permissionManager.grantPrivilege(privilege, role);
return Response.created(
URI.create(
String.format(
"/api/admin/roles/%s/permissions/UUID-%s",
role.getName(),
permission.getUuid()
)
)
).build();
return Response
.created(
uriInfo
.getRequestUriBuilder()
.path(role.getName())
.path("permissions")
.path(String.format("UUID-%s", permission.getUuid()))
.build()
).build();
}
}
}
@DELETE
@Path("/{roleIdentifier}/permissions/{permissionIdentifier}")
@AuthorizationRequired
@ -365,10 +369,10 @@ public class RolesApi {
final String permissionIdentifierParam
) {
final Role role = repository.findRole(roleIdentifier);
final Identifier permissionIdentifier = identifierExtractor
.parseIdentifier(roleIdentifier);
final Permission permission;
switch (permissionIdentifier.getType()) {
case ID:
@ -398,17 +402,17 @@ public class RolesApi {
)
);
break;
default:
return Response
.status(Response.Status.BAD_REQUEST)
.entity("Permissions can only be identified by ID or UUID.")
.build();
}
permissionRepository.delete(permission);
return Response.ok().build();
}
}

View File

@ -55,6 +55,9 @@ import org.libreccm.security.UserRepository;
import java.util.stream.Collectors;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
/**
* Provides RESTful API endpoints for managing users. Access to all endpoints
* defined by this class requires admin privileges.
@ -65,6 +68,9 @@ import java.util.stream.Collectors;
@Path("/users")
public class UsersApi {
@Context
private UriInfo uriInfo;
@Inject
private GroupManager groupManager;
@ -221,12 +227,12 @@ public class UsersApi {
);
return Response
.status(Response.Status.CREATED)
.contentLocation(
URI.create(String.format("/api/users/%s", user.getName())
)
)
.build();
.created(
uriInfo
.getBaseUriBuilder()
.path(user.getName())
.build()
).build();
}
/**