Search in sources :

Example 36 with UriTemplate

use of org.wso2.carbon.apimgt.core.models.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);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ArrayList(java.util.ArrayList) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) CompositeAPIEndpointDTO(org.wso2.carbon.apimgt.core.template.dto.CompositeAPIEndpointDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) TemplateBuilderDTO(org.wso2.carbon.apimgt.core.template.dto.TemplateBuilderDTO) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

Example 37 with UriTemplate

use of org.wso2.carbon.apimgt.core.models.UriTemplate in project carbon-apimgt by wso2.

the class ApiDAOImplIT method addAPIWithGivenData.

/**
 * This method adds an API with given information
 *
 * @param apiName                API name
 * @param apiVersion             API version
 * @param apiContext             API context
 * @param apiProvider            API provider
 * @param apiVisibility          API visibility
 * @param visibleRoles           roles that are eligible to consume the API
 * @param initialLifecycleStatus initial lifecycle status
 * @param description            API description
 * @param tags                   tag list for the API
 * @param uriTemplates           URI templates, i.e - resources
 * @param finalLifecycleStatus   final lifecycle status
 * @throws APIMgtDAOException if it fails to add the API
 */
private void addAPIWithGivenData(String apiName, String apiVersion, String apiContext, String apiProvider, API.Visibility apiVisibility, Set<String> visibleRoles, String initialLifecycleStatus, String description, Set<String> tags, Map<String, UriTemplate> uriTemplates, String finalLifecycleStatus) throws APIMgtDAOException {
    API.APIBuilder builder;
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    builder = SampleTestObjectCreator.createCustomAPI(apiName, apiVersion, apiContext);
    builder.provider(apiProvider);
    builder.createdBy(apiProvider);
    builder.visibility(apiVisibility);
    // visible roles should be added for restricted APIs
    if (apiVisibility != null && API.Visibility.RESTRICTED.toString().equalsIgnoreCase(apiVisibility.toString())) {
        builder.visibleRoles(visibleRoles);
    }
    builder.lifeCycleStatus(initialLifecycleStatus);
    builder.description(description);
    builder.tags(tags);
    builder.uriTemplates(uriTemplates);
    builder.endpoint(Collections.emptyMap());
    API api = builder.build();
    apiDAO.addAPI(api);
    apiDAO.changeLifeCycleStatus(api.getId(), finalLifecycleStatus);
}
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)

Example 38 with UriTemplate

use of org.wso2.carbon.apimgt.core.models.UriTemplate in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetResourcesOfApi.

@Test
public void testGetResourcesOfApi() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    API api = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api);
    List<UriTemplate> uriTemplateList = apiDAO.getResourcesOfApi(api.getContext(), api.getVersion());
    for (Map.Entry<String, UriTemplate> entry : api.getUriTemplates().entrySet()) {
        UriTemplate uriTemplate = entry.getValue();
        for (UriTemplate v : uriTemplateList) {
            if (v.getTemplateId().equals(uriTemplate.getTemplateId())) {
                Assert.assertEquals(uriTemplate.getAuthType(), v.getAuthType(), TestUtil.printDiff(uriTemplate.getAuthType(), v.getAuthType()));
                Assert.assertEquals(uriTemplate.getHttpVerb(), v.getHttpVerb(), TestUtil.printDiff(uriTemplate.getHttpVerb(), v.getHttpVerb()));
                Assert.assertEquals(uriTemplate.getUriTemplate(), v.getUriTemplate(), TestUtil.printDiff(uriTemplate.getUriTemplate(), v.getUriTemplate()));
                Assert.assertEquals(uriTemplate.getPolicy().getPolicyName(), policyDAO.getApiPolicyByUuid(v.getPolicy().getUuid()).getPolicyName(), TestUtil.printDiff(uriTemplate.getPolicy().getPolicyName(), policyDAO.getApiPolicyByUuid(v.getPolicy().getUuid()).getPolicyName()));
            }
        }
    }
}
Also used : CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) HashMap(java.util.HashMap) Map(java.util.Map) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 39 with UriTemplate

