Search in sources :

Example 1 with API

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

the class ApiDAOImpl method getUserRatingForApiFromUser.

@Override
public Rating getUserRatingForApiFromUser(String apiId, String userId) 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 USER_IDENTIFIER = ? AND API_ID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            statement.setString(1, userId);
            statement.setString(2, apiId);
            statement.execute();
            try (ResultSet rs = statement.getResultSet()) {
                if (rs.next()) {
                    return constructRatingFromResultSet(rs);
                }
            }
        } catch (SQLException e) {
            String errorMessage = "getting User Rating for API: " + apiId + ", User: " + userId;
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
        }
    } catch (SQLException e) {
        String errorMessage = "getting User Rating for API: " + apiId + ", User: " + userId;
        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 2 with API

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

the class ApiDAOImpl method updateAPIWorkflowStatus.

/**
 * Update an existing API workflow state
 *
 * @param apiID          The {@link String} of the API that needs to be updated
 * @param workflowStatus workflow status
 * @throws APIMgtDAOException if error occurs while accessing data layer
 */
@Override
public void updateAPIWorkflowStatus(String apiID, APILCWorkflowStatus workflowStatus) throws APIMgtDAOException {
    final String query = "UPDATE AM_API SET LAST_UPDATED_TIME = ?, LC_WORKFLOW_STATUS=? WHERE UUID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            connection.setAutoCommit(false);
            statement.setTimestamp(1, Timestamp.valueOf(LocalDateTime.now()));
            statement.setString(2, workflowStatus.toString());
            statement.setString(3, apiID);
            statement.execute();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String msg = "updating workflow status for API: " + apiID + " to Status: " + workflowStatus.name();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String msg = "updating workflow status for API: " + apiID + " to Status: " + workflowStatus.name();
        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)

Example 3 with API

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

the class ApiDAOImpl method updateAPI.

/**
 * Update an existing API
 *
 * @param apiID         The {@link String} of the API that needs to be updated
 * @param substituteAPI Substitute {@link API} object that will replace the existing API
 * @throws APIMgtDAOException if error occurs while accessing data layer
 */
@Override
public void updateAPI(String apiID, API substituteAPI) throws APIMgtDAOException {
    final String query = "UPDATE AM_API SET CONTEXT = ?, IS_DEFAULT_VERSION = ?, DESCRIPTION = ?, VISIBILITY = ?, " + "IS_RESPONSE_CACHED = ?, CACHE_TIMEOUT = ?, TECHNICAL_OWNER = ?, TECHNICAL_EMAIL = ?, " + "BUSINESS_OWNER = ?, BUSINESS_EMAIL = ?, CORS_ENABLED = ?, CORS_ALLOW_ORIGINS = ?, " + "CORS_ALLOW_CREDENTIALS = ?, CORS_ALLOW_HEADERS = ?, CORS_ALLOW_METHODS = ?, LAST_UPDATED_TIME = ?," + "UPDATED_BY = ?, LC_WORKFLOW_STATUS = ?, SECURITY_SCHEME = ? WHERE UUID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            connection.setAutoCommit(false);
            statement.setString(1, substituteAPI.getContext());
            statement.setBoolean(2, substituteAPI.isDefaultVersion());
            statement.setString(3, substituteAPI.getDescription());
            statement.setString(4, substituteAPI.getVisibility().toString());
            statement.setBoolean(5, substituteAPI.isResponseCachingEnabled());
            statement.setInt(6, substituteAPI.getCacheTimeout());
            BusinessInformation businessInformation = substituteAPI.getBusinessInformation();
            statement.setString(7, businessInformation.getTechnicalOwner());
            statement.setString(8, businessInformation.getTechnicalOwnerEmail());
            statement.setString(9, businessInformation.getBusinessOwner());
            statement.setString(10, businessInformation.getBusinessOwnerEmail());
            CorsConfiguration corsConfiguration = substituteAPI.getCorsConfiguration();
            statement.setBoolean(11, corsConfiguration.isEnabled());
            statement.setString(12, String.join(",", corsConfiguration.getAllowOrigins()));
            statement.setBoolean(13, corsConfiguration.isAllowCredentials());
            statement.setString(14, String.join(",", corsConfiguration.getAllowHeaders()));
            statement.setString(15, String.join(",", corsConfiguration.getAllowMethods()));
            statement.setTimestamp(16, Timestamp.valueOf(LocalDateTime.now()));
            statement.setString(17, substituteAPI.getUpdatedBy());
            statement.setString(18, substituteAPI.getWorkflowStatus());
            statement.setInt(19, substituteAPI.getSecurityScheme());
            statement.setString(20, apiID);
            statement.execute();
            // Delete current visible roles if they exist
            deleteVisibleRoles(connection, apiID);
            if (API.Visibility.RESTRICTED == substituteAPI.getVisibility()) {
                addVisibleRole(connection, apiID, substituteAPI.getVisibleRoles());
            }
            deleteAPIPermission(connection, apiID);
            updateApiPermission(connection, substituteAPI.getPermissionMap(), apiID);
            deleteTransports(connection, apiID);
            addTransports(connection, apiID, substituteAPI.getTransport());
            deleteThreatProtectionPolicies(connection, apiID);
            if (substituteAPI.getThreatProtectionPolicies() != null) {
                addThreatProtectionPolicies(connection, apiID, substituteAPI.getThreatProtectionPolicies());
            }
            // Delete current tag mappings if they exist
            deleteTagsMapping(connection, apiID);
            addTagsMapping(connection, apiID, substituteAPI.getTags());
            deleteLabelsMapping(connection, apiID);
            addLabelMapping(connection, apiID, substituteAPI.getLabels());
            deleteSubscriptionPolicies(connection, apiID);
            addSubscriptionPolicies(connection, substituteAPI.getPolicies(), apiID);
            deleteEndPointsForApi(connection, apiID);
            addEndPointsForApi(connection, apiID, substituteAPI.getEndpoint());
            deleteEndPointsForOperation(connection, apiID);
            deleteUrlMappings(connection, apiID);
            addUrlMappings(connection, substituteAPI.getUriTemplates().values(), apiID);
            deleteApiPolicy(connection, apiID);
            if (substituteAPI.getApiPolicy() != null) {
                addApiPolicy(connection, substituteAPI.getApiPolicy().getUuid(), apiID);
            }
            connection.commit();
        } catch (SQLException | IOException e) {
            connection.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating API: " + substituteAPI.getProvider() + " - " + substituteAPI.getName() + " - " + substituteAPI.getVersion(), e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating API: " + substituteAPI.getProvider() + " - " + substituteAPI.getName() + " - " + substituteAPI.getVersion(), e);
    }
}
Also used : BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 4 with API

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

