PortletResponseUtil, where were you hiding?
Posted on
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:
``` java
DLFileEntry dlFileEntry = null;
try {
dlFileEntry =
DLFileEntryLocalServiceUtil.getDLFileEntryByUuidAndGroupId(
uuid, themeDisplay.getScopeGroupId());
PortletResponseUtil.sendFile(
resourceRequest, resourceResponse, dlFileEntry.getName(),
dlFileEntry.getContentStream(), dlFileEntry.getMimeType());
}
catch (PortalException e) {
_log.error(e.getMessage(), e);
}
catch (SystemException e) {
_log.error(e.getMessage(), e);
}
```