Search in sources :

Example 96 with Documentation

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

the class APIPublisherImplTestCase method testAddDocumentationInfoJsonParseException.

@Test(description = "Parse exception when adding documentation info", expectedExceptions = APIManagementException.class)
public void testAddDocumentationInfoJsonParseException() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    DocumentInfo documentInfo = new DocumentInfo.Builder().fileName("sample_doc.pdf").name("howto_guide").id("").permission("data").build();
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO);
    apiPublisher.addDocumentationInfo(API_ID, documentInfo);
}
Also used : ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Example 97 with Documentation

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

the class APIPublisherImplTestCase method testUpdateDocumentation.

@Test(description = "Update Documentation Info")
public void testUpdateDocumentation() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    DocumentInfo documentInfo = new DocumentInfo.Builder().fileName("sample_doc.pdf").name("howto_guide").id(DOC_ID).permission("[{\"groupId\": \"testGroup\",\"permission\":[\"READ\",\"UPDATE\",\"DELETE\"]}]").build();
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO);
    Mockito.when(apiDAO.isDocumentExist(API_ID, documentInfo)).thenReturn(true);
    apiPublisher.updateDocumentation(API_ID, documentInfo);
    Mockito.verify(apiDAO, Mockito.times(1)).updateDocumentInfo(API_ID, documentInfo, USER);
}
Also used : ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Example 98 with Documentation

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

the class ApisApiServiceImpl method apisApiIdDocumentsPost.

