1 29 30 package nextapp.echo2.webrender; 31 32 import java.io.IOException ; 33 34 import javax.servlet.ServletException ; 35 import javax.servlet.http.HttpServlet ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 39 import nextapp.echo2.webrender.service.CoreServices; 40 import nextapp.echo2.webrender.service.DebugPaneService; 41 42 45 public abstract class WebRenderServlet extends HttpServlet { 46 47 51 private static final ThreadLocal activeConnection = new InheritableThreadLocal (); 52 53 58 public static final boolean DISABLE_CACHING = false; 59 60 63 public static final String SERVICE_ID_PARAMETER = "serviceId"; 64 65 70 public static final String SERVICE_ID_DEFAULT = "Echo.Default"; 71 72 77 public static final String SERVICE_ID_NEW_INSTANCE = "Echo.NewInstance"; 78 79 85 public static final String SERVICE_ID_SESSION_EXPIRED = "Echo.Expired"; 86 87 90 private static MultipartRequestWrapper multipartRequestWrapper; 91 92 private static final long startupTime = System.currentTimeMillis(); 93 94 97 private static final ServiceRegistry services = new ServiceRegistry(); 98 99 static { 100 CoreServices.install(services); 101 services.add(DebugPaneService.INSTANCE); 102 } 103 104 111 public static interface MultipartRequestWrapper { 112 113 122 public HttpServletRequest getWrappedRequest(HttpServletRequest request) 123 throws IOException , ServletException ; 124 } 125 126 132 public static final Connection getActiveConnection() { 133 return (Connection) activeConnection.get(); 134 } 135 136 142 public static MultipartRequestWrapper getMultipartRequestWrapper() { 143 return multipartRequestWrapper; 144 } 145 146 160 public static final void setMultipartRequestWrapper(MultipartRequestWrapper multipartRequestWrapper) { 161 if (WebRenderServlet.multipartRequestWrapper == null) { 162 WebRenderServlet.multipartRequestWrapper = multipartRequestWrapper; 163 } else { 164 if (multipartRequestWrapper == null || 165 !WebRenderServlet.multipartRequestWrapper.getClass().getName().equals( 166 multipartRequestWrapper.getClass().getName())) { 167 throw new IllegalStateException ("MultipartRequestWrapper already set."); 168 } 169 } 170 } 171 172 177 public final void doGet(HttpServletRequest request, HttpServletResponse response) 178 throws IOException , ServletException { 179 process(request, response); 180 } 181 182 187 public final void doPost(HttpServletRequest request, HttpServletResponse response) 188 throws IOException , ServletException { 189 process(request, response); 190 } 191 192 198 private static Service getService(UserInstance userInstance, String id) { 199 Service service; 200 201 service = services.get(id); 202 if (id == null) { 203 if (userInstance == null) { 204 id = SERVICE_ID_NEW_INSTANCE; 205 } else { 206 id = SERVICE_ID_DEFAULT; 207 } 208 } else { 209 if (userInstance == null) { 210 id = SERVICE_ID_SESSION_EXPIRED; 211 } 212 } 213 214 service = services.get(id); 215 216 if (service == null) { 217 if (SERVICE_ID_DEFAULT.equals(id)) { 218 throw new RuntimeException ("Service not registered: SERVICE_ID_DEFAULT"); 219 } else if (SERVICE_ID_NEW_INSTANCE.equals(id)) { 220 throw new RuntimeException ("Service not registered: SERVICE_ID_NEW_INSTANCE"); 221 } else if (SERVICE_ID_SESSION_EXPIRED.equals(id)) { 222 throw new RuntimeException ("Service not registered: SERVICE_ID_SESSION_EXPIRED"); 223 } 224 } 225 226 return service; 227 } 228 229 234 public static ServiceRegistry getServiceRegistry() { 235 return services; 236 } 237 238 244 protected void process(HttpServletRequest request, HttpServletResponse response) 245 throws IOException , ServletException { 246 Connection conn = null; 247 try { 248 conn = new Connection(this, request, response); 249 activeConnection.set(conn); 250 String serviceId = request.getParameter(SERVICE_ID_PARAMETER); 251 Service service = getService(conn.getUserInstance(), serviceId); 252 if (service == null) { 253 throw new ServletException ("Service id \"" + serviceId + "\" not registered."); 254 } 255 int version = service.getVersion(); 256 257 if ((!DISABLE_CACHING) && version != Service.DO_NOT_CACHE) { 259 response.setHeader("Cache-Control", "max-age=3600"); 265 response.setDateHeader("Expires", System.currentTimeMillis() + (86400000)); 266 response.setDateHeader("Last-Modified", startupTime); 267 } else { 268 response.setHeader("Pragma", "no-cache"); 269 response.setHeader("Cache-Control", "no-store"); 270 response.setHeader("Expires", "0"); 271 } 272 273 service.service(conn); 274 275 } catch (ServletException ex) { 276 if (conn != null) { 277 conn.disposeUserInstance(); 278 } 279 throw(ex); 280 } catch (IOException ex) { 281 if (conn != null) { 282 conn.disposeUserInstance(); 283 } 284 throw(ex); 285 } catch (RuntimeException ex) { 286 if (conn != null) { 287 conn.disposeUserInstance(); 288 } 289 throw(ex); 290 } finally { 291 activeConnection.set(null); 292 } 293 } 294 } 295 | Popular Tags |