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;
58 import java.io.PrintWriter;
59 import java.util.Hashtable;
60 import java.util.Locale;
61 import java.util.Vector;
62 import javax.servlet.ServletConfig;
63 import javax.servlet.ServletContext;
64 import javax.servlet.http.HttpServletRequest;
65 import javax.servlet.http.HttpServletResponse;
66 import javax.servlet.http.HttpSession;
67 import org.apache.ecs.Document;
68 import org.apache.ecs.Element;
69 import org.apache.ecs.StringElement;
70 import org.apache.turbine.om.security.User;
71 import org.apache.turbine.services.mimetype.TurbineMimeTypes;
72 import org.apache.turbine.services.resources.TurbineResources;
73 import org.apache.turbine.services.template.TurbineTemplate;
74 import org.apache.turbine.util.CookieParser;
75 import org.apache.turbine.util.FormMessages;
76 import org.apache.turbine.util.ParameterParser;
77 import org.apache.turbine.util.ServerData;
78 import org.apache.turbine.util.SystemError;
79 import org.apache.turbine.util.pool.Recyclable;
80 import org.apache.turbine.util.pool.RecyclableSupport;
81 import org.apache.turbine.util.security.AccessControlList;
82 import org.apache.turbine.util.template.TemplateInfo;
83
84 /***
85 * DefaultTurbineRunData is the default implementation of the
86 * TurbineRunData interface, which is distributed by the Turbine
87 * RunData service, if another implementation is not defined in
88 * the default or specified RunData configuration.
89 * TurbineRunData is an extension to RunData, which
90 * is an interface to run-rime information that is passed
91 * within Turbine. This provides the threading mechanism for the
92 * entire system because multiple requests can potentially come in
93 * at the same time. Thus, there is only one RunData implementation
94 * for each request that is being serviced.
95 *
96 * <p>DefaultTurbineRunData implements the Recyclable interface making
97 * it possible to pool its instances for recycling.
98 *
99 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
100 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
101 * @author <a href="mailto:bhoeneis@ee.ethz.ch">Bernie Hoeneisen</a>
102 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
103 * @version $Id: DefaultTurbineRunData.java,v 1.3 2002/07/11 16:53:25 mpoeschl Exp $
104 */
105 public class DefaultTurbineRunData
106 extends RecyclableSupport
107 implements TurbineRunData,
108 Recyclable
109 {
110 /***
111 * The default locale.
112 */
113 private static Locale defaultLocale;
114
115 /***
116 * The default locale checked flag.
117 */
118 private static boolean defaultLocaleChecked;
119
120 /***
121 * The default charset.
122 */
123 private static String defaultCharSet;
124
125 /***
126 * The default charset checked flag.
127 */
128 private static boolean defaultCharSetChecked;
129
130 /***
131 * A reference to the GET/POST data parser.
132 */
133 private ParameterParser parameters;
134
135 /***
136 * A reference to a cookie parser.
137 */
138 public CookieParser cookies;
139
140 /***
141 * The servlet request interface.
142 */
143 private HttpServletRequest req;
144
145 /***
146 * The servlet response interface.
147 */
148 private HttpServletResponse res;
149
150 /***
151 * The servlet session information.
152 */
153 private HttpSession session;
154
155 /***
156 * The servlet configuration.
157 */
158 private ServletConfig config;
159
160 /***
161 * The servlet context information.
162 * Note that this is from the
163 * "Turbine" Servlet context.
164 */
165 private ServletContext servletContext;
166
167 /***
168 * The access control list.
169 */
170 private AccessControlList acl;
171
172 /***
173 * Determines if there is information in the document or not.
174 */
175 private boolean pageSet;
176
177 /***
178 * This creates an ECS Document.
179 */
180 private Document page;
181
182 /***
183 * Cached action name to execute for this request.
184 */
185 private String action;
186
187 /***
188 * This is the layout that the page will use to render the screen.
189 */
190 private String layout;
191
192 /***
193 * Cached screen name to execute for this request.
194 */
195 private String screen;
196
197 /***
198 * The character encoding of template files.
199 */
200 private String templateEncoding;
201
202 /***
203 * Information used by a Template system (such as
204 * Velocity/Freemarker).
205 */
206 private TemplateInfo templateInfo;
207
208 /***
209 * This is where output messages from actions should go.
210 */
211 private StringElement message;
212
213 /***
214 * This is a dedicated message class where output messages from
215 * actions should go.
216 */
217 private FormMessages messages;
218
219 /***
220 * The user object.
221 */
222 private User user;
223
224 /***
225 * This is what will build the <title></title> of the document.
226 */
227 private String title;
228
229 /***
230 * Determines if there is information in the outputstream or
231 * not.
232 */
233 private boolean outSet;
234
235 /***
236 * Cache the output stream because it can be used in many
237 * different places.
238 */
239 private PrintWriter out;
240
241 /***
242 * The locale.
243 */
244 private Locale locale;
245
246 /***
247 * The HTTP charset.
248 */
249 private String charSet;
250
251 /***
252 * The HTTP content type to return.
253 */
254 private String contentType = "text/html";
255
256 /***
257 * If this is set, also set the status code to 302.
258 */
259 private String redirectURI;
260
261 /***
262 * The HTTP status code to return.
263 */
264 private int statusCode = HttpServletResponse.SC_OK;
265
266 /***
267 * This is a vector to hold critical system errors.
268 */
269 private Vector errors = new Vector();
270
271 /***
272 * JNDI Contexts.
273 */
274 private Hashtable jndiContexts;
275
276 /***
277 * Holds ServerData (basic properties) about this RunData object.
278 */
279 private ServerData serverData;
280
281 /***
282 * @see #getRemoteAddr()
283 */
284 private String remoteAddr;
285
286 /***
287 * @see #getRemoteHost()
288 */
289 private String remoteHost;
290
291 /***
292 * @see #getUserAgent()
293 */
294 private String userAgent;
295
296 /***
297 * A holder for stack trace.
298 */
299 private String stackTrace;
300
301 /***
302 * A holder ofr stack trace exception.
303 */
304 private Throwable stackTraceException;
305
306 /***
307 * Put things here and they will be shown on the default Error
308 * screen. This is great for debugging variable values when an
309 * exception is thrown.
310 */
311 private Hashtable varDebug = new Hashtable();
312
313 /***
314 * Attempts to get the User object from the session. If it does
315 * not exist, it returns null.
316 *
317 * @param session An HttpSession.
318 * @return A User.
319 */
320 public static User getUserFromSession(HttpSession session)
321 {
322 try
323 {
324 return (User) session.getValue(User.SESSION_KEY);
325 }
326 catch ( ClassCastException e )
327 {
328 return null;
329 }
330 }
331
332 /***
333 * Allows one to invalidate the user in a session.
334 *
335 * @param session An HttpSession.
336 * @return True if user was invalidated.
337 */
338 public static boolean removeUserFromSession(HttpSession session)
339 {
340 try
341 {
342 session.removeValue(User.SESSION_KEY);
343 }
344 catch ( Exception e )
345 {
346 return false;
347 }
348 return true;
349 }
350
351
352 /***
353 * Gets the default locale defined by properties named
354 * "locale.default.lang" and "locale.default.country".
355 *
356 * @return the default locale or null.
357 */
358 protected static Locale getDefaultLocale()
359 {
360 if (!defaultLocaleChecked)
361 {
362 /* Get the default locale and cache it in a static variable. */
363 String lang = TurbineResources.getString("locale.default.language");
364 String country = TurbineResources.getString("locale.default.country");
365 if (lang != null)
366 {
367 defaultLocale = country != null ?
368 new Locale(lang,country) : new Locale(lang,"");
369 }
370 else if (country != null)
371 {
372 defaultLocale = new Locale("",country);
373 }
374 else
375 {
376 defaultLocale = null;
377 }
378 defaultLocaleChecked = true;
379 }
380 return defaultLocale;
381 }
382
383 /***
384 * Gets the default charset defined by a property named
385 * "locale.default.charset" or by the specified locale.
386 * If the specified locale is null, the default locale is applied.
387 *
388 * @return the name of the default charset or null.
389 */
390 protected String getDefaultCharSet()
391 {
392 if (!defaultCharSetChecked)
393 {
394 /* Get the default charset and cache it in a static variable. */
395 defaultCharSet = TurbineResources.
396 getString("locale.default.charset");
397 defaultCharSetChecked = true;
398 }
399
400 String charset = defaultCharSet;
401 if (charset == null)
402 {
403 /* Default charset isn't specified, get the locale specific one. */
404 Locale locale = this.locale;
405 if (locale == null)
406 {
407 locale = getDefaultLocale();
408 }
409 if ((locale != null) &&
410 !locale.equals(Locale.US))
411 {
412 charset = TurbineMimeTypes.getCharSet(locale);
413 }
414 }
415 return charset;
416 }
417
418 /***
419 * Constructs a run data object.
420 */
421 public DefaultTurbineRunData()
422 {
423 super();
424 }
425
426 /***
427 * Recycles a run data object.
428 */
429 public void recycle()
430 {
431 super.recycle();
432 }
433
434 /***
435 * Disposes a run data object.
436 */
437 public void dispose()
438 {
439 parameters = null;
440 cookies = null;
441 req = null;
442 res = null;
443 session = null;
444 config = null;
445 servletContext = null;
446 acl = null;
447 pageSet = false;
448 page = null;
449 action = null;
450 layout = null;
451 screen = null;
452 templateEncoding = null;
453 templateInfo = null;
454 message = null;
455 messages = null;
456 user = null;
457 title = null;
458 outSet = false;
459 out = null;
460 locale = null;
461 charSet = null;
462 contentType = "text/html";
463 redirectURI = null;
464 statusCode = HttpServletResponse.SC_OK;
465 errors.clear();
466 jndiContexts = null;
467 serverData = null;
468 remoteAddr = null;
469 remoteHost = null;
470 userAgent = null;
471 stackTrace = null;
472 stackTraceException = null;
473 varDebug.clear();
474
475 super.dispose();
476 }
477
478 // ***************************************
479 // Implementation of the RunData interface
480 // ***************************************
481
482 /***
483 * Gets the parameters.
484 *
485 * @return a parameter parser.
486 */
487 public ParameterParser getParameters()
488 {
489 // Parse the parameters first, if not yet done.
490 if ((this.parameters != null) &&
491 (this.parameters.getRequest() != this.req))
492 {
493 this.parameters.setRequest(this.req);
494 }
495 return this.parameters;
496 }
497
498 /***
499 * Gets the cookies.
500 *
501 * @return a cookie parser.
502 */
503 public CookieParser getCookies()
504 {
505 // Parse the cookies first, if not yet done.
506 if ((this.cookies != null) &&
507 (this.cookies.getRunData() != this))
508 {
509 this.cookies.setRunData(this);
510 }
511 return this.cookies;
512 }
513
514 /***
515 * Gets the servlet request.
516 *
517 * @return the request.
518 */
519 public HttpServletRequest getRequest()
520 {
521 return this.req;
522 }
523
524 /***
525 * Gets the servlet response.
526 *
527 * @return the resposne.
528 */
529 public HttpServletResponse getResponse()
530 {
531 return this.res;
532 }
533
534 /***
535 * Gets the servlet session information.
536 *
537 * @return the session.
538 */
539 public HttpSession getSession()
540 {
541 return session;
542 }
543
544 /***
545 * Gets the servlet configuration used during servlet init.
546 *
547 * @return the configuration.
548 */
549 public ServletConfig getServletConfig()
550 {
551 return this.config;
552 }
553
554 /***
555 * Gets the servlet context used during servlet init.
556 *
557 * @return the context.
558 */
559 public ServletContext getServletContext()
560 {
561 return this.servletContext;
562 }
563
564 /***
565 * Gets the access control list.
566 *
567 * @return the access control list.
568 */
569 public AccessControlList getACL()
570 {
571 return acl;
572 }
573
574 /***
575 * Sets the access control list.
576 *
577 * @param acl an access control list.
578 */
579 public void setACL(AccessControlList acl)
580 {
581 this.acl = acl;
582 }
583
584 /***
585 * Checks to see if the page is set.
586 *
587 * @return true if the page is set.
588 * @deprecated no replacement planned, ECS is no longer a requirement
589 */
590 public boolean isPageSet()
591 {
592 return pageSet;
593 }
594
595 /***
596 * Gets the page.
597 *
598 * @return a document.
599 * @deprecated no replacement planned, ECS is no longer a requirement
600 */
601 public Document getPage()
602 {
603 pageSet = true;
604 if ( this.page == null )
605 this.page = new Document();
606 return this.page;
607 }
608
609 /***
610 * Whether or not an action has been defined.
611 *
612 * @return true if an action has been defined.
613 */
614 public boolean hasAction()
615 {
616 return ( this.action != null &&
617 this.action.length() > 0 &&
618 !this.action.equalsIgnoreCase("null") );
619 }
620
621 /***
622 * Gets the action. It returns an empty string if null so
623 * that it is easy to do conditionals on it based on the
624 * equalsIgnoreCase() method.
625 *
626 * @return a string, "" if null.
627 */
628 public String getAction()
629 {
630 return (hasAction() ? this.action : "");
631 }
632
633 /***
634 * Sets the action for the request.
635 *
636 * @param action a atring.
637 */
638 public void setAction (String action )
639 {
640 this.action = action;
641 }
642
643 /***
644 * If the Layout has not been defined by the screen then set the
645 * layout to be "DefaultLayout". The screen object can also
646 * override this method to provide intelligent determination of
647 * the Layout to execute. You can also define that logic here as
648 * well if you want it to apply on a global scale. For example,
649 * if you wanted to allow someone to define layout "preferences"
650 * where they could dynamicially change the layout for the entire
651 * site.
652 *
653 * @return a string.
654 */
655
656 public String getLayout()
657 {
658 if ( this.layout == null )
659 {
660 /*
661 * This will return something if the template
662 * services are running. If we get nothing we
663 * will fall back to the ECS layout.
664 */
665 layout = TurbineTemplate.getDefaultLayoutName(this);
666
667 if (layout == null)
668 {
669 layout = "DefaultLayout";
670 }
671 }
672
673 return this.layout;
674 }
675
676 /***
677 * Set the layout for the request.
678 *
679 * @param layout a string.
680 */
681 public void setLayout ( String layout )
682 {
683 this.layout = layout;
684 }
685
686 /***
687 * Convenience method for a template info that
688 * returns the layout template being used.
689 *
690 * @return a string.
691 */
692 public String getLayoutTemplate()
693 {
694 return getTemplateInfo().getLayoutTemplate();
695 }
696
697 /***
698 * Modifies the layout template for the screen. This convenience
699 * method allows for a layout to be modified from within a
700 * template. For example;
701 *
702 * $data.setLayoutTemplate("/NewLayout.vm")
703 *
704 * @param layout a layout template.
705 */
706 public void setLayoutTemplate(String layout)
707 {
708 getTemplateInfo().setLayoutTemplate(layout);
709 }
710
711 /***
712 * Whether or not a screen has been defined.
713 *
714 * @return true if a screen has been defined.
715 */
716 public boolean hasScreen()
717 {
718 return ( this.screen != null &&
719 this.screen.length() > 0 );
720 }
721
722 /***
723 * Gets the screen to execute.
724 *
725 * @return a string.
726 */
727 public String getScreen()
728 {
729 return (hasScreen() ? this.screen : "");
730 }
731
732 /***
733 * Sets the screen for the request.
734 *
735 * @param screen a string.
736 */
737 public void setScreen (String screen )
738 {
739 this.screen = screen;
740 }
741
742 /***
743 * Convenience method for a template info that
744 * returns the name of the template being used.
745 *
746 * @return a string.
747 */
748 public String getScreenTemplate()
749 {
750 return getTemplateInfo().getScreenTemplate();
751 }
752
753 /***
754 * Sets the screen template for the request. For
755 * example;
756 *
757 * $data.setScreenTemplate("NewScreen.vm")
758 *
759 * @param screen a screen template.
760 */
761 public void setScreenTemplate(String screen)
762 {
763 getTemplateInfo().setScreenTemplate(screen);
764 }
765
766 /***
767 * Gets the character encoding to use for reading template files.
768 *
769 * @return the template encoding or null if not specified.
770 */
771 public String getTemplateEncoding()
772 {
773 return templateEncoding;
774 }
775
776 /***
777 * Sets the character encoding to use for reading template files.
778 *
779 * @param encoding the template encoding.
780 */
781 public void setTemplateEncoding(String encoding)
782 {
783 templateEncoding = encoding;
784 }
785
786 /***
787 * Gets the template info. Creates a new one if needed.
788 *
789 * @return a template info.
790 */
791 public TemplateInfo getTemplateInfo()
792 {
793 if ( templateInfo == null )
794 {
795 templateInfo = new TemplateInfo( this );
796 }
797 return templateInfo;
798 }
799
800 /***
801 * Whether or not a message has been defined.
802 *
803 * @return true if a message has been defined.
804 */
805 public boolean hasMessage()
806 {
807 return ( this.message != null &&
808 this.message.toString().length() > 0 );
809 }
810
811 /***
812 * Gets the results of an action or another message
813 * to be displayed as a string.
814 *
815 * @return a string.
816 */
817 public String getMessage()
818 {
819 return (this.message == null ? null : this.message.toString());
820 }
821
822 /***
823 * Sets the message for the request as a string.
824 *
825 * @param msg a string.
826 */
827 public void setMessage (String msg )
828 {
829 this.message = new StringElement(msg);
830 }
831
832 /***
833 * Adds the string to message. If message has prior messages from
834 * other actions or screens, this method can be used to chain them.
835 *
836 * @param msg a string.
837 */
838 public void addMessage(String msg)
839 {
840 addMessage(new StringElement(msg));
841 }
842
843 /***
844 * Gets the results of an action or another message
845 * to be displayed as an ECS string element.
846 *
847 * @return a string element.
848 */
849 public StringElement getMessageAsHTML()
850 {
851 return this.message;
852 }
853
854 /***
855 * Sets the message for the request as an ECS element.
856 *
857 * @param msg an element.
858 */
859 public void setMessage (Element msg )
860 {
861 this.message = new StringElement(msg);
862 }
863
864 /***
865 * Adds the ECS element to message. If message has prior messages from
866 * other actions or screens, this method can be used to chain them.
867 *
868 * @param msg an element.
869 */
870 public void addMessage(Element msg)
871 {
872 if (msg != null)
873 {
874 if ( message != null )
875 {
876 message.addElement(msg);
877 }
878 else
879 {
880 message = new StringElement(msg);
881 }
882 }
883 }
884
885 /***
886 * Unsets the message for the request.
887 */
888 public void unsetMessage ( )
889 {
890 this.message = null;
891 }
892
893 /***
894 * Gets a FormMessages object where all the messages to the
895 * user should be stored.
896 *
897 * @return a FormMessages.
898 */
899 public FormMessages getMessages()
900 {
901 if (this.messages == null)
902 {
903 this.messages = new FormMessages();
904 }
905 return this.messages;
906 }
907
908 /***
909 * Sets the FormMessages object for the request.
910 *
911 * @param msgs A FormMessages.
912 */
913 public void setMessages (FormMessages msgs)
914 {
915 this.messages = msgs;
916 }
917
918 /***
919 * Gets the title of the page.
920 *
921 * @return a string.
922 */
923 public String getTitle()
924 {
925 return (this.title == null ? "" : this.title);
926 }
927
928 /***
929 * Sets the title of the page.
930 *
931 * @param title a string.
932 */
933 public void setTitle (String title )
934 {
935 this.title = title;
936 }
937
938 /***
939 * Checks if a user exists in this session.
940 *
941 * @return true if a user exists in this session.
942 */
943 public boolean userExists()
944 {
945 user = getUserFromSession();
946 return ( user != null );
947 }
948
949 /***
950 * Gets the user.
951 *
952 * @return a user.
953 */
954 public User getUser()
955 {
956 return this.user;
957 }
958
959 /***
960 * Sets the user.
961 *
962 * @param user a user.
963 */
964 public void setUser(User user)
965 {
966 this.user = user;
967 }
968
969 /***
970 * Attempts to get the user from the session. If it does
971 * not exist, it returns null.
972 *
973 * @return a user.
974 */
975 public User getUserFromSession()
976 {
977 return getUserFromSession(session);
978 }
979
980 /***
981 * Allows one to invalidate the user in the default session.
982 *
983 * @return true if user was invalidated.
984 */
985 public boolean removeUserFromSession()
986 {
987 return removeUserFromSession(session);
988 }
989
990 /***
991 * Checks to see if out is set.
992 *
993 * @return true if out is set.
994 * @deprecated no replacement planned, response writer will not be cached
995 */
996 public boolean isOutSet()
997 {
998 return outSet;
999 }
1000
1001 /***
1002 * Gets the print writer. First time calling this
1003 * will set the print writer via the response.
1004 *
1005 * @return a print writer.
1006 * @throws IOException.
1007 */
1008 public PrintWriter getOut()
1009 throws IOException
1010 {
1011 // Check to see if null first.
1012 if ( this.out == null )
1013 setOut(res.getWriter());
1014 pageSet = false;
1015 outSet = true;
1016 return this.out;
1017 }
1018
1019 /***
1020 * Declares that output will be direct to the response stream,
1021 * even though getOut() may never be called. Useful for response
1022 * mechanisms that may call res.getWriter() themselves
1023 * (such as JSP.)
1024 */
1025 public void declareDirectResponse()
1026 {
1027 outSet = true;
1028 pageSet = false;
1029 }
1030
1031 /***
1032 * Gets the locale. If it has not already been defined with
1033 * setLocale(), then properties named "locale.default.lang"
1034 * and "locale.default.country" are checked from the Resource
1035 * Service and the corresponding locale is returned. If these
1036 * properties are undefined, JVM's default locale is returned.
1037 *
1038 * @return the locale.
1039 */
1040 public Locale getLocale()
1041 {
1042 Locale locale = this.locale;
1043 if (locale == null)
1044 {
1045 locale = getDefaultLocale();
1046 if (locale == null)
1047 {
1048 locale = Locale.getDefault();
1049 }
1050 }
1051 return locale;
1052 }
1053
1054 /***
1055 * Sets the locale.
1056 *
1057 * @param locale the new locale.
1058 */
1059 public void setLocale(Locale locale)
1060 {
1061 this.locale = locale;
1062 }
1063
1064 /***
1065 * Gets the charset. If it has not already been defined with
1066 * setCharSet(), then a property named "locale.default.charset"
1067 * is checked from the Resource Service and returned. If this
1068 * property is undefined, the default charset of the locale
1069 * is returned. If the locale is undefined, null is returned.
1070 *
1071 * @return the name of the charset or null.
1072 */
1073 public String getCharSet()
1074 {
1075 String charset = this.charSet;
1076 if (charset == null)
1077 {
1078 charset = getDefaultCharSet();
1079 }
1080 return charset;
1081 }
1082
1083 /***
1084 * Sets the charset.
1085 *
1086 * @param charset the name of the new charset.
1087 */
1088 public void setCharSet(String charset)
1089 {
1090 this.charSet = charset;
1091 }
1092
1093 /***
1094 * Gets the HTTP content type to return. If a charset
1095 * has been specified, it is included in the content type.
1096 * If the charset has not been specified and the main type
1097 * of the content type is "text", the default charset is
1098 * included. If the default charset is undefined, but the
1099 * default locale is defined and it is not the US locale,
1100 * a locale specific charset is included.
1101 *
1102 * @return the content type or an empty string.
1103 */
1104 public String getContentType()
1105 {
1106 String ct = this.contentType;
1107 if (ct != null)
1108 {
1109 String charset = this.charSet;
1110 if (charset == null)
1111 {
1112 if (ct.startsWith("text/"))
1113 {
1114 charset = getDefaultCharSet();
1115 if (charset != null)
1116 {
1117 ct += "; charset=" + charset;
1118 }
1119 }
1120 }
1121 else
1122 {
1123 ct += "; charset=" + charset;
1124 }
1125 }
1126 else
1127 {
1128 ct = "";
1129 }
1130 return ct;
1131 }
1132
1133 /***
1134 * Sets the HTTP content type to return.
1135 *
1136 * @param ct a string.
1137 */
1138 public void setContentType(String ct)
1139 {
1140 this.contentType = ct;
1141 }
1142
1143 /***
1144 * Gets the redirect URI. If this is set, also make sure to set
1145 * the status code to 302.
1146 *
1147 * @return a string, "" if null.
1148 */
1149 public String getRedirectURI()
1150 {
1151 return (this.redirectURI == null ? "" : redirectURI);
1152 }
1153
1154 /***
1155 * Sets the redirect uri. If this is set, also make sure to set
1156 * the status code to 302.
1157 *
1158 * @param ruri a string.
1159 */
1160 public void setRedirectURI(String ruri)
1161 {
1162 this.redirectURI = ruri;
1163 }
1164
1165 /***
1166 * Gets the HTTP status code to return.
1167 *
1168 * @return the status.
1169 */
1170 public int getStatusCode()
1171 {
1172 return statusCode;
1173 }
1174
1175 /***
1176 * Sets the HTTP status code to return.
1177 *
1178 * @param sc the status.
1179 */
1180 public void setStatusCode(int sc)
1181 {
1182 this.statusCode = sc;
1183 }
1184
1185 /***
1186 * Gets an array of system errors.
1187 *
1188 * @return a SystemError[].
1189 */
1190 public SystemError[] getSystemErrors()
1191 {
1192 SystemError[] result = new SystemError[errors.size()];
1193 errors.copyInto(result);
1194 return result;
1195 }
1196
1197 /***
1198 * Adds a critical system error.
1199 *
1200 * @param err a system error.
1201 */
1202 public void setSystemError (SystemError err)
1203 {
1204 this.errors.addElement( err );
1205 }
1206
1207 /***
1208 * Gets JNDI Contexts.
1209 *
1210 * @return a hashtable.
1211 */
1212 public Hashtable getJNDIContexts()
1213 {
1214 if ( jndiContexts == null )
1215 jndiContexts = new Hashtable();
1216 return jndiContexts;
1217 }
1218
1219 /***
1220 * Sets JNDI Contexts.
1221 *
1222 * @param contexts a hashtable.
1223 */
1224 public void setJNDIContexts(Hashtable contexts)
1225 {
1226 this.jndiContexts = contexts;
1227 }
1228
1229 /***
1230 * Gets the cached server scheme.
1231 *
1232 * @return a string.
1233 */
1234 public String getServerScheme()
1235 {
1236 return getServerData().getServerScheme();
1237 }
1238
1239 /***
1240 * Gets the cached server name.
1241 *
1242 * @return a string.
1243 */
1244 public String getServerName()
1245 {
1246 return getServerData().getServerName();
1247 }
1248
1249 /***
1250 * Gets the cached server port.
1251 *
1252 * @return an int.
1253 */
1254 public int getServerPort()
1255 {
1256 return getServerData().getServerPort();
1257 }
1258
1259 /***
1260 * Gets the cached context path.
1261 *
1262 * @return a string.
1263 */
1264 public String getContextPath()
1265 {
1266 return getServerData().getContextPath();
1267 }
1268
1269 /***
1270 * Gets the cached script name.
1271 *
1272 * @return a string.
1273 */
1274 public String getScriptName()
1275 {
1276 return getServerData().getScriptName();
1277 }
1278
1279 /***
1280 * Gets the server data ofy the request.
1281 *
1282 * @return server data.
1283 */
1284 public ServerData getServerData()
1285 {
1286 return this.serverData;
1287 }
1288
1289 /***
1290 * Gets the IP address of the client that sent the request.
1291 *
1292 * @return a string.
1293 */
1294 public String getRemoteAddr()
1295 {
1296 if ( this.remoteAddr == null )
1297 {
1298 this.remoteAddr = this.getRequest().getRemoteAddr();
1299 }
1300
1301 return this.remoteAddr;
1302 }
1303
1304 /***
1305 * Gets the qualified name of the client that sent the request.
1306 *
1307 * @return a string.
1308 */
1309 public String getRemoteHost()
1310 {
1311 if ( this.remoteHost == null )
1312 {
1313 this.remoteHost = this.getRequest().getRemoteHost();
1314 }
1315
1316 return this.remoteHost;
1317 }
1318
1319 /***
1320 * Get the user agent for the request.
1321 *
1322 * @return a string.
1323 */
1324 public String getUserAgent()
1325 {
1326 if ( this.userAgent == null )
1327 {
1328 this.userAgent = this.getRequest().getHeader( "User-Agent" );
1329 }
1330
1331 return this.userAgent;
1332 }
1333
1334 /***
1335 * Pulls a user object from the session and increments the access
1336 * counter and sets the last access date for the object.
1337 */
1338 public void populate()
1339 {
1340 user = getUserFromSession();
1341
1342 if ( user != null )
1343 {
1344 user.setLastAccessDate();
1345 user.incrementAccessCounter();
1346 user.incrementAccessCounterForSession();
1347 }
1348 }
1349
1350 /***
1351 * Saves a user object into the session.
1352 */
1353 public void save()
1354 {
1355 session.putValue(User.SESSION_KEY, (Object) user );
1356 }
1357
1358 /***
1359 * Gets the stack trace if set.
1360 *
1361 * @return the stack trace.
1362 */
1363 public String getStackTrace()
1364 {
1365 return stackTrace;
1366 }
1367
1368 /***
1369 * Gets the stack trace exception if set.
1370 *
1371 * @return the stack exception.
1372 */
1373 public Throwable getStackTraceException()
1374 {
1375 return stackTraceException;
1376 }
1377
1378 /***
1379 * Sets the stack trace.
1380 *
1381 * @param trace the stack trace.
1382 * @param exp the exception.
1383 */
1384 public void setStackTrace(String trace,
1385 Throwable exp)
1386 {
1387 stackTrace = trace;
1388 stackTraceException = exp;
1389 }
1390
1391 /***
1392 * Gets a table of debug variables.
1393 *
1394 * @return a hashtable for debug variables.
1395 */
1396 public Hashtable getVarDebug()
1397 {
1398 return varDebug;
1399 }
1400
1401 // **********************************************
1402 // Implementation of the TurbineRunData interface
1403 // **********************************************
1404
1405 /***
1406 * Gets the parameter parser without parsing the parameters.
1407 *
1408 * @return the parameter parser.
1409 */
1410 public ParameterParser getParameterParser()
1411 {
1412 return parameters;
1413 }
1414
1415 /***
1416 * Sets the parameter parser.
1417 *
1418 * @param parser a parameter parser.
1419 */
1420 public void setParameterParser(ParameterParser parser)
1421 {
1422 parameters = parser;
1423 }
1424
1425 /***
1426 * Gets the cookie parser without parsing the cookies.
1427 *
1428 * @return the cookie parser.
1429 */
1430 public CookieParser getCookieParser()
1431 {
1432 return cookies;
1433 }
1434
1435 /***
1436 * Sets the cookie parser.
1437 *
1438 * @param parser a cookie parser.
1439 */
1440 public void setCookieParser(CookieParser parser)
1441 {
1442 cookies = parser;
1443 }
1444
1445 /***
1446 * Sets the servlet request.
1447 *
1448 * @param req a request.
1449 */
1450 public void setRequest(HttpServletRequest req)
1451 {
1452 this.req = req;
1453 }
1454
1455 /***
1456 * Sets the servlet response.
1457 *
1458 * @param res a response.
1459 */
1460 public void setResponse(HttpServletResponse res)
1461 {
1462 this.res = res;
1463 }
1464
1465 /***
1466 * Sets the servlet session inforamtion.
1467 *
1468 * @param sess a session.
1469 */
1470 public void setSession(HttpSession sess)
1471 {
1472 this.session = sess;
1473 }
1474
1475 /***
1476 * Setsthe servlet configuration used during servlet init.
1477 *
1478 * @param config a configuration.
1479 */
1480 public void setServletConfig(ServletConfig config)
1481 {
1482 this.config = config;
1483 if (config == null)
1484 {
1485 this.servletContext = null;
1486 }
1487 else
1488 {
1489 this.servletContext = config.getServletContext();
1490 }
1491 }
1492
1493 /***
1494 * Sets the server data of the request.
1495 *
1496 * @param serverData server data.
1497 */
1498 public void setServerData(ServerData serverData)
1499 {
1500 this.serverData = serverData;
1501 }
1502
1503 // ********************
1504 // Miscellanous setters
1505 // ********************
1506
1507 /***
1508 * Sets the print writer.
1509 *
1510 * @param out a print writer.
1511 * @deprecated no replacement planned, response writer will not be cached
1512 */
1513 protected void setOut(PrintWriter out)
1514 {
1515 this.out = out;
1516 }
1517
1518 /***
1519 * Sets the cached server scheme that is stored in the server data.
1520 *
1521 * @param ss a string.
1522 */
1523 protected void setServerScheme(String ss)
1524 {
1525 getServerData().setServerScheme(ss);
1526 }
1527
1528 /***
1529 * Sets the cached server same that is stored in the server data.
1530 *
1531 * @param sn a string.
1532 */
1533 protected void setServerName(String sn)
1534 {
1535 getServerData().setServerName(sn);
1536 }
1537
1538 /***
1539 * Sets the cached server port that is stored in the server data.
1540 *
1541 * @param port an int.
1542 */
1543 protected void setServerPort(int port)
1544 {
1545 getServerData().setServerPort(port);
1546 }
1547
1548 /***
1549 * Sets the cached context path that is stored in the server data.
1550 *
1551 * @param cp a string.
1552 */
1553 protected void setContextPath(String cp)
1554 {
1555 getServerData().setContextPath(cp);
1556 }
1557
1558 /***
1559 * Sets the cached script name that is stored in the server data.
1560 *
1561 * @param sn a string.
1562 */
1563 protected void setScriptName(String sn)
1564 {
1565 getServerData().setScriptName(sn);
1566 }
1567 }
This page was automatically generated by Maven