View Javadoc

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