mas01mj@584: mas01mj@602: mas01mj@602: #include "httpd.h" mas01mj@602: #include "http_config.h" mas01mj@602: #include "http_log.h" mas01mj@602: #include "http_protocol.h" 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@602: module AP_MODULE_DECLARE_DATA audiodb_module; mas01mj@602: mas01mj@602: typedef struct { mas01mj@602: char *dbpath; mas01mj@602: } adb_config; mas01mj@602: mas01mj@602: /** mas01mj@602: * Config bits and pieces mas01mj@602: **/ mas01mj@602: mas01mj@616: static void *create_audiodb_dir_config(apr_pool_t* p, server_rec* s) { mas01mj@602: adb_config *config = (adb_config *)apr_pcalloc(p, sizeof(adb_config)); mas01mj@602: config->dbpath = NULL; mas01mj@602: return config; mas01mj@602: } mas01mj@602: mas01mj@616: /*static const char* set_database_path(cmd_parms *parms, void *mconfig, const char *arg) { mas01mj@602: adb_config *config = ap_get_module_config(parms->server->module_config, &audiodb_module); mas01mj@602: config->dbpath = (char *)arg; mas01mj@602: return NULL; mas01mj@616: }*/ mas01mj@602: mas01mj@602: static const command_rec mod_audiodb_cmds[] = { mas01mj@602: mas01mj@616: AP_INIT_TAKE1("DatabasePath", ap_set_file_slot, (void*)APR_OFFSETOF(adb_config, dbpath), ACCESS_CONF, "The AudioDB database to use"), mas01mj@602: {NULL} mas01mj@602: }; mas01mj@602: mas01mj@602: static int log_message(void *user_data, librdf_log_message *message) { mas01mj@584: fprintf(stderr, "%s\n", librdf_log_message_message(message)); mas01mj@584: fflush(stderr); mas01mj@584: return 1; mas01mj@584: } mas01mj@584: mas01mj@602: mas01mj@584: static int adb_handle_sparql_req(request_rec *r) { mas01mj@605: librdf_world* world = NULL; mas01mj@605: librdf_storage* storage = NULL; mas01mj@605: librdf_uri *output_uri = NULL; mas01mj@605: int rc = DECLINED; mas01mj@605: mas01mj@584: if(strcmp(r->handler, "audiodb-sparql-handler") != 0) { mas01mj@605: goto error; mas01mj@584: } mas01mj@584: mas01mj@606: rc = OK; mas01mj@606: mas01mj@616: adb_config* config = ap_get_module_config(r->per_dir_config, mas01mj@602: &audiodb_module); mas01mj@602: mas01mj@606: r->status = HTTP_BAD_REQUEST; 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@605: mas01mj@605: mas01mj@605: if (r->method_number == M_POST) { mas01mj@605: if (apreq_body(h, &form_table) != APR_SUCCESS) mas01mj@605: goto error; mas01mj@605: } mas01mj@605: else { mas01mj@605: if (apreq_args(h, &form_table) != APR_SUCCESS) mas01mj@605: goto error; mas01mj@605: } mas01mj@605: 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@605: mas01mj@605: if(!query_string) mas01mj@605: query_string = "DESCRIBE "; mas01mj@605: mas01mj@605: world = librdf_new_world(); mas01mj@584: librdf_world_open(world); mas01mj@602: librdf_world_set_logger(world, NULL, log_message); mas01mj@602: mas01mj@606: if(!config->dbpath) { mas01mj@602: ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "DatabasePath is required"); mas01mj@605: goto error; mas01mj@602: } mas01mj@605: mas01mj@605: // First make sure we actually have a valid query mas01mj@605: librdf_query *query; mas01mj@606: if (!(query = librdf_new_query(world, "sparql", NULL, (unsigned char*)query_string, NULL))) { mas01mj@605: ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to parse query"); mas01mj@606: ap_rprintf(r, "Unable to parse query"); mas01mj@605: goto error; mas01mj@605: } mas01mj@602: mas01mj@605: storage = librdf_new_storage(world, "audiodb", config->dbpath, NULL); mas01mj@606: if(!storage) { mas01mj@602: ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to open audioDB: %s", config->dbpath); mas01mj@606: ap_rprintf(r, "Unable to open audioDB"); mas01mj@605: goto error; mas01mj@584: } mas01mj@584: mas01mj@584: librdf_model *model; mas01mj@606: if (!(model = librdf_new_model(world, storage, NULL))) { mas01mj@602: ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to create model"); mas01mj@606: ap_rprintf(r, "Unable to create model"); mas01mj@605: goto error; mas01mj@584: } mas01mj@584: mas01mj@584: librdf_query_results *results; mas01mj@606: if (!(results = librdf_query_execute(query, model))) { mas01mj@602: ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to execute query"); mas01mj@606: ap_rprintf(r, "Unable to execute query"); mas01mj@605: goto error; mas01mj@584: } mas01mj@584: mas01mj@606: mas01mj@599: mas01mj@606: if(!output) mas01mj@606: output = "application/sparql-results+xml"; mas01mj@606: mas01mj@606: if(strcmp(output, "text/json") == 0) { mas01mj@606: r->content_type = "text/json"; mas01mj@600: output_uri = librdf_new_uri( world,(unsigned char *) JSON_URI ); mas01mj@606: } mas01mj@606: else { mas01mj@606: r->content_type = "application/sparql-results+xml"; mas01mj@600: output_uri = librdf_new_uri( world,(unsigned char *) SPARQL_URI ); mas01mj@606: } 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@599: ap_rprintf(r, out); mas01mj@605: mas01mj@606: r->status = HTTP_OK; mas01mj@584: mas01mj@605: error: mas01mj@605: mas01mj@605: if(output_uri) mas01mj@605: librdf_free_uri(output_uri); mas01mj@605: mas01mj@605: if(storage) { mas01mj@605: librdf_storage_close(storage); mas01mj@605: librdf_free_storage(storage); mas01mj@605: } mas01mj@605: mas01mj@605: if(world) mas01mj@605: librdf_free_world(world); mas01mj@605: mas01mj@605: return rc; mas01mj@584: 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@616: create_audiodb_dir_config, mas01mj@584: NULL, mas01mj@584: NULL, mas01mj@616: //create_audiodb_config, mas01mj@584: NULL, mas01mj@602: mod_audiodb_cmds, mas01mj@584: mod_audiodb_register_hooks, mas01mj@584: };