Grails (iOS specific): Returning video (mp4) file gives Broken Pipe exception (getOutputStream() has already been called for this response) - ios

I'm trying to return an mp4 file from my Grails controller so that it can be played in the browser. The following is the simplest version of what I have:
def file = new File(<path to mp4 file>)
response.outputStream << file.newInputStream()
The strange thing is that this works when hitting it from a desktop (Chrome on my MacBook), works on an Android phone, but does not work on an iPad Air.
The one header that's different in the iOS request is for "range" of "0-1", but it looks like that might not be causing a problem (tested by adding that request on my laptop).
The exception says:
ERROR errors.GrailsExceptionResolver - SocketException occurred when processing request: [GET]
and further down it says
getOutputStream() has already been called for this response.
I've found many others with similar errors, but they talk about webRequest.setRenderView(false), flushing and closing the outputstream, and many other options. I've tried all of those, but nothing seems to work.
The part that really gets me is that it works on everything except iOS.
Any thoughts would be greatly appreciated. Thanks in advance!
UPDATE 1
Per Graeme's answer below, the accept header from Chrome is:
accept -> text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
And iOS produces multiple requests, which have the following accept headers:
accept -> text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept -> */*
The second accept header, */* is the what occurs during the exception.
I have also created a JIRA issue for Grails:
http://jira.grails.org/browse/GRAILS-11325

Might be related to the Accept header that gets sent, as Grails has some parsing depending on the Accept header. If you could post an example in a JIRA with steps to reproduce that would help.
http://jira.grails.org/browse/GRAILS

This turned out to be an iOS specific issue. The range header is required to be implemented, and if you try to return the entire file content for the response of a range request, iOS will not make additional requests.
The following is the code I used:
try {
def rangeValue = request.getHeader("range")
log.debug("rangeValue: ${rangeValue}")
if (rangeValue != null) {
// Get start and end string, substring(6) removes "bytes="
def (start, end) = rangeValue.substring(6).split("-")
def startInt = start.toLong()
def endInt = end.toLong()
def fileSize = file.length()
response.reset()
response.setStatus(206)
response.setHeader("Accept-Ranges", "bytes")
// WARNING: Do not sent Content-length, as it appears to prevent videos from working in iOS
response.setHeader("Content-range", "bytes ${start}-${end}/"+Long.toString(fileSize))
response.setContentType("video/quicktime")
def bytes = new byte[endInt-startInt+1]
def inputStream = file.newInputStream()
// Skip to the point in the inputStream that the range is requesting
inputStream.skip(startInt)
// Read a chunk of the input stream into the bytes array
inputStream.read(bytes, 0, bytes.length)
response.outputStream << bytes
}
else {
response.outputStream << file.newInputStream()
}
} catch (ClientAbortException e) {
log.error("User aborted download")
}
There are a few important notes:
If the Content-length header is returned in the response, iOS will not play the video. Seems like this could be related to content being gzipped - https://stackoverflow.com/a/2359184/2601060
When using the inputStream.read() function, it will always start reading at the beginning of the stream, so make sure to skip() to the proper position in the file (the startInt)
A response can be reset() to make sure that anything that has already been written is not included (this may not be required, but prevents automatic grails actions from providing default behavior)

Related

TIdHTTP->Get() , Server ignores parameters

