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.main.CmsException; 033import org.opencms.main.CmsLog; 034import org.opencms.main.OpenCms; 035import org.opencms.staticexport.CmsDefaultLinkSubstitutionHandler; 036import org.opencms.util.CmsStringUtil; 037import org.opencms.util.CmsUUID; 038 039import java.util.Map; 040import java.util.regex.Matcher; 041import java.util.regex.Pattern; 042 043import org.apache.commons.logging.Log; 044 045/** 046 * Class to generate or parse a link to a PDF thumbnail.<p> 047 */ 048public class CmsPdfThumbnailLink { 049 050 /** 051 * Exception which is thrown when parsing a thumbnail link fails.<p> 052 */ 053 public static class ParseException extends Exception { 054 055 /** Serial version id. */ 056 private static final long serialVersionUID = 1L; 057 058 /** 059 * Creates a new instance.<p> 060 * 061 * @param message the exception message 062 */ 063 public ParseException(String message) { 064 065 super(message); 066 } 067 068 } 069 070 /** The marker string used to identify PDF thumbnails. */ 071 public static final String MARKER = "pdfthumbnail"; 072 073 /** The name of the request parameter for the thumbnail options. */ 074 public static final String PARAM_OPTIONS = "options"; 075 076 /** The name of the reqex for parsing the thumbnail URI path. */ 077 public static final String REGEX = MARKER + "/(" + CmsUUID.UUID_REGEX + ")\\.(jpg|png|gif)$"; 078 079 /** The compiled regular expression for matching thumbnail URI paths. */ 080 public static final Pattern REGEX_COMPILED = Pattern.compile(REGEX); 081 082 /** The logger instance for this class. */ 083 private static final Log LOG = CmsLog.getLog(CmsPdfThumbnailLink.class); 084 085 /** The image format. */ 086 private String m_format; 087 088 /** The height. */ 089 private int m_height = -1; 090 091 /** The thumbnail URI path. */ 092 private String m_link; 093 094 /** The options parameter value. */ 095 private String m_options; 096 097 /** The page number. */ 098 private int m_page = 0; 099 100 /** The PDF resource. */ 101 private CmsResource m_pdfResource; 102 103 /** The width. */ 104 private int m_width = -1; 105 106 /** 107 * Creates a new thumbnail link for the given resource and options.<p> 108 * 109 * @param cms the current CMS context 110 * @param pdfResource the PDF resource for which to create a thumbnail link 111 * @param width thumbnail width 112 * @param height thumbnail height 113 * @param format the thumbnail image format (png, gif..,) 114 */ 115 public CmsPdfThumbnailLink(CmsObject cms, CmsResource pdfResource, int width, int height, String format) { 116 117 m_pdfResource = pdfResource; 118 m_width = width; 119 m_height = height; 120 m_format = format.toLowerCase(); 121 m_options = "w:" + m_width + ",h:" + m_height; 122 cms.getRequestContext().setAttribute(CmsDefaultLinkSubstitutionHandler.ATTR_IS_IMAGE_LINK, "true"); 123 try { 124 m_link = OpenCms.getLinkManager().substituteLink( 125 cms, 126 "/" + MARKER + "/" + pdfResource.getStructureId() + "." + format); 127 } finally { 128 cms.getRequestContext().removeAttribute(CmsDefaultLinkSubstitutionHandler.ATTR_IS_IMAGE_LINK); 129 } 130 131 } 132 133 /** 134 * Parses a thumbnail link object from the given link path and options.<p> 135 * 136 * @param cms the current CMS context 137 * @param link the link 138 * @param options the options 139 * 140 * @throws ParseException 141 * @throws CmsException 142 */ 143 public CmsPdfThumbnailLink(CmsObject cms, String link, String options) 144 throws ParseException, CmsException { 145 146 m_link = link; 147 Map<String, String> optionMap = CmsStringUtil.splitAsMap(options, ",", ":"); 148 Matcher matcher = REGEX_COMPILED.matcher(link); 149 if (!matcher.find()) { 150 throw new ParseException("Link " + link + " does not match pattern"); 151 } 152 String uuidStr = matcher.group(1); 153 m_format = matcher.group(2); 154 m_options = options; 155 CmsUUID uuid = new CmsUUID(uuidStr); 156 m_pdfResource = cms.readResource(uuid); 157 try { 158 String widthStr = optionMap.get("w"); 159 m_width = Integer.parseInt(widthStr); 160 } catch (Exception e) { 161 LOG.info(e.getLocalizedMessage(), e); 162 } 163 164 try { 165 String heightStr = optionMap.get("h"); 166 m_height = Integer.parseInt(heightStr); 167 } catch (Exception e) { 168 LOG.info(e.getLocalizedMessage(), e); 169 } 170 171 try { 172 String pageStr = optionMap.get("page"); 173 m_page = Integer.parseInt(pageStr); 174 } catch (Exception e) { 175 LOG.info(e.getLocalizedMessage(), e); 176 } 177 } 178 179 /** 180 * Gets the image format.<p> 181 * 182 * @return the image format 183 */ 184 public String getFormat() { 185 186 return m_format; 187 } 188 189 /** 190 * Returns the height.<p> 191 * 192 * @return the height 193 */ 194 public int getHeight() { 195 196 return m_height; 197 } 198 199 /** 200 * Gets the link, with the options appended as a request parameter.<p> 201 * 202 * @return the link with the options 203 */ 204 public String getLinkWithOptions() { 205 206 return m_link + "?" + PARAM_OPTIONS + "=" + m_options; 207 } 208 209 /** 210 * Returns the page.<p> 211 * 212 * @return the page 213 */ 214 public int getPage() { 215 216 return m_page; 217 } 218 219 /** 220 * Returns the width.<p> 221 * 222 * @return the width 223 */ 224 public int getWidth() { 225 226 return m_width; 227 } 228 229 /** 230 * Gets the PDF resource.<p> 231 * 232 * @return the PDF resource 233 */ 234 CmsResource getPdfResource() { 235 236 return m_pdfResource; 237 } 238 239}