xref: /OpenGrok/plugins/src/test/java/opengrok/auth/plugin/util/DummyHttpServletRequestUser.java (revision 0e4c55544f8ea0a68e8bae37b0e502097e008ec1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
22  */
23 package opengrok.auth.plugin.util;
24 
25 import jakarta.servlet.AsyncContext;
26 import jakarta.servlet.DispatcherType;
27 import jakarta.servlet.RequestDispatcher;
28 import jakarta.servlet.ServletContext;
29 import jakarta.servlet.ServletInputStream;
30 import jakarta.servlet.ServletRequest;
31 import jakarta.servlet.ServletResponse;
32 import jakarta.servlet.http.Cookie;
33 import jakarta.servlet.http.HttpServletRequest;
34 import jakarta.servlet.http.HttpServletResponse;
35 import jakarta.servlet.http.HttpSession;
36 import jakarta.servlet.http.HttpUpgradeHandler;
37 import jakarta.servlet.http.Part;
38 
39 import java.io.BufferedReader;
40 import java.io.IOException;
41 import java.io.UnsupportedEncodingException;
42 import java.security.Principal;
43 import java.util.Base64;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.Enumeration;
47 import java.util.HashMap;
48 import java.util.Locale;
49 import java.util.Map;
50 
51 public class DummyHttpServletRequestUser implements HttpServletRequest {
52 
53     private final Map<String, String> headers = new HashMap<>();
54     private final Map<String, Object> attrs = new HashMap<>();
55     private final HttpSession sessions = new HttpSession() {
56 
57         private final Map<String, Object> attrs = new HashMap<>();
58 
59         @Override
60         public long getCreationTime() {
61             return 0;
62         }
63 
64         @Override
65         public String getId() {
66             return "abcd";
67         }
68 
69         @Override
70         public long getLastAccessedTime() {
71             return 0;
72         }
73 
74         @Override
75         public ServletContext getServletContext() {
76             return (ServletContext) DummyHttpServletRequestUser.this;
77         }
78 
79         @Override
80         public void setMaxInactiveInterval(int i) {
81         }
82 
83         @Override
84         public int getMaxInactiveInterval() {
85             return 3600;
86         }
87 
88         @Override
89         @SuppressWarnings("deprecation")
90         public jakarta.servlet.http.HttpSessionContext getSessionContext() {
91             throw new UnsupportedOperationException("Not supported yet.");
92         }
93 
94         @Override
95         public Object getAttribute(String string) {
96             return attrs.get(string);
97         }
98 
99         @Override
100         @SuppressWarnings("deprecation")
101         public Object getValue(String string) {
102             throw new UnsupportedOperationException("Not supported yet.");
103         }
104 
105         @Override
106         public Enumeration<String> getAttributeNames() {
107             throw new UnsupportedOperationException("Not supported yet.");
108         }
109 
110         @Override
111         @SuppressWarnings("deprecation")
112         public String[] getValueNames() {
113             throw new UnsupportedOperationException("Not supported yet.");
114         }
115 
116         @Override
117         public void setAttribute(String string, Object o) {
118             attrs.put(string, o);
119         }
120 
121         @Override
122         @SuppressWarnings("deprecation")
123         public void putValue(String string, Object o) {
124         }
125 
126         @Override
127         public void removeAttribute(String string) {
128             attrs.remove(string);
129         }
130 
131         @Override
132         @SuppressWarnings("deprecation")
133         public void removeValue(String string) {
134         }
135 
136         @Override
137         public void invalidate() {
138         }
139 
140         @Override
141         public boolean isNew() {
142             return true;
143         }
144     };
145 
146     @Override
getAuthType()147     public String getAuthType() {
148         throw new UnsupportedOperationException("Not supported yet.");
149     }
150 
151     @Override
getCookies()152     public Cookie[] getCookies() {
153         throw new UnsupportedOperationException("Not supported yet.");
154     }
155 
156     @Override
getDateHeader(String string)157     public long getDateHeader(String string) {
158         throw new UnsupportedOperationException("Not supported yet.");
159     }
160 
setHeader(String string, String value)161     public void setHeader(String string, String value) {
162         headers.put(string, value);
163     }
164 
165     @Override
getHeader(String string)166     public String getHeader(String string) {
167         return headers.get(string);
168     }
169 
170     @Override
getHeaders(String string)171     public Enumeration<String> getHeaders(String string) {
172         throw new UnsupportedOperationException("Not supported yet.");
173     }
174 
175     @Override
getHeaderNames()176     public Enumeration<String> getHeaderNames() {
177         return Collections.enumeration(headers.keySet());
178     }
179 
180     @Override
getIntHeader(String string)181     public int getIntHeader(String string) {
182         throw new UnsupportedOperationException("Not supported yet.");
183     }
184 
185     @Override
getMethod()186     public String getMethod() {
187         throw new UnsupportedOperationException("Not supported yet.");
188     }
189 
190     @Override
getPathInfo()191     public String getPathInfo() {
192         throw new UnsupportedOperationException("Not supported yet.");
193     }
194 
195     @Override
getPathTranslated()196     public String getPathTranslated() {
197         throw new UnsupportedOperationException("Not supported yet.");
198     }
199 
200     @Override
getContextPath()201     public String getContextPath() {
202         throw new UnsupportedOperationException("Not supported yet.");
203     }
204 
205     @Override
getQueryString()206     public String getQueryString() {
207         throw new UnsupportedOperationException("Not supported yet.");
208     }
209 
210     @Override
getRemoteUser()211     public String getRemoteUser() {
212         throw new UnsupportedOperationException("Not supported yet.");
213     }
214 
215     @Override
isUserInRole(String string)216     public boolean isUserInRole(String string) {
217         throw new UnsupportedOperationException("Not supported yet.");
218     }
219 
220     @Override
getUserPrincipal()221     public Principal getUserPrincipal() {
222         String authHeader = getHeader("authorization");
223         if (authHeader == null) {
224             return null;
225         }
226 
227         if (!authHeader.startsWith("Basic")) {
228             return null;
229         }
230 
231         String encodedValue = authHeader.split(" ")[1];
232         Base64.Decoder decoder = Base64.getDecoder();
233         String username = new String(decoder.decode(encodedValue)).split(":")[0];
234         return () -> username;
235     }
236 
237     @Override
getRequestedSessionId()238     public String getRequestedSessionId() {
239         throw new UnsupportedOperationException("Not supported yet.");
240     }
241 
242     @Override
getRequestURI()243     public String getRequestURI() {
244         throw new UnsupportedOperationException("Not supported yet.");
245     }
246 
247     @Override
getRequestURL()248     public StringBuffer getRequestURL() {
249         throw new UnsupportedOperationException("Not supported yet.");
250     }
251 
252     @Override
getServletPath()253     public String getServletPath() {
254         throw new UnsupportedOperationException("Not supported yet.");
255     }
256 
257     @Override
getSession(boolean bln)258     public HttpSession getSession(boolean bln) {
259         return sessions;
260     }
261 
262     @Override
getSession()263     public HttpSession getSession() {
264         return sessions;
265     }
266 
267     @Override
changeSessionId()268     public String changeSessionId() {
269         return null;
270     }
271 
272     @Override
isRequestedSessionIdValid()273     public boolean isRequestedSessionIdValid() {
274         throw new UnsupportedOperationException("Not supported yet.");
275     }
276 
277     @Override
isRequestedSessionIdFromCookie()278     public boolean isRequestedSessionIdFromCookie() {
279         throw new UnsupportedOperationException("Not supported yet.");
280     }
281 
282     @Override
isRequestedSessionIdFromURL()283     public boolean isRequestedSessionIdFromURL() {
284         throw new UnsupportedOperationException("Not supported yet.");
285     }
286 
287     @Override
288     @Deprecated
isRequestedSessionIdFromUrl()289     public boolean isRequestedSessionIdFromUrl() {
290         throw new UnsupportedOperationException("Not supported yet.");
291     }
292 
293     @Override
authenticate(HttpServletResponse httpServletResponse)294     public boolean authenticate(HttpServletResponse httpServletResponse) {
295         return false;
296     }
297 
298     @Override
login(String s, String s1)299     public void login(String s, String s1) {
300 
301     }
302 
303     @Override
logout()304     public void logout() {
305 
306     }
307 
308     @Override
getParts()309     public Collection<Part> getParts() {
310         return null;
311     }
312 
313     @Override
getPart(String s)314     public Part getPart(String s) {
315         return null;
316     }
317 
318     @Override
upgrade(Class<T> aClass)319     public <T extends HttpUpgradeHandler> T upgrade(Class<T> aClass) {
320         return null;
321     }
322 
323     @Override
getAttribute(String string)324     public Object getAttribute(String string) {
325         return attrs.get(string);
326     }
327 
328     @Override
getAttributeNames()329     public Enumeration<String> getAttributeNames() {
330         return Collections.enumeration(attrs.keySet());
331     }
332 
333     @Override
getCharacterEncoding()334     public String getCharacterEncoding() {
335         throw new UnsupportedOperationException("Not supported yet.");
336     }
337 
338     @Override
setCharacterEncoding(String string)339     public void setCharacterEncoding(String string) throws UnsupportedEncodingException {
340         throw new UnsupportedOperationException("Not supported yet.");
341     }
342 
343     @Override
getContentLength()344     public int getContentLength() {
345         throw new UnsupportedOperationException("Not supported yet.");
346     }
347 
348     @Override
getContentLengthLong()349     public long getContentLengthLong() {
350         return 0;
351     }
352 
353     @Override
getContentType()354     public String getContentType() {
355         throw new UnsupportedOperationException("Not supported yet.");
356     }
357 
358     @Override
getInputStream()359     public ServletInputStream getInputStream() throws IOException {
360         throw new UnsupportedOperationException("Not supported yet.");
361     }
362 
363     @Override
getParameter(String string)364     public String getParameter(String string) {
365         throw new UnsupportedOperationException("Not supported yet.");
366     }
367 
368     @Override
getParameterNames()369     public Enumeration<String> getParameterNames() {
370         throw new UnsupportedOperationException("Not supported yet.");
371     }
372 
373     @Override
getParameterValues(String string)374     public String[] getParameterValues(String string) {
375         throw new UnsupportedOperationException("Not supported yet.");
376     }
377 
378     @Override
getParameterMap()379     public Map<String, String[]> getParameterMap() {
380         throw new UnsupportedOperationException("Not supported yet.");
381     }
382 
383     @Override
getProtocol()384     public String getProtocol() {
385         throw new UnsupportedOperationException("Not supported yet.");
386     }
387 
388     @Override
getScheme()389     public String getScheme() {
390         throw new UnsupportedOperationException("Not supported yet.");
391     }
392 
393     @Override
getServerName()394     public String getServerName() {
395         throw new UnsupportedOperationException("Not supported yet.");
396     }
397 
398     @Override
getServerPort()399     public int getServerPort() {
400         throw new UnsupportedOperationException("Not supported yet.");
401     }
402 
403     @Override
getReader()404     public BufferedReader getReader() throws IOException {
405         throw new UnsupportedOperationException("Not supported yet.");
406     }
407 
408     @Override
getRemoteAddr()409     public String getRemoteAddr() {
410         throw new UnsupportedOperationException("Not supported yet.");
411     }
412 
413     @Override
getRemoteHost()414     public String getRemoteHost() {
415         throw new UnsupportedOperationException("Not supported yet.");
416     }
417 
418     @Override
setAttribute(String name, Object o)419     public void setAttribute(String name, Object o) {
420         attrs.put(name, o);
421     }
422 
423     @Override
removeAttribute(String name)424     public void removeAttribute(String name) {
425         attrs.remove(name);
426     }
427 
428     @Override
getLocale()429     public Locale getLocale() {
430         throw new UnsupportedOperationException("Not supported yet.");
431     }
432 
433     @Override
getLocales()434     public Enumeration<Locale> getLocales() {
435         throw new UnsupportedOperationException("Not supported yet.");
436     }
437 
438     @Override
isSecure()439     public boolean isSecure() {
440         throw new UnsupportedOperationException("Not supported yet.");
441     }
442 
443     @Override
getRequestDispatcher(String string)444     public RequestDispatcher getRequestDispatcher(String string) {
445         throw new UnsupportedOperationException("Not supported yet.");
446     }
447 
448     @Override
449     @Deprecated
getRealPath(String string)450     public String getRealPath(String string) {
451         throw new UnsupportedOperationException("Not supported yet.");
452     }
453 
454     @Override
getRemotePort()455     public int getRemotePort() {
456         throw new UnsupportedOperationException("Not supported yet.");
457     }
458 
459     @Override
getLocalName()460     public String getLocalName() {
461         throw new UnsupportedOperationException("Not supported yet.");
462     }
463 
464     @Override
getLocalAddr()465     public String getLocalAddr() {
466         throw new UnsupportedOperationException("Not supported yet.");
467     }
468 
469     @Override
getLocalPort()470     public int getLocalPort() {
471         throw new UnsupportedOperationException("Not supported yet.");
472     }
473 
474     @Override
getServletContext()475     public ServletContext getServletContext() {
476         return null;
477     }
478 
479     @Override
startAsync()480     public AsyncContext startAsync() throws IllegalStateException {
481         return null;
482     }
483 
484     @Override
startAsync(ServletRequest servletRequest, ServletResponse servletResponse)485     public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException {
486         return null;
487     }
488 
489     @Override
isAsyncStarted()490     public boolean isAsyncStarted() {
491         return false;
492     }
493 
494     @Override
isAsyncSupported()495     public boolean isAsyncSupported() {
496         return false;
497     }
498 
499     @Override
getAsyncContext()500     public AsyncContext getAsyncContext() {
501         return null;
502     }
503 
504     @Override
getDispatcherType()505     public DispatcherType getDispatcherType() {
506         return null;
507     }
508 }
509