AMF-Connection
view release on metacpan or search on metacpan
lib/AMF/Connection.pm view on Meta::CPAN
my $request = new AMF::Connection::Message;
$request->setEncoding( $class->{'encoding'} );
# add AMF any request headers
map { $request->addHeader( $_ ); } @{ $class->{'headers'} };
# TODO - prepare HTTP/S request headers based on AMF headers received/set if any - and credentials
foreach my $call (@batch)
{
next
unless (defined $call && ref ($call) =~ m/HASH/
&& defined $call->{'operation'} && defined $call->{'arguments'});
my $operation = $call->{'operation'};
my $arguments = $call->{'arguments'};
my $body = new AMF::Connection::MessageBody;
$class->{'response_counter'}++;
$body->setResponse( "/".$class->{'response_counter'} );
if( $class->{'encoding'} == 3 ) { # AMF3
$body->setTarget( 'null' );
my (@operation) = split('\.',$operation);
my $method = pop @operation;
my $service = join('.',@operation);
my $destination = (defined $call->{'destination'}) ? $call->{'destination'} : $service;
my $remoting_message = $class->_brew_flex_remoting_message( $service, $method, {}, $arguments, $destination);
$body->setData( [ $remoting_message ] ); # it seems we need array ref here - to be checked
} else {
$body->setTarget( $operation );
$body->setData( $arguments );
};
$request->addBody( $body );
}
my $request_stream = new AMF::Connection::OutputStream($class->{'output_amf_options'});
# serialize request
$request->serialize($request_stream);
#use Data::Dumper;
#print STDERR Dumper( $request );
# set any extra HTTP header
map { $class->{'ua'}->default_header( $_ => $class->{'http_headers'}->{$_} ); } keys %{ $class->{'http_headers'} };
my $http_response = $class->{'ua'}->post(
$class->{'endpoint'}.$class->{'append_to_endpoint'}, # TODO - check if append to URL this really work for HTTP POST
Content_Type => "application/x-amf",
Content => $request_stream->getStreamData()
);
croak "HTTP POST error: ".$http_response->status_line."\n"
unless($http_response->is_success);
my $response_stream = new AMF::Connection::InputStream( $http_response->decoded_content, $class->{'input_amf_options'});
my $response = new AMF::Connection::Message;
$response->deserialize( $response_stream );
#print STDERR Dumper( $response )."\n";
# process AMF response headers
$class->_process_response_headers( $response );
my @all = @{ $response->getBodies() };
# we make sure the main response is always returned first
return (wantarray) ? @all : $all[0];
};
# TODO
#
# sub command { } - to send "flex.messaging.messages.CommandMessage" instead
#
sub setCredentials {
my ($class, $username, $password) = @_;
$class->addHeader( 'Credentials', { 'userid' => $username,'password' => $password }, 0 );
};
sub _process_response_headers {
my ($class,$message) = @_;
foreach my $header (@{ $message->getHeaders()}) {
if($header->getName eq 'ReplaceGatewayUrl') { # another way used by server to keep cookies-less sessions
$class->setEndpoint( $header->getValue )
unless( ref($header->getValue) );
} elsif($header->getName eq 'AppendToGatewayUrl') { # generally used for cokies-less sessions E.g. ';jsessionid=99226346ED3FF5296D08146B02ECCA28'
$class->{'append_to_endpoint'} = $header->getValue
unless( ref($header->getValue) );
};
};
};
# just an hack to avoid rewrite class mapping local-to-remote and viceversa and make Storable::AMF happy
sub _brew_flex_remoting_message {
my ($class,$source,$operation,$headers,$body,$destination) = @_;
return bless( {
'clientId' => _generateID(),
'destination' => $destination,
'messageId' => _generateID(),
'timestamp' => time() . '00',
'timeToLive' => 0,
'headers' => ($headers) ? $headers : {},
'body' => $body,
'correlationId' => undef,
'operation' => $operation,
'source' => $source # for backwards compatibility - google for it!
}, 'flex.messaging.messages.RemotingMessage' );
};
sub _generateID {
my $uniqueid;
( run in 0.422 second using v1.01-cache-2.11-cpan-754626df90b )