View Javadoc

1   package org.eclipse.jetty.http.spi;
2   
3   //========================================================================
4   //Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
5   //------------------------------------------------------------------------
6   //All rights reserved. This program and the accompanying materials
7   //are made available under the terms of the Eclipse Public License v1.0
8   //and Apache License v2.0 which accompanies this distribution.
9   //The Eclipse Public License is available at 
10  //http://www.eclipse.org/legal/epl-v10.html
11  //The Apache License v2.0 is available at
12  //http://www.opensource.org/licenses/apache2.0.php
13  //You may elect to redistribute this code under either of these licenses. 
14  //========================================================================
15  
16  import java.io.IOException;
17  import java.io.InputStream;
18  import java.io.OutputStream;
19  import java.net.InetSocketAddress;
20  import java.net.URI;
21  import java.net.URISyntaxException;
22  import java.util.Enumeration;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import com.sun.net.httpserver.Headers;
30  import com.sun.net.httpserver.HttpContext;
31  import com.sun.net.httpserver.HttpExchange;
32  import com.sun.net.httpserver.HttpPrincipal;
33  
34  /**
35   * Jetty implementation of {@link com.sun.net.httpserver.HttpExchange}
36   */
37  public class JettyHttpExchangeDelegate extends HttpExchange
38  {
39  
40      private HttpContext _httpContext;
41  
42      private HttpServletRequest _req;
43  
44      private HttpServletResponse _resp;
45  
46      private Headers _responseHeaders = new Headers();
47  
48      private int _responseCode = 0;
49  
50      private InputStream _is;
51  
52      private OutputStream _os;
53  
54      private HttpPrincipal _httpPrincipal;
55  
56      JettyHttpExchangeDelegate(HttpContext jaxWsContext, HttpServletRequest req, HttpServletResponse resp)
57      {
58          this._httpContext = jaxWsContext;
59          this._req = req;
60          this._resp = resp;
61          try
62          {
63              this._is = req.getInputStream();
64              this._os = resp.getOutputStream();
65          }
66          catch (IOException ex)
67          {
68              throw new RuntimeException(ex);
69          }
70      }
71  
72      @Override
73      public Headers getRequestHeaders()
74      {
75          Headers headers = new Headers();
76          Enumeration<?> en = _req.getHeaderNames();
77          while (en.hasMoreElements())
78          {
79              String name = (String)en.nextElement();
80              Enumeration<?> en2 = _req.getHeaders(name);
81              while (en2.hasMoreElements())
82              {
83                  String value = (String)en2.nextElement();
84                  headers.add(name,value);
85              }
86          }
87          return headers;
88      }
89  
90      @Override
91      public Headers getResponseHeaders()
92      {
93          return _responseHeaders;
94      }
95  
96      @Override
97      public URI getRequestURI()
98      {
99          try
100         {
101             String uriAsString = _req.getRequestURI();
102             if (_req.getQueryString() != null)
103             {
104                 uriAsString += "?" + _req.getQueryString();
105             }
106 
107             return new URI(uriAsString);
108         }
109         catch (URISyntaxException ex)
110         {
111             throw new RuntimeException(ex);
112         }
113     }
114 
115     @Override
116     public String getRequestMethod()
117     {
118         return _req.getMethod();
119     }
120 
121     @Override
122     public HttpContext getHttpContext()
123     {
124         return _httpContext;
125     }
126 
127     @Override
128     public void close()
129     {
130         try
131         {
132             _resp.getOutputStream().close();
133         }
134         catch (IOException ex)
135         {
136             throw new RuntimeException(ex);
137         }
138     }
139 
140     @Override
141     public InputStream getRequestBody()
142     {
143         return _is;
144     }
145 
146     @Override
147     public OutputStream getResponseBody()
148     {
149         return _os;
150     }
151 
152     @Override
153     public void sendResponseHeaders(int rCode, long responseLength) throws IOException
154     {
155         this._responseCode = rCode;
156 
157         for (Map.Entry<String, List<String>> stringListEntry : _responseHeaders.entrySet())
158         {
159             String name = stringListEntry.getKey();
160             List<String> values = stringListEntry.getValue();
161 
162             for (String value : values)
163             {
164                 _resp.setHeader(name,value);
165             }
166         }
167         if (responseLength > 0)
168         {
169             _resp.setHeader("content-length","" + responseLength);
170         }
171         _resp.setStatus(rCode);
172     }
173 
174     @Override
175     public InetSocketAddress getRemoteAddress()
176     {
177         return new InetSocketAddress(_req.getRemoteAddr(),_req.getRemotePort());
178     }
179 
180     @Override
181     public int getResponseCode()
182     {
183         return _responseCode;
184     }
185 
186     @Override
187     public InetSocketAddress getLocalAddress()
188     {
189         return new InetSocketAddress(_req.getLocalAddr(),_req.getLocalPort());
190     }
191 
192     @Override
193     public String getProtocol()
194     {
195         return _req.getProtocol();
196     }
197 
198     @Override
199     public Object getAttribute(String name)
200     {
201         return _req.getAttribute(name);
202     }
203 
204     @Override
205     public void setAttribute(String name, Object value)
206     {
207         _req.setAttribute(name,value);
208     }
209 
210     @Override
211     public void setStreams(InputStream i, OutputStream o)
212     {
213         _is = i;
214         _os = o;
215     }
216 
217     @Override
218     public HttpPrincipal getPrincipal()
219     {
220         return _httpPrincipal;
221     }
222 
223     public void setPrincipal(HttpPrincipal principal)
224     {
225         this._httpPrincipal = principal;
226     }
227 
228 }