Search in sources :

Example 1 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project elasticsearch by elastic.

the class Ec2DiscoveryClusterFormationTests method startHttpd.

/**
     * Creates mock EC2 endpoint providing the list of started nodes to the DescribeInstances API call
     */
@BeforeClass
public static void startHttpd() throws Exception {
    logDir = createTempDir();
    httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
    httpServer.createContext("/", (s) -> {
        Headers headers = s.getResponseHeaders();
        headers.add("Content-Type", "text/xml; charset=UTF-8");
        String action = null;
        for (NameValuePair parse : URLEncodedUtils.parse(IOUtils.toString(s.getRequestBody()), StandardCharsets.UTF_8)) {
            if ("Action".equals(parse.getName())) {
                action = parse.getValue();
                break;
            }
        }
        assertThat(action, equalTo("DescribeInstances"));
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
        xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
        StringWriter out = new StringWriter();
        XMLStreamWriter sw;
        try {
            sw = xmlOutputFactory.createXMLStreamWriter(out);
            sw.writeStartDocument();
            String namespace = "http://ec2.amazonaws.com/doc/2013-02-01/";
            sw.setDefaultNamespace(namespace);
            sw.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, "DescribeInstancesResponse", namespace);
            {
                sw.writeStartElement("requestId");
                sw.writeCharacters(UUID.randomUUID().toString());
                sw.writeEndElement();
                sw.writeStartElement("reservationSet");
                {
                    Path[] files = FileSystemUtils.files(logDir);
                    for (int i = 0; i < files.length; i++) {
                        Path resolve = files[i].resolve("transport.ports");
                        if (Files.exists(resolve)) {
                            List<String> addresses = Files.readAllLines(resolve);
                            Collections.shuffle(addresses, random());
                            sw.writeStartElement("item");
                            {
                                sw.writeStartElement("reservationId");
                                sw.writeCharacters(UUID.randomUUID().toString());
                                sw.writeEndElement();
                                sw.writeStartElement("instancesSet");
                                {
                                    sw.writeStartElement("item");
                                    {
                                        sw.writeStartElement("instanceId");
                                        sw.writeCharacters(UUID.randomUUID().toString());
                                        sw.writeEndElement();
                                        sw.writeStartElement("imageId");
                                        sw.writeCharacters(UUID.randomUUID().toString());
                                        sw.writeEndElement();
                                        sw.writeStartElement("instanceState");
                                        {
                                            sw.writeStartElement("code");
                                            sw.writeCharacters("16");
                                            sw.writeEndElement();
                                            sw.writeStartElement("name");
                                            sw.writeCharacters("running");
                                            sw.writeEndElement();
                                        }
                                        sw.writeEndElement();
                                        sw.writeStartElement("privateDnsName");
                                        sw.writeCharacters(addresses.get(0));
                                        sw.writeEndElement();
                                        sw.writeStartElement("dnsName");
                                        sw.writeCharacters(addresses.get(0));
                                        sw.writeEndElement();
                                        sw.writeStartElement("instanceType");
                                        sw.writeCharacters("m1.medium");
                                        sw.writeEndElement();
                                        sw.writeStartElement("placement");
                                        {
                                            sw.writeStartElement("availabilityZone");
                                            sw.writeCharacters("use-east-1e");
                                            sw.writeEndElement();
                                            sw.writeEmptyElement("groupName");
                                            sw.writeStartElement("tenancy");
                                            sw.writeCharacters("default");
                                            sw.writeEndElement();
                                        }
                                        sw.writeEndElement();
                                        sw.writeStartElement("privateIpAddress");
                                        sw.writeCharacters(addresses.get(0));
                                        sw.writeEndElement();
                                        sw.writeStartElement("ipAddress");
                                        sw.writeCharacters(addresses.get(0));
                                        sw.writeEndElement();
                                    }
                                    sw.writeEndElement();
                                }
                                sw.writeEndElement();
                            }
                            sw.writeEndElement();
                        }
                    }
                }
                sw.writeEndElement();
            }
            sw.writeEndElement();
            sw.writeEndDocument();
            sw.flush();
            final byte[] responseAsBytes = out.toString().getBytes(StandardCharsets.UTF_8);
            s.sendResponseHeaders(200, responseAsBytes.length);
            OutputStream responseBody = s.getResponseBody();
            responseBody.write(responseAsBytes);
            responseBody.close();
        } catch (XMLStreamException e) {
            Loggers.getLogger(Ec2DiscoveryClusterFormationTests.class).error("Failed serializing XML", e);
            throw new RuntimeException(e);
        }
    });
    httpServer.start();
}
Also used : Path(java.nio.file.Path) NameValuePair(org.apache.http.NameValuePair) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) InetSocketAddress(java.net.InetSocketAddress) Headers(com.sun.net.httpserver.Headers) OutputStream(java.io.OutputStream) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) BeforeClass(org.junit.BeforeClass)

