use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition in project carbon-apimgt by wso2.
the class ApiDAOImplIT method testUpdateGetDedicatedGatewayWhenLabelsAreNull.
@Test
public void testUpdateGetDedicatedGatewayWhenLabelsAreNull() throws Exception {
ApiDAO apiDAO = DAOFactory.getApiDAO();
API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().apiDefinition(SampleTestObjectCreator.apiDefinition);
API api = builder.build();
testAddGetEndpoint();
apiDAO.addAPI(api);
DedicatedGateway dedicatedGateway = new DedicatedGateway();
dedicatedGateway.setEnabled(true);
dedicatedGateway.setUpdatedBy(api.getCreatedBy());
dedicatedGateway.setApiId(api.getId());
apiDAO.updateDedicatedGateway(dedicatedGateway, null);
DedicatedGateway result = apiDAO.getDedicatedGateway(api.getId());
Assert.assertEquals(result.isEnabled(), dedicatedGateway.isEnabled());
}
use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition in project carbon-apimgt by wso2.
the class ApiDAOImplIT method testAddGetCommentsOfAPI.
@Test
public void testAddGetCommentsOfAPI() throws Exception {
ApiDAO apiDAO = DAOFactory.getApiDAO();
API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().apiDefinition(SampleTestObjectCreator.apiDefinition);
API api = builder.build();
testAddGetEndpoint();
apiDAO.addAPI(api);
Comment comment1 = SampleTestObjectCreator.createDefaultComment(api.getId());
Comment comment2 = SampleTestObjectCreator.createAlternativeComment(api.getId());
apiDAO.addComment(comment1, api.getId());
apiDAO.addComment(comment2, api.getId());
List<Comment> commentsFromDB = apiDAO.getCommentsForApi(api.getId());
Assert.assertEquals(commentsFromDB.size(), 2);
Assert.assertTrue(commentsFromDB.get(0).getCommentText().equals(comment1.getCommentText()) || commentsFromDB.get(0).getCommentText().equals(comment2.getCommentText()));
Assert.assertTrue(commentsFromDB.get(1).getCommentText().equals(comment1.getCommentText()) || commentsFromDB.get(1).getCommentText().equals(comment2.getCommentText()));
}
use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition in project carbon-apimgt by wso2.
the class AuthenticatorService method getApplicationScopes.
/**
* This method returns the scopes for a given application.
*
* @param appName Name the application
* @return scopes - A space separated list of scope keys
* @throws APIManagementException When retrieving scopes from swagger definition fails
*/
private String getApplicationScopes(String appName) throws APIManagementException {
String scopes;
String applicationRestAPI = null;
if (AuthenticatorConstants.STORE_APPLICATION.equals(appName)) {
applicationRestAPI = RestApiUtil.getStoreRestAPIResource();
} else if (AuthenticatorConstants.PUBLISHER_APPLICATION.equals(appName)) {
applicationRestAPI = RestApiUtil.getPublisherRestAPIResource();
} else if (AuthenticatorConstants.ADMIN_APPLICATION.equals(appName)) {
applicationRestAPI = RestApiUtil.getAdminRestAPIResource();
}
try {
if (applicationRestAPI != null) {
APIDefinition apiDefinitionFromSwagger20 = new APIDefinitionFromSwagger20();
Map<String, Scope> applicationScopesMap = apiDefinitionFromSwagger20.getScopesFromSecurityDefinitionForWebApps(applicationRestAPI);
scopes = String.join(" ", applicationScopesMap.keySet());
// Set openid scope
if (StringUtils.isEmpty(scopes)) {
scopes = KeyManagerConstants.OPEN_ID_CONNECT_SCOPE;
} else {
scopes = scopes + " " + KeyManagerConstants.OPEN_ID_CONNECT_SCOPE;
}
} else {
String errorMsg = "Error while getting application rest API resource.";
log.error(errorMsg, ExceptionCodes.INTERNAL_ERROR);
throw new APIManagementException(errorMsg, ExceptionCodes.INTERNAL_ERROR);
}
} catch (APIManagementException e) {
String errorMsg = "Error while reading scopes from swagger definition.";
log.error(errorMsg, e, ExceptionCodes.INTERNAL_ERROR);
throw new APIManagementException(errorMsg, e, ExceptionCodes.INTERNAL_ERROR);
}
return scopes;
}
use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition in project carbon-apimgt by wso2.
the class PublisherCommonUtils method updateAsyncAPIDefinition.
/**
* update AsyncPI definition of the given api.
*
* @param apiId API Id
* @param response response of the AsyncAPI definition validation call
* @param organization identifier of the organization
* @return updated AsyncAPI definition
* @throws APIManagementException when error occurred updating AsyncAPI definition
* @throws FaultGatewaysException when error occurred publishing API to the gateway
*/
public static String updateAsyncAPIDefinition(String apiId, APIDefinitionValidationResponse response, String organization) throws APIManagementException, FaultGatewaysException {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// this will fall if user does not have access to the API or the API does not exist
API existingAPI = apiProvider.getAPIbyUUID(apiId, organization);
existingAPI.setOrganization(organization);
String apiDefinition = response.getJsonContent();
AsyncApiParser asyncApiParser = new AsyncApiParser();
// Set uri templates
Set<URITemplate> uriTemplates = asyncApiParser.getURITemplates(apiDefinition, APIConstants.API_TYPE_WS.equals(existingAPI.getType()) || !APIConstants.WSO2_GATEWAY_ENVIRONMENT.equals(existingAPI.getGatewayVendor()));
if (uriTemplates == null || uriTemplates.isEmpty()) {
throw new APIManagementException(ExceptionCodes.NO_RESOURCES_FOUND);
}
existingAPI.setUriTemplates(uriTemplates);
// Update ws uri mapping
existingAPI.setWsUriMapping(asyncApiParser.buildWSUriMapping(apiDefinition));
// updating APi with the new AsyncAPI definition
existingAPI.setAsyncApiDefinition(apiDefinition);
apiProvider.saveAsyncApiDefinition(existingAPI, apiDefinition);
apiProvider.updateAPI(existingAPI);
// retrieves the updated AsyncAPI definition
return apiProvider.getAsyncAPIDefinition(existingAPI.getId().getUUID(), organization);
}
use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition in project carbon-apimgt by wso2.
the class PublisherCommonUtils method updateSwagger.
/**
* update swagger definition of the given api.
*
* @param apiId API Id
* @param response response of a swagger definition validation call
* @param organization Organization Identifier
* @return updated swagger definition
* @throws APIManagementException when error occurred updating swagger
* @throws FaultGatewaysException when error occurred publishing API to the gateway
*/
public static String updateSwagger(String apiId, APIDefinitionValidationResponse response, boolean isServiceAPI, String organization) throws APIManagementException, FaultGatewaysException {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// this will fail if user does not have access to the API or the API does not exist
API existingAPI = apiProvider.getAPIbyUUID(apiId, organization);
APIDefinition oasParser = response.getParser();
String apiDefinition = response.getJsonContent();
if (isServiceAPI) {
apiDefinition = oasParser.copyVendorExtensions(existingAPI.getSwaggerDefinition(), apiDefinition);
} else {
apiDefinition = OASParserUtil.preProcess(apiDefinition);
}
if (APIConstants.API_TYPE_SOAPTOREST.equals(existingAPI.getType())) {
List<SOAPToRestSequence> sequenceList = SequenceGenerator.generateSequencesFromSwagger(apiDefinition);
existingAPI.setSoapToRestSequences(sequenceList);
}
Set<URITemplate> uriTemplates = null;
uriTemplates = oasParser.getURITemplates(apiDefinition);
if (uriTemplates == null || uriTemplates.isEmpty()) {
throw new APIManagementException(ExceptionCodes.NO_RESOURCES_FOUND);
}
Set<org.wso2.carbon.apimgt.api.model.Scope> scopes = oasParser.getScopes(apiDefinition);
// validating scope roles
for (org.wso2.carbon.apimgt.api.model.Scope scope : scopes) {
String roles = scope.getRoles();
if (roles != null) {
for (String aRole : roles.split(",")) {
boolean isValidRole = APIUtil.isRoleNameExist(RestApiCommonUtil.getLoggedInUsername(), aRole);
if (!isValidRole) {
throw new APIManagementException("Role '" + aRole + "' Does not exist.");
}
}
}
}
List<APIResource> removedProductResources = apiProvider.getRemovedProductResources(uriTemplates, existingAPI);
if (!removedProductResources.isEmpty()) {
throw new APIManagementException("Cannot remove following resource paths " + removedProductResources.toString() + " because they are used by one or more API Products", ExceptionCodes.from(ExceptionCodes.API_PRODUCT_USED_RESOURCES, existingAPI.getId().getApiName(), existingAPI.getId().getVersion()));
}
// set existing operation policies to URI templates
apiProvider.setOperationPoliciesToURITemplates(apiId, uriTemplates);
existingAPI.setUriTemplates(uriTemplates);
existingAPI.setScopes(scopes);
PublisherCommonUtils.validateScopes(existingAPI);
// Update API is called to update URITemplates and scopes of the API
SwaggerData swaggerData = new SwaggerData(existingAPI);
String updatedApiDefinition = oasParser.populateCustomManagementInfo(apiDefinition, swaggerData);
apiProvider.saveSwaggerDefinition(existingAPI, updatedApiDefinition, organization);
existingAPI.setSwaggerDefinition(updatedApiDefinition);
API unModifiedAPI = apiProvider.getAPIbyUUID(apiId, organization);
existingAPI.setStatus(unModifiedAPI.getStatus());
apiProvider.updateAPI(existingAPI, unModifiedAPI);
// retrieves the updated swagger definition
// TODO see why we need to get it
String apiSwagger = apiProvider.getOpenAPIDefinition(apiId, organization);
// instead of passing same
return oasParser.getOASDefinitionForPublisher(existingAPI, apiSwagger);
}
Aggregations