4 let open_db connection_string =
6 new PG.connection connection_string
9 prerr_endline (PG.string_of_error e) ;
12 let extract_position s =
13 let sharp_pos = String.rindex s '#' + 1 in
14 String.sub s sharp_pos ((String.length s) - sharp_pos)
16 let mk_new_msg () = ref []
18 let msg_output_string msg s = msg := s::!msg
20 let msg_serialize msg =
21 List.fold_left (fun acc s -> s ^ acc) "" !msg
23 let msg_output_header msg obj =
24 msg_output_string msg "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n" ;
25 msg_output_string msg ("<rdf:RDF xml:lang=\"en\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:h=\"http://www.cs.unibo.it/helm/schemas/mattone.rdf#\">\n <h:Object rdf:about=\"" ^ obj ^ "\">\n")
27 let msg_output_trailer msg =
28 msg_output_string msg " </h:Object>\n</rdf:RDF>\n"
30 let forward_metadata db obj =
31 let res = db#exec ("SELECT * FROM refObj WHERE source = '" ^ obj ^ "';") in
32 let msg = mk_new_msg () in
33 msg_output_header msg obj ;
34 for i = res#ntuples downto 1 do
35 let position = extract_position (res#getvalue (i - 1) 2)
36 and occurrence = res#getvalue (i - 1) 1 in
37 msg_output_string msg " <h:refObj>\n <h:Occurrence>\n" ;
38 msg_output_string msg (" <h:position>" ^ position ^ "</h:position>\n") ;
39 msg_output_string msg (" <h:occurrence>" ^ occurrence ^ "</h:occurrence>\n") ;
40 msg_output_string msg " </h:Occurrence>\n </h:refObj>\n"
42 msg_output_trailer msg ;
45 let backward_metadata db obj =
46 let res = db#exec ("SELECT * FROM refObj WHERE h_occurrence = '" ^ obj ^ "';") in
47 let msg = mk_new_msg () in
48 msg_output_header msg obj ;
49 for i = res#ntuples downto 1 do
50 let position = extract_position (res#getvalue (i - 1) 2)
51 and occurrence = res#getvalue (i - 1) 0 in
52 msg_output_string msg " <h:backPointer>\n <h:Occurrence>\n" ;
53 msg_output_string msg (" <h:position>" ^ position ^ "</h:position>\n") ;
54 msg_output_string msg (" <h:occurrence>" ^ occurrence ^ "</h:occurrence>\n") ;
55 msg_output_string msg " </h:Occurrence>\n </h:backPointer>\n"
57 msg_output_trailer msg ;
60 let debug_print s = prerr_endline ("[RDFly] " ^ s)
62 let mk_return_fun contype msg outchan =
64 ~body:msg ~headers:["Content-Type", contype] outchan
66 let return_html = mk_return_fun "text/html"
67 let return_xml = mk_return_fun "text/xml"
68 let return_400 body ch = Http_daemon.respond_error ~code:400 ~body ch
69 let return_html_error s = return_html ("<html><body>" ^ s ^ "</body></html>")
71 let callback (req: Http_types.request) ch =
73 debug_print ("Connection from " ^ req#clientAddr) ;
74 debug_print ("Received request: " ^ req#path) ;
77 return_html_error "yeah right..." ch
79 let obj = req#param "object"
80 and kind = req#param "kind" in
81 let db = open_db "dbname='mowgli' user='helm'" in
84 "forward" -> return_xml (forward_metadata db obj) ch
85 | "backward" -> return_xml (backward_metadata db obj) ch
86 | s -> return_html_error ("unsupported kind: " ^ s) ch
89 | invalid_request -> Http_daemon.respond_error ~status:(`Client_error `Bad_request) ch)
91 | Http_types.Param_not_found attr_name ->
92 return_400 (Printf.sprintf "Parameter '%s' is missing" attr_name) ch
94 return_html_error ("Uncaught exception: " ^ (Printexc.to_string exc)) ch
100 ~timeout:(Some 600) ~port:58088 callback