MongoDB - GridFS: SocketException when returning file content in Controller

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

MongoDB - GridFS: SocketException when returning file content in Controller

Steve Hummingbird
I am trying to return a file in a controller, which I have returned from GridFS.
This is the relevant code from my controller:


        def download(String versionId) {

                GridFSDBFile dbFile = versionService.getFile(versionId)
                response.setContentType(dbFile.getContentType())
                response.setHeader("Content-disposition", "attachment;filename=${dbFile.getFilename()}")
                response.outputStream << dbFile.inputStream
        }


The service looks like this:

        def getFile(ObjectId id) {
                return gridfs.findOne(id)
        }

This works well most of the time. However, I discovered that when I use Safari on an iPhone to call my Service, I randomly see the following Exception in my log. The file still downloads fine.
Any ideas how I could work around this or what I might be doing wrong?

Thanks,
Steve


 Error 2013-09-02 21:32:16,362 [http-bio-8080-exec-26] ERROR errors.GrailsExceptionResolver  - SocketException occurred when processing request: [GET] /App/file/522348d4036417c84d65d9b4/version/522348d4036417c84d65d9b5
Broken pipe. Stacktrace follows:
Message: Broken pipe
   Line | Method
->>  92 | socketWrite in java.net.SocketOutputStream
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   136 | write       in     ''
|   168 | download .  in org.app.core.VersionController$$EOGJLVJw
|   895 | runTask     in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run . . . . in     ''
^   680 | run         in java.lang.Thread
| Error 2013-09-02 21:32:16,372 [http-bio-8080-exec-26] ERROR errors.GrailsExceptionResolver  - IllegalStateException occurred when processing request: [GET] /App/file/522348d4036417c84d65d9b4/version/522348d4036417c84d65d9b5
getOutputStream() has already been called for this response. Stacktrace follows:
Message: Error processing GroovyPageView: getOutputStream() has already been called for this response
   Line | Method
->> 639 | runTask in /grails-app/views/error.gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Caused by IllegalStateException: getOutputStream() has already been called for this response
->>  14 | run     in Users_steve_Projects__App_grails_app_views_error_gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run . . in     ''
^   680 | run     in java.lang.Thread
| Error 2013-09-02 21:32:16,375 [http-bio-8080-exec-26] ERROR [/App].[grails]  - Servlet.service() for servlet grails threw exception
Message: getOutputStream() has already been called for this response
   Line | Method
->>  14 | run     in Users_steve_Projects__App_grails_app_views_error_gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run . . in     ''
^   680 | run     in java.lang.Thread
| Error 2013-09-02 21:32:16,377 [http-bio-8080-exec-26] ERROR [/App].[default]  - Servlet.service() for servlet [default] in context with path [/App] threw exception [Request processing failed; nested exception is org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: getOutputStream() has already been called for this response] with root cause
Message: getOutputStream() has already been called for this response
   Line | Method
->>  14 | run     in Users_steve_Projects__App_grails_app_views_error_gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run . . in     ''
^   680 | run     in java.lang.Thread
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: MongoDB - GridFS: SocketException when returning file content in Controller

xunitc
Hi.
I got this exception before.
Please try this:
1. Add "response.contentLength = dbFile.length" in your code.
2. Add "return;" at the end.

Hope it can help you.

2013/9/3 Steve Hummingbird <[hidden email]>:

> I am trying to return a file in a controller, which I have returned from GridFS.
> This is the relevant code from my controller:
>
>
>         def download(String versionId) {
>
>                 GridFSDBFile dbFile = versionService.getFile(versionId)
>                 response.setContentType(dbFile.getContentType())
>                 response.setHeader("Content-disposition", "attachment;filename=${dbFile.getFilename()}")
>                 response.outputStream << dbFile.inputStream
>         }
>
>
> The service looks like this:
>
>         def getFile(ObjectId id) {
>                 return gridfs.findOne(id)
>         }
>
> This works well most of the time. However, I discovered that when I use Safari on an iPhone to call my Service, I randomly see the following Exception in my log. The file still downloads fine.
> Any ideas how I could work around this or what I might be doing wrong?
>
> Thanks,
> Steve
>
>
>  Error 2013-09-02 21:32:16,362 [http-bio-8080-exec-26] ERROR errors.GrailsExceptionResolver  - SocketException occurred when processing request: [GET] /App/file/522348d4036417c84d65d9b4/version/522348d4036417c84d65d9b5
> Broken pipe. Stacktrace follows:
> Message: Broken pipe
>    Line | Method
> ->>  92 | socketWrite in java.net.SocketOutputStream
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> |   136 | write       in     ''
> |   168 | download .  in org.app.core.VersionController$$EOGJLVJw
> |   895 | runTask     in java.util.concurrent.ThreadPoolExecutor$Worker
> |   918 | run . . . . in     ''
> ^   680 | run         in java.lang.Thread
> | Error 2013-09-02 21:32:16,372 [http-bio-8080-exec-26] ERROR errors.GrailsExceptionResolver  - IllegalStateException occurred when processing request: [GET] /App/file/522348d4036417c84d65d9b4/version/522348d4036417c84d65d9b5
> getOutputStream() has already been called for this response. Stacktrace follows:
> Message: Error processing GroovyPageView: getOutputStream() has already been called for this response
>    Line | Method
> ->> 639 | runTask in /grails-app/views/error.gsp
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>
> Caused by IllegalStateException: getOutputStream() has already been called for this response
> ->>  14 | run     in Users_steve_Projects__App_grails_app_views_error_gsp
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> |   895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
> |   918 | run . . in     ''
> ^   680 | run     in java.lang.Thread
> | Error 2013-09-02 21:32:16,375 [http-bio-8080-exec-26] ERROR [/App].[grails]  - Servlet.service() for servlet grails threw exception
> Message: getOutputStream() has already been called for this response
>    Line | Method
> ->>  14 | run     in Users_steve_Projects__App_grails_app_views_error_gsp
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> |   895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
> |   918 | run . . in     ''
> ^   680 | run     in java.lang.Thread
> | Error 2013-09-02 21:32:16,377 [http-bio-8080-exec-26] ERROR [/App].[default]  - Servlet.service() for servlet [default] in context with path [/App] threw exception [Request processing failed; nested exception is org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: getOutputStream() has already been called for this response] with root cause
> Message: getOutputStream() has already been called for this response
>    Line | Method
> ->>  14 | run     in Users_steve_Projects__App_grails_app_views_error_gsp
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> |   895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
> |   918 | run . . in     ''
> ^   680 | run     in java.lang.Thread
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>



