Java Code Examples for org.wso2.carbon.governance.api.generic.GenericArtifactManager#getGenericArtifact()

The following examples show how to use org.wso2.carbon.governance.api.generic.GenericArtifactManager#getGenericArtifact() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RegistryForumManager.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Return the registry resources ratings for the given topic resource.
 * @param topicId Id of the topic which the ratings should be returned of.
 * @param username Username
 * @param tenantDomain Tenant domain.
 * @return User rating if the user is signed in, and average rating.
 * @throws ForumException
 */
@Override
public Map<String, Object> getRating(String topicId, String username, String tenantDomain) throws ForumException {

    Map<String, Object> rating = new HashMap<String, Object>();

    try {
        Registry registry = getRegistry(username, tenantDomain);
        GenericArtifactManager artifactManager = getArtifactManager(registry, TOPIC_RXT_KEY);
        GenericArtifact genericArtifact = artifactManager.getGenericArtifact(topicId);

        // Get the user rating.
        if(username != null){
            rating.put("userRating", registry.getRating(genericArtifact.getPath(), username));
        }

        // Get average rating.
        rating.put("averageRating", registry.getAverageRating(genericArtifact.getPath()));

        return rating;

    } catch (RegistryException e) {
        throw new ForumException(String.format("Cannot get rating for the topic id : '%s'", topicId), e);
    }

}
 
Example 2
Source File: DocumentIndexer.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch api status and access control details to document artifacts
 *
 * @param registry
 * @param documentResource
 * @param fields
 * @throws RegistryException
 * @throws APIManagementException
 */
private void fetchRequiredDetailsFromAssociatedAPI(Registry registry, Resource documentResource,
        Map<String, List<String>> fields) throws RegistryException, APIManagementException {
    Association apiAssociations[] = registry
            .getAssociations(documentResource.getPath(), APIConstants.DOCUMENTATION_ASSOCIATION);

    //a document can have one api association
    Association apiAssociation = apiAssociations[0];
    String apiPath = apiAssociation.getSourcePath();

    if (registry.resourceExists(apiPath)) {
        Resource apiResource = registry.get(apiPath);
        GenericArtifactManager apiArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
        GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiResource.getUUID());
        String apiStatus = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_STATUS).toLowerCase();
        String publisherRoles = apiResource.getProperty(APIConstants.PUBLISHER_ROLES);
        fields.put(APIConstants.API_OVERVIEW_STATUS, Arrays.asList(apiStatus));
        fields.put(APIConstants.PUBLISHER_ROLES, Arrays.asList(publisherRoles));
    } else {
        log.warn("API does not exist at " + apiPath);
    }
}
 
Example 3
Source File: AbstractAPIManager.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
public API getAPI(String apiPath) throws APIManagementException {
    try {
        GenericArtifactManager artifactManager = getAPIGenericArtifactManagerFromUtil(registry,
                APIConstants.API_KEY);
        Resource apiResource = registry.get(apiPath);
        String artifactId = apiResource.getUUID();
        if (artifactId == null) {
            throw new APIManagementException("artifact id is null for : " + apiPath);
        }
        GenericArtifact apiArtifact = artifactManager.getGenericArtifact(artifactId);
        return getApi(apiArtifact);

    } catch (RegistryException e) {
        String msg = "Failed to get API from : " + apiPath;
        throw new APIManagementException(msg, e);
    }
}
 
Example 4
Source File: AbstractAPIManager.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
public Documentation getDocumentation(APIIdentifier apiId, DocumentationType docType,
                                      String docName) throws APIManagementException {
    Documentation documentation = null;
    String docPath = APIUtil.getAPIDocPath(apiId) + docName;
    GenericArtifactManager artifactManager = getAPIGenericArtifactManagerFromUtil(registry,
            APIConstants.DOCUMENTATION_KEY);
    try {
        Resource docResource = registry.get(docPath);
        GenericArtifact artifact = artifactManager.getGenericArtifact(docResource.getUUID());
        documentation = APIUtil.getDocumentation(artifact);
    } catch (RegistryException e) {
        String msg = "Failed to get documentation details";
        throw new APIManagementException(msg, e);
    }
    return documentation;
}
 
Example 5
Source File: AbstractAPIManager.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
public API getAPI(APIIdentifier identifier, APIIdentifier oldIdentifier, String oldContext) throws
        APIManagementException {
    String apiPath = APIUtil.getAPIPath(identifier);
    try {
        GenericArtifactManager artifactManager = getAPIGenericArtifactManagerFromUtil(registry,
                APIConstants.API_KEY);
        Resource apiResource = registry.get(apiPath);
        String artifactId = apiResource.getUUID();
        if (artifactId == null) {
            throw new APIManagementException("artifact id is null for : " + apiPath);
        }
        GenericArtifact apiArtifact = artifactManager.getGenericArtifact(artifactId);
        return APIUtil.getAPI(apiArtifact, registry, oldIdentifier, oldContext);

    } catch (RegistryException e) {
        String msg = "Failed to get API from : " + apiPath;
        throw new APIManagementException(msg, e);
    }
}
 
