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