1 package stemmaweb::Controller::Stemweb;
3 use namespace::autoclean;
4 use Encode qw/ decode_utf8 /;
5 use JSON qw/ decode_json encode_json from_json /;
11 BEGIN { extends 'Catalyst::Controller' }
13 ## TODO Move the /algorithms/available function to the Stemweb module
14 my $STEMWEB_BASE_URL = 'http://slinkola.users.cs.helsinki.fi';
18 stemmaweb::Controller::Stemweb - Client listener for Stemweb results
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>.
30 Content-Type: application/json
39 result: <error message> }
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.
46 sub result :Local :Args(0) {
48 if( $c->request->method eq 'POST' ) {
49 # TODO: Verify the sender!
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>;
59 $answer = from_json( $pdata );
61 $answer = from_json( $c->request->body );
63 # Find a tradition with the defined Stemweb job ID.
64 # TODO: Maybe get Stemweb to pass back the tradition ID...
65 my $m = $c->model('Directory');
67 $m->scan( sub{ push( @traditions, $_[0] )
68 if $_[0]->$_isa('Text::Tradition')
69 && $_[0]->has_stemweb_jobid
70 && $_[0]->stemweb_jobid eq $answer->{job_id};
72 if( @traditions == 1 ) {
73 my $tradition = shift @traditions;
74 if( $answer->{status} == 0 ) {
76 $tradition->record_stemweb_result( $answer );
77 $m->save( $tradition );
78 } catch( Text::Tradition::Error $e ) {
79 return _json_error( $c, 500, $e->message );
81 return _json_error( $c, 500, $@ );
83 # If we got here, success!
84 $c->stash->{'result'} = { 'status' => 'success' };
85 $c->forward('View::JSON');
87 return _json_error( $c, 500,
88 "Stemweb failure not handled: " . $answer->{result} );
90 } elsif( @traditions ) {
91 return _json_error( $c, 500,
92 "Multiple traditions with Stemweb job ID " . $answer->{job_id} . "!" );
94 return _json_error( $c, 400,
95 "No tradition found with Stemweb job ID " . $answer->{job_id} );
98 return _json_error( $c, 403, 'Please use POST!' );
104 GET stemweb/request/?
105 tradition=<tradition ID> &
106 algorithm=<algorithm ID> &
107 [<algorithm parameters>]
109 Send a request for the given tradition with the given parameters to Stemweb.
110 Processes and returns the JSON response given by the Stemweb server.
114 sub request :Local :Args(0) {
115 my( $self, $c ) = @_;
116 # Look up the relevant tradition and check permissions.
117 my $reqparams = $c->req->params;
118 my $tid = delete $reqparams->{tradition};
119 my $t = $c->model('Directory')->tradition( $tid );
120 my $ok = _check_permission( $c, $t );
122 return( _json_error( $c, 403,
123 'You do not have permission to update stemmata for this tradition' ) )
124 unless $ok eq 'full';
126 # Form the request for Stemweb.
127 my $algorithm = delete $reqparams->{algorithm};
128 my $return_uri = URI->new( $c->uri_for( '/stemweb/result' ) );
129 my $stemweb_request = {
130 return_path => $return_uri->path,
131 return_host => $return_uri->host_port,
132 data => $t->collation->as_tsv,
133 userid => $t->user->email,
134 parameters => $reqparams };
136 # Call to the appropriate URL with the request parameters.
138 my $ua = LWP::UserAgent->new();
139 my $resp = $ua->post( $STEMWEB_BASE_URL . "/algorithms/process/$algorithm/",
140 'Content-Type' => 'application/json; charset=utf-8',
141 'Content' => encode_json( $stemweb_request ) );
142 if( $resp->is_success ) {
144 $c->log->debug( 'Got a response from the server: '
145 . decode_utf8( $stemweb_response ) );
146 my $stemweb_response = decode_json( $resp->content );
148 $t->set_stemweb_jobid( $stemweb_response->{jobid} );
149 } catch( Text::Tradition::Error $e ) {
150 return _json_error( $c, 429, $e->message );
152 $c->model('Directory')->save( $t );
153 $c->stash->{'result'} = $stemweb_response;
154 $c->forward('View::JSON');
155 } elsif( $resp->code == 500 && $resp->header('Client-Warning')
156 && $resp->header('Client-Warning') eq 'Internal response' ) {
157 # The server was unavailable.
158 return _json_error( $c, 503, "The Stemweb server is currently unreachable." );
160 return _json_error( $c, 500, "Stemweb error: " . $resp->code . " / "
165 # Helper to check what permission, if any, the active user has for
166 # the given tradition
167 sub _check_permission {
168 my( $c, $tradition ) = @_;
169 my $user = $c->user_exists ? $c->user->get_object : undef;
171 return 'full' if ( $user->is_admin ||
172 ( $tradition->has_user && $tradition->user->id eq $user->id ) );
174 # Text doesn't belong to us, so maybe it's public?
175 return 'readonly' if $tradition->public;
177 # ...nope. Forbidden!
178 return _json_error( $c, 403, 'You do not have permission to view this tradition.' );
181 # Helper to throw a JSON exception
183 my( $c, $code, $errmsg ) = @_;
184 $c->response->status( $code );
185 $c->stash->{'result'} = { 'error' => $errmsg };
186 $c->forward('View::JSON');