4 (* First of all we load the configuration *)
6 let configuration_file = "/projects/helm/etc/rdfly.conf.xml" in
7 Helm_registry.load_from configuration_file
10 let open_db ?host ?database ?port ?password ?user =
12 M.quick_connect ?host ?database ?port ?password ?user
18 let extract_position s =
19 let sharp_pos = String.rindex s '#' + 1 in
20 String.sub s sharp_pos ((String.length s) - sharp_pos)
22 let mk_new_msg () = ref []
24 let msg_output_string msg s = msg := s::!msg
26 let msg_serialize msg =
27 List.fold_left (fun acc s -> s ^ acc) "" !msg
29 let msg_output_header msg obj =
30 msg_output_string msg "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n" ;
31 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")
33 let msg_output_trailer msg =
34 msg_output_string msg " </h:Object>\n</rdf:RDF>\n"
36 let msg_output_dc_header msg obj =
37 msg_output_string msg "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n" ;
38 msg_output_string msg ("<rdf:RDF xml:lang=\"en\"\n xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n xmlns:dc=\"http://purl.org/metadata/dublin_core#\"\n xmlns:dcq=\"http://purl.org/metadata/dublin_core_qualifiers#\"\n xmlns:h=\"http:/www.cs.unibo.it/helm/schemas/schema-h.rdf#\"\n xmlns:hth=\"http://www.cs.unibo.it/helm/schemas/schema-hth.rdf#\">\n")
41 let msg_output_dc_trailer msg =
42 msg_output_string msg " </h:DirectoryOfObjects>\n</rdf:RDF>"
45 let value_of_optional_value =
51 let forward_metadata db obj =
52 let res = M.exec db ("SELECT * FROM refObj WHERE source = '" ^ obj ^ "';") in
53 let msg = mk_new_msg () in
54 msg_output_header msg obj ;
57 let position = extract_position (value_of_optional_value (cols.(2))) in
58 let occurrence = value_of_optional_value (cols.(1)) in
59 msg_output_string msg " <h:refObj>\n <h:Occurrence>\n" ;
60 msg_output_string msg (" <h:position>" ^ position ^ "</h:position>\n") ;
61 msg_output_string msg (" <h:occurrence>" ^ occurrence ^ "</h:occurrence>\n") ;
62 msg_output_string msg " </h:Occurrence>\n </h:refObj>\n"
64 msg_output_trailer msg ;
68 let backward_metadata db obj =
69 let res = M.exec db ("SELECT * FROM refObj WHERE h_occurrence = '" ^ obj ^ "';") in
70 let msg = mk_new_msg () in
71 msg_output_header msg obj ;
74 let position = extract_position (value_of_optional_value (cols.(2))) in
75 let occurrence = value_of_optional_value (cols.(0)) in
76 msg_output_string msg " <h:backPointer>\n <h:Occurrence>\n" ;
77 msg_output_string msg (" <h:position>" ^ position ^ "</h:position>\n") ;
78 msg_output_string msg (" <h:occurrence>" ^ occurrence ^ "</h:occurrence>\n") ;
79 msg_output_string msg " </h:Occurrence>\n </h:backPointer>\n"
81 msg_output_trailer msg ;
85 let dc_metadata db obj =
87 [ "dc:creator","dccreator" ;
89 "dc:description","dcdescription" ;
90 "dc:format","dcformat" ;
91 "dc:identifier","dcidentifier" ;
92 "dc:language","dclanguage" ;
93 "dc:publisher","dcpublisher" ;
94 "dcq:RelationType","dcqRelationType" ;
95 "dc:relation","dcrelation" ;
96 "dc:rights","dcrights" ;
97 "dc:source","dcsource" ;
98 "dc:subject","dcsubject" ;
99 "dc:title","dctitle" ;
100 "hth:ResourceFormat","hthResourceFormat" ;
101 "hth:contact","hthcontact" ;
102 "hth:firstVersion","hthfirstVersion" ;
103 "hth:institution","hthinstitution" ;
104 "hth:modified","hthmodified"
107 let msg = mk_new_msg () in
108 msg_output_dc_header msg obj ;
110 (fun (propertyname,tablename) ->
113 ("SELECT * FROM " ^ tablename ^ " WHERE uri = '" ^ obj ^ "';") in
116 let value = value_of_optional_value (cols.(0)) in
117 msg_output_string msg
118 (" <" ^ propertyname ^ ">" ^ value ^ "</" ^ propertyname ^ ">\n") ;
121 msg_output_dc_trailer msg ;
125 let debug_print s = prerr_endline ("[RDFly] " ^ s)
127 let mk_return_fun contype msg outchan =
129 ~body:msg ~headers:["Content-Type", contype] outchan
131 let return_html = mk_return_fun "text/html"
132 let return_xml = mk_return_fun "text/xml"
133 let return_400 body ch = Http_daemon.respond_error ~code:400 ~body ch
134 let return_html_error s = return_html ("<html><body>" ^ s ^ "</body></html>")
138 Some (Helm_registry.get key)
139 with Helm_registry.Key_not_found _ -> None
141 let get_int_option key =
143 Some (Helm_registry.get_int key)
144 with Helm_registry.Key_not_found _ -> None
146 let host = get_option "rdfly.mysql_connection.host";;
147 let database = get_option "rdfly.mysql_connection.database";;
148 let port = get_int_option "rdfly.mysql_connection.port";;
149 let password = get_option "rdfly.mysql_connection.password";;
150 let user = get_option "rdfly.mysql_connection.user";;
151 let daemonport = Helm_registry.get_int "rdfly.port";;
153 let callback (req: Http_types.request) ch =
155 debug_print ("Connection from " ^ req#clientAddr) ;
156 debug_print ("Received request: " ^ req#path) ;
159 return_html_error "yeah right..." ch
161 let obj = req#param "object"
162 and kind = req#param "kind" in
163 let db = open_db ?host ?database ?port ?password ?user () in
166 "forward" -> return_xml (forward_metadata db obj) ch
167 | "backward" -> return_xml (backward_metadata db obj) ch
168 | "dc" -> return_xml (dc_metadata db obj) ch
169 | s -> return_html_error ("unsupported kind: " ^ s) ch
172 | invalid_request -> Http_daemon.respond_error ~status:(`Client_error `Bad_request) ch)
174 | Http_types.Param_not_found attr_name ->
175 return_400 (Printf.sprintf "Parameter '%s' is missing" attr_name) ch
177 return_html_error ("Uncaught exception: " ^ (Printexc.to_string exc)) ch
180 Sys.catch_break true;
183 ~timeout:(Some 600) ~port:daemonport callback