CORION/Apache/Tika/Server.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 192 CC 13 SCAR 36.9
CC
13
Cov
0%
fetch
110.0
launch
6.0
synchronous
6.0
CRAP 40.1
1 package CORION::Apache::Tika::Server;
2 use strict;
3 use Carp qw(croak);
4 # Fire up/stop a Tika instance
5 use Moo;
6 use HTTP::Request::Common;
7 use CORION::Apache::Tika::DocInfo;
8 use Data::Dumper;
9 use Promises;
10  
11 =head1 SYNOPSIS
12  
13 use CORION::Apache::Tika::Server;
14  
15 # Launch our own Apache Tika instance
16 my $tika= CORION::Apache::Tika::Server->new();
17 $tika->launch();
18  
19 my $fn= shift;
20  
21 use Data::Dumper;
22 print Dumper $tika->get_meta($fn);
23 print Dumper $tika->get_text($fn);
24 print Dumper $tika->get_language($fn);
25  
26 my $info = $tika->get_all($fn);
27 print Dumper $info->meta;
28  
29 =cut
30  
31 use vars '$VERSION';
32 $VERSION = '0.06';
33  
34 extends 'CORION::Apache::Tika';
35  
36 sub load_module {
37 my( $module ) = @_;
38 $module =~ s!::!/!g;
39 require "$module.pm"
40 }
41  
42 has pid => (
43 is => 'rw',
44 #isa => 'Int',
45 );
46  
47 has port => (
48 is => 'ro',
49 #isa => 'Int',
50 default => sub { 9998 },
51 );
52  
53 has fh => (
54 is => 'rw',
55 #isa => 'Array',
56 );
57  
58 has connection_class => (
59 is => 'ro',
60 default => 'CORION::Apache::Tika::Connection::AEHTTP',
61 );
62  
63 has ua => (
64 is => 'ro',
65 #isa => 'Str',
66 default => sub { load_module( $_[0]->connection_class ); $_[0]->connection_class->new },
67 );
68  
69 sub cmdline {
70 my( $self )= @_;
71 $self->java,
72 @{$self->java_args},
73 '-jar',
74 $self->jarfile,
75 #'--port', $self->port,
76 @{$self->tika_args},
77 };
78  
79 sub launch {
80 my( $self )= @_;
81 if( !$self->pid ) {
82 my $cmdline= join " ", $self->cmdline; # well, for Windows...
83 #warn $cmdline;
84 my $pid= open my $fh, "$cmdline |"
85 or croak "Couldn't launch [$cmdline]: $!/$^E";
86 $self->pid( $pid );
87 $self->fh( $fh );
88 sleep 2; # Java...
89 };
90 }
91  
92 sub url {
93 # Should return URI instead
94 my( $self, $type )= @_;
95 $type||= 'text';
96
97 my $url= {
98 text => 'rmeta',
99 test => 'tika', # but GET instead of PUT
100 meta => 'rmeta',
101 #all => 'all',
102 language => 'language/string',
103 all => 'rmeta',
104 # unpack
105 }->{ $type };
106
107 sprintf
108 'http://127.0.0.1:%s/%s',
109 $self->port,
110 $url
111 };
112  
113  
114 sub synchronous($) {
115 my $promise = $_[0];
116 my @res;
117 if( $promise->is_unfulfilled ) {
118 require AnyEvent;
119 my $await = AnyEvent->condvar;
120 $promise->then(sub{ $await->send(@_)});
121 @res = $await->recv;
122 } else {
123 @res = @{ $promise->result }
124 }
125 @res
126 };
127  
128 # /rmeta
129 # /unpacker
130 # /all
131 # /tika
132 # /language
133 # hello world
134 sub fetch {
135 my( $self, %options )= @_;
136 $options{ type }||= 'text';
137 my $url= $self->url( $options{ type } );
138
139 my $content= $options{ content };
140 if(! $content and $options{ filename }) {
141 # read $options{ filename }
142 open my $fh, '<', $options{ filename }
143 or croak "Couldn't read '$options{ filename }': $!";
144 binmode $fh;
145 local $/;
146 $content = <$fh>;
147 };
148
149 my $method;
150 if( 'test' eq $options{ type } ) {
151 $method= 'get';
152  
153 } else {
154 $method= 'put';
155 ;
156 };
157
158 my $headers = $options{ headers } || {};
159
160 my ($code,$res) = synchronous
161 $self->ua->request( $method, $url, $content, %$headers );
162 my $info;
163 if( 'all' eq $options{ type }
164 or 'text' eq $options{ type }
165 or 'meta' eq $options{ type } ) {
166 if( $code !~ /^2..$/ ) {
167 croak "Got HTTP error code $code";
168 };
169 my $item = $res->[0];
170
171 # Should/could this be lazy?
172 my $c = delete $item->{'X-TIKA:content'};
173 # Ghetto-strip HTML we don't want:
174 if( $c =~ m!<body>(.*)</body>!s ) {
175 $c = $1;
176
177 if( $item->{"Content-Type"} and $item->{"Content-Type"} =~ m!^text/plain\b!) {
178 # Also strip the enclosing <p>..</p>
179 $c =~ s!\A\s*<p>(.*)\s*</p>\s*\z!$1!s;
180 };
181 } else {
182 warn "Couldn't find HTML body in response";
183 };
184
185 $info= CORION::Apache::Tika::DocInfo->new({
186 content => $c,
187 meta => $item,
188 });
189
190 if( ! defined $info->{meta}->{"meta:language"} ) {
191 # Yay. Two requests.
192 my $lang_meta = $self->fetch(%options, type => 'language', 'Content-Type' => $item->{'Content-Type'});
193 $info->{meta}->{"meta:language"} = $lang_meta->meta->{"info"};
194 };
195
196 } else {
197 # Must be '/language'
198 if( $code !~ /^2..$/ ) {
199 croak "Got HTTP error code $code";
200 };
201 if( ref $res ) {
202 $res = $res->[0];
203 } else {
204 $res = { info => $res };
205 };
206  
207 my $c = delete $res->{'X-TIKA:content'};
208 $info= CORION::Apache::Tika::DocInfo->new({
209 meta => $res,
210 content => undef,
211 });
212 };
213 $info
214 }
215  
216 sub DEMOLISH {
217 kill -9 => $_[0]->pid
218 if( $_[0] and $_[0]->pid );
219 }
220  
221 __PACKAGE__->meta->make_immutable;
222  
223 1;
224  
225 =head1 REPOSITORY
226  
227 The public repository of this module is
228 L<https://github.com/Corion/apache-tika>.
229  
230 =head1 SUPPORT
231  
232 The public support forum of this module is
233 L<https://perlmonks.org/>.
234  
235 =head1 BUG TRACKER
236  
237 Please report bugs in this module via the RT CPAN bug queue at
238 L<https://rt.cpan.org/Public/Dist/Display.html?Name=CORION-Apache-Tika>
239 or via mail to L<corion-apache-tika-Bugs@rt.cpan.org>.
240  
241 =head1 AUTHOR
242  
243 Max Maischein C<corion@cpan.org>
244  
245 =head1 COPYRIGHT (c)
246  
247 Copyright 2014-2016 by Max Maischein C<corion@cpan.org>.
248  
249 =head1 LICENSE
250  
251 This module is released under the same terms as Perl itself.
252  
253 =cut