use of org.wso2.carbon.apimgt.core.models.UriTemplate in project carbon-apimgt by wso2.

the class ApiDAOImplIT method createAPIsAndGetIDsOfAddedAPIs.

/**
 * This method creates few APIs and returns the ID list of those APIs
 *
 * @return the ID list of added APIs
 * @throws APIMgtDAOException if it fails to creates APIs
 */
private List<String> createAPIsAndGetIDsOfAddedAPIs() throws APIMgtDAOException {
    Set<String> visibleRoles = new HashSet<>();
    Set<String> apiTags = new HashSet<>();
    List<String> apiIDList = new ArrayList<>();
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    Map<String, UriTemplate> uriTemplateMap;
    // Construct an API which has public visibility
    apiTags.add("Car");
    apiTags.add("Van");
    uriTemplateMap = getUriTemplateMap(new String[] { "/toyota", "/nissan" });
    addAPIWithGivenData("PublicAPI", "1.2.3", "PublicContext", "Paul", API.Visibility.PUBLIC, null, APIStatus.CREATED.getStatus(), "This is a public API, visible to all.", apiTags, uriTemplateMap, APIStatus.PUBLISHED.getStatus());
    visibleRoles.clear();
    apiTags.clear();
    uriTemplateMap.clear();
    apiIDList.add(apiDAO.getAPIs(new HashSet<String>(), "Paul").get(0).getId());
    // Construct an API which is visible to manager role only
    apiTags.add("Pizza");
    apiTags.add("Cake");
    uriTemplateMap = getUriTemplateMap(new String[] { "/pizzahut", "/dominos" });
    visibleRoles.add(MANAGER_ROLE);
    addAPIWithGivenData("ManagerOnlyAPI", "2.3.4", "managerContext", "Mark", API.Visibility.RESTRICTED, visibleRoles, APIStatus.CREATED.getStatus(), "Users with manager role can view this API.", apiTags, uriTemplateMap, APIStatus.PUBLISHED.getStatus());
    visibleRoles.clear();
    apiTags.clear();
    uriTemplateMap.clear();
    apiIDList.add(apiDAO.getAPIs(new HashSet<String>(), "Mark").get(0).getId());
    // Construct an API which is visible to admin and manager roles
    apiTags.add("Java");
    uriTemplateMap = getUriTemplateMap(new String[] { "/desktop", "/laptop", "nikoncam" });
    visibleRoles.add(ADMIN);
    visibleRoles.add(MANAGER_ROLE);
    addAPIWithGivenData("AdminManagerAPI", "3.4.5", "adminManager", "Alex", API.Visibility.RESTRICTED, visibleRoles, APIStatus.CREATED.getStatus(), "Admin and manager can see this API.", apiTags, uriTemplateMap, APIStatus.PUBLISHED.getStatus());
    visibleRoles.clear();
    apiTags.clear();
    uriTemplateMap.clear();
    apiIDList.add(apiDAO.getAPIs(new HashSet<String>(), "Alex").get(0).getId());
    // Construct an API in created state, this should not be shown in store
    apiTags.add("Movie");
    apiTags.add("TV");
    uriTemplateMap = getUriTemplateMap(new String[] { "/cnn", "/bbc" });
    addAPIWithGivenData("CreatedStateAPI", "4.5.6", "createdContext", "Colin", API.Visibility.PUBLIC, null, APIStatus.CREATED.getStatus(), "This API is in created state. Should not be shown in store.", apiTags, uriTemplateMap, APIStatus.CREATED.getStatus());
    visibleRoles.clear();
    apiTags.clear();
    uriTemplateMap.clear();
    apiIDList.add(apiDAO.getAPIs(new HashSet<String>(), "Colin").get(0).getId());
    // Construct an API which is visible to employee role only
    apiTags.add("Salary");
    apiTags.add("Bonus");
    uriTemplateMap = getUriTemplateMap(new String[] { "/cash", "/cheque" });
    visibleRoles.add(EMPLOYEE_ROLE);
    addAPIWithGivenData("EmployeeAPI", "5.6.7", "employeeCtx", "Emma", API.Visibility.RESTRICTED, visibleRoles, APIStatus.CREATED.getStatus(), "API for Employees.", apiTags, uriTemplateMap, APIStatus.PUBLISHED.getStatus());
    visibleRoles.clear();
    apiTags.clear();
    uriTemplateMap.clear();
    apiIDList.add(apiDAO.getAPIs(new HashSet<String>(), "Emma").get(0).getId());
    // Construct an API which is visible to all roles, except admin role
    apiTags.add("Science");
    apiTags.add("Technology");
    uriTemplateMap = getUriTemplateMap(new String[] { "/velocity", "/distance" });
    visibleRoles.add(EMPLOYEE_ROLE);
    visibleRoles.add(MANAGER_ROLE);
    visibleRoles.add(CUSTOMER_ROLE);
    addAPIWithGivenData("NonAdminAPI", "6.7.8", "nonAdmin", "Nancy", API.Visibility.RESTRICTED, visibleRoles, APIStatus.CREATED.getStatus(), "This API should be visible to all roles, except admin role.", apiTags, uriTemplateMap, APIStatus.PROTOTYPED.getStatus());
    visibleRoles.clear();
    apiTags.clear();
    uriTemplateMap.clear();
    apiIDList.add(apiDAO.getAPIs(new HashSet<String>(), "Nancy").get(0).getId());
    return apiIDList;
}
Also used : ArrayList(java.util.ArrayList) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) HashSet(java.util.HashSet)

