| 1 | package UR::Service::JsonRpcServer; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use UR; | ||
| 7 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 8 | |||
| 9 | UR::Object::Type->define( | ||
| 10 | class_name => __PACKAGE__, | ||
| 11 | is => 'UR::Object', | ||
| 12 | properties => [ | ||
| 13 | host => { type => 'String', is_transient => 1, default_value => '0.0.0.0', doc => 'The local address to listen on'}, | ||
| 14 | port => { type => 'String', is_transient => 1, default_value => 8080, doc => 'The local port to listen on'}, | ||
| 15 | server => { type => 'Net::HTTPServer', is_transient => 1, doc => 'The Net::HTTPServer instance for this Server instance' }, | ||
| 16 | api_root => { type => 'String', is_transient => 1, default_value => 'URapi' }, | ||
| 17 | ], | ||
| 18 | id_by => ['host','port'], | ||
| 19 | doc => 'An object serving as a web server to respond to JSON-RPC requests; wraps Net::HTTPServer', | ||
| 20 | ); | ||
| 21 | |||
| 22 | =pod | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | UR::Service::JsonRpcServer - A self-contained JSON-RPC server for UR namespaces | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use lib '/path/to/your/moduletree'; | ||
| 31 | use YourNamespace; | ||
| 32 | |||
| 33 | my $rpc = UR::Service::JsonRpcServer->create(host => 'localhost', | ||
| 34 | port => '8080', | ||
| 35 | api_root => 'URapi', | ||
| 36 | docroot => '/html/pages/path', | ||
| 37 | ); | ||
| 38 | $rpc->process(); | ||
| 39 | |||
| 40 | =head1 Description | ||
| 41 | |||
| 42 | This is a class containing an implementation of a JSON-RPC server to respond to | ||
| 43 | requests involving UR-based namespaces and their objects. It uses Net::HTTPServer | ||
| 44 | as the web server back-end library. | ||
| 45 | |||
| 46 | Incoming requests are divided into two major categories: | ||
| 47 | |||
| 48 | =over 4 | ||
| 49 | |||
| 50 | =item http://server:port/C<api-root>/class/Namespace/Class | ||
| 51 | |||
| 52 | This is the URL for a call to a class metnod on C<Namespace::Class> | ||
| 53 | |||
| 54 | =item http://server:port/C<api-root>/obj/Namespace/Class/id | ||
| 55 | |||
| 56 | This is the URL for a method call on an object of class Namespace::Class with the given id | ||
| 57 | |||
| 58 | =back | ||
| 59 | |||
| 60 | =head1 Constructor | ||
| 61 | |||
| 62 | The constructor takes the following named parameters: | ||
| 63 | |||
| 64 | =over 4 | ||
| 65 | |||
| 66 | =item host | ||
| 67 | |||
| 68 | The hostname to listen on. This can be an ip address, host name, or undef. | ||
| 69 | The default value is '0.0.0.0'. This argument is passed along verbatim to | ||
| 70 | the Net::HTTPServer constructor. | ||
| 71 | |||
| 72 | =item port | ||
| 73 | |||
| 74 | The TCP port to listen on. The default value is 8080. This argument is passed | ||
| 75 | along verbatim to the Net::HTTPServer constructor. | ||
| 76 | |||
| 77 | =item api_root | ||
| 78 | |||
| 79 | The root path that the http server will listen for requests on. The constructor registers | ||
| 80 | two paths with the Net::HTTPServer with RegisterRegex() for /C<api_root>/class/* and | ||
| 81 | /C<api_root>/obj/* to respond to class and instance metod calls. | ||
| 82 | |||
| 83 | =back | ||
| 84 | |||
| 85 | All other arguments are passed along to the Net::HTTPServer constructor. | ||
| 86 | |||
| 87 | =head1 Methods | ||
| 88 | |||
| 89 | =over 4 | ||
| 90 | |||
| 91 | =item $rpc->process() | ||
| 92 | |||
| 93 | A wrapper to the Net::HTTPServer Process() method. With no arguments, this call will | ||
| 94 | block forever from the perspective of the caller, and process all http requests coming in. | ||
| 95 | You can optionally pass in a timeout value in seconds, and it will respond to requests | ||
| 96 | for the given number of seconds before returning. | ||
| 97 | |||
| 98 | =back | ||
| 99 | |||
| 100 | =head1 Client Side | ||
| 101 | |||
| 102 | There are (or will be) client-side code in both Perl and Javascript. The Perl code is (will be) | ||
| 103 | implemented as a UR::Context layer that will return light-weight object instances containing only | ||
| 104 | class info and IDs. All method calls will be serialized and sent over the wire for the server | ||
| 105 | process to execute them. | ||
| 106 | |||
| 107 | The Javascript interface is defined in the file urinterface.js. An example: | ||
| 108 | |||
| 109 | var UR = new URInterface('http://localhost:8080/URApi/'); // Connect to the server | ||
| 110 | var FooThingy = UR.get_class('Foo::Thingy'); // Get the class object for Foo::Thingy | ||
| 111 | var thingy = FooThingy.get(1234); // Retrieve an instance with ID 1234 | ||
| 112 | var result = thingy.call('method_name', 1, 2, 3); // Call $thingy->method_name(1,2,3) on the server | ||
| 113 | |||
| 114 | |||
| 115 | =head1 SEE ALSO | ||
| 116 | |||
| 117 | Ney::HTTPServer, urinterface.js | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | use Net::HTTPServer; | ||
| 122 | use JSON; | ||
| 123 | use Class::Inspector; | ||
| 124 | |||
| 125 | sub create { | ||
| 126 | my($class,%args) = @_; | ||
| 127 | |||
| 128 | my $api_root = delete $args{'api_root'}; | ||
| 129 | |||
| 130 | my $server = Net::HTTPServer->new(%args); | ||
| 131 | return unless $server; | ||
| 132 | |||
| 133 | my %create_args = ( host => $args{'host'}, port => $args{'port'} ); | ||
| 134 | $create_args{'api_root'} = $api_root if defined $api_root; | ||
| 135 | my $self = $class->SUPER::create(%create_args); | ||
| 136 | return unless $self; | ||
| 137 | |||
| 138 | $self->server($server); | ||
| 139 | $server->RegisterRegex("^/$api_root/class/*", sub { $self->_api_entry_classes(@_) } ) if $api_root; | ||
| 140 | $server->RegisterRegex("^/$api_root/obj/*", sub { $self->_api_entry_obj(@_) } ) if $api_root; | ||
| 141 | |||
| 142 | my $port = $server->Start(); | ||
| 143 | if ($args{'port'} eq 'scan') { | ||
| 144 | $self->port($port); | ||
| 145 | } | ||
| 146 | |||
| 147 | return $self; | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | sub process { | ||
| 152 | my $self = shift; | ||
| 153 | |||
| 154 | #$self->server->Process(@_); | ||
| 155 | my $server = $self->server; | ||
| 156 | $server->Process(@_); | ||
| 157 | |||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | sub _api_entry_classes { | ||
| 162 | my($self,$request) = @_; | ||
| 163 | |||
| 164 | my $response = $request->Response(); | ||
| 165 | |||
| 166 | #$DB::single = 1; | ||
| 167 | my $data = $self->_get_post_data_from_request($request); | ||
| 168 | #my $struct = decode_json($data); | ||
| 169 | my $struct = jsonToObj($data); | ||
| 170 | |||
| 171 | my $class = $self->_parse_class_from_request($request); | ||
| 172 | unless ($class) { | ||
| 173 | $response->Code(404); | ||
| 174 | $response->Print("Couldn't parse URL " . $request->URL); | ||
| 175 | return $response; | ||
| 176 | } | ||
| 177 | |||
| 178 | my $method = $struct->{'method'}; | ||
| 179 | my $params = $struct->{'params'}; | ||
| 180 | |||
| 181 | my @retval; | ||
| 182 | |||
| 183 | if ($method eq '_get_class_info') { # called when the other end gets a class object | ||
| 184 | eval { | ||
| 185 | my $class_object = $class->__meta__; | ||
| 186 | my %id_names = map { $_ => 1 } $class_object->all_id_property_names(); | ||
| 187 | my @id_names = keys(%id_names); | ||
| 188 | |||
| 189 | my %property_names = map { $_ => 1 } | ||
| 190 | grep { ! exists $id_names{$_} } | ||
| 191 | $class_object->all_property_names(); | ||
| 192 | my @property_names = keys(%property_names); | ||
| 193 | |||
| 194 | my $possible_method_names = Class::Inspector->methods($class, 'public'); | ||
| 195 | my @method_names = grep { ! exists $id_names{$_} and ! exists $property_names{$_} } | ||
| 196 | @$possible_method_names; | ||
| 197 | |||
| 198 | push @retval, { id_properties => \@id_names, | ||
| 199 | properties => \@property_names, | ||
| 200 | methods => \@method_names }; | ||
| 201 | }; | ||
| 202 | } else { | ||
| 203 | eval { | ||
| 204 | @retval = $class->$method(@$params); | ||
| 205 | }; | ||
| 206 | } | ||
| 207 | |||
| 208 | my $return_struct = { id => $struct->{'id'}, version => $struct->{'version'}, result => \@retval}; | ||
| 209 | if ($@) { | ||
| 210 | $return_struct->{'result'} = undef; | ||
| 211 | $return_struct->{'error'} = $@; | ||
| 212 | } else { | ||
| 213 | foreach my $item ( @retval ) { | ||
| 214 | my $reftype = ref $item; | ||
| 215 | if ($reftype && $reftype ne 'ARRAY' && $reftype ne 'HASH') { # If it's an object of some sort | ||
| 216 | my %copy = %$item; | ||
| 217 | $copy{'object_type'} = $class; | ||
| 218 | $item = \%copy; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | $return_struct->{'result'} = \@retval; | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | #my $encoded_result = to_json($return_struct, {convert_blessed => 1}); | ||
| 226 | my $encoded_result = objToJson($return_struct); | ||
| 227 | $response->Print($encoded_result); | ||
| 228 | |||
| 229 | return $response; | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | sub _api_entry_obj { | ||
| 234 | my($self,$request) = @_; | ||
| 235 | |||
| 236 | my $response = $request->Response(); | ||
| 237 | |||
| 238 | #$DB::single = 1; | ||
| 239 | my $data = $self->_get_post_data_from_request($request); | ||
| 240 | #my $struct = decode_json($data); | ||
| 241 | my $struct = jsonToObj($data); | ||
| 242 | |||
| 243 | my($class,$id) = $self->_parse_class_and_id_from_request($request); | ||
| 244 | unless ($class) { | ||
| 245 | $response->Code(404); | ||
| 246 | $response->Print("Couldn't parse URL " . $request->URL); | ||
| 247 | return $response; | ||
| 248 | } | ||
| 249 | |||
| 250 | my $method = $struct->{'method'}; | ||
| 251 | my $params = $struct->{'params'}; | ||
| 252 | |||
| 253 | my @retval; | ||
| 254 | eval { | ||
| 255 | my $obj = $class->get($id); | ||
| 256 | @retval = $obj->$method(@$params); | ||
| 257 | }; | ||
| 258 | |||
| 259 | my $return_struct = { id => $struct->{'id'}, version => $struct->{'version'}}; | ||
| 260 | if ($@) { | ||
| 261 | $return_struct->{'result'} = undef; | ||
| 262 | $return_struct->{'error'} = $@; | ||
| 263 | } else { | ||
| 264 | foreach my $item ( @retval ) { | ||
| 265 | my $reftype = ref $item; | ||
| 266 | if ($reftype && $reftype ne 'ARRAY' && $reftype ne 'HASH') { # If it's an object of some sort | ||
| 267 | my %copy = %$item; | ||
| 268 | $copy{'object_type'} = $class; | ||
| 269 | $item = \%copy; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | $return_struct->{'result'} = \@retval; | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | #my $encoded_result = to_json($return_struct, {convert_blessed => 1}); | ||
| 277 | my $encoded_result = objToJson($return_struct); | ||
| 278 | $response->Print($encoded_result); | ||
| 279 | |||
| 280 | return $response; | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | |||
| 285 | |||
| 286 | ## This one uses the last part of the URL as the ID - won't work with a generic get() | ||
| 287 | #sub old_api_entry_point { | ||
| 288 | # my($self,$request) = @_; | ||
| 289 | # | ||
| 290 | # my $response = $request->Response(); | ||
| 291 | # | ||
| 292 | ##$DB::single = 1; | ||
| 293 | # my $data = $self->_get_post_data_from_request($request); | ||
| 294 | # my $struct = decode_json($data); | ||
| 295 | # | ||
| 296 | # my($class,$id) = $self->_parse_class_and_id_from_request($request); | ||
| 297 | # unless ($class) { | ||
| 298 | # $response->Code(404); | ||
| 299 | # $response->Print("Couldn't parse URL " . $request->URL); | ||
| 300 | # return $response; | ||
| 301 | # } | ||
| 302 | # | ||
| 303 | # my $method = $struct->{'method'}; | ||
| 304 | # my $params = $struct->{'params'}; | ||
| 305 | # my @retval; | ||
| 306 | # eval { | ||
| 307 | # my $obj = $class->get($id); | ||
| 308 | # if ($method eq 'get') { | ||
| 309 | # my %copy = %$obj; | ||
| 310 | # $retval[0] = \%copy; | ||
| 311 | # } else { | ||
| 312 | # @retval = $obj->$method(@$params); | ||
| 313 | # } | ||
| 314 | # }; | ||
| 315 | # | ||
| 316 | # my $return_struct = { id => $struct->{'id'}, version => $struct->{'version'}}; | ||
| 317 | # if ($@) { | ||
| 318 | # $return_struct->{'result'} = undef; | ||
| 319 | # $return_struct->{'error'} = $@; | ||
| 320 | # } else { | ||
| 321 | # $return_struct->{'result'} = \@retval; | ||
| 322 | # } | ||
| 323 | # | ||
| 324 | # | ||
| 325 | # my $encoded_result = to_json($return_struct, {convert_blessed => 1}); | ||
| 326 | # $response->Print($encoded_result); | ||
| 327 | # | ||
| 328 | # return $response; | ||
| 329 | #} | ||
| 330 | |||
| 331 | |||
| 332 | # URLs are expected to look something like this: | ||
| 333 | # http://server/URapi/Namespace/Class/Name/ID | ||
| 334 | # and would translate to the class Namespace::Class::Name with the ID property ID | ||
| 335 | sub _parse_class_and_id_from_request { | ||
| 336 | my($self,$request) = @_; | ||
| 337 | |||
| 338 | my $api_root = $self->api_root; | ||
| 339 | my $url = $request->URL(); | ||
| 340 | |||
| 341 | my @api_root = split(/\//,$api_root); | ||
| 342 | my @url_parts = split(/\//, $url); | ||
| 343 | shift @url_parts until ($url_parts[0]); | ||
| 344 | |||
| 345 | { no warnings 'uninitialized'; | ||
| 346 | while($api_root[0] eq $url_parts[0]) { | ||
| 347 | shift @api_root; | ||
| 348 | shift @url_parts; | ||
| 349 | } | ||
| 350 | } | ||
| 351 | shift @url_parts if ($url_parts[0] eq 'class' || $url_parts[0] eq 'obj'); | ||
| 352 | |||
| 353 | my $id = pop @url_parts; | ||
| 354 | my $class = join('::', @url_parts); | ||
| 355 | |||
| 356 | return($class,$id); | ||
| 357 | } | ||
| 358 | |||
| 359 | # This works for URLs that don't have an ID at the end | ||
| 360 | sub _parse_class_from_request { | ||
| 361 | my($self,$request) = @_; | ||
| 362 | |||
| 363 | my $api_root = $self->api_root; | ||
| 364 | my $url = $request->URL(); | ||
| 365 | |||
| 366 | my @api_root = split(/\//,$api_root); | ||
| 367 | my @url_parts = split(/\//, $url); | ||
| 368 | shift @url_parts until ($url_parts[0]); | ||
| 369 | |||
| 370 | { no warnings 'uninitialized'; | ||
| 371 | while($api_root[0] eq $url_parts[0]) { | ||
| 372 | shift @api_root; | ||
| 373 | shift @url_parts; | ||
| 374 | } | ||
| 375 | } | ||
| 376 | |||
| 377 | shift @url_parts if ($url_parts[0] eq 'class' || $url_parts[0] eq 'obj'); | ||
| 378 | |||
| 379 | my $class = join('::', @url_parts); | ||
| 380 | return $class; | ||
| 381 | } | ||
| 382 | |||
| 383 | |||
| 384 | sub _get_post_data_from_request { | ||
| 385 | my($self,$request) = @_; | ||
| 386 | |||
| 387 | my $message = $request->Request; | ||
| 388 | my($data) = ($message =~ m/\r\n\r\n(.*)/m); | ||
| 389 | |||
| 390 | return $data; | ||
| 391 | } | ||
| 392 | |||
| 393 | 1; | ||
| 394 | |||
| 395 |