Search in sources :

Example 1 with PatchRequest

use of io.jans.scim.model.scim2.patch.PatchRequest in project jans by JanssenProject.

the class BulkWebService method execute.

private Pair<Response, String> execute(Verb verb, BaseScimWebService ws, String data, String fragment) {
    Response response = null;
    String idCreated = null;
    try {
        if (ws == userWS)
            switch(verb) {
                case PUT:
                    UserResource user = mapper.readValue(data, UserResource.class);
                    response = userWS.updateUser(user, fragment, "id", null);
                    break;
                case DELETE:
                    response = userWS.deleteUser(fragment);
                    break;
                case PATCH:
                    PatchRequest pr = mapper.readValue(data, PatchRequest.class);
                    response = userWS.patchUser(pr, fragment, "id", null);
                    break;
                case POST:
                    user = mapper.readValue(data, UserResource.class);
                    response = userWS.createUser(user, "id", null);
                    if (CREATED.getStatusCode() == response.getStatus()) {
                        user = mapper.readValue(response.getEntity().toString(), UserResource.class);
                        idCreated = user.getId();
                    }
                    break;
            }
        else if (ws == groupWS)
            switch(verb) {
                case PUT:
                    GroupResource group = mapper.readValue(data, GroupResource.class);
                    response = groupWS.updateGroup(group, fragment, "id", null);
                    break;
                case DELETE:
                    response = groupWS.deleteGroup(fragment);
                    break;
                case PATCH:
                    PatchRequest pr = mapper.readValue(data, PatchRequest.class);
                    response = groupWS.patchGroup(pr, fragment, "id", null);
                    break;
                case POST:
                    group = mapper.readValue(data, GroupResource.class);
                    response = groupWS.createGroup(group, "id", null);
                    if (CREATED.getStatusCode() == response.getStatus()) {
                        group = mapper.readValue(response.getEntity().toString(), GroupResource.class);
                        idCreated = group.getId();
                    }
                    break;
            }
        else if (ws == fidoDeviceWS)
            switch(verb) {
                case PUT:
                    FidoDeviceResource dev = mapper.readValue(data, FidoDeviceResource.class);
                    response = fidoDeviceWS.updateDevice(dev, fragment, "id", null);
                    break;
                case DELETE:
                    response = fidoDeviceWS.deleteDevice(fragment);
                    break;
                case PATCH:
                    PatchRequest pr = mapper.readValue(data, PatchRequest.class);
                    response = fidoDeviceWS.patchDevice(pr, fragment, "id", null);
                    break;
                case POST:
                    response = fidoDeviceWS.createDevice();
                    break;
            }
        else if (ws == fido2DeviceWS)
            switch(verb) {
                case PUT:
                    Fido2DeviceResource dev = mapper.readValue(data, Fido2DeviceResource.class);
                    response = fido2DeviceWS.updateF2Device(dev, fragment, "id", null);
                    break;
                case DELETE:
                    response = fido2DeviceWS.deleteF2Device(fragment);
                    break;
                case PATCH:
                    PatchRequest pr = mapper.readValue(data, PatchRequest.class);
                    response = fido2DeviceWS.patchF2Device(pr, fragment, "id", null);
                    break;
                case POST:
                    response = fido2DeviceWS.createDevice();
                    break;
            }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        response = getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Unexpected error: " + e.getMessage());
    }
    return new Pair<>(response, idCreated);
}
Also used : BulkResponse(io.jans.scim.model.scim2.bulk.BulkResponse) Response(javax.ws.rs.core.Response) Fido2DeviceResource(io.jans.scim.model.scim2.fido.Fido2DeviceResource) FidoDeviceResource(io.jans.scim.model.scim2.fido.FidoDeviceResource) UserResource(io.jans.scim.model.scim2.user.UserResource) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) GroupResource(io.jans.scim.model.scim2.group.GroupResource) Pair(io.jans.util.Pair)

Example 2 with PatchRequest

use of io.jans.scim.model.scim2.patch.PatchRequest in project jans by JanssenProject.

