001package ch.qos.logback.access.jetty;
002
003import ch.qos.logback.access.common.spi.WrappedHttpRequest;
004import jakarta.servlet.AsyncContext;
005import jakarta.servlet.DispatcherType;
006import jakarta.servlet.RequestDispatcher;
007import jakarta.servlet.ServletConnection;
008import jakarta.servlet.ServletContext;
009import jakarta.servlet.ServletException;
010import jakarta.servlet.ServletInputStream;
011import jakarta.servlet.ServletRequest;
012import jakarta.servlet.ServletResponse;
013import jakarta.servlet.http.Cookie;
014import jakarta.servlet.http.HttpServletRequest;
015import jakarta.servlet.http.HttpServletResponse;
016import jakarta.servlet.http.HttpSession;
017import jakarta.servlet.http.HttpUpgradeHandler;
018import jakarta.servlet.http.Part;
019import org.eclipse.jetty.http.HttpCookie;
020import org.eclipse.jetty.http.HttpField;
021import org.eclipse.jetty.http.HttpScheme;
022import org.eclipse.jetty.http.HttpURI;
023import org.eclipse.jetty.http.HttpVersion;
024import org.eclipse.jetty.server.Request;
025import org.eclipse.jetty.server.Session;
026import org.eclipse.jetty.util.Fields;
027
028import java.io.BufferedReader;
029import java.io.IOException;
030import java.io.UnsupportedEncodingException;
031import java.security.Principal;
032import java.util.Collection;
033import java.util.Collections;
034import java.util.Enumeration;
035import java.util.HashMap;
036import java.util.List;
037import java.util.Locale;
038import java.util.Map;
039import java.util.Set;
040import java.util.TreeMap;
041import java.util.stream.Collectors;
042
043import static ch.qos.logback.access.common.spi.IAccessEvent.NA;
044import static java.nio.charset.StandardCharsets.UTF_8;
045
046public class RequestWrapper implements HttpServletRequest, WrappedHttpRequest {
047
048    static final Cookie[] EMPTY_COOKIE_ARRAY = new Cookie[0];
049    static final String[] EMPTY_STRING_ARRAY = new String[0];
050
051    Request request;
052    StringBuffer requestURL;
053
054    public RequestWrapper(Request request) {
055        this.request = request;
056    }
057
058    @Override
059    public String getAuthType() {
060        return null;
061    }
062
063    @Override
064    public Cookie[] getCookies() {
065        List<HttpCookie> httpCookies = Request.getCookies(request);
066        List<Cookie> cookieList = httpCookies.stream().map(httpCookie -> new Cookie(httpCookie.getName(), httpCookie.getValue())).collect(
067                Collectors.toList());
068
069        return  cookieList.toArray(EMPTY_COOKIE_ARRAY);
070    }
071
072    @Override
073    public long getDateHeader(String name) {
074        return 0;
075    }
076
077    @Override
078    public String getHeader(String name) {
079        return null;
080    }
081
082    @Override
083    public Enumeration<String> getHeaders(String name) {
084        return null;
085    }
086
087    @Override
088    public Enumeration<String> getHeaderNames() {
089
090        return null;
091    }
092
093    @Override
094    public Map<String, String> buildRequestHeaderMap() {
095        Map<String, String> requestHeaderMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
096        for (HttpField f : request.getHeaders()) {
097            requestHeaderMap.put(f.getName(), f.getValue());
098        }
099        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}