use of org.wso2.carbon.apimgt.core.models.UriTemplate in project carbon-apimgt by wso2.
the class ApiDAOImpl method deleteEndPointsForOperation.
private void deleteEndPointsForOperation(Connection connection, String apiId) throws SQLException, IOException {
final String query = "DELETE FROM AM_API_RESOURCE_ENDPOINT WHERE API_ID = ?";
Set<String> endpoints = new HashSet<>();
Map<String, UriTemplate> uriTemplates = getUriTemplates(connection, apiId);
for (Map.Entry<String, UriTemplate> uriTemplate : uriTemplates.entrySet()) {
Map<String, Endpoint> apiEndPoints = getEndPointsForOperation(connection, apiId, uriTemplate.getValue().getTemplateId());
for (Map.Entry<String, Endpoint> apiEndpoint : apiEndPoints.entrySet()) {
Endpoint endpoint = apiEndpoint.getValue();
if (APIMgtConstants.API_SPECIFIC_ENDPOINT.equals(endpoint.getApplicableLevel())) {
endpoints.add(endpoint.getId());
}
}
}
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, apiId);
preparedStatement.execute();
for (String endpointId : endpoints) {
if (!isEndpointAssociated(connection, endpointId)) {
deleteEndpoint(connection, endpointId);
}
}
}
}
use of org.wso2.carbon.apimgt.core.models.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.core.models.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.core.models.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.core.models.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);
}
Aggregations