Search in sources :

Example 6 with Broker

use of org.wso2.broker.core.Broker in project carbon-apimgt by wso2.

the class BrokerManager method loadUsers.

/**
 * Loads the users from users.yaml during broker startup
 */
private static void loadUsers() throws ConfigurationException {
    Path usersYamlFile;
    String usersFilePath = System.getProperty(BrokerSecurityConstants.SYSTEM_PARAM_USERS_CONFIG);
    if (usersFilePath == null || usersFilePath.trim().isEmpty()) {
        // use current path.
        usersYamlFile = Paths.get("", BrokerSecurityConstants.USERS_FILE_NAME).toAbsolutePath();
    } else {
        usersYamlFile = Paths.get(usersFilePath).toAbsolutePath();
    }
    ConfigProvider configProvider = ConfigProviderFactory.getConfigProvider(usersYamlFile, null);
    UsersFile usersFile = configProvider.getConfigurationObject(BrokerSecurityConstants.USERS_CONFIG_NAMESPACE, UsersFile.class);
    if (usersFile != null) {
        List<User> users = usersFile.getUsers();
        for (User user : users) {
            UserStoreManager.addUser(user);
        }
    }
}
Also used : Path(java.nio.file.Path) User(org.wso2.broker.core.security.authentication.user.User) BrokerConfigProvider(org.wso2.broker.common.BrokerConfigProvider) ConfigProvider(org.wso2.carbon.config.provider.ConfigProvider) UsersFile(org.wso2.broker.core.security.authentication.user.UsersFile)

Example 7 with Broker

use of org.wso2.broker.core.Broker in project carbon-apimgt by wso2.

the class BrokerManager method initConfigProvider.

/**
 * Loads configurations during the broker start up.
 * method will try to <br/>
 * (1) Load the configuration file specified in 'broker.file' (e.g. -Dbroker.file=<FilePath>). <br/>
 * (2) If -Dbroker.file is not specified, the broker.yaml file exists in current directory and load it. <br/>
 * <p>
 * <b>Note: </b> if provided configuration file cannot be read broker will not start.
 *
 * @param startupContext startup context of the broker
 */
private static void initConfigProvider(StartupContext startupContext) throws ConfigurationException {
    Path brokerYamlFile;
    String brokerFilePath = System.getProperty(BrokerConfiguration.SYSTEM_PARAM_BROKER_CONFIG_FILE);
    if (brokerFilePath == null || brokerFilePath.trim().isEmpty()) {
        // use current path.
        brokerYamlFile = Paths.get("", BrokerConfiguration.BROKER_FILE_NAME).toAbsolutePath();
    } else {
        brokerYamlFile = Paths.get(brokerFilePath).toAbsolutePath();
    }
    ConfigProvider configProvider = ConfigProviderFactory.getConfigProvider(brokerYamlFile, null);
    startupContext.registerService(BrokerConfigProvider.class, (BrokerConfigProvider) configProvider::getConfigurationObject);
}
Also used : Path(java.nio.file.Path) BrokerConfigProvider(org.wso2.broker.common.BrokerConfigProvider) ConfigProvider(org.wso2.carbon.config.provider.ConfigProvider)

Example 8 with Broker

use of org.wso2.broker.core.Broker in project carbon-apimgt by wso2.

the class BundleActivator method start.

