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