Search in sources :

Example 46 with APIMgtDAOException

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

the class DAOFactory method getAnalyticsDAO.

/**
 * To get the AnalyticsDao object. Depends on different vendors.
 *
 * @return AnalyticsDAO object
 * @throws APIMgtDAOException if error during getting analytics database connection
 */
public static AnalyticsDAO getAnalyticsDAO() throws APIMgtDAOException {
    AnalyticsDAO analyticsDAO;
    boolean isAnalyticsEnabled = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getAnalyticsConfigurations().isEnabled();
    if (isAnalyticsEnabled) {
        try (Connection connection = DAOUtil.getAnalyticsConnection()) {
            analyticsDAO = getAnalyticsDaoImplForVendor(connection);
        } catch (SQLException e) {
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting FunctionDAO", e);
        }
    } else {
        // if analytics is not enabled create a normal AMDB data connection to check db driver
        try (Connection connection = DAOUtil.getConnection()) {
            analyticsDAO = getAnalyticsDaoImplForVendor(connection);
        } catch (SQLException e) {
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting FunctionDAO", e);
        }
    }
    return analyticsDAO;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 47 with APIMgtDAOException

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

the class LabelDAOImpl method updateLabel.

/**
 * @see LabelDAO#updateLabel(Label)
 */
@Override
public Label updateLabel(Label updatedLabel) throws APIMgtDAOException {
    final String query = "UPDATE AM_LABELS SET NAME=? , TYPE_NAME=? WHERE LABEL_ID=?";
    String labelId = updatedLabel.getId();
    List<String> accessURLs = updatedLabel.getAccessUrls();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            connection.setAutoCommit(false);
            statement.setString(1, updatedLabel.getName());
            if (updatedLabel.getType() != null) {
                statement.setString(2, updatedLabel.getType().toUpperCase(Locale.ENGLISH));
            } else {
                statement.setString(2, updatedLabel.getType());
            }
            statement.setString(3, updatedLabel.getId());
            statement.executeUpdate();
            connection.commit();
            deleteLabelAccessUrlMappings(labelId);
            if (APIMgtConstants.LABEL_TYPE_GATEWAY.equalsIgnoreCase(updatedLabel.getType())) {
                insertAccessUrlMappings(labelId, accessURLs);
            } else {
                accessURLs = new ArrayList<>();
            }
        } catch (SQLException e) {
            connection.rollback();
            String message = "Error while updating the label" + " [label id] " + labelId;
            throw new APIMgtDAOException(message, e, ExceptionCodes.LABEL_UPDATE_FAILED);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
        return new Label.Builder().id(labelId).name(updatedLabel.getName()).description(updatedLabel.getDescription()).accessUrls(accessURLs).type(updatedLabel.getType()).build();
    } catch (SQLException e) {
        String message = "Error while updating the label [label ID] " + labelId;
        throw new APIMgtDAOException(message, e, ExceptionCodes.LABEL_UPDATE_FAILED);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 48 with APIMgtDAOException

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

the class LabelDAOImpl method getLabelsByName.

/**
 * @see LabelDAO#getLabelsByName(List)
 */
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
@Override
public List<Label> getLabelsByName(List<String> labelNames) throws APIMgtDAOException {
    List<Label> matchingLabels = new ArrayList<>();
    if (!labelNames.isEmpty()) {
        final String query = "SELECT LABEL_ID, NAME FROM AM_LABELS WHERE NAME IN (" + DAOUtil.getParameterString(labelNames.size()) + ")";
        try (Connection connection = DAOUtil.getConnection();
            PreparedStatement statement = connection.prepareStatement(query)) {
            for (int i = 0; i < labelNames.size(); ++i) {
                statement.setString(i + 1, labelNames.get(i));
            }
            try (ResultSet rs = statement.executeQuery()) {
                while (rs.next()) {
                    Label label = new Label.Builder().id(rs.getString("LABEL_ID")).name(rs.getString("NAME")).accessUrls(getLabelAccessUrls(rs.getString("LABEL_ID"))).build();
                    matchingLabels.add(label);
                }
            }
        } catch (SQLException e) {
            String message = DAOUtil.DAO_ERROR_PREFIX + "retrieving labels";
            throw new APIMgtDAOException(message, e);
        }
    }
    return matchingLabels;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 49 with APIMgtDAOException

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

the class LabelDAOImpl method deleteLabelAccessUrlMappings.

/**
 * Delete access urls of a label by label id
 *
 * @param labelId Id of the label
 * @throws APIMgtDAOException if error occurs while deleting label access urls
 */
private void deleteLabelAccessUrlMappings(String labelId) throws APIMgtDAOException {
    final String query = "DELETE FROM AM_LABEL_ACCESS_URL_MAPPING WHERE LABEL_ID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            connection.setAutoCommit(false);
            statement.setString(1, labelId);
            statement.execute();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String message = DAOUtil.DAO_ERROR_PREFIX + "deleting the label access url mappings [label id] " + labelId;
            throw new APIMgtDAOException(message, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String message = DAOUtil.DAO_ERROR_PREFIX + "deleting the label access url mappings [label id] " + labelId;
        throw new APIMgtDAOException(message, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 50 with APIMgtDAOException

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

the class FunctionDAOImpl method getUserFunctionsForEvent.

/**
 * {@inheritDoc}
 */
@Override
public List<Function> getUserFunctionsForEvent(String userName, Event event) throws APIMgtDAOException {
    if (userName == null) {
        throw new IllegalArgumentException("Username must not be null");
    }
    if (event == null) {
        throw new IllegalArgumentException("Event must not be null");
    }
    List<Function> functions = new ArrayList<>();
    final String sqlQuery = "SELECT FUNCTION_NAME, FUNCTION_URI " + "FROM AM_LAMBDA_FUNCTION JOIN AM_EVENT_FUNCTION_MAPPING " + "ON AM_LAMBDA_FUNCTION.FUNCTION_ID = AM_EVENT_FUNCTION_MAPPING.FUNCTION_ID " + "WHERE USER_NAME = ? AND EVENT = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setString(1, userName);
        preparedStatement.setString(2, event.getEventAsString());
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String functionName = resultSet.getString("FUNCTION_NAME");
                URI endpointURI = new URI(resultSet.getString("FUNCTION_URI"));
                functions.add(new Function(functionName, endpointURI));
            }
        } catch (URISyntaxException e) {
            throw new APIMgtDAOException("Not a URI", e);
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error retrieving functions for event: " + event + " of user: " + userName, e);
    }
    return functions;
}
Also used : Function(org.wso2.carbon.apimgt.core.models.Function) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

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