the class GroupWebService method patchGroup.

@Path("{id}")
@PATCH
@Consumes({ MEDIA_TYPE_SCIM_JSON, MediaType.APPLICATION_JSON })
@Produces({ MEDIA_TYPE_SCIM_JSON + UTF8_CHARSET_FRAGMENT, MediaType.APPLICATION_JSON + UTF8_CHARSET_FRAGMENT })
@HeaderParam("Accept")
@DefaultValue(MEDIA_TYPE_SCIM_JSON)
@ProtectedApi(scopes = { "https://jans.io/scim/groups.write" })
@RefAdjusted
public Response patchGroup(PatchRequest request, @PathParam("id") String id, @QueryParam(QUERY_PARAM_ATTRIBUTES) String attrsList, @QueryParam(QUERY_PARAM_EXCLUDED_ATTRS) String excludedAttrsList) {
    Response response;
    try {
        log.debug("Executing web service method. patchGroup");
        response = inspectPatchRequest(request, GroupResource.class);
        if (response != null)
            return response;
        GluuGroup gluuGroup = groupService.getGroupByInum(id);
        if (gluuGroup == null)
            return notFoundResponse(id, groupResourceType);
        response = externalConstraintsService.applyEntityCheck(gluuGroup, request, httpHeaders, uriInfo, HttpMethod.PATCH, groupResourceType);
        if (response != null)
            return response;
        boolean skipValidation = isMembersValidationSkipped();
        boolean displayExcluded = isDisplayExcluded(skipValidation, attrsList, excludedAttrsList);
        GroupResource group = new GroupResource();
        // Fill group instance with all info from gluuGroup
        scim2GroupService.transferAttributesToGroupResource(gluuGroup, group, !skipValidation, endpointUrl, usersUrl);
        GroupResource original = (GroupResource) ScimResourceUtil.clone(group);
        Predicate<String> p = skipValidation ? selectionFilterSkipPredicate : (filter -> false);
        // Apply patches one by one in sequence
        for (PatchOperation po : request.getOperations()) {
            group = (GroupResource) scim2PatchService.applyPatchOperation(group, po, p);
        }
        log.debug("patchGroup. Revising final resource representation still passes validations");
        // Throws exception if final representation does not pass overall validation
        executeValidation(group);
        checkDisplayNameExistence(group.getDisplayName(), id);
        // Update timestamp
        group.getMeta().setLastModified(DateUtil.millisToISOString(System.currentTimeMillis()));
        if (!displayExcluded) {
            scim2GroupService.restoreMembersDisplay(original, group);
        }
        // Replaces the information found in gluuGroup with the contents of group
        scim2GroupService.replaceGroupInfo(gluuGroup, group, skipValidation, !displayExcluded, endpointUrl, usersUrl);
        String json = resourceSerializer.serialize(group, attrsList, excludedAttrsList);
        response = Response.ok(new URI(group.getMeta().getLocation())).entity(json).build();
    } catch (DuplicateEntryException e) {
        log.error(e.getMessage());
        response = getErrorResponse(Response.Status.CONFLICT, ErrorScimType.UNIQUENESS, e.getMessage());
    } catch (InvalidAttributeValueException e) {
        log.error(e.getMessage(), e);
        response = getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.MUTABILITY, e.getMessage());
    } catch (SCIMException e) {
        response = getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.INVALID_SYNTAX, e.getMessage());
    } catch (Exception e) {
        log.error("Failure at patchGroup method", e);
        response = getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Unexpected error: " + e.getMessage());
    }
    return response;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Produces(javax.ws.rs.Produces) QUERY_PARAM_FILTER(io.jans.scim.model.scim2.Constants.QUERY_PARAM_FILTER) URISyntaxException(java.net.URISyntaxException) Path(javax.ws.rs.Path) QUERY_PARAM_SORT_ORDER(io.jans.scim.model.scim2.Constants.QUERY_PARAM_SORT_ORDER) BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) MediaType(javax.ws.rs.core.MediaType) SCIMException(io.jans.scim.model.exception.SCIMException) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) GluuGroup(io.jans.scim.model.GluuGroup) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) URI(java.net.URI) DELETE(javax.ws.rs.DELETE) SortOrder(io.jans.orm.model.SortOrder) Predicate(java.util.function.Predicate) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) QUERY_PARAM_EXCLUDED_ATTRS(io.jans.scim.model.scim2.Constants.QUERY_PARAM_EXCLUDED_ATTRS) GroupResource(io.jans.scim.model.scim2.group.GroupResource) List(java.util.List) Response(javax.ws.rs.core.Response) ErrorScimType(io.jans.scim.model.scim2.ErrorScimType) Scim2PatchService(io.jans.scim.service.scim2.Scim2PatchService) PostConstruct(javax.annotation.PostConstruct) QUERY_PARAM_START_INDEX(io.jans.scim.model.scim2.Constants.QUERY_PARAM_START_INDEX) QUERY_PARAM_SORT_BY(io.jans.scim.model.scim2.Constants.QUERY_PARAM_SORT_BY) GroupService(io.jans.scim.service.GroupService) PathParam(javax.ws.rs.PathParam) QUERY_PARAM_COUNT(io.jans.scim.model.scim2.Constants.QUERY_PARAM_COUNT) GET(javax.ws.rs.GET) QUERY_PARAM_ATTRIBUTES(io.jans.scim.model.scim2.Constants.QUERY_PARAM_ATTRIBUTES) DuplicateEntryException(io.jans.orm.exception.operation.DuplicateEntryException) DateUtil(io.jans.scim.model.scim2.util.DateUtil) HttpMethod(javax.ws.rs.HttpMethod) ScimResourceUtil(io.jans.scim.model.scim2.util.ScimResourceUtil) Inject(javax.inject.Inject) Named(javax.inject.Named) POST(javax.ws.rs.POST) ProtectedApi(io.jans.scim.service.filter.ProtectedApi) UTF8_CHARSET_FRAGMENT(io.jans.scim.model.scim2.Constants.UTF8_CHARSET_FRAGMENT) RefAdjusted(io.jans.scim.service.scim2.interceptor.RefAdjusted) GROUP_OVERHEAD_BYPASS_PARAM(io.jans.scim.model.scim2.Constants.GROUP_OVERHEAD_BYPASS_PARAM) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) SearchRequest(io.jans.scim.model.scim2.SearchRequest) PagedResult(io.jans.orm.model.PagedResult) MEDIA_TYPE_SCIM_JSON(io.jans.scim.model.scim2.Constants.MEDIA_TYPE_SCIM_JSON) Scim2GroupService(io.jans.scim.service.scim2.Scim2GroupService) PUT(javax.ws.rs.PUT) GluuGroup(io.jans.scim.model.GluuGroup) URI(java.net.URI) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) GroupResource(io.jans.scim.model.scim2.group.GroupResource) URISyntaxException(java.net.URISyntaxException) SCIMException(io.jans.scim.model.exception.SCIMException) DuplicateEntryException(io.jans.orm.exception.operation.DuplicateEntryException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) Response(javax.ws.rs.core.Response) SCIMException(io.jans.scim.model.exception.SCIMException) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) DuplicateEntryException(io.jans.orm.exception.operation.DuplicateEntryException) Path(javax.ws.rs.Path) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) RefAdjusted(io.jans.scim.service.scim2.interceptor.RefAdjusted) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ProtectedApi(io.jans.scim.service.filter.ProtectedApi)

