| 1 | package SADI::Simple::ServiceBase; | ||
| 2 | { | ||
| 3 | $SADI::Simple::ServiceBase::VERSION = '0.15'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use SADI::Simple::ServiceDescription; | ||
| 10 | use Log::Log4perl; | ||
| 11 | use RDF::Trine::Parser 0.135; | ||
| 12 | use RDF::Trine::Model 0.135; | ||
| 13 | use RDF::Trine::Statement 0.135; | ||
| 14 | |||
| 15 | use base qw( SADI::Simple::Base ); | ||
| 16 | |||
| 17 | use constant RDF_TYPE_URI => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'; | ||
| 18 | |||
| 19 | sub new { | ||
| 20 | |||
| 21 | my $class = shift; | ||
| 22 | my @args = @_; | ||
| 23 | |||
| 24 | my $self = {}; | ||
| 25 | $self->{Signature} = SADI::Simple::ServiceDescription->new(@_); | ||
| 26 | |||
| 27 | bless $self, ref($class) || $class; | ||
| 28 | return $self; | ||
| 29 | |||
| 30 | } | ||
| 31 | |||
| 32 | #----------------------------------------------------------------- | ||
| 33 | # process_it | ||
| 34 | #----------------------------------------------------------------- | ||
| 35 | sub process_it { | ||
| 36 | my ( $self, $values, $service ) = @_; | ||
| 37 | |||
| 38 | # subclass over-rides this | ||
| 39 | } | ||
| 40 | |||
| 41 | #----------------------------------------------------------------- | ||
| 42 | # as_uni_string | ||
| 43 | #----------------------------------------------------------------- | ||
| 44 | #use SADI::Data::String; | ||
| 45 | #use Unicode::String; | ||
| 46 | # | ||
| 47 | #sub as_uni_string { | ||
| 48 | # my ( $self, $value ) = @_; | ||
| 49 | # return new SADI::Data::String( Unicode::String::latin1($value) ); | ||
| 50 | #} | ||
| 51 | |||
| 52 | #----------------------------------------------------------------- | ||
| 53 | # log_request | ||
| 54 | # | ||
| 55 | # should be called when a request from a client comes; it returns | ||
| 56 | # information about the current call (request) that can be used in a | ||
| 57 | # log entry | ||
| 58 | #----------------------------------------------------------------- | ||
| 59 | |||
| 60 | my @ENV_TO_REPORT = | ||
| 61 | ( 'REMOTE_ADDR', 'REQUEST_URI' ,'HTTP_USER_AGENT', 'CONTENT_LENGTH', 'CONTENT_TYPE', 'HTTP_ACCEPT' ); | ||
| 62 | |||
| 63 | sub log_request { | ||
| 64 | my ($self) = shift; | ||
| 65 | |||
| 66 | my @buf; | ||
| 67 | foreach my $elem (@ENV_TO_REPORT) { | ||
| 68 | push( @buf, "$elem: $ENV{$elem}" ) if exists $ENV{$elem}; | ||
| 69 | } | ||
| 70 | return join( ", ", @buf ); | ||
| 71 | } | ||
| 72 | |||
| 73 | #sub get_service_signature { | ||
| 74 | # my ( $self, $name ) = @_; | ||
| 75 | # my $sig = undef; | ||
| 76 | # eval { | ||
| 77 | # my $services = SADI::Generators::GenServices->new->read_services( $name, ); | ||
| 78 | # # iterate over the services (should be only 1) | ||
| 79 | # foreach my $s (@$services) { | ||
| 80 | # $sig = $s; | ||
| 81 | # last; | ||
| 82 | # } | ||
| 83 | # }; | ||
| 84 | # $LOG->error("Problems retrieving service signature!\n$@") if $@; | ||
| 85 | # return $sig if $sig; | ||
| 86 | # $self->throw("Couldn't find a signature for '$name'!."); | ||
| 87 | #} | ||
| 88 | |||
| 89 | # returns the request content type | ||
| 90 | # defaults to application/rdf+xml | ||
| 91 | sub get_request_content_type { | ||
| 92 | my ($self) = @_; | ||
| 93 | my $CONTENT_TYPE = 'application/rdf+xml'; | ||
| 94 | if (defined $ENV{CONTENT_TYPE}) { | ||
| 95 | $CONTENT_TYPE = 'text/rdf+n3' if $ENV{CONTENT_TYPE} =~ m|text/rdf\+n3|gi; | ||
| 96 | $CONTENT_TYPE = 'text/rdf+n3' if $ENV{CONTENT_TYPE} =~ m|text/n3|gi; | ||
| 97 | $CONTENT_TYPE = 'application/n-quads' if $ENV{CONTENT_TYPE} =~ m|application/n\-quads|gi; | ||
| 98 | |||
| 99 | } | ||
| 100 | return $CONTENT_TYPE; | ||
| 101 | } | ||
| 102 | |||
| 103 | # returns the response requested content type | ||
| 104 | # defaults to application/rdf+xml | ||
| 105 | sub get_response_content_type { | ||
| 106 | my ($self) = @_; | ||
| 107 | my $CONTENT_TYPE = 'application/rdf+xml'; | ||
| 108 | if (defined $ENV{HTTP_ACCEPT}) { | ||
| 109 | $CONTENT_TYPE = 'text/rdf+n3' if $ENV{HTTP_ACCEPT} =~ m|text/rdf\+n3|gi; | ||
| 110 | $CONTENT_TYPE = 'text/rdf+n3' if $ENV{HTTP_ACCEPT} =~ m|text/n3|gi; | ||
| 111 | $CONTENT_TYPE = 'application/n-quads' if $ENV{HTTP_ACCEPT} =~ m|application/n\-quads|gi; | ||
| 112 | |||
| 113 | } | ||
| 114 | return $CONTENT_TYPE; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _build_model | ||
| 118 | { | ||
| 119 | my ($self, $data, $content_type) = @_; | ||
| 120 | |||
| 121 | my $parser; | ||
| 122 | if ($self->get_request_content_type eq 'text/rdf+n3') { | ||
| 123 | $parser = RDF::Trine::Parser->new('turtle'); | ||
| 124 | } else { | ||
| 125 | $parser = RDF::Trine::Parser->new('rdfxml'); | ||
| 126 | } | ||
| 127 | |||
| 128 | my $model = RDF::Trine::Model->temporary_model; | ||
| 129 | $parser->parse_into_model(undef, $data, $model); | ||
| 130 | |||
| 131 | return $model; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub _get_inputs_from_model | ||
| 135 | { | ||
| 136 | my ($self, $model) = @_; | ||
| 137 | |||
| 138 | # my $type = new RDF::Trine::Node::Resource(RDF_TYPE_URI); | ||
| 139 | my $type = new RDF::Trine::Node::Resource('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'); | ||
| 140 | |||
| 141 | my $input_class = new RDF::Trine::Node::Resource($self->{Signature}->InputClass); | ||
| 142 | |||
| 143 | return $model->subjects($type, $input_class); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub _type_outputs | ||
| 147 | { | ||
| 148 | my ($self, $output_model, $inputs) = @_; | ||
| 149 | # my $rdf_type = new RDF::Trine::Node::Resource(RDF_TYPE_URI); | ||
| 150 | my $rdf_type = new RDF::Trine::Node::Resource('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'); | ||
| 151 | my $output_type = new RDF::Trine::Node::Resource($self->{Signature}->OutputClass); | ||
| 152 | my %input_uri_hash = (); | ||
| 153 | $input_uri_hash{$_->uri()} = 1 foreach @$inputs; | ||
| 154 | my %visited_output_uris = (); | ||
| 155 | foreach my $s ($output_model->subjects()) { | ||
| 156 | next if $s->is_blank(); | ||
| 157 | my $uri = $s->uri(); | ||
| 158 | if ($input_uri_hash{$uri} && !$visited_output_uris{$uri}) { | ||
| 159 | my $statement = RDF::Trine::Statement->new($s, $rdf_type, $output_type); | ||
| 160 | if ($self->{'Signature'}->NanoPublisher){ | ||
| 161 | $output_model->add_statement($statement, $s); # need to add context here, for nanopublications | ||
| 162 | } else { | ||
| 163 | $output_model->add_statement($statement); | ||
| 164 | } | ||
| 165 | $visited_output_uris{$uri} = 1; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | use constant SERVICE_ERROR_TEMPLATE => <<TEMPLATE; | ||
| 171 | <rdf:RDF | ||
| 172 | xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" | ||
| 173 | xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" | ||
| 174 | xmlns:sadi="http://sadiframework.org/ontologies/sadi.owl#"> | ||
| 175 | <sadi:Exception> | ||
| 176 | <rdfs:label>[% message %]</rdfs:label> | ||
| 177 | <rdfs:comment>[% comment %]</rdfs:comment> | ||
| 178 | <sadi:hasStackTrace rdf:parseType="Collection"> | ||
| 179 | <sadi:StackTraceElement> | ||
| 180 | <rdfs:label>[% stack %]</rdfs:label> | ||
| 181 | </sadi:StackTraceElement> | ||
| 182 | </sadi:hasStackTrace> | ||
| 183 | </sadi:Exception> | ||
| 184 | </rdf:RDF> | ||
| 185 | TEMPLATE | ||
| 186 | |||
| 187 | sub _add_error_to_model | ||
| 188 | { | ||
| 189 | my ($self, $model, $msg, $comment, $stack) = @_; | ||
| 190 | |||
| 191 | my $LOG = Log::Log4perl->get_logger(__PACKAGE__); | ||
| 192 | |||
| 193 | # generate from template | ||
| 194 | my $error_rdf = ''; | ||
| 195 | my $tt = Template->new( ABSOLUTE => 1, TRIM => 1 ); | ||
| 196 | |||
| 197 | my $input = SERVICE_ERROR_TEMPLATE; | ||
| 198 | |||
| 199 | $msg ||= ''; | ||
| 200 | $comment ||= ''; | ||
| 201 | $stack ||= ''; | ||
| 202 | |||
| 203 | use CGI; | ||
| 204 | $tt->process( | ||
| 205 | \$input, | ||
| 206 | { | ||
| 207 | message => CGI::escapeHTML($msg), | ||
| 208 | comment => CGI::escapeHTML($comment), | ||
| 209 | stack => CGI::escapeHTML($stack), | ||
| 210 | }, | ||
| 211 | \$error_rdf | ||
| 212 | ) || $LOG->logdie( $tt->error() ); | ||
| 213 | |||
| 214 | # if problem generating error doc, return | ||
| 215 | return unless defined ($error_rdf); | ||
| 216 | return if $error_rdf eq ''; | ||
| 217 | |||
| 218 | my $parser = RDF::Trine::Parser->new('rdfxml'); | ||
| 219 | $parser->parse_into_model(undef, $error_rdf, $model); | ||
| 220 | } | ||
| 221 | |||
| 222 | 1; | ||
| 223 | |||
| 224 | __END__ |