Search in sources :

Example 1 with APIMappingUtil

use of org.wso2.carbon.apimgt.rest.api.store.v1.mappings.APIMappingUtil in project carbon-apimgt by wso2.

the class APIMappingUtilTestCase method testToAPIDTO.

@Test
public void testToAPIDTO() throws APIManagementException {
    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();
    APIMappingUtil apiMappingUtil = new APIMappingUtil();
    APIDTO apidto = apiMappingUtil.toAPIDTO(api1);
    Assert.assertEquals(apidto.getName(), "testapi1");
}
Also used : APIDTO(org.wso2.carbon.apimgt.rest.api.store.dto.APIDTO) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) API(org.wso2.carbon.apimgt.core.models.API) Test(org.junit.Test)

Example 2 with APIMappingUtil

use of org.wso2.carbon.apimgt.rest.api.store.v1.mappings.APIMappingUtil in project carbon-apimgt by wso2.

the class APIMappingUtilTestCase method testAPIListDTO.

@Test
public void testAPIListDTO() throws APIManagementException {
    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 api2Id = UUID.randomUUID().toString();
    Endpoint api2SandBoxEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("abcd123").build();
    Endpoint api2ProdEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("cdef123").build();
    API api2 = createApi("provider1", api2Id, "testapi2", "1.0.0", "Test API 2 - version 1.0.0", createEndpointTypeToIdMap(api2SandBoxEndpointId, api2ProdEndpointId)).build();
    List<API> apiList = new ArrayList<>();
    apiList.add(api1);
    apiList.add(api2);
    APIMappingUtil apiMappingUtil = new APIMappingUtil();
    APIListDTO apiListDTO = apiMappingUtil.toAPIListDTO(apiList);
    Assert.assertEquals(apiListDTO.getList().get(0).getName(), "testapi1");
    Assert.assertEquals(apiListDTO.getList().get(1).getName(), "testapi2");
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) ArrayList(java.util.ArrayList) APIListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.APIListDTO) API(org.wso2.carbon.apimgt.core.models.API) Test(org.junit.Test)

Example 3 with APIMappingUtil

use of org.wso2.carbon.apimgt.rest.api.store.v1.mappings.APIMappingUtil in project carbon-apimgt by wso2.

the class RestApiPublisherUtils method attachFileToProductDocument.

/**
 * 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
 * @param organization organization of the API
 * @throws APIManagementException if unable to add the file
 */
public static void attachFileToProductDocument(String productId, Documentation documentation, InputStream inputStream, Attachment fileDetails, String organization) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    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;
        PublisherCommonUtils.addDocumentationContentForFile(docInputStream, mediaType, filename, apiProvider, productId, documentId, organization);
        docFile.deleteOnExit();
    } catch (FileNotFoundException e) {
        RestApiUtil.handleInternalServerError("Unable to read the file from path ", e, log);
    } finally {
        IOUtils.closeQuietly(docInputStream);
    }
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 4 with APIMappingUtil

use of org.wso2.carbon.apimgt.rest.api.store.v1.mappings.APIMappingUtil in project carbon-apimgt by wso2.

the class RestApiPublisherUtils method attachFileToDocument.

/**
 * 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
 * @param organization  identifier of an organization
 * @throws APIManagementException if unable to add the file
 */
public static void attachFileToDocument(String apiId, Documentation documentation, InputStream inputStream, Attachment fileDetails, String organization) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    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;
        PublisherCommonUtils.addDocumentationContentForFile(docInputStream, mediaType, filename, apiProvider, apiId, documentId, organization);
        docFile.deleteOnExit();
    } catch (FileNotFoundException e) {
        RestApiUtil.handleInternalServerError("Unable to read the file from path ", e, log);
    } finally {
        IOUtils.closeQuietly(docInputStream);
    }
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)2 Test (org.junit.Test)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2 API (org.wso2.carbon.apimgt.core.models.API)2 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)2 ArrayList (java.util.ArrayList)1 APIDTO (org.wso2.carbon.apimgt.rest.api.store.dto.APIDTO)1 APIListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.APIListDTO)1