Search in sources :

Example 16 with Subscriber

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

the class ApiMgtDAO method getUserRatingInfo.

/**
 * @param uuid API uuid
 * @param userId     User Id
 * @param conn       Database connection
 * @throws APIManagementException if failed to get user API Ratings
 */
private JSONObject getUserRatingInfo(String uuid, String userId, Connection conn) throws APIManagementException, SQLException {
    PreparedStatement ps = null;
    ResultSet rs = null;
    JSONObject ratingObj = new JSONObject();
    int userRating = 0;
    int id = -1;
    String ratingId = null;
    try {
        int tenantId;
        tenantId = APIUtil.getTenantId(userId);
        // Get subscriber Id
        Subscriber subscriber = getSubscriber(userId, tenantId, conn);
        if (subscriber == null) {
            String msg = "Could not load Subscriber records for: " + userId;
            log.error(msg);
            throw new APIManagementException(msg);
        }
        // Get API Id
        id = getAPIID(uuid, conn);
        String sqlQuery = SQLConstants.GET_API_RATING_INFO_SQL;
        if (id == -1) {
            String msg = "Could not load API record for API with UUID: " + uuid;
            log.error(msg);
            throw new APIManagementException(msg);
        }
        // This query to get rating information from the AM_API_RATINGS table
        ps = conn.prepareStatement(sqlQuery);
        ps.setInt(1, subscriber.getId());
        ps.setInt(2, id);
        rs = ps.executeQuery();
        while (rs.next()) {
            ratingId = rs.getString("RATING_ID");
            userRating = rs.getInt("RATING");
        }
        if (ratingId != null) {
            // A rating record exists
            ratingObj.put(APIConstants.RATING_ID, ratingId);
            ratingObj.put(APIConstants.USER_NAME, userId);
            ratingObj.put(APIConstants.RATING, userRating);
        }
    } catch (SQLException e) {
        handleException("Failed to retrieve API ratings ", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, null, rs);
    }
    return ratingObj;
}
Also used : JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 17 with Subscriber

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

the class ApiMgtDAO method initSubscribedAPIDetailed.

private void initSubscribedAPIDetailed(Connection connection, SubscribedAPI subscribedAPI, Subscriber subscriber, ResultSet result) throws SQLException, APIManagementException {
    subscribedAPI.setSubscriptionId(result.getInt("SUBS_ID"));
    subscribedAPI.setSubStatus(result.getString("SUB_STATUS"));
    subscribedAPI.setSubCreatedStatus(result.getString("SUBS_CREATE_STATE"));
    String tierName = result.getString(APIConstants.SUBSCRIPTION_FIELD_TIER_ID);
    String requestedTierName = result.getString(APIConstants.SUBSCRIPTION_FIELD_TIER_ID_PENDING);
    subscribedAPI.setTier(new Tier(tierName));
    subscribedAPI.setRequestedTier(new Tier(requestedTierName));
    subscribedAPI.setUUID(result.getString("SUB_UUID"));
    // setting NULL for subscriber. If needed, Subscriber object should be constructed &
    // passed in
    int applicationId = result.getInt("APP_ID");
    Application application = new Application(result.getString("APP_NAME"), subscriber);
    application.setId(result.getInt("APP_ID"));
    application.setTokenType(result.getString("APP_TOKEN_TYPE"));
    application.setCallbackUrl(result.getString("CALLBACK_URL"));
    application.setUUID(result.getString("APP_UUID"));
    if (multiGroupAppSharingEnabled) {
        application.setGroupId(getGroupId(connection, application.getId()));
        application.setOwner(result.getString("OWNER"));
    }
    subscribedAPI.setApplication(application);
}
Also used : Tier(org.wso2.carbon.apimgt.api.model.Tier) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 18 with Subscriber

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

the class ApiMgtDAO method populateAppRegistrationWorkflowDTO.

