Search in sources :

Example 1 with RecommendationEnvironment

use of org.wso2.carbon.apimgt.impl.recommendationmgt.RecommendationEnvironment in project carbon-apimgt by wso2.

the class APIProviderImpl method updateAPI.

/**
 * Updates an existing API
 *
 * @param api API
 * @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to update API
 * @throws org.wso2.carbon.apimgt.api.FaultGatewaysException on Gateway Failure
 */
@Override
public void updateAPI(API api) throws APIManagementException, FaultGatewaysException {
    boolean isValid = isAPIUpdateValid(api);
    if (!isValid) {
        throw new APIManagementException(" User doesn't have permission for update");
    }
    API oldApi = getAPIbyUUID(api.getUuid(), api.getOrganization());
    String organization = api.getOrganization();
    if (!oldApi.getStatus().equals(api.getStatus())) {
        // Use changeAPIStatus for that kind of updates.
        throw new APIManagementException("Invalid API update operation involving API status changes");
    }
    validateKeyManagers(api);
    Gson gson = new Gson();
    Map<String, String> oldMonetizationProperties = gson.fromJson(oldApi.getMonetizationProperties().toString(), HashMap.class);
    if (oldMonetizationProperties != null && !oldMonetizationProperties.isEmpty()) {
        Map<String, String> newMonetizationProperties = gson.fromJson(api.getMonetizationProperties().toString(), HashMap.class);
        if (newMonetizationProperties != null) {
            for (Map.Entry<String, String> entry : oldMonetizationProperties.entrySet()) {
                String newValue = newMonetizationProperties.get(entry.getKey());
                if (StringUtils.isAllBlank(newValue)) {
                    newMonetizationProperties.put(entry.getKey(), entry.getValue());
                }
            }
            JSONParser parser = new JSONParser();
            try {
                JSONObject jsonObj = (JSONObject) parser.parse(gson.toJson(newMonetizationProperties));
                api.setMonetizationProperties(jsonObj);
            } catch (ParseException e) {
                throw new APIManagementException("Error when parsing monetization properties ", e);
            }
        }
    }
    String publishedDefaultVersion = getPublishedDefaultVersion(api.getId());
    // Update WSDL in the registry
    if (api.getWsdlUrl() != null && api.getWsdlResource() == null) {
        updateWsdlFromUrl(api);
    }
    if (api.getWsdlResource() != null) {
        updateWsdlFromResourceFile(api);
    }
    boolean updatePermissions = false;
    if (APIUtil.isAccessControlEnabled()) {
        if (!oldApi.getAccessControl().equals(api.getAccessControl()) || (APIConstants.API_RESTRICTED_VISIBILITY.equals(oldApi.getAccessControl()) && !api.getAccessControlRoles().equals(oldApi.getAccessControlRoles())) || !oldApi.getVisibility().equals(api.getVisibility()) || (APIConstants.API_RESTRICTED_VISIBILITY.equals(oldApi.getVisibility()) && !api.getVisibleRoles().equals(oldApi.getVisibleRoles()))) {
            updatePermissions = true;
        }
    } else if (!oldApi.getVisibility().equals(api.getVisibility()) || (APIConstants.API_RESTRICTED_VISIBILITY.equals(oldApi.getVisibility()) && !api.getVisibleRoles().equals(oldApi.getVisibleRoles()))) {
        updatePermissions = true;
    }
    updateEndpointSecurity(oldApi, api);
    String apiUUid = updateApiArtifact(api, true, updatePermissions);
    api.setUuid(apiUUid);
    if (!oldApi.getContext().equals(api.getContext())) {
        api.setApiHeaderChanged(true);
    }
    int tenantId;
    String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(api.getId().getProviderName()));
    try {
        tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
    } catch (UserStoreException e) {
        throw new APIManagementException("Error in retrieving Tenant Information while updating api :" + api.getId().getApiName(), e);
    }
    validateResourceThrottlingTiers(api, tenantDomain);
    // get product resource mappings on API before updating the API. Update uri templates on api will remove all
    // product mappings as well.
    List<APIProductResource> productResources = apiMgtDAO.getProductMappingsForAPI(api);
    updateAPI(api, tenantId, userNameWithoutChange);
    updateProductResourceMappings(api, organization, productResources);
    if (log.isDebugEnabled()) {
        log.debug("Successfully updated the API: " + api.getId() + " in the database");
    }
    JSONObject apiLogObject = new JSONObject();
    apiLogObject.put(APIConstants.AuditLogConstants.NAME, api.getId().getApiName());
    apiLogObject.put(APIConstants.AuditLogConstants.CONTEXT, api.getContext());
    apiLogObject.put(APIConstants.AuditLogConstants.VERSION, api.getId().getVersion());
    apiLogObject.put(APIConstants.AuditLogConstants.PROVIDER, api.getId().getProviderName());
    APIUtil.logAuditMessage(APIConstants.AuditLogConstants.API, apiLogObject.toString(), APIConstants.AuditLogConstants.UPDATED, this.username);
    // update doc visibility
    List<Documentation> docsList = getAllDocumentation(api.getId());
    if (docsList != null) {
        Iterator it = docsList.iterator();
        while (it.hasNext()) {
            Object docsObject = it.next();
            Documentation docs = (Documentation) docsObject;
            updateDocVisibility(api, docs);
        }
    }
    // notify key manager with API update
    registerOrUpdateResourceInKeyManager(api, tenantDomain);
    int apiId = apiMgtDAO.getAPIID(api.getUuid());
    if (publishedDefaultVersion != null) {
        if (api.isPublishedDefaultVersion() && !api.getId().getVersion().equals(publishedDefaultVersion)) {
            APIIdentifier previousDefaultVersionIdentifier = new APIIdentifier(api.getId().getProviderName(), api.getId().getApiName(), publishedDefaultVersion);
            sendUpdateEventToPreviousDefaultVersion(previousDefaultVersionIdentifier, organization);
        }
    }
    APIEvent apiEvent = new APIEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.API_UPDATE.name(), tenantId, tenantDomain, api.getId().getApiName(), apiId, api.getUuid(), api.getId().getVersion(), api.getType(), api.getContext(), APIUtil.replaceEmailDomainBack(api.getId().getProviderName()), api.getStatus());
    APIUtil.sendNotification(apiEvent, APIConstants.NotifierType.API.name());
    // Extracting API details for the recommendation system
    if (recommendationEnvironment != null) {
        RecommenderEventPublisher extractor = new RecommenderDetailsExtractor(api, tenantDomain, APIConstants.ADD_API);
        Thread recommendationThread = new Thread(extractor);
        recommendationThread.start();
    }
}
Also used : RecommenderDetailsExtractor(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommenderDetailsExtractor) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) Gson(com.google.gson.Gson) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONObject(org.json.simple.JSONObject) APIEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIEvent) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Iterator(java.util.Iterator) RecommenderEventPublisher(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommenderEventPublisher) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ParseException(org.json.simple.parser.ParseException) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 2 with RecommendationEnvironment

