public static Map<String, List<Object>> parseAttributes(String samlResponse,
      Set<String> attributes) throws ServletException {
    Map<String, List<Object>> parsedResult = new HashMap<>();
    // TODO - Using a library for managing collections
    if (attributes == null || attributes.isEmpty()) {
      return parsedResult;
    }
    byte[] samlAssertionDecoded = new byte[0];
    try {
      samlAssertionDecoded = Base64.decodeBase64(samlResponse.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("Unsupported encoding to decode Saml Response");
    }
    ByteArrayInputStream is = new ByteArrayInputStream(samlAssertionDecoded);
    Assertion assertion;
    try {
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.parse(is);
      Element element = document.getDocumentElement();
      Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element);
      if (unmarshaller == null) {
        throw new RuntimeException("Could not find an unmarshaller for XML parsing");
      }
      XMLObject responseXmlObj = unmarshaller.unmarshall(element);
      Response response = (Response) responseXmlObj;
      if (response.getAssertions() == null || response.getAssertions().isEmpty()) {
        throw new ServletException("No assertions found in SAML response");
      }
      assertion = response.getAssertions().get(0);
    } catch (UnmarshallingException | ParserConfigurationException | SAXException | IOException e) {
      throw new ServletException("Could not parse the SAML assertion", e);
    }
    for (AttributeStatement attributeStatement : assertion.getAttributeStatements()) {
      for (Attribute attribute : attributeStatement.getAttributes()) {
        if (attributes.contains(attribute.getName())) {
          List<Object> values = new ArrayList<>(attribute.getAttributeValues());
          parsedResult.put(attribute.getName(), values);
        }
      }
    }
    return parsedResult;
  }
public static Map<String, List<Object>> parseAttributes(String samlResponse,
      Set<String> attributes) throws ServletException {
    Map<String, List<Object>> parsedResult = new HashMap<>();
   
    if (attributes == null || attributes.isEmpty()) {
      return parsedResult;
    }
    byte[] samlAssertionDecoded = new byte[0];
    try {
      samlAssertionDecoded = Base64.decodeBase64(samlResponse.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("Unsupported encoding to decode Saml Response");
    }
    ByteArrayInputStream is = new ByteArrayInputStream(samlAssertionDecoded);
    Assertion assertion;
    try {
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.parse(is);
      Element element = document.getDocumentElement();
      Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element);
      if (unmarshaller == null) {
        throw new RuntimeException("Could not find an unmarshaller for XML parsing");
      }
      XMLObject responseXmlObj = unmarshaller.unmarshall(element);
      Response response = (Response) responseXmlObj;
      if (response.getAssertions() == null || response.getAssertions().isEmpty()) {
        throw new ServletException("No assertions found in SAML response");
      }
      assertion = response.getAssertions().get(0);
    } catch (UnmarshallingException | ParserConfigurationException | SAXException | IOException e) {
      throw new ServletException("Could not parse the SAML assertion", e);
    }
    for (AttributeStatement attributeStatement : assertion.getAttributeStatements()) {
      for (Attribute attribute : attributeStatement.getAttributes()) {
        if (attributes.contains(attribute.getName())) {
          List<Object> values = new ArrayList<>(attribute.getAttributeValues());
          parsedResult.put(attribute.getName(), values);
        }
      }
    }
    return parsedResult;
  }
public static map<string, list<object>> parseattributes(string samlresponse, set<string> attributes) throws servletexception { map<string, list<object>> parsedresult = new hashmap<>(); if (attributes == null || attributes.isempty()) { return parsedresult; } byte[] samlassertiondecoded = new byte[0]; try { samlassertiondecoded = base64.decodebase64(samlresponse.getbytes("utf-8")); } catch (unsupportedencodingexception e) { throw new runtimeexception("unsupported encoding to decode saml response"); } bytearrayinputstream is = new bytearrayinputstream(samlassertiondecoded); assertion assertion; try { documentbuilder builder = factory.newdocumentbuilder(); document document = builder.parse(is); element element = document.getdocumentelement(); unmarshaller unmarshaller = unmarshallerfactory.getunmarshaller(element); if (unmarshaller == null) { throw new runtimeexception("could not find an unmarshaller for xml parsing"); } xmlobject responsexmlobj = unmarshaller.unmarshall(element); response response = (response) responsexmlobj; if (response.getassertions() == null || response.getassertions().isempty()) { throw new servletexception("no assertions found in saml response"); } assertion = response.getassertions().get(0); } catch (unmarshallingexception | parserconfigurationexception | saxexception | ioexception e) { throw new servletexception("could not parse the saml assertion", e); } for (attributestatement attributestatement : assertion.getattributestatements()) { for (attribute attribute : attributestatement.getattributes()) { if (attributes.contains(attribute.getname())) { list<object> values = new arraylist<>(attribute.getattributevalues()); parsedresult.put(attribute.getname(), values); } } } return parsedresult; }