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