Search in sources :

Example 1 with Application

use of org.wso2.carbon.apimgt.api.model.subscription.Application in project carbon-apimgt by wso2.

the class APISubscriptionDAOImpl method createSubscriptionWithApiAndAppInformation.

private Subscription createSubscriptionWithApiAndAppInformation(ResultSet rs) throws APIMgtDAOException {
    Subscription subscription = null;
    try {
        if (rs.next()) {
            String subscriptionId = rs.getString("SUBS_UUID");
            String subscriptionTier = rs.getString("SUBS_POLICY");
            String subscriptionPolicyId = rs.getString("SUBS_POLICY_ID");
            API.APIBuilder apiBuilder = new API.APIBuilder(rs.getString("API_PROVIDER"), rs.getString("API_NAME"), rs.getString("API_VERSION"));
            apiBuilder.id(rs.getString("API_ID"));
            apiBuilder.context(rs.getString("API_CONTEXT"));
            API api = apiBuilder.build();
            Application app = new Application(rs.getString("APP_NAME"), rs.getString("APP_OWNER"));
            app.setId(rs.getString("APP_ID"));
            app.setStatus(rs.getString("APP_STATUS"));
            subscription = new Subscription(subscriptionId, app, api, new SubscriptionPolicy(subscriptionPolicyId, subscriptionTier));
            subscription.setStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUB_STATUS")));
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "creating subscriptions api and app information", e);
    }
    return subscription;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SQLException(java.sql.SQLException) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 2 with Application

use of org.wso2.carbon.apimgt.api.model.subscription.Application in project carbon-apimgt by wso2.

the class APISubscriptionDAOImpl method validateSubscription.

/**
 * Validates a subscription
 *
 * @param apiContext  Context of the API
 * @param apiVersion  Version of the API
 * @param clientId    Client id of the application
 * @return Subscription Validation Information
 * @throws APIMgtDAOException   If failed to get subscription validation results.
 */
public SubscriptionValidationResult validateSubscription(String apiContext, String apiVersion, String clientId) throws APIMgtDAOException {
    final String validateSubscriptionSql = "SELECT SUBS.API_ID AS API_ID, SUBS.APPLICATION_ID AS APP_ID, " + "SUBS.SUB_STATUS AS SUB_STATUS, API.PROVIDER AS API_PROVIDER, API.NAME AS API_NAME, " + "APP.NAME AS APP_NAME, APP.CREATED_BY AS APP_OWNER, POLICY.NAME AS SUBS_POLICY " + "FROM AM_SUBSCRIPTION SUBS, AM_API API, AM_APPLICATION APP, AM_SUBSCRIPTION_POLICY POLICY, " + "AM_APP_KEY_MAPPING KEYS " + "WHERE API.CONTEXT = ? AND API.VERSION = ? AND KEYS.CLIENT_ID= ? " + "AND APP.AND SUBS.API_ID = API.UUID AND SUBS.APPLICATION_ID = APP.UUID " + "AND SUBS.TIER_ID = POLICY.UUID AND KEYS.APPLICATION_ID = APP.UUID";
    SubscriptionValidationResult validationInfo = new SubscriptionValidationResult(false);
    try (Connection conn = DAOUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(validateSubscriptionSql)) {
        ps.setString(1, apiContext);
        ps.setString(2, apiVersion);
        ps.setString(3, clientId);
        try (ResultSet rs = ps.executeQuery()) {
            if (rs.next()) {
                validationInfo.setValid(true);
                validationInfo.setApiId(rs.getString("API_ID"));
                validationInfo.setApplicationId(rs.getString("APP_ID"));
                validationInfo.setSubscriptionStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUB_STATUS")));
                validationInfo.setApiProvider(rs.getString("API_PROVIDER"));
                validationInfo.setApiName(rs.getString("API_NAME"));
                validationInfo.setApplicationName(rs.getString("APP_NAME"));
                validationInfo.setApplicationOwner(rs.getString("APP_OWNER"));
                validationInfo.setSubscriptionStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUBS_POLICY")));
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "validating subscription(apiContext: " + apiContext + ", apiVersion: " + apiVersion + ", clientId: " + clientId + ")", e);
    }
    return validationInfo;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) SubscriptionValidationResult(org.wso2.carbon.apimgt.core.models.SubscriptionValidationResult) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with Application

use of org.wso2.carbon.apimgt.api.model.subscription.Application in project carbon-apimgt by wso2.

the class APISubscriptionDAOImpl method createSubscriptionsWithAppInformationOnly.

private List<Subscription> createSubscriptionsWithAppInformationOnly(ResultSet rs) throws APIMgtDAOException {
    List<Subscription> subscriptionList = new ArrayList<>();
    try {
        Subscription subscription;
        while (rs.next()) {
            String subscriptionId = rs.getString("SUBS_UUID");
            String subscriptionTier = rs.getString("SUBS_POLICY");
            Application app = new Application(rs.getString("APP_NAME"), rs.getString("APP_OWNER"));
            app.setId(rs.getString("APP_ID"));
            app.setStatus(rs.getString("APP_STATUS"));
            subscription = new Subscription(subscriptionId, app, null, new SubscriptionPolicy(subscriptionTier));
            subscription.setStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUB_STATUS")));
            subscriptionList.add(subscription);
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "creating subscriptions app information", e);
    }
    return subscriptionList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 4 with Application

use of org.wso2.carbon.apimgt.api.model.subscription.Application in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method addApplication.

/**
 * Add a new instance of an Application
 *
 * @param application The {@link Application} object to be added
 * @throws APIMgtDAOException   If failed to add application.
 */
@Override
public void addApplication(Application application) throws APIMgtDAOException {
    final String addAppQuery = "INSERT INTO AM_APPLICATION (UUID, NAME, APPLICATION_POLICY_ID, " + "DESCRIPTION, APPLICATION_STATUS, CREATED_BY, CREATED_TIME, UPDATED_BY, " + "LAST_UPDATED_TIME) VALUES (?, ?,?,?,?,?,?,?,?)";
    try (Connection conn = DAOUtil.getConnection()) {
        conn.setAutoCommit(false);
        try (PreparedStatement ps = conn.prepareStatement(addAppQuery)) {
            ps.setString(1, application.getId());
            ps.setString(2, application.getName());
            ps.setString(3, application.getPolicy().getUuid());
            ps.setString(4, application.getDescription());
            if (APIMgtConstants.DEFAULT_APPLICATION_NAME.equals(application.getName())) {
                ps.setString(5, APIMgtConstants.ApplicationStatus.APPLICATION_APPROVED);
            } else {
                ps.setString(5, APIMgtConstants.ApplicationStatus.APPLICATION_CREATED);
            }
            ps.setString(6, application.getCreatedUser());
            ps.setTimestamp(7, Timestamp.valueOf(application.getCreatedTime()));
            ps.setString(8, application.getCreatedUser());
            ps.setTimestamp(9, Timestamp.valueOf(application.getCreatedTime()));
            ps.executeUpdate();
            addApplicationPermission(conn, application.getPermissionMap(), application.getId());
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding application: " + application.getId(), ex);
        } finally {
            conn.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding application: " + application.getId(), ex);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 5 with Application

use of org.wso2.carbon.apimgt.api.model.subscription.Application in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method getApplication.

/**
 * Retrieve a given instance of an Application
 *
 * @param appId The UUID that uniquely identifies an Application
 * @return valid {@link Application} object or null
 * @throws APIMgtDAOException   If failed to retrieve application.
 */
@Override
public Application getApplication(String appId) throws APIMgtDAOException {
    final String completeGetAppQuery = GET_APPS_WITH_POLICY_QUERY + " AND APPLICATION.UUID = ?";
    Application application;
    try (Connection conn = DAOUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(completeGetAppQuery)) {
        ps.setString(1, appId);
        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);
            }
            application.setApplicationKeys(getApplicationKeys(appId));
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting application: " + appId, 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)

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