Example usage for javax.xml.stream XMLStreamWriter flush

List of usage examples for javax.xml.stream XMLStreamWriter flush

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter flush.

Prototype

public void flush() throws XMLStreamException;

Source Link

Document

Write any cached data to the underlying output mechanism.

Usage

From source file:org.wso2.carbon.core.persistence.PersistenceUtils.java

/**
 * Creates a registry Resource for a given Policy
 *
 * @param policy - Policy instance/*from   w w w  .j  av  a 2 s. c om*/
 *               todo now policyId, and policyType is removed, adjust invocations of this method (PersistenceUtils.createPolicyElement)  - kasung
 * @return - created policy resource
 * @throws Exception - error on serialization
 */
public static OMElement createPolicyElement(Policy policy) throws Exception {
    // String policyId, int policyType

    // Set the policy as a string in the resource
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream);
    policy.serialize(writer);
    writer.flush();

    OMElement policyElement = AXIOMUtil.stringToOM(outputStream.toString());

    /**
     * @see org.wso2.carbon.core.persistence.file.ModuleFilePersistenceManager#getAll(String, String) for the argument
     */
    if (policyElement.getParent() instanceof OMDocument) {
        policyElement.detach();
    }
    return policyElement;
}

From source file:org.wso2.carbon.core.persistence.PersistenceUtils.java

/**
 * Creates a registry Resource for a given Policy
 *
 * @param configRegistry config registry
 * @param policy         - Policy instance
 * @param policyId       - policy uuid//from   w w w .  j  ava  2s.  c  om
 * @param policyType     - policy type
 * @return - created policy resource
 * @throws Exception - error on serialization
 */
public static Resource createPolicyResource(Registry configRegistry, Policy policy, String policyId,
        String policyType) throws RegistryException {
    try {
        Resource policyResource = configRegistry.newResource();
        policyResource.setProperty(RegistryResources.ServiceProperties.POLICY_UUID, policyId);

        // Set the policy as a string in the resource
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream);
        policy.serialize(writer);
        writer.flush();
        policyResource.setContent(outputStream.toString());

        policyResource.setProperty(RegistryResources.ServiceProperties.POLICY_TYPE, policyType);
        policyResource.setMediaType("application/policy+xml");
        return policyResource;
    } catch (XMLStreamException e) {
        log.error("Error creating the registry resource for " + policyId, e);
        throw new RegistryException("Error creating the registry resource for " + policyId, e);
    }
}

From source file:org.wso2.carbon.core.transports.util.AtomProcessor.java

public void process(HttpServletRequest request, HttpServletResponse response,
        ConfigurationContext configurationContext) throws Exception {
    try {/*from   ww w  .  jav a2  s .  co  m*/
        response.setContentType("text/xml; charset=utf-8");
        AtomFeed atomFeed = FeedFactory.getAtomFeed(FeedConstants.WSO2WSAS_ATOM_FEED,
                configurationContext.getAxisConfiguration());
        if (atomFeed != null) {
            XMLStreamWriter writer = XMLOutputFactory.newInstance()
                    .createXMLStreamWriter(response.getOutputStream());
            writer.writeProcessingInstruction("xml-stylesheet",
                    "  type=\"text/xsl\" href=\"" + (configurationContext.getContextRoot().equals("/") ? ""
                            : configurationContext.getContextRoot()) + "/styles/atom.xsl\"");
            OMElement feedElement = atomFeed.getFeedElement(configurationContext.getServiceContextPath());
            feedElement.serialize(writer);
            writer.flush();
        }
    } catch (Exception e) {
        log.error("Could not process ATOM feed", e);
    }
}

From source file:org.wso2.carbon.core.transports.util.RequestProcessorUtil.java

/**
 * @param byteArrayOutStream/*from ww  w  . j  a  va2s. com*/
 * @param out
 * @param annotatedXsl
 * @param contextRoot
 * @param annotation         : If annotation is false PI would not be attached.
 */
