How do I read from a config file in Flex? - apache-flex

I'm working on a project for school right now and we're trying to get it set up so that it is easily deployable. The webapp portion of it is written entirely in Adobe flex.
However, we need links to certain files / url's within the code which are different on different machines.
For instance, my server might use 8180 as the port while someone else uses 8080.
Or one person is using Windows so a filepath would be C:/... while mine would be /home/...
Is there any way we could put these files into a separate config file and read them dynamically within the mxml files?
It would be really nice if we didn't have to recompile for each individual deployment...
Thanks in advance!

You can use HTTPService to load an XML file (or any text file) that is in a location relative to the Flex application SWF. Simply execute the HTTPService on application startup, parse the file, and make the data available wherever you need it.

Check out appcorelib, the docs will show you how to use a relative URL like from an assets folder:
loadXML("assets/xml/config.xml);
Don't need to worry about crossdomain if the xml and flex app are on same server.

If you have enabled the local-file sandbox, you may be able to use URLLoader to read a local file:
http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=05B_Security_176_04.html
However, your SWF must also be local.
If you are loading the SWF remotely, you can connect back to the loading server for a list of resources. That should probably be the preferred solution in most cases.

You can pass in parameters into a SWF by adding FlashVars to the HTML that it's running from.
FLashVars

I strongly agree with brd6644. You will need a cross domain policy file on the server where the config files reside. Just copy the following XML to a file named "crossdomain.xml" and put it on the server root of the server that contains your config files.
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy
SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>
You may not need this if the swf resides on the same server as the config files. Also, as you can see, this cross domain policy allows access from all domains, so if you care a lot about security (may not be important for a school project), read up a bit on them to see how to configure. Here is a good article
Also, here is some sample HTTPService code:
private function init():void{
get_bands_service.url = yeswewillArtistsURL;
get_bands_service.method = "GET";
get_bands_service.addEventListener(FaultEvent.FAULT, onServiceFault);
get_bands_service.requestTimeout = 20;
get_bands_service.send();
}
<mx:HTTPService id="get_bands_service" result="parseBandsServiceResult();" useProxy="false" />

Related

How to deploy XML file with my web application?

I've written an ASP.net web application. In the interest of following the advice in "The Pragmatic Programmer" to put application logic in configuration, I wrote a large XML file that describes various business rules.
When I test the application on my local development workstation, I copy the file to c:\xxxxx\myfile.xml and then write the code to read the file from this location.
What is the correct way to deploy this xml file as part of my web application so that it gets read from the directory in which the web application is deployed?
Update: The XML file in question is for server-side configuration and should never be available for download to the end-user.
If you're not embedding this as a resource, you should:
Create a directory under which this file will reside.
Create the file and set its Build Action to Content.
Create a web.config file in that directory which forbids access to the directory so you don't expose your business rules to the Internet.
Add a setting in your application's main web.config that gives the path to this file relative to the root of the application, i.e. "~/MySecureFolder/MyBusinessRules.Xml".
Have some code that calls HttpServerUtility.MapPath Method to convert the value from the setting in web.config to a virtual path.
I don't know if this is what you want - Click on the XML file, then open the Property Window and find the "Build Action" property. Set the value to "Embedded Resources"
I think what you need to do is:
Add the XML file to your web application project in Visual Studio
Right click on the file and select 'Properties...'
Set the Build Action to 'Content' and Copy to Output Directory to 'Do not copy'
this will ensure that your XML file is deployed along with the rest of your web app.
If you want to make available your XML file from http requests to your server, you should
place it in your web publication folder.
This ASP instruction should help you to find your publication path:
Request.ServerVariables("APPL_PHYSICAL_PATH")

How to configure the flex crossdomain.xml in tipfy

I would like to know how to configure the flex crossdomain.xml in tipfy with the Google app engine skd.
Please advice. Thanks.
Edit:
Tipfy is a framework using in Gae.
I would like to know:
where I can place the crossdomain.xml, in the root or other place,
do I need script to redirect to the xml,
what files that I need to modify, eg. app.yaml.
any other things or file I need to modify or create to make it work.
Thanks.
Place the crossdomain.xml anywhere in your app (eg, the root of the app), then use a static file handler to configure it in app.yaml. You don't need to touch any framework code at all.
If you are using GAE the app url will be typically http://myapp.appspot.com and the Flash player will need a cross domain file from http://myapp.appspot.com/crossdomain.xml. In a Java environment it is as simple as creating this file in web folder of the project, that's it. It will be the same for Python, put this in your app's web root folder, not Google's web server root folder. This worked for me.

How to make embeddable flash content?

Hi I'm new to adobe flash/flex so please forgive me if my question isn't too clear. I'm developing a website with a flash object that dynamically generates its content and I want the flash object itself to be embeddable into other website like how youtube does it. I have no clue how to approach this and any help would be really appreciated.
You need two things:
1) Distribute the url or embed code for your swf online somewhere (like done in youtube). You get the code by publishing your flash object and then copy paste the html embed tags.
2) If you're dynamically loading stuff into the flash object you will need to allow data loads from all hosts. Lets say that you have a source file at www.domain.com that the flash object loads. Some one takes the Flash application and puts it on their site at www.otherdomain.com. This application then tries to do a cross domain data load www.otherdomain.com <- www.domain.com. That will fail unless you have you explicitly allow cross domain loads for www.domain.com. You do this by adding a crossdomain.xml file to your websites root or preferably the folder where the source file is kept. If you put in the webroot then all content hosted there will be available to load from anywhere. The xml file should contain all the domain that are allowed to load anything from your domain (in this case it should just contain a * to allow any domain to load from your domain).
Here's a basic example that allows any domain to load data
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM
"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>
More info on that (http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html)
The above answer is better than this one, but if you're brand new to Flash and Flex you might want to look into Adobe's distribution servies - http://www.adobe.com/flashplatform/services/distribution/ - I'm not sure if it will do everything you want but for a newbie it might not be a bad way to go.
=Ryan ryan#adobe.com

