Search in sources :

Example 66 with Broker

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

the class SolaceNotifierUtils method getThirdPartySolaceBrokerEnvironmentNameOfAPIDeployment.

/**
 * Get third party Solace broker environment Name for API deployment
 *
 * @param api Name of the API
 * @return String of the name of environment in Solace broker
 * @throws APIManagementException is error occurs when getting the name of the environment name
 */
private String getThirdPartySolaceBrokerEnvironmentNameOfAPIDeployment(API api) throws APIManagementException {
    apiMgtDAO = ApiMgtDAO.getInstance();
    Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
    List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentsByApiUUID(api.getUuid());
    for (APIRevisionDeployment deployment : deployments) {
        String environmentName = deployment.getDeployment();
        if (gatewayEnvironments.containsKey(environmentName)) {
            Environment deployedEnvironment = gatewayEnvironments.get(environmentName);
            if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(deployedEnvironment.getProvider())) {
                return environmentName;
            }
        }
    }
    return null;
}
Also used : Environment(org.wso2.carbon.apimgt.api.model.Environment) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)

Example 67 with Broker

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

the class SolaceStoreUtils method getSolaceURLsInfo.

/**
 * Get SolaceUrlsInfo using admin APIs and map into DTOs to parse Devportal
 *
 * @param solaceEnvironment Solace environment Object
 * @param organizationName Solace broker organization name
 * @param environmentName      Name of the  Solace environment
 * @param availableProtocols List of available protocols
 * @return List of SolaceURLsDTO
 * @throws APIManagementException if error occurred when creating
 */
public static List<SolaceURLsDTO> getSolaceURLsInfo(Environment solaceEnvironment, String organizationName, String environmentName, List<String> availableProtocols) throws APIManagementException {
    // Create solace admin APIs instance
    SolaceAdminApis solaceAdminApis = new SolaceAdminApis(solaceEnvironment.getServerURL(), solaceEnvironment.getUserName(), solaceEnvironment.getPassword(), solaceEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_DEV_NAME));
    List<SolaceURLsDTO> solaceURLsDTOs = new ArrayList<>();
    HttpResponse response = solaceAdminApis.environmentGET(organizationName, environmentName);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String responseString = null;
        try {
            responseString = EntityUtils.toString(response.getEntity());
            org.json.JSONObject jsonObject = new org.json.JSONObject(responseString);
            JSONArray protocols = jsonObject.getJSONArray("messagingProtocols");
            for (int i = 0; i < protocols.length(); i++) {
                org.json.JSONObject protocolDetails = protocols.getJSONObject(i);
                String protocolName = protocolDetails.getJSONObject("protocol").getString("name");
                // Get solace protocol URLs for available protocols
                if (availableProtocols.contains(protocolName)) {
                    String endpointURI = protocolDetails.getString("uri");
                    SolaceURLsDTO subscriptionInfoSolaceProtocolURLsDTO = new SolaceURLsDTO();
                    subscriptionInfoSolaceProtocolURLsDTO.setProtocol(protocolName);
                    subscriptionInfoSolaceProtocolURLsDTO.setEndpointURL(endpointURI);
                    solaceURLsDTOs.add(subscriptionInfoSolaceProtocolURLsDTO);
                }
            }
        } catch (IOException e) {
            throw new APIManagementException("Error occurred when retrieving protocols URLs from Solace " + "admin apis");
        }
    }
    return solaceURLsDTOs;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) HttpResponse(org.apache.http.HttpResponse) SolaceURLsDTO(org.wso2.carbon.apimgt.solace.dtos.SolaceURLsDTO) IOException(java.io.IOException) SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 68 with Broker

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

the class SolaceNotifierUtils method checkApiProductAlreadyDeployedInSolace.

/**
 * Check whether the given API product is already deployed in the Solace broker
 *
 * @param api          Name of the API
 * @param organization Name of the organization
 * @return returns true if the given API product is already deployed in the Solace
 * @throws APIManagementException If an error occurs when checking API product availability
 */
private boolean checkApiProductAlreadyDeployedInSolace(API api, String organization) throws IOException, APIManagementException {
    Map<String, Environment> environmentMap = APIUtil.getReadOnlyGatewayEnvironments();
    Environment solaceEnvironment = environmentMap.get(SolaceConstants.SOLACE_ENVIRONMENT);
    if (solaceEnvironment != null) {
        SolaceAdminApis solaceAdminApis = new SolaceAdminApis(solaceEnvironment.getServerURL(), solaceEnvironment.getUserName(), solaceEnvironment.getPassword(), solaceEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_DEV_NAME));
        String apiNameWithContext = generateApiProductNameForSolaceBroker(api, getThirdPartySolaceBrokerEnvironmentNameOfAPIDeployment(api));
        CloseableHttpResponse response = solaceAdminApis.apiProductGet(organization, apiNameWithContext);
        if (response != null) {
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                log.info("API product found in Solace Broker");
                return true;
            } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                log.error("API product not found in Solace broker");
                log.error(EntityUtils.toString(response.getEntity()));
                throw new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
            } else {
                log.error("Cannot find API product in Solace Broker");
                log.error(EntityUtils.toString(response.getEntity()));
                throw new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
            }
        }
        return false;
    } else {
        throw new APIManagementException("Solace Environment configurations are not provided properly");
    }
}
Also used : SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Environment(org.wso2.carbon.apimgt.api.model.Environment) HttpResponseException(org.apache.http.client.HttpResponseException)

