KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > hmux > HmuxResponse


1 /*
2  * Copyright (c) 1998-2006 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.hmux;
30
31 import com.caucho.server.connection.AbstractHttpRequest;
32 import com.caucho.server.connection.AbstractHttpResponse;
33 import com.caucho.server.util.CauchoSystem;
34 import com.caucho.util.Alarm;
35 import com.caucho.util.CharBuffer;
36 import com.caucho.vfs.WriteStream;
37
38 import javax.servlet.http.Cookie JavaDoc;
39 import java.io.IOException JavaDoc;
40
41 /**
42  * Handles a response for a srun connection, i.e. a connection to
43  * a web server plugin.
44  */

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

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