KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > server22 > HttpResponse


1 package com.quadcap.http.server22;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.util.Calendar JavaDoc;
42 import java.util.Date JavaDoc;
43 import java.util.Enumeration JavaDoc;
44 import java.util.Hashtable JavaDoc;
45 import java.util.Locale JavaDoc;
46 import java.util.TimeZone JavaDoc;
47 import java.util.Vector JavaDoc;
48
49 import java.text.DateFormat JavaDoc;
50 import java.text.SimpleDateFormat JavaDoc;
51
52 import java.io.ByteArrayOutputStream JavaDoc;
53 import java.io.IOException JavaDoc;
54 import java.io.OutputStream JavaDoc;
55 import java.io.OutputStreamWriter JavaDoc;
56 import java.io.PrintWriter JavaDoc;
57
58 import javax.servlet.ServletOutputStream JavaDoc;
59 import javax.servlet.http.Cookie JavaDoc;
60 import javax.servlet.http.HttpServletResponse JavaDoc;
61
62 import com.quadcap.net.server.WorkerOutputStream;
63
64 import com.quadcap.util.Debug;
65
66 /**
67  * An HTTP servlet response. This interface allows a servlet's
68  * <code>service</code> method to manipulate HTTP-protocol specified
69  * header information and return data to its client. It is implemented
70  * by network service developers for use within servlets.
71  *
72  * @author Stan Bailes
73  */

