Search in sources :

Example 1 with Label

use of org.wso2.carbon.apimgt.core.models.Label 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 2 with Label

use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.

the class ApiDAOImpl method getAPIsByStatus.

/**
 * @see ApiDAO#getAPIsByStatus(Set, List, List)
 */
@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<API> getAPIsByStatus(Set<String> roles, List<String> statuses, List<String> labels) throws APIMgtDAOException {
    // check for null at the beginning before constructing the query to retrieve APIs from database
    if (roles == null || statuses == null) {
        String errorMessage = "Role list or API status list should not be null to retrieve APIs.";
        log.error(errorMessage);
        throw new APIMgtDAOException(errorMessage);
    }
    // the below query will be used to retrieve the union of,
    // published/prototyped APIs (statuses) with public visibility and
    // published/prototyped APIs with restricted visibility where APIs are restricted based on roles of the user
    String labelQuery = null;
    if (labels.isEmpty()) {
        labelQuery = "SELECT LABEL_ID FROM  AM_LABELS WHERE TYPE_NAME='STORE'";
    } else {
        labelQuery = "SELECT LABEL_ID FROM  AM_LABELS WHERE NAME IN ( " + DAOUtil.getParameterString(labels.size()) + ") AND TYPE_NAME='STORE'";
    }
    final String query = "Select UUID, PROVIDER, NAME, CONTEXT, VERSION, DESCRIPTION, CURRENT_LC_STATUS, " + "LIFECYCLE_INSTANCE_ID, LC_WORKFLOW_STATUS, SECURITY_SCHEME  FROM (" + API_SUMMARY_SELECT + " WHERE " + "VISIBILITY = '" + API.Visibility.PUBLIC + "' " + "AND " + "CURRENT_LC_STATUS  IN (" + DAOUtil.getParameterString(statuses.size()) + ") AND " + "API_TYPE_ID = (SELECT TYPE_ID FROM AM_API_TYPES WHERE TYPE_NAME = ?)" + "UNION " + API_SUMMARY_SELECT + " WHERE " + "VISIBILITY = '" + API.Visibility.RESTRICTED + "' " + "AND " + "UUID IN (SELECT API_ID FROM AM_API_VISIBLE_ROLES WHERE ROLE IN " + "(" + DAOUtil.getParameterString(roles.size()) + ")) " + " AND CURRENT_LC_STATUS  IN (" + DAOUtil.getParameterString(statuses.size()) + ") AND " + " API_TYPE_ID = (SELECT TYPE_ID FROM AM_API_TYPES WHERE TYPE_NAME = ?)) A" + " JOIN AM_API_LABEL_MAPPING LM ON A.UUID=LM.API_ID WHERE LM.LABEL_ID IN (" + labelQuery + ")";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        int i = 0;
        // put desired API status into the query (to get APIs with public visibility)
        for (String status : statuses) {
            statement.setString(++i, status);
        }
        statement.setString(++i, ApiType.STANDARD.toString());
        // put desired roles into the query
        for (String role : roles) {
            statement.setString(++i, role);
        }
        // put desired API status into the query (to get APIs with restricted visibility)
        for (String status : statuses) {
            statement.setString(++i, status);
        }
        statement.setString(++i, ApiType.STANDARD.toString());
        // Set the label names in the query
        for (String label : labels) {
            statement.setString(++i, label);
        }
        return constructAPISummaryList(connection, statement);
    } catch (SQLException e) {
        String errorMessage = "Error while retrieving API list in store.";
        throw new APIMgtDAOException(errorMessage, 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 3 with Label

use of org.wso2.carbon.apimgt.core.models.Label 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 4 with Label

use of org.wso2.carbon.apimgt.core.models.Label 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 5 with Label

use of org.wso2.carbon.apimgt.core.models.Label 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)

Aggregations

Label (org.wso2.carbon.apimgt.core.models.Label)65 ArrayList (java.util.ArrayList)52 Test (org.testng.annotations.Test)45 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)32 API (org.wso2.carbon.apimgt.core.models.API)29 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)28 SQLException (java.sql.SQLException)21 PreparedStatement (java.sql.PreparedStatement)20 HashMap (java.util.HashMap)16 Connection (java.sql.Connection)15 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)14 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)13 Test (org.junit.Test)12 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)11 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)11 Map (java.util.Map)10 BeforeTest (org.testng.annotations.BeforeTest)9 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)8 Response (javax.ws.rs.core.Response)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8