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