cab3b2e71cff07cf4e68cc7b8b0f005ab99eed3b
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Stemweb.pm
1 package stemmaweb::Controller::Stemweb;
2 use Moose;
3 use namespace::autoclean;
4 use Encode qw/ decode_utf8 /;
5 use JSON;
6 use LWP::UserAgent;
7 use Safe::Isa;
8 use TryCatch;
9 use URI;
10
11 BEGIN { extends 'Catalyst::Controller' }
12
13 ## TODO Move the /algorithms/available function to the Stemweb module
14 my $STEMWEB_BASE_URL = 'http://slinkola.users.cs.helsinki.fi';
15
16 =head1 NAME
17
18 stemmaweb::Controller::Stemweb - Client listener for Stemweb results
19
20 =head1 DESCRIPTION
21
22 This is a client listener for the Stemweb API as implemented by the protocol defined at
23 L<https://docs.google.com/document/d/1aNYGAo1v1WPDZi6LXZ30FJSMJwF8RQPYbOkKqHdCZEc/pub>.
24
25 =head1 METHODS
26
27 =head2 result
28
29  POST stemweb/result
30  Content-Type: application/json
31  (On success):
32  { job_id: <ID number>
33    status: 0
34    format: <format>
35    result: <data> }
36  (On failure):
37  { jobid: <ID number>
38    status: >1
39    result: <error message> }
40    
41 Used by the Stemweb server to notify us that one or more stemma graphs
42 has been calculated in response to an earlier request.
43
44 =cut
45
46 sub result :Local :Args(0) {
47         my( $self, $c ) = @_;
48         if( $c->request->method eq 'POST' ) {
49                 # TODO: Verify the sender!
50                 my $answer;
51                 if( ref( $c->request->body ) eq 'File::Temp' ) {
52                         # Read in the file and parse that.
53                         open( POSTDATA, $c->request->body ) or die "Failed to open post data file";
54                         binmode( POSTDATA, ':utf8' );
55                         # JSON should be all one line
56                         my $pdata = <POSTDATA>;
57                         chomp $pdata;
58                         close POSTDATA;
59                         try {
60                                 $answer = from_json( $pdata );
61                         } catch {
62                                 return _json_error( $c, 400, 
63                                         "Could not parse POST request '' $pdata '' as JSON: $@" );
64                         }
65                 } else {
66                         $answer = from_json( $c->request->body );
67                 }
68                 return _process_stemweb_result( $c, $answer );
69         } else {
70                 return _json_error( $c, 403, 'Please use POST!' );
71         }
72 }
73
74 =head2 query
75
76  GET stemweb/query/<jobid>
77
78 A backup method to query the stemweb server to check a particular job status.
79 Returns a result as in /stemweb/result above, but status can also be -1 to 
80 indicate that the job is still running.
81
82 =cut
83
84 sub query :Local :Args(1) {
85         my( $self, $c, $jobid ) = @_;
86         my $ua = LWP::UserAgent->new();
87         my $resp = $ua->get( $STEMWEB_BASE_URL . "/algorithms/jobstatus/$jobid" );
88         if( $resp->is_success ) {
89                 # Process it
90                 my $response = decode_utf8( $resp->content );
91                 $c->log->debug( "Got a response from the server: $response" );
92                 my $answer;
93                 try {
94                         $answer = from_json( $response );
95                 } catch {
96                         return _json_error( $c, 500, 
97                                 "Could not parse stemweb response '' $response '' as JSON: $@" );
98                 }
99                 return _process_stemweb_result( $c, $answer );
100         } elsif( $resp->code == 500 && $resp->header('Client-Warning')
101                 && $resp->header('Client-Warning') eq 'Internal response' ) {
102                 # The server was unavailable.
103                 return _json_error( $c, 503, "The Stemweb server is currently unreachable." );
104         } else {
105                 return _json_error( $c, 500, "Stemweb error: " . $resp->code . " / "
106                         . $resp->content );
107         }
108 }
109
110
111 ## Helper function for parsing Stemweb result data either by push or by pull
112 sub _process_stemweb_result {
113         my( $c, $answer ) = @_;
114         # Find a tradition with the defined Stemweb job ID.
115         # TODO: Maybe get Stemweb to pass back the tradition ID...
116         my $m = $c->model('Directory');
117         my @traditions;
118         ## STUPID HACK: unless we load the possible tradition owners
119         ## within scope of the scan, they will not exist when the affected
120         ## tradition is saved.
121         my @users;
122         $m->scan( sub{ push( @traditions, $_[0] )
123                                         if $_[0]->$_isa('Text::Tradition')
124                                         && $_[0]->has_stemweb_jobid 
125                                         && $_[0]->stemweb_jobid eq $answer->{job_id}; 
126                         push( @users, $_[0] ) if $_[0]->$_isa('Text::Tradition::User');
127                                 } );
128         if( @traditions == 1 ) {
129                 my $tradition = shift @traditions;
130                 if( $answer->{status} == 0 ) {
131                         my $stemmata;
132                         try {
133                                 $stemmata = $tradition->record_stemweb_result( $answer );
134                                 $m->save( $tradition );
135                         } catch( Text::Tradition::Error $e ) {
136                                 return _json_error( $c, 500, $e->message );
137                         } catch {
138                                 return _json_error( $c, 500, $@ );
139                         }
140                         # If we got here, success!
141                         my @steminfo = map { { 
142                                         name => $_->identifier, 
143                                         directed => _json_bool( !$_->is_undirected ),
144                                         svg => $_->as_svg() } } 
145                                 @$stemmata;
146                         $c->stash->{'result'} = { 
147                                 'status' => 'success',
148                                 'stemmata' => \@steminfo };
149                 } elsif( $answer->{status} < 1 ) {
150                         $c->stash->{'result'} = { 'status' => 'running' };
151                 } else {
152                         return _json_error( $c, 500,
153                                 "Stemweb failure not handled: " . $answer->{result} );
154                 }
155         } elsif( @traditions ) {
156                 return _json_error( $c, 500, 
157                         "Multiple traditions with Stemweb job ID " . $answer->{job_id} . "!" );
158         } else {
159                 # Possible that the tradition got updated in the meantime...
160                 if( $answer->{status} == 0 ) {
161                         $c->stash->{'result'} = { 'status' => 'notfound' };
162                 } else {
163                         return _json_error( $c, 400, 
164                                 "No tradition found with Stemweb job ID " . $answer->{job_id} );
165                 }
166         }
167         $c->forward('View::JSON');
168 }
169
170 =head2 request
171
172  GET stemweb/request/?
173         tradition=<tradition ID> &
174         algorithm=<algorithm ID> &
175         [<algorithm parameters>]
176    
177 Send a request for the given tradition with the given parameters to Stemweb.
178 Processes and returns the JSON response given by the Stemweb server.
179
180 =cut
181
182 sub request :Local :Args(0) {
183         my( $self, $c ) = @_;
184         # Look up the relevant tradition and check permissions.
185         my $reqparams = $c->req->params;
186         my $tid = delete $reqparams->{tradition};
187         my $t = $c->model('Directory')->tradition( $tid );
188         my $ok = _check_permission( $c, $t );
189         return unless $ok;
190         return( _json_error( $c, 403, 
191                         'You do not have permission to update stemmata for this tradition' ) )
192                 unless $ok eq 'full';
193         
194         # Form the request for Stemweb.
195         my $algorithm = delete $reqparams->{algorithm};
196         my $return_uri = URI->new( $c->uri_for( '/stemweb/result' ) );
197         my $stemweb_request = {
198                 return_path => $return_uri->path,
199                 return_host => $return_uri->host_port,
200                 data => $t->collation->as_tsv,
201                 userid => $c->user->get_object->email,
202                 parameters => $reqparams };
203                 
204         # Call to the appropriate URL with the request parameters.
205         my $ua = LWP::UserAgent->new();
206         $c->log->debug( 'Sending request to Stemweb: ' . to_json( $stemweb_request ) ); 
207         my $resp = $ua->post( $STEMWEB_BASE_URL . "/algorithms/process/$algorithm/",
208                 'Content-Type' => 'application/json; charset=utf-8', 
209                 'Content' => encode_json( $stemweb_request ) ); 
210         if( $resp->is_success ) {
211                 # Process it
212                 $c->log->debug( 'Got a response from the server: '
213                         . decode_utf8( $resp->content ) );
214                 my $stemweb_response = decode_json( $resp->content );
215                 try {
216                         $t->set_stemweb_jobid( $stemweb_response->{jobid} );
217                 } catch( Text::Tradition::Error $e ) {
218                         return _json_error( $c, 429, $e->message );
219                 }
220                 $c->model('Directory')->save( $t );
221                 $c->stash->{'result'} = $stemweb_response;
222                 $c->forward('View::JSON');
223         } elsif( $resp->code == 500 && $resp->header('Client-Warning')
224                 && $resp->header('Client-Warning') eq 'Internal response' ) {
225                 # The server was unavailable.
226                 return _json_error( $c, 503, "The Stemweb server is currently unreachable." );
227         } else {
228                 return _json_error( $c, 500, "Stemweb error: " . $resp->code . " / "
229                         . $resp->content );
230         }
231 }
232
233 # Helper to check what permission, if any, the active user has for
234 # the given tradition
235 sub _check_permission {
236         my( $c, $tradition ) = @_;
237     my $user = $c->user_exists ? $c->user->get_object : undef;
238     if( $user ) {
239         return 'full' if ( $user->is_admin || 
240                 ( $tradition->has_user && $tradition->user->id eq $user->id ) );
241     }
242         # Text doesn't belong to us, so maybe it's public?
243         return 'readonly' if $tradition->public;
244
245         # ...nope. Forbidden!
246         return _json_error( $c, 403, 'You do not have permission to view this tradition.' );
247 }
248
249 # Helper to throw a JSON exception
250 sub _json_error {
251         my( $c, $code, $errmsg ) = @_;
252         $c->response->status( $code );
253         $c->stash->{'result'} = { 'error' => $errmsg };
254         $c->forward('View::JSON');
255         return 0;
256 }
257
258 sub _json_bool {
259         return $_[0] ? JSON::true : JSON::false;
260 }
261
262
263 1;