Search in sources :

Example 86 with Documentation

use of org.wso2.carbon.apimgt.persistence.dto.Documentation in project carbon-apimgt by wso2.

the class PublisherCommonUtils method addDocumentationContent.

/**
 * Add documentation content of inline and markdown documents.
 *
 * @param documentation Documentation
 * @param apiProvider   API Provider
 * @param apiId         API/API Product UUID
 * @param documentId    Document ID
 * @param organization  Identifier of the organization
 * @param inlineContent Inline content string
 * @throws APIManagementException If an error occurs while adding the documentation content
 */
public static void addDocumentationContent(Documentation documentation, APIProvider apiProvider, String apiId, String documentId, String organization, String inlineContent) throws APIManagementException {
    DocumentationContent content = new DocumentationContent();
    content.setSourceType(DocumentationContent.ContentSourceType.valueOf(documentation.getSourceType().toString()));
    content.setTextContent(inlineContent);
    apiProvider.addDocumentationContent(apiId, documentId, organization, content);
}
Also used : DocumentationContent(org.wso2.carbon.apimgt.api.model.DocumentationContent)

Example 87 with Documentation

use of org.wso2.carbon.apimgt.persistence.dto.Documentation in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdDocumentsDocumentIdGet.

@Override
public Response apisApiIdDocumentsDocumentIdGet(String apiId, String documentId, String xWSO2Tenant, String ifModifiedSince, MessageContext messageContext) {
    Documentation documentation;
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        if (!RestAPIStoreUtils.isUserAccessAllowedForAPIByUUID(apiId, organization)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, log);
        }
        documentation = apiConsumer.getDocumentation(apiId, documentId, organization);
        if (null != documentation) {
            DocumentDTO documentDTO = DocumentationMappingUtil.fromDocumentationToDTO(documentation);
            return Response.ok().entity(documentDTO).build();
        } else {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_DOCUMENTATION, documentId, log);
        }
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else {
            RestApiUtil.handleInternalServerError("Error while getting API " + apiId, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 88 with Documentation

use of org.wso2.carbon.apimgt.persistence.dto.Documentation in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdDocumentsGet.

@Override
public Response apisApiIdDocumentsGet(String apiId, Integer limit, Integer offset, String xWSO2Tenant, String ifNoneMatch, MessageContext messageContext) {
    // pre-processing
    // setting default limit and offset values if they are not set
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        // this will fail if user doesn't have access to the API or the API does not exist
        // APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId, organization);
        // List<Documentation> documentationList = apiConsumer.getAllDocumentation(apiIdentifier, username);
        List<Documentation> documentationList = apiConsumer.getAllDocumentation(apiId, organization);
        DocumentListDTO documentListDTO = DocumentationMappingUtil.fromDocumentationListToDTO(documentationList, offset, limit);
        // todo : set total count properly
        DocumentationMappingUtil.setPaginationParams(documentListDTO, apiId, offset, limit, documentationList.size());
        return Response.ok().entity(documentListDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else {
            RestApiUtil.handleInternalServerError("Error while getting API " + apiId, e, log);
        }
    }
    /*catch (UnsupportedEncodingException e) {
            String errorMessage = "Error while Decoding apiId" + apiId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }*/
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 89 with Documentation

use of org.wso2.carbon.apimgt.persistence.dto.Documentation in project carbon-apimgt by wso2.

the class SearchApiServiceImpl method searchGet.

@Override
public Response searchGet(Integer limit, Integer offset, String xWSO2Tenant, String query, String ifNoneMatch, MessageContext messageContext) {
    SearchResultListDTO resultListDTO = new SearchResultListDTO();
    List<SearchResultDTO> allmatchedResults = new ArrayList<>();
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    query = query == null ? "*" : query;
    String organization = RestApiUtil.getOrganization(messageContext);
    try {
        if (!query.contains(":")) {
            query = (APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":" + query);
        }
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        Map<String, Object> result = null;
        // Extracting search queries for the recommendation system
        apiConsumer.publishSearchQuery(query, username);
        if (query.startsWith(APIConstants.CONTENT_SEARCH_TYPE_PREFIX)) {
            result = apiConsumer.searchPaginatedContent(query, organization, offset, limit);
        } else {
            result = apiConsumer.searchPaginatedAPIs(query, organization, offset, limit, null, null);
        }
        ArrayList<Object> apis;
        /* Above searchPaginatedAPIs method underneath calls searchPaginatedAPIsByContent method,searchPaginatedAPIs
            method and searchAPIDoc method in AbstractApiManager. And those methods respectively returns ArrayList,
            TreeSet and a HashMap.
            Hence the below logic.
            */
        Object apiSearchResults = result.get("apis");
        if (apiSearchResults instanceof List<?>) {
            apis = (ArrayList<Object>) apiSearchResults;
        } else if (apiSearchResults instanceof HashMap) {
            Collection<String> values = ((HashMap) apiSearchResults).values();
            apis = new ArrayList<Object>(values);
        } else {
            apis = new ArrayList<Object>();
            apis.addAll((Collection<?>) apiSearchResults);
        }
        for (Object searchResult : apis) {
            if (searchResult instanceof API) {
                API api = (API) searchResult;
                SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIToAPIResultDTO(api);
                allmatchedResults.add(apiResult);
            } else if (searchResult instanceof Map.Entry) {
                Map.Entry pair = (Map.Entry) searchResult;
                SearchResultDTO docResult = SearchResultMappingUtil.fromDocumentationToDocumentResultDTO((Documentation) pair.getKey(), (API) pair.getValue());
                allmatchedResults.add(docResult);
            } else if (searchResult instanceof APIProduct) {
                APIProduct apiProduct = (APIProduct) searchResult;
                SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIToAPIResultDTO(apiProduct);
                allmatchedResults.add(apiResult);
            }
        }
        Object totalLength = result.get("length");
        Integer length = 0;
        if (totalLength != null) {
            length = (Integer) totalLength;
        }
        List<Object> allmatchedObjectResults = new ArrayList<>(allmatchedResults);
        resultListDTO.setList(allmatchedObjectResults);
        resultListDTO.setCount(allmatchedResults.size());
        SearchResultMappingUtil.setPaginationParams(resultListDTO, query, offset, limit, length);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving search results";
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return Response.ok().entity(resultListDTO).build();
}
Also used : HashMap(java.util.HashMap) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) ArrayList(java.util.ArrayList) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) SearchResultDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SearchResultDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SearchResultListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SearchResultListDTO) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) API(org.wso2.carbon.apimgt.api.model.API) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) HashMap(java.util.HashMap) Map(java.util.Map)

