Search in sources :

Example 86 with Provider

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

the class SseApiHandler method isThrottled.

private boolean isThrottled(org.apache.axis2.context.MessageContext axisCtx, MessageContext synCtx) {
    AuthenticationContext authenticationContext = APISecurityUtils.getAuthenticationContext(synCtx);
    ThrottleInfo throttleInfo = getThrottlingInfo(authenticationContext, synCtx);
    boolean isThrottled = SseUtils.isRequestBlocked(authenticationContext, throttleInfo.getApiContext(), throttleInfo.getApiVersion(), authenticationContext.getUsername(), throttleInfo.getRemoteIp(), CarbonContext.getThreadLocalCarbonContext().getTenantDomain());
    if (!isThrottled) {
        // do throttling if request is not blocked by global conditions only
        isThrottled = SseUtils.isThrottled(throttleInfo.getSubscriberTenantDomain(), throttleInfo.getResourceLevelThrottleKey(), throttleInfo.getSubscriptionLevelThrottleKey(), throttleInfo.getApplicationLevelThrottleKey());
    }
    if (isThrottled) {
        handleThrottledOut(synCtx);
        return true;
    }
    if (APIUtil.isAnalyticsEnabled()) {
        AnalyticsDataProvider provider = new SseResponseEventDataProvider(synCtx);
        axisCtx.setProperty(SSE_ANALYTICS_INFO, provider);
    }
    return false;
}
Also used : SseResponseEventDataProvider(org.wso2.carbon.apimgt.gateway.handlers.streaming.sse.analytics.SseResponseEventDataProvider) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) ThrottleInfo(org.wso2.carbon.apimgt.gateway.handlers.streaming.sse.throttling.ThrottleInfo) AnalyticsDataProvider(org.wso2.carbon.apimgt.common.analytics.collectors.AnalyticsDataProvider)

Example 87 with Provider

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

the class SseResponseStreamInterceptor method publishAnalyticsData.

private void publishAnalyticsData(int eventCount, MessageContext axi2Ctx) throws AnalyticsException {
    Object responseEventProvider = axi2Ctx.getProperty(SSE_ANALYTICS_INFO);
    if (responseEventProvider == null) {
        log.error("SSE Analytics event provider is null.");
        return;
    }
    SseResponseEventDataProvider provider = (SseResponseEventDataProvider) responseEventProvider;
    provider.setResponseCode((int) axi2Ctx.getProperty(SynapseConstants.HTTP_SC));
    GenericRequestDataCollector dataCollector = new GenericRequestDataCollector(provider);
    for (int count = 0; count < eventCount; count++) {
        dataCollector.collectData();
    }
}
Also used : SseResponseEventDataProvider(org.wso2.carbon.apimgt.gateway.handlers.streaming.sse.analytics.SseResponseEventDataProvider) GenericRequestDataCollector(org.wso2.carbon.apimgt.common.analytics.collectors.impl.GenericRequestDataCollector) JSONObject(org.json.JSONObject)

Example 88 with Provider

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

the class WebSocketAnalyticsMetricsHandler method collectData.

private void collectData(ChannelHandlerContext ctx) {
    AnalyticsDataProvider provider = new WebSocketAnalyticsDataProvider(ctx);
    GenericRequestDataCollector collector = new GenericRequestDataCollector(provider);
    try {
        collector.collectData();
    } catch (AnalyticsException e) {
        log.error("Error Occurred when collecting analytics data", e);
    }
}
Also used : GenericRequestDataCollector(org.wso2.carbon.apimgt.common.analytics.collectors.impl.GenericRequestDataCollector) AnalyticsException(org.wso2.carbon.apimgt.common.analytics.exceptions.AnalyticsException) AnalyticsDataProvider(org.wso2.carbon.apimgt.common.analytics.collectors.AnalyticsDataProvider)

Example 89 with Provider

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

the class RegistryPersistenceImpl method deleteAPIProduct.