public void populateAppRegistrationWorkflowDTO(ApplicationRegistrationWorkflowDTO workflowDTO) throws APIManagementException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    Application application = null;
    Subscriber subscriber = null;
    String registrationEntry = SQLConstants.GET_APPLICATION_REGISTRATION_ENTRY_BY_SUBSCRIBER_SQL;
    try {
        conn = APIMgtDBUtil.getConnection();
        ps = conn.prepareStatement(registrationEntry);
        ps.setString(1, workflowDTO.getExternalWorkflowReference());
        rs = ps.executeQuery();
        while (rs.next()) {
            subscriber = new Subscriber(rs.getString("USER_ID"));
            subscriber.setId(rs.getInt("SUBSCRIBER_ID"));
            application = new Application(rs.getString("NAME"), subscriber);
            application.setId(rs.getInt("APPLICATION_ID"));
            application.setUUID(rs.getString("UUID"));
            application.setTokenType(rs.getString("APP_TYPE"));
            application.setApplicationWorkFlowStatus(rs.getString("APPLICATION_STATUS"));
            application.setCallbackUrl(rs.getString("CALLBACK_URL"));
            application.setDescription(rs.getString("DESCRIPTION"));
            application.setTier(rs.getString("APPLICATION_TIER"));
            workflowDTO.setApplication(application);
            workflowDTO.setKeyType(rs.getString("TOKEN_TYPE"));
            workflowDTO.setUserName(subscriber.getName());
            workflowDTO.setDomainList(rs.getString("ALLOWED_DOMAINS"));
            workflowDTO.setValidityTime(rs.getLong("VALIDITY_PERIOD"));
            String tenantDomain = MultitenantUtils.getTenantDomain(subscriber.getName());
            String keyManagerUUID = rs.getString("KEY_MANAGER");
            workflowDTO.setKeyManager(keyManagerUUID);
            KeyManagerConfigurationDTO keyManagerConfigurationByUUID = getKeyManagerConfigurationByUUID(conn, keyManagerUUID);
            if (keyManagerConfigurationByUUID != null) {
                OAuthAppRequest request = ApplicationUtils.createOauthAppRequest(application.getName(), null, application.getCallbackUrl(), rs.getString("TOKEN_SCOPE"), rs.getString("INPUTS"), application.getTokenType(), keyManagerConfigurationByUUID.getOrganization(), keyManagerConfigurationByUUID.getName());
                request.setMappingId(workflowDTO.getWorkflowReference());
                request.getOAuthApplicationInfo().setApplicationUUID(application.getUUID());
                workflowDTO.setAppInfoDTO(request);
            } else {
                throw new APIManagementException("Error occured while finding the KeyManager from uuid " + keyManagerUUID + ".", ExceptionCodes.KEY_MANAGER_NOT_REGISTERED);
            }
        }
    } catch (SQLException | IOException e) {
        handleException("Error occurred while retrieving an " + "Application Registration Entry for Workflow : " + workflowDTO.getExternalWorkflowReference(), e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, rs);
    }
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 19 with Subscriber

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

the class ApiMgtDAO method getAllAPIUsageByProvider.

/**
 * @param providerName Name of the provider
 * @return UserApplicationAPIUsage of given provider
 * @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to get
 *                                                           UserApplicationAPIUsage for given provider
 */
