View source with formatted comments or as raw
    1/*  Part of ClioPatria SeRQL and SPARQL server
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@cs.vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (C): 2010, VU University Amsterdam
    7
    8    This program is free software; you can redistribute it and/or
    9    modify it under the terms of the GNU General Public License
   10    as published by the Free Software Foundation; either version 2
   11    of the License, or (at your option) any later version.
   12
   13    This program is distributed in the hope that it will be useful,
   14    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16    GNU General Public License for more details.
   17
   18    You should have received a copy of the GNU General Public
   19    License along with this library; if not, write to the Free Software
   20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21
   22    As a special exception, if you link this library with other files,
   23    compiled with a Free Software compiler, to produce an executable, this
   24    library does not by itself cause the resulting executable to be covered
   25    by the GNU General Public License. This exception does not however
   26    invalidate any other reasons why the executable file might be covered by
   27    the GNU General Public License.
   28*/
   29
   30:- module(api_export, []).   31:- use_module(library(http/http_dispatch)).   32:- use_module(library(http/http_parameters)).   33:- use_module(library(semweb/rdf_turtle_write)).   34:- use_module(library(semweb/rdf_db)).   35:- use_module(library(semweb/rdf_schema)).   36:- use_module(rdfql(rdf_io)).   37:- use_module(rdfql(rdf_turtle_io)).   38:- use_module(user(user_db)).   39
   40:- http_handler(api(export_graph),         export_graph,  []).   41:- http_handler(api(export_graph_schema),  export_graph_schema,  []).   42
   43/** <module> Export data from the server
   44
   45*/
   46
   47%%	export_graph(+Request)
   48%
   49%	Export a named graph in a   given  serialization. Whether or not
   50%	exporting of a named graph is  defined by authorized/1 using the
   51%	term:
   52%
   53%		* read(default, download(Graph))
   54
   55export_graph(Request) :-
   56	http_parameters(Request,
   57			[ graph(Graph),
   58			  format(Format),
   59			  mimetype(Mime)
   60			],
   61			[ attribute_declarations(http_param)
   62			]
   63		       ),
   64	authorized(read(default, download(Graph))),
   65	send_graph(Graph, Format, Mime).
   66
   67send_graph(Graph, Format, default) :- !,
   68	default_mime_type(Format, MimeType),
   69	send_graph(Graph, Format, MimeType).
   70send_graph(Graph, Format, MimeType) :- !,
   71	format('Transfer-Encoding: chunked~n'),
   72	format('Content-type: ~w; charset=UTF8~n~n', [MimeType]),
   73	send_graph(Graph, Format).
   74
   75send_graph(Graph, turtle) :- !,
   76	rdf_save_turtle(stream(current_output),
   77			[ graph(Graph),
   78			  base(Graph)
   79			]).
   80send_graph(Graph, canonical_turtle) :- !,
   81	rdf_save_canonical_turtle(stream(current_output), [graph(Graph)]).
   82send_graph(Graph, rdfxml) :- !,
   83	rdf_save(stream(current_output), [graph(Graph)]).
   84
   85default_mime_type(turtle, text/turtle).
   86default_mime_type(canonical_turtle, text/turtle).
   87default_mime_type(rdfxml, application/'rdf+xml').
   88
   89%%	export_graph_schema(+Request)
   90%
   91%	HTTP handler that computes the schema from the actual data in a
   92%	graph.
   93%
   94%	@see The computation is implemented by rdf_graph_schema/2.
   95
   96export_graph_schema(Request) :-
   97	http_parameters(Request,
   98			[ graph(Graph),
   99			  format(Format),
  100			  mimetype(Mime)
  101			],
  102			[ attribute_declarations(http_param)
  103			]
  104		       ),
  105	authorized(read(default, download(Graph))),
  106	rdf_graph_schema(Graph, Triples),
  107	(   Mime == default
  108	->  default_mime_type(Format, MimeType)
  109	;   MimeType = Mime
  110	),
  111	write_graph(Triples,
  112		    [ serialization(Format),
  113		      mimetype(MimeType)
  114		    ]).
  115
  116
  117%%	http_param(?Name, ?Attributes).
  118
  119http_param(graph,
  120	   [ description('Name of the graph')]).
  121http_param(format,
  122	   [ oneof([turtle,
  123		    canonical_turtle,
  124		    rdfxml
  125		   ]),
  126	     default(turtle),
  127	     description('Output serialization')
  128	   ]).
  129http_param(mimetype,
  130	   [ default(default),
  131	     description('MIME-type to use. If "default", it depends on format')
  132	   ])