View Javadoc

1   // ========================================================================
2   // Copyright (c) 2010-2011 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  package org.eclipse.jetty.nested;
14  
15  import java.io.IOException;
16  
17  import javax.servlet.ServletException;
18  import javax.servlet.ServletRequest;
19  import javax.servlet.ServletResponse;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.eclipse.jetty.server.AbstractConnector;
24  import org.eclipse.jetty.server.Connector;
25  
26  /**
27   * Nested Jetty Connector
28   * <p>
29   * This Jetty {@link Connector} allows a jetty instance to be nested inside another servlet container.
30   * Requests received by the outer servlet container should be passed to jetty using the {@link #service(ServletRequest, ServletResponse)} method of this connector. 
31   *
32   */
33  public class NestedConnector extends AbstractConnector
34  {
35      String _serverInfo;
36      
37      public NestedConnector()
38      {
39          setAcceptors(0);
40      }
41      
42      public void open() throws IOException
43      {
44      }
45  
46      public void close() throws IOException
47      {
48      }
49  
50      public int getLocalPort()
51      {
52          return -1;
53      }
54  
55      public Object getConnection()
56      {
57          return null;
58      }
59  
60      @Override
61      protected void accept(int acceptorID) throws IOException, InterruptedException
62      {
63          throw new IllegalStateException();
64      }
65      
66      /**
67       * Service a request of the outer servlet container by passing it to the nested instance of Jetty.
68       * @param outerRequest
69       * @param outerResponse
70       * @throws IOException
71       * @throws ServletException
72       */
73      public void service(ServletRequest outerRequest, ServletResponse outerResponse) throws IOException, ServletException
74      {
75          HttpServletRequest request = (HttpServletRequest)outerRequest;
76          HttpServletResponse response = (HttpServletResponse)outerResponse;
77          NestedConnection connection=new NestedConnection(this,new NestedEndPoint(request,response),request,response,_serverInfo);
78          connection.service();
79      }
80  
81  }