]> git.evergreen-ils.org Git - working/NCIPServer.git/blob - lib/NCIP.pm
8cb6a9ea1548e68795c48aca00ee71143841ffe7
[working/NCIPServer.git] / lib / NCIP.pm
1 package NCIP;
2 use NCIP::Configuration;
3 use NCIP::Handler;
4 use Modern::Perl;
5 use XML::LibXML;
6 use Try::Tiny;
7 use Module::Load;
8 use Template;
9
10 use Object::Tiny qw{xmldoc config namespace ils};
11
12 our $VERSION           = '0.01';
13 our $strict_validation = 0;        # move to config file
14
15 =head1 NAME
16   
17     NCIP
18
19 =head1 SYNOPSIS
20
21     use NCIP;
22     my $nicp = NCIP->new($config_dir);
23
24 =head1 FUNCTIONS
25
26 =cut
27
28 sub new {
29     my $proto      = shift;
30     my $class      = ref $proto || $proto;
31     my $config_dir = shift;
32     my $self       = {};
33     my $config     = NCIP::Configuration->new($config_dir);
34     $self->{config}    = $config;
35     $self->{namespace} = $config->('NCIP.namespace.value');
36
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;
43
44 }
45
46 =head2 process_request()
47
48  my $response = $ncip->process_request($xml);
49
50 =cut
51
52 sub process_request {
53     my $self           = shift;
54     my $xml            = shift;
55     my ($request_type) = $self->handle_initiation($xml);
56     unless ($request_type) {
57
58       # We have invalid xml, or we can't figure out what kind of request this is
59       # Handle error here
60         warn "We can't find request type";
61         my $output = $self->_error("We can't find request type");
62         return $output;
63     }
64     my $handler = NCIP::Handler->new(
65         {
66             namespace => $self->namespace(),
67             type      => $request_type,
68             ils       => $self->ils
69         }
70     );
71     return $handler->handle( $self->xmldoc );
72 }
73
74 =head2 handle_initiation
75
76 =cut
77
78 sub handle_initiation {
79     my $self = shift;
80     my $xml  = shift;
81     my $dom;
82     eval { $dom = XML::LibXML->load_xml( string => $xml ); };
83     if ($@) {
84         warn "Invalid xml, caught error: $@";
85     }
86     if ($dom) {
87
88         # should check validity with validate at this point
89         if ( $strict_validation && !$self->validate($dom) ) {
90
91             # we want strict validation, bail out if dom doesnt validate
92             warn " Not valid xml";
93
94             # throw/log error
95             return;
96         }
97         my $request_type = $self->parse_request($dom);
98
99         # do whatever we should do to initiate, then hand back request_type
100         if ($request_type) {
101             $self->{xmldoc} = $dom;
102             return $request_type;
103         }
104     }
105     else {
106         warn "We have no DOM";
107
108         return;
109     }
110 }
111
112 sub validate {
113
114     # this should perhaps be in it's own module
115     my $self = shift;
116     my $dom  = shift;
117     try {
118         $dom->validate();
119     }
120     catch {
121         warn "Bad xml, caught error: $_";
122         return;
123     };
124
125     # we could validate against the dtd here, might be good?
126     # my $dtd = XML::LibXML::Dtd->parse_string($dtd_str);
127     # $dom->validate($dtd);
128     # perhaps we could check the ncip version and validate that too
129     return 1;
130 }
131
132 sub parse_request {
133     my $self = shift;
134     my $dom  = shift;
135     my $nodes =
136       $dom->getElementsByTagNameNS( $self->namespace(), 'NCIPMessage' );
137     if ($nodes) {
138         warn "got nodes";
139         my @childnodes = $nodes->[0]->childNodes();
140         warn $nodes;
141         if ( $childnodes[1] ) {
142             return $childnodes[1]->localname();
143         }
144         else {
145             warn "Got a node, but no child node";
146             return;
147         }
148     }
149     else {
150         warn "Invalid XML";
151         return;
152     }
153     return;
154 }
155
156 sub _error {
157     my $self = shift;
158     my $error_detail = shift;
159     my $vars;
160     $vars->{'error_detail'} = $error_detail;
161     my $template = Template->new(
162         { INCLUDE_PATH => $self->config->('NCIP.templates.value'), } );
163     my $output;
164     $template->process( 'problem.tt', $vars, \$output );
165     return $output;
166 }
167 1;