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.fileupload;
    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.Enumeration;
    26  import java.util.HashMap;
    27  import java.util.Locale;
    28  import java.util.Map;
    29
    30  import javax.servlet.RequestDispatcher;
    31  import javax.servlet.ServletInputStream;
    32  import javax.servlet.http.Cookie;
    33  import javax.servlet.http.HttpServletRequest;
    34  import javax.servlet.http.HttpSession;
    35
    36  public class MockHttpServletRequest implements HttpServletRequest {
    37
    38      private static class MyServletInputStream
    39          extends javax.servlet.ServletInputStream {
    40
    41          private final InputStream in;
    42          private final int readLimit;
    43
    44          /**
    45           * Creates a new instance, which returns the given
    46           * streams data.
    47           */
    48          MyServletInputStream(final InputStream in, final int readLimit) {
    49              this.in = in;
    50              this.readLimit = readLimit;
    51          }
    52
    53          @Override
    54          public int read() throws IOException {
    55              return in.read();
    56          }
    57
    58          @Override
    59          public int read(final byte[] b, final int off, final int len) throws IOException {
    60              if (readLimit > 0) {
    61                  return in.read(b, off, Math.min(readLimit, len));
    62              }
    63              return in.read(b, off, len);
    64          }
    65
    66      }
    67
    68      private final InputStream requestData;
    69
    70      private long length;
    71
    72      private final String strContentType;
    73
    74      private int readLimit = -1;
    75
    76      private final Map<String, String> headers = new HashMap<>();
    77
    78      /**
    79       * Creates a new instance with the given request data
    80       * and content type.
    81       */
    82      public MockHttpServletRequest(
    83              final byte[] requestData,
    84              final String strContentType) {
    85          this(new ByteArrayInputStream(requestData),
    86                  requestData.length, strContentType);
    87      }
    88
    89      /**
    90       * Creates a new instance with the given request data
    91       * and content type.
    92       */
    93      public MockHttpServletRequest(
    94              final InputStream requestData,
    95              final long length,
    96              final String strContentType) {
    97          this.requestData = requestData;
    98          this.length = length;
    99          this.strContentType = strContentType;
    100         this.headers.put(FileUploadBase.CONTENT_TYPE, strContentType);
    101     }
    102
    103     /**
    104      * @see javax.servlet.ServletRequest#getAttribute(String)
    105      */
    106     @Override
    107     public Object getAttribute(final String arg0) {
    108         return null;
    109     }
    110
    111     /**
    112      * @see javax.servlet.ServletRequest#getAttributeNames()
    113      */
    114     @Override
    115     public Enumeration<String> getAttributeNames() {
    116         return null;
    117     }
    118
    119     /**
    120      * @see javax.servlet.http.HttpServletRequest#getAuthType()
    121      */
    122     @Override
    123     public String getAuthType() {
    124         return null;
    125     }
    126
    127     /**
    128      * @see javax.servlet.ServletRequest#getCharacterEncoding()
    129      */
    130     @Override
    131     public String getCharacterEncoding() {
    132         return null;
    133     }
    134
    135     /**
    136      * @see javax.servlet.ServletRequest#getContentLength()
    137      */
    138     @Override
    139     public int getContentLength() {
    140         int iLength = 0;
    141
    142         if (null == requestData) {
    143             iLength = -1;
    144         } else {
    145             if (length > Integer.MAX_VALUE) {
    146                 throw new RuntimeException("Value '" + length + "' is too large to be converted to int");
    147             }
    148             iLength = (int) length;
    149         }
    150         return iLength;
    151     }
    152
    153     /**
    154      * @see javax.servlet.ServletRequest#getContentType()
    155      */
    156     @Override
    157     public String getContentType() {
    158         return strContentType;
    159     }
    160
    161     /**
    162      * @see javax.servlet.http.HttpServletRequest#getContextPath()
    163      */
    164     @Override
    165     public String getContextPath() {
    166         return null;
    167     }
    168
    169     /**
    170      * @see javax.servlet.http.HttpServletRequest#getCookies()
    171      */
    172     @Override
    173     public Cookie[] getCookies() {
    174         return null;
    175     }
    176
    177     /**
    178      * @see javax.servlet.http.HttpServletRequest#getDateHeader(String)
    179      */
    180     @Override
    181     public long getDateHeader(final String arg0) {
    182         return 0;
    183     }
    184
    185     /**
    186      * @see javax.servlet.http.HttpServletRequest#getHeader(String)
    187      */
    188     @Override
    189     public String getHeader(final String headerName) {
    190         return headers.get(headerName);
    191     }
    192
    193     /**
    194      * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
    195      */
    196     @Override
    197     public Enumeration<String> getHeaderNames() {
    198         // todo - implement
    199         return null;
    200     }
    201
    202     /**
    203      * @see javax.servlet.http.HttpServletRequest#getHeaders(String)
    204      */
    205     @Override
    206     public Enumeration<String> getHeaders(final String arg0) {
    207         // todo - implement
    208         return null;
    209     }
    210
    211     /**
    212      * @see javax.servlet.ServletRequest#getInputStream()
    213      */
    214     @Override
    215     public ServletInputStream getInputStream() throws IOException {
    216         return new MyServletInputStream(requestData, readLimit);
    217     }
    218
    219     /**
    220      * @see javax.servlet.http.HttpServletRequest#getIntHeader(String)
    221      */
    222     @Override
    223     public int getIntHeader(final String arg0) {
    224         return 0;
    225     }
    226
    227     /**
    228      * @see javax.servlet.ServletRequest#getLocalAddr()
    229      */
    230     @Override
    231     public String getLocalAddr() {
    232         return null;
    233     }
    234
    235     /**
    236      * @see javax.servlet.ServletRequest#getLocale()
    237      */
    238     @Override
    239     public Locale getLocale() {
    240         return null;
    241     }
    242
    243     /**
    244      * @see javax.servlet.ServletRequest#getLocales()
    245      */
    246     @Override
    247     public Enumeration<Locale> getLocales() {
    248         return null;
    249     }
    250
    251     /**
    252      * @see javax.servlet.ServletRequest#getLocalName()
    253      */
    254     @Override
    255     public String getLocalName() {
    256         return null;
    257     }
    258
    259     /**
    260      * @see javax.servlet.ServletRequest#getLocalPort()
    261      */
    262     @Override
    263     public int getLocalPort() {
    264         return 0;
    265     }
    266
    267     /**
    268      * @see javax.servlet.http.HttpServletRequest#getMethod()
    269      */
    270     @Override
    271     public String getMethod() {
    272         return null;
    273     }
    274
    275     /**
    276      * @see javax.servlet.ServletRequest#getParameter(String)
    277      */
    278     @Override
    279     public String getParameter(final String arg0) {
    280         return null;
    281     }
    282
    283     /**
    284      * @see javax.servlet.ServletRequest#getParameterMap()
    285      */
    286     @Override
    287     public Map<String, String[]> getParameterMap() {
    288         return null;
    289     }
    290
    291     /**
    292      * @see javax.servlet.ServletRequest#getParameterNames()
    293      */
    294     @Override
    295     public Enumeration<String> getParameterNames() {
    296         return null;
    297     }
    298
    299     /**
    300      * @see javax.servlet.ServletRequest#getParameterValues(String)
    301      */
    302     @Override
    303     public String[] getParameterValues(final String arg0) {
    304         return null;
    305     }
    306
    307     /**
    308      * @see javax.servlet.http.HttpServletRequest#getPathInfo()
    309      */
    310     @Override
    311     public String getPathInfo() {
    312         return null;
    313     }
    314
    315     /**
    316      * @see javax.servlet.http.HttpServletRequest#getPathTranslated()
    317      */
    318     @Override
    319     public String getPathTranslated() {
    320         return null;
    321     }
    322
    323     /**
    324      * @see javax.servlet.ServletRequest#getProtocol()
    325      */
    326     @Override
    327     public String getProtocol() {
    328         return null;
    329     }
    330
    331     /**
    332      * @see javax.servlet.http.HttpServletRequest#getQueryString()
    333      */
    334     @Override
    335     public String getQueryString() {
    336         return null;
    337     }
    338
    339     /**
    340      * @see javax.servlet.ServletRequest#getReader()
    341      */
    342     @Override
    343     public BufferedReader getReader() throws IOException {
    344         return null;
    345     }
    346
    347     /**
    348      * @see javax.servlet.ServletRequest#getRealPath(String)
    349      * @deprecated
    350      */
    351     @Override
    352     @Deprecated
    353     public String getRealPath(final String arg0) {
    354         return null;
    355     }
    356
    357     /**
    358      * @see javax.servlet.ServletRequest#getRemoteAddr()
    359      */
    360     @Override
    361     public String getRemoteAddr() {
    362         return null;
    363     }
    364
    365     /**
    366      * @see javax.servlet.ServletRequest#getRemoteHost()
    367      */
    368     @Override
    369     public String getRemoteHost() {
    370         return null;
    371     }
    372
    373     /**
    374      * @see javax.servlet.ServletRequest#getRemotePort()
    375      */
    376     @Override
    377     public int getRemotePort() {
    378         return 0;
    379     }
    380
    381     /**
    382      * @see javax.servlet.http.HttpServletRequest#getRemoteUser()
    383      */
    384     @Override
    385     public String getRemoteUser() {
    386         return null;
    387     }
    388
    389     /**
    390      * @see javax.servlet.ServletRequest#getRequestDispatcher(String)
    391      */
    392     @Override
    393     public RequestDispatcher getRequestDispatcher(final String arg0) {
    394         return null;
    395     }
    396
    397     /**
    398      * @see javax.servlet.http.HttpServletRequest#getRequestedSessionId()
    399      */
    400     @Override
    401     public String getRequestedSessionId() {
    402         return null;
    403     }
    404
    405     /**
    406      * @see javax.servlet.http.HttpServletRequest#getRequestURI()
    407      */
    408     @Override
    409     public String getRequestURI() {
    410         return null;
    411     }
    412
    413     /**
    414      * @see javax.servlet.http.HttpServletRequest#getRequestURL()
    415      */
    416     @Override
    417     public StringBuffer getRequestURL() {
    418         return null;
    419     }
    420
    421     /**
    422      * @see javax.servlet.ServletRequest#getScheme()
    423      */
    424     @Override
    425     public String getScheme() {
    426         return null;
    427     }
    428
    429     /**
    430      * @see javax.servlet.ServletRequest#getServerName()
    431      */
    432     @Override
    433     public String getServerName() {
    434         return null;
    435     }
    436
    437     /**
    438      * @see javax.servlet.ServletRequest#getServerPort()
    439      */
    440     @Override
    441     public int getServerPort() {
    442         return 0;
    443     }
    444
    445     /**
    446      * @see javax.servlet.http.HttpServletRequest#getServletPath()
    447      */
    448     @Override
    449     public String getServletPath() {
    450         return null;
    451     }
    452
    453     /**
    454      * @see javax.servlet.http.HttpServletRequest#getSession()
    455      */
    456     @Override
    457     public HttpSession getSession() {
    458         return null;
    459     }
    460
    461     /**
    462      * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
    463      */
    464     @Override
    465     public HttpSession getSession(final boolean arg0) {
    466         return null;
    467     }
    468
    469     /**
    470      * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
    471      */
    472     @Override
    473     public Principal getUserPrincipal() {
    474         return null;
    475     }
    476
    477     /**
    478      * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromCookie()
    479      */
    480     @Override
    481     public boolean isRequestedSessionIdFromCookie() {
    482         return false;
    483     }
    484
    485     /**
    486      * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromUrl()
    487      * @deprecated
    488      */
    489     @Override
    490     @Deprecated
    491     public boolean isRequestedSessionIdFromUrl() {
    492         return false;
    493     }
    494
    495     /**
    496      * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromURL()
    497      */
    498     @Override
    499     public boolean isRequestedSessionIdFromURL() {
    500         return false;
    501     }
    502
    503     /**
    504      * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdValid()
    505      */
    506     @Override
    507     public boolean isRequestedSessionIdValid() {
    508         return false;
    509     }
    510
    511     /**
    512      * @see javax.servlet.ServletRequest#isSecure()
    513      */
    514     @Override
    515     public boolean isSecure() {
    516         return false;
    517     }
    518
    519     /**
    520      * @see javax.servlet.http.HttpServletRequest#isUserInRole(String)
    521      */
    522     @Override
    523     public boolean isUserInRole(final String arg0) {
    524         return false;
    525     }
    526
    527     /**
    528      * @see javax.servlet.ServletRequest#removeAttribute(String)
    529      */
    530     @Override
    531     public void removeAttribute(final String arg0) {
    532     }
    533
    534     /**
    535      * @see javax.servlet.ServletRequest#setAttribute(String, Object)
    536      */
    537     @Override
    538     public void setAttribute(final String arg0, final Object arg1) {
    539     }
    540
    541     /**
    542      * @see javax.servlet.ServletRequest#setCharacterEncoding(String)
    543      */
    544     @Override
    545     public void setCharacterEncoding(final String arg0)
    546         throws UnsupportedEncodingException {
    547     }
    548
    549     /**
    550      * For testing attack scenarios in SizesTest.
    551      */
    552     public void setContentLength(final long length) {
    553         this.length = length;
    554     }
    555
    556     /**
    557      * Sets the read limit. This can be used to limit the number of bytes to read ahead.
    558      *
    559      * @param readLimit the read limit to use
    560      */
    561     public void setReadLimit(final int readLimit) {
    562         this.readLimit = readLimit;
    563     }
    564
    565 }