001: /**
002: *
003: */package clime.messadmin.example;
004:
005: import java.io.IOException;
006: import java.util.ArrayList;
007: import java.util.Collection;
008: import java.util.Locale;
009:
010: import javax.servlet.ServletException;
011: import javax.servlet.http.HttpServlet;
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpServletResponse;
014: import javax.servlet.http.HttpSession;
015:
016: /**
017: * @author Cédrik LIME
018: */
019: abstract class BaseSample extends HttpServlet {
020:
021: /**
022: * Using Struts Locale location for demonstration purposes: {@value}
023: * @see org.apache.struts.Globals#LOCALE_KEY
024: */
025: private static final String STRUTS_LOCALE_KEY = "org.apache.struts.action.LOCALE";//$NON-NLS-1$
026: /**
027: * Maximum workload simulation time: {@value} ms.
028: */
029: private static final long MAX_WORKLOAD_TIME = 400;
030:
031: /**
032: * @see javax.servlet.http.HttpServlet#HttpServlet()
033: */
034: public BaseSample() {
035: super ();
036: }
037:
038: /**
039: * {@inheritDoc}
040: */
041: protected void doGet(HttpServletRequest req,
042: HttpServletResponse resp) throws ServletException,
043: IOException {
044: process(req, resp);
045: }
046:
047: /**
048: * {@inheritDoc}
049: */
050: protected void doPost(HttpServletRequest req,
051: HttpServletResponse resp) throws ServletException,
052: IOException {
053: process(req, resp);
054: }
055:
056: /**
057: *
058: * @param req
059: * @param resp
060: * @throws IOException
061: * @throws ServletException
062: */
063: protected abstract void process(HttpServletRequest req,
064: HttpServletResponse resp) throws IOException,
065: ServletException;
066:
067: protected Locale getLocale(HttpServletRequest req) {
068: return (Locale) req.getSession()
069: .getAttribute(STRUTS_LOCALE_KEY);
070: }
071:
072: protected Long getWorkload(HttpServletRequest req) {
073: return (Long) req.getAttribute("workload");//$NON-NLS-1$
074: }
075:
076: /**
077: * @param req
078: * @param resp
079: */
080: protected void doSomeWork(HttpServletRequest req,
081: HttpServletResponse resp) {
082: // Create HttpSession and set Struts java.util.Locale
083: HttpSession session = req.getSession(true);
084: Locale[] allLocales = Locale.getAvailableLocales();
085: Locale locale = (Locale) session
086: .getAttribute(STRUTS_LOCALE_KEY);
087: if (null == locale) {
088: locale = allLocales[(int) (Math.random() * allLocales.length)];
089: session.setAttribute(STRUTS_LOCALE_KEY, locale);
090: }
091: // simulate workload
092: long workload = (long) (MAX_WORKLOAD_TIME * Math.random());
093: try {
094: Thread.sleep(workload);
095: } catch (InterruptedException ise) {
096: ise.printStackTrace();
097: }
098: req.setAttribute("workload", new Long(workload));//$NON-NLS-1$
099: // set a non-serializable attribute at random
100: if (Math.random() < 0.4) {
101: Collection hiddenNonSerializableObject = new ArrayList(1);
102: hiddenNonSerializableObject
103: .add(NonSerializableObject.INSTANCE);
104: session.setAttribute("Non-Serializable attr.",
105: hiddenNonSerializableObject);
106: // } else {
107: // session.removeAttribute("Non-Serializable attr.");
108: }
109: resp.setContentType("text/html");//$NON-NLS-1$
110: setNoCache(resp);
111: }
112:
113: protected void setNoCache(HttpServletResponse resp) {
114: // <strong>NOTE</strong> - This header will be overridden
115: // automatically if a <code>RequestDispatcher.forward()</code> call is
116: // ultimately invoked.
117: //resp.setHeader("Pragma", "No-cache"); // HTTP 1.0 //$NON-NLS-1$ //$NON-NLS-2$
118: resp.setHeader("Cache-Control", "no-cache,no-store,max-age=0"); // HTTP 1.1 //$NON-NLS-1$ //$NON-NLS-2$
119: resp.setDateHeader("Expires", 0); // 0 means now //$NON-NLS-1$
120: // should we decide to enable caching, here are the current vary:
121: resp.addHeader("Vary", "Accept-Language,Accept-Encoding");
122: }
123:
124: /**
125: * This class is for demonstrating the flagging of HttpSessions with non-serializable objects attributes.
126: * It has no usefulness except that it's not Serializable.
127: * @author Cédrik LIME
128: */
129: private static class NonSerializableObject {
130: public static final NonSerializableObject INSTANCE = new NonSerializableObject();
131: }
132: }
|