001package org.apache.turbine.util;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import java.io.IOException;
025import java.io.PrintWriter;
026import java.nio.charset.Charset;
027import java.util.Locale;
028import java.util.Map;
029
030import javax.naming.Context;
031import javax.servlet.ServletConfig;
032import javax.servlet.ServletContext;
033import javax.servlet.http.HttpServletRequest;
034import javax.servlet.http.HttpServletResponse;
035import javax.servlet.http.HttpSession;
036
037import org.apache.fulcrum.parser.CookieParser;
038import org.apache.fulcrum.parser.ParameterParser;
039import org.apache.fulcrum.security.acl.AccessControlList;
040import org.apache.turbine.om.security.User;
041import org.apache.turbine.pipeline.PipelineData;
042import org.apache.turbine.util.template.TemplateInfo;
043
044/**
045 * RunData is an interface to run-time information that is passed
046 * within Turbine. This provides the threading mechanism for the
047 * entire system because multiple requests can potentially come in
048 * at the same time.  Thus, there is only one RunData instance
049 * for each request that is being serviced.
050 *
051 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
052 * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
053 * @author <a href="mailto:bhoeneis@ee.ethz.ch">Bernie Hoeneisen</a>
054 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
055 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
056 * @version $Id$
057 */
058public interface RunData extends PipelineData
059{
060    /**
061     * Gets the parameters.
062     *
063     * @return a parameter parser.
064     */
065    ParameterParser getParameters();
066
067    /**
068     * Gets the cookies.
069     *
070     * @return a cookie parser.
071     */
072    CookieParser getCookies();
073
074    /**
075     * Gets the servlet request.
076     *
077     * @return the request.
078     */
079    HttpServletRequest getRequest();
080
081    /**
082     * Gets the servlet response.
083     *
084     * @return the resposne.
085     */
086    HttpServletResponse getResponse();
087
088    /**
089     * Gets the servlet session information.
090     *
091     * @return the session.
092     */
093    HttpSession getSession();
094
095    /**
096     * Gets the servlet configuration used during servlet init.
097     *
098     * @return the configuration.
099     */
100    ServletConfig getServletConfig();
101
102    /**
103     * Gets the servlet context used during servlet init.
104     *
105     * @return the context.
106     */
107    ServletContext getServletContext();
108
109    /**
110     * Gets the access control list.
111     *
112     * @return the access control list.
113     *
114     * @param <A> a type extending {@link AccessControlList}
115     */
116    <A extends AccessControlList> A getACL();
117
118    /**
119     * Sets the access control list.
120     *
121     * @param <A> ACL type
122     * @param acl an access control list.
123     */
124    <A extends AccessControlList> void setACL(A acl);
125
126    /**
127     * Whether or not an action has been defined.
128     *
129     * @return true if an action has been defined.
130     */
131    boolean hasAction();
132
133    /**
134     * Gets the action. It returns an empty string if null so
135     * that it is easy to do conditionals on it based on the
136     * equalsIgnoreCase() method.
137     *
138     * @return a string, "" if null.
139     */
140    String getAction();
141
142    /**
143     * Sets the action for the request.
144     *
145     * @param action a string.
146     */
147    void setAction(String action);
148
149    /**
150     * If the Layout has not been defined by the screen then set the
151     * layout to be "DefaultLayout".  The screen object can also
152     * override this method to provide intelligent determination of
153     * the Layout to execute.  You can also define that logic here as
154     * well if you want it to apply on a global scale.  For example,
155     * if you wanted to allow someone to define layout "preferences"
156     * where they could dynamically change the layout for the entire
157     * site.
158     *
159     * @return a string.
160     */
161    String getLayout();
162
163    /**
164     * Set the layout for the request.
165     *
166     * @param layout a string.
167     */
168    void setLayout(String layout);
169
170    /**
171     * Convenience method for a template info that
172     * returns the layout template being used.
173     *
174     * @return a string.
175     */
176    String getLayoutTemplate();
177
178    /**
179     * Modifies the layout template for the screen. This convenience
180     * method allows for a layout to be modified from within a
181     * template. For example;
182     *
183     *    $data.setLayoutTemplate("NewLayout.vm")
184     *
185     * @param layout a layout template.
186     */
187    void setLayoutTemplate(String layout);
188
189    /**
190     * Whether or not a screen has been defined.
191     *
192     * @return true if a screen has been defined.
193     */
194    boolean hasScreen();
195
196    /**
197     * Gets the screen to execute.
198     *
199     * @return a string.
200     */
201    String getScreen();
202
203    /**
204     * Sets the screen for the request.
205     *
206     * @param screen a string.
207     */
208    void setScreen(String screen);
209
210    /**
211     * Convenience method for a template info that
212     * returns the name of the template being used.
213     *
214     * @return a string.
215     */
216    String getScreenTemplate();
217
218    /**
219     * Sets the screen template for the request. For
220     * example;
221     *
222     *    $data.setScreenTemplate("NewScreen.vm")
223     *
224     * @param screen a screen template.
225     */
226    void setScreenTemplate(String screen);
227
228    /**
229     * Gets the character encoding to use for reading template files.
230     *
231     * @return the template encoding or null if not specified.
232     */
233    String getTemplateEncoding();
234
235    /**
236     * Sets the character encoding to use for reading template files.
237     *
238     * @param encoding the template encoding.
239     */
240    void setTemplateEncoding(String encoding);
241
242    /**
243     * Gets the template info. Creates a new one if needed.
244     *
245     * @return a template info.
246     */
247    TemplateInfo getTemplateInfo();
248
249    /**
250     * Whether or not a message has been defined.
251     *
252     * @return true if a message has been defined.
253     */
254    boolean hasMessage();
255
256    /**
257     * Gets the results of an action or another message
258     * to be displayed as a string.
259     *
260     * @return a string.
261     */
262    String getMessage();
263
264    /**
265     * Sets the message for the request as a string.
266     *
267     * @param msg a string.
268     */
269    void setMessage(String msg);
270
271    /**
272     * Adds the string to message. If message has prior messages from
273     * other actions or screens, this method can be used to chain them.
274     *
275     * @param msg a string.
276     */
277    void addMessage(String msg);
278
279    /**
280     * Gets the results of an action or another message
281     * to be displayed as a string.
282     *
283     * @return a string.
284     */
285    String getMessageAsHTML();
286
287    /**
288     * Unsets the message for the request.
289     */
290    void unsetMessage();
291
292    /**
293     * Gets a FormMessages object where all the messages to the
294     * user should be stored.
295     *
296     * @return a FormMessages.
297     */
298    FormMessages getMessages();
299
300    /**
301     * Sets the FormMessages object for the request.
302     *
303     * @param msgs A FormMessages.
304     */
305    void setMessages(FormMessages msgs);
306
307    /**
308     * Gets the title of the page.
309     *
310     * @return a string.
311     */
312    String getTitle();
313
314    /**
315     * Sets the title of the page.
316     *
317     * @param title a string.
318     */
319    void setTitle(String title);
320
321    /**
322     * Checks if a user exists in this session.
323     *
324     * @return true if a user exists in this session.
325     */
326    boolean userExists();
327
328    /**
329     * Gets the user.
330     *
331     * @param <T> a type extending {@link User}
332     *
333     * @return a user.
334     */
335    <T extends User> T getUser();
336
337    /**
338     * Sets the user.
339     *
340     * @param user a user.
341     *
342     * @param <T> a type extending {@link User}
343     */
344    <T extends User> void setUser(T user);
345
346    /**
347     * Attempts to get the user from the session. If it does
348     * not exist, it returns null.
349     *
350     * @return a user.
351     *
352     * @param <T> a type extending {@link User}
353     */
354    <T extends User> T getUserFromSession();
355
356    /**
357     * Allows one to invalidate the user in the default session.
358     *
359     * @return true if user was invalidated.
360     */
361    boolean removeUserFromSession();
362
363    /**
364     * Checks to see if out is set.
365     *
366     * @return true if out is set.
367     * @deprecated no replacement planned, response writer will not be cached
368     */
369    @Deprecated
370    boolean isOutSet();
371
372    /**
373     * Gets the print writer. First time calling this
374     * will set the print writer via the response.
375     *
376     * @return a print writer.
377     * @throws IOException on failure getting the PrintWriter
378     */
379    PrintWriter getOut()
380            throws IOException;
381
382    /**
383     * Declares that output will be direct to the response stream,
384     * even though getOut() may never be called.  Useful for response
385     * mechanisms that may call res.getWriter() themselves
386     * (such as JSP.)
387     */
388    void declareDirectResponse();
389
390    /**
391     * Gets the locale. If it has not already been defined with
392     * setLocale(), then  properties named "locale.default.lang"
393     * and "locale.default.country" are checked from the Resource
394     * Service and the corresponding locale is returned. If these
395     * properties are undefined, JVM's default locale is returned.
396     *
397     * @return the locale.
398     */
399    Locale getLocale();
400
401    /**
402     * Sets the locale.
403     *
404     * @param locale the new locale.
405     */
406    void setLocale(Locale locale);
407
408    /**
409     * Gets the charset. If it has not already been defined with
410     * setCharSet(), then a property named "locale.default.charset"
411     * is checked from the Resource Service and returned. If this
412     * property is undefined, the default charset of the locale
413     * is returned. If the locale is undefined, null is returned.
414     *
415     * @return the name of the charset or null.
416     */
417    @Deprecated
418    String getCharSet();
419
420    /**
421     * Sets the charset.
422     *
423     * @param charset the name of the new charset.
424     */
425    @Deprecated
426    void setCharSet(String charset);
427
428    /**
429     * Gets the charset. If it has not already been defined with
430     * setCharSet(), then a property named "locale.default.charset"
431     * is checked from the Resource Service and returned. If this
432     * property is undefined, the default charset of the locale
433     * is returned. If the locale is undefined, null is returned.
434     *
435     * @return the charset or null.
436     */
437    Charset getCharset();
438
439    /**
440     * Sets the charset.
441     *
442     * @param charset the new charset.
443     */
444    void setCharset(Charset charset);
445
446    /**
447     * Gets the HTTP content type to return. If a charset
448     * has been specified, it is included in the content type.
449     * If the charset has not been specified and the main type
450     * of the content type is "text", the default charset is
451     * included. If the default charset is undefined, but the
452     * default locale is defined and it is not the US locale,
453     * a locale specific charset is included.
454     *
455     * @return the content type or an empty string.
456     */
457    String getContentType();
458
459    /**
460     * Sets the HTTP content type to return.
461     *
462     * @param ct the new content type.
463     */
464    void setContentType(String ct);
465
466    /**
467     * Gets the redirect URI. If this is set, also make sure to set
468     * the status code to 302.
469     *
470     * @return a string, "" if null.
471     */
472    String getRedirectURI();
473
474    /**
475     * Sets the redirect uri. If this is set, also make sure to set
476     * the status code to 302.
477     *
478     * @param ruri a string.
479     */
480    void setRedirectURI(String ruri);
481
482    /**
483     * Gets the HTTP status code to return.
484     *
485     * @return the status.
486     */
487    int getStatusCode();
488
489    /**
490     * Sets the HTTP status code to return.
491     *
492     * @param sc the status.
493     */
494    void setStatusCode(int sc);
495
496    /**
497     * Gets an array of system errors.
498     *
499     * @return a SystemError[].
500     */
501    SystemError[] getSystemErrors();
502
503    /**
504     * Adds a critical system error.
505     *
506     * @param err a system error.
507     */
508    void setSystemError(SystemError err);
509
510    /**
511     * Gets JNDI Contexts.
512     *
513     * @return a hashtable.
514     */
515    Map<String, Context> getJNDIContexts();
516
517    /**
518     * Sets JNDI Contexts.
519     *
520     * @param contexts a hashtable.
521     */
522    void setJNDIContexts(Map<String, Context> contexts);
523
524    /**
525     * Gets the cached server scheme.
526     *
527     * @return a string.
528     */
529    String getServerScheme();
530
531    /**
532     * Gets the cached server name.
533     *
534     * @return a string.
535     */
536    String getServerName();
537
538    /**
539     * Gets the cached server port.
540     *
541     * @return an int.
542     */
543    int getServerPort();
544
545    /**
546     * Gets the cached context path.
547     *
548     * @return a string.
549     */
550    String getContextPath();
551
552    /**
553     * Gets the cached script name.
554     *
555     * @return a string.
556     */
557    String getScriptName();
558
559    /**
560     * Gets the server data used by the request.
561     *
562     * @return server data.
563     */
564    ServerData getServerData();
565
566    /**
567     * Gets the IP address of the client that sent the request.
568     *
569     * @return a string.
570     */
571    String getRemoteAddr();
572
573    /**
574     * Gets the qualified name of the client that sent the request.
575     *
576     * @return a string.
577     */
578    String getRemoteHost();
579
580    /**
581     * Get the user agent for the request.
582     *
583     * @return a string.
584     */
585    String getUserAgent();
586
587    /**
588     * Pulls a user object from the session and increments the access
589     * counter and sets the last access date for the object.
590     */
591    void populate();
592
593    /**
594     * Saves a user object into the session.
595     */
596    void save();
597
598    /**
599     * Gets the stack trace if set.
600     *
601     * @return the stack trace.
602     */
603    String getStackTrace();
604
605    /**
606     * Gets the stack trace exception if set.
607     *
608     * @return the stack exception.
609     */
610    Throwable getStackTraceException();
611
612    /**
613     * Sets the stack trace.
614     *
615     * @param trace the stack trace.
616     * @param exp the exception.
617     */
618    void setStackTrace(String trace,
619                       Throwable exp);
620
621    /**
622     * Sets a name/value pair in an internal Map that is accessible from the
623     * Error screen.  This is a good way to get debugging information
624     * when an exception is thrown.
625     *
626     * @param name name of the variable
627     * @param value value of the variable.
628     */
629    void setDebugVariable(String name, Object value);
630
631    /**
632     * Gets a Map of debug variables.
633     *
634     * @return a Map of debug variables.
635     */
636    Map<String, Object> getDebugVariables();
637}