| 1 | package WebService::SOS; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Carp qw(cluck); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use HTTP::Request::Common; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use URI::Escape; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use WebService::SOS::Capabilities; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use WebService::SOS::SensorDescription; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use WebService::SOS::Observations; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use WebService::SOS::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 1 | our $VERSION = '0.01'; | |||||||||
| 18 | |||||||||||
| 19 | # for extra debugging output | ||||||||||
| 20 | 1 | has 'debug' => ( | |||||||||
| 21 | is => 'ro', | ||||||||||
| 22 | isa => 'Bool', | ||||||||||
| 23 | default => 0, | ||||||||||
| 24 | ); | ||||||||||
| 25 | |||||||||||
| 26 | # return raw xml instead of an object | ||||||||||
| 27 | 1 | has 'rawxml' => ( | |||||||||
| 28 | is => 'ro', | ||||||||||
| 29 | isa => 'Bool', | ||||||||||
| 30 | default => 0, | ||||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | 1 | has 'ua' => ( | |||||||||
| 34 | is => 'ro', | ||||||||||
| 35 | isa => 'LWP::UserAgent', | ||||||||||
| 36 | lazy_build => 1, | ||||||||||
| 37 | ); | ||||||||||
| 38 | |||||||||||
| 39 | 1 | has 'server_url' => ( | |||||||||
| 40 | is => 'ro', | ||||||||||
| 41 | isa => 'Str', | ||||||||||
| 42 | default => 'http://lighthouse.tamucc.edu/sos', | ||||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | 1 | has 'method' => ( | |||||||||
| 46 | is => 'ro', | ||||||||||
| 47 | isa => enum([qw(post get)]), | ||||||||||
| 48 | default => 'post', | ||||||||||
| 49 | ); | ||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub _build_ua { | ||||||||
|
Subroutine
_build_ua: not called CC=1 SCAR=6.9
| |||||||||||
| 52 | 0 | my ($self) = @_; | |||||||||
| 53 | 0 | return LWP::UserAgent->new( agent => 'WebService::SOS/' . $VERSION ); | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | sub _check_response { | ||||||||
|
Subroutine
_check_response: not called CC=5 SCAR=34.0
| |||||||||||
| 57 | 0 | my ($self, $response) = @_; | |||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | unless ($response->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | cluck("Error communicating with SOS server: ".$response->code." ".$response->message); | |||||||||
| 61 | 0 | return ( return => 1, retval => undef ); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | return ( return => 1, retval => $response->content ) if $self->rawxml; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | |||||||||||
| 66 | 0 | my %args = ( xml => $response->content, namespace_map => { ows => 'http://www.opengis.net/ows/1.1' } ); | |||||||||
| 67 | # kluge due to bad xml at some sos servers | ||||||||||
| 68 | 0 | ▶ | $args{namespace_map} = { ows => 'http://www.opengis.net/ows' } if $response->content =~ m|xmlns="http://www.opengis.net/ows"|; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | |||||||||||
| 70 | 0 | my $resp = WebService::SOS::Exception->new(%args); | |||||||||
| 71 | 0 | ▶ | return ( return => 1, retval => $resp ) if $resp->exception; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | |||||||||||
| 73 | 0 | return (return => 0 ); | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | sub GetCapabilities { | ||||||||
|
Subroutine
GetCapabilities: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 77 | 0 | my $self = shift; | |||||||||
| 78 | 0 | my $response; | |||||||||
| 79 | 0 | ▶ | if ($self->{method} =~ /^get$/i) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | my $request = $self->server_url."?request=GetCapabilities&service=SOS&version=1.0.0"; | |||||||||
| 81 | 0 | ▶ | print STDERR "GetCapabilities: GET $request\n" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | $response = $self->ua->get($request); | |||||||||
| 83 | } | ||||||||||
| 84 | else { | ||||||||||
| 85 | 0 | my $message = '<?xml version="1.0" encoding="UTF-8"?> | |||||||||
| 86 | <sos:GetCapabilities | ||||||||||
| 87 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||||||||
| 88 | xsi:schemaLocation="http://schemas.opengis.net/sos/1.0.0/sosAll.xsd" | ||||||||||
| 89 | xmlns:sos="http://www.opengis.net/sos/1.0" | ||||||||||
| 90 | xmlns:gml="http://www.opengis.net/gml/3.2" | ||||||||||
| 91 | xmlns:ogc="http://www.opengis.net/ogc" | ||||||||||
| 92 | xmlns:om="http://www.opengis.net/om/1.0" service="SOS"> | ||||||||||
| 93 | </sos:GetCapabilities>'; | ||||||||||
| 94 | 0 | ▶ | print STDERR "GetCapabilities: POST\n$message\n" if $self->debug > 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 0 | $response = $self->ua->request(POST $self->server_url, Content_Type => 'text/xml', Content => $message); | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 0 | my %check = _check_response($self,$response); | |||||||||
| 99 | 0 | ▶ | return $check{retval} if $check{return}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | |||||||||||
| 101 | 0 | return WebService::SOS::Capabilities->new( xml => $response->content ); | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 0 | ▶ | sub DescribeSensor { | ||||||||
|
Subroutine
DescribeSensor: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 105 | 0 | my ($self,$procedure) = @_; | |||||||||
| 106 | 0 | my $outputFormat = 'text/xml; subtype="sensorML/1.0.1"'; | |||||||||
| 107 | 0 | my $response; | |||||||||
| 108 | 0 | ▶ | if ($self->{method} =~ /^get$/i) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 0 | my $request = sprintf('%s?request=DescribeSensor&service=SOS&version=1.0.0&outputFormat=%s&procedure=%s', | |||||||||
| 110 | $self->server_url, | ||||||||||
| 111 | uri_escape($outputFormat), | ||||||||||
| 112 | uri_escape($procedure) | ||||||||||
| 113 | ); | ||||||||||
| 114 | 0 | ▶ | print STDERR "DescribeSensor: $request\n" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | $response = $self->ua->get($request); | |||||||||
| 116 | } | ||||||||||
| 117 | else { | ||||||||||
| 118 | 0 | my $message = sprintf('<?xml version="1.0" encoding="UTF-8"?> | |||||||||
| 119 | <DescribeSensor | ||||||||||
| 120 | xmlns="http://www.opengis.net/sos/1.0" | ||||||||||
| 121 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||||||||
| 122 | xsi:schemaLocation="http://www.opengis.net/sos/1.0 http://schemas.opengis.net/sos/1.0.0/sosAll.xsd" | ||||||||||
| 123 | service="SOS" | ||||||||||
| 124 | outputFormat="text/xml;subtype="sensorML/1.0.1"" | ||||||||||
| 125 | version="1.0.0"> | ||||||||||
| 126 | <procedure>%s</procedure> | ||||||||||
| 127 | </DescribeSensor>',$procedure); | ||||||||||
| 128 | 0 | ▶ | print STDERR "DescribeSensor: POST\n$message\n" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 0 | $response = $self->ua->request(POST $self->server_url, Content_Type => 'text/xml', Content => $message); | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 0 | my %check = _check_response($self,$response); | |||||||||
| 133 | 0 | ▶ | return $check{retval} if $check{return}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | |||||||||||
| 135 | 0 | return WebService::SOS::SensorDescription->new( xml => $response->content ); | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | 0 | ▶ | sub GetObservation { | ||||||||
|
Subroutine
GetObservation: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 139 | 0 | my ($self,$offering,$observedProperty,$beginTime,$endTime) = @_; | |||||||||
| 140 | 0 | my $responseFormat = 'text/xml; subtype="om/1.0.0"'; | |||||||||
| 141 | 0 | my $response; | |||||||||
| 142 | 0 | ▶ | if ($self->{method} =~ /^get$/i) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | 0 | my $request = sprintf('%s?request=GetObservation&service=SOS&version=1.0.0&responseFormat=%s&offering=%s&observedProperty=%s&eventTime=%s', | |||||||||
| 144 | $self->server_url, | ||||||||||
| 145 | uri_escape($responseFormat), | ||||||||||
| 146 | uri_escape($offering), | ||||||||||
| 147 | uri_escape($observedProperty), | ||||||||||
| 148 | uri_escape("$beginTime/$endTime") | ||||||||||
| 149 | ); | ||||||||||
| 150 | 0 | ▶ | print STDERR "GetObservation: $request\n" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | 0 | $response = $self->ua->get($request); | |||||||||
| 152 | } | ||||||||||
| 153 | else { | ||||||||||
| 154 | 0 | my $message = sprintf('<?xml version="1.0" encoding="UTF-8"?> | |||||||||
| 155 | <sos:GetObservation | ||||||||||
| 156 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||||||||
| 157 | xsi:schemaLocation="http://schemas.opengis.net/sos/1.0.0/sosAll.xsd" | ||||||||||
| 158 | xmlns:sos="http://www.opengis.net/sos/1.0" | ||||||||||
| 159 | xmlns:gml="http://www.opengis.net/gml/3.2" | ||||||||||
| 160 | xmlns:ogc="http://www.opengis.net/ogc" | ||||||||||
| 161 | xmlns:om="http://www.opengis.net/om/1.0" service="SOS" version="1.0.0"> | ||||||||||
| 162 | <sos:offering>%s</sos:offering> | ||||||||||
| 163 | <sos:observedProperty>%s</sos:observedProperty> | ||||||||||
| 164 | <sos:responseFormat>%s</sos:responseFormat> | ||||||||||
| 165 | <sos:eventTime> | ||||||||||
| 166 | <ogc:TM_During> | ||||||||||
| 167 | <ogc:PropertyName>om:samplingTime</ogc:PropertyName> | ||||||||||
| 168 | <gml:TimePeriod> | ||||||||||
| 169 | <gml:beginPosition>%s</gml:beginPosition> | ||||||||||
| 170 | <gml:endPosition>%s</gml:endPosition> | ||||||||||
| 171 | </gml:TimePeriod> | ||||||||||
| 172 | </ogc:TM_During> | ||||||||||
| 173 | </sos:eventTime> | ||||||||||
| 174 | <result> | ||||||||||
| 175 | <ogc:PropertyIsEqualTo> | ||||||||||
| 176 | <ogc:PropertyName>VerticalDatum</ogc:PropertyName> | ||||||||||
| 177 | <ogc:Literal>urn:ioos:def:datum:noaa::MLLW</ogc:Literal> | ||||||||||
| 178 | </ogc:PropertyIsEqualTo> | ||||||||||
| 179 | </result> | ||||||||||
| 180 | </sos:GetObservation>',$offering,$observedProperty,$responseFormat,$beginTime,$endTime); | ||||||||||
| 181 | |||||||||||
| 182 | 0 | ▶ | print STDERR "GetObservation: POST\n$message\n" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 183 | 0 | $response = $self->ua->request(POST $self->server_url, Content_Type => 'text/xml', Content => $message); | |||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | 0 | my %check = _check_response($self,$response); | |||||||||
| 187 | 0 | ▶ | return $check{retval} if $check{return}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 188 | |||||||||||
| 189 | 0 | return WebService::SOS::Observations->new( xml => $response->content ); | |||||||||
| 190 | } | ||||||||||
| 191 | |||||||||||
| 192 | 1 | __PACKAGE__->meta->make_immutable(); | |||||||||
| 193 | |||||||||||
| 194 | 1 | 1; | |||||||||
| 195 | |||||||||||
| 196 | __END__ | ||||||||||