use of io.swagger.models.Operation in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method removeScopeFromSwaggerDefinition.
@Override
public String removeScopeFromSwaggerDefinition(String resourceConfigJSON, String name) {
SwaggerParser swaggerParser = new SwaggerParser();
Swagger swagger = swaggerParser.parse(resourceConfigJSON);
Map<String, SecuritySchemeDefinition> securitySchemeDefinitionMap = swagger.getSecurityDefinitions();
if (securitySchemeDefinitionMap != null && !securitySchemeDefinitionMap.isEmpty()) {
OAuth2Definition oAuth2Definition = (OAuth2Definition) securitySchemeDefinitionMap.get(APIMgtConstants.OAUTH2SECURITY);
if (oAuth2Definition != null) {
// Removing Scope from Swagger SecurityDefinition
oAuth2Definition.getScopes().remove(name);
// Finding Security requirements at root level
List<SecurityRequirement> securityRequirements = swagger.getSecurity();
if (securityRequirements != null && !securityRequirements.isEmpty()) {
// Get List of Security Requirements
Iterator<SecurityRequirement> securityRequirementIterator = securityRequirements.iterator();
while (securityRequirementIterator.hasNext()) {
SecurityRequirement securityRequirement = securityRequirementIterator.next();
Map<String, List<String>> secListMap = securityRequirement.getRequirements();
// get Oauth2Security scopes
List<String> scopesList = secListMap.get(APIMgtConstants.OAUTH2SECURITY);
if (scopesList != null) {
// Remove Scope from root level
scopesList.remove(name);
}
// Check root level security Requirements is empty
if (securityRequirement.getRequirements().isEmpty()) {
// Check root level security Requirements
securityRequirementIterator.remove();
}
}
if (securityRequirements.isEmpty()) {
// Remove root level security
swagger.setSecurity(null);
}
}
Map<String, Path> pathMap = swagger.getPaths();
if (pathMap != null && !pathMap.isEmpty()) {
for (Map.Entry<String, Path> pathEntry : pathMap.entrySet()) {
Path path = pathEntry.getValue();
List<Operation> operationList = path.getOperations();
for (Operation operation : operationList) {
List<Map<String, List<String>>> operationSecurityList = operation.getSecurity();
if (operationSecurityList != null && !operationSecurityList.isEmpty()) {
Iterator<Map<String, List<String>>> securityMapIterator = operationSecurityList.iterator();
while (securityMapIterator.hasNext()) {
Map<String, List<String>> securityMap = securityMapIterator.next();
List<String> scopesList = securityMap.get(APIMgtConstants.OAUTH2SECURITY);
scopesList.remove(name);
if (scopesList.isEmpty()) {
securityMapIterator.remove();
}
}
if (operationSecurityList.isEmpty()) {
operation.setSecurity(null);
}
}
}
}
}
}
}
return Json.pretty(swagger);
}
use of io.swagger.models.Operation in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method populateConfigMapForScopes.
/*
* This method populates resource to scope mappings into localConfigMap
*
* @param swagger swagger oc of the apis
* @param String namespacee unigue identifier of the api
*
* */
private void populateConfigMapForScopes(Swagger swagger, String namespace) {
Map<String, String> configMap = ServiceReferenceHolder.getInstance().getRestAPIConfigurationMap(namespace);
// update local cache with configs defined in configuration file(dep.yaml)
if (!localConfigMap.containsKey(namespace)) {
localConfigMap.put(namespace, new ConcurrentHashMap<>());
}
if (configMap != null) {
localConfigMap.get(namespace).putAll(configMap);
}
// update local cache with the resource to scope mapping read from swagger
if (swagger != null) {
for (Map.Entry<String, Path> entry : swagger.getPaths().entrySet()) {
Path resource = entry.getValue();
Map<HttpMethod, Operation> operationsMap = resource.getOperationMap();
for (Map.Entry<HttpMethod, Operation> httpverbEntry : operationsMap.entrySet()) {
if (httpverbEntry.getValue().getVendorExtensions().size() > 0 && httpverbEntry.getValue().getVendorExtensions().get(APIMgtConstants.SWAGGER_X_SCOPE) != null) {
String path = httpverbEntry.getKey() + "_" + entry.getKey();
if (!localConfigMap.get(namespace).containsKey(path)) {
localConfigMap.get(namespace).put(path, httpverbEntry.getValue().getVendorExtensions().get(APIMgtConstants.SWAGGER_X_SCOPE).toString());
}
}
}
}
}
}
use of io.swagger.models.Operation in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method setApiResourceBuilderProperties.
/**
* Extract properties in Operation entry and assign them to api resource builder properties.
*
* @param operationEntry Map entry to be extracted properties
* @param uriTemplateBuilder Uri template builder to assign related properties
* @param resourcePath resource path
* @return APIResource.Builder object
*/
private APIResource.Builder setApiResourceBuilderProperties(Map.Entry<HttpMethod, Operation> operationEntry, UriTemplate.UriTemplateBuilder uriTemplateBuilder, String resourcePath) {
Operation operation = operationEntry.getValue();
APIResource.Builder apiResourceBuilder = new APIResource.Builder();
List<String> producesList = operation.getProduces();
if (producesList != null) {
String produceSeparatedString = "\"";
produceSeparatedString += String.join("\",\"", producesList) + "\"";
apiResourceBuilder.produces(produceSeparatedString);
}
List<String> consumesList = operation.getConsumes();
if (consumesList != null) {
String consumesSeparatedString = "\"";
consumesSeparatedString += String.join("\",\"", consumesList) + "\"";
apiResourceBuilder.consumes(consumesSeparatedString);
}
if (operation.getOperationId() != null) {
uriTemplateBuilder.templateId(operation.getOperationId());
} else {
uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(resourcePath, operationEntry.getKey().name()));
}
uriTemplateBuilder.httpVerb(operationEntry.getKey().name());
apiResourceBuilder.uriTemplate(uriTemplateBuilder.build());
return apiResourceBuilder;
}
use of io.swagger.models.Operation in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method generateSwaggerFromResources.
@Override
public String generateSwaggerFromResources(CompositeAPI.Builder api) {
Swagger swagger = new Swagger();
Info info = new Info();
info.setTitle(api.getName());
info.setDescription(api.getDescription());
info.setVersion(api.getVersion());
swagger.setInfo(info);
Map<String, Path> stringPathMap = new HashMap();
for (UriTemplate uriTemplate : api.getUriTemplates().values()) {
String uriTemplateString = uriTemplate.getUriTemplate();
List<Parameter> parameterList = getParameters(uriTemplateString);
if (!HttpMethod.GET.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.DELETE.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.OPTIONS.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.HEAD.toString().equalsIgnoreCase(uriTemplate.getHttpVerb())) {
parameterList.add(getDefaultBodyParameter());
}
Operation operation = new Operation();
operation.setParameters(parameterList);
operation.setOperationId(uriTemplate.getTemplateId());
operation.addResponse("200", getDefaultResponse());
if (stringPathMap.containsKey(uriTemplateString)) {
Path path = stringPathMap.get(uriTemplateString);
path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
} else {
Path path = new Path();
path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
stringPathMap.put(uriTemplateString, path);
}
}
swagger.setPaths(stringPathMap);
swagger.setPaths(stringPathMap);
return Json.pretty(swagger);
}
use of io.swagger.models.Operation in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method isOperationNotExist.
private boolean isOperationNotExist(Map.Entry<HttpMethod, Operation> entry, String uri, Map<String, UriTemplate> uriTemplateMap, String securityName) {
HttpMethod httpMethod = entry.getKey();
Operation operation = entry.getValue();
String operationId = APIUtils.generateOperationIdFromPath(uri, httpMethod.name());
if (uriTemplateMap.containsKey(operationId)) {
assignScopesToOperation(operation, securityName, uriTemplateMap.get(operationId).getScopes());
uriTemplateMap.remove(operationId);
return false;
} else {
return true;
}
}
Aggregations