/** * Serialize the Auth. Request * * @param xmlObject * @return serialized auth. req */ protected String marshall(XMLObject xmlObject) throws SSOAgentException { try { System.setProperty( "javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory(); Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject); Element element = marshaller.marshall(xmlObject); ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream(); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); output.setByteStream(byteArrayOutputStrm); writer.write(element, output); return new String(byteArrayOutputStrm.toByteArray(), Charset.forName("UTF-8")); } catch (ClassNotFoundException e) { throw new SSOAgentException("Error in marshalling SAML2 Assertion", e); } catch (InstantiationException e) { throw new SSOAgentException("Error in marshalling SAML2 Assertion", e); } catch (MarshallingException e) { throw new SSOAgentException("Error in marshalling SAML2 Assertion", e); } catch (IllegalAccessException e) { throw new SSOAgentException("Error in marshalling SAML2 Assertion", e); } }
private WSNDeviceAppConfiguration createWsnDeviceAppConfiguration(final WsnDevice wsnDevice) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { Map<String, String> configuration = convert(wsnDevice.getConfiguration()); WSNDeviceAppConfiguration.Builder builder = WSNDeviceAppConfiguration.builder(wsnDevice.getUrn(), wsnDevice.getType()) .setNodeSerialInterface(wsnDevice.getSerialinterface()) .setMaximumMessageRate(wsnDevice.getMaximummessagerate()) .setNodeUSBChipID(wsnDevice.getUsbchipid()); if (wsnDevice.getDefaultChannelPipeline() != null) { String configurationFileName = wsnDevice.getDefaultChannelPipeline().getConfigurationFile(); Object configurationXml = wsnDevice.getDefaultChannelPipeline().getConfigurationXml(); File defaultChannelPipelineConfigurationFile; if (configurationFileName != null) { checkFileExists(configurationFileName); checkFileReadable(configurationFileName); defaultChannelPipelineConfigurationFile = new File(configurationFileName); } else if (configurationXml != null) { defaultChannelPipelineConfigurationFile = File.createTempFile("tr.iwsn-", ""); defaultChannelPipelineConfigurationFile.deleteOnExit(); Node firstChild = ((Node) configurationXml).getFirstChild(); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); writer.writeToURI(firstChild, defaultChannelPipelineConfigurationFile.toURI().toString()); } else { throw new RuntimeException( "The default channel pipeline configuration for node \"" + wsnDevice.getUrn() + "\" is missing either the configuration file or the xml."); } builder.setDefaultChannelPipelineConfigurationFile(defaultChannelPipelineConfigurationFile); } if (wsnDevice.getTimeouts() != null) { builder.setTimeoutCheckAliveMillis(wsnDevice.getTimeouts().getCheckalive()); builder.setTimeoutFlashMillis(wsnDevice.getTimeouts().getFlash()); builder.setTimeoutNodeApiMillis(wsnDevice.getTimeouts().getNodeapi()); builder.setTimeoutResetMillis(wsnDevice.getTimeouts().getReset()); } if (wsnDevice.getConfiguration() != null) { builder.setConfiguration(configuration); } return builder.build(); }
/** * Serializes the specified SAML 2.0 based XML content representation to its corresponding actual * XML syntax representation. * * @param xmlObject the SAML 2.0 based XML content object * @return a {@link String} representation of the actual XML representation of the SAML 2.0 based * XML content representation * @throws SSOException if an error occurs during the marshalling process */ public static String marshall(XMLObject xmlObject) throws SSOException { try { // Explicitly sets the special XML parser library to be used, in the global variables System.setProperty( "javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); MarshallerFactory marshallerFactory = Configuration.getMarshallerFactory(); Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject); Element element = marshaller.marshall(xmlObject); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS implementation = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = implementation.createLSSerializer(); LSOutput output = implementation.createLSOutput(); output.setByteStream(byteArrayOutputStream); writer.write(element, output); return new String(byteArrayOutputStream.toByteArray(), Charset.forName("UTF-8")); } catch (ClassNotFoundException | InstantiationException | MarshallingException | IllegalAccessException e) { throw new SSOException("Error in marshalling SAML2 Assertion", e); } }
@SuppressWarnings("unchecked") public static <T> T getDOMImplUncached() { try { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); return (T) registry.getDOMImplementation("LS 3.0"); } catch (Exception e) { throw new RuntimeException(e); } }
private void lazyInit() throws ClassNotFoundException, InstantiationException, IllegalAccessException { synchronized (MONITOR) { if (DOM_IMPL == null) { final DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance(); DOM_IMPL = (DOMImplementationLS) reg.getDOMImplementation("LS"); } } }
private LSInput getLSInput() throws Exception { DOMImplementationLS impl; DOMImplementation docImpl = builder.getDOMImplementation(); // Try to get the DOMImplementation from doc first before // defaulting to the sun implementation. if (docImpl != null && docImpl.hasFeature("LS", "3.0")) { impl = (DOMImplementationLS) docImpl.getFeature("LS", "3.0"); } else { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); if (impl == null) { System.setProperty( DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl"); registry = DOMImplementationRegistry.newInstance(); impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); } } return impl.createLSInput(); }
@Test public void testDOMImplementationRegistry() { try { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementation domImpl = registry.getDOMImplementation("XML"); Assert.assertTrue( domImpl != null, "Non null implementation is expected for getDOMImplementation('XML')"); } catch (Exception e) { e.printStackTrace(); Assert.fail("Exception occured: " + e.getMessage()); } }
@SuppressWarnings("unchecked") public static synchronized <T> T getDOMImpl() { if (IMPL == null) { try { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); IMPL = registry.getDOMImplementation("LS 3.0"); } catch (Exception e) { throw new RuntimeException(e); } } return (T) IMPL; }
/** * Creates instance of a Get Capabilities outputter. Reads the sosGetCapabilities.xml file as a * template for the response. */ public GetCapsOutputter() { document = parseTemplateXML(); exceptionFlag = false; getCaps = getObs = descSen = null; prepOperationsMetadata(); try { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); } catch (Exception ex) { System.out.println(ex.getMessage()); } }
public static void writeXML(Document doc, Writer stream) throws ESException { try { DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) reg.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); writer.getDomConfig().setParameter("format-pretty-print", true); LSOutput out = impl.createLSOutput(); out.setCharacterStream(stream); writer.write(doc, out); } catch (Exception e) { throw new ESException(e); } }
/** * Normalize and pretty-print XML so that it can be compared using string compare. The following * code does the following: - Removes comments - Makes sure attributes are ordered consistently - * Trims every element - Pretty print the document * * @param xml The XML to be normalized * @return The equivalent XML, but now normalized */ protected String normalizeXML(String xml) throws Exception { // Remove all white space adjoining tags ("trim all elements") xml = xml.replaceAll("\\s*<", "<"); xml = xml.replaceAll(">\\s*", ">"); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domLS = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSParser lsParser = domLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); LSInput input = domLS.createLSInput(); input.setStringData(xml); Document document = lsParser.parse(input); LSSerializer lsSerializer = domLS.createLSSerializer(); lsSerializer.getDomConfig().setParameter("comments", Boolean.FALSE); lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); return lsSerializer.writeToString(document); }
private String beautify(String unformattedXml) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(unformattedXml)); Document doc = db.parse(is); DOMImplementationRegistry domReg = DOMImplementationRegistry.newInstance(); DOMImplementationLS lsImpl = (DOMImplementationLS) domReg.getDOMImplementation("LS"); LSSerializer lsSerializer = lsImpl.createLSSerializer(); lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); LSOutput output = lsImpl.createLSOutput(); output.setEncoding("UTF-8"); StringWriter destination = new StringWriter(); output.setCharacterStream(destination); lsSerializer.write(doc, output); return destination.toString(); } catch (Exception e) { // format failed, return unformatted xml return unformattedXml; } }
/** * Serializing a SAML2 object into a String * * @param xmlObject object that needs to serialized. * @return serialized object * @throws Exception */ public static String marshall(XMLObject xmlObject) throws Exception { try { doBootstrap(); System.setProperty( "javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory(); Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject); Element element = marshaller.marshall(xmlObject); ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream(); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); output.setByteStream(byteArrayOutputStrm); writer.write(element, output); return byteArrayOutputStrm.toString(); } catch (Exception e) { throw new Exception("Error Serializing the SAML Response", e); } }
/** * Utility method for rendering an {@link Element} into a {@link Writer}. * * @param element {@link Element} to be rendered. * @param writer {@link Writer} instance. * @param omitXmlDeclaration whether to omit the XML declaration from output. * @throws Exception if an error occurs during serialization. */ public static void renderElement(Element element, Writer writer, boolean omitXmlDeclaration) throws Exception { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer lsSerializer = impl.createLSSerializer(); { DOMConfiguration domConfiguration = lsSerializer.getDomConfig(); if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) { domConfiguration.setParameter("format-pretty-print", Boolean.TRUE); } if (omitXmlDeclaration && domConfiguration.canSetParameter("xml-declaration", Boolean.FALSE)) { domConfiguration.setParameter("xml-declaration", Boolean.FALSE); } } LSOutput lsOutput = impl.createLSOutput(); lsOutput.setEncoding("UTF-8"); lsOutput.setCharacterStream(writer); lsSerializer.write(element, lsOutput); }
@Test public void writeAtomViaLowerlevelLibs() throws ParserConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final Document doc = builder.newDocument(); final Element entry = doc.createElement("entry"); entry.setAttribute("xmlns", "http://www.w3.org/2005/Atom"); entry.setAttribute("xmlns:m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"); entry.setAttribute("xmlns:d", "http://schemas.microsoft.com/ado/2007/08/dataservices"); entry.setAttribute("xmlns:gml", "http://www.opengis.net/gml"); entry.setAttribute("xmlns:georss", "http://www.georss.org/georss"); doc.appendChild(entry); final Element category = doc.createElement("category"); category.setAttribute("term", "Microsoft.Test.OData.Services.AstoriaDefaultService.Customer"); category.setAttribute("scheme", "http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"); entry.appendChild(category); final Element properties = doc.createElement("m:properties"); entry.appendChild(properties); final Element name = doc.createElement("d:Name"); name.setAttribute("m:type", "Edm.String"); name.appendChild(doc.createTextNode("A name")); properties.appendChild(name); final Element customerId = doc.createElement("d:CustomerId"); customerId.setAttribute("m:type", "Edm.Int32"); customerId.appendChild(doc.createTextNode("0")); properties.appendChild(customerId); final Element bci = doc.createElement("d:BackupContactInfo"); bci.setAttribute( "m:type", "Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails)"); properties.appendChild(bci); final Element topelement = doc.createElement("d:element"); topelement.setAttribute( "m:type", "Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails"); bci.appendChild(topelement); final Element altNames = doc.createElement("d:AlternativeNames"); altNames.setAttribute("m:type", "Collection(Edm.String)"); topelement.appendChild(altNames); final Element element1 = doc.createElement("d:element"); element1.setAttribute("m:type", "Edm.String"); element1.appendChild(doc.createTextNode("myname")); altNames.appendChild(element1); final Element emailBag = doc.createElement("d:EmailBag"); emailBag.setAttribute("m:type", "Collection(Edm.String)"); topelement.appendChild(emailBag); final Element element2 = doc.createElement("d:element"); element2.setAttribute("m:type", "Edm.String"); element2.appendChild(doc.createTextNode("*****@*****.**")); emailBag.appendChild(element2); final Element contactAlias = doc.createElement("d:ContactAlias"); contactAlias.setAttribute( "m:type", "Microsoft.Test.OData.Services.AstoriaDefaultService.Aliases"); topelement.appendChild(contactAlias); final Element altNames2 = doc.createElement("d:AlternativeNames"); altNames2.setAttribute("m:type", "Collection(Edm.String)"); contactAlias.appendChild(altNames2); final Element element3 = doc.createElement("d:element"); element3.setAttribute("m:type", "Edm.String"); element3.appendChild(doc.createTextNode("myAlternativeName")); altNames2.appendChild(element3); final StringWriter writer = new StringWriter(); final DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = (DOMImplementationLS) reg.getDOMImplementation("LS"); final LSSerializer serializer = impl.createLSSerializer(); final LSOutput lso = impl.createLSOutput(); lso.setCharacterStream(writer); serializer.write(doc, lso); assertFalse(writer.toString().isEmpty()); }
public Document parseBPELFile(InputStream bpelStream) { // System.setProperty("javax.xml.parsers.DocumentBuilderFactory", // "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); // Get Document Builder Factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // turn off validation, we use the new validation package factory.setValidating(false); // ignore comments factory.setIgnoringComments(true); // turn on namespaces factory.setNamespaceAware(true); Document doc = null; try { DocumentBuilder builder = factory.newDocumentBuilder(); doc = builder.parse(bpelStream); if (doc != null) { // System.setProperty("javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema","org.apache.xerces.jaxp.validation.XMLSchemaFactory"); // Validate SchemaFactory constraintFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplementationLS = (DOMImplementationLS) registry.getDOMImplementation("LS"); if (domImplementationLS != null) { // set our own resolver implementation constraintFactory.setResourceResolver(new BPELResolver(domImplementationLS)); } // should we validate against "executable" or "abstract" BPEL ? // default is "executable" Source bpelConstraints = new StreamSource(new File(FILENAME_BPEL_EXECUTABLE_SCHEMA)); if (doc.getDocumentElement().getNamespaceURI() != null) { if (doc.getDocumentElement().getNamespaceURI().equals(BPEL_ABSTRACT_NS)) { bpelConstraints = new StreamSource(new File(FILENAME_BPEL_ABSTRACT_SCHEMA)); } } Schema schema = constraintFactory.newSchema(bpelConstraints); Validator validator = schema.newValidator(); try { validator.validate(new DOMSource(doc)); this.successfulValidation = true; } catch (SAXException e) { // schema validation failed! // we continue with the transformation, however, the user is notified this.successfulValidation = false; this.validationException = e.getMessage(); } } } catch (Exception e) { // a lot can go wrong, no chance to recover e.printStackTrace(); } return doc; }