Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 463c7350f6671e4f5e630ee308437e4c81941d0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
/*******************************************************************************
 * Copyright (c) 1999, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.http.servlet;

import java.io.*;
import java.util.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.equinox.http.*;
import org.eclipse.equinox.socket.SocketInterface;

/**
 * The implementation of the HttpServletResponse interface.
 *
 * Per Servlet 2.2 Section 3.3.3.3, this object need not be thread-safe.
 **/

public class HttpServletResponseImpl implements HttpServletResponse {
	private Http http;
	protected HttpServletRequestImpl request;
	private SocketInterface socket;
	protected ServletOutputStreamImpl servletOutputStream;
	protected String charset = null;
	protected static final String defaultCharset = "ISO-8859-1"; //$NON-NLS-1$

	protected int contentLength = 0;
	protected String contentType = null;
	protected Vector cookies = null;
	protected Hashtable headers = null;
	protected int status = SC_OK;
	protected String statusString = "OK"; //$NON-NLS-1$

	private boolean gotOutputStream = false;
	protected PrintWriter writer = null;

	private boolean keepAlive = false;

	public HttpServletResponseImpl(SocketInterface socket, Http http) throws IOException {
		this.http = http;
		this.socket = socket;
		servletOutputStream = new ServletOutputStreamImpl(socket.getOutputStream(), this);
	}

	/**
	 * Called by the request in its constructor.
	 */
	protected void setRequest(HttpServletRequestImpl request) {
		this.request = request;
	}

	/**
	 * Adds the specified cookie to the response.  It can be called
	 * multiple times to set more than one cookie.
	 *
	 * @param cookie the Cookie to return to the client
	 */
	public void addCookie(Cookie cookie) {
		if (cookies == null) {
			synchronized (this) {
				if (cookies == null) {
					cookies = new Vector(20);
				}
			}
		}

		synchronized (cookies) {
			if (!cookies.contains(cookie)) {
				//System.out.println("addCookie: "+cookie.getName()+"="+cookie.getValue());
				cookies.addElement(cookie);
			}
		}

		//http.logDebug("ServletResponse::addCookie:  added cookie "+cookie.getName());
	}

	/**
	 * Checks whether the response message header has a field with
	 * the specified name.
	 *
	 * @param name the header field name
	 * @return true if the response message header has a field with
	 * the specified name; false otherwise
	 */
	public boolean containsHeader(String name) {
		if (headers == null) {
			return (false);
		}

		Object value = headers.get(name);

		if (value != null) {
			return (true);
		}

		Enumeration headerEnumeration = headers.keys();

		while (headerEnumeration.hasMoreElements()) {
			String key = (String) headerEnumeration.nextElement();

			if (name.equalsIgnoreCase(key)) {
				return (true);
			}
		}

		return (false);
	}

	/**
	 * Encodes the specified URL for use in the
	 * <code>sendRedirect</code> method or, if encoding is not needed,
	 * returns the URL unchanged.  The implementation of this method
	 * should include the logic to determine whether the session ID
	 * needs to be encoded in the URL.  Because the rules for making
	 * this determination differ from those used to decide whether to
	 * encode a normal link, this method is seperate from the
	 * <code>encodeUrl</code> method.
	 *
	 * <p>All URLs sent to the HttpServletResponse.sendRedirect
	 * method should be run through this method.  Otherwise, URL
	 * rewriting cannot be used with browsers which do not support
	 * cookies.
	 *
	 * @param url the url to be encoded.
	 * @return the encoded URL if encoding is needed; the unchanged URL
	 * otherwise.
	 * deprecated
	 *
	 * @see #sendRedirect
	 * @see #encodeUrl
	 */

	public String encodeRedirectUrl(String url) {
		return (encodeRedirectURL(url));
	}

	/**
	 * Encodes the specified URL for use in the
	 * <code>sendRedirect</code> method or, if encoding is not needed,
	 * returns the URL unchanged.  The implementation of this method
	 * should include the logic to determine whether the session ID
	 * needs to be encoded in the URL.  Because the rules for making
	 * this determination differ from those used to decide whether to
	 * encode a normal link, this method is seperate from the
	 * <code>encodeUrl</code> method.
	 *
	 * <p>All URLs sent to the HttpServletResponse.sendRedirect
	 * method should be run through this method.  Otherwise, URL
	 * rewriting canont be used with browsers which do not support
	 * cookies.
	 *
	 * <p>After this method is called, the response should be considered
	 * to be committed and should not be written to.
	 *
	 * @param url the url to be encoded.
	 * @return the encoded URL if encoding is needed; the unchanged URL
	 * otherwise.
	 *
	 * @see #sendRedirect
	 * @see #encodeUrl
	 */

