o finalize -_handleRequest:

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gsweb/trunk@19042 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
mguesdon 2004-04-06 11:12:35 +00:00
parent 30120bc203
commit c0e11bd5dc

View file

@ -1,6 +1,6 @@
/** GSWActionRequestHandler.m - <title>GSWeb: Class GSWActionRequestHandler</title> /** GSWActionRequestHandler.m - <title>GSWeb: Class GSWActionRequestHandler</title>
Copyright (C) 1999-2003 Free Software Foundation, Inc. Copyright (C) 1999-2004 Free Software Foundation, Inc.
Written by: Manuel Guesdon <mguesdon@orange-concept.com> Written by: Manuel Guesdon <mguesdon@orange-concept.com>
Date: Feb 1999 Date: Feb 1999
@ -37,22 +37,26 @@ RCS_ID("$Id$")
//==================================================================== //====================================================================
@implementation GSWActionRequestHandler @implementation GSWActionRequestHandler
//--------------------------------------------------------------------
-(NSString*)defaultActionClassName -(NSString*)defaultActionClassName
{ {
[self subclassResponsibility: _cmd]; [self subclassResponsibility: _cmd];
return nil; return nil;
}; };
//--------------------------------------------------------------------
-(NSString*)defaultDefaultActionName -(NSString*)defaultDefaultActionName
{ {
return @"default"; return @"default";
}; };
//--------------------------------------------------------------------
-(BOOL)defaultShouldAddToStatistics -(BOOL)defaultShouldAddToStatistics
{ {
return YES; return YES;
}; };
//--------------------------------------------------------------------
-(id)init -(id)init
{ {
if ((self=[super init])) if ((self=[super init]))
@ -64,6 +68,7 @@ RCS_ID("$Id$")
return self; return self;
}; };
//--------------------------------------------------------------------
-(id)initWithDefaultActionClassName:(NSString*)defaultActionClassName -(id)initWithDefaultActionClassName:(NSString*)defaultActionClassName
defaultActionName:(NSString*)defaultActionName defaultActionName:(NSString*)defaultActionName
shouldAddToStatistics:(BOOL)shouldAddToStatistics shouldAddToStatistics:(BOOL)shouldAddToStatistics
@ -77,6 +82,7 @@ RCS_ID("$Id$")
return self; return self;
}; };
//--------------------------------------------------------------------
-(void)dealloc -(void)dealloc
{ {
DESTROY(_actionClassName); DESTROY(_actionClassName);
@ -105,7 +111,6 @@ RCS_ID("$Id$")
//-------------------------------------------------------------------- //--------------------------------------------------------------------
-(GSWResponse*)handleRequest:(GSWRequest*)aRequest -(GSWResponse*)handleRequest:(GSWRequest*)aRequest
{ {
//OK
GSWResponse* response=nil; GSWResponse* response=nil;
GSWApplication* application=nil; GSWApplication* application=nil;
LOGObjectFnStart(); LOGObjectFnStart();
@ -253,28 +258,7 @@ RCS_ID("$Id$")
NSDebugMLog(@"_actionClassClass=%@",_actionClassClass); NSDebugMLog(@"_actionClassClass=%@",_actionClassClass);
NSDebugMLog(@"testActionName=%@",testActionName); NSDebugMLog(@"testActionName=%@",testActionName);
} }
}
/*
NSString* tmpActionName=[NSString stringWithFormat:@"%@Action",];
SEL tmpActionSel=NSSelectorFromString(tmpActionName);
Class aClass = NSClassFromString(@"DirectAction");
NSDebugMLLog(@"requests",@"tmpActionName=%@",
tmpActionName);
if (tmpActionSel && aClass)
{
if ([aClass instancesRespondToSelector:tmpActionSel])
{
actionName=[requestHandlerPathArray objectAtIndex:0];
className=@"DirectAction";
};
};
if (!actionName)
{
className=[requestHandlerPathArray objectAtIndex:0];
actionName=@"default";
};
}; };
*/
}; };
break; break;
case 2: case 2:
@ -333,55 +317,43 @@ RCS_ID("$Id$")
NSString* actionClassName=nil; NSString* actionClassName=nil;
GSWContext* context=nil; GSWContext* context=nil;
GSWAction* action=nil; GSWAction* action=nil;
NSException* exception=nil;
BOOL hasSession=NO;
LOGObjectFnStart(); LOGObjectFnStart();
application=[GSWApplication application]; application=[GSWApplication application];
NS_DURING NS_DURING
{ {
statisticsStore=[application statisticsStore]; NS_DURING
if (_shouldAddToStatistics)
[self registerWillHandleActionRequest];
[application awake];
requestHandlerPathArray=[self getRequestHandlerPathForRequest:aRequest];
NSDebugMLLog(@"requests",@"requestHandlerPathArray=%@",
requestHandlerPathArray);
[self getRequestActionClassNameInto:&actionClassName
classInto:&actionClass
nameInto:&actionName
forPath:requestHandlerPathArray];
NSDebugMLLog(@"requests",@"className=%@",actionClassName);
NSDebugMLLog(@"requests",@"actionClass=%@",actionClass);
NSDebugMLLog(@"requests",@"actionName=%@",actionName);
if (actionClass)
{ {
GSWResourceManager* resourceManager=nil; statisticsStore=[application statisticsStore];
GSWDeployedBundle* appBundle=nil; if (_shouldAddToStatistics)
id<GSWActionResults> actionResult=nil; [self registerWillHandleActionRequest];
resourceManager=[application resourceManager]; [application awake];
appBundle=[resourceManager _appProjectBundle];
[resourceManager _allFrameworkProjectBundles];//So what ?
action=[self getActionInstanceOfClass:actionClass requestHandlerPathArray=[self getRequestHandlerPathForRequest:aRequest];
withRequest:aRequest]; NSDebugMLLog(@"requests",@"requestHandlerPathArray=%@",
requestHandlerPathArray);
NSAssert1(action,@"Direct action of class named %@ can't be created",
actionClassName);
// Parse path into actionClassName,actionClass and actionName
NS_DURING NS_DURING
{ { [self getRequestActionClassNameInto:&actionClassName
actionResult=[action performActionNamed:actionName]; classInto:&actionClass
nameInto:&actionName
forPath:requestHandlerPathArray];
NSDebugMLLog(@"requests",@"className=%@",actionClassName);
NSDebugMLLog(@"requests",@"actionClass=%@",actionClass);
NSDebugMLLog(@"requests",@"actionName=%@",actionName);
} }
NS_HANDLER NS_HANDLER
{ {
LOGException(@"%@ (%@)",localException,[localException reason]); LOGException(@"%@ (%@)",localException,[localException reason]);
response=[application handleActionRequestErrorWithRequest:aRequest response=[application handleActionRequestErrorWithRequest:aRequest
exception:localException exception:localException
reason:"InvocationError" reason:@"InvalidPathError"
requestHanlder:self requestHanlder:self
actionClassName:actionClassName actionClassName:actionClassName
actionName:actionName actionName:actionName
@ -391,87 +363,152 @@ RCS_ID("$Id$")
}; };
NS_ENDHANDLER; NS_ENDHANDLER;
response=[actionResult generateResponse]; if (actionClass)
context=[action _context];
[[NSNotificationCenter defaultCenter]postNotificationName:@"DidHandleRequestNotification"
object:context];
[self _setRecordingHeadersToResponse:response
forRequest:aRequest
inContext:context];
}
else
{
NSException* exception=nil;
if ([actionClassName length]>0)
{ {
exception=[NSException exceptionWithName:NSInvalidArgumentException//TODO better name (IllegalStateException) GSWResourceManager* resourceManager=nil;
reason:[NSString stringWithFormat:@"Can't find action class named '%@'",actionClassName] GSWDeployedBundle* appBundle=nil;
userInfo:nil]; id<GSWActionResults> actionResult=nil;
response=[application handleActionRequestErrorWithRequest:aRequest
exception:exception resourceManager=[application resourceManager];
reason:"InvocationError" appBundle=[resourceManager _appProjectBundle];
requestHanlder:self [resourceManager _allFrameworkProjectBundles];//So what ?
actionClassName:actionClassName
actionName:actionName NS_DURING
actionClass:actionClass {
actionObject:action]; action=[self getActionInstanceOfClass:actionClass
withRequest:aRequest];
}
NS_HANDLER
{
LOGException(@"%@ (%@)",localException,[localException reason]);
response=[application handleActionRequestErrorWithRequest:aRequest
exception:localException
reason:@"InstantiationError"
requestHanlder:self
actionClassName:actionClassName
actionName:actionName
actionClass:actionClass
actionObject:action];
[localException raise];
};
NS_ENDHANDLER;
NSAssert1(action,@"Direct action of class named %@ can't be created",
actionClassName);
NS_DURING
{
actionResult=[action performActionNamed:actionName];
}
NS_HANDLER
{
LOGException(@"%@ (%@)",localException,[localException reason]);
response=[application handleActionRequestErrorWithRequest:aRequest
exception:localException
reason:@"InvocationError"
requestHanlder:self
actionClassName:actionClassName
actionName:actionName
actionClass:actionClass
actionObject:action];
[localException raise];
};
NS_ENDHANDLER;
response=[actionResult generateResponse];
context=[action _context];
[[NSNotificationCenter defaultCenter]postNotificationName:@"DidHandleRequestNotification"
object:context];
[self _setRecordingHeadersToResponse:response
forRequest:aRequest
inContext:context];
} }
else else
{ {
exception=[NSException exceptionWithName:NSInvalidArgumentException//TODO better name (IllegalStateException) NSException* exception=nil;
reason:[NSString stringWithFormat:@"Can't execute action with path: '%@'",
requestHandlerPathArray]
userInfo:nil];
response=[application handleActionRequestErrorWithRequest:aRequest
exception:exception
reason:"InvocationError"
requestHanlder:self
actionClassName:actionClassName
actionName:actionName
actionClass:actionClass
actionObject:action];
};
NSDebugMLog(@"Exception=%@",exception);
NSDebugMLog(@"Response=%@",response);
[exception raise];
NSDebugMLog(@"Not raised ? Exception=%@",exception);
};
if ([application isCachingEnabled])
{
//TODO
};
{
//Finir ? if ([actionClassName length]>0)
}; {
exception=[NSException exceptionWithName:NSInvalidArgumentException//TODO better name (IllegalStateException)
reason:[NSString stringWithFormat:@"Can't find action class named '%@'",actionClassName]
userInfo:nil];
response=[application handleActionRequestErrorWithRequest:aRequest
exception:exception
reason:@"InvocationError"
requestHanlder:self
actionClassName:actionClassName
actionName:actionName
actionClass:actionClass
actionObject:action];
}
else
{
exception=[NSException exceptionWithName:NSInvalidArgumentException//TODO better name (IllegalStateException)
reason:[NSString stringWithFormat:@"Can't execute action with path: '%@'",
requestHandlerPathArray]
userInfo:nil];
response=[application handleActionRequestErrorWithRequest:aRequest
exception:exception
reason:@"InvocationError"
requestHanlder:self
actionClassName:actionClassName
actionName:actionName
actionClass:actionClass
actionObject:action];
};
NSDebugMLog(@"Exception=%@",exception);
NSDebugMLog(@"Response=%@",response);
[exception raise];
NSDebugMLog(@"Not raised ? Exception=%@",exception);
};
if ([application isCachingEnabled])
{
//TODO
};
}
NS_HANDLER
{
LOGException(@"%@ (%@)",localException,[localException reason]);
NSDebugMLog(@"EXCEPTION localException=%@",localException);
if (!response)
{
if (!context)
{
context=[action _context];
if (!context)
{
context=[GSWApp _context];
if (!context)
{
context = [application createContextForRequest:aRequest];
};
};
};
NS_DURING
{
response = [self generateErrorResponseWithException:localException
inContext:context];
}
NS_HANDLER
{
[NSException raise:@"Exception"
format:@"Exception wduring error reponse generation in %@: %@",
[self className],localException];
};
NS_ENDHANDLER;
};
};
NS_ENDHANDLER;
} }
NS_HANDLER NS_HANDLER
{ {
LOGException(@"%@ (%@)",localException,[localException reason]); ASSIGN(exception,localException); // re-raise later
NSDebugMLog(@"EXCEPTION localException=%@",localException);
if (!response)
{
if (!context)
{
context=[action _context];
if (!context)
{
context=[GSWApp _context];
if (!context)
{
context = [application createContextForRequest:aRequest];
};
};
};
response = [self generateErrorResponseWithException:localException
inContext:context];
};
}; };
NS_ENDHANDLER; NS_ENDHANDLER;
NSDebugMLLog(@"requests",@"response=%@",response); NSDebugMLLog(@"requests",@"response=%@",response);
RETAIN(response); RETAIN(response);
if (!context) if (!context)
@ -479,20 +516,32 @@ RCS_ID("$Id$")
if (context) if (context)
{ {
[context _putAwakeComponentsToSleep]; [context _putAwakeComponentsToSleep];
hasSession=[context hasSession];
[application saveSessionForContext:context]; [application saveSessionForContext:context];
}; };
NSDebugMLLog(@"requests",@"response=%@",response); NSDebugMLLog(@"requests",@"response=%@",response);
AUTORELEASE(response); AUTORELEASE(response);
AUTORELEASE(exception);
[application sleep]; [application sleep];
[self registerDidHandleActionRequestWithActionNamed:actionName]; if (exception)
[exception raise];
else
{
if (statisticsStore && _shouldAddToStatistics)
[self registerDidHandleActionRequestWithActionNamed:actionName];
//TODO do not fnalize if already done (in handleException for exemple) //TODO do not finalize if already done (in handleException for exemple)
if (response) if (response)
[response _finalizeInContext:context]; {
[application _setContext:nil]; [response _finalizeInContext:context];
if(hasSession && [application isPageRefreshOnBacktrackEnabled])
[response disableClientCaching];
};
[application _setContext:nil];
};
LOGObjectFnStop(); LOGObjectFnStop();
return response; return response;
@ -528,6 +577,7 @@ RCS_ID("$Id$")
return [[GSWActionRequestHandler new] autorelease]; return [[GSWActionRequestHandler new] autorelease];
}; };
//--------------------------------------------------------------------
+(GSWActionRequestHandler*)handlerWithDefaultActionClassName:(NSString*)defaultActionClassName +(GSWActionRequestHandler*)handlerWithDefaultActionClassName:(NSString*)defaultActionClassName
defaultActionName:(NSString*)defaultActionName defaultActionName:(NSString*)defaultActionName
shouldAddToStatistics:(BOOL)shouldAddToStatistics shouldAddToStatistics:(BOOL)shouldAddToStatistics