1 #include "opensrf/osrf_app_session.h"
2 #include "opensrf/osrf_application.h"
3 #include "objson/object.h"
4 #include "opensrf/utils.h"
5 #include "opensrf/osrf_log.h"
7 #define OSRF_VERSION_CACHE_TIME 300
9 int osrfAppInitialize();
10 int osrfAppChildInit();
11 int osrfVersion( osrfMethodContext* );
14 int osrfAppInitialize() {
15 osrfLogInit("opensrf.version");
17 osrfAppRegisterMethod(
19 "opensrf.version.verify",
21 "The data for a service/method/params combination will be retrieved "
22 "from the necessary server and the MD5 sum of the total values received "
24 "( serviceName, methodName, [param1, ...] )", 2 );
29 int osrfAppChildInit() {
33 int osrfVersion( osrfMethodContext* ctx ) {
35 OSRF_METHOD_VERIFY_CONTEXT(ctx);
37 /* First, see if the data is in the cache */
38 char* json = jsonObjectToJSON(ctx->params);
39 char* paramsmd5 = md5sum(json);
40 char* cachedmd5 = osrfCacheGetString(paramsmd5);
44 osrfLog( OSRF_DEBUG, "Found %s object in cache, returning....", cachedmd5 );
45 jsonObject* resp = jsonNewObject(cachedmd5);
46 osrfAppRequestRespondComplete( ctx->session, ctx->request, resp );
53 jsonObject* serv = jsonObjectGetIndex(ctx->params, 0);
54 jsonObject* meth = jsonObjectGetIndex(ctx->params, 1);
55 char* service = jsonObjectGetString(serv);
56 char* methd = jsonObjectGetString(meth);
58 if( service && methd ) {
59 /* shove the additional params into an array */
60 jsonObject* tmpArray = jsonNewObject(NULL);
62 for( i = 2; i != ctx->params->size; i++ )
63 jsonObjectPush( tmpArray, jsonObjectClone(jsonObjectGetIndex(ctx->params, i)));
65 osrfAppSession* ses = osrfAppSessionClientInit(service);
66 int reqid = osrfAppSessionMakeRequest( ses, tmpArray, methd, 1, NULL );
67 osrfMessage* omsg = osrfAppSessionRequestRecv( ses, reqid, 60 );
68 jsonObjectFree(tmpArray);
72 jsonObject* result = osrfMessageGetResult( omsg );
73 char* resultjson = jsonObjectToJSON(result);
74 char* resultmd5 = md5sum(resultjson);
76 osrfMessageFree(omsg);
79 jsonObject* resp = jsonNewObject(resultmd5);
80 osrfAppRequestRespondComplete( ctx->session, ctx->request, resp );
82 osrfAppSessionFree(ses);
83 osrfLog(OSRF_DEBUG, "Found version string %s, caching and returning...", resultmd5 );
84 osrfCachePutString( paramsmd5, resultmd5, OSRF_VERSION_CACHE_TIME );
90 osrfAppSessionFree(ses);