From 345f89d4077399698522893c907e355202c3fbbc Mon Sep 17 00:00:00 2001 From: Irina Peshinskaya Date: Mon, 18 Feb 2019 09:07:04 +0100 Subject: [PATCH] TT#47534 Add configured defailt mime-type to Entities FW And enable non_json_data processing for Items PUT Change-Id: Ib87bed6aadce2f9217baa60aedc233364560876a --- .../Controller/API/VoicemailGreetingsItem.pm | 2 +- lib/NGCP/Panel/Role/API.pm | 16 ++++- lib/NGCP/Panel/Role/EntitiesItem.pm | 63 ++++++++++++------- 3 files changed, 58 insertions(+), 23 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/VoicemailGreetingsItem.pm b/lib/NGCP/Panel/Controller/API/VoicemailGreetingsItem.pm index 24da08a9d3..e230c9c473 100644 --- a/lib/NGCP/Panel/Controller/API/VoicemailGreetingsItem.pm +++ b/lib/NGCP/Panel/Controller/API/VoicemailGreetingsItem.pm @@ -15,7 +15,7 @@ __PACKAGE__->set_config({ 'Uploads' => {'greetingfile' => ['audio/x-wav', 'application/octet-stream']}, }, GET => { - #'application/json' is default, if no accept header was recieved. + #'application/json' is first one and thus is default, if no accept header was received. 'ReturnContentType' => ['application/json', 'audio/x-wav', 'audio/mpeg', 'audio/ogg'],#, }, allowed_roles => [qw/admin reseller subscriberadmin subscriber/], diff --git a/lib/NGCP/Panel/Role/API.pm b/lib/NGCP/Panel/Role/API.pm index 68ccad8e2c..a66176efe2 100644 --- a/lib/NGCP/Panel/Role/API.pm +++ b/lib/NGCP/Panel/Role/API.pm @@ -1662,6 +1662,21 @@ sub mime_type_from_query_params { return; } +sub mime_type_from_allowed_default { + my ($self, $c, $config_allowed_types, $system_default) = @_; + my $mime_type_from_config; + if ($config_allowed_types) { + if (!ref $config_allowed_types + && $config_allowed_types ne $system_default ) { + $mime_type_from_config = $config_allowed_types; + } elsif (ref $config_allowed_types eq 'ARRAY' + && $config_allowed_types->[0] ne $system_default) { + $mime_type_from_config = $config_allowed_types->[0]; + } + } + return $mime_type_from_config; +} + sub supported_mime_types_extensions { my ($self) = @_; my $action_config = $self->get_item_config('action'); @@ -1672,7 +1687,6 @@ sub supported_mime_types_extensions { return []; } - sub return_requested_type { my ($self, $c, $id, $item, $return_type) = @_; try{ diff --git a/lib/NGCP/Panel/Role/EntitiesItem.pm b/lib/NGCP/Panel/Role/EntitiesItem.pm index bb10cd720a..52fff8a8f7 100644 --- a/lib/NGCP/Panel/Role/EntitiesItem.pm +++ b/lib/NGCP/Panel/Role/EntitiesItem.pm @@ -14,7 +14,7 @@ use Data::HAL::Link qw(); use NGCP::Panel::Utils::Generic qw(:all); use NGCP::Panel::Utils::DateTime; use NGCP::Panel::Utils::ValidateJSON qw(); - +use TryCatch; ##### --------- common part sub auto :Private { @@ -188,42 +188,44 @@ sub get { my $action_config = $self->get_config('action'); my $config_allowed_types = $action_config->{GET}->{ReturnContentType}; - my $apllication_json = 'application/json'; + my $system_default = 'application/json'; + my $mime_type_from_config = $self->mime_type_from_allowed_default( + $c, $config_allowed_types, $system_default + ); my $header_accept = $c->request->header('Accept'); my $mime_type_from_accept_header; if( ( defined $header_accept - #apllication/json is default, so we will not consider it in the Accept header + #apllication/json is default, so we will not consider it in the Accept header, until our default in config is different from 'application/json' #*/* allows us everything, so we will first try query parameter or default configured default mime-type - #while we will not implement more complex logic, when application/json is not default - && ($header_accept !~ m!\bapplication/json\b!) - && ($header_accept !~ m#(?mime_type_from_query_params($c); + #TODO: to method if( defined $mime_type_from_accept_header || defined $mime_type_from_query_params #no header Accept passed, check configured return type - || ( $config_allowed_types - && ( - ( ( !ref $config_allowed_types) - && $config_allowed_types ne $apllication_json) - || ( ref $config_allowed_types eq 'ARRAY' - && !grep { $_ eq $apllication_json } @{ $config_allowed_types } ) - ) - ) - + || defined $mime_type_from_config ) { my $return_type = $mime_type_from_accept_header // $mime_type_from_query_params; if ($return_type) { return unless $self->check_return_type($c, $return_type, $config_allowed_types); - } elsif (!ref $config_allowed_types) { - $return_type = $config_allowed_types; + } else { + $return_type = $mime_type_from_config; } $self->return_requested_type($c, $id, $item, $return_type); # in case this method is not defined, we should return a reasonable error explaining the Accept Header @@ -310,7 +312,7 @@ sub put { my $item = $self->item_by_id_valid($c, $id); last unless $item; my $method_config = $self->get_config('action')->{PUT}; - my ($resource, $data) = $self->get_valid_data( + my ($resource, $data, $non_json_data) = $self->get_valid_data( c => $c, id => $id, method => 'PUT', @@ -321,8 +323,27 @@ sub put { last unless $resource; my $old_resource = $self->resource_from_item($c, $item); - ($item, $form, $process_extras) = $self->update_item($c, $item, $old_resource, $resource, $form, $process_extras ); - last unless $item; + my ($data_processed_result); + if (!$non_json_data || !$data) { + ($item, $form, $process_extras) = $self->update_item($c, $item, $old_resource, $resource, $form, $process_extras ); + last unless $item; + } else { + try { + #$processed_ok(array), $processed_failed(array), $info, $error + $data_processed_result = $self->process_data( + c => $c, + item => $item, + data => \$data, + resource => $resource, + form => $form, + process_extras => $process_extras, + ); + } catch($e) { + $c->log->error("failed to proces non json data: $e"); + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error"); + last; + }; + } my $hal = $self->get_journal_item_hal($c, $item, { form => $form }); last unless $self->add_journal_item_hal($c, { hal => $hal });