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