]> err.no Git - sope/blobdiff - sope-appserver/NGObjWeb/WOContext.m
hotfix
[sope] / sope-appserver / NGObjWeb / WOContext.m
index d5403214555e945437ae530d85cdef4cf27cebab..1b08905714c11bcb38125af53de59abf160276f1 100644 (file)
@@ -1,20 +1,21 @@
 /*
-  Copyright (C) 2000-2004 SKYRIX Software AG
+  Copyright (C) 2000-2006 SKYRIX Software AG
+  Copyright (C) 2006      Helge Hess
 
-  This file is part of OpenGroupware.org.
+  This file is part of SOPE.
 
-  OGo is free software; you can redistribute it and/or modify it under
+  SOPE 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, or (at your option) any
   later version.
 
-  OGo is distributed in the hope that it will be useful, but WITHOUT ANY
+  SOPE 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 OGo; see the file COPYING.  If not, write to the
+  License along with SOPE; see the file COPYING.  If not, write to the
   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.
 */
@@ -28,7 +29,7 @@
 #include <NGObjWeb/WORequest.h>
 #include <NGObjWeb/WOResponse.h>
 #include <NGObjWeb/WOSession.h>
-#import <EOControl/EONull.h>
+#include <Foundation/NSNull.h>
 #include "WOElementID.h"
 #include "common.h"
 #include <time.h>
@@ -49,32 +50,42 @@ static Class WOAppClass = Nil;
 @implementation WOContext
 
 + (int)version {
-  return 7;
-}
-
-static Class MutableStrClass    = Nil;
-static int  contextCount        = 0;
-static int  logComponents       = -1;
-static int  relativeURLs        = -1;
-static BOOL debugOn             = NO;
-static int  debugCursor         = -1;
-static BOOL debugComponentAwake = NO;
-static BOOL testNSURLs          = NO;
-static BOOL newCURLStyle        = NO;
+  return 8;
+}
+
+static Class    WOContextClass       = Nil;
+static Class    MutableStrClass      = Nil;
+static int      contextCount         = 0;
+static int      logComponents        = -1;
+static int      relativeURLs         = -1;
+static BOOL     debugOn              = NO;
+static int      debugCursor          = -1;
+static BOOL     debugComponentAwake  = NO;
+static BOOL     testNSURLs           = NO;
+static BOOL     newCURLStyle         = NO;
 static NSString *WOApplicationSuffix = nil;
 
 + (void)initialize {
-  NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
-  static BOOL didInitialize = NO;
-  if (didInitialize) return;
+  static BOOL    didInit = NO;
+  NSUserDefaults *ud;
+  NSString       *cn;
+
+  if (didInit) return;
+
+  didInit = YES;
+
+  ud = [NSUserDefaults standardUserDefaults];
 
   if (WOAppClass == Nil)
     WOAppClass = [WOApplication class];
   if (MutableStrClass == Nil)
     MutableStrClass = [NSMutableString class];
   
-  didInitialize = YES;
-    
+  cn             = [ud stringForKey:@"WOContextClass"];
+  WOContextClass = NSClassFromString(cn);
+  NSAssert1(WOContextClass != Nil,
+            @"Couldn't instantiate WOContextClass (%@)!", cn);
+
   logComponents = [[ud objectForKey:@"WOLogComponents"] boolValue] ? 1 : 0;
   relativeURLs  = [[ud objectForKey:@"WOUseRelativeURLs"] boolValue]? 1 : 0;
   debugCursor         = [ud boolForKey:@"WODebugCursor"] ? 1 : 0;
@@ -82,18 +93,23 @@ static NSString *WOApplicationSuffix = nil;
   WOApplicationSuffix = [[ud stringForKey:@"WOApplicationSuffix"] copy];
 }
 
