View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Blacksmith, Inc.
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.web;
20  
21  
22  /***
23  * An implementation of WORequestHandler that dispatches Component actions.
24  *
25  * @author michael@mpowers.net
26  * @author $Author: cgruber $
27  * @version $Revision: 905 $
28  */
29  public class WOComponentRequestHandler
30  	extends WORequestHandler
31  {
32      public WOResponse handleRequest( WORequest aRequest )
33      {
34          WOApplication application = aRequest.application();
35          WOContext context = WOContext.contextWithRequest( aRequest );
36          WOResponse response = null;
37              
38          // no concurrent access is allowed to a user's session:
39          // a user/browser/machine with multiple requests will have to wait
40          synchronized ( aRequest.request.getSession() )
41          {
42              try
43              {
44                  application.awake();
45                  
46                  WOSession session = null;
47      /*            
48                  // the NeXT way
49                  String sessionID = aRequest.sessionID();
50                  if ( sessionID != null )
51                  {
52                      session = application.restoreSessionWithID( sessionID, context );
53                      if ( session == null )
54                      {
55                          response = application.handleSessionRestorationErrorInContext( context );
56                      }
57                  }
58                  else
59                  {
60                      session = application.createSessionForRequest( aRequest );
61                      if ( session == null )
62                      {
63                          response = application.handleSessionCreationErrorInContext( context );
64                      }
65                      else
66                      {
67                          session.setContext( context );
68                      }
69                  }
70      */
71                  // the servlet way
72                  String sessionID = aRequest.sessionID();
73                  if ( sessionID != null )
74                  {
75                      session = application.restoreSessionWithID( sessionID, context );
76                  }
77      
78                  if ( session == null )
79                  {
80                      session = application.createSessionForRequest( aRequest );
81                      if ( session == null )
82                      {
83                          response = application.handleSessionCreationErrorInContext( context );
84                      }
85                      else
86                      {
87                          session.setContext( context );
88                      }
89                  }
90                  
91                  context.setSession( session );
92                  
93                  session.awake();
94                  
95                  if ( response == null )
96                  {
97                      
98                      WOComponent page;
99                      String contextID = aRequest.contextID(); 
100                     
101                     if ( contextID != null )
102                     {
103                         page = session.restorePageForContextID( contextID );
104                     }
105                     else
106                     {
107                         page = application.pageWithName( aRequest.pageName(), context ); 
108                     }
109                     
110                     if ( page == null )
111                     {
112                         //FIXME: should we call this a restoration error, or do we
113                         // allow a bookmark with an expired context id to resume gracefully?
114                         page = application.pageWithName( aRequest.pageName(), context ); 
115                         //!response = application.handlePageRestorationErrorInContext( context );
116                     }
117                     //!else
118                     {
119                         context.pushElement( page ); //? needed?
120                         page.ensureAwakeInContext( context ); //? shouldn't this be in WOApplication?
121     
122                         // only take values from request if there are values in the request
123                         System.out.println("should I takeValuesFromRequest ? " + ( aRequest.formValueKeys().count() > 0 ));
124                         if ( aRequest.formValueKeys().count() > 0 )
125                         {
126                             application.takeValuesFromRequest( aRequest, context );
127                         }
128                         
129                         // only invoke action if there is a sender id to invoke
130                         WOActionResults result;
131                         System.out.println("senderID: " + aRequest.senderID());
132                         if ( aRequest.senderID() != null )
133                         {
134                             result = application.invokeAction( aRequest, context );
135                         }
136                         else
137                         {
138                             result = null;
139                         }
140                         
141                         if ( result == null || result == page ) // same page is returned
142                         {
143                             result = page;
144                             
145                             // generate response
146                             response = new WOResponse();
147                             application.appendToResponse( response, context );
148                             page.sleep();
149                         }
150                         else // different page is returned
151                         if ( result instanceof WOComponent )
152                         {
153                             page.sleep();
154                             page = (WOComponent) result;
155                             page.ensureAwakeInContext( context );
156                             context.popElement(); // removes page
157                             context.pushElement( page );
158                             
159                             // generate response
160                             response = new WOResponse();
161                             application.appendToResponse( response, context );
162                             page.sleep();
163                         }
164                         else // WOResponse was returnd
165                         {
166                             response = (WOResponse) result;
167                         }
168                         
169                         context.popElement();
170                         session.sleep();
171                         session.savePage( page );
172                         session.setContext( null );
173                         application.saveSessionForContext( context );
174                     }
175                 }
176             }
177             catch ( Throwable t )
178             {
179                 response = application.handleException( t, context );
180             }
181     
182             application.sleep();
183         }
184         return response;
185     }
186 }
187 
188 /*
189  * $Log$
190  * Revision 1.2  2006/02/19 01:44:02  cgruber
191  * Add xmlrpc files
192  * Remove jclark and replace with dom4j and javax.xml.sax stuff
193  * Re-work dependencies and imports so it all compiles.
194  *
195  * Revision 1.1  2006/02/16 13:22:22  cgruber
196  * Check in all sources in eclipse-friendly maven-enabled packages.
197  *
198  * Revision 1.8  2003/08/07 00:15:15  chochos
199  * general cleanup (mostly removing unused imports)
200  *
201  * Revision 1.7  2003/01/17 20:34:57  mpowers
202  * Better handling for components and parents in the context's element stack.
203  *
204  * Revision 1.6  2003/01/15 19:50:49  mpowers
205  * Fixed issues with WOSession and Serializable.
206  * Can now persist sessions between classloaders (hot swap of class impls).
207  *
208  * Revision 1.4  2003/01/14 15:51:09  mpowers
209  * No longer calling takeValues or invokeAction if there are no request params
210  *
211  * Revision 1.3  2003/01/13 22:24:34  mpowers
212  * Request-response cycle is working with session and page persistence.
213  *
214  * Revision 1.1  2003/01/09 16:13:59  mpowers
215  * Implemented WOComponentRequestHandler:
216  * Bringing the request-response cycle more into conformance.
217  *
218  * Revision 1.2  2002/12/17 14:57:44  mpowers
219  * Minor corrections to WORequests's parsing, and updated javadocs.
220  *
221  * Revision 1.1.1.1  2000/12/21 15:53:19  mpowers
222  * Contributing wotonomy.
223  *
224  * Revision 1.2  2000/12/20 16:25:50  michael
225  * Added log to all files.
226  *
227  *
228  */
229