From a3382a92f620808c7adb9bb2c1490202de208bfc Mon Sep 17 00:00:00 2001 From: Irina Peshinskaya Date: Wed, 11 Apr 2018 12:32:47 +0200 Subject: [PATCH] TT#35538 Faxes allow application/json and multipart/form-data Change-Id: Ic08995e096581c1b0f3dc7d6abce1fe15720cde7 --- lib/NGCP/Panel/Controller/API/Faxes.pm | 153 ++++++--------------- lib/NGCP/Panel/Controller/API/FaxesItem.pm | 75 +--------- lib/NGCP/Panel/Role/API/Faxes.pm | 35 +---- 3 files changed, 55 insertions(+), 208 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/Faxes.pm b/lib/NGCP/Panel/Controller/API/Faxes.pm index d06b25aac2..b94e16a8a4 100644 --- a/lib/NGCP/Panel/Controller/API/Faxes.pm +++ b/lib/NGCP/Panel/Controller/API/Faxes.pm @@ -3,25 +3,24 @@ use NGCP::Panel::Utils::Generic qw(:all); use Sipwise::Base; +use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::Faxes/; -use boolean qw(true); -use Data::HAL qw(); -use Data::HAL::Link qw(); use HTTP::Headers qw(); use HTTP::Status qw(:constants); use NGCP::Panel::Utils::DateTime; use NGCP::Panel::Utils::API::Subscribers; use NGCP::Panel::Utils::Fax; -use Encode qw( encode_utf8 ); - -use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::Faxes/; __PACKAGE__->set_config({ allowed_roles => [qw/admin reseller subscriberadmin subscriber/], - GET => { - ContentType => ['multipart/form-data'], - } + dont_validate_hal => 1, + no_item_created => 1, + backward_allow_empty_upload => 1, + POST => { + 'ContentType' => ['multipart/form-data','application/json'], + 'Uploads' => [qw/faxfile/], + }, }); sub allowed_methods{ @@ -46,7 +45,6 @@ sub query_params { second => sub { }, }, }, - { param => 'time_from', description => 'Filter for faxes performed after or at the given time stamp.', @@ -59,7 +57,6 @@ sub query_params { second => sub { }, }, }, - { param => 'time_to', description => 'Filter for faxes performed before or at the given time stamp.', @@ -72,119 +69,53 @@ sub query_params { second => sub { }, }, }, - { param => 'sid', description => 'Filter for a fax with the specific session id', - query => { - first => sub { - my $q = shift; - { 'me.sid' => $q }; - }, - second => sub { }, - }, + query_type => 'string_eq', }, - ]; } -sub GET :Allow { - my ($self, $c) = @_; - my $page = $c->request->params->{page} // 1; - my $rows = $c->request->params->{rows} // 10; - { - my $items = $self->item_rs($c); - (my $total_count, $items) = $self->paginate_order_collection($c, $items); - my (@embedded, @links); - for my $item ($items->all) { - push @embedded, $self->hal_from_item($c, $item); - push @links, Data::HAL::Link->new( - relation => 'ngcp:'.$self->resource_name, - href => sprintf('/%s%d', $c->request->path, $item->id), - ); - } - push @links, - Data::HAL::Link->new( - relation => 'curies', - href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}', - name => 'ngcp', - templated => true, - ), - Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), - $self->collection_nav_links($c, $page, $rows, $total_count, $c->request->path, $c->request->query_params); +sub create_item { + my ($self, $c, $resource, $form, $process_extras) = @_; - my $hal = Data::HAL->new( - embedded => [@embedded], - links => [@links], - ); - $hal->resource({ - total_count => $total_count, - }); - my $response = HTTP::Response->new(HTTP_OK, undef, - HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json); - $c->response->headers($response->headers); - $c->response->body($response->content); + if (!$c->config->{features}->{faxserver}) { + $c->log->error("faxserver feature is not active."); + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Faxserver feature is not active."); return; } - return; -} - -sub POST :Allow { - my ($self, $c) = @_; - { - if (!$c->config->{features}->{faxserver}) { - $c->log->error("faxserver feature is not active."); - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Faxserver feature is not active."); - return; - } - last unless $self->forbid_link_header($c); - last unless $self->valid_media_type($c, 'multipart/form-data'); - my $json_utf8 = encode_utf8($c->req->param('json')); - last unless $self->require_wellformed_json($c, 'application/json', $json_utf8 ); - my $resource = JSON::from_json($json_utf8, { utf8 => 0 }); - $resource->{faxfile} = $self->get_upload($c, 'faxfile'); - $c->log->debug("upload received"); - my $form = $self->get_form($c); - last unless $self->validate_form( + my $billing_subscriber = NGCP::Panel::Utils::API::Subscribers::get_active_subscriber($self, $c, $resource->{subscriber_id}); + unless($billing_subscriber) { + $c->log->error("invalid subscriber id $$resource{subscriber_id} for fax send"); + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Fax subscriber not found."); + return; + } + my $prov_subscriber = $billing_subscriber->provisioning_voip_subscriber; + return unless $prov_subscriber; + my $faxpref = $prov_subscriber->voip_fax_preference; + unless ($faxpref && $faxpref->active){ + $c->log->error("invalid subscriber fax preferences"); + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid subscriber fax preferences"); + return; + } + try { + $c->log->debug("contacting fax server"); + my $output = NGCP::Panel::Utils::Fax::send_fax( c => $c, - resource => $resource, - form => $form, + subscriber => $billing_subscriber, + destination => $form->values->{destination}, + upload => $form->values->{faxfile}, + data => $form->values->{data}, ); - $c->log->debug("form validated"); - my $billing_subscriber = NGCP::Panel::Utils::API::Subscribers::get_active_subscriber($self, $c, $resource->{subscriber_id}); - unless($billing_subscriber) { - $c->log->error("invalid subscriber id $$resource{subscriber_id} for fax send"); - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Fax subscriber not found."); - last; - } - my $prov_subscriber = $billing_subscriber->provisioning_voip_subscriber; - last unless $prov_subscriber; - my $faxpref = $prov_subscriber->voip_fax_preference; - unless ($faxpref && $faxpref->active){ - $c->log->error("invalid subscriber fax preferences"); - $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid subscriber fax preferences"); - last; - } - try { - $c->log->debug("contacting fax server"); - my $output = NGCP::Panel::Utils::Fax::send_fax( - c => $c, - subscriber => $billing_subscriber, - destination => $form->values->{destination}, - upload => $form->values->{faxfile}, - data => $form->values->{data}, - ); - $c->log->debug("faxserver output:\n"); - $c->log->debug($output); - } catch($e) { - $c->log->error("failed to send fax: $e"); - $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error"); - return; - }; - $c->response->status(HTTP_CREATED); - $c->response->body(q()); - } + $c->log->debug("faxserver output:\n"); + $c->log->debug($output); + } catch($e) { + $c->log->error("failed to send fax: $e"); + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error"); + return; + }; return; } diff --git a/lib/NGCP/Panel/Controller/API/FaxesItem.pm b/lib/NGCP/Panel/Controller/API/FaxesItem.pm index ea38afabf0..4605d2cd0f 100644 --- a/lib/NGCP/Panel/Controller/API/FaxesItem.pm +++ b/lib/NGCP/Panel/Controller/API/FaxesItem.pm @@ -3,82 +3,21 @@ use NGCP::Panel::Utils::Generic qw(:all); use Sipwise::Base; -use HTTP::Headers qw(); -use HTTP::Status qw(:constants); - -use NGCP::Panel::Utils::ValidateJSON qw(); -use NGCP::Panel::Utils::Subscriber; -require Catalyst::ActionRole::ACL; -require NGCP::Panel::Role::HTTPMethods; -require Catalyst::ActionRole::RequireSSL; - -sub allowed_methods{ - return [qw/GET OPTIONS HEAD/]; -} - use parent qw/NGCP::Panel::Role::EntitiesItem NGCP::Panel::Role::API::Faxes/; -sub resource_name{ - return 'faxes'; -} - -sub dispatch_path{ - return '/api/faxes/'; -} - -sub relation{ - return 'http://purl.org/sipwise/ngcp-api/#rel-faxes'; -} - __PACKAGE__->set_config({ allowed_roles => [qw/admin reseller subscriberadmin subscriber/], + dont_validate_hal => 1, }); -sub GET :Allow { - my ($self, $c, $id) = @_; - { - last unless $self->valid_id($c, $id); - my $item = $self->item_by_id($c, $id); - last unless $self->resource_exists($c, fax => $item); - - my $hal = $self->hal_from_item($c, $item); - - my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( - (map { # XXX Data::HAL must be able to generate links with multiple relations - s|rel="(http://purl.org/sipwise/ngcp-api/#rel-resellers)"|rel="item $1"|; - s/rel=self/rel="item self"/; - $_ - } $hal->http_headers), - ), $hal->as_json); - $c->response->headers($response->headers); - $c->response->body($response->content); - return; - } - return; +sub allowed_methods{ + return [qw/GET OPTIONS HEAD/]; } - - - - - - -#sub DELETE :Allow { -# my ($self, $c, $id) = @_; -# -# my $guard = $c->model('DB')->txn_scope_guard; -# { -# my $item = $self->item_by_id($c, $id); -# last unless $self->resource_exists($c, fax => $item); -# -# $item->delete; -# NGCP::Panel::Utils::Subscriber::vmnotify( 'c' => $c, 'fax' => $item ); -# $guard->commit; -# -# $c->response->status(HTTP_NO_CONTENT); -# $c->response->body(q()); -# } -# return; +#sub delete_item { +# my ($self, $c, $item) = @_; +# $item->delete; +# NGCP::Panel::Utils::Subscriber::vmnotify( 'c' => $c, 'fax' => $item ); #} 1; diff --git a/lib/NGCP/Panel/Role/API/Faxes.pm b/lib/NGCP/Panel/Role/API/Faxes.pm index 2045129ee9..0d85942053 100644 --- a/lib/NGCP/Panel/Role/API/Faxes.pm +++ b/lib/NGCP/Panel/Role/API/Faxes.pm @@ -52,29 +52,12 @@ sub get_form { return NGCP::Panel::Form::get("NGCP::Panel::Form::Subscriber::WebfaxAPI", $c); } -sub hal_from_item { - my ($self, $c, $item, $form) = @_; - - my $hal = Data::HAL->new( - links => [ - Data::HAL::Link->new( - relation => 'curies', - href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}', - name => 'ngcp', - templated => true, - ), - Data::HAL::Link->new(relation => 'collection', href => sprintf("/api/%s/", $self->resource_name)), - Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), - Data::HAL::Link->new(relation => 'self', href => sprintf("%s%d", $self->dispatch_path, $item->id)), - Data::HAL::Link->new(relation => 'ngcp:subscribers', href => sprintf("/api/subscribers/%d", $item->provisioning_voip_subscriber->voip_subscriber->id)), - Data::HAL::Link->new(relation => 'ngcp:faxrecordings', href => sprintf("/api/faxrecordings/%d", $item->id)), - ], - relation => 'ngcp:'.$self->resource_name, - ); - - my $resource = $self->resource_from_item($c, $item, $form); - $hal->resource($resource); - return $hal; +sub hal_links { + my($self, $c, $item, $resource, $form) = @_; + return [ + Data::HAL::Link->new(relation => 'ngcp:subscribers', href => sprintf("/api/subscribers/%d", $item->provisioning_voip_subscriber->voip_subscriber->id)), + Data::HAL::Link->new(relation => 'ngcp:faxrecordings', href => sprintf("/api/faxrecordings/%d", $item->id)), + ]; } sub resource_from_item { @@ -103,11 +86,5 @@ sub resource_from_item { return \%resource; } -sub item_by_id { - my ($self, $c, $id) = @_; - my $item_rs = $self->item_rs($c); - return $item_rs->find($id); -} - 1; # vim: set tabstop=4 expandtab: