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.Locale;
    28  import java.util.Map;
    29
    30  import org.apache.commons.fileupload2.core.AbstractFileUpload;
    31
    32  import jakarta.servlet.AsyncContext;
    33  import jakarta.servlet.DispatcherType;
    34  import jakarta.servlet.ReadListener;
    35  import jakarta.servlet.RequestDispatcher;
    36  import jakarta.servlet.ServletConnection;
    37  import jakarta.servlet.ServletContext;
    38  import jakarta.servlet.ServletException;
    39  import jakarta.servlet.ServletInputStream;
    40  import jakarta.servlet.ServletRequest;
    41  import jakarta.servlet.ServletResponse;
    42  import jakarta.servlet.http.Cookie;
    43  import jakarta.servlet.http.HttpServletRequest;
    44  import jakarta.servlet.http.HttpServletResponse;
    45  import jakarta.servlet.http.HttpSession;
    46  import jakarta.servlet.http.HttpUpgradeHandler;
    47  import jakarta.servlet.http.Part;
    48
    49  public class JakartaMockServletHttpRequest implements HttpServletRequest {
    50
    51      private static final class MyServletInputStream extends jakarta.servlet.ServletInputStream {
    52
    53          private final InputStream inputStream;
    54          private final int readLimit;
    55
    56          /**
    57           * Creates a new instance, which returns the given streams data.
    58           */
    59          public MyServletInputStream(final InputStream inputStream, final int readLimit) {
    60              this.inputStream = inputStream;
    61              this.readLimit = readLimit;
    62          }
    63
    64          @Override
    65          public boolean isFinished() {
    66              return false;
    67          }
    68
    69          @Override
    70          public boolean isReady() {
    71              return false;
    72          }
    73
    74          @Override
    75          public int read() throws IOException {
    76              return inputStream.read();
    77          }
    78
    79          @Override
    80          public int read(final byte[] b, final int off, final int len) throws IOException {
    81              if (readLimit > 0) {
    82                  return inputStream.read(b, off, Math.min(readLimit, len));
    83              }
    84              return inputStream.read(b, off, len);
    85          }
    86
    87          @Override
    88          public void setReadListener(final ReadListener readListener) {
    89              throw new IllegalStateException("Not implemented");
    90          }
    91
    92      }
    93
    94      private final InputStream requestInputStream;
    95
    96      private long requestLength;
    97
    98      private final String contentType;
    99
    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 }