--
Xunitc.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Xunitc
Reply | Threaded
Open this post in threaded view
|

Re: MongoDB - GridFS: SocketException when returning file content in Controller

Steve Hummingbird
Thanks, but unfortunately the exception is still showing up. I can see the content-length header being returned properly in the response (see below) after adding the line you were suggesting (which is a good thing to do nonetheless).
I have added the return statement as well, but I guess that is something that shouldn't be necessary, unless I misunderstood something.

        • Response Headersview source
                • Cache-Control:private
                • Content-disposition:attachment;filename=ABC.json
                • Content-Length:3310246
                • Content-Type:application/json
                • Date:Mon, 02 Sep 2013 22:29:30 GMT
                • Expires:Thu, 01 Jan 1970 01:00:00 CET
                • Server:Apache-Coyote/1.1



 
On Sep 3, 2013, at 12:05 AM, xunitc wrote:

> Hi.
> I got this exception before.
> Please try this:
> 1. Add "response.contentLength = dbFile.length" in your code.
> 2. Add "return;" at the end.
>
> Hope it can help you.
>
> 2013/9/3 Steve Hummingbird <[hidden email]>:
>> I am trying to return a file in a controller, which I have returned from GridFS.
>> This is the relevant code from my controller:
>>
>>
>>        def download(String versionId) {
>>
>>                GridFSDBFile dbFile = versionService.getFile(versionId)
>>                response.setContentType(dbFile.getContentType())
>>                response.setHeader("Content-disposition", "attachment;filename=${dbFile.getFilename()}")
>>                response.outputStream << dbFile.inputStream
>>        }
>>
>>
>> The service looks like this:
>>
>>        def getFile(ObjectId id) {
>>                return gridfs.findOne(id)
>>        }
>>
>> This works well most of the time. However, I discovered that when I use Safari on an iPhone to call my Service, I randomly see the following Exception in my log. The file still downloads fine.
>> Any ideas how I could work around this or what I might be doing wrong?
>>
>> Thanks,
>> Steve
>>
>>
>> Error 2013-09-02 21:32:16,362 [http-bio-8080-exec-26] ERROR errors.GrailsExceptionResolver  - SocketException occurred when processing request: [GET] /App/file/522348d4036417c84d65d9b4/version/522348d4036417c84d65d9b5
>> Broken pipe. Stacktrace follows:
>> Message: Broken pipe
>>   Line | Method
>> ->>  92 | socketWrite in java.net.SocketOutputStream
>> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>> |   136 | write       in     ''
>> |   168 | download .  in org.app.core.VersionController$$EOGJLVJw
>> |   895 | runTask     in java.util.concurrent.ThreadPoolExecutor$Worker
>> |   918 | run . . . . in     ''
>> ^   680 | run         in java.lang.Thread
>> | Error 2013-09-02 21:32:16,372 [http-bio-8080-exec-26] ERROR errors.GrailsExceptionResolver  - IllegalStateException occurred when processing request: [GET] /App/file/522348d4036417c84d65d9b4/version/522348d4036417c84d65d9b5
>> getOutputStream() has already been called for this response. Stacktrace follows:
>> Message: Error processing GroovyPageView: getOutputStream() has already been called for this response
>>   Line | Method
>> ->> 639 | runTask in /grails-app/views/error.gsp
>> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>>
>> Caused by IllegalStateException: getOutputStream() has already been called for this response
>> ->>  14 | run     in Users_steve_Projects__App_grails_app_views_error_gsp
>> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>> |   895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
>> |   918 | run . . in     ''
>> ^   680 | run     in java.lang.Thread
>> | Error 2013-09-02 21:32:16,375 [http-bio-8080-exec-26] ERROR [/App].[grails]  - Servlet.service() for servlet grails threw exception
>> Message: getOutputStream() has already been called for this response
>>   Line | Method
>> ->>  14 | run     in Users_steve_Projects__App_grails_app_views_error_gsp
>> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>> |   895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
>> |   918 | run . . in     ''
>> ^   680 | run     in java.lang.Thread
>> | Error 2013-09-02 21:32:16,377 [http-bio-8080-exec-26] ERROR [/App].[default]  - Servlet.service() for servlet [default] in context with path [/App] threw exception [Request processing failed; nested exception is org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: getOutputStream() has already been called for this response] with root cause
>> Message: getOutputStream() has already been called for this response
>>   Line | Method
>> ->>  14 | run     in Users_steve_Projects__App_grails_app_views_error_gsp
>> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>> |   895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
>> |   918 | run . . in     ''
>> ^   680 | run     in java.lang.Thread
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>
>
> --
> Xunitc.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email