Search in sources :

Example 1 with Subscription

use of org.wso2.carbon.identity.event.bean.Subscription in project product-microgateway by wso2.

the class AuthenticationFailureTestCase method testWithInvalidSubscription.

@Test(description = "Test with invalid subscription")
public void testWithInvalidSubscription() throws Exception {
    Map<String, String> headers = new HashMap<>();
    headers.put(HttpHeaderNames.AUTHORIZATION.toString(), "Bearer " + invalidSubscriptionToken);
    HttpResponse response = HttpClientRequest.doGet(getServiceURLHttp("pizzashack/1.0.0/menu"), headers);
    Assert.assertNotNull(response);
    Assert.assertEquals(response.getResponseCode(), 403, "Response code mismatched");
    Assert.assertTrue(response.getData().contains("Resource forbidden"));
}
Also used : HashMap(java.util.HashMap) HttpResponse(org.wso2.micro.gateway.tests.util.HttpResponse) Test(org.testng.annotations.Test)

Example 2 with Subscription

use of org.wso2.carbon.identity.event.bean.Subscription in project product-microgateway by wso2.

the class ThrottlePolicyGenerator method generate.

/**
 * Generate ballerina and stream source for a given app and subs policies.
 *
 * @param outPath     Destination file path to save generated source files. If not provided
 *                    {@code definitionPath} will be used as the default destination path
 * @param projectName Project name
 * @throws IOException when file operations fail
 */
public void generate(String outPath, String projectName) throws IOException {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    String policyFileLocation = CmdUtils.getProjectDirectoryPath(projectName) + File.separator + CliConstants.GW_DIST_POLICIES_FILE;
    ThrottlePolicyListMapper throttlePolicyListMapper = mapper.readValue(new File(policyFileLocation), ThrottlePolicyListMapper.class);
    // read application throttle policies and subscription throttle policies
    List<ThrottlePolicyMapper> applicationPolicies = throttlePolicyListMapper.getApplicationPolicies();
    List<ThrottlePolicyMapper> subscriptionPolicies = throttlePolicyListMapper.getSubscriptionPolicies();
    List<ThrottlePolicyMapper> resourcePolicies = throttlePolicyListMapper.getResourcePolicies();
    if (applicationPolicies == null && subscriptionPolicies == null && resourcePolicies == null) {
        return;
    }
    checkDuplicatePolicyNames(applicationPolicies, subscriptionPolicies, resourcePolicies);
    List<GenSrcFile> genFiles = new ArrayList<>();
    GenSrcFile initGenFile = generateInitBal(applicationPolicies, subscriptionPolicies, resourcePolicies);
    genFiles.add(initGenFile);
    CodegenUtils.writeGeneratedSources(genFiles, Paths.get(outPath), true);
}
Also used : GenSrcFile(org.wso2.apimgt.gateway.cli.model.template.GenSrcFile) ThrottlePolicyListMapper(org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottlePolicyListMapper) ThrottlePolicyMapper(org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottlePolicyMapper) ArrayList(java.util.ArrayList) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) GenSrcFile(org.wso2.apimgt.gateway.cli.model.template.GenSrcFile) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with Subscription

use of org.wso2.carbon.identity.event.bean.Subscription in project product-microgateway by wso2.

the class HashUtils method detectChanges.

/**
 * Generate hashes for the specified apis, subscription and application policies, then compare with the previously
 * generated hashes and detect if there are changes with them.
 *
 * @param apis                 APIs list
 * @param subscriptionPolicies Subscription Policies list
 * @param appPolicies          Application policies list
 * @param projectName          Name of the project
 * @return true if there are changes detected vs the previous check
 * @throws HashingException error while change detection
 */