Example 2 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project OpenAttestation by OpenAttestation.

the class ReportsBO method getHostAttestationReport.

// BUG #497 XXX TODO needs rewrite to use HostAgentFactory and HostAgent interfaces
public String getHostAttestationReport(Hostname hostName) {
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLStreamWriter xtw;
    StringWriter sw = new StringWriter();
    IManifestStrategy manifestStrategy;
    IManifestStrategyFactory strategyFactory;
    HashMap<String, ? extends IManifest> pcrManifestMap = null;
    TblHosts tblHosts = null;
    String attestationReport = "";
    try {
        tblHosts = getTblHostsJpaController().findByName(hostName.toString());
        if (tblHosts == null) {
            throw new ASException(ErrorCode.AS_HOST_NOT_FOUND, hostName.toString());
        }
        manifestStrategy = getManifestStrategy(tblHosts);
        // BUG #497  this is now obtained by IntelHostAgent using TAHelper's getQuoteInformationForHost which is what was called by TrustAgentManifestStrategy.getManifest()
        pcrManifestMap = manifestStrategy.getManifest(tblHosts);
    } catch (ASException aex) {
        throw aex;
    } catch (CryptographyException e) {
        throw new ASException(e, ErrorCode.AS_ENCRYPTION_ERROR, e.getCause() == null ? e.getMessage() : e.getCause().getMessage());
    } catch (Exception ex) {
        throw new ASException(ex);
    }
    try {
        // XXX BUG #497 this entire section in try{}catch{} has  moved to TAHelper and used by IntelHostAgent
        // We need to check if the host supports TPM or not. Only way we can do it
        // using the host table contents is by looking at the AIK Certificate. Based
        // on this flag we generate the attestation report.
        boolean tpmSupport = true;
        String hostType = tblHosts.getVmmMleId().getName();
        if (tblHosts.getAIKCertificate() == null || tblHosts.getAIKCertificate().isEmpty()) {
            tpmSupport = false;
        }
        // xtw = xof.createXMLStreamWriter(new FileWriter("c:\\temp\\nb_xml.xml"));
        xtw = xof.createXMLStreamWriter(sw);
        xtw.writeStartDocument();
        xtw.writeStartElement("Host_Attestation_Report");
        xtw.writeAttribute("Host_Name", hostName.toString());
        xtw.writeAttribute("Host_VMM", hostType);
        xtw.writeAttribute("TXT_Support", String.valueOf(tpmSupport));
        if (tpmSupport == true) {
            ArrayList<IManifest> pcrMFList = new ArrayList<IManifest>();
            pcrMFList.addAll(pcrManifestMap.values());
            for (IManifest pcrInfo : pcrMFList) {
                PcrManifest pInfo = (PcrManifest) pcrInfo;
                xtw.writeStartElement("PCRInfo");
                xtw.writeAttribute("ComponentName", String.valueOf(pInfo.getPcrNumber()));
                xtw.writeAttribute("DigestValue", pInfo.getPcrValue().toUpperCase());
                xtw.writeEndElement();
            }
        } else {
            xtw.writeStartElement("PCRInfo");
            xtw.writeAttribute("Error", "Host does not support TPM.");
            xtw.writeEndElement();
        }
        xtw.writeEndElement();
        xtw.writeEndDocument();
        xtw.flush();
        xtw.close();
        attestationReport = sw.toString();
    } catch (Exception ex) {
        throw new ASException(ex);
    }
    return attestationReport;
}
Also used : IManifestStrategy(com.intel.mountwilson.manifest.IManifestStrategy) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) IManifestStrategyFactory(com.intel.mountwilson.manifest.IManifestStrategyFactory) ASException(com.intel.mountwilson.as.common.ASException) CryptographyException(com.intel.mtwilson.crypto.CryptographyException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) CryptographyException(com.intel.mtwilson.crypto.CryptographyException) PcrManifest(com.intel.mountwilson.manifest.data.PcrManifest) TblPcrManifest(com.intel.mtwilson.as.data.TblPcrManifest) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) TblHosts(com.intel.mtwilson.as.data.TblHosts) ASException(com.intel.mountwilson.as.common.ASException) IManifest(com.intel.mountwilson.manifest.data.IManifest)

