code:     public Document getMetaData(
          HttpServletRequest request, FedizContext config
      ) throws ProcessingException {
  
          try {
              ByteArrayOutputStream bout = new ByteArrayOutputStream(4096);
              Writer streamWriter = new OutputStreamWriter(bout, "UTF-8");
              XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(streamWriter);
  
              Protocol protocol = config.getProtocol();
  
              writer.writeStartDocument("UTF-8", "1.0");
  
              String referenceID = IDGenerator.generateID("_");
              writer.writeStartElement("md", "EntityDescriptor", SAML2_METADATA_NS);
              writer.writeAttribute("ID", referenceID);
              
              String serviceURL = protocol.getApplicationServiceURL();
              if (serviceURL == null) {
                  serviceURL = extractFullContextPath(request);
              }
              
              writer.writeAttribute("entityID", serviceURL);
              
              writer.writeNamespace("md", SAML2_METADATA_NS);
              writer.writeNamespace("fed", WS_FEDERATION_NS);
              writer.writeNamespace("wsa", WS_ADDRESSING_NS);
              writer.writeNamespace("auth", WS_FEDERATION_NS);
              writer.writeNamespace("xsi", SCHEMA_INSTANCE_NS);
  
              if (protocol instanceof FederationProtocol) {
                  writeFederationMetadata(writer, config, serviceURL);
              } else if (protocol instanceof SAMLProtocol) {
                  writeSAMLMetadata(writer, request, config, serviceURL);
              }
              
              writer.writeEndElement(); // EntityDescriptor
  
              writer.writeEndDocument();
              
              streamWriter.flush();
              bout.flush();
              //
  
              if (LOG.isDebugEnabled()) {
                  String out = new String(bout.toByteArray());
                  LOG.debug("***************** unsigned ****************");
                  LOG.debug(out);
                  LOG.debug("***************** unsigned ****************");
              }
  
              InputStream is = new ByteArrayInputStream(bout.toByteArray());
              
              boolean hasSigningKey = false;
              try {
                  if (config.getSigningKey().getCrypto() != null) {
                      hasSigningKey = true;
                  }
              } catch (Exception ex) {
                  LOG.info("No signingKey element found in config: " + ex.getMessage());
              }
              if (hasSigningKey) {
                  Document doc = DOMUtils.readXml(is);
                  Document result = SignatureUtils.signMetaInfo(
                      config.getSigningKey().getCrypto(), config.getSigningKey().getKeyAlias(), config.getSigningKey().getKeyPassword(), doc, referenceID);
                  if (result != null) {
                      return result;
                  } else {
                      throw new ProcessingException("Failed to sign the metadata document: result=null");
                  }
              }
              return DOMUtils.readXml(is);
          } catch (ProcessingException e) {
              throw e;
          } catch (Exception e) {
              LOG.error("Error creating service metadata information ", e);
              throw new ProcessingException("Error creating service metadata information: " + e.getMessage());
          }
  
      }
  
  