public static void writeDocument(ByteArrayOutputStream byteArrayOutStream, OutputStream out,
        String annotatedXsl, String contextRoot, boolean annotation) {
    XMLStreamWriter writer;
    ByteArrayInputStream bais = null;
    XMLStreamReader reader = null;
    try {
        bais = new ByteArrayInputStream(byteArrayOutStream.toByteArray());
        reader = XMLInputFactory.newInstance().createXMLStreamReader(bais);
        StAXOMBuilder builder = new StAXOMBuilder(reader);
        OMElement docElem = builder.getDocumentElement();
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
        if (annotatedXsl != null && annotation) {
            writer.writeProcessingInstruction("xml-stylesheet", "  type=\"text/xsl\" href=\""
                    + (contextRoot.equals("/") ? "" : contextRoot) + "/styles/" + annotatedXsl + "\"");
        }
        docElem.serialize(writer);
        writer.flush();
    } catch (XMLStreamException e) {
        log.error("Error occurred while trying to write processing instruction for attaching "
                + "annotated style sheet", e);
    } finally {
        try {
            if (bais != null) {
                bais.close();
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (XMLStreamException e) {
            log.error(e.getMessage(), e);
        }

    }
}

From source file:org.wso2.carbon.core.transports.util.RssProcessor.java

public void process(HttpServletRequest request, HttpServletResponse response,
        ConfigurationContext configurationContext) throws Exception {
    try {/*ww w. ja v a  2s . c  o  m*/
        response.setContentType("text/xml; charset=utf-8");
        RSSFeed rssFeed = FeedFactory.getRSSFeed(FeedConstants.WSO2WSAS_RSS_FEED,
                configurationContext.getAxisConfiguration());
        if (rssFeed != null) {
            XMLStreamWriter writer = XMLOutputFactory.newInstance()
                    .createXMLStreamWriter(response.getOutputStream());
            writer.writeProcessingInstruction("xml-stylesheet",
                    "  type=\"text/xsl\" href=\"" + (configurationContext.getContextRoot().equals("/") ? ""
                            : configurationContext.getContextRoot()) + "/styles/rss.xsl\"");
            OMElement feedElement = rssFeed.getFeedElement(configurationContext.getServiceContextPath());
            feedElement.serialize(writer);
            writer.flush();
        }
    } catch (Exception e) {
        log.error("Could not process RSS feed", e);
    }
}

From source file:org.wso2.carbon.dataservices.core.engine.DSOMDataSource.java

public void execute(XMLStreamWriter xmlWriter) throws XMLStreamException {
    try {//from  w w  w  .j a va 2 s.c o  m
        this.getDataService().invoke(xmlWriter, this.getOpName(), this.getParams());
        /* flush the stream, if there's a result */
        if (xmlWriter != null) {
            xmlWriter.flush();
        }
    } catch (DataServiceFault e) {
        throw new XMLStreamException(e.getMessage(), e);
    }
}

From source file:org.wso2.carbon.dss.samples.test.EventingSampleTestCase.java

private void updateAxis2_ClientXML() throws Exception {
    String axis2_client_path = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "axis2" + File.separator + "axis2_client.xml";

    String mail_transport_config = ProductConstant.SYSTEM_TEST_RESOURCE_LOCATION + "artifacts" + File.separator
            + "DSS" + File.separator + "resources" + File.separator + "mailTransport.xml";

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;

    try {//from w w  w .j  av  a 2  s  .c om
        OMElement axis2_client_xml = AXIOMUtil.stringToOM(FileManager.readFile(axis2_client_path));

        axis2_client_xml.addChild(AXIOMUtil.stringToOM(FileManager.readFile(mail_transport_config)));
        axis2_client_xml.build();
        fileOutputStream = new FileOutputStream(axis2_client_path);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        axis2_client_xml.serialize(writer);

    } catch (Exception e) {
        throw new Exception("axis2_client.xml update fails");
    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (writer != null) {
            writer.flush();
        }
    }
}

From source file:org.wso2.carbon.greg.server.mgt.RegistryConfiguratorTestCase.java

private void increaseSearchIndexStartTimeDelay() throws Exception {
    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    OMElement documentElement = getRegistryXmlOmElement();
    try {// www .jav a 2s  . com
        AXIOMXPath xpathExpression = new AXIOMXPath(
                "/wso2registry/indexingConfiguration/startingDelayInSeconds");
        OMElement indexConfigNode = (OMElement) xpathExpression.selectSingleNode(documentElement);
        indexConfigNode.setText("60");

        AXIOMXPath xpathExpression1 = new AXIOMXPath(
                "/wso2registry/indexingConfiguration/indexingFrequencyInSeconds");
        OMElement indexConfigNode1 = (OMElement) xpathExpression1.selectSingleNode(documentElement);
        indexConfigNode1.setText("5");

        fileOutputStream = new FileOutputStream(getRegistryXMLPath());
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        documentElement.serialize(writer);
        documentElement.build();
        Thread.sleep(2000);

    } catch (Exception e) {
        log.error("registry.xml edit fails" + e.getMessage());
        throw new Exception("registry.xml edit fails" + e.getMessage());
    } finally {
        assert fileOutputStream != null;
        fileOutputStream.close();
        assert writer != null;
        writer.flush();
    }
}

From source file:org.wso2.carbon.greg.server.mgt.RegistryConfiguratorTestCase.java

private void enableJmxManagement() throws Exception {
    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    OMElement documentElement = getRegistryXmlOmElement();
    try {/*from w  w  w . j  av a2s  .  com*/
        AXIOMXPath xpathExpression = new AXIOMXPath("/wso2registry/jmx");
        OMElement indexConfigNode = (OMElement) xpathExpression.selectSingleNode(documentElement);
        OMAttribute omAttribute = indexConfigNode.getAttribute(new QName("enabled"));
        omAttribute.setAttributeValue("true");

        fileOutputStream = new FileOutputStream(getRegistryXMLPath());
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        documentElement.serialize(writer);
        documentElement.build();
        Thread.sleep(2000);

    } catch (Exception e) {
        log.error("registry.xml edit fails" + e.getMessage());
        throw new Exception("registry.xml edit fails" + e.getMessage());
    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (writer != null) {
            writer.flush();
        }
    }
}

From source file:org.wso2.carbon.greg.server.mgt.RegistryConfiguratorTestCase.java

private void enableWorkList() throws Exception {
    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;

    String workList;/*from   w ww  .jav  a  2  s . co  m*/

    workList = "<workList serverURL=\"local://services/\" remote=\"false\">\n" + "        <username>"
            + userNameWithoutDomain + "</username>\n" + "        <password>"
            + automationContext.getContextTenant().getTenantAdmin().getPassword() + "</password>\n"
            + "    </workList>";

    try {
        OMElement registryXML = getRegistryXmlOmElement();

        registryXML.addChild(AXIOMUtil.stringToOM(workList));
        registryXML.build();
        fileOutputStream = new FileOutputStream(getRegistryXMLPath());
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        registryXML.serialize(writer);

    } catch (Exception e) {
        throw new Exception("registry.xml update fails");
    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (writer != null) {
            writer.flush();
        }
    }

}