View Javadoc

    1   //========================================================================
    2   //Copyright 2006 Mort Bay Consulting Pty. Ltd.
    3   //------------------------------------------------------------------------
    4   //Licensed under the Apache License, Version 2.0 (the "License");
    5   //you may not use this file except in compliance with the License.
    6   //You may obtain a copy of the License at
    7   //http://www.apache.org/licenses/LICENSE-2.0
    8   //Unless required by applicable law or agreed to in writing, software
    9   //distributed under the License is distributed on an "AS IS" BASIS,
    10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  //See the License for the specific language governing permissions and
    12  //limitations under the License.
    13  //========================================================================
    14
    15  package org.mortbay.jetty.ajp;
    16
    17  import java.io.ByteArrayInputStream;
    18  import java.io.IOException;
    19  import java.security.cert.CertificateFactory;
    20  import java.security.cert.X509Certificate;
    21  import java.util.Collection;
    22  import java.util.Iterator;
    23
    24  import javax.servlet.ServletInputStream;
    25  import javax.servlet.http.HttpServletResponse;
    26
    27  import org.mortbay.io.Buffer;
    28  import org.mortbay.io.EndPoint;
    29  import org.mortbay.jetty.Connector;
    30  import org.mortbay.jetty.HttpConnection;
    31  import org.mortbay.jetty.HttpException;
    32  import org.mortbay.jetty.Request;
    33  import org.mortbay.jetty.Server;
    34
    35  /**
    36   * Connection implementation of the Ajp13 protocol. <p/> XXX Refactor to remove
    37   * duplication of HttpConnection
    38   *
    39   * @author Markus Kobler markus(at)inquisitive-mind.com
    40   * @author Greg Wilkins
    41   */
    42  public class Ajp13Connection extends HttpConnection
    43  {
    44      public Ajp13Connection(Connector connector, EndPoint endPoint, Server server)
    45      {
    46          super(connector, endPoint, server,
    47                  new Ajp13Parser(connector, endPoint),
    48                  new Ajp13Generator(connector, endPoint, connector.getHeaderBufferSize(), connector.getResponseBufferSize()),
    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      public boolean isConfidential(Request request)
    58      {
    59          return ((Ajp13Request) request).isSslSecure();
    60      }
    61
    62      public boolean isIntegral(Request request)
    63      {
    64          return ((Ajp13Request) request).isSslSecure();
    65      }
    66
    67      public ServletInputStream getInputStream()
    68      {
    69          if (_in == null)
    70              _in = new Ajp13Parser.Input((Ajp13Parser) _parser, _connector.getMaxIdleTime());
    71          return _in;
    72      }
    73
    74      private class RequestHandler implements Ajp13Parser.EventHandler
    75      {
    76          boolean _delayedHandling = false;
    77
    78          public void startForwardRequest() throws IOException
    79          {
    80              _delayedHandling = false;
    81              _uri.clear();
    82              ((Ajp13Request) _request).setSslSecure(false);
    83              _request.setTimeStamp(System.currentTimeMillis());
    84              _request.setUri(_uri);
    85
    86          }
    87
    88          public void parsedAuthorizationType(Buffer authType) throws IOException
    89          {
    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 certCollection = cf.generateCertificates(bis);
    116                 X509Certificate[] certificates = new X509Certificate[certCollection.size()];
    117
    118                 int i=0;
    119                 Iterator iter=certCollection.iterator();
    120                 while(iter.hasNext())
    121                     certificates[i++] = (X509Certificate)iter.next();
    122
    123                 _request.setAttribute("javax.servlet.request.X509Certificate", certificates);
    124             }
    125             catch (Exception e)
    126             {
    127                 org.mortbay.log.Log.warn(e.toString());
    128                 org.mortbay.log.Log.ignore(e);
    129                 if (sslCert!=null)
    130                     _request.setAttribute("javax.servlet.request.X509Certificate", sslCert.toString());
    131             }
    132         }
    133
    134         public void parsedSslCipher(Buffer sslCipher) throws IOException
    135         {
    136             _request.setAttribute("javax.servlet.request.cipher_suite", sslCipher.toString());
    137         }
    138
    139         public void parsedSslSession(Buffer sslSession) throws IOException
    140         {
    141             _request.setAttribute("javax.servlet.request.ssl_session", sslSession.toString());
    142         }
    143
    144         public void parsedSslKeySize(int keySize) throws IOException
    145         {
    146            _request.setAttribute("javax.servlet.request.key_size", new Integer(keySize));
    147         }
    148
    149         public void parsedMethod(Buffer method) throws IOException
    150         {
    151             if (method == null)
    152                 throw new HttpException(HttpServletResponse.SC_BAD_REQUEST);
    153             _request.setMethod(method.toString());
    154         }
    155
    156         public void parsedUri(Buffer uri) throws IOException
    157         {
    158             _uri.parse(uri.toString());
    159         }
    160
    161         public void parsedProtocol(Buffer protocol) throws IOException
    162         {
    163             if (protocol != null && protocol.length()>0)
    164             {
    165                 _request.setProtocol(protocol.toString());
    166             }
    167         }
    168
    169         public void parsedRemoteAddr(Buffer addr) throws IOException
    170         {
    171             if (addr != null && addr.length()>0)
    172             {
    173                 ((Ajp13Request) _request).setRemoteAddr(addr.toString());
    174             }
    175         }
    176
    177         public void parsedRemoteHost(Buffer name) throws IOException
    178         {
    179             if (name != null && name.length()>0)
    180             {
    181                 ((Ajp13Request) _request).setRemoteHost(name.toString());
    182             }
    183         }
    184
    185         public void parsedServerName(Buffer name) throws IOException
    186         {
    187             if (name != null && name.length()>0)
    188             {
    189                 _request.setServerName(name.toString());
    190             }
    191         }
    192
    193         public void parsedServerPort(int port) throws IOException
    194         {
    195             ((Ajp13Request) _request).setServerPort(port);
    196         }
    197
    198         public void parsedSslSecure(boolean secure) throws IOException
    199         {
    200             ((Ajp13Request) _request).setSslSecure(secure);
    201         }
    202
    203         public void parsedQueryString(Buffer value) throws IOException
    204         {
    205             String u = _uri + "?" + value;
    206             _uri.parse(u);
    207         }
    208
    209         public void parsedHeader(Buffer name, Buffer value) throws IOException
    210         {
    211             _requestFields.add(name, value);
    212         }
    213
    214         public void parsedRequestAttribute(String key, Buffer value) throws IOException
    215         {
    216             _request.setAttribute(key, value.toString());
    217         }
    218
    219         public void parsedRequestAttribute(String key, int value) throws IOException
    220         {
    221             _request.setAttribute(key, Integer.toString(value));
    222         }
    223
    224         public void headerComplete() throws IOException
    225         {
    226             if (((Ajp13Parser) _parser).getContentLength() <= 0)
    227             {
    228                 handleRequest();
    229             }
    230             else
    231             {
    232                 _delayedHandling = true;
    233             }
    234         }
    235
    236         public void messageComplete(long contextLength) throws IOException
    237         {
    238         }
    239
    240         public void content(Buffer ref) throws IOException
    241         {
    242             if (_delayedHandling)
    243             {
    244                 _delayedHandling = false;
    245                 handleRequest();
    246             }
    247         }
    248
    249     }
    250
    251 }