Example 3 with PatchRequest

use of io.jans.scim.model.scim2.patch.PatchRequest in project jans by JanssenProject.

the class BaseScimWebService method inspectPatchRequest.

protected Response inspectPatchRequest(PatchRequest patch, Class<? extends BaseScimResource> cls) {
    Response response = null;
    List<String> schemas = patch.getSchemas();
    if (schemas != null && schemas.size() == 1 && schemas.get(0).equals(PATCH_REQUEST_SCHEMA_ID)) {
        List<PatchOperation> ops = patch.getOperations();
        if (ops != null) {
            // Adjust paths if they came prefixed
            String defSchema = ScimResourceUtil.getDefaultSchemaUrn(cls);
            List<String> urns = extService.getUrnsOfExtensions(cls);
            urns.add(defSchema);
            for (PatchOperation op : ops) {
                if (op.getPath() != null)
                    op.setPath(ScimResourceUtil.adjustNotationInPath(op.getPath(), defSchema, urns));
            }
            for (PatchOperation op : ops) {
                if (op.getType() == null)
                    response = getErrorResponse(BAD_REQUEST, ErrorScimType.INVALID_SYNTAX, "Operation '" + op.getOperation() + "' not recognized");
                else {
                    String path = op.getPath();
                    if (StringUtils.isEmpty(path) && op.getType().equals(PatchOperationType.REMOVE))
                        response = getErrorResponse(BAD_REQUEST, ErrorScimType.NO_TARGET, "Path attribute is required for remove operation");
                    else if (op.getValue() == null && !op.getType().equals(PatchOperationType.REMOVE))
                        response = getErrorResponse(BAD_REQUEST, ErrorScimType.INVALID_SYNTAX, "Value attribute is required for operations other than remove");
                }
                if (response != null)
                    break;
            }
        } else
            response = getErrorResponse(BAD_REQUEST, ErrorScimType.INVALID_SYNTAX, "Patch request MUST contain the attribute 'Operations'");
    } else
        response = getErrorResponse(BAD_REQUEST, ErrorScimType.INVALID_SYNTAX, "Wrong schema(s) supplied in Search Request");
    log.info("inspectPatchRequest. Preprocessing of patch request {}", response == null ? "passed" : "failed");
    return response;
}
Also used : ErrorResponse(io.jans.scim.model.scim2.ErrorResponse) Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation)

