    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.fileupload2.jakarta.servlet5;
    018
    019import java.io.BufferedReader;
    020import java.io.ByteArrayInputStream;
    021import java.io.IOException;
    022import java.io.InputStream;
    023import java.io.UnsupportedEncodingException;
    024import java.security.Principal;
    025import java.util.Collection;
    026import java.util.Enumeration;
    027import java.util.HashMap;
    028import java.util.Locale;
    029import java.util.Map;
    030
    031import org.apache.commons.fileupload2.core.AbstractFileUpload;
    032
    033import jakarta.servlet.AsyncContext;
    034import jakarta.servlet.DispatcherType;
    035import jakarta.servlet.ReadListener;
    036import jakarta.servlet.RequestDispatcher;
    037import jakarta.servlet.ServletContext;
    038import jakarta.servlet.ServletException;
    039import jakarta.servlet.ServletInputStream;
    040import jakarta.servlet.ServletRequest;
    041import jakarta.servlet.ServletResponse;
    042import jakarta.servlet.http.Cookie;
    043import jakarta.servlet.http.HttpServletRequest;
    044import jakarta.servlet.http.HttpServletResponse;
    045import jakarta.servlet.http.HttpSession;
    046import jakarta.servlet.http.HttpUpgradeHandler;
    047import jakarta.servlet.http.Part;
    048
    049/**
    050 * Test fixture for {@link HttpServletRequest}.
    051 */
    052public class JakartaMockHttpServletRequest implements HttpServletRequest {
    053
    054    private static final class MyServletInputStream extends ServletInputStream {
    055
    056        private final InputStream inputStream;
    057        private final int readLimit;
    058
    059        /**
    060         * Creates a new instance, which returns the given streams data.
    061         */
    062        MyServletInputStream(final InputStream inputStream, final int readLimit) {
    063            this.inputStream = inputStream;
    064            this.readLimit = readLimit;
    065        }
    066
    067        @Override
    068        public boolean isFinished() {
    069            return false;
    070        }
    071
    072        @Override
    073        public boolean isReady() {
    074            return false;
    075        }
    076
    077        @Override
    078        public int read() throws IOException {
    079            return inputStream.read();
    080        }
    081
    082        @Override
    083        public int read(final byte[] b, final int off, final int len) throws IOException {
    084            if (readLimit > 0) {
    085                return inputStream.read(b, off, Math.min(readLimit, len));
    086            }
    087            return inputStream.read(b, off, len);
    088        }
    089
    090        @Override
    091        public void setReadListener(final ReadListener readListener) {
    092
    093        }
    094
    095    }
    096
    097    private final InputStream requestInputStream;
    098
    099    private final long requestLength;
    100
    101    private final String contentType;
    102
    103    private final int readLimit;
    104
    105    private final Map<String, String> headers = new HashMap<>();
    106
    107    /**
    108     * Creates a new instance with the given request data and content type.
    109     */
    110    public JakartaMockHttpServletRequest(final byte[] requestData, final String contentType) {
    111        this(new ByteArrayInputStream(requestData), requestData.length, contentType, -1);
    112    }
    113
    114    /**
    115     * Creates a new instance with the given request data and content type.
    116     */
    117    public JakartaMockHttpServletRequest(final InputStream requestInputStream, final long requestLength, final String contentType, final int readLimit) {
    118        this.requestInputStream = requestInputStream;
    119        this.requestLength = requestLength;
    120        this.contentType = contentType;
    121        this.headers.put(AbstractFileUpload.CONTENT_TYPE, contentType);
    122        this.readLimit = readLimit;
    123    }
    124
    125    @Override
    126    public boolean authenticate(final HttpServletResponse response) throws IOException, ServletException {
    127        return false;
    128    }
    129
    130    @Override
    131    public String changeSessionId() {
    132        return null;
    133    }
    134
    135    @Override
    136    public AsyncContext getAsyncContext() {
    137        return null;
    138    }
    139
    140    /**
    141     * @see ServletRequest#getAttribute(String)
    142     */
    143    @Override
    144    public Object getAttribute(final String arg0) {
    145        return null;
    146    }
    147
    148    /**
    149     * @see ServletRequest#getAttributeNames()
    150     */
    151    @Override
    152    public Enumeration<String> getAttributeNames() {
    153        return null;
    154    }
    155
    156    /**
    157     * @see HttpServletRequest#getAuthType()
    158     */
    159    @Override
    160    public String getAuthType() {
    161        return null;
    162    }
    163
    164    /**
    165     * @see ServletRequest#getCharacterEncoding()
    166     */
    167    @Override
    168    public String getCharacterEncoding() {
    169        return null;
    170    }
    171
    172    /**
    173     * @see ServletRequest#getContentLength()
    174     */
    175    @Override
    176    public int getContentLength() {
    177        return requestInputStream != null ? Math.toIntExact(requestLength) : -1;
    178    }
    179
    180    @Override
    181    public long getContentLengthLong() {
    182        return 0;
    183    }
    184
    185    /**
    186     * @see ServletRequest#getContentType()
    187     */
    188    @Override
    189    public String getContentType() {
    190        return contentType;
    191    }
    192
    193    /**
    194     * @see HttpServletRequest#getContextPath()
    195     */
    196    @Override
    197    public String getContextPath() {
    198        return null;
    199    }
    200
    201    /**
    202     * @see HttpServletRequest#getCookies()
    203     */
    204    @Override
    205    public Cookie[] getCookies() {
    206        return null;
    207    }
    208
    209    /**
    210     * @see HttpServletRequest#getDateHeader(String)
    211     */
    212    @Override
    213    public long getDateHeader(final String arg0) {
    214        return 0;
    215    }
    216
    217    @Override
    218    public DispatcherType getDispatcherType() {
    219        return null;
    220    }
    221
    222    /**
    223     * @see HttpServletRequest#getHeader(String)
    224     */
    225    @Override
    226    public String getHeader(final String headerName) {
    227        return headers.get(headerName);
    228    }
    229
    230    /**
    231     * @see HttpServletRequest#getHeaderNames()
    232     */
    233    @Override
    234    public Enumeration<String> getHeaderNames() {
    235        // todo - implement
    236        return null;
    237    }
    238
    239    /**
    240     * @see HttpServletRequest#getHeaders(String)
    241     */
    242    @Override
    243    public Enumeration<String> getHeaders(final String arg0) {
    244        // todo - implement
    245        return null;
    246    }
    247
    248    /**
    249     * @see ServletRequest#getInputStream()
    250     */
    251    @Override
    252    public ServletInputStream getInputStream() throws IOException {
    253        return new MyServletInputStream(requestInputStream, readLimit);
    254    }
    255
    256    /**
    257     * @see HttpServletRequest#getIntHeader(String)
    258     */
    259    @Override
    260    public int getIntHeader(final String arg0) {
    261        return 0;
    262    }
    263
    264    /**
    265     * @see ServletRequest#getLocalAddr()
    266     */
    267    @Override
    268    @SuppressWarnings("javadoc") // This is a Servlet 2.4 method
    269    public String getLocalAddr() {
    270        return null;
    271    }
    272
    273    /**
    274     * @see ServletRequest#getLocale()
    275     */
    276    @Override
    277    public Locale getLocale() {
    278        return null;
    279    }
    280
    281    /**
    282     * @see ServletRequest#getLocales()
    283     */
    284    @Override
    285    public Enumeration<Locale> getLocales() {
    286        return null;
    287    }
    288
    289    /**
    290     * @see ServletRequest#getLocalName()
    291     */
    292    @Override
    293    @SuppressWarnings("javadoc") // This is a Servlet 2.4 method
    294    public String getLocalName() {
    295        return null;
    296    }
    297
    298    /**
    299     * @see ServletRequest#getLocalPort()
    300     */
    301    @Override
    302    @SuppressWarnings("javadoc") // This is a Servlet 2.4 method
    303    public int getLocalPort() {
    304        return 0;
    305    }
    306
    307    /**
    308     * @see HttpServletRequest#getMethod()
    309     */
    310    @Override
    311    public String getMethod() {
    312        return null;
    313    }
    314
    315    /**
    316     * @see ServletRequest#getParameter(String)
    317     */
    318    @Override
    319    public String getParameter(final String arg0) {
    320        return null;
    321    }
    322
    323    /**
    324     * @see ServletRequest#getParameterMap()
    325     */
    326    @Override
    327    public Map<String, String[]> getParameterMap() {
    328        return null;
    329    }
    330
    331    /**
    332     * @see ServletRequest#getParameterNames()
    333     */
    334    @Override
    335    public Enumeration<String> getParameterNames() {
    336        return null;
    337    }
    338
    339    /**
    340     * @see ServletRequest#getParameterValues(String)
    341     */
    342    @Override
    343    public String[] getParameterValues(final String arg0) {
    344        return null;
    345    }
    346
    347    @Override
    348    public Part getPart(final String name) throws IOException, ServletException {
    349        return null;
    350    }
    351
    352    @Override
    353    public Collection<Part> getParts() throws IOException, ServletException {
    354        return null;
    355    }
    356
    357    /**
    358     * @see HttpServletRequest#getPathInfo()
    359     */
    360    @Override
    361    public String getPathInfo() {
    362        return null;
    363    }
    364
    365    /**
    366     * @see HttpServletRequest#getPathTranslated()
    367     */
    368    @Override
    369    public String getPathTranslated() {
    370        return null;
    371    }
    372
    373    /**
    374     * @see ServletRequest#getProtocol()
    375     */
    376    @Override
    377    public String getProtocol() {
    378        return null;
    379    }
    380
    381    /**
    382     * @see HttpServletRequest#getQueryString()
    383     */
    384    @Override
    385    public String getQueryString() {
    386        return null;
    387    }
    388
    389    /**
    390     * @see ServletRequest#getReader()
    391     */
    392    @Override
    393    public BufferedReader getReader() throws IOException {
    394        return null;
    395    }
    396
    397    /**
    398     * @see ServletRequest#getRealPath(String)
    399     * @deprecated
    400     */
    401    @Override
    402    @Deprecated
    403    public String getRealPath(final String arg0) {
    404        return null;
    405    }
    406
    407    /**
    408     * @see ServletRequest#getRemoteAddr()
    409     */
    410    @Override
    411    public String getRemoteAddr() {
    412        return null;
    413    }
    414
    415    /**
    416     * @see ServletRequest#getRemoteHost()
    417     */
    418    @Override
    419    public String getRemoteHost() {
    420        return null;
    421    }
    422
    423    /**
    424     * @see ServletRequest#getRemotePort()
    425     */
    426    @Override
    427    @SuppressWarnings("javadoc") // This is a Servlet 2.4 method
    428    public int getRemotePort() {
    429        return 0;
    430    }
    431
    432    /**
    433     * @see HttpServletRequest#getRemoteUser()
    434     */
    435    @Override
    436    public String getRemoteUser() {
    437        return null;
    438    }
    439
    440    /**
    441     * @see ServletRequest#getRequestDispatcher(String)
    442     */
    443    @Override
    444    public RequestDispatcher getRequestDispatcher(final String arg0) {
    445        return null;
    446    }
    447
    448    /**
    449     * @see HttpServletRequest#getRequestedSessionId()
    450     */
    451    @Override
    452    public String getRequestedSessionId() {
    453        return null;
    454    }
    455
    456    /**
    457     * @see HttpServletRequest#getRequestURI()
    458     */
    459    @Override
    460    public String getRequestURI() {
    461        return null;
    462    }
    463
    464    /**
    465     * @see HttpServletRequest#getRequestURL()
    466     */
    467    @Override
    468    public StringBuffer getRequestURL() {
    469        return null;
    470    }
    471
    472    /**
    473     * @see ServletRequest#getScheme()
    474     */
    475    @Override
    476    public String getScheme() {
    477        return null;
    478    }
    479
    480    /**
    481     * @see ServletRequest#getServerName()
    482     */
    483    @Override
    484    public String getServerName() {
    485        return null;
    486    }
    487
    488    /**
    489     * @see ServletRequest#getServerPort()
    490     */
    491    @Override
    492    public int getServerPort() {
    493        return 0;
    494    }
    495
    496    @Override
    497    public ServletContext getServletContext() {
    498        return null;
    499    }
    500
    501    /**
    502     * @see HttpServletRequest#getServletPath()
    503     */
    504    @Override
    505    public String getServletPath() {
    506        return null;
    507    }
    508
    509    /**
    510     * @see HttpServletRequest#getSession()
    511     */
    512    @Override
    513    public HttpSession getSession() {
    514        return null;
    515    }
    516
    517    /**
    518     * @see HttpServletRequest#getSession(boolean)
    519     */
    520    @Override
    521    public HttpSession getSession(final boolean arg0) {
    522        return null;
    523    }
    524
    525    /**
    526     * @see HttpServletRequest#getUserPrincipal()
    527     */
    528    @Override
    529    public Principal getUserPrincipal() {
    530        return null;
    531    }
    532
    533    @Override
    534    public boolean isAsyncStarted() {
    535        return false;
    536    }
    537
    538    @Override
    539    public boolean isAsyncSupported() {
    540        return false;
    541    }
    542
    543    /**
    544     * @see HttpServletRequest#isRequestedSessionIdFromCookie()
    545     */
    546    @Override
    547    public boolean isRequestedSessionIdFromCookie() {
    548        return false;
    549    }
    550
    551    /**
    552     * @see HttpServletRequest#isRequestedSessionIdFromUrl()
    553     * @deprecated
    554     */
    555    @Override
    556    @Deprecated
    557    public boolean isRequestedSessionIdFromUrl() {
    558        return false;
    559    }
    560
    561    /**
    562     * @see HttpServletRequest#isRequestedSessionIdFromURL()
    563     */
    564    @Override
    565    public boolean isRequestedSessionIdFromURL() {
    566        return false;
    567    }
    568
    569    /**
    570     * @see HttpServletRequest#isRequestedSessionIdValid()
    571     */
    572    @Override
    573    public boolean isRequestedSessionIdValid() {
    574        return false;
    575    }
    576
    577    /**
    578     * @see ServletRequest#isSecure()
    579     */
    580    @Override
    581    public boolean isSecure() {
    582        return false;
    583    }
    584
    585    /**
    586     * @see HttpServletRequest#isUserInRole(String)
    587     */
    588    @Override
    589    public boolean isUserInRole(final String arg0) {
    590        return false;
    591    }
    592
    593    @Override
    594    public void login(final String username, final String password) throws ServletException {
    595
    596    }
    597
    598    @Override
    599    public void logout() throws ServletException {
    600
    601    }
    602
    603    /**
    604     * @see ServletRequest#removeAttribute(String)
    605     */
    606    @Override
    607    public void removeAttribute(final String arg0) {
    608    }
    609
    610    /**
    611     * @see ServletRequest#setAttribute(String, Object)
    612     */
    613    @Override
    614    public void setAttribute(final String arg0, final Object arg1) {
    615    }
    616
    617    /**
    618     * @see ServletRequest#setCharacterEncoding(String)
    619     */
    620    @Override
    621    public void setCharacterEncoding(final String arg0) throws UnsupportedEncodingException {
    622    }
    623
    624    @Override
    625    public AsyncContext startAsync() throws IllegalStateException {
    626        return null;
    627    }
    628
    629    @Override
    630    public AsyncContext startAsync(final ServletRequest servletRequest, final ServletResponse servletResponse) throws IllegalStateException {
    631        return null;
    632    }
    633
    634    @Override
    635    public <T extends HttpUpgradeHandler> T upgrade(final Class<T> handlerClass) throws IOException, ServletException {
    636        return null;
    637    }
    638
    639}