File: WOComponentRequestHandler.m

package info (click to toggle)
sope 5.12.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,588 kB
  • sloc: objc: 169,223; sh: 3,823; ansic: 3,409; javascript: 446; python: 318; makefile: 185
file content (284 lines) | stat: -rw-r--r-- 8,302 bytes parent folder | download | duplicates (9)
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
/*
  Copyright (C) 2000-2005 SKYRIX Software AG

  This file is part of SOPE.

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

  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 SOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

#include "WOComponentRequestHandler.h"
#include "WORequestHandler+private.h"
#include "WOContext+private.h"
#include <NGObjWeb/WOApplication.h>
#include <NGObjWeb/WORequest.h>
#include <NGObjWeb/WOResponse.h>
#include <NGObjWeb/WOSession.h>
#include <NGObjWeb/WOComponent.h>
#include "common.h"

@interface WOApplication(Privates)
- (WOSession *)_initializeSessionInContext:(WOContext *)_ctx;
- (void)_setCurrentContext:(WOContext *)_ctx;
@end

@interface WORequestHandler(URI)
- (BOOL)doesRejectFavicon;
@end

@implementation WOComponentRequestHandler

- (WOResponse *)restoreSessionWithID:(NSString *)_sid
  inContext:(WOContext *)_ctx
{
  WOApplication *app;
  WOSession *session;
  
  app = [WOApplication application];
  if (_sid == nil) {
    // invalid session ID (or no session-ID ?!, which is no error ?) */
    return [app handleSessionRestorationErrorInContext:_ctx];
  }
  
  if ((session = [app restoreSessionWithID:_sid inContext:_ctx]) != nil) {
    /* awake restored session */
    [_ctx setSession:session];
    [session _awakeWithContext:_ctx];
    
    [session awake];
    return nil;
  }
  
  return [app handleSessionRestorationErrorInContext:_ctx];
}

/*
  The request handler path of a component URI looks like this:

    sessionID/componentName/contextID/elementID/instance/server
*/

