Chris@10: :- module(urispace,[init/0]). Chris@10: Chris@10: :- use_module(library('http/thread_httpd')). Chris@27: :- use_module(library('http/http_cors')). Chris@10: :- use_module(library('semweb/rdf_db')). Chris@10: :- use_module(log). Chris@10: :- use_module(chord_parser). Chris@10: Chris@10: :- style_check(-discontiguous). Chris@10: Chris@27: :- set_setting_default(http:cors, [*]). Chris@27: Chris@27: Chris@10: server(Port, Options) :- Chris@10: http_server(reply,[ port(Port),timeout(20)| Options]). Chris@10: Chris@10: Chris@10: namespace('http://dbtune.org/chord'). Chris@10: Chris@10: /** Chris@10: * Handles documents Chris@10: */ Chris@10: reply(Request) :- Chris@10: member(path(Path),Request), Chris@10: atom_concat(SymbolT,'.rdf',Path), Chris@10: atom_concat('/',Symbol,SymbolT), Chris@27: cors_enable, Chris@10: !, Chris@10: (parse(Symbol,RDF) -> Chris@10: ( Chris@10: current_output(S), Chris@10: set_stream(S,encoding(utf8)), Chris@10: format('Content-type: application/rdf+xml; charset=UTF-8~n~n', []), Chris@10: rdf_write_xml(S,RDF)); Chris@10: ( Chris@10: throw(http_reply(server_error('The specified chord symbol is not valid~n~n'))) Chris@10: )). Chris@10: Chris@10: /** Chris@10: * Sends back towards a png representation of the chord Chris@10: */ Chris@10: reply(Request) :- Chris@10: member(path(Path),Request), Chris@10: member(accept(AcceptHeader),Request), Chris@10: log:log('Accept header: ~w ',[AcceptHeader]), Chris@10: accept_png(AcceptHeader), Chris@10: !, Chris@10: atom_concat('/',Symbol,Path), Chris@27: cors_enable, Chris@10: (parse(Symbol,RDF) -> Chris@10: ( Chris@10: member(rdf(_,'http://xmlns.com/foaf/0.1/depiction',Pic),RDF), Chris@10: throw(http_reply(see_other(Pic),[]))); Chris@10: ( Chris@10: throw(http_reply(server_error('The specified chord symbol is not valid~n~n'))) Chris@10: )). Chris@10: accept_png('image/png'). Chris@10: Chris@10: Chris@10: Chris@10: /** Chris@10: * Sends back 303 to RDF document describing the resource Chris@10: */ Chris@10: reply(Request) :- Chris@10: member(path(Path),Request), Chris@10: %member(accept(AcceptHeader),Request), Chris@10: accept_rdf(AcceptHeader), Chris@10: !, Chris@10: namespace(NS), Chris@10: format(atom(Redirect),'~w~w.rdf',[NS,Path]), Chris@10: log:log('Sending a 303 towards ~w',Redirect), Chris@27: cors_enable, Chris@10: throw(http_reply(see_other(Redirect),[])). Chris@10: Chris@10: accept_rdf('application/rdf+xml'). Chris@10: accept_rdf('text/xml'). Chris@10: accept_rdf(AcceptHeader) :- Chris@10: sub_atom(AcceptHeader,_,_,_,'application/rdf+xml'). Chris@10: accept_rdf(AcceptHeader) :- Chris@10: sub_atom(AcceptHeader,_,_,_,'text/xml'). Chris@10: accept_rdf(_). Chris@10: Chris@10: Chris@10: Chris@10: /** Chris@10: * Sends back towards the default representation of the resource Chris@10: * (usually html) Chris@10: */ Chris@10: Chris@10: html('http://www4.wiwiss.fu-berlin.de/rdf_browser/?browse_uri='). Chris@10: reply(Request) :- Chris@10: member(path(Path),Request), Chris@10: !, Chris@10: html(Html),namespace(Namespace), Chris@10: format(atom(Redirect),'~w~w~w',[Html,Namespace,Path]), Chris@10: log:log('Sending a 303 towards ~w',Redirect), Chris@27: cors_enable, Chris@10: throw(http_reply(see_other(Redirect),[])). Chris@10: Chris@10: port(1111). Chris@10: init :- Chris@10: port(P), Chris@10: server(P,[]), Chris@10: nl, Chris@10: writeln(' - Server launched'),nl. Chris@10: Chris@10: :- Chris@10: nl,writeln('----------------'),nl, Chris@10: writeln(' - Launch the server on port 1111 by running ?-init.'), Chris@10: nl,writeln('----------------'),nl. Chris@10: Chris@10: