Search in sources :

Example 21 with Resources

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.

the class SOAPOperationBindingUtils method getSoapOperationMapping.

/**
 * Gets soap operations to rest resources mapping for a wsdl byte content
 *
 * @param wsdlContent WSDL byte content
 * @return swagger json string with the soap operation mapping
 * @throws APIManagementException if an error occurs when generating swagger
 */
public static String getSoapOperationMapping(byte[] wsdlContent) throws APIManagementException {
    WSDL11SOAPOperationExtractor processor = APIMWSDLReader.getWSDLSOAPOperationExtractor(wsdlContent);
    WSDLInfo wsdlInfo = processor.getWsdlInfo();
    return getGeneratedSwaggerFromWSDL(wsdlInfo);
}
Also used : WSDLInfo(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLInfo) WSDL11SOAPOperationExtractor(org.wso2.carbon.apimgt.impl.wsdl.WSDL11SOAPOperationExtractor)

Example 22 with Resources

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.

the class SequenceUtils method getRestToSoapConvertedSequence.

/**
 * Gets soap to rest converted sequence from the registry
 * <p>
 * Note: this method is directly invoked from the jaggery layer
 *
 * @param api API
 * @param seqType  to identify the sequence is whether in/out sequence
 * @return converted sequences string for a given operation
 * @throws APIManagementException throws exceptions on unsuccessful retrieval of resources in registry
 */
public static String getRestToSoapConvertedSequence(API api, String seqType) throws APIManagementException {
    JSONObject resultJson = new JSONObject();
    List<SOAPToRestSequence> sequences = api.getSoapToRestSequences();
    if (sequences == null) {
        handleException("Cannot find any resource policies for the api " + api.getUuid());
    }
    for (SOAPToRestSequence sequence : sequences) {
        if (sequence.getDirection().toString().equalsIgnoreCase(seqType)) {
            String content = sequence.getContent();
            String resourceName = sequence.getPath();
            String httpMethod = sequence.getMethod();
            Map<String, String> resourceMap = new HashMap<>();
            resourceMap.put(SOAPToRESTConstants.RESOURCE_ID, sequence.getUuid());
            resourceMap.put(SOAPToRESTConstants.METHOD, httpMethod);
            resourceMap.put(SOAPToRESTConstants.CONTENT, content);
            resultJson.put(resourceName + "_" + httpMethod, resourceMap);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Saved sequence for type " + seqType + " for api:" + api.getId().getProviderName() + "-" + api.getId().getApiName() + "-" + api.getId().getVersion() + " is: " + resultJson.toJSONString());
    }
    return resultJson.toJSONString();
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)

Example 23 with Resources

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.

the class ServiceCatalogDAO method getServiceByNameAndVersion.

/**
 * Get service information by name and version
 *
 * @param name          Service name
 * @param version       Service version
 * @param tenantId     ID of the owner's tenant
 * @return ServiceEntry
 * throws APIManagementException if failed to retrieve
 */
public ServiceEntry getServiceByNameAndVersion(String name, String version, int tenantId) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement ps = connection.prepareStatement(SQLConstants.ServiceCatalogConstants.GET_SERVICE_BY_NAME_AND_VERSION)) {
        ps.setString(1, name);
        ps.setString(2, version);
        ps.setInt(3, tenantId);
        try (ResultSet rs = ps.executeQuery()) {
            if (rs.next()) {
                ServiceEntry serviceEntry = getServiceParams(rs, false);
                return serviceEntry;
            }
        }
    } catch (SQLException e) {
        handleException("Error while executing SQL for getting catalog entry resources", e);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry)

Example 24 with Resources

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetSwaggerDefinitionTimeStamps.

