AMF-Connection

 view release on metacpan or  search on metacpan

examples/amfclient.pl  view on Meta::CPAN


use AMF::Connection;
use HTTP::Cookies;

use JSON;

BEGIN
  {
    no strict 'refs';

    # blessed hash object to JSON object
    map
      {
        my $amf_class = $_;
        my $foo = $amf_class."::TO_JSON";

        # unbless object
        *$foo = sub {
            my $f = $_[0];

            #process_amf_object ($f, $amf_class);

            +{ %{$f} };
          }
      } (
          # add your own remote service classes here - or use an SWFDecompiler

          'flex.messaging.messages.AcknowledgeMessage'
        );

    # blessed hash object to JSON array
    map
      {
        my $foo = $_."::TO_JSON";
        # unbless
        *$foo = sub {
            $_[0]->{'externalizedData'};
          }
      } (
          'flex.messaging.io.ArrayCollection'
        );
  }

my $endpoint = 'http://swxformat.org/php/amf.php';
my $service = 'Twitter';

examples/amfclient.pl  view on Meta::CPAN

#$client->addHeader( 'serviceBrowser', 'true' );
$client->setHTTPCookieJar( HTTP::Cookies->new(file => "/tmp/lwpcookies.txt", autosave => 1, ignore_discard => 1 ) );

my $params = [  "italy" ];
my ($response) = $client->call( $service.'.'.$method, $params );

my $json = JSON->new;
$json->ascii(1);
$json->utf8(1);
$json->pretty(1);
$json->allow_blessed(1);
$json->convert_blessed(1);
my $json_data = $json->encode( $response->getData );

if ( $response->is_success ) {
        print $json_data;
} else {
        die "Can not send remote request for $service.$method method with params on $endpoint using AMF".$client->getEncoding()." encoding:\n".$json_data."\n";
        };

examples/get-brightcove-videos-metadata.pl  view on Meta::CPAN


# see http://code.google.com/p/get-flash-videos/

use AMF::Connection;
use JSON;

BEGIN
  {
    no strict 'refs';

    # blessed hash object to JSON object
    map
      {
        my $amf_class = $_;
        my $foo = $amf_class."::TO_JSON";

        # unbless object
        *$foo = sub {
            my $f = $_[0];

            #process_amf_object ($f, $amf_class);

            +{ %{$f} };
          }
      } (
          # add your own remote service classes here - or use an SWFDecompiler

          'com.brightcove.templating.SecondaryContentDTO',
	  'com.brightcove.templating.FeaturedItemDTO',
	  'com.brightcove.catalog.trimmed.VideoDTO',
	  'com.brightcove.utils.BrightcoveDateDTO',
	  'com.brightcove.catalog.TagDTO',
	  'com.brightcove.catalog.VideoCuePointDTO'
        );

    # blessed hash object to JSON array
    map
      {
        my $foo = $_."::TO_JSON";
        # unbless
        *$foo = sub {
            $_[0]->{'externalizedData'};
          }
      } (
          'flex.messaging.io.ArrayCollection'
        );
  }

my $endpoint = 'http://c.brightcove.com/services/amfgateway';
my $service = 'com.brightcove.templating.TemplatingFacade';

examples/get-brightcove-videos-metadata.pl  view on Meta::CPAN

                                                                                      }
                                                       }
                                                     ];

my $response = $client->call( $service.'.'.$method, $params );

my $json = JSON->new;
$json->ascii(1);
$json->utf8(1);
$json->pretty(1);
$json->allow_blessed(1);
$json->convert_blessed(1);
my $json_data = $json->encode( $response->getData );

if ( $response->is_success ) {
	print $json_data;
} else {
	die "Can not send remote request for $service.$method method with params on $endpoint using AMF".$client->getEncoding()." encoding:\n".$json_data."\n";
	};

lib/AMF/Connection.pm  view on Meta::CPAN

		'http_headers' => {},
		'http_cookie_jar' => new HTTP::Cookies(),
		'response_counter' => 0,
		'encoding' => 0, # default is AMF0 encoding
		'ua'	=> new LWP::UserAgent(),
		'append_to_endpoint' => ''
		};

	$self->{'ua'}->cookie_jar( $self->{'http_cookie_jar'} );

        return bless($self, $class);
	};

# plus add paramters, referer, user agent, authentication/credentials ( see also SecureAMFChannel stuff ), 
# plus timezone on retunred dates to pass to de-serializer - see AMF3 spec saying "it is suggested that time zone be queried independnetly as needed" - unelss local DateTime default to right locale!

# we pass the string, and let Storable::AMF to parse the options into a scalar - see Input/OutputStream and Storable::AMF0 documentation