	public String encodeRedirectURL(String url) {
		return (url);
	}

	/**
	 * Encodes the specified URL by including the session ID in it,
	 * or, if encoding is not needed, returns the URL unchanged.
	 * The implementation of this method should include the logic to
	 * determine whether the session ID needs to be encoded in the URL.
	 * For example, if the browser supports cookies, or session
	 * tracking is turned off, URL encoding is unnecessary.
	 *
	 * <p>All URLs emitted by a Servlet should be run through this
	 * method.  Otherwise, URL rewriting cannot be used with browsers
	 * which do not support cookies.
	 *
	 * @param url the url to be encoded.
	 * @return the encoded URL if encoding is needed; the unchanged URL
	 * otherwise.
	 * deprecated
	 */
	public String encodeUrl(String url) {
		return (encodeURL(url));
	}

	/**
	 * Encodes the specified URL by including the session ID in it,
	 * or, if encoding is not needed, returns the URL unchanged.
	 * The implementation of this method should include the logic to
	 * determine whether the session ID needs to be encoded in the URL.
	 * For example, if the browser supports cookies, or session
	 * tracking is turned off, URL encoding is unnecessary.
	 *
	 * <p>All URLs emitted by a Servlet should be run through this
	 * method.  Otherwise, URL rewriting cannot be used with browsers
	 * which do not support cookies.
	 *
	 * @param url the url to be encoded.
	 * @return the encoded URL if encoding is needed; the unchanged URL
	 * otherwise.
	 */

	public String encodeURL(String url) {
		return (url);
	}

	/**
	 * Close the response output.
	 *
	 */
	public void close() {
		if (writer != null) {
			writer.close();
		} else {
			try {
				servletOutputStream.close();
			} catch (IOException e) {
				// TODO: consider logging
			}
		}

		if (!keepAlive) /* if the no Keep-Alive, then close socket */
		{
			try {
				socket.close();
			} catch (IOException e) {
				// TODO: consider logging
			}
		}
	}

	/**
	 * Returns the character set encoding used for this MIME body.
	 * The character encoding is either the one specified in the
	 * assigned content type, or one which the client understands
	 * as specified in the Accept-Charset header of the request.
	 * If no charset can be determined, it defaults to the
	 * value of the System property 'file.encoding' if that
	 * is a supported character set. If it is not supported, then
	 * we use <tt>ISO-8859-1</tt> as the character set.
	 */
	public String getCharacterEncoding() {
		if (charset == null) {
			OutputStream dummy = new ByteArrayOutputStream();
			// BUGBUG Per the spec, the default charset is ISO8859_1
			// Servlet 2.3 Section 5.4

			/* Get the default file encoding charset */
			String fileEncoding = (new SecureAction()).getProperty("file.encoding", defaultCharset); //$NON-NLS-1$
			if ((fileEncoding == null) || (fileEncoding.length() < 1)) {
				fileEncoding = defaultCharset;
			}
			String fileEncodingAlias = null;

			/* We need to look at Accept-Charset from the request to
			 * select the encoding charset.
			 */
			String accept = request.getHeaderUpper("ACCEPT-CHARSET"); //$NON-NLS-1$

			if (accept != null) {
				/*
				 * We prefer the file.encoding charset if specified
				 * in the Accept-Charset and supported by the JRE. Otherwise,
				 * we simply select the first value supported by the
				 * JRE. We do not support the q value in Accept-Charset.
				 */

				Tokenizer tokenizer = new Tokenizer(accept);

				parseloop: while (true) {
					String acceptCharset = tokenizer.getToken(",;"); //$NON-NLS-1$

					if (!acceptCharset.equals("*")) //$NON-NLS-1$
					{
						try /* is charset valid? */
						{
							String encodingAlias = new OutputStreamWriter(dummy, acceptCharset).getEncoding();

							if (charset == null) /* charset has not been set */
							{
								charset = acceptCharset;
							} else {
								if (fileEncodingAlias == null) {
									try {
										fileEncodingAlias = new OutputStreamWriter(dummy, fileEncoding).getEncoding();
									} catch (UnsupportedEncodingException ee) {
										// TODO: consider logging
									}
								}

								if (encodingAlias.equals(fileEncodingAlias)) {
									charset = acceptCharset; /* prefer the file.encoding charset */

									break parseloop;
								}
							}
						} catch (UnsupportedEncodingException e) {
							/* charset is not supported */
						}
					}

					char c = tokenizer.getChar();

					if (c == ';') {
						tokenizer.getToken(","); /* ignore q value *///$NON-NLS-1$
						c = tokenizer.getChar();
					}

					if (c == '\0') {
						break parseloop;
					}
				}
			}

			if (charset == null) /* if Accept-Charset did not produce a valid charset */
			{
				charset = fileEncoding;

				try /* is charset valid? */
				{
					new OutputStreamWriter(dummy, charset);
				} catch (UnsupportedEncodingException e) {
					/* charset is not supported */
					charset = defaultCharset;
				}
			}
		}

		return (charset);
	}

