Search in sources :

Example 46 with Patch

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;
}
Also used : AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) User(org.wso2.charon3.core.objects.User)

Example 47 with Patch

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 });
    }
}
Also used : JSONObject(org.json.JSONObject) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) Attribute(org.wso2.charon3.core.attributes.Attribute) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) JSONArray(org.json.JSONArray) AttributeSchema(org.wso2.charon3.core.schema.AttributeSchema) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 48 with Patch

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;
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) PatchOperation(org.wso2.charon3.core.utils.codeutils.PatchOperation) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONException(org.json.JSONException)

Example 49 with Patch

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();
}
Also used : Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) LogConfigDelegate(org.wso2.ei.dashboard.micro.integrator.delegates.LogConfigDelegate) Ack(org.wso2.ei.dashboard.core.rest.model.Ack) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses)

Example 50 with Patch

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;
}
Also used : AbstractMap(java.util.AbstractMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map) CLICompileTimeException(org.wso2.apimgt.gateway.cli.exception.CLICompileTimeException)

Aggregations

BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)19 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)11 Test (org.testng.annotations.Test)11 JSONArray (org.json.JSONArray)9 JSONObject (org.json.JSONObject)9 Attribute (org.wso2.charon3.core.attributes.Attribute)9 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)9 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)9 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)9 CharonException (org.wso2.charon3.core.exceptions.CharonException)9 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)8 List (java.util.List)7 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)7 LinkedHashMap (java.util.LinkedHashMap)6 Map (java.util.Map)6 JSONException (org.json.JSONException)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 AttributeSchema (org.wso2.charon3.core.schema.AttributeSchema)6 ExtractableResponse (io.restassured.response.ExtractableResponse)5