List of usage examples for javax.xml.stream XMLStreamWriter flush
public void flush() throws XMLStreamException;
From source file:org.apache.olingo.commons.core.data.AtomSerializer.java
private void link(final Writer outWriter, final Link link) throws XMLStreamException { final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter); writer.writeStartDocument();/* w w w . j a v a 2s . c om*/ writer.writeStartElement(Constants.ELEM_LINKS); writer.writeDefaultNamespace(version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES)); writer.writeStartElement(Constants.ELEM_URI); writer.writeCharacters(link.getHref()); writer.writeEndElement(); writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); }
From source file:org.apache.olingo.commons.core.serialization.AtomSerializer.java
private void entity(final Writer outWriter, final Entity entity) throws XMLStreamException, EdmPrimitiveTypeException { final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter); if (entity.getType() == null && entity.getProperties().isEmpty()) { writer.writeStartDocument();/*from ww w . ja va 2s. co m*/ writer.setDefaultNamespace(namespaceMetadata); entityRef(writer, entity); } else { startDocument(writer, Constants.ATOM_ELEM_ENTRY); entity(writer, entity); } writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); }
From source file:org.apache.olingo.commons.core.serialization.AtomSerializer.java
private void entitySet(final Writer outWriter, final EntitySet entitySet) throws XMLStreamException, EdmPrimitiveTypeException { final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter); startDocument(writer, Constants.ATOM_ELEM_FEED); entitySet(writer, entitySet);/*from w ww .j a va 2 s . c o m*/ writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); }
From source file:org.apache.olingo.commons.core.serialization.AtomSerializer.java
private void entitySet(final Writer outWriter, final ResWrap<EntitySet> entitySet) throws XMLStreamException, EdmPrimitiveTypeException { final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter); startDocument(writer, Constants.ATOM_ELEM_FEED); addContextInfo(writer, entitySet);/* ww w .ja va2 s.co m*/ entitySet(writer, entitySet.getPayload()); writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); }
From source file:org.apache.synapse.commons.json.JsonDataSource.java
public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException { XMLStreamReader reader = getReader(); xmlWriter.writeStartDocument();/*from w w w .j a v a 2 s. co m*/ while (reader.hasNext()) { int x = reader.next(); switch (x) { case XMLStreamConstants.START_ELEMENT: xmlWriter.writeStartElement(reader.getPrefix(), reader.getLocalName(), reader.getNamespaceURI()); int namespaceCount = reader.getNamespaceCount(); for (int i = namespaceCount - 1; i >= 0; i--) { xmlWriter.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i)); } int attributeCount = reader.getAttributeCount(); for (int i = 0; i < attributeCount; i++) { xmlWriter.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i), reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } break; case XMLStreamConstants.START_DOCUMENT: break; case XMLStreamConstants.CHARACTERS: xmlWriter.writeCharacters(reader.getText()); break; case XMLStreamConstants.CDATA: xmlWriter.writeCData(reader.getText()); break; case XMLStreamConstants.END_ELEMENT: xmlWriter.writeEndElement(); break; case XMLStreamConstants.END_DOCUMENT: xmlWriter.writeEndDocument(); break; case XMLStreamConstants.SPACE: break; case XMLStreamConstants.COMMENT: xmlWriter.writeComment(reader.getText()); break; case XMLStreamConstants.DTD: xmlWriter.writeDTD(reader.getText()); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: xmlWriter.writeProcessingInstruction(reader.getPITarget(), reader.getPIData()); break; case XMLStreamConstants.ENTITY_REFERENCE: xmlWriter.writeEntityRef(reader.getLocalName()); break; default: throw new OMException(); } } xmlWriter.writeEndDocument(); xmlWriter.flush(); xmlWriter.close(); }
From source file:org.asimba.wa.integrationtest.saml2.model.AuthnRequest.java
/** * Get String with the SAML2 AuthnRequest message * @param format -1=plain, 1=base64//from ww w . ja v a 2 s.c o m * @return * @throws XMLStreamException * @throws IOException */ public String getRequest(int format) throws XMLStreamException, IOException { _logger.info("For ID: " + this._id); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION, true); DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(baos, compresser); StringWriter sw = new StringWriter(); XMLOutputFactory factory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = null; // ugly but effective: if (format == base64) { writer = factory.createXMLStreamWriter(deflaterOutputStream); } else { writer = factory.createXMLStreamWriter(sw); } writer.writeStartElement("samlp", "AuthnRequest", "urn:oasis:names:tc:SAML:2.0:protocol"); writer.writeNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol"); writer.writeAttribute("ID", _id); writer.writeAttribute("Version", "2.0"); writer.writeAttribute("IssueInstant", this._issueInstant); writer.writeAttribute("ProtocolBinding", "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"); writer.writeAttribute("AssertionConsumerServiceURL", _acsUrl); writeIssuer(writer); writeNameIDPolicy(writer); writeRequestedAuthnContext(writer); writer.writeEndElement(); writer.flush(); if (format == base64) { deflaterOutputStream.close(); byte[] bain = baos.toByteArray(); byte[] encoded = Base64.encodeBase64(bain, false); String result = new String(encoded, Charset.forName("UTF-8")); return result; } else { return sw.toString(); } }
From source file:org.asimba.wa.integrationtest.saml2.model.Response.java
/** * Build response based on current state.<br/> * Only do Base64-encoding of the XML-document -- no deflating whatsoever may be done. * /* w w w. ja v a 2s .com*/ * @return * @throws XMLStreamException */ public String getResponse(int format) throws XMLStreamException { _logger.info("For ID: " + getId()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); StringWriter sw = new StringWriter(); XMLOutputFactory factory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = null; // ugly but effective: if (format == base64) { writer = factory.createXMLStreamWriter(baos); } else { writer = factory.createXMLStreamWriter(sw); } writer.writeStartElement("saml2p", "Response", "urn:oasis:names:tc:SAML:2.0:protocol"); writer.writeNamespace("saml2p", "urn:oasis:names:tc:SAML:2.0:protocol"); writer.writeNamespace("xs", "http://www.w3.org/2001/XMLSchema"); writer.writeAttribute("Destination", _destination); writer.writeAttribute("ID", _id); writer.writeAttribute("InResponseTo", _inResponseTo); writer.writeAttribute("IssueInstant", _issueInstant); writer.writeAttribute("Version", "2.0"); writeIssuer(writer); writeStatus(writer); _assertion.writeAssertion(writer); writer.writeEndElement(); // SAML2 writer.flush(); if (format == base64) { byte[] bain = baos.toByteArray(); byte[] encoded = Base64.encodeBase64(bain, false); String result = new String(encoded, Charset.forName("UTF-8")); return result; } else { return sw.toString(); } }
From source file:org.atricore.idbus.capabilities.sso.support.core.signature.JSR105SamlR2SignerImpl.java
public oasis.names.tc.saml._1_0.protocol.ResponseType sign( oasis.names.tc.saml._1_0.protocol.ResponseType response) throws SamlR2SignatureException { try {//from w ww. j av a 2s.c om // Marshall the Assertion object as a DOM tree: if (logger.isDebugEnabled()) logger.debug("Marshalling SAMLR11 Response to DOM Tree [" + response.getResponseID() + "]"); // Instantiate the document to be signed javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); // XML Signature needs to be namespace aware dbf.setNamespaceAware(true); javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder(); JAXBContext context = JAXBContext.newInstance(SAMLR11Constants.SAML_PROTOCOL_PKG, response.getClass().getClassLoader()); Marshaller m = context.createMarshaller(); Class<oasis.names.tc.saml._1_0.protocol.ResponseType> clazz = (Class<oasis.names.tc.saml._1_0.protocol.ResponseType>) response .getClass(); // Remove the 'Type' suffix from the xml type name and use it as XML element! XmlType t = clazz.getAnnotation(XmlType.class); String element = t.name().substring(0, t.name().length() - 4); JAXBElement<oasis.names.tc.saml._1_0.protocol.ResponseType> jaxbResponse = new JAXBElement<oasis.names.tc.saml._1_0.protocol.ResponseType>( new QName(SAMLR11Constants.SAML_PROTOCOL_NS, element), clazz, response); // remove prefixes from signature elements of embedded signed assertion so that signature validation - // which removes those prefixes - doesn't fail StringWriter swrsp = new StringWriter(); XMLStreamWriter sw = new NamespaceFilterXMLStreamWriter(swrsp); // TODO : Use XML Utils!!!! m.marshal(jaxbResponse, sw); sw.flush(); Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(swrsp.toString().getBytes())); doc = sign(doc, response.getResponseID()); if (logger.isDebugEnabled()) logger.debug("Unmarshalling SAMLR11 Response from DOM Tree [" + response.getResponseID() + "]"); // Unmarshall the assertion Unmarshaller u = context.createUnmarshaller(); jaxbResponse = (JAXBElement<oasis.names.tc.saml._1_0.protocol.ResponseType>) u.unmarshal(doc); return jaxbResponse.getValue(); } catch (JAXBException e) { throw new SamlR2SignatureException("JAXB Error signing SAMLR11 Response " + response.getResponseID(), e); } catch (ParserConfigurationException e) { throw new SamlR2SignatureException( "XML Parser Error signing SAMLR11 Response " + response.getResponseID(), e); } catch (XMLStreamException e) { throw new SamlR2SignatureException( "XML Parser Error signing SAMLR11 Response " + response.getResponseID(), e); } catch (IOException e) { throw new SamlR2SignatureException("I/O Error signing SAMLR11 Response " + response.getResponseID(), e); } catch (SAXException e) { throw new SamlR2SignatureException( "XML Parser Error signing SAMLR11 Response " + response.getResponseID(), e); } }
From source file:org.atricore.idbus.capabilities.sso.support.core.util.XmlUtils.java
public static String marshalSamlR2(Object msg, String msgQName, String msgLocalName) throws Exception { //JAXBContext jaxbContext = createJAXBContext(userPackages); JAXBContext jaxbContext = JAXBUtils.getJAXBContext(samlContextPackages, constructionType, samlContextPackages.toString(), XmlUtils.class.getClassLoader(), new HashMap<String, Object>()); Marshaller m = JAXBUtils.getJAXBMarshaller(jaxbContext); JAXBElement jaxbRequest = new JAXBElement(new QName(msgQName, msgLocalName), msg.getClass(), msg); Writer writer = new StringWriter(); XMLStreamWriter xmlStreamWriter = new NamespaceFilterXMLStreamWriter(writer); // Support XMLDsig // TODO : What about non-sun XML Bind stacks! m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() { @Override// w w w . ja v a 2 s.c o m public String[] getPreDeclaredNamespaceUris() { return new String[] { SAMLR2Constants.SAML_PROTOCOL_NS, SAMLR2Constants.SAML_ASSERTION_NS, "http://www.w3.org/2000/09/xmldsig#", "http://www.w3.org/2001/04/xmlenc#", "http://www.w3.org/2001/XMLSchema" }; } @Override public String getPreferredPrefix(String nsUri, String suggestion, boolean requirePrefix) { if (nsUri.equals(SAMLR2Constants.SAML_PROTOCOL_NS)) return "samlp"; else if (nsUri.equals(SAMLR2Constants.SAML_ASSERTION_NS)) return "saml"; else if (nsUri.equals("http://www.w3.org/2000/09/xmldsig#")) return "ds"; else if (nsUri.equals("http://www.w3.org/2001/04/xmlenc#")) return "enc"; else if (nsUri.equals("http://www.w3.org/2001/XMLSchema")) return "xsd"; return suggestion; } }); m.marshal(jaxbRequest, xmlStreamWriter); xmlStreamWriter.flush(); JAXBUtils.releaseJAXBMarshaller(jaxbContext, m); return writer.toString(); }
From source file:org.atricore.idbus.capabilities.sso.support.core.util.XmlUtils.java
public static Document marshalSamlR2AsDom(Object msg, String msgQName, String msgLocalName, String[] userPackages) throws Exception { // JAXB Element JAXBElement jaxbMsg = new JAXBElement(new QName(msgQName, msgLocalName), msg.getClass(), msg); JAXBContext jaxbContext = JAXBUtils.getJAXBContext(samlContextPackages, constructionType, samlContextPackages.toString(), XmlUtils.class.getClassLoader(), new HashMap<String, Object>()); Marshaller marshaller = JAXBUtils.getJAXBMarshaller(jaxbContext); // Marshal as string and then parse with DOM ... StringWriter writer = new StringWriter(); XMLStreamWriter xmlStreamWriter = new NamespaceFilterXMLStreamWriter(writer); // TODO : What about non-sun XML Bind stacks! marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() { @Override//from w ww .j av a 2 s . co m public String[] getPreDeclaredNamespaceUris() { return new String[] { SAMLR2Constants.SAML_PROTOCOL_NS, SAMLR2Constants.SAML_ASSERTION_NS, "http://www.w3.org/2000/09/xmldsig#", "http://www.w3.org/2001/04/xmlenc#", "http://www.w3.org/2001/XMLSchema" }; } @Override public String getPreferredPrefix(String nsUri, String suggestion, boolean requirePrefix) { if (nsUri.equals(SAMLR2Constants.SAML_PROTOCOL_NS)) return "samlp"; else if (nsUri.equals(SAMLR2Constants.SAML_ASSERTION_NS)) return "saml"; else if (nsUri.equals("http://www.w3.org/2000/09/xmldsig#")) return "ds"; else if (nsUri.equals("http://www.w3.org/2001/04/xmlenc#")) return "enc"; else if (nsUri.equals("http://www.w3.org/2001/XMLSchema")) return "xsd"; return suggestion; } }); marshaller.marshal(jaxbMsg, xmlStreamWriter); xmlStreamWriter.flush(); // Instantiate the document to be signed javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); // XML Signature needs to be namespace aware dbf.setNamespaceAware(true); Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(writer.toString().getBytes())); JAXBUtils.releaseJAXBMarshaller(jaxbContext, marshaller); return doc; }