| 1 | package Apache::SPARQL; | ||
| 2 | |||
| 3 | $Apache::SPARQL::VERSION = '0.22'; | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 0 | ▶ | use mod_perl; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use LWP::UserAgent; | ||
| 9 | |||
| 10 | use constant MP2 => ($mod_perl::VERSION >= 1.99) ? 1 : 0; | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | if (MP2) { | ||
| 14 | require Apache2; | ||
| 15 | require Apache::RequestRec; | ||
| 16 | require Apache::RequestIO; | ||
| 17 | require Apache::RequestUtil; | ||
| 18 | require Apache::Const; | ||
| 19 | require Apache::Log; | ||
| 20 | require Apache::URI; | ||
| 21 | require Apache::File; | ||
| 22 | require APR::Table; | ||
| 23 | require APR::URI; | ||
| 24 | require CGI; | ||
| 25 | |||
| 26 | CGI->compile(qw(param)); | ||
| 27 | } else { | ||
| 28 | require Apache; | ||
| 29 | require Apache::Constants; | ||
| 30 | require Apache::Log; | ||
| 31 | require Apache::Request; | ||
| 32 | require Apache::URI; | ||
| 33 | require Apache::File; | ||
| 34 | }; | ||
| 35 | }; | ||
| 36 | |||
| 37 | # see http://www.w3.org/2001/sw/DataAccess/proto-wd/#mapping | ||
| 38 | %Apache::SPARQL::Responses = ( | ||
| 39 | 'OK' => 200, | ||
| 40 | 'GraphCreated' => 201, | ||
| 41 | 'OperationRequestAccepted' => 202, | ||
| 42 | 'PermanentlyMoved' => 301, | ||
| 43 | 'TemporarilyMoved' => 307, | ||
| 44 | 'MalformedRequest' => 400, | ||
| 45 | 'MalformedQuery' => 400, | ||
| 46 | 'Unauthorized' => 401, | ||
| 47 | 'Forbidden' => 403, | ||
| 48 | 'NotFound' => 404, | ||
| 49 | 'NoDeletionPerformed' => 409, | ||
| 50 | 'RequestRefused' => 409, | ||
| 51 | 'OperationPointError' => 500, | ||
| 52 | 'UnsupportedOperation' => 501, | ||
| 53 | 'Unavailable' => 503 | ||
| 54 | ); | ||
| 55 | |||
| 56 | sub handler($$) { | ||
| 57 | my ($class, $ap) = @_; | ||
| 58 | |||
| 59 | return $Apache::SPARQL::Responses{ 'MalformedRequest' } | ||
| 60 | unless( $ap->method eq 'GET' or | ||
| 61 | $ap->method eq 'OPTIONS' ); | ||
| 62 | |||
| 63 | my $uri_query_string = $ap->args; | ||
| 64 | if( $uri_query_string and | ||
| 65 | $uri_query_string ne '' and | ||
| 66 | $uri_query_string !~ m/^\s*$/ ) { | ||
| 67 | |||
| 68 | my $query_lang = $class->_param($ap, 'query-lang'); | ||
| 69 | $query_lang = 'sparql' | ||
| 70 | unless($query_lang); | ||
| 71 | |||
| 72 | my $query = $class->_param($ap, 'query'); | ||
| 73 | my $query_uri = $class->_param($ap, 'query-uri'); | ||
| 74 | my @graph_id = $class->_param($ap, 'graph-id'); | ||
| 75 | |||
| 76 | # see some more at http://lists.w3.org/Archives/Public/public-rdf-dawg/2005AprJun/0054.html | ||
| 77 | my @data = $class->_param($ap, 'data'); | ||
| 78 | push @graph_id, @data; #merge graph-id and data parameters - need to clarify this | ||
| 79 | my $format = $class->_param($ap, 'format'); | ||
| 80 | my $output_xslt = $class->_param($ap, 'output-xslt'); | ||
| 81 | my $output_type = $class->_param($ap, 'output-type'); | ||
| 82 | my $limit = $class->_param($ap, 'limit'); | ||
| 83 | |||
| 84 | # FIXME - if no query params assumes getGraph() - correct? | ||
| 85 | if( $query or | ||
| 86 | $query_uri ) { | ||
| 87 | return $class->query( $ap, $query, $query_uri, \@graph_id, $output_xslt, $output_type, $format, $limit ); | ||
| 88 | } else { | ||
| 89 | return $class->getGraph( $ap, \@graph_id, $format ); | ||
| 90 | }; | ||
| 91 | } else { | ||
| 92 | return $class->getServiceDescription( $ap ); | ||
| 93 | }; | ||
| 94 | }; | ||
| 95 | |||
| 96 | sub query { | ||
| 97 | my ($class, $ap, $query, $query_uri, $graph_id, $output_xslt, $output_type, $format, $limit ) = @_; | ||
| 98 | |||
| 99 | return $Apache::SPARQL::Responses{ 'MalformedRequest' }; | ||
| 100 | }; | ||
| 101 | |||
| 102 | sub getGraph { | ||
| 103 | my ($class, $ap, $graph_id, $format ) = @_; | ||
| 104 | |||
| 105 | return $Apache::SPARQL::Responses{ 'MalformedRequest' }; | ||
| 106 | }; | ||
| 107 | |||
| 108 | sub getServiceDescription { | ||
| 109 | my ($class, $ap) = @_; | ||
| 110 | |||
| 111 | return $Apache::SPARQL::Responses{ 'MalformedRequest' }; | ||
| 112 | }; | ||
| 113 | |||
| 114 | sub _cat { | ||
| 115 | my ($class, $ap, $file_or_uri) = @_; | ||
| 116 | |||
| 117 | my $subr = $ap->lookup_uri( $file_or_uri ); | ||
| 118 | my $filename = $subr->filename; | ||
| 119 | |||
| 120 | if( -e $filename && -r _ ) { | ||
| 121 | my $content=''; | ||
| 122 | my $fh = Apache::File->new($filename); | ||
| 123 | while(<$fh>) { | ||
| 124 | $content.=$_; | ||
| 125 | }; | ||
| 126 | close($fh); | ||
| 127 | return $content; | ||
| 128 | } else { | ||
| 129 | return $class->_wget( $ap, $file_or_uri ); | ||
| 130 | }; | ||
| 131 | }; | ||
| 132 | |||
| 133 | # does HTTP GET of given URI | ||
| 134 | sub _wget { | ||
| 135 | my ($class, $ap, $uri, $accept) = @_; | ||
| 136 | |||
| 137 | my $ua = LWP::UserAgent->new( timeout => 60 ); | ||
| 138 | |||
| 139 | my $server = $ap->server; | ||
| 140 | my %headers = ( "User-Agent" => "sparqlserver\@".$server->server_hostname."/".$Apache::SPARQL::VERSION ); | ||
| 141 | |||
| 142 | if( $accept ) { | ||
| 143 | $headers{'Accept'} = $accept; | ||
| 144 | } else { | ||
| 145 | #otherwise try any | ||
| 146 | $headers{'Accept'} = 'application/rdf+xml,application/xml,text/xml,application/x-turtle,application/turtle,text/rdf+n3;q=0.9,*/*;q=0.5'; | ||
| 147 | }; | ||
| 148 | |||
| 149 | my $response = $ua->get( $uri, %headers ); | ||
| 150 | |||
| 151 | return | ||
| 152 | unless($response->is_success); | ||
| 153 | |||
| 154 | return $response->content; | ||
| 155 | }; | ||
| 156 | |||
| 157 | sub _mp2 { | ||
| 158 | return MP2; | ||
| 159 | }; | ||
| 160 | |||
| 161 | sub _param { | ||
| 162 | my ($class, $ap, $field) = @_; | ||
| 163 | |||
| 164 | if ( $class->_mp2 ) { | ||
| 165 | if($field) { | ||
| 166 | return CGI::param($field); | ||
| 167 | } else { | ||
| 168 | my @params = CGI::param(); | ||
| 169 | return @params; | ||
| 170 | }; | ||
| 171 | }; | ||
| 172 | |||
| 173 | my $request = Apache::Request->new($ap); | ||
| 174 | |||
| 175 | if($field) { | ||
| 176 | return $request->param($field); | ||
| 177 | } else { | ||
| 178 | my @params = $request->param; | ||
| 179 | return @params; | ||
| 180 | }; | ||
| 181 | }; | ||
| 182 | |||
| 183 | sub _nometh { | ||
| 184 | my ($class, $ap, $field) = @_; | ||
| 185 | |||
| 186 | my $caller = (caller(1))[3]; | ||
| 187 | $caller =~ s/.*:://; | ||
| 188 | |||
| 189 | $ap->log()->error(sprintf("package %s does not define a '%s' method", $class, $caller)); | ||
| 190 | |||
| 191 | return 0; | ||
| 192 | }; | ||
| 193 | |||
| 194 | sub _header_in { | ||
| 195 | my ($class, $ap, $field) = @_; | ||
| 196 | |||
| 197 | return ( $class->_mp2 ) ? $ap->headers_in()->{$field} : $ap->header_in($field); | ||
| 198 | }; | ||
| 199 | |||
| 200 | sub _header_out { | ||
| 201 | my ($class, $ap, $field, $value) = @_; | ||
| 202 | |||
| 203 | ( $class->_mp2 ) ? $ap->headers_out()->{$field} = $value: $ap->header_out($field,$value); | ||
| 204 | }; | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | =head1 NAME | ||
| 209 | |||
| 210 | Apache::SPARQL - mod_perl handler base class to implement a SPARQL query service using HTTP bindings. | ||
| 211 | |||
| 212 | =head1 SYNOPSIS | ||
| 213 | |||
| 214 | <Location /rdfstore> | ||
| 215 | |||
| 216 | SetHandler perl-script | ||
| 217 | PerlHandler Apache::SPARQL | ||
| 218 | |||
| 219 | </Location> | ||
| 220 | |||
| 221 | # | ||
| 222 | |||
| 223 | my $ua = LWP::UserAgent->new(); | ||
| 224 | my $req = HTTP::Request->new(GET => "http://pim.example/rdfstore?query=PREFIX+foaf%3A+%3Chttp%3A%2F%2Fxmlns.com%2Ffoaf%2F0.1%2F%3E%0D%0ASELECT+%3Fpg%0D%0AWHERE%0D%0A++%28%3Fwho+foaf%3Aname+%22Norm+Walsh%22%29%0D%0A++%28%3Fwho+foaf%3Aweblog+%3Fpg%29%0D%0A"); | ||
| 225 | |||
| 226 | my $res = $ua->request($req); | ||
| 227 | |||
| 228 | =head1 DESCRIPTION | ||
| 229 | |||
| 230 | Apache::SPARQL is a mod_perl handler base class to implement a SPARQL query service using HTTP bindings. | ||
| 231 | |||
| 232 | =head1 IMPORTANT | ||
| 233 | |||
| 234 | This package is a base class and not expected to be invoked | ||
| 235 | directly. Please use one of the backends handlers instead. | ||
| 236 | |||
| 237 | =head1 SUPPPORTED BACKENDS | ||
| 238 | |||
| 239 | Apache::SPARQL::RDFStore | ||
| 240 | |||
| 241 | =head1 MOD_PERL COMPATIBILITY | ||
| 242 | |||
| 243 | This handler will work with both mod_perl 1.x and mod_perl 2.x. | ||
| 244 | |||
| 245 | =head1 AUTHOR | ||
| 246 | |||
| 247 | Alberto Reggiori <alberto@asemantics.com> | ||
| 248 | |||
| 249 | =head1 SEE ALSO | ||
| 250 | |||
| 251 | Apache::SPARQL::RDFStore | ||
| 252 | http://www.w3.org/TR/rdf-sparql-protocol/ | ||
| 253 | http://www.w3.org/2001/sw/DataAccess/proto-wd/ (editor working draft) | ||
| 254 | http://www.w3.org/2001/sw/DataAccess/prot26 | ||
| 255 | http://www.w3.org/TR/rdf-sparql-query/ | ||
| 256 | http://www.w3.org/2001/sw/DataAccess/rq23/ (edit working draft) | ||
| 257 | |||
| 258 | =head1 LICENSE | ||
| 259 | |||
| 260 | see LICENSE file included into this distribution | ||
| 261 | |||
| 262 | =cut | ||
| 263 | |||
| 264 | return 1; | ||