Search in sources :

Example 86 with APIBuilder

use of org.wso2.carbon.apimgt.core.models.API.APIBuilder 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 87 with APIBuilder

use of org.wso2.carbon.apimgt.core.models.API.APIBuilder 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 88 with APIBuilder

use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.

the class APIPublisherImpl method addAPIFromWSDLURL.

@Override
public String addAPIFromWSDLURL(API.APIBuilder apiBuilder, String wsdlUrl, boolean isHttpBinding) throws APIManagementException {
    WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessor(wsdlUrl);
    if (!processor.canProcess()) {
        throw new APIMgtWSDLException("Unable to process WSDL by the processor " + processor.getClass().getName(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
    }
    apiBuilder.uriTemplates(APIMWSDLUtils.getUriTemplatesForWSDLOperations(processor.getWsdlInfo().getHttpBindingOperations(), isHttpBinding));
    String uuid = addAPI(apiBuilder);
    if (log.isDebugEnabled()) {
        log.debug("Successfully added the API. uuid: " + uuid);
    }
    byte[] wsdlContentBytes = processor.getWSDL();
    getApiDAO().addOrUpdateWSDL(uuid, wsdlContentBytes, getUsername());
    if (log.isDebugEnabled()) {
        log.debug("Successfully added the content of WSDL URL to database. WSDL URL: " + wsdlUrl);
    }
    if (APIMgtConstants.WSDLConstants.WSDL_VERSION_20.equals(processor.getWsdlInfo().getVersion())) {
        log.info("Extraction of HTTP Binding operations is not supported for WSDL 2.0.");
    }
    return uuid;
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)

Example 89 with APIBuilder

use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.

the class APIPublisherImpl method updateCheckListItem.

/**
 * This method used to Update the lifecycle checklist of API
 *
 * @param apiId            UUID of the API
 * @param status           Current API lifecycle status.
 * @param checkListItemMap Check list item values.
 * @throws APIManagementException If failed to update checklist item values.
 */
@Override
public void updateCheckListItem(String apiId, String status, Map<String, Boolean> checkListItemMap) throws APIManagementException {
    API api = getApiDAO().getAPI(apiId);
    try {
        API.APIBuilder apiBuilder = new API.APIBuilder(api);
        apiBuilder.lastUpdatedTime(LocalDateTime.now());
        apiBuilder.updatedBy(getUsername());
        apiBuilder.lifecycleState(getApiLifecycleManager().getLifecycleDataForState(apiBuilder.getLifecycleInstanceId(), apiBuilder.getLifeCycleStatus()));
        for (Map.Entry<String, Boolean> checkListItem : checkListItemMap.entrySet()) {
            getApiLifecycleManager().checkListItemEvent(api.getLifecycleInstanceId(), api.getLifeCycleStatus(), checkListItem.getKey(), checkListItem.getValue());
        }
    } catch (LifecycleException e) {
        String errorMsg = "Couldn't get the lifecycle status of api ID " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
    }
}
Also used : LifecycleException(org.wso2.carbon.lcm.core.exception.LifecycleException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) Map(java.util.Map) HashMap(java.util.HashMap)

Example 90 with APIBuilder

use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.

the class APIPublisherImpl method validateSubscriptionPolicies.

private void validateSubscriptionPolicies(API.APIBuilder apiBuilder) throws APIManagementException {
    Set<Policy> subPolicies = new HashSet();
    for (Policy subscriptionPolicy : apiBuilder.getPolicies()) {
        Policy policy = getPolicyByName(APIMgtAdminService.PolicyLevel.subscription, subscriptionPolicy.getPolicyName());
        if (policy == null) {
            throw new APIManagementException("Api Policy " + apiBuilder.getApiPolicy() + "Couldn't " + "find", ExceptionCodes.POLICY_NOT_FOUND);
        }
        subPolicies.add(policy);
    }
    apiBuilder.policies(subPolicies);
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashSet(java.util.HashSet)

Aggregations

API (org.wso2.carbon.apimgt.core.models.API)85 Test (org.testng.annotations.Test)49 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)49 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)45 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)44 HashMap (java.util.HashMap)43 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)37 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)32 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)30 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)29 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)28 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)27 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)26 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)24 LifecycleState (org.wso2.carbon.lcm.core.impl.LifecycleState)23 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)20 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)18 HashSet (java.util.HashSet)17 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)17 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)16