Example usage for javax.xml.stream XMLInputFactory newInstance

List of usage examples for javax.xml.stream XMLInputFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.stream XMLInputFactory newInstance.

Prototype

public static XMLInputFactory newInstance() throws FactoryConfigurationError 

Source Link

Document

Creates a new instance of the factory in exactly the same manner as the #newFactory() method.

Usage

From source file:org.wso2.carbon.device.mgt.mobile.android.impl.AndroidDeviceManager.java

@Override
public PlatformConfiguration getConfiguration() throws DeviceManagementException {
    Resource resource;/*from   www  .  j  a  va 2s  . c  o m*/
    try {
        String androidRegPath = MobileDeviceManagementUtil
                .getPlatformConfigPath(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
        resource = MobileDeviceManagementUtil.getRegistryResource(androidRegPath);
        if (resource != null) {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
            factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
            XMLStreamReader reader = factory
                    .createXMLStreamReader(new StringReader(new String((byte[]) resource.getContent(),
                            Charset.forName(AndroidPluginConstants.MobilePluginConstants.CHARSET_UTF8))));
            JAXBContext context = JAXBContext.newInstance(PlatformConfiguration.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            return (PlatformConfiguration) unmarshaller.unmarshal(reader);
        }
        return null;
    } catch (AndroidDeviceMgtPluginException e) {
        throw new DeviceManagementException(
                "Error occurred while retrieving the Registry instance : " + e.getMessage(), e);
    } catch (JAXBException | XMLStreamException e) {
        throw new DeviceManagementException(
                "Error occurred while parsing the Android configuration : " + e.getMessage(), e);
    } catch (RegistryException e) {
        throw new DeviceManagementException(
                "Error occurred while retrieving the Registry resource of Android Configuration : "
                        + e.getMessage(),
                e);
    }
}

From source file:org.wso2.carbon.dssapi.observer.APIObserver.java

@Override
public void serviceUpdate(AxisEvent axisEvent, AxisService axisService) {

    if (axisEvent.getEventType() == AxisEvent.SERVICE_DEPLOY) {
        DataService dataService = (DataService) axisService.getParameter(DBConstants.DATA_SERVICE_OBJECT)
                .getValue();/* www  . j a va2 s  .c  o m*/
        if (dataService != null) {
            String location = dataService.getDsLocation();
            location = location.substring(0, location.lastIndexOf("/"));
            File file = new File(location + "/" + dataService.getName() + APIUtil.APPLICATION_XML);
            if (file.exists()) {
                Application application;
                try {

                    JAXBContext jaxbContext = JAXBContext.newInstance(Application.class);
                    Unmarshaller jaxbUnMarshaller = jaxbContext.createUnmarshaller();
                    jaxbUnMarshaller.setProperty("jaxb.encoding", "UTF-8");
                    application = (Application) jaxbUnMarshaller.unmarshal(file);
                    String serviceContents = new DataServiceAdmin()
                            .getDataServiceContentAsString(dataService.getName());
                    XMLStreamReader reader = XMLInputFactory.newInstance()
                            .createXMLStreamReader(new StringReader(serviceContents));
                    OMElement configElement = (new StAXOMBuilder(reader)).getDocumentElement();
                    configElement.build();
                    Data data = new Data();
                    data.populate(configElement);
                    String tempDeployedTime = new ServiceAdmin(
                            DataHolder.getConfigurationContext().getAxisConfiguration())
                                    .getServiceData(data.getName()).getServiceDeployedTime();
                    if (!application.getDeployedTime().equalsIgnoreCase(tempDeployedTime)) {
                        APIUtil.updateApi(dataService.getName(), application.getUserName(), data,
                                application.getVersion());
                        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
                        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                        jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
                        application.setDeployedTime(tempDeployedTime);
                        jaxbMarshaller.marshal(application, file);
                    }
                } catch (JAXBException e) {
                    log.error("An error occurred while reading the data for " + dataService.getName(), e);
                } catch (XMLStreamException e) {
                    log.error("An error occurred while reading xml data for " + dataService.getName(), e);
                } catch (AxisFault axisFault) {
                    log.error("An error occurred while reading the service " + dataService.getName(),
                            axisFault);
                } catch (Exception e) {
                    log.error("Couldn't get service meta data for the service " + dataService.getName(), e);
                }
                //Logged observed errors and let the process to continue
            }

        }
    }

}

From source file:org.wso2.carbon.dssapi.util.APIUtil.java

/**
 * This method is used to populate data of the dss service
 *
 * @param serviceId data service Name/* ww w . j a va2 s. c  o  m*/
 * @return data object of data service
 * @throws AxisFault
 * @throws XMLStreamException
 */
public static Data populateDataServiceData(String serviceId) throws AxisFault, XMLStreamException {
    String serviceContents = new DataServiceAdmin().getDataServiceContentAsString(serviceId);
    XMLStreamReader reader = XMLInputFactory.newInstance()
            .createXMLStreamReader(new StringReader(serviceContents));
    OMElement configElement = (new StAXOMBuilder(reader)).getDocumentElement();
    configElement.build();
    Data data = new Data();
    data.populate(configElement);
    return data;
}

From source file:org.wso2.carbon.ei.migration.service.dao.ServerProfileDAO.java

/**
 * Transform the password by new encryption algorithm
 *
 * @param filePath// ww  w. ja v  a2s  .  com
 * @throws MigrationClientException
 */
public void transformSPPassword(String filePath) throws MigrationClientException {
    XMLStreamReader parser = null;
    FileInputStream stream = null;
    try {
        log.info("Migrating password in: " + filePath);
        stream = new FileInputStream(filePath);
        parser = XMLInputFactory.newInstance().createXMLStreamReader(stream);
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        OMElement documentElement = builder.getDocumentElement();
        Iterator it = documentElement.getChildElements();
        String newEncryptedPassword = null;
        while (it.hasNext()) {
            OMElement element = (OMElement) it.next();
            if (Boolean.parseBoolean(element.getAttributeValue(Constant.SECURE_PASSWORD_Q))) {
                String password = element.getAttributeValue(Constant.PASSWORD_Q);
                newEncryptedPassword = Utility.getNewEncryptedValue(password);
                if (StringUtils.isNotEmpty(newEncryptedPassword)) {
                    element.getAttribute(Constant.PASSWORD_Q).setAttributeValue(newEncryptedPassword);
                }
            }
        }
        if (newEncryptedPassword != null) {
            OutputStream outputStream = new FileOutputStream(new File(filePath));
            documentElement.serialize(outputStream);
            isModified = true;
        }
    } catch (XMLStreamException | FileNotFoundException e) {
        new MigrationClientException("Error while writing the file: " + e);
    } catch (CryptoException e) {
        new MigrationClientException("Error while encrypting the password: " + e);
    } finally {
        try {
            if (parser != null) {
                parser.close();
            }
            if (stream != null) {
                try {
                    if (stream != null) {
                        stream.close();
                    }
                } catch (IOException e) {
                    log.error("Error occurred while closing Input stream", e);
                }
            }
        } catch (XMLStreamException ex) {
            log.error("Error while closing XML stream", ex);
        }
    }
}

From source file:org.wso2.carbon.ei.migration.service.migrator.EntitlementMediatorMigrator.java

/**
 * Migrate the password in entitlement mediators
 *
 * @param filePath/* w w  w . j av a 2  s .  c  om*/
 * @throws MigrationClientException
 */
private void transformEMPassword(String filePath) throws MigrationClientException {
    isModified = false;
    XMLStreamReader parser = null;
    FileInputStream stream = null;
    try {
        log.info("Migrating password in: " + filePath);
        stream = new FileInputStream(filePath);
        parser = XMLInputFactory.newInstance().createXMLStreamReader(stream);
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        OMElement documentElement = builder.getDocumentElement();
        loopAndEncrypt(documentElement.getChildElements());
        if (isModified) {
            OutputStream outputStream = new FileOutputStream(filePath);
            documentElement.serialize(outputStream);
        }
    } catch (XMLStreamException | FileNotFoundException e) {
        throw new MigrationClientException("Error while writing the file: " + e);
    } finally {
        try {
            if (parser != null) {
                parser.close();
            }
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    log.error("Error occurred while closing Input stream", e);
                }
            }
        } catch (XMLStreamException ex) {
            log.error("Error while closing XML stream", ex);
        }
    }
}