	/**
	 * Returns an output stream for writing binary response data.
	 *
	 * @see getWriter
	 * @exception IllegalStateException if getWriter has been
	 *	called on this same request.
	 * @exception IOException if an I/O exception has occurred
	 */
	public ServletOutputStream getOutputStream() {
		if (!gotOutputStream) {
			synchronized (this) {
				if (writer != null) {
					throw new IllegalStateException();
				}
				gotOutputStream = true;
			}
		}

		return (servletOutputStream);
	}

	/**
	 * Returns a print writer for writing formatted text responses.  The
	 * MIME type of the response will be modified, if necessary, to reflect
	 * the character encoding used, through the <em>charset=...</em>
	 * property.  This means that the content type must be set before
	 * calling this method.
	 *
	 * @see getOutputStream
	 * @see setContentType
	 *
	 * @exception UnsupportedEncodingException if no such charset can
	 * be provided
	 * @exception IllegalStateException if getOutputStream has been
	 *	called on this same request.
	 * @exception IOException on other errors.
	 */
	public PrintWriter getWriter() throws IOException {
		if (writer == null) {
			synchronized (this) {
				if (writer == null) {
					if (gotOutputStream) {
						throw new IllegalStateException();
					}

					String encoding = getCharacterEncoding();

					writer = new ServletPrintWriter(servletOutputStream, encoding);

					if (contentType == null) {
						// BUGBUG Must not set a default content type.
						// Servlet 2.3 Section 5.3
						contentType = "text/plain; charset=" + encoding; //$NON-NLS-1$

						setHeader("Content-Type", contentType); //$NON-NLS-1$
					} else {
						if (contentType.toLowerCase().indexOf("charset=") == -1) // 99372 //$NON-NLS-1$
						{
							contentType = contentType + "; charset=" + encoding; //$NON-NLS-1$

							setHeader("Content-Type", contentType); //$NON-NLS-1$
						}
					}
				}
			}
		}

		return (writer);
	}

	boolean gotStreamOrWriter() {
		return (writer != null || gotOutputStream);
	}

	/**
	 * Sends an error response to the client using the specified
	 * status code and a default message.
	 * @param sc the status code
	 * @exception IOException If an I/O error has occurred.
	 */
	public void sendError(int statusCode) throws IOException {
		// BUGBUG Must clear buffer if response has not yet been committed
		// or throw an IllegalStateException.
		sendError(statusCode, http.getStatusPhrase(statusCode));
	}

	/**
	 * Sends an error response to the client using the specified status
	 * code and descriptive message.  If setStatus has previously been
	 * called, it is reset to the error status code.  The message is
	 * sent as the body of an HTML page, which is returned to the user
	 * to describe the problem.  The page is sent with a default HTML
	 * header; the message is enclosed in simple body tags
	 * (&lt;body&gt;&lt;/body&gt;).
	 *
	 * @param sc the status code
	 * @param msg the detail message
	 * @exception IOException If an I/O error has occurred.
	 */
	public void sendError(int sc, String msg) throws IOException {
		// BUGBUG Must clear buffer if response has not yet been committed
		// or throw an IllegalStateException.
		status = sc;
		statusString = msg;

		if (Http.DEBUG) {
			http.logDebug("Error: " + sc + " - " + msg); //$NON-NLS-1$ //$NON-NLS-2$
		}

		servletOutputStream.print("<html><body><h3>"); //$NON-NLS-1$
		servletOutputStream.print(sc);
		servletOutputStream.print(" - "); //$NON-NLS-1$
		servletOutputStream.print(msg);
		servletOutputStream.print("</h3></body></html>"); //$NON-NLS-1$
		servletOutputStream.close(); /* commit and close the response */

	}