-+ (id)contextWithRequest:(WORequest *)_request {
-  return [[(WOContext *)[self alloc] initWithRequest:_request] autorelease];
++ (id)contextWithRequest:(WORequest *)_r {
+  return [[(WOContext *)[WOContextClass alloc] initWithRequest:_r] autorelease];
 }
 
 - (id)initWithRequest:(WORequest *)_request {
   if ((self = [super init])) {
-    unsigned char buf[24];
-    self->qpJoin = @"&";
+    char buf[24];
+    self->qpJoin = @"&amp;";
     
-    sprintf(buf, "%03x%08x%08x", ++contextCount, (int)time(NULL), (int)self);
+    sprintf(buf, "%03x%08x%08x", ++contextCount, (int)time(NULL),
+           (unsigned int)(unsigned long)self);
     self->ctxId = [[NSString alloc] initWithCString:buf];
     
+    /* per default close tags in XML style */
+    self->wcFlags.xmlStyleEmptyElements = 1;
+    self->wcFlags.allowEmptyAttributes  = 0;
+    
     self->elementID = [[WOElementID alloc] init];
     self->awakeComponents = [[NSMutableSet alloc] initWithCapacity:64];
     
@@ -274,19 +290,26 @@ static NSString *WOApplicationSuffix = nil;
   [super dealloc];
 }
 
+/* session */
+
 - (void)setSession:(WOSession *)_session {
   ASSIGN(self->session, _session);
 }
+- (void)setNewSession:(WOSession *)_session {
+  [self setSession:_session];
+  self->wcFlags.hasNewSession = 1;
+}
 
-- (WOSession *)session {
-  // in WO4 -session creates a new session if none is associated
+- (id)session {
+  /* in WO4 -session creates a new session if none is associated */
   
   if (self->session == nil) {
     [[self application] _initializeSessionInContext:self];
     
-    if (self->session == nil)
+    if (self->session == nil) {
       [self logWithFormat:@"%s: missing session for context ..",
               __PRETTY_FUNCTION__];
+    }
   }
   
   return self->session;
@@ -321,9 +344,14 @@ static NSString *WOApplicationSuffix = nil;
 - (BOOL)hasSession {
   return (self->session != nil) ? YES : NO;
 }
+- (BOOL)hasNewSession {
+  if (!self->wcFlags.hasNewSession)
+    return NO;
+  return [self hasSession];
+}
 
 - (BOOL)savePageRequired {
-  return self->savePageRequired;
+  return self->wcFlags.savePageRequired ? YES : NO;
 }
 
 /* cursors */
@@ -350,7 +378,7 @@ static NSString *WOApplicationSuffix = nil;
 
 /* components */
 
-- (WOComponent *)component {
+- (id)component {
   return (self->componentStackCount > 0)
     ? self->componentStack[self->componentStackCount - 1]
     : nil;
@@ -360,7 +388,7 @@ static NSString *WOApplicationSuffix = nil;
   [_page ensureAwakeInContext:self];
   ASSIGN(self->page, _page);
 }
-- (WOComponent *)page {
+- (id)page {
   return self->page;
 }
 
@@ -486,8 +514,7 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
   }
   
   if ([serverURL length] == 0) {
-    [self logWithFormat:
-           @"ERROR: could not find x-webobjects-server-url header !"];
+    [self errorWithFormat:@"could not find x-webobjects-server-url header !"];
     return nil;
   }
   
@@ -524,20 +551,24 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
 }
 
 - (NSURL *)applicationURL {
-  if (self->appURL == nil) {
-    NSString *s;
+  NSString *s;
+  
+  if (self->appURL != nil)
+    return self->appURL;
 
-    s = [self->request adaptorPrefix];
-    if ([s length] > 0) {
-      s = [NSString stringWithFormat:@"%@/%@/", 
-                     s, [self->request applicationName]];
-    }
-    else
-      s = [[self->request applicationName] stringByAppendingString:@"/"];
-    
-    self->appURL =
-      [[NSURL URLWithString:s relativeToURL:[self serverURL]] retain];
+  // TODO: we should ensure that the suffix (.woa) is in the URL
+  
+  s = [self->request adaptorPrefix];
+  if ([s length] > 0) {
+    s = [[[s stringByAppendingString:@"/"]
+             stringByAppendingString:[self->request applicationName]]
+             stringByAppendingString:@"/"];
   }
+  else
+    s = [[self->request applicationName] stringByAppendingString:@"/"];
+  
+  self->appURL =
+    [[NSURL URLWithString:s relativeToURL:[self serverURL]] retain];
   return self->appURL;
 }
 - (NSURL *)urlForKey:(NSString *)_key {
@@ -548,10 +579,10 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
 /* forms */
 
 - (void)setInForm:(BOOL)_form {
-  self->inForm = _form;
+  self->wcFlags.inForm = _form ? 1 : 0;
 }
 - (BOOL)isInForm {
-  return self->inForm;
+  return self->wcFlags.inForm ? YES : NO;
 }
 
 - (void)addActiveFormElement:(WOElement *)_formElement {
@@ -598,7 +629,7 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
     return;
   else if (WOGetKVCGetMethod(self, _key) == NULL) {
     if (_value == nil)
-      _value = [EONull null];
+      _value = [NSNull null];
     
     if (self->variables == nil) {
       self->variables =
@@ -638,18 +669,16 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
     sid = [[self session] sessionID];
   
   return [NSString stringWithFormat:
-                     @"<0x%08X[%@]: %@ app=%@ sn=%@ eid=%@ rqeid=%@>",
-                     (unsigned)self, NSStringFromClass([self class]),
+                     @"<0x%p[%@]: %@ app=%@ sn=%@ eid=%@ rqeid=%@>",
+                     self, NSStringFromClass([self class]),
                      [self  contextID],
                      [app name],
-                     sid ? sid : @"none",
+                    sid != nil ? sid : (NSString *)@"none",
                      [self  elementID],
                      [self  senderID]];
 }
 
-@end /* WOContext */
-
-@implementation WOContext(ElementIDs)
+/* ElementIDs */
 
 - (NSString *)elementID {
   return [self->elementID elementID];
@@ -682,14 +711,33 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
   return [self->reqElementID consumeElementID];
 }
 
-@end /* WOContext(ElementIDs) */
-
-@implementation WOContext(URLs)
+/* URLs */
 
 - (void)_generateCompleteURLs {
   /* described in Apple TIL article 70101 */
 }
 
+- (void)setQueryPathSeparator:(NSString *)_sp {
+  ASSIGNCOPY(self->qpJoin, _sp);
+}
+- (NSString *)queryPathSeparator {
+  return self->qpJoin;
+}
+
+- (void)setGenerateXMLStyleEmptyElements:(BOOL)_flag {
+  self->wcFlags.xmlStyleEmptyElements = _flag ? 1 : 0;
+}
+- (BOOL)generateXMLStyleEmptyElements {
+  return self->wcFlags.xmlStyleEmptyElements ? YES : NO;
+}
+
+- (void)setGenerateEmptyAttributes:(BOOL)_flag {
+  self->wcFlags.allowEmptyAttributes = _flag ? 1 : 0;
+}
+- (BOOL)generateEmptyAttributes {
+  return self->wcFlags.allowEmptyAttributes ? YES : NO;
+}
+
 - (NSString *)queryStringFromDictionary:(NSDictionary *)_queryDict {
   NSEnumerator    *keys;
   NSString        *key;
@@ -698,21 +746,48 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
   
   qs   = [MutableStrClass stringWithCapacity:256];
   keys = [_queryDict keyEnumerator];
-  for (isFirst = YES; (key = [keys nextObject]); ) {
-    NSString *value;
+  for (isFirst = YES; (key = [keys nextObject]) != nil; ) {
+    id value;
     
-    if (isFirst)
-      isFirst = NO;
-    else
-      [qs appendString:self->qpJoin];
+    value = [_queryDict objectForKey:key];
+
+    /* check for multi-value parameter */
+
+    if ([value isKindOfClass:[NSArray class]]) {
+      NSArray  *a = value;
+      unsigned i, count;
+      
+      for (i = 0, count = [a count]; i < count; i++) {
+       value = [a objectAtIndex:i];
+       
+       if (isFirst) isFirst = NO;
+       else [qs appendString:self->qpJoin];
+
+       // TODO: code duplication ...
+       value = ![value isNotNull] ? (NSString *)nil : [value stringValue];
+       key   = [key   stringByEscapingURL];
+       value = [value stringByEscapingURL];
     
-    value = [[_queryDict objectForKey:key] stringValue];
+       [qs appendString:key];
+       if (value != nil) {
+         [qs appendString:@"="];
+         [qs appendString:value];
+       }
+      }
+      continue;
+    }
+
+    /* regular, single-value parameter */
+    
+    if (isFirst) isFirst = NO;
+    else [qs appendString:self->qpJoin];
     
+    value = ![value isNotNull] ? (NSString *)nil : [value stringValue];
     key   = [key   stringByEscapingURL];
     value = [value stringByEscapingURL];
     
     [qs appendString:key];
-    if (value) {
+    if (value != nil) {
       [qs appendString:@"="];
       [qs appendString:value];
     }
@@ -750,11 +825,20 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
   //   26% -urlWithRequestHandler...
   //   21% -elementID (was 40% !! :-)
   //   ~20% mutable string ops
+  
+  /* 
+     This makes the request handler save the page in the session at the
+     end of the request (only necessary if the page generates URLs which
+     refer the context).
+  */
+  self->wcFlags.savePageRequired = 1;
+  
   if (newCURLStyle) {
+    // TODO: who uses that? Its not enabled per default
+    // TODO: what does this do?
     NSMutableString *qs;
     NSString *p;
   
-    self->savePageRequired = YES;
     qs = [MutableStrClass stringWithCapacity:64];
   
     [qs appendString:WORequestValueSenderID];
@@ -785,8 +869,19 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
     static NSMutableString *url = nil; // THREAD
     static IMP addStr = NULL;
     NSString *s;
-  
-    self->savePageRequired = YES;
+    NSString *coRqhKey;
+
+    coRqhKey = [WOAppClass componentRequestHandlerKey];
+
+    /* 
+       Optimization: use relative URL if the request already was a component
+                     action (with a valid session)
+    */
+    if (!self->wcFlags.hasNewSession) {
+      if ([[self->request requestHandlerKey] isEqualToString:coRqhKey])
+       return [self->elementID elementID];
+    }
+    
     if (url == nil) {
       url = [[MutableStrClass alloc] initWithCapacity:256];
       addStr = [url methodForSelector:@selector(appendString:)];
@@ -794,7 +889,7 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
     }
     else
       [url setString:@"/"];
-  
+    
     /*
       Note: component actions *always* require sessions to be able to locate
       the request component !
@@ -803,8 +898,7 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
     addStr(url, @selector(appendString:), @"/");
     addStr(url, @selector(appendString:), [self->elementID elementID]);
   
-    s = [self urlWithRequestHandlerKey:
-               [WOAppClass componentRequestHandlerKey]
+    s = [self urlWithRequestHandlerKey:coRqhKey
              path:url queryString:nil];
     return s;
   }
@@ -815,11 +909,11 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
   queryString:(NSString *)_query
 {
   if (testNSURLs) { /* use NSURLs for processing */
-    NSURL    *rqUrl;
+    NSURL *rqUrl;
     
     if ([_path hasPrefix:@"/"]) {
 #if DEBUG
-      [self logWithFormat:@"WARNING: got absolute path '%@'", _path];
+      [self warnWithFormat:@"got absolute path '%@'", _path];
 #endif
       _path = [_path substringFromIndex:1];
     }
@@ -852,7 +946,7 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
   
     url = [MutableStrClass stringWithCapacity:256];
     addStr = [url methodForSelector:@selector(appendString:)];
-  
+
     /* static part */
     if (self->urlPrefix == nil) {
       if (!relativeURLs) {
@@ -866,7 +960,7 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
          addStr(url, @selector(appendString:), tmp);
        }
       }
-  
+
       addStr(url, @selector(appendString:), [self->request adaptorPrefix]);
       addStr(url, @selector(appendString:), @"/");
       tmp = [[self request] applicationName];
@@ -941,16 +1035,26 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
 #endif
 }
 
-@end /* WOContext(URLs) */
+/* languages for resource lookup (non-WO) */
+
+- (NSArray *)resourceLookupLanguages {
+  return [self hasSession] 
+    ? [[self session] languages]
+    : [[self request] browserLanguages];
+}
+
 
-@implementation WOContext(DeprecatedMethodsInWO4)
+/* DeprecatedMethodsInWO4 */
 
-- (WOApplication *)application {
+- (id)application {
   if (self->application == nil)
     self->application = [WOAppClass application];
 
-  if (self->application == nil)
-    NSLog(@"%s: missing application for context %@", __PRETTY_FUNCTION__, self);
+  if (self->application == nil) {
+    [self logWithFormat:
+           @"%s: missing application for context %@", 
+           __PRETTY_FUNCTION__, self];
+  }
   
   return self->application;
 }
@@ -982,34 +1086,30 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
 
 #if DEBUG
   if ([url length] == 0) {
-    NSLog(@"WARNING(%s): could not determine session URL prefix !",
-          __PRETTY_FUNCTION__);
+    [self warnWithFormat:@"(%s): could not determine session URL prefix !",
+            __PRETTY_FUNCTION__];
   }
 #endif
   
   return url;
 }
 
-@end /* WOContext(DeprecatedMethodsInWO4) */
+@end /* WOContext */
+
 
 @implementation WOComponent(Cursors)
 
 - (void)pushCursor:(id)_obj {
-  NSMutableArray *ctxStack;
-  
   if (debugCursor)
     [self logWithFormat:@"enter cursor: %@", _obj];
   
-  if ((ctxStack = [self objectForKey:@"_ODCycleCtx"]) == nil) {
-    ctxStack = [NSMutableArray arrayWithCapacity:8];
-    [self setObject:ctxStack forKey:@"_ODCycleCtx"];
-  }
+  if (!self->cycleContext)
+    self->cycleContext = [[NSMutableArray alloc] initWithCapacity:8];
   
   /* add to cursor stack */
-  [ctxStack addObject:(_obj ? _obj : [NSNull null])];
+  [self->cycleContext addObject:(_obj != nil ? _obj : (id)[NSNull null])];
   
   /* set active cursor */
-  if (![_obj isNotNull]) _obj = nil;
   [self setObject:_obj forKey:@"_"];
 }
 
@@ -1022,7 +1122,7 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
   [self setObject:nil forKey:@"_"];
   
   /* restore old ctx */
-  if ((ctxStack = [self objectForKey:@"_ODCycleCtx"])) {
+  if ((ctxStack = self->cycleContext) != nil) {
     unsigned count;
     
     if ((count = [ctxStack count]) > 0) {
@@ -1041,7 +1141,7 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
   }
 #if DEBUG
   else {
-    [self debugWithFormat:@"WARNING: -popCursor called without cycle ctx !"];
+    [self warnWithFormat:@"-popCursor called without cycle ctx !"];
   }
 #endif
   
@@ -1058,7 +1158,7 @@ void WOContext_leaveComponent(WOContext *self, WOComponent *_component) {
   
   // TODO: why do we check for _ODCycleCtx, if we query '_' ?
   
-  if ((ctxStack = [self objectForKey:@"_ODCycleCtx"]) == nil)
+  if ((ctxStack = self->cycleContext) == nil)
     /* no cycle context setup for component ... */
     return self;
   if ([ctxStack count] == 0)