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.jsp; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.file.CmsResourceFilter; 033import org.opencms.flex.CmsFlexController; 034import org.opencms.i18n.CmsLocaleManager; 035import org.opencms.main.CmsException; 036import org.opencms.main.CmsLog; 037import org.opencms.main.OpenCms; 038import org.opencms.pdftools.CmsPdfLink; 039import org.opencms.util.CmsStringUtil; 040 041import java.io.UnsupportedEncodingException; 042import java.net.URLEncoder; 043import java.util.ArrayList; 044import java.util.List; 045import java.util.Locale; 046import java.util.Map; 047import java.util.SortedMap; 048import java.util.TreeMap; 049 050import javax.servlet.ServletRequest; 051import javax.servlet.jsp.tagext.BodyTagSupport; 052 053import org.apache.commons.logging.Log; 054 055/** 056 * JSP tag to generate a link to a PDF produced from a given XML content.<p> 057 */ 058public class CmsJspTagPdf extends BodyTagSupport implements I_CmsJspTagParamParent { 059 060 /** Logger instance for this class. */ 061 private static final Log LOG = CmsLog.getLog(CmsJspTagPdf.class); 062 063 /** Serial version id. */ 064 private static final long serialVersionUID = 1L; 065 066 /** The path of the content resource for which the PDF link should be generated. */ 067 private String m_content; 068 069 /** File name (optional). */ 070 private String m_filename; 071 072 /** The path of the JSP used to generate the XHTML for the content (which is used to generate the PDF). */ 073 private String m_format; 074 075 /** The locale attribute. */ 076 private String m_locale; 077 078 /** Parameter encoding. */ 079 private String m_paramEncoding; 080 081 /** The map of parameters. */ 082 private SortedMap<String, String> m_parameters; 083 084 /** 085 * The implementation of the tag.<p> 086 * 087 * @param request the current request 088 * @param format the format path 089 * @param content the content path 090 * @param localeStr the name of the locale to include in the PDF link 091 * @param params map of parameters 092 * @param paramEncoding the character encoding to use for URL parameters 093 * 094 * @return the link to the PDF 095 * 096 * @throws CmsException if something goes wrong 097 */ 098 public static String pdfTagAction( 099 ServletRequest request, 100 String format, 101 String content, 102 String localeStr, 103 String filename, 104 SortedMap<String, String> params, 105 String paramEncoding) 106 throws CmsException { 107 108 filename = filename != null ? filename.replaceFirst("\\.pdf$", "") : null; 109 CmsFlexController controller = CmsFlexController.getController(request); 110 CmsObject cms = OpenCms.initCmsObject(controller.getCmsObject()); 111 if (localeStr != null) { 112 Locale localeObj = CmsLocaleManager.getLocale(localeStr); 113 cms.getRequestContext().setLocale(localeObj); 114 } 115 CmsResource formatterRes = cms.readResource(format); 116 CmsResource contentRes = cms.readResource(content, CmsResourceFilter.ignoreExpirationOffline(cms)); 117 CmsPdfLink pdfLink = new CmsPdfLink(cms, formatterRes, contentRes, filename); 118 StringBuilder paramBuf = new StringBuilder(); 119 if ((params != null) && !params.isEmpty()) { 120 paramBuf.append("?"); 121 List<String> paramList = new ArrayList<>(); 122 for (Map.Entry<String, String> entry : params.entrySet()) { 123 try { 124 paramList.add( 125 URLEncoder.encode(entry.getKey(), paramEncoding) 126 + "=" 127 + URLEncoder.encode(entry.getValue(), paramEncoding)); 128 } catch (UnsupportedEncodingException e) { 129 LOG.error(e.getLocalizedMessage(), e); 130 } 131 } 132 paramBuf.append(CmsStringUtil.listAsString(paramList, "&")); 133 } 134 return pdfLink.getLink() + paramBuf.toString(); 135 } 136 137 /** 138 * @see org.opencms.jsp.I_CmsJspTagParamParent#addParameter(java.lang.String, java.lang.String) 139 */ 140 public void addParameter(String name, String value) { 141 142 m_parameters.put(name, value); 143 144 } 145 146 /** 147 * @see javax.servlet.jsp.tagext.BodyTagSupport#doEndTag() 148 */ 149 @Override 150 public int doEndTag() { 151 152 try { 153 pageContext.getOut().print( 154 pdfTagAction( 155 pageContext.getRequest(), 156 m_format, 157 m_content, 158 m_locale, 159 m_filename, 160 m_parameters, 161 getParamEncoding())); 162 } catch (Exception e) { 163 throw new RuntimeException(e); 164 } 165 return EVAL_PAGE; 166 } 167 168 /** 169 * @see javax.servlet.jsp.tagext.Tag#doStartTag() 170 */ 171 @Override 172 public int doStartTag() { 173 174 m_parameters = new TreeMap<>(); 175 return EVAL_BODY_BUFFERED; 176 } 177 178 /** 179 * Setter for the content path.<p> 180 * 181 * @param content the content path 182 */ 183 public void setContent(String content) { 184 185 m_content = content; 186 } 187 188 /** 189 * Sets the file name for the PDF download link. 190 * 191 * @param filename the file name 192 */ 193 public void setFilename(String filename) { 194 195 m_filename = filename; 196 } 197 198 /** 199 * Setter for the format path.<p> 200 * 201 * @param format the format path 202 */ 203 public void setFormat(String format) { 204 205 m_format = format; 206 } 207 208 /** 209 * Sets the locale to use for the PDF link.<p> 210 * 211 * @param locale the locale to use 212 */ 213 public void setLocale(String locale) { 214 215 m_locale = locale; 216 } 217 218 /** 219 * Sets the parameter encoding. 220 * 221 * @param encoding the parameter encoding 222 */ 223 public void setParamEncoding(String encoding) { 224 225 m_paramEncoding = encoding; 226 } 227 228 /** 229 * Gets the parameter encoding. 230 * 231 * @return the parameter encoding 232 */ 233 private String getParamEncoding() { 234 235 if (m_paramEncoding != null) { 236 return m_paramEncoding; 237 } 238 return "UTF-8"; 239 } 240 241}