Search in sources :

Example 21 with APIDefinitionFromSwagger20

use of org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20 in project carbon-apimgt by wso2.

the class APIPublisherImpl method createUriTemplateList.

private void createUriTemplateList(API.APIBuilder apiBuilder, boolean update) throws APIManagementException {
    Map<String, UriTemplate> uriTemplateMap = new HashMap();
    if (apiBuilder.getUriTemplates().isEmpty()) {
        apiBuilder.uriTemplates(APIUtils.getDefaultUriTemplates());
        apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
    }
    for (UriTemplate uriTemplate : apiBuilder.getUriTemplates().values()) {
        UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(uriTemplate);
        if (StringUtils.isEmpty(uriTemplateBuilder.getTemplateId())) {
            uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(uriTemplate.getUriTemplate(), uriTemplate.getHttpVerb()));
        }
        Map<String, Endpoint> endpointMap = uriTemplateBuilder.getEndpoint();
        validateEndpoints(endpointMap, update);
        validateApiPolicy(uriTemplateBuilder.getPolicy());
        uriTemplateMap.put(uriTemplateBuilder.getTemplateId(), uriTemplateBuilder.build());
    }
    apiBuilder.uriTemplates(uriTemplateMap);
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) HashMap(java.util.HashMap) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate)

Example 22 with APIDefinitionFromSwagger20

use of org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20 in project carbon-apimgt by wso2.

the class APIPublisherImpl method getAPIbyUUID.

@Override
public API getAPIbyUUID(String uuid) throws APIManagementException {
    API api = null;
    try {
        api = super.getAPIbyUUID(uuid);
        if (api != null) {
            api.setUserSpecificApiPermissions(getAPIPermissionsOfLoggedInUser(getUsername(), api));
            String permissionString = api.getApiPermission();
            if (!StringUtils.isEmpty(permissionString)) {
                api.setApiPermission(replaceGroupIdWithName(permissionString));
            }
            if (!getScopesForApi(uuid).isEmpty()) {
                String swagger = getApiSwaggerDefinition(uuid);
                List<String> globalScopes = new APIDefinitionFromSwagger20().getGlobalAssignedScopes(swagger);
                List<APIResource> apiResourceList = new APIDefinitionFromSwagger20().parseSwaggerAPIResources(new StringBuilder(swagger));
                api.setScopes(globalScopes);
                for (APIResource apiResource : apiResourceList) {
                    if (apiResource.getUriTemplate().getScopes().isEmpty()) {
                        UriTemplate retrievedUriTemplateFromApi = api.getUriTemplates().get(apiResource.getUriTemplate().getTemplateId());
                        if (retrievedUriTemplateFromApi != null) {
                            UriTemplate.UriTemplateBuilder uriTemplate = new UriTemplate.UriTemplateBuilder(retrievedUriTemplateFromApi);
                            uriTemplate.scopes(apiResource.getScope());
                            api.getUriTemplates().replace(apiResource.getUriTemplate().getTemplateId(), uriTemplate.build());
                        }
                    }
                }
            }
        }
    } catch (ParseException e) {
        String errorMsg = "Error occurred while parsing the permission json string for API " + api.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.JSON_PARSE_ERROR);
    }
    return api;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIResource(org.wso2.carbon.apimgt.core.models.APIResource) API(org.wso2.carbon.apimgt.core.models.API) ParseException(org.json.simple.parser.ParseException) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate)

Example 23 with APIDefinitionFromSwagger20

use of org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20 in project carbon-apimgt by wso2.

the class APIStoreImpl method updateCompositeApi.

/**
 * {@inheritDoc}
 */
@Override
public void updateCompositeApi(CompositeAPI.Builder apiBuilder) throws APIManagementException {
    apiBuilder.provider(getUsername());
    apiBuilder.updatedBy(getUsername());
    CompositeAPI originalAPI = getApiDAO().getCompositeAPI(apiBuilder.getId());
    apiBuilder.createdTime(originalAPI.getCreatedTime());
    // workflow status is an internal property and shouldn't be allowed to update externally
    apiBuilder.workflowStatus(originalAPI.getWorkflowStatus());
    APIUtils.verifyValidityOfApiUpdate(apiBuilder, originalAPI);
    try {
        String updatedSwagger = apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder);
        InputStream gatewayConfig = getApiDAO().getCompositeAPIGatewayConfig(apiBuilder.getId());
        GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
        APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
        gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
        String updatedGatewayConfig = gatewaySourceGenerator.getGatewayConfigFromSwagger(IOUtils.toString(gatewayConfig, StandardCharsets.UTF_8), updatedSwagger);
        CompositeAPI api = apiBuilder.build();
        if (originalAPI.getContext() != null && !originalAPI.getContext().equals(apiBuilder.getContext())) {
            if (isContextExist(api.getContext())) {
                throw new APIManagementException("Context already Exist", ExceptionCodes.API_ALREADY_EXISTS);
            }
        }
        // publishing config to gateway
        gateway.addCompositeAPI(api);
        getApiDAO().updateApiDefinition(api.getId(), updatedSwagger, api.getUpdatedBy());
        getApiDAO().updateCompositeAPIGatewayConfig(api.getId(), new ByteArrayInputStream(updatedGatewayConfig.getBytes(StandardCharsets.UTF_8)), api.getUpdatedBy());
        if (log.isDebugEnabled()) {
            log.debug("API " + api.getName() + "-" + api.getVersion() + " was updated successfully.");
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while updating the API - " + apiBuilder.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (IOException e) {
        String errorMsg = "Error occurred while reading gateway configuration the API - " + apiBuilder.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_DAO_EXCEPTION);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) IOException(java.io.IOException) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