public UserApplicationAPIUsage[] getAllAPIUsageByProvider(String providerName) throws APIManagementException {
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet result = null;
    try {
        String sqlQuery = SQLConstants.GET_APP_API_USAGE_BY_PROVIDER_SQL;
        connection = APIMgtDBUtil.getConnection();
        ps = connection.prepareStatement(sqlQuery);
        ps.setString(1, APIUtil.replaceEmailDomainBack(providerName));
        result = ps.executeQuery();
        Map<String, UserApplicationAPIUsage> userApplicationUsages = new TreeMap<String, UserApplicationAPIUsage>();
        while (result.next()) {
            int subId = result.getInt("SUBSCRIPTION_ID");
            String userId = result.getString("USER_ID");
            String application = result.getString("APPNAME");
            int appId = result.getInt("APPLICATION_ID");
            String subStatus = result.getString("SUB_STATUS");
            String subsCreateState = result.getString("SUBS_CREATE_STATE");
            String key = userId + "::" + application;
            UserApplicationAPIUsage usage = userApplicationUsages.get(key);
            if (usage == null) {
                usage = new UserApplicationAPIUsage();
                usage.setUserId(userId);
                usage.setApplicationName(application);
                usage.setAppId(appId);
                userApplicationUsages.put(key, usage);
            }
            APIIdentifier apiId = new APIIdentifier(result.getString("API_PROVIDER"), result.getString("API_NAME"), result.getString("API_VERSION"));
            SubscribedAPI apiSubscription = new SubscribedAPI(new Subscriber(userId), apiId);
            apiSubscription.setSubStatus(subStatus);
            apiSubscription.setSubCreatedStatus(subsCreateState);
            apiSubscription.setUUID(result.getString("SUB_UUID"));
            apiSubscription.setTier(new Tier(result.getString("SUB_TIER_ID")));
            Application applicationObj = new Application(result.getString("APP_UUID"));
            apiSubscription.setApplication(applicationObj);
            usage.addApiSubscriptions(apiSubscription);
        }
        return userApplicationUsages.values().toArray(new UserApplicationAPIUsage[userApplicationUsages.size()]);
    } catch (SQLException e) {
        handleException("Failed to find API Usage for :" + providerName, e);
        return null;
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, connection, result);
    }
}
Also used : UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) Tier(org.wso2.carbon.apimgt.api.model.Tier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TreeMap(java.util.TreeMap) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) ResultSet(java.sql.ResultSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 20 with Subscriber

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

the class ApiMgtDAO method getApplicationByUUID.

/**
 * Retrieves the Application which is corresponding to the given UUID String
 *
 * @param uuid UUID of Application
 * @return
 * @throws APIManagementException
 */
public Application getApplicationByUUID(String uuid) throws APIManagementException {
    Connection connection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    int applicationId = 0;
    Application application = null;
    try {
        connection = APIMgtDBUtil.getConnection();
        String query = SQLConstants.GET_APPLICATION_BY_UUID_SQL;
        prepStmt = connection.prepareStatement(query);
        prepStmt.setString(1, uuid);
        rs = prepStmt.executeQuery();
        if (rs.next()) {
            String applicationName = rs.getString("NAME");
            String subscriberId = rs.getString("SUBSCRIBER_ID");
            String subscriberName = rs.getString("USER_ID");
            Subscriber subscriber = new Subscriber(subscriberName);
            subscriber.setId(Integer.parseInt(subscriberId));
            application = new Application(applicationName, subscriber);
            application.setDescription(rs.getString("DESCRIPTION"));
            application.setStatus(rs.getString("APPLICATION_STATUS"));
            application.setCallbackUrl(rs.getString("CALLBACK_URL"));
            applicationId = rs.getInt("APPLICATION_ID");
            application.setId(applicationId);
            application.setGroupId(rs.getString("GROUP_ID"));
            application.setUUID(rs.getString("UUID"));
            application.setTier(rs.getString("APPLICATION_TIER"));
            application.setTokenType(rs.getString("TOKEN_TYPE"));
            application.setOwner(rs.getString("CREATED_BY"));
            application.setOrganization(rs.getString("ORGANIZATION"));
            subscriber.setId(rs.getInt("SUBSCRIBER_ID"));
            if (multiGroupAppSharingEnabled) {
                if (application.getGroupId() == null || application.getGroupId().isEmpty()) {
                    application.setGroupId(getGroupId(connection, application.getId()));
                }
            }
            Timestamp createdTime = rs.getTimestamp("CREATED_TIME");
            application.setCreatedTime(createdTime == null ? null : String.valueOf(createdTime.getTime()));
            try {
                Timestamp updated_time = rs.getTimestamp("UPDATED_TIME");
                application.setLastUpdatedTime(updated_time == null ? null : String.valueOf(updated_time.getTime()));
            } catch (SQLException e) {
                // fixing Timestamp issue with default value '0000-00-00 00:00:00'for existing applications created
                application.setLastUpdatedTime(application.getCreatedTime());
            }
        }
        // Get custom attributes of application
        if (application != null) {
            Map<String, String> applicationAttributes = getApplicationAttributes(connection, applicationId);
            application.setApplicationAttributes(applicationAttributes);
        }
    } catch (SQLException e) {
        handleException("Error while obtaining details of the Application : " + uuid, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
    }
    return application;
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Application(org.wso2.carbon.apimgt.api.model.Application) Timestamp(java.sql.Timestamp)

Aggregations

Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)98 Test (org.junit.Test)64 Application (org.wso2.carbon.apimgt.api.model.Application)63 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)60 PreparedStatement (java.sql.PreparedStatement)39 SQLException (java.sql.SQLException)39 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)39 ResultSet (java.sql.ResultSet)37 Connection (java.sql.Connection)31 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)25 Tier (org.wso2.carbon.apimgt.api.model.Tier)20 ArrayList (java.util.ArrayList)19 HashSet (java.util.HashSet)19 Date (java.util.Date)14 HashMap (java.util.HashMap)11 LinkedHashSet (java.util.LinkedHashSet)10 JSONObject (org.json.simple.JSONObject)10 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)10 TreeMap (java.util.TreeMap)9