KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > ViewPageServlet


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.shim;
22
23 import javax.servlet.http.*;
24 import javax.servlet.*;
25 import java.io.IOException JavaDoc;
26 import com.methodhead.sitecontext.SiteContext;
27 import com.methodhead.aikp.IntKey;
28 import com.methodhead.persistable.PersistableException;
29 import org.apache.log4j.Logger;
30
31 public class ViewPageServlet
32 extends
33   HttpServlet {
34
35   // constructors /////////////////////////////////////////////////////////////
36

37   // constants ////////////////////////////////////////////////////////////////
38

39   // classes //////////////////////////////////////////////////////////////////
40

41   // methods //////////////////////////////////////////////////////////////////
42

43   /**
44    * Calls {@link #doGet doGet()}.
45    */

46   public void doPost(
47     HttpServletRequest request,
48     HttpServletResponse response )
49   throws
50     IOException JavaDoc,
51     ServletException {
52
53     doGet( request, response );
54   }
55
56   /**
57    * Displays a page. The request attribute {@link ShimGlobals#PAGEALIAS_KEY
58    * ShimGlobals.PAGEALIAS_KEY} specifies the page to be displayed. If that
59    * attribute is not set, the root page in the site map is displayed. If the
60    * site map is empty, this method forwards to <code>/emptySite.do</code>. If
61    * the alias is invalid, this method forwards to
62    * <code>/pageNotFound.do</code>. The site context for this request should
63    * be set in the request or session.
64    */

65   public void doGet(
66     HttpServletRequest request,
67     HttpServletResponse response )
68   throws
69     IOException JavaDoc,
70     ServletException {
71
72     //
73
// load the page
74
//
75
SiteContext siteContext = SiteContext.getContext( request );
76
77     if ( logger_.isDebugEnabled() ) {
78       logger_.debug( "Using site context \"" + siteContext + "\"" );
79     }
80
81     Page page = new Page();
82     page.setSiteContext( siteContext );
83
84     String JavaDoc alias = ( String JavaDoc )request.getAttribute( ShimGlobals.PAGEALIAS_KEY );
85
86     //
87
// no alias?
88
//
89
if ( alias == null ) {
90
91       if ( logger_.isDebugEnabled() ) {
92         logger_.debug( "No alias specified; looking for root link" );
93       }
94
95       //
96
// get the root link
97
//
98
SiteMap siteMap = ShimUtils.getSiteMap( request );
99       Link root = ( Link )siteMap.getRoot();
100
101       //
102
// no links in the site map?
103
//
104
if ( root == null ) {
105
106         if ( logger_.isDebugEnabled() ) {
107           logger_.debug( "No links in the site map; forwarding to /emptySite.do" );
108         }
109
110         request.getRequestDispatcher( "/emptySite.do" ).forward( request, response );
111
112         return;
113       }
114       else {
115
116         if ( logger_.isDebugEnabled() ) {
117           logger_.debug( "Found root link; loading page for id \"" + root.getPageId() + "\"" );
118         }
119
120         //
121
// load the root page
122
//
123
page.loadFull( new IntKey( root.getPageId() ) );
124       }
125     }
126     else {
127
128       try {
129         if ( logger_.isDebugEnabled() ) {
130           logger_.debug( "Loading page for alias \"" + alias + "\"" );
131         }
132
133         page.loadFullForAlias( alias );
134       }
135       catch ( PersistableException e ) {
136
137         //
138
// set a 410 Gone status; if we do 404, we'll get our 404 jsp
139
//
140
response.setStatus( HttpServletResponse.SC_GONE );
141
142         //
143
// does the pagenotfound page exist?
144
//
145
try {
146           page.loadFullForAlias( "pagenotfound" );
147
148           if ( logger_.isDebugEnabled() ) {
149             logger_.debug( "Loaded page for alias \"pagenotfound\"" );
150           }
151         }
152         catch ( PersistableException ee ) {
153
154           if ( logger_.isDebugEnabled() ) {
155             logger_.debug( "Couldn't load page for alias \"" + alias + "\" or \"pagenotfound\"; forwarding to /pageNotFound.do" );
156           }
157
158           request.getRequestDispatcher( "/pageNotFound.do" ).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.getLogger( ViewPageServlet.class );
192 }
193
Popular Tags