mas01mj@584: mas01mj@584: #include mas01mj@584: #include mas01mj@584: #include mas01mj@584: #include mas01mj@584: #include mas01mj@584: #include mas01mj@584: #include mas01mj@584: #include mas01mj@584: #include mas01mj@584: #include "apreq2/apreq_module_apache2.h" mas01mj@599: #include mas01mj@584: mas01mj@600: #define BASE_URI "http://purl.org/ontology/af/" mas01mj@600: #define JSON_URI "http://www.w3.org/2001/sw/DataAccess/json-sparql/" mas01mj@600: #define SPARQL_URI "http://www.w3.org/TR/2006/WD-rdf-sparql-XMLres-20070614/" mas01mj@584: mas01mj@584: static int log_out(void *user_data, librdf_log_message *message) mas01mj@584: { mas01mj@584: fprintf(stderr, "%s\n", librdf_log_message_message(message)); mas01mj@584: fflush(stderr); mas01mj@584: return 1; mas01mj@584: } mas01mj@584: mas01mj@584: static int adb_handle_sparql_req(request_rec *r) { mas01mj@584: if(strcmp(r->handler, "audiodb-sparql-handler") != 0) { mas01mj@584: return DECLINED; mas01mj@584: } mas01mj@584: mas01mj@584: r->content_type = "text/plain"; mas01mj@584: r->status = OK; mas01mj@584: r->status_line = "200 OK"; mas01mj@584: mas01mj@584: if(!r->args) { mas01mj@584: r->args = ""; mas01mj@584: } mas01mj@584: mas01mj@584: const apr_table_t *form_table; mas01mj@584: apreq_handle_t *h = apreq_handle_apache2(r); mas01mj@584: if(apreq_args(h, &form_table) != APR_SUCCESS) mas01mj@584: return DECLINED; mas01mj@584: mas01mj@600: const char *query_string = apr_table_get(form_table, "query"); mas01mj@600: const char *output = apr_table_get(form_table, "output"); mas01mj@600: if(!output) mas01mj@600: output = "xml"; mas01mj@584: mas01mj@584: int rc = 0; mas01mj@584: librdf_world* world = librdf_new_world(); mas01mj@584: librdf_world_open(world); mas01mj@584: librdf_world_set_logger(world, NULL, log_out); mas01mj@599: librdf_storage* storage = librdf_new_storage(world, "audiodb", "/tmp/test_database.adb", NULL); mas01mj@584: if(!storage) mas01mj@584: { mas01mj@584: rc = 2; mas01mj@584: goto error; mas01mj@584: } mas01mj@584: mas01mj@584: librdf_model *model; mas01mj@584: if (!(model = librdf_new_model(world, storage, NULL))) mas01mj@584: { mas01mj@584: rc = 5; mas01mj@584: goto error; mas01mj@584: } mas01mj@584: mas01mj@584: librdf_query *query; mas01mj@600: if (!(query = librdf_new_query(world, "sparql", NULL, (unsigned char*)query_string, NULL))) mas01mj@584: { mas01mj@584: rc = 3; mas01mj@584: goto error; mas01mj@584: } mas01mj@584: mas01mj@584: librdf_query_results *results; mas01mj@584: if (!(results = librdf_query_execute(query, model))) mas01mj@584: { mas01mj@584: rc = 4; mas01mj@584: goto error; mas01mj@584: } mas01mj@584: mas01mj@599: mas01mj@600: librdf_uri *output_uri; mas01mj@600: mas01mj@600: if(strcmp(output, "json") == 0) mas01mj@600: output_uri = librdf_new_uri( world,(unsigned char *) JSON_URI ); mas01mj@600: else mas01mj@600: output_uri = librdf_new_uri( world,(unsigned char *) SPARQL_URI ); mas01mj@600: mas01mj@600: const unsigned char* out = librdf_query_results_to_string(results, output_uri, librdf_new_uri(world, (unsigned char*) BASE_URI)); mas01mj@600: mas01mj@600: librdf_free_uri(output_uri); mas01mj@599: librdf_storage_close(storage); mas01mj@599: librdf_free_storage(storage); mas01mj@599: librdf_free_world(world); mas01mj@599: ap_rprintf(r, out); mas01mj@584: mas01mj@584: rc = 0; mas01mj@584: return r->status; mas01mj@584: mas01mj@584: error: mas01mj@584: ap_rprintf(r, "Fail %d", rc); mas01mj@584: return OK; mas01mj@584: } mas01mj@584: mas01mj@584: static void mod_audiodb_register_hooks (apr_pool_t *p) { mas01mj@584: ap_hook_handler(adb_handle_sparql_req, NULL, NULL, APR_HOOK_FIRST); mas01mj@584: } mas01mj@584: mas01mj@584: module AP_MODULE_DECLARE_DATA audiodb_module = { mas01mj@584: STANDARD20_MODULE_STUFF, mas01mj@584: NULL, mas01mj@584: NULL, mas01mj@584: NULL, mas01mj@584: NULL, mas01mj@584: NULL, mas01mj@584: mod_audiodb_register_hooks, mas01mj@584: };