KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > rundata > DefaultTurbineRunData


1 package org.apache.turbine.services.rundata;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.IOException JavaDoc;
58 import java.io.PrintWriter JavaDoc;
59 import java.util.Hashtable JavaDoc;
60 import java.util.Locale JavaDoc;
61 import java.util.Vector JavaDoc;
62
63 import javax.servlet.ServletConfig JavaDoc;
64 import javax.servlet.ServletContext JavaDoc;
65 import javax.servlet.http.HttpServletRequest JavaDoc;
66 import javax.servlet.http.HttpServletResponse JavaDoc;
67 import javax.servlet.http.HttpSession JavaDoc;
68
69 import org.apache.commons.collections.FastHashMap;
70 import org.apache.commons.logging.Log;
71 import org.apache.commons.logging.LogFactory;
72 import org.apache.fulcrum.TurbineServices;
73 import org.apache.fulcrum.localization.LocalizationService;
74 import org.apache.fulcrum.mimetype.MimeTypeService;
75 import org.apache.fulcrum.parser.CookieParser;
76 import org.apache.fulcrum.parser.ParameterParser;
77 import org.apache.fulcrum.pool.Recyclable;
78 import org.apache.fulcrum.security.entity.User;
79 import org.apache.fulcrum.security.util.AccessControlList;
80 import org.apache.turbine.Turbine;
81 import org.apache.turbine.services.yaaficomponent.YaafiComponentService;
82
83 /**
84  * DefaultTurbineRunData is the default implementation of the TurbineRunData
85  * interface, which is distributed by the Turbine RunData service, if another
86  * implementation is not defined in the default or specified RunData
87  * configuration. TurbineRunData is an extension to RunData, which is an
88  * interface to run-rime information that is passed within Turbine. This
89  * provides the threading mechanism for the entire system because multiple
90  * requests can potentially come in at the same time. Thus, there is only one
91  * RunData implementation for each request that is being serviced.
92  *
93  * <p>
94  * DefaultTurbineRunData implements the Recyclable interface making it possible
95  * to pool its instances for recycling.
96  *
97  * @author <a HREF="mailto:ilkka.priha@simsoft.fi">Ilkka Priha </a>
98  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens </a>
99  * @author <a HREF="mailto:bhoeneis@ee.ethz.ch">Bernie Hoeneisen </a>
100  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall </a>
101  * @version $Id: DefaultTurbineRunData.java,v 1.17 2004/11/12 10:26:23 epugh Exp $
102  */

