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@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2010-2018, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   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
   48/** <module> Export data from the server
   49
   50*/
   51
   52%!  export_graph(+Request)
   53%
   54%   Export a named graph in a   given  serialization. Whether or not
   55%   exporting of a named graph is  defined by authorized/1 using the
   56%   term:
   57%
   58%           * read(default, download(Graph))
   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
   99%!  export_graph_schema(+Request)
  100%
  101%   HTTP handler that computes the schema from the actual data in a
  102%   graph.
  103%
  104%   @see The computation is implemented by rdf_graph_schema/2.
  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
  127%!  http_param(?Name, ?Attributes).
  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           ])