public static boolean detectChanges(List<ExtendedAPI> apis, List<SubscriptionThrottlePolicyDTO> subscriptionPolicies, List<ApplicationThrottlePolicyDTO> appPolicies, String projectName) throws HashingException {
    boolean hasChanges = true;
    Map<String, String> allHashesMap = new HashMap<>();
    Map<String, String> apiHashesMap = getMapOfHashes(apis);
    Map<String, String> appPolicyHashesMap = getMapOfHashes(appPolicies);
    Map<String, String> subsPolicyHashesMap = getMapOfHashes(subscriptionPolicies);
    logger.debug("API calculated hashes {}", apiHashesMap);
    logger.debug("App policy calculated hashes {}", appPolicyHashesMap);
    logger.debug("Subscription policy calculated hashes {}", subsPolicyHashesMap);
    allHashesMap.putAll(apiHashesMap);
    allHashesMap.putAll(appPolicyHashesMap);
    allHashesMap.putAll(subsPolicyHashesMap);
    try {
        Map<String, String> storedHashes = loadStoredResourceHashes(projectName);
        if (equalMaps(storedHashes, allHashesMap)) {
            logger.debug("No changes detected after calculating hashes.");
            hasChanges = false;
        } else {
            logger.debug("Storing calculated resource hashes.");
            storeResourceHashes(allHashesMap, projectName);
            logger.debug("Storing calculated resource hashes success.");
        }
    } catch (IOException e) {
        throw new HashingException("Error while resource change detection", e);
    }
    return hasChanges;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) HashingException(org.wso2.apimgt.gateway.cli.exception.HashingException)

Example 4 with Subscription

use of org.wso2.carbon.identity.event.bean.Subscription in project carbon-identity-framework by wso2.

the class EventDistributionTask method run.

@Override
public void run() {
    running = true;
    // Run forever until stop the bundle. Will stop in eventQueue.take()
    while (running) {
        try {
            final PublisherEvent event = eventQueue.take();
            for (final NotificationSendingModule module : notificationSendingModules) {
                // If the module is subscribed to the event, module will be executed.
                try {
                    if (module.isSubscribed(event)) {
                        // Create a runnable and submit to the thread pool for sending message.
                        Runnable msgSender = new Runnable() {

                            @Override
                            public void run() {
                                if (log.isDebugEnabled()) {
                                    log.debug("Executing " + module.getModuleName() + " on event" + event.getEventName());
                                }
                                try {
                                    module.sendMessage(event);
                                } catch (NotificationManagementException e) {
                                    log.error("Error while invoking notification sending module " + module.getModuleName(), e);
                                }
                            }
                        };
                        NotificationManagementServiceDataHolder.getInstance().getThreadPool().submit(msgSender);
                    }
                } catch (NotificationManagementException e) {
                    log.error("Error while getting subscription status from notification module " + module.getModuleName(), e);
                }
            }
        } catch (InterruptedException e) {
            log.error("Error while picking up event from event queue", e);
        }
    }
}
Also used : PublisherEvent(org.wso2.carbon.identity.notification.mgt.bean.PublisherEvent)

Example 5 with Subscription

use of org.wso2.carbon.identity.event.bean.Subscription in project carbon-identity-framework by wso2.

the class NotificationMgtConfigBuilder method buildModuleConfigurations.

/**
 * Building per module configuration objects
 *
 * @param moduleName Name of the module
 * @return ModuleConfiguration object which has configurations for the given module name
 */
private ModuleConfiguration buildModuleConfigurations(String moduleName) {
    Properties moduleProperties = getModuleProperties(moduleName);
    List<Subscription> subscriptionList = buildSubscriptionList(moduleName, moduleProperties);
    return new ModuleConfiguration(getModuleProperties(moduleName), subscriptionList);
}
Also used : ModuleConfiguration(org.wso2.carbon.identity.notification.mgt.bean.ModuleConfiguration) Properties(java.util.Properties) Subscription(org.wso2.carbon.identity.notification.mgt.bean.Subscription)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)79 ArrayList (java.util.ArrayList)74 Test (org.junit.Test)61 Test (org.testng.annotations.Test)61 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)58 SQLException (java.sql.SQLException)55 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)51 Connection (java.sql.Connection)49 PreparedStatement (java.sql.PreparedStatement)48 ResultSet (java.sql.ResultSet)39 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)37 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)35 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)34 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)34 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)31 Application (org.wso2.carbon.apimgt.core.models.Application)30 API (org.wso2.carbon.apimgt.core.models.API)28 Response (javax.ws.rs.core.Response)24 Application (org.wso2.carbon.apimgt.api.model.Application)22 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)22