コード例 #1
0
  @Override
  public void serveResource(
      StrutsPortletAction originalStrutsPortletAction,
      PortletConfig portletConfig,
      ResourceRequest resourceRequest,
      ResourceResponse resourceResponse) {

    long entryId = ParamUtil.getLong(resourceRequest, "entryId");
    ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);

    BlogsEntry entry;
    try {
      entry = BlogsEntryServiceUtil.getEntry(entryId);
      StringBundler sb = new StringBundler();

      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(entry.getContent());
      sb.append("</body>");
      sb.append("</html>");

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

      destinationFile = DocumentConversionUtil.convert(entry.getEntryId() + "", is, "html", "pdf");
      OutputStream out = resourceResponse.getPortletOutputStream();
      InputStream in = new FileInputStream(destinationFile);
      HttpServletResponse httpRes = PortalUtil.getHttpServletResponse(resourceResponse);
      HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(resourceRequest);
      String contentType = MimeTypesUtil.getContentType(destinationFile);
      ServletResponseUtil.sendFile(httpReq, httpRes, destinationFile.getName(), in, contentType);
      out.close();

    } catch (PortalException e) {
      System.out.println("PortalException : " + getClass().getName() + "\n" + e);
      // e.printStackTrace();
    } catch (SystemException e) {
      System.out.println("SystemException : " + getClass().getName() + "\n" + e);
      // e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      System.out.println("UnsupportedEncodingException : " + getClass().getName() + "\n" + e);
      // e.printStackTrace();
    } catch (Exception e) {
      System.out.println("Exception : " + getClass().getName() + "\n" + e);
      // e.printStackTrace();
    }
  }