Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.StringReader;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import javax.xml.parsers.*;
- import org.w3c.dom.*;
- import org.xml.sax.InputSource;
- public class Example2 {
- private static final String reqMsg =
- "<samlp:AttributeQuery " +
- " xmlns:samlp='urn:oasis:names:tc:SAML:2.0:protocol' " +
- " ID='%%ID%%' IssueInstant='%%ISSUEINST%%' Version='2.0'>" +
- " <saml:Issuer xmlns:saml='urn:oasis:names:tc:SAML:2.0:assertion'>"
- +
- "%%ISSUER%%" +
- " </saml:Issuer>" +
- " <saml:Subject xmlns:saml='urn:oasis:names:tc:SAML:2.0:assertion'>"
- +
- " <saml:NameID Format='urn:oasis:names:tc:SAML:2.0:nameid-format:
- unspecified'" +
- " xmlns:saml='urn:oasis:names:tc:SAML:2.0:assertion'> " +
- "%%SUBJECT%%" +
- " </saml:NameID>" +
- " </saml:Subject>" +
- "</samlp:AttributeQuery>";
- public static String getAttributeRequest(String id, String issuer, String subject) {
- Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("US/Arizona"));
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
- String res = Example2.reqMsg
- res = res.replaceAll("%%ID%%", id);
- res = res.replaceAll("%%ISSUEINST%%", sdf.format(cal.getTime()) +
- "T" +
- sdfTime.format(cal.getTime()) + "Z");
- res = res.replaceAll("%%ISSUER%%", issuer);
- res = res.replaceAll("%%SUBJECT%%", subject);
- return res;
- }
- public static Element getElementFromString(String s) throws Exception {
- DocumentBuilderFactory factory = DocumentBuilderFactory.
- newInstance();
- factory.setNamespaceAware(true);
- DocumentBuilder builder = factory.newDocumentBuilder();
- InputSource is = new InputSource(new StringReader(s));
- Document d = builder.parse(is);
- return d.getDocumentElement();
- }
- public static void main(String[] args) throws Exception {
- String id = UUID.randomUUID().toString();
- String issuer = "http://www.prise.es/uoc-book/example2";
- String subject = "cr01";
- String reqMsg = getAttributeRequest(id, issuer, subject);
- System.out.println(reqMsg);
- Element req = getElementFromString(reqMsg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment