Search in sources :

Example 11 with Subscriber

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

the class RegistrySearchUtilTestCase method testDevPortalUserQueryInDevPortal.

@Test
public void testDevPortalUserQueryInDevPortal() throws APIPersistenceException {
    // Normal dev portal api listing
    String inputQuery = "";
    UserContext ctx = new UserContext("devUser", organization, null, devPortalRoles);
    String searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    String expected = "store_view_roles=(null OR internal\\/subscriber OR internal\\/everyone)&name=*" + "&enableStore=(true OR null)&group=true&group.field=name&group.ngroups=true&group.sort=versionTimestamp desc" + "&lcState=(PUBLISHED OR PROTOTYPED)";
    Assert.assertEquals("Generated query mismatched. ", expected, searchQuery);
    // search for 'test' in description
    inputQuery = "description:test";
    expected = "store_view_roles=(null OR internal\\/subscriber OR internal\\/everyone)&" + "description=*test*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for description search. ", expected, searchQuery);
    // search for provider 'pubuser'
    inputQuery = "provider:pubuser";
    expected = "store_view_roles=(null OR internal\\/subscriber OR internal\\/everyone)&" + "provider=*pubuser*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for provider search. ", expected, searchQuery);
    // search for propertyname 'test'
    inputQuery = "property_name:test";
    expected = "store_view_roles=(null OR internal\\/subscriber OR internal\\/everyone)" + "&api_meta.property_name__display=*test*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for property search. ", expected, searchQuery);
}
Also used : UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) Test(org.junit.Test)

Example 12 with Subscriber

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

the class SubscriptionDataStore method initializeStore.

/**
 * This method is used to initilize a task to retrieve subscriptions.
 */
private void initializeStore() {
    this.subscribersMap = new ConcurrentHashMap<>();
    this.throttlingStatusMap = new ConcurrentHashMap<>();
    executor.submit(() -> {
        List<WebhooksDTO> subscriptions = loadSubscriptions();
        for (WebhooksDTO subscriber : subscriptions) {
            String subscriptionKey = subscriber.getApiUUID() + "_" + subscriber.getTopicName();
            String throttleKey = subscriber.getAppID() + "_" + subscriber.getApiUUID();
            addSubscriber(subscriptionKey, subscriber);
            throttlingStatusMap.put(throttleKey, false);
        }
    });
}
Also used : WebhooksDTO(org.wso2.carbon.apimgt.impl.dto.WebhooksDTO)

Example 13 with Subscriber

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

the class ApiMgtDAO method getAPIRatings.

/**
 * @param uuid API uuid
 * @param conn       Database connection
 * @throws APIManagementException if failed to get API Ratings
 */
private JSONArray getAPIRatings(String uuid, Connection conn) throws APIManagementException, SQLException {
    PreparedStatement ps = null;
    PreparedStatement psSubscriber = null;
    ResultSet rs = null;
    ResultSet rsSubscriber = null;
    JSONArray ratingArray = new JSONArray();
    int userRating = 0;
    String ratingId = null;
    int id = -1;
    int subscriberId = -1;
    try {
        // Get API Id
        id = getAPIID(uuid, conn);
        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
        String sqlQuery = SQLConstants.GET_API_ALL_RATINGS_SQL;
        ps = conn.prepareStatement(sqlQuery);
        ps.setInt(1, id);
        rs = ps.executeQuery();
        while (rs.next()) {
            JSONObject ratingObj = new JSONObject();
            String subscriberName = null;
            ratingId = rs.getString("RATING_ID");
            subscriberId = rs.getInt("SUBSCRIBER_ID");
            userRating = rs.getInt("RATING");
            ratingObj.put(APIConstants.RATING_ID, ratingId);
            // SQL Query to get subscriber name
            String sqlSubscriberQuery = SQLConstants.GET_SUBSCRIBER_NAME_FROM_ID_SQL;
            psSubscriber = conn.prepareStatement(sqlSubscriberQuery);
            psSubscriber.setInt(1, subscriberId);
            rsSubscriber = psSubscriber.executeQuery();
            while (rsSubscriber.next()) {
                subscriberName = rsSubscriber.getString("USER_ID");
            }
            ratingObj.put(APIConstants.USER_NAME, subscriberName);
            ratingObj.put(APIConstants.RATING, userRating);
            ratingArray.add(ratingObj);
        }
    } catch (SQLException e) {
        handleException("Failed to retrieve API ratings ", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, null, rs);
        APIMgtDBUtil.closeAllConnections(psSubscriber, null, rsSubscriber);
    }
    return ratingArray;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONObject(org.json.simple.JSONObject) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) JSONArray(org.json.simple.JSONArray) PreparedStatement(java.sql.PreparedStatement)

