summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOComponentRequestHandler.java
blob: 9f799879c701fdc0a1927ed8441e5b2e6ed2a44f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.web;


/**
* An implementation of WORequestHandler that dispatches Component actions.
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 905 $
*/
public class WOComponentRequestHandler
	extends WORequestHandler
{
    public WOResponse handleRequest( WORequest aRequest )
    {
        WOApplication application = aRequest.application();
        WOContext context = WOContext.contextWithRequest( aRequest );
        WOResponse response = null;
            
        // no concurrent access is allowed to a user's session:
        // a user/browser/machine with multiple requests will have to wait
        synchronized ( aRequest.request.getSession() )
        {
            try
            {
                application.awake();
                
                WOSession session = null;
    /*            
                // the NeXT way
                String sessionID = aRequest.sessionID();
                if ( sessionID != null )
                {
                    session = application.restoreSessionWithID( sessionID, context );
                    if ( session == null )
                    {
                        response = application.handleSessionRestorationErrorInContext( context );
                    }
                }
                else
                {
                    session = application.createSessionForRequest( aRequest );
                    if ( session == null )
                    {
                        response = application.handleSessionCreationErrorInContext( context );
                    }
                    else
                    {
                        session.setContext( context );
                    }
                }
    */
                // the servlet way
                String sessionID = aRequest.sessionID();
                if ( sessionID != null )
                {
                    session = application.restoreSessionWithID( sessionID, context );
                }
    
                if ( session == null )
                {
                    session = application.createSessionForRequest( aRequest );
                    if ( session == null )
                    {
                        response = application.handleSessionCreationErrorInContext( context );
                    }
                    else
                    {
                        session.setContext( context );
                    }
                }
                
                context.setSession( session );
                
                session.awake();
                
                if ( response == null )
                {
                    
                    WOComponent page;
                    String contextID = aRequest.contextID(); 
                    
                    if ( contextID != null )
                    {
                        page = session.restorePageForContextID( contextID );
                    }
                    else
                    {
                        page = application.pageWithName( aRequest.pageName(), context ); 
                    }
                    
                    if ( page == null )
                    {
                        //FIXME: should we call this a restoration error, or do we
                        // allow a bookmark with an expired context id to resume gracefully?
                        page = application.pageWithName( aRequest.pageName(), context ); 
                        //!response = application.handlePageRestorationErrorInContext( context );
                    }
                    //!else
                    {
                        context.pushElement( page ); //? needed?
                        page.ensureAwakeInContext( context ); //? shouldn't this be in WOApplication?
    
                        // only take values from request if there are values in the request
                        System.out.println("should I takeValuesFromRequest ? " + ( aRequest.formValueKeys().count() > 0 ));
                        if ( aRequest.formValueKeys().count() > 0 )
                        {
                            application.takeValuesFromRequest( aRequest, context );
                        }
                        
                        // only invoke action if there is a sender id to invoke
                        WOActionResults result;
                        System.out.println("senderID: " + aRequest.senderID());
                        if ( aRequest.senderID() != null )
                        {
                            result = application.invokeAction( aRequest, context );
                        }
                        else
                        {
                            result = null;
                        }
                        
                        if ( result == null || result == page ) // same page is returned
                        {
                            result = page;
                            
                            // generate response
                            response = new WOResponse();
                            application.appendToResponse( response, context );
                            page.sleep();
                        }
                        else // different page is returned
                        if ( result instanceof WOComponent )
                        {
                            page.sleep();
                            page = (WOComponent) result;
                            page.ensureAwakeInContext( context );
                            context.popElement(); // removes page
                            context.pushElement( page );
                            
                            // generate response
                            response = new WOResponse();
                            application.appendToResponse( response, context );
                            page.sleep();
                        }
                        else // WOResponse was returnd
                        {
                            response = (WOResponse) result;
                        }
                        
                        context.popElement();
                        session.sleep();
                        session.savePage( page );
                        session.setContext( null );
                        application.saveSessionForContext( context );
                    }
                }
            }
            catch ( Throwable t )
            {
                response = application.handleException( t, context );
            }
    
            application.sleep();
        }
        return response;
    }
}

/*
 * $Log$
 * Revision 1.2  2006/02/19 01:44:02  cgruber
 * Add xmlrpc files
 * Remove jclark and replace with dom4j and javax.xml.sax stuff
 * Re-work dependencies and imports so it all compiles.
 *
 * Revision 1.1  2006/02/16 13:22:22  cgruber
 * Check in all sources in eclipse-friendly maven-enabled packages.
 *
 * Revision 1.8  2003/08/07 00:15:15  chochos
 * general cleanup (mostly removing unused imports)
 *
 * Revision 1.7  2003/01/17 20:34:57  mpowers
 * Better handling for components and parents in the context's element stack.
 *
 * Revision 1.6  2003/01/15 19:50:49  mpowers
 * Fixed issues with WOSession and Serializable.
 * Can now persist sessions between classloaders (hot swap of class impls).
 *
 * Revision 1.4  2003/01/14 15:51:09  mpowers
 * No longer calling takeValues or invokeAction if there are no request params
 *
 * Revision 1.3  2003/01/13 22:24:34  mpowers
 * Request-response cycle is working with session and page persistence.
 *
 * Revision 1.1  2003/01/09 16:13:59  mpowers
 * Implemented WOComponentRequestHandler:
 * Bringing the request-response cycle more into conformance.
 *
 * Revision 1.2  2002/12/17 14:57:44  mpowers
 * Minor corrections to WORequests's parsing, and updated javadocs.
 *
 * Revision 1.1.1.1  2000/12/21 15:53:19  mpowers
 * Contributing wotonomy.
 *
 * Revision 1.2  2000/12/20 16:25:50  michael
 * Added log to all files.
 *
 *
 */