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