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.xml.containerpage; 029 030import org.opencms.file.CmsFile; 031import org.opencms.file.CmsObject; 032import org.opencms.file.CmsProperty; 033import org.opencms.file.CmsPropertyDefinition; 034import org.opencms.main.CmsException; 035import org.opencms.main.CmsLog; 036import org.opencms.main.OpenCms; 037import org.opencms.util.CmsStringUtil; 038import org.opencms.xml.content.CmsDefaultXmlContentHandler; 039import org.opencms.xml.content.CmsXmlContent; 040import org.opencms.xml.types.I_CmsXmlContentValue; 041 042import java.net.URI; 043import java.net.URISyntaxException; 044import java.util.Arrays; 045import java.util.Locale; 046 047import org.apache.commons.logging.Log; 048 049/** 050 * Content handler for HTML redirects.<p> 051 */ 052public class CmsHtmlRedirectHandler extends CmsDefaultXmlContentHandler { 053 054 /** The logger instance for this class. */ 055 private static final Log LOG = CmsLog.getLog(CmsHtmlRedirectHandler.class); 056 057 /** 058 * @see org.opencms.xml.content.CmsDefaultXmlContentHandler#prepareForWrite(org.opencms.file.CmsObject, org.opencms.xml.content.CmsXmlContent, org.opencms.file.CmsFile) 059 */ 060 @Override 061 public CmsFile prepareForWrite(CmsObject cms, CmsXmlContent content, CmsFile file) throws CmsException { 062 063 CmsFile result = super.prepareForWrite(cms, content, file); 064 try { 065 String linkStr = getStringValue(cms, content, "Link"); 066 String typeStr = getStringValue(cms, content, "Type"); 067 068 if ("sublevel".equals(typeStr)) { 069 Locale locale = OpenCms.getLocaleManager().getDefaultLocale(cms, file); 070 String title = org.opencms.xml.containerpage.Messages.get().getBundle(locale).key( 071 org.opencms.xml.containerpage.Messages.GUI_REDIRECT_SUBLEVEL_TITLE_0); 072 CmsProperty titleProp = new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, title, null); 073 cms.writePropertyObjects(file, Arrays.asList(titleProp)); 074 } else if (!CmsStringUtil.isEmptyOrWhitespaceOnly(linkStr)) { 075 boolean hasScheme = false; 076 try { 077 URI uri = new URI(linkStr); 078 hasScheme = uri.getScheme() != null; 079 } catch (URISyntaxException e) { 080 LOG.debug(e.getLocalizedMessage(), e); 081 } 082 if (!hasScheme) { 083 linkStr = cms.getRequestContext().removeSiteRoot(linkStr); 084 } 085 Locale locale = OpenCms.getLocaleManager().getDefaultLocale(cms, file); 086 String title = org.opencms.xml.containerpage.Messages.get().getBundle(locale).key( 087 org.opencms.xml.containerpage.Messages.GUI_REDIRECT_TITLE_1, 088 linkStr); 089 CmsProperty titleProp = new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, title, null); 090 cms.writePropertyObjects(file, Arrays.asList(titleProp)); 091 } 092 } catch (CmsException e) { 093 LOG.error(e.getLocalizedMessage(), e); 094 } 095 return result; 096 } 097 098 private String getStringValue(CmsObject cms, CmsXmlContent content, String node) { 099 100 I_CmsXmlContentValue val = content.getValue(node, Locale.ENGLISH); 101 if (val == null) { 102 return null; 103 } 104 return val.getStringValue(cms); 105 } 106 107}