@Override
public void deleteAPIProduct(Organization org, String apiId) throws APIPersistenceException {
    boolean tenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(org.getName());
        tenantFlowStarted = holder.isTenantFlowStarted();
        Registry registry = holder.getRegistry();
        GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry);
        GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
        if (artifactManager == null) {
            String errorMessage = "Failed to retrieve artifact manager when deleting API Product" + apiId;
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        GenericArtifact apiProductArtifact = artifactManager.getGenericArtifact(apiId);
        APIProductIdentifier identifier = new APIProductIdentifier(apiProductArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER), apiProductArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME), apiProductArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
        // this is the product resource collection path
        String productResourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(identifier.getProviderName()) + RegistryConstants.PATH_SEPARATOR + identifier.getName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
        // this is the product rxt instance path
        String apiProductArtifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(identifier.getProviderName()) + RegistryConstants.PATH_SEPARATOR + identifier.getName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + APIConstants.API_RESOURCE_NAME;
        Resource apiProductResource = registry.get(productResourcePath);
        String productResourceUUID = apiProductResource.getUUID();
        if (productResourceUUID == null) {
            throw new APIManagementException("artifact id is null for : " + productResourcePath);
        }
        Resource apiArtifactResource = registry.get(apiProductArtifactPath);
        String apiArtifactResourceUUID = apiArtifactResource.getUUID();
        if (apiArtifactResourceUUID == null) {
            throw new APIManagementException("artifact id is null for : " + apiProductArtifactPath);
        }
        // Delete the dependencies associated with the api product artifact
        GovernanceArtifact[] dependenciesArray = apiProductArtifact.getDependencies();
        if (dependenciesArray.length > 0) {
            for (GovernanceArtifact artifact : dependenciesArray) {
                registry.delete(artifact.getPath());
            }
        }
        // delete registry resources
        artifactManager.removeGenericArtifact(apiProductArtifact);
        artifactManager.removeGenericArtifact(productResourceUUID);
        /* remove empty directories */
        String apiProductCollectionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getName();
        if (registry.resourceExists(apiProductCollectionPath)) {
            // at the moment product versioning is not supported so we are directly deleting this collection as
            // this is known to be empty
            registry.delete(apiProductCollectionPath);
        }
        String productProviderPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getName();
        if (registry.resourceExists(productProviderPath)) {
            Resource providerCollection = registry.get(productProviderPath);
            CollectionImpl collection = (CollectionImpl) providerCollection;
            // if there is no api product for given provider delete the provider directory
            if (collection.getChildCount() == 0) {
                if (log.isDebugEnabled()) {
                    log.debug("No more API Products from the provider " + identifier.getProviderName() + " found. " + "Removing provider collection from registry");
                }
                registry.delete(productProviderPath);
            }
        }
    } catch (RegistryException e) {
        String msg = "Failed to get API";
        throw new APIPersistenceException(msg, e);
    } catch (APIManagementException e) {
        String msg = "Failed to get API";
        throw new APIPersistenceException(msg, e);
    } finally {
        if (tenantFlowStarted) {
            RegistryPersistenceUtil.endTenantFlow();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl)

Example 90 with Provider

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

the class RegistryPersistenceImpl method getAllMediationPolicies.

@Override
public List<MediationInfo> getAllMediationPolicies(Organization org, String apiId) throws MediationPolicyPersistenceException {
    boolean isTenantFlowStarted = false;
    List<MediationInfo> mediationList = new ArrayList<MediationInfo>();
    MediationInfo mediation;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        BasicAPI api = getbasicAPIInfo(apiId, registry);
        if (api == null) {
            throw new MediationPolicyPersistenceException("API not foud ", ExceptionCodes.API_NOT_FOUND);
        }
        String apiPath = GovernanceUtils.getArtifactPath(registry, apiId);
        int prependIndex = apiPath.lastIndexOf("/api");
        String apiResourcePath = apiPath.substring(0, prependIndex);
        // apiResourcePath = apiResourcePath.substring(0, apiResourcePath.lastIndexOf("/"));
        // Getting API registry resource
        Resource resource = registry.get(apiResourcePath);
        // resource eg: /_system/governance/apimgt/applicationdata/provider/admin/calculatorAPI/2.0
        if (resource instanceof Collection) {
            Collection typeCollection = (Collection) resource;
            String[] typeArray = typeCollection.getChildren();
            for (String type : typeArray) {
                // Check for mediation policy sequences
                if ((type.equalsIgnoreCase(apiResourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN)) || (type.equalsIgnoreCase(apiResourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT)) || (type.equalsIgnoreCase(apiResourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT))) {
                    Resource typeResource = registry.get(type);
                    // typeResource : in / out / fault
                    if (typeResource instanceof Collection) {
                        String[] mediationPolicyArr = ((Collection) typeResource).getChildren();
                        if (mediationPolicyArr.length > 0) {
                            for (String mediationPolicy : mediationPolicyArr) {
                                Resource policyResource = registry.get(mediationPolicy);
                                // policyResource eg: custom_in_message
                                // Get uuid of the registry resource
                                String resourceId = policyResource.getUUID();
                                // Get mediation policy config
                                try {
                                    String contentString = IOUtils.toString(policyResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
                                    // Extract name from the policy config
                                    OMElement omElement = AXIOMUtil.stringToOM(contentString);
                                    OMAttribute attribute = omElement.getAttribute(new QName("name"));
                                    String mediationPolicyName = attribute.getAttributeValue();
                                    mediation = new MediationInfo();
                                    mediation.setId(resourceId);
                                    mediation.setName(mediationPolicyName);
                                    // Extracting mediation policy type from the registry resource path
                                    String resourceType = type.substring(type.lastIndexOf("/") + 1);
                                    mediation.setType(resourceType);
                                    mediationList.add(mediation);
                                } catch (XMLStreamException e) {
                                    // If exception been caught flow will continue with next mediation policy
                                    log.error("Error occurred while getting omElement out of" + " mediation content", e);
                                } catch (IOException e) {
                                    log.error("Error occurred while converting the content " + "stream of mediation " + mediationPolicy + " to string", e);
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Error occurred  while getting Api Specific mediation policies ";
        throw new MediationPolicyPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return mediationList;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) OMElement(org.apache.axiom.om.OMElement) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) XMLStreamException(javax.xml.stream.XMLStreamException) MediationInfo(org.wso2.carbon.apimgt.persistence.dto.MediationInfo) Collection(org.wso2.carbon.registry.core.Collection) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)82 ArrayList (java.util.ArrayList)70 API (org.wso2.carbon.apimgt.api.model.API)64 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)50 Test (org.junit.Test)49 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)45 HashMap (java.util.HashMap)40 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)36 IOException (java.io.IOException)35 Resource (org.wso2.carbon.registry.core.Resource)34 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)32 HashSet (java.util.HashSet)30 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)29 UserStoreException (org.wso2.carbon.user.api.UserStoreException)29 PreparedStatement (java.sql.PreparedStatement)28 Connection (java.sql.Connection)27 SQLException (java.sql.SQLException)27 ResultSet (java.sql.ResultSet)25 QName (javax.xml.namespace.QName)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)25