Search in sources :

Example 6 with KeyManagerConfigurationDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO in project carbon-apimgt by wso2.

the class ApiMgtDAO method getKeyManagerConfigurationByName.

private KeyManagerConfigurationDTO getKeyManagerConfigurationByName(Connection connection, String organization, String name) throws SQLException, IOException {
    final String query = "SELECT * FROM AM_KEY_MANAGER WHERE NAME = ? AND ORGANIZATION = ?";
    try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, name);
        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.setDisplayName(resultSet.getString("DISPLAY_NAME"));
                keyManagerConfigurationDTO.setDescription(resultSet.getString("DESCRIPTION"));
                keyManagerConfigurationDTO.setType(resultSet.getString("TYPE"));
                keyManagerConfigurationDTO.setEnabled(resultSet.getBoolean("ENABLED"));
                keyManagerConfigurationDTO.setOrganization(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);
                }
                return keyManagerConfigurationDTO;
            }
        }
    }
    return null;
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 7 with KeyManagerConfigurationDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO in project carbon-apimgt by wso2.

the class ApiMgtDAO method populateAppRegistrationWorkflowDTO.

public void populateAppRegistrationWorkflowDTO(ApplicationRegistrationWorkflowDTO workflowDTO) throws APIManagementException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    Application application = null;
    Subscriber subscriber = null;
    String registrationEntry = SQLConstants.GET_APPLICATION_REGISTRATION_ENTRY_BY_SUBSCRIBER_SQL;
    try {
        conn = APIMgtDBUtil.getConnection();
        ps = conn.prepareStatement(registrationEntry);
        ps.setString(1, workflowDTO.getExternalWorkflowReference());
        rs = ps.executeQuery();
        while (rs.next()) {
            subscriber = new Subscriber(rs.getString("USER_ID"));
            subscriber.setId(rs.getInt("SUBSCRIBER_ID"));
            application = new Application(rs.getString("NAME"), subscriber);
            application.setId(rs.getInt("APPLICATION_ID"));
            application.setUUID(rs.getString("UUID"));
            application.setTokenType(rs.getString("APP_TYPE"));
            application.setApplicationWorkFlowStatus(rs.getString("APPLICATION_STATUS"));
            application.setCallbackUrl(rs.getString("CALLBACK_URL"));
            application.setDescription(rs.getString("DESCRIPTION"));
            application.setTier(rs.getString("APPLICATION_TIER"));
            workflowDTO.setApplication(application);
            workflowDTO.setKeyType(rs.getString("TOKEN_TYPE"));
            workflowDTO.setUserName(subscriber.getName());
            workflowDTO.setDomainList(rs.getString("ALLOWED_DOMAINS"));
            workflowDTO.setValidityTime(rs.getLong("VALIDITY_PERIOD"));
            String tenantDomain = MultitenantUtils.getTenantDomain(subscriber.getName());
            String keyManagerUUID = rs.getString("KEY_MANAGER");
            workflowDTO.setKeyManager(keyManagerUUID);
            KeyManagerConfigurationDTO keyManagerConfigurationByUUID = getKeyManagerConfigurationByUUID(conn, keyManagerUUID);
            if (keyManagerConfigurationByUUID != null) {
                OAuthAppRequest request = ApplicationUtils.createOauthAppRequest(application.getName(), null, application.getCallbackUrl(), rs.getString("TOKEN_SCOPE"), rs.getString("INPUTS"), application.getTokenType(), keyManagerConfigurationByUUID.getOrganization(), keyManagerConfigurationByUUID.getName());
                request.setMappingId(workflowDTO.getWorkflowReference());
                request.getOAuthApplicationInfo().setApplicationUUID(application.getUUID());
                workflowDTO.setAppInfoDTO(request);
            } else {
                throw new APIManagementException("Error occured while finding the KeyManager from uuid " + keyManagerUUID + ".", ExceptionCodes.KEY_MANAGER_NOT_REGISTERED);
            }
        }
    } catch (SQLException | IOException e) {
        handleException("Error occurred while retrieving an " + "Application Registration Entry for Workflow : " + workflowDTO.getExternalWorkflowReference(), e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, rs);
    }
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 8 with KeyManagerConfigurationDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO in project carbon-apimgt by wso2.

the class ApiMgtDAO method getKeyManagerConfigurationByUUID.

private KeyManagerConfigurationDTO getKeyManagerConfigurationByUUID(Connection connection, String uuid) throws SQLException, IOException {
    final String query = "SELECT * FROM AM_KEY_MANAGER WHERE UUID = ?";
    try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, uuid);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            if (resultSet.next()) {
                KeyManagerConfigurationDTO keyManagerConfigurationDTO = new KeyManagerConfigurationDTO();
                keyManagerConfigurationDTO.setUuid(resultSet.getString("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);
                }
                return keyManagerConfigurationDTO;
            }
        }
    }
    return null;
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 9 with KeyManagerConfigurationDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO 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 10 with KeyManagerConfigurationDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO 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)

Aggregations

KeyManagerConfigurationDTO (org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)43 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)30 Gson (com.google.gson.Gson)16 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)12 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)11 JSONObject (org.json.simple.JSONObject)10 JsonObject (com.google.gson.JsonObject)9 PreparedStatement (java.sql.PreparedStatement)9 KeyManager (org.wso2.carbon.apimgt.api.model.KeyManager)9 OAuthAppRequest (org.wso2.carbon.apimgt.api.model.OAuthAppRequest)9 Map (java.util.Map)8 APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)8 APIAdminImpl (org.wso2.carbon.apimgt.impl.APIAdminImpl)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)7 IdentityProvider (org.wso2.carbon.identity.application.common.model.IdentityProvider)7 LinkedHashMap (java.util.LinkedHashMap)6 Application (org.wso2.carbon.apimgt.api.model.Application)6