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