Search in sources :

Example 76 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException 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 77 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.

the class SystemApplicationDaoImpl method isConsumerKeyExistForApplication.

@Override
public boolean isConsumerKeyExistForApplication(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 true;
            } else {
                return false;
            }
        }
    } 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 78 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException 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 79 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException 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 80 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.

the class APIPublisherImpl method removePendingLifecycleWorkflowTaskForAPI.

/**
 * {@inheritDoc}
 */
@Override
public void removePendingLifecycleWorkflowTaskForAPI(String apiId) throws APIManagementException {
    try {
        API api = getApiDAO().getAPI(apiId);
        if (APILCWorkflowStatus.PENDING.toString().equals(api.getWorkflowStatus())) {
            // change the state back
            getApiDAO().updateAPIWorkflowStatus(apiId, APILCWorkflowStatus.APPROVED);
            // call executor's cleanup task
            cleanupPendingTaskForAPIStateChange(apiId);
        } else {
            String msg = "API does not have a pending lifecycle state change.";
            log.error(msg);
            throw new APIManagementException(msg, ExceptionCodes.WORKFLOW_NO_PENDING_TASK);
        }
    } catch (APIMgtDAOException e) {
        String msg = "Error occurred while changing api lifecycle workflow status";
        log.error(msg, e);
        throw new APIManagementException(msg, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API)

Aggregations

APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)333 SQLException (java.sql.SQLException)190 Connection (java.sql.Connection)144 PreparedStatement (java.sql.PreparedStatement)127 Test (org.testng.annotations.Test)84 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)72 ResultSet (java.sql.ResultSet)70 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)57 API (org.wso2.carbon.apimgt.core.models.API)57 ArrayList (java.util.ArrayList)49 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)35 Application (org.wso2.carbon.apimgt.core.models.Application)24 HashMap (java.util.HashMap)23 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)22 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)21 BeforeTest (org.testng.annotations.BeforeTest)21 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)20 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)20 IOException (java.io.IOException)19 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)17