Search in sources :

Example 16 with API

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

the class ApiDAOImpl method getDedicatedGateway.

/**
 * @see ApiDAO#getDedicatedGateway(String)
 */
@Override
public DedicatedGateway getDedicatedGateway(String apiId) throws APIMgtDAOException {
    final String query = "SELECT HAS_OWN_GATEWAY FROM AM_API WHERE UUID = ?";
    DedicatedGateway dedicatedGateway;
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            statement.setString(1, apiId);
            statement.execute();
            try (ResultSet rs = statement.getResultSet()) {
                if (rs.next()) {
                    dedicatedGateway = new DedicatedGateway();
                    dedicatedGateway.setEnabled(rs.getBoolean(ContainerBasedGatewayConstants.IS_DEDICATED_GATEWAY_ENABLED));
                    return dedicatedGateway;
                } else {
                    throw new APIMgtDAOException("Couldn't Find Dedicated Gateway details ", ExceptionCodes.DEDICATED_GATEWAY_DETAILS_NOT_FOUND);
                }
            }
        } catch (SQLException e) {
            String errorMessage = "Error while retrieving dedicated gateway details of API : " + apiId;
            throw new APIMgtDAOException(errorMessage, e);
        }
    } catch (SQLException e) {
        String message = "Error while creating database connection/prepared-statement";
        throw new APIMgtDAOException(message, 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) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway)

Example 17 with API

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

the class ApiDAOImpl method isAPIVersionsExist.

@Override
public boolean isAPIVersionsExist(String apiName) throws APIMgtDAOException {
    final String query = "SELECT COUNT (NAME) FROM AM_API WHERE NAME = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, apiName);
        statement.execute();
        try (ResultSet rs = statement.getResultSet()) {
            if (rs.next() && rs.getInt(1) > 1) {
                return true;
            } else {
                return false;
            }
        }
    } catch (SQLException e) {
        String errorMessage = "getting existence of versioned API: " + apiName;
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, 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)

Example 18 with API

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

the class ApiDAOImpl method getAPIsByStatus.

@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<API> getAPIsByStatus(List<String> gatewayLabels, String status) throws APIMgtDAOException {
    final String query = "SELECT DISTINCT UUID, PROVIDER, A.NAME, CONTEXT, VERSION, DESCRIPTION, CURRENT_LC_STATUS," + " LIFECYCLE_INSTANCE_ID, LC_WORKFLOW_STATUS, SECURITY_SCHEME FROM AM_API A INNER JOIN " + " AM_API_LABEL_MAPPING M ON A.UUID" + " = M.API_ID INNER JOIN AM_LABELS L ON L.LABEL_ID = M.LABEL_ID  WHERE L.TYPE_NAME='GATEWAY' " + "AND L.NAME" + " " + "IN" + " " + "(" + DAOUtil.getParameterString(gatewayLabels.size()) + ") AND A" + ".CURRENT_LC_STATUS=?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        int i = 0;
        for (String label : gatewayLabels) {
            statement.setString(++i, label);
        }
        statement.setString(++i, status);
        return constructAPISummaryList(connection, statement);
    } catch (SQLException e) {
        String msg = "getting APIs for given gateway labels: " + gatewayLabels.toString() + " with status: " + status;
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 19 with API

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

the class ApiDAOImpl method deleteComment.

@Override
public void deleteComment(String commentId, String apiId) throws APIMgtDAOException {
    final String deleteCommentQuery = "DELETE FROM AM_API_COMMENTS WHERE UUID = ? AND API_ID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(deleteCommentQuery)) {
        try {
            connection.setAutoCommit(false);
            statement.setString(1, commentId);
            statement.setString(2, apiId);
            statement.execute();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String errorMessage = "deleting comment for API " + apiId + ", Comment " + commentId;
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String errorMessage = "deleting comment for API " + apiId + ", Comment " + commentId;
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 20 with API

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

the class ApiDAOImpl method addDocumentInfo.

/**
 * Add artifact resource meta data to an API
 *
 * @param apiId        UUID of API
 * @param documentInfo {@link DocumentInfo}
 * @throws APIMgtDAOException if error occurs while accessing data layer
 */
@Override
public void addDocumentInfo(String apiId, DocumentInfo documentInfo) throws APIMgtDAOException {
    try (Connection connection = DAOUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            ApiResourceDAO.addResourceWithoutValue(connection, apiId, documentInfo.getId(), ResourceCategory.DOC);
            DocMetaDataDAO.addDocumentInfo(connection, documentInfo);
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String msg = "adding Document Info for API: " + apiId + " , Document Name: " + documentInfo.getName();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String msg = "adding Document Info for API: " + apiId + " , Document Name: " + documentInfo.getName();
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, 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