From: <fa...@vh...> - 2005-09-12 15:16:23
|
Author: fabrice Date: 2005-09-12 17:07:11 +0200 (Mon, 12 Sep 2005) New Revision: 795 Added: ccm-ldn-subsite/trunk/src/com/arsdigita/london/subsite/SubsiteDynamicHostProvider.java Log: DynamicHostProvider for subsites Added: ccm-ldn-subsite/trunk/src/com/arsdigita/london/subsite/SubsiteDynamicHostProvider.java =================================================================== --- ccm-ldn-subsite/trunk/src/com/arsdigita/london/subsite/SubsiteDynamicHostProvider.java 2005-09-12 15:06:53 UTC (rev 794) +++ ccm-ldn-subsite/trunk/src/com/arsdigita/london/subsite/SubsiteDynamicHostProvider.java 2005-09-12 15:07:11 UTC (rev 795) @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2005 RuntimeCollective Ltd. All Rights Reserved. + * + * 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. + * + * 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 com.arsdigita.london.subsite; + +import com.arsdigita.util.servlet.HttpHost; +import com.arsdigita.web.ServerDynamicHostProvider; + +import org.apache.log4j.Logger; + +import java.net.URL; +import java.net.MalformedURLException; + +public class SubsiteDynamicHostProvider extends ServerDynamicHostProvider { + + private static final Logger s_log = Logger.getLogger(SubsiteDynamicHostProvider.class); + + public SubsiteDynamicHostProvider() { + super(); + } + + public String getName() { + if (!Subsite.getContext().hasSite()) { + return super.getName(); + } + + URL url = getSubsiteURL(); + return url.getHost(); + } + + public int getPort() { + if (!Subsite.getContext().hasSite()) { + return super.getPort(); + } + + URL url = getSubsiteURL(); + int port = url.getPort(); + if (port == -1) { + port = 80; + } + + return port; + } + + public URL getSubsiteURL() { + String hostname = Subsite.getContext().getSite().getHostname(); + if (hostname.indexOf('/') == -1) { + hostname = "http://"+hostname; + } + + URL url = null; + try { + url = new URL(hostname); + } catch (MalformedURLException e) { + s_log.error("Could not generate URL out of subsite hostname : "+hostname, e); + } + + return url; + } +} |