74 public class HttpResponse implements HttpServletResponse JavaDoc {
75     WebWorker w;
76     Hashtable JavaDoc headers = new Hashtable JavaDoc();
77     String JavaDoc protocol;
78     int sc = SC_OK;
79     String JavaDoc sm = "Request Completed";
80     boolean changedCode = false;
81     Locale JavaDoc defaultLocale = Locale.getDefault();
82     Locale JavaDoc locale = defaultLocale;
83
84     int contentLength = -1;
85     String JavaDoc contentType = null;
86     StringBuffer JavaDoc headerBuf = new StringBuffer JavaDoc();
87     boolean headerBufValid = true;
88     boolean anyHeaders = false;
89     boolean keepAlive = false;
90     public boolean getWriterCalled = false;
91     public boolean getOutputStreamCalled = false;
92
93     byte[] fixedHeaders = null;
94     ByteArrayOutputStream JavaDoc fixedH = new ByteArrayOutputStream JavaDoc();
95     
96     HttpOutputStream os;
97     PrintWriter JavaDoc writer = null;
98     JspWriter jWriter = new JspWriter(8192, true);
99     boolean jWriterInit = false;
100
101     Vector JavaDoc cookies = null;
102     long calbase = -1;
103     byte[] curTime = null;
104
105     static DateFormat JavaDoc cookieDateFormat;
106     static TimeZone JavaDoc GMT;
107     static SimpleDateFormat JavaDoc df;
108     static SimpleDateFormat JavaDoc df2;
109
110     static {
111         GMT = TimeZone.getTimeZone("GMT");
112         cookieDateFormat = new SimpleDateFormat JavaDoc("E, dd-MMM-yy HH:mm:ss 'GMT'");
113         cookieDateFormat.setTimeZone(GMT);
114         df = new SimpleDateFormat JavaDoc("'Date: 'EEE, dd MMM yyyy HH:mm:ss 'GMT\r\n'");
115         df.setTimeZone(GMT);
116         df2 = new SimpleDateFormat JavaDoc("EEE, dd MMM yyyy HH:mm:ss 'GMT'");
117         df2.setTimeZone(GMT);
118     }
119
120     /**
121      * Construct a new HttpResponse for the specified worker.
122      *
123      * @param w the worker handling this http request
124      */

125     public HttpResponse(WebWorker w) {
126     this.w = w;
127     }
128
129     public void reset(HttpOutputStream os) {
130     this.os = os;
131         this.os.setResponse(this);
132         headers.clear();
133         sc = SC_OK;
134         jWriterInit = false;
135         sm = "Request Completed";
136         changedCode = false;
137         locale = defaultLocale;
138         contentLength = -1;
139         contentType = null;
140         headerBuf.setLength(0);
141         headerBufValid = true;
142         writer = null;
143         cookies = null;
144         anyHeaders = false;
145         getWriterCalled = false;
146         getOutputStreamCalled = false;
147     }
148
149     void setProtocol(String JavaDoc p) {
150         this.protocol = p;
151     }
152
153     void setKeepAlive(boolean v) {
154         this.keepAlive = v;
155     }
156
157     static final void write(OutputStream JavaDoc out, String JavaDoc s) throws IOException JavaDoc {
158     for (int i = 0; i < s.length(); i++) {
159         out.write((byte)s.charAt(i));
160     }
161     }
162
163     static final byte[] headerSep = { (byte)':', (byte)' ' };
164     static final byte[] CRLF = { (byte)'\r', (byte)'\n' };
165     static final byte[] OKRESPONSE =
166     " 200 Request Completed\r\n".getBytes();
167     static final byte[] CONTENT_TYPE = "Content-Type: ".getBytes();
168     static final byte[] CONTENT_LENGTH = "Content-Length: ".getBytes();
169     static final byte[] CONNECTION_KEEPALIVE =
170     "Connection: Keep-Alive\r\n".getBytes();
171     static final byte[] CONNECTION_CLOSE =
172     "Connection: Close\r\n".getBytes();
173     static final byte[] DEFAULT_CONTENT_TYPE =
174     "Content-Type: text/plain\r\n".getBytes();
175
176     final void writeHeader(WorkerOutputStream out, String JavaDoc name, String JavaDoc val)
177         throws IOException JavaDoc
178     {
179         //#ifdef DEBUG
180
if (Trace.level() > 1) {
181             Debug.println("header: " + name + ": " + val);
182         }
183         //#endif
184
out.write(name);
185         out.write(headerSep);
186         out.write(val); // XXX folding?
187
out.write(CRLF);
188     }
189     
190     /**
191      * Write the result code and the message headers for the response
192      */

193     public void writeHeaders() throws IOException JavaDoc {
194     WorkerOutputStream out = os.getOutputStream();
195         out.write(protocol);
196         if (changedCode) {
197             out.write(' ');
198             out.writeInt(sc);
199             out.write(' ');
200             out.write(sm);
201             out.write(CRLF);
202         } else {
203             out.write(OKRESPONSE);
204         }
205
206         if (keepAlive) {
207             out.write(CONNECTION_KEEPALIVE);
208         } else {
209             out.write(CONNECTION_CLOSE);
210         }
211
212         if (contentType != null) {
213             out.write(CONTENT_TYPE);
214             out.write(contentType);
215             out.write(CRLF);
216         } else {
217             out.write(DEFAULT_CONTENT_TYPE);
218         }
219         
220         if (contentLength >= 0) {
221             out.write(CONTENT_LENGTH);
222             out.writeInt(contentLength);
223             out.write(CRLF);
224         }
225         if (fixedHeaders == null) {
226             if (fixedH != null) {
227                 fixedHeaders = fixedH.toByteArray();
228                 fixedH = null;
229             }
230             out.write(fixedHeaders);
231         }
232
233         if (headerBufValid) {
234             out.write(headerBuf.toString());
235         } else if (anyHeaders) {
236             Enumeration JavaDoc hdrs = headers.keys();
237             while (hdrs.hasMoreElements()) {
238                 String JavaDoc headerName = (String JavaDoc)hdrs.nextElement();
239                 Object JavaDoc headerVal = headers.get(headerName);
240                 if (headerVal instanceof String JavaDoc) {
241                     writeHeader(out, headerName, headerVal.toString());
242                 } else {
243                     Vector JavaDoc v = (Vector JavaDoc)headerVal;
244                     for (int i = 0; i < v.size(); i++) {
245                         writeHeader(out, headerName, v.elementAt(i).toString());
246                     }
247                 }
248             }
249         }
250         
251         if (cookies != null) {
252         writeCookies(out);
253     }
254         writeDateHeader(out);
255         out.write(CRLF);
256     }
257
258     static byte[] digits = "0123456789".getBytes();
259     static byte[] GMTString = " GMT\r\n".getBytes();
260     
261     final void writeDateHeader(WorkerOutputStream out) throws IOException JavaDoc {
262         long now = System.currentTimeMillis();
263         long msec = now - calbase;
264         if (msec >= 60000) {
265             Calendar JavaDoc c = Calendar.getInstance(GMT);
266             c.setTime(new Date JavaDoc(now));
267             c.set(Calendar.SECOND, 0);
268             c.set(Calendar.MILLISECOND, 0);
269             Date JavaDoc d = c.getTime();
270             calbase = d.getTime();
271             curTime = df.format(d).getBytes();
272             msec = now - calbase;
273             if (msec < 0) msec = 0;
274         }
275         int sec = (int)(msec / 1000);
276         curTime[29] = digits[sec / 10];
277         curTime[30] = digits[sec % 10];
278         out.write(curTime);
279     }
280
281     void writeAttr(WorkerOutputStream out, String JavaDoc name, String JavaDoc val)
282     throws IOException JavaDoc
283     {
284         if (val != null) {
285             out.write(';');
286         out.write(name);
287         out.write('=');
288         out.write(val);
289         }
290     }
291
292     // from RFC 2068, token special case characters
293
private static final String JavaDoc tspecials = "()<>@,;:\\\"/[]?={} \t";
294
295     /**
296      * Return true iff the string counts as an HTTP/1.1 "token".
297      */

298     private static boolean isToken (String JavaDoc value) {
299     final int len = value.length();
300
301     for (int i = 0; i < len; i++) {
302         char c = value.charAt(i);
303         if (c < 0x20 || c >= 0x7f || tspecials.indexOf(c) != -1)
304                 return false;
305     }
306     return true;
307     }
308
309     private final void writeTok(WorkerOutputStream out, String JavaDoc val, int v)
310         throws IOException JavaDoc
311     {
312         if (v == 0 || isToken(val)) {
313             out.write(val);
314         } else {
315             out.write('"');
316             out.write(val);
317             out.write('"');
318         }
319     }
320     
321     void writeCookies(WorkerOutputStream out) throws IOException JavaDoc {
322         out.write("Set-Cookie: ");
323         for (int i = 0; i < cookies.size(); i++) {
324             Cookie JavaDoc cookie = (Cookie JavaDoc)cookies.elementAt(i);
325             final int version = cookie.getVersion();
326             if (i > 0) out.write('\t');
327             out.write(cookie.getName());
328             String JavaDoc val = cookie.getValue();
329             if (val != null) {
330                 out.write('=');
331                 writeTok(out, val, version);
332             }
333
334             writeAttr(out, "Path", cookie.getPath());
335             if (version == 0) {
336                 writeAttr(out, "Domain", cookie.getDomain());
337                 if (cookie.getMaxAge() >= 0) {
338                     Date JavaDoc d = new Date JavaDoc();
339                     long ms = d.getTime() + cookie.getMaxAge() * 1000;
340                     d.setTime(ms);
341                     writeAttr(out, "Expires",
342                               cookieDateFormat.format(d));
343                 }
344             } else {
345                 out.write(";Version=1");
346                 out.write(";Domain=");
347                 writeTok(out, cookie.getDomain(), version);
348                 if (cookie.getMaxAge() >= 0) {
349                     writeAttr(out, "Max-Age", "" + cookie.getMaxAge());
350                 } else {
351                     out.write(";Discard");
352                 }
353                 writeAttr(out, "Comment", cookie.getComment());
354             }
355             if (cookie.getSecure()) out.write(";Ssecure");
356             out.write(CRLF);
357         }
358     }
359
360     final void setFixedHeader(String JavaDoc name, String JavaDoc val) {
361         try {
362             write(fixedH, name);
363             write(fixedH, ": ");
364             write(fixedH, val);
365             fixedH.write(CRLF);
366         } catch (Exception JavaDoc e) {}
367     }
368     
369     /**
370      * ----- SerlvetResponse methods -----
371      */

372     
373     /**
374      * Sets the content length for this response.
375      *
376      * @param len the content length
377      */

378     public void setContentLength(int len) {
379         contentLength = len;
380     }
381
382     final void maybeSetContentLength(int len) {
383         if (contentLength == -1) {
384             contentLength = len;
385         }
386     }
387
388     int getContentLength() { return contentLength; }
389
390     /**
391      * Sets the content type for this response. This type may later
392      * be implicitly modified by addition of properties such as the MIME
393      * <em>charset=&lt;value&gt;</em> if the service finds it necessary,
394      * and the appropriate media type property has not been set.
395      *
396      * <p>This response property may only be assigned one time. If a
397      * writer is to be used to write a text response, this method must
398      * be called before the method <code>getWriter</code>. If an
399      * output stream will be used to write a response, this method must
400      * be called before the output stream is used to write response
401      * data.
402      *
403      * @param type the content's MIME type
404      */

405     public void setContentType(String JavaDoc type) {
406         contentType = type;
407     }
408
409     /**
410      * Returns an output stream for writing binary response data.
411      *
412      * @exception IllegalStateException if getWriter has been
413      * called on this same request.
414      * @exception IOException if an I/O exception has occurred
415      */

416     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
417         if (getWriterCalled) {
418             throw new IllegalStateException JavaDoc("getWriter() already called");
419         }
420         getOutputStreamCalled = true;
421         return os;
422     }
423
424     /**
425      * Returns a print writer for writing formatted text responses. The
426      * MIME type of the response will be modified, if necessary, to reflect
427      * the character encoding used, through the <em>charset=...</em>
428      * property. This means that the content type must be set before
429      * calling this method.
430      *
431      *
432      * @exception UnsupportedEncodingException if no such encoding can
433      * be provided
434      * @exception IllegalStateException if getOutputStream has been
435      * called on this same request.
436      * @exception IOException on other errors.
437      */

