Search in sources :

Example 26 with Topic

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

the class WebhooksApiServiceImpl method webhooksSubscriptionsGet.

public Response webhooksSubscriptionsGet(String applicationId, String apiId, String xWSO2Tenant, MessageContext messageContext) {
    if (StringUtils.isNotEmpty(applicationId)) {
        String username = RestApiCommonUtil.getLoggedInUsername();
        try {
            APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
            WebhookSubscriptionListDTO WebhookSubscriptionListDTO;
            Set<Subscription> subscriptionSet = apiConsumer.getTopicSubscriptions(applicationId, apiId);
            WebhookSubscriptionListDTO = AsyncAPIMappingUtil.fromSubscriptionListToDTO(subscriptionSet);
            return Response.ok().entity(WebhookSubscriptionListDTO).build();
        } catch (APIManagementException e) {
            if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
                RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
            } else {
                RestApiUtil.handleInternalServerError("Failed to get topic subscriptions of Async API " + apiId, e, log);
            }
        }
    } else {
        RestApiUtil.handleBadRequest("Application Id cannot be empty", log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) WebhookSubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.WebhookSubscriptionListDTO) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) Subscription(org.wso2.carbon.apimgt.api.model.webhooks.Subscription)

Example 27 with Topic

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

the class AsyncAPIMappingUtil method fromTopicToDTO.

/**
 * Converts Topic object to DTO.
 *
 * @param topic Topic object
 * @return TopicDTO
 */
public static TopicDTO fromTopicToDTO(Topic topic) {
    TopicDTO topicDTO = new TopicDTO();
    topicDTO.setApiId(topic.getApiId());
    topicDTO.setName(topic.getName());
    topicDTO.setType(topic.getType());
    return topicDTO;
}
Also used : TopicDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.TopicDTO)

Example 28 with Topic

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

the class SampleTestObjectCreator method createDefaultSiddhiAppforAppPolicy.

public static String createDefaultSiddhiAppforAppPolicy() {
    ApplicationPolicy policy = createDefaultApplicationPolicy();
    RequestCountLimit limit = (RequestCountLimit) createDefaultApplicationPolicy().getDefaultQuotaPolicy().getLimit();
    String siddhiApp = "@App:name('application_" + policy.getPolicyName() + "')\n" + "@App:description('ExecutionPlan for app_" + policy.getPolicyName() + "')\n" + "@source(type='inMemory', topic='apim', @map(type='passThrough'))\n" + "define stream RequestStream (messageID string, appKey string, appTier string, " + "subscriptionKey string," + " apiKey string, apiTier string, subscriptionTier string, resourceKey string," + " resourceTier string," + " userId string,  apiContext string, apiVersion string, appTenant string, apiTenant string," + " appId " + "string, apiName string, propertiesMap string);\n" + "@sink(type='jms', @map(type='text'),\n" + "factory.initial='org.wso2.andes.jndi.PropertiesFileInitialContextFactory'," + " provider.url='tcp://localhost:5672', destination='TEST.FOO', connection.factory." + "type='topic',\n" + "connection.factory.jndi.name='TopicConnectionFactory')\n" + "define stream GlobalThrottleStream (throttleKey string, isThrottled bool" + ", expiryTimeStamp long);\n" + "FROM RequestStream\n" + "SELECT messageID, (appTier == '" + policy.getPolicyName() + "') AS isEligible, appKey AS throttleKey, " + "propertiesMap\n" + "INSERT INTO EligibilityStream;\n" + "FROM EligibilityStream[isEligible==true]#throttler:timeBatch(" + policy.getDefaultQuotaPolicy().getLimit().getUnitTime() + " " + policy.getDefaultQuotaPolicy().getLimit().getTimeUnit() + ", 0)\n" + "select throttleKey, (count(messageID) >= " + limit.getRequestCount() + ")" + " as isThrottled, expiryTimeStamp group by throttleKey\n" + "INSERT ALL EVENTS into ResultStream;\n" + "from ResultStream#throttler:emitOnStateChange(throttleKey, isThrottled)\n" + "select *\n" + "insert into GlobalThrottleStream;\n";
    return siddhiApp;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)

Example 29 with Topic

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

the class SampleTestObjectCreator method createDefaultSiddhiAppForAPILevelDefaultThrottlePolicy.

