Search in sources :

Example 81 with URITemplate

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);
}
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 82 with URITemplate

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();
    }
}
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 83 with URITemplate

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

Example 84 with URITemplate

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);
}
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)

Example 85 with URITemplate

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;
    }
}
Also used : BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) APIResource(org.wso2.carbon.apimgt.core.models.APIResource) Info(io.swagger.models.Info) ServiceMethodInfo(org.wso2.msf4j.ServiceMethodInfo) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) Contact(io.swagger.models.Contact) SwaggerParser(io.swagger.parser.SwaggerParser) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Swagger(io.swagger.models.Swagger) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API)

Aggregations

URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)122 HashMap (java.util.HashMap)71 ArrayList (java.util.ArrayList)67 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)42 Scope (org.wso2.carbon.apimgt.api.model.Scope)41 API (org.wso2.carbon.apimgt.api.model.API)38 UriTemplate (org.wso2.carbon.apimgt.core.models.UriTemplate)38 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)37 HashSet (java.util.HashSet)36 LinkedHashSet (java.util.LinkedHashSet)28 PreparedStatement (java.sql.PreparedStatement)25 ResultSet (java.sql.ResultSet)23 API (org.wso2.carbon.apimgt.core.models.API)22 Map (java.util.Map)21 Tier (org.wso2.carbon.apimgt.api.model.Tier)21 Connection (java.sql.Connection)20 OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)20 LinkedHashMap (java.util.LinkedHashMap)19 List (java.util.List)19 SQLException (java.sql.SQLException)16