the class ApiDAOImpl method constructAPISummaryList.

private List<API> constructAPISummaryList(Connection connection, PreparedStatement statement) throws SQLException {
    List<API> apiList = new ArrayList<>();
    try (ResultSet rs = statement.executeQuery()) {
        while (rs.next()) {
            String apiPrimaryKey = rs.getString("UUID");
            API apiSummary = new API.APIBuilder(rs.getString("PROVIDER"), rs.getString("NAME"), rs.getString("VERSION")).id(apiPrimaryKey).context(rs.getString("CONTEXT")).description(rs.getString("DESCRIPTION")).lifeCycleStatus(rs.getString("CURRENT_LC_STATUS")).lifecycleInstanceId(rs.getString("LIFECYCLE_INSTANCE_ID")).workflowStatus(rs.getString("LC_WORKFLOW_STATUS")).securityScheme(rs.getInt("SECURITY_SCHEME")).threatProtectionPolicies(getThreatProtectionPolicies(connection, apiPrimaryKey)).build();
            apiList.add(apiSummary);
        }
    }
    return apiList;
}
Also used : ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API)

Example 5 with API

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

Aggregations

API (org.wso2.carbon.apimgt.core.models.API)359 Test (org.testng.annotations.Test)320 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)253 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)179 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)154 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)132 HashMap (java.util.HashMap)129 ArrayList (java.util.ArrayList)112 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)106 Test (org.junit.Test)83 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)83 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)82 SQLException (java.sql.SQLException)75 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)75 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)70 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)65 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)61 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)60 Connection (java.sql.Connection)58 Response (javax.ws.rs.core.Response)58