annotate chord/urispace/urispace.pl @ 27:d95e683fbd35 tip

Enable CORS on urispace redirects as well
author Chris Cannam
date Tue, 20 Feb 2018 14:52:02 +0000
parents 7902f428bc60
children
rev   line source
Chris@10 1 :- module(urispace,[init/0]).
Chris@10 2
Chris@10 3 :- use_module(library('http/thread_httpd')).
Chris@27 4 :- use_module(library('http/http_cors')).
Chris@10 5 :- use_module(library('semweb/rdf_db')).
Chris@10 6 :- use_module(log).
Chris@10 7 :- use_module(chord_parser).
Chris@10 8
Chris@10 9 :- style_check(-discontiguous).
Chris@10 10
Chris@27 11 :- set_setting_default(http:cors, [*]).
Chris@27 12
Chris@27 13
Chris@10 14 server(Port, Options) :-
Chris@10 15 http_server(reply,[ port(Port),timeout(20)| Options]).
Chris@10 16
Chris@10 17
Chris@10 18 namespace('http://dbtune.org/chord').
Chris@10 19
Chris@10 20 /**
Chris@10 21 * Handles documents
Chris@10 22 */
Chris@10 23 reply(Request) :-
Chris@10 24 member(path(Path),Request),
Chris@10 25 atom_concat(SymbolT,'.rdf',Path),
Chris@10 26 atom_concat('/',Symbol,SymbolT),
Chris@27 27 cors_enable,
Chris@10 28 !,
Chris@10 29 (parse(Symbol,RDF) ->
Chris@10 30 (
Chris@10 31 current_output(S),
Chris@10 32 set_stream(S,encoding(utf8)),
Chris@10 33 format('Content-type: application/rdf+xml; charset=UTF-8~n~n', []),
Chris@10 34 rdf_write_xml(S,RDF));
Chris@10 35 (
Chris@10 36 throw(http_reply(server_error('The specified chord symbol is not valid~n~n')))
Chris@10 37 )).
Chris@10 38
Chris@10 39 /**
Chris@10 40 * Sends back towards a png representation of the chord
Chris@10 41 */
Chris@10 42 reply(Request) :-
Chris@10 43 member(path(Path),Request),
Chris@10 44 member(accept(AcceptHeader),Request),
Chris@10 45 log:log('Accept header: ~w ',[AcceptHeader]),
Chris@10 46 accept_png(AcceptHeader),
Chris@10 47 !,
Chris@10 48 atom_concat('/',Symbol,Path),
Chris@27 49 cors_enable,
Chris@10 50 (parse(Symbol,RDF) ->
Chris@10 51 (
Chris@10 52 member(rdf(_,'http://xmlns.com/foaf/0.1/depiction',Pic),RDF),
Chris@10 53 throw(http_reply(see_other(Pic),[])));
Chris@10 54 (
Chris@10 55 throw(http_reply(server_error('The specified chord symbol is not valid~n~n')))
Chris@10 56 )).
Chris@10 57 accept_png('image/png').
Chris@10 58
Chris@10 59
Chris@10 60
Chris@10 61 /**
Chris@10 62 * Sends back 303 to RDF document describing the resource
Chris@10 63 */
Chris@10 64 reply(Request) :-
Chris@10 65 member(path(Path),Request),
Chris@10 66 %member(accept(AcceptHeader),Request),
Chris@10 67 accept_rdf(AcceptHeader),
Chris@10 68 !,
Chris@10 69 namespace(NS),
Chris@10 70 format(atom(Redirect),'~w~w.rdf',[NS,Path]),
Chris@10 71 log:log('Sending a 303 towards ~w',Redirect),
Chris@27 72 cors_enable,
Chris@10 73 throw(http_reply(see_other(Redirect),[])).
Chris@10 74
Chris@10 75 accept_rdf('application/rdf+xml').
Chris@10 76 accept_rdf('text/xml').
Chris@10 77 accept_rdf(AcceptHeader) :-
Chris@10 78 sub_atom(AcceptHeader,_,_,_,'application/rdf+xml').
Chris@10 79 accept_rdf(AcceptHeader) :-
Chris@10 80 sub_atom(AcceptHeader,_,_,_,'text/xml').
Chris@10 81 accept_rdf(_).
Chris@10 82
Chris@10 83
Chris@10 84
Chris@10 85 /**
Chris@10 86 * Sends back towards the default representation of the resource
Chris@10 87 * (usually html)
Chris@10 88 */
Chris@10 89
Chris@10 90 html('http://www4.wiwiss.fu-berlin.de/rdf_browser/?browse_uri=').
Chris@10 91 reply(Request) :-
Chris@10 92 member(path(Path),Request),
Chris@10 93 !,
Chris@10 94 html(Html),namespace(Namespace),
Chris@10 95 format(atom(Redirect),'~w~w~w',[Html,Namespace,Path]),
Chris@10 96 log:log('Sending a 303 towards ~w',Redirect),
Chris@27 97 cors_enable,
Chris@10 98 throw(http_reply(see_other(Redirect),[])).
Chris@10 99
Chris@10 100 port(1111).
Chris@10 101 init :-
Chris@10 102 port(P),
Chris@10 103 server(P,[]),
Chris@10 104 nl,
Chris@10 105 writeln(' - Server launched'),nl.
Chris@10 106
Chris@10 107 :-
Chris@10 108 nl,writeln('----------------'),nl,
Chris@10 109 writeln(' - Launch the server on port 1111 by running ?-init.'),
Chris@10 110 nl,writeln('----------------'),nl.
Chris@10 111
Chris@10 112