KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > http > RunnerResponse


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.http;
30
31 import java.io.*;
32 import java.net.*;
33 import java.util.*;
34 import javax.servlet.*;
35 import javax.servlet.http.*;
36
37 import com.caucho.util.*;
38 import com.caucho.vfs.*;
39
40 import com.caucho.server.connection.AbstractHttpRequest;
41 import com.caucho.server.connection.AbstractHttpResponse;
42
43 /**
44  * Handles a response for a srun connection, i.e. a connection to
45  * a web server plugin.
46  */

47 public class RunnerResponse extends AbstractHttpResponse {
48   RunnerRequest _req;
49   
50   RunnerResponse(RunnerRequest request)
51   {
52     super(request);
53     
54     _req = request;
55   }
56
57   /**
58    * Return true for the top request.
59    */

60   public boolean isTop()
61   {
62     if (! (_request instanceof AbstractHttpRequest))
63       return false;
64     else {
65       return ((AbstractHttpRequest) _request).isTop();
66     }
67   }
68   
69   protected boolean writeHeadersInt(WriteStream os, int length)
70     throws IOException
71   {
72     CharBuffer cb = _cb;
73     cb.clear();
74     int version = _req.getVersion();
75     if (version >= HttpRequest.HTTP_1_1)
76       cb.append("HTTP/1.1 ");
77     else
78       cb.append("HTTP/1.0 ");
79     cb.append((char) ((_statusCode / 100) % 10 + '0'));
80     cb.append((char) ((_statusCode / 10) % 10 + '0'));
81     cb.append((char) (_statusCode % 10 + '0'));
82     cb.append(' ');
83     cb.append(_statusMessage);
84
85     _req.writeStatus(cb);
86
87     if (_statusCode >= 400) {
88       removeHeader("ETag");
89       removeHeader("Last-Modified");
90     }
91     else if (_isNoCache) {
92       removeHeader("ETag");
93       removeHeader("Last-Modified");
94
95       setHeader("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");
96       _req.writeHeader("Cache-Control", "no-cache");
97     }
98     else if (isPrivateCache())
99       _req.writeHeader("Cache-Control", "private");
100
101     for (int i = 0; i < _headerKeys.size(); i++) {
102       String key = (String) _headerKeys.get(i);
103       String value = (String) _headerValues.get(i);
104
105       _req.writeHeader(key, value);
106     }
107
108     if (_contentLength >= 0) {
109       cb.clear();
110       cb.append(_contentLength);
111       _req.writeHeader("Content-Length", cb);
112     }
113     else if (length >= 0) {
114       cb.clear();
115       cb.append(length);
116       _req.writeHeader("Content-Length", cb);
117     }
118
119     long now = Alarm.getCurrentTime();
120     for (int i = 0; i < _cookiesOut.size(); i++) {
121       Cookie cookie = (Cookie) _cookiesOut.get(i);
122       int cookieVersion = cookie.getVersion();
123
124       fillCookie(cb, cookie, now, 0);
125       _req.writeHeader("Set-Cookie", cb);
126       if (cookieVersion > 0) {
127         fillCookie(cb, cookie, now, cookieVersion);
128         _req.writeHeader("Set-Cookie2", cb);
129       }
130     }
131
132     if (_contentType != null)
133       _req.writeHeader("Content-Type", _contentType);
134
135     _req.sendHeader();
136
137     return false;
138   }
139 }
140
Popular Tags