1   package ch.qos.logback.access.jetty;
2   
3   import ch.qos.logback.access.common.spi.WrappedHttpRequest;
4   import jakarta.servlet.AsyncContext;
5   import jakarta.servlet.DispatcherType;
6   import jakarta.servlet.RequestDispatcher;
7   import jakarta.servlet.ServletConnection;
8   import jakarta.servlet.ServletContext;
9   import jakarta.servlet.ServletException;
10  import jakarta.servlet.ServletInputStream;
11  import jakarta.servlet.ServletRequest;
12  import jakarta.servlet.ServletResponse;
13  import jakarta.servlet.http.Cookie;
14  import jakarta.servlet.http.HttpServletRequest;
15  import jakarta.servlet.http.HttpServletResponse;
16  import jakarta.servlet.http.HttpSession;
17  import jakarta.servlet.http.HttpUpgradeHandler;
18  import jakarta.servlet.http.Part;
19  import org.eclipse.jetty.http.HttpCookie;
20  import org.eclipse.jetty.http.HttpField;
21  import org.eclipse.jetty.http.HttpScheme;
22  import org.eclipse.jetty.http.HttpURI;
23  import org.eclipse.jetty.http.HttpVersion;
24  import org.eclipse.jetty.server.Request;
25  import org.eclipse.jetty.server.Session;
26  import org.eclipse.jetty.util.Fields;
27  
28  import java.io.BufferedReader;
29  import java.io.IOException;
30  import java.io.UnsupportedEncodingException;
31  import java.security.Principal;
32  import java.util.Collection;
33  import java.util.Collections;
34  import java.util.Enumeration;
35  import java.util.HashMap;
36  import java.util.List;
37  import java.util.Locale;
38  import java.util.Map;
39  import java.util.Set;
40  import java.util.TreeMap;
41  import java.util.stream.Collectors;
42  
43  import static ch.qos.logback.access.common.spi.IAccessEvent.NA;
44  import static java.nio.charset.StandardCharsets.UTF_8;
45  
46  public class RequestWrapper implements HttpServletRequest, WrappedHttpRequest {
47  
48      static final Cookie[] EMPTY_COOKIE_ARRAY = new Cookie[0];
49      static final String[] EMPTY_STRING_ARRAY = new String[0];
50  
51      Request request;
52      StringBuffer requestURL;
53  
54      public RequestWrapper(Request request) {
55          this.request = request;
56      }
57  
58      @Override
59      public String getAuthType() {
60          return null;
61      }
62  
63      @Override
64      public Cookie[] getCookies() {
65          List<HttpCookie> httpCookies = Request.getCookies(request);
66          List<Cookie> cookieList = httpCookies.stream().map(httpCookie -> new Cookie(httpCookie.getName(), httpCookie.getValue())).collect(
67                  Collectors.toList());
68  
69          return  cookieList.toArray(EMPTY_COOKIE_ARRAY);
70      }
71  
72      @Override
73      public long getDateHeader(String name) {
74          return 0;
75      }
76  
77      @Override
78      public String getHeader(String name) {
79          return null;
80      }
81  
82      @Override
83      public Enumeration<String> getHeaders(String name) {
84          return null;
85      }
86  
87      @Override
88      public Enumeration<String> getHeaderNames() {
89  
90          return null;
91      }
92  
93      @Override
94      public Map<String, String> buildRequestHeaderMap() {
95          Map<String, String> requestHeaderMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
96          for (HttpField f : request.getHeaders()) {
97              requestHeaderMap.put(f.getName(), f.getValue());
98          }
99          return requestHeaderMap;
100     }
101 
102     @Override
103     public int getIntHeader(String name) {
104         return 0;
105     }
106 
107     @Override
108     public String getMethod() {
109         return request.getMethod();
110     }
111 
112     @Override
113     public String getPathInfo() {
114         return null;
115     }
116 
117     @Override
118     public String getPathTranslated() {
119         return null;
120     }
121 
122     @Override
123     public String getContextPath() {
124         return null;
125     }
126 
127     @Override
128     public String getQueryString() {
129         return request.getHttpURI().getQuery();
130     }
131 
132     @Override
133     public String getRemoteUser() {
134         return null;
135     }
136 
137     @Override
138     public boolean isUserInRole(String role) {
139         return false;
140     }
141 
142     @Override
143     public Principal getUserPrincipal() {
144         return null;
145     }
146 
147     @Override
148     public String getRequestedSessionId() {
149         return null;
150     }
151 
152     @Override
153     public String getRequestURI() {
154         return request.getHttpURI().getPath();
155     }
156 
157     @Override
158     public StringBuffer getRequestURL() {
159         if (requestURL == null) {
160             String result = request.getHttpURI().asString();
161             requestURL = new StringBuffer(result);
162         }
163         return requestURL;
164     }
165 
166     @Override
167     public String getServletPath() {
168         return null;
169     }
170 
171     @Override
172     public String getSessionID() {
173         Session session = request.getSession(false);
174         if (session == null) {
175             return NA;
176         } else {
177             return session.getId();
178         }
179     }
180 
181     @Override
182     public HttpSession getSession(boolean create) {
183         throw new UnsupportedOperationException();
184     }
185 
186     @Override
187     public HttpSession getSession() {
188         throw new UnsupportedOperationException();
189     }
190 
191     @Override
192     public String changeSessionId() {
193         throw new UnsupportedOperationException();
194     }
195 
196     @Override
197     public boolean isRequestedSessionIdValid() {
198         throw new UnsupportedOperationException();
199     }
200 
201     @Override
202     public boolean isRequestedSessionIdFromCookie() {
203         return false;
204     }
205 
206     @Override
207     public boolean isRequestedSessionIdFromURL() {
208         throw new UnsupportedOperationException();
209     }
210 
211     @Override
212     public boolean authenticate(HttpServletResponse response) throws IOException, ServletException {
213         return false;
214     }
215 
216     @Override
217     public void login(String username, String password) throws ServletException {
218 
219     }
220 
221     @Override
222     public void logout() throws ServletException {
223 
224     }
225 
226     @Override
227     public Collection<Part> getParts() throws IOException, ServletException {
228         return null;
229     }
230 
231     @Override
232     public Part getPart(String name) throws IOException, ServletException {
233         return null;
234     }
235 
236     @Override
237     public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException {
238         return null;
239     }
240 
241     @Override
242     public Object getAttribute(String name) {
243         return request.getAttribute(name);
244     }
245 
246     @Override
247     public Enumeration<String> getAttributeNames() {
248         Set<String> attributeNamesSet = request.getAttributeNameSet();
249         return Collections.enumeration(attributeNamesSet);
250     }
251 
252     @Override
253     public String getCharacterEncoding() {
254         return null;
255     }
256 
257     @Override
258     public void setCharacterEncoding(String env) throws UnsupportedEncodingException {
259 
260     }
261 
262     @Override
263     public int getContentLength() {
264         return 0;
265     }
266 
267     @Override
268     public long getContentLengthLong() {
269         return 0;
270     }
271 
272     @Override
273     public String getContentType() {
274         return null;
275     }
276 
277     @Override
278     public ServletInputStream getInputStream() throws IOException {
279         return null;
280     }
281 
282     @Override
283     public Map<String, String[]> buildRequestParameterMap() {
284         Map<String, String[]> results = new HashMap<>();
285         Fields allParameters = Request.extractQueryParameters(request, UTF_8);
286         for (Fields.Field field : allParameters) {
287            results.put(field.getName(), field.getValues().toArray(EMPTY_STRING_ARRAY));
288         }
289         return results;
290     }
291 
292     @Override
293     public String getParameter(String name) {
294         throw new UnsupportedOperationException();
295     }
296 
297     @Override
298     public Enumeration<String> getParameterNames() {
299         throw new UnsupportedOperationException();
300     }
301 
302     @Override
303     public String[] getParameterValues(String name) {
304         throw new UnsupportedOperationException();
305     }
306 
307     @Override
308     public Map<String, String[]> getParameterMap() {
309         throw new UnsupportedOperationException();
310     }
311 
312     @Override
313     public String getProtocol() {
314         return request.getConnectionMetaData().getProtocol();
315     }
316 
317     @Override
318     public String getScheme() {
319         return request.getHttpURI().getScheme();
320     }
321 
322     @Override
323     public String getServerName() {
324         return Request.getServerName(request);
325     }
326 
327     @Override
328     public int getServerPort() {
329         return Request.getServerPort(request);
330     }
331 
332     @Override
333     public BufferedReader getReader() throws IOException {
334         return null;
335     }
336 
337     @Override
338     public String getRemoteAddr() {
339         return Request.getRemoteAddr(request);
340     }
341 
342     @Override
343     public String getRemoteHost() {
344         return Request.getRemoteAddr(request);
345     }
346 
347     @Override
348     public void setAttribute(String name, Object o) {
349 
350     }
351 
352     @Override
353     public void removeAttribute(String name) {
354 
355     }
356 
357     @Override
358     public Locale getLocale() {
359         return Request.getLocales(request).get(0);
360     }
361 
362     @Override
363     public Enumeration<Locale> getLocales() {
364         return Collections.enumeration(Request.getLocales(request));
365     }
366 
367     @Override
368     public boolean isSecure() {
369         return HttpScheme.HTTPS.is(request.getHttpURI().getScheme());
370     }
371 
372     @Override
373     public RequestDispatcher getRequestDispatcher(String path) {
374         return null;
375     }
376 
377     @Override
378     public int getRemotePort() {
379         return Request.getRemotePort(request);
380     }
381 
382     @Override
383     public String getLocalName() {
384         return null;
385     }
386 
387     @Override
388     public String getLocalAddr() {
389         return Request.getLocalAddr(request);
390     }
391 
392     @Override
393     public int getLocalPort() {
394         return Request.getLocalPort(request);
395     }
396 
397     @Override
398     public ServletContext getServletContext() {
399         return null;
400     }
401 
402     @Override
403     public AsyncContext startAsync() throws IllegalStateException {
404         return null;
405     }
406 
407     @Override
408     public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse)
409             throws IllegalStateException {
410         return null;
411     }
412 
413     @Override
414     public boolean isAsyncStarted() {
415         return false;
416     }
417 
418     @Override
419     public boolean isAsyncSupported() {
420         return false;
421     }
422 
423     @Override
424     public AsyncContext getAsyncContext() {
425         return null;
426     }
427 
428     @Override
429     public DispatcherType getDispatcherType() {
430         return null;
431     }
432 
433     @Override
434     public String getRequestId() {
435         return request.getConnectionMetaData().getId() + "#" + request.getId();
436     }
437 
438 
439     @Override
440     public String getProtocolRequestId() {
441        HttpVersion httpVersion = request.getConnectionMetaData().getHttpVersion();
442        if(httpVersion == HttpVersion.HTTP_2 || httpVersion == (HttpVersion.HTTP_3)) {
443            return request.getId();
444        } else {
445            return NA;
446        }
447     }
448 
449     @Override
450     public ServletConnection getServletConnection() {
451         return null;
452     }
453 
454 }