Loading ...
Sorry, an error occurred while loading the content.

Re[2]: My own dispatching sub - is it possible?

Expand Messages
  • Nikita Dedik
    Greetings! Well, if that can help you, this is how I ve got to use one sub for handling all the incoming SOAP requests without losing any needed info.
    Message 1 of 1 , Mar 8 9:26 AM
    • 0 Attachment
      Greetings!

      Well, if that can help you, this is how I've got to use one sub for
      handling all the incoming SOAP requests without losing any needed
      info. Sincerely speaking, I had to parse a lot of SOAP::Lite code to
      understand how to do it :). This script can be executed as both CGI
      and daemon application - see implementation for details. The core code
      that lets you use your own dispatcher is:
      -> on_action(sub { return; })
      -> on_dispatch(sub { $handler->request(shift); return qw(RequestsHandler handle); })
      -> dispatch_to($handler)
      And handle() sub retrieves method name and URI and - in this example -
      requires the module represented by URI. In this example if URI is
      "urn:dir1/dir2/test.pl" it gonna be the following:
      <WEB_SERVER_ROOT>/dir1/dir2/test.pl
      Thus you get all the SOAP requests in one sub and link/call any
      package/method you need basing its import scheme on SOAP method URI
      and name any way you like.

      #!perl -w

      use SOAP::Transport::HTTP;
      require URI;

      $DAEMON_PORT = 8000;

      my $handler = RequestsHandler->new(!!! PUT ANY GLOBAL PARAMETERS TO PASS TO REQUESTS HANDLER !!!);

      my $server = (!$ENV{'SCRIPT_FILENAME'} ?
      SOAP::Transport::HTTP::Daemon
      -> new(LocalAddr => 'localhost', LocalPort => $DAEMON_PORT) :
      SOAP::Transport::HTTP::CGI
      -> new);
      print "SOAP server started at ", $server->url, "\n" if (!$ENV{'SCRIPT_FILENAME'});
      $server
      -> on_action(sub { return; })
      -> on_dispatch(sub { $handler->request(shift); return qw(RequestsHandler handle); })
      -> dispatch_to($handler)
      -> handle;

      package RequestsHandler;

      use Inline;

      sub new {
      my $class = shift;
      my $self = {};
      $self->{!!! GLOBAL PARAMETER 1 !!!} = shift;
      ...
      $self->{!!! GLOBAL PARAMETER N !!!} = shift;
      $self->{_request} = undef;
      bless ($self, $class);
      return $self;
      }

      sub request {
      my $self = shift;
      return (@_ ? $self->{_request} = shift : $self->{_request});
      }

      sub handle {
      my $self = shift;
      my $method_uri = URI->new($self->{_request}->namespaceuriof || '')->path;
      my $method_name = $self->{_request}->dataof->name;
      require "$self->{'_root_dir'}/$method_uri";
      return &$method_name(@_);
      }

      --
      Nikita Dedik
    Your message has been successfully submitted and would be delivered to recipients shortly.