Commit 20c265d5 authored by Tuuli Veini's avatar Tuuli Veini

Non-working endpoints and attribute trimming

parent da3cb5cf
Pipeline #173997310 passed with stage
in 8 minutes and 32 seconds
......@@ -16,6 +16,7 @@ import com.unboundid.scim2.common.types.Member;
import com.unboundid.scim2.common.utils.ApiConstants;
import com.unboundid.scim2.common.utils.Parser;
import com.unboundid.scim2.server.annotations.ResourceType;
import com.unboundid.scim2.server.utils.ResourcePreparer;
import com.unboundid.scim2.server.utils.ResourceTypeDefinition;
import com.unboundid.scim2.server.utils.SchemaAwareFilterEvaluator;
import fi.jyu.vasara.scim.entity.ScimGroupEntity;
......@@ -34,6 +35,8 @@ import org.springframework.web.bind.annotation.*;
import javax.inject.Inject;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import java.net.URI;
import java.net.URISyntaxException;
......@@ -45,6 +48,8 @@ import static com.unboundid.scim2.common.utils.ApiConstants.MEDIA_TYPE_SCIM;
/**
* End point to access Group resources.
*
* 4.8.2020: Handling attributes in queries does not work
*/
@RestController
@ResourceType(description = "Access Group Resources", name = "Group", schema = GroupResource.class)
......@@ -61,19 +66,28 @@ public class ScimGroupController {
@Value("${scim2.baseUrl}")
private String baseUri;
@Context
private UriInfo uriInfo; // null
private static final ResourceTypeDefinition RESOURCE_TYPE_DEFINITION =
ResourceTypeDefinition.fromJaxRsResource(ScimGroupController.class);
/**
* Retrieve all groups with HTTP GET
* @param filterString filter (optional)
* @param attributes included attributes (optional)
* @param excludedAttributes excluded attributes (optional)
* @return all groups that match query parameters
* @throws ScimException if filtering fails
* @throws ScimException if filtering fails or requested attributes are invalid
* @throws URISyntaxException if URI is invalid
*/
@GetMapping(produces = {MEDIA_TYPE_SCIM, MediaType.APPLICATION_JSON_VALUE})
public ListResponse<ScimGroupEntity> retrieveAll(@RequestParam(value = ApiConstants.QUERY_PARAMETER_FILTER, required = false)
final String filterString) throws ScimException, URISyntaxException {
final String filterString,
@RequestParam(value = ApiConstants.QUERY_PARAMETER_ATTRIBUTES, required = false)
final String attributes,
@RequestParam(value = ApiConstants.QUERY_PARAMETER_EXCLUDED_ATTRIBUTES, required = false)
final String excludedAttributes) throws ScimException, URISyntaxException {
ScimSessionFactory scimSessionFactory = new ScimSessionFactory();
scimSessionFactory.initFromProcessEngineConfiguration(((ProcessEngineConfigurationImpl) processEngineConfiguration), "mappings.xml");
List<ScimGroupEntity> scimGroups = scimSessionFactory.getCommandExecutorTxRequired().execute(new Command<List<ScimGroupEntity>>() {
......@@ -88,6 +102,7 @@ public class ScimGroupController {
}
if (filterString != null) scimGroups = filter(scimGroups, filterString);
if (attributes != null || excludedAttributes != null) scimGroups = trimAll(scimGroups, attributes, excludedAttributes);
return new ListResponse<>(scimGroups.size(), scimGroups, 1, 20);
}
......@@ -95,14 +110,30 @@ public class ScimGroupController {
/**
* Retrieve group with HTTP GET
* @param groupId id of retrieved group
* @param attributes shown attributes (optional)
* @param excludedAttributes hidden attributes (optional)
* @return group
* @throws ResourceNotFoundException if no group with given id exists
* @throws URISyntaxException if URI is invalid
* @throws BadRequestException if request has invalid attributes
*/
@GetMapping(value = "/{id}", produces = {MEDIA_TYPE_SCIM, MediaType.APPLICATION_JSON_VALUE})
public ScimGroupEntity retrieve(@PathVariable("id") String groupId) throws ResourceNotFoundException, URISyntaxException {
public ScimGroupEntity retrieve(@PathVariable("id") String groupId,
@RequestParam(value = ApiConstants.QUERY_PARAMETER_ATTRIBUTES, required = false)
final String attributes,
@RequestParam(value = ApiConstants.QUERY_PARAMETER_EXCLUDED_ATTRIBUTES, required = false)
final String excludedAttributes) throws ResourceNotFoundException, URISyntaxException, BadRequestException {
ScimGroupEntity group = getGroup(groupId);
if (attributes != null || excludedAttributes != null) {
// final ResourcePreparer<GenericScimResource> resourcePreparer =
// new ResourcePreparer<>(ResourceTypeDefinition.fromJaxRsResource(this.getClass()),
// attributes, excludedAttributes, new URI(baseUri)); // package-private constructor
ResourcePreparer<GenericScimResource> resourcePreparer =
new ResourcePreparer<>(RESOURCE_TYPE_DEFINITION, uriInfo); // uriInfo is null; attributes from query not passed
group = trimAttributes(group, resourcePreparer);
}
return group;
}
......@@ -274,6 +305,49 @@ public class ScimGroupController {
return foundGroups;
}
/**
* Trim group's attributes based on query parameters
* @param group group
* @param resourcePreparer resourcepreparer containing information about resourcetype
* @return trimmed group
* @throws URISyntaxException if URI for any member is invalid
*/
public ScimGroupEntity trimAttributes(ScimGroupEntity group,
ResourcePreparer<GenericScimResource> resourcePreparer) throws URISyntaxException {
GenericScimResource genericGroup = toGenericResource(group);
genericGroup = resourcePreparer.trimRetrievedResource(genericGroup);
group = toGroupEntity(group, genericGroup);
return group;
}
/**
* Trim attributes of all groups based on query parameters
* @param groups list of groups
* @param attributes shown attributes
* @param excludedAttributes hidden attributes
* @return list of trimmed groups
* @throws BadRequestException if request has invalid attributes
* @throws URISyntaxException if URI is invalid
*/
public List<ScimGroupEntity> trimAll(List<ScimGroupEntity> groups,
String attributes,
String excludedAttributes) throws BadRequestException, URISyntaxException {
List<ScimGroupEntity> trimmedGroups = new ArrayList<>();
// final ResourcePreparer<GenericScimResource> resourcePreparer =
// new ResourcePreparer<>(ResourceTypeDefinition.fromJaxRsResource(this.getClass()),
// attributes, excludedAttributes, new URI(baseUri)); // package-private constructor
ResourcePreparer<GenericScimResource> resourcePreparer =
new ResourcePreparer<>(RESOURCE_TYPE_DEFINITION, uriInfo); // uriInfo is null; attributes from query not passed
for (ScimGroupEntity groupEntity : groups) {
groupEntity = trimAttributes(groupEntity, resourcePreparer);
trimmedGroups.add(groupEntity);
}
return trimmedGroups;
}
/**
* Update group's information in database
* @param group updated group
......
package fi.jyu.vasara.scim.controller;
import com.unboundid.scim2.common.GenericScimResource;
import com.unboundid.scim2.common.exceptions.ForbiddenException;
import com.unboundid.scim2.common.exceptions.ResourceNotFoundException;
import com.unboundid.scim2.common.messages.ListResponse;
import com.unboundid.scim2.common.types.ResourceTypeResource;
import com.unboundid.scim2.common.utils.ApiConstants;
import com.unboundid.scim2.server.annotations.ResourceType;
import com.unboundid.scim2.server.utils.ResourceTypeDefinition;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import java.util.*;
import static com.unboundid.scim2.common.utils.ApiConstants.MEDIA_TYPE_SCIM;
/**
* End point to access resource types.
*
* 4.8.2020: Does not work because of lacking JAX-RS configuration
*/
@RestController
@ResourceType(description = "SCIM 2.0 Resource Type", name = "ResourceType", schema = ResourceTypeResource.class, discoverable = false)
@RequestMapping("/scim/ResourceTypes")
public class ScimResourceTypesController {
@Context
private Application application; // null
/**
* Get all resource types
* @return list of resource types
*/
public Collection<ResourceTypeResource> getResourceTypes() {
Set<ResourceTypeResource> resourceTypes = new HashSet<>();
for (Class<?> resourceClass : application.getClasses()) { // NPE
ResourceTypeDefinition resourceTypeDefinition = ResourceTypeDefinition.fromJaxRsResource(resourceClass);
if (resourceTypeDefinition != null && resourceTypeDefinition.isDiscoverable()) {
resourceTypes.add(resourceTypeDefinition.toScimResource());
}
}
for (Object resourceInstance : application.getSingletons()) {
ResourceTypeDefinition resourceTypeDefinition = ResourceTypeDefinition.fromJaxRsResource(resourceInstance.getClass());
if (resourceTypeDefinition != null && resourceTypeDefinition.isDiscoverable()) {
resourceTypes.add(resourceTypeDefinition.toScimResource());
}
}
return resourceTypes;
}
/**
* Retrieve all resource types with GET
* @param filterString filter
* @return list of resource types
* @throws ForbiddenException if filter is provided
*/
@GetMapping(produces = {MEDIA_TYPE_SCIM, MediaType.APPLICATION_JSON_VALUE})
public ListResponse<GenericScimResource> retrieveAll(@RequestParam(value = ApiConstants.QUERY_PARAMETER_FILTER, required = false)
final String filterString) throws ForbiddenException {
if (filterString != null) {
throw new ForbiddenException("Filtering not allowed");
}
Collection<GenericScimResource> resources = new ArrayList<>();
for (ResourceTypeResource resource : getResourceTypes()) {
resources.add(resource.asGenericScimResource());
}
return new ListResponse<>(resources);
}
/**
* Retrieve a single resource type with GET
* @param id resource type id
* @return retrieved resource type
* @throws ResourceNotFoundException if no resource type with given id exists
*/
@GetMapping(value = "/{id}", produces = {MEDIA_TYPE_SCIM, MediaType.APPLICATION_JSON_VALUE})
public GenericScimResource retrieve(@PathVariable("id") String id) throws ResourceNotFoundException {
ResourceTypeResource resourceTypeResource = null;
for (ResourceTypeResource resource : getResourceTypes()) {
if (resource.getName().equals(id)) {
resourceTypeResource = resource;
break;
}
}
if (resourceTypeResource == null) throw new ResourceNotFoundException(id);
return resourceTypeResource.asGenericScimResource();
}
}
package fi.jyu.vasara.scim.controller;
import com.unboundid.scim2.common.GenericScimResource;
import com.unboundid.scim2.common.exceptions.ForbiddenException;
import com.unboundid.scim2.common.exceptions.ResourceNotFoundException;
import com.unboundid.scim2.common.messages.ListResponse;
import com.unboundid.scim2.common.types.SchemaResource;
import com.unboundid.scim2.common.utils.ApiConstants;
import com.unboundid.scim2.server.annotations.ResourceType;
import com.unboundid.scim2.server.utils.ResourceTypeDefinition;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import java.util.*;
import static com.unboundid.scim2.common.utils.ApiConstants.MEDIA_TYPE_SCIM;
/**
* End point to access schemas.
*
* 4.8.2020: Does not work because of lacking JAX-RS configuration
*/
@ResourceType(description = "SCIM 2.0 Schema", name = "Schema", schema = SchemaResource.class, discoverable = false)
@RestController
@RequestMapping("/scim/Schemas")
public class ScimSchemasController {
@Context
private Application application; // null
/**
* Get all schemas
* @return list of schemas
*/
public Collection<SchemaResource> getSchemas() {
Set<SchemaResource> schemas = new HashSet<>();
for (Class<?> resourceClass : application.getClasses()) { // NPE
ResourceTypeDefinition resourceTypeDefinition = ResourceTypeDefinition.fromJaxRsResource(resourceClass);
if (resourceTypeDefinition != null && resourceTypeDefinition.isDiscoverable()) {
if (resourceTypeDefinition.getCoreSchema() != null) {
schemas.add(resourceTypeDefinition.getCoreSchema());
}
schemas.addAll(resourceTypeDefinition.getSchemaExtensions().keySet());
}
}
for (Object resourceInstance : application.getSingletons()) {
ResourceTypeDefinition resourceTypeDefinition = ResourceTypeDefinition.fromJaxRsResource(resourceInstance.getClass());
if (resourceTypeDefinition != null && resourceTypeDefinition.isDiscoverable()) {
if (resourceTypeDefinition.getCoreSchema() != null) {
schemas.add(resourceTypeDefinition.getCoreSchema());
}
schemas.addAll(resourceTypeDefinition.getSchemaExtensions().keySet());
}
}
return schemas;
}
/**
* Retrieve all schemas with GET
* @param filterString filter
* @return list of schemas
* @throws ForbiddenException if filter is provided
*/
@GetMapping(produces = {MEDIA_TYPE_SCIM, MediaType.APPLICATION_JSON_VALUE})
public ListResponse<GenericScimResource> retrieveAll(@RequestParam(value = ApiConstants.QUERY_PARAMETER_FILTER, required = false)
final String filterString) throws ForbiddenException {
if (filterString != null) {
throw new ForbiddenException("Filtering not allowed");
}
Collection<SchemaResource> schemas = getSchemas();
List<GenericScimResource> preparedSchemas = new ArrayList<>();
for (SchemaResource resource : schemas) {
preparedSchemas.add(resource.asGenericScimResource());
}
return new ListResponse<>(preparedSchemas);
}
/**
* Retrieve a single schema with GET
* @param id schema id
* @return schema with given id
* @throws ResourceNotFoundException if no schema with given id exists
*/
@GetMapping(value = "/{id}", produces = {MEDIA_TYPE_SCIM, MediaType.APPLICATION_JSON_VALUE})
public GenericScimResource retrieve(@PathVariable("id") String id) throws ResourceNotFoundException {
for (SchemaResource resource : getSchemas()) {
if (resource.getId().equals(id)) {
return resource.asGenericScimResource();
}
}
throw new ResourceNotFoundException(id);
}
}
......@@ -14,6 +14,7 @@ import com.unboundid.scim2.common.types.*;
import com.unboundid.scim2.common.utils.ApiConstants;
import com.unboundid.scim2.common.utils.Parser;
import com.unboundid.scim2.server.annotations.ResourceType;
import com.unboundid.scim2.server.utils.ResourcePreparer;
import com.unboundid.scim2.server.utils.ResourceTypeDefinition;
import com.unboundid.scim2.server.utils.SchemaAwareFilterEvaluator;
import fi.jyu.vasara.scim.ScimSessionFactory;
......@@ -31,6 +32,8 @@ import org.springframework.web.bind.annotation.*;
import javax.inject.Inject;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
......@@ -39,6 +42,8 @@ import static com.unboundid.scim2.common.utils.ApiConstants.MEDIA_TYPE_SCIM;
/**
* End point to access User resources.
*
* 4.8.2020: Handling attributes in queries does not work
*/
@RestController
@ResourceType(description = "Access User Resources", name = "User", schema = UserResource.class, optionalSchemaExtensions = EnterpriseUserExtension.class)
......@@ -55,19 +60,28 @@ public class ScimUserController {
@Value("${scim2.baseUrl}")
private String baseUri;
@Context
private UriInfo uriInfo; // null
private static final ResourceTypeDefinition RESOURCE_TYPE_DEFINITION =
ResourceTypeDefinition.fromJaxRsResource(ScimGroupController.class);
ResourceTypeDefinition.fromJaxRsResource(ScimUserController.class);
/**
* Retrieve all users with HTTP GET
* @param filterString filter (optional)
* @param attributes shown attributes (optional)
* @param excludedAttributes hidden attributes (optional)
* @return all users that match query parameters
* @throws ScimException if filtering fails
* @throws ScimException if filtering fails or requested attributes are invalid
* @throws URISyntaxException if URI is invalid
*/
@GetMapping(produces = {MEDIA_TYPE_SCIM, MediaType.APPLICATION_JSON_VALUE})
public ListResponse<ScimUserEntity> retrieveAll(@RequestParam(value = ApiConstants.QUERY_PARAMETER_FILTER, required = false)
final String filterString) throws ScimException, URISyntaxException {
final String filterString,
@RequestParam(value = ApiConstants.QUERY_PARAMETER_ATTRIBUTES, required = false)
final String attributes,
@RequestParam(value = ApiConstants.QUERY_PARAMETER_EXCLUDED_ATTRIBUTES, required = false)
final String excludedAttributes) throws ScimException, URISyntaxException {
ScimSessionFactory scimSessionFactory = new ScimSessionFactory();
scimSessionFactory.initFromProcessEngineConfiguration(((ProcessEngineConfigurationImpl) processEngineConfiguration), "mappings.xml");
List<ScimUserEntity> scimUsers = scimSessionFactory.getCommandExecutorTxRequired().execute(new Command<List<ScimUserEntity>>() {
......@@ -82,6 +96,7 @@ public class ScimUserController {
}
if (filterString != null) scimUsers = filter(scimUsers, filterString);
if (attributes != null || excludedAttributes != null) scimUsers = trimAll(scimUsers, attributes, excludedAttributes);
return new ListResponse<>(scimUsers.size(), scimUsers, 1, 20);
}
......@@ -89,14 +104,30 @@ public class ScimUserController {
/**
* Retrieve user with HTTP GET
* @param userId id of retrieved user
* @param attributes shown attributes (optional)
* @param excludedAttributes hidden attributes (optional)
* @return user
* @throws ResourceNotFoundException if no user with given id exists
* @throws URISyntaxException if URI is invalid
* @throws BadRequestException if request has invalid attributes
*/
@GetMapping(value = "/{id}", produces = {MEDIA_TYPE_SCIM, MediaType.APPLICATION_JSON_VALUE})
public ScimUserEntity retrieve(@PathVariable("id") String userId) throws ResourceNotFoundException, URISyntaxException {
public ScimUserEntity retrieve(@PathVariable("id") String userId,
@RequestParam(value = ApiConstants.QUERY_PARAMETER_ATTRIBUTES, required = false)
final String attributes,
@RequestParam(value = ApiConstants.QUERY_PARAMETER_EXCLUDED_ATTRIBUTES, required = false)
final String excludedAttributes) throws ResourceNotFoundException, URISyntaxException, BadRequestException {
ScimUserEntity scimUser = getUser(userId);
if (attributes != null || excludedAttributes != null) {
// final ResourcePreparer<GenericScimResource> resourcePreparer =
// new ResourcePreparer<>(ResourceTypeDefinition.fromJaxRsResource(this.getClass()),
// attributes, excludedAttributes, new URI(baseUri)); // package-private constructor
ResourcePreparer<GenericScimResource> resourcePreparer =
new ResourcePreparer<>(RESOURCE_TYPE_DEFINITION, uriInfo); // uriInfo is null; attributes from query not passed
scimUser = trimAttributes(scimUser, resourcePreparer);
}
return scimUser;
}
......@@ -283,6 +314,49 @@ public class ScimUserController {
return foundUsers;
}
/**
* Trim user's attributes based on query parameters
* @param user trimmed user
* @param resourcePreparer resourcepreparer containing information about resourcetype
* @return trimmed user
* @throws URISyntaxException if URI for any group is invalid
*/
public ScimUserEntity trimAttributes(ScimUserEntity user,
ResourcePreparer<GenericScimResource> resourcePreparer) throws URISyntaxException {
GenericScimResource genericUser = toUserResource(user).asGenericScimResource();
genericUser = resourcePreparer.trimRetrievedResource(genericUser);
user = toUserEntity(user, genericUser);
return user;
}
/**
* Trim attributes of all users based on query parameters
* @param users list of users
* @param attributes included attributes
* @param excludedAttributes excluded attributes
* @return list of users with trimmed attributes
* @throws BadRequestException if request has invalid attributes
* @throws URISyntaxException if URI is invalid
*/
public List<ScimUserEntity> trimAll(List<ScimUserEntity> users,
String attributes,
String excludedAttributes) throws BadRequestException, URISyntaxException {
List<ScimUserEntity> trimmedUsers = new ArrayList<>();
// final ResourcePreparer<GenericScimResource> resourcePreparer =
// new ResourcePreparer<>(ResourceTypeDefinition.fromJaxRsResource(this.getClass()),
// attributes, excludedAttributes, new URI(baseUri)); // package-private constructor
ResourcePreparer<GenericScimResource> resourcePreparer =
new ResourcePreparer<>(RESOURCE_TYPE_DEFINITION, uriInfo); // uriInfo is null; attributes from query not passed
for (ScimUserEntity userEntity : users) {
userEntity = trimAttributes(userEntity, resourcePreparer);
trimmedUsers.add(userEntity);
}
return trimmedUsers;
}
/**
* Updates user's attributes in database
* @param user updated user
......
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