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 JettyHttpExchange 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  
57      public JettyHttpExchange(HttpContext jaxWsContext , HttpServletRequest req,
58              HttpServletResponse resp)
59      {
60          this._httpContext = jaxWsContext;
61          this._req = req;
62          this._resp = resp;
63          try
64          {
65              this._is = req.getInputStream();
66              this._os = resp.getOutputStream();
67          }
68          catch (IOException ex)
69          {
70              throw new RuntimeException(ex);
71          }
72      }
73  
74      @Override
75      public Headers getRequestHeaders()
76      {
77          Headers headers = new Headers();
78          Enumeration<?> en = _req.getHeaderNames();
79          while (en.hasMoreElements())
80          {
81              String name = (String) en.nextElement();
82              Enumeration<?> en2 = _req.getHeaders(name);
83              while (en2.hasMoreElements())
84              {
85                  String value = (String) en2.nextElement();
86                  headers.add(name, value);
87              }
88          }
89          return headers;
90      }
91  
92      @Override
93      public Headers getResponseHeaders()
94      {
95          return _responseHeaders;
96      }
97  
98      @Override
99      public URI getRequestURI()
100     {
101         try
102         {
103         	String uriAsString = _req.getRequestURI();
104         	if (_req.getQueryString() != null)
105         		uriAsString += "?" + _req.getQueryString();
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)
154             throws IOException
155     {
156         this._responseCode = rCode;
157 
158         for (Map.Entry<String, List<String>> stringListEntry : _responseHeaders.entrySet())
159         {
160             String name = stringListEntry.getKey();
161             List<String> values = stringListEntry.getValue();
162 
163             for (String value : values)
164             {
165                 _resp.setHeader(name, value);
166             }
167         }
168         if (responseLength > 0)
169             _resp.setHeader("content-length", "" + responseLength);
170         _resp.setStatus(rCode);
171     }
172 
173     @Override
174     public InetSocketAddress getRemoteAddress()
175     {
176         return new InetSocketAddress(_req.getRemoteAddr(), _req.getRemotePort());
177     }
178 
179     @Override
180     public int getResponseCode()
181     {
182         return _responseCode;
183     }
184 
185     @Override
186     public InetSocketAddress getLocalAddress()
187     {
188         return new InetSocketAddress(_req.getLocalAddr(), _req.getLocalPort());
189     }
190 
191     @Override
192     public String getProtocol()
193     {
194         return _req.getProtocol();
195     }
196 
197     @Override
198     public Object getAttribute(String name)
199     {
200         return _req.getAttribute(name);
201     }
202 
203     @Override
204     public void setAttribute(String name, Object value)
205     {
206         _req.setAttribute(name, value);
207     }
208 
209     @Override
210     public void setStreams(InputStream i, OutputStream o)
211     {
212         _is = i;
213         _os = o;
214     }
215 
216     @Override
217     public HttpPrincipal getPrincipal()
218     {
219     	return _httpPrincipal;
220     }
221     
222     public void setPrincipal(HttpPrincipal principal)
223     {
224     	this._httpPrincipal = principal;
225     }
226 
227 }