Example 69 with Broker

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

the class TokenRevocationNotifierImpl method sendMessageOnRealtime.

/**
 * Method to publish the revoked token on to the realtime message broker
 *
 * @param revokedToken requested revoked token
 * @param properties realtime notifier properties read from the config
 */
@Override
public void sendMessageOnRealtime(String revokedToken, Properties properties) {
    // Variables related to Realtime Notifier
    String realtimeNotifierTTL = realTimeNotifierProperties.getProperty("ttl", DEFAULT_TTL);
    long expiryTimeForJWT = Long.parseLong(properties.getProperty("expiryTime"));
    String eventId = properties.getProperty(APIConstants.NotificationEvent.EVENT_ID);
    String tokenType = properties.getProperty(APIConstants.NotificationEvent.TOKEN_TYPE);
    int tenantId = (int) properties.get(APIConstants.NotificationEvent.TENANT_ID);
    Object[] objects = new Object[] { eventId, revokedToken, realtimeNotifierTTL, expiryTimeForJWT, tokenType, tenantId };
    EventPublisherEvent tokenRevocationEvent = new EventPublisherEvent(APIConstants.TOKEN_REVOCATION_STREAM_ID, System.currentTimeMillis(), objects);
    APIUtil.publishEvent(EventPublisherType.TOKEN_REVOCATION, tokenRevocationEvent, tokenRevocationEvent.toString());
}
Also used : EventPublisherEvent(org.wso2.carbon.apimgt.eventing.EventPublisherEvent)

Example 70 with Broker

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

the class WebhooksSubscriptionEventHandler method populateProperties.

/**
 * Method to publish the subscription request on to the realtime message broker
 *
 * @param subscriptionEvent subscription event
 * @return Properties       subscription properties
 */
private Properties populateProperties(WebhooksSubscriptionEvent subscriptionEvent) throws APIManagementException {
    Properties properties = new Properties();
    properties.put(APIConstants.Webhooks.API_UUID, subscriptionEvent.getApiUUID());
    properties.put(APIConstants.Webhooks.APP_ID, subscriptionEvent.getAppID());
    properties.put(APIConstants.Webhooks.TENANT_DOMAIN, subscriptionEvent.getTenantDomain());
    properties.put(APIConstants.Webhooks.TENANT_ID, subscriptionEvent.getTenantId());
    properties.put(APIConstants.Webhooks.CALLBACK, subscriptionEvent.getCallback());
    properties.put(APIConstants.Webhooks.TOPIC, subscriptionEvent.getTopic());
    putIfNotNull(properties, APIConstants.Webhooks.SECRET, subscriptionEvent.getSecret());
    String leaseSeconds = subscriptionEvent.getLeaseSeconds();
    putIfNotNull(properties, APIConstants.Webhooks.LEASE_SECONDS, leaseSeconds);
    Date currentTime = new Date();
    Timestamp updatedTimestamp = new Timestamp(currentTime.getTime());
    subscriptionEvent.setUpdatedTime(updatedTimestamp);
    properties.put(APIConstants.Webhooks.UPDATED_AT, updatedTimestamp);
    long expiryTime = 0;
    if (!StringUtils.isEmpty(leaseSeconds)) {
        long leaseSecondsInLong;
        try {
            leaseSecondsInLong = Long.parseLong(leaseSeconds);
        } catch (NumberFormatException e) {
            throw new APIManagementException("Error while parsing leaseSeconds param", e);
        }
        expiryTime = updatedTimestamp.toInstant().plusSeconds(leaseSecondsInLong).toEpochMilli();
    }
    subscriptionEvent.setExpiryTime(expiryTime);
    properties.put(APIConstants.Webhooks.EXPIRY_AT, "" + expiryTime);
    properties.put(APIConstants.Webhooks.TIER, "" + subscriptionEvent.getTier());
    return properties;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Properties(java.util.Properties) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Aggregations

Test (org.testng.annotations.Test)19 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)19 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)16 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)16 InMemoryBroker (org.wso2.siddhi.core.util.transport.InMemoryBroker)16 Environment (org.wso2.carbon.apimgt.api.model.Environment)15 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)13 ArrayList (java.util.ArrayList)11 SolaceAdminApis (org.wso2.carbon.apimgt.solace.SolaceAdminApis)9 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)7 DeployerException (org.wso2.carbon.apimgt.impl.deployer.exceptions.DeployerException)7 NotifierException (org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException)7 IOException (java.io.IOException)6 Broker (org.wso2.carbon.apimgt.core.api.Broker)6 Test (org.junit.Test)5 API (org.wso2.carbon.apimgt.core.models.API)5 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)4 ExternalGatewayDeployer (org.wso2.carbon.apimgt.impl.deployer.ExternalGatewayDeployer)4 JsonParser (com.google.gson.JsonParser)3