Example 3 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project OpenAttestation by OpenAttestation.

the class CitrixHostAgent method getHostAttestationReport.

public String getHostAttestationReport(String pcrList) throws IOException {
    String attestationReport = "";
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLStreamWriter xtw;
    StringWriter sw = new StringWriter();
    try {
        xtw = xof.createXMLStreamWriter(sw);
        xtw.writeStartDocument();
        xtw.writeStartElement("Host_Attestation_Report");
        xtw.writeAttribute("Host_Name", this.client.hostIpAddress);
        xtw.writeAttribute("vCenterVersion", "5.0");
        xtw.writeAttribute("HostVersion", "5.0");
        //xtw.writeAttribute("TXT_Support", tpmSupport.toString());
        HashMap<String, PcrManifest> pcrMap = client.getQuoteInformationForHost(pcrList);
        Iterator it = pcrMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            xtw.writeStartElement("PCRInfo");
            PcrManifest pcr = (PcrManifest) pairs.getValue();
            xtw.writeAttribute("ComponentName", Integer.toString(pcr.getPcrNumber()));
            xtw.writeAttribute("DigestValue", pcr.getPcrValue());
            xtw.writeEndElement();
            // avoids a ConcurrentModificationException
            it.remove();
        }
        xtw.writeEndElement();
        xtw.writeEndDocument();
        xtw.flush();
        xtw.close();
        attestationReport = sw.toString();
    } catch (XMLStreamException ex) {
        //            Logger.getLogger(CitrixHostAgent.class.getName()).log(Level.SEVERE, null, ex);
        log.error("Cannot get host attestation report", ex);
    }
    log.debug("getHostAttestationReport report:" + attestationReport);
    return attestationReport;
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException) PcrManifest(com.intel.mountwilson.manifest.data.PcrManifest) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project spring-framework by spring-projects.

the class StaxStreamHandlerTests method createStaxHandler.

@Override
protected AbstractStaxHandler createStaxHandler(Result result) throws XMLStreamException {
    XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
    XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(result);
    return new StaxStreamHandler(streamWriter);
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 5 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project spring-framework by spring-projects.

the class StaxUtilsTests method isStaxResult.

@Test
public void isStaxResult() throws Exception {
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
    Result result = StaxUtils.createCustomStaxResult(streamWriter);
    assertTrue("Not a StAX Result", StaxUtils.isStaxResult(result));
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) StAXResult(javax.xml.transform.stax.StAXResult) DOMResult(javax.xml.transform.dom.DOMResult) Test(org.junit.Test)

Aggregations

XMLOutputFactory (javax.xml.stream.XMLOutputFactory)42 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)28 StringWriter (java.io.StringWriter)18 XMLStreamException (javax.xml.stream.XMLStreamException)15 XMLEventWriter (javax.xml.stream.XMLEventWriter)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Test (org.junit.Test)8 StAXResult (javax.xml.transform.stax.StAXResult)7 IOException (java.io.IOException)6 DOMResult (javax.xml.transform.dom.DOMResult)6 StreamResult (javax.xml.transform.stream.StreamResult)6 Result (javax.xml.transform.Result)5 PcrManifest (com.intel.mountwilson.manifest.data.PcrManifest)3 OutputStream (java.io.OutputStream)3 SAXResult (javax.xml.transform.sax.SAXResult)3 IManifest (com.intel.mountwilson.manifest.data.IManifest)2 Headers (com.sun.net.httpserver.Headers)2 OutputStreamWriter (java.io.OutputStreamWriter)2 InetSocketAddress (java.net.InetSocketAddress)2 Path (java.nio.file.Path)2