01: package com.icesoft.faces.webapp.http.core;
02:
03: import com.icesoft.faces.context.View;
04: import com.icesoft.faces.webapp.command.Command;
05: import com.icesoft.faces.webapp.command.SessionExpired;
06: import com.icesoft.faces.webapp.http.common.Request;
07: import com.icesoft.faces.webapp.http.common.Response;
08: import com.icesoft.faces.webapp.http.common.ResponseHandler;
09: import com.icesoft.faces.webapp.http.common.Server;
10: import com.icesoft.faces.webapp.http.common.standard.FixedXMLContentHandler;
11: import com.icesoft.faces.webapp.http.servlet.SessionDispatcher;
12:
13: import javax.faces.FacesException;
14: import java.io.IOException;
15: import java.io.Writer;
16: import java.util.Map;
17:
18: public class ViewBoundServer implements Server {
19: private final static Command SessionExpired = new SessionExpired();
20: private Map views;
21: private SessionDispatcher.Listener.Monitor sessionMonitor;
22: private Server server;
23:
24: public ViewBoundServer(Server server,
25: SessionDispatcher.Listener.Monitor sessionMonitor, Map views) {
26: this .server = server;
27: this .sessionMonitor = sessionMonitor;
28: this .views = views;
29: }
30:
31: public synchronized void service(Request request) throws Exception {
32: String viewNumber = request.getParameter("ice.view.active");
33: if (viewNumber == null) {
34: request.respondWith(new ResponseHandler() {
35: public void respond(Response response) throws Exception {
36: response.setStatus(500);
37: response.writeBody().write(
38: "Cannot match view instance. 'ice.view.active' parameter is missing."
39: .getBytes());
40: }
41: });
42: } else {
43: View view = (View) views.get(viewNumber);
44: if (view == null) {
45: //todo: revisit this -- maybe the session was not created yet
46: request.respondWith(new FixedXMLContentHandler() {
47: public void writeTo(Writer writer)
48: throws IOException {
49: SessionExpired.serializeTo(writer);
50: }
51: });
52: } else {
53: try {
54: view.updateOnXMLHttpRequest(request);
55: sessionMonitor.touchSession();
56: server.service(request);
57: } catch (FacesException e) {
58: //"workaround" for exceptions zealously captured & wrapped by the JSF implementations
59: Throwable nestedException = e.getCause();
60: if (nestedException == null
61: || nestedException instanceof Error) {
62: throw e;
63: } else {
64: throw (Exception) nestedException;
65: }
66: } finally {
67: view.release();
68: }
69: }
70: }
71: }
72:
73: public void shutdown() {
74: server.shutdown();
75: }
76: }
|