438     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
439         if (getOutputStreamCalled) {
440             throw new
441                 IllegalStateException JavaDoc("getOutputStream() already called");
442         }
443         if (writer == null) {
444             String JavaDoc encoding = getCharacterEncoding();
445             OutputStreamWriter JavaDoc ww = null;
446             OutputStream JavaDoc os = getOutputStream();
447             if (encoding != null) {
448                 ww = new OutputStreamWriter JavaDoc(os, encoding);
449             } else {
450                 ww = new OutputStreamWriter JavaDoc(os);
451             }
452             writer = new PrintWriter JavaDoc(ww);
453             getWriterCalled = true;
454         }
455         return writer;
456     }
457
458     public JspWriter getJspWriter(int bufferSize, boolean autoFlush)
459         throws IOException JavaDoc
460     {
461         if (!jWriterInit) {
462             jWriterInit = true;
463             jWriter.setHttpOutputStream(os);
464             jWriter.setBufferSize(bufferSize);
465             jWriter.setAutoFlush(autoFlush);
466         }
467         return jWriter;
468     }
469
470     /**
471      * Returns the character set encoding used for this MIME body.
472      * The character encoding is either the one specified in the
473      * assigned content type, or one which the client understands.
474      * If no content type has yet been assigned, it is implicitly
475      * set to <em>text/plain</em>
476      */

