Search in sources :

Example 11 with DocumentContent

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

the class AbstractAPIManager method getDocumentationContent.

@Override
public DocumentationContent getDocumentationContent(String apiId, String docId, String organization) throws APIManagementException {
    try {
        DocumentContent content = apiPersistenceInstance.getDocumentationContent(new Organization(organization), apiId, docId);
        DocumentationContent docContent = null;
        if (content != null) {
            docContent = DocumentMapper.INSTANCE.toDocumentationContent(content);
        } else {
            String msg = "Failed to get the document content. Artifact corresponding to document id " + docId + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
        return docContent;
    } catch (DocumentationPersistenceException e) {
        throw new APIManagementException("Error while retrieving document content ", e);
    }
}
Also used : DocumentationContent(org.wso2.carbon.apimgt.api.model.DocumentationContent) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) DocumentContent(org.wso2.carbon.apimgt.persistence.dto.DocumentContent) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 12 with DocumentContent

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

the class AbstractAPIManager method getDocumentationContent.

/**
 * This method used to get the content of a documentation
 *
 * @param docId Document ID
 * @return {@link InputStream} Input stream for document content
 * @throws APIManagementException if the requested documentation content is not available
 */
public DocumentContent getDocumentationContent(String docId) throws APIManagementException {
    try {
        DocumentInfo documentInfo = getDocumentationSummary(docId);
        DocumentContent.Builder documentContentBuilder = new DocumentContent.Builder();
        if (documentInfo != null) {
            documentContentBuilder.documentInfo(documentInfo);
            if (DocumentInfo.SourceType.FILE.equals(documentInfo.getSourceType())) {
                InputStream inputStream = getApiDAO().getDocumentFileContent(docId);
                if (inputStream != null) {
                    documentContentBuilder = documentContentBuilder.fileContent(inputStream);
                } else {
                    throw new APIManagementException("Couldn't find file content of  document", ExceptionCodes.DOCUMENT_CONTENT_NOT_FOUND);
                }
            } else if (documentInfo.getSourceType().equals(DocumentInfo.SourceType.INLINE)) {
                String inlineContent = getApiDAO().getDocumentInlineContent(docId);
                if (inlineContent != null) {
                    documentContentBuilder = documentContentBuilder.inlineContent(inlineContent);
                } else {
                    throw new APIManagementException("Couldn't find inline content of  document", ExceptionCodes.DOCUMENT_CONTENT_NOT_FOUND);
                }
            }
        } else {
            throw new APIManagementException("Couldn't fnd document", ExceptionCodes.DOCUMENT_NOT_FOUND);
        }
        return documentContentBuilder.build();
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while retrieving document content";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) InputStream(java.io.InputStream) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 13 with DocumentContent

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

the class ApisApiServiceImpl method apisApiIdDocumentsDocumentIdContentGet.

/**
 * Retrieves the content of the document
 *
 * @param apiId           API ID
 * @param documentId      Document ID
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return content of the document
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdDocumentsDocumentIdContentGet(String apiId, String documentId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = apisApiIdDocumentsDocumentIdContentGetFingerprint(apiId, documentId, ifNoneMatch, ifModifiedSince, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        DocumentContent documentationContent = apiStore.getDocumentationContent(documentId);
        DocumentInfo documentInfo = documentationContent.getDocumentInfo();
        if (DocumentInfo.SourceType.FILE.equals(documentInfo.getSourceType())) {
            String filename = documentInfo.getFileName();
            return Response.ok(documentationContent.getFileContent()).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_TYPE).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"").header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
        } else if (DocumentInfo.SourceType.INLINE.equals(documentInfo.getSourceType())) {
            String content = documentationContent.getInlineContent();
            return Response.ok(content).header(RestApiConstants.HEADER_CONTENT_TYPE, MediaType.TEXT_PLAIN).header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
        } else if (DocumentInfo.SourceType.URL.equals(documentInfo.getSourceType())) {
            String sourceUrl = documentInfo.getSourceURL();
            return Response.seeOther(new URI(sourceUrl)).header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving document " + documentId + " of the API " + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving source URI location of " + documentId;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
        log.error(errorMessage, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 14 with DocumentContent

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

the class APIImportExportTestCase method testGetApiDetails.

@Test(description = "Test getAPIDetails - single API")
void testGetApiDetails() throws APIManagementException {
    printTestMethodName();
    apiPublisher = Mockito.mock(APIPublisher.class);
    String api1Id = UUID.randomUUID().toString();
    Endpoint api1SandBoxEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("abcd").build();
    Endpoint api1ProdEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("cdef").build();
    API api1 = createApi("provider1", api1Id, "testapi1", "1.0.0", "Test API 1 - version 1.0.0", createEndpointTypeToIdMap(api1SandBoxEndpointId, api1ProdEndpointId)).build();
    String api1Doc1Id = UUID.randomUUID().toString();
    DocumentInfo api1Doc1Info = createAPIDoc(api1Doc1Id, "api1doc1", "", "API 1 DOC 1", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.INLINE, "", DocumentInfo.Visibility.PRIVATE);
    String api1Doc2Id = UUID.randomUUID().toString();
    DocumentInfo api1Doc2Info = createAPIDoc(api1Doc2Id, "api1doc2.pdf", "api1doc2.pdf", "API 1 DOC 2", DocumentInfo.DocType.PUBLIC_FORUM, "other type", DocumentInfo.SourceType.FILE, "", DocumentInfo.Visibility.API_LEVEL);
    String api1Doc3Id = UUID.randomUUID().toString();
    DocumentInfo api1Doc3Info = createAPIDoc(api1Doc3Id, "api1doc3", "", "API 1 DOC 3", DocumentInfo.DocType.OTHER, "other type", DocumentInfo.SourceType.OTHER, "", DocumentInfo.Visibility.API_LEVEL);
    List<DocumentInfo> api1DocumentInfo = new ArrayList<>();
    api1DocumentInfo.add(api1Doc1Info);
    api1DocumentInfo.add(api1Doc2Info);
    api1DocumentInfo.add(api1Doc3Info);
    // contents for documents
    DocumentContent api1Doc1Content = createDocContent(api1Doc1Info, "Sample inline content for API1 DOC 1", null);
    DocumentContent api1Doc2Content = createDocContent(api1Doc2Info, "", api1Doc2Stream);
    DocumentContent api1Doc3Content = createDocContent(api1Doc3Info, "", null);
    Mockito.when(apiPublisher.getAPIbyUUID(api1Id)).thenReturn(api1);
    Mockito.when(apiPublisher.getApiSwaggerDefinition(api1Id)).thenReturn(api1Definition);
    Mockito.when(apiPublisher.getApiGatewayConfig(api1Id)).thenReturn(api1GatewayConfig);
    Mockito.when(apiPublisher.getAllDocumentation(api1Id, 0, Integer.MAX_VALUE)).thenReturn(api1DocumentInfo);
    Mockito.when(apiPublisher.getDocumentationContent(api1Doc1Id)).thenReturn(api1Doc1Content);
    Mockito.when(apiPublisher.getDocumentationContent(api1Doc2Id)).thenReturn(api1Doc2Content);
    Mockito.when(apiPublisher.getDocumentationContent(api1Doc3Id)).thenReturn(api1Doc3Content);
    Mockito.when(apiPublisher.getThumbnailImage(api1Id)).thenReturn(getClass().getClassLoader().getResourceAsStream("api1_thumbnail.png"));
    List<API> apis = new ArrayList<>();
    apis.add(api1);
    Mockito.when(apiPublisher.searchAPIs(Integer.MAX_VALUE, 0, "*")).thenReturn(apis);
    ApiImportExportManager importExportManager = new ApiImportExportManager(apiPublisher);
    Set<APIDetails> apiDetailsSet = importExportManager.getAPIDetails(Integer.MAX_VALUE, 0, "*");
    Assert.assertEquals(new ArrayList<>(apiDetailsSet).get(0).getApi().getId().equals(api1Id), true, "APIDetails not retrieved correctly for API: " + api1.getName() + ", version: " + api1.getVersion());
}
Also used : APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) ArrayList(java.util.ArrayList) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) API(org.wso2.carbon.apimgt.core.models.API) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Example 15 with DocumentContent

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

the class APIImportExportTestCase method testGetMultipleApiDetailsWithNonFatalErrors.

@Test(description = "Test getAPIDetails - multiple APIs with non-critical error in retrieving information of one API")
void testGetMultipleApiDetailsWithNonFatalErrors() throws APIManagementException {
    printTestMethodName();
    apiPublisher = Mockito.mock(APIPublisher.class);
    String api4Id = UUID.randomUUID().toString();
    Endpoint api4SandBoxEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("abcd").build();
    Endpoint api4ProdEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("cdef").build();
    API api4 = createApi("provider4", api4Id, "testapi4", "1.0.0", "Test API 4 - version 1.0.0", createEndpointTypeToIdMap(api4SandBoxEndpointId, api4ProdEndpointId)).build();
    String api4Doc1Id = UUID.randomUUID().toString();
    DocumentInfo api4Doc1Info = createAPIDoc(api4Doc1Id, "api1doc1", "", "API 4 DOC 1", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.INLINE, "", DocumentInfo.Visibility.PRIVATE);
    String api4Doc2Id = UUID.randomUUID().toString();
    DocumentInfo api4Doc2Info = createAPIDoc(api4Doc2Id, "api1doc2.pdf", "api1doc2.pdf", "API 4 DOC 2", DocumentInfo.DocType.PUBLIC_FORUM, "other type", DocumentInfo.SourceType.FILE, "", DocumentInfo.Visibility.API_LEVEL);
    String api4Doc3Id = UUID.randomUUID().toString();
    DocumentInfo api4Doc3Info = createAPIDoc(api4Doc3Id, "api1doc3", "", "API 4 DOC 3", DocumentInfo.DocType.OTHER, "other type", DocumentInfo.SourceType.OTHER, "", DocumentInfo.Visibility.API_LEVEL);
    List<DocumentInfo> api1DocumentInfo = new ArrayList<>();
    api1DocumentInfo.add(api4Doc1Info);
    api1DocumentInfo.add(api4Doc2Info);
    api1DocumentInfo.add(api4Doc3Info);
    // contents for documents
    DocumentContent api4Doc1Content = createDocContent(api4Doc1Info, "Sample inline content for API1 DOC 1", null);
    DocumentContent api4Doc2Content = createDocContent(api4Doc2Info, "", api1Doc2Stream);
    DocumentContent api4Doc3Content = createDocContent(api4Doc3Info, "", null);
    Mockito.when(apiPublisher.getAPIbyUUID(api4Id)).thenReturn(api4);
    Mockito.when(apiPublisher.getApiSwaggerDefinition(api4Id)).thenReturn(api1Definition);
    Mockito.when(apiPublisher.getApiGatewayConfig(api4Id)).thenReturn(api1GatewayConfig);
    Mockito.when(apiPublisher.getAllDocumentation(api4Id, 0, Integer.MAX_VALUE)).thenReturn(api1DocumentInfo);
    Mockito.when(apiPublisher.getDocumentationContent(api4Doc1Id)).thenReturn(api4Doc1Content);
    Mockito.when(apiPublisher.getDocumentationContent(api4Doc2Id)).thenReturn(api4Doc2Content);
    Mockito.when(apiPublisher.getDocumentationContent(api4Doc3Id)).thenReturn(api4Doc3Content);
    Mockito.when(apiPublisher.getThumbnailImage(api4Id)).thenReturn(null);
    String api5Id = UUID.randomUUID().toString();
    Endpoint api5SandBoxEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("abcd").build();
    Endpoint api5ProdEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("cdef").build();
    API api5 = createApi("provider5", api5Id, "testapi4", "1.0.0", "Test API 5 - version 1.0.0", createEndpointTypeToIdMap(api5SandBoxEndpointId, api5ProdEndpointId)).build();
    String api5Doc1Id = UUID.randomUUID().toString();
    DocumentInfo api5Doc1Info = createAPIDoc(api5Doc1Id, "api1doc1", "", "API 5 DOC 1", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.INLINE, "", DocumentInfo.Visibility.PRIVATE);
    String api5Doc3Id = UUID.randomUUID().toString();
    DocumentInfo api5Doc3Info = createAPIDoc(api5Doc3Id, "api1doc3", "", "API 5 DOC 3", DocumentInfo.DocType.OTHER, "other type", DocumentInfo.SourceType.OTHER, "", DocumentInfo.Visibility.API_LEVEL);
    List<DocumentInfo> api5DocumentInfo = new ArrayList<>();
    api5DocumentInfo.add(api5Doc1Info);
    api5DocumentInfo.add(api5Doc3Info);
    // contents for documents
    DocumentContent api5Doc1Content = createDocContent(api5Doc1Info, "Sample inline content for API1 DOC 1", null);
    DocumentContent api5Doc3Content = createDocContent(api5Doc3Info, "", null);
    Mockito.when(apiPublisher.getAPIbyUUID(api5Id)).thenReturn(api5);
    Mockito.when(apiPublisher.getApiSwaggerDefinition(api5Id)).thenReturn(api1Definition);
    Mockito.when(apiPublisher.getApiGatewayConfig(api5Id)).thenReturn(api1GatewayConfig);
    Mockito.when(apiPublisher.getAllDocumentation(api5Id, 0, Integer.MAX_VALUE)).thenReturn(api5DocumentInfo);
    Mockito.when(apiPublisher.getDocumentationContent(api5Doc1Id)).thenReturn(api5Doc1Content);
    Mockito.when(apiPublisher.getDocumentationContent(api5Doc3Id)).thenReturn(api5Doc3Content);
    Mockito.when(apiPublisher.getThumbnailImage(api5Id)).thenReturn(getClass().getClassLoader().getResourceAsStream("api1_thumbnail.png"));
    Mockito.when(apiPublisher.getAllDocumentation(api4Id, 0, Integer.MAX_VALUE)).thenThrow(APIManagementException.class);
    List<API> apis = new ArrayList<>();
    apis.add(api4);
    apis.add(api5);
    Mockito.when(apiPublisher.searchAPIs(Integer.MAX_VALUE, 0, "*")).thenReturn(apis);
    ApiImportExportManager importExportManager = new ApiImportExportManager(apiPublisher);
    Set<APIDetails> apiDetailsSet = importExportManager.getAPIDetails(Integer.MAX_VALUE, 0, "*");
    Assert.assertEquals(apiDetailsSet.size() == 2, true, "Error getting API details");
}
Also used : APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) ArrayList(java.util.ArrayList) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) API(org.wso2.carbon.apimgt.core.models.API) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Aggregations

DocumentContent (org.wso2.carbon.apimgt.core.models.DocumentContent)16 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)16 API (org.wso2.carbon.apimgt.core.models.API)9 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)9 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)7 APIDetails (org.wso2.carbon.apimgt.core.models.APIDetails)7 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Test (org.testng.annotations.Test)4 DocumentationPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException)4 InputStream (java.io.InputStream)3 Response (javax.ws.rs.core.Response)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)3 DocumentContent (org.wso2.carbon.apimgt.persistence.dto.DocumentContent)3 IOException (java.io.IOException)2