public static String createDefaultSiddhiAppForAPILevelDefaultThrottlePolicy() {
    APIPolicy apiPolicy = createDefaultAPIPolicy();
    String siddhiApp = "\n@App:name('resource_" + apiPolicy.getPolicyName() + "_default')" + "\n@App:description('ExecutionPlan for resource_" + apiPolicy.getPolicyName() + "_default')\n" + "\n@source(type='inMemory', topic='apim', @map(type='passThrough'))" + "\ndefine stream RequestStream (messageID string, appKey string," + " appTier string, subscriptionKey string," + " apiKey string, apiTier string, subscriptionTier string, resourceKey string," + " resourceTier string, userId string,  apiContext string, apiVersion string, appTenant string," + " apiTenant string," + " appId string, apiName string, propertiesMap string);\n" + "\n@sink(type='jms', @map(type='text')," + "\nfactory.initial='org.wso2.andes.jndi.PropertiesFileInitialContextFactory'," + " provider.url='tcp://localhost:5672'," + " destination='TEST.FOO', connection.factory.type='topic'," + "\nconnection.factory.jndi.name='TopicConnectionFactory')" + "\ndefine stream GlobalThrottleStream (throttleKey string, isThrottled bool," + " expiryTimeStamp long);\n" + "\nFROM RequestStream" + "\nSELECT messageID, (resourceTier == 'SampleAPIPolicy' AND " + "NOT(((3232238595l<=cast(map:get(propertiesMap,'ip'),'Long')" + " AND 3232258067l>=cast(map:get(propertiesMap,'ip'),'Long')) AND " + "(cast(map:get(propertiesMap,'ip'),'Long')==2066353720l)) " + "OR ((regex:find('Chrome',cast(map:get(propertiesMap,'Browser'),'string')))" + " AND (regex:find('attributed'," + "cast(map:get(propertiesMap,'/path/path2'),'string')))" + " AND (cast(map:get(propertiesMap,'Location'),'string')=='Colombo'))))" + " AS isEligible, resourceKey AS throttleKey, propertiesMap" + "\nINSERT INTO EligibilityStream;\n" + "\nFROM EligibilityStream[isEligible==true]#throttler:timeBatch(1000 s, 0)" + "\nselect throttleKey, (count(messageID) >= 10000) as isThrottled," + " expiryTimeStamp group by throttleKey" + "\nINSERT ALL EVENTS into ResultStream;\n" + "\nfrom ResultStream#throttler:emitOnStateChange(throttleKey, isThrottled)" + "\nselect *" + "\ninsert into GlobalThrottleStream;\n";
    return siddhiApp;
}
Also used : APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 30 with Topic

use of org.wso2.carbon.apimgt.api.model.webhooks.Topic in project siddhi by wso2.

the class InMemoryTransportTestCase method inMemoryWithFailingSource.

@Test
public void inMemoryWithFailingSource() throws InterruptedException {
    log.info("Test failing inMemorySource");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='testFailingInMemory', topic='WSO2', @map(type='passThrough')) " + "define stream FooStream (symbol string, price float, volume long); " + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                wso2Count.incrementAndGet();
            }
        }
    });
    siddhiAppRuntime.start();
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    InMemoryBroker.publish("IBM", new Event(System.currentTimeMillis(), new Object[] { "IBM", 75.6f, 100L }));
    TestFailingInMemorySource.fail = true;
    TestFailingInMemorySource.connectionCallback.onError(new ConnectionUnavailableException("Connection Lost"));
    Thread.sleep(500);
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
    TestFailingInMemorySource.fail = false;
    Thread.sleep(5500);
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    InMemoryBroker.publish("IBM", new Event(System.currentTimeMillis(), new Object[] { "IBM", 75.6f, 100L }));
    // assert event count
    AssertJUnit.assertEquals("Number of WSO2 events", 2, wso2Count.get());
    AssertJUnit.assertEquals("Number of errors", 1, TestFailingInMemorySource.numberOfErrorOccurred);
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) ConnectionUnavailableException(org.wso2.siddhi.core.exception.ConnectionUnavailableException) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)28 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)25 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)25 InMemoryBroker (org.wso2.siddhi.core.util.transport.InMemoryBroker)16 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)13 Event (org.wso2.siddhi.core.event.Event)7 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 HashMap (java.util.HashMap)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 Timestamp (java.sql.Timestamp)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 LinkedHashSet (java.util.LinkedHashSet)4 JsonParser (com.google.gson.JsonParser)3 ResultSet (java.sql.ResultSet)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)3