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