use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project charon by wso2.
the class ServerSideValidator method validateUpdatedSCIMObject.
/**
* Perform validation on SCIM Object update on service provider side.
*
* @param oldObject Old scim object used for comparison.
* @param newObject Updated scim object.
* @param resourceSchema Schema for the scim resource.
* @param validatePerPatchOperation Whether this validation is done per patch operation.
* @return Validated scim object.
* @throws CharonException When error occurred while validating the scim object.
* @throws BadRequestException When error in the user's input.
*/
public static AbstractSCIMObject validateUpdatedSCIMObject(AbstractSCIMObject oldObject, AbstractSCIMObject newObject, SCIMResourceTypeSchema resourceSchema, boolean validatePerPatchOperation) throws CharonException, BadRequestException {
if (newObject instanceof User) {
// Set display names for complex multivalued attributes.
setDisplayNameInComplexMultiValuedAttributes(newObject, resourceSchema);
}
// Check for read only and immutable attributes.
AbstractSCIMObject validatedObject = checkIfReadOnlyAndImmutableAttributesModified(oldObject, newObject, resourceSchema);
// Copy meta attribute from old to new.
validatedObject.setAttribute(oldObject.getAttribute(SCIMConstants.CommonSchemaConstants.META));
// Copy id attribute to new group object.
validatedObject.setAttribute(oldObject.getAttribute(SCIMConstants.CommonSchemaConstants.ID));
// Edit last modified date.
validatedObject.setLastModifiedInstant(Instant.now());
// If this check done per scim patch operation, Only validate the update cause for required attribute removal.
if (validatePerPatchOperation) {
// Check for required attributes.
validatePatchOperationEffectForRequiredAttributes(oldObject, newObject, resourceSchema);
} else {
// Check for required attributes.
validateSCIMObjectForRequiredAttributes(newObject, resourceSchema);
}
// Check for schema list.
validateSchemaList(validatedObject, resourceSchema);
return validatedObject;
}
use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project charon by wso2.
the class PatchOperationUtil method doPatchAddOnPathWithoutFiltersForLevelOne.
/**
* Perform patch add operation for the simple path (level one). As example attributes members, nickname, name.
*
* @param oldResource Original resource SCIM object.
* @param schema SCIM resource schema.
* @param decoder JSON decoder.
* @param operation Operation to be performed.
* @param attributePart Attribute provided in path which needs to be added on resource.
* @throws BadRequestException
* @throws CharonException
* @throws InternalErrorException
*/
private static void doPatchAddOnPathWithoutFiltersForLevelOne(AbstractSCIMObject oldResource, SCIMResourceTypeSchema schema, JSONDecoder decoder, PatchOperation operation, String attributePart) throws BadRequestException, CharonException, InternalErrorException {
Attribute attribute = oldResource.getAttribute(attributePart);
if (attribute != null) {
if (log.isDebugEnabled()) {
log.debug("Attribute: " + attribute.getName() + " extracted from old resource.");
}
checkMutability(attribute);
if (SCIMDefinitions.DataType.COMPLEX.equals(attribute.getType()) && attribute.getMultiValued()) {
if (log.isDebugEnabled()) {
log.debug(String.format("Attribute: %s is a multi-valued complex attribute", attribute.getName()));
}
if (attribute instanceof MultiValuedAttribute) {
// This is complex multi attribute case.
JSONArray jsonArray = getJsonArray(operation);
AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attribute.getName(), schema);
MultiValuedAttribute newMultiValuedAttribute = decoder.buildComplexMultiValuedAttribute(attributeSchema, jsonArray);
for (Attribute newAttribute : newMultiValuedAttribute.getAttributeValues()) {
((MultiValuedAttribute) attribute).setAttributeValue(newAttribute);
}
} else {
throw new BadRequestException("Attribute: " + attribute.getName() + " is not a instance of MultiValuedAttribute.", ResponseCodeConstants.INVALID_SYNTAX);
}
} else if (SCIMDefinitions.DataType.COMPLEX.equals(attribute.getType())) {
if (log.isDebugEnabled()) {
log.debug(String.format("Attribute: %s is a complex attribute but not multi-valued", attribute.getName()));
}
// This is complex attribute case but not multi valued.
JSONObject jsonObject = getJsonObject(operation);
AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attribute.getName(), schema);
ComplexAttribute newComplexAttribute = generateComplexAttribute(decoder, jsonObject, attributeSchema);
addComplexAttributeToResourceExcludingMultiValued(attribute, newComplexAttribute, attributePart);
} else if (attribute.getMultiValued()) {
// This is multivalued primitive case.
if (log.isDebugEnabled()) {
log.debug(String.format("Attribute: %s is a multi-valued primitive attribute", attribute.getName()));
}
((MultiValuedAttribute) attribute).deletePrimitiveValues();
JSONArray jsonArray = getJsonArray(operation);
addPrimitiveMultiValuedAttributeToResource(attribute, jsonArray);
} else {
// This is the simple attribute case, can replace the value.
if (log.isDebugEnabled()) {
log.debug(String.format("Attribute: %s is a simple attribute", attribute.getName()));
}
if (attribute instanceof SimpleAttribute) {
((SimpleAttribute) attribute).setValue(operation.getValues());
} else {
throw new BadRequestException("Attribute: " + attribute.getName() + " is not a instance of SimpleAttribute.", ResponseCodeConstants.INVALID_SYNTAX);
}
}
} else {
// Create and add the attribute.
if (log.isDebugEnabled()) {
log.debug(String.format("Attribute: %s is not found in the resource so create newly and add it", attributePart));
}
createAttributeOnResourceWithPathWithoutFiltersForLevelOne(oldResource, schema, decoder, operation, new String[] { attributePart });
}
}
use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project charon by wso2.
the class JSONDecoder method decodeRequest.
/*
* This method is to extract operations from the PATCH request body and create separate PatchOperation objects
* for each operation
* @param scimResourceString
* @return
*/
public ArrayList<PatchOperation> decodeRequest(String scimResourceString) throws BadRequestException {
ArrayList<PatchOperation> operationList = new ArrayList<PatchOperation>();
try {
// decode the string into json representation
JSONObject decodedJsonObj = new JSONObject(new JSONTokener(scimResourceString));
// obtain the Operations values
JSONArray operationJsonList = (JSONArray) decodedJsonObj.opt(SCIMConstants.OperationalConstants.OPERATIONS);
// for each operation, create a PatchOperation object and add the relevant values to it
for (int count = 0; count < operationJsonList.length(); count++) {
JSONObject operation = (JSONObject) operationJsonList.get(count);
PatchOperation patchOperation = new PatchOperation();
String op = (String) operation.opt(SCIMConstants.OperationalConstants.OP);
patchOperation.setExecutionOrder(count + 1);
if (op == null) {
throw new BadRequestException("Operation can not be null.", ResponseCodeConstants.INVALID_SYNTAX);
} else if (op.equalsIgnoreCase(SCIMConstants.OperationalConstants.ADD)) {
patchOperation.setOperation(SCIMConstants.OperationalConstants.ADD);
} else if (op.equalsIgnoreCase(SCIMConstants.OperationalConstants.REMOVE)) {
patchOperation.setOperation(SCIMConstants.OperationalConstants.REMOVE);
} else if (op.equalsIgnoreCase(SCIMConstants.OperationalConstants.REPLACE)) {
patchOperation.setOperation(SCIMConstants.OperationalConstants.REPLACE);
} else {
throw new BadRequestException("Unknown operation: " + op, ResponseCodeConstants.INVALID_SYNTAX);
}
patchOperation.setPath((String) operation.opt(SCIMConstants.OperationalConstants.PATH));
patchOperation.setValues(operation.opt(SCIMConstants.OperationalConstants.VALUE));
operationList.add(patchOperation);
}
} catch (JSONException e) {
logger.error("json error in decoding the request", e);
throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
}
return operationList;
}
use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project product-mi-tooling by wso2.
the class GroupsApi method updateLogLevel.
@PATCH
@Path("/{group-id}/log-configs")
@Consumes({ "application/json" })
@Produces({ "application/json" })
@Operation(summary = "Update log level", description = "", tags = { "logConfigs" })
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Logger update status", content = @Content(schema = @Schema(implementation = SuccessStatus.class))), @ApiResponse(responseCode = "200", description = "Unexpected error", content = @Content(schema = @Schema(implementation = Error.class))) })
public Response updateLogLevel(@PathParam("group-id") @Parameter(description = "Group ID of the node") String groupId, @Valid LogConfigUpdateRequest request) throws ManagementApiException {
LogConfigDelegate logConfigDelegate = new LogConfigDelegate();
Ack ack = logConfigDelegate.updateLogLevel(groupId, request);
Response.ResponseBuilder responseBuilder = Response.ok().entity(ack);
HttpUtils.setHeaders(responseBuilder);
return responseBuilder.build();
}
use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project product-microgateway by wso2.
the class BallerinaPath method buildContext.
@Override
public BallerinaPath buildContext(PathItem item, ExtendedAPI api) throws BallerinaServiceGenException, CLICompileTimeException {
Map.Entry<String, BallerinaOperation> entry;
BallerinaOperation operation;
// Therefore we have to manually check if each http verb exists
if (item.getGet() != null) {
setServersToOperationLevel(item.getGet(), item.getServers());
try {
operation = new BallerinaOperation().buildContext(item.getGet(), api);
} catch (CLICompileTimeException e) {
throw new CLICompileTimeException("Error while parsing the information under GET resource.\n\t-" + e.getTerminalMsg(), e);
}
entry = new AbstractMap.SimpleEntry<>("get", operation);
operations.add(entry);
}
if (item.getPut() != null) {
setServersToOperationLevel(item.getPut(), item.getServers());
try {
operation = new BallerinaOperation().buildContext(item.getPut(), api);
} catch (CLICompileTimeException e) {
throw new CLICompileTimeException("Error while parsing the information under PUT resource.\n\t-" + e.getTerminalMsg(), e);
}
entry = new AbstractMap.SimpleEntry<>("put", operation);
operations.add(entry);
}
if (item.getPost() != null) {
setServersToOperationLevel(item.getPost(), item.getServers());
try {
operation = new BallerinaOperation().buildContext(item.getPost(), api);
} catch (CLICompileTimeException e) {
throw new CLICompileTimeException("Error while parsing the information under POST resource.\n\t-" + e.getTerminalMsg(), e);
}
entry = new AbstractMap.SimpleEntry<>("post", operation);
operations.add(entry);
}
if (item.getDelete() != null) {
setServersToOperationLevel(item.getDelete(), item.getServers());
try {
operation = new BallerinaOperation().buildContext(item.getDelete(), api);
} catch (CLICompileTimeException e) {
throw new CLICompileTimeException("Error while parsing the information under DELETE resource.\n\t-" + e.getTerminalMsg(), e);
}
entry = new AbstractMap.SimpleEntry<>("delete", operation);
operations.add(entry);
}
if (item.getOptions() != null) {
setServersToOperationLevel(item.getOptions(), item.getServers());
try {
operation = new BallerinaOperation().buildContext(item.getOptions(), api);
} catch (CLICompileTimeException e) {
throw new CLICompileTimeException("Error while parsing the information under OPTIONS resource.\n\t-" + e.getTerminalMsg(), e);
}
entry = new AbstractMap.SimpleEntry<>("options", operation);
operations.add(entry);
}
if (item.getHead() != null) {
setServersToOperationLevel(item.getHead(), item.getServers());
try {
operation = new BallerinaOperation().buildContext(item.getHead(), api);
} catch (CLICompileTimeException e) {
throw new CLICompileTimeException("Error while parsing the information under HEAD resource.\n\t-" + e.getTerminalMsg(), e);
}
entry = new AbstractMap.SimpleEntry<>("head", operation);
operations.add(entry);
}
if (item.getPatch() != null) {
setServersToOperationLevel(item.getPatch(), item.getServers());
try {
operation = new BallerinaOperation().buildContext(item.getPatch(), api);
} catch (CLICompileTimeException e) {
throw new CLICompileTimeException("Error while parsing the information under HEAD resource.\n\t-" + e.getTerminalMsg(), e);
}
entry = new AbstractMap.SimpleEntry<>("patch", operation);
operations.add(entry);
}
return this;
}
Aggregations