I've got this query:
https://api-v3.mojepanstwo.pl/dane/krs_podmioty.json?conditions[krs_podmioty.nip]=7282827109
In a browser, it works OK, showing data specific for the given nip number.
But in Indy, I get a response as if the query part was omitted:
https://api-v3.mojepanstwo.pl/dane/krs_podmioty.json
I've tried this so far:
BurL = "https://api-v3.mojepanstwo.pl/dane/krs_podmioty.json?conditions[krs_podmioty.nip]=7282827109";
BurL = TIdURI::URLEncode("https://api-v3.mojepanstwo.pl/dane/krs_podmioty.json?conditions[krs_podmioty.nip]=7282827109");
End even raw urlencoded data:
BurL= "https://api-v3.mojepanstwo.pl/dane/krs_podmioty.json?conditions%5Bkrs_podmioty.nip%5D=7282827109";
Code:
try {
Resp = IdHTTPKrs->Get(BurL);
} catch (EIdHTTPProtocolException& e) {
ShowMessage(e.Message);
}
What's wrong, and how can I fix this? Or, maybe I am too tired already and am missing something obvious?
I suspect there is something with the [] part of the query, but I am just guessing here. Similar queries without the [] work OK.
I am using C++Builder XE6 pro, with Indy 10.6.0.512
Your Indy version is out of date. The latest version, at the time of this writing, is 10.6.2.5448. Using the latest version, I can't reproduce your issue. Both URL encodings return the same data for me. As they should be, since a web server is required to decode urlencoded characters when processing the requested URL. conditions%5Bkrs_podmioty.nip%5D=7282827109 and conditions[krs_podmioty.nip]=7282827109 should be getting processed the exact same way by the server, as they are sematically identical data.

DocuSign Connect update XML desserialization error

I have been using DocuSign SOAP and REST based API calls to create envelope and am also using their Connect feature to update the recipient and envelope statuses for my clients.
I am getting a strange error parsing DocuSign Connect update for one client.
The error says "There is an error in XML document (1, 16174)".
Here is my code...
Dim sr As New StreamReader(Request.InputStream)
Dim reader As XmlReader = New XmlTextReader(New StringReader(xml))
Dim serializer As New XmlSerializer(GetType(DocuSignEnvelopeInformation), "http://www.docusign.net/API/3.0")
If Not serializer Is Nothing Then
envelopeInfo = TryCast(serializer.Deserialize(reader), DocuSignEnvelopeInformation)
Dim envid As String = envelopeInfo.EnvelopeStatus.EnvelopeID.ToString
I have tried bunch of things such as removing the XML definition from the XML document but did not work. The strange thing is that the same code works for all of my other clients. This is the only client that is having issues. They have added closed 65 tags in the document to be signed but I don't think that the tags are causing issues on their end since I also tried removing them.
Please advise.
Minal
I have run into this issue before when there are unsupported characters in the tab values or in the PDF byte stream itself when it is decoded. I suspect that copying and pasting values into tabs from external programs like Word introduce some invisible weird characters like 
 - carriage returns and the like. You should validate your XML in its entirety.

Does Ruby's 'open_uri' reliably close sockets after read or on fail?

