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