Java Code Examples for org.wso2.carbon.registry.core.Resource#getContentStream()

The following examples show how to use org.wso2.carbon.registry.core.Resource#getContentStream() . 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: DatasourceMigrator.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Migrate the password in datasource configuration
 *
 * @param tenantId
 * @param dataSources
 * @throws MigrationClientException
 */
private void updatePasswordInRegistryDataSources(int tenantId, List<Resource> dataSources)
        throws MigrationClientException {
    for (Resource dataSource : dataSources) {
        try {
            InputStream contentStream = dataSource.getContentStream();
            OMElement omElement = Utility.toOM(contentStream);
            Iterator pit = ((OMElement) ((OMElement) omElement.getChildrenWithName(Constant.DEFINITION_Q).next())
                    .getChildrenWithName(Constant.CONFIGURATION_Q).next()).getChildrenWithName(Constant.PASSWORD_Q);
            while (pit.hasNext()) {
                OMElement passwordElement = (OMElement) pit.next();
                if (Boolean.parseBoolean(passwordElement.getAttributeValue(Constant.ENCRYPTED_Q))) {
                    String password = passwordElement.getText();
                    String newEncryptedPassword = Utility.getNewEncryptedValue(password);
                    if (StringUtils.isNotEmpty(newEncryptedPassword)) {
                        passwordElement.setText(newEncryptedPassword);
                        dataSource.setContent(omElement.toString().getBytes());
                        DataSourceDAO.saveDataSource(tenantId, dataSource);
                    }
                }
            }
        } catch (XMLStreamException | CryptoException | RegistryException | DataSourceException e) {
            throw new MigrationClientException(e.getMessage());
        }
    }
}
 
Example 2
Source File: APIMgtGoogleAnalyticsUtils.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize the google analytics publisher by reading tenants google analytics
 * configuration from the registry
 *
 * @param tenantDomain Tenant domain of the current tenant
 */
public void init(String tenantDomain) {
    configKey = APIConstants.GA_CONFIGURATION_LOCATION;

    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);

        Registry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry();
        Resource resource = registry.get(configKey);
        InputStream in = resource.getContentStream();
        StAXOMBuilder builder = new StAXOMBuilder(in);
        this.gaConfig = new GoogleAnalyticsConfig(builder.getDocumentElement());
    } catch (RegistryException | XMLStreamException e) {
        // flow should not break. Therefore ignoring the exception
        log.error("Failed to retrieve google analytics configurations for tenant:" + tenantDomain);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
 
Example 3
Source File: RegistryBasedTaskRepository.java    From carbon-commons with Apache License 2.0 6 votes vote down vote up
private TaskInfo getTaskInfoRegistryPath(String path) throws Exception {
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(
                MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
        Resource resource = getRegistry().get(path);
        InputStream in = resource.getContentStream();
        TaskInfo taskInfo;
        /*
         * the following synchronized block is to avoid
         * "org.xml.sax.SAXException: FWK005" error where the XML parser is
         * not thread safe
         */
        synchronized (getTaskUnmarshaller()) {
            taskInfo = (TaskInfo) getTaskUnmarshaller().unmarshal(in);
        }
        in.close();
        taskInfo.getProperties().put(TaskInfo.TENANT_ID_PROP,
                String.valueOf(this.getTenantId()));
        return taskInfo;
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
 
Example 4
Source File: SecurityConfigAdmin.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
private Policy loadPolicy(Resource resource) throws org.wso2.carbon.registry.api.RegistryException,
        XMLStreamException {

    InputStream in = resource.getContentStream();
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    XMLStreamReader parser = xmlInputFactory.createXMLStreamReader(in);
    StAXOMBuilder builder = new StAXOMBuilder(parser);

    OMElement policyElement = builder.getDocumentElement();
    return PolicyEngine.getPolicy(policyElement);

}
 
Example 5
Source File: RegistryDataManager.java    From product-ei with Apache License 2.0 4 votes vote down vote up
/**
 * Encrypt the security policy password by new algorithm and update
 *
 * @param tenantId
 * @throws RegistryException
 * @throws CryptoException
 * @throws XMLStreamException
 */
private void updateSecurityPolicyPassword(int tenantId) throws RegistryException, CryptoException,
        XMLStreamException {
    InputStream resourceContent = null;
    XMLStreamReader parser = null;
    try {
        Registry registry = MigrationServiceDataHolder.getRegistryService().getConfigSystemRegistry(tenantId);
        List<String> policyPaths = getSTSPolicyPaths(registry);
        String newEncryptedPassword = null;
        for (String resourcePath : policyPaths) {
            if (registry.resourceExists(resourcePath)) {
                Resource resource = registry.get(resourcePath);
                resourceContent = resource.getContentStream();
                parser = XMLInputFactory.newInstance().createXMLStreamReader(resourceContent);
                StAXOMBuilder builder = new StAXOMBuilder(parser);
                OMElement documentElement = builder.getDocumentElement();
                Iterator it = documentElement.getChildrenWithName(new QName(Constant.CARBON_SEC_CONFIG));

                while (it != null && it.hasNext()) {
                    OMElement secConfig = (OMElement) it.next();
                    Iterator kerberosProperties = secConfig.getChildrenWithName(new QName(Constant.KERBEROS));
                    Iterator propertySet = null;
                    if ((kerberosProperties != null && kerberosProperties.hasNext())) {
                        propertySet = ((OMElement) kerberosProperties.next()).getChildElements();
                    }
                    if (propertySet != null) {
                        while (propertySet.hasNext()) {
                            OMElement kbProperty = (OMElement) propertySet.next();
                            if (Constant.SERVICE_PRINCIPAL_PASSWORD
                                    .equals(kbProperty.getAttributeValue(Constant.NAME_Q))) {
                                String encryptedPassword = kbProperty.getText();
                                newEncryptedPassword = Utility.getNewEncryptedValue(encryptedPassword);
                                if (StringUtils.isNotEmpty(newEncryptedPassword)) {
                                    kbProperty.setText(newEncryptedPassword);
                                }
                            }
                        }
                    }
                }
                if (StringUtils.isNotEmpty(newEncryptedPassword)) {
                    resource.setContent(RegistryUtils.encodeString(documentElement.toString()));
                    registry.beginTransaction();
                    registry.put(resourcePath, resource);
                    registry.commitTransaction();
                }
            }
        }
    } finally {
        try {
            if (parser != null) {
                parser.close();
            }
            if (resourceContent != null) {
                try {
                    resourceContent.close();
                } catch (IOException e) {
                    log.error("Error occurred while closing Input stream", e);
                }
            }
        } catch (XMLStreamException ex) {
            log.error("Error while closing XML stream", ex);
        }
    }

}
 
Example 6
Source File: SecurityDeploymentInterceptor.java    From carbon-identity with Apache License 2.0 3 votes vote down vote up
private Policy loadPolicy(Resource resource) throws org.wso2.carbon.registry.api.RegistryException, XMLStreamException {

        InputStream in = resource.getContentStream();
        XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(in);
        StAXOMBuilder builder = new StAXOMBuilder(parser);

        OMElement policyElement = builder.getDocumentElement();
        return PolicyEngine.getPolicy(policyElement);

    }