OpenInteract2/Response.pm

filter: Statementstmt 22.1% 27 / 122 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 28.0% 7 / 25 Podpod 22.2% 4 / 18 totaltotal 14.7% 38 / 258 CC 35 SCAR 34.6
CC
35
Cov
14%
set_file_info
110.0
cookie
42.0
header
30.0
CRAP 31.7
« OpenInteract2/ActionResolver.pm ↑ Summary OpenInteract2/Request/Standalone.pm »
1 package OpenInteract2::Response;
2  
3 # $Id: Response.pm,v 1.28 2005/03/17 14:57:58 sjn Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use base qw( Class::Factory Class::Accessor::Fast );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use HTTP::Status qw( RC_FOUND );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use OpenInteract2::Context qw( CTX DEPLOY_URL );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 87 $OpenInteract2::Response::VERSION = sprintf("%d.%02d", q$Revision: 1.28 $ =~ /(\d+)\.(\d+)/);
14  
15 87 my ( $log );
16  
17 ########################################
18 # ACCESSORS
19  
20 # TODO: 'error_hold' is temporary, until we get the error reporting
21 # back to the template worked out
22  
23 87 my @FIELDS = qw(
24 status controller send_file send_filehandle content charset
25 );
26 87 __PACKAGE__->mk_accessors( @FIELDS, 'error_hold' );
27  
28 87 my ( $RESPONSE_TYPE, $RESPONSE_CLASS );
29  
30 0 ▶sub set_implementation_type {
Subroutine set_implementation_type: not called CC=2 SCAR=17.9
Pod undocumented
31 0 my ( $class, $type ) = @_;
32 0 my $impl_class = eval { $class->get_factory_class( $type ) };
33 0 ▶ oi_error $@ if ( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
34 0 $RESPONSE_TYPE = $type;
35 0 $RESPONSE_CLASS = $impl_class;
36 0 return $impl_class;
37 }
38  
39 0 ▶sub get_implementation_type {
Subroutine get_implementation_type: not called CC=1 SCAR=6.9
Pod undocumented
40 0 return $RESPONSE_TYPE;
41 }
42  
43 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
44 0 my ( $class, @params ) = @_;
45 0 ▶ $log ||= get_logger( LOG_RESPONSE );
Condition0%0/3
$log ||= get_logger("OI2.RESPONSE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.RESPONSE")
$log get_logger("OI2.RESPONSE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.RESPONSE")
ABresult
1X1
011
000
46 0 ▶ unless ( $RESPONSE_CLASS ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($RESPONSE_CLASS) 0 0 0
47 0 $log->fatal( "No response implementation type set" );
48 0 oi_error 'Before creating an OpenInteract2::Response object you ',
49 'must set the request type with "set_implementation_type()"';
50 }
51 0 my $self = bless( { _cookie => {},
52 _header => {} }, $RESPONSE_CLASS );
53 0 $self->init( @params );
54 0 CTX->response( $self );
55 0 return $self;
56 }
57  
58 ########################################
59 # HEADERS
60  
61 # header shortcuts
62  
63 0 ▶sub content_type {
Subroutine content_type: not called CC=2 SCAR=17.9
Pod undocumented
64 0 my ( $self, $value ) = @_;
65 0 ▶ if ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 0 0 0
66 0 $self->{_header}{'Content-Type'} = $value;
67 }
68 0 return $self->{_header}{'Content-Type'};
69 }
70  
71  
72 0 ▶sub content_type_header {
Subroutine content_type_header: not called CC=2 SCAR=17.9
Pod undocumented
73 0 my ( $self ) = @_;
74 0 my $content_type = $self->content_type;
75 0 ▶ if ( $self->charset ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->charset) 0 0 0
76 0 $content_type .= "; charset=" . $self->charset;
77 }
78 0 return $content_type;
79 }
80  
81  
82 # TODO: We may want to dereference $self->{_header} before sending it
83 # back, otherwise people can add headers directly to the hash. OTOH,
84 # this may be a good thing...
85  
86 # TODO: Use an HTTP::Headers object here?
87  
88 0 ▶sub header {
Subroutine header: not called CC=5 SCAR=34.0
Pod undocumented
89 0 my ( $self, $name, $value ) = @_;
90 0 ▶ unless ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
91 0 return $self->{_header};
92 }
93 0 ▶ $log ||= get_logger( LOG_RESPONSE );
Condition0%0/3
$log ||= get_logger("OI2.RESPONSE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.RESPONSE")
$log get_logger("OI2.RESPONSE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.RESPONSE")
ABresult
1X1
011
000
94 0 ▶ if ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 0 0 0
95 0 $self->{_header}{ $name } = $value;
96 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Setting header '${name}' to '${value}'") 0 0 0
97 $log->debug( "Setting header '$name' to '$value'" );
98 }
99 0 return $self->{_header}{ $name };
100 }
101  
102 0 ▶sub remove_header {
Subroutine remove_header: not called CC=4 SCAR=30.0
Pod undocumented
103 0 my ( $self, $name ) = @_;
104 0 ▶ $log ||= get_logger( LOG_RESPONSE );
Condition0%0/3
$log ||= get_logger("OI2.RESPONSE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.RESPONSE")
$log get_logger("OI2.RESPONSE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.RESPONSE")
ABresult
1X1
011
000
105 0 ▶ if ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 0 0 0
106 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Removing header '${name}' from response") 0 0 0
107 $log->debug( "Removing header '$name' from response" );
108 0 return delete $self->{_header}{ $name };
109 }
110 }
111  
112 0 ▶sub is_redirect {
Subroutine is_redirect: not called CC=3 SCAR=24.8
Pod undocumented
113 0 my ( $self ) = @_;
114 0 ▶ my $is_redir = ( $self->status && $self->status == RC_FOUND );
Condition0%0/3
$self->status && $self->status == 302
!ll&&!rl&&r
000
MC/DC0%0/2
$self->status && $self->status == 302
$self->status $self->status == 302
Truth table0%0/3
A && B
A= $self->statusB= $self->status == 302
ABresult
0X0
100
111
115 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug(sprintf("Checking to see if status '%s' is redirect: %s", $self->status, $is_redir)) 0 0 0
116 $log->debug( sprintf( "Checking to see if status '%s' is redirect: %s",
117 $self->status, $is_redir ) );
118 0 return $is_redir;
119 }
120  
121 # Cookies are special types of headers; the response should collect
122 # all of these and put them into the header in send()
123 # $cookie should be a CGI::Cookie object
124  
125 0 ▶sub cookie {
Subroutine cookie: not called CC=6 SCAR=37.4
Pod undocumented
126 0 my ( $self, $cookie ) = @_;
127 0 ▶ $log ||= get_logger( LOG_RESPONSE );
Condition0%0/3
$log ||= get_logger("OI2.RESPONSE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.RESPONSE")
$log get_logger("OI2.RESPONSE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.RESPONSE")
ABresult
1X1
011
000
128 0 ▶ unless ( $cookie ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cookie) 0 0 0
129 0 return [ values %{ $self->{_cookie} } ];
130 }
131 0 ▶ unless ( UNIVERSAL::isa( $cookie, 'CGI::Cookie' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($cookie, "CGI::Cookie")) 0 0 0
132 0 $log->warn( "Tried to add a 'cookie()' to the response without ",
133 "passing a valid cookie object. (Must be a ",
134 "'CGI::Cookie' object or have it as a parent.)" );
135 0 return;
136 }
137 0 my $name = $cookie->name;
138 0 ▶ unless ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
139 0 $log->warn( "Cannot add cookie to response without a name; ",
140 "please ensure the 'CGI::Cookie' object set has a name" );
141 0 return;
142 }
143 $log->is_debug &&
144 0 ▶ $log->debug( "Setting cookie '$name' to '$cookie'" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Setting cookie '${name}' to '${cookie}'") 0 0 0
145 0 $self->{_cookie}{ $name } = $cookie;
146 0 return $cookie;
147 }
148  
149 0 ▶sub remove_cookie {
Subroutine remove_cookie: not called CC=4 SCAR=30.0
Pod undocumented
150 0 my ( $self, $name ) = @_;
151 0 ▶ $log ||= get_logger( LOG_RESPONSE );
Condition0%0/3
$log ||= get_logger("OI2.RESPONSE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.RESPONSE")
$log get_logger("OI2.RESPONSE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.RESPONSE")
ABresult
1X1
011
000
152 0 ▶ if ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 0 0 0
153 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Removing cookie '${name}' from response") 0 0 0
154 $log->debug( "Removing cookie '$name' from response" );
155 0 return delete $self->{_cookie}{ $name };
156 }
157 }
158  
159  
160 0 ▶sub save_session {
Subroutine save_session: not called CC=1 SCAR=6.9
Pod undocumented
161 0 my ( $self ) = @_;
162 0 OpenInteract2::SessionManager->save( CTX->request->session );
163 }
164  
165  
166 0 ▶sub set_file_info {
Subroutine set_file_info: not called CC=10 SCAR=47.0
Pod undocumented
167 0 my ( $self ) = @_;
168 0 ▶ $log ||= get_logger( LOG_RESPONSE );
Condition0%0/3
$log ||= get_logger("OI2.RESPONSE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.RESPONSE")
$log get_logger("OI2.RESPONSE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.RESPONSE")
ABresult
1X1
011
000
169 0 my $filename = $self->send_file;
170 0 my $filehandle = $self->send_filehandle;
171 0 ▶ unless ( $filename || $filehandle ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename or $filehandle) 0 0 0
Condition0%0/3
$filename or $filehandle
l!l&&r!l&&!r
000
MC/DC0%0/2
$filename or $filehandle
$filename $filehandle
Truth table0%0/3
A or B
A= $filenameB= $filehandle
ABresult
1X1
011
000
172 0 return undef;
173 }
174  
175 0 ▶ if ( $filename ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filename) { } 0 0 0
elsif ($filehandle) { } 0 0 0
176 0 ▶ unless ( -f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $filename) 0 0 0
177 0 oi_error "Cannot set outbound file information for ",
178 "'$filename': file does not exist";
179 }
180 $log->is_debug &&
181 0 ▶ $log->debug( "Set response information for file '$filename'" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Set response information for file '${filename}'") 0 0 0
182 0 ▶ unless ( $self->header( 'Content-Length' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->header("Content-Length")) 0 0 0
183 0 $self->header( 'Content-Length', (stat $filename)[7] );
184 }
185 0 ▶ unless ( $self->header( 'Content-Type' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->header("Content-Type")) 0 0 0
186 0 $self->content_type(
187 OpenInteract2::File->get_mime_type({
188 filename => $filename
189 })
190 );
191 }
192 }
193 elsif ( $filehandle ) {
194 0 $self->header( 'Content-Length', (stat $filehandle)[7] );
195 }
196 }
197  
198 ########################################
199 # PROPERTIES
200  
201 0 ▶sub return_url {
Subroutine return_url: not called CC=4 SCAR=30.0
Pod undocumented
202 0 my ( $self, $return_url ) = @_;
203 0 ▶ if ( $return_url ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return_url) 0 0 0
204 $self->{return_url} =
205 0 OpenInteract2::URL->create_relative_to_absolute( $return_url );
206 }
207 0 ▶ unless ( $self->{return_url} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"return_url"}) 0 0 0
208 0 ▶ my $url = DEPLOY_URL || '/';
Condition0%0/2
DEPLOY_URL() || '/'
l!l
00
MC/DC0%0/1
DEPLOY_URL() || '/'
DEPLOY_URL()
Truth table0%0/2
A
A= DEPLOY_URL()
Aresult
11
00
209 $self->{return_url} =
210 0 OpenInteract2::URL->create_relative_to_absolute( $url );
211 }
212 0 return $self->{return_url};
213 }
214  
215 ########################################
216 # FACTORY
217  
218 0 ▶sub factory_log {
Subroutine factory_log: not called CC=1 SCAR=6.9
Pod undocumented
219 0 my ( $self, @msg ) = @_;
220 0 get_logger( LOG_RESPONSE )->info( @msg );
221 }
222  
223 0 ▶sub factory_error {
Subroutine factory_error: not called CC=1 SCAR=6.9
Pod undocumented
224 0 my ( $self, @msg ) = @_;
225 0 get_logger( LOG_RESPONSE )->error( @msg );
226 0 die @msg, "\n";
227 }
228  
229  
230 ########################################
231 # OVERRIDE
232  
233 # Initialize new object
234 0 ▶sub init { die "Subclass must implement init()" }
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
235  
236 # Set/send HTTP headers + cookies + content
237 0 ▶sub send { die "Subclass must implement send()" }
Subroutine send: not called CC=1 SCAR=6.9
Pod documented
238  
239 # Redirect to another URL (yay, proper HTTP redirects!)
240 0 ▶sub redirect { die "Subclass must implement redirect()" }
Subroutine redirect: not called CC=1 SCAR=6.9
Pod documented
241  
242 87 1;
243  
244 __END__
« OpenInteract2/ActionResolver.pm ↑ Summary OpenInteract2/Request/Standalone.pm »