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