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();
}
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;
}
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;
}
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);
}
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));
}
Aggregations