34
35:- module(api_export, []). 36:- use_module(library(http/http_dispatch)). 37:- use_module(library(http/http_parameters)). 38:- use_module(library(semweb/rdf_turtle_write)). 39:- use_module(library(semweb/rdf_db)). 40:- use_module(library(semweb/rdf_schema)). 41:- use_module(rdfql(rdf_io)). 42:- use_module(rdfql(rdf_turtle_io)). 43:- use_module(user(user_db)). 44
45:- http_handler(api(export_graph), export_graph, []). 46:- http_handler(api(export_graph_schema), export_graph_schema, []). 47
51
59
60export_graph(Request) :-
61 http_parameters(Request,
62 [ graph(Graph),
63 format(Format),
64 mimetype(Mime)
65 ],
66 [ attribute_declarations(http_param)
67 ]
68 ),
69 authorized(read(default, download(Graph))),
70 send_graph(Graph, Format, Mime).
71
72send_graph(Graph, Format, default) :-
73 !,
74 default_mime_type(Format, MimeType),
75 send_graph(Graph, Format, MimeType).
76send_graph(Graph, Format, MimeType) :-
77 !,
78 format('Transfer-Encoding: chunked~n'),
79 format('Content-type: ~w; charset=UTF8~n~n', [MimeType]),
80 send_graph(Graph, Format).
81
82send_graph(Graph, turtle) :-
83 !,
84 rdf_save_turtle(stream(current_output),
85 [ graph(Graph),
86 base(Graph)
87 ]).
88send_graph(Graph, canonical_turtle) :-
89 !,
90 rdf_save_canonical_turtle(stream(current_output), [graph(Graph)]).
91send_graph(Graph, rdfxml) :-
92 !,
93 rdf_save(stream(current_output), [graph(Graph)]).
94
95default_mime_type(turtle, text/turtle).
96default_mime_type(canonical_turtle, text/turtle).
97default_mime_type(rdfxml, application/'rdf+xml').
98
105
106export_graph_schema(Request) :-
107 http_parameters(Request,
108 [ graph(Graph),
109 format(Format),
110 mimetype(Mime)
111 ],
112 [ attribute_declarations(http_param)
113 ]
114 ),
115 authorized(read(default, download(Graph))),
116 rdf_graph_schema(Graph, Triples),
117 ( Mime == default
118 -> default_mime_type(Format, MimeType)
119 ; MimeType = Mime
120 ),
121 write_graph(Triples,
122 [ serialization(Format),
123 mimetype(MimeType)
124 ]).
125
126
128
129http_param(graph,
130 [ description('Name of the graph')]).
131http_param(format,
132 [ oneof([turtle,
133 canonical_turtle,
134 rdfxml
135 ]),
136 default(turtle),
137 description('Output serialization')
138 ]).
139http_param(mimetype,
140 [ default(default),
141 description('MIME-type to use. If "default", it depends on format')
142 ])