use of org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20 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.impl.APIDefinitionFromSwagger20 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.core.impl.APIDefinitionFromSwagger20 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.impl.APIDefinitionFromSwagger20 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.impl.APIDefinitionFromSwagger20 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);
}
}
Aggregations