1 package stemmaweb::Controller::Stemweb;
3 use namespace::autoclean;
4 use JSON qw/ from_json /;
10 BEGIN { extends 'Catalyst::Controller' }
12 ## TODO Move the /algorithms/available function to the Stemweb module
13 my $STEMWEB_BASE_URL = 'http://slinkola.users.cs.helsinki.fi';
17 stemmaweb::Controller::Stemweb - Client listener for Stemweb results
21 This is a client listener for the Stemweb API as implemented by the protocol defined at
22 L<https://docs.google.com/document/d/1aNYGAo1v1WPDZi6LXZ30FJSMJwF8RQPYbOkKqHdCZEc/pub>.
29 Content-Type: application/json
38 result: <error message> }
40 Used by the Stemweb server to notify us that one or more stemma graphs
41 has been calculated in response to an earlier request.
45 sub result :Local :Args(0) {
47 if( $c->request->method eq 'POST' ) {
48 # TODO: Verify the sender!
50 if( ref( $c->request->body ) eq 'File::Temp' ) {
51 # Read in the file and parse that.
52 open( POSTDATA, $c->request->body ) or die "Failed to open post data file";
53 binmode( POSTDATA, ':utf8' );
54 # JSON should be all one line
55 my $pdata = <POSTDATA>;
58 $answer = from_json( $pdata );
60 $answer = from_json( $c->request->body );
62 # Find a tradition with the defined Stemweb job ID.
63 # TODO: Maybe get Stemweb to pass back the tradition ID...
64 my $m = $c->model('Directory');
66 $m->scan( sub{ push( @traditions, $_[0] )
67 if $_[0]->$_isa('Text::Tradition')
68 && $_[0]->has_stemweb_jobid
69 && $_[0]->stemweb_jobid eq $answer->{job_id};
71 if( @traditions == 1 ) {
72 my $tradition = shift @traditions;
73 if( $answer->{status} == 0 ) {
75 $tradition->record_stemweb_result( $answer );
76 $m->save( $tradition );
77 } catch( Text::Tradition::Error $e ) {
78 return _json_error( $c, 500, $e->message );
80 return _json_error( $c, 500, $@ );
82 # If we got here, success!
83 $c->stash->{'result'} = { 'status' => 'success' };
84 $c->forward('View::JSON');
86 return _json_error( $c, 500,
87 "Stemweb failure not handled: " . $answer->{result} );
89 } elsif( @traditions ) {
90 return _json_error( $c, 500,
91 "Multiple traditions with Stemweb job ID " . $answer->{job_id} . "!" );
93 return _json_error( $c, 400,
94 "No tradition found with Stemweb job ID " . $answer->{job_id} );
97 return _json_error( $c, 403, 'Please use POST!' );
103 GET stemweb/request/?
104 tradition=<tradition ID> &
105 algorithm=<algorithm ID> &
106 [<algorithm parameters>]
108 Send a request for the given tradition with the given parameters to Stemweb.
109 Processes and returns the JSON response given by the Stemweb server.
113 sub request :Local :Args(0) {
114 my( $self, $c ) = @_;
115 # Look up the relevant tradition and check permissions.
116 my $reqparams = $c->req->params;
117 my $tid = delete $reqparams->{tradition};
118 my $t = $c->model('Directory')->tradition( $tid );
119 my $ok = _check_permission( $c, $t );
121 return( _json_error( $c, 403,
122 'You do not have permission to update stemmata for this tradition' ) )
123 unless $ok eq 'full';
125 # Form the request for Stemweb.
126 my $algorithm = delete $reqparams->{algorithm};
127 my $return_uri = URI->new( $c->uri_for( '/stemweb/result' ) );
128 my $stemweb_request = {
129 return_path => $return_uri->path,
130 return_host => $return_uri->host_port,
131 data => $t->collation->as_tsv,
132 userid => $c->user->email,
133 parameters => $reqparams };
135 # Call to the appropriate URL with the request parameters.
136 my $ua = LWP::UserAgent->new();
137 my $resp = $ua->post( $STEMWEB_BASE_URL . "/algorithms/process/$algorithm/",
138 'Content-Type' => 'application/json; charset=utf-8',
139 'Content' => encode_json( $stemweb_request ) );
140 if( $resp->is_success ) {
142 my $stemweb_response = decode_json( $resp->content );
144 $t->set_stemweb_jobid( $stemweb_response->{jobid} );
145 } catch( Text::Tradition::Error $e ) {
146 return _json_error( $c, 429, $e->message );
148 $c->model('Directory')->save( $t );
149 $c->stash->{'result'} = $stemweb_response;
150 $c->forward('View::JSON');
151 } elsif( $resp->code == 500 && $resp->header('Client-Warning')
152 && $resp->header('Client-Warning') eq 'Internal response' ) {
153 # The server was unavailable.
154 return _json_error( $c, 503, "The Stemweb server is currently unreachable." );
156 return _json_error( $c, 500, "Stemweb error: " . $resp->status . " / "
161 # Helper to check what permission, if any, the active user has for
162 # the given tradition
163 sub _check_permission {
164 my( $c, $tradition ) = @_;
165 my $user = $c->user_exists ? $c->user->get_object : undef;
167 return 'full' if ( $user->is_admin ||
168 ( $tradition->has_user && $tradition->user->id eq $user->id ) );
170 # Text doesn't belong to us, so maybe it's public?
171 return 'readonly' if $tradition->public;
173 # ...nope. Forbidden!
174 return _json_error( $c, 403, 'You do not have permission to view this tradition.' );
177 # Helper to throw a JSON exception
179 my( $c, $code, $errmsg ) = @_;
180 $c->response->status( $code );
181 $c->stash->{'result'} = { 'error' => $errmsg };
182 $c->forward('View::JSON');