@Test
public void testGetSwaggerDefinitionTimeStamps() throws Exception {
    APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    UserRegistry registry = Mockito.mock(UserRegistry.class);
    Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(-1234);
    PowerMockito.mockStatic(OASParserUtil.class);
    Mockito.when(registryService.getGovernanceUserRegistry(Mockito.anyString(), Mockito.anyInt())).thenThrow(RegistryException.class).thenReturn(registry);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(null, registryService, registry, tenantManager);
    Assert.assertNull(abstractAPIManager.getSwaggerDefinitionTimeStamps(identifier));
    Assert.assertNull(abstractAPIManager.getSwaggerDefinitionTimeStamps(identifier));
    abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
    Map<String, String> result = new HashMap<String, String>();
    result.put("swagger1", "scopes:apim_create,resources:{get:/*}");
    result.put("swagger2", "scopes:apim_view,resources:{get:/menu}");
// Mockito.when(apiDefinitionFromOpenAPISpec.getAPIOpenAPIDefinitionTimeStamps((APIIdentifier) Mockito.any(),
// (org.wso2.carbon.registry.api.Registry) Mockito.any())).thenReturn(result);
// Assert.assertEquals(abstractAPIManager.getSwaggerDefinitionTimeStamps(identifier).size(),2);
// abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN;
// result.put("swagger3","");
// Assert.assertEquals(abstractAPIManager.getSwaggerDefinitionTimeStamps(identifier).size(),3);
}
Also used : HashMap(java.util.HashMap) UserStoreException(org.wso2.carbon.user.core.UserStoreException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 25 with Resources

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.

the class APIProviderImpl method addAPIProductWithoutPublishingToGateway.

@Override
public Map<API, List<APIProductResource>> addAPIProductWithoutPublishingToGateway(APIProduct product) throws APIManagementException {
    Map<API, List<APIProductResource>> apiToProductResourceMapping = new HashMap<>();
    validateApiProductInfo(product);
    String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(product.getId().getProviderName()));
    if (log.isDebugEnabled()) {
        log.debug("API Product details successfully added to the registry. API Product Name: " + product.getId().getName() + ", API Product Version : " + product.getId().getVersion() + ", API Product context : " + // todo: log context
        "change");
    }
    List<APIProductResource> resources = product.getProductResources();
    // list to hold resources which are actually in an existing api. If user has created an API product with invalid
    // API or invalid resource of a valid API, that content will be removed .validResources array will have only
    // legitimate apis
    List<APIProductResource> validResources = new ArrayList<APIProductResource>();
    for (APIProductResource apiProductResource : resources) {
        API api;
        String apiUUID;
        if (apiProductResource.getProductIdentifier() != null) {
            APIIdentifier productAPIIdentifier = apiProductResource.getApiIdentifier();
            String emailReplacedAPIProviderName = APIUtil.replaceEmailDomain(productAPIIdentifier.getProviderName());
            APIIdentifier emailReplacedAPIIdentifier = new APIIdentifier(emailReplacedAPIProviderName, productAPIIdentifier.getApiName(), productAPIIdentifier.getVersion());
            apiUUID = apiMgtDAO.getUUIDFromIdentifier(emailReplacedAPIIdentifier, product.getOrganization());
            api = getAPIbyUUID(apiUUID, product.getOrganization());
        } else {
            apiUUID = apiProductResource.getApiId();
            api = getAPIbyUUID(apiUUID, product.getOrganization());
        // if API does not exist, getLightweightAPIByUUID() method throws exception.
        }
        if (api != null) {
            validateApiLifeCycleForApiProducts(api);
            if (api.getSwaggerDefinition() != null) {
                api.setSwaggerDefinition(getOpenAPIDefinition(apiUUID, product.getOrganization()));
            }
            if (!apiToProductResourceMapping.containsKey(api)) {
                apiToProductResourceMapping.put(api, new ArrayList<>());
            }
            List<APIProductResource> apiProductResources = apiToProductResourceMapping.get(api);
            apiProductResources.add(apiProductResource);
            apiProductResource.setApiIdentifier(api.getId());
            apiProductResource.setProductIdentifier(product.getId());
            if (api.isAdvertiseOnly()) {
                apiProductResource.setEndpointConfig(APIUtil.generateEndpointConfigForAdvertiseOnlyApi(api));
            } else {
                apiProductResource.setEndpointConfig(api.getEndpointConfig());
            }
            apiProductResource.setEndpointSecurityMap(APIUtil.setEndpointSecurityForAPIProduct(api));
            URITemplate uriTemplate = apiProductResource.getUriTemplate();
            Map<String, URITemplate> templateMap = apiMgtDAO.getURITemplatesForAPI(api);
            if (uriTemplate == null) {
            // if no resources are define for the API, we ingore that api for the product
            } else {
                String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getResourceURI();
                if (templateMap.containsKey(key)) {
                    // Since the template ID is not set from the request, we manually set it.
                    uriTemplate.setId(templateMap.get(key).getId());
                    // request has a valid API id and a valid resource. we add it to valid resource map
                    validResources.add(apiProductResource);
                } else {
                    // ignore
                    log.warn("API with id " + apiProductResource.getApiId() + " does not have a resource " + uriTemplate.getResourceURI() + " with http method " + uriTemplate.getHTTPVerb());
                }
            }
        }
    }
    // set the valid resources only
    product.setProductResources(validResources);
    // now we have validated APIs and it's resources inside the API product. Add it to database
    String provider = APIUtil.replaceEmailDomain(product.getId().getProviderName());
    // Set version timestamp
    product.setVersionTimestamp(String.valueOf(System.currentTimeMillis()));
    // Create registry artifact
    String apiProductUUID = createAPIProduct(product);
    product.setUuid(apiProductUUID);
    // Add to database
    apiMgtDAO.addAPIProduct(product, product.getOrganization());
    return apiToProductResourceMapping;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) ArrayList(java.util.ArrayList) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) ArrayList(java.util.ArrayList) List(java.util.List) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Aggregations

ArrayList (java.util.ArrayList)49 Test (org.testng.annotations.Test)41 HashMap (java.util.HashMap)30 File (java.io.File)26 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)24 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)21 IOException (java.io.IOException)20 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)20 FileInputStream (java.io.FileInputStream)19 Map (java.util.Map)17 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)16 JSONObject (org.json.simple.JSONObject)15 Resource (org.wso2.carbon.registry.core.Resource)15 List (java.util.List)14 Scope (org.wso2.carbon.apimgt.core.models.Scope)14 KeyManager (org.wso2.carbon.apimgt.core.api.KeyManager)13 API (org.wso2.carbon.apimgt.api.model.API)12 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)12 API (org.wso2.carbon.apimgt.core.models.API)12 Collection (org.wso2.carbon.registry.core.Collection)12