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