1 22 package org.jboss.test.security.servlets; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.io.InputStream ; 27 import java.io.ObjectInputStream ; 28 import java.security.Principal ; 29 import java.util.HashSet ; 30 import java.lang.reflect.Method ; 31 import javax.servlet.http.HttpServlet ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.servlet.ServletException ; 35 import javax.naming.InitialContext ; 36 import javax.rmi.PortableRemoteObject ; 37 import org.jboss.test.security.interfaces.RunAsServiceRemoteHome; 38 import org.jboss.test.security.interfaces.RunAsServiceRemote; 39 import org.jboss.test.security.interfaces.CallerInfo; 40 import org.jboss.security.SimplePrincipal; 41 42 48 public class RunAsServlet extends HttpServlet 49 { 50 protected void doRequest(HttpServletRequest request, HttpServletResponse response) 51 throws ServletException , IOException 52 { 53 Principal caller = request.getUserPrincipal(); 54 55 InputStream is = request.getInputStream(); 56 ObjectInputStream ois = new ObjectInputStream (is); 57 try 58 { 59 String method = (String ) ois.readObject(); 60 if( method == null ) 61 throw new ServletException ("No method parameter given"); 62 CallerInfo info = (CallerInfo) ois.readObject(); 63 InitialContext ctx = new InitialContext (); 64 Object obj = ctx.lookup("jacc/RunAs"); 65 obj = PortableRemoteObject.narrow(obj, RunAsServiceRemoteHome.class); 66 RunAsServiceRemoteHome home = (RunAsServiceRemoteHome) obj; 67 System.out.println("Found RunAsServiceRemoteHome"); 68 RunAsServiceRemote bean = home.create(); 69 System.out.println("Created RunAsServiceRemote"); 70 Class [] sig = {CallerInfo.class}; 71 Method methodCall = bean.getClass().getMethod(method, sig); 72 Object [] args = {info}; 73 methodCall.invoke(bean, args); 74 bean.remove(); 75 } 76 catch(Exception e) 77 { 78 e.printStackTrace(); 79 throw new ServletException (e); 80 } 81 82 PrintWriter pw = response.getWriter(); 83 pw.write("<html>\n"); 84 pw.write("<br>Saw UserPrincipal: "+caller); 85 pw.write("<br>PathInfo: "+request.getPathInfo()); 86 pw.write("</html>\n"); 87 } 88 89 protected void doPost(HttpServletRequest request, HttpServletResponse response) 90 throws ServletException , IOException 91 { 92 doRequest(request, response); 93 } 94 95 protected void doGet(HttpServletRequest request, HttpServletResponse response) 96 throws ServletException , IOException 97 { 98 doRequest(request, response); 99 } 100 } 101 | Popular Tags |