Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/ajp/tomcat4/Ajp13Response.java


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;
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              // 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 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          // Send all specified cookies (if any)
93    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         // 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[] 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         // 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 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         // The response is now committed
143         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; // 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 }