	/**
	 * Sends a temporary redirect response to the client using the
	 * specified redirect location URL.  The URL must be absolute (for
	 * example, <code><em>https://hostname/path/file.html</em></code>).
	 * Relative URLs are not permitted here.
	 *
	 * @param location the redirect location URL
	 * @exception IOException If an I/O error has occurred.
	 */
	public void sendRedirect(String location) throws IOException {
		// BUGBUG The input URL may be relative. We must translate to
		// a fully qualified URL (it should already have been encoded).
		// If this cannot be done, throw an IllegalArgument exception.
		// BUGBUG Must clear buffer if response has not yet been committed
		// or throw an IllegalStateException.
		setStatus(SC_MOVED_TEMPORARILY);
		setHeader("Location", location); //$NON-NLS-1$
		servletOutputStream.close(); /* commit and close the response */
	}

	/**
	 * Sets the content length for this response.
	 *
	 * @param len the content length
	 */

	public void setContentLength(int len) {
		// BUGBUG response should be considered commited and closed
		// when content length has been written.
		// Not sure if content length is bytes or chars?
		contentLength = len;
		setIntHeader("Content-Length", len); //$NON-NLS-1$
	}

	/**
	 * Sets the content type for this response.  This type may later
	 * be implicitly modified by addition of properties such as the MIME
	 * <em>charset=&lt;value&gt;</em> if the service finds it necessary,
	 * and the appropriate media type property has not been set.
	 *
	 * <p>This response property may only be assigned one time.  If a
	 * writer is to be used to write a text response, this method must
	 * be called before the method <code>getWriter</code>.  If an
	 * output stream will be used to write a response, this method must
	 * be called before the output stream is used to write response
	 * data.
	 *
	 * @param type the content's MIME type
	 * @see getOutputStream
	 * @see getWriter */
	public void setContentType(String type) {
		if (contentType == null) {
			synchronized (this) {
				if (contentType == null) {
					contentType = type;

					setHeader("Content-Type", type); //$NON-NLS-1$

					int index = type.indexOf(';', 0);
					if (index >= 0) {
						Tokenizer tokenizer = new Tokenizer(type);

						// TODO: verify next statement. It was String mimetype = tokenizer.getToken(";"); 
						tokenizer.getToken(";"); //$NON-NLS-1$
						tokenizer.getChar(); /* eat semicolon */

						parseloop: while (true) {
							String attribute = tokenizer.getToken("="); //$NON-NLS-1$
							char c = tokenizer.getChar();

							if (c != '=') {
								break parseloop; /* invalid content type */
							}

							String value = tokenizer.getString(";"); //$NON-NLS-1$
							c = tokenizer.getChar();

							if ("charset".equalsIgnoreCase(attribute)) //$NON-NLS-1$
							{
								charset = value;
							}

							if (c == '\0') {
								break parseloop;
							}
						}
					}
				}
			}
		}
	}

	/**
	 * This method was created in VisualAge.
	 */
	protected void setCookies() {
		if (cookies == null) {
			return;
		}

		int numCookies = cookies.size();

		if (numCookies > 0) {
			StringBuffer value = new StringBuffer(256);

			for (int i = 0; i < numCookies; i++) {
				Cookie cookie = (Cookie) cookies.elementAt(i);

				if (i > 0) {
					value.append(',');
				}

				value.append(cookie.getName());
				value.append("=\""); //$NON-NLS-1$
				value.append(cookie.getValue());
				value.append('\"');
				if ((cookie.getVersion()) != 0) {
					value.append(";Version="); //$NON-NLS-1$
					value.append(cookie.getVersion());
				}
				if ((cookie.getComment()) != null) {
					value.append(";Comment=\""); //$NON-NLS-1$
					value.append(cookie.getComment());
					value.append('\"');
				}
				if ((cookie.getDomain()) != null) {
					value.append(";Domain=\""); //$NON-NLS-1$
					value.append(cookie.getDomain());
					value.append('\"');
				}
				if ((cookie.getMaxAge()) != -1) {
					value.append(";Max-Age=\""); //$NON-NLS-1$
					value.append(cookie.getMaxAge());
					value.append('\"');
				}
				if ((cookie.getPath()) != null) {
					value.append(";Path=\""); //$NON-NLS-1$
					value.append(cookie.getPath());
					value.append('\"');
				}
				if (cookie.getSecure()) {
					value.append(";Secure"); //$NON-NLS-1$
				}
			}

			setHeader("Set-Cookie", value.toString()); //$NON-NLS-1$
			//System.out.println("Set-Cookie: "+value.toString());
		}
	}