Example 4 with PatchRequest

use of io.jans.scim.model.scim2.patch.PatchRequest in project jans by JanssenProject.

the class PatchGroupTest method patch2.

@Test(dependsOnMethods = "patch1")
public void patch2() throws Exception {
    List<UserResource> users = getTestUsers("aaa");
    assertTrue(users.size() > 0);
    // Define one "add" operation to insert the users retrieved in the created group
    PatchOperation operation = new PatchOperation();
    operation.setOperation("add");
    operation.setPath("members");
    List<Member> memberList = new ArrayList<>();
    users.stream().forEach(u -> {
        Member m = new Member();
        m.setType(ScimResourceUtil.getType(usrClass));
        m.setValue(u.getId());
        m.setDisplay(u.getDisplayName());
        m.setRef("/scim/v2/Users/" + u.getId());
        memberList.add(m);
    });
    operation.setValue(memberList);
    // Apply the patch to the group
    PatchRequest pr = new PatchRequest();
    pr.setOperations(Collections.singletonList(operation));
    Response response = client.patchGroup(pr, group.getId(), null, null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    group = response.readEntity(groupCls);
    // Verify the new users are there
    Set<Member> members = group.getMembers();
    assertNotNull(members);
    assertTrue(members.stream().allMatch(m -> m.getDisplay() != null && m.getType() != null && m.getValue() != null && m.getRef() != null));
    // Verify the Ids are the same (both provided and returned)
    Set<String> userIds = users.stream().map(UserResource::getId).collect(Collectors.toSet());
    assertTrue(members.stream().map(Member::getValue).collect(Collectors.toSet()).equals(userIds));
}
Also used : Response(javax.ws.rs.core.Response) Member(io.jans.scim.model.scim2.group.Member) UserResource(io.jans.scim.model.scim2.user.UserResource) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) Set(java.util.Set) Test(org.testng.annotations.Test) BaseTest(io.jans.scim2.client.BaseTest) Collectors(java.util.stream.Collectors) ScimResourceUtil(io.jans.scim.model.scim2.util.ScimResourceUtil) ArrayList(java.util.ArrayList) GroupResource(io.jans.scim.model.scim2.group.GroupResource) List(java.util.List) Response(javax.ws.rs.core.Response) Assert(org.testng.Assert) Parameters(org.testng.annotations.Parameters) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) io.jans.scim.model.scim2(io.jans.scim.model.scim2) Collections(java.util.Collections) Status(javax.ws.rs.core.Response.Status) UserResource(io.jans.scim.model.scim2.user.UserResource) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) ArrayList(java.util.ArrayList) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) Member(io.jans.scim.model.scim2.group.Member) Test(org.testng.annotations.Test) BaseTest(io.jans.scim2.client.BaseTest)

