/**
   * Base64 the given SAML message.
   *
   * @param message the SAML message
   * @return Base64 encoded message
   * @throws MarshallingException if there is a problem marshalling the XMLObject
   * @throws UnsupportedEncodingException If the named charset is not supported
   */
  @Nonnull
  public String encodeMessage(@Nonnull final SAMLObject message)
      throws MarshallingException, UnsupportedEncodingException {
    final Element domMessage = XMLObjectSupport.marshall(message);
    final String messageXML = SerializeSupport.nodeToString(domMessage);

    return Base64Support.encode(messageXML.getBytes("UTF-8"), Base64Support.UNCHUNKED);
  }
  /**
   * DEFLATE (RFC1951) compresses the given SAML message.
   *
   * @param message SAML message
   * @return DEFLATE compressed message
   * @throws MessageEncodingException thrown if there is a problem compressing the message
   */
  protected String deflateAndBase64Encode(SAMLObject message) throws MessageEncodingException {
    log.debug("Deflating and Base64 encoding SAML message");
    try {
      String messageStr = SerializeSupport.nodeToString(marshallMessage(message));

      ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
      Deflater deflater = new Deflater(Deflater.DEFLATED, true);
      DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater);
      deflaterStream.write(messageStr.getBytes("UTF-8"));
      deflaterStream.finish();

      return Base64Support.encode(bytesOut.toByteArray(), Base64Support.UNCHUNKED);
    } catch (IOException e) {
      throw new MessageEncodingException("Unable to DEFLATE and Base64 encode SAML message", e);
    }
  }