	/**
	 *
	 * Adds a field to the response header with the given name and
	 * date-valued field.  The date is specified in terms of
	 * milliseconds since the epoch.  If the date field had already
	 * been set, the new value overwrites the previous one.  The
	 * <code>containsHeader</code> method can be used to test for the
	 * presence of a header before setting its value.
	 *
	 * @param name the name of the header field
	 * @param value the header field's date value
	 *
	 * @see #containsHeader
	 */
	public void setDateHeader(String name, long date) {
		HttpDate d = new HttpDate(date);
		setHeader(name, d.toString());
	}

	/**
	 *
	 * Adds a field to the response header with the given name and value.
	 * If the field had already been set, the new value overwrites the
	 * previous one.  The <code>containsHeader</code> method can be
	 * used to test for the presence of a header before setting its
	 * value.
	 *
	 * @param name the name of the header field
	 * @param value the header field's value
	 *
	 * @see #containsHeader
	 */
	public void setHeader(String name, String value) {
		// BUGBUG Headers set after the response is committed must be ignored
		// Servlet 2.2 Section 6.2
		if (headers == null) {
			synchronized (this) {
				if (headers == null) {
					headers = new Hashtable(31);
				}
			}
		}
		headers.put(name, value);
	}

	/**
	 * Adds a field to the response header with the given name and
	 * integer value.  If the field had already been set, the new value
	 * overwrites the previous one.  The <code>containsHeader</code>
	 * method can be used to test for the presence of a header before
	 * setting its value.
	 *
	 * @param name the name of the header field
	 * @param value the header field's integer value
	 *
	 * @see #containsHeader
	 */
	public void setIntHeader(String name, int value) {
		setHeader(name, String.valueOf(value));
	}

	/**
	 * Sets the status code for this response.  This method is used to
	 * set the return status code when there is no error (for example,
	 * for the status codes SC_OK or SC_MOVED_TEMPORARILY).  If there
	 * is an error, the <code>sendError</code> method should be used
	 * instead.
	 *
	 * @param sc the status code
	 *
	 * @see #sendError
	 */
	public void setStatus(int statusCode) {
		status = statusCode;
		statusString = http.getStatusPhrase(statusCode);
	}

	/**
	 * Sets the status code and message for this response.  If the
	 * field had already been set, the new value overwrites the
	 * previous one.  The message is sent as the body of an HTML
	 * page, which is returned to the user to describe the problem.
	 * The page is sent with a default HTML header; the message
	 * is enclosed in simple body tags (&lt;body&gt;&lt;/body&gt;).
	 *
	 * @param sc the status code
	 * @param sm the status message
	 * deprecated
	 */
	public void setStatus(int si, String ss) {
		status = si;
		statusString = ss;

	}

	/**
	 * Write the response headers to the ServletOutputStream.
	 *
	 * @param length Content length of the of the buffered content
	 * or -1 if the length is unknown.
	 */
	void writeHeaders(int length) throws IOException {
		setCookies();

		if ((length != -1) && !containsHeader("Content-Length")) //$NON-NLS-1$
		{
			setContentLength(length);
		}

		if (containsHeader("Content-Length")) //$NON-NLS-1$
		{
			String requestConnection = request.getHeaderUpper("CONNECTION"); //$NON-NLS-1$

			if (requestConnection != null) {
				if (requestConnection.toLowerCase().indexOf("keep-alive") >= 0) //$NON-NLS-1$
				{
					setHeader("Connection", "Keep-Alive"); //$NON-NLS-1$ //$NON-NLS-2$

					keepAlive = true;
				}
			}
		}

		if (!keepAlive) {
			setHeader("Connection", "close"); //$NON-NLS-1$ //$NON-NLS-2$
		}

		servletOutputStream.print("HTTP/1.0 "); //$NON-NLS-1$
		servletOutputStream.print(status);
		servletOutputStream.print(" "); //$NON-NLS-1$
		servletOutputStream.println(statusString);

		//      System.out.print("HTTP/1.0 ");
		//      System.out.print(status);
		//      System.out.print(" ");
		//      System.out.println(statusString);

		/* Write response headers */
		if (headers != null) {
			Enumeration headerEnumeration = headers.keys();
			while (headerEnumeration.hasMoreElements()) {
				String name = (String) headerEnumeration.nextElement();
				String value = (String) headers.get(name);
				servletOutputStream.print(name);
				servletOutputStream.print(": "); //$NON-NLS-1$
				servletOutputStream.println(value);

				//              System.out.print(name);
				//              System.out.print(": ");
				//              System.out.println(value);
			}
		}

		servletOutputStream.println(); /* Terminate the headers */

		//      System.out.println();
	}

