Flash and Amazon SimpleDB

I did some research about using Amazons SimpleDB service within a Flash based application and found these basic facts:

  • http://sdb.amazonaws.com/crossdomain.xml is not available. This means no direct calls from Flash clients in browsers are possible!
  • Even if it would be possible some day, you would need to find a secure way to deliver your AWS key to the Flash client.
  • Using a web proxy is a solution that would cause a lot of traffic on your server.

So using Amazons SimpleDB for browser based Flash applications makes not much sense to me.
It might be more useful for Flash applications running on local machines like Flash projectors or Adobe AIR applications but there is still a security problem with hiding your AWS key.

Anyway, I found two ActionScript libraries that might be useful:
http://code.google.com/p/actionscript-simpledb-library/
http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1365

And this thread in Amazons discussion forums:
http://developer.amazonwebservices.com/connect/thread.jspa?threadID=19698

URLRequest with HTTP authentication

I found a good explanation how to support HTTP authentication with URLRequests here.

That’s the most interesting part:

Best I can tell, for some reason, this only works where request method is POST; the headers don’t get set with GET requests.

Interestingly, it also fails unless at least one URLVariables name-value pair gets packaged with the request, as indicated above. That’s why many of the examples you see out there (including mine) attach “name=John+Doe” — it’s just a placeholder for some data that URLRequest seems to require when setting any custom HTTP headers. Without it, even a properly authenticated POST request will also fail.

You’ll almost surely have to modify your crossdomain.xml file to accommodate the header(s) you’re going to be sending. In my case, I’m using this, which is a rather wide-open policy file in that it accepts from any domain, so in your case, you might want to limit things a bit more, depending on how security-conscious you are:

<?xml version="1.0"?>
<cross-domain-policy>
    <allow-access-from domain="*" />
    <allow-http-request-headers-from domain="*" headers="Authorization" />
</cross-domain-policy>

… and that seems to work; more information on this one is available from Adobe here).

Apparently, Flash player version 9.0.115.0 completely blocks all Authorization headers (more information on this one here), so you’ll probably want to keep that in mind, too.

So this little code snippet explains the basics:

// Base64Encoder contained in Flex SDK
import mx.utils.Base64Encoder;
 
// Encode username and password
var base64Encoder : Base64Encoder = new Base64Encoder();        
    base64Encoder.encode( "username:password" );
 
// Create authorization request header
var urlRequestHeader : URLRequestHeader = new URLRequestHeader( "Authorization", "Basic " + base64Encoder.toString() );
 
// URLRequest setup
var urlRequest : URLRequest = new URLRequest( "url" );        
    // Needs to send some data!!        
    urlRequest.data = new URLVariables( "name=John+Doe" );        
    // Only supported with POST method!!        
    urlRequest.method = URLRequestMethod.POST;        
    // Apply authorization request header        
    urlRequest.requestHeaders = new Array( urlRequestHeader );

DisplayObject.rotationX,Y,Z

Just a quick note about working with DisplayObject.rotationX,Y,Z:

Keep in mind that your DisplayObject rotates around it’s center point. This may result in weird optics if you forget to align your DisplayObject properly.

Update: Well, there is much more to know!

There’s need for an DisplayObjectContainer with an properly assigned PerspectiveProjection.

Copy & paste this code and you’ll see what I’m talking about:

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener( Event.RESIZE, onStageResize );
 
function onStageResize( event : Event ) : void
{
	draw();
}
 
function createRectSprite( w : int, h : int, rotationY : Number, container : Sprite, useProjection : Boolean ) : Sprite
{
	// 3D object
	var graphicSprite : Sprite = new Sprite();
		graphicSprite.name = "graphicSprite";
		graphicSprite.rotationY = rotationY;
		graphicSprite.buttonMode = true;
		graphicSprite.useHandCursor = true;
 
	// Make sure 3D objects content is centered
	var g : Graphics = graphicSprite.graphics;
		g.beginFill( 0, .5 );
		g.drawRect( w * -.5, h * -.5, w, h );
		g.endFill();
 
	// 3D Container
    var containerSprite : Sprite = new Sprite();
        containerSprite.addChild( graphicSprite );
 
	// Apply projection to 3D Container instead of 3D Object
	if ( useProjection )
	{
		var projection:PerspectiveProjection = new PerspectiveProjection();             
			projection.projectionCenter = new Point( 0, 0 );
 
		containerSprite.transform.perspectiveProjection = projection;
    }
 
	// Visualize center
	g = containerSprite.graphics;
	g.beginFill( 0xff0000 );
	g.drawCircle( 0, 0, 2 );
	g.endFill();
 
    container.addChild( containerSprite );    
    return containerSprite;
}
 
var left : Sprite;
var right : Sprite;
 
function draw() : void
{
	while ( numChildren > 0 ) { removeChildAt( 0 ); }
 
	var container : Sprite = new Sprite()
		container.name = "container";
		container.x = stage.stageWidth * .5;
		container.y = stage.stageHeight * .5;
	addChild( container );
 
	var stepX : int = stage.stageWidth / 3;
 
	var w : int = 200;
	var h : int = 200;
 
	left = createRectSprite( w, h, -60, this, true );
	left.name = "left";
	left.x = stepX;
 
	right = createRectSprite( w, h, 60, this, true );
	right.name = "right";
	right.x = stepX * 2;
 
    left.y =
    right.y = stage.stageHeight * .5;
}
 
function onEventHitTest( event : Event ) : void
{
	var containerSprite : DisplayObjectContainer;
	var graphicSprite : DisplayObject;
 
	containerSprite = this.getChildByName( "left" ) as DisplayObjectContainer;
	graphicSprite = containerSprite.getChildByName( "graphicSprite" ) as DisplayObject;
 
	trace( "hit left containerSprite:", containerSprite.hitTestPoint( mouseX, mouseY, true ) );
	trace( "hit left graphicSprite:", graphicSprite.hitTestPoint( mouseX, mouseY, true ) );
 
	containerSprite = this.getChildByName( "right" ) as DisplayObjectContainer;
	graphicSprite = containerSprite.getChildByName( "graphicSprite" ) as DisplayObject;
 
	trace( "hit right containerSprite:", containerSprite.hitTestPoint( mouseX, mouseY, true ) );
	trace( "hit right graphicSprite:", graphicSprite.hitTestPoint( mouseX, mouseY, true ) );
	trace( "" );
}
 
stage.addEventListener( MouseEvent.MOUSE_DOWN, onEventHitTest );
 
draw();

Mac – Local Server Development Basics

Since I had to figure out some basics about local server development on my Mac, I really recommend these tools:

  • MAMP – Apache and MySQL Server
  • Virtual Host X – Create up to 3 virtual host with the free version.
    In other words: This provides an easy to use GUI which enables you to forward an local URL like “http://myProject.local” to your workspace directory.

But one thing sucks: You have to set read/write access (in finder) for everyone. I wonder if that might cause system security problems?

And I strongly recommend to read this nice tutorial about setting up virtual hosts manually: http://www.sawmac.com/mamp/virtualhosts/

Zend Framework CLI Tool Usage

God damn it. I spent hours trying to generate the basic filestructure using the Zend_Tool (zf.php) shipping within the Zend Framework download. It didn’t work and I got really frustrated.

Well, guess what. Now I downloaded the next version (1.9.5) and it works!

Just open the terminal, navigate to the bin directory within the framework download and type (Mac OS X):

./zf.sh create project ./

This is what get’s generated: Zend_1_9_5_Structure