001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.chain.web.jakarta.servlet;
018
019import java.io.BufferedReader;
020import java.io.IOException;
021import java.security.Principal;
022import java.util.Arrays;
023import java.util.Collection;
024import java.util.Enumeration;
025import java.util.HashMap;
026import java.util.Locale;
027import java.util.Map;
028
029import org.apache.commons.chain.web.MockEnumeration;
030import org.apache.commons.chain.web.MockPrincipal;
031
032import jakarta.servlet.AsyncContext;
033import jakarta.servlet.DispatcherType;
034import jakarta.servlet.RequestDispatcher;
035import jakarta.servlet.ServletContext;
036import jakarta.servlet.ServletException;
037import jakarta.servlet.ServletInputStream;
038import jakarta.servlet.ServletRequest;
039import jakarta.servlet.ServletResponse;
040import jakarta.servlet.http.Cookie;
041import jakarta.servlet.http.HttpServletRequest;
042import jakarta.servlet.http.HttpServletResponse;
043import jakarta.servlet.http.HttpSession;
044import jakarta.servlet.http.HttpUpgradeHandler;
045import jakarta.servlet.http.Part;
046
047/**
048 * Mock Object for {@code HttpServletRequest} (Version 2.3)
049 */
050public class MockHttpServletRequest implements HttpServletRequest {
051    public MockHttpServletRequest() {
052    }
053
054    public MockHttpServletRequest(HttpSession session) {
055        setHttpSession(session);
056    }
057
058    public MockHttpServletRequest(String contextPath, String servletPath,
059                                  String pathInfo, String queryString) {
060        setPathElements(contextPath, servletPath, pathInfo, queryString);
061    }
062
063    public MockHttpServletRequest(String contextPath, String servletPath,
064                                  String pathInfo, String queryString,
065                                  HttpSession session) {
066        setPathElements(contextPath, servletPath, pathInfo, queryString);
067        setHttpSession(session);
068    }
069
070    protected Map<String, Object> attributes = new HashMap<>();
071    protected String contextPath = null;
072    protected Map<String, String[]> headers = new HashMap<>();
073    protected Cookie[] cookies = new Cookie[0];
074    protected Locale locale = null;
075    protected Map<String, String[]> parameters = new HashMap<>();
076    protected String pathInfo = null;
077    protected Principal principal = null;
078    protected String queryString = null;
079    protected String servletPath = null;
080    protected HttpSession session = null;
081
082    // --------------------------------------------------------- Public Methods
083
084    public void addHeader(String name, String value) {
085        String values[] = headers.get(name);
086        if (values == null) {
087            String results[] = new String[] { value };
088            headers.put(name, results);
089            return;
090        }
091        String results[] = new String[values.length + 1];
092        System.arraycopy(values, 0, results, 0, values.length);
093        results[values.length] = value;
094        headers.put(name, results);
095    }
096
097    public void addParameter(String name, String value) {
098        String values[] = parameters.get(name);
099        if (values == null) {
100            String results[] = new String[] { value };
101            parameters.put(name, results);
102            return;
103        }
104        String results[] = new String[values.length + 1];
105        System.arraycopy(values, 0, results, 0, values.length);
106        results[values.length] = value;
107        parameters.put(name, results);
108    }
109
110    public void addCookie(String name, String value) {
111        addCookie(new Cookie(name, value));
112    }
113
114    public void addCookie(Cookie cookie) {
115        Cookie[] newValues = new Cookie[cookies.length + 1];
116        System.arraycopy(cookies, 0, newValues, 0, cookies.length);
117        cookies = newValues;
118        cookies[cookies.length - 1] = cookie;
119    }
120
121    public void setHttpSession(HttpSession session) {
122        this.session = session;
123    }
124
125    public void setLocale(Locale locale) {
126        this.locale = locale;
127    }
128
129    public void setPathElements(String contextPath, String servletPath,
130                                String pathInfo, String queryString) {
131
132        this.contextPath = contextPath;
133        this.servletPath = servletPath;
134        this.pathInfo = pathInfo;
135        this.queryString = queryString;
136    }
137
138    public void setUserPrincipal(Principal principal) {
139        this.principal = principal;
140    }
141
142    // --------------------------------------------- HttpServletRequest Methods
143
144    @Override
145    public String getAuthType() {
146        throw new UnsupportedOperationException();
147    }
148
149    @Override
150    public String getContextPath() {
151        return contextPath;
152    }
153
154    @Override
155    public Cookie[] getCookies() {
156        return cookies;
157    }
158
159    @Override
160    public long getDateHeader(String name) {
161        throw new UnsupportedOperationException();
162    }
163
164    @Override
165    public String getHeader(String name) {
166        String values[] = headers.get(name);
167        if (values != null) {
168            return values[0];
169        } else {
170            return null;
171        }
172    }
173
174    @Override
175    public Enumeration<String> getHeaderNames() {
176        return new MockEnumeration<>(headers.keySet().iterator());
177    }
178
179    @Override
180    public Enumeration<String> getHeaders(String name) {
181        String headers[] = this.headers.get(name);
182        if (headers == null) {
183            headers = new String[0];
184        }
185        return new MockEnumeration<>(Arrays.asList(headers).iterator());
186    }
187
188    @Override
189    public int getIntHeader(String name) {
190        throw new UnsupportedOperationException();
191    }
192
193    @Override
194    public String getMethod() {
195        throw new UnsupportedOperationException();
196    }
197
198    @Override
199    public String getPathInfo() {
200        return pathInfo;
201    }
202
203    @Override
204    public String getPathTranslated() {
205        throw new UnsupportedOperationException();
206    }
207
208    @Override
209    public String getQueryString() {
210        return queryString;
211    }
212
213    @Override
214    public String getRemoteUser() {
215        if (principal != null) {
216            return principal.getName();
217        } else {
218            return null;
219        }
220    }
221
222    @Override
223    public String getRequestedSessionId() {
224        throw new UnsupportedOperationException();
225    }
226
227    @Override
228    public String getRequestURI() {
229        StringBuffer sb = new StringBuffer();
230        if (contextPath != null) {
231            sb.append(contextPath);
232        }
233        if (servletPath != null) {
234            sb.append(servletPath);
235        }
236        if (pathInfo != null) {
237            sb.append(pathInfo);
238        }
239        if (sb.length() > 0) {
240            return sb.toString();
241        }
242        throw new UnsupportedOperationException();
243    }
244
245    @Override
246    public StringBuffer getRequestURL() {
247        throw new UnsupportedOperationException();
248    }
249
250    @Override
251    public String getServletPath() {
252        return servletPath;
253    }
254
255    @Override
256    public HttpSession getSession() {
257        return getSession(true);
258    }
259
260    @Override
261    public HttpSession getSession(boolean create) {
262        if (create && (session == null)) {
263            throw new UnsupportedOperationException();
264        }
265        return session;
266    }
267
268    @Override
269    public Principal getUserPrincipal() {
270        return principal;
271    }
272
273    @Override
274    public boolean isRequestedSessionIdFromCookie() {
275        throw new UnsupportedOperationException();
276    }
277
278    @Deprecated
279    @Override
280    public boolean isRequestedSessionIdFromUrl() {
281        throw new UnsupportedOperationException();
282    }
283
284    @Override
285    public boolean isRequestedSessionIdFromURL() {
286        throw new UnsupportedOperationException();
287    }
288
289    @Override
290    public boolean isRequestedSessionIdValid() {
291        throw new UnsupportedOperationException();
292    }
293
294    @Override
295    public boolean isUserInRole(String role) {
296        if (principal instanceof MockPrincipal) {
297            return ((MockPrincipal) principal).isUserInRole(role);
298        } else {
299            return false;
300        }
301    }
302
303    // ------------------------------------------------- ServletRequest Methods
304
305    @Override
306    public Object getAttribute(String name) {
307        return attributes.get(name);
308    }
309
310    @Override
311    public Enumeration<String> getAttributeNames() {
312        return new MockEnumeration<>(attributes.keySet().iterator());
313    }
314
315    @Override
316    public String getCharacterEncoding() {
317        throw new UnsupportedOperationException();
318    }
319
320    @Override
321    public int getContentLength() {
322        throw new UnsupportedOperationException();
323    }
324
325    @Override
326    public String getContentType() {
327        throw new UnsupportedOperationException();
328    }
329
330    @Override
331    public ServletInputStream getInputStream() {
332        throw new UnsupportedOperationException();
333    }
334
335    @Override
336    public Locale getLocale() {
337        return locale;
338    }
339
340    @Override
341    public Enumeration<Locale> getLocales() {
342        throw new UnsupportedOperationException();
343    }
344
345    public String getLocalAddr() {
346        throw new UnsupportedOperationException();
347    }
348
349    public String getLocalName() {
350        throw new UnsupportedOperationException();
351    }
352
353    public int getLocalPort() {
354        throw new UnsupportedOperationException();
355    }
356
357    @Override
358    public String getParameter(String name) {
359        String values[] = parameters.get(name);
360        if (values != null) {
361            return values[0];
362        } else {
363            return null;
364        }
365    }
366
367    @Override
368    public Map<String, String[]> getParameterMap() {
369        return parameters;
370    }
371
372    @Override
373    public Enumeration<String> getParameterNames() {
374        return new MockEnumeration<>(parameters.keySet().iterator());
375    }
376
377    @Override
378    public String[] getParameterValues(String name) {
379        return parameters.get(name);
380    }
381
382    @Override
383    public String getProtocol() {
384        throw new UnsupportedOperationException();
385    }
386
387    @Override
388    public BufferedReader getReader() {
389        throw new UnsupportedOperationException();
390    }
391
392    @Deprecated
393    @Override
394    public String getRealPath(String path) {
395        throw new UnsupportedOperationException();
396    }
397
398    @Override
399    public String getRemoteAddr() {
400        throw new UnsupportedOperationException();
401    }
402
403    @Override
404    public String getRemoteHost() {
405        throw new UnsupportedOperationException();
406    }
407
408    public int getRemotePort() {
409        throw new UnsupportedOperationException();
410    }
411
412    @Override
413    public RequestDispatcher getRequestDispatcher(String path) {
414        throw new UnsupportedOperationException();
415    }
416
417    @Override
418    public String getScheme() {
419        return "http";
420    }
421
422    @Override
423    public String getServerName() {
424        return "localhost";
425    }
426
427    @Override
428    public int getServerPort() {
429        return 8080;
430    }
431
432    @Override
433    public boolean isSecure() {
434        return false;
435    }
436
437    @Override
438    public void removeAttribute(String name) {
439        attributes.remove(name);
440    }
441
442    @Override
443    public void setAttribute(String name, Object value) {
444        if (value == null) {
445            attributes.remove(name);
446        } else {
447            attributes.put(name, value);
448        }
449    }
450
451    @Override
452    public void setCharacterEncoding(String name) {
453        throw new UnsupportedOperationException();
454    }
455
456    @Override
457    public long getContentLengthLong() {
458        throw new UnsupportedOperationException();
459    }
460
461    @Override
462    public ServletContext getServletContext() {
463        throw new UnsupportedOperationException();
464    }
465
466    @Override
467    public AsyncContext startAsync() throws IllegalStateException {
468        throw new UnsupportedOperationException();
469    }
470
471    @Override
472    public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse)
473            throws IllegalStateException {
474        throw new UnsupportedOperationException();
475    }
476
477    @Override
478    public boolean isAsyncStarted() {
479        throw new UnsupportedOperationException();
480    }
481
482    @Override
483    public boolean isAsyncSupported() {
484        throw new UnsupportedOperationException();
485    }
486
487    @Override
488    public AsyncContext getAsyncContext() {
489        throw new UnsupportedOperationException();
490    }
491
492    @Override
493    public DispatcherType getDispatcherType() {
494        throw new UnsupportedOperationException();
495    }
496
497// ***************************************************************************
498//    Only from JEE10
499//
500//    @Override
501//    public String getRequestId() {
502//        throw new UnsupportedOperationException();
503//    }
504//
505//    @Override
506//    public String getProtocolRequestId() {
507//        throw new UnsupportedOperationException();
508//    }
509//
510//    @Override
511//    public ServletConnection getServletConnection() {
512//        throw new UnsupportedOperationException();
513//    }
514//
515// ***************************************************************************
516
517    @Override
518    public String changeSessionId() {
519        throw new UnsupportedOperationException();
520    }
521
522    @Override
523    public boolean authenticate(HttpServletResponse response) throws IOException, ServletException {
524        throw new UnsupportedOperationException();
525    }
526
527    @Override
528    public void login(String username, String password) throws ServletException {
529        throw new UnsupportedOperationException();
530    }
531
532    @Override
533    public void logout() throws ServletException {
534        throw new UnsupportedOperationException();
535    }
536
537    @Override
538    public Collection<Part> getParts() throws IOException, ServletException {
539        throw new UnsupportedOperationException();
540    }
541
542    @Override
543    public Part getPart(String name) throws IOException, ServletException {
544        throw new UnsupportedOperationException();
545    }
546
547    @Override
548    public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException {
549        throw new UnsupportedOperationException();
550    }
551}