annotate sparql/mod_audiodb/mod_audiodb.c @ 602:783a1a5e51b2

mod_audiodb now uses apache config correctly. * Added DatabasePath to apache config * Replaced return codes with DECLINED and logging * General tidying
author mas01mj
date Mon, 17 Aug 2009 15:36:31 +0000
parents 337e5962218a
children d7eb0a7440ad
rev   line source
mas01mj@584 1
mas01mj@602 2
mas01mj@602 3 #include "httpd.h"
mas01mj@602 4 #include "http_config.h"
mas01mj@602 5 #include "http_log.h"
mas01mj@602 6 #include "http_protocol.h"
mas01mj@584 7 #include <ap_config.h>
mas01mj@584 8 #include <apr_strings.h>
mas01mj@584 9 #include <librdf.h>
mas01mj@584 10 #include <apreq_module.h>
mas01mj@584 11 #include <apreq_parser.h>
mas01mj@584 12 #include <apreq_param.h>
mas01mj@584 13 #include "apreq2/apreq_module_apache2.h"
mas01mj@599 14 #include <rasqal.h>
mas01mj@584 15
mas01mj@600 16 #define BASE_URI "http://purl.org/ontology/af/"
mas01mj@600 17 #define JSON_URI "http://www.w3.org/2001/sw/DataAccess/json-sparql/"
mas01mj@600 18 #define SPARQL_URI "http://www.w3.org/TR/2006/WD-rdf-sparql-XMLres-20070614/"
mas01mj@584 19
mas01mj@602 20 module AP_MODULE_DECLARE_DATA audiodb_module;
mas01mj@602 21
mas01mj@602 22 typedef struct {
mas01mj@602 23 char *dbpath;
mas01mj@602 24 } adb_config;
mas01mj@602 25
mas01mj@602 26 /**
mas01mj@602 27 * Config bits and pieces
mas01mj@602 28 **/
mas01mj@602 29
mas01mj@602 30 static void *create_audiodb_config(apr_pool_t* p, server_rec* s) {
mas01mj@602 31 adb_config *config = (adb_config *)apr_pcalloc(p, sizeof(adb_config));
mas01mj@602 32 config->dbpath = NULL;
mas01mj@602 33 return config;
mas01mj@602 34 }
mas01mj@602 35
mas01mj@602 36 static const char* set_database_path(cmd_parms *parms, void *mconfig, const char *arg) {
mas01mj@602 37 adb_config *config = ap_get_module_config(parms->server->module_config, &audiodb_module);
mas01mj@602 38 config->dbpath = (char *)arg;
mas01mj@602 39 return NULL;
mas01mj@602 40 }
mas01mj@602 41
mas01mj@602 42 static const command_rec mod_audiodb_cmds[] = {
mas01mj@602 43
mas01mj@602 44 AP_INIT_TAKE1("DatabasePath", set_database_path, NULL, RSRC_CONF, "The AudioDB database to use"),
mas01mj@602 45 {NULL}
mas01mj@602 46 };
mas01mj@602 47
mas01mj@602 48 static int log_message(void *user_data, librdf_log_message *message) {
mas01mj@584 49 fprintf(stderr, "%s\n", librdf_log_message_message(message));
mas01mj@584 50 fflush(stderr);
mas01mj@584 51 return 1;
mas01mj@584 52 }
mas01mj@584 53
mas01mj@602 54
mas01mj@584 55 static int adb_handle_sparql_req(request_rec *r) {
mas01mj@584 56 if(strcmp(r->handler, "audiodb-sparql-handler") != 0) {
mas01mj@584 57 return DECLINED;
mas01mj@584 58 }
mas01mj@584 59
mas01mj@602 60 adb_config* config = ap_get_module_config(r->server->module_config,
mas01mj@602 61 &audiodb_module);
mas01mj@602 62
mas01mj@584 63 r->content_type = "text/plain";
mas01mj@584 64 r->status = OK;
mas01mj@584 65 r->status_line = "200 OK";
mas01mj@584 66
mas01mj@584 67 if(!r->args) {
mas01mj@584 68 r->args = "";
mas01mj@584 69 }
mas01mj@584 70
mas01mj@584 71 const apr_table_t *form_table;
mas01mj@584 72 apreq_handle_t *h = apreq_handle_apache2(r);
mas01mj@584 73 if(apreq_args(h, &form_table) != APR_SUCCESS)
mas01mj@584 74 return DECLINED;
mas01mj@584 75
mas01mj@600 76 const char *query_string = apr_table_get(form_table, "query");
mas01mj@600 77 const char *output = apr_table_get(form_table, "output");
mas01mj@600 78 if(!output)
mas01mj@600 79 output = "xml";
mas01mj@584 80
mas01mj@584 81 librdf_world* world = librdf_new_world();
mas01mj@584 82 librdf_world_open(world);
mas01mj@602 83 librdf_world_set_logger(world, NULL, log_message);
mas01mj@602 84
mas01mj@602 85 if(!config->dbpath)
mas01mj@602 86 {
mas01mj@602 87 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "DatabasePath is required");
mas01mj@602 88 return DECLINED;
mas01mj@602 89 }
mas01mj@602 90
mas01mj@602 91 librdf_storage* storage = librdf_new_storage(world, "audiodb", config->dbpath, NULL);
mas01mj@584 92 if(!storage)
mas01mj@584 93 {
mas01mj@602 94 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to open audioDB: %s", config->dbpath);
mas01mj@602 95 return DECLINED;
mas01mj@584 96 }
mas01mj@584 97
mas01mj@584 98 librdf_model *model;
mas01mj@584 99 if (!(model = librdf_new_model(world, storage, NULL)))
mas01mj@584 100 {
mas01mj@602 101 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to create model");
mas01mj@602 102 return DECLINED;
mas01mj@584 103 }
mas01mj@584 104
mas01mj@584 105 librdf_query *query;
mas01mj@600 106 if (!(query = librdf_new_query(world, "sparql", NULL, (unsigned char*)query_string, NULL)))
mas01mj@584 107 {
mas01mj@602 108 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to parse query");
mas01mj@602 109 return DECLINED;
mas01mj@584 110 }
mas01mj@584 111
mas01mj@584 112 librdf_query_results *results;
mas01mj@584 113 if (!(results = librdf_query_execute(query, model)))
mas01mj@584 114 {
mas01mj@602 115 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to execute query");
mas01mj@602 116 return DECLINED;
mas01mj@584 117 }
mas01mj@584 118
mas01mj@599 119
mas01mj@600 120 librdf_uri *output_uri;
mas01mj@600 121
mas01mj@600 122 if(strcmp(output, "json") == 0)
mas01mj@600 123 output_uri = librdf_new_uri( world,(unsigned char *) JSON_URI );
mas01mj@600 124 else
mas01mj@600 125 output_uri = librdf_new_uri( world,(unsigned char *) SPARQL_URI );
mas01mj@600 126
mas01mj@600 127 const unsigned char* out = librdf_query_results_to_string(results, output_uri, librdf_new_uri(world, (unsigned char*) BASE_URI));
mas01mj@600 128
mas01mj@600 129 librdf_free_uri(output_uri);
mas01mj@599 130 librdf_storage_close(storage);
mas01mj@599 131 librdf_free_storage(storage);
mas01mj@599 132 librdf_free_world(world);
mas01mj@599 133 ap_rprintf(r, out);
mas01mj@584 134
mas01mj@602 135 return OK;
mas01mj@584 136
mas01mj@584 137 }
mas01mj@584 138
mas01mj@584 139 static void mod_audiodb_register_hooks (apr_pool_t *p) {
mas01mj@584 140 ap_hook_handler(adb_handle_sparql_req, NULL, NULL, APR_HOOK_FIRST);
mas01mj@584 141 }
mas01mj@584 142
mas01mj@584 143 module AP_MODULE_DECLARE_DATA audiodb_module = {
mas01mj@584 144 STANDARD20_MODULE_STUFF,
mas01mj@584 145 NULL,
mas01mj@584 146 NULL,
mas01mj@602 147 create_audiodb_config,
mas01mj@584 148 NULL,
mas01mj@602 149 mod_audiodb_cmds,
mas01mj@584 150 mod_audiodb_register_hooks,
mas01mj@584 151 };