Search in sources :

Example 36 with API

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

the class ApiFileDAOImpl method getAPIs.

/**
 * @see ApiDAO#getAPIs(Set, String)
 */
@Override
public List<API> getAPIs(Set<String> roles, String user) throws APIMgtDAOException {
    File[] files = new File(storagePath).listFiles();
    List<API> apiList = new ArrayList<>();
    final FilenameFilter filenameFilter = (dir, name) -> (name.endsWith(APIMgtConstants.APIFileUtilConstants.JSON_EXTENSION) && name.contains(APIMgtConstants.APIFileUtilConstants.API_DEFINITION_FILE_PREFIX) && !dir.isHidden());
    if (files != null) {
        for (File file : files) {
            apiList.add((API) fetchObject(file, FileApi.class, filenameFilter));
        }
    }
    apiList.removeIf(Objects::isNull);
    return apiList;
}
Also used : FilenameFilter(java.io.FilenameFilter) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway) LoggerFactory(org.slf4j.LoggerFactory) Rating(org.wso2.carbon.apimgt.core.models.Rating) JsonReader(com.google.gson.stream.JsonReader) APIFileUtils(org.wso2.carbon.apimgt.core.util.APIFileUtils) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Map(java.util.Map) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIMgtConstants(org.wso2.carbon.apimgt.core.util.APIMgtConstants) Logger(org.slf4j.Logger) API(org.wso2.carbon.apimgt.core.models.API) Set(java.util.Set) ExceptionCodes(org.wso2.carbon.apimgt.core.exception.ExceptionCodes) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Comment(org.wso2.carbon.apimgt.core.models.Comment) Objects(java.util.Objects) List(java.util.List) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) FileApi(org.wso2.carbon.apimgt.core.models.FileApi) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) InputStream(java.io.InputStream) FilenameFilter(java.io.FilenameFilter) ArrayList(java.util.ArrayList) Objects(java.util.Objects) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) File(java.io.File)

Example 37 with API

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

the class ApiFileDAOImpl method addAPI.

/**
 * @see ApiDAO#addAPI(API api)
 */
@Override
public void addAPI(API api) throws APIMgtDAOException {
    // Save API definition
    FileApi fileApi = new FileApi(api);
    String apiExportDirectory = APIFileUtils.getAPIBaseDirectory(storagePath, fileApi);
    APIFileUtils.createDirectory(apiExportDirectory);
    APIFileUtils.exportApiDefinitionToFileSystem(fileApi, apiExportDirectory);
    // Export gateway config to file system
    APIFileUtils.exportGatewayConfigToFileSystem(api.getGatewayConfig(), api, apiExportDirectory);
    // Export swagger definition to file system.
    APIFileUtils.exportSwaggerDefinitionToFileSystem(api.getApiDefinition(), api, apiExportDirectory);
}
Also used : FileApi(org.wso2.carbon.apimgt.core.models.FileApi)

Example 38 with API

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

the class ApiDAOImpl method getRatingByUUID.

@Override
public Rating getRatingByUUID(String apiId, String ratingId) throws APIMgtDAOException {
    final String query = "SELECT UUID, API_ID, RATING, USER_IDENTIFIER, " + "CREATED_BY, CREATED_TIME, UPDATED_BY, LAST_UPDATED_TIME " + "FROM AM_API_RATINGS WHERE UUID = ? AND API_ID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            statement.setString(1, ratingId);
            statement.setString(2, apiId);
            statement.execute();
            try (ResultSet rs = statement.getResultSet()) {
                if (rs.next()) {
                    return constructRatingFromResultSet(rs);
                }
            }
        } catch (SQLException e) {
            String errorMessage = "getting Rating: " + ratingId + " for API: " + apiId;
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
        }
    } catch (SQLException e) {
        String errorMessage = "getting Rating: " + ratingId + " for API: " + apiId;
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
    }
    return null;
}
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 39 with API

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

the class PolicyDAOImpl method addAPIPipeline.

/**
 * Adding pipelines of API policy to database
 *
 * @param connection connection to db
 * @param uuid       policy id/ uuid of the policy
 * @throws SQLException if error occurred while inserting pipeline to db
 */
