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