Search in sources :

Example 36 with APIMgtDAOException

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

the class ApiDAOImpl method getEndpoints.

/**
 * get all Endpoints
 *
 * @return List of endpoints.
 * @throws APIMgtDAOException If failed to retrieve endpoints.
 */
@Override
public List<Endpoint> getEndpoints() throws APIMgtDAOException {
    final String query = "SELECT UUID,NAME,ENDPOINT_CONFIGURATION,TPS,TYPE,SECURITY_CONFIGURATION," + "APPLICABLE_LEVEL FROM AM_ENDPOINT WHERE APPLICABLE_LEVEL='" + APIMgtConstants.GLOBAL_ENDPOINT + "'";
    List<Endpoint> endpointList = new ArrayList<>();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query);
        ResultSet resultSet = statement.executeQuery()) {
        while (resultSet.next()) {
            endpointList.add(constructEndPointDetails(resultSet));
        }
    } catch (SQLException | IOException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting Endpoints", e);
    }
    return endpointList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 37 with APIMgtDAOException

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

the class ApiFileDAOImpl method updateAPI.

/**
 * @see ApiDAO#updateAPI(String apiID, API substituteAPI)
 */
@Override
public void updateAPI(String apiID, API substituteAPI) throws APIMgtDAOException {
    API oldAPI = getAPI(apiID);
    if (oldAPI == null) {
        String errorMsg = "Error while updating API. Unable to find API with Id: " + apiID;
        log.error(errorMsg);
        throw new APIMgtDAOException(errorMsg, ExceptionCodes.API_NOT_FOUND);
    }
    // set immutable properties from old API
    API updatedAPI = new API.APIBuilder(substituteAPI).id(apiID).provider(oldAPI.getProvider()).name(oldAPI.getName()).version(oldAPI.getVersion()).context(oldAPI.getContext()).createdTime(oldAPI.getCreatedTime()).createdBy(oldAPI.getCreatedBy()).lifecycleInstanceId(oldAPI.getLifecycleInstanceId()).lifeCycleStatus(oldAPI.getLifeCycleStatus()).copiedFromApiId(oldAPI.getCopiedFromApiId()).build();
    // Adding the API override the existing files.
    addAPI(updatedAPI);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API)

Example 38 with APIMgtDAOException

use of org.wso2.carbon.apimgt.api.APIMgtDAOException 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 39 with APIMgtDAOException

use of org.wso2.carbon.apimgt.api.APIMgtDAOException 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 40 with APIMgtDAOException

use of org.wso2.carbon.apimgt.api.APIMgtDAOException 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)

Aggregations

APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)333 SQLException (java.sql.SQLException)192 Connection (java.sql.Connection)146 PreparedStatement (java.sql.PreparedStatement)129 Test (org.testng.annotations.Test)84 ResultSet (java.sql.ResultSet)72 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)72 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)57 API (org.wso2.carbon.apimgt.core.models.API)57 ArrayList (java.util.ArrayList)50 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)35 Application (org.wso2.carbon.apimgt.core.models.Application)24 HashMap (java.util.HashMap)23 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)22 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)21 BeforeTest (org.testng.annotations.BeforeTest)21 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)20 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)20 IOException (java.io.IOException)19 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)17