annotate sparql/mod_audiodb/mod_audiodb.c @ 770:c54bc2ffbf92 tip

update tags
author convert-repo
date Fri, 16 Dec 2011 11:34:01 +0000
parents 881378d8309d
children
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@616 30 static void *create_audiodb_dir_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@616 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@616 40 }*/
mas01mj@602 41
mas01mj@602 42 static const command_rec mod_audiodb_cmds[] = {
mas01mj@602 43
mas01mj@616 44 AP_INIT_TAKE1("DatabasePath", ap_set_file_slot, (void*)APR_OFFSETOF(adb_config, dbpath), ACCESS_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@605 56 librdf_world* world = NULL;
mas01mj@605 57 librdf_storage* storage = NULL;
mas01mj@605 58 librdf_uri *output_uri = NULL;
mas01mj@605 59 int rc = DECLINED;
mas01mj@605 60
mas01mj@584 61 if(strcmp(r->handler, "audiodb-sparql-handler") != 0) {
mas01mj@605 62 goto error;
mas01mj@584 63 }
mas01mj@584 64
mas01mj@606 65 rc = OK;
mas01mj@606 66
mas01mj@616 67 adb_config* config = ap_get_module_config(r->per_dir_config,
mas01mj@602 68 &audiodb_module);
mas01mj@602 69
mas01mj@606 70 r->status = HTTP_BAD_REQUEST;
mas01mj@584 71
mas01mj@584 72 if(!r->args) {
mas01mj@584 73 r->args = "";
mas01mj@584 74 }
mas01mj@584 75
mas01mj@584 76 const apr_table_t *form_table;
mas01mj@584 77 apreq_handle_t *h = apreq_handle_apache2(r);
mas01mj@605 78
mas01mj@605 79
mas01mj@605 80 if (r->method_number == M_POST) {
mas01mj@605 81 if (apreq_body(h, &form_table) != APR_SUCCESS)
mas01mj@605 82 goto error;
mas01mj@605 83 }
mas01mj@605 84 else {
mas01mj@605 85 if (apreq_args(h, &form_table) != APR_SUCCESS)
mas01mj@605 86 goto error;
mas01mj@605 87 }
mas01mj@605 88
mas01mj@584 89
mas01mj@600 90 const char *query_string = apr_table_get(form_table, "query");
mas01mj@600 91 const char *output = apr_table_get(form_table, "output");
mas01mj@605 92
mas01mj@605 93 if(!query_string)
mas01mj@605 94 query_string = "DESCRIBE ";
mas01mj@605 95
mas01mj@605 96 world = librdf_new_world();
mas01mj@584 97 librdf_world_open(world);
mas01mj@602 98 librdf_world_set_logger(world, NULL, log_message);
mas01mj@602 99
mas01mj@606 100 if(!config->dbpath) {
mas01mj@602 101 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "DatabasePath is required");
mas01mj@605 102 goto error;
mas01mj@602 103 }
mas01mj@605 104
mas01mj@605 105 // First make sure we actually have a valid query
mas01mj@605 106 librdf_query *query;
mas01mj@606 107 if (!(query = librdf_new_query(world, "sparql", NULL, (unsigned char*)query_string, NULL))) {
mas01mj@605 108 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to parse query");
mas01mj@606 109 ap_rprintf(r, "Unable to parse query");
mas01mj@605 110 goto error;
mas01mj@605 111 }
mas01mj@602 112
mas01mj@605 113 storage = librdf_new_storage(world, "audiodb", config->dbpath, NULL);
mas01mj@606 114 if(!storage) {
mas01mj@602 115 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to open audioDB: %s", config->dbpath);
mas01mj@606 116 ap_rprintf(r, "Unable to open audioDB");
mas01mj@605 117 goto error;
mas01mj@584 118 }
mas01mj@584 119
mas01mj@584 120 librdf_model *model;
mas01mj@606 121 if (!(model = librdf_new_model(world, storage, NULL))) {
mas01mj@602 122 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to create model");
mas01mj@606 123 ap_rprintf(r, "Unable to create model");
mas01mj@605 124 goto error;
mas01mj@584 125 }
mas01mj@584 126
mas01mj@584 127 librdf_query_results *results;
mas01mj@606 128 if (!(results = librdf_query_execute(query, model))) {
mas01mj@602 129 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to execute query");
mas01mj@606 130 ap_rprintf(r, "Unable to execute query");
mas01mj@605 131 goto error;
mas01mj@584 132 }
mas01mj@584 133
mas01mj@606 134
mas01mj@599 135
mas01mj@606 136 if(!output)
mas01mj@606 137 output = "application/sparql-results+xml";
mas01mj@606 138
mas01mj@606 139 if(strcmp(output, "text/json") == 0) {
mas01mj@606 140 r->content_type = "text/json";
mas01mj@600 141 output_uri = librdf_new_uri( world,(unsigned char *) JSON_URI );
mas01mj@606 142 }
mas01mj@606 143 else {
mas01mj@606 144 r->content_type = "application/sparql-results+xml";
mas01mj@600 145 output_uri = librdf_new_uri( world,(unsigned char *) SPARQL_URI );
mas01mj@606 146 }
mas01mj@600 147 const unsigned char* out = librdf_query_results_to_string(results, output_uri, librdf_new_uri(world, (unsigned char*) BASE_URI));
mas01mj@600 148
mas01mj@599 149 ap_rprintf(r, out);
mas01mj@605 150
mas01mj@606 151 r->status = HTTP_OK;
mas01mj@584 152
mas01mj@605 153 error:
mas01mj@605 154
mas01mj@605 155 if(output_uri)
mas01mj@605 156 librdf_free_uri(output_uri);
mas01mj@605 157
mas01mj@605 158 if(storage) {
mas01mj@605 159 librdf_storage_close(storage);
mas01mj@605 160 librdf_free_storage(storage);
mas01mj@605 161 }
mas01mj@605 162
mas01mj@605 163 if(world)
mas01mj@605 164 librdf_free_world(world);
mas01mj@605 165
mas01mj@605 166 return rc;
mas01mj@584 167
mas01mj@584 168 }
mas01mj@584 169
mas01mj@584 170 static void mod_audiodb_register_hooks (apr_pool_t *p) {
mas01mj@584 171 ap_hook_handler(adb_handle_sparql_req, NULL, NULL, APR_HOOK_FIRST);
mas01mj@584 172 }
mas01mj@584 173
mas01mj@584 174 module AP_MODULE_DECLARE_DATA audiodb_module = {
mas01mj@584 175 STANDARD20_MODULE_STUFF,
mas01mj@616 176 create_audiodb_dir_config,
mas01mj@584 177 NULL,
mas01mj@584 178 NULL,
mas01mj@616 179 //create_audiodb_config,
mas01mj@584 180 NULL,
mas01mj@602 181 mod_audiodb_cmds,
mas01mj@584 182 mod_audiodb_register_hooks,
mas01mj@584 183 };