下面列出了org.w3c.dom.ls.LSOutput#setByteStream ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Serialize XML objects
*
* @param xmlObject : XACML or SAML objects to be serialized
* @return serialized XACML or SAML objects
*/
private String marshall(XMLObject xmlObject) throws EntitlementProxyException {
try {
doBootstrap();
System.setProperty(DOCUMENT_BUILDER_FACTORY, DOCUMENT_BUILDER_FACTORY_IMPL);
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 (Exception e) {
log.error("Error Serializing the SAML Response");
throw new EntitlementProxyException("Error Serializing the SAML Response", e);
}
}
/**
* `
* Serialize XML objects
*
* @param xmlObject : XACML or SAML objects to be serialized
* @return serialized XACML or SAML objects
* @throws EntitlementException
*/
private String marshall(XMLObject xmlObject) throws EntitlementException {
try {
doBootstrap();
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
MarshallerFactory marshallerFactory = XMLObjectProviderRegistrySupport.getMarshallerFactory();
Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject);
Element element = marshaller.marshall(xmlObject);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl =
(DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
LSOutput output = impl.createLSOutput();
output.setByteStream(byteArrayOutputStream);
writer.write(element, output);
return byteArrayOutputStream.toString();
} catch (Exception e) {
log.error("Error Serializing the SAML Response");
throw new EntitlementException("Error Serializing the SAML Response", e);
}
}
/**
* `
* Serialize XML objects
*
* @param xmlObject : XACML or SAML objects to be serialized
* @return serialized XACML or SAML objects
* @throws EntitlementException
*/
private String marshall(XMLObject xmlObject) throws EntitlementException {
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 byteArrayOutputStream = new ByteArrayOutputStream();
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl =
(DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
LSOutput output = impl.createLSOutput();
output.setByteStream(byteArrayOutputStream);
writer.write(element, output);
return byteArrayOutputStream.toString();
} catch (Exception e) {
log.error("Error Serializing the SAML Response");
throw new EntitlementException("Error Serializing the SAML Response", e);
}
}
/**
* Serializing a SAML2 object into a String
*
* @param xmlObject object that needs to serialized.
* @return serialized object
* @throws SAMLSSOException
*/
public static String marshall(XMLObject xmlObject) throws SAMLSSOException {
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 byteArrayOutputStrm.toString();
} catch (Exception e) {
log.error("Error Serializing the SAML Response");
throw new SAMLSSOException("Error Serializing the SAML Response", e);
}
}
/**
* Serializing a SAML2 object into a String
*
* @param xmlObject object that needs to serialized.
* @return serialized object
* @throws SAML2SSOUIAuthenticatorException
*/
public static String marshall(XMLObject xmlObject) throws SAML2SSOUIAuthenticatorException {
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) {
log.error("Error Serializing the SAML Response");
throw new SAML2SSOUIAuthenticatorException("Error Serializing the SAML Response", e);
}
}
private static String marshall(XMLObject xmlObject) throws org.wso2.carbon.identity.base.IdentityException {
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 byteArrayOutputStrm.toString("UTF-8");
} catch (Exception e) {
log.error("Error Serializing the SAML Response");
throw IdentityException.error("Error Serializing the SAML Response", e);
}
}
/**
* 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 = XMLObjectProviderRegistrySupport.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);
}
}
/**
* 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);
}
}
/**
* Writes a DOM Document to the given OutputStream using the "UTF-8"
* encoding. The XML declaration is omitted.
*
* @param outStream
* The destination OutputStream object.
* @param doc
* A Document node.
*/
void writeDocument(OutputStream outStream, Document doc) {
DOMImplementationRegistry domRegistry = null;
try {
domRegistry = DOMImplementationRegistry.newInstance();
// Fortify Mod: Broaden try block to capture all potential exceptions
// } catch (Exception e) {
// LOGR.warning(e.getMessage());
// }
DOMImplementationLS impl = (DOMImplementationLS) domRegistry
.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("xml-declaration", false);
writer.getDomConfig().setParameter("format-pretty-print", true);
LSOutput output = impl.createLSOutput();
output.setEncoding("UTF-8");
output.setByteStream(outStream);
writer.write(doc, output);
} catch (Exception e) {
LOGR.warning(e.getMessage());
}
}
private static String elementToString(Element e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DOMImplementationLS ls = (DOMImplementationLS) e.getOwnerDocument().getImplementation().getFeature("LS", "3.0"); // NOI18N
LSOutput output = ls.createLSOutput();
output.setByteStream(baos);
LSSerializer ser = ls.createLSSerializer();
ser.write(e, output);
return baos.toString();
}
/**
* Writes a Node out to an OutputStream using the DOM, level 3, Load/Save serializer. The written content
* is encoded using the encoding specified in the output stream configuration.
*
* @param node the node to write out
* @param output the output stream to write the XML to
* @param serializerParams parameters to pass to the {@link DOMConfiguration} of the serializer
* instance, obtained via {@link LSSerializer#getDomConfig()}. May be null.
*/
public static void writeNode(Node node, OutputStream output, Map<String, Object> serializerParams) {
DOMImplementationLS domImplLS = getLSDOMImpl(node);
LSSerializer serializer = getLSSerializer(domImplLS, serializerParams);
LSOutput serializerOut = domImplLS.createLSOutput();
serializerOut.setByteStream(output);
serializer.write(node, serializerOut);
}
@SuppressWarnings("unchecked")
private void getGroupings(Integer organisationId, HttpServletResponse response) throws IOException {
Document doc = OrganisationGroupServlet.docBuilder.newDocument();
Element groupsElement = doc.createElement("groups");
doc.appendChild(groupsElement);
List<OrganisationGrouping> groupings = userManagementService.findByProperty(OrganisationGrouping.class,
"organisationId", organisationId);
for (OrganisationGrouping grouping : groupings) {
Element groupingElement = doc.createElement("grouping");
groupingElement.setAttribute("id", grouping.getGroupingId().toString());
groupingElement.setAttribute("name", StringEscapeUtils.escapeXml(grouping.getName()));
groupsElement.appendChild(groupingElement);
for (OrganisationGroup group : grouping.getGroups()) {
Element groupElement = doc.createElement("group");
groupElement.setAttribute("id", group.getGroupId().toString());
groupElement.setAttribute("name", StringEscapeUtils.escapeXml(group.getName()));
groupingElement.appendChild(groupElement);
for (User user : group.getUsers()) {
Element userElement = doc.createElement("user");
userElement.setAttribute("id", user.getUserId().toString());
userElement.setAttribute("firstname", StringEscapeUtils.escapeXml(user.getFirstName()));
userElement.setAttribute("lastname", StringEscapeUtils.escapeXml(user.getLastName()));
groupElement.appendChild(userElement);
}
}
}
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
LSOutput lsOutput = domImplementation.createLSOutput();
lsOutput.setEncoding("UTF-8");
lsOutput.setByteStream(response.getOutputStream());
lsSerializer.write(doc, lsOutput);
}
/** Returns a <code>LSOutput</code> instance.
* @param impl A <code>DOMImplementationLS</code> instance
* @param os Optional <code>OutputStream</code> instance
* @param encoding Optional character encoding, default is UTF-8
* @return A <code>LSOutput</code> instance
* @see <a href="http://www.w3.org/TR/2004/REC-DOM-Level-3-LS-20040407/">DOM Level 3 Load and Save Specification</a>
*/
public static LSOutput createLSOutput(DOMImplementationLS impl, OutputStream os, String encoding) {
LSOutput out = impl.createLSOutput();
if (os != null) {
out.setByteStream(os);
}
if (encoding != null) {
out.setEncoding(encoding);
}
return out;
}
/**
* string value of document
*
* @return the string
*/
public final String stringValue()
{
if(log.isDebugEnabled())
{
log.debug("stringValue()");
}
if(document == null)
{
return this.xml.toString();
}
else
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
DOMImplementationRegistry registry = DOMImplementationRegistry
.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry
.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print",
Boolean.TRUE);
LSOutput output = impl.createLSOutput();
output.setByteStream(out);
writer.write(document, output);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return out.toString();
}
}
/**
* Serialize XML objects
*
* @param xmlObject : XACML or SAML objects to be serialized
* @return serialized XACML or SAML objects
*/
private String marshall(XMLObject xmlObject) throws EntitlementProxyException {
try {
doBootstrap();
System.setProperty(
DOCUMENT_BUILDER_FACTORY,
DOCUMENT_BUILDER_FACTORY_IMPL);
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 (Exception e) {
log.error("Error Serializing the SAML Response");
throw new EntitlementProxyException("Error Serializing the SAML Response", e);
}
}
/**
* string value of document
*
* @return the string
*/
public final String stringValue()
{
if(log.isDebugEnabled())
{
log.debug("stringValue()");
}
if(document == null)
{
return this.xml.toString();
}
else
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
DOMImplementationRegistry registry = DOMImplementationRegistry
.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry
.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print",
Boolean.TRUE);
LSOutput output = impl.createLSOutput();
output.setByteStream(out);
writer.write(document, output);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return out.toString();
}
}
public static void writeDocument(Document doc, Writer out) throws IOException
{
// happy to replace this code w/ the non-deprecated code, but I couldn't get the transformer
// approach to work.
// OutputFormat format = new OutputFormat(doc);
// format.setIndenting(true);
// format.setIndent(2);
// XMLSerializer serializer = new XMLSerializer(out, format);
// serializer.serialize(doc);
DOMImplementationLS impl = (DOMImplementationLS) doc.getImplementation();
LSSerializer writer = impl.createLSSerializer();
DOMConfiguration config = writer.getDomConfig();
if (config.canSetParameter("format-pretty-print", Boolean.TRUE))
{
config.setParameter("format-pretty-print", Boolean.TRUE);
}
// what a crappy way to force the stream to be UTF-8. yuck!
ByteArrayOutputStream baos = new ByteArrayOutputStream();
LSOutput output = impl.createLSOutput();
output.setEncoding("UTF-8");
output.setByteStream(baos);
writer.write(doc, output);
out.write(baos.toString());
out.flush();
}
public String toXml()
{
Document document;
DocumentBuilder docBuilder;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try
{
docBuilder = factory.newDocumentBuilder();
document = docBuilder.newDocument();
}
catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
DOMImplementationLS implLS = (DOMImplementationLS) docBuilder.getDOMImplementation();
LSSerializer domWriter = implLS.createLSSerializer();
LSOutput output = implLS.createLSOutput();
//DOMException: FEATURE_NOT_SUPPORTED: The parameter format-pretty-print is recognized but the requested value cannot be set.
//domWriter.getDomConfig().setParameter(Constants.DOM_FORMAT_PRETTY_PRINT, Boolean.TRUE);
output.setEncoding("UTF-8");
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
output.setByteStream(outStream);
Element root = document.createElement("requests");
for(Request req : _requests)
{
Element reqNode = document.createElement("request");
reqNode.setAttribute("id", Integer.toString(req.getId()));
reqNode.setAttribute("type", req.getType());
for(RequestPart part : req.getParts())
{
Element partNode = null;
if(part instanceof RequestForPart)
{
partNode = document.createElement("for");
writeForPart(partNode, (RequestForPart) part, document);
}
else
{
partNode = document.createElement("part");
partNode.setAttribute("name", part.getName());
partNode.setAttribute("value", part.getValue());
}
reqNode.appendChild(partNode);
}
root.appendChild(reqNode);
}
document.appendChild(root);
domWriter.write(document, output);
return outStream.toString();
}
private void getNotifications(Integer userId, HttpServletRequest request, HttpServletResponse response)
throws IOException {
Document doc = NotificationServlet.docBuilder.newDocument();
Element notificationsElement = doc.createElement("Notifications");
doc.appendChild(notificationsElement);
Long lessonId = WebUtil.readLongParam(request, CentralConstants.PARAM_LESSON_ID, true);
Integer limit = WebUtil.readIntParam(request, "limit", true);
Integer offset = WebUtil.readIntParam(request, "offset", true);
Boolean pendingOnly = WebUtil.readBooleanParam(request, "pendingOnly", true);
List<Subscription> subscriptions = eventNotificationService
.getNotificationSubscriptions(lessonId, userId, pendingOnly, limit, offset);
for (Subscription subscription : subscriptions) {
Element notificationElement = doc.createElement("Notification");
notificationElement.setAttribute("id", subscription.getUid().toString());
Boolean pending = !DeliveryMethodNotification.LAST_OPERATION_SEEN
.equals(subscription.getLastOperationMessage());
notificationElement.setAttribute("pending", pending.toString());
Long notificationLessonId = subscription.getEvent().getEventSessionId();
if (notificationLessonId != null) {
notificationElement.setAttribute("lessonId", notificationLessonId.toString());
}
String message = subscription.getEvent().getMessage();
Matcher matcher = NotificationServlet.anchorPattern.matcher(message);
if (matcher.find()) {
String href = StringEscapeUtils.escapeXml(matcher.group(2));
notificationElement.setAttribute("href", href);
message = matcher.group(3);
}
notificationElement.appendChild(doc.createCDATASection(message));
notificationsElement.appendChild(notificationElement);
}
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
LSOutput lsOutput = domImplementation.createLSOutput();
lsOutput.setEncoding("UTF-8");
lsOutput.setByteStream(response.getOutputStream());
lsSerializer.write(doc, lsOutput);
}
/**
* Saves the working project.
*/
public void saveWorkingProject() throws ParserConfigurationException
{
if( workingProject != null )
{
// Create document
DocumentBuilder db = dbFactory.newDocumentBuilder();
Document dom = db.newDocument();
// XML entry for the path waypoints and vars
Element pathElement = dom.createElement("Path" );
// Save generator type
pathElement.setAttribute( "GeneratorType", settings.getGeneratorType().name() );
// Save shared vars
settings.getSharedGeneratorVars().writeXMLAttributes( pathElement );
// Write generator vars to xml file
settings.getGeneratorVars().writeXMLAttributes( pathElement );
dom.appendChild( pathElement );
// Write waypoints to xml file
for( Waypoint wp : waypointList )
{
Element waypointEle = dom.createElement("Waypoint" );
Element xEle = dom.createElement("X" );
Element yEle = dom.createElement("Y" );
Element angleEle = dom.createElement("Angle" );
Text xText = dom.createTextNode("" + wp.getX() );
Text yText = dom.createTextNode("" + wp.getY() );
Text angleText = dom.createTextNode("" + wp.getAngle() );
xEle.appendChild( xText );
yEle.appendChild( yText );
angleEle.appendChild( angleText );
waypointEle.appendChild( xEle );
waypointEle.appendChild( yEle );
waypointEle.appendChild( angleEle );
pathElement.appendChild( waypointEle );
}
FileOutputStream fos;
try
{
fos = new FileOutputStream( workingProject );
DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) reg.getDOMImplementation("LS" );
LSSerializer serializer = impl.createLSSerializer();
serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE );
LSOutput lso = impl.createLSOutput();
lso.setByteStream( fos );
serializer.write( dom, lso );
}
catch( Exception e )
{
throw new RuntimeException( e );
}
}
}