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);
}
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;
}
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;
}
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));
}
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());
}
Aggregations