From source file:org.wso2.carbon.ei.migration.service.migrator.InputOutputDataMigration.java

/**
 * Migrate password in event publisher and receiver which is found in the provided path
 *
 * @param folder//from   ww  w .  j  a  v a2 s.c o  m
 * @throws MigrationClientException
 */
private static void migrateData(File folder) throws MigrationClientException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    XMLStreamReader parser;
    FileInputStream stream;
    try {
        File[] configs = folder.listFiles();
        if (configs != null) {
            for (File fileEntry : configs) {
                if (fileEntry.isFile() && fileEntry.getName().toLowerCase().endsWith(".xml")) {
                    stream = new FileInputStream(fileEntry);
                    parser = XMLInputFactory.newInstance().createXMLStreamReader(stream);
                    StAXOMBuilder builder = new StAXOMBuilder(parser);
                    OMElement documentElement = builder.getDocumentElement();
                    Iterator it = ((OMElement) documentElement.getChildrenWithName(Constant.TO_Q).next())
                            .getChildElements();
                    String newEncryptedPassword = null;
                    while (it.hasNext()) {
                        OMElement element = (OMElement) it.next();
                        if (Boolean.parseBoolean(element.getAttributeValue(Constant.ENCRYPTED_Q))) {
                            String password = element.getText();
                            newEncryptedPassword = Utility.getNewEncryptedValue(password);
                            if (StringUtils.isNotEmpty(newEncryptedPassword)) {
                                element.setText(newEncryptedPassword);
                            }
                        }
                    }
                    if (newEncryptedPassword != null) {
                        OutputStream outputStream = new FileOutputStream(
                                new File(fileEntry.getAbsolutePath()).getPath());
                        documentElement.serialize(outputStream);
                    }
                }
            }
        }
    } catch (IOException | CryptoException | XMLStreamException e) {
        throw new MigrationClientException(e.getMessage());
    }
}

