added simple request utilty to display fieldmapper objects
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 8 Feb 2006 23:07:48 +0000 (23:07 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 8 Feb 2006 23:07:48 +0000 (23:07 +0000)
now building liboils_utils.so so external binaries can link in

git-svn-id: svn://svn.open-ils.org/ILS/trunk@3016 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/Makefile
Open-ILS/src/c-apps/Makefile
Open-ILS/src/c-apps/oils_utils.c
Open-ILS/src/c-apps/oils_utils.h
Open-ILS/src/extras/Makefile
Open-ILS/src/extras/oils_requestor.c [new file with mode: 0644]

index b533739..8ce6f0f 100644 (file)
@@ -12,7 +12,7 @@ all: mod_xmlbuilder mod_ils_rest_gateway c_apps client-xul
 
 install:       perl-install web-install server-xul string-templates-install storage-bootstrap cgi-bootstrap xsl-install c_apps-install circ_rules-install 
 
-web-install:   webcore-install autojs-install  mod_xmlbuilder-install mod_ils_rest_gateway-install 
+web-install:   webcore-install autojs-install  mod_xmlbuilder-install mod_ils_rest_gateway-install  
 
 circ_rules-install:
        @echo $@
@@ -35,6 +35,14 @@ mod_xmlbuilder:
        @echo $@
        make -C apachemods mod_xmlbuilder.so
 
+oils_cgi:
+       @echo $@
+       make -C apachemods oils_cgi.so
+
+oils_cgi-install:
+       @echo $@
+       make -C apachemods oils_cgi-install
+
 mod_xmlbuilder-install:
        @echo $@
        make -C apachemods mod_xmlbuilder-install
@@ -74,10 +82,12 @@ libfieldmapper-install:
 c_apps:        libfieldmapper
        @echo $@
        make -C c-apps
+       make -C extras oils_requestor
 
 c_apps-install:        libfieldmapper-install
        @echo $@
        make -C c-apps install
+       make -C extras oils_requestor-install
 
 autojs-install:
        @echo $@
@@ -157,4 +167,5 @@ clean:
        @echo $@
        make -C extras clean
        make -C apachemods clean
+       make -C c-apps clean
 
index a678500..f774261 100644 (file)
@@ -8,11 +8,27 @@ oils_utils.o: oils_utils.c oils_utils.h
 oils_auth.o:   oils_auth.c
 
 
-oils_auth.so:  oils_auth.o oils_utils.o oils_event.o
-       $(CC) -shared -W1 $(LDLIBS) $(LDFLAGS) oils_utils.o oils_event.o oils_auth.o -o $(TMPDIR)/oils_auth.so
+oils_auth.so:  oils_auth.o liboils_utils.so
+       $(CC) -shared -W1 $(LDLIBS) $(LDFLAGS) -loils_utils oils_auth.o -o $(TMPDIR)/$@
+
+liboils_utils.so:      oils_utils.o oils_event.o oils_constants.h
+       $(CC) -shared -W1 $(LDLIBS) $(LDFLAGS) oils_utils.o oils_event.o -o $@
+       cp $@ $(TMPDIR)/
+       mkdir -p $(TMPDIR)/openils/
+       cp oils_event.h $(TMPDIR)/openils/
+       cp oils_utils.h $(TMPDIR)/openils/
+       cp oils_constants.h $(TMPDIR)/openils/
+
+
 
 install:
+       @echo $@;
        cp $(TMPDIR)/oils_auth.so $(LIBDIR)/
+       cp $(TMPDIR)/liboils_utils.so $(LIBDIR)/
+       mkdir -p $(INCDIR)/
+       cp oils_utils.h $(INCDIR)
+       cp oils_event.h $(INCDIR)
+       cp oils_constants.h $(INCDIR)
 
 clean:
        rm -f *.o *.so
index e7cb31b..4baabc2 100644 (file)
@@ -107,3 +107,45 @@ char* oilsUtilsFetchOrgSetting( int orgid, char* setting ) {
 
 }
 
+
+
+char* oilsUtilsLogin( char* uname, char* passwd, char* type, int orgId ) {
+       if(!(uname && passwd)) return NULL;
+
+       osrfLogDebug(OSRF_LOG_MARK, "Logging in with username %s", uname );
+       char* token = NULL;
+
+       jsonObject* params = jsonParseString("[\"%s\"]", uname);
+
+       jsonObject* o = oilsUtilsQuickReq( "open-ils.auth",
+               "open-ils.auth.authenticate.init", params );
+
+       char* seed = jsonObjectGetString(o);
+       char* passhash = md5sum(passwd);
+       char buf[256];
+       bzero(buf, 256);
+       snprintf(buf, 255, "%s%s", seed, passhash);
+       char* fullhash = md5sum(buf);
+
+       jsonObjectFree(o);
+       jsonObjectFree(params);
+       free(passhash);
+
+       params = jsonParseString( "[\"%s\", \"%s\", \"%s\", \"%d\"]", uname, fullhash, type, orgId );
+       o = oilsUtilsQuickReq( "open-ils.auth",
+               "open-ils.auth.authenticate.complete", params );
+
+       if(o) {
+               char* tok = jsonObjectGetString(
+                       jsonObjectGetKey(jsonObjectGetKey(o,"payload"), "authtoken"));
+               if(tok) token = strdup(tok);
+       }
+
+       free(fullhash);
+       jsonObjectFree(params);
+       jsonObjectFree(o);
+
+       return token;
+}
+
+
index 38da943..66304c7 100644 (file)
@@ -71,3 +71,11 @@ jsonObject* oilsUtilsFetchUserByUsername( char* name );
  * Caller must free the returned string
  */
 char* oilsUtilsFetchOrgSetting( int orgid, char* setting );
+
+
+/**
+ * Logs into the auth server with the given username and password
+ * @return The authtoken string which must be de-allocated by the caller
+ */
+char* oilsUtilsLogin( char* uname, char* passwd, char* type, int orgId );
+
index b711f39..87f867e 100644 (file)
@@ -1,4 +1,10 @@
-all:
+LDLIBS += -lreadline -loils_utils
+all:   oils_requestor
 
+oils_requestor:        oils_requestor.o
+oils_requestor-install:
+       cp oils_requestor $(BINDIR)
+oils_requestor.o:      oils_requestor.c
 
 clean:
+       /bin/rm -f *.o oils_requestor
diff --git a/Open-ILS/src/extras/oils_requestor.c b/Open-ILS/src/extras/oils_requestor.c
new file mode 100644 (file)
index 0000000..8d0a54f
--- /dev/null
@@ -0,0 +1,181 @@
+#include <getopt.h>
+#include <stdio.h>
+#include <string.h>
+#include <readline/readline.h>
+#include "opensrf/utils.h"
+#include "opensrf/osrf_system.h"
+#include "opensrf/osrf_app_session.h"
+#include "openils/fieldmapper_lookup.h"
+#include "openils/oils_event.h"
+#include "openils/oils_utils.h"
+
+char* script           = NULL;
+char* authtoken        = NULL;
+
+int do_request( char* request );
+char* format_response( jsonObject* o );
+
+int main( int argc, char* argv[] ) {
+       
+       char c;
+       char* username          = NULL;
+       char* password          = NULL;
+       char* config            = NULL;
+       char* context           = NULL;
+       char* request;
+
+       while( (c = getopt( argc, argv, "f:u:p:s:c:" )) != -1 ) {
+               switch(c) {
+                       case '?': return -1;
+                       case 'f': config                = strdup(optarg);
+                       case 'c': context               = strdup(optarg);
+                       case 'u': username      = strdup(optarg);
+                       case 'p': password      = strdup(optarg);
+                       case 's': script                = strdup(optarg);
+               }
+       }
+
+       if(!(config && context)) {
+               fprintf(stderr, "Config or config context not provided. Exiting...\n");
+               return -1;
+       }
+
+       if( ! osrf_system_bootstrap_client(config, context) ) {
+               fprintf(stderr, "Unable to connect to OpenSRF network...\n");
+               return 1;
+       }
+
+       if( username && password &&
+                       ( authtoken = oilsUtilsLogin(username, password, "staff", -1 )) ) {
+               printf("Login Session: %s\n", authtoken);
+       }
+
+       while((request=readline("oils# "))) 
+               if(do_request(request)) break;
+
+       free(authtoken);
+       return 1;
+}
+
+
+int do_request( char* request ) {
+
+       if(!strcasecmp(request, "exit") || !strcasecmp(request,"quit"))
+               return 1;
+
+       if(!strcmp(request,"")) return 0;
+
+       char* service;
+       char* method;
+       char* tmp;
+       char* item;
+       growing_buffer* buffer = buffer_init(256);
+       
+       service = strtok_r(request, " ", &tmp);
+       method = strtok_r(NULL, " ", &tmp);
+       while( (item = strtok_r(NULL, " ", &tmp)) ) 
+               buffer_fadd(buffer, "%s", item);
+
+       if( service && method ) {
+
+               jsonObject* params = NULL;
+               if(buffer->n_used > 0) 
+                       params = jsonParseString("[%s]", buffer->buf);
+
+               osrfAppSession* session = osrf_app_client_session_init(service);
+               int req_id = osrf_app_session_make_req( session, params, method, 1, NULL );
+               osrfMessage* omsg;
+
+               while( (omsg = osrfAppSessionRequestRecv( session, req_id, 120 )) ) {
+                       jsonObject* res = osrfMessageGetResult(omsg);
+                       char* data = format_response(res);
+                       printf("\n%s\n", data);
+                       free(data);
+                       osrfMessageFree(omsg);
+               }
+
+               osrfAppSessionFree(session);
+               jsonObjectFree(params);
+
+       } else {
+               fprintf(stderr, "STATMENT DOES NOT PARSE: %s\n", request);
+       }
+
+       buffer_free(buffer);
+       return 0;
+}
+
+
+char* format_response( jsonObject* o ) {
+       if(!o) return NULL;
+
+       int width = 20;
+
+       if( o->classname && isFieldmapper(o->classname) ) {
+
+               int i = 0;
+               char* key;
+               growing_buffer* buffer = buffer_init(256);
+
+               buffer_fadd(buffer, " FM Class: %s\n", o->classname);
+
+               while( (key = fm_pton(o->classname, i++)) ) {
+                       char* val = oilsFMGetString(o, key);
+                       jsonObject* item;
+
+                       int l = strlen(key + 2);
+                       buffer_fadd(buffer, " %s: ", key);
+
+                       if(val) {
+
+                               while( l++ < width ) buffer_add(buffer, "-");
+                               buffer_fadd(buffer, " %s\n", val);
+                               free(val);
+
+                       } else if( (item = oilsFMGetObject(o, key))) {
+
+                               if(item->type != JSON_NULL ) {
+                                       char* d = format_response(item);
+                                       buffer_add(buffer, "\n====================================\n");
+                                       buffer_fadd(buffer, "%s\n", d);
+                                       buffer_add(buffer, "====================================\n");
+                                       free(d);
+                               } else {
+                                       while( l++ < width ) buffer_add(buffer, "-");
+                                       buffer_add(buffer," NULL \n");
+                               }
+
+                       } else {
+
+                               while( l++ < width ) buffer_add(buffer, "-");
+                               buffer_add(buffer," NULL \n");
+                       }
+
+                       free(key);
+               }
+
+               char* data = buffer_data(buffer);
+               buffer_free(buffer);
+               return data;
+       }
+
+       char* jjson;
+       if( o->type == JSON_ARRAY ) {
+               int i = 0;
+               growing_buffer* arrb = buffer_init(256);
+               for( i = 0; i != o->size; i++ ) {
+                       char* d = format_response(jsonObjectGetIndex(o, i));
+                       buffer_fadd(arrb, "%s\n", d);
+               }
+
+               jjson = buffer_data(arrb);
+               buffer_free(arrb);
+
+       } else {
+               char* json = jsonObjectToJSON(o);
+               jjson = jsonFormatString(json);
+               free(json);
+       }
+
+       return jjson;
+}