1 16 package org.mortbay.jetty.servlet; 17 18 import java.io.IOException ; 19 import java.util.HashMap ; 20 import java.util.Locale ; 21 22 import javax.servlet.ServletContext ; 23 import javax.servlet.http.HttpServletResponse ; 24 25 import org.mortbay.http.HttpContext; 26 import org.mortbay.http.HttpException; 27 import org.mortbay.http.HttpRequest; 28 import org.mortbay.http.HttpResponse; 29 30 31 37 public class ServletHttpContext extends HttpContext 38 { 39 private HashMap _localeEncodingMap = new HashMap (); 40 private ServletHandler _servletHandler=null; 41 42 43 45 public ServletHttpContext() 46 { 47 super(); 48 } 49 50 51 54 public ServletContext getServletContext() 55 { 56 ServletHandler shandler=getServletHandler(); 57 if (shandler!=null) 58 return shandler.getServletContext(); 59 throw new IllegalStateException (); 60 } 61 62 63 68 public synchronized ServletHandler getServletHandler() 69 { 70 if (_servletHandler==null) 71 _servletHandler=(ServletHandler) getHandler(ServletHandler.class); 72 if (_servletHandler==null) 73 { 74 _servletHandler=new ServletHandler(); 75 addHandler(_servletHandler); 76 } 77 return _servletHandler; 78 } 79 80 81 91 public synchronized ServletHolder addServlet(String pathSpec, 92 String className) 93 throws ClassNotFoundException , 94 InstantiationException , 95 IllegalAccessException 96 { 97 return addServlet(className,pathSpec,className); 98 } 99 100 101 111 public synchronized ServletHolder addServlet(String name, 112 String pathSpec, 113 String className) 114 throws ClassNotFoundException , 115 InstantiationException , 116 IllegalAccessException 117 { 118 return getServletHandler().addServlet(name,pathSpec,className,null); 119 } 120 121 122 protected boolean jSecurityCheck(String pathInContext, 123 HttpRequest request, 124 HttpResponse response) 125 throws IOException 126 { 127 if (getAuthenticator() instanceof FormAuthenticator && 128 pathInContext.endsWith(FormAuthenticator.__J_SECURITY_CHECK) && 129 getAuthenticator().authenticate(getRealm(), 130 pathInContext, 131 request, 132 response)==null) 133 return false; 134 return true; 135 } 136 137 138 public boolean checkSecurityConstraints(String pathInContext, 139 HttpRequest request, 140 HttpResponse response) 141 throws HttpException, IOException 142 { 143 if (!super.checkSecurityConstraints(pathInContext,request,response) || 144 ! jSecurityCheck(pathInContext,request,response)) 145 return false; 146 147 return true; 148 } 149 150 151 public void addLocaleEncoding(String locale,String encoding) 152 { 153 _localeEncodingMap.put(locale, encoding); 154 } 155 156 157 166 public String getLocaleEncoding(Locale locale) 167 { 168 String encoding = (String )_localeEncodingMap.get(locale.toString()); 169 if (encoding==null) 170 encoding = (String )_localeEncodingMap.get(locale.getLanguage()); 171 return encoding; 172 } 173 174 175 public String toString() 176 { 177 return "Servlet"+super.toString(); 178 } 179 180 181 184 public void sendError(HttpResponse response,int code,String msg) 185 throws IOException 186 { 187 Object wrapper = response.getWrapper(); 188 if (wrapper!=null && wrapper instanceof HttpServletResponse ) 189 ((HttpServletResponse )wrapper).sendError(code,msg); 190 else 191 super.sendError(response,code,msg); 192 } 193 194 195 public void destroy() 196 { 197 super.destroy(); 198 if (_localeEncodingMap!=null) 199 _localeEncodingMap.clear(); 200 _localeEncodingMap=null; 201 } 202 203 204 205 208 public Object enterContextScope(HttpRequest request, HttpResponse response) 209 { 210 ServletHttpRequest srequest = (ServletHttpRequest) request.getWrapper(); 212 ServletHttpResponse sresponse = (ServletHttpResponse) response.getWrapper(); 213 if (srequest==null) 214 { 215 srequest = new ServletHttpRequest(getServletHandler(),null,request); 217 sresponse = new ServletHttpResponse(srequest,response); 218 request.setWrapper(srequest); 219 response.setWrapper(sresponse); 220 } 221 222 return super.enterContextScope(request,response); 223 } 224 225 226 229 protected void doStop() throws Exception 230 { 231 super.doStop(); 232 _servletHandler=null; 233 } 234 } 235 | Popular Tags |