protected String buildXMLResultString(ResultSequence rs) throws Exception {
    DOMImplementationLS domLS =
        (DOMImplementationLS) domDoc.getImplementation().getFeature("LS", "3.0");
    LSOutput outputText = domLS.createLSOutput();
    LSSerializer serializer = domLS.createLSSerializer();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputText.setByteStream(outputStream);

    String actual = new String();
    Iterator iterator = rs.iterator();
    boolean queueSpace = false;
    while (iterator.hasNext()) {
      AnyType aat = (AnyType) iterator.next();
      if (aat instanceof NodeType) {
        NodeType nodeType = (NodeType) aat;
        Node node = nodeType.node_value();
        serializer.write(node, outputText);
        queueSpace = false;
      } else {
        if (queueSpace) outputText.getByteStream().write(32);
        outputText.getByteStream().write(aat.getStringValue().getBytes("UTF-8"));
        queueSpace = true;
      }
    }

    actual = outputStream.toString("UTF-8");
    actual = actual.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
    actual = actual.replace("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>", "");
    outputStream.close();
    return actual.trim();
  }
  /**
   * 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);
    }
  }
 /**
  * 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);
   }
 }
Ejemplo n.º 4
0
  /**
   * Converts a given {@link Document} to an AXIOM {@link org.apache.axiom.soap.SOAPEnvelope}.
   *
   * @param document the document to be converted
   * @return the converted envelope
   * @throws IllegalArgumentException in case of errors
   * @see org.apache.rampart.util.Axis2Util.getSOAPEnvelopeFromDOMDocument(Document, boolean)
   */
  public static SOAPEnvelope toEnvelope(Document document) {
    try {
      DOMImplementation implementation = document.getImplementation();
      Assert.isInstanceOf(DOMImplementationLS.class, implementation);

      DOMImplementationLS loadSaveImplementation = (DOMImplementationLS) implementation;
      LSOutput output = loadSaveImplementation.createLSOutput();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      output.setByteStream(bos);

      LSSerializer serializer = loadSaveImplementation.createLSSerializer();
      serializer.write(document, output);

      ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

      XMLInputFactory inputFactory = StAXUtils.getXMLInputFactory();

      StAXSOAPModelBuilder stAXSOAPModelBuilder =
          new StAXSOAPModelBuilder(inputFactory.createXMLStreamReader(bis), null);
      SOAPEnvelope envelope = stAXSOAPModelBuilder.getSOAPEnvelope();

      // Necessary to build a correct Axiom tree, see SWS-483
      envelope.serialize(new NullOutputStream());

      return envelope;
    } catch (Exception ex) {
      IllegalArgumentException iaex =
          new IllegalArgumentException("Error in converting Document to SOAP Envelope");
      iaex.initCause(ex);
      throw iaex;
    }
  }
Ejemplo n.º 5
0
  /**
   * 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);
    }
  }