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.HttpScheme;
021import org.eclipse.jetty.http.HttpVersion;
022import org.eclipse.jetty.server.Request;
023import org.eclipse.jetty.server.Session;
024import org.eclipse.jetty.util.Fields;
025
026import java.io.BufferedReader;
027import java.io.IOException;
028import java.io.UnsupportedEncodingException;
029import java.security.Principal;
030import java.util.Collection;
031import java.util.Collections;
032import java.util.Enumeration;
033import java.util.HashMap;
034import java.util.List;
035import java.util.Locale;
036import java.util.Map;
037import java.util.Set;
038import java.util.stream.Collectors;
039
040import static ch.qos.logback.access.common.spi.IAccessEvent.NA;
041import static ch.qos.logback.access.jetty.HeaderUtil.buildHeaderMap;
042import static java.nio.charset.StandardCharsets.UTF_8;
043
044public class RequestWrapper implements HttpServletRequest, WrappedHttpRequest {
045
046    static final Cookie[] EMPTY_COOKIE_ARRAY = new Cookie[0];
047    static final String[] EMPTY_STRING_ARRAY = new String[0];
048
049    Request request;
050    StringBuffer requestURL;
051
052    public RequestWrapper(Request request) {
053        this.request = request;
054    }
055
056    @Override
057    public String getAuthType() {
058        return null;
059    }
060
061    @Override
062    public Cookie[] getCookies() {
063        List<HttpCookie> httpCookies = Request.getCookies(request);
064        List<Cookie> cookieList = httpCookies.stream().map(httpCookie -> new Cookie(httpCookie.getName(), httpCookie.getValue())).collect(
065                Collectors.toList());
066
067        return  cookieList.toArray(EMPTY_COOKIE_ARRAY);
068    }
069
070    @Override
071    public long getDateHeader(String name) {
072        return 0;
073    }
074
075    @Override
076    public String getHeader(String name) {
077        return null;
078    }
079
080    @Override
081    public Enumeration<String> getHeaders(String name) {
082        return null;
083    }
084
085    @Override
086    public Enumeration<String> getHeaderNames() {
087
088        return null;
089    }
090
091    @Override
092    public Map<String, String> buildRequestHeaderMap() {
093        return buildHeaderMap(request.getHeaders());
094    }
095
096    @Override
097    public int getIntHeader(String name) {
098        return 0;
099    }
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}