| 1 | =pod | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Geo::OGC::Service - Perl extension for geospatial web services | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | In a service.psgi file write something like this | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | use Plack::Builder; | ||
| 14 | use Geo::OGC::Service; | ||
| 15 | use Geo::OGC::Service::XXX; | ||
| 16 | my $server = Geo::OGC::Service->new({ | ||
| 17 | config => '/var/www/etc/test.conf', | ||
| 18 | services => { | ||
| 19 | XXX => 'Geo::OGC::Service::XXX', | ||
| 20 | } | ||
| 21 | }); | ||
| 22 | builder { | ||
| 23 | mount "/XXX" => $server->to_app; | ||
| 24 | mount "/" => $default_app; | ||
| 25 | }; | ||
| 26 | |||
| 27 | The bones of a service class are | ||
| 28 | |||
| 29 | package Geo::OGC::Service::XXX; | ||
| 30 | sub process_request { | ||
| 31 | my ($self, $responder) = @_; | ||
| 32 | my $writer = $responder->([200, [ 'Content-Type' => 'text/plain', | ||
| 33 | 'Content-Encoding' => 'UTF-8' ]]); | ||
| 34 | $writer->write("I'm ok!"); | ||
| 35 | $writer->close; | ||
| 36 | } | ||
| 37 | |||
| 38 | Geo::OGC::Service::WFS exists in the CPAN and Geo::OGC::Service::WMTS | ||
| 39 | will be there real soon now. | ||
| 40 | |||
| 41 | =head1 DESCRIPTION | ||
| 42 | |||
| 43 | Geo::OGC::Service is a subclass of Plack::Component and a middleware | ||
| 44 | between a web client and an actual content providing service object. | ||
| 45 | A Geo::OGC::Service object has a to_app method for booting a web | ||
| 46 | service. | ||
| 47 | |||
| 48 | A Geo::OGC::Service object creates a specialized service object as a | ||
| 49 | result of a web service request. The specialized service object is a | ||
| 50 | hash reference blessed into an appropriate class (the class is deduced | ||
| 51 | from GET parameter, POSTed service name, or from the script name in | ||
| 52 | the case of RESTful services). The new object contains keys env, | ||
| 53 | request, plugin, config, service, and optionally posted, filter, and | ||
| 54 | parameters. | ||
| 55 | |||
| 56 | =over | ||
| 57 | |||
| 58 | =item env | ||
| 59 | |||
| 60 | The PSGI $env. | ||
| 61 | |||
| 62 | =item request | ||
| 63 | |||
| 64 | A Plack::Request object constructed from the $env; | ||
| 65 | |||
| 66 | =item plugin | ||
| 67 | |||
| 68 | The plugin object given as an argument to Geo::OGC::Service in its | ||
| 69 | constructor as a top level attribute or as a service specific | ||
| 70 | attribute. | ||
| 71 | |||
| 72 | =item config | ||
| 73 | |||
| 74 | The constructed configuration for the web service. | ||
| 75 | |||
| 76 | =item service | ||
| 77 | |||
| 78 | The name of the requested service. | ||
| 79 | |||
| 80 | =item parameters | ||
| 81 | |||
| 82 | A hash made from Plack::Request->parameters (thus removing its multi | ||
| 83 | value nature). The keys are all converted to lower case and the values | ||
| 84 | are decoded to Perl's internal format assuming they are in the | ||
| 85 | encoding defined $request->content_encoding (or UTF-8). | ||
| 86 | |||
| 87 | =item posted | ||
| 88 | |||
| 89 | A XML::LibXML documentElement of the POSTed XML. The XML is decoded | ||
| 90 | into Perl's internal format. | ||
| 91 | |||
| 92 | =item filter | ||
| 93 | |||
| 94 | A XML::LibXML documentElement contructed from a filter GET | ||
| 95 | parameter. The XML is decoded into Perl's internal format. | ||
| 96 | |||
| 97 | =back | ||
| 98 | |||
| 99 | =head2 SERVICE CONFIGURATION | ||
| 100 | |||
| 101 | Setting up a PSGI service consists typically of three things: | ||
| 102 | |||
| 103 | 1) write a service.psgi file (see above) and put it somewhere like | ||
| 104 | |||
| 105 | /var/www/service/service.psgi | ||
| 106 | |||
| 107 | 2) Set up starman service and add to its init-file line something like | ||
| 108 | |||
| 109 | exec starman --daemonize --error-log /var/log/starman/log --l localhost:5000 /var/www/service/service.psgi | ||
| 110 | |||
| 111 | 3) Add a proxy service to your httpd configuration. For Apache it | ||
| 112 | would be something like this: | ||
| 113 | |||
| 114 | <Location /Service> | ||
| 115 | ProxyPass http://localhost:5000 | ||
| 116 | ProxyPassReverse http://localhost:5000 | ||
| 117 | </Location> | ||
| 118 | |||
| 119 | Setting up a geospatial web service through this module requires a | ||
| 120 | configuration file, for example | ||
| 121 | |||
| 122 | /var/www/etc/service.conf | ||
| 123 | |||
| 124 | (make sure this file is not served by your httpd) | ||
| 125 | |||
| 126 | The configuration must be in JSON format. I.e., something like | ||
| 127 | |||
| 128 | { | ||
| 129 | Common: { | ||
| 130 | "CORS": { | ||
| 131 | "Allow-Origin" : "*", | ||
| 132 | "Allow-Headers" : "Content-Type, X-Requested-With" | ||
| 133 | }, | ||
| 134 | "Content-Type": "text/xml; charset=utf-8", | ||
| 135 | "TARGET_NAMESPACE": "http://ogr.maptools.org/" | ||
| 136 | }, | ||
| 137 | WFS: { | ||
| 138 | "resource": "http://$HTTP_HOST/WFS", | ||
| 139 | "version": "1.1.0", | ||
| 140 | "TARGET_NAMESPACE": "http://ogr.maptools.org/", | ||
| 141 | "PREFIX": "ogr", | ||
| 142 | "Transaction": "Insert,Update,Delete", | ||
| 143 | "FeatureTypeList": [ | ||
| 144 | { | ||
| 145 | } | ||
| 146 | ] | ||
| 147 | }, | ||
| 148 | "WMS": { | ||
| 149 | "resource": "http://$HTTP_HOST/WMS" | ||
| 150 | }, | ||
| 151 | "TMS": { | ||
| 152 | "resource": "http://$HTTP_HOST/TMS" | ||
| 153 | }, | ||
| 154 | "WMTS": { | ||
| 155 | "resource": "http://$HTTP_HOST/WMTS" | ||
| 156 | }, | ||
| 157 | "TileSets": [ | ||
| 158 | ], | ||
| 159 | "BoundingBox3857": { | ||
| 160 | "SRS": "EPSG:3857", | ||
| 161 | "minx": 2399767, | ||
| 162 | "miny": 8645741, | ||
| 163 | "maxx": 2473612, | ||
| 164 | "maxy": 8688005 | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | The keys and structure of this file depend on the type of the | ||
| 169 | service(s) you are setting up. "CORS" is the only one that is | ||
| 170 | recognized by this module. "CORS" is either a string denoting the | ||
| 171 | allowed origin or a hash of "Allow-Origin", "Allow-Methods", | ||
| 172 | "Allow-Headers", and "Max-Age". | ||
| 173 | |||
| 174 | $HTTP_HOST and $SCRIPT_NAME are replaced in runtime to the HTTP_HOST | ||
| 175 | and SCRIPT_NAME values respectively in the environment given by Plack. | ||
| 176 | |||
| 177 | =head2 EXPORT | ||
| 178 | |||
| 179 | None by default. | ||
| 180 | |||
| 181 | =head2 METHODS | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | package Geo::OGC::Service; | ||
| 186 | |||
| 187 | use 5.010000; # say // and //= | ||
| 188 | use Carp; | ||
| 189 | use Modern::Perl; | ||
| 190 | use Encode qw(decode encode); | ||
| 191 | use Plack::Request; | ||
| 192 | use Plack::Builder; | ||
| 193 | use JSON; | ||
| 194 | use XML::LibXML; | ||
| 195 | use Clone 'clone'; | ||
| 196 | use XML::LibXML::PrettyPrint; | ||
| 197 | |||
| 198 | use parent qw/Plack::Component/; | ||
| 199 | |||
| 200 | binmode STDERR, ":utf8"; | ||
| 201 | |||
| 202 | our $VERSION = '0.14'; | ||
| 203 | |||
| 204 | =pod | ||
| 205 | |||
| 206 | =head3 new | ||
| 207 | |||
| 208 | This creates a new Geo::OGC::Service app. You need to call it in the | ||
| 209 | psgi file as a class method with a named parameter hash reference. The | ||
| 210 | parameters are | ||
| 211 | |||
| 212 | config, services | ||
| 213 | |||
| 214 | config is required and it is a path to a file or a reference to an | ||
| 215 | anonymous hash containing the configuration for the services. The top | ||
| 216 | level keys are service names. If it is a file, it is expected to be | ||
| 217 | JSON. A configuration in a file may use top level Common hash and | ||
| 218 | references. A reference is a key,value pair, where the value begins | ||
| 219 | with 'ref:/' followed by a top level key name. The Common block is | ||
| 220 | cloned and references are solved and cloned into each service | ||
| 221 | configuration. | ||
| 222 | |||
| 223 | services is a reference to a hash of service names associated with | ||
| 224 | names of classes, which will process service requests. The key of the | ||
| 225 | hash is the requested service. | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | sub new { | ||
| 230 | my ($class, $parameters) = @_; | ||
| 231 | my $self = Plack::Component->new($parameters); | ||
| 232 | if (not ref $self->{config}) { | ||
| 233 | open my $fh, '<', $self->{config} or croak "Can't open file '$self->{config}': $!\n"; | ||
| 234 | my @json = <$fh>; | ||
| 235 | close $fh; | ||
| 236 | $self->{config} = decode_json "@json"; | ||
| 237 | expand_config($self->{config}); | ||
| 238 | $self->{config}{debug} //= 0; | ||
| 239 | } | ||
| 240 | croak "A configuration file is needed." unless $self->{config}; | ||
| 241 | croak "No services are defined." unless $self->{services}; | ||
| 242 | return bless $self, $class; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub expand_config { | ||
| 246 | my $config = shift; | ||
| 247 | my $had_ref; | ||
| 248 | do { | ||
| 249 | $had_ref = 0; | ||
| 250 | for my $j (keys %$config) { | ||
| 251 | $had_ref += config_ref($config->{$j}, $config); | ||
| 252 | } | ||
| 253 | } while ($had_ref); | ||
| 254 | for my $j (keys %$config) { | ||
| 255 | next if $j eq 'Common'; | ||
| 256 | next if $j =~ /^BoundingBox/; | ||
| 257 | next unless ref $config->{$j} eq 'HASH'; | ||
| 258 | for my $c (keys %{$config->{Common}}) { | ||
| 259 | $config->{$j}{$c} //= clone($config->{Common}{$c}); | ||
| 260 | } | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | sub config_ref { | ||
| 265 | my ($config, $refs) = @_; | ||
| 266 | my $had_ref = 0; | ||
| 267 | if (ref $config eq 'ARRAY') { | ||
| 268 | for my $j (@$config) { | ||
| 269 | $had_ref += config_ref($j, $refs); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | elsif (ref $config eq 'HASH') { | ||
| 273 | for my $j (keys %$config) { | ||
| 274 | my $r = $config->{$j}; | ||
| 275 | if (ref $r) { | ||
| 276 | $had_ref += config_ref($r, $refs); | ||
| 277 | } else { | ||
| 278 | if ($r =~ /^ref:/) { | ||
| 279 | my $target = config_target($refs, $r); | ||
| 280 | croak "config reference not found: '$r'." unless $target; | ||
| 281 | $config->{$j} = clone($target); | ||
| 282 | $had_ref = 1; | ||
| 283 | } | ||
| 284 | } | ||
| 285 | } | ||
| 286 | } | ||
| 287 | return $had_ref; | ||
| 288 | } | ||
| 289 | |||
| 290 | sub config_target { | ||
| 291 | my ($config, $ref) = @_; | ||
| 292 | my @path = split /\//, $ref; | ||
| 293 | shift @path; | ||
| 294 | if (ref $config eq 'HASH') { | ||
| 295 | return $config->{$path[0]}; | ||
| 296 | } | ||
| 297 | return undef; | ||
| 298 | } | ||
| 299 | |||
| 300 | =pod | ||
| 301 | |||
| 302 | =head3 call | ||
| 303 | |||
| 304 | This method is called internally by the method to_app of | ||
| 305 | Plack::Component. The method fails unless this module | ||
| 306 | is running in a psgi.streaming environment. Otherwise, | ||
| 307 | it returns a subroutine, which calls the respond method. | ||
| 308 | |||
| 309 | =cut | ||
| 310 | |||
| 311 | sub call { | ||
| 312 | my ($self, $env) = @_; | ||
| 313 | if (! $env->{'psgi.streaming'}) { # after Lyra-Core/lib/Lyra/Trait/Async/PsgiApp.pm | ||
| 314 | return [ 500, ["Content-Type" => "text/plain"], ["Internal Server Error (Server Implementation Mismatch)"] ]; | ||
| 315 | } | ||
| 316 | return sub { | ||
| 317 | my $responder = shift; | ||
| 318 | $self->respond($responder, $env); | ||
| 319 | } | ||
| 320 | } | ||
| 321 | |||
| 322 | =pod | ||
| 323 | |||
| 324 | =head3 respond | ||
| 325 | |||
| 326 | This method is called for each request from the Internet. The call is | ||
| 327 | responded during the execution of the subroutine. | ||
| 328 | |||
| 329 | In the default case this method constructs a new service object using | ||
| 330 | the method 'service' and calls its process_request method with PSGI | ||
| 331 | style $responder object as a parameter. | ||
| 332 | |||
| 333 | This subroutine may fail while interpreting the request, or while | ||
| 334 | processing the request. | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | sub respond { | ||
| 339 | my ($self, $responder, $env) = @_; | ||
| 340 | # TODO: logging | ||
| 341 | if ($env->{REQUEST_METHOD} eq 'OPTIONS') { | ||
| 342 | $responder->([200, ['Content-Length' => 0, | ||
| 343 | 'Content-Type' => 'text/plain', | ||
| 344 | Geo::OGC::Service::Common::CORS($self)]]); | ||
| 345 | } else { | ||
| 346 | my $service; | ||
| 347 | eval { | ||
| 348 | $service = $self->service($responder, $env); | ||
| 349 | }; | ||
| 350 | if ($@) { | ||
| 351 | print STDERR "$@"; | ||
| 352 | error($responder, { exceptionCode => 'ResourceNotFound', | ||
| 353 | ExceptionText => "Internal error while interpreting the request." } ); | ||
| 354 | } elsif ($service) { | ||
| 355 | eval { | ||
| 356 | $service->process_request($responder); | ||
| 357 | }; | ||
| 358 | if ($@) { | ||
| 359 | print STDERR "$@"; | ||
| 360 | error($responder, { exceptionCode => 'ResourceNotFound', | ||
| 361 | ExceptionText => "Internal error while processing the request." } ); | ||
| 362 | } | ||
| 363 | } | ||
| 364 | } | ||
| 365 | } | ||
| 366 | |||
| 367 | =pod | ||
| 368 | |||
| 369 | =head3 service | ||
| 370 | |||
| 371 | This method does a preliminary interpretation of the request and | ||
| 372 | converts it into a service object, which is returned. | ||
| 373 | |||
| 374 | The returned service object contains | ||
| 375 | |||
| 376 | config => the configuration for this type of service | ||
| 377 | env => the PSGI environment | ||
| 378 | |||
| 379 | and may contain | ||
| 380 | |||
| 381 | plugin => plugin object | ||
| 382 | |||
| 383 | posted => XML::LibXML DOM document element of the posted data | ||
| 384 | filter => XML::LibXML DOM document element of the filter | ||
| 385 | parameters => hash of rquest parameters obtained from Plack::Request | ||
| 386 | |||
| 387 | config is the service specific part of the config given to this | ||
| 388 | Geo::OGC::Service object in its constructor, possibly worked to clone | ||
| 389 | the Common block and references. The service should treat it strictly | ||
| 390 | read-only as it is shared between workers. | ||
| 391 | |||
| 392 | plugin is the plugin object that was given to this Geo::OGC::Service | ||
| 393 | object in its constructor. | ||
| 394 | |||
| 395 | Note: all keys in request parameters are converted to lower case in | ||
| 396 | parameters. | ||
| 397 | |||
| 398 | This subroutine may fail due to a request for an unknown service. The | ||
| 399 | error is reported as an XML message using OGC conventions. | ||
| 400 | |||
| 401 | =cut | ||
| 402 | |||
| 403 | sub service { | ||
| 404 | my ($self, $responder, $env) = @_; | ||
| 405 | |||
| 406 | my $request = Plack::Request->new($env); | ||
| 407 | my $parameters = $request->parameters; | ||
| 408 | |||
| 409 | my $service = { | ||
| 410 | env => $env, | ||
| 411 | request => $request, | ||
| 412 | # will get below: | ||
| 413 | # plugin | ||
| 414 | # posted | ||
| 415 | # filter | ||
| 416 | # parameters | ||
| 417 | # service | ||
| 418 | # config | ||
| 419 | }; | ||
| 420 | |||
| 421 | my $encoding = $request->content_encoding // 'utf8'; | ||
| 422 | |||
| 423 | my %names; | ||
| 424 | for my $key (sort keys %$parameters) { | ||
| 425 | $names{lc($key)} = $key; | ||
| 426 | $parameters->{$key} = decode $encoding => $parameters->{$key}; | ||
| 427 | } | ||
| 428 | |||
| 429 | my $post = $names{postdata} // $names{'xforms:model'}; | ||
| 430 | $post = $post ? $parameters->{$post} : decode($encoding, $request->content); | ||
| 431 | |||
| 432 | if ($post) { | ||
| 433 | my $parser = XML::LibXML->new(no_blanks => 1); | ||
| 434 | my $dom; | ||
| 435 | eval { | ||
| 436 | $dom = $parser->load_xml(string => $post); | ||
| 437 | }; | ||
| 438 | if ($@) { | ||
| 439 | error($responder, { exceptionCode => 'ResourceNotFound', | ||
| 440 | ExceptionText => "Error in posted XML:\n$@" } ); | ||
| 441 | return; | ||
| 442 | } | ||
| 443 | $service->{posted} = $dom->documentElement(); | ||
| 444 | } else { | ||
| 445 | for my $key (keys %names) { | ||
| 446 | if ($key eq 'filter' and $parameters->{$names{filter}} =~ /^</) { | ||
| 447 | my $filter = $parameters->{$names{filter}}; | ||
| 448 | my $s = '<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">'; | ||
| 449 | $filter =~ s/<ogc:Filter>/$s/; | ||
| 450 | my $parser = XML::LibXML->new(no_blanks => 1); | ||
| 451 | my $dom; | ||
| 452 | eval { | ||
| 453 | $dom = $parser->load_xml(string => $filter); | ||
| 454 | }; | ||
| 455 | if ($@) { | ||
| 456 | error($responder, { exceptionCode => 'ResourceNotFound', | ||
| 457 | ExceptionText => "Error in XML filter:\n$@" } ); | ||
| 458 | return; | ||
| 459 | } | ||
| 460 | $service->{filter} = $dom->documentElement(); | ||
| 461 | } else { | ||
| 462 | $service->{parameters}{$key} = $parameters->{$names{$key}}; | ||
| 463 | } | ||
| 464 | } | ||
| 465 | } | ||
| 466 | |||
| 467 | # service may also be an attribute in the top element of posted XML | ||
| 468 | my $service_from_posted = sub { | ||
| 469 | my $node = shift; | ||
| 470 | return undef unless $node; | ||
| 471 | return $node->getAttribute('service'); | ||
| 472 | }; | ||
| 473 | |||
| 474 | # RESTful way to define the service | ||
| 475 | my $service_from_script_name = sub { | ||
| 476 | my $env = shift; | ||
| 477 | my ($script_name) = $env->{SCRIPT_NAME} =~ /(\w+)$/; | ||
| 478 | return $script_name; | ||
| 479 | }; | ||
| 480 | |||
| 481 | my $requested_service = $parameters->{service} // | ||
| 482 | $service_from_posted->($service->{posted}) // | ||
| 483 | $service_from_script_name->($env) // ''; | ||
| 484 | |||
| 485 | if (exists $self->{services}{$requested_service}) { | ||
| 486 | $service->{service} = $requested_service; | ||
| 487 | my $class = $self->{services}{$requested_service}; | ||
| 488 | if (ref $class) { | ||
| 489 | $service->{plugin} = $class->{plugin}; | ||
| 490 | $class = $class->{service}; | ||
| 491 | } | ||
| 492 | $service->{plugin} //= $self->{plugin}; | ||
| 493 | my $config = $self->{config}; | ||
| 494 | $service->{config} = get_config($config, $requested_service); | ||
| 495 | if ($service->{config}{resource}) { | ||
| 496 | my $host = $env->{HTTP_HOST}; | ||
| 497 | $service->{config}{resource} =~ s/\$HTTP_HOST/$host/ if $host; | ||
| 498 | my $script = $env->{SCRIPT_NAME}; | ||
| 499 | $service->{config}{resource} =~ s/\$SCRIPT_NAME/$script/ if $script; | ||
| 500 | } | ||
| 501 | return bless $service, $class; | ||
| 502 | } | ||
| 503 | |||
| 504 | error($responder, { exceptionCode => 'InvalidParameterValue', | ||
| 505 | locator => 'service', | ||
| 506 | ExceptionText => "'$requested_service' is not a known service to this server" } ); | ||
| 507 | return undef; | ||
| 508 | } | ||
| 509 | |||
| 510 | # the value of the key, whose name is the service | ||
| 511 | # may be the config for the service | ||
| 512 | # or it may be the name of the key | ||
| 513 | # whose value is the config for the service | ||
| 514 | sub get_config { | ||
| 515 | my ($config, $service) = @_; | ||
| 516 | if (exists $config->{$service}) { | ||
| 517 | if (ref $config->{$service}) { | ||
| 518 | return $config->{$service}; | ||
| 519 | } | ||
| 520 | if (ref $config->{$config->{$service}}) { | ||
| 521 | return $config->{$config->{$service}}; | ||
| 522 | } | ||
| 523 | return undef; | ||
| 524 | } | ||
| 525 | return $config; | ||
| 526 | } | ||
| 527 | |||
| 528 | =pod | ||
| 529 | |||
| 530 | =head3 error($responder, $msg) | ||
| 531 | |||
| 532 | Stream an error report as an XML message of type | ||
| 533 | |||
| 534 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 535 | <ExceptionReport> | ||
| 536 | <Exception exceptionCode="$msg->{exceptionCode}" locator="$msg->{locator}"> | ||
| 537 | <ExceptionText>$msg->{ExceptionText}<ExceptionText> | ||
| 538 | <Exception> | ||
| 539 | </ExceptionReport> | ||
| 540 | |||
| 541 | =cut | ||
| 542 | |||
| 543 | sub error { | ||
| 544 | my ($responder, $msg, $headers) = @_; | ||
| 545 | my $writer = Geo::OGC::Service::XMLWriter::Caching->new($headers); | ||
| 546 | $writer->open_element('ExceptionReport', { version => "1.0" }); | ||
| 547 | my $attributes = { exceptionCode => $msg->{exceptionCode} }; | ||
| 548 | my $content; | ||
| 549 | $content = [ ExceptionText => $msg->{ExceptionText} ] if exists $msg->{ExceptionText}; | ||
| 550 | if (exists $msg->{locator}) { | ||
| 551 | $attributes->{locator} = $msg->{locator}; | ||
| 552 | } | ||
| 553 | $writer->element('Exception', $attributes, $content); | ||
| 554 | $writer->close_element; | ||
| 555 | $writer->stream($responder); | ||
| 556 | } | ||
| 557 | |||
| 558 | =pod | ||
| 559 | |||
| 560 | =head1 Geo::OGC::Service::Common | ||
| 561 | |||
| 562 | A base type for all OGC services. | ||
| 563 | |||
| 564 | =head2 SYNOPSIS | ||
| 565 | |||
| 566 | $service->DescribeService($writer); | ||
| 567 | $service->Operation($writer, $operation, $protocols, $parameters); | ||
| 568 | |||
| 569 | =head2 DESCRIPTION | ||
| 570 | |||
| 571 | The class contains methods for common tasks for all services. | ||
| 572 | |||
| 573 | =head2 METHODS | ||
| 574 | |||
| 575 | =cut | ||
| 576 | |||
| 577 | package Geo::OGC::Service::Common; | ||
| 578 | use Modern::Perl; | ||
| 579 | |||
| 580 | =pod | ||
| 581 | |||
| 582 | =head3 CORS | ||
| 583 | |||
| 584 | Return the CORS headers as a list according to the configuration. CORS | ||
| 585 | may be in the configuration as a scalar or as a hash. A scalar value | ||
| 586 | is taken as a value for Access-Control-Allow-Origin. A hash may have | ||
| 587 | the following keys. (Note the missing prefix Access-Control-.) | ||
| 588 | |||
| 589 | key default value | ||
| 590 | ----------------- ---------------------------------- | ||
| 591 | Allow-Origin | ||
| 592 | Allow-Credentials | ||
| 593 | Expose-Headers | ||
| 594 | Max-Age 60*60*24 | ||
| 595 | Allow-Methods GET,POST | ||
| 596 | Allow-Headers origin,x-requested-with,content-type | ||
| 597 | |||
| 598 | =cut | ||
| 599 | |||
| 600 | sub CORS { | ||
| 601 | my $self = shift; | ||
| 602 | # default CORS response headers: | ||
| 603 | my %default = ( | ||
| 604 | 'Allow-Origin' => '', | ||
| 605 | 'Allow-Credentials' => '', | ||
| 606 | 'Expose-Headers' => '', | ||
| 607 | 'Max-Age' => 60*60*24, | ||
| 608 | 'Allow-Methods' => 'GET,POST', | ||
| 609 | 'Allow-Headers' => 'origin,x-requested-with,content-type' | ||
| 610 | ); | ||
| 611 | # where CORS is in the configuration | ||
| 612 | my $config = $self->{config}{Common}{CORS} // $self->{config}{CORS}; | ||
| 613 | my @cors; | ||
| 614 | if (ref $config eq 'HASH') { | ||
| 615 | for my $key (keys %default) { | ||
| 616 | my $val = $config->{$key} // $default{$key}; | ||
| 617 | push @cors, ('Access-Control-'.$key => $val); | ||
| 618 | } | ||
| 619 | } else { | ||
| 620 | $default{'Allow-Origin'} = $config; | ||
| 621 | for my $key (keys %default) { | ||
| 622 | my $val = $default{$key}; | ||
| 623 | push @cors, ('Access-Control-'.$key => $val); | ||
| 624 | } | ||
| 625 | } | ||
| 626 | return @cors; | ||
| 627 | } | ||
| 628 | |||
| 629 | =pod | ||
| 630 | |||
| 631 | =head3 DescribeService($writer) | ||
| 632 | |||
| 633 | Create ows:ServiceIdentification and ows:ServiceProvider elements. | ||
| 634 | |||
| 635 | =cut | ||
| 636 | |||
| 637 | sub DescribeService { | ||
| 638 | my ($self, $writer) = @_; | ||
| 639 | $writer->element('ows:ServiceIdentification', | ||
| 640 | [['ows:Title' => $self->{config}{Title} // "Yet another $self->{service} server"], | ||
| 641 | ['ows:Abstract' => $self->{config}{Abstract} // ''], | ||
| 642 | ['ows:ServiceType', {codeSpace=>"OGC"}, "OGC $self->{service}"], | ||
| 643 | ['ows:ServiceTypeVersion', $self->{config}{ServiceTypeVersion} // '1.0.0'], | ||
| 644 | ['ows:Fees' => $self->{config}{Fees} // 'NONE'], | ||
| 645 | ['ows:AccessConstraints' => $self->{config}{AccessConstraints} // 'NONE']]); | ||
| 646 | $writer->element('ows:ServiceProvider', | ||
| 647 | [['ows:ProviderName' => $self->{config}{ProviderName} // 'Nobody in particular'], | ||
| 648 | ['ows:ProviderSite', { 'xlink:type'=>"simple", | ||
| 649 | 'xlink:href' => $self->{config}{ProviderSite} // '' }], | ||
| 650 | ['ows:ServiceContact' => $self->{config}{ServiceContact}]]); | ||
| 651 | } | ||
| 652 | |||
| 653 | =pod | ||
| 654 | |||
| 655 | =head3 Operation($writer, $operation, $protocols, $parameters) | ||
| 656 | |||
| 657 | Create ows:Operation element and its ows:DCP and ows:Parameter sub | ||
| 658 | elements. | ||
| 659 | |||
| 660 | =cut | ||
| 661 | |||
| 662 | sub Operation { | ||
| 663 | my ($self, $writer, $operation, $protocols, $parameters) = @_; | ||
| 664 | my @p; | ||
| 665 | for my $p (@$parameters) { | ||
| 666 | for my $n (keys %$p) { | ||
| 667 | push @p, [$self->Parameter($n, $p->{$n})]; | ||
| 668 | } | ||
| 669 | } | ||
| 670 | my $constraint; | ||
| 671 | $constraint = [ 'ows:Constraint' => {name => 'GetEncoding'}, $protocols->{Get} ] if ref $protocols->{Get}; | ||
| 672 | my @http; | ||
| 673 | push @http, [ 'ows:Get' => { 'xlink:type'=>'simple', 'xlink:href'=>$self->{config}{resource} }, $constraint ] | ||
| 674 | if $protocols->{Get}; | ||
| 675 | push @http, [ 'ows:Post' => { 'xlink:type'=>'simple', 'xlink:href'=>$self->{config}{resource} } ] | ||
| 676 | if $protocols->{Post}; | ||
| 677 | $writer->element('ows:Operation' => { name => $operation }, [['ows:DCP' => ['ows:HTTP' => \@http ]], @p]); | ||
| 678 | } | ||
| 679 | |||
| 680 | sub Parameter { | ||
| 681 | my ($self, $name, $values) = @_; | ||
| 682 | my $wrap = $values->[0] =~ /allowedvalues/i; | ||
| 683 | $values = $values->[1] if $wrap; | ||
| 684 | my @values; | ||
| 685 | for my $value (@$values) { | ||
| 686 | push @values, ['ows:Value' => $value]; | ||
| 687 | } | ||
| 688 | @values = ('ows:AllowedValues' => [@values]) if $wrap; | ||
| 689 | return ('ows:Parameter', { name => $name }, \@values); | ||
| 690 | } | ||
| 691 | |||
| 692 | =pod | ||
| 693 | |||
| 694 | =head1 Geo::OGC::Service::XMLWriter | ||
| 695 | |||
| 696 | A helper class for writing XML. | ||
| 697 | |||
| 698 | =head2 SYNOPSIS | ||
| 699 | |||
| 700 | my $writer = Geo::OGC::Service::XMLWriter::Caching->new(); | ||
| 701 | $writer->open_element( | ||
| 702 | 'wfs:WFS_Capabilities', | ||
| 703 | { 'xmlns:gml' => "http://www.opengis.net/gml" }); | ||
| 704 | $writer->element('ows:ServiceProvider', | ||
| 705 | [['ows:ProviderName'], | ||
| 706 | ['ows:ProviderSite', {'xlink:type'=>"simple", 'xlink:href'=>""}], | ||
| 707 | ['ows:ServiceContact']]); | ||
| 708 | $writer->close_element; | ||
| 709 | $writer->stream($responder); | ||
| 710 | |||
| 711 | or | ||
| 712 | |||
| 713 | my $writer = Geo::OGC::Service::XMLWriter::Streaming->new($responder); | ||
| 714 | $writer->prolog; | ||
| 715 | $writer->open_element('MyXML'); | ||
| 716 | while (a long time) { | ||
| 717 | $writer->element('MyElement'); | ||
| 718 | } | ||
| 719 | $writer->close_element; | ||
| 720 | # $writer is closed when it goes out of scope | ||
| 721 | |||
| 722 | =head2 DESCRIPTION | ||
| 723 | |||
| 724 | The classes Geo::OGC::Service::XMLWriter (abstract), | ||
| 725 | Geo::OGC::Service::XMLWriter::Streaming (concrete), and | ||
| 726 | Geo::OGC::Service::XMLWriter::Caching (concrete) are provided as a | ||
| 727 | convenience for writing XML to the client. | ||
| 728 | |||
| 729 | The element method has the syntax | ||
| 730 | |||
| 731 | $writer->element($tag[, $attributes][, $content]) | ||
| 732 | |||
| 733 | or | ||
| 734 | |||
| 735 | $writer->element($element) | ||
| 736 | |||
| 737 | where $element is a reference to an array [$tag[, $attributes][, | ||
| 738 | $content]]. | ||
| 739 | |||
| 740 | $attributes is a reference to a hash | ||
| 741 | |||
| 742 | $content is nothing, undef, '/>', plain content (string), an element | ||
| 743 | (as above), a list of elements, or a reference to a list of | ||
| 744 | elements. If there is no $content or $content is undef, a self-closing | ||
| 745 | tag is written. If $content is '/>' a closing tag is written. | ||
| 746 | |||
| 747 | Setting $tag to 0 or 1, allows writing plain content. | ||
| 748 | |||
| 749 | If $attribute{$key} is undefined the attribute is not written at all. | ||
| 750 | |||
| 751 | =cut | ||
| 752 | |||
| 753 | package Geo::OGC::Service::XMLWriter; | ||
| 754 | use Modern::Perl; | ||
| 755 | use Encode qw(decode encode is_utf8); | ||
| 756 | use Carp; | ||
| 757 | |||
| 758 | sub element { | ||
| 759 | my $self = shift; | ||
| 760 | my $tag = shift; | ||
| 761 | return unless defined $tag; | ||
| 762 | if (ref($tag) eq 'ARRAY') { | ||
| 763 | for my $element ($tag, @_) { | ||
| 764 | $self->element(@$element); | ||
| 765 | } | ||
| 766 | return; | ||
| 767 | } | ||
| 768 | my $attributes; | ||
| 769 | $attributes = shift if @_ and ref($_[0]) eq 'HASH'; | ||
| 770 | if (@_ && defined($_[0]) && $_[0] eq '/>') { | ||
| 771 | $self->write("</$tag>"); | ||
| 772 | return; | ||
| 773 | } | ||
| 774 | if ($tag =~ /^\d/) { | ||
| 775 | $self->write($_[0]); | ||
| 776 | return; | ||
| 777 | } | ||
| 778 | $self->write("<$tag"); | ||
| 779 | if ($attributes) { | ||
| 780 | for my $a (keys %$attributes) { | ||
| 781 | my $attr = $attributes->{$a}; | ||
| 782 | if (defined $attr) { | ||
| 783 | $attr = decode utf8 => $attr unless is_utf8($attr); | ||
| 784 | $self->write(" $a=\"$attr\""); | ||
| 785 | } | ||
| 786 | } | ||
| 787 | } | ||
| 788 | if (@_ == 0 || !defined($_[0])) { | ||
| 789 | $self->write(" />"); | ||
| 790 | } else { | ||
| 791 | $self->write(">"); | ||
| 792 | for my $element (@_) { | ||
| 793 | next unless defined($element); | ||
| 794 | if (ref $element eq 'ARRAY') { | ||
| 795 | $self->element(@$element); | ||
| 796 | } elsif (ref $element) { | ||
| 797 | croak ref($element)." can't be used as an XML element."; | ||
| 798 | } elsif ($element eq '>') { | ||
| 799 | } else { | ||
| 800 | if (is_utf8($element)) { | ||
| 801 | $self->write($element); | ||
| 802 | } else { | ||
| 803 | $self->write(decode utf8 => $element); | ||
| 804 | } | ||
| 805 | } | ||
| 806 | } | ||
| 807 | $self->write("</$tag>"); | ||
| 808 | } | ||
| 809 | } | ||
| 810 | |||
| 811 | sub open_element { | ||
| 812 | my $self = shift; | ||
| 813 | my $element = shift; | ||
| 814 | my $attributes; | ||
| 815 | for my $x (@_) { | ||
| 816 | $attributes = $x, next if ref($x) eq 'HASH'; | ||
| 817 | } | ||
| 818 | $self->write("<$element"); | ||
| 819 | if ($attributes) { | ||
| 820 | for my $a (keys %$attributes) { | ||
| 821 | my $attr = $attributes->{$a}; | ||
| 822 | if (defined $attr) { | ||
| 823 | $attr = decode utf8 => $attr unless is_utf8($attr); | ||
| 824 | $self->write(" $a=\"$attr\""); | ||
| 825 | } | ||
| 826 | } | ||
| 827 | } | ||
| 828 | $self->write(">"); | ||
| 829 | $self->{open_element} = [] unless $self->{open_element}; | ||
| 830 | push @{$self->{open_element}}, $element; | ||
| 831 | } | ||
| 832 | |||
| 833 | sub close_element { | ||
| 834 | my $self = shift; | ||
| 835 | my $element = pop @{$self->{open_element}}; | ||
| 836 | $self->write("</$element>"); | ||
| 837 | } | ||
| 838 | |||
| 839 | =pod | ||
| 840 | |||
| 841 | =head1 Geo::OGC::Service::XMLWriter::Streaming | ||
| 842 | |||
| 843 | A helper class for writing XML into a stream. | ||
| 844 | |||
| 845 | =head2 SYNOPSIS | ||
| 846 | |||
| 847 | my $w = Geo::OGC::Service::XMLWriter::Streaming($responder, $headers, $declaration); | ||
| 848 | |||
| 849 | Using $w as XMLWriter sets writer, which is obtained from $responder, | ||
| 850 | to write XML. The writer is closed when $w is destroyed. | ||
| 851 | |||
| 852 | $headers and $declaration are optional. The defaults are | ||
| 853 | 'Content-Type' => 'text/xml; charset=utf-8' and '<?xml version="1.0" | ||
| 854 | encoding="UTF-8"?>'. | ||
| 855 | |||
| 856 | =cut | ||
| 857 | |||
| 858 | package Geo::OGC::Service::XMLWriter::Streaming; | ||
| 859 | use Modern::Perl; | ||
| 860 | |||
| 861 | our @ISA = qw(Geo::OGC::Service::XMLWriter Plack::Util::Prototype); # can't use parent since Plack is not yet | ||
| 862 | |||
| 863 | sub new { | ||
| 864 | my ($class, $responder, $headers, $declaration) = @_; | ||
| 865 | my %headers; | ||
| 866 | if (ref $headers) { | ||
| 867 | %headers = @$headers; | ||
| 868 | } else { | ||
| 869 | $headers{'Content-Type'} = $headers; | ||
| 870 | } | ||
| 871 | $headers{'Content-Type'} //= 'text/xml; charset=utf-8'; | ||
| 872 | my $self = $responder->([200, [%headers]]); | ||
| 873 | $self->{declaration} = $declaration //= '<?xml version="1.0" encoding="UTF-8"?>'; | ||
| 874 | return bless $self, $class; | ||
| 875 | } | ||
| 876 | |||
| 877 | sub prolog { | ||
| 878 | my $self = shift; | ||
| 879 | $self->write($self->{declaration}); | ||
| 880 | } | ||
| 881 | |||
| 882 | sub DESTROY { | ||
| 883 | my $self = shift; | ||
| 884 | $self->close; | ||
| 885 | } | ||
| 886 | |||
| 887 | =pod | ||
| 888 | |||
| 889 | =head1 Geo::OGC::Service::XMLWriter::Caching | ||
| 890 | |||
| 891 | A helper class for writing XML into a cache. | ||
| 892 | |||
| 893 | =head2 SYNOPSIS | ||
| 894 | |||
| 895 | my $w = Geo::OGC::Service::XMLWriter::Caching($headers, $declaration); | ||
| 896 | $w->stream($responder); | ||
| 897 | |||
| 898 | Using $w to produce XML caches the XML. The cached XML can be | ||
| 899 | written by a writer obtained from a $responder. | ||
| 900 | |||
| 901 | $headers and $declaration are optional. The defaults are as in | ||
| 902 | Geo::OGC::Service::XMLWriter::Streaming. | ||
| 903 | |||
| 904 | =cut | ||
| 905 | |||
| 906 | package Geo::OGC::Service::XMLWriter::Caching; | ||
| 907 | use Modern::Perl; | ||
| 908 | use Encode qw(decode encode is_utf8); | ||
| 909 | |||
| 910 | our @ISA = qw(Geo::OGC::Service::XMLWriter); | ||
| 911 | |||
| 912 | sub new { | ||
| 913 | my ($class, $headers, $declaration) = @_; | ||
| 914 | my %headers; | ||
| 915 | if (ref $headers) { | ||
| 916 | %headers = @$headers; | ||
| 917 | } else { | ||
| 918 | $headers{'Content-Type'} = $headers; | ||
| 919 | } | ||
| 920 | $headers{'Content-Type'} //= 'text/xml; charset=utf-8'; | ||
| 921 | my $self = { | ||
| 922 | cache => [], | ||
| 923 | headers => \%headers, | ||
| 924 | declaration => $declaration //= '<?xml version="1.0" encoding="UTF-8"?>' | ||
| 925 | }; | ||
| 926 | $self->{cache} = []; | ||
| 927 | return bless $self, $class; | ||
| 928 | } | ||
| 929 | |||
| 930 | sub write { | ||
| 931 | my $self = shift; | ||
| 932 | my $line = shift; | ||
| 933 | push @{$self->{cache}}, $line; | ||
| 934 | } | ||
| 935 | |||
| 936 | sub to_string { | ||
| 937 | my $self = shift; | ||
| 938 | my $xml = $self->{declaration}; | ||
| 939 | for my $line (@{$self->{cache}}) { | ||
| 940 | $xml .= $line; | ||
| 941 | } | ||
| 942 | return $xml; | ||
| 943 | } | ||
| 944 | |||
| 945 | sub stream { | ||
| 946 | my $self = shift; | ||
| 947 | my $responder = shift; | ||
| 948 | my $debug = shift; | ||
| 949 | my $writer = $responder->([200, [ %{$self->{headers}} ]]); | ||
| 950 | $writer->write($self->{declaration}); | ||
| 951 | my $xml = ''; | ||
| 952 | for my $line (@{$self->{cache}}) { | ||
| 953 | $writer->write(encode utf8 => $line); | ||
| 954 | $xml .= $line if $debug; | ||
| 955 | } | ||
| 956 | $writer->close; | ||
| 957 | if ($debug) { | ||
| 958 | my $parser = XML::LibXML->new(no_blanks => 1); | ||
| 959 | my $pp = XML::LibXML::PrettyPrint->new(indent_string => " "); | ||
| 960 | my $dom = $parser->load_xml(string => $xml); | ||
| 961 | $pp->pretty_print($dom); | ||
| 962 | say STDERR $dom->toString; | ||
| 963 | } | ||
| 964 | } | ||
| 965 | |||
| 966 | 1; | ||
| 967 | __END__ |