Search in sources :

Example 21 with UriTemplate

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);
            }
        }
    }
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) PreparedStatement(java.sql.PreparedStatement) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 22 with UriTemplate

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);
}
Also used : HashMap(java.util.HashMap) API(org.wso2.carbon.apimgt.core.models.API) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) Test(org.testng.annotations.Test)

Example 23 with UriTemplate

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();
    }
}
Also used : ArrayList(java.util.ArrayList) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) Map(java.util.Map) ResourcesListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.ResourcesListDTO)

Example 24 with UriTemplate

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;
}
Also used : UriTemplateDTO(org.wso2.carbon.apimgt.rest.api.core.dto.UriTemplateDTO) ArrayList(java.util.ArrayList)

Example 25 with UriTemplate

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);
}
Also used : Path(io.swagger.models.Path) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Operation(io.swagger.models.Operation) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) List(java.util.List) ArrayList(java.util.ArrayList) HttpMethod(io.swagger.models.HttpMethod) SecurityRequirement(io.swagger.models.SecurityRequirement)

Aggregations

UriTemplate (org.wso2.carbon.apimgt.core.models.UriTemplate)38 HashMap (java.util.HashMap)30 API (org.wso2.carbon.apimgt.core.models.API)22 Test (org.testng.annotations.Test)15 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)13 ArrayList (java.util.ArrayList)11 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)11 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)10 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)9 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)8 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)8 Map (java.util.Map)7 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)7 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)7 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)7 LifecycleState (org.wso2.carbon.lcm.core.impl.LifecycleState)7 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)6 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)6 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)6 Swagger (io.swagger.models.Swagger)5