Add a function osrfMethodVerifyContext() to do what the
authorscottmk <scottmk@9efc2488-bf62-4759-914b-345cdb29e865>
Wed, 14 Jan 2009 14:36:14 +0000 (14:36 +0000)
committerscottmk <scottmk@9efc2488-bf62-4759-914b-345cdb29e865>
Wed, 14 Jan 2009 14:36:14 +0000 (14:36 +0000)
existing OSRF_METHOD_VERIFY_CONTEXT macro does.  Use it in
_osrfAppRunSystemMethod() and osrfAppEcho().

git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1626 9efc2488-bf62-4759-914b-345cdb29e865

include/opensrf/osrf_application.h
src/libopensrf/osrf_application.c

index 8b1e247..d9bca62 100644 (file)
@@ -31,10 +31,10 @@ extern "C" {
   This macro verifies methods receive the correct parameters */
 #define _OSRF_METHOD_VERIFY_CONTEXT(d) \
        if(!d) return -1; \
-       if(!d->session) { osrfLogError( OSRF_LOG_MARK,  "Session is NULL in app reqeust" ); return -1; }\
-       if(!d->method) { osrfLogError( OSRF_LOG_MARK,  "Method is NULL in app reqeust" ); return -1; }\
+       if(!d->session) { osrfLogError( OSRF_LOG_MARK,  "Session is NULL in app request" ); return -1; }\
+       if(!d->method) { osrfLogError( OSRF_LOG_MARK,  "Method is NULL in app request" ); return -1; }\
        if(d->method->argc) {\
-               if(!d->params) { osrfLogError( OSRF_LOG_MARK,  "Params is NULL in app reqeust %s", d->method->name ); return -1; }\
+               if(!d->params) { osrfLogError( OSRF_LOG_MARK,  "Params is NULL in app request %s", d->method->name ); return -1; }\
                if( d->params->type != JSON_ARRAY ) { \
                        osrfLogError( OSRF_LOG_MARK,  "'params' is not a JSON array for method %s", d->method->name);\
                        return -1; }\
@@ -187,6 +187,12 @@ int osrfAppRespondComplete( osrfMethodContext* context, const jsonObject* data )
 int osrfAppRunChildInit(const char* appname);
 void osrfAppRunExitCode();
 
+/**
+  Determine whether the context looks healthy.
+  Return 0 if it does, or -1 if it doesn't.
+  */
+int osrfMethodVerifyContext( osrfMethodContext* ctx );
+
 #ifdef __cplusplus
 }
 #endif
index 5737787..d8d1a18 100644 (file)
@@ -419,7 +419,10 @@ static void _osrfAppSetIntrospectMethod( osrfMethodContext* ctx, const osrfMetho
   message
  */
 static int _osrfAppRunSystemMethod(osrfMethodContext* ctx) {
-       OSRF_METHOD_VERIFY_CONTEXT(ctx);
+       if( osrfMethodVerifyContext( ctx ) < 0 ) {
+               osrfLogError( OSRF_LOG_MARK,  "_osrfAppRunSystemMethod: Received invalid method context" );
+               return -1;
+       }
 
        if(     !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL ) || 
                        !strcmp(ctx->method->name, OSRF_SYSMETHOD_INTROSPECT_ALL_ATOMIC )) {
@@ -502,7 +505,11 @@ static int osrfAppIntrospectAll( osrfMethodContext* ctx ) {
 }
 
 static int osrfAppEcho( osrfMethodContext* ctx ) {
-       OSRF_METHOD_VERIFY_CONTEXT(ctx);
+       if( osrfMethodVerifyContext( ctx ) < 0 ) {
+               osrfLogError( OSRF_LOG_MARK,  "osrfAppEcho: Received invalid method context" );
+               return -1;
+       }
+       
        int i;
        for( i = 0; i < ctx->params->size; i++ ) {
                const jsonObject* str = jsonObjectGetIndex(ctx->params,i);
@@ -511,3 +518,53 @@ static int osrfAppEcho( osrfMethodContext* ctx ) {
        return 1;
 }
 
+/**
+  Determine whether the context looks healthy.
+  Return 0 if it does, or -1 if it doesn't.
+ */
+int osrfMethodVerifyContext( osrfMethodContext* ctx )
+{
+       if( !ctx ) {
+               osrfLogError( OSRF_LOG_MARK,  "Context is NULL in app request" );
+               return -1;
+       }
+       
+       if( !ctx->session ) {
+               osrfLogError( OSRF_LOG_MARK,  "Session is NULL in app request" );
+               return -1;
+       }
+
+       if( !ctx->method )
+       {
+               osrfLogError( OSRF_LOG_MARK, "Method is NULL in app request" );
+               return -1;
+       }
+
+       if( ctx->method->argc ) {
+               if( !ctx->params ) {
+                       osrfLogError( OSRF_LOG_MARK,
+                               "Params is NULL in app request %s", ctx->method->name );
+                       return -1;
+               }
+               if( ctx->params->type != JSON_ARRAY ) {
+                       osrfLogError( OSRF_LOG_MARK,
+                               "'params' is not a JSON array for method %s", ctx->method->name );
+                       return -1;
+               }
+       }
+
+       if( !ctx->method->name ) {
+               osrfLogError( OSRF_LOG_MARK, "Method name is NULL" );
+                return -1;
+       }
+
+       // Log the call, with the method and parameters
+       char* params_str = jsonObjectToJSON( ctx->params );
+       if( params_str ) {
+               osrfLogInfo( OSRF_LOG_MARK, "CALL:      %s %s - %s",
+                        ctx->session->remote_service, ctx->method->name, params_str );
+               free( params_str );
+       }
+       return 0;
+}
+