Guest User

Untitled

a guest
Jul 20th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. import java.io.StringReader;
  2. import java.text.SimpleDateFormat;
  3. import java.util.*;
  4. import javax.xml.parsers.*;
  5. import org.w3c.dom.*;
  6. import org.xml.sax.InputSource;
  7.  
  8. public class Example2 {
  9.     private static final String reqMsg =
  10.         "<samlp:AttributeQuery " +
  11.         " xmlns:samlp='urn:oasis:names:tc:SAML:2.0:protocol' " +
  12.         " ID='%%ID%%' IssueInstant='%%ISSUEINST%%' Version='2.0'>" +
  13.         " <saml:Issuer xmlns:saml='urn:oasis:names:tc:SAML:2.0:assertion'>"
  14.         +
  15.         "%%ISSUER%%" +
  16.         " </saml:Issuer>" +
  17.         " <saml:Subject xmlns:saml='urn:oasis:names:tc:SAML:2.0:assertion'>"
  18.         +
  19.         " <saml:NameID Format='urn:oasis:names:tc:SAML:2.0:nameid-format:
  20.         unspecified'" +
  21.         " xmlns:saml='urn:oasis:names:tc:SAML:2.0:assertion'> " +
  22.         "%%SUBJECT%%" +
  23.         " </saml:NameID>" +
  24.         " </saml:Subject>" +
  25.         "</samlp:AttributeQuery>";
  26.        
  27.     public static String getAttributeRequest(String id, String issuer, String subject) {
  28.    
  29.         Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("US/Arizona"));
  30.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  31.         SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
  32.         String res = Example2.reqMsg
  33.         res = res.replaceAll("%%ID%%", id);
  34.         res = res.replaceAll("%%ISSUEINST%%", sdf.format(cal.getTime()) +
  35.         "T" +
  36.         sdfTime.format(cal.getTime()) + "Z");
  37.         res = res.replaceAll("%%ISSUER%%", issuer);
  38.         res = res.replaceAll("%%SUBJECT%%", subject);
  39.         return res;
  40.     }
  41.    
  42.     public static Element getElementFromString(String s) throws Exception {
  43.         DocumentBuilderFactory factory = DocumentBuilderFactory.
  44.         newInstance();
  45.         factory.setNamespaceAware(true);
  46.         DocumentBuilder builder = factory.newDocumentBuilder();
  47.         InputSource is = new InputSource(new StringReader(s));
  48.         Document d = builder.parse(is);
  49.         return d.getDocumentElement();
  50.     }
  51.     public static void main(String[] args) throws Exception {
  52.         String id = UUID.randomUUID().toString();
  53.         String issuer = "http://www.prise.es/uoc-book/example2";
  54.         String subject = "cr01";
  55.         String reqMsg = getAttributeRequest(id, issuer, subject);
  56.         System.out.println(reqMsg);
  57.         Element req = getElementFromString(reqMsg);
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment