blib/lib/Yote/Server/ModperlOperator.pm untested

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 215 CC 13 SCAR 27.0
CC
13
Cov
0%
handle_request
56.0
handle_json_request
12.0
new
6.0
CRAP 14.9
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 my( $path_args );
92 my $path = [ @path ];
93 while( @path ) {
94 my $k = shift @path;
95 my $v = shift @path;
96 $path_args->{$k} = $v;
97 }
98  
99 my $state_manager_class = $appinfo->{state_manager_class} || 'Yote::Server::ModperlOperatorStateManager';
100 my $state_manager = "$state_manager_class"->new( {
101 app_info => $appinfo,
102 app_path => $app_path,
103 path_args => $path_args,
104 app => $app,
105 login => $login,
106 op => $self,
107 req => $req,
108 session => $session,
109 path => $path,
110 template => $template,
111 uri => $ruri,
112 } );
113  
114 my $res;
115 eval {
116 $state_manager->_check_actions();
117 $res = $self->make_page( $state_manager );
118 $root->{STORE}->stow_all;
119 };
120 if( $@ ) {
121 print STDERR Data::Dumper->Dump([$@,"ERRY"]);
122 }
123 return $res;
124  
125 } #handle_request
126  
127 sub handle_json_request {
128 my( $self, $req ) = @_;
129  
130 my $json_payload = uri_unescape(scalar($req->param('p')));
131  
132 my $in_json = decode_json( $json_payload );
133  
134 my( $out_json, @uploads );
135  
136 #
137 # scan the payload for files
138 #
139 my $filecount = $req->param('f');
140 for (0..($filecount-1)) {
141 my $f = $req->upload( "f$_" );
142 push @uploads, $f;
143 }
144 eval {
145 $out_json = $self->{server}->invoke_payload( $json_payload, \@uploads );
146 $self->{root}->{STORE}->stow_all;
147 };
148 if( $@ ) {
149 my $err = ref $@ ? $@ : { err => "INTERNAL ERROR" };
150 $out_json = to_json( $err );
151 }
152 $req->headers_out->set(Type => 'text/json; charset=utf-8' );
153 $out_json = Encode::decode('utf8',$out_json);
154 $req->print( mark_raw($out_json) );
155 return OK;
156 } #handle_json_request
157  
158  
159 sub tmpl {
160 my( $self, @path ) = @_;
161 join( '/', $self->{template_path}, @path ).'.tx';
162 }
163  
164 sub make_page {
165 my( $self, $state_manager ) = @_;
166  
167 my $req = $state_manager->{req};
168 if( $state_manager->{redirect} ) {
169 $req->headers_out->set(Location => $state_manager->{redirect});
170 return REDIRECT;
171 }
172
173 my $template = $state_manager->{template};
174  
175 my $html = $self->{tx}->render( $self->tmpl( $template ), {%$state_manager} );
176  
177 $req->print( mark_raw($html) );
178  
179 return OK;
180 } #make_page
181  
182 package Yote::Server::ModperlOperatorStateManager;
183  
184 sub new {
185 my( $pkg, $args ) = @_;
186 my $self = {%$args};
187 bless $self, $pkg;
188 }
189  
190 sub logout {
191 my( $self ) = @_;
192  
193 my $req = $self->{req};
194 my $app = $self->{app};
195 if( $app ) {
196 $app->logout();
197 }
198 my $appinfo = $self->{app_info};
199 my $cookie_path = "/$appinfo->{cookie_path}";
200 my $token_cookie = Apache2::Cookie->new( $req,
201 -name => "yoken",
202 -path => $cookie_path,
203 -value => 0 );
204 $token_cookie->bake( $req );
205 }
206  
207 sub tmpl {
208 my( $self, @path ) = @_;
209 $self->{op}->tmpl( $self->{app_info}{template_path}, @path);
210 }
211  
212 sub upload {
213 my( $self, $name ) = @_;
214  
215 my $upload = $self->{req}->upload( $name );
216
217 if( $upload ) {
218 my $fn = $upload->filename;
219 my( $original_name, $extension ) = ( $fn =~ m!([^/]+\.([^/\.]+))$! );
220  
221 my $tmprand = "/tmp/".UUID::Tiny::create_uuid_as_string();
222 $upload->link( $tmprand );
223  
224 my $img = $self->{session}->{STORE}->newobj( {
225 file_name => $original_name,
226 file_extension => $extension,
227 file_path => $tmprand,
228 } );
229 return $img;
230 }
231 } #upload
232  
233 sub _check_actions {
234 my( $self );
235 # login check, et al go here
236 }
237  
238 1;
239  
240 __END__