private static void addAPIPipeline(Connection connection, List<Pipeline> pipelines, String uuid) throws SQLException, APIMgtDAOException {
    final String query = "INSERT INTO AM_CONDITION_GROUP (UUID, QUOTA_TYPE, UNIT_TIME, TIME_UNIT, DESCRIPTION, QUOTA, " + "QUOTA_UNIT) VALUES (?,?,?,?,?,?,?)";
    String dbProductName = connection.getMetaData().getDatabaseProductName();
    try (PreparedStatement statement = connection.prepareStatement(query, new String[] { DAOUtil.getConvertedAutoGeneratedColumnName(dbProductName, APIMgtConstants.ThrottlePolicyConstants.COLUMN_CONDITION_GROUP_ID) })) {
        for (Pipeline pipeline : pipelines) {
            statement.setString(1, uuid);
            statement.setString(2, pipeline.getQuotaPolicy().getType());
            statement.setLong(3, pipeline.getQuotaPolicy().getLimit().getUnitTime());
            statement.setString(4, pipeline.getQuotaPolicy().getLimit().getTimeUnit());
            statement.setString(5, pipeline.getDescription());
            Limit limit = pipeline.getQuotaPolicy().getLimit();
            setDefaultThrottlePolicyDetailsPreparedStmt(limit, statement);
            statement.executeUpdate();
            ResultSet rs = statement.getGeneratedKeys();
            if (rs.next()) {
                // get the auto increment id
                int conditionId = rs.getInt(1);
                List<Condition> conditionList = pipeline.getConditions();
                for (Condition condition : conditionList) {
                    if (PolicyConstants.IP_CONDITION_TYPE.equals(condition.getType()) || PolicyConstants.IP_SPECIFIC_TYPE.equals(condition.getType()) || PolicyConstants.IP_RANGE_TYPE.equals(condition.getType())) {
                        addIPCondition(connection, condition, conditionId);
                    } else if (PolicyConstants.HEADER_CONDITION_TYPE.equals(condition.getType())) {
                        addHeaderCondition(connection, condition, conditionId);
                    } else if (PolicyConstants.JWT_CLAIMS_CONDITION_TYPE.equals(condition.getType())) {
                        addJWTClaimCondition(connection, condition, conditionId);
                    } else if (PolicyConstants.QUERY_PARAMS_CONDITION_TYPE.equals(condition.getType())) {
                        addParamCondition(connection, condition, conditionId);
                    } else {
                        // unsupported Condition
                        throw new IllegalArgumentException("Unsupported Condition type: " + condition.getType());
                    }
                }
            } else {
                String errorMsg = "Unable to retrieve auto incremented id, hence unable to add Pipeline Condition";
                throw new IllegalStateException(errorMsg);
            }
        }
    }
}
Also used : JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) Condition(org.wso2.carbon.apimgt.core.models.policy.Condition) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Limit(org.wso2.carbon.apimgt.core.models.policy.Limit) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Example 40 with API

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

the class PolicyDAOImpl method getApiPolicyByUuid.

@Override
public APIPolicy getApiPolicyByUuid(String uuid) throws APIMgtDAOException {
    try {
        String sqlQuery = "SELECT UUID, NAME, DEFAULT_QUOTA_TYPE, DEFAULT_TIME_UNIT, DEFAULT_UNIT_TIME, " + "DEFAULT_QUOTA, " + "DEFAULT_QUOTA_UNIT, DESCRIPTION, DISPLAY_NAME, IS_DEPLOYED, APPLICABLE_LEVEL from " + "AM_API_POLICY WHERE UUID = ?";
        try (Connection connection = DAOUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
            preparedStatement.setString(1, uuid);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    APIPolicy apiPolicy = new APIPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
                    setCommonPolicyDetails(apiPolicy, resultSet);
                    apiPolicy.setUserLevel(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
                    apiPolicy.setPipelines(getPipelines(apiPolicy.getUuid(), connection));
                    return apiPolicy;
                } else {
                    // not found
                    String msg = "API Policy not found for id: " + uuid;
                    log.warn(msg);
                    throw new APIMgtDAOException(msg, ExceptionCodes.POLICY_NOT_FOUND);
                }
            }
        }
    } catch (SQLException e) {
        String errorMsg = "Error in retrieving API policy with id: " + uuid;
        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) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

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