|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# Copyright 2012 CatalystIT |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 2 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
|
| 23 |
use Getopt::Long; |
| 24 |
|
| 25 |
my ($help, $config, $daemon); |
| 26 |
|
| 27 |
GetOptions( |
| 28 |
'config|c=s' => \$config, |
| 29 |
'daemon|d' => \$daemon, |
| 30 |
'help|?' => \$help, |
| 31 |
); |
| 32 |
|
| 33 |
if($help || !$config){ |
| 34 |
print <<EOF |
| 35 |
$0 --config=my.conf |
| 36 |
Parameters : |
| 37 |
--daemon | d - go to background; prints pid to stdout |
| 38 |
--config | c - config file |
| 39 |
--help | ? - this message |
| 40 |
|
| 41 |
Config file format: |
| 42 |
Lines of the form: |
| 43 |
name: value |
| 44 |
|
| 45 |
# comments are supported |
| 46 |
No quotes |
| 47 |
|
| 48 |
Parameter Names: |
| 49 |
host - ip address or hostname to bind to, defaults all available |
| 50 |
port - port to bind to, defaults 80 |
| 51 |
log - log file path, stderr if omitted |
| 52 |
koha - koha intranet base url, eg http://librarian.koha |
| 53 |
user - koha user, authentication |
| 54 |
password - koha user password, authentication |
| 55 |
match - marc_matchers.code: ISBN or ISSN |
| 56 |
overlay_action - import_batches.overlay_action: replace, create_new or ignore |
| 57 |
nomatch_action - import_batches.nomatch_action: create_new or ignore |
| 58 |
item_action - import_batches.item_action: always_add, |
| 59 |
add_only_for_matches, add_only_for_new or ignore |
| 60 |
import_mode - stage or direct |
| 61 |
framework - to be used if import_mode is direct |
| 62 |
|
| 63 |
All process related parameters (all but ip and port) have default values as |
| 64 |
per Koha import process. |
| 65 |
EOF |
| 66 |
; |
| 67 |
exit; |
| 68 |
} |
| 69 |
|
| 70 |
my $server = ImportProxyServer->new; |
| 71 |
$server->config_file($config); |
| 72 |
|
| 73 |
if ($daemon) { |
| 74 |
print $server->background; |
| 75 |
} else { |
| 76 |
$server->run; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
{ |
| 81 |
package ImportProxyServer; |
| 82 |
|
| 83 |
use base qw(HTTP::Server::Simple::CGI); |
| 84 |
|
| 85 |
use LWP::UserAgent; |
| 86 |
use HTTP::Status qw(:constants status_message); |
| 87 |
use XML::Simple; |
| 88 |
|
| 89 |
use constant AUTH_URI => "/cgi-bin/koha/mainpage.pl"; |
| 90 |
use constant IMPORT_SVC_URI => "/cgi-bin/koha/svc/import_bib"; |
| 91 |
|
| 92 |
sub config_file { |
| 93 |
my $self = shift; |
| 94 |
$self->{'config_file'} = shift if (@_); |
| 95 |
return $self->{'config_file'}; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
sub parse_config { |
| 100 |
my $self = shift; |
| 101 |
|
| 102 |
my $config_file = $self->config_file or die "No config file"; |
| 103 |
|
| 104 |
open CONF, $config_file or die "Cannot open config file $config: $!"; |
| 105 |
|
| 106 |
my %param; |
| 107 |
my $line = 0; |
| 108 |
while (<CONF>) { |
| 109 |
$line++; |
| 110 |
chomp; |
| 111 |
s/\s*#.*//o; # remove comments |
| 112 |
s/^\s+//o; # trim leading spaces |
| 113 |
s/\s+$//o; # trim trailing spaces |
| 114 |
next unless $_; |
| 115 |
|
| 116 |
my ($p, $v) = m/(\S+?):\s*(.*)/o; |
| 117 |
die "Invalid config line $line: $_" unless defined $v; |
| 118 |
$param{$p} = $v; |
| 119 |
} |
| 120 |
|
| 121 |
$self->{LOCAL}->{koha} = delete( $param{koha} ) |
| 122 |
or die "No koha base url in config file"; |
| 123 |
$self->{LOCAL}->{user} = delete( $param{user} ) |
| 124 |
or die "No koha user in config file"; |
| 125 |
$self->{LOCAL}->{password} = delete( $param{password} ) |
| 126 |
or die "No koha user password in config file"; |
| 127 |
|
| 128 |
$self->host( delete $param{host} ); |
| 129 |
$self->port( delete( $param{port} ) || 80 ); |
| 130 |
|
| 131 |
my $log_fh; |
| 132 |
if (my $logfile = delete $param{log}) { |
| 133 |
open $log_fh, ">>$logfile" or die "Cannot open $logfile for write: $!"; |
| 134 |
} else { |
| 135 |
$log_fh = \*STDERR; |
| 136 |
} |
| 137 |
$self->{LOCAL}->{log_fh} = $log_fh; |
| 138 |
|
| 139 |
my $prefix = ""; |
| 140 |
while ( my ($p, $v) = each %param ) { |
| 141 |
$prefix .= "$p: $v\n"; |
| 142 |
} |
| 143 |
|
| 144 |
$self->{LOCAL}->{prefix} = $prefix; |
| 145 |
} |
| 146 |
|
| 147 |
sub log { |
| 148 |
my $self = shift; |
| 149 |
my $log_fh = $self->{LOCAL}->{log_fh} |
| 150 |
or warn "No log fh", |
| 151 |
return; |
| 152 |
print $log_fh map "$_\n", @_; |
| 153 |
} |
| 154 |
|
| 155 |
sub print_banner {}; |
| 156 |
|
| 157 |
sub run { |
| 158 |
my $self = shift; |
| 159 |
|
| 160 |
$self->parse_config; |
| 161 |
|
| 162 |
my $ua = LWP::UserAgent->new; |
| 163 |
$ua->timeout(10); |
| 164 |
$ua->cookie_jar({}); |
| 165 |
$self->{LOCAL}->{ua} = $ua; |
| 166 |
|
| 167 |
$self->SUPER::run(@_); |
| 168 |
} |
| 169 |
|
| 170 |
sub response_start { |
| 171 |
my ( $self, $status ) = @_; |
| 172 |
print "HTTP/1.0 $status ". status_message($status); |
| 173 |
print "\r\n"; |
| 174 |
# print "Content-Type: text/html; charset='UTF-8'\r\n"; |
| 175 |
|
| 176 |
} |
| 177 |
sub bad_req_error { |
| 178 |
my ( $self, $cgi ) = @_; |
| 179 |
$self->response_start(HTTP_BAD_REQUEST); |
| 180 |
die $cgi->headers->as_string, $cgi->param; |
| 181 |
} |
| 182 |
sub handle_request { |
| 183 |
my ( $self, $cgi ) = @_; |
| 184 |
|
| 185 |
my $data = $cgi->param('POSTDATA') |
| 186 |
or return $self->bad_req_error($cgi); |
| 187 |
|
| 188 |
$data = $self->{LOCAL}->{prefix} . $data; |
| 189 |
|
| 190 |
my $ua = $self->{LOCAL}->{ua}; |
| 191 |
my $base_url = $self->{LOCAL}->{koha}; |
| 192 |
my $resp = $ua->post( $base_url.IMPORT_SVC_URI, 'Content-Type' => 'text/plain', Content => $data ); |
| 193 |
my $status = $resp->code; |
| 194 |
if ($status == HTTP_UNAUTHORIZED || $status == HTTP_FORBIDDEN) { |
| 195 |
my $user = $self->{LOCAL}->{user}; |
| 196 |
my $password = $self->{LOCAL}->{password}; |
| 197 |
$resp = $ua->post( $base_url.AUTH_URI, { userid => $user, password => $password } ); |
| 198 |
$resp = $ua->post( $base_url.IMPORT_SVC_URI, 'Content-Type' => 'text/plain', Content => $data ) |
| 199 |
if $resp->is_success; |
| 200 |
} |
| 201 |
$self->log("Unsuccessful request", $resp->request->as_string, $resp->as_string) |
| 202 |
unless $resp->is_success; |
| 203 |
|
| 204 |
$self->response_start($resp->code); |
| 205 |
print $resp->headers->as_string; |
| 206 |
print "\r\n"; |
| 207 |
|
| 208 |
if ($resp->is_success) { |
| 209 |
my ($koha_status, $bib, $batch_id, $error); |
| 210 |
if ( my $r = eval { XMLin($resp->content) } ) { |
| 211 |
$koha_status = $r->{status}; |
| 212 |
$batch_id = $r->{import_batch_id}; |
| 213 |
$error = $r->{error}; |
| 214 |
} |
| 215 |
else { |
| 216 |
$koha_status = "error"; |
| 217 |
$error = "Response iformat error:\n$resp->content" |
| 218 |
} |
| 219 |
|
| 220 |
if ($koha_status eq "ok") { |
| 221 |
printf "Got it. Thanks.\nImport batch id: %s\0", $batch_id; |
| 222 |
} else { |
| 223 |
printf "%s. Please contact administrator.\0", $error; |
| 224 |
} |
| 225 |
} |
| 226 |
else { |
| 227 |
print $resp->content; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
} # package |
| 232 |
=comment |
| 233 |
use HTTP::Proxy; |
| 234 |
use HTTP::Proxy::HeaderFilter::simple; |
| 235 |
use HTTP::Proxy::BodyFilter::complete; |
| 236 |
use HTTP::Proxy::BodyFilter::simple; |
| 237 |
|
| 238 |
my $header_filter = HTTP::Proxy::HeaderFilter::simple->new( |
| 239 |
sub { |
| 240 |
my ( $self, $headers, $message) = @_; |
| 241 |
$headers->header('Content-Type' => 'text/plain'); |
| 242 |
$message->url($svc_url); |
| 243 |
} |
| 244 |
); |
| 245 |
my $body_filter = HTTP::Proxy::BodyFilter::simple->new( |
| 246 |
sub { |
| 247 |
my ( $self, $dataref, $message, $protocol, $buffer) = @_; |
| 248 |
$$dataref = $prefix.$$dataref unless $buffer; |
| 249 |
} |
| 250 |
); |
| 251 |
my $proxy = HTTP::Proxy->new( |
| 252 |
host => $host, |
| 253 |
port => $port ); |
| 254 |
$proxy->push_filter( |
| 255 |
method => 'POST', |
| 256 |
request => $header_filter, |
| 257 |
request => HTTP::Proxy::BodyFilter::complete->new, |
| 258 |
request => $body_filter, |
| 259 |
); |
| 260 |
$proxy->start; |
| 261 |
=cut |
| 262 |
|
| 263 |
|