Search in sources :

Example 21 with API

use of org.wso2.carbon.apimgt.api.model.API 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 22 with API

use of org.wso2.carbon.apimgt.api.model.API 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 23 with API

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

the class ApiDAOImpl method isAPINameExists.

/**
 * @see ApiDAO#isAPINameExists(String, String)
 */
@Override
public boolean isAPINameExists(String apiName, String providerName) throws APIMgtDAOException {
    final String apiExistsQuery = "SELECT 1 FROM AM_API WHERE LOWER(NAME) = ? AND PROVIDER = ? AND " + "API_TYPE_ID = (SELECT TYPE_ID FROM AM_API_TYPES WHERE TYPE_NAME = ?)";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(apiExistsQuery)) {
        statement.setString(1, apiName.toLowerCase(Locale.ENGLISH));
        statement.setString(2, providerName);
        statement.setString(3, ApiType.STANDARD.toString());
        try (ResultSet rs = statement.executeQuery()) {
            if (rs.next()) {
                return true;
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "checking if API: " + apiName + "exists for provider " + providerName, e);
    }
    return false;
}
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 24 with API

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

the class ApiDAOImpl method getAPI.

/**
 * {@inheritDoc}
 */
@Override
public API getAPI(String apiID) throws APIMgtDAOException {
    final String query = API_SELECT + " WHERE UUID = ? AND API_TYPE_ID = " + "(SELECT TYPE_ID FROM AM_API_TYPES WHERE TYPE_NAME = ?)";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, apiID);
        statement.setString(2, ApiType.STANDARD.toString());
        API api = constructAPIFromResultSet(connection, statement);
        if (api == null) {
            throw new APIMgtDAOException("API with ID " + apiID + " does not exist", ExceptionCodes.API_NOT_FOUND);
        }
        return api;
    } catch (SQLException | IOException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting API: " + apiID, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) IOException(java.io.IOException)

Example 25 with API

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

the class ApiDAOImpl method initApiTypes.

static void initApiTypes() throws APIMgtDAOException {
    try (Connection connection = DAOUtil.getConnection()) {
        try {
            if (!isApiTypesExist(connection)) {
                connection.setAutoCommit(false);
                addApiTypes(connection);
                connection.commit();
            }
        } catch (SQLException e) {
            connection.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding API types", e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding API types", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)582 ArrayList (java.util.ArrayList)374 API (org.wso2.carbon.apimgt.core.models.API)359 Test (org.testng.annotations.Test)350 HashMap (java.util.HashMap)318 Test (org.junit.Test)316 API (org.wso2.carbon.apimgt.api.model.API)307 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)255 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)253 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)234 SQLException (java.sql.SQLException)190 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)186 IOException (java.io.IOException)181 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)179 PreparedStatement (java.sql.PreparedStatement)169 Connection (java.sql.Connection)158 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)154 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)149 JSONObject (org.json.simple.JSONObject)142 Resource (org.wso2.carbon.registry.core.Resource)139