Example 90 with Documentation

use of org.wso2.carbon.apimgt.persistence.dto.Documentation in project carbon-apimgt by wso2.

the class SearchResultMappingUtil method fromDocumentationToDocumentResultDTO.

/**
 * Get Document result representation for content search
 * @param document
 * @return
 */
public static DocumentSearchResultDTO fromDocumentationToDocumentResultDTO(Documentation document, API api) {
    DocumentSearchResultDTO docResultDTO = new DocumentSearchResultDTO();
    docResultDTO.setId(document.getId());
    docResultDTO.setName(document.getName());
    docResultDTO.setDocType(DocumentSearchResultDTO.DocTypeEnum.valueOf(document.getType().toString()));
    docResultDTO.setType(SearchResultDTO.TypeEnum.DOC);
    docResultDTO.setSummary(document.getSummary());
    docResultDTO.setVisibility(DocumentSearchResultDTO.VisibilityEnum.valueOf(document.getVisibility().toString()));
    docResultDTO.setSourceType(DocumentSearchResultDTO.SourceTypeEnum.valueOf(document.getSourceType().toString()));
    docResultDTO.setOtherTypeName(document.getOtherTypeName());
    APIIdentifier apiId = api.getId();
    docResultDTO.setApiName(apiId.getApiName());
    docResultDTO.setApiVersion(apiId.getVersion());
    docResultDTO.setApiProvider(apiId.getProviderName());
    docResultDTO.setApiUUID(api.getUUID());
    return docResultDTO;
}
Also used : APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) DocumentSearchResultDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.DocumentSearchResultDTO)

Aggregations

Documentation (org.wso2.carbon.apimgt.api.model.Documentation)56 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 Test (org.testng.annotations.Test)38 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)34 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)33 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)32 ArrayList (java.util.ArrayList)29 Resource (org.wso2.carbon.registry.core.Resource)27 HashMap (java.util.HashMap)26 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)26 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)24 API (org.wso2.carbon.apimgt.api.model.API)23 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)23 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)22 Test (org.junit.Test)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)18 Registry (org.wso2.carbon.registry.core.Registry)18 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)17 DocumentationPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException)17