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