Search in sources :

Example 1 with ApiType

use of org.wso2.carbon.apimgt.core.dao.ApiType in project carbon-apimgt by wso2.

the class ApiDAOImpl method addApiTypes.

private static void addApiTypes(Connection connection) throws SQLException {
    final String query = "INSERT INTO AM_API_TYPES (TYPE_NAME) VALUES (?)";
    try (PreparedStatement statement = connection.prepareStatement(query)) {
        for (ApiType apiType : ApiType.values()) {
            statement.setString(1, apiType.toString());
            statement.addBatch();
        }
        statement.executeBatch();
    }
}
Also used : ApiType(org.wso2.carbon.apimgt.core.dao.ApiType) PreparedStatement(java.sql.PreparedStatement)

Example 2 with ApiType

use of org.wso2.carbon.apimgt.core.dao.ApiType in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsGet.

/**
 * Get all subscriptions.
 * {@code <p/>}
 * If apiId is specified this will return the subscribed applications of that api
 * If application id is specified this will return the api subscriptions of that application
 *
 * @param apiId         ID of the API
 * @param applicationId ID of the Application
 * @param offset        offset value
 * @param limit         limit value
 * @param ifNoneMatch   If-None-Match header value
 * @param request       msf4j request object
 * @return Subscription List
 * @throws NotFoundException If failed to get the subscription
 */
@Override
public Response subscriptionsGet(String apiId, String applicationId, String apiType, Integer offset, Integer limit, String ifNoneMatch, Request request) throws NotFoundException {
    List<Subscription> subscribedApiList = null;
    SubscriptionListDTO subscriptionListDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        if (!StringUtils.isEmpty(apiId)) {
            subscribedApiList = apiStore.getSubscriptionsByAPI(apiId);
            subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
        } else if (!StringUtils.isEmpty(applicationId)) {
            Application application = apiStore.getApplicationByUuid(applicationId);
            if (application != null) {
                if (!StringUtils.isEmpty(apiType)) {
                    ApiType apiTypeEnum = ApiType.fromString(apiType);
                    if (apiTypeEnum == null) {
                        throw new APIManagementException("API Type specified is invalid", ExceptionCodes.API_TYPE_INVALID);
                    }
                    subscribedApiList = apiStore.getAPISubscriptionsByApplication(application, apiTypeEnum);
                } else {
                    subscribedApiList = apiStore.getAPISubscriptionsByApplication(application);
                }
                subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
            } else {
                String errorMessage = "Application not found: " + applicationId;
                APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.APPLICATION_NOT_FOUND);
                HashMap<String, String> paramList = new HashMap<String, String>();
                paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
                log.error(errorMessage, e);
                return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
            }
        } else {
            // mandatory parameters not provided
            String errorMessage = "Either applicationId or apiId should be provided";
            ErrorHandler errorHandler = ExceptionCodes.PARAMETER_NOT_PROVIDED;
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
            log.error(errorMessage);
            return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving subscriptions";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, applicationId);
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(subscriptionListDTO).build();
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApiType(org.wso2.carbon.apimgt.core.dao.ApiType) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 3 with ApiType

use of org.wso2.carbon.apimgt.core.dao.ApiType in project carbon-apimgt by wso2.

the class APISubscriptionDAOImpl method getAPISubscriptionsByApplication.

@Override
public List<Subscription> getAPISubscriptionsByApplication(String applicationId, ApiType apiType) throws APIMgtDAOException {
    final String getSubscriptionsByAppSql = "SELECT SUBS.UUID AS SUBS_UUID, SUBS.TIER_ID AS SUBS_TIER, " + "SUBS.API_ID AS API_ID, SUBS.APPLICATION_ID AS APP_ID, SUBS.SUB_STATUS AS SUB_STATUS, " + "SUBS.SUB_TYPE AS SUB_TYPE, API.PROVIDER AS API_PROVIDER, API.NAME AS API_NAME, " + "API.CONTEXT AS API_CONTEXT, API.VERSION AS API_VERSION, POLICY.NAME AS SUBS_POLICY " + "FROM AM_SUBSCRIPTION SUBS, AM_API API, AM_SUBSCRIPTION_POLICY POLICY  " + "WHERE SUBS.APPLICATION_ID = ? AND SUBS.API_ID = API.UUID AND SUBS.TIER_ID = POLICY.UUID " + "AND API.API_TYPE_ID = (SELECT TYPE_ID FROM AM_API_TYPES WHERE TYPE_NAME = ?)";
    try (Connection conn = DAOUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(getSubscriptionsByAppSql)) {
        ps.setString(1, applicationId);
        ps.setString(2, apiType.toString());
        try (ResultSet rs = ps.executeQuery()) {
            return createSubscriptionsWithApiInformationOnly(rs);
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting api subscriptions by application(appId: " + applicationId + ", apiType: " + apiType + ")", 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)

Aggregations

PreparedStatement (java.sql.PreparedStatement)2 ApiType (org.wso2.carbon.apimgt.core.dao.ApiType)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)1 ErrorHandler (org.wso2.carbon.apimgt.core.exception.ErrorHandler)1 Application (org.wso2.carbon.apimgt.core.models.Application)1 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1 SubscriptionListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO)1