use of org.wso2.siddhi.query.api.extension.Extension in project charon by wso2.
the class PatchOperationUtil method doPatchReplaceWithFiltersForLevelTwo.
/*
* This method is to do patch replace for level two attributes with a filter present.
* @param oldResource
* @param attributeParts
* @param expressionNode
* @param operation
* @param schema
* @param decoder
* @return
* @throws CharonException
* @throws BadRequestException
* @throws JSONException
* @throws InternalErrorException
*/
private static AbstractSCIMObject doPatchReplaceWithFiltersForLevelTwo(AbstractSCIMObject oldResource, String[] attributeParts, ExpressionNode expressionNode, PatchOperation operation, SCIMResourceTypeSchema schema, JSONDecoder decoder) throws CharonException, BadRequestException, JSONException, InternalErrorException {
Attribute attribute = oldResource.getAttribute(attributeParts[0]);
boolean isValueFound = false;
if (attribute != null) {
if (attribute.getMultiValued()) {
List<Attribute> subValues = ((MultiValuedAttribute) attribute).getAttributeValues();
if (subValues != null) {
for (Attribute subValue : subValues) {
Map<String, Attribute> subAttributes = ((ComplexAttribute) subValue).getSubAttributesList();
// this map is to avoid concurrent modification exception.
Map<String, Attribute> tempSubAttributes = (Map<String, Attribute>) CopyUtil.deepCopy(subAttributes);
for (Iterator<Attribute> iterator = tempSubAttributes.values().iterator(); iterator.hasNext(); ) {
Attribute subAttribute = iterator.next();
if (subAttribute.getName().equals(expressionNode.getAttributeValue())) {
if (((SimpleAttribute) subAttribute).getValue().equals(expressionNode.getValue())) {
Attribute replacingAttribute = subAttributes.get(attributeParts[1]);
if (replacingAttribute == null) {
// add the attribute
AttributeSchema replacingAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
if (replacingAttributeSchema.getMultiValued()) {
MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(replacingAttributeSchema.getName());
DefaultAttributeFactory.createAttribute(replacingAttributeSchema, multiValuedAttribute);
multiValuedAttribute.setAttributePrimitiveValue(operation.getValues());
((ComplexAttribute) subValue).setSubAttribute(multiValuedAttribute);
break;
} else {
SimpleAttribute simpleAttribute = new SimpleAttribute(replacingAttributeSchema.getName(), operation.getValues());
DefaultAttributeFactory.createAttribute(replacingAttributeSchema, simpleAttribute);
((ComplexAttribute) subValue).setSubAttribute(simpleAttribute);
break;
}
}
if (replacingAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || replacingAttribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
throw new BadRequestException("Can not remove a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
} else {
if (replacingAttribute.getMultiValued()) {
((MultiValuedAttribute) replacingAttribute).getAttributePrimitiveValues().remove(expressionNode.getValue());
((MultiValuedAttribute) replacingAttribute).setAttributePrimitiveValue(operation.getValues());
} else {
((SimpleAttribute) (replacingAttribute)).setValue(operation.getValues());
}
isValueFound = true;
}
}
}
}
}
if (!isValueFound) {
throw new BadRequestException("No matching filter value found.", ResponseCodeConstants.NO_TARGET);
}
}
} else if (attribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
// this is only valid for extension
Attribute subAttribute = attribute.getSubAttribute(attributeParts[1]);
if (subAttribute == null) {
// add the attribute
AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
if (subAttributeSchema != null) {
if (subAttributeSchema.getMultiValued()) {
MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(subAttributeSchema.getName());
DefaultAttributeFactory.createAttribute(subAttributeSchema, multiValuedAttribute);
multiValuedAttribute.setAttributeValue(decoder.buildComplexAttribute(subAttributeSchema, (JSONObject) operation.getValues()));
((ComplexAttribute) (attribute)).setSubAttribute(multiValuedAttribute);
} else {
throw new BadRequestException("Attribute : " + attributeParts[1] + "is not a multi valued attribute.", ResponseCodeConstants.INVALID_PATH);
}
} else {
throw new BadRequestException("Attribute : " + attributeParts[0] + "." + attributeParts[1] + "does not exists.", ResponseCodeConstants.INVALID_PATH);
}
} else {
List<Attribute> subValues = ((MultiValuedAttribute) (subAttribute)).getAttributeValues();
if (subValues != null) {
for (Iterator<Attribute> subValueIterator = subValues.iterator(); subValueIterator.hasNext(); ) {
Attribute subValue = subValueIterator.next();
Map<String, Attribute> subValuesSubAttribute = ((ComplexAttribute) subValue).getSubAttributesList();
for (Iterator<Attribute> iterator = subValuesSubAttribute.values().iterator(); iterator.hasNext(); ) {
Attribute subSubAttribute = iterator.next();
if (subSubAttribute.getName().equals(expressionNode.getAttributeValue())) {
if (((SimpleAttribute) (subSubAttribute)).getValue().equals(expressionNode.getValue())) {
if (subValue.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subValue.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
throw new BadRequestException("Can not remove a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
} else {
subValueIterator.remove();
isValueFound = true;
}
}
}
}
}
AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
subValues.add(decoder.buildComplexAttribute(attributeSchema, (JSONObject) operation.getValues()));
if (!isValueFound) {
throw new BadRequestException("No matching filter value found.", ResponseCodeConstants.NO_TARGET);
}
}
}
} else {
throw new BadRequestException("Attribute : " + expressionNode.getAttributeValue() + " " + "is not a multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
}
} else {
// add the attribute
AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0], schema);
if (attributeSchema != null) {
if (attributeSchema.getMultiValued()) {
MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
String complexName = attributeSchema.getName() + "_" + SCIMConstants.DEFAULT + "_" + SCIMConstants.DEFAULT;
ComplexAttribute complexAttribute = new ComplexAttribute(complexName);
DefaultAttributeFactory.createAttribute(attributeSchema, complexAttribute);
AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
if (subAttributeSchema != null) {
if (subAttributeSchema.getMultiValued()) {
MultiValuedAttribute multiValuedSubAttribute = new MultiValuedAttribute(subAttributeSchema.getName());
DefaultAttributeFactory.createAttribute(subAttributeSchema, multiValuedSubAttribute);
multiValuedAttribute.setAttributePrimitiveValue(operation.getValues());
complexAttribute.setSubAttribute(multiValuedSubAttribute);
multiValuedAttribute.setAttributeValue(complexAttribute);
oldResource.setAttribute(multiValuedAttribute);
} else {
SimpleAttribute simpleAttribute = new SimpleAttribute(subAttributeSchema.getName(), operation.getValues());
DefaultAttributeFactory.createAttribute(subAttributeSchema, simpleAttribute);
complexAttribute.setSubAttribute(simpleAttribute);
multiValuedAttribute.setAttributeValue(complexAttribute);
oldResource.setAttribute(multiValuedAttribute);
}
} else {
throw new BadRequestException("Attribute : " + attributeParts[0] + "." + attributeParts[1] + "does not exists.", ResponseCodeConstants.INVALID_PATH);
}
} else {
ComplexAttribute extensionComplexAttribute = new ComplexAttribute(attributeSchema.getName());
DefaultAttributeFactory.createAttribute(attributeSchema, extensionComplexAttribute);
AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
if (subAttributeSchema != null) {
if (subAttributeSchema.getMultiValued()) {
MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(subAttributeSchema.getName());
DefaultAttributeFactory.createAttribute(subAttributeSchema, multiValuedAttribute);
multiValuedAttribute.setAttributeValue(decoder.buildComplexAttribute(subAttributeSchema, (JSONObject) operation.getValues()));
oldResource.setAttribute(multiValuedAttribute);
} else {
throw new BadRequestException("Attribute : " + attributeParts[1] + "is not a multi valued attribute.", ResponseCodeConstants.INVALID_PATH);
}
} else {
throw new BadRequestException("Attribute : " + attributeParts[0] + "." + attributeParts[1] + "does not exists.", ResponseCodeConstants.INVALID_PATH);
}
}
} else {
throw new BadRequestException("No such attribute with the name : " + attributeParts[0], ResponseCodeConstants.INVALID_PATH);
}
}
return oldResource;
}
use of org.wso2.siddhi.query.api.extension.Extension in project charon by wso2.
the class AbstractValidator method setDisplayNameInComplexMultiValuedAttributes.
/*
* This method is basically for adding display sub attribute to multivalued attributes
* which has 'display' as a sub attribute in the respective attribute schema
*
* @param scimObject
* @param resourceSchema
* @throws CharonException
* @throws BadRequestException
*/
protected static void setDisplayNameInComplexMultiValuedAttributes(AbstractSCIMObject scimObject, SCIMResourceTypeSchema resourceSchema) throws CharonException, BadRequestException {
Map<String, Attribute> attributeList = scimObject.getAttributeList();
ArrayList<AttributeSchema> attributeSchemaList = resourceSchema.getAttributesList();
for (AttributeSchema attributeSchema : attributeSchemaList) {
if (attributeSchema.getMultiValued() && attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
if (attributeSchema.getSubAttributeSchema(SCIMConstants.CommonSchemaConstants.DISPLAY) != null) {
if (attributeList.containsKey(attributeSchema.getName())) {
Attribute multiValuedAttribute = attributeList.get(attributeSchema.getName());
setDisplayNameInComplexMultiValuedSubAttributes(multiValuedAttribute, attributeSchema);
}
}
} else if (attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
// this is only valid for extension schema
List<SCIMAttributeSchema> subAttributeSchemaList = attributeSchema.getSubAttributeSchemas();
for (AttributeSchema subAttributeSchema : subAttributeSchemaList) {
if (subAttributeSchema.getMultiValued() && subAttributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
if (subAttributeSchema.getSubAttributeSchema(SCIMConstants.CommonSchemaConstants.DISPLAY) != null) {
Attribute extensionAttribute = attributeList.get(attributeSchema.getName());
if (extensionAttribute != null) {
if ((((ComplexAttribute) extensionAttribute).getSubAttribute(subAttributeSchema.getName())) != null) {
Attribute multiValuedAttribute = (attributeList.get(attributeSchema.getName())).getSubAttribute(subAttributeSchema.getName());
setDisplayNameInComplexMultiValuedSubAttributes(multiValuedAttribute, subAttributeSchema);
}
}
}
}
}
}
}
}
use of org.wso2.siddhi.query.api.extension.Extension in project charon by wso2.
the class AbstractSCIMObject method deleteSubSubAttribute.
/*
* This deletion method is only applicable for extension schema
* Deleting a sub attribute of complex attribute is the responsibility of an attribute holder.
*
* @param grandParentAttribute
* @param parentAttribute
* @param childAttribute
*/
public void deleteSubSubAttribute(String childAttribute, String parentAttribute, String grandParentAttribute) throws CharonException {
if (attributeList.containsKey(grandParentAttribute)) {
ComplexAttribute grandParent = (ComplexAttribute) attributeList.get(grandParentAttribute);
Attribute parent = ((ComplexAttribute) grandParent).getSubAttribute(parentAttribute);
((ComplexAttribute) (parent)).removeSubAttribute(childAttribute);
}
}
use of org.wso2.siddhi.query.api.extension.Extension in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisImportDefinitionPost.
/**
* Import an API from a Swagger or WSDL
*
* @param type definition type. If not specified, default will be SWAGGER
* @param fileInputStream file content stream, can be either archive or a single text file
* @param fileDetail file details
* @param url URL of the definition
* @param additionalProperties Additional attributes specified as a stringified JSON with API's schema
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param implementationType WSDL based API implementation type (SOAP or HTTP_BINDING)
* @param request msf4j request object
* @return Imported API
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisImportDefinitionPost(String type, InputStream fileInputStream, FileInfo fileDetail, String url, String additionalProperties, String implementationType, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
if (StringUtils.isBlank(type)) {
type = APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString();
}
Response response = buildResponseIfParamsInvalid(type, fileInputStream, url);
if (response != null) {
return response;
}
API.APIBuilder apiBuilder = null;
APIDTO additionalPropertiesAPI = null;
if (!StringUtils.isBlank(additionalProperties)) {
if (log.isDebugEnabled()) {
log.debug("Deseriallizing additionalProperties: " + additionalProperties);
}
ObjectMapper mapper = new ObjectMapper();
additionalPropertiesAPI = mapper.readValue(additionalProperties, APIDTO.class);
apiBuilder = MappingUtil.toAPI(additionalPropertiesAPI);
if (log.isDebugEnabled()) {
log.debug("Successfully deseriallized additionalProperties: " + additionalProperties);
}
}
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
String uuid = "";
if (APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString().equals(type)) {
if (log.isDebugEnabled()) {
log.debug("Adding an API by importing a swagger.");
}
if (fileInputStream != null) {
uuid = apiPublisher.addApiFromDefinition(fileInputStream);
} else {
URL swaggerUrl = new URL(url);
HttpURLConnection urlConn = (HttpURLConnection) swaggerUrl.openConnection();
uuid = apiPublisher.addApiFromDefinition(urlConn);
}
} else {
if (log.isDebugEnabled()) {
log.debug("Adding an API by importing a WSDL.");
}
// context, version when creating an API from WSDL
if (additionalPropertiesAPI != null) {
final String soap = RestApiConstants.IMPORT_DEFINITION_WSDL_IMPL_TYPE_SOAP;
final String httpBinding = RestApiConstants.IMPORT_DEFINITION_WSDL_IMPL_TYPE_HTTP;
if (implementationType != null && !soap.equals(implementationType) && !httpBinding.equals(implementationType)) {
String msg = "Invalid implementation type. Should be one of '" + soap + "' or '" + httpBinding + "'";
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
boolean isHttpBinding = httpBinding.equals(implementationType);
if (fileInputStream != null) {
if (fileDetail.getFileName() == null) {
String msg = "File name cannot be null.";
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
if (fileDetail.getFileName().endsWith(".zip")) {
uuid = apiPublisher.addAPIFromWSDLArchive(apiBuilder, fileInputStream, isHttpBinding);
if (log.isDebugEnabled()) {
log.debug("Successfully added API with WSDL archive " + fileDetail.getFileName());
}
} else if (fileDetail.getFileName().endsWith(".wsdl")) {
uuid = apiPublisher.addAPIFromWSDLFile(apiBuilder, fileInputStream, isHttpBinding);
if (log.isDebugEnabled()) {
log.debug("Successfully added API with WSDL file " + fileDetail.getFileName());
}
} else {
String msg = "Unsupported extension type of file: " + fileDetail.getFileName();
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
} else {
uuid = apiPublisher.addAPIFromWSDLURL(apiBuilder, url, isHttpBinding);
if (log.isDebugEnabled()) {
log.debug("Successfully added API with WSDL URL " + url);
}
}
} else {
String msg = "'additionalProperties' should be specified when creating an API from WSDL";
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
}
API returnAPI = apiPublisher.getAPIbyUUID(uuid);
return Response.status(Response.Status.CREATED).entity(MappingUtil.toAPIDto(returnAPI)).build();
} catch (APIManagementException e) {
String errorMessage = "Error while adding new API";
HashMap<String, String> paramList = new HashMap<String, String>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (IOException e) {
String errorMessage = "Error while adding new API";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
}
}
use of org.wso2.siddhi.query.api.extension.Extension in project siddhi by wso2.
the class DocumentationUtils method updateHeadingsInMarkdownFile.
/**
* Update the documentation home page
*
* @param inputFile The path to the input file
* @param outputFile The path to the output file
* @param extensionRepositoryName The name of the extension repository
* @param latestDocumentationVersion The version of the latest documentation generated
* @param namespaceMetaDataList Metadata in this repository
* @throws MojoFailureException if the Mojo fails to find template file or create new documentation file
*/
public static void updateHeadingsInMarkdownFile(File inputFile, File outputFile, String extensionRepositoryName, String latestDocumentationVersion, List<NamespaceMetaData> namespaceMetaDataList) throws MojoFailureException {
// Retrieving the content of the README.md file
List<String> inputFileLines = new ArrayList<>();
try {
inputFileLines = Files.readLines(inputFile, Constants.DEFAULT_CHARSET);
} catch (IOException ignored) {
}
// Generating data model
Map<String, Object> rootDataModel = new HashMap<>();
rootDataModel.put("inputFileLines", inputFileLines);
rootDataModel.put("extensionRepositoryName", extensionRepositoryName);
rootDataModel.put("latestDocumentationVersion", latestDocumentationVersion);
rootDataModel.put("metaData", namespaceMetaDataList);
rootDataModel.put("formatDescription", new FormatDescriptionMethod());
generateFileFromTemplate(Constants.MARKDOWN_HEADINGS_UPDATE_TEMPLATE + Constants.MARKDOWN_FILE_EXTENSION + Constants.FREEMARKER_TEMPLATE_FILE_EXTENSION, rootDataModel, outputFile.getParent(), outputFile.getName());
}
Aggregations