Server/ModperlOperator.pm untested

filter: Statementstmt 0.0% 0 / 150 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 224 CC 13 SCAR 26.8
CC
13
Cov
0%
handle_request
56.0
handle_json_request
12.0
new
6.0
CRAP 14.6
1 package Yote::Server::ModperlOperator;
2  
3 use strict;
4 no strict 'refs';
5  
6 use Apache2::Cookie;
7 use Apache2::Const qw(:common);
8  
9 use Data::Dumper;
10 use Text::Xslate qw(mark_raw);
11 use Encode;
12 use HTML::Entities;
13 use JSON;
14 use URI::Escape;
15  
16 use Yote::Server;
17  
18 sub new {
19 my( $pkg, %options ) = @_;
20  
21 #
22 # Setup the yote part of this
23 #
24 my $yote_root_dir = '/opt/yote';
25 eval {
26 require Yote::Server::ConfigData;
27 $yote_root_dir = Yote::Server::ConfigData->config( 'yote_root' );
28 };
29 unshift @INC, "$yote_root_dir/lib";
30 my $yote_options = Yote::Server::load_options( $yote_root_dir );
31 my $server = new Yote::Server( $yote_options );
32 my $store = $server->store;
33 my $root = $store->fetch_server_root;
34  
35  
36 bless {
37 apps => $options{apps}, # hash of app -> app info
38 template_path => $options{template_path},
39 root => $root,
40 server => $server,
41 tx => new Text::Xslate(
42 function => {
43 html_encode => sub {
44 # have to convert the text from perl interlal to octets
45 my $txt = shift;
46 if( length($txt) != length( Encode::decode('utf8', $txt ) ) ) {
47 $txt = Encode::decode( 'utf8', $txt );
48 }
49 mark_raw( encode_entities($txt));
50 },
51 }
52 ),
53 }, $pkg;
54  
55 } #new
56  
57 sub handle_request {
58 my( $self, $req ) = @_;
59  
60 my $ruri = $req->uri;
61 $ruri =~ s!^/!!;
62 my( $app_path, @path ) = split '/', $ruri;
63  
64 my $jar = Apache2::Cookie::Jar->new($req);
65 my $token_cookie = $jar->cookies("yoken");
66 my $root = $self->{root};
67 my $appinfo = $self->{apps}{$app_path};
68  
69 my( $app, $login, $session );
70 $session = $root ? $root->fetch_session( $token_cookie ? $token_cookie->value : 0 ) : undef;
71 unless( $token_cookie && $token_cookie->value eq $session->get__token ) {
72 my $cookie_path = "/$appinfo->{cookie_path}";
73 $token_cookie = Apache2::Cookie->new( $req,
74 -name => "yoken",
75 -expires => '+1D',
76 -path => $cookie_path,
77 -value => $session->get__token );
78 $token_cookie->bake( $req );
79 }
80 my $template = $appinfo->{main_template} || 'main';
81 if( $appinfo && $root ) {
82 $root->{SESSION} = $session;
83 ( $app, $login ) = $root->fetch_app( $appinfo->{app_name} );
84 $app->{SESSION} = $session;
85 if( $login ) {
86 $login->{SESSION} = $session;
87 }
88 $template = "$app_path/$template";
89 }
90  
91 #
92 # assume the path is split into key/val pairs.
93 # this is presumptuous, but some things might use it
94 # maybe shouldn't include this?
95 #
96 my( $path_args );
97 my $path = [ @path ];
98 while( @path ) {
99 my $k = shift @path;
100 my $v = shift @path;
101 $path_args->{$k} = $v;
102 }
103  
104 my $state_manager_class = $appinfo->{state_manager_class} || 'Yote::Server::ModperlOperatorStateManager';
105 my $state_manager = "$state_manager_class"->new( {
106 app_info => $appinfo,
107 app_path => $app_path,
108 path_args => $path_args,
109 app => $app,
110 login => $login,
111 op => $self, #this operator
112 req => $req,
113 session => $session,
114 path => $path,
115 template => $template,
116 uri => $ruri,
117 } );
118  
119 my $res;
120 eval {
121 $state_manager->_check_actions();
122 $res = $self->make_page( $state_manager );
123 $root->{STORE}->stow_all;
124 };
125 if( $@ ) {
126 print STDERR Data::Dumper->Dump([$@,"ERRY"]);
127 }
128 return $res;
129  
130 } #handle_request
131  
132 sub handle_json_request {
133 my( $self, $req ) = @_;
134  
135 my $json_payload = uri_unescape(scalar($req->param('p')));
136  
137 my $in_json = decode_json( $json_payload );
138  
139 my( $out_json, @uploads );
140  
141 #
142 # scan the payload for files
143 #
144 my $filecount = $req->param('f');
145 for (0..($filecount-1)) {
146 my $f = $req->upload( "f$_" );
147 push @uploads, $f;
148 }
149 eval {
150 $out_json = $self->{server}->invoke_payload( $json_payload, \@uploads );
151 };
152 if( $@ ) {
153 my $err = ref $@ ? $@ : { err => "INTERNAL ERROR" };
154 $out_json = to_json( $err );
155 }
156 $req->content_type('text/json; charset=utf-8');
157 $out_json = Encode::decode('utf8',$out_json);
158 $req->print( mark_raw($out_json) );
159 return OK;
160 } #handle_json_request
161  
162  
163 sub tmpl {
164 my( $self, @path ) = @_;
165 join( '/', $self->{template_path}, @path ).'.tx';
166 }
167  
168 sub make_page {
169 my( $self, $state_manager ) = @_;
170  
171 my $req = $state_manager->{req};
172 if( $state_manager->{redirect} ) {
173 $req->headers_out->set(Location => $state_manager->{redirect});
174 return REDIRECT;
175 }
176 $req->content_type('text/html');
177 my $template = $state_manager->template;
178  
179 my $html = $self->{tx}->render( $self->tmpl( $template ), {%$state_manager} );
180  
181 $req->print( mark_raw($html) );
182  
183 return OK;
184 } #make_page
185  
186 package Yote::Server::ModperlOperatorStateManager;
187  
188 sub new {
189 my( $pkg, $args ) = @_;
190 my $self = {%$args};
191 bless $self, $pkg;
192 }
193  
194 #
195 # Can be overridden. Template to render this request.
196 #
197 sub template {
198 shift->{template};
199 }
200  
201 sub logout {
202 my( $self ) = @_;
203  
204 my $req = $self->{req};
205 my $app = $self->{app};
206 if( $app ) {
207 $app->logout();
208 }
209 my $appinfo = $self->{app_info};
210 my $cookie_path = "/$appinfo->{cookie_path}";
211 my $token_cookie = Apache2::Cookie->new( $req,
212 -name => "yoken",
213 -path => $cookie_path,
214 -value => 0 );
215 $token_cookie->bake( $req );
216 }
217  
218 #
219 # Render a template with the given path (list)
220 #
221 sub tmpl {
222 my( $self, @path ) = @_;
223 $self->{op}->tmpl( $self->{app_info}{template_path}, @path);
224 }
225  
226 sub upload {
227 my( $self, $name ) = @_;
228  
229 my $upload = $self->{req}->upload( $name );
230
231 if( $upload ) {
232 my $fn = $upload->filename;
233 my( $original_name, $extension ) = ( $fn =~ m!([^/]+\.([^/\.]+))$! );
234  
235 my $tmprand = "/tmp/".UUID::Tiny::create_uuid_as_string();
236 $upload->link( $tmprand );
237  
238 my $img = $self->{session}->{STORE}->newobj( {
239 file_name => $original_name,
240 file_extension => $extension,
241 file_path => $tmprand,
242 } );
243 return $img;
244 }
245 } #upload
246  
247 sub _check_actions {
248 my( $self );
249 # login check, et al go here
250 }
251  
252 1;
253  
254 __END__