Search in sources :

Example 11 with API

use of org.wso2.carbon.apimgt.keymgt.model.entity.API in project product-apim by wso2.

the class ApiClient method setApiKey.

/**
 * Helper method to configure the first api key found
 *
 * @param apiKey API key
 */
public void setApiKey(String apiKey) {
    for (RequestInterceptor apiAuthorization : apiAuthorizations.values()) {
        if (apiAuthorization instanceof ApiKeyAuth) {
            ApiKeyAuth keyAuth = (ApiKeyAuth) apiAuthorization;
            keyAuth.setApiKey(apiKey);
            return;
        }
    }
    throw new RuntimeException("No API key authentication configured!");
}
Also used : RequestInterceptor(feign.RequestInterceptor) ApiKeyAuth(org.wso2.carbon.apimgt.rest.integration.tests.store.auth.ApiKeyAuth)

Example 12 with API

use of org.wso2.carbon.apimgt.keymgt.model.entity.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 13 with API

use of org.wso2.carbon.apimgt.keymgt.model.entity.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 14 with API

use of org.wso2.carbon.apimgt.keymgt.model.entity.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 15 with API

use of org.wso2.carbon.apimgt.keymgt.model.entity.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)

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