View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.fileupload2.jakarta.servlet6;
    18
    19  import java.io.BufferedReader;
    20  import java.io.ByteArrayInputStream;
    21  import java.io.IOException;
    22  import java.io.InputStream;
    23  import java.io.UnsupportedEncodingException;
    24  import java.security.Principal;
    25  import java.util.Collection;
    26  import java.util.Enumeration;
    27  import java.util.HashMap;
    28  import java.util.Locale;
    29  import java.util.Map;
    30
    31  import org.apache.commons.fileupload2.core.AbstractFileUpload;
    32
    33  import jakarta.servlet.AsyncContext;
    34  import jakarta.servlet.DispatcherType;
    35  import jakarta.servlet.ReadListener;
    36  import jakarta.servlet.RequestDispatcher;
    37  import jakarta.servlet.ServletConnection;
    38  import jakarta.servlet.ServletContext;
    39  import jakarta.servlet.ServletException;
    40  import jakarta.servlet.ServletInputStream;
    41  import jakarta.servlet.ServletRequest;
    42  import jakarta.servlet.ServletResponse;
    43  import jakarta.servlet.http.Cookie;
    44  import jakarta.servlet.http.HttpServletRequest;
    45  import jakarta.servlet.http.HttpServletResponse;
    46  import jakarta.servlet.http.HttpSession;
    47  import jakarta.servlet.http.HttpUpgradeHandler;
    48  import jakarta.servlet.http.Part;
    49
    50  /**
    51   * Test fixture for {@link HttpServletRequest}.
    52   */
    53  public class JakartaMockHttpServletRequest implements HttpServletRequest {
    54
    55      private static final class MyServletInputStream extends ServletInputStream {
    56
    57          private final InputStream inputStream;
    58          private final int readLimit;
    59
    60          /**
    61           * Creates a new instance, which returns the given streams data.
    62           */
    63          MyServletInputStream(final InputStream inputStream, final int readLimit) {
    64              this.inputStream = inputStream;
    65              this.readLimit = readLimit;
    66          }
    67
    68          @Override
    69          public boolean isFinished() {
    70              return false;
    71          }
    72
    73          @Override
    74          public boolean isReady() {
    75              return false;
    76          }
    77
    78          @Override
    79          public int read() throws IOException {
    80              return inputStream.read();
    81          }
    82
    83          @Override
    84          public int read(final byte[] b, final int off, final int len) throws IOException {
    85              if (readLimit > 0) {
    86                  return inputStream.read(b, off, Math.min(readLimit, len));
    87              }
    88              return inputStream.read(b, off, len);
    89          }
    90
    91          @Override
    92          public void setReadListener(final ReadListener readListener) {
    93
    94          }
    95
    96      }
    97
    98      private final InputStream requestInputStream;
    99
    100     private final long requestLength;
    101
    102     private final String contentType;
    103
    104     private final int readLimit;
    105
    106     private final Map<String, String> headers = new HashMap<>();
    107
    108     /**
    109      * Creates a new instance with the given request data and content type.
    110      */
    111     public JakartaMockHttpServletRequest(final byte[] requestData, final String contentType) {
    112         this(new ByteArrayInputStream(requestData), requestData.length, contentType, -1);
    113     }
    114
    115     /**
    116      * Creates a new instance with the given request data and content type.
    117      */
    118     public JakartaMockHttpServletRequest(final InputStream requestInputStream, final long requestLength, final String contentType, final int readLimit) {
    119         this.requestInputStream = requestInputStream;
    120         this.requestLength = requestLength;
    121         this.contentType = contentType;
    122         this.headers.put(AbstractFileUpload.CONTENT_TYPE, contentType);
    123         this.readLimit = readLimit;
    124     }
    125
    126     @Override
    127     public boolean authenticate(final HttpServletResponse response) throws IOException, ServletException {
    128         return false;
    129     }
    130
    131     @Override
    132     public String changeSessionId() {
    133         return null;
    134     }
    135
    136     @Override
    137     public AsyncContext getAsyncContext() {
    138         return null;
    139     }
    140
    141     /**
    142      * @see ServletRequest#getAttribute(String)
    143      */
    144     @Override
    145     public Object getAttribute(final String arg0) {
    146         return null;
    147     }
    148
    149     /**
    150      * @see ServletRequest#getAttributeNames()
    151      */
    152     @Override
    153     public Enumeration<String> getAttributeNames() {
    154         return null;
    155     }
    156
    157     /**
    158      * @see HttpServletRequest#getAuthType()
    159      */
    160     @Override
    161     public String getAuthType() {
    162         return null;
    163     }
    164
    165     /**
    166      * @see ServletRequest#getCharacterEncoding()
    167      */
    168     @Override
    169     public String getCharacterEncoding() {
    170         return null;
    171     }
    172
    173     /**
    174      * @see ServletRequest#getContentLength()
    175      */
    176     @Override
    177     public int getContentLength() {
    178         return requestInputStream != null ? Math.toIntExact(requestLength) : -1;
    179     }
    180
    181     @Override
    182     public long getContentLengthLong() {
    183         return 0;
    184     }
    185
    186     /**
    187      * @see ServletRequest#getContentType()
    188      */
    189     @Override
    190     public String getContentType() {
    191         return contentType;
    192     }
    193
    194     /**
    195      * @see HttpServletRequest#getContextPath()
    196      */
    197     @Override
    198     public String getContextPath() {
    199         return null;
    200     }
    201
    202     /**
    203      * @see HttpServletRequest#getCookies()
    204      */
    205     @Override
    206     public Cookie[] getCookies() {
    207         return null;
    208     }
    209
    210     /**
    211      * @see HttpServletRequest#getDateHeader(String)
    212      */
    213     @Override
    214     public long getDateHeader(final String arg0) {
    215         return 0;
    216     }
    217
    218     @Override
    219     public DispatcherType getDispatcherType() {
    220         return null;
    221     }
    222
    223     /**
    224      * @see HttpServletRequest#getHeader(String)
    225      */
    226     @Override
    227     public String getHeader(final String headerName) {
    228         return headers.get(headerName);
    229     }
    230
    231     /**
    232      * @see HttpServletRequest#getHeaderNames()
    233      */
    234     @Override
    235     public Enumeration<String> getHeaderNames() {
    236         // todo - implement
    237         return null;
    238     }
    239
    240     /**
    241      * @see HttpServletRequest#getHeaders(String)
    242      */
    243     @Override
    244     public Enumeration<String> getHeaders(final String arg0) {
    245         // todo - implement
    246         return null;
    247     }
    248
    249     /**
    250      * @see ServletRequest#getInputStream()
    251      */
    252     @Override
    253     public ServletInputStream getInputStream() throws IOException {
    254         return new MyServletInputStream(requestInputStream, readLimit);
    255     }
    256
    257     /**
    258      * @see HttpServletRequest#getIntHeader(String)
    259      */
    260     @Override
    261     public int getIntHeader(final String arg0) {
    262         return 0;
    263     }
    264
    265     /**
    266      * @see ServletRequest#getLocalAddr()
    267      */
    268     @Override
    269     @SuppressWarnings("javadoc") // This is a Servlet 2.4 method
    270     public String getLocalAddr() {
    271         return null;
    272     }
    273
    274     /**
    275      * @see ServletRequest#getLocale()
    276      */
    277     @Override
    278     public Locale getLocale() {
    279         return null;
    280     }
    281
    282     /**
    283      * @see ServletRequest#getLocales()
    284      */
    285     @Override
    286     public Enumeration<Locale> getLocales() {
    287         return null;
    288     }
    289
    290     /**
    291      * @see ServletRequest#getLocalName()
    292      */
    293     @Override
    294     @SuppressWarnings("javadoc") // This is a Servlet 2.4 method
    295     public String getLocalName() {
    296         return null;
    297     }
    298
    299     /**
    300      * @see ServletRequest#getLocalPort()
    301      */
    302     @Override
    303     @SuppressWarnings("javadoc") // This is a Servlet 2.4 method
    304     public int getLocalPort() {
    305         return 0;
    306     }
    307
    308     /**
    309      * @see HttpServletRequest#getMethod()
    310      */
    311     @Override
    312     public String getMethod() {
    313         return null;
    314     }
    315
    316     /**
    317      * @see ServletRequest#getParameter(String)
    318      */
    319     @Override
    320     public String getParameter(final String arg0) {
    321         return null;
    322     }
    323
    324     /**
    325      * @see ServletRequest#getParameterMap()
    326      */
    327     @Override
    328     public Map<String, String[]> getParameterMap() {
    329         return null;
    330     }
    331
    332     /**
    333      * @see ServletRequest#getParameterNames()
    334      */
    335     @Override
    336     public Enumeration<String> getParameterNames() {
    337         return null;
    338     }
    339
    340     /**
    341      * @see ServletRequest#getParameterValues(String)
    342      */
    343     @Override
    344     public String[] getParameterValues(final String arg0) {
    345         return null;
    346     }
    347
    348     @Override
    349     public Part getPart(final String name) throws IOException, ServletException {
    350         return null;
    351     }
    352
    353     @Override
    354     public Collection<Part> getParts() throws IOException, ServletException {
    355         return null;
    356     }
    357
    358     /**
    359      * @see HttpServletRequest#getPathInfo()
    360      */
    361     @Override
    362     public String getPathInfo() {
    363         return null;
    364     }
    365
    366     /**
    367      * @see HttpServletRequest#getPathTranslated()
    368      */
    369     @Override
    370     public String getPathTranslated() {
    371         return null;
    372     }
    373
    374     /**
    375      * @see ServletRequest#getProtocol()
    376      */
    377     @Override
    378     public String getProtocol() {
    379         return null;
    380     }
    381
    382     @Override
    383     public String getProtocolRequestId() {
    384         return null;
    385     }
    386
    387     /**
    388      * @see HttpServletRequest#getQueryString()
    389      */
    390     @Override
    391     public String getQueryString() {
    392         return null;
    393     }
    394
    395     /**
    396      * @see ServletRequest#getReader()
    397      */
    398     @Override
    399     public BufferedReader getReader() throws IOException {
    400         return null;
    401     }
    402
    403     /**
    404      * @see ServletRequest#getRemoteAddr()
    405      */
    406     @Override
    407     public String getRemoteAddr() {
    408         return null;
    409     }
    410
    411     /**
    412      * @see ServletRequest#getRemoteHost()
    413      */
    414     @Override
    415     public String getRemoteHost() {
    416         return null;
    417     }
    418
    419     /**
    420      * @see ServletRequest#getRemotePort()
    421      */
    422     @Override
    423     @SuppressWarnings("javadoc") // This is a Servlet 2.4 method
    424     public int getRemotePort() {
    425         return 0;
    426     }
    427
    428     /**
    429      * @see HttpServletRequest#getRemoteUser()
    430      */
    431     @Override
    432     public String getRemoteUser() {
    433         return null;
    434     }
    435
    436     /**
    437      * @see ServletRequest#getRequestDispatcher(String)
    438      */
    439     @Override
    440     public RequestDispatcher getRequestDispatcher(final String arg0) {
    441         return null;
    442     }
    443
    444     /**
    445      * @see HttpServletRequest#getRequestedSessionId()
    446      */
    447     @Override
    448     public String getRequestedSessionId() {
    449         return null;
    450     }
    451
    452     @Override
    453     public String getRequestId() {
    454         return null;
    455     }
    456
    457     /**
    458      * @see HttpServletRequest#getRequestURI()
    459      */
    460     @Override
    461     public String getRequestURI() {
    462         return null;
    463     }
    464
    465     /**
    466      * @see HttpServletRequest#getRequestURL()
    467      */
    468     @Override
    469     public StringBuffer getRequestURL() {
    470         return null;
    471     }
    472
    473     /**
    474      * @see ServletRequest#getScheme()
    475      */
    476     @Override
    477     public String getScheme() {
    478         return null;
    479     }
    480
    481     /**
    482      * @see ServletRequest#getServerName()
    483      */
    484     @Override
    485     public String getServerName() {
    486         return null;
    487     }
    488
    489     /**
    490      * @see ServletRequest#getServerPort()
    491      */
    492     @Override
    493     public int getServerPort() {
    494         return 0;
    495     }
    496
    497     @Override
    498     public ServletConnection getServletConnection() {
    499         return null;
    500     }
    501
    502     @Override
    503     public ServletContext getServletContext() {
    504         return null;
    505     }
    506
    507     /**
    508      * @see HttpServletRequest#getServletPath()
    509      */
    510     @Override
    511     public String getServletPath() {
    512         return null;
    513     }
    514
    515     /**
    516      * @see HttpServletRequest#getSession()
    517      */
    518     @Override
    519     public HttpSession getSession() {
    520         return null;
    521     }
    522
    523     /**
    524      * @see HttpServletRequest#getSession(boolean)
    525      */
    526     @Override
    527     public HttpSession getSession(final boolean arg0) {
    528         return null;
    529     }
    530
    531     /**
    532      * @see HttpServletRequest#getUserPrincipal()
    533      */
    534     @Override
    535     public Principal getUserPrincipal() {
    536         return null;
    537     }
    538
    539     @Override
    540     public boolean isAsyncStarted() {
    541         return false;
    542     }
    543
    544     @Override
    545     public boolean isAsyncSupported() {
    546         return false;
    547     }
    548
    549     /**
    550      * @see HttpServletRequest#isRequestedSessionIdFromCookie()
    551      */
    552     @Override
    553     public boolean isRequestedSessionIdFromCookie() {
    554         return false;
    555     }
    556
    557     /**
    558      * @see HttpServletRequest#isRequestedSessionIdFromURL()
    559      */
    560     @Override
    561     public boolean isRequestedSessionIdFromURL() {
    562         return false;
    563     }
    564
    565     /**
    566      * @see HttpServletRequest#isRequestedSessionIdValid()
    567      */
    568     @Override
    569     public boolean isRequestedSessionIdValid() {
    570         return false;
    571     }
    572
    573     /**
    574      * @see ServletRequest#isSecure()
    575      */
    576     @Override
    577     public boolean isSecure() {
    578         return false;
    579     }
    580
    581     /**
    582      * @see HttpServletRequest#isUserInRole(String)
    583      */
    584     @Override
    585     public boolean isUserInRole(final String arg0) {
    586         return false;
    587     }
    588
    589     @Override
    590     public void login(final String username, final String password) throws ServletException {
    591
    592     }
    593
    594     @Override
    595     public void logout() throws ServletException {
    596
    597     }
    598
    599     /**
    600      * @see ServletRequest#removeAttribute(String)
    601      */
    602     @Override
    603     public void removeAttribute(final String arg0) {
    604     }
    605
    606     /**
    607      * @see ServletRequest#setAttribute(String, Object)
    608      */
    609     @Override
    610     public void setAttribute(final String arg0, final Object arg1) {
    611     }
    612
    613     /**
    614      * @see ServletRequest#setCharacterEncoding(String)
    615      */
    616     @Override
    617     public void setCharacterEncoding(final String arg0) throws UnsupportedEncodingException {
    618     }
    619
    620     @Override
    621     public AsyncContext startAsync() throws IllegalStateException {
    622         return null;
    623     }
    624
    625     @Override
    626     public AsyncContext startAsync(final ServletRequest servletRequest, final ServletResponse servletResponse) throws IllegalStateException {
    627         return null;
    628     }
    629
    630     @Override
    631     public <T extends HttpUpgradeHandler> T upgrade(final Class<T> handlerClass) throws IOException, ServletException {
    632         return null;
    633     }
    634
    635 }