/**
   * 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);
  }
  /**
   * Helper method that marshalls the given message.
   *
   * @param message message the marshall and serialize
   * @return marshalled message
   * @throws MessageEncodingException thrown if the give message can not be marshalled into its DOM
   *     representation
   */
  protected Element marshallMessage(XMLObject message) throws MessageEncodingException {
    log.debug("Marshalling message");

    try {
      return XMLObjectSupport.marshall(message);
    } catch (MarshallingException e) {
      throw new MessageEncodingException("Error marshalling message", e);
    }
  }