477     public String JavaDoc getCharacterEncoding () {
478         String JavaDoc s = contentType;
479         if (s == null) return null;
480         
481         int idx = s.indexOf(';');
482         while (idx > 0) {
483             s = s.substring(idx+1).trim();
484             idx = s.indexOf(';');
485             String JavaDoc c = s;
486             if (idx > 0) {
487                 c = s.substring(0, idx).trim();
488             }
489             if (c.startsWith("charset=")) {
490                 return c.substring(8);
491             }
492         }
493         return null;
494     }
495
496     /**
497      * ----- HttpServletResponse methods -----
498      */

499     
500     
501     /**
502      * Adds the specified cookie to the response. It can be called
503      * multiple times to set more than one cookie.
504      *
505      * @param cookie the Cookie to return to the client
506      */

507     public void addCookie(Cookie JavaDoc cookie) {
508     //#ifdef DEBUG
509
if (Trace.level() > 7) {
510         Debug.println("addCookie(" + cookie + ")");
511     }
512     //#endif
513
if (cookies == null) {
514             cookies = new Vector JavaDoc();
515         }
516         cookies.addElement(cookie);
517     }
518
519     /**
520      * Checks whether the response message header has a field with
521      * the specified name.
522      *
523      * @param name the header field name
524      * @return true if the response message header has a field with
525      * the specified name; false otherwise
526      */

