KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ajp > tomcat4 > Ajp13Response


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.ajp.tomcat4;
18
19 import java.io.IOException JavaDoc;
20
21 import java.util.Iterator JavaDoc;
22
23 import javax.servlet.http.Cookie JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpSession JavaDoc;
26
27 import org.apache.catalina.connector.HttpResponseBase;
28 import org.apache.catalina.Globals;
29 import org.apache.catalina.util.CookieTools;
30
31 import org.apache.ajp.Ajp13;
32 import org.apache.tomcat.util.http.MimeHeaders;
33
34 public class Ajp13Response extends HttpResponseBase {
35
36     private Ajp13 ajp13;
37     private boolean finished = false;
38     private boolean headersSent = false;
39     private MimeHeaders headers = new MimeHeaders();
40     private StringBuffer JavaDoc cookieValue = new StringBuffer JavaDoc();
41
42     String JavaDoc getStatusMessage() {
43         return getStatusMessage(getStatus());
44     }
45
46     public void recycle() {
47         super.recycle();
48         this.finished = false;
49         this.headersSent = false;
50         this.headers.recycle();
51     }
52
53     protected void sendHeaders() throws IOException JavaDoc {
54
55         if (headersSent) {
56             // don't send headers twice
57
return;
58         }
59         headersSent = true;
60
61         int numHeaders = 0;
62
63         if (getContentType() != null) {
64             numHeaders++;
65     }
66         
67     if (getContentLength() >= 0) {
68             numHeaders++;
69     }
70
71     // Add the session ID cookie if necessary
72
HttpServletRequest JavaDoc hreq = (HttpServletRequest JavaDoc) request.getRequest();
73     HttpSession JavaDoc session = hreq.getSession(false);
74
75     if ((session != null) && session.isNew() && (getContext() != null)
76             && getContext().getCookies()) {
77         Cookie JavaDoc cookie = new Cookie JavaDoc(Globals.SESSION_COOKIE_NAME,
78                        session.getId());
79         cookie.setMaxAge(-1);
80         String JavaDoc contextPath = null;
81             if (context != null)
82                 contextPath = context.getPath();
83         if ((contextPath != null) && (contextPath.length() > 0))
84         cookie.setPath(contextPath);
85         else
86             cookie.setPath("/");
87         if (hreq.isSecure())
88         cookie.setSecure(true);
89         addCookie(cookie);
90     }
91
92         // Send all specified cookies (if any)
93
synchronized (cookies) {
94         Iterator JavaDoc items = cookies.iterator();
95         while (items.hasNext()) {
96         Cookie JavaDoc cookie = (Cookie JavaDoc) items.next();
97
98                 cookieValue.delete(0, cookieValue.length());
99                 CookieTools.getCookieHeaderValue(cookie, cookieValue);
100                 
101                 addHeader(CookieTools.getCookieHeaderName(cookie),
102                           cookieValue.toString());
103         }
104     }
105
106         // figure out how many headers...
107
// can have multiple headers of the same name...
108
// need to loop through headers once to get total
109
// count, once to add header to outBuf
110
String JavaDoc[] hnames = getHeaderNames();
111         Object JavaDoc[] hvalues = new Object JavaDoc[hnames.length];
112
113         int i;
114         for (i = 0; i < hnames.length; ++i) {
115             String JavaDoc[] tmp = getHeaderValues(hnames[i]);
116             numHeaders += tmp.length;
117             hvalues[i] = tmp;
118         }
119
120         ajp13.beginSendHeaders(getStatus(), getStatusMessage(getStatus()), numHeaders);
121
122         // send each header
123
if (getContentType() != null) {
124         ajp13.sendHeader("Content-Type", getContentType());
125     }
126         
127     if (getContentLength() >= 0) {
128         ajp13.sendHeader("Content-Length", String.valueOf(getContentLength()));
129     }
130
131         for (i = 0; i < hnames.length; ++i) {
132         String JavaDoc name = hnames[i];
133             String JavaDoc[] values = (String JavaDoc[])hvalues[i];
134
135             for (int j = 0; j < values.length; ++j) {
136                 ajp13.sendHeader(name, values[j]);
137             }
138         }
139
140         ajp13.endSendHeaders();
141
142         // The response is now committed
143
committed = true;
144     }
145
146     public void finishResponse() throws IOException JavaDoc {
147     if(!this.finished) {
148             try {
149                 super.finishResponse();
150             } catch( Throwable JavaDoc t ) {
151                 t.printStackTrace();
152             }
153             this.finished = true; // Avoid END_OF_RESPONSE sent 2 times
154
ajp13.finish();
155     }
156     }
157
158     void setAjp13(Ajp13 ajp13) {
159         this.ajp13 = ajp13;
160     }
161
162     Ajp13 getAjp13() {
163         return this.ajp13;
164     }
165 }
166
Popular Tags