views:

1857

answers:

2

I am using Flex to connect to a Rest service. To access order #32, for instance, I can call the URL http://[service]/orders/32. The URL must be configured as a destination - since the client will connect to different instances of the service. All of this is using the Blaze Proxy, since it involves GET, PUT, DELETE and POST calls. The problem is:- how do I append the "32" to the end of a destination when using HttpService? All I do is set the destination, and at some point this is converted into a URL. I have traced the code, but I don't know where this is done, so can't replace it.

Options are: 1. Resolve the destination to a URL within the Flex client, and then set the URL (with the appended data) as the URL. 2. Write my own java Flex Adapter that overrides the standard Proxy, and map parameters to the url like the following: http://[service]/order/{id}?id=32 to http://[service]/order/32

Has anyone come across this problem before, and are there any simple ways to resolve this?

A: 

Here's a simple way to resolve the url to the HTTPService within Flex via the click event's handler.

here's a service:

<mx:HTTPService
 id="UCService"
 result="UCServiceHandler(event)" 
 showBusyCursor="true"
 resultFormat="e4x"
 />

Then here's the handler:

        private function UCmainHandler(UCurl:String) {

            UCService.url = UCurl;
            UCService.send();

        }

And here's a sample click event:

<mx:Button label="add to cart" click="UCmainHandler('http://sampleurl.com/cart/add/p18_q1?destination=cart')" />

Of course you could pass other values to the click handler, or even have the handler add things to the url based on other current settings etc...

Hope that helps!

defmeta
I want to be able to modify the URL configured as a destination in the proxy-config.xml. Unfortunately, the URL is not available to the application unless I work through the actual config and modify it
Verdant
+1  A: 

Just so everyone knows, this is how I resolved this issue:

I created a custom HTTPProxyAdapter on the server

public MyHTTPProxyAdapter extends flex.messaging.services.http.HTTPProxyAdapter {

public Object invoke(Message message) {
    // modify the message - if required
    process(message);
    return super.invoke(message);
}

private void process(Message message) {
        HTTPMessage http = (HTTPMessage)message;
        if(http != null) {
            String url = http.getUrl();
            ASObject o = (ASObject)http.getBody();
            if(o != null) {
                Set keys = o.keySet();
                Iterator it = keys.iterator();
                while(it.hasNext()) {
                    String key = (String)it.next();
                    String token = "[" + key +"]";
                    if(url.contains(token)) {
                        url = url.replace(token, o.get(key).toString());
                        o.remove(key);
                    }

                }
                http.setUrl(url);
            }
        }
    }
}

Then replaced the destination adapter to my adapter. I can now use the following URL in the config.xml and anything in square brackets will be replaced by the Query string:

<destination id="user-getbytoken">
        <properties>
            <url>http://localhost:8080/myapp/public/client/users/token/[id]&lt;/url&gt;
        </properties>
</destination>

In this example, setting the destination to user-getbytoken and the parameters {id:123} will result in the url of http://localhost:8080/myapp/public/client/users/token/123

Verdant