527     public boolean containsHeader(String JavaDoc name) {
528         return headers.get(name) != null;
529     }
530
531     /**
532      * Sets the status code and message for this response. If the
533      * field had already been set, the new value overwrites the
534      * previous one. The message is sent as the body of an HTML
535      * page, which is returned to the user to describe the problem.
536      * The page is sent with a default HTML header; the message
537      * is enclosed in simple body tags (&lt;body&gt;&lt;/body&gt;).
538      *
539      * @param sc the status code
540      * @param sm the status message
541      */

542     public void setStatus(int sc, String JavaDoc sm) {
543         this.changedCode = true;
544         this.sc = sc;
545         this.sm = sm;
546     }
547
548     /**
549      * Sets the status code for this response. This method is used to
550      * set the return status code when there is no error (for example,
551      * for the status codes SC_OK or SC_MOVED_TEMPORARILY). If there
552      * is an error, the <code>sendError</code> method should be used
553      * instead.
554      *
555      * @param sc the status code
556      *
557      */

558     public void setStatus(int sc) {
559         this.changedCode = true;
560         this.sc = sc;
561         this.sm = "";
562     }
563
564     /**
565      *
566      * Adds a field to the response header with the given name and value.
567      * If the field had already been set, the new value overwrites the
568      * previous one. The <code>containsHeader</code> method can be
569      * used to test for the presence of a header before setting its
570      * value.
571      *
572      * @param name the name of the header field
573      * @param value the header field's value
574      *
575      */

576     public void setHeader(String JavaDoc name, String JavaDoc value) {
577         if (name.charAt(0) == 'C') {
578             if (name.equals("Content-Type")) {
579                 contentType = value;
580                 return;
581             } else if (name.equals("Content-Length")) {
582                 contentLength = Integer.parseInt(value);
583                 return;
584             }
585         }
586         if (headerBufValid) {
587             if (headers.get(name) == null) {
588                 appendHeader(name, value);
589             } else {
590                 headerBufValid = false;
591             }
592         }
593         anyHeaders = true;
594     headers.put(name, value);
595     }
596
597     final void appendHeader(String JavaDoc name, String JavaDoc value) {
598         headerBuf.append(name);
599         headerBuf.append(": ");
600         headerBuf.append(value);
601         headerBuf.append("\r\n");
602     }
603
604     /**
605      * Add a new header value -- this method allows multiple headers with
606      * the same name.
607      */

