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    /** The path of the JSP used to generate the XHTML for the content (which is used to generate the PDF). */
070    private String m_format;
071
072    /** The locale attribute. */
073    private String m_locale;
074
075    /** The map of parameters. */
076    private SortedMap<String, String> m_parameters;
077
078    /** Parameter encoding. */
079    private String m_paramEncoding;
080
081    /**
082     * The implementation of the tag.<p>
083     *
084     * @param request the current request
085     * @param format the format path
086     * @param content the content path
087     * @param localeStr the name of the locale to include in the PDF link
088     * @param params map of parameters
089     * @param paramEncoding the character encoding to use for URL parameters
090     *
091     * @return the link to the PDF
092     *
093     * @throws CmsException if something goes wrong
094     */
095    public static String pdfTagAction(
096        ServletRequest request,
097        String format,
098        String content,
099        String localeStr,
100        SortedMap<String, String> params,
101        String paramEncoding)
102    throws CmsException {
103
104        CmsFlexController controller = CmsFlexController.getController(request);
105        CmsObject cms = OpenCms.initCmsObject(controller.getCmsObject());
106        if (localeStr != null) {
107            Locale localeObj = CmsLocaleManager.getLocale(localeStr);
108            cms.getRequestContext().setLocale(localeObj);
109        }
110        CmsResource formatterRes = cms.readResource(format);
111        CmsResource contentRes = cms.readResource(content, CmsResourceFilter.ignoreExpirationOffline(cms));
112        CmsPdfLink pdfLink = new CmsPdfLink(cms, formatterRes, contentRes);
113        StringBuilder paramBuf = new StringBuilder();
114        if ((params != null) && !params.isEmpty()) {
115            paramBuf.append("?");
116            List<String> paramList = new ArrayList<>();
117            for (Map.Entry<String, String> entry : params.entrySet()) {
118                try {
119                    paramList.add(
120                        URLEncoder.encode(entry.getKey(), paramEncoding)
121                            + "="
122                            + URLEncoder.encode(entry.getValue(), paramEncoding));
123                } catch (UnsupportedEncodingException e) {
124                    LOG.error(e.getLocalizedMessage(), e);
125                }
126            }
127            paramBuf.append(CmsStringUtil.listAsString(paramList, "&amp;"));
128        }
129        return pdfLink.getLink() + paramBuf.toString();
130    }
131
132    /**
133     * @see org.opencms.jsp.I_CmsJspTagParamParent#addParameter(java.lang.String, java.lang.String)
134     */
135    public void addParameter(String name, String value) {
136
137        m_parameters.put(name, value);
138
139    }
140
141    /**
142     * @see javax.servlet.jsp.tagext.BodyTagSupport#doEndTag()
143     */
144    @Override
145    public int doEndTag() {
146
147        try {
148            pageContext.getOut().print(
149                pdfTagAction(
150                    pageContext.getRequest(),
151                    m_format,
152                    m_content,
153                    m_locale,
154                    m_parameters,
155                    getParamEncoding()));
156        } catch (Exception e) {
157            throw new RuntimeException(e);
158        }
159        return EVAL_PAGE;
160    }
161
162    /**
163     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
164     */
165    @Override
166    public int doStartTag() {
167
168        m_parameters = new TreeMap<>();
169        return EVAL_BODY_BUFFERED;
170    }
171
172    /**
173     * Setter for the content path.<p>
174     *
175     * @param content the content path
176     */
177    public void setContent(String content) {
178
179        m_content = content;
180    }
181
182    /**
183     * Setter for the format path.<p>
184     *
185     * @param format the format path
186     */
187    public void setFormat(String format) {
188
189        m_format = format;
190    }
191
192    /**
193     * Sets the locale to use for the PDF link.<p>
194     *
195     * @param locale the locale to use
196     */
197    public void setLocale(String locale) {
198
199        m_locale = locale;
200    }
201
202    /**
203     * Sets the parameter encoding.
204     *
205     * @param encoding the parameter encoding
206     */
207    public void setParamEncoding(String encoding) {
208
209        m_paramEncoding = encoding;
210    }
211
212    /**
213     * Gets the parameter encoding.
214     *
215     * @return the parameter encoding
216     */
217    private String getParamEncoding() {
218
219        if (m_paramEncoding != null) {
220            return m_paramEncoding;
221        }
222        return "UTF-8";
223    }
224
225}