blib/lib/Apache/AxKit/Provider/XMLDOMProvider.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Apache::AxKit::Provider::XMLDOMProvider;
2  
3 1 ▶use base qw(Apache::AxKit::Provider);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Apache;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Apache::Log;
9 use Apache::AxKit::Exception;
10 use Apache::AxKit::Provider::File;
11 use XML::LibXML;
12 use LWP::UserAgent;
13 use Time::Piece;
14 # only used for debugging
15 #use Data::Dumper;
16  
17 require Exporter;
18  
19 our @ISA = qw(Exporter Apache::AxKit::Provider);
20  
21 # Items to export into callers namespace by default. Note: do not export
22 # names by default without a very good reason. Use EXPORT_OK instead.
23 # Do not simply export all your public functions/methods/constants.
24  
25 # This allows declaration use Apache::AxKit::Provider::XMLDOMProvider ':all';
26 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
27 # will save memory.
28 our %EXPORT_TAGS = ( 'all' => [ qw(
29 new init process exists mtime get_fh get_strref key get_styles get_ext_ent_handler
30 ) ] );
31  
32 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
33  
34 our @EXPORT = qw(
35 new init process exists mtime get_fh get_strref key get_styles get_ext_ent_handler
36 );
37  
38 our $VERSION = '0.03';
39  
40 # sub: init
41 # here we do some initialization stuff.
42 sub init {
43 my $self = shift;
44 my $r = $self->{apache};
45 my $mtime_element = $r->dir_config('RemoteXMLmTimeElement');
46  
47 # prepare remote XML
48 my $url = $r->dir_config('RemoteXML');
49 # append query if defined
50 my $query = $r->args();
51 if ( defined $query ) {
52 $url .= "?" . $query;
53 }
54  
55 # get xml with substitute request
56 my $ua = LWP::UserAgent->new();
57 $ua->timeout(10);
58 my $response = $ua->get($url);
59  
60 # store some stuff for later use
61 $self->{response} = $response;
62 $self->{xmlfile} = $url;
63 $self->{id} = $url;
64 $self->{mtime_element} = $mtime_element;
65 }
66  
67 # sub: get_fh
68 # we don't want to handle files, so we just throw an exception here.
69 sub get_fh {
70 throw Apache::AxKit::Exception::IO( -text => "Can't get filehandle for XMLDOMProvider (not yet implemented)!" );
71 }
72  
73 # sub: get_strref
74 # since we refused to work with file handles, we HAVE to define this.
75 # returns a string containing the remote XML-DOM
76 sub get_strref {
77 my $self = shift;
78 my $response = $self->{response};
79 my $string = $response->content();
80  
81 # debug
82 #my $h = $response->server;
83 #throw Apache::AxKit::Exception::Error(-text => "Last Modified Header: \"$h\"");
84  
85 # some XML validation
86 my $parser = XML::LibXML->new();
87 $parser->validation(0);
88 $parser->load_ext_dtd(0);
89 $parser->expand_xinclude(0);
90 $parser->expand_entities(0);
91 my $dom;
92 eval {
93 $dom = $parser->parse_string($string);
94 };
95 if ($@) {
96 throw Apache::AxKit::Exception::Error( -text => "Input must be well-formed XML: $@" );
97 }
98  
99 # if everything went fine, return xml-string-reference
100 return \$string;
101 }
102  
103 # sub: mtime
104 # we want to cache our stuff; the mtime is determined
105 # from the mdate (YYYY-MM-DD hh:mm) element "<mdate></mdate>" placed
106 # somewhere in the remote XML-DOM;
107 # CAUTION: do not provide multiple mdate elements in one single XML-DOM;
108 # if mdate is not provided, we refuse to cache
109 sub mtime {
110 my $self = shift;
111 my $mtime_element = $self->{mtime_element};
112  
113 # debug
114 #print STDERR Dumper( $self->{id} );
115  
116 # return cached mtime, if this is the second call per request
117 return $self->{mtime}->{$self->{id}} if defined $self->{mtime}->{$self->{id}};
118  
119 # mtime stuff
120 # twiggle out mdate from string-content
121 $self->{response}->content() =~ m/.*\<$mtime_element\>([^\<]*)\<\/$mtime_element\>/;
122 my $mdate = $1;
123  
124 # debug
125 #print STDERR Dumper( "mdate: ", $mdate );
126  
127 my $mtime;
128 # test if $mdate is provided in xml-dom
129  
130 if ( defined $mdate ) {
131 # create time-piece object
132 # mdate format example: 2007-06-26 02:42
133 my $time_obj = Time::Piece->strptime( $mdate, "%Y-%m-%d %H:%M" );
134 $mtime = $time_obj->epoch();
135  
136 # debug
137 #print STDERR Dumper( "mtime in secs: ", $mtime );
138 }
139 else {
140 # else invalidate cache
141 $mtime = time();
142
143 # debug
144 #print STDERR Dumper("now: ", $mtime );
145 }
146  
147 # cache mtime, since mtime is called twice per request
148 $self->{mtime}->{$self->{id}} = $mtime;
149  
150 # debug
151 #print STDERR Dumper( "return value: ", $mtime );
152  
153 return $mtime;
154 }
155  
156 # sub: process
157 # we always answer requests :-)
158 sub process {
159 my $self = shift;
160 return 1;
161 }
162  
163 # sub: key
164 # should return a unique identifier for the resource.
165 # we assume the id from the uri (including query params) is a good one.
166 sub key {
167 my $self = shift;
168 return $self->{id};
169 }
170  
171 # sub: exists
172 # returns 1 if remote XML-DOM is accessible
173 # throws AxKit IO exception otherwise
174 sub exists {
175 my $self = shift;
176 my $response = $self->{response};
177 my $url = $self->{xmlfile};
178 if ( $response->is_success() ) {
179 return 1;
180 }
181 else {
182 throw Apache::AxKit::Exception::IO( -text => $response->status_line() . ": Cannot access upstream resource: \"$url\"" );
183 }
184 }
185  
186 1;
187 __END__
↑ Summary