Search in sources :

Example 46 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class DocumentIndexer method fetchDocumentContent.

/**
 * Write document content to document artifact as its raw content
 *
 * @param registry
 * @param documentResource
 * @return
 * @throws RegistryException
 * @throws IOException
 * @throws APIManagementException
 */
private String fetchDocumentContent(Registry registry, Resource documentResource) throws RegistryException, IOException, APIManagementException {
    GenericArtifactManager docArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
    GenericArtifact documentArtifact = docArtifactManager.getGenericArtifact(documentResource.getUUID());
    String sourceType = documentArtifact.getAttribute(APIConstants.DOC_SOURCE_TYPE);
    String contentString = null;
    if (Documentation.DocumentSourceType.FILE.name().equals(sourceType)) {
        String path = documentArtifact.getAttribute(APIConstants.DOC_FILE_PATH);
        int indexOfApimgt = path.indexOf(APIConstants.APIMGT_REGISTRY_LOCATION);
        String filepath = path.substring(indexOfApimgt);
        Resource contentResource = registry.get(filepath);
        int indexOfFiles = filepath.indexOf(APIConstants.DOCUMENT_FILE_DIR) + APIConstants.DOCUMENT_FILE_DIR.length() + 1;
        String fileName = filepath.substring(indexOfFiles);
        String extension = FilenameUtils.getExtension(fileName);
        InputStream inputStream = null;
        try {
            inputStream = contentResource.getContentStream();
            switch(extension) {
                case APIConstants.PDF_EXTENSION:
                    PDFParser pdfParser = new PDFParser(new RandomAccessBufferedFileInputStream(inputStream));
                    pdfParser.parse();
                    COSDocument cosDocument = pdfParser.getDocument();
                    PDFTextStripper stripper = new PDFTextStripper();
                    contentString = stripper.getText(new PDDocument(cosDocument));
                    break;
                case APIConstants.DOC_EXTENSION:
                    {
                        POIFSFileSystem pfs = new POIFSFileSystem(inputStream);
                        WordExtractor msWord2003Extractor = new WordExtractor(pfs);
                        contentString = msWord2003Extractor.getText();
                        break;
                    }
                case APIConstants.DOCX_EXTENSION:
                    XWPFDocument doc = new XWPFDocument(inputStream);
                    XWPFWordExtractor msWord2007Extractor = new XWPFWordExtractor(doc);
                    contentString = msWord2007Extractor.getText();
                    break;
                case APIConstants.XLS_EXTENSION:
                    {
                        POIFSFileSystem pfs = new POIFSFileSystem(inputStream);
                        ExcelExtractor extractor = new ExcelExtractor(pfs);
                        contentString = extractor.getText();
                        break;
                    }
                case APIConstants.XLSX_EXTENSION:
                    XSSFWorkbook xssfSheets = new XSSFWorkbook(inputStream);
                    XSSFExcelExtractor xssfExcelExtractor = new XSSFExcelExtractor(xssfSheets);
                    contentString = xssfExcelExtractor.getText();
                    break;
                case APIConstants.PPT_EXTENSION:
                    {
                        POIFSFileSystem fs = new POIFSFileSystem(inputStream);
                        PowerPointExtractor extractor = new PowerPointExtractor(fs);
                        contentString = extractor.getText();
                        break;
                    }
                case APIConstants.PPTX_EXTENSION:
                    XMLSlideShow xmlSlideShow = new XMLSlideShow(inputStream);
                    XSLFPowerPointExtractor xslfPowerPointExtractor = new XSLFPowerPointExtractor(xmlSlideShow);
                    contentString = xslfPowerPointExtractor.getText();
                    break;
                case APIConstants.TXT_EXTENSION:
                case APIConstants.WSDL_EXTENSION:
                case APIConstants.XML_DOC_EXTENSION:
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                    String line;
                    StringBuilder contentBuilder = new StringBuilder();
                    while ((line = reader.readLine()) != null) {
                        contentBuilder.append(line);
                    }
                    contentString = contentBuilder.toString();
                    break;
            }
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    } else if (Documentation.DocumentSourceType.INLINE.name().equals(sourceType)) {
        String fileName = ((ResourceImpl) documentResource).getName();
        String pathToDocFile = documentResource.getPath();
        String pathToContent = pathToDocFile.substring(0, pathToDocFile.lastIndexOf(fileName)) + APIConstants.INLINE_DOCUMENT_CONTENT_DIR + RegistryConstants.PATH_SEPARATOR + fileName;
        if (registry.resourceExists(pathToContent)) {
            Resource contentResource = registry.get(pathToContent);
            InputStream instream = null;
            BufferedReader reader = null;
            String line;
            try {
                instream = contentResource.getContentStream();
                reader = new BufferedReader(new InputStreamReader(instream));
                StringBuilder contentBuilder = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    contentBuilder.append(line);
                }
                contentString = contentBuilder.toString();
            } finally {
                if (reader != null) {
                    IOUtils.closeQuietly(reader);
                }
            }
        }
    }
    return contentString;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) InputStreamReader(java.io.InputStreamReader) XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) RandomAccessBufferedFileInputStream(org.apache.pdfbox.io.RandomAccessBufferedFileInputStream) InputStream(java.io.InputStream) PowerPointExtractor(org.apache.poi.hslf.extractor.PowerPointExtractor) XSLFPowerPointExtractor(org.apache.poi.xslf.extractor.XSLFPowerPointExtractor) PDFParser(org.apache.pdfbox.pdfparser.PDFParser) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) Resource(org.wso2.carbon.registry.core.Resource) COSDocument(org.apache.pdfbox.cos.COSDocument) WordExtractor(org.apache.poi.hwpf.extractor.WordExtractor) XWPFWordExtractor(org.apache.poi.xwpf.extractor.XWPFWordExtractor) XSLFPowerPointExtractor(org.apache.poi.xslf.extractor.XSLFPowerPointExtractor) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) ExcelExtractor(org.apache.poi.hssf.extractor.ExcelExtractor) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) BufferedReader(java.io.BufferedReader) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) RandomAccessBufferedFileInputStream(org.apache.pdfbox.io.RandomAccessBufferedFileInputStream) PDFTextStripper(org.apache.pdfbox.text.PDFTextStripper)