- (WOResponse *)handleRequest:(WORequest *)_request {
  // TODO: this should be integrated into the WORequestHandler default
  //       mechanism
  NSString      *sessionID        = nil;
  WOApplication *application      = nil;
  WOContext     *context;
  WOResponse    *response         = nil;
  WOSession     *session          = nil;
  WOComponent   *component        = nil;
  BOOL          isLocked          = NO;
  NSString      *handlerPath      = nil;

  if (_request == nil) return nil;
  
  if ([self doesRejectFavicon] && [[_request uri] isNotNull]) {
    // TODO: code copied from WORequestHandler ...
    if ([@"/favicon.ico" isEqualToString:[_request uri]]) {
      response = [WOResponse responseWithRequest:_request];
      [response setStatus:404 /* not found */];
      [self debugWithFormat:@"rejected favicon request: %@", [_request uri]];
      return response;
    }
  }
  
  application = [WOApplication application];
  handlerPath = [_request requestHandlerPath];
  
#if 0
  [self logWithFormat:@"[component request handler] path: '%@'", handlerPath];
#endif

  if (![application allowsConcurrentRequestHandling]) {
    [application lockRequestHandling];
    isLocked = YES;
  }
  
  context = [WOContext contextWithRequest:_request];
  [application _setCurrentContext:context];
  
  /*
    parse handler path (URL)

    The format is:

      session/context.element-id
  */
  if ([handlerPath isNotEmpty]) {
    NSArray *spath = [_request requestHandlerPathArray];
    
    if ([spath count] > 1)
      [context setRequestSenderID:[spath objectAtIndex:1]];
    if ([spath isNotEmpty])
      sessionID = [spath objectAtIndex:0];
  }
  
  if (![sessionID isNotEmpty])
    sessionID = [application sessionIDFromRequest:_request];
  
#if 1
  [self logWithFormat:@"%s: made context %@ (cid=%@, sn=%@) ..",
	  __PRETTY_FUNCTION__, context, [context contextID], sessionID];
#endif
  
  [application awake];
  
  /* restore or create session */
  if ([sessionID isNotEmpty]) {
    if ((response = [self restoreSessionWithID:sessionID inContext:context]))
      session = nil;
    else {
      /* 
	 Note: this creates a _new_ session if the restoration handler did not
	       return a response! We check that below by comparing the session
	       IDs.
      */
      session = [context session];
    }
    
    [self debugWithFormat:@"restored session (id=%@): %@", sessionID, session];
    
    if (session && (![sessionID isEqualToString:[session sessionID]])) {
      [self errorWithFormat:@"session-ids do not match (%@ vs %@)",
	      sessionID, [session sessionID]];
    }
    
    if ([session isNotNull]) {
      NSString *eid;
      
      /*
	 only try to restore a page if we still have the same session and if
	 the request contains an element-id (eg if we reconnect to the main
	 URL we do not have an element-id
      */
      eid = [context currentElementID];
      if ([sessionID isEqualToString:[session sessionID]] && eid != nil) {
	/* awake stored page from "old" session */
	component = [session restorePageForContextID:eid];
	
	if (component == nil) {
	  [self logWithFormat:@"could not restore component from session: %@",
	        session];
	  response = [application handlePageRestorationErrorInContext:context];
	}
      }
      else /* a new session was created (but no restore-error response ret.) */
	component = [application pageWithName:nil inContext:context];
    }
    else if (response == nil) {
      [[WOApplication application] warnWithFormat:
                                     @"got no session restoration error, "
                                     @"but missing session!"];
    }
  }
  else {
    /* create new session */
    session = [application _initializeSessionInContext:context];
    if ([session isNotNull]) {
      /* awake created session */
      [session awake];
      component = [application pageWithName:nil inContext:context];
    }
    else
      response = [application handleSessionCreationErrorInContext:context];
  }
  
  if ((session != nil) && (component != nil) && (response == nil)) {
    WOComponent *newPage = nil;

    [[session retain] autorelease];
    
#if DEBUG
    NSAssert(application, @"missing application object ..");
    NSAssert(session,     @"missing session object ..");
#endif
    
    /* set request page in context */
    [context setPage:component];
    
    /* run take-values phase */
    [application takeValuesFromRequest:_request inContext:context];
    
    /* run invoke-action phase */
    newPage = [application invokeActionForRequest:_request inContext:context];
    
    /* process resulting page */
    if (newPage == nil) {
      if ((newPage = [context page]) == nil) {
        newPage = [application pageWithName:nil inContext:context];
        [context setPage:newPage];
      }
    }
    else if ([newPage isKindOfClass:[WOComponent class]])
      [context setPage:newPage];

    [self debugWithFormat:@"%s: new page: %@", __PRETTY_FUNCTION__, newPage];
    
    /* generate response */
    
    response = [self generateResponseForComponent:[context page]
		     inContext:context
		     application:application];
  }
  else {
    [self warnWithFormat:@"%s: did not enter request/response transaction ...",
            __PRETTY_FUNCTION__];
  }
  
  /* tear down */

  /* sleep objects */
  [context sleepComponents];
  [session sleep];
  
  /* save objects */
  
  if (session != nil) {
    if ([context savePageRequired])
      [session savePage:[context page]];
    
    [self debugWithFormat:@"saving session %@", [session sessionID]];
    
    if ([session storesIDsInCookies]) {
      [self debugWithFormat:@"add cookie to session: %@", session];
      [self addCookiesForSession:session
	    toResponse:response
	    inContext:context];
    }
    
#if 1 // TODO: explain that
    [application saveSessionForContext:context];
#else
    [self saveSession:session
	  inContext:context
	  withResponse:response
	  application:application];
#endif
  }
  
  [application sleep];
  
  /* locking */
  
  if (isLocked) {
    [application unlockRequestHandling];
    isLocked = NO;
  }
  
  [application _setCurrentContext:nil];
  return response;
}

@end /* WOComponentRequestHandler */