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  import java.util.Enumeration;
17  
18  import javax.servlet.DispatcherType;
19  import javax.servlet.ServletException;
20  import javax.servlet.ServletInputStream;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.eclipse.jetty.http.HttpFields;
25  import org.eclipse.jetty.http.HttpURI;
26  import org.eclipse.jetty.io.Connection;
27  import org.eclipse.jetty.server.HttpConnection;
28  
29  
30  public class NestedConnection extends HttpConnection
31  {
32      protected NestedConnection(final NestedConnector connector, final NestedEndPoint endp, final HttpServletRequest outerRequest, HttpServletResponse outerResponse,String nestedIn)
33          throws IOException
34      {
35          super(connector,
36                endp,
37                connector.getServer(),
38                new NestedParser(),
39                new NestedGenerator(connector.getResponseBuffers(),endp,outerResponse,nestedIn),
40                new NestedRequest(outerRequest));
41  
42          ((NestedRequest)_request).setConnection(this);
43          
44          // Set the request line
45          _request.setDispatcherType(DispatcherType.REQUEST);
46          _request.setScheme(outerRequest.getScheme());
47          _request.setMethod(outerRequest.getMethod());
48          String uri=outerRequest.getQueryString()==null?outerRequest.getRequestURI():(outerRequest.getRequestURI()+"?"+outerRequest.getQueryString());
49          _request.setUri(new HttpURI(uri));
50          _request.setPathInfo(outerRequest.getRequestURI());
51          _request.setQueryString(outerRequest.getQueryString());
52          _request.setProtocol(outerRequest.getProtocol());
53          
54          // Set the headers
55          HttpFields fields = getRequestFields();
56          for (Enumeration<String> e=outerRequest.getHeaderNames();e.hasMoreElements();)
57          {
58              String header=e.nextElement();
59              String value=outerRequest.getHeader(header);
60              fields.add(header,value);
61          }
62          
63          // Let outer parse the cookies
64          _request.setCookies(outerRequest.getCookies());
65          
66          // copy request attributes
67          for (Enumeration<String> e=outerRequest.getAttributeNames();e.hasMoreElements();)
68          {
69              String attr=e.nextElement();
70              _request.setAttribute(attr,outerRequest.getAttribute(attr));
71          }
72          
73          // customize the request
74          connector.customize(endp,_request);
75          
76          // System.err.println(_request.getMethod()+" "+_request.getUri()+" "+_request.getProtocol());
77          // System.err.println(fields.toString());
78      }
79  
80      void service() throws IOException, ServletException
81      {
82          setCurrentConnection(this);
83          try
84          {
85              getServer().handle(this);
86              completeResponse();
87              _generator.flushBuffer();
88              _endp.flush();
89          }
90          finally
91          {
92              setCurrentConnection(null);
93          }
94      }
95  
96      /* (non-Javadoc)
97       * @see org.eclipse.jetty.server.HttpConnection#getInputStream()
98       */
99      @Override
100     public ServletInputStream getInputStream() throws IOException
101     {
102         return ((NestedEndPoint)_endp).getServletInputStream();
103     }
104 
105     /* (non-Javadoc)
106      * @see org.eclipse.jetty.server.HttpConnection#handle()
107      */
108     @Override
109     public Connection handle() throws IOException
110     {
111         throw new IllegalStateException();
112     }
113 
114 }