Example 6
Source File: AbstractAPIManager.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
public APIProduct getAPIProduct(String productPath) throws APIManagementException {
    try {
        GenericArtifactManager artifactManager = getAPIGenericArtifactManagerFromUtil(registry,
                APIConstants.API_KEY);
        Resource productResource = registry.get(productPath);
        String artifactId = productResource.getUUID();
        if (artifactId == null) {
            throw new APIManagementException("artifact id is null for : " + productPath);
        }
        GenericArtifact productArtifact = artifactManager.getGenericArtifact(artifactId);
        return APIUtil.getAPIProduct(productArtifact, registry);

    } catch (RegistryException e) {
        String msg = "Failed to get API Product from : " + productPath;
        throw new APIManagementException(msg, e);
    }
}
 
Example 7
Source File: APIKeyMgtUtil.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * This returns API object for given APIIdentifier. Reads from registry entry for given APIIdentifier
 * creates API object
 *
 * @param identifier APIIdentifier object for the API
 * @return API object for given identifier
 * @throws APIManagementException on error in getting API artifact
 */
public static API getAPI(APIIdentifier identifier) throws APIManagementException {
    String apiPath = APIUtil.getAPIPath(identifier);

    try {
        Registry registry = APIKeyMgtDataHolder.getRegistryService().getGovernanceSystemRegistry();
        GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry,
                APIConstants.API_KEY);
        if (artifactManager == null) {
            String errorMessage = "Artifact manager is null when retrieving API " + identifier.getApiName();
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        Resource apiResource = registry.get(apiPath);
        String artifactId = apiResource.getUUID();
        if (artifactId == null) {
            throw new APIManagementException("artifact id is null for : " + apiPath);
        }
        GenericArtifact apiArtifact = artifactManager.getGenericArtifact(artifactId);
        return APIUtil.getAPI(apiArtifact, registry);

    } catch (RegistryException e) {
        return null;
    }
}
 
Example 8
Source File: RegistryForumManager.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Rates the given topic using registry rating,
 * @param topicId ID of the topic which should be rated.
 * @param rating User rate for the topic.
 * @param username Username
 * @param tenantDomain Tenant domain.
 * @return  Average rating.
 * @throws ForumException When the topic cannot be rated.
 */
@Override
public float rateTopic(String topicId, int rating, String username, String tenantDomain)throws ForumException {
    Registry registry;

    try {
        registry = getRegistry(username, tenantDomain);
        GenericArtifactManager artifactManager = getArtifactManager(registry, TOPIC_RXT_KEY);
        GenericArtifact genericArtifact = artifactManager.getGenericArtifact(topicId);
        registry.rateResource(genericArtifact.getPath(), rating);
        return registry.getAverageRating(genericArtifact.getPath());
    } catch (RegistryException e) {
        throw new ForumException("Unable to get Registry of User", e);
    }
}
 
Example 9
Source File: RegistryForumManager.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
@Override
public ForumTopicDTO fetchForumTopicWithReplies(String topicId, int start, int count, String username, String tenantDomain)
        throws ForumException{

    Registry registry = getRegistry(username, tenantDomain);

    GenericArtifactManager topicArtifactManager = getArtifactManager(registry, TOPIC_RXT_KEY);
    GenericArtifactManager replyArtifactManager = getArtifactManager(registry, REPLY_RXT_KEY);

    if(topicArtifactManager == null){
        if(log.isDebugEnabled()){
            log.debug("Could not get artifact manager for topic.rxt, probably no topics found");
        }
        return null;
    }

    final String replyTopicId = topicId;

    Map<String, List<String>> listMap = new HashMap<String, List<String>>();
    listMap.put(ForumConstants.OVERVIEW_REPLY_TOPIC_ID, new ArrayList<String>() {{
        add(replyTopicId);
    }});

    ForumTopicDTO topicDTO = null;
    PaginationContext.init(start, count, "DESC", ForumConstants.OVERVIEW_REPLY_TIMESTAMP, Integer.MAX_VALUE);

    try {
        GenericArtifact topicArtifact = topicArtifactManager.getGenericArtifact(topicId);

        if(topicArtifact == null){
            log.info("Could not find topic with id " + topicId);
            return null;
        }

        topicDTO = createForumTopicDTOFromArtifact(topicArtifact, registry);

        // Set ratings of the topic.
        // NOTE : Taking this operation out from 'createForumTopicDTOFromArtifact' for performance's sake
        topicDTO.setUserRating(registry.getRating(topicArtifact.getPath(), username));
        topicDTO.setAverageRating(registry.getAverageRating(topicArtifact.getPath()));

        final String searchValue = topicId;

        GenericArtifact[] replyArtifacts = replyArtifactManager.findGenericArtifacts(listMap);

        if (replyArtifacts == null || replyArtifacts.length == 0) {
            if (log.isDebugEnabled()) {
                log.debug("No Replies Found for topic " + topicDTO.getSubject());
            }
            return topicDTO;
        }

        List<ForumReplyDTO> replies = new ArrayList<ForumReplyDTO>();
        for(GenericArtifact replyArtifact : replyArtifacts){
            ForumReplyDTO replyDTO = createReplyDtoFromArtifact(replyArtifact, registry);
            replies.add(replyDTO);
        }
        topicDTO.setReplies(replies);
        return topicDTO;
    } catch (RegistryException e) {
        log.error("Error while getting artifacts for topic " + topicId + " " + e.getMessage());
        throw new ForumException("Error while getting artifacts for topic " + topicId);
    } finally {
        PaginationContext.destroy();
    }
}
 