	/**
	 * @see javax.servlet.http.HttpServletResponse#addDateHeader(String, long)
	 */
	public void addDateHeader(String name, long date) throws UnsupportedOperationException {
		throw new UnsupportedOperationException(HttpMsg.HTTP_ONLY_SUPPORTS_2_1);
	}

	/**
	 * @see javax.servlet.http.HttpServletResponse#addHeader(String, String)
	 */
	public void addHeader(String name, String value) throws UnsupportedOperationException {
		throw new UnsupportedOperationException(HttpMsg.HTTP_ONLY_SUPPORTS_2_1);
	}

	/**
	 * @see javax.servlet.http.HttpServletResponse#addIntHeader(String, int)
	 */
	public void addIntHeader(String name, int value) throws UnsupportedOperationException {
		throw new UnsupportedOperationException(HttpMsg.HTTP_ONLY_SUPPORTS_2_1);
	}

	/**
	 * @see javax.servlet.ServletResponse#flushBuffer()
	 */
	public void flushBuffer() throws IOException, UnsupportedOperationException {
		throw new UnsupportedOperationException(HttpMsg.HTTP_ONLY_SUPPORTS_2_1);
	}

	/**
	 * @see javax.servlet.ServletResponse#getBufferSize()
	 */
	public int getBufferSize() throws UnsupportedOperationException {
		throw new UnsupportedOperationException(HttpMsg.HTTP_ONLY_SUPPORTS_2_1);
	}

	/**
	 * @see javax.servlet.ServletResponse#getLocale()
	 */
	public Locale getLocale() throws UnsupportedOperationException {
		throw new UnsupportedOperationException(HttpMsg.HTTP_ONLY_SUPPORTS_2_1);
	}

	/**
	 * @see javax.servlet.ServletResponse#isCommitted()
	 */
	public boolean isCommitted() throws UnsupportedOperationException {
		throw new UnsupportedOperationException(HttpMsg.HTTP_ONLY_SUPPORTS_2_1);
	}

	/**
	 * @see javax.servlet.ServletResponse#reset()
	 */
	public void reset() throws UnsupportedOperationException {
		throw new UnsupportedOperationException(HttpMsg.HTTP_ONLY_SUPPORTS_2_1);
	}

	/**
	 * @see javax.servlet.ServletResponse#resetBuffer()
	 */
	public void resetBuffer() throws UnsupportedOperationException {
		throw new UnsupportedOperationException(HttpMsg.HTTP_ONLY_SUPPORTS_2_1);
	}

	/**
	 * @see javax.servlet.ServletResponse#setBufferSize(int)
	 */
	public void setBufferSize(int size) throws UnsupportedOperationException {
		throw new UnsupportedOperationException(HttpMsg.HTTP_ONLY_SUPPORTS_2_1);
	}

	/**
	 * @see javax.servlet.ServletResponse#setLocale(Locale)
	 */
	public void setLocale(Locale loc) throws UnsupportedOperationException {
		throw new UnsupportedOperationException(HttpMsg.HTTP_ONLY_SUPPORTS_2_1);
	}

	/* JavaServlet 2.4 API - For compilation only.  Will not implement.
	 * 
	 */
	public String getContentType() {
		//return contentType;
		throw new UnsupportedOperationException(HttpMsg.HTTP_ONLY_SUPPORTS_2_1);
	}

	/* For compilation only.  Will not implement.
	 * 
	 */
	public void setCharacterEncoding(String arg0) {
		//this.contentType = contentType;
		throw new UnsupportedOperationException(HttpMsg.HTTP_ONLY_SUPPORTS_2_1);

	}
}

Back to the top