| 1 | |||
| 2 | package Xmldoom::ORB::Apache; | ||
| 3 | |||
| 4 | use Xmldoom::ORB::Transport; | ||
| 5 | use Xmldoom::Definition; | ||
| 6 | use Apache; | ||
| 7 | use CGI; | ||
| 8 | use strict; | ||
| 9 | |||
| 10 | use Data::Dumper; | ||
| 11 | |||
| 12 | my $DATABASE = undef; | ||
| 13 | my $TRANSPORT = undef; | ||
| 14 | |||
| 15 | sub handler { | ||
| 16 | my $r = shift; | ||
| 17 | |||
| 18 | # setup our database definitions if they haven't been already | ||
| 19 | if ( not defined $DATABASE ) | ||
| 20 | { | ||
| 21 | # load the database/object XML files. | ||
| 22 | $DATABASE = Xmldoom::Definition::parse_database_uri( $r->dir_config( 'XmldoomDatabaseXML' ) ); | ||
| 23 | Xmldoom::Definition::parse_object_uri( $DATABASE, $r->dir_config( 'XmldoomObjectsXML' ) ); | ||
| 24 | |||
| 25 | # load the connection factory | ||
| 26 | my $conn_factory_class = $r->dir_config( 'XmldoomConnFactory' ); | ||
| 27 | $DATABASE->set_connection_factory( $conn_factory_class->new() ); | ||
| 28 | |||
| 29 | # load the transport | ||
| 30 | my $format = $r->dir_config( 'XmldoomFormat' ) || "xml"; | ||
| 31 | $TRANSPORT = Xmldoom::ORB::Transport::get_transport($format); | ||
| 32 | if ( not defined $TRANSPORT ) | ||
| 33 | { | ||
| 34 | die "Unknown transport format passed to Xmldoom::ORB::Apache: ".$format; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | my $req_location = $r->location; | ||
| 39 | my $req_uri = $r->uri; | ||
| 40 | my $req_query = $r->args; | ||
| 41 | |||
| 42 | my $obj_and_op; | ||
| 43 | |||
| 44 | # attampt to determine the object type requested. | ||
| 45 | if ( $req_uri =~ /^$req_location/ ) | ||
| 46 | { | ||
| 47 | $obj_and_op = $req_uri; | ||
| 48 | |||
| 49 | # remove the script name | ||
| 50 | $obj_and_op =~ s/^$req_location//; | ||
| 51 | |||
| 52 | # remove everything that comes after a '?' mark | ||
| 53 | $obj_and_op =~ s/\?.*//; | ||
| 54 | |||
| 55 | # remove beginning and trailing slashes | ||
| 56 | $obj_and_op =~ s/^\///; | ||
| 57 | $obj_and_op =~ s/\/$//; | ||
| 58 | } | ||
| 59 | |||
| 60 | my $object_name; | ||
| 61 | my $operation; | ||
| 62 | |||
| 63 | if ( $obj_and_op =~ /(.*)\/(.*)/ ) | ||
| 64 | { | ||
| 65 | $object_name = $1; | ||
| 66 | $operation = $2; | ||
| 67 | } | ||
| 68 | |||
| 69 | my $definition = $DATABASE->get_object( $object_name ); | ||
| 70 | |||
| 71 | #print "uri: " . $r->uri . "\n"; | ||
| 72 | #print "location: " . $r->location . "\n"; | ||
| 73 | #print "path_info: " . $r->path_info . "\n"; | ||
| 74 | #print "object_name: " . $object_name . "\n"; | ||
| 75 | #print "operation: $operation\n"; | ||
| 76 | |||
| 77 | # read POST data from the client | ||
| 78 | my $buffer = undef; | ||
| 79 | if ( $r->method() eq 'POST' ) | ||
| 80 | { | ||
| 81 | # Will this work without 'Content-Length' ? | ||
| 82 | $r->read($buffer, $r->header_in('Content-Length')); | ||
| 83 | } | ||
| 84 | #print STDERR "POST: $buffer\n"; | ||
| 85 | |||
| 86 | my $cgi = CGI->new( $req_query ); | ||
| 87 | |||
| 88 | # send the format header | ||
| 89 | $r->send_http_header( $TRANSPORT->get_mime_type() ); | ||
| 90 | |||
| 91 | if ( $operation eq 'load' ) | ||
| 92 | { | ||
| 93 | # load the object | ||
| 94 | my $key = { }; | ||
| 95 | foreach my $pname ( $cgi->param ) | ||
| 96 | { | ||
| 97 | $key->{$pname} = $cgi->param( $pname ); | ||
| 98 | } | ||
| 99 | my $data = $definition->load( $key ); | ||
| 100 | |||
| 101 | # write it! | ||
| 102 | $TRANSPORT->write_object($data); | ||
| 103 | } | ||
| 104 | elsif ( $operation eq 'search' ) | ||
| 105 | { | ||
| 106 | my $criteria = Xmldoom::Criteria::XML::parse_string($buffer, $DATABASE); | ||
| 107 | my $rs = $definition->search_rs( $criteria ); | ||
| 108 | |||
| 109 | my $count; | ||
| 110 | if ( $cgi->param( 'includeCount' ) ) | ||
| 111 | { | ||
| 112 | $count = $definition->count( $criteria ); | ||
| 113 | } | ||
| 114 | |||
| 115 | # write it! | ||
| 116 | $TRANSPORT->write_object_list($rs, $count); | ||
| 117 | } | ||
| 118 | elsif ( $operation eq 'count' ) | ||
| 119 | { | ||
| 120 | my $criteria = Xmldoom::Criteria::XML::parse_string($buffer, $DATABASE); | ||
| 121 | my $count = $definition->count( $criteria ); | ||
| 122 | |||
| 123 | # write it! | ||
| 124 | $TRANSPORT->write_count($count); | ||
| 125 | } | ||
| 126 | }; | ||
| 127 | |||
| 128 | 1; | ||
| 129 |