2 use NCIP::Configuration;
10 use Object::Tiny qw{xmldoc config namespace ils};
12 our $VERSION = '0.01';
13 our $strict_validation = 0; # move to config file
22 my $nicp = NCIP->new($config_dir);
30 my $class = ref $proto || $proto;
31 my $config_dir = shift;
33 my $config = NCIP::Configuration->new($config_dir);
34 $self->{config} = $config;
35 $self->{namespace} = $config->('NCIP.namespace.value');
37 # load the ILS dependent module
38 my $module = 'NCIP::ILS::' . $config->('NCIP.ils.value');
39 load $module || die "Can not load ILS module $module";
40 my $ils = $module->new( name => $config->('NCIP.ils.value') );
41 $self->{'ils'} = $ils;
42 return bless $self, $class;
46 =head2 process_request()
48 my $response = $ncip->process_request($xml);
55 my ($request_type) = $self->handle_initiation($xml);
56 unless ($request_type) {
58 # We have invalid xml, or we can't figure out what kind of request this is
60 # warn "We can't find request type";
61 my $output = $self->_error("We can't find request type");
64 my $handler = NCIP::Handler->new(
66 namespace => $self->namespace(),
67 type => $request_type,
69 template_dir => $self->config->('NCIP.templates.value'),
72 return $handler->handle( $self->xmldoc );
75 =head2 handle_initiation
79 sub handle_initiation {
83 eval { $dom = XML::LibXML->load_xml( string => $xml ); };
85 warn "Invalid xml, caught error: $@";
89 # should check validity with validate at this point
90 if ( $strict_validation && !$self->validate($dom) ) {
92 # we want strict validation, bail out if dom doesnt validate
93 # warn " Not valid xml";
98 my $request_type = $self->parse_request($dom);
100 # do whatever we should do to initiate, then hand back request_type
102 $self->{xmldoc} = $dom;
103 return $request_type;
107 warn "We have no DOM";
115 # this should perhaps be in it's own module
122 warn "Bad xml, caught error: $_";
126 # we could validate against the dtd here, might be good?
127 # my $dtd = XML::LibXML::Dtd->parse_string($dtd_str);
128 # $dom->validate($dtd);
129 # perhaps we could check the ncip version and validate that too
137 $dom->getElementsByTagNameNS( $self->namespace(), 'NCIPMessage' );
139 my @childnodes = $nodes->[0]->childNodes();
140 if ( $childnodes[1] ) {
141 return $childnodes[1]->localname();
144 warn "Got a node, but no child node";
157 my $error_detail = shift;
159 $vars->{'error_detail'} = $error_detail;
160 my $template = Template->new(
161 { INCLUDE_PATH => $self->config->('NCIP.templates.value'), } );
163 $template->process( 'problem.tt', $vars, \$output );