]> err.no Git - sope/blob - sope-appserver/NGObjWeb/WOContext.m
added an ivar to WOComponent, minor cleanups
[sope] / sope-appserver / NGObjWeb / WOContext.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include <NGObjWeb/WOContext.h>
23 #include "NSObject+WO.h"
24 #include "WOComponent+private.h"
25 #include "WOContext+private.h"
26 #include "WOApplication+private.h"
27 #include <NGObjWeb/WOApplication.h>
28 #include <NGObjWeb/WORequest.h>
29 #include <NGObjWeb/WOResponse.h>
30 #include <NGObjWeb/WOSession.h>
31 #import <EOControl/EONull.h>
32 #include "WOElementID.h"
33 #include "common.h"
34 #include <time.h>
35
36
37 @interface WOContext(Privates5)
38 - (NSArray *)_componentStack;
39 @end
40
41 @interface WOComponent(Cursors)
42 - (void)pushCursor:(id)_obj;
43 - (id)popCursor;
44 - (id)cursor;
45 @end
46
47 static Class WOAppClass = Nil;
48
49 @implementation WOContext
50
51 + (int)version {
52   return 7;
53 }
54
55 static Class MutableStrClass    = Nil;
56 static int  contextCount        = 0;
57 static int  logComponents       = -1;
58 static int  relativeURLs        = -1;
59 static BOOL debugOn             = NO;
60 static int  debugCursor         = -1;
61 static BOOL debugComponentAwake = NO;
62 static BOOL testNSURLs          = NO;
63 static BOOL newCURLStyle        = NO;
64 static NSString *WOApplicationSuffix = nil;
65
66 + (void)initialize {
67   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
68   static BOOL didInitialize = NO;
69   if (didInitialize) return;
70
71   if (WOAppClass == Nil)
72     WOAppClass = [WOApplication class];
73   if (MutableStrClass == Nil)
74     MutableStrClass = [NSMutableString class];
75   
76   didInitialize = YES;
77     
78   logComponents = [[ud objectForKey:@"WOLogComponents"] boolValue] ? 1 : 0;
79   relativeURLs  = [[ud objectForKey:@"WOUseRelativeURLs"] boolValue]? 1 : 0;
80   debugCursor         = [ud boolForKey:@"WODebugCursor"] ? 1 : 0;
81   debugComponentAwake = [ud boolForKey:@"WODebugComponentAwake"];
82   WOApplicationSuffix = [[ud stringForKey:@"WOApplicationSuffix"] copy];
83 }
84
85 + (id)contextWithRequest:(WORequest *)_request {
86   return [[(WOContext *)[self alloc] initWithRequest:_request] autorelease];
87 }
88
89 - (id)initWithRequest:(WORequest *)_request {
90   if ((self = [super init])) {
91     unsigned char buf[24];
92     self->qpJoin = @"&";
93     
94     sprintf(buf, "%03x%08x%08x", ++contextCount, (int)time(NULL), (int)self);
95     self->ctxId = [[NSString alloc] initWithCString:buf];
96     
97     self->elementID = [[WOElementID alloc] init];
98     self->awakeComponents = [[NSMutableSet alloc] initWithCapacity:64];
99     
100     self->request  = [_request retain];
101     self->response = [[WOResponse responseWithRequest:_request] retain];
102   }
103   return self;
104 }
105
106 + (id)context {
107   return [[[self alloc] init] autorelease];
108 }
109 - (id)init {
110   return [self initWithRequest:nil];
111 }
112
113 /* components */
114
115 - (void)_addAwakeComponent:(WOComponent *)_component {
116   if (_component == nil)
117     return;
118
119   if ([self->awakeComponents containsObject:_component])
120     return;
121
122   /* wake up component */
123   if (debugComponentAwake)
124     [self logWithFormat:@"mark component awake: %@", _component];
125   
126   [self->awakeComponents addObject:_component];
127 }
128
129 - (void)_awakeComponent:(WOComponent *)_component {
130   if (_component == nil)
131     return;
132
133   if ([self->awakeComponents containsObject:_component])
134     return;
135
136   /* wake up component */
137   if (debugComponentAwake)
138     [self logWithFormat:@"awake component: %@", _component];
139     
140   [_component _awakeWithContext:self];
141
142   [self _addAwakeComponent:_component];
143   
144   if (debugComponentAwake)
145     [self logWithFormat:@"woke up component: %@", _component];
146 }
147
148 - (void)sleepComponents {
149   NSEnumerator *e;
150   WOComponent  *component;
151   BOOL sendSleepToPage;
152
153   if (debugComponentAwake) {
154     [self logWithFormat:@"sleep %d components ...", 
155             [self->awakeComponents count]];
156   }
157   
158   sendSleepToPage = YES;
159   e = [self->awakeComponents objectEnumerator];
160   while ((component = [e nextObject])) {
161     if (debugComponentAwake)
162       [self logWithFormat:@"  sleep component: %@", component];
163     [component _sleepWithContext:self];
164     if (component == self->page) sendSleepToPage = NO;
165   }
166   if (sendSleepToPage && (self->page != nil)) {
167     if (debugComponentAwake)
168       [self logWithFormat:@"  sleep page: %@", self->page];
169     [self->page _sleepWithContext:self];
170   }
171   
172   if (debugComponentAwake) {
173     [self logWithFormat:@"done sleep %d components.", 
174             [self->awakeComponents count]];
175   }
176   [self->awakeComponents removeAllObjects];
177 }
178
179 #if WITH_DEALLOC_OBSERVERS
180 - (void)addDeallocObserver:(id)_observer {
181   if (_observer == NULL) return;
182   
183   /* check array */
184   if (self->deallocObservers == NULL) {
185     self->deallocObservers        = calloc(8, sizeof(id));
186     self->deallocObserverCount    = 0;
187     self->deallocObserverCapacity = 8;
188   }
189
190   /* check capacity */
191   if (self->deallocObserverCapacity == self->deallocObserverCount) {
192     /* need to increase array */
193     id *newa;
194     
195     newa = calloc(self->deallocObserverCapacity * 2, sizeof(id));
196     memcpy(newa, self->deallocObservers, 
197            sizeof(id) * self->deallocObserverCount);
198     free(self->deallocObservers);
199     self->deallocObservers = newa;
200     self->deallocObserverCapacity *= 2;
201   }
202   
203   /* register */
204   self->deallocObservers[self->deallocObserverCount] = _observer;
205   self->deallocObserverCount++;
206 }
207 - (void)removeDeallocObserver:(id)_observer {
208   /* the observer currently will only grow (this should be OK for WOContext) */
209   register int i;
210   if (_observer == NULL) return;
211   
212   for (i = self->deallocObserverCount - 1; i >= 0; i++) {
213     if ((self->deallocObservers[i]) == _observer)
214       self->deallocObservers[i] = NULL;
215   }
216 }
217 #endif
218
219 - (void)dealloc {
220   [self sleepComponents];
221   
222 #if WITH_DEALLOC_OBSERVERS
223   if (self->deallocObservers) {
224     register int i;
225     
226 #if DEBUG
227     printf("%s: dealloc observer capacity: %i\n",
228            __PRETTY_FUNCTION__, self->deallocObserverCapacity);
229 #endif
230     
231     /* GC!! process in reverse order ... */
232     for (i = self->deallocObserverCount - 1; i >= 0; i++)
233       [self->deallocObservers[i] _objectWillDealloc:self];
234     
235     free(self->deallocObservers);
236     self->deallocObservers = NULL;
237   }
238 #endif
239
240   [self->activeUser            release];
241   [self->rootURL               release];
242   [self->objectPermissionCache release];
243   [self->traversalStack        release];
244   [self->clientObject          release];
245   [self->objectDispatcher      release];
246   [self->soRequestType         release];
247   [self->pathInfo              release];
248   
249   [[NSNotificationCenter defaultCenter]
250                          postNotificationName:@"WOContextWillDeallocate"
251                          object:self->ctxId];
252   
253   { /* release component stack */
254     int i;
255     for (i = (self->componentStackCount - 1); i >= 0; i--) {
256       [self->componentStack[i] release]; self->componentStack[i] = nil;
257       [self->contentStack[i]   release]; self->contentStack[i]   = nil;
258     }
259   }
260   
261   [self->urlPrefix         release];
262   [self->elementID         release];
263   [self->reqElementID      release];
264   [self->activeFormElement release];
265   [self->page              release];
266   [self->awakeComponents   release];
267   [self->appURL            release];
268   [self->baseURL           release];
269   [self->session           release];
270   [self->variables         release];
271   [self->request           release];
272   [self->response          release];
273   [self->ctxId             release];
274   [super dealloc];
275 }
276
277 - (void)setSession:(WOSession *)_session {
278   ASSIGN(self->session, _session);
279 }
280
281 - (WOSession *)session {
282   // in WO4 -session creates a new session if none is associated
283   
284   if (self->session == nil) {
285     [[self application] _initializeSessionInContext:self];
286     
287     if (self->session == nil)
288       [self logWithFormat:@"%s: missing session for context ..",
289               __PRETTY_FUNCTION__];
290   }
291   
292   return self->session;
293 }
294
295 - (NSString *)contextID {
296   NSAssert(self->ctxId, @"context without id !");
297 #if 0
298   // in WO4 -contextID returns nil if there is no associated session
299   return self->session ? self->ctxId : nil;
300 #else
301   /*
302     IMHO the above isn't true, otherwise session cannot be automagically
303     generated!
304     
305     TODO: well, we might want to generate component URLs which work without
306           a session - at least in theory the ID tree should be stable even
307           without a session (and if proper uids are used for dynamic content).
308           eg this would be quite useful for SOPE.
309   */
310   return self->ctxId;
311 #endif
312 }
313
314 - (WORequest *)request {
315   return self->request;
316 }
317 - (WOResponse *)response {
318   return self->response;
319 }
320
321 - (BOOL)hasSession {
322   return (self->session != nil) ? YES : NO;
323 }
324
325 - (BOOL)savePageRequired {
326   return self->savePageRequired;
327 }
328
329 /* cursors */
330
331 - (void)pushCursor:(id)_obj {
332   if (debugCursor == -1) {
333     debugCursor = [[NSUserDefaults standardUserDefaults]
334                                    boolForKey:@"WODebugCursor"]
335       ? 1 : 0;
336   }
337   
338   if (debugCursor) [self logWithFormat:@"enter cursor: %@", _obj];
339   [[self component] pushCursor:_obj];
340 }
341
342 - (id)popCursor {
343   if (debugCursor) [self logWithFormat:@"leave cursor ..."];
344   return [[self component] popCursor];
345 }
346
347 - (id)cursor {
348   return [(id <WOPageGenerationContext>)[self component] cursor];
349 }
350
351 /* components */
352
353 - (WOComponent *)component {
354   return (self->componentStackCount > 0)
355     ? self->componentStack[self->componentStackCount - 1]
356     : nil;
357 }
358
359 - (void)setPage:(WOComponent *)_page {
360   [_page ensureAwakeInContext:self];
361   ASSIGN(self->page, _page);
362 }
363 - (WOComponent *)page {
364   return self->page;
365 }
366
367 void WOContext_enterComponent
368 (WOContext *self, WOComponent *_component, WOElement *_content)
369 {
370   WOComponent *parent = nil;
371 #if DEBUG
372   NSCAssert(_component, @"missing component to enter ...");
373 #endif
374   
375   if (logComponents) {
376     [self->application logWithFormat:@"enter component %@ (content=%@) ..",
377                          [_component name], _content];
378   }
379   
380   parent = self->componentStackCount > 0
381     ? self->componentStack[self->componentStackCount - 1]
382     : nil;
383   
384   NSCAssert2(self->componentStackCount < NGObjWeb_MAX_COMPONENT_NESTING_DEPTH,
385              @"exceeded maximum component nesting depth (%i):\n%@",
386              NGObjWeb_MAX_COMPONENT_NESTING_DEPTH,
387              [self _componentStack]);
388   self->componentStack[(int)self->componentStackCount] = [_component retain];
389   self->contentStack[(int)self->componentStackCount]   = [_content   retain];
390   self->componentStackCount++;
391   
392   [self _awakeComponent:_component];
393   
394   if (parent) {
395     if ([_component synchronizesVariablesWithBindings])
396       WOComponent_syncFromParent(_component, parent);
397   }
398 }
399 void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
400   WOComponent *parent = nil;
401
402   BEGIN_PROFILE;
403
404   parent = (self->componentStackCount > 1)
405     ? self->componentStack[self->componentStackCount - 2]
406     : nil;
407   
408   if (parent) {
409     if ([_component synchronizesVariablesWithBindings])
410       WOComponent_syncToParent(_component, parent);
411   }
412
413   PROFILE_CHECKPOINT("after sync");
414   
415   /* remove last object */
416   self->componentStackCount--;
417   NSCAssert(self->componentStackCount >= 0,
418             @"tried to pop component from empty component stack !");
419   [self->componentStack[(int)self->componentStackCount] release];
420   self->componentStack[(int)self->componentStackCount] = nil;
421   [self->contentStack[(int)self->componentStackCount] release];
422   self->contentStack[(int)self->componentStackCount] = nil;
423   
424   if (logComponents)
425     [self->application logWithFormat:@"left component %@.", [_component name]];
426
427   END_PROFILE;
428 }
429
430 - (void)enterComponent:(WOComponent *)_comp content:(WOElement *)_content {
431   WOContext_enterComponent(self, _comp, _content);
432 }
433 - (void)leaveComponent:(WOComponent *)_component {
434   BEGIN_PROFILE;
435   WOContext_leaveComponent(self, _component);
436   END_PROFILE;
437 }
438
439 - (WOComponent *)parentComponent {
440   return (self->componentStackCount > 1)
441     ? self->componentStack[(int)self->componentStackCount - 2]
442     : nil;
443 }
444
445 - (WODynamicElement *)componentContent {
446   return (self->componentStackCount > 0)
447     ? self->contentStack[(int)self->componentStackCount - 1]
448     : nil;
449 }
450
451 - (unsigned)componentStackCount {
452   return self->componentStackCount;
453 }
454 - (NSArray *)_componentStack {
455   return [NSArray arrayWithObjects:self->componentStack
456                   count:self->componentStackCount];
457 }
458
459 /* URLs */
460
461 - (NSURL *)serverURL {
462   WORequest *rq;
463   NSString  *serverURL;
464   NSURL     *url;
465   NSString  *host;
466     
467   if ((rq = [self request]) == nil) {
468     [self logWithFormat:@"missing request in -baseURL call .."];
469     return nil;
470   }
471   
472   if ((serverURL = [rq headerForKey:@"x-webobjects-server-url"]) == nil) {
473     if ((host = [rq headerForKey:@"host"]))
474       serverURL = [@"http://" stringByAppendingString:host];
475   }
476   else {
477     // TODO: fix that (host is also broken for example with SOUP)
478     /* sometimes the port is broken in the server URL ... */
479     if ([serverURL hasSuffix:@":0"]) { // bad bad bad
480       if ((host = [rq headerForKey:@"host"])) {
481         NSString *scheme;
482         scheme = [serverURL hasPrefix:@"https://"] ? @"https://" : @"http://";
483         serverURL = [scheme stringByAppendingString:host];
484       }
485     }
486   }
487   
488   if ([serverURL length] == 0) {
489     [self logWithFormat:
490             @"ERROR: could not find x-webobjects-server-url header !"];
491     return nil;
492   }
493   
494   if ((url = [NSURL URLWithString:serverURL]) == nil) {
495     [self logWithFormat:@"could not construct NSURL from string '%@'",
496             serverURL];
497     return nil;
498   }
499   return url;
500 }
501
502 - (NSURL *)baseURL {
503   WORequest *rq;
504   NSURL     *serverURL;
505
506   if (self->baseURL) 
507     return self->baseURL;
508     
509   if ((rq = [self request]) == nil) {
510     [self logWithFormat:@"missing request in -baseURL call .."];
511     return nil;
512   }
513     
514   serverURL = [self serverURL];
515   self->baseURL =
516     [[NSURL URLWithString:[rq uri] relativeToURL:serverURL] retain];
517     
518   if (self->baseURL == nil) {
519     [self logWithFormat:
520             @"could not construct NSURL for uri '%@' and base '%@' ...",
521             [rq uri], serverURL];
522   }
523   return self->baseURL;
524 }
525
526 - (NSURL *)applicationURL {
527   if (self->appURL == nil) {
528     NSString *s;
529
530     s = [self->request adaptorPrefix];
531     if ([s length] > 0) {
532       s = [NSString stringWithFormat:@"%@/%@/", 
533                       s, [self->request applicationName]];
534     }
535     else
536       s = [[self->request applicationName] stringByAppendingString:@"/"];
537     
538     self->appURL =
539       [[NSURL URLWithString:s relativeToURL:[self serverURL]] retain];
540   }
541   return self->appURL;
542 }
543 - (NSURL *)urlForKey:(NSString *)_key {
544   _key = [_key stringByAppendingString:@"/"];
545   return [NSURL URLWithString:_key relativeToURL:[self applicationURL]];
546 }
547
548 /* forms */
549
550 - (void)setInForm:(BOOL)_form {
551   self->inForm = _form;
552 }
553 - (BOOL)isInForm {
554   return self->inForm;
555 }
556
557 - (void)addActiveFormElement:(WOElement *)_formElement {
558   if (self->activeFormElement) {
559     [[self component] debugWithFormat:@"active form element already set !"];
560     return;
561   }
562   
563   ASSIGN(self->activeFormElement, _formElement);
564   [self setRequestSenderID:[self elementID]];
565 }
566 - (WOElement *)activeFormElement {
567   return self->activeFormElement;
568 }
569
570 /* context variables (transient) */
571
572 - (void)setObject:(id)_obj forKey:(NSString *)_key {
573   if (self->variables == nil) {
574     self->variables =
575       [[NSMutableDictionary allocWithZone:[self zone]]
576                             initWithCapacity:16];
577   }
578
579   if (_obj)
580     [self->variables setObject:_obj forKey:_key];
581   else
582     [self->variables removeObjectForKey:_key];
583 }
584 - (id)objectForKey:(NSString *)_key {
585   return [self->variables objectForKey:_key];
586 }
587 - (void)removeObjectForKey:(NSString *)_key {
588   [self->variables removeObjectForKey:_key];
589 }
590
591 - (NSDictionary *)variableDictionary {
592   return self->variables;
593 }
594
595 - (void)takeValue:(id)_value forKey:(NSString *)_key {
596   if (WOSetKVCValueUsingMethod(self, _key, _value))
597     // method is used
598     return;
599   else if (WOGetKVCGetMethod(self, _key) == NULL) {
600     if (_value == nil)
601       _value = [EONull null];
602     
603     if (self->variables == nil) {
604       self->variables =
605         [[NSMutableDictionary allocWithZone:[self zone]]
606                               initWithCapacity:16];
607     }
608     [self->variables setObject:_value forKey:_key];
609     return;
610   }
611   else {
612     // only a 'get' method is defined for _key !
613     [self handleTakeValue:_value forUnboundKey:_key];
614   }
615 }
616 - (id)valueForKey:(NSString *)_key {
617   id value;
618   
619   if ((value = WOGetKVCValueUsingMethod(self, _key)))
620     return value;
621   value = [self->variables objectForKey:_key];
622   return value;
623 }
624
625 /* NSCopying */
626
627 - (id)copyWithZone:(NSZone *)_zone {
628   return [self retain];
629 }
630
631 /* description */
632
633 - (NSString *)description {
634   NSString *sid = nil;
635   WOApplication *app = [self application];
636
637   if ([self hasSession])
638     sid = [[self session] sessionID];
639   
640   return [NSString stringWithFormat:
641                      @"<0x%08X[%@]: %@ app=%@ sn=%@ eid=%@ rqeid=%@>",
642                      (unsigned)self, NSStringFromClass([self class]),
643                      [self  contextID],
644                      [app name],
645                      sid ? sid : @"none",
646                      [self  elementID],
647                      [self  senderID]];
648 }
649
650 @end /* WOContext */
651
652 @implementation WOContext(ElementIDs)
653
654 - (NSString *)elementID {
655   return [self->elementID elementID];
656 }
657 - (void)appendElementIDComponent:(NSString *)_eid {
658   [self->elementID appendElementIDComponent:_eid];
659 }
660 - (void)appendZeroElementIDComponent {
661   [self->elementID appendZeroElementIDComponent];
662 }
663 - (void)deleteAllElementIDComponents {
664   [self->elementID deleteAllElementIDComponents];
665 }
666 - (void)deleteLastElementIDComponent {
667   [self->elementID deleteLastElementIDComponent];
668 }
669 - (void)incrementLastElementIDComponent {
670   [self->elementID incrementLastElementIDComponent];
671 }
672 - (void)appendIntElementIDComponent:(int)_eid {
673   [self->elementID appendIntElementIDComponent:_eid];
674 }
675
676 /* the following can be later moved to WOElementID */
677
678 - (id)currentElementID {
679   return [self->reqElementID currentElementID];
680 }
681 - (id)consumeElementID {
682   return [self->reqElementID consumeElementID];
683 }
684
685 @end /* WOContext(ElementIDs) */
686
687 @implementation WOContext(URLs)
688
689 - (void)_generateCompleteURLs {
690   /* described in Apple TIL article 70101 */
691 }
692
693 - (NSString *)queryStringFromDictionary:(NSDictionary *)_queryDict {
694   NSEnumerator    *keys;
695   NSString        *key;
696   BOOL            isFirst;
697   NSMutableString *qs;
698   
699   qs   = [MutableStrClass stringWithCapacity:256];
700   keys = [_queryDict keyEnumerator];
701   for (isFirst = YES; (key = [keys nextObject]); ) {
702     NSString *value;
703     
704     if (isFirst)
705       isFirst = NO;
706     else
707       [qs appendString:self->qpJoin];
708     
709     value = [[_queryDict objectForKey:key] stringValue];
710     
711     key   = [key   stringByEscapingURL];
712     value = [value stringByEscapingURL];
713     
714     [qs appendString:key];
715     if (value) {
716       [qs appendString:@"="];
717       [qs appendString:value];
718     }
719   }
720   
721   return qs;
722 }
723
724 - (NSString *)directActionURLForActionNamed:(NSString *)_actionName
725   queryDictionary:(NSDictionary *)_queryDict
726 {
727   NSMutableString *url;
728   NSString        *qs;
729
730   url = [MutableStrClass stringWithCapacity:256];
731   
732   if (!testNSURLs)
733      [url appendString:@"/"];
734   
735   [url appendString:_actionName];
736   
737   /* add query parameters */
738   
739   qs = [self queryStringFromDictionary:_queryDict];
740     
741   return [self urlWithRequestHandlerKey:
742                  [WOAppClass directActionRequestHandlerKey]
743                path:url queryString:qs];
744 }
745
746 - (NSString *)componentActionURL {
747   // TODO: add a -cComponentActionURL 
748   //       (without NSString for use with appendContentCString:)
749   // Profiling:
750   //   26% -urlWithRequestHandler...
751   //   21% -elementID (was 40% !! :-)
752   //   ~20% mutable string ops
753   if (newCURLStyle) {
754     NSMutableString *qs;
755     NSString *p;
756   
757     self->savePageRequired = YES;
758     qs = [MutableStrClass stringWithCapacity:64];
759   
760     [qs appendString:WORequestValueSenderID];
761     [qs appendString:@"="];
762     [qs appendString:[self elementID]];
763     [qs appendString:self->qpJoin];
764     [qs appendString:WORequestValueSessionID];
765     [qs appendString:@"="];
766     [qs appendString:[[self session] sessionID]];
767     [qs appendString:self->qpJoin];
768     [qs appendString:WORequestValueContextID];
769     [qs appendString:@"="];
770     [qs appendString:[self contextID]];
771   
772     p = [[self page] componentActionURLForContext:self];
773
774     if (testNSURLs) {
775       if ([p hasPrefix:@"/"]) p = [p substringFromIndex:1];
776     }
777   
778     return [self urlWithRequestHandlerKey:
779                    [WOAppClass componentRequestHandlerKey]
780                  path:p
781                  queryString:qs];
782   }
783   else {
784     /* old style URLs ... */
785     static NSMutableString *url = nil; // THREAD
786     static IMP addStr = NULL;
787     NSString *s;
788   
789     self->savePageRequired = YES;
790     if (url == nil) {
791       url = [[MutableStrClass alloc] initWithCapacity:256];
792       addStr = [url methodForSelector:@selector(appendString:)];
793       addStr(url, @selector(appendString:), @"/");
794     }
795     else
796       [url setString:@"/"];
797   
798     /*
799       Note: component actions *always* require sessions to be able to locate
800       the request component !
801     */
802     addStr(url, @selector(appendString:), [[self session] sessionID]);
803     addStr(url, @selector(appendString:), @"/");
804     addStr(url, @selector(appendString:), [self->elementID elementID]);
805   
806     s = [self urlWithRequestHandlerKey:
807                 [WOAppClass componentRequestHandlerKey]
808               path:url queryString:nil];
809     return s;
810   }
811 }
812
813 - (NSString *)urlWithRequestHandlerKey:(NSString *)_key
814   path:(NSString *)_path
815   queryString:(NSString *)_query
816 {
817   if (testNSURLs) { /* use NSURLs for processing */
818     NSURL    *rqUrl;
819     
820     if ([_path hasPrefix:@"/"]) {
821 #if DEBUG
822       [self logWithFormat:@"WARNING: got absolute path '%@'", _path];
823 #endif
824       _path = [_path substringFromIndex:1];
825     }
826     
827     if (_key == nil) _key = [WOAppClass componentRequestHandlerKey];
828     rqUrl = [self urlForKey:_key];
829   
830     if ([_query length] > 0) {
831       NSMutableString *s;
832     
833       s = [_path mutableCopy];
834       [s appendString:@"?"];
835       [s appendString:_query];
836       rqUrl = [NSURL URLWithString:s relativeToURL:rqUrl];
837       [s release];
838     }
839     else
840       rqUrl = [NSURL URLWithString:_path relativeToURL:rqUrl];
841     
842     //[self logWithFormat:@"constructed component URL: %@", rqUrl];
843     
844     return [rqUrl stringValueRelativeToURL:[self baseURL]];
845   }
846   else {
847     NSMutableString *url;
848     NSString *tmp;
849     IMP addStr;
850   
851     if (_key == nil) _key = [WOAppClass componentRequestHandlerKey];
852   
853     url = [MutableStrClass stringWithCapacity:256];
854     addStr = [url methodForSelector:@selector(appendString:)];
855   
856     /* static part */
857     if (self->urlPrefix == nil) {
858       if (!relativeURLs) {
859         if ((tmp = [self->request headerForKey:@"x-webobjects-server-url"])) {
860           if ([tmp hasSuffix:@":0"] && [tmp length] > 2) // TODO: BAD BAD BAD
861             tmp = [tmp substringToIndex:([tmp length] - 2)];
862           addStr(url, @selector(appendString:), tmp);
863         }
864         else if ((tmp = [self->request headerForKey:@"host"])) {
865           addStr(url, @selector(appendString:), @"http://");
866           addStr(url, @selector(appendString:), tmp);
867         }
868       }
869   
870       addStr(url, @selector(appendString:), [self->request adaptorPrefix]);
871       addStr(url, @selector(appendString:), @"/");
872       tmp = [[self request] applicationName];
873       if ([tmp length] == 0)
874         tmp = [(WOApplication *)[self application] name];
875       if ([tmp length] > 0) {
876         addStr(url, @selector(appendString:), tmp);
877         if (WOApplicationSuffix)
878           addStr(url, @selector(appendString:), WOApplicationSuffix);
879         addStr(url, @selector(appendString:), @"/");
880       }
881       
882       /* cache prefix */
883       self->urlPrefix = [url copy];
884       if (debugOn) [self debugWithFormat:@"URL prefix: '%@'", self->urlPrefix];
885     }
886     else {
887       /* prefix is cached :-) */
888       addStr(url, @selector(appendString:), self->urlPrefix);
889     }
890   
891     /* variable part */
892     addStr(url, @selector(appendString:), _key);
893     if (_path) 
894       addStr(url, @selector(appendString:), _path);
895     if ([_query length] > 0) {
896       addStr(url, @selector(appendString:), @"?");
897       addStr(url, @selector(appendString:), _query);
898     }
899     return url;
900   }
901 }
902 - (NSString *)completeURLWithRequestHandlerKey:(NSString *)_key
903   path:(NSString *)_path queryString:(NSString *)_query
904   isSecure:(BOOL)_isSecure port:(int)_port
905 {
906   NSMutableString *url = [MutableStrClass stringWithCapacity:256];
907   [url appendString:_isSecure ? @"https://" : @"http://"];
908   [url appendString:[[self request] headerForKey:@"host"]];
909   if (_port > 0) {
910     if (!(_isSecure && _port == 443) && !(!_isSecure && _port == 80))
911       [url appendFormat:@":%i", _port];
912   }
913   [url appendString:[self urlWithRequestHandlerKey:_key
914                           path:_path
915                           queryString:_query]];
916   return url;
917 }
918
919 - (void)setRequestSenderID:(NSString *)_rid {
920   WOElementID *eid;
921   
922   eid = [[WOElementID alloc] initWithString:_rid];
923   [self->reqElementID release];
924   self->reqElementID = eid;
925 }
926 - (NSString *)senderID {
927 #if 1
928   return [self->reqElementID elementID];
929 #else
930   NSMutableString *eid;
931   IMP addStr;
932   int i;
933   
934   eid = [MutableStrClass stringWithCapacity:(self->reqElementIdCount * 4) + 1];
935   addStr = [eid methodForSelector:@selector(appendString:)];
936   for (i = 0; i < self->reqElementIdCount; i++) {
937     if (i != 0) addStr(eid, @selector(appendString:), @".");
938     addStr(eid, @selector(appendString:), [self->reqElementId[i] stringValue]);
939   }
940   return eid;
941 #endif
942 }
943
944 @end /* WOContext(URLs) */
945
946 @implementation WOContext(DeprecatedMethodsInWO4)
947
948 - (WOApplication *)application {
949   if (self->application == nil)
950     self->application = [WOAppClass application];
951
952   if (self->application == nil)
953     NSLog(@"%s: missing application for context %@", __PRETTY_FUNCTION__, self);
954   
955   return self->application;
956 }
957
958 - (void)setDistributionEnabled:(BOOL)_flag {
959   IS_DEPRECATED;
960   [[self session] setDistributionEnabled:_flag];
961 }
962 - (BOOL)isDistributionEnabled {
963   IS_DEPRECATED;
964   return [[self session] isDistributionEnabled];
965 }
966
967 - (NSString *)url {
968   return [self componentActionURL];
969 }
970
971 - (NSString *)urlSessionPrefix {
972   NSMutableString *url;
973   NSString *tmp;
974
975   url = [MutableStrClass stringWithCapacity:128];
976   
977   [url appendString:[[self request] adaptorPrefix]];
978   [url appendString:@"/"];
979   tmp = [[self request] applicationName];
980   [url appendString:
981          tmp ? tmp : [(WOApplication *)[self application] name]];
982
983 #if DEBUG
984   if ([url length] == 0) {
985     NSLog(@"WARNING(%s): could not determine session URL prefix !",
986           __PRETTY_FUNCTION__);
987   }
988 #endif
989   
990   return url;
991 }
992
993 @end /* WOContext(DeprecatedMethodsInWO4) */
994
995 @implementation WOComponent(Cursors)
996
997 - (void)pushCursor:(id)_obj {
998   NSMutableArray *ctxStack;
999   
1000   if (debugCursor)
1001     [self logWithFormat:@"enter cursor: %@", _obj];
1002   
1003   if ((ctxStack = self->cycleContext) == nil)
1004     self->cycleContext = [[NSMutableArray alloc] initWithCapacity:8];
1005   
1006   /* add to cursor stack */
1007   [ctxStack addObject:(_obj ? _obj : [NSNull null])];
1008   
1009   /* set active cursor */
1010   if (![_obj isNotNull]) _obj = nil;
1011   [self setObject:_obj forKey:@"_"];
1012 }
1013
1014 - (id)popCursor {
1015   NSMutableArray *ctxStack;
1016   id old;
1017   
1018   /* retrieve last context */
1019   old  = [[self objectForKey:@"_"] retain];
1020   [self setObject:nil forKey:@"_"];
1021   
1022   /* restore old ctx */
1023   if ((ctxStack = self->cycleContext) != nil) {
1024     unsigned count;
1025     
1026     if ((count = [ctxStack count]) > 0) {
1027       [ctxStack removeObjectAtIndex:(count - 1)];
1028       count--;
1029       
1030       if (count > 0) {
1031         id obj;
1032         
1033         obj = [ctxStack objectAtIndex:(count - 1)];
1034       
1035         if (![obj isNotNull]) obj = nil;
1036         [self setObject:obj forKey:@"_"];
1037       }      
1038     }
1039   }
1040 #if DEBUG
1041   else {
1042     [self debugWithFormat:@"WARNING: -popCursor called without cycle ctx !"];
1043   }
1044 #endif
1045   
1046   if (debugCursor) {
1047     [self logWithFormat:@"leave cursor: %@ (restored=%@)",
1048             old, [self cursor]];
1049   }
1050   
1051   return [old autorelease];
1052 }
1053
1054 - (id)cursor {
1055   NSMutableArray *ctxStack;
1056   
1057   // TODO: why do we check for _ODCycleCtx, if we query '_' ?
1058   
1059   if ((ctxStack = self->cycleContext) == nil)
1060     /* no cycle context setup for component ... */
1061     return self;
1062   if ([ctxStack count] == 0)
1063     /* nothing contained in cycle context ... */
1064     return self;
1065   
1066   return [self objectForKey:@"_"];
1067 }
1068
1069 @end /* WOComponent(Cursors) */