Index: fedd/abac-src/ttg/demo/src/AccessMediator.java
===================================================================
--- fedd/abac-src/ttg/demo/src/AccessMediator.java	(revision 8780cbecc6db2bdc2b9ef31eec6e5209ad3bff15)
+++ fedd/abac-src/ttg/demo/src/AccessMediator.java	(revision 8780cbecc6db2bdc2b9ef31eec6e5209ad3bff15)
@@ -0,0 +1,210 @@
+import com.nailabs.abac.credential.*;
+import com.nailabs.abac.process.*;
+import com.nailabs.abac.trust.*;
+import com.nailabs.abac.test.*;
+import edu.stanford.peer.rbtm.credential.*;
+import java.io.*;
+import java.text.*;
+import java.util.*;
+import java.rmi.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+
+public class AccessMediator implements Filter {
+    private Entity eid = null;
+    private EntityExpression target = null;
+    NegotiatorFactory clientFactory = null;
+    private FilterConfig conf = null;
+    Calendar cal = Calendar.getInstance();
+    DateFormat formatter = DateFormat.getDateInstance(DateFormat.FULL);
+
+    /** maximum no. of rmi lookup retries if a remote exception occurs */
+    private int MAX_RETRIES = 1;
+
+    private void getClientFactory(String hostName) {
+        StringBuffer url = new StringBuffer("//");
+        url.append(hostName).append("/");
+        url.append("ClientAuthenticationService");
+        //url.append("ClientRTMLService");
+        System.out.println("Attempting to contact" + url);
+        try {
+            clientFactory = 
+                (NegotiatorFactory)Naming.lookup(url.toString());
+        }
+        catch(Exception ex) {
+            ex.printStackTrace();
+        }
+    }
+
+    private Negotiator getClient(String host, String client,
+                                    RMINegotiator me)
+    {
+        for(int i = 0; i < MAX_RETRIES; i++) {
+            try {
+                return (Negotiator)clientFactory.getNegotiator(client, me);
+            }
+            catch(Exception ex) {
+		ex.printStackTrace();
+                getClientFactory(host);
+            }
+        }
+        return null;
+    } 
+
+    /** shared configuration hash table */
+    HashMap map = null;        
+
+    public void init(FilterConfig config) throws ServletException {
+        String hashCode = config.getInitParameter("EntityHash");
+	String shortName = config.getInitParameter("EntityName");
+        String cfile = config.getInitParameter("PolicyFile");
+        String targetName = config.getInitParameter("PrimaryRole");
+	map = FrontierManager.loadConfiguration(cfile);
+	//map = RtmlTest.loadConfiguration(cfile); 
+	config.getServletContext().log("map = " + map);	
+        conf = config;
+        conf.getServletContext().log("Loading new filter for " + shortName +
+                                     " from " + cfile + " with role " + 
+                                     targetName + "\n");
+        //RtmlFrontier.addFrontier(map);	
+        FrontierManager.addFrontier(map);	
+	//ResourcePolicy resourcePolicy = 
+	//    (ResourcePolicy)map.get("ResourcePolicy");
+
+        try {
+            //eid = new RtmlEntity(shortName, hashCode);
+	    //target = resourcePolicy.requires(targetName);
+            eid = new SimpleEntity(shortName);
+            target = StaticCredential.getRole(targetName);
+            System.out.println("Frontiers: " + 
+            FrontierManager.getFrontiers().toString());
+            //Iterator i = FrontierManager.getFrontiers().iterator();
+        }
+        catch(Exception ex) {
+            ex.printStackTrace();
+        }
+    }
+
+    public void destroy() { }
+
+    public  RtmlEntity getEntityId(HttpServletRequest request) {
+        final String KEY = "EntityID";
+	final String HASH = "HashID";
+        String shortName = null;
+	String hashCode = null;
+        HttpSession session = request.getSession();
+
+        shortName = request.getParameter("id");
+	hashCode = request.getParameter("hash");
+        if(session.isNew()) {     // new id bootstrapped
+            session.setAttribute(KEY, shortName);
+        } else if(shortName != null) { // forcing a new id manually
+            session.invalidate();
+            session = request.getSession();
+            session.setAttribute(KEY, shortName);
+            session.setAttribute(HASH, hashCode);
+        } else {                  // use the session's entity id value
+            shortName = (String)session.getAttribute(KEY);
+            hashCode = (String)session.getAttribute(HASH);
+        }
+	try {
+	    System.out.println("Using RtmlEntity = " + shortName + "(" +
+			       hashCode + ")");
+	    return new RtmlEntity(shortName, hashCode);
+	} catch(Exception ex) {
+	    ex.printStackTrace();
+	}
+	return null;
+    }
+
+    public Entity getEntity(HttpServletRequest request) {
+        final String KEY = "EntityID";
+        String shortName = null;
+        HttpSession session = request.getSession();
+
+        shortName = request.getParameter("id");
+        if(session.isNew()) {     // new id bootstrapped
+            session.setAttribute(KEY, shortName);
+        } else if(shortName != null) { // forcing a new id manually
+            session.invalidate();
+            session = request.getSession();
+            session.setAttribute(KEY, shortName);
+        } else {                  // use the session's entity id value
+            shortName = (String)session.getAttribute(KEY);
+        }
+	try {
+	    System.out.println("Using SimpleEntity = " + shortName);
+	    SimpleEntity id = new SimpleEntity(shortName);
+	    return (Entity)id;
+	} catch(Exception ex) {
+	    ex.printStackTrace();
+	}
+	return null;
+    }
+
+    public void doFilter(ServletRequest request, ServletResponse response,
+                         FilterChain chain)
+        throws java.io.IOException, ServletException {
+        Entity peer = getEntity((HttpServletRequest)request);
+        //RtmlEntity peer = getEntityId((HttpServletRequest)request);
+        System.out.print("Filtering request from id = " + peer );
+        System.out.println(" for resource of " + eid.toString()); 
+	HashMap localMap = (HashMap)map.clone();
+	localMap.put("peer", peer);
+        NegotiationContext context = new NegotiationContext(map);
+        boolean success = false;
+        
+        if(peer == null) {
+            printMessage(response.getWriter(), 
+                         "<h1>Authentication required!</h1>");
+            return;
+        }
+        try {
+            String host = request.getRemoteHost();
+            RMINegotiator agent = new RMINegotiator(context);
+            //context.getGraph().addObserver(observer);
+            Negotiator peerAgent = getClient(host, peer.toString(), agent);
+            for(int i = 0; i < 4 && peerAgent == null; i++) {
+                peerAgent = getClient(host, peer.toString(), agent);
+            }
+	    Entity realPeer = peerAgent.getSelf();
+            TrustTarget primary = new TrustTarget(eid, target, realPeer); 
+            agent.setPeer(peerAgent);
+            agent.setRoot(primary);
+            success = agent.negotiate();
+        }
+        catch(Exception ex) {
+            ex.printStackTrace();
+        }
+        // allow or block this servlet
+        if(!success) {
+            printMessage(response.getWriter(),
+                         "<h1>Sorry, page cannot be displayed!</h1>");
+            return;
+        }
+        chain.doFilter(request, response);
+    }
+
+
+    protected void printExpiration(PrintWriter out) {
+        out.print("<meta http-equiv=\"Expires\" content=\"");
+        out.print(formatter.format(cal.getTime()));
+        out.println("\">");
+    }
+
+    protected void printMessage(PrintWriter out, String body) {
+        out.print("<html>\n\t<head>\n\t");
+        printExpiration(out);
+        out.println("\t</head>\n\t<body>");
+            out.println(body);
+            out.println("\t</body>\n</html>");
+            out.flush();
+            return;
+
+    }
+}
+
+
+
+
Index: fedd/abac-src/ttg/demo/src/CVS/Entries
===================================================================
--- fedd/abac-src/ttg/demo/src/CVS/Entries	(revision 8780cbecc6db2bdc2b9ef31eec6e5209ad3bff15)
+++ fedd/abac-src/ttg/demo/src/CVS/Entries	(revision 8780cbecc6db2bdc2b9ef31eec6e5209ad3bff15)
@@ -0,0 +1,4 @@
+/AccessMediator.java/1.9/Fri Jul 18 19:39:23 2003//
+/DiscoveryServlet.java/1.5/Fri Jul 18 23:43:17 2003//
+/SessionServlet.java/1.7/Mon Jul 21 20:41:34 2003//
+D
Index: fedd/abac-src/ttg/demo/src/CVS/Repository
===================================================================
--- fedd/abac-src/ttg/demo/src/CVS/Repository	(revision 8780cbecc6db2bdc2b9ef31eec6e5209ad3bff15)
+++ fedd/abac-src/ttg/demo/src/CVS/Repository	(revision 8780cbecc6db2bdc2b9ef31eec6e5209ad3bff15)
@@ -0,0 +1,1 @@
+ttg/demo/src
Index: fedd/abac-src/ttg/demo/src/CVS/Root
===================================================================
--- fedd/abac-src/ttg/demo/src/CVS/Root	(revision 8780cbecc6db2bdc2b9ef31eec6e5209ad3bff15)
+++ fedd/abac-src/ttg/demo/src/CVS/Root	(revision 8780cbecc6db2bdc2b9ef31eec6e5209ad3bff15)
@@ -0,0 +1,1 @@
+:ext:jjacobs@src.columbia.sparta.com/cvs/abac
Index: fedd/abac-src/ttg/demo/src/DiscoveryServlet.java
===================================================================
--- fedd/abac-src/ttg/demo/src/DiscoveryServlet.java	(revision 8780cbecc6db2bdc2b9ef31eec6e5209ad3bff15)
+++ fedd/abac-src/ttg/demo/src/DiscoveryServlet.java	(revision 8780cbecc6db2bdc2b9ef31eec6e5209ad3bff15)
@@ -0,0 +1,86 @@
+import java.io.*;
+import java.util.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+import com.nailabs.abac.credential.*;
+
+/**
+ *
+ */
+public class DiscoveryServlet extends HttpServlet {
+    public DiscoveryService service = null;
+    public String fileName = new String();
+
+    /** Initialize the discovery service with a credential store */
+    public void init(ServletConfig config) throws ServletException {
+	fileName = config.getInitParameter("PolicyFile");
+	service = new DiscoveryService(fileName);
+    }
+
+    /**
+     * 
+     */
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+        throws ServletException, java.io.IOException {
+	String issuer = req.getParameter("Issuer");
+	String defines = req.getParameter("Defines");
+	String subject = req.getParameter("Subject");
+	PrintWriter out = resp.getWriter();
+	String results[] = new String[0];
+
+	if(issuer != null) {
+	    //out.println("<!--issuer = " + issuer + "-->");
+	    results = service.getCredentialsIssuedBy(issuer);
+	}
+	if(defines != null) {
+	    StringTokenizer st = new StringTokenizer(defines, ".");
+	    if(st.countTokens() < 2) {
+		out.close();
+		return;
+	    }
+	    String entity = st.nextToken();
+	    String role = st.nextToken();
+	    //out.print("<!--entity=");
+	    //out.print(entity);
+	    //out.print(", role=");
+	    //out.print(role);
+	    //out.println("-->");
+	    results = service.findCredentialsDefiningRole(entity, role);
+	}
+	if(subject != null) {
+	    results = service.findCredentialsBySubject(subject);
+	}
+
+	for(int i = 0; i < results.length; i++) {
+	    out.println(results[i]);
+	}
+	out.close();
+    }
+
+    /**
+     * Post handles adding a credential domain since the XML file is 
+     * potentially quite large.
+     */
+    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
+        throws ServletException, java.io.IOException {
+	String xml = req.getParameter("xml");
+	String reset = req.getParameter("reset");
+	PrintWriter out = resp.getWriter();
+
+	out.println("<html><head><title>Adding RTML Credential</title");
+	out.println("<body>");
+	if(xml == null) {
+	    service = new DiscoveryService(fileName);
+	    System.out.println("Intializing service with file " + fileName);
+	    out.println("Deleting credential domain . . .");
+	} else {
+	    service.addCredentialDomain(xml);
+	    out.println("Added credential domain . . .");
+	}
+	//out.println("<br>reset = " + reset);
+	out.println("</body>");
+	out.println("</html>");
+	out.println(xml);
+	out.close();
+    }
+}
Index: fedd/abac-src/ttg/demo/src/SessionServlet.java
===================================================================
--- fedd/abac-src/ttg/demo/src/SessionServlet.java	(revision 8780cbecc6db2bdc2b9ef31eec6e5209ad3bff15)
+++ fedd/abac-src/ttg/demo/src/SessionServlet.java	(revision 8780cbecc6db2bdc2b9ef31eec6e5209ad3bff15)
@@ -0,0 +1,161 @@
+import com.nailabs.abac.process.*;
+import com.nailabs.abac.trust.*;
+import com.nailabs.abac.test.*;
+import edu.stanford.peer.rbtm.credential.*;
+import java.io.*;
+import java.util.*;
+import java.rmi.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+
+public class SessionServlet extends HttpServlet {
+    /** the key name for looking up the entity id of a session */
+    static final String KEY = "EntityID";
+    /** the hash code for the RTML Entity's signature */
+    static final String HASH = "HashID";
+    /** default action */
+    String action = null;
+    /** 
+     * sets up a new session and invalidates the old session if it exists 
+     * @param request the incoming HTTP request 
+     * @param id the new entity id for the new session ot be created
+     */
+    public void setSessionId(HttpServletRequest request,String id,String hash)
+    {
+        HttpSession session = request.getSession(true);
+
+        // end current session and create a new one
+        if(!session.isNew()) {  
+            try {
+                session.invalidate();
+                session = request.getSession(true);
+            }
+            catch(Exception ex) {
+                ex.printStackTrace();
+            }
+        }
+        session.setAttribute(KEY, id);
+        session.setAttribute(HASH, hash);
+    }
+
+    /** public accessor method for the entity id of the current http session */
+    public String getSessionId(HttpServletRequest request) {
+        HttpSession session = request.getSession(false);
+        
+        if(session == null) {
+            return null;
+        }
+        return (String)session.getAttribute(KEY);
+    }
+
+    public String getSessionIdHash(HttpServletRequest request) {
+        HttpSession session = request.getSession(false);
+        
+        if(session == null) {
+            return null;
+        }
+        return (String)session.getAttribute(HASH);	
+    }
+
+    protected void printStatus(PrintWriter out, String uid, String action) {
+        out.println("<html><head><title>ABAC Status</title></head>");
+        out.println("<body>");
+        out.println("  <center>");
+        out.println("<h1>User Authentication</h1>");
+        out.print("    <p>You are currently logged in as: ");
+        out.print(uid);
+        out.println("</p>\n");
+        out.println("    <form action=\"" + action + "\" method=\"post\">");
+        out.println("  <input type=\"submit\" value=\"Logout\">");
+        out.println("  <input type=\"hidden\" name=\"end\" value=\"end\">");
+        out.println("    </form>");
+        out.println("  </center>");
+        out.println("<body>");
+        out.println("</html>");        
+    }
+
+    protected void printLogin(PrintWriter out, String action)
+    {
+        final String TR = "<tr>", END_TR = "</tr>";
+        final String TD = "<td>", END_TD = "</td>";
+        final String TH = "<th>", END_TH = "</th>";
+
+        out.println("<html><head><title>ABAC Login</title></head>");
+        out.println("<body>");
+        out.println("  <center>");
+        out.println("<h1>User Authentication</h1>");
+        out.println("</p>\n");
+        out.println("\t<form action=\"" + action +"\" method=\"post\">");
+        out.println("\t<table>");
+        out.println(TR + TH);
+        out.println("User Name:" + END_TH + TD);
+        out.println("\t\t<input type=\"text\" name =\"username\" />");
+        out.println(END_TD + END_TR);
+        out.println(TR + TH);
+        out.println("Password:" + END_TH + TD);
+        out.println("\t\t<input type=\"password\" name=\"password\" />");
+        out.println(END_TD + END_TR);
+        out.println("\t</table>");
+        out.println("<p> <INPUT type=\"submit\" value=\"Send\">");
+        out.println("<INPUT type=\"reset\"> </p>");
+        out.println("\t</form>");
+        out.println("  </center>");
+        out.println("<body>");
+        out.println("</html>");
+    }
+    
+    public void init(ServletConfig config) throws ServletException {
+        config.getServletContext().log("Loading authentication servlet.");
+    }
+
+
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+        throws ServletException, java.io.IOException {
+        PrintWriter out = resp.getWriter();
+        String uid = getSessionId(req);
+        String action = req.getRequestURI().toString();
+
+        if(uid == null) { 
+            printLogin(out, action);
+        } else {
+            printStatus(out, uid, action);
+        }
+        out.close();
+    }
+
+
+    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
+        throws ServletException, java.io.IOException {
+        PrintWriter out = resp.getWriter();
+        String uid = req.getParameter("username");
+        String pass = req.getParameter("password");
+        String action = req.getRequestURI().toString();
+
+        System.out.println("Authenticating for user " + uid);
+
+        System.out.print("Parameters =  ");
+        Enumeration e = req.getParameterNames();
+        while(e.hasMoreElements()) {
+            System.out.print(e.nextElement());
+            System.out.print(", ");
+        }
+        System.out.println("");
+
+        if(req.getParameter("end") != null) {
+            try {
+                req.getSession().invalidate();
+            }
+            catch(Exception ex) {
+                ex.printStackTrace();
+            }
+            printLogin(out, action);
+        } else {
+            setSessionId(req, uid, "FakeHashKey" + uid);
+            printStatus(out, getSessionId(req), action);
+        }
+        out.close();
+    }
+}
+
+
