Java Code Examples for org.wso2.carbon.apimgt.api.APIProvider#updateDocumentation()

The following examples show how to use org.wso2.carbon.apimgt.api.APIProvider#updateDocumentation() . 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: RestApiPublisherUtils.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Attaches a file to the specified document
 *
 * @param apiId identifier of the API, the document belongs to
 * @param documentation Documentation object
 * @param inputStream input Stream containing the file
 * @param fileDetails file details object as cxf Attachment
 * @throws APIManagementException if unable to add the file
 */
public static void attachFileToDocument(String apiId, Documentation documentation, InputStream inputStream,
                                        Attachment fileDetails) throws APIManagementException {

    APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
    String tenantDomain = RestApiUtil.getLoggedInUserTenantDomain();
    String documentId = documentation.getId();
    String randomFolderName = RandomStringUtils.randomAlphanumeric(10);
    String tmpFolder = System.getProperty(RestApiConstants.JAVA_IO_TMPDIR) + File.separator
            + RestApiConstants.DOC_UPLOAD_TMPDIR + File.separator + randomFolderName;
    File docFile = new File(tmpFolder);

    boolean folderCreated = docFile.mkdirs();
    if (!folderCreated) {
        RestApiUtil.handleInternalServerError("Failed to add content to the document " + documentId, log);
    }

    InputStream docInputStream = null;
    try {
        ContentDisposition contentDisposition = fileDetails.getContentDisposition();
        String filename = contentDisposition.getParameter(RestApiConstants.CONTENT_DISPOSITION_FILENAME);
        if (StringUtils.isBlank(filename)) {
            filename = RestApiConstants.DOC_NAME_DEFAULT + randomFolderName;
            log.warn(
                    "Couldn't find the name of the uploaded file for the document " + documentId + ". Using name '"
                            + filename + "'");
        }
        APIIdentifier apiIdentifier = APIMappingUtil
                .getAPIIdentifierFromUUID(apiId, tenantDomain);

        RestApiUtil.transferFile(inputStream, filename, docFile.getAbsolutePath());
        docInputStream = new FileInputStream(docFile.getAbsolutePath() + File.separator + filename);
        String mediaType = fileDetails.getHeader(RestApiConstants.HEADER_CONTENT_TYPE);
        mediaType = mediaType == null ? RestApiConstants.APPLICATION_OCTET_STREAM : mediaType;
        apiProvider.addFileToDocumentation(apiIdentifier, documentation, filename, docInputStream, mediaType);
        apiProvider.updateDocumentation(apiIdentifier, documentation);
        docFile.deleteOnExit();
    } catch (FileNotFoundException e) {
        RestApiUtil.handleInternalServerError("Unable to read the file from path ", e, log);
    } finally {
        IOUtils.closeQuietly(docInputStream);
    }
}
 
Example 2
Source File: RestApiPublisherUtils.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Attaches a file to the specified product document
 *
 * @param productId identifier of the API Product, the document belongs to
 * @param documentation Documentation object
 * @param inputStream input Stream containing the file
 * @param fileDetails file details object as cxf Attachment
 * @throws APIManagementException if unable to add the file
 */
public static void attachFileToProductDocument(String productId, Documentation documentation, InputStream inputStream,
        Attachment fileDetails) throws APIManagementException {

    APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
    String tenantDomain = RestApiUtil.getLoggedInUserTenantDomain();
    String documentId = documentation.getId();
    String randomFolderName = RandomStringUtils.randomAlphanumeric(10);
    String tmpFolder = System.getProperty(RestApiConstants.JAVA_IO_TMPDIR) + File.separator
            + RestApiConstants.DOC_UPLOAD_TMPDIR + File.separator + randomFolderName;
    File docFile = new File(tmpFolder);

    boolean folderCreated = docFile.mkdirs();
    if (!folderCreated) {
        RestApiUtil.handleInternalServerError("Failed to add content to the document " + documentId, log);
    }

    InputStream docInputStream = null;
    try {
        ContentDisposition contentDisposition = fileDetails.getContentDisposition();
        String filename = contentDisposition.getParameter(RestApiConstants.CONTENT_DISPOSITION_FILENAME);
        if (StringUtils.isBlank(filename)) {
            filename = RestApiConstants.DOC_NAME_DEFAULT + randomFolderName;
            log.warn(
                    "Couldn't find the name of the uploaded file for the document " + documentId + ". Using name '"
                            + filename + "'");
        }
        APIProductIdentifier productIdentifier = APIMappingUtil
                .getAPIProductIdentifierFromUUID(productId, tenantDomain);

        RestApiUtil.transferFile(inputStream, filename, docFile.getAbsolutePath());
        docInputStream = new FileInputStream(docFile.getAbsolutePath() + File.separator + filename);
        String mediaType = fileDetails.getHeader(RestApiConstants.HEADER_CONTENT_TYPE);
        mediaType = mediaType == null ? RestApiConstants.APPLICATION_OCTET_STREAM : mediaType;
        apiProvider.addFileToProductDocumentation(productIdentifier, documentation, filename, docInputStream, mediaType);
        apiProvider.updateDocumentation(productIdentifier, documentation);
        docFile.deleteOnExit();
    } catch (FileNotFoundException e) {
        RestApiUtil.handleInternalServerError("Unable to read the file from path ", e, log);
    } finally {
        IOUtils.closeQuietly(docInputStream);
    }
}
 
Example 3
Source File: ApiProductsApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
@Override
public Response apiProductsApiProductIdDocumentsDocumentIdPut(String apiProductId, String documentId,
        DocumentDTO body, String ifMatch, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
        String tenantDomain = RestApiUtil.getLoggedInUserTenantDomain();
        String sourceUrl = body.getSourceUrl();
        Documentation oldDocument = apiProvider.getProductDocumentation(documentId, tenantDomain);

        //validation checks for existence of the document
        if (oldDocument == null) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PRODUCT_DOCUMENTATION, documentId, log);
            return null;
        }
        if (body.getType() == DocumentDTO.TypeEnum.OTHER && org.apache.commons.lang3.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);
            return null;
        }
        if (body.getSourceType() == DocumentDTO.SourceTypeEnum.URL &&
                (org.apache.commons.lang3.StringUtils.isBlank(sourceUrl) || !RestApiUtil.isURL(sourceUrl))) {
            RestApiUtil.handleBadRequest("Invalid document sourceUrl Format", log);
            return null;
        }

        //overriding some properties
        body.setName(oldDocument.getName());

        Documentation newDocumentation = DocumentationMappingUtil.fromDTOtoDocumentation(body);
        //this will fail if user does not have access to the API or the API does not exist
        APIProductIdentifier apiIdentifier = APIMappingUtil.getAPIProductIdentifierFromUUID(apiProductId, tenantDomain);
        newDocumentation.setFilePath(oldDocument.getFilePath());
        apiProvider.updateDocumentation(apiIdentifier, newDocumentation);

        //retrieve the updated documentation
        newDocumentation = apiProvider.getProductDocumentation(documentId, tenantDomain);
        return Response.ok().entity(DocumentationMappingUtil.fromDocumentationToDTO(newDocumentation)).build();
    } catch (APIManagementException e) {
        //Auth failure occurs when cross tenant accessing APIs. Sends 404, since we don't need to expose the existence of the resource
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API_PRODUCT, apiProductId, e, log);
        } else if (isAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure(
                    "Authorization failure while updating document : " + documentId + " of API Product " + apiProductId, e, log);
        } else {
            String errorMessage = "Error while updating the document " + documentId + " for API Product : " + apiProductId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}