1 package stemmaweb::Controller::Stemweb;
3 use namespace::autoclean;
4 use Encode qw/ decode_utf8 /;
8 use Scalar::Util qw/ looks_like_number /;
12 BEGIN { extends 'Catalyst::Controller' }
14 ## TODO Move the /algorithms/available function to the Stemweb module
15 my $STEMWEB_BASE_URL = 'http://slinkola.users.cs.helsinki.fi';
19 stemmaweb::Controller::Stemweb - Client listener for Stemweb results
23 This is a client listener for the Stemweb API as implemented by the protocol defined at
24 L<https://docs.google.com/document/d/1aNYGAo1v1WPDZi6LXZ30FJSMJwF8RQPYbOkKqHdCZEc/pub>.
31 Content-Type: application/json
40 result: <error message> }
42 Used by the Stemweb server to notify us that one or more stemma graphs
43 has been calculated in response to an earlier request.
47 sub result :Local :Args(0) {
49 if( $c->request->method eq 'POST' ) {
50 # TODO: Verify the sender!
52 if( ref( $c->request->body ) eq 'File::Temp' ) {
53 # Read in the file and parse that.
54 $c->log->debug( "Request body is in a temp file" );
55 open( POSTDATA, $c->request->body )
56 or return _json_error( $c, 500, "Failed to open post data file" );
57 binmode( POSTDATA, ':utf8' );
58 # JSON should be all one line
59 my $pdata = <POSTDATA>;
63 $answer = from_json( $pdata );
65 return _json_error( $c, 400,
66 "Could not parse POST request '' $pdata '' as JSON: $@" );
69 $answer = from_json( $c->request->body );
71 $c->log->debug( "Received push notification from Stemweb: "
72 . to_json( $answer ) );
73 return _process_stemweb_result( $c, $answer );
75 return _json_error( $c, 403, 'Please use POST!' );
81 GET algorithms/available
83 Queries the Stemweb server for available stemma generation algorithms and their
84 parameters. Returns the JSON answer as obtained from Stemweb.
88 sub available :Local :Args(0) {
90 my $ua = LWP::UserAgent->new();
91 my $resp = $ua->get( $STEMWEB_BASE_URL . '/algorithms/available' );
92 if( $resp->is_success ) {
93 $c->stash->{'result'} = decode_json( $resp->content );
95 $c->stash->{'result'} = {};
97 $c->forward('View::JSON');
102 GET stemweb/query/<jobid>
104 A backup method to query the stemweb server to check a particular job status.
105 Returns a result as in /stemweb/result above, but status can also be -1 to
106 indicate that the job is still running.
110 sub query :Local :Args(1) {
111 my( $self, $c, $jobid ) = @_;
112 my $ua = LWP::UserAgent->new();
113 my $resp = $ua->get( $STEMWEB_BASE_URL . "/algorithms/jobstatus/$jobid" );
114 if( $resp->is_success ) {
116 my $response = decode_utf8( $resp->content );
117 $c->log->debug( "Got a response from the server: $response" );
120 $answer = from_json( $response );
122 return _json_error( $c, 500,
123 "Could not parse stemweb response '' $response '' as JSON: $@" );
125 return _process_stemweb_result( $c, $answer );
126 } elsif( $resp->code == 500 && $resp->header('Client-Warning')
127 && $resp->header('Client-Warning') eq 'Internal response' ) {
128 # The server was unavailable.
129 return _json_error( $c, 503, "The Stemweb server is currently unreachable." );
131 return _json_error( $c, 500, "Stemweb error: " . $resp->code . " / "
137 ## Helper function for parsing Stemweb result data either by push or by pull
138 sub _process_stemweb_result {
139 my( $c, $answer ) = @_;
140 # Find the specified tradition and check its job ID.
141 my $m = $c->model('Directory');
142 my $tradition = $m->tradition( $answer->{textid} );
143 unless( $tradition ) {
144 return _json_error( $c, 400, "No tradition found with ID "
145 . $answer->{textid} );
147 if( $answer->{status} == 0 ) {
149 if( $tradition->has_stemweb_jobid
150 && $tradition->stemweb_jobid eq $answer->{jobid} ) {
152 $stemmata = $tradition->record_stemweb_result( $answer );
153 $m->save( $tradition );
154 } catch( Text::Tradition::Error $e ) {
155 return _json_error( $c, 500, $e->message );
157 return _json_error( $c, 500, $@ );
160 # It may be that we already received a callback meanwhile.
161 # Check all stemmata for the given jobid and return them.
162 @$stemmata = grep { $_->came_from_jobid && $_->from_jobid eq $answer->{jobid} } $tradition->stemmata;
165 # If we got here, success!
166 # TODO Use helper in Root.pm to do this
167 my @steminfo = map { {
168 name => $_->identifier,
169 directed => _json_bool( !$_->is_undirected ),
170 svg => $_->as_svg() } }
172 $c->stash->{'result'} = {
173 'status' => 'success',
174 'stemmata' => \@steminfo };
176 # Hm, no stemmata found on this tradition with this jobid.
177 # Clear the tradition jobid so that the user can try again.
178 if( $tradition->has_stemweb_jobid ) {
179 $tradition->_clear_stemweb_jobid;
180 $m->save( $tradition );
182 $c->stash->{'result'} = { status => 'notfound' };
184 } elsif( $answer->{status} < 1 ) {
185 $c->stash->{'result'} = { 'status' => 'running' };
187 return _json_error( $c, 500,
188 "Stemweb failure not handled: " . $answer->{result} );
190 $c->forward('View::JSON');
195 GET stemweb/request/?
196 tradition=<tradition ID> &
197 algorithm=<algorithm ID> &
198 [<algorithm parameters>]
200 Send a request for the given tradition with the given parameters to Stemweb.
201 Processes and returns the JSON response given by the Stemweb server.
205 sub request :Local :Args(0) {
206 my( $self, $c ) = @_;
207 # Look up the relevant tradition and check permissions.
208 my $reqparams = $c->req->params;
209 my $tid = delete $reqparams->{tradition};
210 my $t = $c->model('Directory')->tradition( $tid );
211 my $ok = _check_permission( $c, $t );
213 return( _json_error( $c, 403,
214 'You do not have permission to update stemmata for this tradition' ) )
215 unless $ok eq 'full';
217 # Form the request for Stemweb.
218 my $algorithm = delete $reqparams->{algorithm};
219 my $return_uri = URI->new( $c->uri_for( '/stemweb/result' ) );
220 my $stemweb_request = {
221 return_path => $return_uri->path,
222 return_host => $return_uri->host_port,
223 data => $t->collation->as_tsv({noac => 1}),
224 userid => $c->user->get_object->email,
226 parameters => _cast_nonstrings( $reqparams ) };
228 # Call to the appropriate URL with the request parameters.
229 my $ua = LWP::UserAgent->new();
230 $c->log->debug( 'Sending request to Stemweb: ' . to_json( $stemweb_request ) );
231 my $resp = $ua->post( $STEMWEB_BASE_URL . "/algorithms/process/$algorithm/",
232 'Content-Type' => 'application/json; charset=utf-8',
233 'Content' => encode_json( $stemweb_request ) );
234 if( $resp->is_success ) {
236 $c->log->debug( 'Got a response from the server: '
237 . decode_utf8( $resp->content ) );
238 my $stemweb_response = decode_json( $resp->content );
240 $t->set_stemweb_jobid( $stemweb_response->{jobid} );
241 } catch( Text::Tradition::Error $e ) {
242 return _json_error( $c, 429, $e->message );
244 $c->model('Directory')->save( $t );
245 $c->stash->{'result'} = $stemweb_response;
246 $c->forward('View::JSON');
247 } elsif( $resp->code == 500 && $resp->header('Client-Warning')
248 && $resp->header('Client-Warning') eq 'Internal response' ) {
249 # The server was unavailable.
250 return _json_error( $c, 503, "The Stemweb server is currently unreachable." );
252 return _json_error( $c, 500, "Stemweb error: " . $resp->code . " / "
257 # Helper to check what permission, if any, the active user has for
258 # the given tradition
259 sub _check_permission {
260 my( $c, $tradition ) = @_;
261 my $user = $c->user_exists ? $c->user->get_object : undef;
263 return 'full' if ( $user->is_admin ||
264 ( $tradition->has_user && $tradition->user->id eq $user->id ) );
266 # Text doesn't belong to us, so maybe it's public?
267 return 'readonly' if $tradition->public;
269 # ...nope. Forbidden!
270 return _json_error( $c, 403, 'You do not have permission to view this tradition.' );
273 # QUICK HACK to deal with strict Stemweb validation.
274 sub _cast_nonstrings {
276 foreach my $k ( keys %$params ) {
277 my $v = $params->{$k};
278 if( looks_like_number( $v ) ) {
279 $params->{$k} = $v * 1;
280 } elsif ( !defined $v || $v eq 'true' ) {
281 $params->{$k} = _json_bool( $v );
287 # Helper to throw a JSON exception
289 my( $c, $code, $errmsg ) = @_;
290 $c->response->status( $code );
291 $c->stash->{'result'} = { 'error' => $errmsg };
292 $c->forward('View::JSON');
297 return $_[0] ? JSON::true : JSON::false;