View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.ajp;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.IOException;
23  import java.security.cert.CertificateFactory;
24  import java.security.cert.X509Certificate;
25  import java.util.Collection;
26  
27  import javax.servlet.ServletInputStream;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.eclipse.jetty.http.HttpException;
31  import org.eclipse.jetty.io.Buffer;
32  import org.eclipse.jetty.io.EndPoint;
33  import org.eclipse.jetty.server.BlockingHttpConnection;
34  import org.eclipse.jetty.server.Connector;
35  import org.eclipse.jetty.server.Request;
36  import org.eclipse.jetty.server.Server;
37  import org.eclipse.jetty.util.log.Log;
38  import org.eclipse.jetty.util.log.Logger;
39  
40  /**
41   * Connection implementation of the Ajp13 protocol. <p/> XXX Refactor to remove
42   * duplication of HttpConnection
43   * 
44   */
45  public class Ajp13Connection extends BlockingHttpConnection
46  {
47      private static final Logger LOG = Log.getLogger(Ajp13Connection.class);
48  
49      public Ajp13Connection(Connector connector, EndPoint endPoint, Server server)
50      {
51          super(connector, endPoint, server,
52                  new Ajp13Parser(connector.getRequestBuffers(), endPoint),
53                  new Ajp13Generator(connector.getResponseBuffers(), endPoint),
54                  new Ajp13Request()
55                  );
56          
57          ((Ajp13Parser)_parser).setEventHandler(new RequestHandler());
58          ((Ajp13Parser)_parser).setGenerator((Ajp13Generator)_generator);
59          ((Ajp13Request)_request).setConnection(this);
60      }
61  
62      @Override
63      public boolean isConfidential(Request request)
64      {
65          return ((Ajp13Request) request).isSslSecure();
66      }
67  
68      @Override
69      public boolean isIntegral(Request request)
70      {
71          return ((Ajp13Request) request).isSslSecure();
72      }
73  
74      @Override
75      public ServletInputStream getInputStream()
76      {
77          if (_in == null)
78              _in = new Ajp13Parser.Input((Ajp13Parser) _parser, _connector.getMaxIdleTime());
79          return _in;
80      }
81  
82      private class RequestHandler implements Ajp13Parser.EventHandler
83      {
84          public void startForwardRequest() throws IOException
85          {
86              _uri.clear();
87  	    
88              ((Ajp13Request) _request).setSslSecure(false);
89              _request.setTimeStamp(System.currentTimeMillis());
90              _request.setUri(_uri);
91              
92          }
93  
94          public void parsedAuthorizationType(Buffer authType) throws IOException
95          {
96              //TODO JASPI this doesn't appear to make sense yet... how does ajp auth fit into jetty auth?
97  //            _request.setAuthType(authType.toString());
98          }
99  
100         public void parsedRemoteUser(Buffer remoteUser) throws IOException
101         {
102             ((Ajp13Request)_request).setRemoteUser(remoteUser.toString());
103         }
104 
105         public void parsedServletPath(Buffer servletPath) throws IOException
106         {
107             _request.setServletPath(servletPath.toString());
108         }
109 
110         public void parsedContextPath(Buffer context) throws IOException
111         {
112             _request.setContextPath(context.toString());
113         }
114 
115         public void parsedSslCert(Buffer sslCert) throws IOException
116         {
117             try 
118             {
119                 CertificateFactory cf = CertificateFactory.getInstance("X.509");
120                 ByteArrayInputStream bis = new ByteArrayInputStream(sslCert.toString().getBytes());
121 
122                 Collection<? extends java.security.cert.Certificate> certCollection = cf.generateCertificates(bis);
123                 X509Certificate[] certificates = new X509Certificate[certCollection.size()];
124 
125                 int i=0;
126                 for (Object aCertCollection : certCollection)
127                 {
128                     certificates[i++] = (X509Certificate) aCertCollection;
129                 }
130 
131                 _request.setAttribute("javax.servlet.request.X509Certificate", certificates);
132             } 
133             catch (Exception e) 
134             {
135                 LOG.warn(e.toString());
136                 LOG.ignore(e);
137                 if (sslCert!=null)
138                     _request.setAttribute("javax.servlet.request.X509Certificate", sslCert.toString());
139             }
140         }
141 
142         public void parsedSslCipher(Buffer sslCipher) throws IOException
143         {
144             _request.setAttribute("javax.servlet.request.cipher_suite", sslCipher.toString());
145         }
146 
147         public void parsedSslSession(Buffer sslSession) throws IOException
148         {
149             _request.setAttribute("javax.servlet.request.ssl_session", sslSession.toString());
150         }
151 
152         public void parsedSslKeySize(int keySize) throws IOException
153         {
154            _request.setAttribute("javax.servlet.request.key_size", new Integer(keySize));
155         }
156 
157         public void parsedMethod(Buffer method) throws IOException
158         {
159             if (method == null)
160                 throw new HttpException(HttpServletResponse.SC_BAD_REQUEST);
161             _request.setMethod(method.toString());
162         }
163 
164         public void parsedUri(Buffer uri) throws IOException
165         {
166             _uri.parse(uri.toString());
167         }
168 
169         public void parsedProtocol(Buffer protocol) throws IOException
170         {
171             if (protocol != null && protocol.length()>0)
172             {
173                 _request.setProtocol(protocol.toString());
174             }
175         }
176 
177         public void parsedRemoteAddr(Buffer addr) throws IOException
178         {
179             if (addr != null && addr.length()>0)
180             {
181                 _request.setRemoteAddr(addr.toString());
182             }
183         }
184 
185         public void parsedRemoteHost(Buffer name) throws IOException
186         {
187             if (name != null && name.length()>0)
188             {
189                 _request.setRemoteHost(name.toString());
190             }
191         }
192 
193         public void parsedServerName(Buffer name) throws IOException
194         {
195             if (name != null && name.length()>0)
196             {
197                 _request.setServerName(name.toString());
198             }
199         }
200 
201         public void parsedServerPort(int port) throws IOException
202         {
203             _request.setServerPort(port);
204         }
205 
206         public void parsedSslSecure(boolean secure) throws IOException
207         {
208             ((Ajp13Request) _request).setSslSecure(secure);
209         }
210 
211         public void parsedQueryString(Buffer value) throws IOException
212         {
213             String u = _uri + "?" + value;
214             _uri.parse(u);
215         }
216 
217         public void parsedHeader(Buffer name, Buffer value) throws IOException
218         {
219             _requestFields.add(name, value);
220         }
221 
222         public void parsedRequestAttribute(String key, Buffer value) throws IOException
223         {
224             if (value==null)
225                 _request.removeAttribute(key);
226             else
227                 _request.setAttribute(key,value.toString());
228         }
229         
230         public void parsedRequestAttribute(String key, int value) throws IOException
231         {
232             _request.setAttribute(key, Integer.toString(value));
233         }
234 
235         public void headerComplete() throws IOException
236         {
237             handleRequest();
238         }
239 
240         public void messageComplete(long contextLength) throws IOException
241         {
242         }
243 
244         public void content(Buffer ref) throws IOException
245         {
246         }
247 
248     }
249 
250 }