Example 10
Source File: AbstractAPIManager.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
public List<Documentation> getAllDocumentation(Identifier id) throws APIManagementException {
    List<Documentation> documentationList = new ArrayList<Documentation>();
    String resourcePath = StringUtils.EMPTY;
    String docArtifactKeyType = StringUtils.EMPTY;
    if (id instanceof APIIdentifier) {
        resourcePath = APIUtil.getAPIPath((APIIdentifier) id);
    } else if (id instanceof APIProductIdentifier) {
        resourcePath = APIUtil.getAPIProductPath((APIProductIdentifier) id);
    }
    docArtifactKeyType = APIConstants.DOCUMENTATION_KEY;

    try {
        Association[] docAssociations = registry.getAssociations(resourcePath,
                APIConstants.DOCUMENTATION_ASSOCIATION);
        if (docAssociations == null) {
            return documentationList;
        }
        for (Association association : docAssociations) {
            String docPath = association.getDestinationPath();

            Resource docResource = registry.get(docPath);
            GenericArtifactManager artifactManager = getAPIGenericArtifactManager(registry,
                    docArtifactKeyType);
            GenericArtifact docArtifact = artifactManager.getGenericArtifact(docResource.getUUID());
            Documentation doc = APIUtil.getDocumentation(docArtifact);
            Date contentLastModifiedDate;
            Date docLastModifiedDate = docResource.getLastModified();
            if (Documentation.DocumentSourceType.INLINE.equals(doc.getSourceType()) || Documentation.DocumentSourceType.MARKDOWN.equals(doc.getSourceType())) {
                String contentPath = StringUtils.EMPTY;
                if (id instanceof APIIdentifier) {
                    contentPath = APIUtil.getAPIDocContentPath((APIIdentifier) id, doc.getName());
                } else if (id instanceof APIProductIdentifier) {
                    contentPath = APIUtil.getProductDocContentPath((APIProductIdentifier) id, doc.getName());
                }

                contentLastModifiedDate = registry.get(contentPath).getLastModified();
                doc.setLastUpdated((contentLastModifiedDate.after(docLastModifiedDate) ?
                        contentLastModifiedDate : docLastModifiedDate));
            } else {
                doc.setLastUpdated(docLastModifiedDate);
            }
            documentationList.add(doc);
        }

    } catch (RegistryException e) {
        String msg = "Failed to get documentations for api/product " + id.getName();
        throw new APIManagementException(msg, e);
    }
    return documentationList;
}
 
Example 11
Source File: AbstractAPIManager.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
public Set<API> getSubscriberAPIs(Subscriber subscriber) throws APIManagementException {
    SortedSet<API> apiSortedSet = new TreeSet<API>(new APINameComparator());
    Set<SubscribedAPI> subscribedAPIs = apiMgtDAO.getSubscribedAPIs(subscriber, null);
    for (SubscribedAPI subscribedApi : subscribedAPIs) {
        Application application = subscribedApi.getApplication();
        if (application != null) {
            int applicationId = application.getId();
            Set<APIKey> keys = getApplicationKeys(applicationId);
            for (APIKey key : keys) {
                application.addKey(key);
            }
        }
    }
    boolean isTenantFlowStarted = false;
    try {
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            isTenantFlowStarted = true;
            startTenantFlow(tenantDomain);
        }
        for (SubscribedAPI subscribedAPI : subscribedAPIs) {
            String apiPath = APIUtil.getAPIPath(subscribedAPI.getApiId());
            Resource resource;
            try {
                resource = registry.get(apiPath);
                GenericArtifactManager artifactManager = getAPIGenericArtifactManager(registry,
                        APIConstants.API_KEY);
                GenericArtifact artifact = artifactManager.getGenericArtifact(
                        resource.getUUID());
                API api = getAPI(artifact);
                if (api != null) {
                    apiSortedSet.add(api);
                }
            } catch (RegistryException e) {
                String msg = "Failed to get APIs for subscriber: " + subscriber.getName();
                throw  new APIManagementException(msg, e);
            }
        }
    } finally {
        if (isTenantFlowStarted) {
            endTenantFlow();
        }
    }
    return apiSortedSet;
}