Example 14 with Subscriber

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

the class ApiMgtDAO method getSubscriber.

/**
 * returns a subscriber record for given username,tenant Id
 *
 * @param username   UserName
 * @param tenantId   Tenant Id
 * @param connection
 * @return Subscriber
 * @throws APIManagementException if failed to get subscriber
 */
private Subscriber getSubscriber(String username, int tenantId, Connection connection) throws APIManagementException {
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    Subscriber subscriber = null;
    String sqlQuery;
    if (forceCaseInsensitiveComparisons) {
        sqlQuery = SQLConstants.GET_SUBSCRIBER_CASE_INSENSITIVE_SQL;
    } else {
        sqlQuery = SQLConstants.GET_SUBSCRIBER_DETAILS_SQL;
    }
    try {
        prepStmt = connection.prepareStatement(sqlQuery);
        prepStmt.setString(1, username);
        prepStmt.setInt(2, tenantId);
        rs = prepStmt.executeQuery();
        if (rs.next()) {
            subscriber = new Subscriber(rs.getString("USER_ID"));
            subscriber.setEmail(rs.getString("EMAIL_ADDRESS"));
            subscriber.setId(rs.getInt("SUBSCRIBER_ID"));
            subscriber.setSubscribedDate(rs.getDate("DATE_SUBSCRIBED"));
            subscriber.setTenantId(rs.getInt("TENANT_ID"));
            return subscriber;
        }
    } catch (SQLException e) {
        handleException("Error when reading the application information from" + " the persistence store.", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(prepStmt, null, rs);
    }
    return subscriber;
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 15 with Subscriber

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

the class ApiMgtDAO method getApplicationsWithPagination.

/**
 * Retrieve the applications by user/application name
 *
 * @param user
 * @param owner
 * @param tenantId
 * @param limit
 * @param offset
 * @param sortBy
 * @param sortOrder
 * @param appName
 * @return
 * @throws APIManagementException
 */
public Application[] getApplicationsWithPagination(String user, String owner, int tenantId, int limit, int offset, String sortBy, String sortOrder, String appName) throws APIManagementException {
    Connection connection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    String sqlQuery = null;
    List<Application> applicationList = new ArrayList<>();
    sqlQuery = SQLConstantManagerFactory.getSQlString("GET_APPLICATIONS_BY_TENANT_ID");
    Application[] applications = null;
    try {
        connection = APIMgtDBUtil.getConnection();
        String driverName = connection.getMetaData().getDriverName();
        if (driverName.contains("Oracle")) {
            limit = offset + limit;
        }
        sqlQuery = sqlQuery.replace("$1", sortBy);
        sqlQuery = sqlQuery.replace("$2", sortOrder);
        prepStmt = connection.prepareStatement(sqlQuery);
        prepStmt.setInt(1, tenantId);
        prepStmt.setString(2, "%" + owner + "%");
        prepStmt.setString(3, "%" + appName + "%");
        prepStmt.setInt(4, offset);
        prepStmt.setInt(5, limit);
        rs = prepStmt.executeQuery();
        Application application;
        while (rs.next()) {
            String applicationName = rs.getString("NAME");
            String subscriberName = rs.getString("CREATED_BY");
            Subscriber subscriber = new Subscriber(subscriberName);
            application = new Application(applicationName, subscriber);
            application.setName(applicationName);
            application.setId(rs.getInt("APPLICATION_ID"));
            application.setUUID(rs.getString("UUID"));
            application.setGroupId(rs.getString("GROUP_ID"));
            subscriber.setTenantId(rs.getInt("TENANT_ID"));
            subscriber.setId(rs.getInt("SUBSCRIBER_ID"));
            application.setStatus(rs.getString("APPLICATION_STATUS"));
            application.setOwner(subscriberName);
            applicationList.add(application);
        }
        applications = applicationList.toArray(new Application[applicationList.size()]);
    } catch (SQLException e) {
        handleException("Error while obtaining details of the Application for tenant id : " + tenantId, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
    }
    return applications;
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Application(org.wso2.carbon.apimgt.api.model.Application)

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