@Activate
protected void start(BundleContext bundleContext) {
    try {
        // Set default timestamp to UTC
        java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("Etc/UTC"));
        Context ctx = jndiContextManager.newInitialContext();
        DataSource dataSourceAMDB = new DataSourceImpl((HikariDataSource) ctx.lookup("java:comp/env/jdbc/WSO2AMDB"));
        DAOUtil.initialize(dataSourceAMDB);
        boolean isAnalyticsEnabled = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getAnalyticsConfigurations().isEnabled();
        if (isAnalyticsEnabled) {
            DataSource dataSourceStatDB = new DataSourceImpl((HikariDataSource) ctx.lookup("java:comp/env/jdbc/WSO2AMSTATSDB"));
            DAOUtil.initializeAnalyticsDataSource(dataSourceStatDB);
        }
        WorkflowExtensionsConfigBuilder.build(configProvider);
        ServiceDiscoveryConfigBuilder.build(configProvider);
        ContainerBasedGatewayConfigBuilder.build(configProvider);
        BrokerManager.start();
        Broker broker = new BrokerImpl();
        BrokerUtil.initialize(broker);
    } catch (NamingException e) {
        log.error("Error occurred while jndi lookup", e);
    }
    // deploying default policies
    try {
        ThrottlerUtil.addDefaultAdvancedThrottlePolicies();
        if (log.isDebugEnabled()) {
            log.debug("Checked default throttle policies successfully");
        }
    } catch (APIManagementException e) {
        log.error("Error occurred while deploying default policies", e);
    }
    // securing files
    try {
        boolean fileEncryptionEnabled = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getFileEncryptionConfigurations().isEnabled();
        if (fileEncryptionEnabled) {
            FileEncryptionUtility fileEncryptionUtility = FileEncryptionUtility.getInstance();
            fileEncryptionUtility.init();
            fileEncryptionUtility.encryptFiles();
        }
    } catch (APIManagementException e) {
        log.error("Error occurred while encrypting files", e);
    }
}
Also used : Context(javax.naming.Context) BundleContext(org.osgi.framework.BundleContext) BrokerImpl(org.wso2.carbon.apimgt.core.impl.BrokerImpl) Broker(org.wso2.carbon.apimgt.core.api.Broker) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) FileEncryptionUtility(org.wso2.carbon.apimgt.core.impl.FileEncryptionUtility) DataSourceImpl(org.wso2.carbon.apimgt.core.dao.impl.DataSourceImpl) NamingException(javax.naming.NamingException) DataSource(org.wso2.carbon.apimgt.core.dao.impl.DataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Activate(org.osgi.service.component.annotations.Activate)

Example 9 with Broker

use of org.wso2.broker.core.Broker in project carbon-apimgt by wso2.

the class BrokerUtil method publishToTopic.

/**
 * Publish to broker topic
 *
 * @param topicName     publishing topic name
 * @param gatewayEvent    topic message data object
 */
public static void publishToTopic(String topicName, GatewayEvent gatewayEvent) throws GatewayException {
    TopicSession topicSession = null;
    Topic topic = null;
    TopicPublisher topicPublisher = null;
    TopicConnection topicConnection = null;
    try {
        topicConnection = getTopicConnection();
        topicConnection.start();
        topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        topic = topicSession.createTopic(topicName);
        topicPublisher = topicSession.createPublisher(topic);
        TextMessage textMessage = topicSession.createTextMessage(new Gson().toJson(gatewayEvent));
        topicPublisher.publish(textMessage);
    } catch (JMSException e) {
        String errorMessage = "Error occurred while publishing " + gatewayEvent.getEventType() + " event to JMS " + "topic :" + topicName;
        log.error(errorMessage, e);
        throw new GatewayException(errorMessage, ExceptionCodes.GATEWAY_EXCEPTION);
    } catch (BrokerException e) {
        String errorMessage = "Error occurred while obtaining broker topic connection for topic : " + topicName;
        log.error(errorMessage, e);
        throw new GatewayException(errorMessage, ExceptionCodes.GATEWAY_EXCEPTION);
    } finally {
        if (topicPublisher != null) {
            try {
                topicPublisher.close();
            } catch (JMSException e) {
                log.error("Error occurred while closing topic publisher for topic : " + topicName);
            }
        }
        if (topicSession != null) {
            try {
                topicSession.close();
            } catch (JMSException e) {
                log.error("Error occurred while closing topic session for topic : " + topicName);
            }
        }
        if (topicConnection != null) {
            try {
                topicConnection.close();
            } catch (JMSException e) {
                log.error("Error occurred while closing topic connection for topic : " + topicName);
            }
        }
    }
}
Also used : TopicSession(javax.jms.TopicSession) BrokerException(org.wso2.carbon.apimgt.core.exception.BrokerException) TopicPublisher(javax.jms.TopicPublisher) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) Gson(com.google.gson.Gson) JMSException(javax.jms.JMSException) Topic(javax.jms.Topic) TopicConnection(javax.jms.TopicConnection) TextMessage(javax.jms.TextMessage)

Example 10 with Broker

use of org.wso2.broker.core.Broker in project carbon-apimgt by wso2.

the class APIGatewayPublisherImplTestCase method testDeleteAPIWhenLabelsAreNull.

@Test
public void testDeleteAPIWhenLabelsAreNull() throws GatewayException, ContainerBasedGatewayException {
    APIGatewayPublisherImpl apiGatewayPublisher = new APIGatewayPublisherImpl();
    Broker broker = Mockito.mock(BrokerImpl.class, Mockito.RETURNS_DEEP_STUBS);
    BrokerUtil.initialize(broker);
    ContainerBasedGatewayGenerator containerBasedGatewayGenerator = Mockito.mock(ContainerBasedGatewayGenerator.class);
    apiGatewayPublisher.setContainerBasedGatewayGenerator(containerBasedGatewayGenerator);
    API api = SampleTestObjectCreator.createDefaultAPI().lifeCycleStatus(APIStatus.PUBLISHED.getStatus()).hasOwnGateway(true).labels(null).build();
    apiGatewayPublisher.deleteAPI(api);
    Mockito.verify(containerBasedGatewayGenerator, Mockito.times(0)).removeContainerBasedGateway("label", api);
}
Also used : Broker(org.wso2.carbon.apimgt.core.api.Broker) API(org.wso2.carbon.apimgt.core.models.API) Test(org.junit.Test)

Aggregations

Broker (org.wso2.carbon.apimgt.core.api.Broker)6 Test (org.junit.Test)5 API (org.wso2.carbon.apimgt.core.models.API)5 ArrayList (java.util.ArrayList)4 BrokerConfigProvider (org.wso2.broker.common.BrokerConfigProvider)3 APIEvent (org.wso2.carbon.apimgt.core.models.events.APIEvent)3 ApplicationEvent (org.wso2.carbon.apimgt.core.models.events.ApplicationEvent)3 BlockEvent (org.wso2.carbon.apimgt.core.models.events.BlockEvent)3 PolicyEvent (org.wso2.carbon.apimgt.core.models.events.PolicyEvent)3 SubscriptionEvent (org.wso2.carbon.apimgt.core.models.events.SubscriptionEvent)3 HikariDataSource (com.zaxxer.hikari.HikariDataSource)2 Path (java.nio.file.Path)2 GatewayException (org.wso2.carbon.apimgt.core.exception.GatewayException)2 ConfigProvider (org.wso2.carbon.config.provider.ConfigProvider)2 Gson (com.google.gson.Gson)1 JMSException (javax.jms.JMSException)1 TextMessage (javax.jms.TextMessage)1 Topic (javax.jms.Topic)1 TopicConnection (javax.jms.TopicConnection)1 TopicPublisher (javax.jms.TopicPublisher)1