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