org.opencms.pdftools.CmsPdfLink Maven / Gradle / Ivy
Show all versions of opencms-core Show documentation
/*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.pdftools;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsVfsResourceNotFoundException;
import org.opencms.i18n.CmsLocaleManager;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsUUID;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This class is responsbile for creating and parsing links to generated PDFs.
*/
public class CmsPdfLink {
/**
* Exception which is thrown when parsing a link as a PDF link fails.
*
* @param cms the current CMS context
* @param formatter the formatter resource
* @param content the content resource
* @param filename the file name to use for the PDF download link
*
* @throws CmsException if something goes wrong
*/
public CmsPdfLink(CmsObject cms, CmsResource formatter, CmsResource content, String filename)
throws CmsException {
Locale locale = cms.getRequestContext().getLocale();
m_content = content;
m_locale = locale;
String detailName = cms.getDetailName(
content,
cms.getRequestContext().getLocale(),
OpenCms.getLocaleManager().getDefaultLocales());
String s = "/"
+ PDF_LINK_PREFIX
+ "/"
+ locale
+ "/"
+ formatter.getStructureId()
+ "/"
+ detailName
+ (filename != null ? "/" + filename : "")
+ ".pdf";
m_link = OpenCms.getLinkManager().substituteLink(cms, s);
}
/**
* Creates a PDF link object by parsing it from a link string.
*
* @param cms the current CMS context
* @param link the link as a string
*
* @throws CmsPdfLinkParseException if the given link is not a PDF link
* @throws CmsException if something else goes wrong
*/
public CmsPdfLink(CmsObject cms, String link)
throws CmsPdfLinkParseException, CmsException {
Matcher matcher = PDF_LINK_REGEX_COMPILED.matcher(link);
m_link = link;
if (matcher.find()) {
String localeStr = matcher.group(1);
String formatterId = matcher.group(2);
String detailName = matcher.group(3);
CmsUUID id = cms.readIdForUrlName(detailName);
if (id == null) {
throw new CmsVfsResourceNotFoundException(
org.opencms.db.generic.Messages.get().container(
org.opencms.db.generic.Messages.ERR_READ_RESOURCE_1,
detailName));
}
m_content = cms.readResource(id, CmsResourceFilter.ignoreExpirationOffline(cms));
m_locale = CmsLocaleManager.getLocale(localeStr);
m_formatter = cms.readResource(new CmsUUID(formatterId));
} else {
throw new CmsPdfLinkParseException();
}
}
/**
* Returns the content.
*
* @return the content
*/
public CmsResource getContent() {
return m_content;
}
/**
* Gets the formatter resource.
*
* @return the formatter resource
*/
public CmsResource getFormatter() {
return m_formatter;
}
/**
* Returns the link.
*
* @return the link
*/
public String getLink() {
return m_link;
}
/**
* Returns the locale.
*
* @return the locale
*/
public Locale getLocale() {
return m_locale;
}
}