Search in sources :

Example 51 with DocumentInfo

use of org.wso2.carbon.apimgt.core.models.DocumentInfo 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)

Example 52 with DocumentInfo

use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.

the class ApiDAOImpl method isDocumentExist.

@Override
public boolean isDocumentExist(String apiId, DocumentInfo documentInfo) throws APIMgtDAOException {
    final String query = "SELECT 1 FROM AM_API_DOC_META_DATA INNER JOIN AM_API_RESOURCES " + "ON AM_API_DOC_META_DATA.UUID=AM_API_RESOURCES.UUID WHERE AM_API_RESOURCES.API_ID = ? AND " + "AM_API_DOC_META_DATA.NAME=?";
    boolean exist = false;
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, apiId);
        preparedStatement.setString(2, documentInfo.getName());
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            return resultSet.next();
        }
    } catch (SQLException e) {
        String msg = "checking Document exists for API: " + apiId + " , Document Name: " + documentInfo.getName();
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 53 with DocumentInfo

use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.

the class APIPublisherImpl method updateDocumentation.

/**
 * Updates a given documentation
 *
 * @param apiId        UUID of the API.
 * @param documentInfo Documentation
 * @return UUID of the updated document.
 * @throws APIManagementException if failed to update docs
 */
@Override
public String updateDocumentation(String apiId, DocumentInfo documentInfo) throws APIManagementException {
    try {
        LocalDateTime localDateTime = LocalDateTime.now();
        DocumentInfo document;
        DocumentInfo.Builder docBuilder = new DocumentInfo.Builder(documentInfo);
        docBuilder.updatedBy(getUsername());
        docBuilder.lastUpdatedTime(localDateTime);
        if (StringUtils.isEmpty(docBuilder.getId())) {
            docBuilder = docBuilder.id(UUID.randomUUID().toString());
        }
        if (documentInfo.getPermission() != null && !("").equals(documentInfo.getPermission())) {
            HashMap roleNamePermissionList;
            roleNamePermissionList = APIUtils.getAPIPermissionArray(documentInfo.getPermission());
            docBuilder.permissionMap(roleNamePermissionList);
        }
        document = docBuilder.build();
        if (getApiDAO().isDocumentExist(apiId, document)) {
            getApiDAO().updateDocumentInfo(apiId, document, getUsername());
            return document.getId();
        } else {
            String msg = "Document " + document.getName() + " not found for the api " + apiId;
            log.error(msg);
            throw new APIManagementException(msg, ExceptionCodes.DOCUMENT_NOT_FOUND);
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Unable to update the documentation";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (ParseException e) {
        String errorMsg = "Unable to update the documentation due to json parse error";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.JSON_PARSE_ERROR);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ParseException(org.json.simple.parser.ParseException) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 54 with DocumentInfo

use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testFingerprintAfterUpdatingDocumentContent.

@Test
public void testFingerprintAfterUpdatingDocumentContent() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
    API api = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api);
    DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo();
    apiDAO.addDocumentInfo(api.getId(), documentInfo);
    apiDAO.addDocumentInlineContent(documentInfo.getId(), SampleTestObjectCreator.createDefaultInlineDocumentationContent(), ADMIN);
    String fingerprintBeforeUpdate = ETagUtils.generateETag(apiDAO.getLastUpdatedTimeOfDocumentContent(api.getId(), documentInfo.getId()));
    Assert.assertNotNull(fingerprintBeforeUpdate);
    Thread.sleep(1);
    apiDAO.addDocumentInlineContent(documentInfo.getId(), SampleTestObjectCreator.createAlternativeInlineDocumentationContent(), ADMIN);
    String fingerprintAfterUpdate = ETagUtils.generateETag(apiDAO.getLastUpdatedTimeOfDocumentContent(api.getId(), documentInfo.getId()));
    Assert.assertNotNull(fingerprintBeforeUpdate);
    Assert.assertNotEquals(fingerprintBeforeUpdate, fingerprintAfterUpdate);
}
Also used : CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Example 55 with DocumentInfo

use of org.wso2.carbon.apimgt.core.models.DocumentInfo in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testDocumentAdd.

@Test
public void testDocumentAdd() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().apiDefinition(SampleTestObjectCreator.apiDefinition);
    API api = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api);
    DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultFileDocumentationInfo();
    Assert.assertFalse(apiDAO.isDocumentExist(api.getId(), documentInfo));
    apiDAO.addDocumentInfo(api.getId(), documentInfo);
    apiDAO.addDocumentFileContent(documentInfo.getId(), IOUtils.toInputStream(SampleTestObjectCreator.createDefaultInlineDocumentationContent()), "inline1.txt", documentInfo.getCreatedBy());
    Assert.assertTrue(apiDAO.isDocumentExist(api.getId(), documentInfo));
    List<DocumentInfo> documentInfoList = apiDAO.getDocumentsInfoList(api.getId());
    Assert.assertEquals(documentInfoList.get(0), documentInfo);
    apiDAO.deleteDocument(documentInfo.getId());
    Assert.assertFalse(apiDAO.isDocumentExist(api.getId(), documentInfo));
}
Also used : CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Aggregations

DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)71 Test (org.testng.annotations.Test)28 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)28 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)22 Response (javax.ws.rs.core.Response)21 Test (org.junit.Test)21 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)21 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)20 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)18 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)18 API (org.wso2.carbon.apimgt.core.models.API)17 DocumentContent (org.wso2.carbon.apimgt.core.models.DocumentContent)16 HashMap (java.util.HashMap)14 DocumentDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentDTO)12 ArrayList (java.util.ArrayList)11 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)9 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)9 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)9 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)8 APIDetails (org.wso2.carbon.apimgt.core.models.APIDetails)7