I have been using open_uri to pull down an ftp path as a data source for some time, but suddenly found that I'm getting nearly continual "530 Sorry, the maximum number of allowed clients (95) are already connected."
I am not sure if my code is faulty or if it is someone else who's accessing the server and unfortunately there's no way for me to really seemingly know for sure who's at fault.
Essentially I am reading FTP URI's with:
def self.read_uri(uri)
begin
uri = open(uri).read
uri == "Error" ? nil : uri
rescue OpenURI::HTTPError
nil
end
end
I'm guessing that I need to add some additional error handling code in here...
I want to be sure that I take every precaution to close down all connections so that my connections are not the problem in question, however I thought that open_uri + read would take this precaution vs using the Net::FTP methods.
The bottom line is I've got to be 100% sure that these connections are being closed and I don't somehow have a bunch open connections laying around.
Can someone please advise as to correctly using read_uri to pull in ftp with a guarantee that it's closing the connection? Or should I shift the logic over to Net::FTP which could yield more control over the situation if open_uri is not robust enough?
If I do need to use the Net::FTP methods instead, is there a read method that I should be familiar with vs pulling it down to a tmp location and then reading it (as I'd much prefer to keep it in a buffer vs the fs if possible)?
I suspect you are not closing the handles. OpenURI's docs start with this comment:
It is possible to open http/https/ftp URL as usual like opening a file:
open("http://www.ruby-lang.org/") {|f|
f.each_line {|line| p line}
}
I looked at the source and the open_uri method does close the stream if you pass a block, so, tweaking the above example to fit your code:
uri = ''
open("http://www.ruby-lang.org/") {|f|
uri = f.read
}
Should get you close to what you want.
Here's one way to handle exceptions:
# The list of URLs to pass in to check if one times out or is refused.
urls = %w[
http://www.ruby-lang.org/
http://www2.ruby-lang.org/
]
# the method
def self.read_uri(urls)
content = ''
open(urls.shift) { |f| content = f.read }
content == "Error" ? nil : content
rescue OpenURI::HTTPError
retry if (urls.any?)
nil
end
Try using a block:
data = open(uri){|f| f.read}

Symfony mailer: Swift_TransportException between message sending

On a current project which I'm currently working, i have a symfony task that runs some mass data insertion to database and runs it for at least half an hour.
When the task starts a mail notification is sent correctly, the problem is that at the of the task execution we can't send another mail to notify about the end of processing.
The mailer factory is currently configured with the spool delivery strategy but, in this specific situation, we desire to fire a notification immediately, using the sendNextImmediately() method.
I'm are getting the exception:
[Swift_TransportException]
Expected response code 250 but got code "451", with message "451 4.4.2 Timeout - closing connection. 74sm1186065wem.17
"
and the flowing error on php log file:
Warning: fwrite(): SSL: Broken pipe in /var/www/project/lib/vendor/symfony/lib/vendor/swiftmailer/classes/Swift/Transport/StreamBuffer.php on line 209
Can anyone give some help?
Is there any way that i can perhaps refresh symfony mailer to establish a new connection?
Doing a Symfony2 project, I ran across this failure too. We were using a permanently running php script, which produced the error.
We figured out that following code does the job:
private function sendEmailMessage($renderedTemplate, $subject, $toEmail)
{
$mailer = $this->getContainer()->get('mailer');
/* #var $mailer \Swift_Mailer */
if(!$mailer->getTransport()->isStarted()){
$mailer->getTransport()->start();
}
$sendException = null;
/* #var $message \Swift_Message */
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($this->getContainer()->getParameter('email_from'))
->setTo($toEmail)
->setBody($renderedTemplate);
$mailer->send($message);
$mailer->getTransport()->stop();
//throw $sendException;
}
For Symfony1 Users
My guess was that the connection was being hold for too long (with no activity at all), causing an ssl connection timeout.
For now, the problem can be solved by stopping the Swift_Transport instance and starting it again explicitly, just before sending the second message.
Here is the code:
$this->getMailer()->getRealtimeTransport()->stop();
$this->getMailer()->getRealtimeTransport()->start();
$this->getMailer()->sendNextImmediately()->send($message);
I had exactly the same problem and above solutions were very helpful, but there is one thing I had to do differently: order.
$this->getMailer()->sendNextImmediately()->send($message);
$this->getMailer()->getRealtimeTransport()->stop();
It didn't worked for me if I tried to stop Transport before sending message (connection timeout was already hanging). Also You don't need to run getRealtimeTransport()->start() - it will be started automatically.

Updating a BlackBerry application installed on a user's device

In a situation where a BlackBerry application is installed to a user's device via OTA (BIS), and that application has a "Check for updates" button, one simple approach would be to launch the browser with the address of the .jad file which would then present the user with the "You have version 1.0 installed, would you like to download and install version 1.1?" dialog. But even if there are no updates, the user would get the "You have 1.0, would you like to replace it with 1.0 dialog", which is a hassle and is meaningless.
Is there an better method for doing this in a more seamless manner? For example, are there accepted ways for the application to check the server for an update (with user's permission), inform the user if an update is available, and install the update OTA without going through the browser/jad/ota/replace/restart device loop?
Targeting RIM OS 4.1+
Thank you.
One way would be to fetch the JAD file using an HTTP connection in your app, parse for the version available on the server and only launch the browser if there is a newer version available, or after additionally asking the user if the upgrade is desired.
There are elements of the API that would allow you to also fetch the COD file(s) and install the modules yourself, but that seems like just increasing potential bug space unless you really need to avoid using the Browser OTA install.
A similar method but one that I find a bit better than Richard's thought above because the client does not need a hard-coded JAD path this way (important since JAD files may differ for different BB OS versions):
create a simple web page (php, jsp, servlet, cgi, whatever) that accepts app name and current app version as input; if you need it, also include OS version in the input.
This URL will be constructed by the client by obtaining the appropriate data (details below) and appending it to the known base URL.
the web page will parse the information, and calculate the proper version to run.
Note that you might not need all of the information above: if you only have one downloadable version of your app, you would really only need the device to send the client software version and nothing else. The calculation of proper version can be a simple hard-coded check (if ($version != LATEST_VERSION)) or something more complex, involving lookup into a database or elsewhere.
This page will output plain text, non-HTML. It will write three values, one per line:
"y" if an update is required , "n" if not.
The current-most version for this client to use. This is only necessary if you want the client to display it.
the download URL for the correct JAD.
The client application will parse that data, and if the first flag is "Y" will display message "The current version is (contents of second line). Would you like to update?" When update is selected, it will launch the URL provided in the third line.
Reference
Obtaining Application Version
import net.rim.device.api.system.ApplicationDescriptor;
...
// Returns current app version in the format Major.Minor.Minor.Build, eg 1.5.1.123
String version = ApplicationDescriptor.currentApplicationDescriptor().getVersion();
Obtaining Hardware and Platform Info
import net.rim.device.api.system.ApplicationDescriptor;
...
// Obtain the platform version string in the format A.B.C.DDD, eg 5.0.0.464
String softwareVersion = DeviceInfo.getSoftwareVersion();
// Obtain the hardware name:
String hardwareName = DeviceInfo.getDeviceName();
Launch HTTP URL
import net.rim.blackberry.api.browser.Browser;
Browser.getDefaultSession().displayPage("http://example.com");
Read HTTP file
String url = "full/url/assembled/with/data/above"
// YOU assemble "url" value - and include more error handling than is here in this sample:
HttpConnection conn;
try {
conn = ConnectionHelper.getHttpConnection(url);
LineInputStream stream = new LineInputStream(conn.openInputStream());
String lineOneYesNo = stream.readLine(true);
String lineTwoCurrentVersion = stream.readLine(true))
String lineThreeDownloadURL = stream.readLine(true))
// ***
// * Parse the data above and handle as described.
// ***
return data;
} catch (IOException e) {
// Add appropriate erorro handling here
return;
}
getHttpConnection Implementation
public static HttpConnection getHttpConnection(String URL) throws IOException {
HttpConnection c = null;
StringBuffer conn = new StringBuffer(URL);
// *** IMPORTANT ***
// YOU must define this method below, as it will append
// values to the connection string based on connection
// type (MDS, TCP, WIFI, WAP2, etc)
//
configureConnectionString(conn);
c = (HttpConnection) Connector.open(conn.toString());
int rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP Error: " + rc);
}
return c;
}
Reference: Simple LineInputStream implementation
http://svn.bbssh.org/trunk/BBSSH_Common/src/org/bbssh/io/LineInputStream.java
Sample Input URL 1
This URL is constructed by the client and sent to the server:
http://example.com/versioncheck.do/app-name/hardware-name/os-version/app-version
e.g. http://example.com/versioncheck.do/MyApplication/Bold9000/5.0.466/1.5.1.0
Sample Input URL 2
Alternative format for the same thing:
http://example.com/versioncheck.php?appName=A&hardwareName=B&osVersion=C&appVersion=D
e.g. http://example.com/versioncheck.php?appName=?MyApplication&hardwareName=Bold9000?osVersion=5.0.466&appVersion=1.5.1.0
Sample Output
y
1.3.1.125
http://example.com/ota/5.0.0/MyApp.jad

Resources