sub setInputAMFOptions {
	my ($class, $options) = @_;

lib/AMF/Connection.pm  view on Meta::CPAN

			$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!

lib/AMF/Connection.pm  view on Meta::CPAN

=head1 DESCRIPTION

I was looking for a simple Perl module to automate data extraction from an existing Flash+Flex/AMS application, and I could not find a decent client implementation. So, this module was born based on available online documentation.

This module has been inspired to SabreAMF PHP implementation of AMF client libraries.

AMF::Connection is meant to provide a simple AMF library to write client applications for invocation of remote services as used by most flex/AIR RIAs. 

The module includes basic support for synchronous HTTP/S based RPC request-response access, where the client sends a request to the server to be processed and the server returns a response to the client containing the processing outcome. Data is sent...

AMF0 and AMF3 support is provided using the Storable::AMF module. While HTTP/S requestes to the AMF endpoint are carried out using the LWP::UserAgent module. The requests are sent using the HTTP POST method as AMF0 encoded data by default. AMF3 encod...

If encoding is set to AMF3 the Flex Messaging framework is used on returned responses content (I.e. objects casted to "flex.messaging.messages.AcknowledgeMessage" and "flex.messaging.messages.ErrorMessage" are returned).

Simple batch requests and responses is provided also.

See the sample usage synopsis above to start using the module.

=head1 DATE TYPE SUPPORT

The latest 0.79 version of Storable::AMF added basic date support with the new_date() and perl_date() utilitiy functions. This is just great. Internally an AMF Date Type represents a timestamp in milliseconds since the epoch in UTC ("neutral") timezo...

lib/AMF/Connection.pm  view on Meta::CPAN

There is still an issue when arbitrary Date structures are returned from an AMF server to an AMF::Connection (E.g. as part of values of structured AMF Objects). In this case, the AMF::Connection does not try to reparse the Perl object structure retur...

All this means that an AMF::Connection client application can not rely on those Date returned by the server as being Perl timestamps (seconds since the epoch) and will need explicitly call perl_date() or divide the timestamp by 1000 *explicitly*.

=head2 USING OLD Storable::AMF VERSIONS

In older versions (pre 0.79) of Storable::AMF there was no support for the AMF0/AFM3 Date Type, and everything was being passed as string to the server (E.g. "2010-09-22T02:34:00"). Or as double (number) if the date was in timestamp seconds since the...

=head1 MAPPING OBJECTS

By combining the power of Perl bless {}, $package_name syntax and the Storable::AMF freeze()/thaw() interface, it is possible to pass arbitrary structured AMF Objects the corresponding AMF server can interpret. This is possible due a simple Perl obje...

This means that an AMF::Connection application can wrap all ActionScript / Flex AMF Objects around Perl Objects and get them sent; and returned into a Perl object BLOB using the power of Storable::AMF freeze()/thaw().A

For example to send a SearchQueryFx AMF Object to the server an AMF::Connection advanced search call(), the following code could be used:

my $client = new AMF::Connection ( ... );

# ... prepare parameters...

my $searchAMFObject = bless( {
                   'searchId' => $searchId,
                   'startHit' => int($startHit),
                   'searchString' => $searchString,
                   'hitsPerPage' => ($hitsPerPage) ? int($hitsPerPage) : 20,
                   'sortId' => $sortId,
       }, 'com.mycompany.application.flex.data.SearchQueryFx');

my $response = $client->call( "MySearchSevice.searchAdvanced", [ $searchAMFObject ] );

#....

lib/AMF/Connection/InputStream.pm  view on Meta::CPAN


	if (defined $storable_amf_options)
	  {
	    if ($Storable::AMF::VERSION < 0.84)
	      {
	        croak "Storable::AMF 0.84 or newer needed to set stream options\n";
	      }
	    $self->{'options'} = Storable::AMF::parse_option ($storable_amf_options);
	  }

	return bless($self, $class);
	};

sub readBuffer {
	my ($class, $length) = @_;

	croak "Buffer underrun at position: ". $class->{'cursor'} . ". Trying to fetch ". $length . " bytes from buffer total length ".length($class->{'stream'})
		if($length + $class->{'cursor'} > length($class->{'stream'}));

        my $data = substr($class->{'stream'},$class->{'cursor'},$length);
	$class->{'cursor'}+=$length;

lib/AMF/Connection/Message.pm  view on Meta::CPAN

sub new {
	my $proto = shift;
	my $class = ref($proto) || $proto;
	
	my $self = {
		'encoding' => 0, # default is AMF0 encoding
		'bodies' => [],
		'headers' => []
		};

	return bless($self, $class);
	};

sub serialize {
	my ($class, $stream) = @_;

	croak "Stream $stream is not a valid output stream"
		unless(ref($stream) and $stream->isa("AMF::Connection::OutputStream"));

	# we default to AMF0 encoding
	$stream->writeByte(0x00);

lib/AMF/Connection/MessageBody.pm  view on Meta::CPAN

	my $proto = shift;
	my $class = ref($proto) || $proto;
	my ($target,$response,$data) = @_;
	
	my $self = {
		'target' => $target,
		'response' => $response,
		'data' => $data # we might want to have some kind of mapper between remote objects and local / user registered ones
		};

	return bless($self, $class);
	};

sub setTarget {
	my ($class, $target) = @_;

	$class->{'target'} = $target;
	};

sub getTarget {
	my ($class) = @_;

lib/AMF/Connection/MessageHeader.pm  view on Meta::CPAN

	my ($name, $value, $required) = @_;
	
	my $self = {};

	$self->{'name'} = $name;

	$self->{'value'} = $value;

	$self->{'required'} = ($required=~m/(1|yes)/) ? 1 : 0;

	return bless($self, $class);
	};

sub isRequired {
	my ($class) = @_;

	return $class->{'required'};
	};

sub getName {
	my ($class) = @_;

lib/AMF/Connection/OutputStream.pm  view on Meta::CPAN


	if (defined $storable_amf_options)
          {
            if ($Storable::AMF::VERSION < 0.84)
              {
                croak "Storable::AMF 0.84 or newer needed to set stream options\n";
              }
            $self->{'options'} = Storable::AMF::parse_option ($storable_amf_options);
          }

	return bless($self, $class);
	};

sub writeBuffer {
	my ($class, $str) = @_;

	$class->{'stream'}.=$str;
	};

sub writeByte {
	my ($class, $byte) = @_;



( run in 0.838 second using v1.01-cache-2.11-cpan-de7293f3b23 )