1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "opensrf/osrf_json.h"
4 #include "opensrf/utils.h"
5 #include "opensrf/log.h"
7 #define OSRF_VERSION_CACHE_TIME 300
9 int osrfAppInitialize();
10 int osrfAppChildInit();
11 int osrfVersion( osrfMethodContext* );
14 int osrfAppInitialize() {
16 osrfAppRegisterMethod(
18 "opensrf.version.verify",
20 "The data for a service/method/params combination will be retrieved "
21 "from the necessary server and the MD5 sum of the total values received "
22 "will be returned. PARAMS( serviceName, methodName, [param1, ...] )",
28 int osrfAppChildInit() {
32 int osrfVersion( osrfMethodContext* ctx ) {
33 if( osrfMethodVerifyContext( ctx ) ) {
34 osrfLogError( OSRF_LOG_MARK, "Invalid method context" );
38 /* First, see if the data is in the cache */
39 char* json = jsonObjectToJSON(ctx->params);
40 char* paramsmd5 = md5sum(json);
41 char* cachedmd5 = osrfCacheGetString(paramsmd5);
45 osrfLogDebug(OSRF_LOG_MARK, "Found %s object in cache, returning....", cachedmd5 );
46 jsonObject* resp = jsonNewObject(cachedmd5);
47 osrfAppRespondComplete( ctx, resp );
54 const jsonObject* serv = jsonObjectGetIndex(ctx->params, 0);
55 const jsonObject* meth = jsonObjectGetIndex(ctx->params, 1);
56 const char* service = jsonObjectGetString(serv);
57 const char* methd = jsonObjectGetString(meth);
59 if( service && methd ) {
60 /* shove the additional params into an array */
61 jsonObject* tmpArray = jsonNewObject(NULL);
63 for( i = 2; i != ctx->params->size; i++ )
64 jsonObjectPush( tmpArray, jsonObjectClone(jsonObjectGetIndex(ctx->params, i)));
66 osrfAppSession* ses = osrfAppSessionClientInit(service);
67 int reqid = osrfAppSessionSendRequest( ses, tmpArray, methd, 1 );
68 osrfMessage* omsg = osrfAppSessionRequestRecv( ses, reqid, 60 );
69 jsonObjectFree(tmpArray);
73 jsonObject* result = osrfMessageGetResult( omsg );
74 char* resultjson = jsonObjectToJSON(result);
75 char* resultmd5 = md5sum(resultjson);
77 osrfMessageFree(omsg);
80 jsonObject* resp = jsonNewObject(resultmd5);
81 osrfAppRespondComplete( ctx, resp );
83 osrfAppSessionFree(ses);
84 osrfLogDebug(OSRF_LOG_MARK, "Found version string %s, caching and returning...", resultmd5 );
85 osrfCachePutString( paramsmd5, resultmd5, OSRF_VERSION_CACHE_TIME );
91 osrfAppSessionFree(ses);