1 16 17 package org.apache.ajp.tomcat4; 18 19 import java.io.IOException ; 20 21 import java.util.Iterator ; 22 23 import javax.servlet.http.Cookie ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpSession ; 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 cookieValue = new StringBuffer (); 41 42 String 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 { 54 55 if (headersSent) { 56 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 HttpServletRequest hreq = (HttpServletRequest ) request.getRequest(); 73 HttpSession session = hreq.getSession(false); 74 75 if ((session != null) && session.isNew() && (getContext() != null) 76 && getContext().getCookies()) { 77 Cookie cookie = new Cookie (Globals.SESSION_COOKIE_NAME, 78 session.getId()); 79 cookie.setMaxAge(-1); 80 String 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 synchronized (cookies) { 94 Iterator items = cookies.iterator(); 95 while (items.hasNext()) { 96 Cookie cookie = (Cookie ) 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 String [] hnames = getHeaderNames(); 111 Object [] hvalues = new Object [hnames.length]; 112 113 int i; 114 for (i = 0; i < hnames.length; ++i) { 115 String [] tmp = getHeaderValues(hnames[i]); 116 numHeaders += tmp.length; 117 hvalues[i] = tmp; 118 } 119 120 ajp13.beginSendHeaders(getStatus(), getStatusMessage(getStatus()), numHeaders); 121 122 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 name = hnames[i]; 133 String [] values = (String [])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 committed = true; 144 } 145 146 public void finishResponse() throws IOException { 147 if(!this.finished) { 148 try { 149 super.finishResponse(); 150 } catch( Throwable t ) { 151 t.printStackTrace(); 152 } 153 this.finished = true; 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 |