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   * 
39   */
40  public class Ajp13Connection extends HttpConnection
41  {
42      public Ajp13Connection(Connector connector, EndPoint endPoint, Server server)
43      {
44          super(connector, endPoint, server,
45                  new Ajp13Parser(connector.getRequestBuffers(), endPoint),
46                  new Ajp13Generator(connector.getResponseBuffers(), endPoint),
47                  new Ajp13Request()
48                  );
49          
50          ((Ajp13Parser)_parser).setEventHandler(new RequestHandler());
51          ((Ajp13Parser)_parser).setGenerator((Ajp13Generator)_generator);
52          ((Ajp13Request)_request).setConnection(this);
53      }
54  
55      public boolean isConfidential(Request request)
56      {
57          return ((Ajp13Request) request).isSslSecure();
58      }
59  
60      public boolean isIntegral(Request request)
61      {
62          return ((Ajp13Request) request).isSslSecure();
63      }
64  
65      public ServletInputStream getInputStream()
66      {
67          if (_in == null)
68              _in = new Ajp13Parser.Input((Ajp13Parser) _parser, _connector.getMaxIdleTime());
69          return _in;
70      }
71  
72      private class RequestHandler implements Ajp13Parser.EventHandler
73      {
74          boolean _delayedHandling = false;
75  
76          public void startForwardRequest() throws IOException
77          {
78              _delayedHandling = false;
79              _uri.clear();
80  	    
81              ((Ajp13Request) _request).setSslSecure(false);
82              _request.setTimeStamp(System.currentTimeMillis());
83              _request.setUri(_uri);
84              
85          }
86  
87          public void parsedAuthorizationType(Buffer authType) throws IOException
88          {
89              //TODO JASPI this doesn't appear to make sense yet... how does ajp auth fit into jetty auth?
90  //            _request.setAuthType(authType.toString());
91          }
92  
93          public void parsedRemoteUser(Buffer remoteUser) throws IOException
94          {
95              ((Ajp13Request)_request).setRemoteUser(remoteUser.toString());
96          }
97  
98          public void parsedServletPath(Buffer servletPath) throws IOException
99          {
100             _request.setServletPath(servletPath.toString());
101         }
102 
103         public void parsedContextPath(Buffer context) throws IOException
104         {
105             _request.setContextPath(context.toString());
106         }
107 
108         public void parsedSslCert(Buffer sslCert) throws IOException
109         {
110             try 
111             {
112                 CertificateFactory cf = CertificateFactory.getInstance("X.509");
113                 ByteArrayInputStream bis = new ByteArrayInputStream(sslCert.toString().getBytes());
114 
115                 Collection<? extends java.security.cert.Certificate> certCollection = cf.generateCertificates(bis);
116                 X509Certificate[] certificates = new X509Certificate[certCollection.size()];
117 
118                 int i=0;
119                 for (Object aCertCollection : certCollection)
120                 {
121                     certificates[i++] = (X509Certificate) aCertCollection;
122                 }
123 
124                 _request.setAttribute("javax.servlet.request.X509Certificate", certificates);
125             } 
126             catch (Exception e) 
127             {
128                 org.eclipse.jetty.util.log.Log.warn(e.toString());
129                 org.eclipse.jetty.util.log.Log.ignore(e);
130                 if (sslCert!=null)
131                     _request.setAttribute("javax.servlet.request.X509Certificate", sslCert.toString());
132             }
133         }
134 
135         public void parsedSslCipher(Buffer sslCipher) throws IOException
136         {
137             _request.setAttribute("javax.servlet.request.cipher_suite", sslCipher.toString());
138         }
139 
140         public void parsedSslSession(Buffer sslSession) throws IOException
141         {
142             _request.setAttribute("javax.servlet.request.ssl_session", sslSession.toString());
143         }
144 
145         public void parsedSslKeySize(int keySize) throws IOException
146         {
147            _request.setAttribute("javax.servlet.request.key_size", new Integer(keySize));
148         }
149 
150         public void parsedMethod(Buffer method) throws IOException
151         {
152             if (method == null)
153                 throw new HttpException(HttpServletResponse.SC_BAD_REQUEST);
154             _request.setMethod(method.toString());
155         }
156 
157         public void parsedUri(Buffer uri) throws IOException
158         {
159             _uri.parse(uri.toString());
160         }
161 
162         public void parsedProtocol(Buffer protocol) throws IOException
163         {
164             if (protocol != null && protocol.length()>0)
165             {
166                 _request.setProtocol(protocol.toString());
167             }
168         }
169 
170         public void parsedRemoteAddr(Buffer addr) throws IOException
171         {
172             if (addr != null && addr.length()>0)
173             {
174                 _request.setRemoteAddr(addr.toString());
175             }
176         }
177 
178         public void parsedRemoteHost(Buffer name) throws IOException
179         {
180             if (name != null && name.length()>0)
181             {
182                 _request.setRemoteHost(name.toString());
183             }
184         }
185 
186         public void parsedServerName(Buffer name) throws IOException
187         {
188             if (name != null && name.length()>0)
189             {
190                 _request.setServerName(name.toString());
191             }
192         }
193 
194         public void parsedServerPort(int port) throws IOException
195         {
196             _request.setServerPort(port);
197         }
198 
199         public void parsedSslSecure(boolean secure) throws IOException
200         {
201             ((Ajp13Request) _request).setSslSecure(secure);
202         }
203 
204         public void parsedQueryString(Buffer value) throws IOException
205         {
206             String u = _uri + "?" + value;
207             _uri.parse(u);
208         }
209 
210         public void parsedHeader(Buffer name, Buffer value) throws IOException
211         {
212             _requestFields.add(name, value);
213         }
214 
215         public void parsedRequestAttribute(String key, Buffer value) throws IOException
216         {
217             _request.setAttribute(key, value.toString());
218         }
219         
220         public void parsedRequestAttribute(String key, int value) throws IOException
221         {
222             _request.setAttribute(key, Integer.toString(value));
223         }
224 
225         public void headerComplete() throws IOException
226         {
227             if (((Ajp13Parser) _parser).getContentLength() <= 0)
228             {
229                 handleRequest();
230             }
231             else
232             {
233                 _delayedHandling = true;
234             }
235         }
236 
237         public void messageComplete(long contextLength) throws IOException
238         {
239         }
240 
241         public void content(Buffer ref) throws IOException
242         {
243             if (_delayedHandling)
244             {
245                 _delayedHandling = false;
246                 handleRequest();
247             }
248         }
249 
250     }
251 
252 }