Example 5 with PatchRequest

use of io.jans.scim.model.scim2.patch.PatchRequest in project jans by JanssenProject.

the class PatchGroupTest method patch3.

@Test(dependsOnMethods = "patch2")
public void patch3() {
    Member[] members = group.getMembers().toArray(new Member[0]);
    // Try modifying one of the members. This should fail because of mutability
    PatchOperation operation = new PatchOperation();
    operation.setOperation("replace");
    operation.setPath(String.format("members[value eq \"%s\"].value", members[0].getValue()));
    operation.setValue(members[1].getValue());
    PatchRequest pr = new PatchRequest();
    pr.setOperations(Collections.singletonList(operation));
    Response response = client.patchGroup(pr, group.getId(), null, null);
    assertEquals(response.getStatus(), BAD_REQUEST.getStatusCode());
    // Try modifying one of the members. This should not fail ...
    operation.setValue(members[0].getValue());
    response = client.patchGroup(pr, group.getId(), null, null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    // Try deleting value subattribute. This should fail ...
    operation.setOperation("remove");
    response = client.patchGroup(pr, group.getId(), null, null);
    assertEquals(response.getStatus(), BAD_REQUEST.getStatusCode());
    // Try removing one of the members. This should not fail ...
    operation.setPath(String.format("members[value eq \"%s\"]", members[0].getValue()));
    response = client.patchGroup(pr, group.getId(), null, null);
    group = response.readEntity(GroupResource.class);
    assertEquals(response.getStatus(), OK.getStatusCode());
    assertEquals(members.length - 1, group.getMembers().size());
}
Also used : Response(javax.ws.rs.core.Response) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) Member(io.jans.scim.model.scim2.group.Member) GroupResource(io.jans.scim.model.scim2.group.GroupResource) Test(org.testng.annotations.Test) BaseTest(io.jans.scim2.client.BaseTest)

Aggregations

Response (javax.ws.rs.core.Response)19 UserResource (io.jans.scim.model.scim2.user.UserResource)14 Test (org.testng.annotations.Test)14 PatchOperation (io.jans.scim.model.scim2.patch.PatchOperation)10 UserBaseTest (io.jans.scim2.client.UserBaseTest)10 PatchRequest (io.jans.scim.model.scim2.patch.PatchRequest)9 Parameters (org.testng.annotations.Parameters)9 GroupResource (io.jans.scim.model.scim2.group.GroupResource)4 BaseTest (io.jans.scim2.client.BaseTest)4 ListResponse (io.jans.scim.model.scim2.ListResponse)3 InstantMessagingAddress (io.jans.scim.model.scim2.user.InstantMessagingAddress)3 PhoneNumber (io.jans.scim.model.scim2.user.PhoneNumber)3 DuplicateEntryException (io.jans.orm.exception.operation.DuplicateEntryException)2 SCIMException (io.jans.scim.model.exception.SCIMException)2 CustomAttributes (io.jans.scim.model.scim2.CustomAttributes)2 BulkResponse (io.jans.scim.model.scim2.bulk.BulkResponse)2 Member (io.jans.scim.model.scim2.group.Member)2 Address (io.jans.scim.model.scim2.user.Address)2 ScimResourceUtil (io.jans.scim.model.scim2.util.ScimResourceUtil)2 ArrayList (java.util.ArrayList)2