Search in sources :

Example 91 with URITemplate

use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.

the class APIPublisherImpl method saveSwagger20Definition.

/**
 * {@inheritDoc}
 */
@Override
public void saveSwagger20Definition(String apiId, String jsonText) throws APIManagementException {
    try {
        LocalDateTime localDateTime = LocalDateTime.now();
        Map<String, String> oldScopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(getApiSwaggerDefinition(apiId));
        Map<String, String> newScopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(jsonText);
        Map<String, String> updatedScopes = new HashMap<>(newScopes);
        updatedScopes.keySet().retainAll(oldScopes.keySet());
        oldScopes.keySet().removeAll(updatedScopes.keySet());
        newScopes.keySet().removeAll(updatedScopes.keySet());
        for (Map.Entry<String, String> scopeEntry : newScopes.entrySet()) {
            getKeyManager().registerScope(new Scope(scopeEntry.getKey(), scopeEntry.getValue()));
        }
        for (Map.Entry<String, String> scopeEntry : oldScopes.entrySet()) {
            getKeyManager().deleteScope(scopeEntry.getKey());
        }
        for (Map.Entry<String, String> scopeEntry : updatedScopes.entrySet()) {
            Scope scope = getKeyManager().retrieveScope(scopeEntry.getKey());
            scope.setDescription(scopeEntry.getValue());
            getKeyManager().updateScope(scope);
        }
        API api = getAPIbyUUID(apiId);
        Map<String, UriTemplate> oldUriTemplateMap = api.getUriTemplates();
        List<APIResource> apiResourceList = apiDefinitionFromSwagger20.parseSwaggerAPIResources(new StringBuilder(jsonText));
        Map<String, UriTemplate> updatedUriTemplateMap = new HashMap<>();
        for (APIResource apiResource : apiResourceList) {
            updatedUriTemplateMap.put(apiResource.getUriTemplate().getTemplateId(), apiResource.getUriTemplate());
        }
        Map<String, UriTemplate> uriTemplateMapNeedTobeUpdate = APIUtils.getMergedUriTemplates(oldUriTemplateMap, updatedUriTemplateMap);
        API.APIBuilder apiBuilder = new API.APIBuilder(api);
        apiBuilder.uriTemplates(uriTemplateMapNeedTobeUpdate);
        createUriTemplateList(apiBuilder, true);
        apiBuilder.updatedBy(getUsername());
        apiBuilder.lastUpdatedTime(localDateTime);
        api = apiBuilder.build();
        GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
        APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
        gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
        String existingGatewayConfig = getApiGatewayConfig(apiId);
        String updatedGatewayConfig = gatewaySourceGenerator.getGatewayConfigFromSwagger(existingGatewayConfig, jsonText);
        getApiDAO().updateAPI(apiId, api);
        getApiDAO().updateApiDefinition(apiId, jsonText, getUsername());
        getApiDAO().updateGatewayConfig(apiId, updatedGatewayConfig, getUsername());
    } catch (APIMgtDAOException e) {
        String errorMsg = "Couldn't update the Swagger Definition";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) HashMap(java.util.HashMap) APIResource(org.wso2.carbon.apimgt.core.models.APIResource) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Scope(org.wso2.carbon.apimgt.core.models.Scope) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) Map(java.util.Map) HashMap(java.util.HashMap) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

Example 92 with URITemplate

use of org.wso2.carbon.apimgt.api.model.URITemplate 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 93 with URITemplate

use of org.wso2.carbon.apimgt.api.model.URITemplate 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 94 with URITemplate

use of org.wso2.carbon.apimgt.api.model.URITemplate 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)

Example 95 with URITemplate

use of org.wso2.carbon.apimgt.api.model.URITemplate in project carbon-apimgt by wso2.

the class APIStoreImpl method setGatewayDefinitionSource.

private void setGatewayDefinitionSource(CompositeAPI.Builder apiBuilder) throws APIManagementException {
    List<UriTemplate> list = new ArrayList<>(apiBuilder.getUriTemplates().values());
    List<TemplateBuilderDTO> resourceList = new ArrayList<>();
    String appId = null;
    List<CompositeAPIEndpointDTO> endpointDTOs = new ArrayList<CompositeAPIEndpointDTO>();
    try {
        appId = apiBuilder.getApplicationId();
        List<Subscription> subscriptions = getApiSubscriptionDAO().getAPISubscriptionsByApplication(apiBuilder.getApplicationId(), ApiType.STANDARD);
        for (Subscription subscription : subscriptions) {
            CompositeAPIEndpointDTO endpointDTO = new CompositeAPIEndpointDTO();
            API api = subscription.getApi();
            endpointDTO.setEndpointName(api.getName());
            // TODO: currently only HTTPS endpoint considered. Websocket APIs and http transport should considered
            endpointDTO.setTransportType(APIMgtConstants.HTTPS);
            // TODO: replace host with gateway domain host
            String endpointUrl = APIMgtConstants.HTTPS + APIMgtConstants.WEB_PROTOCOL_SUFFIX + config.getHostname() + "/" + api.getContext() + "/" + api.getVersion();
            endpointDTO.setEndpointUrl(endpointUrl);
            endpointDTOs.add(endpointDTO);
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error while getting subscriptions of the application " + appId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
    for (UriTemplate uriTemplate : list) {
        TemplateBuilderDTO dto = new TemplateBuilderDTO();
        dto.setTemplateId(uriTemplate.getTemplateId());
        dto.setUriTemplate(uriTemplate.getUriTemplate());
        dto.setHttpVerb(uriTemplate.getHttpVerb());
        resourceList.add(dto);
    }
    GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
    APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
    gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
    String gatewayConfig = gatewaySourceGenerator.getCompositeAPIConfigStringFromTemplate(resourceList, endpointDTOs);
    if (log.isDebugEnabled()) {
        log.debug("API " + apiBuilder.getName() + "gateway config: " + gatewayConfig);
    }
    apiBuilder.gatewayConfig(gatewayConfig);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ArrayList(java.util.ArrayList) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) CompositeAPIEndpointDTO(org.wso2.carbon.apimgt.core.template.dto.CompositeAPIEndpointDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) TemplateBuilderDTO(org.wso2.carbon.apimgt.core.template.dto.TemplateBuilderDTO) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

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