From 870fc7d377383fd4e27be9c648ba2613d2f25a75 Mon Sep 17 00:00:00 2001 From: Mike Rylander Date: Wed, 28 Oct 2020 19:39:28 -0400 Subject: [PATCH] Allow configurable parameter names and HTTP method, and JSONPath to trim the result Signed-off-by: Mike Rylander --- bin/sip2-mediator | 58 +++++++++++++++++++++++++++++-------- lib/SIP2Mediator/Message.pm | 4 ++- lib/SIP2Mediator/Server.pm | 22 ++++++++++---- 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/bin/sip2-mediator b/bin/sip2-mediator index 037d8a6..114879a 100755 --- a/bin/sip2-mediator +++ b/bin/sip2-mediator @@ -25,6 +25,10 @@ my $http_host = '127.0.0.1'; my $http_port = 80; my $http_proto = 'http'; my $http_path = '/sip2-mediator'; +my $http_method = 'POST'; +my $sess_param = 'session'; +my $mess_param = 'message'; +my $response_jpath = ''; my $max_clients = 120; my $syslog_facility = 'LOCAL0'; my $syslog_level = 'LOG_INFO'; @@ -62,6 +66,29 @@ Options: --http-path <$http_path> URL path for HTTP API server + To use the Evergreen gateway directly, use + "/osrf-gateway-v1?service=open-ils.sip2&method=open-ils.sip2.request". + To use the Evergreen OpenAPI server, use + "/openapi3/v0/ + + --http-method <$http_method> + HTTP method to use with the server + To use the Evergreen gateway directly, use + "GET". + + --session-param <$sess_param> + CGI parameter name to pass the session key + To use the Evergreen gateway directly, use + "param". + + --message-param <$mess_param> + CGI parameter name to pass the SIP2 message + To use the Evergreen gateway directly, use + "param". + + --response-json-path <$response_jpath> + JSON Path expression used to extract the result message + from the HTTP response. For Evergreen use "\$.payload[0]". --max-clients <$max_clients> Maximum number of SIP client connections allowed. @@ -83,18 +110,22 @@ USAGE } GetOptions( - 'sip-address=s' => \$sip_address, - 'sip-port=s' => \$sip_port, - 'http-host=s' => \$http_host, - 'http-port=s' => \$http_port, - 'http-proto=s' => \$http_proto, - 'http-path=s' => \$http_path, - 'max-clients=s' => \$max_clients, - 'syslog-facility=s' => \$syslog_facility, - 'syslog-level=s' => \$syslog_level, - 'daemonize' => \$daemonize, - 'ascii' => \$ascii, - 'help' => sub { usage(0); } + 'sip-address=s' => \$sip_address, + 'sip-port=s' => \$sip_port, + 'http-host=s' => \$http_host, + 'http-port=s' => \$http_port, + 'http-proto=s' => \$http_proto, + 'http-path=s' => \$http_path, + 'http-method=s' => \$http_method, + 'session-param=s' => \$sess_param, + 'message-param=s' => \$mess_param, + 'response-json-path=s' => \$response_jpath, + 'max-clients=s' => \$max_clients, + 'syslog-facility=s' => \$syslog_facility, + 'syslog-level=s' => \$syslog_level, + 'daemonize' => \$daemonize, + 'ascii' => \$ascii, + 'help' => sub { usage(0); } ) or usage(1); my $mediator = SIP2Mediator::Server->new({ @@ -104,6 +135,9 @@ my $mediator = SIP2Mediator::Server->new({ http_port => $http_port, http_proto => $http_proto, http_path => $http_path, + session_param => $sess_param, + message_param => $sess_param, + response_jpath => $response_jpath, max_clients => $max_clients, syslog_facility => $syslog_facility, syslog_level => $syslog_level, diff --git a/lib/SIP2Mediator/Message.pm b/lib/SIP2Mediator/Message.pm index 17a2e64..bb379c7 100644 --- a/lib/SIP2Mediator/Message.pm +++ b/lib/SIP2Mediator/Message.pm @@ -17,6 +17,7 @@ use strict; use warnings; use Locale::gettext; use JSON::XS; use Sys::Syslog qw(syslog); +use JSON::Path qw/jpath1/; use SIP2Mediator::Spec; use SIP2Mediator::Field; use SIP2Mediator::FixedField; @@ -157,11 +158,12 @@ sub to_json { } sub from_json { - my ($class, $msg_json) = @_; + my ($class, $msg_json, $jpath) = @_; return undef unless $msg_json; my $hash = $json->decode($msg_json); + $hash = jpath1($hash, $jpath) if $jpath; return $class->from_hash($hash); } diff --git a/lib/SIP2Mediator/Server.pm b/lib/SIP2Mediator/Server.pm index 558a57d..b0b91e2 100644 --- a/lib/SIP2Mediator/Server.pm +++ b/lib/SIP2Mediator/Server.pm @@ -253,13 +253,23 @@ sub relay_sip_request { syslog(LOG_WARNING => "[$sclient] SIP socket disapeared ".$self->seskey); return 0; } + my $sess_param = $self->config->{session_param} || 'session'; + my $mess_param = $self->config->{message_param} || 'message'; + my $http_method = $self->config->{http_method} || 'POST'; + + my $post = sprintf('%s=%s&%s=%s', + $sess_param, $self->seskey, $mess_param, url_encode_utf8($msg->to_json)); + + my $path = $self->config->{http_path}; + if ($http_method eq 'GET') { + $path .= ($path =~ /\?/) ? '&' : '?'; + $path .= $post; + $post = undef; + } - my $post = sprintf('session=%s&message=%s', - $self->seskey, url_encode_utf8($msg->to_json)); - - syslog(LOG_DEBUG => "POST: $post"); + syslog(LOG_DEBUG => "$http_method: $post"); - $self->http_socket->write_request(POST => $self->config->{http_path}, $post); + $self->http_socket->write_request($http_method => $path, $post); return 1; } @@ -310,7 +320,7 @@ sub read_http_socket { syslog(LOG_DEBUG => "[$sclient] HTTP response: $content"); - my $msg = SIP2Mediator::Message->from_json($content); + my $msg = SIP2Mediator::Message->from_json($content, $self->config->{response_jpath}); if (!$msg) { syslog('LOG_ERR',