PortletResponseUtil, where were you hiding?

Posted on 20/09/2016 by Charalampos Chrysikopoulos

It is strange, but even after all these years, there are moments that I wonder, how could I miss this and that in the liferay world.

A fine example for this case is the class PortletResponseUtil. In the method serveResource in a custom portlet you can serve binary content (or text). So if you want to serve a file, from documents and media, for example, then you do it by implementing this method.

In the OutputStream of the response you must stream the content and set in the headers the mime type of the file, and also the file name. So, all this is not necessary, because the class PortletResponseUtil is doing it for you:

                DLFileEntry dlFileEntry = null;
                try {
                    dlFileEntry =
                            uuid, themeDisplay.getScopeGroupId());
                        resourceRequest, resourceResponse, dlFileEntry.getName(),
                        dlFileEntry.getContentStream(), dlFileEntry.getMimeType());
                catch (PortalException e) {
                    _log.error(e.getMessage(), e);
                catch (SystemException e) {
                    _log.error(e.getMessage(), e);
This entry was posted in Liferay and tagged Liferay, Utility by Charalampos Chrysikopoulos

Add new comment