2 use NCIP::Configuration;
7 use XML::LibXML::Simple qw/XMLin/;
13 use Object::Tiny qw{config namespace ils};
15 our $VERSION = '0.01';
16 our $strict_validation = 0; # move to config file
25 my $nicp = NCIP->new($config_dir);
33 my $class = ref $proto || $proto;
34 my $config_dir = shift;
36 my $config = NCIP::Configuration->new($config_dir);
37 $self->{config} = $config;
38 $self->{namespace} = $config->('NCIP.namespace.value');
39 Log::Log4perl->init($config_dir . "/log4perl.conf");
40 # load the ILS dependent module
41 my $module = $config->('NCIP.ils.value');
42 load $module || die "Can not load ILS module $module";
43 my $ils = $module->new( name => $config->('NCIP.ils.value') );
44 $self->{'ils'} = $ils;
45 return bless $self, $class;
49 =head2 process_request()
51 my $output = $ncip->process_request($xml);
59 # Declare our response object:
62 # Make an object out of the XML request message:
63 my $request = $self->handle_initiation($xml);
65 # Get the request type from the message:
66 my $type = $self->{ils}->parse_request_type($request);
68 my $message = lc($type);
69 if ($self->{ils}->can($message)) {
70 $response = $self->{ils}->$message($request);
72 $response = $self->{ils}->unsupportedservice($request);
77 # The ILS is responsible for handling internal errors, so we
78 # assume that not having a response object at this point means we
79 # got an invalid message sent to us, or it got garbled in
82 my $problem = NCIP::Problem->new();
83 $problem->ProblemType("Invalid Message Syntax Error");
84 $problem->ProblemDetail("Unable to parse the NCIP message.");
85 $problem->ProblemElement("NULL");
86 $problem->ProblemValue("Unable to parse the NCIP message.");
87 # Make a response and add our problem.
88 $response = NCIP::Response->new();
89 $response->problem($problem);
92 return $self->render_output($response);
95 =head2 handle_initiation
99 sub handle_initiation {
104 my $log = Log::Log4perl->get_logger("NCIP");
106 eval { $dom = XML::LibXML->load_xml( string => $xml ); };
108 $log->info("Invalid xml we can not parse it ");
112 # should check validity with validate at this point
113 if ( $strict_validation && !$self->validate($dom) ) {
115 # we want strict validation, bail out if dom doesnt validate
116 # warn " Not valid xml";
121 return XMLin( $dom, NsStrip => 1, NormaliseSpace => 2 );
124 $log->info("We have no DOM");
132 # this should perhaps be in it's own module
139 warn "Bad xml, caught error: $_";
143 # we could validate against the schema here, might be good?
144 # my $schema = XML::LibXML::Schema->new(string => $schema_str);
145 # eval { $schema->validate($dom); }
146 # perhaps we could check the ncip version and validate that too
152 my $output = $self->render_output($response);
154 Accepts a NCIP::Response object and renders the response.tt template
155 based on its input. The template output is returned.
161 my $response = shift;
163 my $template = Template->new(
165 INCLUDE_PATH => $self->config->('NCIP.templates.value'),
171 $template->process( 'response.tt', $response, \$output );