use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20TestCase method testGenerateMergedResourceDefinition.
@Test
public void testGenerateMergedResourceDefinition() throws IOException {
APIDefinitionFromSwagger20 apiDefinitionFromSwagger20 = new APIDefinitionFromSwagger20();
String sampleApi = IOUtils.toString(this.getClass().getResourceAsStream(File.separator + "swagger" + File.separator + "swaggerWithAuthorization.yaml"));
UriTemplate uriTemplate1 = new UriTemplate.UriTemplateBuilder().uriTemplate("/apis").httpVerb("post").scopes(Arrays.asList("apim:api_create")).build();
UriTemplate uriTemplate2 = new UriTemplate.UriTemplateBuilder().uriTemplate("/endpoints").httpVerb("post").scopes(Arrays.asList("apim:api_create")).build();
Map<String, UriTemplate> hasTemplateMap = new HashMap<>();
hasTemplateMap.put(APIUtils.generateOperationIdFromPath(uriTemplate1.getUriTemplate(), uriTemplate1.getHttpVerb()), uriTemplate1);
hasTemplateMap.put(APIUtils.generateOperationIdFromPath(uriTemplate2.getUriTemplate(), uriTemplate2.getHttpVerb()), uriTemplate2);
API api = new API.APIBuilder("admin", "admin", "1.0.0").uriTemplates(hasTemplateMap).id(UUID.randomUUID().toString()).build();
apiDefinitionFromSwagger20.generateMergedResourceDefinition(sampleApi, api);
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class ResourcesApiServiceImpl method resourcesGet.
@Override
public Response resourcesGet(String apiContext, String apiVersion, String accept, Request request) throws NotFoundException {
try {
APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
List<UriTemplate> resourcesOfApi = new ArrayList<>();
if (!StringUtils.isEmpty(apiContext) && !StringUtils.isEmpty(apiVersion)) {
resourcesOfApi = apiMgtAdminService.getAllResourcesForApi(apiContext, apiVersion);
}
ResourcesListDTO resourcesListDTO = new ResourcesListDTO();
resourcesListDTO.setList(MappingUtil.convertToResourceListDto(resourcesOfApi));
return Response.ok(resourcesListDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving resources.";
Map<String, String> paramList = new HashMap<String, String>();
if (!StringUtils.isEmpty(apiContext)) {
paramList.put(APIMgtConstants.ExceptionsConstants.API_CONTEXT, apiContext);
}
if (!StringUtils.isEmpty(apiVersion)) {
paramList.put(APIMgtConstants.ExceptionsConstants.API_VERSION, apiVersion);
}
org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class MappingUtil method convertToResourceListDto.
/**
* Convert Uritemplate list to UriTemplateDTO list
*
* @param resourcesOfApi list of uriTemplates
* @return ResourcesListDTO
*/
public static List<UriTemplateDTO> convertToResourceListDto(List<UriTemplate> resourcesOfApi) {
List<UriTemplateDTO> uriTemplateDTOArrayList = new ArrayList<>();
resourcesOfApi.forEach((v) -> {
UriTemplateDTO uriTemplateDTO = new UriTemplateDTO();
uriTemplateDTO.setUriTemplate(v.getUriTemplate());
uriTemplateDTO.setAuthType(v.getAuthType());
uriTemplateDTO.setPolicy(v.getPolicy().getUuid());
uriTemplateDTO.setHttpVerb(v.getHttpVerb());
uriTemplateDTO.setScope("");
uriTemplateDTOArrayList.add(uriTemplateDTO);
});
return uriTemplateDTOArrayList;
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method generateMergedResourceDefinition.
@Override
public String generateMergedResourceDefinition(String resourceConfigJson, API api) {
SwaggerParser swaggerParser = new SwaggerParser();
Swagger swagger = swaggerParser.parse(resourceConfigJson);
addSecuritySchemeToSwaggerDefinition(swagger, api);
String securityName = getOauthSecurityName(swagger);
if (!StringUtils.isEmpty(securityName)) {
List<SecurityRequirement> securityRequirements = swagger.getSecurity();
if (securityRequirements != null) {
for (SecurityRequirement securityRequirement : securityRequirements) {
Map<String, List<String>> requirementMap = securityRequirement.getRequirements();
if (requirementMap.containsKey(securityName)) {
requirementMap.replace(securityName, api.getScopes());
} else {
if (!api.getScopes().isEmpty()) {
requirementMap.put(securityName, api.getScopes());
}
}
}
} else {
if (!api.getScopes().isEmpty()) {
SecurityRequirement securityRequirement = new SecurityRequirement();
securityRequirement.setRequirements(securityName, api.getScopes());
swagger.addSecurity(securityRequirement);
}
}
Map<String, UriTemplate> uriTemplateMap = new HashMap<>(api.getUriTemplates());
swagger.getPaths().entrySet().removeIf(entry -> isPathNotExist(uriTemplateMap, securityName, entry));
uriTemplateMap.forEach((k, v) -> {
Path path = swagger.getPath(v.getUriTemplate());
if (path == null) {
path = new Path();
swagger.path(v.getUriTemplate(), path);
}
Map<HttpMethod, Operation> operationMap = path.getOperationMap();
Operation operation = operationMap.get(getHttpMethodForVerb(v.getHttpVerb().toUpperCase()));
if (operation != null) {
assignScopesToOperation(operation, securityName, v.getScopes());
} else {
Operation operationTocCreate = new Operation();
operationTocCreate.addSecurity(securityName, v.getScopes());
operationTocCreate.addResponse("200", getDefaultResponse());
List<Parameter> parameterList = getParameters(v.getUriTemplate());
if (!HttpMethod.GET.toString().equalsIgnoreCase(v.getHttpVerb()) && !HttpMethod.DELETE.toString().equalsIgnoreCase(v.getHttpVerb()) && !HttpMethod.OPTIONS.toString().equalsIgnoreCase(v.getHttpVerb()) && !HttpMethod.HEAD.toString().equalsIgnoreCase(v.getHttpVerb())) {
parameterList.add(getDefaultBodyParameter());
}
operationTocCreate.setParameters(parameterList);
path.set(v.getHttpVerb().toLowerCase(), operationTocCreate);
}
});
}
return Json.pretty(swagger);
}
use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method generateApiFromSwaggerResource.
/**
* return API Object
*
* @param provider Provider of the API.
* @param apiDefinition API definition as string
* @return API object.
* @throws APIManagementException If failed to generate API from swagger.
*/
@Override
public API.APIBuilder generateApiFromSwaggerResource(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("Swagger doesn't contains the info");
} else {
String apiName = apiInfo.getTitle();
String apiVersion = apiInfo.getVersion();
String apiDescription = apiInfo.getDescription();
Contact contact = apiInfo.getContact();
BusinessInformation businessInformation = new BusinessInformation();
if (contact != null) {
businessInformation.setBusinessOwner(contact.getName());
businessInformation.setBusinessOwnerEmail(contact.getEmail());
}
API.APIBuilder apiBuilder = new API.APIBuilder(provider, apiName, apiVersion);
apiBuilder.businessInformation(businessInformation);
apiBuilder.description(apiDescription);
apiBuilder.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;
}
}
Aggregations