Flex External Configuration File

My problem: I have a program in Flex3 that accesses a server. The program itself is on a server and accessed through a web browser. The point is that I don't want to hardcode in the swf file the IP of the server to access, since it changes and for various other reasons...
How can I do that? Can I put a file in the same directory and what then?
To access a config file on the same server as the SWF, you should be able to use an HTTPService or URLLoader with a relative URL rather than absolute. You can get fancier (changing ports) by accessing the url field of your base Application and creating a new absolute URL from that.
If the SWF is hosted separately from the HTML, you can use BrowserManager url to build your config url instead.
See this article: Externalizing Service Configuration using BlazeDS and LCDS
It will also work for HTTPService with some minor modifications.

Where would I keep my .txt files for inclusion in a Flex app?

I'm working on a flex app, and I have Flash & AS3 experience up to now. I have text file I need to request using URLLoader, so I placed it in the same directory as the SWF
deploy > maps > map1.txt
but when run the SWF I get the following error
*** Security Sandbox Violation ***
SecurityError: Error #2148: SWF file file:///Users/him/Documents/Clients/Geekery/Bounce/deploy/Bounce.swf cannot access local resource /maps/map1.txt. Only local-with-filesystem and trusted local SWF files may access local resources.
at flash.net::URLStream/load()
at flash.net::URLLoader/load()
at com.geekery.Bounce::BounceMap()
at Bounce/loadMap()
at Bounce()
Which seems odd to me. Is there a special place I should be keeping files like this? Or is there some way I can allow files to be loaded form the same directory as the SWF?
Are you using a relative or absolute url? Have you tried loader.load( new URLRequest( 'maps/map1.txt' ) ) ?
If the file is static, you can use the #Embed tag to do this. See this example.
What you need is a crossdomain.xml to set the permissions for the .swf application on the same server. More info at below links
Cross-domain policy file specification
loadPolicyFile()
If this is not really what you are looking for then you can just go for the [Embed] solution.

Resources