From source file:org.wso2.carbon.ei.migration.service.migrator.UserStorePasswordMigrator.java

/**
 * Encrypt by new algorithm and update the password in the provided file
 *
 * @param filePath/*from w  w  w  .  ja  v a 2s  .co  m*/
 * @throws FileNotFoundException
 * @throws CryptoException
 */
private void updatePassword(String filePath) throws FileNotFoundException, CryptoException {
    XMLStreamReader parser = null;
    FileInputStream stream = null;
    try {
        log.info("Migrating password in: " + filePath);
        stream = new FileInputStream(filePath);
        parser = XMLInputFactory.newInstance().createXMLStreamReader(stream);
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        OMElement documentElement = builder.getDocumentElement();
        Iterator it = documentElement.getChildElements();
        String newEncryptedPassword = null;
        while (it.hasNext()) {
            OMElement element = (OMElement) it.next();
            if (Constant.PASSWORD.equals(element.getAttributeValue(Constant.NAME_Q))
                    || Constant.CONNECTION_PASSWORD.equals(element.getAttributeValue(Constant.NAME_Q))) {
                String encryptedPassword = element.getText();
                newEncryptedPassword = Utility.getNewEncryptedValue(encryptedPassword);
                if (StringUtils.isNotEmpty(newEncryptedPassword)) {
                    element.setText(newEncryptedPassword);
                }
            }
        }
        if (newEncryptedPassword != null) {
            OutputStream outputStream = new FileOutputStream(filePath);
            documentElement.serialize(outputStream);
        }
    } catch (XMLStreamException ex) {
        log.error("Error while updating password for: " + filePath);
    } finally {
        try {
            if (parser != null) {
                parser.close();
            }
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    log.error("Error occurred while closing Input stream", e);
                }
            }
        } catch (XMLStreamException ex) {
            log.error("Error while closing XML stream", ex);
        }
    }
}

From source file:org.wso2.carbon.ei.migration.service.RegistryDataManager.java

/**
 * Encrypt the security policy password by new algorithm and update
 *
 * @param tenantId//from w  w w  .j  a v  a 2 s.  com
 * @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);
        }
    }

}

From source file:org.wso2.carbon.ei.migration.util.Utility.java

public static OMElement toOM(InputStream inputStream) throws XMLStreamException {
    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
    StAXOMBuilder builder = new StAXOMBuilder(reader);
    return builder.getDocumentElement();
}

From source file:org.wso2.carbon.email.mgt.config.ConfigBuilder.java

private Config loadEmailConfigFile() {
    String confXml = CarbonUtils.getCarbonConfigDirPath() + File.separator
            + I18nMgtConstants.EMAIL_CONF_DIRECTORY + File.separator + I18nMgtConstants.EMAIL_ADMIN_CONF_FILE;

    Config emailConfig = new EmailNotificationConfig();

    File configfile = new File(confXml);
    if (!configfile.exists()) {
        log.warn("Email Configuration File is not present at: " + confXml);
    }/*from w ww  . j  a  v  a 2s.co m*/

    XMLStreamReader parser = null;
    InputStream stream = null;

    try {
        stream = new FileInputStream(configfile);
        parser = XMLInputFactory.newInstance().createXMLStreamReader(stream);
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        OMElement documentElement = builder.getDocumentElement();
        Iterator iterator = documentElement.getChildElements();
        while (iterator.hasNext()) {
            OMElement omElement = (OMElement) iterator.next();
            String configType1 = omElement.getAttributeValue(new QName("type"));
            String configType2 = omElement.getAttributeValue(new QName("display"));
            String configType3 = omElement.getAttributeValue(new QName("locale"));
            String configType4 = omElement.getAttributeValue(new QName("emailContentType"));
            String configFinal = configType1 + "|" + configType2 + "|" + configType3 + "|" + configType4;
            if (StringUtils.isNotBlank(configFinal)) {
                emailConfig.setProperty(configFinal, loadEmailConfig(omElement));
            }

        }
    } catch (XMLStreamException e) {
        log.warn("Error while loading email config. using default configuration", e);
    } catch (FileNotFoundException e) {
        log.warn("Error while loading email config. using default configuration", e);
    } finally {
        try {
            if (parser != null) {
                parser.close();
            }
            if (stream != null) {
                stream.close();
            }
        } catch (XMLStreamException e) {
            log.error("Error while closing XML stream", e);
        } catch (IOException e) {
            log.error("Error while closing input stream", e);
        }
    }
    return emailConfig;

}