001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.pdftools; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.jsp.util.CmsJspStandardContextBean; 033import org.opencms.loader.CmsTemplateLoaderFacade; 034import org.opencms.main.OpenCms; 035import org.opencms.xml.containerpage.CmsContainerElementBean; 036 037import java.util.Collections; 038 039import javax.servlet.http.HttpServletRequest; 040import javax.servlet.http.HttpServletResponse; 041 042/** 043 * Utility class for PDF formatting.<p> 044 */ 045public final class CmsPdfFormatterUtils { 046 047 /** 048 * Private constructor to prevent instantiation.<p> 049 */ 050 private CmsPdfFormatterUtils() { 051 052 // do nothing 053 } 054 055 /** 056 * Executes a JSP with a given content as input and returns the output of the JSP.<p> 057 * 058 * @param cms the current CMS context 059 * @param request the current request 060 * @param response the current response 061 * @param jsp the jsp resource to execute 062 * @param content the content to render with the JSP 063 * 064 * @return the output of the JSP 065 * @throws Exception if something goes wrong 066 */ 067 public static byte[] executeJsp( 068 CmsObject cms, 069 HttpServletRequest request, 070 HttpServletResponse response, 071 CmsResource jsp, 072 CmsResource content) throws Exception { 073 074 CmsTemplateLoaderFacade loaderFacade = new CmsTemplateLoaderFacade( 075 OpenCms.getResourceManager().getLoader(jsp), 076 content, 077 jsp); 078 CmsResource loaderRes = loaderFacade.getLoaderStartResource(); 079 request.setAttribute(CmsJspStandardContextBean.ATTRIBUTE_CMS_OBJECT, cms); 080 CmsJspStandardContextBean context = CmsJspStandardContextBean.getInstance(request); 081 CmsContainerElementBean element = new CmsContainerElementBean( 082 content.getStructureId(), 083 jsp.getStructureId(), 084 Collections.<String, String> emptyMap(), 085 false); 086 context.setElement(element); 087 return loaderFacade.getLoader().dump( 088 cms, 089 loaderRes, 090 null, 091 cms.getRequestContext().getLocale(), 092 request, 093 response); 094 } 095 096}