use of io.swagger.models.Swagger in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method generateCompositeApiFromSwaggerResource.
@Override
public CompositeAPI.Builder generateCompositeApiFromSwaggerResource(String provider, String apiDefinition) throws APIManagementException {
SwaggerParser swaggerParser = new SwaggerParser();
Swagger swagger = swaggerParser.parse(apiDefinition);
if (swagger == null) {
throw new APIManagementException("Swagger could not be generated from provided API definition");
}
Info apiInfo = swagger.getInfo();
if (apiInfo == null) {
throw new APIManagementException("Provided Swagger definition doesn't contain API information");
} else {
String apiName = apiInfo.getTitle();
String apiVersion = apiInfo.getVersion();
String apiDescription = apiInfo.getDescription();
CompositeAPI.Builder apiBuilder = new CompositeAPI.Builder().provider(provider).name(apiName).version(apiVersion).description(apiDescription).context(swagger.getBasePath());
List<APIResource> apiResourceList = parseSwaggerAPIResources(new StringBuilder(apiDefinition));
Map<String, UriTemplate> uriTemplateMap = new HashMap();
for (APIResource apiResource : apiResourceList) {
uriTemplateMap.put(apiResource.getUriTemplate().getTemplateId(), apiResource.getUriTemplate());
}
apiBuilder.uriTemplates(uriTemplateMap);
apiBuilder.id(UUID.randomUUID().toString());
return apiBuilder;
}
}
use of io.swagger.models.Swagger in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method updateScopesOnSwaggerDefinition.
@Override
public String updateScopesOnSwaggerDefinition(String resourceConfigJSON, Scope scope) {
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
Map<String, String> scopeMap = oAuth2Definition.getScopes();
if (scopeMap != null && scopeMap.containsKey(scope.getName())) {
scopeMap.replace(scope.getName(), scope.getDescription());
}
}
}
return Json.pretty(swagger);
}
use of io.swagger.models.Swagger in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method getScopeOfResourcePath.
@Override
public String getScopeOfResourcePath(String resourceConfigsJSON, Request request, ServiceMethodInfo serviceMethodInfo) throws APIManagementException {
SwaggerParser swaggerParser = new SwaggerParser();
Swagger swagger = swaggerParser.parse(resourceConfigsJSON);
String basepath = swagger.getBasePath();
String verb = (String) request.getProperty(APIMgtConstants.HTTP_METHOD);
// TODO change to this if msf4j2.3.0-m2 or higher
// Method resourceMethod = (Method) request.getProperty("method");
Method resourceMethod = serviceMethodInfo.getMethod();
if (resourceMethod == null || verb == null) {
String message = "Could not read required properties from HTTP Request. HTTP_METHOD=" + verb + " resourceTemplate=" + resourceMethod;
log.error(message);
throw new APIManagementException(message, ExceptionCodes.SWAGGER_URL_MALFORMED);
}
String apiPrefix = resourceMethod.getDeclaringClass().getAnnotation(javax.ws.rs.ApplicationPath.class).value();
String pathTemplate = "";
if (resourceMethod.getAnnotation(javax.ws.rs.Path.class) != null) {
pathTemplate = resourceMethod.getAnnotation(javax.ws.rs.Path.class).value();
}
String nameSpace = getNamespaceFromBasePath(basepath);
if (basepath.contains(APIMgtConstants.APPType.PUBLISHER)) {
nameSpace = APIMgtConstants.NAMESPACE_PUBLISHER_API;
} else if (basepath.contains(APIMgtConstants.APPType.STORE)) {
nameSpace = APIMgtConstants.NAMESPACE_STORE_API;
} else if (basepath.contains(APIMgtConstants.APPType.ADMIN)) {
nameSpace = APIMgtConstants.NAMESPACE_ADMIN_API;
} else if (basepath.contains(APIMgtConstants.APPType.ANALYTICS)) {
nameSpace = APIMgtConstants.NAMESPACE_ANALYTICS_API;
}
// if namespace is not available in local cache add it.
if (nameSpace != null && !localConfigMap.containsKey(nameSpace)) {
localConfigMap.put(nameSpace, new ConcurrentHashMap<>());
}
if (nameSpace != null && localConfigMap.containsKey(nameSpace) && localConfigMap.get(nameSpace).isEmpty()) {
populateConfigMapForScopes(swagger, nameSpace);
}
String resourceConfig = verb + "_" + apiPrefix + pathTemplate;
if (localConfigMap.get(nameSpace).containsKey(resourceConfig)) {
return localConfigMap.get(nameSpace).get(resourceConfig).toString();
}
return null;
}
use of io.swagger.models.Swagger in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method getScopesFromSecurityDefinition.
@Override
public Map<String, String> getScopesFromSecurityDefinition(String resourceConfigJSON) throws APIManagementException {
SwaggerParser swaggerParser = new SwaggerParser();
Swagger swagger = swaggerParser.parse(resourceConfigJSON);
Map<String, String> scopes = new HashMap<>();
Map<String, SecuritySchemeDefinition> securityDefinitions = swagger.getSecurityDefinitions();
if (securityDefinitions != null) {
for (Map.Entry<String, SecuritySchemeDefinition> securitySchemeDefinitionEntry : securityDefinitions.entrySet()) {
if (securitySchemeDefinitionEntry.getValue() instanceof OAuth2Definition) {
OAuth2Definition securityDefinition = (OAuth2Definition) securitySchemeDefinitionEntry.getValue();
if (securityDefinition != null) {
scopes.putAll(securityDefinition.getScopes());
}
}
}
}
return scopes;
}
use of io.swagger.models.Swagger 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);
}
Aggregations