001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.ajp.tomcat4;
018:
019: import java.io.IOException;
020:
021: import java.util.Iterator;
022:
023: import javax.servlet.http.Cookie;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpSession;
026:
027: import org.apache.catalina.connector.HttpResponseBase;
028: import org.apache.catalina.Globals;
029: import org.apache.catalina.util.CookieTools;
030:
031: import org.apache.ajp.Ajp13;
032: import org.apache.tomcat.util.http.MimeHeaders;
033:
034: public class Ajp13Response extends HttpResponseBase {
035:
036: private Ajp13 ajp13;
037: private boolean finished = false;
038: private boolean headersSent = false;
039: private MimeHeaders headers = new MimeHeaders();
040: private StringBuffer cookieValue = new StringBuffer();
041:
042: String getStatusMessage() {
043: return getStatusMessage(getStatus());
044: }
045:
046: public void recycle() {
047: super .recycle();
048: this .finished = false;
049: this .headersSent = false;
050: this .headers.recycle();
051: }
052:
053: protected void sendHeaders() throws IOException {
054:
055: if (headersSent) {
056: // don't send headers twice
057: return;
058: }
059: headersSent = true;
060:
061: int numHeaders = 0;
062:
063: if (getContentType() != null) {
064: numHeaders++;
065: }
066:
067: if (getContentLength() >= 0) {
068: numHeaders++;
069: }
070:
071: // Add the session ID cookie if necessary
072: HttpServletRequest hreq = (HttpServletRequest) request
073: .getRequest();
074: HttpSession session = hreq.getSession(false);
075:
076: if ((session != null) && session.isNew()
077: && (getContext() != null) && getContext().getCookies()) {
078: Cookie cookie = new Cookie(Globals.SESSION_COOKIE_NAME,
079: session.getId());
080: cookie.setMaxAge(-1);
081: String contextPath = null;
082: if (context != null)
083: contextPath = context.getPath();
084: if ((contextPath != null) && (contextPath.length() > 0))
085: cookie.setPath(contextPath);
086: else
087: cookie.setPath("/");
088: if (hreq.isSecure())
089: cookie.setSecure(true);
090: addCookie(cookie);
091: }
092:
093: // Send all specified cookies (if any)
094: synchronized (cookies) {
095: Iterator items = cookies.iterator();
096: while (items.hasNext()) {
097: Cookie cookie = (Cookie) items.next();
098:
099: cookieValue.delete(0, cookieValue.length());
100: CookieTools.getCookieHeaderValue(cookie, cookieValue);
101:
102: addHeader(CookieTools.getCookieHeaderName(cookie),
103: cookieValue.toString());
104: }
105: }
106:
107: // figure out how many headers...
108: // can have multiple headers of the same name...
109: // need to loop through headers once to get total
110: // count, once to add header to outBuf
111: String[] hnames = getHeaderNames();
112: Object[] hvalues = new Object[hnames.length];
113:
114: int i;
115: for (i = 0; i < hnames.length; ++i) {
116: String[] tmp = getHeaderValues(hnames[i]);
117: numHeaders += tmp.length;
118: hvalues[i] = tmp;
119: }
120:
121: ajp13.beginSendHeaders(getStatus(),
122: getStatusMessage(getStatus()), numHeaders);
123:
124: // send each header
125: if (getContentType() != null) {
126: ajp13.sendHeader("Content-Type", getContentType());
127: }
128:
129: if (getContentLength() >= 0) {
130: ajp13.sendHeader("Content-Length", String
131: .valueOf(getContentLength()));
132: }
133:
134: for (i = 0; i < hnames.length; ++i) {
135: String name = hnames[i];
136: String[] values = (String[]) hvalues[i];
137:
138: for (int j = 0; j < values.length; ++j) {
139: ajp13.sendHeader(name, values[j]);
140: }
141: }
142:
143: ajp13.endSendHeaders();
144:
145: // The response is now committed
146: committed = true;
147: }
148:
149: public void finishResponse() throws IOException {
150: if (!this .finished) {
151: try {
152: super .finishResponse();
153: } catch (Throwable t) {
154: t.printStackTrace();
155: }
156: this .finished = true; // Avoid END_OF_RESPONSE sent 2 times
157: ajp13.finish();
158: }
159: }
160:
161: void setAjp13(Ajp13 ajp13) {
162: this .ajp13 = ajp13;
163: }
164:
165: Ajp13 getAjp13() {
166: return this.ajp13;
167: }
168: }
|