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.wicket.protocol.ws.api; 018 019import java.io.BufferedReader; 020import java.io.IOException; 021import java.io.UnsupportedEncodingException; 022import java.security.Principal; 023import java.util.Collection; 024import java.util.Collections; 025import java.util.Enumeration; 026import java.util.HashMap; 027import java.util.Locale; 028import java.util.Map; 029 030import javax.servlet.AsyncContext; 031import javax.servlet.DispatcherType; 032import javax.servlet.RequestDispatcher; 033import javax.servlet.ServletContext; 034import javax.servlet.ServletException; 035import javax.servlet.ServletInputStream; 036import javax.servlet.ServletRequest; 037import javax.servlet.ServletResponse; 038import javax.servlet.http.Cookie; 039import javax.servlet.http.HttpServletRequest; 040import javax.servlet.http.HttpServletResponse; 041import javax.servlet.http.HttpSession; 042import javax.servlet.http.HttpUpgradeHandler; 043import javax.servlet.http.Part; 044 045/** 046 * A copy of the http servlet request used to create the WebSocket. 047 * Copy its details because they are discarded at the end of the request (even if we 048 * use AsyncContext from Servlet 3 spec). This copy is used later for following requests 049 * through the WebSocket connection to construct WebSocketRequest. 050 * 051 * @since 6.0 052 */ 053public class ServletRequestCopy implements HttpServletRequest 054{ 055 private final String contextPath; 056 private final String servletPath; 057 private final String pathInfo; 058 private final String requestUri; 059 private final HttpSessionCopy httpSession; 060 private final StringBuffer requestURL; 061 private final Map<String, Object> attributes = new HashMap<>(); 062 private final Map<String, Enumeration<String>> headers = new HashMap<>(); 063 private final Map<String, String[]> parameters = new HashMap<>(); 064 private final String method; 065 private final String serverName; 066 private final int serverPort; 067 private final String protocol; 068 private final String scheme; 069 private final String contentType; 070 private final Locale locale; 071 private final Enumeration<Locale> locales; 072 private final boolean isSecure; 073 private final String remoteUser; 074 private final String remoteAddr; 075 private final String remoteHost; 076 private final int remotePort; 077 private final String localAddr; 078 private final String localName; 079 private final int localPort; 080 private final String pathTranslated; 081 private final String requestedSessionId; 082 private final Principal principal; 083 084 private String characterEncoding; 085 086 public ServletRequestCopy(HttpServletRequest request) { 087 this.servletPath = request.getServletPath(); 088 this.contextPath = request.getContextPath(); 089 this.pathInfo = request.getPathInfo(); 090 this.requestUri = request.getRequestURI(); 091 this.requestURL = request.getRequestURL(); 092 this.method = request.getMethod(); 093 this.serverName = request.getServerName(); 094 this.serverPort = request.getServerPort(); 095 this.protocol = request.getProtocol(); 096 this.scheme = request.getScheme(); 097 this.characterEncoding = request.getCharacterEncoding(); 098 this.contentType = request.getContentType(); 099 this.locale = request.getLocale(); 100 this.locales = request.getLocales(); 101 this.isSecure = request.isSecure(); 102 this.remoteUser = request.getRemoteUser(); 103 this.remoteAddr = request.getRemoteAddr(); 104 this.remoteHost = request.getRemoteHost(); 105 this.remotePort = request.getRemotePort(); 106 this.localAddr = request.getLocalAddr(); 107 this.localName = request.getLocalName(); 108 this.localPort = request.getLocalPort(); 109 this.pathTranslated = request.getPathTranslated(); 110 this.requestedSessionId = request.getRequestedSessionId(); 111 this.principal = request.getUserPrincipal(); 112 113 HttpSession session = request.getSession(true); 114 httpSession = new HttpSessionCopy(session); 115 116 String s; 117 Enumeration<String> e = request.getHeaderNames(); 118 while (e != null && e.hasMoreElements()) { 119 s = e.nextElement(); 120 Enumeration<String> headerValues = request.getHeaders(s); 121 this.headers.put(s, headerValues); 122 } 123 124 e = request.getAttributeNames(); 125 while (e != null && e.hasMoreElements()) { 126 s = e.nextElement(); 127 attributes.put(s, request.getAttribute(s)); 128 } 129 130 e = request.getParameterNames(); 131 while (e != null && e.hasMoreElements()) { 132 s = e.nextElement(); 133 parameters.put(s, request.getParameterValues(s)); 134 } 135 } 136 137 @Override 138 public String getServerName() { 139 return serverName; 140 } 141 142 @Override 143 public int getServerPort() { 144 return serverPort; 145 } 146 147 @Override 148 public BufferedReader getReader() throws IOException 149 { 150 return null; 151 } 152 153 @Override 154 public String getRemoteAddr() 155 { 156 return remoteAddr; 157 } 158 159 @Override 160 public String getRemoteHost() 161 { 162 return remoteHost; 163 } 164 165 @Override 166 public HttpSession getSession(boolean create) { 167 return httpSession; 168 } 169 170 @Override 171 public String getMethod() { 172 return method; 173 } 174 175 @Override 176 public String getAuthType() 177 { 178 return null; 179 } 180 181 @Override 182 public Cookie[] getCookies() 183 { 184 return new Cookie[0]; 185 } 186 187 @Override 188 public long getDateHeader(String name) 189 { 190 return 0; 191 } 192 193 @Override 194 public String getHeader(String name) 195 { 196 Enumeration<String> values = headers.get(name); 197 if (values != null && values.hasMoreElements()) 198 { 199 return values.nextElement(); 200 } 201 return null; 202 } 203 204 @Override 205 public Enumeration<String> getHeaders(final String name) 206 { 207 return headers.get(name); 208 } 209 210 @Override 211 public Enumeration<String> getParameterNames() { 212 return Collections.enumeration(parameters.keySet()); 213 } 214 215 @Override 216 public String getParameter(String name) { 217 return parameters.get(name) != null ? parameters.get(name)[0] : null; 218 } 219 220 @Override 221 public String[] getParameterValues(final String name) { 222 return parameters.get(name); 223 } 224 225 @Override 226 public Map getParameterMap() 227 { 228 return parameters; 229 } 230 231 @Override 232 public String getProtocol() 233 { 234 String _protocol = "ws"; 235 if ("https".equalsIgnoreCase(protocol)) 236 { 237 _protocol = "wss"; 238 } 239 return _protocol; 240 } 241 242 @Override 243 public String getScheme() 244 { 245 String _scheme = "ws"; 246 if ("https".equalsIgnoreCase(scheme)) 247 { 248 _scheme = "wss"; 249 } 250 return _scheme; 251 } 252 253 @Override 254 public Enumeration<String> getHeaderNames() { 255 return Collections.enumeration(headers.keySet()); 256 } 257 258 @Override 259 public int getIntHeader(String name) 260 { 261 Enumeration<String> values = headers.get(name); 262 int result = -1; 263 if (values.hasMoreElements()) 264 { 265 String value = values.nextElement(); 266 result = Integer.parseInt(value, 10); 267 } 268 return result; 269 } 270 271 @Override 272 public Object getAttribute(String name) { 273 return attributes.get(name); 274 } 275 276 @Override 277 public Enumeration<String> getAttributeNames() { 278 return Collections.enumeration(attributes.keySet()); 279 } 280 281 @Override 282 public String getCharacterEncoding() 283 { 284 return characterEncoding; 285 } 286 287 @Override 288 public void setCharacterEncoding(String characterEncoding) throws UnsupportedEncodingException 289 { 290 this.characterEncoding = characterEncoding; 291 } 292 293 @Override 294 public int getContentLength() 295 { 296 return 0; 297 } 298 299 @Override 300 public long getContentLengthLong() 301 { 302 return 0; 303 } 304 305 @Override 306 public String getContentType() 307 { 308 return contentType; 309 } 310 311 @Override 312 public ServletInputStream getInputStream() throws IOException 313 { 314 return null; 315 } 316 317 @Override 318 public void setAttribute(String name, Object o) { 319 attributes.put(name, o); 320 } 321 322 @Override 323 public void removeAttribute(String name) { 324 attributes.remove(name); 325 } 326 327 @Override 328 public Locale getLocale() 329 { 330 return locale; 331 } 332 333 @Override 334 public Enumeration getLocales() 335 { 336 return locales; 337 } 338 339 @Override 340 public boolean isSecure() 341 { 342 return isSecure; 343 } 344 345 @Override 346 public RequestDispatcher getRequestDispatcher(String path) 347 { 348 return null; 349 } 350 351 @Override 352 public String getRealPath(String path) 353 { 354 return null; 355 } 356 357 @Override 358 public int getRemotePort() 359 { 360 return remotePort; 361 } 362 363 @Override 364 public String getLocalName() 365 { 366 return localName; 367 } 368 369 @Override 370 public String getLocalAddr() 371 { 372 return localAddr; 373 } 374 375 @Override 376 public int getLocalPort() 377 { 378 return localPort; 379 } 380 381 @Override 382 public ServletContext getServletContext() 383 { 384 return null; 385 } 386 387 @Override 388 public AsyncContext startAsync() throws IllegalStateException 389 { 390 return null; 391 } 392 393 @Override 394 public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException 395 { 396 return null; 397 } 398 399 @Override 400 public boolean isAsyncStarted() 401 { 402 return false; 403 } 404 405 @Override 406 public boolean isAsyncSupported() 407 { 408 return false; 409 } 410 411 @Override 412 public AsyncContext getAsyncContext() 413 { 414 return null; 415 } 416 417 @Override 418 public DispatcherType getDispatcherType() 419 { 420 return null; 421 } 422 423 @Override 424 public String getContextPath() { 425 return contextPath; 426 } 427 428 @Override 429 public String getQueryString() 430 { 431 return null; 432 } 433 434 @Override 435 public String getRemoteUser() 436 { 437 return remoteUser; 438 } 439 440 @Override 441 public boolean isUserInRole(String role) 442 { 443 return false; 444 } 445 446 @Override 447 public Principal getUserPrincipal() 448 { 449 return principal; 450 } 451 452 @Override 453 public String getRequestedSessionId() 454 { 455 return requestedSessionId; 456 } 457 458 @Override 459 public String getServletPath() { 460 return servletPath; 461 } 462 463 @Override 464 public String getPathInfo() { 465 return pathInfo; 466 } 467 468 @Override 469 public String getPathTranslated() 470 { 471 return pathTranslated; 472 } 473 474 @Override 475 public String getRequestURI() { 476 return requestUri; 477 } 478 479 @Override 480 public HttpSession getSession() { 481 return httpSession; 482 } 483 484 @Override 485 public String changeSessionId() 486 { 487 return null; 488 } 489 490 @Override 491 public boolean isRequestedSessionIdValid() 492 { 493 return false; 494 } 495 496 @Override 497 public boolean isRequestedSessionIdFromCookie() 498 { 499 return false; 500 } 501 502 @Override 503 public boolean isRequestedSessionIdFromURL() 504 { 505 return false; 506 } 507 508 @Override 509 public boolean isRequestedSessionIdFromUrl() 510 { 511 return false; 512 } 513 514 @Override 515 public boolean authenticate(HttpServletResponse response) throws IOException, ServletException 516 { 517 return false; 518 } 519 520 @Override 521 public void login(String username, String password) throws ServletException 522 { 523 } 524 525 @Override 526 public void logout() throws ServletException 527 { 528 } 529 530 @Override 531 public Collection<Part> getParts() throws IOException, ServletException 532 { 533 return Collections.emptyList(); 534 } 535 536 @Override 537 public Part getPart(String name) throws IOException, ServletException 538 { 539 return null; 540 } 541 542 @Override 543 public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException 544 { 545 return null; 546 } 547 548 @Override 549 public StringBuffer getRequestURL() { 550 return requestURL; 551 } 552}