Example 40 with UriTemplate

use of org.wso2.carbon.apimgt.core.models.UriTemplate in project carbon-apimgt by wso2.

the class MappingUtil method toAPIDto.

/**
 * This method converts the API Object from models into APIDTO object.
 *
 * @param api API object
 * @return APIDTO object with provided API object
 */
public static APIDTO toAPIDto(API api) throws IOException {
    APIDTO apidto = new APIDTO();
    apidto.setId(api.getId());
    apidto.setName(api.getName());
    apidto.version(api.getVersion());
    apidto.setContext(api.getContext());
    apidto.setDescription(api.getDescription());
    apidto.setIsDefaultVersion(api.isDefaultVersion());
    apidto.setVisibility(APIDTO.VisibilityEnum.valueOf(api.getVisibility().toString()));
    apidto.setResponseCaching(Boolean.toString(api.isResponseCachingEnabled()));
    apidto.setCacheTimeout(api.getCacheTimeout());
    apidto.setVisibleRoles(new ArrayList<>(api.getVisibleRoles()));
    apidto.setProvider(api.getProvider());
    apidto.setPermission(api.getApiPermission());
    apidto.setLifeCycleStatus(api.getLifeCycleStatus());
    apidto.setWorkflowStatus(api.getWorkflowStatus());
    apidto.setTags(new ArrayList<>(api.getTags()));
    apidto.hasOwnGateway(api.hasOwnGateway());
    apidto.setLabels(new ArrayList<>(api.getLabels()));
    apidto.setTransport(new ArrayList<>(api.getTransport()));
    apidto.setUserPermissionsForApi(api.getUserSpecificApiPermissions());
    apidto.setSecurityScheme(mapSecuritySchemeIntToList(api.getSecurityScheme()));
    for (Policy policy : api.getPolicies()) {
        apidto.addPoliciesItem(policy.getPolicyName());
    }
    apidto.setScopes(api.getScopes());
    BusinessInformation businessInformation = api.getBusinessInformation();
    API_businessInformationDTO apiBusinessInformationDTO = new API_businessInformationDTO();
    apiBusinessInformationDTO.setBusinessOwner(businessInformation.getBusinessOwner());
    apiBusinessInformationDTO.setBusinessOwnerEmail(businessInformation.getBusinessOwnerEmail());
    apiBusinessInformationDTO.setTechnicalOwner(businessInformation.getTechnicalOwner());
    apiBusinessInformationDTO.setTechnicalOwnerEmail(businessInformation.getTechnicalOwnerEmail());
    apidto.setBusinessInformation(apiBusinessInformationDTO);
    CorsConfiguration corsConfiguration = api.getCorsConfiguration();
    API_corsConfigurationDTO apiCorsConfigurationDTO = new API_corsConfigurationDTO();
    apiCorsConfigurationDTO.setAccessControlAllowCredentials(corsConfiguration.isAllowCredentials());
    apiCorsConfigurationDTO.setAccessControlAllowHeaders(corsConfiguration.getAllowHeaders());
    apiCorsConfigurationDTO.setAccessControlAllowMethods(corsConfiguration.getAllowMethods());
    apiCorsConfigurationDTO.setAccessControlAllowOrigins(corsConfiguration.getAllowOrigins());
    apiCorsConfigurationDTO.setCorsConfigurationEnabled(corsConfiguration.isEnabled());
    apidto.setCorsConfiguration(apiCorsConfigurationDTO);
    apidto.setEndpoint(fromEndpointToList(api.getEndpoint()));
    for (UriTemplate uriTemplate : api.getUriTemplates().values()) {
        API_operationsDTO apiOperationsDTO = new API_operationsDTO();
        apiOperationsDTO.setId(uriTemplate.getTemplateId());
        apiOperationsDTO.setUritemplate(uriTemplate.getUriTemplate());
        apiOperationsDTO.setAuthType(uriTemplate.getAuthType());
        apiOperationsDTO.setEndpoint(fromEndpointToList(uriTemplate.getEndpoint()));
        apiOperationsDTO.setHttpVerb(uriTemplate.getHttpVerb());
        apiOperationsDTO.setPolicy(uriTemplate.getPolicy().getPolicyName());
        apiOperationsDTO.setScopes(uriTemplate.getScopes());
        apidto.addOperationsItem(apiOperationsDTO);
    }
    if (api.getApiPolicy() != null) {
        apidto.setApiPolicy(api.getApiPolicy().getPolicyName());
    }
    apidto.setCreatedTime(api.getCreatedTime().toString());
    apidto.setLastUpdatedTime(api.getLastUpdatedTime().toString());
    if (api.getThreatProtectionPolicies() != null) {
        List<String> policyIdList = new ArrayList<>(api.getThreatProtectionPolicies());
        List<API_threatProtectionPolicies_listDTO> listDTOS = new ArrayList<>();
        for (String policyId : policyIdList) {
            API_threatProtectionPolicies_listDTO threatProtectionPoliciesListDTO = new API_threatProtectionPolicies_listDTO();
            threatProtectionPoliciesListDTO.setPolicyId(policyId);
            // set priority when implementing
            // threatProtectionPoliciesListDTO.setPriority(10);
            listDTOS.add(threatProtectionPoliciesListDTO);
        }
        API_threatProtectionPoliciesDTO threatProtectionPoliciesDTO = new API_threatProtectionPoliciesDTO();
        threatProtectionPoliciesDTO.setList(listDTOS);
        apidto.setThreatProtectionPolicies(threatProtectionPoliciesDTO);
    }
    return apidto;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) ArrayList(java.util.ArrayList) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) API_threatProtectionPolicies_listDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_threatProtectionPolicies_listDTO) API_operationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_operationsDTO) APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.APIDTO) API_threatProtectionPoliciesDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_threatProtectionPoliciesDTO) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) API_businessInformationDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_businessInformationDTO) API_corsConfigurationDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_corsConfigurationDTO)

Aggregations

UriTemplate (org.wso2.carbon.apimgt.core.models.UriTemplate)38 HashMap (java.util.HashMap)30 API (org.wso2.carbon.apimgt.core.models.API)22 Test (org.testng.annotations.Test)15 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)13 ArrayList (java.util.ArrayList)11 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)11 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)10 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)9 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)8 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)8 Map (java.util.Map)7 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)7 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)7 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)7 LifecycleState (org.wso2.carbon.lcm.core.impl.LifecycleState)7 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)6 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)6 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)6 Swagger (io.swagger.models.Swagger)5