| 1 | package SADI::Simple::SyncService; | ||
| 2 | { | ||
| 3 | $SADI::Simple::SyncService::VERSION = '0.15'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use SADI::Simple::Utils; | ||
| 7 | use SADI::Simple::OutputModel; | ||
| 8 | use RDF::Trine::Model 0.135; | ||
| 9 | use RDF::Trine::Parser 0.135; | ||
| 10 | use Log::Log4perl; | ||
| 11 | use Template; | ||
| 12 | use Encode; | ||
| 13 | |||
| 14 | use base 'SADI::Simple::ServiceBase'; | ||
| 15 | |||
| 16 | sub handle_cgi_request { | ||
| 17 | |||
| 18 | my $self = shift; | ||
| 19 | |||
| 20 | # if this is a GET, send the service interface | ||
| 21 | if ($ENV{REQUEST_METHOD} eq 'GET' or $ENV{REQUEST_METHOD} eq 'HEAD') { | ||
| 22 | |||
| 23 | my $q = new CGI; | ||
| 24 | print $q->header(-type=>$self->get_response_content_type()); | ||
| 25 | print $self->{Signature}->getServiceInterface($self->get_response_content_type()); | ||
| 26 | |||
| 27 | } else { | ||
| 28 | # get the posted data | ||
| 29 | my $data = join "",<STDIN>; | ||
| 30 | |||
| 31 | # call the service | ||
| 32 | my ($output, $success) = $self->invoke($data); | ||
| 33 | |||
| 34 | my $q = new CGI; | ||
| 35 | |||
| 36 | if(!$success) { | ||
| 37 | print $q->header(-status => 500); | ||
| 38 | print $output; | ||
| 39 | } | ||
| 40 | if(($self->get_response_content_type =~ /quads/i) && !($self->{Signature}->NanoPublisher)){ | ||
| 41 | print $q->header(-status => 406, -type => "application/rdf-xml"); | ||
| 42 | print $output; | ||
| 43 | } else { | ||
| 44 | # print the results | ||
| 45 | print $q->header(-type => $self->get_response_content_type()); | ||
| 46 | print $output; | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | } | ||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | # Please keep this, I am expecting it to be useful in future versions. | ||
| 55 | # This method has not been tested yet. | ||
| 56 | |||
| 57 | #sub plack_app { | ||
| 58 | # | ||
| 59 | # my $self = shift; | ||
| 60 | # | ||
| 61 | # return sub { | ||
| 62 | # | ||
| 63 | # my $env = shift; | ||
| 64 | # | ||
| 65 | # my $status; | ||
| 66 | # my $headers = []; | ||
| 67 | # my $body = []; | ||
| 68 | # | ||
| 69 | # push(@$headers, 'Content-type', $self->get_response_content_type()); | ||
| 70 | # | ||
| 71 | # my $request = Plack::Request->new($env); | ||
| 72 | # | ||
| 73 | # if ($request->method eq 'GET' || $request->method eq 'HEAD') { | ||
| 74 | # | ||
| 75 | # my $content_type = SADI::Simple::Utils->get_standard_content_type($env); | ||
| 76 | # push(@$body, $self->{Signature}->getServiceInterface($content_type)); | ||
| 77 | # return [200, $headers, $body]; | ||
| 78 | # | ||
| 79 | # } else { | ||
| 80 | # | ||
| 81 | # my $data = decode($request->content_encoding, $request->content); | ||
| 82 | # push(@$body, $self->invoke($data)); | ||
| 83 | # return [200, $headers, $body]; | ||
| 84 | # | ||
| 85 | # } | ||
| 86 | # | ||
| 87 | # } | ||
| 88 | # | ||
| 89 | #} | ||
| 90 | |||
| 91 | sub invoke { | ||
| 92 | |||
| 93 | my ($self, $data) = @_; | ||
| 94 | |||
| 95 | my $success = 1; | ||
| 96 | my $LOG = Log::Log4perl->get_logger(__PACKAGE__); | ||
| 97 | |||
| 98 | Log::Log4perl::NDC->push ($$); | ||
| 99 | $LOG->info ('*** REQUEST START *** ' . "\n" . $self->log_request); | ||
| 100 | $LOG->debug ("Input raw data (first 1000 characters):\n" . substr ($data, 0, 1000)) if $LOG->is_debug; | ||
| 101 | |||
| 102 | $self->default_throw_with_stack (0); | ||
| 103 | |||
| 104 | my $input_model; | ||
| 105 | my $output_model; | ||
| 106 | # if ($self->get_response_content_type =~ /quads/i){ | ||
| 107 | $output_model = SADI::Simple::OutputModel->new(); # catches quad statements necessary for NanoPubs | ||
| 108 | $output_model->_init($self); | ||
| 109 | # } else { | ||
| 110 | # $output_model = RDF::Trine::Model->temporary_model; | ||
| 111 | # } | ||
| 112 | |||
| 113 | # get/parse the incoming RDF | ||
| 114 | eval { | ||
| 115 | $input_model = $self->_build_model($data, $self->get_request_content_type); | ||
| 116 | }; | ||
| 117 | |||
| 118 | # error in creating parser, or parsing input | ||
| 119 | if ($@) { | ||
| 120 | $success = 0; | ||
| 121 | # construct an outgoing message | ||
| 122 | my $stack = $self->format_stack ($@); | ||
| 123 | #$self->_add_error_to_model($output_model, $@, 'Error parsing input message for sadi service!', $stack); | ||
| 124 | $LOG->error ($stack); | ||
| 125 | $LOG->info ('*** FATAL ERROR RESPONSE BACK ***'); | ||
| 126 | Log::Log4perl::NDC->pop(); | ||
| 127 | #SADI::Simple::Utils->serialize_model($output_model, $self->get_response_content_type); | ||
| 128 | return ("Error parsing input RDF: $@", $success); | ||
| 129 | } | ||
| 130 | |||
| 131 | # do something (this service main task) | ||
| 132 | eval { | ||
| 133 | my @inputs = $self->_get_inputs_from_model($input_model); | ||
| 134 | $self->process_it(\@inputs, $input_model, $output_model); | ||
| 135 | $self->_type_outputs($output_model, \@inputs); | ||
| 136 | }; | ||
| 137 | # error thrown by the implementation class | ||
| 138 | if ($@) { | ||
| 139 | $success = 0; | ||
| 140 | my $stack = $self->format_stack ($@); | ||
| 141 | $self->_add_error_to_model($output_model, $@, 'Error running sadi service!', $stack); | ||
| 142 | $LOG->error ($stack); | ||
| 143 | $LOG->info ('*** REQUEST TERMINATED RESPONSE BACK ***'); | ||
| 144 | Log::Log4perl::NDC->pop(); | ||
| 145 | my $output = SADI::Simple::Utils->serialize_model($output_model, $self->get_response_content_type); | ||
| 146 | |||
| 147 | return ($output, $success); | ||
| 148 | } | ||
| 149 | |||
| 150 | # return result | ||
| 151 | $LOG->info ('*** RESPONSE READY *** '); | ||
| 152 | |||
| 153 | Log::Log4perl::NDC->pop(); | ||
| 154 | my $output; | ||
| 155 | if(($self->get_response_content_type =~ /quads/i) && !($self->{Signature}->NanoPublisher)){ | ||
| 156 | $output = SADI::Simple::Utils->serialize_model($output_model, 'rdfxml'); | ||
| 157 | } else { | ||
| 158 | $output = SADI::Simple::Utils->serialize_model($output_model, $self->get_response_content_type); | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | return ($output, $success); | ||
| 163 | } | ||
| 164 | |||
| 165 | 1; | ||
| 166 | |||
| 167 | __END__ |