001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.fileupload2.jakarta.servlet5; 018 019import java.io.BufferedReader; 020import java.io.ByteArrayInputStream; 021import java.io.IOException; 022import java.io.InputStream; 023import java.io.UnsupportedEncodingException; 024import java.security.Principal; 025import java.util.Collection; 026import java.util.Enumeration; 027import java.util.Locale; 028import java.util.Map; 029 030import org.apache.commons.fileupload2.core.AbstractFileUpload; 031 032import jakarta.servlet.AsyncContext; 033import jakarta.servlet.DispatcherType; 034import jakarta.servlet.ReadListener; 035import jakarta.servlet.RequestDispatcher; 036import jakarta.servlet.ServletContext; 037import jakarta.servlet.ServletException; 038import jakarta.servlet.ServletInputStream; 039import jakarta.servlet.ServletRequest; 040import jakarta.servlet.ServletResponse; 041import jakarta.servlet.http.Cookie; 042import jakarta.servlet.http.HttpServletRequest; 043import jakarta.servlet.http.HttpServletResponse; 044import jakarta.servlet.http.HttpSession; 045import jakarta.servlet.http.HttpUpgradeHandler; 046import jakarta.servlet.http.Part; 047 048public class JakartaMockServletHttpRequest implements HttpServletRequest { 049 050 private static final class MyServletInputStream extends jakarta.servlet.ServletInputStream { 051 052 private final InputStream inputStream; 053 private final int readLimit; 054 055 /** 056 * Creates a new instance, which returns the given streams data. 057 */ 058 public MyServletInputStream(final InputStream inputStream, final int readLimit) { 059 this.inputStream = inputStream; 060 this.readLimit = readLimit; 061 } 062 063 @Override 064 public boolean isFinished() { 065 return false; 066 } 067 068 @Override 069 public boolean isReady() { 070 return false; 071 } 072 073 @Override 074 public int read() throws IOException { 075 return inputStream.read(); 076 } 077 078 @Override 079 public int read(final byte[] b, final int off, final int len) throws IOException { 080 if (readLimit > 0) { 081 return inputStream.read(b, off, Math.min(readLimit, len)); 082 } 083 return inputStream.read(b, off, len); 084 } 085 086 @Override 087 public void setReadListener(final ReadListener readListener) { 088 throw new IllegalStateException("Not implemented"); 089 } 090 091 } 092 093 private final InputStream requestInputStream; 094 095 private long requestLength; 096 097 private final String contentType; 098 099 private int readLimit = -1; 100 101 private final Map<String, String> headers = new java.util.HashMap<>(); 102 103 /** 104 * Creates a new instance with the given request data and content type. 105 */ 106 public JakartaMockServletHttpRequest(final byte[] requestData, final String strContentType) { 107 this(new ByteArrayInputStream(requestData), requestData.length, strContentType); 108 } 109 110 /** 111 * Creates a new instance with the given request data and content type. 112 */ 113 public JakartaMockServletHttpRequest(final InputStream requestData, final long requestLength, final String strContentType) { 114 this.requestInputStream = requestData; 115 this.requestLength = requestLength; 116 this.contentType = strContentType; 117 this.headers.put(AbstractFileUpload.CONTENT_TYPE, strContentType); 118 } 119 120 @Override 121 public boolean authenticate(final HttpServletResponse response) throws IOException, ServletException { 122 return false; 123 } 124 125 @Override 126 public String changeSessionId() { 127 return null; 128 } 129 130 @Override 131 public AsyncContext getAsyncContext() { 132 return null; 133 } 134 135 /** 136 * @see ServletRequest#getAttribute(String) 137 */ 138 @Override 139 public Object getAttribute(final String arg0) { 140 return null; 141 } 142 143 /** 144 * @see ServletRequest#getAttributeNames() 145 */ 146 @Override 147 public Enumeration<String> getAttributeNames() { 148 return null; 149 } 150 151 /** 152 * @see HttpServletRequest#getAuthType() 153 */ 154 @Override 155 public String getAuthType() { 156 return null; 157 } 158 159 /** 160 * @see ServletRequest#getCharacterEncoding() 161 */ 162 @Override 163 public String getCharacterEncoding() { 164 return null; 165 } 166 167 /** 168 * @see ServletRequest#getContentLength() 169 */ 170 @Override 171 public int getContentLength() { 172 return requestInputStream != null ? Math.toIntExact(requestLength) : -1; 173 } 174 175 @Override 176 public long getContentLengthLong() { 177 return getContentLength(); 178 } 179 180 /** 181 * @see ServletRequest#getContentType() 182 */ 183 @Override 184 public String getContentType() { 185 return contentType; 186 } 187 188 /** 189 * @see HttpServletRequest#getContextPath() 190 */ 191 @Override 192 public String getContextPath() { 193 return null; 194 } 195 196 /** 197 * @see HttpServletRequest#getCookies() 198 */ 199 @Override 200 public Cookie[] getCookies() { 201 return null; 202 } 203 204 /** 205 * @see HttpServletRequest#getDateHeader(String) 206 */ 207 @Override 208 public long getDateHeader(final String arg0) { 209 return 0; 210 } 211 212 @Override 213 public DispatcherType getDispatcherType() { 214 return null; 215 } 216 217 /** 218 * @see HttpServletRequest#getHeader(String) 219 */ 220 @Override 221 public String getHeader(final String headerName) { 222 return headers.get(headerName); 223 } 224 225 /** 226 * @see HttpServletRequest#getHeaderNames() 227 */ 228 @Override 229 public Enumeration<String> getHeaderNames() { 230 // todo - implement 231 return null; 232 } 233 234 /** 235 * @see HttpServletRequest#getHeaders(String) 236 */ 237 @Override 238 public Enumeration<String> getHeaders(final String arg0) { 239 // todo - implement 240 return null; 241 } 242 243 /** 244 * @see ServletRequest#getInputStream() 245 */ 246 @Override 247 public ServletInputStream getInputStream() throws IOException { 248 return new MyServletInputStream(requestInputStream, readLimit); 249 } 250 251 /** 252 * @see HttpServletRequest#getIntHeader(String) 253 */ 254 @Override 255 public int getIntHeader(final String arg0) { 256 return 0; 257 } 258 259 /** 260 * @see ServletRequest#getLocalAddr() 261 */ 262 @Override 263 @SuppressWarnings("javadoc") // This is a Servlet 2.4 method 264 public String getLocalAddr() { 265 return null; 266 } 267 268 /** 269 * @see ServletRequest#getLocale() 270 */ 271 @Override 272 public Locale getLocale() { 273 return null; 274 } 275 276 /** 277 * @see ServletRequest#getLocales() 278 */ 279 @Override 280 public Enumeration<Locale> getLocales() { 281 return null; 282 } 283 284 /** 285 * @see ServletRequest#getLocalName() 286 */ 287 @Override 288 @SuppressWarnings("javadoc") // This is a Servlet 2.4 method 289 public String getLocalName() { 290 return null; 291 } 292 293 /** 294 * @see ServletRequest#getLocalPort() 295 */ 296 @Override 297 @SuppressWarnings("javadoc") // This is a Servlet 2.4 method 298 public int getLocalPort() { 299 return 0; 300 } 301 302 /** 303 * @see HttpServletRequest#getMethod() 304 */ 305 @Override 306 public String getMethod() { 307 return null; 308 } 309 310 /** 311 * @see ServletRequest#getParameter(String) 312 */ 313 @Override 314 public String getParameter(final String arg0) { 315 return null; 316 } 317 318 /** 319 * @see ServletRequest#getParameterMap() 320 */ 321 @Override 322 public Map<String, String[]> getParameterMap() { 323 return null; 324 } 325 326 /** 327 * @see ServletRequest#getParameterNames() 328 */ 329 @Override 330 public Enumeration<String> getParameterNames() { 331 return null; 332 } 333 334 /** 335 * @see ServletRequest#getParameterValues(String) 336 */ 337 @Override 338 public String[] getParameterValues(final String arg0) { 339 return null; 340 } 341 342 @Override 343 public Part getPart(final String name) throws IOException, ServletException { 344 return null; 345 } 346 347 @Override 348 public Collection<Part> getParts() throws IOException, ServletException { 349 return null; 350 } 351 352 /** 353 * @see HttpServletRequest#getPathInfo() 354 */ 355 @Override 356 public String getPathInfo() { 357 return null; 358 } 359 360 /** 361 * @see HttpServletRequest#getPathTranslated() 362 */ 363 @Override 364 public String getPathTranslated() { 365 return null; 366 } 367 368 /** 369 * @see ServletRequest#getProtocol() 370 */ 371 @Override 372 public String getProtocol() { 373 return null; 374 } 375 376 public String getProtocolRequestId() { 377 throw new IllegalStateException("Not implemented 6.0.0"); 378 } 379 380 /** 381 * @see HttpServletRequest#getQueryString() 382 */ 383 @Override 384 public String getQueryString() { 385 return null; 386 } 387 388 /** 389 * @see ServletRequest#getReader() 390 */ 391 @Override 392 public BufferedReader getReader() throws IOException { 393 return null; 394 } 395 396 @Override 397 public String getRealPath(final String path) { 398 return null; 399 } 400 401 /** 402 * @see ServletRequest#getRemoteAddr() 403 */ 404 @Override 405 public String getRemoteAddr() { 406 return null; 407 } 408 409 /** 410 * @see ServletRequest#getRemoteHost() 411 */ 412 @Override 413 public String getRemoteHost() { 414 return null; 415 } 416 417 /** 418 * @see ServletRequest#getRemotePort() 419 */ 420 @Override 421 @SuppressWarnings("javadoc") // This is a Servlet 2.4 method 422 public int getRemotePort() { 423 return 0; 424 } 425 426 /** 427 * @see HttpServletRequest#getRemoteUser() 428 */ 429 @Override 430 public String getRemoteUser() { 431 return null; 432 } 433 434 /** 435 * @see ServletRequest#getRequestDispatcher(String) 436 */ 437 @Override 438 public RequestDispatcher getRequestDispatcher(final String arg0) { 439 return null; 440 } 441 442 /** 443 * @see HttpServletRequest#getRequestedSessionId() 444 */ 445 @Override 446 public String getRequestedSessionId() { 447 return null; 448 } 449 450 public String getRequestId() { 451 throw new IllegalStateException("Not implemented 6.0.0"); 452 } 453 454 /** 455 * @see HttpServletRequest#getRequestURI() 456 */ 457 @Override 458 public String getRequestURI() { 459 return null; 460 } 461 462 /** 463 * @see HttpServletRequest#getRequestURL() 464 */ 465 @Override 466 public StringBuffer getRequestURL() { 467 return null; 468 } 469 470 /** 471 * @see ServletRequest#getScheme() 472 */ 473 @Override 474 public String getScheme() { 475 return null; 476 } 477 478 /** 479 * @see ServletRequest#getServerName() 480 */ 481 @Override 482 public String getServerName() { 483 return null; 484 } 485 486 /** 487 * @see ServletRequest#getServerPort() 488 */ 489 @Override 490 public int getServerPort() { 491 return 0; 492 } 493 494 @Override 495 public ServletContext getServletContext() { 496 final var session = getSession(); 497 if (session == null) { 498 return null; 499 } 500 return session.getServletContext(); 501 } 502 503 /** 504 * @see HttpServletRequest#getServletPath() 505 */ 506 @Override 507 public String getServletPath() { 508 return null; 509 } 510 511 /** 512 * @see HttpServletRequest#getSession() 513 */ 514 @Override 515 public HttpSession getSession() { 516 return null; 517 } 518 519 /** 520 * @see HttpServletRequest#getSession(boolean) 521 */ 522 @Override 523 public HttpSession getSession(final boolean arg0) { 524 return null; 525 } 526 527 /** 528 * @see HttpServletRequest#getUserPrincipal() 529 */ 530 @Override 531 public Principal getUserPrincipal() { 532 return null; 533 } 534 535 @Override 536 public boolean isAsyncStarted() { 537 return false; 538 } 539 540 @Override 541 public boolean isAsyncSupported() { 542 return false; 543 } 544 545 /** 546 * @see HttpServletRequest#isRequestedSessionIdFromCookie() 547 */ 548 @Override 549 public boolean isRequestedSessionIdFromCookie() { 550 return false; 551 } 552 553 /** 554 * @see HttpServletRequest#isRequestedSessionIdFromUrl() 555 * @deprecated 556 */ 557 @Override 558 @Deprecated 559 public boolean isRequestedSessionIdFromUrl() { 560 return false; 561 } 562 563 /** 564 * @see HttpServletRequest#isRequestedSessionIdFromURL() 565 */ 566 @Override 567 public boolean isRequestedSessionIdFromURL() { 568 return false; 569 } 570 571 /** 572 * @see HttpServletRequest#isRequestedSessionIdValid() 573 */ 574 @Override 575 public boolean isRequestedSessionIdValid() { 576 return false; 577 } 578 579 /** 580 * @see ServletRequest#isSecure() 581 */ 582 @Override 583 public boolean isSecure() { 584 return false; 585 } 586 587 /** 588 * @see HttpServletRequest#isUserInRole(String) 589 */ 590 @Override 591 public boolean isUserInRole(final String arg0) { 592 return false; 593 } 594 595 @Override 596 public void login(final String username, final String password) throws ServletException { 597 throw new IllegalStateException("Not implemented"); 598 } 599 600 @Override 601 public void logout() throws ServletException { 602 throw new IllegalStateException("Not implemented"); 603 } 604 605 /** 606 * @see ServletRequest#removeAttribute(String) 607 */ 608 @Override 609 public void removeAttribute(final String arg0) { 610 } 611 612 /** 613 * @see ServletRequest#setAttribute(String, Object) 614 */ 615 @Override 616 public void setAttribute(final String arg0, final Object arg1) { 617 } 618 619 /** 620 * @see ServletRequest#setCharacterEncoding(String) 621 */ 622 @Override 623 public void setCharacterEncoding(final String arg0) throws UnsupportedEncodingException { 624 } 625 626 /** 627 * For testing attack scenarios in SizesTest. 628 */ 629 public void setContentLength(final long length) { 630 this.requestLength = length; 631 } 632 633 /** 634 * Sets the read limit. This can be used to limit the number of bytes to read ahead. 635 * 636 * @param readLimit the read limit to use 637 */ 638 public void setReadLimit(final int readLimit) { 639 this.readLimit = readLimit; 640 } 641 642 @Override 643 public AsyncContext startAsync() throws IllegalStateException { 644 throw new IllegalStateException("Not implemented"); 645 } 646 647 @Override 648 public AsyncContext startAsync(final ServletRequest servletRequest, final ServletResponse servletResponse) throws IllegalStateException { 649 throw new IllegalStateException("Not implemented"); 650 } 651 652 @Override 653 public <T extends HttpUpgradeHandler> T upgrade(final Class<T> handlerClass) throws IOException, ServletException { 654 throw new IllegalStateException("Not implemented"); 655 } 656 657// @Override 658// public ServletConnection getServletConnection() { 659// throw new IllegalStateException("Not implemented 6.0.0"); 660// } 661}