103 public class DefaultTurbineRunData implements TurbineRunData, Recyclable {
104     private static final Log log = LogFactory.getLog(DefaultTurbineRunData.class);
105
106     /**
107      * The disposed flag.
108      */

109     private boolean disposed;
110
111     /**
112      * The default locale.
113      */

114     private static Locale JavaDoc defaultLocale;
115
116     /**
117      * The default locale checked flag.
118      */

119     private static boolean defaultLocaleChecked;
120
121     /**
122      * The default charset.
123      */

124     private static String JavaDoc defaultCharSet;
125
126     /**
127      * The default charset checked flag.
128      */

129     private static boolean defaultCharSetChecked;
130
131     /**
132      * A reference to the GET/POST data parser.
133      */

134     private ParameterParser parameters;
135
136     /**
137      * A reference to a cookie parser.
138      */

139     public CookieParser cookies;
140
141     /**
142      * The servlet request interface.
143      */

144     private HttpServletRequest JavaDoc req;
145
146     /**
147      * The servlet response interface.
148      */

149     private HttpServletResponse JavaDoc res;
150
151     /**
152      * The servlet session information.
153      */

154     private HttpSession JavaDoc session;
155
156     /**
157      * The servlet configuration.
158      */

159     private ServletConfig JavaDoc config;
160
161     /**
162      * The servlet context information. Note that this is from the "Turbine"
163      * Servlet context.
164      */

165     private ServletContext JavaDoc servletContext;
166
167     /**
168      * The access control list.
169      */

170     private AccessControlList acl;
171
172     /**
173      * Cached action name to execute for this request.
174      */

175     private String JavaDoc action;
176
177     /**
178      * The character encoding of template files.
179      */

180     private String JavaDoc templateEncoding;
181
182     /**
183      * The user object.
184      */

185     private User user;
186
187     /**
188      * This is what will build the <title></title> of the document.
189      */

190     private String JavaDoc title;
191
192     /**
193      * The locale.
194      */

195     private Locale JavaDoc locale;
196
197     /**
198      * The HTTP charset.
199      */

200     private String JavaDoc charSet;
201
202     /**
203      * The HTTP content type to return.
204      */

205     private String JavaDoc contentType = "text/html";
206
207     /**
208      * If this is set, also set the status code to 302.
209      */

210     private String JavaDoc redirectURI;
211
212     /**
213      * The HTTP status code to return.
214      */

215     private int statusCode = HttpServletResponse.SC_OK;
216
217     /**
218      * This is a vector to hold critical system errors.
219      */

220     private Vector JavaDoc errors = new Vector JavaDoc();
221
222     /**
223      * @see #getRemoteAddr()
224      */

225     private String JavaDoc remoteAddr;
226
227     /**
228      * @see #getRemoteHost()
229      */

230     private String JavaDoc remoteHost;
231
232     /**
233      * @see #getUserAgent()
234      */

235     private String JavaDoc userAgent;
236
237     /**
238      * A holder for stack trace.
239      */

240     private String JavaDoc stackTrace;
241
242     /**
243      * A holder ofr stack trace exception.
244      */

245     private Throwable JavaDoc stackTraceException;
246
247     /**
248      * Put things here and they will be shown on the default Error screen. This
249      * is great for debugging variable values when an exception is thrown.
250      */

251     private Hashtable JavaDoc varDebug = new Hashtable JavaDoc();
252
253     private String JavaDoc target;
254
255     private String JavaDoc message;
256
257     private FastHashMap temp;
258
259     // These were part of server data
260

261     /** Cached serverName, */
262     private String JavaDoc serverName = null;
263
264     /** Cached serverPort. */
265     private int serverPort = 80;
266
267     /** Cached serverScheme. */
268     private String JavaDoc serverScheme = null;
269
270     /** Cached script name. */
271     private String JavaDoc scriptName = null;
272
273     /** Cached context path. */
274     private String JavaDoc contextPath = null;
275
276     public void setTarget(String JavaDoc target) {
277         this.target = target;
278     }
279
280     public String JavaDoc getTarget() {
281         return target;
282     }
283
284     public boolean hasTarget() {
285         return (target != null);
286     }
287
288     public void setTemp(String JavaDoc key, Object JavaDoc value) {
289         temp.put(key, value);
290     }
291
292     public Object JavaDoc getTemp(String JavaDoc key) {
293         return temp.get(key);
294     }
295
296     /**
297      * Constructs a run data object.
298      */

299     public DefaultTurbineRunData() {
300         recycle();
301
302         // This only needs to be made once. We will
303
// clear out the mappings when the RunData object
304
// is recycled.
305
temp = new FastHashMap();
306     }
307
308     /**
309      * Recycles a run data object.
310      */

311     public void recycle() {
312         disposed = false;
313     }
314
315     /**
316      * Disposes a run data object.
317      */

318     public void dispose() {
319         target = null;
320         parameters = null;
321         cookies = null;
322         req = null;
323         res = null;
324         session = null;
325         config = null;
326         servletContext = null;
327         acl = null;
328         action = null;
329         templateEncoding = null;
330         user = null;
331         title = null;
332         locale = null;
333         charSet = null;
334         contentType = "text/html";
335         redirectURI = null;
336         statusCode = HttpServletResponse.SC_OK;
337         errors.clear();
338         remoteAddr = null;
339         remoteHost = null;
340         userAgent = null;
341         stackTrace = null;
342         stackTraceException = null;
343         varDebug.clear();
344         message = null;
345
346         // Clear all the values from the temporary
347
// storage.
348
temp.clear();
349
350         //serverName = null;
351
//serverPort = 80;
352
//serverScheme = null;
353
//scriptName = null;
354
//contextPath = null;
355

356         disposed = true;
357     }
358
359     /**
360      * Checks whether the object is disposed.
361      *
362      * @return true, if the object is disposed.
363      */

364     public boolean isDisposed() {
365         return disposed;
366     }
367
368     // ***************************************
369
// Implementation of the RunData interface
370
// ***************************************
371

372     /**
373      * Gets the parameters. if this is the first time calling this method since
374      * the rundata was borrowed from the pool the request will be parsed.
375      *
376      * @return a parameter parser.
377      */

378     public ParameterParser getParameters() {
379         // Parse the parameters first, if not yet done.
380
if ((this.parameters != null) && (this.parameters.getRequest() == null)) {
381             this.parameters.setRequest(this.req);
382         }
383         return this.parameters;
384     }
385
386     /**
387      * Gets the cookies.
388      *
389      * @return a cookie parser.
390      */

391     public CookieParser getCookies() {
392         return this.cookies;
393     }
394
395     /**
396      * Gets the servlet request.
397      *
398      * @return the request.
399      */

400     public HttpServletRequest JavaDoc getRequest() {
401         return this.req;
402     }
403
404     /**
405      * Gets the servlet response.
406      *
407      * @return the resposne.
408      */

409     public HttpServletResponse JavaDoc getResponse() {
410         return this.res;
411     }
412
413     /**
414      * Gets the servlet session information.
415      *
416      * @return the session.
417      */

418     public HttpSession JavaDoc getSession() {
419         return session;
420     }
421
422     /**
423      * Gets the servlet configuration used during servlet init.
424      *
425      * @return the configuration.
426      */

427     public ServletConfig JavaDoc getServletConfig() {
428         return this.config;
429     }
430
431     /**
432      * Gets the servlet context used during servlet init.
433      *
434      * @return the context.
435      */

436     public ServletContext JavaDoc getServletContext() {
437         return this.servletContext;
438     }
439
440     /**
441      * Gets the access control list.
442      *
443      * @return the access control list.
444      */

445     public AccessControlList getACL() {
446         return acl;
447     }
448
449     /**
450      * Sets the access control list.
451      *
452      * @param acl
453      * an access control list.
454      */

455     public void setACL(AccessControlList acl) {
456         this.acl = acl;
457     }
458
459     /**
460      * Whether or not an action has been defined.
461      *
462      * @return true if an action has been defined.
463      */

464     public boolean hasAction() {
465         return (this.action != null && this.action.length() > 0 && !this.action.equalsIgnoreCase("null"));
466     }
467
468     /**
469      * Gets the action. It returns an empty string if null so that it is easy to
470      * do conditionals on it based on the equalsIgnoreCase() method.
471      *
472      * @return a string, "" if null.
473      */

474     public String JavaDoc getAction() {
475         return (hasAction() ? this.action : "");
476     }
477
478     /**
479      * Sets the action for the request.
480      *
481      * @param action
482      * a atring.
483      */

484     public void setAction(String JavaDoc action) {
485         this.action = action;
486     }
487
488     /**
489      * Gets the character encoding to use for reading template files.
490      *
491      * @return the template encoding or null if not specified.
492      */

493     public String JavaDoc getTemplateEncoding() {
494         return templateEncoding;
495     }
496
497     /**
498      * Sets the character encoding to use for reading template files.
499      *
500      * @param encoding
501      * the template encoding.
502      */

503     public void setTemplateEncoding(String JavaDoc encoding) {
504         templateEncoding = encoding;
505     }
506
507     /**
508      * Gets the title of the page.
509      *
510      * @return a string.
511      */

512     public String JavaDoc getTitle() {
513         return (this.title == null ? "" : this.title);
514     }
515
516     /**
517      * Sets the title of the page.
518      *
519      * @param title
520      * a string.
521      */

522     public void setTitle(String JavaDoc title) {
523         this.title = title;
524     }
525
526     /**
527      * Checks if a user exists in this session.
528      *
529      * @return true if a user exists in this session.
530      */

531     public boolean userExists() {
532         user = getUserFromSession();
533         return (user != null);
534     }
535
536     /**
537      * Gets the user.
538      *
539      * @return a user.
540      */

541     public User getUser() {
542         return this.user;
543     }
544
545     /**
546      * Sets the user.
547      *
548      * @param user
549      * a user.
550      */

551     public void setUser(User user) {
552         this.user = user;
553     }
554
555     /**
556      * Attempts to get the User object from the session. If the user does not
557      * exist in the session, <code>null</code> is returned.
558      *
559      * <p>
560      * Anyone overriding this method should be sure to leverage the
561      * <code>SessionBindingEventProxy</code> when pulling the
562      * <code>User</code> object from the session, allowing hook functions to
563      * be called on the listener when it is removed from the session (which
564      * happens on session timeout).
565      *
566      * @param session
567      * The session to retrieve a <code>User</code> object from.
568      * @return The retreived user, or <code>null</code> if errors occur.
569      * @see org.apache.turbine.services.rundata.SessionBindingEventProxy
570      * @see org.apache.fulcrum.security.entity.User
571      * @see org.apache.fulcrum.security.session.SessionBindingListener
572      */

573     public static User getUserFromSession(HttpSession JavaDoc session) {
574         try {
575             SessionBindingEventProxy proxy = (SessionBindingEventProxy) session.getAttribute(User.SESSION_KEY);
576
577             // If the user isn't yet logged in, return null so that
578
// the session validator can take the correct action
579
// (i.e. make a temporary anonymous user).
580
return (proxy == null ? null : (User) proxy.getListener());
581         } catch (ClassCastException JavaDoc e) {
582             String JavaDoc message = "User object did not implement User interface. "
583                     + "if you are sure the interface is implemented, the user "
584                     + "object in the session and this class may be loaded from "
585                     + "different classloaders. This has been known to happen "
586                     + "when using multiple turbine apps in tomcat that interact "
587                     + "through the use of RequestDispatcher.include or forward.";
588             if (log != null) {
589                 log.error(message, e);
590             } else {
591                 System.err.println(message);
592                 e.printStackTrace();
593             }
594
595             return null;
596         }
597     }
598
599     /**
600      * Allows one to invalidate the user in a session.
601      *
602      * @param session
603      * The session from which to remove the user.
604      * @return Whether the user was removed from the session.
605      */

606     public static boolean removeUserFromSession(HttpSession JavaDoc session) {
607         try {
608             session.removeAttribute(User.SESSION_KEY);
609         } catch (Exception JavaDoc e) {
610             return false;
611         }
612         return true;
613     }
614
615     /**
616      * Retrieves the ACL from the session
617      */

618     public static AccessControlList getACLFromSession(HttpSession JavaDoc session) {
619         try {
620             AccessControlList acl = (AccessControlList) session.getAttribute(AccessControlList.SESSION_KEY);
621
622             // If the acl isn't yet defined, return null
623
return (acl == null ? null : acl);
624         } catch (ClassCastException JavaDoc e) {
625             return null;
626         }
627     }
628
629     /**
630      * Allows one to invalidate the acl in a session.
631      *
632      * @param session
633      * An HttpSession.
634      * @return True if acl was invalidated.
635      */

636     public static boolean removeACLFromSession(HttpSession JavaDoc session) {
637         try {
638             session.removeAttribute(AccessControlList.SESSION_KEY);
639         } catch (Exception JavaDoc e) {
640             return false;
641         }
642         return true;
643     }
644
645     /**
646      * Attempts to get the user from the session. If it does not exist, it
647      * returns null.
648      *
649      * @return a user.
650      */

651     public User getUserFromSession() {
652         return getUserFromSession(session);
653     }
654
655     /**
656      * Allows one to invalidate the user in the default session.
657      *
658      * @return true if user was invalidated.
659      */

660     public boolean removeUserFromSession() {
661         return removeUserFromSession(session);
662     }
663
664     /**
665      * Attempts to get the ACL from the session. If it does not exist, it
666      * returns null.
667      *
668      * @return a acl.
669      */

670     public AccessControlList getACLFromSession() {
671         return getACLFromSession(session);
672     }
673
674     /**
675      * Allows one to invalidate the acl in the default session.
676      *
677      * @return true if acl was invalidated.
678      */

679     public boolean removeACLFromSession() {
680         return removeACLFromSession(session);
681     }
682
683     /**
684      * Gets the print writer.
685      *
686      * @return a print writer.
687      * @throws IOException
688      */

689     public PrintWriter JavaDoc getOut() throws IOException JavaDoc {
690         return res.getWriter();
691     }
692
693     /**
694      * Gets the default locale defined by properties named "locale.default.lang"
695      * and "locale.default.country". If neither property is defined, it will use
696      * the Local.getDefault() method to return the appropriate Locale for the
697      * installed JVM.
698      *
699      * @return the default locale.
700      */

701     protected static Locale JavaDoc getDefaultLocale() {
702         if (!defaultLocaleChecked) {
703             /* Get the default locale and cache it in a static variable. */
704             String JavaDoc lang = Turbine.getConfiguration().getString("locale.default.language");
705             String JavaDoc country = Turbine.getConfiguration().getString("locale.default.country");
706             if (lang != null) {
707                 defaultLocale = country != null ? new Locale JavaDoc(lang, country) : new Locale JavaDoc(lang, "");
708             } else if (country != null) {
709                 defaultLocale = new Locale JavaDoc("", country);
710             } else {
711                 defaultLocale = Locale.getDefault();
712             }
713             defaultLocaleChecked = true;
714         }
715         return defaultLocale;
716     }
717
718     /**
719      * Gets the default charset defined by a property named
720      * "locale.default.charset" or by the default Locale.
721      *
722      * @return the name of the default charset.
723      */

724     protected String JavaDoc getDefaultCharSet() {
725         if (!defaultCharSetChecked) {
726             /*
727              * Get the default charset and cache it in a static variable.
728              */

729             defaultCharSet = Turbine.getConfiguration().getString("locale.default.charset");
730
731             if (defaultCharSet == null) {
732                 /*
733                  * Default charset isn't specified, get the locale specific one.
734                  */

735                 Locale JavaDoc locale = getLocale();
736                 
737                 defaultCharSet = getMimeTypeService().getCharSet(locale);
738                 
739             }
740             defaultCharSetChecked = true;
741         }
742         return defaultCharSet;
743     }
744
745     /**
746      * Gets the locale. If it has not already been defined with setLocale(),
747      * then the Localization service is used to determine the right Locale based
748      * on the HttpServletRequest. If that is not possible, then the
749      * getDefaultLocale() method is used.
750      *
751      * @return the Locale.
752      */

753     public Locale JavaDoc getLocale() {
754         if (locale == null) {
755             locale = getLocalizationService().getLocale(getRequest());
756             if (locale == null) {
757                 locale = getDefaultLocale();
758             }
759         }
760         return locale;
761     }
762
763     /**
764      * Sets the Locale.
765      *
766      * @param locale
767      * the new Locale.
768      */

769     public void setLocale(Locale JavaDoc locale) {
770         this.locale = locale;
771     }
772
773     /**
774      * Gets the charset. If it has not already been defined with setCharSet().
775      * It will then try to get the getDefaultCharSet(). If that isn't defined,
776      * then the Localization/Mimetype service is used to determine the right
777      * charset based on the HttpServletRequest. Please note that this ordering
778      * is still up in the air and may change in the near future as more testing
779      * is done. The issue is that sometimes, even though the Locale is dynamic,
780      * the charset should always be 8859_1. So, the current ordering allows us
781      * to just define things as always being 8859_1 which may or may not be
782      * correct.
783      *
784      * @return the charset.
785      */

786     public String JavaDoc getCharSet() {
787         if (charSet == null) {
788             charSet = getDefaultCharSet();
789             if (charSet == null) {
790                 charSet = getMimeTypeService().getCharSet(getLocale());
791             }
792         }
793         return charSet;
794     }
795
796     /**
797      * Sets the charset.
798      *
799      * @param charset
800      * the name of the new charset.
801      */

802     public void setCharSet(String JavaDoc charset) {
803         this.charSet = charset;
804     }
805
806     /**
807      * Gets the HTTP content type to return. If a charset has been specified, it
808      * is included in the content type. If the charset has not been specified
809      * and the main type of the content type is "text", the default charset is
810      * included. If the default charset is undefined, but the default locale is
811      * defined and it is not the US locale, a locale specific charset is
812      * included.
813      *
814      * @return the content type or an empty string.
815      */

816     public String JavaDoc getContentType() {
817         String JavaDoc ct = this.contentType;
818         if (ct != null) {
819             if (ct.startsWith("text/")) {
820                 String JavaDoc charset = getCharSet();
821                 if (charset != null) {
822                     ct += "; charset=" + charset;
823                 }
824             }
825         } else {
826             ct = "";
827         }
828         return ct;
829     }
830
831     /**
832      * Sets the HTTP content type to return.
833      *
834      * @param ct
835      * a string.
836      */

837     public void setContentType(String JavaDoc ct) {
838         this.contentType = ct;
839     }
840
841     /**
842      * Gets the redirect URI. If this is set, also make sure to set the status
843      * code to 302.
844      *
845      * @return a string, "" if null.
846      */

847     public String JavaDoc getRedirectURI() {
848         return (this.redirectURI == null ? "" : redirectURI);
849     }
850
851     /**
852      * Sets the redirect uri. If this is set, also make sure to set the status
853      * code to 302.
854      *
855      * @param ruri
856      * a string.
857      */

858     public void setRedirectURI(String JavaDoc ruri) {
859         this.redirectURI = ruri;
860     }
861
862     /**
863      * Gets the HTTP status code to return.
864      *
865      * @return the status.
866      */

867     public int getStatusCode() {
868         return statusCode;
869     }
870
871     /**
872      * Sets the HTTP status code to return.
873      *
874      * @param sc
875      * the status.
876      */

877     public void setStatusCode(int sc) {
878         this.statusCode = sc;
879     }
880
881     /**
882      * Gets the cached server scheme.
883      *
884      * @return a string.
885      */

886     public String JavaDoc getServerScheme() {
887         return serverScheme;
888     }
889
890     /**
891      * Gets the cached server name.
892      *
893      * @return a string.
894      */

895     public String JavaDoc getServerName() {
896         return serverName;
897     }
898
899     /**
900      * Gets the cached server port.
901      *
902      * @return an int.
903      */

904     public int getServerPort() {
905         return serverPort;
906     }
907
908     /**
909      * Gets the cached context path.
910      *
911      * @return a string.
912      */

913     public String JavaDoc getContextPath() {
914         return contextPath;
915     }
916
917     /**
918      * Gets the cached script name.
919      *
920      * @return a string.
921      */

922     public String JavaDoc getScriptName() {
923         return scriptName;
924     }
925
926     /**
927      * Gets the IP address of the client that sent the request.
928      *
929      * @return a string.
930      */

931     public String JavaDoc getRemoteAddr() {
932         if (this.remoteAddr == null) {
933             this.remoteAddr = this.getRequest().getRemoteAddr();
934         }
935
936         return this.remoteAddr;
937     }
938
939     /**
940      * Gets the qualified name of the client that sent the request.
941      *
942      * @return a string.
943      */

944     public String JavaDoc getRemoteHost() {
945         if (this.remoteHost == null) {
946             this.remoteHost = this.getRequest().getRemoteHost();
947         }
948
949         return this.remoteHost;
950     }
951
952     /**
953      * Get the user agent for the request.
954      *
955      * @return a string.
956      */

957     public String JavaDoc getUserAgent() {
958         if (this.userAgent == null) {
959             this.userAgent = this.getRequest().getHeader("User-Agent");
960         }
961
962         return this.userAgent;
963     }
964
965     /**
966      * Pulls a user and ACL object from the session and increments the access
967      * counter and sets the last access date for the object.
968      */

969     public void populate() {
970         user = getUserFromSession();
971
972         if (user != null) {
973             user.setLastAccessDate();
974             user.incrementAccessCounter();
975             user.incrementAccessCounterForSession();
976         }
977     }
978
979     /**
980      * <p>
981      * Saves this user object and ACL to the session.
982      * </p>
983      *
984      * <p>
985      * Anyone overriding this method should be sure to leverage the
986      * <code>SessionBindingEventProxy</code> when adding the user and acl into
987      * the session. This allows hook functions to be called on the
988      * <code>User</code> and <code>AccessControlList</code> when it is
989      * removed from the session (which happens on session timeout).
990      * </p>
991      *
992      * @see #setUser(User)
993      * @see #setACL(AccessControlList)
994      * @see org.apache.turbine.services.rundata.SessionBindingEventProxy
995      * @see org.apache.fulcrum.security.entity.User
996      * @see org.apache.fulcrum.security.util.AccessControlList
997      * @see org.apache.fulcrum.security.session.SessionBindingListener
998      */

999     public void save() {
1000        session.setAttribute(User.SESSION_KEY, new SessionBindingEventProxy(user));
1001        session.setAttribute(AccessControlList.SESSION_KEY, (Object JavaDoc) acl);
1002    }
1003
1004    /**
1005     * Gets the stack trace if set.
1006     *
1007     * @return the stack trace.
1008     */

1009    public String JavaDoc getStackTrace() {
1010        return stackTrace;
1011    }
1012
1013    /**
1014     * Gets the stack trace exception if set.
1015     *
1016     * @return the stack exception.
1017     */

1018    public Throwable JavaDoc getStackTraceException() {
1019        return stackTraceException;
1020    }
1021
1022    /**
1023     * Sets the stack trace.
1024     *
1025     * @param trace
1026     * the stack trace.
1027     * @param exp
1028     * the exception.
1029     */

1030    public void setStackTrace(String JavaDoc trace, Throwable JavaDoc exp) {
1031        stackTrace = trace;
1032        stackTraceException = exp;
1033    }
1034
1035    /**
1036     * Gets a table of debug variables.
1037     *
1038     * @return a hashtable for debug variables.
1039     */

1040    public Hashtable JavaDoc getVarDebug() {
1041        return varDebug;
1042    }
1043
1044    // **********************************************
1045
// Implementation of the TurbineRunData interface
1046
// **********************************************
1047

1048    /**
1049     * Gets the parameter parser without parsing the parameters.
1050     *
1051     * @return the parameter parser.
1052     */

1053    public ParameterParser getParameterParser() {
1054        return parameters;
1055    }
1056
1057    /**
1058     * Sets the parameter parser.
1059     *
1060     * @param parser
1061     * a parameter parser.
1062     */

1063    public void setParameterParser(ParameterParser parser) {
1064        parameters = parser;
1065    }
1066
1067    /**
1068     * Gets the cookie parser without parsing the cookies.
1069     *
1070     * @return the cookie parser.
1071     */

1072    public CookieParser getCookieParser() {
1073        return cookies;
1074    }
1075
1076    /**
1077     * Sets the cookie parser.
1078     *
1079     * @param parser
1080     * a cookie parser.
1081     */

1082    public void setCookieParser(CookieParser parser) {
1083        cookies = parser;
1084    }
1085
1086    /**
1087     * Sets the servlet request.
1088     *
1089     * @param req
1090     * a request.
1091     */

1092    public void setRequest(HttpServletRequest JavaDoc req) {
1093        this.req = req;
1094    }
1095
1096    /**
1097     * Sets the servlet response.
1098     *
1099     * @param res
1100     * a response.
1101     */

1102    public void setResponse(HttpServletResponse JavaDoc res) {
1103        this.res = res;
1104    }
1105
1106    /**
1107     * Sets the servlet session inforamtion.
1108     *
1109     * @param sess
1110     * a session.
1111     */

1112    public void setSession(HttpSession JavaDoc sess) {
1113        this.session = sess;
1114    }
1115
1116    /**
1117     * Setsthe servlet configuration used during servlet init.
1118     *
1119     * @param config
1120     * a configuration.
1121     */

1122    public void setServletConfig(ServletConfig JavaDoc config) {
1123        this.config = config;
1124        if (config == null) {
1125            this.servletContext = null;
1126        } else {
1127            this.servletContext = config.getServletContext();
1128        }
1129    }
1130
1131    // ********************
1132
// Miscellanous setters
1133
// ********************
1134

1135    /**
1136     * Sets the cached server scheme that is stored in the server data.
1137     *
1138     * @param ss
1139     * a string.
1140     */

1141    public void setServerScheme(String JavaDoc serverScheme) {
1142        this.serverScheme = serverScheme;
1143    }
1144
1145    /**
1146     * Sets the cached server same that is stored in the server data.
1147     *
1148     * @param sn
1149     * a string.
1150     */

1151    public void setServerName(String JavaDoc serverName) {
1152        this.serverName = serverName;
1153    }
1154
1155    /**
1156     * Sets the cached server port that is stored in the server data.
1157     *
1158     * @param port
1159     * an int.
1160     */

1161    public void setServerPort(int serverPort) {
1162        this.serverPort = serverPort;
1163    }
1164
1165    /**
1166     * Sets the cached context path that is stored in the server data.
1167     *
1168     * @param cp
1169     * a string.
1170     */

1171    public void setContextPath(String JavaDoc contextPath) {
1172        this.contextPath = contextPath;
1173    }
1174
1175    /**
1176     * Sets the cached script name that is stored in the server data.
1177     *
1178     * @param sn
1179     * a string.
1180     */

1181    public void setScriptName(String JavaDoc scriptName) {
1182        this.scriptName = scriptName;
1183    }
1184
1185    public void setMessage(String JavaDoc message) {
1186        this.message = message;
1187    }
1188
1189    public String JavaDoc getMessage() {
1190        return message;
1191    }
1192
1193    /**
1194     * Utility method for accessing the service implementation
1195     *
1196     * @return a MimeTypeService implementation instance
1197     */

1198    protected static MimeTypeService getMimeTypeService() {
1199        try {
1200        YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService(
1201                YaafiComponentService.SERVICE_NAME);
1202        return (MimeTypeService) yaafi.lookup(MimeTypeService.class.getName());
1203        } catch (Exception JavaDoc e) {
1204            throw new RuntimeException JavaDoc("Problem looking up localization service", e);
1205        }
1206    }
1207
1208    /**
1209     * Utility method for accessing the service implementation
1210     *
1211     * @return a LocalizationService implementation instance
1212     */

1213    protected static LocalizationService getLocalizationService() {
1214        try {
1215            YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService(
1216                    YaafiComponentService.SERVICE_NAME);
1217            return (LocalizationService) yaafi.lookup(LocalizationService.class.getName());
1218        } catch (Exception JavaDoc e) {
1219            throw new RuntimeException JavaDoc("Problem looking up localization service", e);
1220        }
1221    }
1222}
Popular Tags