View Javadoc

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