Search in sources :

Example 6 with Application

use of org.wso2.carbon.apimgt.keymgt.model.entity.Application in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method getApplicationByName.

/**
 * Retrieve a given instance of an Application
 *
 * @param appName Name of the Application
 * @param ownerId ID of the application owner.
 * @return valid {@link Application} object or null
 * @throws APIMgtDAOException   If failed to retrieve application.
 */
@Override
public Application getApplicationByName(String appName, String ownerId) throws APIMgtDAOException {
    final String completeGetAppQuery = GET_APPS_WITH_POLICY_QUERY + "AND APPLICATION.NAME = ? AND APPLICATION" + ".CREATED_BY = ?";
    Application application;
    try (Connection conn = DAOUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(completeGetAppQuery)) {
        ps.setString(1, appName);
        ps.setString(2, ownerId);
        try (ResultSet rs = ps.executeQuery()) {
            application = this.createApplicationFromResultSet(rs);
        }
        if (application == null) {
            throw new APIMgtDAOException("Application is not available in the system.", ExceptionCodes.APPLICATION_NOT_FOUND);
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting application(name: " + appName + ", " + "ownerId: " + ownerId + ")", ex);
    }
    return application;
}
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) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 7 with Application

use of org.wso2.carbon.apimgt.keymgt.model.entity.Application in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method getAllApplications.

@Override
public List<Application> getAllApplications() throws APIMgtDAOException {
    List<Application> applicationList = new ArrayList<>();
    try (Connection conn = DAOUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(GET_APPS_QUERY)) {
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                String createdUser = rs.getString("CREATED_BY");
                Application application = new Application(rs.getString("NAME"), createdUser);
                application.setId(rs.getString("UUID"));
                application.setStatus(rs.getString("APPLICATION_STATUS"));
                application.setPolicy(new ApplicationPolicy(rs.getString("APPLICATION_POLICY_ID"), ""));
                applicationList.add(application);
            }
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting all applications", ex);
    }
    return applicationList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 8 with Application

use of org.wso2.carbon.apimgt.keymgt.model.entity.Application in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method createApplicationFromResultSet.

private Application createApplicationFromResultSet(ResultSet rs) throws APIMgtDAOException, SQLException {
    Application application = null;
    if (rs.next()) {
        String createdUser = rs.getString("CREATED_BY");
        application = new Application(rs.getString("NAME"), createdUser);
        application.setId(rs.getString("UUID"));
        application.setDescription(rs.getString("DESCRIPTION"));
        application.setStatus(rs.getString("APPLICATION_STATUS"));
        application.setCreatedTime(rs.getTimestamp("CREATED_TIME").toLocalDateTime());
        application.setUpdatedUser(rs.getString("UPDATED_BY"));
        application.setUpdatedTime(rs.getTimestamp("LAST_UPDATED_TIME").toLocalDateTime());
        application.setPolicy(new ApplicationPolicy(rs.getString("APPLICATION_POLICY_NAME")));
    }
    return application;
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 9 with Application

use of org.wso2.carbon.apimgt.keymgt.model.entity.Application in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method addApplicationKeys.

@Override
public void addApplicationKeys(String appId, String keyType, String consumerKey) throws APIMgtDAOException {
    final String addApplicationKeysQuery = "INSERT INTO AM_APP_KEY_MAPPING (APPLICATION_ID, CLIENT_ID, KEY_TYPE) " + "VALUES (?, ?, ?)";
    try (Connection conn = DAOUtil.getConnection()) {
        conn.setAutoCommit(false);
        try (PreparedStatement ps = conn.prepareStatement(addApplicationKeysQuery)) {
            ps.setString(1, appId);
            ps.setString(2, consumerKey);
            ps.setString(3, keyType);
            ps.executeUpdate();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding application keys(appId: " + appId + ")", ex);
        } finally {
            conn.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding application keys(appId: " + appId + ")", ex);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 10 with Application

use of org.wso2.carbon.apimgt.keymgt.model.entity.Application in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method updateApplicationState.

@Override
public void updateApplicationState(String appID, String state) throws APIMgtDAOException {
    final String updateAppQuery = "UPDATE AM_APPLICATION SET APPLICATION_STATUS=?, LAST_UPDATED_TIME=?  " + "WHERE UUID = ?";
    try (Connection conn = DAOUtil.getConnection()) {
        conn.setAutoCommit(false);
        try (PreparedStatement ps = conn.prepareStatement(updateAppQuery)) {
            ps.setString(1, state);
            ps.setTimestamp(2, Timestamp.valueOf(LocalDateTime.now()));
            ps.setString(3, appID);
            ps.executeUpdate();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating application state(appId: " + appID + ", state: " + state + ")", ex);
        } finally {
            conn.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating application state(appId: " + appID + ", state: " + state + ")", ex);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Test (org.testng.annotations.Test)156 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)143 Application (org.wso2.carbon.apimgt.api.model.Application)130 Application (org.wso2.carbon.apimgt.core.models.Application)121 Test (org.junit.Test)102 ArrayList (java.util.ArrayList)98 SQLException (java.sql.SQLException)94 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)94 PreparedStatement (java.sql.PreparedStatement)88 Connection (java.sql.Connection)83 ResultSet (java.sql.ResultSet)73 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)71 HashMap (java.util.HashMap)70 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)63 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)63 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)61 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)60 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)59 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)58 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)57