use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class APIStoreImpl method updateCompositeApi.
/**
* {@inheritDoc}
*/
@Override
public void updateCompositeApi(CompositeAPI.Builder apiBuilder) throws APIManagementException {
apiBuilder.provider(getUsername());
apiBuilder.updatedBy(getUsername());
CompositeAPI originalAPI = getApiDAO().getCompositeAPI(apiBuilder.getId());
apiBuilder.createdTime(originalAPI.getCreatedTime());
// workflow status is an internal property and shouldn't be allowed to update externally
apiBuilder.workflowStatus(originalAPI.getWorkflowStatus());
APIUtils.verifyValidityOfApiUpdate(apiBuilder, originalAPI);
try {
String updatedSwagger = apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder);
InputStream gatewayConfig = getApiDAO().getCompositeAPIGatewayConfig(apiBuilder.getId());
GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
String updatedGatewayConfig = gatewaySourceGenerator.getGatewayConfigFromSwagger(IOUtils.toString(gatewayConfig, StandardCharsets.UTF_8), updatedSwagger);
CompositeAPI api = apiBuilder.build();
if (originalAPI.getContext() != null && !originalAPI.getContext().equals(apiBuilder.getContext())) {
if (isContextExist(api.getContext())) {
throw new APIManagementException("Context already Exist", ExceptionCodes.API_ALREADY_EXISTS);
}
}
// publishing config to gateway
gateway.addCompositeAPI(api);
getApiDAO().updateApiDefinition(api.getId(), updatedSwagger, api.getUpdatedBy());
getApiDAO().updateCompositeAPIGatewayConfig(api.getId(), new ByteArrayInputStream(updatedGatewayConfig.getBytes(StandardCharsets.UTF_8)), api.getUpdatedBy());
if (log.isDebugEnabled()) {
log.debug("API " + api.getName() + "-" + api.getVersion() + " was updated successfully.");
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while updating the API - " + apiBuilder.getName();
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
} catch (IOException e) {
String errorMsg = "Error occurred while reading gateway configuration the API - " + apiBuilder.getName();
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_DAO_EXCEPTION);
}
}
use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class APIStoreImpl method addCompositeApi.
/**
* {@inheritDoc}
*/
@Override
public String addCompositeApi(CompositeAPI.Builder apiBuilder) throws APIManagementException {
apiBuilder.provider(getUsername());
if (StringUtils.isEmpty(apiBuilder.getId())) {
apiBuilder.id(UUID.randomUUID().toString());
}
LocalDateTime localDateTime = LocalDateTime.now();
apiBuilder.createdTime(localDateTime);
apiBuilder.lastUpdatedTime(localDateTime);
apiBuilder.createdBy(getUsername());
apiBuilder.updatedBy(getUsername());
if (!isApiNameExist(apiBuilder.getName()) && !isContextExist(apiBuilder.getContext())) {
setUriTemplates(apiBuilder);
setGatewayDefinitionSource(apiBuilder);
if (StringUtils.isEmpty(apiBuilder.getApiDefinition())) {
apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
}
try {
CompositeAPI createdAPI = apiBuilder.build();
APIUtils.validate(createdAPI);
// publishing config to gateway
gateway.addCompositeAPI(createdAPI);
getApiDAO().addApplicationAssociatedAPI(createdAPI);
if (log.isDebugEnabled()) {
log.debug("API " + createdAPI.getName() + "-" + createdAPI.getVersion() + " was created " + "successfully.", log);
}
} catch (GatewayException e) {
String message = "Error publishing service configuration to Gateway " + apiBuilder.getName();
log.error(message, e);
throw new APIManagementException(message, e, ExceptionCodes.GATEWAY_EXCEPTION);
} catch (APIMgtDAOException e) {
String message = "Error when adding composite API " + apiBuilder.getName();
throw new APIManagementException(message, e, e.getErrorHandler());
}
} else {
String message = "Duplicate API already Exist with name/Context " + apiBuilder.getName();
log.error(message);
throw new APIManagementException(message, ExceptionCodes.API_ALREADY_EXISTS);
}
return apiBuilder.getId();
}
use of org.wso2.carbon.apimgt.core.models.API.APIBuilder 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.core.models.API.APIBuilder 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);
}
use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class APIStoreImpl method addCompositeApiFromDefinition.
/**
* {@inheritDoc}
*/
@Override
public String addCompositeApiFromDefinition(InputStream apiDefinition) throws APIManagementException {
try {
String apiDefinitionString = IOUtils.toString(apiDefinition);
CompositeAPI.Builder apiBuilder = apiDefinitionFromSwagger20.generateCompositeApiFromSwaggerResource(getUsername(), apiDefinitionString);
apiBuilder.apiDefinition(apiDefinitionString);
addCompositeApi(apiBuilder);
return apiBuilder.getId();
} catch (IOException e) {
throw new APIManagementException("Couldn't Generate ApiDefinition from file", ExceptionCodes.API_DEFINITION_MALFORMED);
}
}
Aggregations