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.file.CmsResourceFilter; 033import org.opencms.file.CmsVfsResourceNotFoundException; 034import org.opencms.i18n.CmsLocaleManager; 035import org.opencms.main.CmsException; 036import org.opencms.main.OpenCms; 037import org.opencms.util.CmsUUID; 038 039import java.util.Locale; 040import java.util.regex.Matcher; 041import java.util.regex.Pattern; 042 043/** 044 * This class is responsbile for creating and parsing links to generated PDFs.<p> 045 */ 046public class CmsPdfLink { 047 048 /** 049 * Exception which is thrown when parsing a link as a PDF link fails.<p< 050 */ 051 public static class CmsPdfLinkParseException extends Exception { 052 053 /** Serial version id. */ 054 private static final long serialVersionUID = 1L; 055 056 } 057 058 /** Group of characters without slashes. */ 059 public static final String NOSLASH_GROUP = "([^/]+)"; 060 061 /** The prefix string for PDF links. */ 062 public static final String PDF_LINK_PREFIX = "pdflink"; 063 064 /** Regular expression for parsing PDF links. */ 065 public static final String PDF_LINK_REGEX = PDF_LINK_PREFIX 066 + "/" 067 + NOSLASH_GROUP 068 + "/" 069 + "(" 070 + CmsUUID.UUID_REGEX 071 + ")" 072 + "/" 073 + NOSLASH_GROUP 074 + "(?:/[^/]+)?" // optional filename to make the download name more user-readable, not used by the handler 075 + "\\.pdf/?"; 076 077 /** Compiled regular expression for parsing PDF links. */ 078 public static final Pattern PDF_LINK_REGEX_COMPILED = Pattern.compile(PDF_LINK_REGEX); 079 080 /** The content resource of the PDF link. */ 081 private CmsResource m_content; 082 083 /** The formatter resource of the PDF link. */ 084 private CmsResource m_formatter; 085 086 /** The PDF link. */ 087 private String m_link; 088 089 /** The locale of the PDF link. */ 090 private Locale m_locale; 091 092 /** 093 * Creates a new PDF link object based on the formatter and content resources and the locale of the current CMS context.<p> 094 * 095 * @param cms the current CMS context 096 * @param formatter the formatter resource 097 * @param content the content resource 098 * @param filename the file name to use for the PDF download link 099 * 100 * @throws CmsException if something goes wrong 101 */ 102 public CmsPdfLink(CmsObject cms, CmsResource formatter, CmsResource content, String filename) 103 throws CmsException { 104 105 Locale locale = cms.getRequestContext().getLocale(); 106 m_content = content; 107 m_locale = locale; 108 String detailName = cms.getDetailName( 109 content, 110 cms.getRequestContext().getLocale(), 111 OpenCms.getLocaleManager().getDefaultLocales()); 112 String s = "/" 113 + PDF_LINK_PREFIX 114 + "/" 115 + locale 116 + "/" 117 + formatter.getStructureId() 118 + "/" 119 + detailName 120 + (filename != null ? "/" + filename : "") 121 + ".pdf"; 122 m_link = OpenCms.getLinkManager().substituteLink(cms, s); 123 } 124 125 /** 126 * Creates a PDF link object by parsing it from a link string.<p> 127 * 128 * @param cms the current CMS context 129 * @param link the link as a string 130 * 131 * @throws CmsPdfLinkParseException if the given link is not a PDF link 132 * @throws CmsException if something else goes wrong 133 */ 134 public CmsPdfLink(CmsObject cms, String link) 135 throws CmsPdfLinkParseException, CmsException { 136 137 Matcher matcher = PDF_LINK_REGEX_COMPILED.matcher(link); 138 m_link = link; 139 if (matcher.find()) { 140 String localeStr = matcher.group(1); 141 String formatterId = matcher.group(2); 142 String detailName = matcher.group(3); 143 CmsUUID id = cms.readIdForUrlName(detailName); 144 if (id == null) { 145 throw new CmsVfsResourceNotFoundException( 146 org.opencms.db.generic.Messages.get().container( 147 org.opencms.db.generic.Messages.ERR_READ_RESOURCE_1, 148 detailName)); 149 } 150 m_content = cms.readResource(id, CmsResourceFilter.ignoreExpirationOffline(cms)); 151 m_locale = CmsLocaleManager.getLocale(localeStr); 152 m_formatter = cms.readResource(new CmsUUID(formatterId)); 153 } else { 154 throw new CmsPdfLinkParseException(); 155 } 156 } 157 158 /** 159 * Returns the content.<p> 160 * 161 * @return the content 162 */ 163 public CmsResource getContent() { 164 165 return m_content; 166 } 167 168 /** 169 * Gets the formatter resource.<p> 170 * 171 * @return the formatter resource 172 */ 173 public CmsResource getFormatter() { 174 175 return m_formatter; 176 } 177 178 /** 179 * Returns the link.<p> 180 * 181 * @return the link 182 */ 183 public String getLink() { 184 185 return m_link; 186 } 187 188 /** 189 * Returns the locale.<p> 190 * 191 * @return the locale 192 */ 193 public Locale getLocale() { 194 195 return m_locale; 196 } 197}