Search in sources :

Example 16 with Application

use of org.wso2.carbon.identity.application.authentication.framework.model.Application in project carbon-apimgt by wso2.

the class SystemApplicationDaoImpl method getConsumerKeyForApplication.

@Override
public String getConsumerKeyForApplication(String appName) throws APIMgtDAOException {
    final String query = "SELECT CONSUMER_KEY FROM AM_SYSTEM_APPS WHERE NAME = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, appName);
        log.debug("Executing query: {} ", query);
        try (ResultSet resultSet = statement.executeQuery()) {
            if (resultSet.next()) {
                return resultSet.getString("CONSUMER_KEY");
            } else {
                throw new APIMgtDAOException("System Application with " + appName + " does not exist", ExceptionCodes.SYSTEM_APP_NOT_FOUND);
            }
        }
    } catch (SQLException e) {
        String errorMsg = "Error while creating database connection/prepared-statement";
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 17 with Application

use of org.wso2.carbon.identity.application.authentication.framework.model.Application in project carbon-apimgt by wso2.

the class SystemApplicationDaoImpl method addApplicationKey.

@Override
public void addApplicationKey(String appName, String consumerKey) throws APIMgtDAOException {
    final String query = "INSERT INTO AM_SYSTEM_APPS (NAME,CONSUMER_KEY,CREATED_TIME) VALUES (?,?,?)";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        connection.setAutoCommit(false);
        try {
            statement.setString(1, appName);
            statement.setString(2, consumerKey);
            statement.setTimestamp(3, Timestamp.valueOf(LocalDateTime.now()), Calendar.getInstance(TimeZone.getTimeZone("UTC")));
            log.debug("Executing query: {} ", query);
            statement.execute();
            connection.commit();
        } catch (SQLException ex) {
            connection.rollback();
            throw new APIMgtDAOException("Couldn't Create System Application", ex);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String errorMsg = "Error while creating database connection/prepared-statement";
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 18 with Application

use of org.wso2.carbon.identity.application.authentication.framework.model.Application in project carbon-apimgt by wso2.

the class PolicyDAOImpl method addApplicationPolicy.

@Override
public void addApplicationPolicy(ApplicationPolicy policy) throws APIMgtDAOException {
    try (Connection connection = DAOUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            addApplicationPolicy(policy, connection);
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String errorMessage = "Error in adding Application policy, policy name: " + policy.getPolicyName();
            log.error(errorMessage, e);
            throw new APIMgtDAOException(errorMessage, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String errorMsg = "Error in obtaining DB connection";
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 19 with Application

use of org.wso2.carbon.identity.application.authentication.framework.model.Application in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImpl method addApplicationPolicy.

@Override
public String addApplicationPolicy(ApplicationPolicy policy) throws APIManagementException {
    try {
        String policyUuid = policy.getUuid();
        if (policyUuid == null) {
            if (log.isDebugEnabled()) {
                log.debug("Policy id is null, hence generating a new UUID for the policy with name: " + policy.getPolicyName());
            }
            policyUuid = UUID.randomUUID().toString();
            policy.setUuid(policyUuid);
        }
        policyDAO.addApplicationPolicy(policy);
        PolicyValidationData policyValidationData = new PolicyValidationData(policyUuid, policy.getPolicyName(), false);
        apiGateway.addPolicy(policyValidationData);
        return policyUuid;
    } catch (APIMgtDAOException e) {
        String errorMessage = "Couldn't add Application for uuid: " + policy.getUuid();
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData)

Example 20 with Application

use of org.wso2.carbon.identity.application.authentication.framework.model.Application in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImpl method updateApplicationPolicy.

@Override
public void updateApplicationPolicy(ApplicationPolicy policy) throws APIManagementException {
    try {
        policyDAO.updateApplicationPolicy(policy);
        PolicyValidationData policyValidationData = new PolicyValidationData(policy.getUuid(), policy.getPolicyName(), false);
        apiGateway.updatePolicy(policyValidationData);
    } catch (APIMgtDAOException e) {
        String errorMessage = "Couldn't update Application policy for uuid: " + policy.getUuid();
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData)

Aggregations

Test (org.testng.annotations.Test)266 ArrayList (java.util.ArrayList)173 SQLException (java.sql.SQLException)172 PreparedStatement (java.sql.PreparedStatement)169 Connection (java.sql.Connection)148 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)145 ResultSet (java.sql.ResultSet)135 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)131 Application (org.wso2.carbon.apimgt.api.model.Application)130 HashMap (java.util.HashMap)122 Application (org.wso2.carbon.apimgt.core.models.Application)121 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)110 Test (org.junit.Test)102 IOException (java.io.IOException)74 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)73 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)71 JSONObject (org.json.simple.JSONObject)68 Response (javax.ws.rs.core.Response)65 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)63 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)63