Search in sources :

Example 6 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class APIPublisherImpl method updateApiGatewayConfig.

/**
 * {@inheritDoc}
 */
@Override
public void updateApiGatewayConfig(String apiId, String configString) throws APIManagementException {
    API api = getAPIbyUUID(apiId);
    GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
    APIConfigContext apiConfigContext = new APIConfigContext(api, config.getGatewayPackageName());
    gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
    try {
        String swagger = gatewaySourceGenerator.getSwaggerFromGatewayConfig(configString);
        getApiDAO().updateApiDefinition(apiId, swagger, getUsername());
        getApiDAO().updateGatewayConfig(apiId, configString, getUsername());
    } catch (APIMgtDAOException e) {
        log.error("Couldn't update configuration for apiId " + apiId, e);
        throw new APIManagementException("Couldn't update configuration for apiId " + apiId, e.getErrorHandler());
    } catch (APITemplateException e) {
        log.error("Error generating swagger from gateway config " + apiId, e);
        throw new APIManagementException("Error generating swagger from gateway config " + apiId, ExceptionCodes.TEMPLATE_EXCEPTION);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) APITemplateException(org.wso2.carbon.apimgt.core.template.APITemplateException) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

Example 7 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method generateCompositeApiFromSwaggerResource.

@Override
public CompositeAPI.Builder generateCompositeApiFromSwaggerResource(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("Provided Swagger definition doesn't contain API information");
    } else {
        String apiName = apiInfo.getTitle();
        String apiVersion = apiInfo.getVersion();
        String apiDescription = apiInfo.getDescription();
        CompositeAPI.Builder apiBuilder = new CompositeAPI.Builder().provider(provider).name(apiName).version(apiVersion).description(apiDescription).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 : 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) 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)

Example 8 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method getScopeOfResourcePath.

