Search in sources :

Example 61 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class ApiMgtDAO method getKeyManagerConfigurations.

public List<KeyManagerConfigurationDTO> getKeyManagerConfigurations() throws APIManagementException {
    List<KeyManagerConfigurationDTO> keyManagerConfigurationDTOS = new ArrayList<>();
    final String query = "SELECT * FROM AM_KEY_MANAGER";
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(query)) {
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                KeyManagerConfigurationDTO keyManagerConfigurationDTO = new KeyManagerConfigurationDTO();
                String uuid = resultSet.getString("UUID");
                keyManagerConfigurationDTO.setUuid(uuid);
                keyManagerConfigurationDTO.setName(resultSet.getString("NAME"));
                keyManagerConfigurationDTO.setDisplayName(resultSet.getString("DISPLAY_NAME"));
                keyManagerConfigurationDTO.setDescription(resultSet.getString("DESCRIPTION"));
                keyManagerConfigurationDTO.setType(resultSet.getString("TYPE"));
                keyManagerConfigurationDTO.setEnabled(resultSet.getBoolean("ENABLED"));
                keyManagerConfigurationDTO.setOrganization(resultSet.getString("ORGANIZATION"));
                keyManagerConfigurationDTO.setTokenType(resultSet.getString("TOKEN_TYPE"));
                keyManagerConfigurationDTO.setExternalReferenceId(resultSet.getString("EXTERNAL_REFERENCE_ID"));
                try (InputStream configuration = resultSet.getBinaryStream("CONFIGURATION")) {
                    String configurationContent = IOUtils.toString(configuration);
                    Map map = new Gson().fromJson(configurationContent, Map.class);
                    keyManagerConfigurationDTO.setAdditionalProperties(map);
                } catch (IOException e) {
                    log.error("Error while converting configurations in " + uuid, e);
                }
                keyManagerConfigurationDTOS.add(keyManagerConfigurationDTO);
            }
        }
    } catch (SQLException e) {
        throw new APIManagementException("Error while retrieving all key manager configurations", e);
    }
    return keyManagerConfigurationDTOS;
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ResultSet(java.sql.ResultSet) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 62 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class ApiMgtDAO method getConsumerKeysForApplication.

/**
 * Retrieves the consumer keys and keymanager in a given application
 *
 * @param appId application id
 * @return Map<ConsumerKey, Pair<keyManagerName, keyManagerTenantDomain>
 * @throws APIManagementException
 */
public Map<String, Pair<String, String>> getConsumerKeysForApplication(int appId) throws APIManagementException {
    Map<String, Pair<String, String>> consumerKeysOfApplication = new HashMap<>();
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(SQLConstants.GET_CONSUMER_KEY_OF_APPLICATION_SQL)) {
        preparedStatement.setInt(1, appId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String consumerKey = resultSet.getString("CONSUMER_KEY");
                String keyManagerName = resultSet.getString("NAME");
                String keyManagerOrganization = resultSet.getString("ORGANIZATION");
                consumerKeysOfApplication.put(consumerKey, Pair.of(keyManagerName, keyManagerOrganization));
            }
        }
    } catch (SQLException e) {
        String msg = "Error occurred while getting consumer keys for application " + appId;
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    }
    return consumerKeysOfApplication;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Pair(org.apache.commons.lang3.tuple.Pair)

Example 63 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class ApiMgtDAO method getKeyManagerConfigurationByID.

public KeyManagerConfigurationDTO getKeyManagerConfigurationByID(String organization, String id) throws APIManagementException {
    final String query = "SELECT * FROM AM_KEY_MANAGER WHERE UUID = ? AND ORGANIZATION = ?";
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(query)) {
        preparedStatement.setString(1, id);
        preparedStatement.setString(2, organization);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            if (resultSet.next()) {
                KeyManagerConfigurationDTO keyManagerConfigurationDTO = new KeyManagerConfigurationDTO();
                String uuid = resultSet.getString("UUID");
                keyManagerConfigurationDTO.setUuid(uuid);
                keyManagerConfigurationDTO.setName(resultSet.getString("NAME"));
                keyManagerConfigurationDTO.setDescription(resultSet.getString("DESCRIPTION"));
                keyManagerConfigurationDTO.setType(resultSet.getString("TYPE"));
                keyManagerConfigurationDTO.setEnabled(resultSet.getBoolean("ENABLED"));
                keyManagerConfigurationDTO.setOrganization(organization);
                keyManagerConfigurationDTO.setDisplayName(resultSet.getString("DISPLAY_NAME"));
                keyManagerConfigurationDTO.setTokenType(resultSet.getString("TOKEN_TYPE"));
                keyManagerConfigurationDTO.setExternalReferenceId(resultSet.getString("EXTERNAL_REFERENCE_ID"));
                try (InputStream configuration = resultSet.getBinaryStream("CONFIGURATION")) {
                    String configurationContent = IOUtils.toString(configuration);
                    Map map = new Gson().fromJson(configurationContent, Map.class);
                    keyManagerConfigurationDTO.setAdditionalProperties(map);
                }
                return keyManagerConfigurationDTO;
            }
        }
    } catch (SQLException | IOException e) {
        throw new APIManagementException("Error while retrieving key manager configuration for " + id + " in organization " + organization, e);
    }
    return null;
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 64 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class ApiMgtDAO method isKeyManagerConfigurationExistById.

public boolean isKeyManagerConfigurationExistById(String organization, String id) throws APIManagementException {
    final String query = "SELECT 1 FROM AM_KEY_MANAGER WHERE UUID = ? AND ORGANIZATION = ?";
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(query)) {
        preparedStatement.setString(1, id);
        preparedStatement.setString(2, organization);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            if (resultSet.next()) {
                return true;
            }
        }
    } catch (SQLException e) {
        throw new APIManagementException("Error while retrieving key manager configuration for " + id + " in organization " + organization, e);
    }
    return false;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 65 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class APIProviderImplTest method testUpdateAPIProductForStateChange.

@Test
public void testUpdateAPIProductForStateChange() throws Exception {
    String provider = "admin";
    PowerMockito.mockStatic(MultitenantUtils.class);
    Mockito.when(MultitenantUtils.getTenantDomain(provider)).thenReturn(APIConstants.SUPER_TENANT_DOMAIN);
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
    APIProduct apiProduct = createMockAPIProduct(provider);
    prepareForLCStateChangeOfAPIProduct(apiProvider, apiProduct);
    PublisherAPIProduct publisherAPIProduct = APIProductMapper.INSTANCE.toPublisherApiProduct(apiProduct);
    Organization organization = new Organization(APIConstants.SUPER_TENANT_DOMAIN);
    Mockito.when(apiPersistenceInstance.updateAPIProduct(organization, publisherAPIProduct)).thenReturn(publisherAPIProduct);
    apiProvider.updateAPIProductForStateChange(apiProduct, APIConstants.CREATED, APIConstants.PUBLISHED);
}
Also used : PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)304 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)106 API (org.wso2.carbon.apimgt.api.model.API)100 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)89 ArrayList (java.util.ArrayList)79 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)72 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)70 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)65 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)64 IOException (java.io.IOException)61 Registry (org.wso2.carbon.registry.core.Registry)58 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)57 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)56 HashMap (java.util.HashMap)54 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)53 Resource (org.wso2.carbon.registry.core.Resource)51 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)49 JSONObject (org.json.simple.JSONObject)45 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)44 URISyntaxException (java.net.URISyntaxException)42