1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.web;
20
21 import net.wotonomy.foundation.NSArray;
22
23 /***
24 * An implementation of WORequestHandler that dispatches
25 * DirectActions. <br><br>
26 *
27 * See WODirectAction for the rules for parsing a request.
28 * In short, className defaults to "DirectAction", and the
29 * action defaults to "default". The action class is expected
30 * to have a constructor that takes a WORequest parameter.
31 *
32 * @author michael@mpowers.net
33 * @author $Author: cgruber $
34 * @version $Revision: 905 $
35 */
36 public class WODirectActionRequestHandler
37 extends WORequestHandler
38 {
39 public WOResponse handleRequest (WORequest aRequest)
40 {
41 WOResponse response = null;
42
43
44
45
46 synchronized ( aRequest.request.getSession() )
47 {
48 WOApplication application = aRequest.application();
49 WOContext context = WOContext.contextWithRequest( aRequest );
50
51 String className = "DirectAction";
52 String actionName = "default";
53 NSArray path = aRequest.requestHandlerPathArray();
54 if ( path.count() > 0 )
55 {
56 className = path.objectAtIndex( 0 ).toString();
57 if ( path.count() > 1 )
58 {
59 actionName = path.objectAtIndex( path.count() - 1 ).toString();
60 }
61 if ( path.count() > 2 )
62 {
63 for ( int i = 1; i < path.count()-1; i++ )
64 {
65 className = className + "." +
66 path.objectAtIndex( i ).toString();
67 }
68 }
69 }
70
71
72
73 WOSession session = null;
74
75 String sessionID = aRequest.sessionID();
76 if ( sessionID != null )
77 {
78 session = application.restoreSessionWithID( sessionID, context );
79 }
80
81 if ( session == null )
82 {
83 session = application.createSessionForRequest( aRequest );
84 if ( session == null )
85 {
86 response = application.handleSessionCreationErrorInContext( context );
87 }
88 else
89 {
90 session.setContext( context );
91 }
92 }
93
94 context.setSession( session );
95
96 application.awake();
97 session.awake();
98
99 try
100 {
101
102 if ( response == null )
103 {
104 Class c = null;
105 c = application.getLocalClass( className );
106 if ( ( c == null ) && ( path.count() == 1 ) )
107 {
108 actionName = className;
109 className = "DirectAction";
110 c = application.getLocalClass( className );
111 }
112 if ( c == null )
113 {
114 throw new RuntimeException(
115 "Could not find class named \"" + className
116 + "\": " );
117 }
118 java.lang.reflect.Constructor ctor =
119 c.getConstructor( new Class[] { WORequest.class } );
120
121 WODirectAction action = (WODirectAction)
122 ctor.newInstance( new Object[] { aRequest } );
123 action.context = context;
124
125 WOActionResults result = action.performActionNamed( actionName );
126 if ( result instanceof WOComponent )
127 {
128
129 context.pushElement( (WOComponent) result );
130 ((WOComponent)result).ensureAwakeInContext( context );
131
132 }
133 response = result.generateResponse();
134 if ( result instanceof WOComponent )
135 {
136
137 ((WOComponent)result).sleep();
138 }
139 }
140 }
141 catch ( NoSuchMethodException exc )
142 {
143 WOResponse error = new WOResponse();
144 exc.printStackTrace();
145 error.setStatus( 404 );
146 error.appendContentString(
147 "Could not find request constructor for class named \""
148 + className + "\": " );
149 response = error;
150 }
151 catch ( Exception exc )
152 {
153 WOResponse error = new WOResponse();
154 error.setStatus( 500 );
155 if ( exc.getMessage() != null )
156 {
157 error.appendContentString( exc.getMessage() );
158 exc.printStackTrace();
159 }
160 else
161 {
162 error.appendContentString( exc.toString() );
163 exc.printStackTrace();
164 }
165 response = error;
166 }
167
168 session.sleep();
169 session.setContext( null );
170 application.saveSessionForContext( context );
171 application.sleep();
172 }
173 return response;
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