@Override
public String getScopeOfResourcePath(String resourceConfigsJSON, Request request, ServiceMethodInfo serviceMethodInfo) throws APIManagementException {
    SwaggerParser swaggerParser = new SwaggerParser();
    Swagger swagger = swaggerParser.parse(resourceConfigsJSON);
    String basepath = swagger.getBasePath();
    String verb = (String) request.getProperty(APIMgtConstants.HTTP_METHOD);
    // TODO change to this if msf4j2.3.0-m2 or higher
    // Method resourceMethod = (Method) request.getProperty("method");
    Method resourceMethod = serviceMethodInfo.getMethod();
    if (resourceMethod == null || verb == null) {
        String message = "Could not read required properties from HTTP Request. HTTP_METHOD=" + verb + " resourceTemplate=" + resourceMethod;
        log.error(message);
        throw new APIManagementException(message, ExceptionCodes.SWAGGER_URL_MALFORMED);
    }
    String apiPrefix = resourceMethod.getDeclaringClass().getAnnotation(javax.ws.rs.ApplicationPath.class).value();
    String pathTemplate = "";
    if (resourceMethod.getAnnotation(javax.ws.rs.Path.class) != null) {
        pathTemplate = resourceMethod.getAnnotation(javax.ws.rs.Path.class).value();
    }
    String nameSpace = getNamespaceFromBasePath(basepath);
    if (basepath.contains(APIMgtConstants.APPType.PUBLISHER)) {
        nameSpace = APIMgtConstants.NAMESPACE_PUBLISHER_API;
    } else if (basepath.contains(APIMgtConstants.APPType.STORE)) {
        nameSpace = APIMgtConstants.NAMESPACE_STORE_API;
    } else if (basepath.contains(APIMgtConstants.APPType.ADMIN)) {
        nameSpace = APIMgtConstants.NAMESPACE_ADMIN_API;
    } else if (basepath.contains(APIMgtConstants.APPType.ANALYTICS)) {
        nameSpace = APIMgtConstants.NAMESPACE_ANALYTICS_API;
    }
    // if namespace is not available in local cache add it.
    if (nameSpace != null && !localConfigMap.containsKey(nameSpace)) {
        localConfigMap.put(nameSpace, new ConcurrentHashMap<>());
    }
    if (nameSpace != null && localConfigMap.containsKey(nameSpace) && localConfigMap.get(nameSpace).isEmpty()) {
        populateConfigMapForScopes(swagger, nameSpace);
    }
    String resourceConfig = verb + "_" + apiPrefix + pathTemplate;
    if (localConfigMap.get(nameSpace).containsKey(resourceConfig)) {
        return localConfigMap.get(nameSpace).get(resourceConfig).toString();
    }
    return null;
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Path(io.swagger.models.Path) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Swagger(io.swagger.models.Swagger) Method(java.lang.reflect.Method) HttpMethod(io.swagger.models.HttpMethod)

Example 9 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class APIManagerFactory method newConsumer.

private APIStore newConsumer(String username) throws APIManagementException {
    // }
    try {
        APIStoreImpl userAwareAPIStore = new APIStoreImpl(username, getIdentityProvider(), getKeyManager(), DAOFactory.getApiDAO(), DAOFactory.getApplicationDAO(), DAOFactory.getAPISubscriptionDAO(), DAOFactory.getPolicyDAO(), DAOFactory.getTagDAO(), DAOFactory.getLabelDAO(), DAOFactory.getWorkflowDAO(), new GatewaySourceGeneratorImpl(), new APIGatewayPublisherImpl());
        // Register all the observers which need to observe 'Store' component
        userAwareAPIStore.registerObserver(new EventLogger());
        userAwareAPIStore.registerObserver(new FunctionTrigger(DAOFactory.getFunctionDAO(), new RestCallUtilImpl()));
        return userAwareAPIStore;
    } catch (APIMgtDAOException e) {
        log.error("Couldn't Create API Consumer", e);
        throw new APIMgtDAOException("Couldn't Create API Consumer", ExceptionCodes.APIMGT_DAO_EXCEPTION);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)

Example 10 with APIManagementException

use of org.wso2.carbon.apimgt.core.exception.APIManagementException in project carbon-apimgt by wso2.

the class APIManagerFactory method newProvider.

private APIPublisher newProvider(String username) throws APIManagementException {
    try {
        APIPublisherImpl apiPublisher = new APIPublisherImpl(username, getIdentityProvider(), getKeyManager(), DAOFactory.getApiDAO(), DAOFactory.getApplicationDAO(), DAOFactory.getAPISubscriptionDAO(), DAOFactory.getPolicyDAO(), geApiLifecycleManager(), DAOFactory.getLabelDAO(), DAOFactory.getWorkflowDAO(), DAOFactory.getTagDAO(), DAOFactory.getThreatProtectionDAO(), new GatewaySourceGeneratorImpl(), new APIGatewayPublisherImpl());
        // Register all the observers which need to observe 'Publisher' component
        apiPublisher.registerObserver(new EventLogger());
        apiPublisher.registerObserver(new FunctionTrigger(DAOFactory.getFunctionDAO(), new RestCallUtilImpl()));
        return apiPublisher;
    } catch (APIMgtDAOException e) {
        log.error("Couldn't Create API Provider", e);
        throw new APIMgtDAOException("Couldn't Create API Provider", ExceptionCodes.APIMGT_DAO_EXCEPTION);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)432 Test (org.testng.annotations.Test)353 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)233 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)202 API (org.wso2.carbon.apimgt.core.models.API)200 Test (org.junit.Test)164 Response (javax.ws.rs.core.Response)160 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)159 HashMap (java.util.HashMap)148 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)146 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)134 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)129 ArrayList (java.util.ArrayList)102 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)100 BeforeTest (org.testng.annotations.BeforeTest)82 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)80 Request (org.wso2.msf4j.Request)80 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)76 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)75 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)71