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