/**
 * Adds new document to an API
 *
 * @param apiId             UUID of API
 * @param body              DTO object including the document's meta information
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return newly added document meta info object
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdDocumentsPost(String apiId, DocumentDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIPublisher apiProvider = RestAPIPublisherUtil.getApiPublisher(username);
        DocumentInfo documentation = MappingUtil.toDocumentInfo(body);
        if (body.getType() == DocumentDTO.TypeEnum.OTHER && StringUtils.isBlank(body.getOtherTypeName())) {
            // check otherTypeName for not null if doc type is OTHER
            RestApiUtil.handleBadRequest("otherTypeName cannot be empty if type is OTHER.", log);
        }
        String sourceUrl = body.getSourceUrl();
        if (body.getSourceType() == DocumentDTO.SourceTypeEnum.URL && (StringUtils.isBlank(sourceUrl) || !RestApiUtil.isURL(sourceUrl))) {
            RestApiUtil.handleBadRequest("Invalid document sourceUrl Format", log);
        }
        // this will fail if user does not have access to the API or the API does not exist
        String docid = apiProvider.addDocumentationInfo(apiId, documentation);
        documentation = apiProvider.getDocumentationSummary(docid);
        DocumentDTO newDocumentDTO = MappingUtil.toDocumentDTO(documentation);
        // Add initial inline content as empty String, if the Document type is INLINE
        if (body.getSourceType() == DocumentDTO.SourceTypeEnum.INLINE) {
            apiProvider.addDocumentationContent(docid, "");
            if (log.isDebugEnabled()) {
                log.debug("The updated source type of the document " + body.getName() + " is: " + body.getSourceType());
            }
        }
        return Response.status(Response.Status.CREATED).entity(newDocumentDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while create  document for 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();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) DocumentDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 99 with Documentation

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

the class ApisApiServiceImpl method apisApiIdDocumentsDocumentIdPut.

/**
 * Updates an API's document
 *
 * @param apiId             UUID of API
 * @param documentId        UUID of the document
 * @param body              DTO object including the document's meta information
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return updated document meta info DTO as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdDocumentsDocumentIdPut(String apiId, String documentId, DocumentDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        String existingFingerprint = apisApiIdDocumentsDocumentIdGetFingerprint(apiId, documentId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        DocumentInfo documentInfoOld = apiPublisher.getDocumentationSummary(documentId);
        // validation checks for existence of the document
        if (documentInfoOld == null) {
            String msg = "Error while getting document";
            log.error(msg);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900314L, msg);
            log.error(msg);
            return Response.status(Response.Status.NOT_FOUND).entity(errorDTO).build();
        }
        if (body.getType() == DocumentDTO.TypeEnum.OTHER && StringUtils.isBlank(body.getOtherTypeName())) {
            // check otherTypeName for not null if doc type is OTHER
            String msg = "otherTypeName cannot be empty if type is OTHER.";
            log.error(msg);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900313L, msg);
            log.error(msg);
            return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
        }
        if (body.getSourceType() == DocumentDTO.SourceTypeEnum.URL && (StringUtils.isBlank(body.getSourceUrl()) || !RestApiUtil.isURL(body.getSourceUrl()))) {
            // check otherTypeName for not null if doc type is OTHER
            String msg = "Invalid document sourceUrl Format";
            log.error(msg);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900313L, msg);
            log.error(msg);
            return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
        }
        // overriding some properties
        body.setName(documentInfoOld.getName());
        body.setDocumentId(documentInfoOld.getId());
        DocumentInfo documentation = MappingUtil.toDocumentInfo(body);
        // this will fail if user does not have access to the API or the API does not exist
        apiPublisher.updateDocumentation(apiId, documentation);
        // retrieve the updated documentation
        DocumentInfo newDocumentation = apiPublisher.getDocumentationSummary(documentId);
        String newFingerprint = apisApiIdDocumentsDocumentIdGetFingerprint(apiId, documentId, null, null, request);
        return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(MappingUtil.toDocumentDTO(newDocumentation)).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while updating the document " + documentId + " for API : " + apiId;
        log.error(errorMessage, e);
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        paramList.put(APIMgtConstants.ExceptionsConstants.DOC_ID, documentId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 100 with Documentation

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

the class ApisApiServiceImpl method apisApiIdDocumentsDocumentIdContentPost.

/**
 * Uploads a document's content and attach to particular document
 *
 * @param apiId             UUID of API
 * @param documentId        UUID of the document
 * @param fileInputStream   file content stream
 * @param fileDetail        meta infomation about the file
 * @param inlineContent     inline documentation content
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return updated document meta information
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdDocumentsDocumentIdContentPost(String apiId, String documentId, InputStream fileInputStream, FileInfo fileDetail, String inlineContent, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIPublisher apiProvider = RestAPIPublisherUtil.getApiPublisher(username);
        String existingFingerprint = apisApiIdDocumentsDocumentIdContentGetFingerprint(apiId, documentId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        if (fileInputStream != null && inlineContent != null) {
            String msg = "Only one of 'file' and 'inlineContent' should be specified";
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900314L, msg);
            log.error(msg);
            return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
        }
        // retrieves the document and send 404 if not found
        DocumentInfo documentation = apiProvider.getDocumentationSummary(documentId);
        if (documentation == null) {
            String msg = "Documentation not found " + documentId;
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900314L, msg);
            log.error(msg);
            return Response.status(Response.Status.NOT_FOUND).entity(errorDTO).build();
        }
        // add content depending on the availability of either input stream or inline content
        if (fileInputStream != null) {
            if (!documentation.getSourceType().equals(DocumentInfo.SourceType.FILE)) {
                String msg = "Source type of document " + documentId + " is not FILE";
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900314L, msg);
                log.error(msg);
                return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
            }
            apiProvider.uploadDocumentationFile(documentId, fileInputStream, fileDetail.getContentType());
        } else if (inlineContent != null) {
            if (!documentation.getSourceType().equals(DocumentInfo.SourceType.INLINE)) {
                String msg = "Source type of document " + documentId + " is not INLINE";
                log.error(msg);
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900976L, msg);
                return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
            }
            apiProvider.addDocumentationContent(documentId, inlineContent);
        } else {
            String msg = "Either 'file' or 'inlineContent' should be specified";
            log.error(msg);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900976L, msg);
            return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
        }
        String newFingerprint = apisApiIdDocumentsDocumentIdContentGetFingerprint(apiId, documentId, null, null, request);
        return Response.status(Response.Status.CREATED).header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding content to document" + documentId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        paramList.put(APIMgtConstants.ExceptionsConstants.DOC_ID, documentId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

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