Example 24 with APIDefinitionFromSwagger20

use of org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20 in project carbon-apimgt by wso2.

the class APIStoreImpl method addCompositeApi.

/**
 * {@inheritDoc}
 */
@Override
public String addCompositeApi(CompositeAPI.Builder apiBuilder) throws APIManagementException {
    apiBuilder.provider(getUsername());
    if (StringUtils.isEmpty(apiBuilder.getId())) {
        apiBuilder.id(UUID.randomUUID().toString());
    }
    LocalDateTime localDateTime = LocalDateTime.now();
    apiBuilder.createdTime(localDateTime);
    apiBuilder.lastUpdatedTime(localDateTime);
    apiBuilder.createdBy(getUsername());
    apiBuilder.updatedBy(getUsername());
    if (!isApiNameExist(apiBuilder.getName()) && !isContextExist(apiBuilder.getContext())) {
        setUriTemplates(apiBuilder);
        setGatewayDefinitionSource(apiBuilder);
        if (StringUtils.isEmpty(apiBuilder.getApiDefinition())) {
            apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
        }
        try {
            CompositeAPI createdAPI = apiBuilder.build();
            APIUtils.validate(createdAPI);
            // publishing config to gateway
            gateway.addCompositeAPI(createdAPI);
            getApiDAO().addApplicationAssociatedAPI(createdAPI);
            if (log.isDebugEnabled()) {
                log.debug("API " + createdAPI.getName() + "-" + createdAPI.getVersion() + " was created " + "successfully.", log);
            }
        } catch (GatewayException e) {
            String message = "Error publishing service configuration to Gateway " + apiBuilder.getName();
            log.error(message, e);
            throw new APIManagementException(message, e, ExceptionCodes.GATEWAY_EXCEPTION);
        } catch (APIMgtDAOException e) {
            String message = "Error when adding composite API " + apiBuilder.getName();
            throw new APIManagementException(message, e, e.getErrorHandler());
        }
    } else {
        String message = "Duplicate API already Exist with name/Context " + apiBuilder.getName();
        log.error(message);
        throw new APIManagementException(message, ExceptionCodes.API_ALREADY_EXISTS);
    }
    return apiBuilder.getId();
}
Also used : LocalDateTime(java.time.LocalDateTime) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI)

Example 25 with APIDefinitionFromSwagger20

use of org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20 in project carbon-apimgt by wso2.

the class APIStoreImpl method setUriTemplates.

private void setUriTemplates(CompositeAPI.Builder apiBuilder) {
    Map<String, UriTemplate> uriTemplateMap = new HashMap();
    if (apiBuilder.getUriTemplates() == null || apiBuilder.getUriTemplates().isEmpty()) {
        apiBuilder.uriTemplates(APIUtils.getDefaultUriTemplates());
        apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
    } else {
        for (UriTemplate uriTemplate : apiBuilder.getUriTemplates().values()) {
            UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(uriTemplate);
            if (StringUtils.isEmpty(uriTemplateBuilder.getTemplateId())) {
                uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(uriTemplate.getUriTemplate(), uriTemplate.getHttpVerb()));
            }
            uriTemplateMap.put(uriTemplateBuilder.getTemplateId(), uriTemplateBuilder.build());
        }
        apiBuilder.uriTemplates(uriTemplateMap);
    }
}
Also used : HashMap(java.util.HashMap) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)15 HashMap (java.util.HashMap)12 API (org.wso2.carbon.apimgt.core.models.API)12 Test (org.testng.annotations.Test)10 UriTemplate (org.wso2.carbon.apimgt.core.models.UriTemplate)9 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)8 Scope (org.wso2.carbon.apimgt.core.models.Scope)7 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)5 IOException (java.io.IOException)4 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)4 GatewayException (org.wso2.carbon.apimgt.core.exception.GatewayException)4 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)4 APIConfigContext (org.wso2.carbon.apimgt.core.template.APIConfigContext)4 LocalDateTime (java.time.LocalDateTime)3 Map (java.util.Map)3 ParseException (org.json.simple.parser.ParseException)3 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)3 APIDefinitionFromSwagger20 (org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20)3 LifecycleException (org.wso2.carbon.lcm.core.exception.LifecycleException)3 FileInputStream (java.io.FileInputStream)2