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