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