608     public void addHeader(String JavaDoc name, String JavaDoc value) {
609         Object JavaDoc obj = headers.get(name);
610         Vector JavaDoc v = null;
611         boolean mustInsert = true;
612         if (obj instanceof String JavaDoc) {
613             v = new Vector JavaDoc();
614             v.addElement(obj.toString());
615         } else if (obj == null) {
616             v = new Vector JavaDoc();
617         } else {
618             v = (Vector JavaDoc)obj;
619             mustInsert = false;
620         }
621         v.addElement(value);
622         if (mustInsert) {
623             anyHeaders = true;
624             headers.put(name, v);
625         }
626         appendHeader(name, value);
627     }
628         
629
630     /**
631      * Adds a field to the response header with the given name and
632      * integer value. If the field had already been set, the new value
633      * overwrites the previous one. The <code>containsHeader</code>
634      * method can be used to test for the presence of a header before
635      * setting its value.
636      *
637      * @param name the name of the header field
638      * @param value the header field's integer value
639      *
640      */

641     public void setIntHeader(String JavaDoc name, int value) {
642         setHeader(name, "" + value);
643     }
644         
645     public void addIntHeader(String JavaDoc name, int value) {
646         addHeader(name, "" + value);
647     }
648         
649     /**
650      *
651      * Adds a field to the response header with the given name and
652      * date-valued field. The date is specified in terms of
653      * milliseconds since the epoch. If the date field had already
654      * been set, the new value overwrites the previous one. The
655      * <code>containsHeader</code> method can be used to test for the
656      * presence of a header before setting its value.
657      *
658      * @param name the name of the header field
659      * @param value the header field's date value
660      *
661      */

662     public void setDateHeader(String JavaDoc name, long date) {
663         Date JavaDoc d = new Date JavaDoc(date);
664         setHeader(name, df2.format(d));
665     }
666
667     public void addDateHeader(String JavaDoc name, long date) {
668         Date JavaDoc d = new Date JavaDoc(date);
669         addHeader(name, df2.format(d));
670     }
671
672     /**
673      * Sends an error response to the client using the specified status
674      * code and descriptive message. If setStatus has previously been
675      * called, it is reset to the error status code. The message is
676      * sent as the body of an HTML page, which is returned to the user
677      * to describe the problem. The page is sent with a default HTML
678      * header; the message is enclosed in simple body tags
679      * (&lt;body&gt;&lt;/body&gt;).
680      *
681      * @param sc the status code
682      * @param msg the detail message
683      * @exception IOException If an I/O error has occurred. */

684     public void sendError(int scode, String JavaDoc msg) throws IOException JavaDoc {
685         setStatus(scode, msg);
686     if (os.discard()) {
687         writer = null;
688     }
689         getWriterCalled = getOutputStreamCalled = false;
690     setContentType("text/html");
691         PrintWriter JavaDoc p = getWriter();
692         p.println("<HEAD><TITLE>" + scode + " " + msg + "</TITLE></HEAD>");
693         p.print("<BODY><H1>");
694     p.print(msg);
695     p.println("</H1>");
696     if (scode == HttpServletResponse.SC_NOT_FOUND) {
697         p.print("The requested URL was not found on this server");
698         p.println("<P>");
699     }
700         p.println("</BODY>");
701     flush();
702     }
703
704     /**
705      * Sends an error response to the client using the specified
706      * status code and a default message.
707      * @param scode the status code
708      * @exception IOException If an I/O error has occurred.
709      */

710     public void sendError(int scode) throws IOException JavaDoc {
711     sm = "Error " + scode;
712         setStatus(scode);
713     if (os.discard()) {
714             writer = null;
715         }
716         getWriterCalled = getOutputStreamCalled = false;
717         PrintWriter JavaDoc p = getWriter();
718         p.println("<html><head><title>" + sm + "</title></head>");
719         p.println("<body>");
720         p.println(sm);
721         p.println("</body></html>");
722     flush();
723     }
724
725     /**
726      * Flush the underlying output stream
727      */

