]> git.evergreen-ils.org Git - working/NCIPServer.git/blob - lib/NCIP.pm
239cf1a5594f7bbfc7c5234d253c45cccf40574d
[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
9 use Object::Tiny qw{xmldoc config namespace ils};
10
11 our $VERSION = '0.01';
12 our $nsURI   = 'http://www.niso.org/2008/ncip';
13
14 =head1 NAME
15   
16     NCIP
17
18 =head1 SYNOPSIS
19
20     use NCIP;
21     my $nicp = NCIP->new($config_dir);
22
23 =head1 FUNCTIONS
24
25 =cut
26
27 sub new {
28     my $proto      = shift;
29     my $class      = ref $proto || $proto;
30     my $config_dir = shift;
31     my $self       = {};
32     my $config     = NCIP::Configuration->new($config_dir);
33     $self->{config}    = $config;
34     $self->{namespace} = $nsURI;
35
36     # load the ILS dependent module
37     my $module = 'NCIP::ILS::' . $config->('NCIP.ils.value');
38     load $module || die "Can not load ILS module $module";
39     my $ils = $module->new( name => $config->('NCIP.ils.value') );
40     $self->{'ils'} = $ils;
41     return bless $self, $class;
42
43 }
44
45 =head2 process_request()
46
47  my $response = $ncip->process_request($xml);
48
49 =cut
50
51 sub process_request {
52     my $self = shift;
53     my $xml  = shift;
54
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         return;
61
62         #bail out for now
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     try {
83         $dom = XML::LibXML->load_xml( string => $xml );
84     }
85     catch {
86         warn "Invalid xml, caught error: $_";
87     };
88     if ($dom) {
89
90         # should check validity with validate at this point
91         if ( $self->validate($dom) ) {
92             my $request_type = $self->parse_request($dom);
93
94             # do whatever we should do to initiate, then hand back request_type
95             if ($request_type) {
96                 $self->{xmldoc} = $dom;
97                 return $request_type;
98             }
99         }
100         else {
101             warn "Not valid xml";
102
103             # not valid throw error
104             return;
105         }
106     }
107     else {
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 = $dom->getElementsByTagNameNS( $nsURI, 'NCIPMessage' );
136     if ($nodes) {
137         my @childnodes = $nodes->[0]->childNodes();
138         if ( $childnodes[1] ) {
139             return $childnodes[1]->localname();
140         }
141         else {
142             return;
143         }
144     }
145     else {
146         warn "Invalid XML";
147         return;
148     }
149     return;
150 }
151
152 1;