1 28 29 package com.caucho.server.security; 30 31 import com.caucho.server.connection.CauchoRequest; 32 import com.caucho.server.connection.CauchoResponse; 33 34 import javax.servlet.FilterChain ; 35 import javax.servlet.ServletContext ; 36 import javax.servlet.ServletException ; 37 import javax.servlet.ServletRequest ; 38 import javax.servlet.ServletResponse ; 39 import java.io.IOException ; 40 import java.util.ArrayList ; 41 import java.util.HashMap ; 42 43 public class SecurityFilterChain implements FilterChain { 44 private FilterChain _next; 45 46 private ServletContext _webApp; 47 48 private AbstractConstraint []_constraints; 49 private HashMap <String ,AbstractConstraint[]> _methodMap; 50 51 SecurityFilterChain(FilterChain next) 52 { 53 _next = next; 54 } 55 56 public void setWebApp(ServletContext app) 57 { 58 _webApp = app; 59 } 60 61 public void setConstraints(ArrayList <AbstractConstraint> constraints) 62 { 63 _constraints = new AbstractConstraint[constraints.size()]; 64 65 constraints.toArray(_constraints); 66 } 67 68 public void setMethodMap(HashMap <String ,AbstractConstraint[]> methodMap) 69 { 70 _methodMap = methodMap; 71 } 72 73 public void destroy() 74 { 75 } 76 77 public void doFilter(ServletRequest request, ServletResponse response) 78 throws ServletException , IOException 79 { 80 CauchoRequest req = (CauchoRequest) request; 83 CauchoResponse res = (CauchoResponse) response; 84 85 AbstractConstraint []constraints = null; 86 if (_methodMap != null) 87 constraints = _methodMap.get(req.getMethod()); 88 89 if (constraints == null) 90 constraints = _constraints; 91 92 boolean isPrivateCache = false; 93 if (constraints != null) { 94 for (int i = 0; i < constraints.length; i++) { 96 AbstractConstraint constraint = constraints[i]; 97 98 if (constraint.needsAuthentication()) 99 continue; 100 101 if (! constraint.isAuthorized(req, res, _webApp)) 102 return; 103 104 if (constraint.isPrivateCache()) 105 isPrivateCache = true; 106 } 107 108 boolean hasAuth = false; 109 for (int i = 0; i < constraints.length; i++) { 110 AbstractConstraint constraint = constraints[i]; 111 112 if (! constraint.needsAuthentication()) 113 continue; 114 115 if (! hasAuth) { 116 hasAuth = true; 117 if (! req.authenticate()) 118 return; 119 } 120 121 if (! constraint.isAuthorized(req, res, _webApp)) 122 return; 123 124 if (constraint.isPrivateCache()) 125 isPrivateCache = true; 126 } 127 } 128 129 if (isPrivateCache) 130 res.setPrivateCache(true); 131 132 _next.doFilter(request, response); 133 } 134 } 135 | Popular Tags |