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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
|
/*
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;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* A pure java implementation of WOContext.
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 905 $
*/
public class WOContext
{
private WOSession session;
private WORequest request;
private WOResponse response;
private List elementStack;
private boolean isInForm;
private boolean isDistributionEnabled;
private static final String EMPTY = "";
private static final String SEP = ".";
private static final String ZERO = "0";
private static final String HTTP = "http://";
private static final String HTTPS = "https://";
// package access
WOComponent page;
WOComponent component;
StringBuffer elementID;
private static volatile int contextCounter = 0;
private String contextID;
/**
* Default constructor performs necessary initialization.
* Subclassers should override the static factory method below.
*/
public WOContext ()
{
contextID = null;
elementID = new StringBuffer();
elementStack = new LinkedList();
}
/**
* Preferred constructor performs necessary initialization.
* Subclassers should override this method.
*/
public WOContext (WORequest aRequest)
{
this();
request = aRequest;
response = new WOResponse();
}
/**
* Simply calls the preferred constructor.
* Included for compatibility.
*/
public static WOContext contextWithRequest (WORequest aRequest)
{
String id = Integer.toString( contextCounter++ );
WOContext result = new WOContext( aRequest );
result.contextID = id;
return result;
}
/**
* Returns the context id.
*/
public String contextID ()
{
return contextID;
}
/**
* Returns the sender id, or null if it doesn't exist.
*/
public String senderID ()
{
return request.senderID();
}
/**
* Returns the element id, or null if it doesn't exist.
*/
public String elementID ()
{
return elementID.toString();
}
/**
* Returns this context's application, or null if it doesn't exist.
*/
public WOApplication application ()
{
return request.application();
}
/**
* Returns whether a session has been created for the associated request.
*/
public boolean hasSession ()
{
return ( session != null );
}
/**
* Returns this context's session, creating one if it doesn't exist.
*/
public WOSession session ()
{
if ( session == null )
{
// so far we can't figure out how the direct action handler can avoid creating a session
throw new RuntimeException( "WOContext.session: Lazy instantiation not yet implemented." );
/*
session = application().restoreSessionWithID(
request.sessionID(), this );
if ( session == null )
{
session = application().createSessionForRequest( request );
session.setContext( this );
}
*/
}
return session;
}
/**
* Package access only.
*/
void setSession( WOSession aSession )
{
session = aSession;
}
/**
* Returns this context's request.
*/
public WORequest request ()
{
return request;
}
/**
* Returns this context's response.
*/
public WOResponse response ()
{
return response;
}
/**
* Returns the current page.
*/
public WOComponent page ()
{
return (WOComponent) elementStack.get( elementStack.size()-1 );
}
/**
* Returns the current component.
*/
public WOComponent component ()
{
Object o;
Iterator i = elementStack.iterator();
while ( i.hasNext() )
{
o = i.next();
if ( o instanceof WOComponent ) return (WOComponent) o;
}
return null;
}
/**
* Returns the current component's parent.
*/
WOComponent parent ()
{
Object o;
Iterator i = elementStack.iterator();
if ( i.hasNext() )
{
// skip current component
o = i.next();
}
while ( i.hasNext() )
{
o = i.next();
if ( o instanceof WOComponent )
{
return (WOComponent) o;
}
}
return null;
}
/**
* Pushes an element onto the stack.
* Package access only.
*/
void pushElement( WOElement aComponent )
{
elementStack.add( 0, aComponent );
}
/**
* Pops an element off the stack.
* Package access only.
*/
WOElement popElement()
{
return (WOElement) elementStack.remove(0);
}
/**
* Returns whether the current context is in a form.
*/
public boolean isInForm ()
{
return isInForm;
}
/**
* Sets whether the current context is in a WOForm.
*/
public void setInForm (boolean inForm)
{
isInForm = inForm;
}
/**
* Appends the specified string to the end of the element id.
* For example, if the element id is "0.1", sending "Test"
* changes the element id to "0.1.Test".
*/
public void appendElementIDComponent (String aString)
{
if ( elementID.length() > 0 )
{
elementID.append( SEP );
}
elementID.append( aString );
}
/**
* Appends a zero to the element id to represent the first
* new child component. For example, if the element id is "0.1",
* calling this changes the element id to "0.1.0".
*/
public void appendZeroElementIDComponent ()
{
if ( elementID.length() > 0 )
{
elementID.append( SEP );
}
elementID.append( ZERO );
}
/**
* Increments the last component of the element id.
* For example, if the element id is "0.1", calling this
* changes the element id to "0.2".
*/
public void incrementLastElementIDComponent ()
{
String last;
String id = elementID.toString();
int index = id.lastIndexOf( SEP );
if ( index == -1 )
{
last = id;
}
else
{
last = id.substring( index + 1 );
}
deleteLastElementIDComponent();
try
{
appendElementIDComponent(
Integer.toString( Integer.parseInt( last ) + 1 ) );
}
catch ( Exception exc )
{
System.err.println( "Error parsing id: " + last );
appendZeroElementIDComponent();
}
//System.out.println( "WOContext: " + elementID );
}
/**
* Deletes the last component of the element id.
* For example, if the element id is "0.1", callling this
* changes the element id to "0".
*/
public void deleteLastElementIDComponent ()
{
int index = elementID.toString().lastIndexOf( SEP );
if ( index == -1 )
{
elementID.setLength( 0 );
}
else
{
elementID.setLength( index );
}
}
/**
* Deletes all components of the element id.
* This makes the element id an empty string.
*/
public void deleteAllElementIDComponents ()
{
elementID.setLength( 0 );
}
/**
* Returns a URL for the named action with query parameters
* as specified by the dictionary.
*/
public String directActionURLForActionNamed (
String anActionName, Map aQueryDict)
{
StringBuffer query = new StringBuffer();
try
{
String key;
Iterator i = aQueryDict.keySet().iterator();
while ( i.hasNext() )
{
key = i.next().toString();
query.append( URI.encode( key,
URI.allowed_within_query ) );
query.append( '=' );
query.append( URI.encode( aQueryDict.get( key ).toString(),
URI.allowed_within_query ) );
if ( i.hasNext() )
{
query.append( '&' );
}
}
}
catch ( IOException exc )
{
// report error
System.err.println(
"directActionURLForActionNamed: " + anActionName + " : " + aQueryDict );
System.err.println( exc );
// delete query string
query = new StringBuffer();
}
return urlWithRequestHandlerKey(
WOApplication.directActionRequestHandlerKey(),
anActionName, query.toString() );
}
/**
* Returns the complete URL for the current component action.
*/
public String componentActionURL ()
{
StringBuffer buffer = new StringBuffer();
buffer.append( request().applicationName() );
buffer.append( '/' );
buffer.append( WOApplication.application().componentRequestHandlerKey() );
buffer.append( '/' );
buffer.append( page().name() );
buffer.append( '/' );
buffer.append( contextID );
buffer.append( '/' );
buffer.append( elementID );
return buffer.toString();
}
/**
* Returns a URL relative to the servlet for the specified
* request handler, action, and query string.
*/
public String urlWithRequestHandlerKey (
String aRequestHandlerKey, String aPath, String aQueryString)
{
StringBuffer buffer = new StringBuffer();
buffer.append( request().applicationName() );
buffer.append( '/' );
buffer.append( aRequestHandlerKey );
buffer.append( '/' );
buffer.append( aPath );
if ( aQueryString != null && aQueryString.trim().length() > 0 )
{
buffer.append( '?' );
buffer.append( aQueryString );
}
return buffer.toString();
}
/**
* Returns the complete URL for the specified request handler,
* path, and query string. isSecure determines the protocol:
* http or https. port is appended to the protocol, if zero,
* the port is omitted from the URL.
*/
public String completeURLWithRequestHandlerKey (
String aRequestHandlerKey, String aPath, String aQueryString,
boolean isSecure, int port)
{
StringBuffer buffer = new StringBuffer();
if ( isSecure )
{
buffer.append( HTTPS );
}
else
{
buffer.append( HTTP );
}
buffer.append( request.applicationHost() );
if ( port != 0 && port != 80 )
{
buffer.append( ':' );
buffer.append( Integer.toString( port ) );
}
buffer.append( urlWithRequestHandlerKey(
aRequestHandlerKey, aPath, aQueryString ) );
return buffer.toString();
}
/**
* Sets whether distribution is enabled.
*/
public void setDistributionEnabled (boolean distributionEnabled)
{
isDistributionEnabled = distributionEnabled;
}
/**
* Returns whether distribution is enabled.
*/
public boolean isDistributionEnabled ()
{
return isDistributionEnabled;
}
/**
* This method is not included in the WOContext specification.
* This implementation returns "" since only cookie ids are
* currently supported.
*/
String urlSessionPrefix ()
{
return EMPTY; // "/" + sessionid;
}
/**
* Returns the relative URL for the current page.
*/
String url ()
{
throw new RuntimeException( "Not implemented yet." );
}
public String toString()
{
return "[WOContext@"+Integer.toHexString(System.identityHashCode(this))
+":id=" + contextID +":page=" + page + ":component=" + component + ":element=" + elementID + "]";
}
}
/*
* $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.19 2003/08/07 00:15:15 chochos
* general cleanup (mostly removing unused imports)
*
* Revision 1.18 2003/02/21 16:40:23 mpowers
* Now reading port and smtp host from system properties.
* Implemented WOApplication.main.
*
* Revision 1.17 2003/01/24 20:12:54 mpowers
* Better parent determination.
*
* Revision 1.16 2003/01/21 22:27:02 mpowers
* Corrected context id usage.
* Implemented backtracking.
*
* Revision 1.15 2003/01/17 20:58:19 mpowers
* Fixed up WOHyperlink.
*
* Revision 1.14 2003/01/17 20:34:57 mpowers
* Better handling for components and parents in the context's element stack.
*
* Revision 1.13 2003/01/14 19:48:36 mpowers
* - fixes to property synchronization
* - forms now pass takeValuesFromRequest to children
* - fixes to action invocation
* - now supporting multipleSubmit in forms
*
* Revision 1.12 2003/01/13 22:24:44 mpowers
* Request-response cycle is working with session and page persistence.
*
* Revision 1.11 2003/01/10 20:17:41 mpowers
* Component action urls are now working.
*
* Revision 1.8 2003/01/09 21:16:48 mpowers
* Bringing request-response cycle more into conformance.
*
* Revision 1.7 2003/01/09 16:13:59 mpowers
* Implemented WOComponentRequestHandler:
* Bringing the request-response cycle more into conformance.
*
* Revision 1.4 2002/12/20 22:56:33 mpowers
* Reimplemented the template parsing again.
* Nested components are now correctly parsed.
* ElementID numbering is now working.
*
* Revision 1.3 2002/12/18 14:12:38 mpowers
* Support for differentiated request handlers.
* Support url generation for WOContext and WORequest.
*
* Revision 1.2 2002/12/17 14:57:42 mpowers
* Minor corrections to WORequests's parsing, and updated javadocs.
*
* Revision 1.1.1.1 2000/12/21 15:53:04 mpowers
* Contributing wotonomy.
*
* Revision 1.3 2000/12/20 16:25:49 michael
* Added log to all files.
*
*
*/
|