Example 47 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class GAConfigMediaTypeHandler method put.

public void put(RequestContext requestContext) throws RegistryException {
    ResourceImpl resource = (ResourceImpl) requestContext.getResource();
    if (!resource.isContentModified()) {
        return;
    }
    // Local entry is updated only if the content of ga-config is updated
    Object content = resource.getContent();
    if (!(content instanceof String)) {
        if (!(content instanceof byte[])) {
            log.warn("The resource content is not of expected type");
            return;
        }
    }
    String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
    GoogleAnalyticsConfigEvent googleAnalyticsConfigEvent = new GoogleAnalyticsConfigEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.GA_CONFIG_UPDATE.toString(), tenantId, tenantDomain);
    APIUtil.sendNotification(googleAnalyticsConfigEvent, APIConstants.NotifierType.GA_CONFIG.name());
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) GoogleAnalyticsConfigEvent(org.wso2.carbon.apimgt.impl.notifier.events.GoogleAnalyticsConfigEvent)

Example 48 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class APIUtilTierTest method testGetAllTiers.

@Test
public void testGetAllTiers() throws APIManagementException, RegistryException {
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
    ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
    ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(ApiMgtDAO.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    PowerMockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(amConfigService);
    Mockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
    Mockito.when(amConfig.getThrottleProperties()).thenReturn(throttleProperties);
    Mockito.when(throttleProperties.isEnableUnlimitedTier()).thenReturn(true);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getGovernanceSystemRegistry()).thenReturn(userRegistry);
    SubscriptionPolicy[] policies = new SubscriptionPolicy[3];
    policies[0] = TestUtils.getUniqueSubscriptionPolicyWithBandwidthLimit();
    policies[1] = TestUtils.getUniqueSubscriptionPolicyWithRequestCountLimit();
    policies[2] = TestUtils.getUniqueSubscriptionPolicyWithBandwidthLimit();
    Mockito.when(apiMgtDAO.getSubscriptionPolicies(Mockito.anyInt())).thenReturn(policies);
    // IsEnabled true scenario
    Assert.assertEquals(3, APIUtil.getAllTiers().size());
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 49 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class ApplicationUtilsTestCase method setup.

@Before
public void setup() throws UserStoreException, RegistryException {
    apiMgtDAOMockCreator = new ApiMgtDAOMockCreator(444);
    apiMgtDAO = apiMgtDAOMockCreator.getMock();
}
Also used : ApiMgtDAOMockCreator(org.wso2.carbon.apimgt.impl.ApiMgtDAOMockCreator) Before(org.junit.Before)

Example 50 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class WorkflowExecutorFactoryTest method testRegistryExceptionWhileInitialisingWorkflowConfigCache.

@Test
public void testRegistryExceptionWhileInitialisingWorkflowConfigCache() throws Exception {
    String errorMessage = "Error occurred while creating workflow configurations for tenant " + tenantDomain;
    Mockito.when(cache.get(Mockito.anyString())).thenReturn(null);
    PowerMockito.whenNew(TenantWorkflowConfigHolder.class).withAnyArguments().thenReturn(tenantWorkflowConfigHolder);
    PowerMockito.doThrow(new RegistryException(errorMessage)).when(tenantWorkflowConfigHolder).load();
    try {
        workflowExecutorFactory.getWorkflowConfigurations();
        Assert.fail("Expected WorkflowException has not occurred while retrieving workflow configuration");
    } catch (WorkflowException e) {
        Assert.assertEquals(e.getMessage(), errorMessage);
    }
}
Also used : RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)235 Resource (org.wso2.carbon.registry.core.Resource)196 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)167 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)145 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)104 Registry (org.wso2.carbon.registry.core.Registry)95 Test (org.junit.Test)81 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)81 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)75 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)70 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)67 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)61 UserStoreException (org.wso2.carbon.user.api.UserStoreException)60 API (org.wso2.carbon.apimgt.api.model.API)58 IOException (java.io.IOException)57 ArrayList (java.util.ArrayList)55 QName (javax.xml.namespace.QName)42 APIResource (org.wso2.carbon.apimgt.api.doc.model.APIResource)40 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)40 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)40