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.http.*;
024: import javax.servlet.*;
025: import java.io.IOException;
026: import com.methodhead.sitecontext.SiteContext;
027: import com.methodhead.aikp.IntKey;
028: import com.methodhead.persistable.PersistableException;
029: import org.apache.log4j.Logger;
030:
031: public class ViewPageServlet extends HttpServlet {
032:
033: // constructors /////////////////////////////////////////////////////////////
034:
035: // constants ////////////////////////////////////////////////////////////////
036:
037: // classes //////////////////////////////////////////////////////////////////
038:
039: // methods //////////////////////////////////////////////////////////////////
040:
041: /**
042: * Calls {@link #doGet doGet()}.
043: */
044: public void doPost(HttpServletRequest request,
045: HttpServletResponse response) throws IOException,
046: ServletException {
047:
048: doGet(request, response);
049: }
050:
051: /**
052: * Displays a page. The request attribute {@link ShimGlobals#PAGEALIAS_KEY
053: * ShimGlobals.PAGEALIAS_KEY} specifies the page to be displayed. If that
054: * attribute is not set, the root page in the site map is displayed. If the
055: * site map is empty, this method forwards to <code>/emptySite.do</code>. If
056: * the alias is invalid, this method forwards to
057: * <code>/pageNotFound.do</code>. The site context for this request should
058: * be set in the request or session.
059: */
060: public void doGet(HttpServletRequest request,
061: HttpServletResponse response) throws IOException,
062: ServletException {
063:
064: //
065: // load the page
066: //
067: SiteContext siteContext = SiteContext.getContext(request);
068:
069: if (logger_.isDebugEnabled()) {
070: logger_.debug("Using site context \"" + siteContext + "\"");
071: }
072:
073: Page page = new Page();
074: page.setSiteContext(siteContext);
075:
076: String alias = (String) request
077: .getAttribute(ShimGlobals.PAGEALIAS_KEY);
078:
079: //
080: // no alias?
081: //
082: if (alias == null) {
083:
084: if (logger_.isDebugEnabled()) {
085: logger_
086: .debug("No alias specified; looking for root link");
087: }
088:
089: //
090: // get the root link
091: //
092: SiteMap siteMap = ShimUtils.getSiteMap(request);
093: Link root = (Link) siteMap.getRoot();
094:
095: //
096: // no links in the site map?
097: //
098: if (root == null) {
099:
100: if (logger_.isDebugEnabled()) {
101: logger_
102: .debug("No links in the site map; forwarding to /emptySite.do");
103: }
104:
105: request.getRequestDispatcher("/emptySite.do").forward(
106: request, response);
107:
108: return;
109: } else {
110:
111: if (logger_.isDebugEnabled()) {
112: logger_
113: .debug("Found root link; loading page for id \""
114: + root.getPageId() + "\"");
115: }
116:
117: //
118: // load the root page
119: //
120: page.loadFull(new IntKey(root.getPageId()));
121: }
122: } else {
123:
124: try {
125: if (logger_.isDebugEnabled()) {
126: logger_.debug("Loading page for alias \"" + alias
127: + "\"");
128: }
129:
130: page.loadFullForAlias(alias);
131: } catch (PersistableException e) {
132:
133: //
134: // set a 410 Gone status; if we do 404, we'll get our 404 jsp
135: //
136: response.setStatus(HttpServletResponse.SC_GONE);
137:
138: //
139: // does the pagenotfound page exist?
140: //
141: try {
142: page.loadFullForAlias("pagenotfound");
143:
144: if (logger_.isDebugEnabled()) {
145: logger_
146: .debug("Loaded page for alias \"pagenotfound\"");
147: }
148: } catch (PersistableException ee) {
149:
150: if (logger_.isDebugEnabled()) {
151: logger_
152: .debug("Couldn't load page for alias \""
153: + alias
154: + "\" or \"pagenotfound\"; forwarding to /pageNotFound.do");
155: }
156:
157: request.getRequestDispatcher("/pageNotFound.do")
158: .forward(request, response);
159:
160: return;
161: }
162: }
163: }
164:
165: if (logger_.isDebugEnabled()) {
166: logger_.debug("Displaying page");
167: }
168:
169: //
170: // set content type
171: //
172: response.setContentType("text/html; charset=iso-8859-1");
173:
174: //
175: // put the loaded page into the request
176: //
177: request.setAttribute(ShimGlobals.PAGE_KEY, page);
178:
179: //
180: // include the template
181: //
182: Template template = new Template();
183: template.setSiteContext(siteContext);
184: template.include(request, response, page.getString("template"));
185: }
186:
187: // properties ///////////////////////////////////////////////////////////////
188:
189: // attributes ///////////////////////////////////////////////////////////////
190:
191: public static Logger logger_ = Logger
192: .getLogger(ViewPageServlet.class);
193: }
|