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