/**
* Copyright (c) 2007 - 2009 Adobe
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
package samples
{
import com.adobe.cairngorm.task.Task;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
/**
* A task that performs a REST-ful request. This can be used directly in
* task groups or else extended to create more specific tasks.
*/
public class RestfulRequest extends Task
{
//------------------------------------------------------------------------
//
// Private Variables
//
//------------------------------------------------------------------------
private var service:HTTPService;
//------------------------------------------------------------------------
//
// Public Properties
//
//------------------------------------------------------------------------
[Bindable]
public var url:String;
[Bindable]
public var parameters:Object;
[Bindable]
public var resultFormat:String;
[Bindable]
public var resultData:Object;
//------------------------------------------------------------------------
//
// Implementation : WorkItem
//
//------------------------------------------------------------------------
override protected function performTask():void
{
if (url == null)
fault();
service = new HTTPService();
service.method = parameters == null ? "get" : "post";
service.resultFormat = resultFormat;
service.url = url;
service.addEventListener(ResultEvent.RESULT, onResult);
service.addEventListener(FaultEvent.FAULT, onFault);
service.send(parameters);
}
//------------------------------------------------------------------------
//
// Protected Methods
//
//------------------------------------------------------------------------
protected function handleResultData(data:Object):void
{
// for optional overriding
}
protected function handleFaultInfo(info:Object):void
{
// for optional overriding
}
//------------------------------------------------------------------------
//
// Event Handlers
//
//------------------------------------------------------------------------
private function onResult(event:ResultEvent):void
{
cleanUp();
resultData = event.result;
handleResultData(resultData);
complete();
}
private function onFault(event:FaultEvent):void
{
cleanUp();
handleFaultInfo(event);
fault(event.fault.faultString);
}
private function cleanUp():void
{
service.removeEventListener(ResultEvent.RESULT, onResult);
service.removeEventListener(FaultEvent.FAULT, onFault);
service = null;
}
}
}