use of org.wso2.carbon.apimgt.impl.recommendationmgt.RecommendationEnvironment in project carbon-apimgt by wso2.

the class APIProviderImpl method extractRecommendationDetails.

private void extractRecommendationDetails(ApiTypeWrapper apiTypeWrapper) {
    // Extracting API or API Product details for the recommendation system
    if (recommendationEnvironment != null) {
        RecommenderEventPublisher extractor = new RecommenderDetailsExtractor(apiTypeWrapper, tenantDomain, APIConstants.ADD_API);
        Thread recommendationThread = new Thread(extractor);
        recommendationThread.start();
    }
}
Also used : RecommenderDetailsExtractor(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommenderDetailsExtractor) RecommenderEventPublisher(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommenderEventPublisher)

Example 3 with RecommendationEnvironment

use of org.wso2.carbon.apimgt.impl.recommendationmgt.RecommendationEnvironment in project carbon-apimgt by wso2.

the class APIConsumerImpl method publishClickedAPI.

public void publishClickedAPI(ApiTypeWrapper clickedApi, String username) {
    if (recommendationEnvironment != null) {
        RecommenderEventPublisher extractor = new RecommenderDetailsExtractor(clickedApi, username, requestedTenant);
        Thread recommendationThread = new Thread(extractor);
        recommendationThread.start();
    }
}
Also used : RecommenderDetailsExtractor(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommenderDetailsExtractor) RecommenderEventPublisher(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommenderEventPublisher)

Example 4 with RecommendationEnvironment

use of org.wso2.carbon.apimgt.impl.recommendationmgt.RecommendationEnvironment in project carbon-apimgt by wso2.

the class APIUtil method isRecommendationEnabled.

/**
 * To check whether the API recommendation is enabled. It can be either enabled globally or tenant vice.
 *
 * @param tenantDomain Tenant domain
 * @return whether recommendation is enabled or not
 */
public static boolean isRecommendationEnabled(String tenantDomain) {
    RecommendationEnvironment recommendationEnvironment = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration().getApiRecommendationEnvironment();
    if (recommendationEnvironment != null) {
        if (recommendationEnvironment.isApplyForAllTenants()) {
            return true;
        } else {
            try {
                org.json.simple.JSONObject tenantConfig = getTenantConfig(tenantDomain);
                Object value = tenantConfig.get(APIConstants.API_TENANT_CONF_ENABLE_RECOMMENDATION_KEY);
                return Boolean.parseBoolean(value.toString());
            } catch (APIManagementException e) {
                log.debug("Error while retrieving Recommendation config from registry", e);
            }
        }
    }
    return false;
}
Also used : JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) RecommendationEnvironment(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommendationEnvironment)

Example 5 with RecommendationEnvironment

use of org.wso2.carbon.apimgt.impl.recommendationmgt.RecommendationEnvironment in project carbon-apimgt by wso2.

the class APIConsumerImpl method publishSearchQuery.

public void publishSearchQuery(String query, String username) {
    if (recommendationEnvironment != null) {
        RecommenderEventPublisher extractor = new RecommenderDetailsExtractor(query, username, requestedTenant);
        Thread recommendationThread = new Thread(extractor);
        recommendationThread.start();
    }
}
Also used : RecommenderDetailsExtractor(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommenderDetailsExtractor) RecommenderEventPublisher(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommenderEventPublisher)

Aggregations

RecommenderDetailsExtractor (org.wso2.carbon.apimgt.impl.recommendationmgt.RecommenderDetailsExtractor)8 RecommenderEventPublisher (org.wso2.carbon.apimgt.impl.recommendationmgt.RecommenderEventPublisher)8 JSONObject (org.json.simple.JSONObject)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 RecommendationEnvironment (org.wso2.carbon.apimgt.impl.recommendationmgt.RecommendationEnvironment)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 API (org.wso2.carbon.apimgt.api.model.API)3 ApplicationEvent (org.wso2.carbon.apimgt.impl.notifier.events.ApplicationEvent)3 WorkflowException (org.wso2.carbon.apimgt.impl.workflow.WorkflowException)3 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 TreeMap (java.util.TreeMap)2 JSONArray (org.json.simple.JSONArray)2 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)2 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)2 ApplicationRegistrationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO)2 ApplicationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO)2 SubscriptionWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO)2