Example usage for com.liferay.portal.kernel.portlet PortletRequestModel PortletRequestModel

List of usage examples for com.liferay.portal.kernel.portlet PortletRequestModel PortletRequestModel

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.portlet PortletRequestModel PortletRequestModel.

Prototype

public PortletRequestModel(PortletRequest portletRequest, PortletResponse portletResponse) 

Source Link

Usage

From source file:com.liferay.journal.web.util.ExportArticleUtil.java

License:Open Source License

public void sendFile(String targetExtension, PortletRequest portletRequest, PortletResponse portletResponse)
        throws IOException {

    if (Validator.isNull(targetExtension)) {
        return;/*from  w w  w .j a  v  a 2s .c o  m*/
    }

    long groupId = ParamUtil.getLong(portletRequest, "groupId");
    String articleId = ParamUtil.getString(portletRequest, "articleId");

    String languageId = LanguageUtil.getLanguageId(portletRequest);
    PortletRequestModel portletRequestModel = new PortletRequestModel(portletRequest, portletResponse);
    ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);
    HttpServletRequest request = _portal.getHttpServletRequest(portletRequest);
    HttpServletResponse response = _portal.getHttpServletResponse(portletResponse);

    JournalArticleDisplay articleDisplay = _journalContent.getDisplay(groupId, articleId, null, "export",
            languageId, 1, portletRequestModel, themeDisplay);

    int pages = articleDisplay.getNumberOfPages();

    StringBundler sb = new StringBundler(pages + 12);

    sb.append("<html>");

    sb.append("<head>");
    sb.append("<meta content=\"");
    sb.append(ContentTypes.TEXT_HTML_UTF8);
    sb.append("\" http-equiv=\"content-type\" />");
    sb.append("<base href=\"");
    sb.append(themeDisplay.getPortalURL());
    sb.append("\" />");
    sb.append("</head>");

    sb.append("<body>");

    sb.append(articleDisplay.getContent());

    for (int i = 2; i <= pages; i++) {
        articleDisplay = _journalContent.getDisplay(groupId, articleId, "export", languageId, i, themeDisplay);

        sb.append(articleDisplay.getContent());
    }

    sb.append("</body>");
    sb.append("</html>");

    InputStream is = new UnsyncByteArrayInputStream(sb.toString().getBytes(StringPool.UTF8));

    String title = articleDisplay.getTitle();
    String sourceExtension = "html";

    String fileName = title.concat(StringPool.PERIOD).concat(sourceExtension);

    String contentType = ContentTypes.TEXT_HTML;

    String id = DLUtil.getTempFileId(articleDisplay.getId(), String.valueOf(articleDisplay.getVersion()),
            languageId);

    File convertedFile = DocumentConversionUtil.convert(id, is, sourceExtension, targetExtension);

    if (convertedFile != null) {
        targetExtension = StringUtil.toLowerCase(targetExtension);

        fileName = title.concat(StringPool.PERIOD).concat(targetExtension);

        contentType = MimeTypesUtil.getContentType(fileName);

        is = new FileInputStream(convertedFile);
    }

    ServletResponseUtil.sendFile(request, response, fileName, is, contentType);
}