View Javadoc

1   //========================================================================
2   //Copyright 2006 Mort Bay Consulting Pty. Ltd.
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   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  package org.mortbay.jetty.ajp;
16  
17  import java.io.ByteArrayInputStream;
18  import java.io.IOException;
19  import java.security.cert.Certificate;
20  import java.security.cert.CertificateException;
21  import java.security.cert.CertificateFactory;
22  import java.security.cert.X509Certificate;
23  import java.util.Collection;
24  import java.util.Iterator;
25  
26  import javax.servlet.ServletInputStream;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.mortbay.io.Buffer;
30  import org.mortbay.io.EndPoint;
31  import org.mortbay.jetty.Connector;
32  import org.mortbay.jetty.HttpConnection;
33  import org.mortbay.jetty.HttpException;
34  import org.mortbay.jetty.Request;
35  import org.mortbay.jetty.Server;
36  
37  /**
38   * Connection implementation of the Ajp13 protocol. <p/> XXX Refactor to remove
39   * duplication of HttpConnection
40   * 
41   * @author Markus Kobler markus(at)inquisitive-mind.com
42   * @author Greg Wilkins
43   */
44  public class Ajp13Connection extends HttpConnection
45  {
46      private boolean _sslSecure = false;
47  
48      public Ajp13Connection(Connector connector, EndPoint endPoint, Server server)
49      {
50          super(connector, endPoint, server);
51          _request = new Ajp13Request(this);
52          _generator = new Ajp13Generator(_connector, _endp, _connector.getHeaderBufferSize(), _connector.getResponseBufferSize());
53          _parser = new Ajp13Parser(_connector, _endp, new RequestHandler(), (Ajp13Generator) _generator);
54          _generator.setSendServerVersion(server.getSendServerVersion());
55          _server = server;
56  
57      }
58  
59      public boolean isConfidential(Request request)
60      {
61          return _sslSecure;
62      }
63  
64      public boolean isIntegral(Request request)
65      {
66          return _sslSecure;
67      }
68  
69      public ServletInputStream getInputStream()
70      {
71          if (_in == null)
72              _in = new Ajp13Parser.Input((Ajp13Parser) _parser, _connector.getMaxIdleTime());
73          return _in;
74      }
75  
76      private class RequestHandler implements Ajp13Parser.EventHandler
77      {
78          boolean _delayedHandling = false;
79  
80          public void startForwardRequest() throws IOException
81          {
82              _delayedHandling = false;
83              _uri.clear();
84              _sslSecure = false;
85              _request.setTimeStamp(System.currentTimeMillis());
86              _request.setUri(_uri);
87              
88          }
89  
90          public void parsedAuthorizationType(Buffer authType) throws IOException
91          {
92              _request.setAuthType(authType.toString());
93          }
94  
95          public void parsedRemoteUser(Buffer remoteUser) throws IOException
96          {
97              ((Ajp13Request)_request).setRemoteUser(remoteUser.toString());
98          }
99  
100         public void parsedServletPath(Buffer servletPath) throws IOException
101         {
102             _request.setServletPath(servletPath.toString());
103         }
104 
105         public void parsedContextPath(Buffer context) throws IOException
106         {
107             _request.setContextPath(context.toString());
108         }
109 
110         public void parsedSslCert(Buffer sslCert) throws IOException
111         {
112             try 
113             {
114                 CertificateFactory cf = CertificateFactory.getInstance("X.509");
115                 ByteArrayInputStream bis = new ByteArrayInputStream(sslCert.toString().getBytes());
116 
117                 Collection certCollection = cf.generateCertificates(bis);
118                 X509Certificate[] certificates = new X509Certificate[certCollection.size()];
119 
120                 int i=0;
121                 Iterator iter=certCollection.iterator();
122                 while(iter.hasNext())
123                     certificates[i++] = (X509Certificate)iter.next();
124 
125                 _request.setAttribute("javax.servlet.request.X509Certificate", certificates);
126             } 
127             catch (Exception e) 
128             {
129                 org.mortbay.log.Log.warn(e.toString());
130                 org.mortbay.log.Log.ignore(e);
131                 if (sslCert!=null)
132                     _request.setAttribute("javax.servlet.request.X509Certificate", sslCert.toString());
133             }
134         }
135 
136         public void parsedSslCipher(Buffer sslCipher) throws IOException
137         {
138             _request.setAttribute("javax.servlet.request.cipher_suite", sslCipher.toString());
139         }
140 
141         public void parsedSslSession(Buffer sslSession) throws IOException
142         {
143             _request.setAttribute("javax.servlet.request.ssl_session", sslSession.toString());
144         }
145         
146         public void parsedSslKeySize(int keySize) throws IOException
147         {
148            _request.setAttribute("javax.servlet.request.key_size", new Integer(keySize));
149         }
150 
151         public void parsedMethod(Buffer method) throws IOException
152         {
153             if (method == null)
154                 throw new HttpException(HttpServletResponse.SC_BAD_REQUEST);
155             _request.setMethod(method.toString());
156         }
157 
158         public void parsedUri(Buffer uri) throws IOException
159         {
160             _uri.parse(uri.toString());
161         }
162 
163         public void parsedProtocol(Buffer protocol) throws IOException
164         {
165             if (protocol != null && protocol.length()>0)
166             {
167                 _request.setProtocol(protocol.toString());
168             }
169         }
170 
171         public void parsedRemoteAddr(Buffer addr) throws IOException
172         {
173             if (addr != null && addr.length()>0)
174             {
175                 ((Ajp13Request) _request).setRemoteAddr(addr.toString());
176             }
177         }
178 
179         public void parsedRemoteHost(Buffer name) throws IOException
180         {
181             if (name != null && name.length()>0)
182             {
183                 ((Ajp13Request) _request).setRemoteHost(name.toString());
184             }
185         }
186 
187         public void parsedServerName(Buffer name) throws IOException
188         {
189             if (name != null && name.length()>0)
190             {
191                 _request.setServerName(name.toString());
192             }
193         }
194 
195         public void parsedServerPort(int port) throws IOException
196         {
197             ((Ajp13Request) _request).setServerPort(port);
198         }
199 
200         public void parsedSslSecure(boolean secure) throws IOException
201         {
202             _sslSecure = secure;
203         }
204 
205         public void parsedQueryString(Buffer value) throws IOException
206         {
207             String u = _uri + "?" + value;
208             _uri.parse(u);
209         }
210 
211         public void parsedHeader(Buffer name, Buffer value) throws IOException
212         {
213             _requestFields.add(name, value);
214         }
215 
216         public void parsedRequestAttribute(String key, Buffer value) throws IOException
217         {
218             _request.setAttribute(key, value.toString());
219         }
220         
221         public void parsedRequestAttribute(String key, int value) throws IOException
222         {
223             _request.setAttribute(key, Integer.toString(value));
224         }
225 
226         public void headerComplete() throws IOException
227         {
228             if (((Ajp13Parser) _parser).getContentLength() <= 0)
229             {
230                 handleRequest();
231             }
232             else
233             {
234                 _delayedHandling = true;
235             }
236         }
237 
238         public void messageComplete(long contextLength) throws IOException
239         {
240         }
241 
242         public void content(Buffer ref) throws IOException
243         {
244             if (_delayedHandling)
245             {
246                 _delayedHandling = false;
247                 handleRequest();
248             }
249         }
250 
251     }
252 
253 }