Use UriInfo and URIBuilder for generating URI for created responses
Former-commit-id: a72ec4e8d9
restapi
parent
407384a75a
commit
ae8e504eaa
|
|
@ -53,6 +53,9 @@ import org.libreccm.security.User;
|
||||||
|
|
||||||
import java.util.stream.Collectors;
|
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>
|
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||||
|
|
@ -61,6 +64,9 @@ import java.util.stream.Collectors;
|
||||||
@Path("/groups")
|
@Path("/groups")
|
||||||
public class GroupsApi {
|
public class GroupsApi {
|
||||||
|
|
||||||
|
@Context
|
||||||
|
private UriInfo uriInfo;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private GroupManager groupManager;
|
private GroupManager groupManager;
|
||||||
|
|
||||||
|
|
@ -139,11 +145,12 @@ public class GroupsApi {
|
||||||
groupRepository.save(group);
|
groupRepository.save(group);
|
||||||
|
|
||||||
return Response
|
return Response
|
||||||
.status(Response.Status.CREATED)
|
.created(
|
||||||
.contentLocation(
|
uriInfo
|
||||||
URI.create(String.format("/api/groups/%s", group.getName()))
|
.getRequestUriBuilder()
|
||||||
)
|
.path(group.getName())
|
||||||
.build();
|
.build()
|
||||||
|
).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PUT
|
@PUT
|
||||||
|
|
@ -209,13 +216,13 @@ public class GroupsApi {
|
||||||
@QueryParam("offset") @DefaultValue("0") final int offset
|
@QueryParam("offset") @DefaultValue("0") final int offset
|
||||||
) {
|
) {
|
||||||
final Group group = repository.findGroup(groupIdentifier);
|
final Group group = repository.findGroup(groupIdentifier);
|
||||||
|
|
||||||
return new ListView<>(
|
return new ListView<>(
|
||||||
groupRepository
|
groupRepository
|
||||||
.findGroupMemberships(group, limit, offset)
|
.findGroupMemberships(group, limit, offset)
|
||||||
.stream()
|
.stream()
|
||||||
.map(GroupUserMembership::new)
|
.map(GroupUserMembership::new)
|
||||||
.collect(Collectors.toList()),
|
.collect(Collectors.toList()),
|
||||||
groupRepository.countGroupMemberships(group),
|
groupRepository.countGroupMemberships(group),
|
||||||
limit,
|
limit,
|
||||||
offset
|
offset
|
||||||
|
|
|
||||||
|
|
@ -58,34 +58,34 @@ import javax.ws.rs.core.UriInfo;
|
||||||
@RequestScoped
|
@RequestScoped
|
||||||
@Path("/roles")
|
@Path("/roles")
|
||||||
public class RolesApi {
|
public class RolesApi {
|
||||||
|
|
||||||
@Context
|
@Context
|
||||||
private UriInfo uriInfo;
|
private UriInfo uriInfo;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private CcmObjectRepository ccmObjectRepository;
|
private CcmObjectRepository ccmObjectRepository;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private IdentifierParser identifierExtractor;
|
private IdentifierParser identifierExtractor;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private PartyRepository partyRepository;
|
private PartyRepository partyRepository;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private PermissionManager permissionManager;
|
private PermissionManager permissionManager;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private PermissionRepository permissionRepository;
|
private PermissionRepository permissionRepository;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private SecurityApiRepository repository;
|
private SecurityApiRepository repository;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private RoleManager roleManager;
|
private RoleManager roleManager;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private RoleRepository roleRepository;
|
private RoleRepository roleRepository;
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/")
|
@Path("/")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
|
@ -98,7 +98,7 @@ public class RolesApi {
|
||||||
) {
|
) {
|
||||||
final long count = roleRepository.countAll();
|
final long count = roleRepository.countAll();
|
||||||
final List<Role> roles = roleRepository.findAll(limit, offset);
|
final List<Role> roles = roleRepository.findAll(limit, offset);
|
||||||
|
|
||||||
return new ListView<>(
|
return new ListView<>(
|
||||||
roles.stream().map(RoleData::new).collect(Collectors.toList()),
|
roles.stream().map(RoleData::new).collect(Collectors.toList()),
|
||||||
count,
|
count,
|
||||||
|
|
@ -106,7 +106,7 @@ public class RolesApi {
|
||||||
offset
|
offset
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/{roleIdentifier}")
|
@Path("/{roleIdentifier}")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
|
@ -118,7 +118,7 @@ public class RolesApi {
|
||||||
) {
|
) {
|
||||||
return new RoleData(repository.findRole(roleIdentifier));
|
return new RoleData(repository.findRole(roleIdentifier));
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("/")
|
@Path("/")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
|
@ -129,14 +129,18 @@ public class RolesApi {
|
||||||
final Role role = new Role();
|
final Role role = new Role();
|
||||||
role.setName(roleData.getName());
|
role.setName(roleData.getName());
|
||||||
role.setDescription(roleData.getDescription());
|
role.setDescription(roleData.getDescription());
|
||||||
|
|
||||||
roleRepository.save(role);
|
roleRepository.save(role);
|
||||||
|
|
||||||
return Response.created(
|
return Response
|
||||||
URI.create(String.format("/api/admin/roles/%s", role.getName()))
|
.created(
|
||||||
).build();
|
uriInfo
|
||||||
|
.getBaseUriBuilder()
|
||||||
|
.path(role.getName())
|
||||||
|
.build()
|
||||||
|
).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PUT
|
@PUT
|
||||||
@Path("/{roleIdentifier}")
|
@Path("/{roleIdentifier}")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
|
@ -148,20 +152,20 @@ public class RolesApi {
|
||||||
final RoleData roleData
|
final RoleData roleData
|
||||||
) {
|
) {
|
||||||
final Role role = repository.findRole(roleIdentifier);
|
final Role role = repository.findRole(roleIdentifier);
|
||||||
|
|
||||||
if (roleData != null
|
if (roleData != null
|
||||||
&& roleData.getName() != null
|
&& roleData.getName() != null
|
||||||
&& !roleData.getName().equals(role.getName())) {
|
&& !roleData.getName().equals(role.getName())) {
|
||||||
role.setName(roleData.getName());
|
role.setName(roleData.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
roleRepository.save(role);
|
roleRepository.save(role);
|
||||||
|
|
||||||
return Response
|
return Response
|
||||||
.ok(String.format("Role %s updated succesfully.", roleIdentifier))
|
.ok(String.format("Role %s updated succesfully.", roleIdentifier))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@DELETE
|
@DELETE
|
||||||
@Path("/{roleIdentifier}")
|
@Path("/{roleIdentifier}")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
|
@ -177,7 +181,7 @@ public class RolesApi {
|
||||||
.ok(String.format("Role %s deleted successfully.", roleIdentifier))
|
.ok(String.format("Role %s deleted successfully.", roleIdentifier))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/{roleIdentifier}/members")
|
@Path("/{roleIdentifier}/members")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
|
@ -202,7 +206,7 @@ public class RolesApi {
|
||||||
offset
|
offset
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PUT
|
@PUT
|
||||||
@Path("/{roleIdentifier}/members/{partyIdentifier}")
|
@Path("/{roleIdentifier}/members/{partyIdentifier}")
|
||||||
@AuthorizationRequired
|
@AuthorizationRequired
|
||||||
|
|
@ -214,9 +218,9 @@ public class RolesApi {
|
||||||
) {
|
) {
|
||||||
final Role role = repository.findRole(groupIdentifier);
|
final Role role = repository.findRole(groupIdentifier);
|
||||||
final Party party = repository.findParty(partyIdentifier);
|
final Party party = repository.findParty(partyIdentifier);
|
||||||
|
|
||||||
roleManager.assignRoleToParty(role, party);
|
roleManager.assignRoleToParty(role, party);
|
||||||
|
|
||||||
return Response
|
return Response
|
||||||
.ok(
|
.ok(
|
||||||
String.format(
|
String.format(
|
||||||
|
|
@ -226,7 +230,7 @@ public class RolesApi {
|
||||||
)
|
)
|
||||||
).build();
|
).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@DELETE
|
@DELETE
|
||||||
@Path("/{roleIdentifier}/members/{partyIdentifier}")
|
@Path("/{roleIdentifier}/members/{partyIdentifier}")
|
||||||
@AuthorizationRequired
|
@AuthorizationRequired
|
||||||
|
|
@ -238,9 +242,9 @@ public class RolesApi {
|
||||||
) {
|
) {
|
||||||
final Role role = repository.findRole(groupIdentifier);
|
final Role role = repository.findRole(groupIdentifier);
|
||||||
final Party party = repository.findParty(partyIdentifier);
|
final Party party = repository.findParty(partyIdentifier);
|
||||||
|
|
||||||
roleManager.removeRoleFromParty(role, party);
|
roleManager.removeRoleFromParty(role, party);
|
||||||
|
|
||||||
return Response
|
return Response
|
||||||
.ok(
|
.ok(
|
||||||
String.format(
|
String.format(
|
||||||
|
|
@ -251,7 +255,7 @@ public class RolesApi {
|
||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/{roleIdentifier}/permissions")
|
@Path("/{roleIdentifier}/permissions")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
|
@ -275,7 +279,7 @@ public class RolesApi {
|
||||||
offset
|
offset
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("/{roleIdentifier}/permissions")
|
@Path("/{roleIdentifier}/permissions")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
|
@ -288,7 +292,7 @@ public class RolesApi {
|
||||||
) {
|
) {
|
||||||
final Role role = repository.findRole(roleIdentifier);
|
final Role role = repository.findRole(roleIdentifier);
|
||||||
final String privilege = permissionData.getGrantedPrivilege();
|
final String privilege = permissionData.getGrantedPrivilege();
|
||||||
|
|
||||||
final Permission permission;
|
final Permission permission;
|
||||||
if (permissionData.getObject() != null) {
|
if (permissionData.getObject() != null) {
|
||||||
final CcmObject object = ccmObjectRepository
|
final CcmObject object = ccmObjectRepository
|
||||||
|
|
@ -317,15 +321,15 @@ public class RolesApi {
|
||||||
permission = permissionManager.grantPrivilege(
|
permission = permissionManager.grantPrivilege(
|
||||||
privilege, role, object
|
privilege, role, object
|
||||||
);
|
);
|
||||||
return Response.created(
|
return Response
|
||||||
URI.create(
|
.created(
|
||||||
String.format(
|
uriInfo
|
||||||
"/api/admin/roles/%s/permissions/UUID-%s",
|
.getBaseUriBuilder()
|
||||||
role.getName(),
|
.path(role.getName())
|
||||||
permission.getUuid()
|
.path("permissions")
|
||||||
)
|
.path(String.format("UUID-%s", permission.getUuid()))
|
||||||
)
|
.build()
|
||||||
).build();
|
).build();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (permissionRepository.existsPermission(privilege, role)) {
|
if (permissionRepository.existsPermission(privilege, role)) {
|
||||||
|
|
@ -339,20 +343,20 @@ public class RolesApi {
|
||||||
).build();
|
).build();
|
||||||
} else {
|
} else {
|
||||||
permission = permissionManager.grantPrivilege(privilege, role);
|
permission = permissionManager.grantPrivilege(privilege, role);
|
||||||
|
|
||||||
return Response.created(
|
return Response
|
||||||
URI.create(
|
.created(
|
||||||
String.format(
|
uriInfo
|
||||||
"/api/admin/roles/%s/permissions/UUID-%s",
|
.getRequestUriBuilder()
|
||||||
role.getName(),
|
.path(role.getName())
|
||||||
permission.getUuid()
|
.path("permissions")
|
||||||
)
|
.path(String.format("UUID-%s", permission.getUuid()))
|
||||||
)
|
.build()
|
||||||
).build();
|
).build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@DELETE
|
@DELETE
|
||||||
@Path("/{roleIdentifier}/permissions/{permissionIdentifier}")
|
@Path("/{roleIdentifier}/permissions/{permissionIdentifier}")
|
||||||
@AuthorizationRequired
|
@AuthorizationRequired
|
||||||
|
|
@ -365,10 +369,10 @@ public class RolesApi {
|
||||||
final String permissionIdentifierParam
|
final String permissionIdentifierParam
|
||||||
) {
|
) {
|
||||||
final Role role = repository.findRole(roleIdentifier);
|
final Role role = repository.findRole(roleIdentifier);
|
||||||
|
|
||||||
final Identifier permissionIdentifier = identifierExtractor
|
final Identifier permissionIdentifier = identifierExtractor
|
||||||
.parseIdentifier(roleIdentifier);
|
.parseIdentifier(roleIdentifier);
|
||||||
|
|
||||||
final Permission permission;
|
final Permission permission;
|
||||||
switch (permissionIdentifier.getType()) {
|
switch (permissionIdentifier.getType()) {
|
||||||
case ID:
|
case ID:
|
||||||
|
|
@ -398,17 +402,17 @@ public class RolesApi {
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return Response
|
return Response
|
||||||
.status(Response.Status.BAD_REQUEST)
|
.status(Response.Status.BAD_REQUEST)
|
||||||
.entity("Permissions can only be identified by ID or UUID.")
|
.entity("Permissions can only be identified by ID or UUID.")
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
permissionRepository.delete(permission);
|
permissionRepository.delete(permission);
|
||||||
|
|
||||||
return Response.ok().build();
|
return Response.ok().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,9 @@ import org.libreccm.security.UserRepository;
|
||||||
|
|
||||||
import java.util.stream.Collectors;
|
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
|
* Provides RESTful API endpoints for managing users. Access to all endpoints
|
||||||
* defined by this class requires admin privileges.
|
* defined by this class requires admin privileges.
|
||||||
|
|
@ -65,6 +68,9 @@ import java.util.stream.Collectors;
|
||||||
@Path("/users")
|
@Path("/users")
|
||||||
public class UsersApi {
|
public class UsersApi {
|
||||||
|
|
||||||
|
@Context
|
||||||
|
private UriInfo uriInfo;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private GroupManager groupManager;
|
private GroupManager groupManager;
|
||||||
|
|
||||||
|
|
@ -221,12 +227,12 @@ public class UsersApi {
|
||||||
);
|
);
|
||||||
|
|
||||||
return Response
|
return Response
|
||||||
.status(Response.Status.CREATED)
|
.created(
|
||||||
.contentLocation(
|
uriInfo
|
||||||
URI.create(String.format("/api/users/%s", user.getName())
|
.getBaseUriBuilder()
|
||||||
)
|
.path(user.getName())
|
||||||
)
|
.build()
|
||||||
.build();
|
).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue