URLStream throws Error#2029 in my flex AIR app - apache-flex

In my AIR app, I am trying to implement a file downloader using URLStream.
public class FileDownloader {
// Class to download files from the internet
// Function called every time data arrives
// called with an argument of how much has been downloaded
public var onProgress :Function = function(loaded:Number, total:Number):void{};
public var onComplete :Function = function():void{};
public var remotePath :String = "";
public var localFile :File = null;
public var running:Boolean = false;
public var stream :URLStream;
private var fileAccess :FileStream;
public function FileDownloader( remotePath :String = "" , localFile :File = null ) {
this.remotePath = remotePath;
this.localFile = localFile;
}
public function load() :void
{
try
{
stream = null;
if( !stream || !stream.connected )
{
stream = new URLStream();
fileAccess = new FileStream();
var requester :URLRequest = new URLRequest( remotePath );
var currentPosition :uint = 0;
var downloadCompleteFlag :Boolean = false;
// Function to call oncomplete, once the download finishes and
// all data has been written to disc
fileAccess.addEventListener( "outputProgress", function ( result ):void {
if( result.bytesPending == 0 && downloadCompleteFlag ) {
stream.close();
fileAccess.close();
running = false;
onComplete();
}
});
fileAccess.openAsync( localFile, FileMode.WRITE );
fileAccess.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent)
{
trace('remotePath: '+remotePath);
trace('io error while wrintg ....'+e.toString());
});
stream.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent)
{
trace('remotePath: '+remotePath);
trace('There was an IO error with the stream: '+e.text);
});
stream.addEventListener( "progress" , function (e:ProgressEvent) :void {
var bytes :ByteArray = new ByteArray();
var thisStart :uint = currentPosition;
currentPosition += stream.bytesAvailable;
// ^^ Makes sure that asyncronicity does not break anything
try
{
//trace('reading from '+remotePath+' ...');
stream.readBytes( bytes, thisStart );
fileAccess.writeBytes( bytes, thisStart );
}
catch(err:Error)
{
trace('remotePath: '+remotePath);
trace('error while writing bytes from...'+err.name+':'+err.message);
if(stream.connected)
stream.close();
abort();
onComplete();
return;
}
onProgress( e.bytesLoaded, e.bytesTotal );
});
stream.addEventListener( "complete", function () :void {
downloadCompleteFlag = true;
});
stream.load( requester );
} else {
// Do something unspeakable
}
running = true;
}
catch(err:Error)
{
trace('error while downloading the file: '+err);
}
}
public function abort():void {
try {
stream.close();
trace('stream closed');
running = false;
}
catch(err:Error) {
trace('error while aborting download');
trace(err);
}
}
}
I simply create an object of the above class and passing the url and the file and call the load function. For some files I get the following error.
remotePath: http://mydomain.com/238/6m_608-450.jpg
error while writing bytes from...Error:Error #2029: This URLStream object does not have a stream opened.
Which means the error is from the file stream(fileAccess) that I am using. I am unable to figure out why this could be happening. If I try to open the url http://mydomain.com/238/6m_608-450.jpg in the browser, it opens properly. This happens randomly for some files. What could be the problem?

I have tried in my office and it works for me (for differents files and filesize).
So, can you describe the files (or types files) which don't work for you (post an url if you can) ?
I would say that when you use the method readBytes your stream (so the URLStream) is ever close.
More, I allows me some advice :
1/ Use flash's constants instead of simple string
2/ Don't forget to remove your listeners once the operation completed
3/ Your method FileDownloader is quite confusing. Use lowercase if it's a function or puts a capital letter with class's name if you use it as a constructor. For me, this function must be a constructor.

Related

Async Vala Example

In the Book "Introduction to Vala" by Dr Michael Lauer, he has mentioned that the lib Soup async api is broken. I'm struggling to write a simple example using session.queue_message that query radio stations using the service from radio-browser. Here is my code. I would appreciate any help form experienced Programmers like "Al Thomas". Thank you.
public class Station : Object {
// A globally unique identifier for the change of the station information
public string changeuuid { get; set; default = ""; }
// A globally unique identifier for the station
public string stationuuid { get; set; default = ""; }
// The name of the station
public string name { get; set; default = ""; }
// The stream URL provided by the user
public string url { get; set; default = ""; }
// and so on ... many properties
public string to_string () {
var builder = new StringBuilder ();
builder.append_printf ("\nchangeuuid = %s\n", changeuuid);
builder.append_printf ("stationuuid = %s\n", stationuuid);
builder.append_printf ("name = %s\n", name);
builder.append_printf ("url = %s\n", url);
return (owned) builder.str;
}
}
public class RadioBrowser : Object {
private static Soup.Session session;
// private static MainLoop main_loop;
public const string API_URL = "https://de1.api.radio-browser.info/json/stations";
public const string USER_AGENT = "github.aeldemery.radiolibrary";
public RadioBrowser (string user_agent = USER_AGENT, uint timeout = 50)
requires (timeout > 0)
{
Intl.setlocale ();
session = new Soup.Session ();
session.timeout = timeout;
session.user_agent = user_agent;
session.use_thread_context = true;
// main_loop = new MainLoop ();
}
private void check_response_status (Soup.Message msg) {
if (msg.status_code != 200) {
var str = "Error: Status message error %s.".printf (msg.reason_phrase);
error (str);
}
}
public Gee.ArrayList<Station> listStations () {
var stations = new Gee.ArrayList<Station> ();
var data_list = Datalist<string> ();
data_list.set_data ("limit", "100");
var parser = new Json.Parser ();
parser.array_element.connect ((pars, array, index) => {
var station = Json.gobject_deserialize (typeof (Station), array.get_element (index)) as Station;
assert_nonnull (station);
stations.add (station);
});
var msg = Soup.Form.request_new_from_datalist (
"POST",
API_URL,
data_list
);
// send_message works but not queue_message
// session.send_message (msg);
session.queue_message (msg, (sess, mess) => {
check_response_status (msg);
try {
parser.load_from_data ((string) msg.response_body.flatten ().data);
} catch (Error e) {
error ("Failed to parse data, error:" + e.message);
}
});
return stations;
}
}
int main (string[] args) {
var radio_browser = new RadioBrowser ();
var stations = radio_browser.listStations ();
assert_nonnull (stations);
foreach (var station in stations) {
print (station.to_string ());
}
return 0;
}
While I'm not Al Thomas, I still might be able to help. ;)
For async calls to work in there needs to be a main loop running, and typically from the main program thread. Thus you want to create and execute the main loop from your main() function, rather than in your application code:
int main (string[] args) {
var loop = new GLib.MainLoop ();
var radio_browser = new RadioBrowser ();
// set up async calls here
// then set the main loop running as the last thing
loop.run();
}
Also, if you want to wait for an async call to complete, you typically need to make the call using the yield keyword from another async function E.g:
public async Gee.ArrayList<Station> listStations () {
…
// When this call is made, execution of listStations() will be
// suspended until the soup response is received
yield session.send_async(msg);
// Execution then resumes normally
check_response_status (msg);
parser.load_from_data ((string) msg.response_body.flatten ().data);
…
return stations;
}
You can then call this from the (non-async) main function using the listStations.begin(…) notation:
int main (string[] args) {
var loop = new GLib.MainLoop ();
var radio_browser = new RadioBrowser ();
radio_browser.listStations.begin((obj, res) => {
var stations = radio_browser.listStations.end(res);
…
loop.quit();
});
loop.run();
}
As further reading, I would recommend the async section of the Vala Tutorial, and the asyc examples on the wiki as well.

'Server side events' send with the ASP Web Api do not arrive?

I created a test source which should send a message to the client every x time. This is the ApiController:
public class TestSourceController : ApiController
{
private static readonly ConcurrentQueue<StreamWriter> ConnectedClients = new ConcurrentQueue<StreamWriter>();
[AllowAnonymous]
[Route("api/sources/test")]
public HttpResponseMessage Get()
{
var response = Request.CreateResponse();
response.Content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>) OnStreamAvailable,
"text/event-stream");
return response;
}
private static void OnStreamAvailable(Stream stream, HttpContent headers, TransportContext context)
{
var clientStream = new StreamWriter(stream);
ConnectedClients.Enqueue(clientStream);
}
private static void DoThings()
{
const string outboundMessage = "Test";
foreach (var clientStream in ConnectedClients)
{
clientStream.WriteLine("data:" + JsonConvert.SerializeObject(outboundMessage));
clientStream.Flush();
}
}
}
The clientStream.Flush(); is called like expected and without exceptions.
I handle it in AngularJS like this:
$scope.handleServerCallback = function (data) {
console.log(data);
$scope.$apply(function() {
$scope.serverData = data;
});
};
$scope.listen = function () {
$scope.eventSource = new window.EventSource("http://localhost:18270/api/sources/test");
$scope.eventSource.onmessage = $scope.handleServerCallback;
$scope.eventSource.onopen = function() { console.log("Opened source"); };
$scope.eventSource.onerror = function (e) { console.error(e); };
};
$scope.listen();
My guess is it's a problem with the server since I can see the "EventStream" from the test call is empty in the chrome debugger.
Does anyone know how to make sure the messages arrive at the client?
The solution was quite easy, according to the spec every line has to end with "\n" and the very last line with "\n\n".
So:
clientStream.WriteLine("data:" + JsonConvert.SerializeObject(outboundMessage) + "\n\n");
Solves it.

NSubstitute HttpPostedFileBase SaveAs

On my journey of unit testing my code and I have this code:
var ufile = Substitute.For<HttpPostedFileBase>();
var server = Substitute.For<HttpServerUtilityBase();
var saved = false;
ufile.FileName.Returns("somefileName");
var fileName = fs.Path.GetFileName(ufile.FileName);
var path = fs.Path.Combine(server.MapPath(upath), fileName);
ufile.When(h => h.SaveAs(path)).Do(x => saved = true);
Assert.IsTrue(saved);
So here is what I am testing which I gleaned from different sites:
public ActionResult UploadFiles()
{
var fileinfo = new List<UploadedImageViewModel>();
foreach (string files in Request.Files)
{
var hpf = Request.Files[files] as HttpPostedFileBase; //
if (hpf != null && hpf.ContentLength > 0)
continue;
var FileName = Path.GetFileName(hpf.FileName); //Gets name of file uploaded
var temppath = Path.Combine(Server.MapPath("~/uploadtemp/"), FileName); // creates a string representation of file location
hpf.SaveAs(temppath);
//resize the image before you save it to the right folder under FileUploads
}
return View(fileinfo);
}
Can someone please help me understand this when().Do() syntax of Nsubstitute? In the docs, it says the do should have an action in it but I will need examples to understand.
Then the SaveAs() method of HttpPostedFileBase is void and in Nsubstitute Docs, it says to use when().Do() for void methods so please tell me what is wrong with my unit test.
//Suppose we have this setup
public class MyClass
{
string ReturnSomething()
{
return "FooBar";
}
void DoSomething(out string reason){
reason = 'Oops';
}
}
The usual stubbing syntax for NSubstitute is to use Returns like this:
myClass.ReturnSomething().Returns("wibble");
This stubs out ReturnSomething(), but the Returns syntax only works for methods with a return value.
For methods that have no return we can instead use When().Do(). This is basically what is meant by an Action in their documentation (as opposed to a Func, which does have a return value). A common need to do this is to fill in output parameters on such methods:
string reason;
myClass.When(c => c.DoSomething(out reason))
.Do(args =>
{
args[0] = "Fail";
});
For more on Action and Func see MSDN: Action, Func.
In the specific case of your unit test, instead of setting a variable saved when SaveAs is invoked, consider asserting using the NSubstitute.Received construct instead.

How to pass content in response from Exception filter in Asp.net WebAPI?

Consider following code:
My problem is:
1) I can't seem to cast the errors to HttpContent
2) I can't use the CreateContent extension method as this doesn't exist on the context.Response.Content.CreateContent
The example here only seems to provide StringContent and I'd like to be able to pass the content as a JsobObject:
http://www.asp.net/web-api/overview/web-api-routing-and-actions/exception-handling
public class ServiceLayerExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
if (context.Response == null)
{
var exception = context.Exception as ModelValidationException;
if ( exception != null )
{
var modelState = new ModelStateDictionary();
modelState.AddModelError(exception.Key, exception.Description);
var errors = modelState.SelectMany(x => x.Value.Errors).Select(x => x.ErrorMessage);
// Cannot cast errors to HttpContent??
// var resp = new HttpResponseMessage(HttpStatusCode.BadRequest) {Content = errors};
// throw new HttpResponseException(resp);
// Cannot create response from extension method??
//context.Response.Content.CreateContent
}
else
{
context.Response = new HttpResponseMessage(context.Exception.ConvertToHttpStatus());
}
}
base.OnException(context);
}
}
context.Response = new HttpResponseMessage(context.Exception.ConvertToHttpStatus());
context.Response.Content = new StringContent("Hello World");
you also have the possibility to use the CreateResponse (added in RC to replace the generic HttpResponseMessage<T> class that no longer exists) method if you want to pass complex objects:
context.Response = context.Request.CreateResponse(
context.Exception.ConvertToHttpStatus(),
new MyViewModel { Foo = "bar" }
);

how to use this as class code in mmxl aaplication?

I have taken this code from one of my friend,but in real it is totally mess there are plenty of error i am getting by running this code,i have some question
How i can use this code in mxml application,as we know we can not use public class in mx script so what are the way to do that
as you can see .mx_internal,i am getting error on that saying'define object before dot' as i remove mx_internal and tried using import mx.binding.mx_internal and use namespace mx_internal application not desplaying anything
now you will tell me ther are easy methods are ther to solve problem but my whole project is on this method only
thanks for help in advance
i am trying to solve this problem from last 15 days ,with no success,pls help me below is code
package components
{
import flash.events.*;
import flash.utils.*;
import mx.binding.*;
import mx.containers.*;
import mx.controls.*;
import mx.core.*;
import mx.events.*;
import mx.styles.*;
public class DialogTitle extends HBox implements IBindingClient
{
private var _110371416title:String = "Dialog Title";
public var _DialogTitle_Image1:Image;
public var _DialogTitle_Image2:Image;
public var _DialogTitle_Label1:Label;
var _bindingsBeginWithWord:Object;
private var _1859425293showCloseButton:Boolean = false;
var _bindingsByDestination:Object;
var _watchers:Array;
var _bindings:Array;
private var _documentDescriptor_:UIComponentDescriptor;
private static var _watcherSetupUtil:IWatcherSetupUtil;
public function DialogTitle()
{
_documentDescriptor_ = new UIComponentDescriptor({type:HBox, propertiesFactory:function () : Object
{
return {height:27, childDescriptors:[new UIComponentDescriptor({type:Spacer, propertiesFactory:function () : Object
{
return {width:5};
}// end function
}), new UIComponentDescriptor({type:Image, id:"_DialogTitle_Image1"}), new UIComponentDescriptor({type:Spacer, propertiesFactory:function () : Object
{
return {width:5};
}// end function
}), new UIComponentDescriptor({type:Label, id:"_DialogTitle_Label1", stylesFactory:function () : void
{
this.fontSize = 14;
this.color = 16777215;
this.fontWeight = "bold";
return;
}// end function
}), new UIComponentDescriptor({type:Spacer, propertiesFactory:function () : Object
{
return {percentWidth:100};
}// end function
}), new UIComponentDescriptor({type:Image, id:"_DialogTitle_Image2", events:{click:"___DialogTitle_Image2_click"}, propertiesFactory:function () : Object
{
return {useHandCursor:true, buttonMode:true, mouseChildren:false, toolTip:"Close"};
}// end function
}), new UIComponentDescriptor({type:Spacer, propertiesFactory:function () : Object
{
return {width:13};
}// end function
})]};
}// end function
});
_bindings = [];
_watchers = [];
_bindingsByDestination = {};
_bindingsBeginWithWord = {};
mx_internal::_document = this;
if (!this.styleDeclaration)
{
this.styleDeclaration = new CSSStyleDeclaration();
}
this.styleDeclaration.defaultFactory = function () : void
{
this.backgroundColor = 9947478;
this.horizontalGap = 0;
this.verticalAlign = "middle";
this.verticalGap = 0;
return;
}// end function
;
this.height = 27;
this.percentWidth = 100;
return;
}// end function
private function _DialogTitle_bindingExprs() : void
{
var _loc_1:* = undefined;
_loc_1 = CustomEmbeddedAssets.logoImageSmall;
_loc_1 = title;
_loc_1 = EmbeddedAssets.dialogClose;
_loc_1 = showCloseButton;
return;
}// end function
public function get showCloseButton() : Boolean
{
return this._1859425293showCloseButton;
}// end function
override public function initialize() : void
{
var target:DialogTitle;
var watcherSetupUtilClass:Object;
.mx_internal::setDocumentDescriptor(_documentDescriptor_);
var bindings:* = _DialogTitle_bindingsSetup();
var watchers:Array;
target;
if (_watcherSetupUtil == null)
{
watcherSetupUtilClass = getDefinitionByName("_components_DialogTitleWatcherSetupUtil");
var _loc_2:* = watcherSetupUtilClass;
_loc_2.watcherSetupUtilClass["init"](null);
}
_watcherSetupUtil.setup(this, function (param1:String)
{
return target[param1];
}// end function
, bindings, watchers);
var i:uint;
while (i < bindings.length)
{
Binding(bindings[i]).execute();
i = (i + 1);
}
mx_internal::_bindings = mx_internal::_bindings.concat(bindings);
mx_internal::_watchers = mx_internal::_watchers.concat(watchers);
super.initialize();
return;
}// end function
public function get title() : String
{
return this._110371416title;
}// end function
private function _DialogTitle_bindingsSetup() : Array
{
var binding:Binding;
var result:Array;
binding = new Binding(this, function () : Object
{
return CustomEmbeddedAssets.logoImageSmall;
}// end function
, function (param1:Object) : void
{
_DialogTitle_Image1.source = param1;
return;
}// end function
, "_DialogTitle_Image1.source");
result[0] = binding;
binding = new Binding(this, function () : String
{
var _loc_1:* = title;
var _loc_2:* = _loc_1 == undefined ? (null) : (String(_loc_1));
return _loc_2;
}// end function
, function (param1:String) : void
{
_DialogTitle_Label1.text = param1;
return;
}// end function
, "_DialogTitle_Label1.text");
result[1] = binding;
binding = new Binding(this, function () : Object
{
return EmbeddedAssets.dialogClose;
}// end function
, function (param1:Object) : void
{
_DialogTitle_Image2.source = param1;
return;
}// end function
, "_DialogTitle_Image2.source");
result[2] = binding;
binding = new Binding(this, function () : Boolean
{
return showCloseButton;
}// end function
, function (param1:Boolean) : void
{
_DialogTitle_Image2.visible = param1;
return;
}// end function
, "_DialogTitle_Image2.visible");
result[3] = binding;
return result;
}// end function
public function set showCloseButton(param1:Boolean) : void
{
var _loc_2:* = this._1859425293showCloseButton;
if (_loc_2 !== param1)
{
this._1859425293showCloseButton = param1;
this.dispatchEvent(PropertyChangeEvent.createUpdateEvent(this, "showCloseButton", _loc_2, param1));
}
return;
}// end function
public function ___DialogTitle_Image2_click(event:MouseEvent) : void
{
dispatchEvent(new Event("onCancel"));
parent.visible = false;
return;
}// end function
public function set title(param1:String) : void
{
var _loc_2:* = this._110371416title;
if (_loc_2 !== param1)
{
this._110371416title = param1;
this.dispatchEvent(PropertyChangeEvent.createUpdateEvent(this, "title", _loc_2, param1));
}
return;
}// end function
public static function set watcherSetupUtil(param1:IWatcherSetupUtil) : void
{
DialogTitle._watcherSetupUtil = param1;
return;
}// end function
}
}
Just throw this code out. This code was generated automatically by mxmlc compiler from MXML class and can't be used in production. It is for computer, not for humans. Write the right code by yourself. I hope it will be clearer and maintainable. And yes, this code wasn't written by your friend :)
This file is decompiled from a .mxml file, which use tags to describe layout. However, some decompiler can't convert it completely, and you will see file like this.
As you see, the argument of UIComponentDescriptor is an object, which contains a lot of key-val pairs. It has some type of keys:
type:Class --> this is the type of this component, which is the node tag of .mxml file
id:String --> this is the ID of a node, which is also the variable name you can use in <fx:Script> tag
event:Object --> events that the component will trigger
stylesFactory:Function --> component's styles setting
propertiesFactory:Function --> contains some properties and child nodes
code like this:
<mx:Canvas id="mainCanvas" borderStyle="none" label="main">
<mx:Button click="onClick(event)" />
</mx:Canvas>
will be converted into:
new UIComponentDescriptor({
"type":Canvas,
"id":mainCanvas,
"styleFactory":function():void {
this.borderStyle="none";
},
"propertiesFactory":function():Object {
return({
"label":"main"
"childDescriptors":[new UIComponentDescriptor({
// button's code in here
});
]
})
}
})
For those events:
As you can write click event in mxml tags both like btnOnClick() and btnOnClick(event), the compiler need to do something to ensure the event handler accept a right argument. So, for the Button tag I just mention, the
value corresponding to key"event" will be like this: {"click":"__on_click"}. Then the compiler will create a new function call __on_click, which may like this:
public function __on_click(event:MouseEvent):void {
onClick(event);
}
Obviously, after you convert the UIComponentDescriptor into .mxml, you should replace the events function.
For the constructor:
.mxml file can not has a constructor, but the compiler will create one in .as file, which contains the UIComponentDescriptor. The constructor will also do some initialization for it's variables. When converting to .mxml file, you need to do this initialization after the variable's declaration.
For example:
public function MyCanvas(){
this._documentDescriptor_ = new UIComponentDescriptor({
//……
});
this.myArray = new Array();
}
you should convert it into this:
<fx:Script><![CDATA[
var myArray:Array = new Array();
]]></fx:Script>
So the heaviest work is to convert UIComponentDescriptor into .mxml tags. And here is a simple python script that can help you do it:
UIComponentDescriptor2XML

Resources