View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.ajp;
15  
16  import java.io.ByteArrayInputStream;
17  import java.io.IOException;
18  import java.security.cert.CertificateFactory;
19  import java.security.cert.X509Certificate;
20  import java.util.Collection;
21  
22  import javax.servlet.ServletInputStream;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.eclipse.jetty.http.HttpException;
26  import org.eclipse.jetty.io.Buffer;
27  import org.eclipse.jetty.io.EndPoint;
28  import org.eclipse.jetty.server.Connector;
29  import org.eclipse.jetty.server.HttpConnection;
30  import org.eclipse.jetty.server.Request;
31  import org.eclipse.jetty.server.Server;
32  
33  /**
34   * Connection implementation of the Ajp13 protocol. <p/> XXX Refactor to remove
35   * duplication of HttpConnection
36   * 
37   */
38  public class Ajp13Connection extends HttpConnection
39  {
40      public Ajp13Connection(Connector connector, EndPoint endPoint, Server server)
41      {
42          super(connector, endPoint, server,
43                  new Ajp13Parser(connector.getRequestBuffers(), endPoint),
44                  new Ajp13Generator(connector.getResponseBuffers(), endPoint),
45                  new Ajp13Request()
46                  );
47          
48          ((Ajp13Parser)_parser).setEventHandler(new RequestHandler());
49          ((Ajp13Parser)_parser).setGenerator((Ajp13Generator)_generator);
50          ((Ajp13Request)_request).setConnection(this);
51      }
52  
53      @Override
54      public boolean isConfidential(Request request)
55      {
56          return ((Ajp13Request) request).isSslSecure();
57      }
58  
59      @Override
60      public boolean isIntegral(Request request)
61      {
62          return ((Ajp13Request) request).isSslSecure();
63      }
64  
65      @Override
66      public ServletInputStream getInputStream()
67      {
68          if (_in == null)
69              _in = new Ajp13Parser.Input((Ajp13Parser) _parser, _connector.getMaxIdleTime());
70          return _in;
71      }
72  
73      private class RequestHandler implements Ajp13Parser.EventHandler
74      {
75          boolean _delayedHandling = false;
76  
77          public void startForwardRequest() throws IOException
78          {
79              _delayedHandling = false;
80              _uri.clear();
81  	    
82              ((Ajp13Request) _request).setSslSecure(false);
83              _request.setTimeStamp(System.currentTimeMillis());
84              _request.setUri(_uri);
85              
86          }
87  
88          public void parsedAuthorizationType(Buffer authType) throws IOException
89          {
90              //TODO JASPI this doesn't appear to make sense yet... how does ajp auth fit into jetty auth?
91  //            _request.setAuthType(authType.toString());
92          }
93  
94          public void parsedRemoteUser(Buffer remoteUser) throws IOException
95          {
96              ((Ajp13Request)_request).setRemoteUser(remoteUser.toString());
97          }
98  
99          public void parsedServletPath(Buffer servletPath) throws IOException
100         {
101             _request.setServletPath(servletPath.toString());
102         }
103 
104         public void parsedContextPath(Buffer context) throws IOException
105         {
106             _request.setContextPath(context.toString());
107         }
108 
109         public void parsedSslCert(Buffer sslCert) throws IOException
110         {
111             try 
112             {
113                 CertificateFactory cf = CertificateFactory.getInstance("X.509");
114                 ByteArrayInputStream bis = new ByteArrayInputStream(sslCert.toString().getBytes());
115 
116                 Collection<? extends java.security.cert.Certificate> certCollection = cf.generateCertificates(bis);
117                 X509Certificate[] certificates = new X509Certificate[certCollection.size()];
118 
119                 int i=0;
120                 for (Object aCertCollection : certCollection)
121                 {
122                     certificates[i++] = (X509Certificate) aCertCollection;
123                 }
124 
125                 _request.setAttribute("javax.servlet.request.X509Certificate", certificates);
126             } 
127             catch (Exception e) 
128             {
129                 org.eclipse.jetty.util.log.Log.warn(e.toString());
130                 org.eclipse.jetty.util.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                 _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                 _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             _request.setServerPort(port);
198         }
199 
200         public void parsedSslSecure(boolean secure) throws IOException
201         {
202             ((Ajp13Request) _request).setSslSecure(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 }