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.ui.apps; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.file.CmsResourceFilter; 033import org.opencms.file.CmsVfsResourceNotFoundException; 034import org.opencms.main.CmsException; 035import org.opencms.main.CmsLog; 036import org.opencms.main.OpenCms; 037import org.opencms.site.CmsSite; 038import org.opencms.util.CmsStringUtil; 039 040import java.io.Serializable; 041import java.util.HashMap; 042import java.util.Map; 043 044import javax.servlet.http.HttpSession; 045 046import org.apache.commons.logging.Log; 047 048/** 049 * Stores the last opened locations for file explorer, page editor and sitemap editor.<p> 050 */ 051public class CmsQuickLaunchLocationCache implements Serializable { 052 053 /** Logger instance for this class. */ 054 private static final Log LOG = CmsLog.getLog(CmsQuickLaunchLocationCache.class); 055 056 /** The serial version id. */ 057 private static final long serialVersionUID = -6144984854691623070L; 058 059 /** The page editor locations. */ 060 private Map<String, CmsResource> m_pageEditorResources = new HashMap<>(); 061 062 /** The sitemap editor locations. */ 063 private Map<String, String> m_sitemapEditorLocations; 064 065 /** The file explorer locations. */ 066 private Map<String, String> m_fileExplorerLocations; 067 068 /** 069 * Constructor.<p> 070 */ 071 public CmsQuickLaunchLocationCache() { 072 073 m_sitemapEditorLocations = new HashMap<String, String>(); 074 m_fileExplorerLocations = new HashMap<String, String>(); 075 } 076 077 /** 078 * Returns the location cache from the user session.<p> 079 * 080 * @param session the session 081 * 082 * @return the location cache 083 */ 084 public static CmsQuickLaunchLocationCache getLocationCache(HttpSession session) { 085 086 CmsQuickLaunchLocationCache cache = (CmsQuickLaunchLocationCache)session.getAttribute( 087 CmsQuickLaunchLocationCache.class.getName()); 088 if (cache == null) { 089 cache = new CmsQuickLaunchLocationCache(); 090 session.setAttribute(CmsQuickLaunchLocationCache.class.getName(), cache); 091 } 092 return cache; 093 } 094 095 /** 096 * Returns the file explorer location for the given site root.<p> 097 * 098 * @param siteRoot the site root 099 * 100 * @return the location 101 */ 102 public String getFileExplorerLocation(String siteRoot) { 103 104 return m_fileExplorerLocations.get(siteRoot); 105 } 106 107 /** 108 * Returns the page editor location for the given site root.<p> 109 * 110 * @param cms the current CMS context 111 * @param siteRoot the site root 112 * 113 * @return the location 114 */ 115 public String getPageEditorLocation(CmsObject cms, String siteRoot) { 116 117 CmsResource res = m_pageEditorResources.get(siteRoot); 118 if (res == null) { 119 return null; 120 } 121 try { 122 String sitePath = cms.getSitePath(res); 123 cms.readResource(sitePath, CmsResourceFilter.ONLY_VISIBLE_NO_DELETED); 124 return sitePath; 125 } catch (CmsVfsResourceNotFoundException e) { 126 try { 127 CmsResource newRes = cms.readResource(res.getStructureId(), CmsResourceFilter.ONLY_VISIBLE_NO_DELETED); 128 CmsSite site = OpenCms.getSiteManager().getSiteForRootPath(newRes.getRootPath()); 129 if (site == null) { 130 return null; 131 } 132 if (normalizePath(site.getSiteRoot()).equals(normalizePath(siteRoot))) { 133 return cms.getSitePath(newRes); 134 } else { 135 return null; 136 } 137 138 } catch (CmsVfsResourceNotFoundException e2) { 139 return null; 140 } catch (CmsException e2) { 141 LOG.error(e.getLocalizedMessage(), e2); 142 return null; 143 } 144 } catch (CmsException e) { 145 LOG.error(e.getLocalizedMessage(), e); 146 return null; 147 } 148 149 } 150 151 /** 152 * Gets the cached location resource for the given site root. 153 * 154 * @param siteRoot the site root 155 * @return the location resource 156 */ 157 public CmsResource getPageEditorResource(String siteRoot) { 158 159 return m_pageEditorResources.get(siteRoot); 160 } 161 162 /** 163 * Returns the sitemap editor location for the given site root.<p> 164 * 165 * @param siteRoot the site root 166 * 167 * @return the location 168 */ 169 public String getSitemapEditorLocation(String siteRoot) { 170 171 return m_sitemapEditorLocations.get(siteRoot); 172 } 173 174 /** 175 * Sets the latest file explorer location for the given site.<p> 176 * 177 * @param siteRoot the site root 178 * @param location the location 179 */ 180 public void setFileExplorerLocation(String siteRoot, String location) { 181 182 m_fileExplorerLocations.put(siteRoot, location); 183 } 184 185 /** 186 * Sets the latest page editor location for the given site.<p> 187 * 188 * @param siteRoot the site root 189 * @param resource the location resource 190 */ 191 public void setPageEditorResource(String siteRoot, CmsResource resource) { 192 193 m_pageEditorResources.put(siteRoot, resource); 194 } 195 196 /** 197 * Sets the latest sitemap editor location for the given site.<p> 198 * 199 * @param siteRoot the site root 200 * @param location the location 201 */ 202 public void setSitemapEditorLocation(String siteRoot, String location) { 203 204 m_sitemapEditorLocations.put(siteRoot, location); 205 } 206 207 /** 208 * Ensures the given path begins and ends with a slash. 209 * 210 * @param path the path 211 * @return the normalized path 212 */ 213 private String normalizePath(String path) { 214 215 return CmsStringUtil.joinPaths("/", path, "/"); 216 } 217}