]> git.evergreen-ils.org Git - working/NCIPServer.git/blob - lib/NCIP.pm
5031f6f5f0eafbcb76ad583f2626027958a13e27
[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
8 use Object::Tiny;
9
10 our $VERSION = '0.01';
11 our $nsURI   = 'http://www.niso.org/2008/ncip';
12
13 =head1 NAME
14   
15     NCIP
16
17 =head1 SYNOPSIS
18
19     use NCIP;
20     my $nicp = NCIP->new($config_dir);
21
22 =head1 FUNCTIONS
23
24 =cut
25
26 sub new {
27     my $proto      = shift;
28     my $class      = ref $proto || $proto;
29     my $config_dir = shift;
30     my $self       = {};
31     my $config     = NCIP::Configuration->new($config_dir);
32     $self->{config} = $config;
33     return bless $self, $class;
34
35 }
36
37 =head2 process_request()
38
39  my $response = $ncip->process_request($xml);
40
41 =cut
42
43 sub process_request {
44     my $self = shift;
45     my $xml  = shift;
46
47     my $request_type = $self->handle_initiation($xml);
48     unless ($request_type) {
49
50       # We have invalid xml, or we can't figure out what kind of request this is
51       # Handle error here
52         return;
53
54         #bail out for now
55     }
56     my $handler = NCIP::Handler->new($request_type);
57     return $handler->handle($xml);
58 }
59
60 =head2 handle_initiation
61
62 =cut
63
64 sub handle_initiation {
65     my $self = shift;
66     my $xml  = shift;
67     my $dom;
68     try {
69         $dom = XML::LibXML->load_xml( string => $xml );
70     }
71     catch {
72         warn "Invalid xml, caught error: $_";
73     };
74     if ($dom) {
75
76         # should check validity with validate at this point
77         if ( $self->validate($dom) ) {
78             my $request_type = $self->parse_request($dom);
79             # do whatever we should do to initiate, then hand back request_type
80             if ($request_type) {
81                 return $request_type;
82             }
83         }
84         else {
85             warn "Not valid xml";
86             # not valid throw error
87             return;
88         }
89     }
90     else {
91         return;
92     }
93 }
94
95 sub validate {
96
97     # this should perhaps be in it's own module
98     my $self = shift;
99     my $dom  = shift;
100     try {
101         $dom->validate();
102     }
103     catch {
104         warn "Bad xml, caught error: $_";
105         return;
106     }
107
108     # we could validate against the dtd here, might be good?
109     # my $dtd = XML::LibXML::Dtd->parse_string($dtd_str);
110     # $dom->validate($dtd);
111     # perhaps we could check the ncip version and validate that too
112     return 1;
113 }
114
115 sub parse_request {
116     my $self  = shift;
117     my $dom   = shift;
118     my $nodes = $dom->getElementsByTagNameNS( $nsURI, 'NCIPMessage' );
119     if ($nodes) {
120         my @childnodes = $nodes->[0]->childNodes();
121         if ( $childnodes[1] ) {
122             return $childnodes[1]->localname();
123         }
124         else {
125             return;
126         }
127     }
128     else {
129         warn "Invalid XML";
130         return;
131     }
132     return;
133 }
134
135 1;