2 use NCIP::Configuration;
11 use Object::Tiny qw{xmldoc config namespace ils};
13 our $VERSION = '0.01';
14 our $strict_validation = 0; # move to config file
23 my $nicp = NCIP->new($config_dir);
31 my $class = ref $proto || $proto;
32 my $config_dir = shift;
34 my $config = NCIP::Configuration->new($config_dir);
35 $self->{config} = $config;
36 $self->{namespace} = $config->('NCIP.namespace.value');
37 Log::Log4perl->init($config_dir . "/log4perl.conf");
38 # load the ILS dependent module
39 my $module = 'NCIP::ILS::' . $config->('NCIP.ils.value');
40 load $module || die "Can not load ILS module $module";
41 my $ils = $module->new( name => $config->('NCIP.ils.value') );
42 $self->{'ils'} = $ils;
43 return bless $self, $class;
47 =head2 process_request()
49 my $response = $ncip->process_request($xml);
56 my ($request_type) = $self->handle_initiation($xml);
57 unless ($request_type) {
59 # We have invalid xml, or we can't figure out what kind of request this is
61 # warn "We can't find request type";
62 my $output = $self->_error("We can't find request type");
65 my $handler = NCIP::Handler->new(
67 namespace => $self->namespace(),
68 type => $request_type,
70 template_dir => $self->config->('NCIP.templates.value'),
73 return $handler->handle( $self->xmldoc );
76 =head2 handle_initiation
80 sub handle_initiation {
84 my $log = Log::Log4perl->get_logger("NCIP");
85 eval { $dom = XML::LibXML->load_xml( string => $xml ); };
87 $log->info("Invalid xml we can not parse it ");
91 # should check validity with validate at this point
92 if ( $strict_validation && !$self->validate($dom) ) {
94 # we want strict validation, bail out if dom doesnt validate
95 # warn " Not valid xml";
100 my $request_type = $self->parse_request($dom);
102 # do whatever we should do to initiate, then hand back request_type
104 $self->{xmldoc} = $dom;
105 return $request_type;
109 $log->info("We have no DOM");
117 # this should perhaps be in it's own module
124 warn "Bad xml, caught error: $_";
128 # we could validate against the schema here, might be good?
129 # my $schema = XML::LibXML::Schema->new(string => $schema_str);
130 # eval { $schema->validate($dom); }
131 # perhaps we could check the ncip version and validate that too
139 $dom->getElementsByTagNameNS( $self->namespace(), 'NCIPMessage' );
141 my @childnodes = $nodes->[0]->childNodes();
142 if ( $childnodes[1] ) {
143 return $childnodes[1]->localname();
146 warn "Got a node, but no child node";
159 my $error_detail = shift;
161 $vars->{'error_detail'} = $error_detail;
162 $vars->{'messagetype'} = 'ItemRequestedResponse'; # No idea what this type should be
163 my $template = Template->new(
164 { INCLUDE_PATH => $self->config->('NCIP.templates.value'), } );
166 $template->process( 'problem.tt', $vars, \$output );