728     void flush() throws IOException JavaDoc {
729         if (writer != null) writer.flush();
730         os.flush();
731     }
732
733     /**
734      * Sends a temporary redirect response to the client using the
735      * specified redirect location URL. The URL must be absolute (for
736      * example, <code><em>https://hostname/path/file.html</em></code>).
737      * Relative URLs are not permitted here.
738      *
739      * @param location the redirect location URL
740      * @exception IOException If an I/O error has occurred.
741      */

742     public void sendRedirect(String JavaDoc location) throws IOException JavaDoc {
743         setHeader("Location", location);
744         sendError(SC_MOVED_TEMPORARILY,
745                   "<a HREF=\"" + location + "\">" + location + "</A>");
746     }
747
748     /**
749      * Encodes the specified URL by including the session ID in it,
750      * or, if encoding is not needed, returns the URL unchanged.
751      * The implementation of this method should include the logic to
752      * determine whether the session ID needs to be encoded in the URL.
753      * For example, if the browser supports cookies, or session
754      * tracking is turned off, URL encoding is unnecessary.
755      *
756      * <p>All URLs emitted by a Servlet should be run through this
757      * method. Otherwise, URL rewriting cannot be used with browsers
758      * which do not support cookies.
759      *
760      * @param url the url to be encoded.
761      * @return the encoded URL if encoding is needed; the unchanged URL
762      * otherwise.
763      */

764     public String JavaDoc encodeUrl (String JavaDoc url) {
765         return url;
766     }
767     public String JavaDoc encodeURL(String JavaDoc url) {
768         return url;
769     }
770
771     /**
772      * Encodes the specified URL for use in the
773      * <code>sendRedirect</code> method or, if encoding is not needed,
774      * returns the URL unchanged. The implementation of this method
775      * should include the logic to determine whether the session ID
776      * needs to be encoded in the URL. Because the rules for making
777      * this determination differ from those used to decide whether to
778      * encode a normal link, this method is seperate from the
779      * <code>encodeUrl</code> method.
780      *
781      * <p>All URLs sent to the HttpServletResponse.sendRedirect
782      * method should be run through this method. Otherwise, URL
783      * rewriting canont be used with browsers which do not support
784      * cookies.
785      *
786      * @param url the url to be encoded.
787      * @return the encoded URL if encoding is needed; the unchanged URL
788      * otherwise.
789      *
790      */

791     public String JavaDoc encodeRedirectUrl (String JavaDoc url) {
792         return url;
793     }
794     public String JavaDoc encodeRedirectURL(String JavaDoc url) {
795         return url;
796     }
797
798     public void flushBuffer() throws IOException JavaDoc {
799         if (writer != null) writer.flush();
800         os.flushBuffered();
801     }
802
803     public void setBufferSize(int size) throws IllegalStateException JavaDoc {
804         os.setBufferSize(size);
805     }
806
807     public int getBufferSize() {
808         return os.getBufferSize();
809     }
810
811     public boolean isCommitted() {
812         return os.isCommitted();
813     }
814
815     public void reset() throws IllegalStateException JavaDoc {
816         os.reset(); // checks for committed, may throw IllegalStateException
817
sc = SC_OK;
818         headers.clear();
819         sm = "Request Completed";
820         changedCode = false;
821         locale = defaultLocale;
822         contentLength = -1;
823         contentType = null;
824         headerBuf.setLength(0);
825         headerBufValid = true;
826         writer = null;
827         cookies = null;
828         anyHeaders = false;
829         getWriterCalled = false;
830         jWriterInit = false;
831         getOutputStreamCalled = false;
832     }
833
834     public void setLocale(Locale JavaDoc locale) {
835         this.locale = locale;
836         if (locale != null) {
837             String JavaDoc lang = locale.getLanguage();
838             if (lang != null) {
839                 setHeader("Content-Language", lang);
840             }
841         }
842                 
843     }
844
845     public Locale JavaDoc getLocale() {
846         return locale;
847     }
848 }
849
Popular Tags