Search in sources :

Example 66 with APIDefinition

use of org.wso2.carbon.apimgt.api.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());
}
Also used : CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway) Test(org.testng.annotations.Test)

Example 67 with APIDefinition

use of org.wso2.carbon.apimgt.api.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()));
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 68 with APIDefinition

use of org.wso2.carbon.apimgt.api.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;
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIDefinition(org.wso2.carbon.apimgt.core.api.APIDefinition) APIDefinitionFromSwagger20(org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20)

Example 69 with APIDefinition

use of org.wso2.carbon.apimgt.api.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);
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) API(org.wso2.carbon.apimgt.api.model.API) AsyncApiParser(org.wso2.carbon.apimgt.impl.definitions.AsyncApiParser) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 70 with APIDefinition

use of org.wso2.carbon.apimgt.api.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);
}
Also used : APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Scope(org.wso2.carbon.apimgt.rest.api.common.annotations.Scope) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) API(org.wso2.carbon.apimgt.api.model.API)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)32 HashMap (java.util.HashMap)30 APIDefinition (org.wso2.carbon.apimgt.api.APIDefinition)30 API (org.wso2.carbon.apimgt.core.models.API)30 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)25 ArrayList (java.util.ArrayList)23 API (org.wso2.carbon.apimgt.api.model.API)20 Map (java.util.Map)19 IOException (java.io.IOException)18 Test (org.testng.annotations.Test)18 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)18 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)16 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)15 Scope (org.wso2.carbon.apimgt.api.model.Scope)15 HashSet (java.util.HashSet)13 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)13 APIDefinitionValidationResponse (org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)12 SwaggerData (org.wso2.carbon.apimgt.api.model.SwaggerData)12 BusinessInformation (org.wso2.carbon.apimgt.core.models.BusinessInformation)12 CorsConfiguration (org.wso2.carbon.apimgt.core.models.CorsConfiguration)12