Skip to content

Instantly share code, notes, and snippets.

@netikular
Last active January 3, 2016 04:09
Show Gist options
  • Save netikular/8406641 to your computer and use it in GitHub Desktop.
Save netikular/8406641 to your computer and use it in GitHub Desktop.
Copied from adobe forums. Comments in source citing original authors.
/*
Copied from: http://forums.adobe.com/thread/951628
Below is our communication class. This class is used for all of our RESTful calls. It handles most of the common errors so we don't have to deal with them over and over again in the applications. It always sets itself up as the listener for the RESULT event, does some initial, common processing, then calls the application RESULT function that is set for this particular instance of the communication class. It also has code to inspect the url the SWF was loaded from, and build the REST service url based on that, so we can use one class for multiple sites.
Mark
Follow up commend from "pradeep"
So I have unraveled the Truth. (I think)
It's more tortured than one would imagine
1/ All HTTP GET requests are stripped of headers. It's not in the Flex stack so it's probably the underlying Flash player runtime
2/ All HTTP GET requests that have content type other than "application/x-www-form-urlencoded" are turned into POST requests
3/ All HTTP POST requests that have no actual posted data are turned into GET requests. See 1/ and 2/
4/ All HTTP PUT and HTTP DELETE requests are turned into POST requests. This appears to be a browser limitation that the Flash player is stuck with.
What this boils down to in practical terms is that if you want to pass headers in all requests, you should always use POST and you should find another way to communicate the semantics of the operation you "really wanted". The Rails community have settled on passing ?_method=PUT/DELETE as a work around for the browser problems underlying 4/
Since Flash adds the wonderful header stripping pain on GET, I'm also using ?_method=GET as a workaround for that. However, since this trips up on 3/,
I am passing a dummy object as the encoded POST data. Which means my service needs to ignore dummy posted data on a ?_method=GET request.
Crucial at this point to know about 2/. That wasted a bunch of my time.
I've built all of this handling into a new RESTService class with MXML markup support so it's possible to pretend this doesn't exist on the client side.
Hope this helps someone.
*/
/* Communication Utilities
*/
package Utility {
/* Importation */
import mx.controls.Alert;
import mx.controls.Button;
import mx.core.Application;
import mx.core.FlexGlobals;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.mxml.HTTPService;
public class Communication extends HTTPService {
/* Non-Bindable Public Properties */
public var Cursor:Boolean = true;
public var DebugFlag:Boolean = false;
public var Message:String;
public var Params:Object;
public var Receiver:Function = null;
public var ReceiverAdditional:Function = null;
public var ErrorHandler:Function = null;
public var btnCancel:Button = null;
/* Bindable Public Properties */
[Bindable] static public var Status:Boolean = true;
[Bindable] static public var StatusMessage:String = "";
/* Static Public Properties */
static public var HTTPS:Boolean = true;
/* Private Properties */
private var Retry:Number = 0;
/* Constructor */
public function Communication() {
resultFormat = "e4x";
method = "POST";
var thisapp:Object = FlexGlobals.topLevelApplication;
var baseurl:String = FlexGlobals.topLevelApplication.url;
var urlarray:Array = baseurl.split("/")
var pattern1:RegExp = new RegExp("http://[^/]*/");
var pattern2:RegExp = new RegExp("https://[^/]*/");
//the swf can be loaded from either http or https, but the data requests are always
//sent with https, NO EXCEPTIONS!!!!
if (pattern1.test(baseurl) || pattern2.test(baseurl)) {
var location:String = new String();
var requestfrom:String = urlarray[3].toString();
requestfrom = requestfrom.toUpperCase();
switch(requestfrom) {
case "ONE":
location = "SERVERONE";
if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {
FlexGlobals.topLevelApplication.is Production = true;
}
break;
case "TWO":
location = "SERVERTWO";
if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {
FlexGlobals.topLevelApplication.is Production = false;
DebugFlag = true;
}
break;
case "THREE":
location = "SERVERTHREE";
if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {
FlexGlobals.topLevelApplication.is Production = true;
}
break;
case "FOUR":
location = "SERVERFOUR";
if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {
FlexGlobals.topLevelApplication.is Production = true;
}
break;
case "FIVE":
location = "SERVERFIVE";
if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {
FlexGlobals.topLevelApplication.is Production = false;
DebugFlag = true;
}
break;
case "SIX":
location = "SERVERSIX";
if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {
FlexGlobals.topLevelApplication.is Production = true;
}
break;
case "SIXTEST":
location = "SERVERSIX";
if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {
FlexGlobals.topLevelApplication.is Production = false;
DebugFlag = true;
}
break;
case "SEVEN":
location = "SERVERSEVEN";
if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {
FlexGlobals.topLevelApplication.is Production = true;
}
break;
case "SEVENTEST":
location = "SERVERSEVEN";
if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {
FlexGlobals.topLevelApplication.is Production = false;
DebugFlag = true;
}
break;
case "EIGHTTEST":
location = "SERVEREIGHT";
if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {
FlexGlobals.topLevelApplication.is Production = false;
DebugFlag = true;
}
break;
case "EIGHT":
location = "SERVEREIGHT";
if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {
FlexGlobals.topLevelApplication.is Production = true;
}
break;
default:
location = "SERVERONE";
if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {
FlexGlobals.topLevelApplication.is Production = true;
}
break;
}
url = "https://" + urlarray[2].toString() + "/scripts/mgwms32.dll?MGWLPN=" + location + "&MGWAPP=ENTRY";
}
else {
//falls to here if run from flash builder since the URL is a local file path
DebugFlag = true;
var debugloc:String = "DEBUGSERVER";
if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {
FlexGlobals.topLevelApplication.isProduction = true;
}
url="https://debugserver/scripts/mgwms32.dll?MGWLPN="+debugloc+"&MGWAPP=ENTRY";
}
}
/* Public Functions */
// Failed communication with the server
public function Failure(event:FaultEvent):void {
if (btnCancel != null) {
btnCancel.enabled=false;
}
Status = false;
removeEventListener(FaultEvent.FAULT, Failure);
var fault:String = event.fault.faultString;
var errorpos:Number = fault.indexOf(":",0);
var error:String = fault.substring(0, errorpos);
var invalid:Boolean = false;
if (error == "Error #1085") invalid = true;
if (error == "Error #1090") invalid = true;
if (invalid) {
StatusMessage = "Received Invalid XML: " + fault.substring((errorpos + 1), fault.length);
} else {
Retry = Retry + 1;
if (Retry > 3) {
if (HTTPS) {
HTTPS = false;
Transmit(true);
return;
}
StatusMessage = "Connection Lost";
} else {
Transmit(true);
}
}
}
// Receive data from the server
public function Receive(event:ResultEvent):void {
if (btnCancel != null) {
btnCancel.enabled=false;
}
Status = true;
StatusMessage = "";
var errorcode:String = event.result.ErrorCode;
var errormessage:String = event.result.ErrorMessage;
if (errorcode == "") {
if (Receiver != null) Receiver.call(this, event);
if (ReceiverAdditional != null) ReceiverAdditional.call(this);
return;
}
else {
if (ErrorHandler != null) {
ErrorHandler.call(this, event);
return;
}
}
Communication.StatusMessage = errorcode + ": " + errormessage;
Alert.show("Error processing request\n Error Code: " + errorcode + " \n " + errormessage);
}
// Transmit data to the server
public function Transmit(usemessage:Boolean):void {
if (btnCancel != null) {
btnCancel.enabled=true;
}
Status = true;
if (usemessage) StatusMessage = Message;
addEventListener(ResultEvent.RESULT, Receive);
addEventListener(FaultEvent.FAULT, Failure);
showBusyCursor = Cursor;
if (DebugFlag) {
Params.DEBUGTRACE = 1
}
if (Params.SCITOKEN == null && Params.SCIAPP != "LOGIN" ) {
Params.SCITOKEN=FlexGlobals.topLevelApplication.a ppUser.Token;
}
send(Params);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment