001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.shim;
022:
023: import javax.servlet.jsp.JspException;
024: import javax.servlet.jsp.tagext.TagSupport;
025: import javax.servlet.http.HttpServletRequest;
026: import java.io.IOException;
027: import com.methodhead.util.ServletUtils;
028: import com.methodhead.sitecontext.SiteContext;
029: import com.methodhead.shim.ShimUtils;
030:
031: public class HeadTag extends TagSupport {
032:
033: // constructors /////////////////////////////////////////////////////////////
034:
035: // constants ////////////////////////////////////////////////////////////////
036:
037: // classes //////////////////////////////////////////////////////////////////
038:
039: // methods //////////////////////////////////////////////////////////////////
040:
041: public int doStartTag() throws JspException {
042:
043: try {
044:
045: //
046: // defining panels?
047: //
048: if (pageContext.getRequest().getAttribute(
049: ShimGlobals.PANELMAP_KEY) != null)
050: return SKIP_BODY;
051:
052: //
053: // get some things we need
054: //
055: HttpServletRequest request = (HttpServletRequest) pageContext
056: .getRequest();
057:
058: Page page = (Page) pageContext.getRequest().getAttribute(
059: ShimGlobals.PAGE_KEY);
060:
061: String mode = (String) pageContext.getSession()
062: .getAttribute(ShimGlobals.MODE_KEY);
063:
064: SiteContext siteContext = SiteContext.getContext(request);
065:
066: String siteContextPath = siteContext.getString("path");
067: if (!siteContextPath.equals("")) {
068: siteContextPath = "/" + siteContextPath;
069: }
070:
071: //
072: // start printing
073: //
074: pageContext.getOut().println("<head>");
075:
076: //
077: // print base tag
078: //
079: if (ShimGlobals.MODE_EDIT.equals(mode)
080: || ShimGlobals.MODE_RICHEDIT.equals(mode)) {
081:
082: //
083: // if we're logged in and editing, use a base url composed from the
084: // site context; otherwise requests will go to the wrong site if we
085: // switch sites
086: //
087: pageContext.getOut().println(
088: "<base href=\"http://"
089: + siteContext.getDomains().get(0) + ":"
090: + request.getServerPort()
091: + request.getContextPath()
092: + siteContextPath + "/\"/>");
093: } else {
094:
095: //
096: // otherwise construct a base url from the request URL to keep requests consistent
097: //
098: pageContext.getOut().println(
099: "<base href=\""
100: + ServletUtils.getBaseUrl(request)
101: + siteContextPath + "/\"/>");
102: }
103:
104: //
105: // print alttitle if it's set
106: //
107: if (!"".equals(page.getString("alttitle")))
108: pageContext.getOut().println(
109: "<title>" + page.getString("alttitle")
110: + "</title>");
111: else
112: pageContext.getOut().println(
113: "<title>" + page.getString("title")
114: + "</title>");
115:
116: //
117: // print meta tags if they're set
118: //
119: if (!"".equals(page.getString("metadescription")))
120: pageContext.getOut().println(
121: "<meta name=\"description\" content=\""
122: + page.getString("metadescription")
123: + "\"/>");
124:
125: if (!"".equals(page.getString("metakeywords")))
126: pageContext.getOut().println(
127: "<meta name=\"keywords\" content=\""
128: + page.getString("metakeywords")
129: + "\"/>");
130: } catch (IOException e) {
131: throw new JspException("Unexpected IOException: "
132: + e.toString());
133: }
134:
135: return EVAL_BODY_INCLUDE;
136: }
137:
138: public int doEndTag() throws JspException {
139:
140: try {
141: pageContext.getOut().println("</head>");
142: } catch (IOException e) {
143: throw new JspException("Unexpected IOException: "
144: + e.toString());
145: }
146:
147: return EVAL_PAGE;
148: }
149:
150: // properties ///////////////////////////////////////////////////////////////
151:
152: // attributes ///////////////////////////////////////////////////////////////
153: }
|