]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/rdfly/rdfly.ml
kind=dc implemented.
[helm.git] / helm / DEVEL / rdfly / rdfly.ml
1
2 module M = Mysql
3
4   (* First of all we load the configuration *)
5 let _ =
6  let configuration_file = "/projects/helm/etc/rdfly.conf.xml" in
7   Helm_registry.load_from configuration_file
8 ;;
9
10 let open_db ?host ?database ?port ?password ?user =
11   try
12     M.quick_connect ?host ?database ?port ?password ?user
13   with
14     M.Error e as exc ->
15       prerr_endline e ;
16       raise exc
17
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)
21
22 let mk_new_msg () = ref []
23
24 let msg_output_string msg s = msg := s::!msg
25
26 let msg_serialize msg =
27   List.fold_left (fun acc s -> s ^ acc) "" !msg
28
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")
32
33 let msg_output_trailer msg =
34   msg_output_string msg "  </h:Object>\n</rdf:RDF>\n"
35
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")
39 ;;
40
41 let msg_output_dc_trailer msg =
42   msg_output_string msg "  </h:DirectoryOfObjects>\n</rdf:RDF>"
43 ;;
44
45 let value_of_optional_value =
46  function
47     None -> assert false
48   | Some v -> v
49 ;;
50
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 ;
55   M.iter res
56    ~f:(function cols ->
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"
63    ) ;
64   msg_output_trailer msg ;
65   msg_serialize msg
66 ;;
67
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 ;
72   M.iter res
73    ~f:(function cols ->
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"
80    ) ;
81   msg_output_trailer msg ;
82   msg_serialize msg
83 ;;
84
85 let dc_metadata db obj =
86  let tables =
87   [ "dc:creator","dccreator" ;
88     "dc:date","dcdate" ;
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"
105  ]
106  in
107   let msg = mk_new_msg () in
108   msg_output_dc_header msg obj ;
109   List.iter
110    (fun (propertyname,tablename) -> 
111      let res =
112       M.exec db
113        ("SELECT * FROM " ^ tablename ^ " WHERE uri = '" ^ obj ^ "';") in
114      M.iter res
115       ~f:(function cols ->
116         let value = value_of_optional_value (cols.(0)) in
117         msg_output_string msg
118          ("    <" ^ propertyname ^ ">" ^ value ^ "</" ^ propertyname ^ ">\n") ;
119       ) ;
120    ) tables ;
121    msg_output_dc_trailer msg ;
122    msg_serialize msg
123 ;;
124
125 let debug_print s = prerr_endline ("[RDFly] " ^ s)
126
127 let mk_return_fun contype msg outchan =
128   Http_daemon.respond
129     ~body:msg ~headers:["Content-Type", contype] outchan
130                                                                                                                                                                                     
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>")
135
136 let get_option key =
137   try
138     Some (Helm_registry.get key)
139   with Helm_registry.Key_not_found _ -> None
140
141 let get_int_option key =
142   try
143     Some (Helm_registry.get_int key)
144   with Helm_registry.Key_not_found _ -> None
145
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";;
152
153 let callback (req: Http_types.request) ch =
154   try
155     debug_print ("Connection from " ^ req#clientAddr) ;
156     debug_print ("Received request: " ^ req#path) ;
157     (match req#path with
158     | "/help" ->
159         return_html_error "yeah right..." ch
160     | "/get" ->
161       let obj = req#param "object"
162       and kind = req#param "kind" in
163       let db = open_db ?host ?database ?port ?password ?user () in
164       begin
165         match kind with
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
170       end ;
171       M.disconnect db
172     | invalid_request -> Http_daemon.respond_error ~status:(`Client_error `Bad_request) ch)
173   with
174   | Http_types.Param_not_found attr_name ->
175       return_400 (Printf.sprintf "Parameter '%s' is missing" attr_name) ch
176   | exc ->
177       return_html_error ("Uncaught exception: " ^ (Printexc.to_string exc)) ch
178
179 let main () =
180   Sys.catch_break true;
181   try
182     Http_daemon.start'
183       ~timeout:(Some 600) ~port:daemonport callback
184   with Sys.Break -> ()
185 in
186                                                                                                                                                                                     
187 main ()
188