Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.http.lightweight.servlet; |
| 20 | |
| 21 | import java.io.BufferedInputStream; |
| 22 | import java.io.BufferedReader; |
| 23 | import java.io.ByteArrayInputStream; |
| 24 | import java.io.ByteArrayOutputStream; |
| 25 | import java.io.IOException; |
| 26 | import java.io.InputStream; |
| 27 | import java.io.InputStreamReader; |
| 28 | import java.io.UnsupportedEncodingException; |
| 29 | import java.net.Socket; |
| 30 | import java.net.URLDecoder; |
| 31 | import java.security.Principal; |
| 32 | import java.text.ParseException; |
| 33 | import java.text.SimpleDateFormat; |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.Arrays; |
| 36 | import java.util.Collections; |
| 37 | import java.util.Enumeration; |
| 38 | import java.util.HashMap; |
| 39 | import java.util.Iterator; |
| 40 | import java.util.List; |
| 41 | import java.util.Locale; |
| 42 | import java.util.Map; |
| 43 | import java.util.StringTokenizer; |
| 44 | |
| 45 | import javax.servlet.RequestDispatcher; |
| 46 | import javax.servlet.ServletInputStream; |
| 47 | import javax.servlet.http.Cookie; |
| 48 | import javax.servlet.http.HttpServletRequest; |
| 49 | import javax.servlet.http.HttpSession; |
| 50 | |
| 51 | import org.apache.felix.http.lightweight.osgi.Logger; |
| 52 | import org.apache.felix.http.lightweight.osgi.ServiceRegistration; |
| 53 | import org.apache.felix.http.lightweight.osgi.ServiceRegistrationResolver; |
| 54 | |
| 55 | /** |
| 56 | * This class represents an HTTP request, which is parses from a given input |
| 57 | * stream, and implements HttpServletRequest for servlet processing. |
| 58 | **/ |
| 59 | public class HttpServletRequestImpl implements HttpServletRequest |
| 60 | { |
| 61 | /** |
| 62 | * HTTP Method |
| 63 | */ |
| 64 | private String m_method; |
| 65 | /** |
| 66 | * Host info of URI |
| 67 | */ |
| 68 | private String m_uriHost; |
| 69 | /** |
| 70 | * URI of HTTP request |
| 71 | */ |
| 72 | private String m_uri; |
| 73 | /** |
| 74 | * HTTP version |
| 75 | */ |
| 76 | private String m_version; |
| 77 | /** |
| 78 | * Headers in HTTP request |
| 79 | */ |
| 80 | private final Map m_headers = new HashMap(); |
| 81 | private final Socket m_socket; |
| 82 | private Cookie[] m_cookies; |
| 83 | private final Locale m_locale = new Locale(System.getProperty("user.language")); |
| 84 | private Map m_attributes; |
| 85 | private final ServiceRegistrationResolver m_resolver; |
| 86 | private String m_servletPath; |
| 87 | /** |
| 88 | * Map of the parameters of the request. |
| 89 | */ |
| 90 | private Map m_parameters; |
| 91 | |
| 92 | /** |
| 93 | * When the body is parsed this value will be set to a non-null value |
| 94 | * regardless of the body content. As such it serves as a flag for parsing |
| 95 | * the body. |
| 96 | */ |
| 97 | private byte[] m_requestBody = null; |
| 98 | private final static String m_encoding = "UTF-8"; |
| 99 | private final Logger m_logger; |
| 100 | private String m_queryString; |
| 101 | /** |
| 102 | * Used to enforce the servlet API getInputStream()/getReader() calls. |
| 103 | */ |
| 104 | private boolean m_getInputStreamCalled = false; |
| 105 | /** |
| 106 | * Used to enforce the servlet API getInputStream()/getReader() calls. |
| 107 | */ |
| 108 | private boolean m_getReaderCalled = false; |
| 109 | |
| 110 | /** |
| 111 | * @param socket Socket assocated with request |
| 112 | * @param serviceRegistrationResolver |
| 113 | * @param logger |
| 114 | */ |
| 115 | public HttpServletRequestImpl(final Socket socket, final ServiceRegistrationResolver serviceRegistrationResolver, final Logger logger) |
| 116 | { |
| 117 | this.m_socket = socket; |
| 118 | this.m_resolver = serviceRegistrationResolver; |
| 119 | this.m_logger = logger; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @return The socket this request is associated with. |
| 124 | */ |
| 125 | protected Socket getSocket() |
| 126 | { |
| 127 | return m_socket; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * (non-Javadoc) |
| 132 | * |
| 133 | * @see javax.servlet.ServletRequest#getInputStream() |
| 134 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 135 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 136 | public ServletInputStream getInputStream() throws IOException |
| 137 | { |
| 138 | |
| 139 | if (m_getReaderCalled) |
| 140 | { |
| 141 | throw new IllegalStateException("getReader() has already been called."); |
| 142 | } |
| 143 | |
| 144 | if (m_requestBody == null) |
| 145 | { |
| 146 | parseBody(new BufferedInputStream(m_socket.getInputStream())); |
| 147 | } |
| 148 | |
| 149 | m_getInputStreamCalled = true; |
| 150 | |
| 151 | return new ConcreteServletInputStream(new ByteArrayInputStream(m_requestBody)); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * (non-Javadoc) |
| 156 | * |
| 157 | * @see javax.servlet.ServletRequest#getReader() |
| 158 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 159 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 160 | public BufferedReader getReader() throws IOException |
| 161 | { |
| 162 | if (m_getInputStreamCalled) |
| 163 | { |
| 164 | throw new IllegalStateException("getInputStream() has already been called."); |
| 165 | } |
| 166 | if (m_requestBody == null) |
| 167 | { |
| 168 | parseBody(new BufferedInputStream(m_socket.getInputStream())); |
| 169 | } |
| 170 | |
| 171 | m_getReaderCalled = true; |
| 172 | |
| 173 | return new BufferedReader(new InputStreamReader(new ByteArrayInputStream( |
| 174 | m_requestBody))); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * This method parses the HTTP request line from the specified input stream |
| 179 | * and stores the result. |
| 180 | * |
| 181 | * @param is |
| 182 | * The input stream from which to read the HTTP request. |
| 183 | * @throws java.io.IOException |
| 184 | * If any I/O error occurs. |
| 185 | **/ |
| 186 | public void parseRequestLine(final ConcreteServletInputStream is) throws IOException |
| 187 | { |
| 188 | String requestLine = is.readLine(); |
| 189 | if (requestLine == null) |
| 190 | { |
| 191 | throw new IOException("Unexpected end of file when reading request line."); |
| 192 | } |
| 193 | StringTokenizer st = new StringTokenizer(requestLine, " "); |
| 194 | if (st.countTokens() != 3) |
| 195 | { |
| 196 | throw new IOException("Malformed HTTP request: " + requestLine); |
| 197 | } |
| 198 | m_method = st.nextToken(); |
| 199 | m_uri = st.nextToken(); |
| 200 | m_version = st.nextToken(); |
| 201 | |
| 202 | // If the URI is absolute, break into host and path. |
| 203 | m_uriHost = ""; |
| 204 | int hostIdx = m_uri.indexOf("//"); |
| 205 | if (hostIdx > 0) |
| 206 | { |
| 207 | int pathIdx = m_uri.indexOf("/", hostIdx + 2); |
| 208 | m_uriHost = m_uri.substring(hostIdx + 2, pathIdx); |
| 209 | m_uri = m_uri.substring(pathIdx); |
| 210 | } |
| 211 | |
| 212 | // If the URI has query string, parse it. |
| 213 | int qsIdx = m_uri.indexOf("?"); |
| 214 | if (qsIdx > 0) |
| 215 | { |
| 216 | m_queryString = m_uri.substring(qsIdx + 1); |
| 217 | m_uri = m_uri.substring(0, qsIdx); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * This method parses the HTTP header lines from the specified input stream |
| 223 | * and stores the results. |
| 224 | * |
| 225 | * The map m_headers is populated with two types of values, Strings if the |
| 226 | * header occurs once or a List in the case that the same header is |
| 227 | * specified multiple times. |
| 228 | * |
| 229 | * @param is |
| 230 | * The input stream from which to read the HTTP header lines. |
| 231 | * @throws java.io.IOException |
| 232 | * If any I/O error occurs. |
| 233 | **/ |
| 234 | public void parseHeader(final ConcreteServletInputStream is) throws IOException |
| 235 | { |
| 236 | for (String s = is.readLine(); (s != null) && (s.length() != 0); s = is.readLine()) |
| 237 | { |
| 238 | int idx = s.indexOf(":"); |
| 239 | if (idx > 0) |
| 240 | { |
| 241 | String header = s.substring(0, idx).trim(); |
| 242 | String value = s.substring(idx + 1).trim(); |
| 243 | |
| 244 | String key = header.toLowerCase(); |
| 245 | |
| 246 | if (!m_headers.containsKey(key)) |
| 247 | { |
| 248 | m_headers.put(key, value); |
| 249 | } |
| 250 | else |
| 251 | { |
| 252 | Object originalValue = m_headers.get(key); |
| 253 | |
| 254 | if (originalValue instanceof String) |
| 255 | { |
| 256 | List headerList = new ArrayList(); |
| 257 | headerList.add(originalValue); |
| 258 | headerList.add(value); |
| 259 | m_headers.put(key, headerList); |
| 260 | } |
| 261 | else if (originalValue instanceof List) |
| 262 | { |
| 263 | ((List) originalValue).add(value); |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | throw new RuntimeException("Unexpected type in m_headers: " |
| 268 | + originalValue.getClass().getName()); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * This method parses the HTTP body from the specified input stream and |
| 277 | * ignores the result. |
| 278 | * |
| 279 | * @param is |
| 280 | * The input stream from which to read the HTTP body. |
| 281 | * @throws java.io.IOException |
| 282 | * If any I/O error occurs. |
| 283 | **/ |
| 284 | public void parseBody(final InputStream is) throws IOException |
| 285 | { |
| 286 | int length = getContentLength(); |
| 287 | |
| 288 | if (length > 0) |
| 289 | { |
| 290 | ByteArrayOutputStream baos = null; |
| 291 | |
| 292 | byte[] buf = new byte[length]; |
| 293 | int left = length; |
| 294 | |
| 295 | do |
| 296 | { |
| 297 | left = left - is.read(buf); |
| 298 | if (left > 0) |
| 299 | { |
| 300 | if (baos == null) |
| 301 | { |
| 302 | baos = new ByteArrayOutputStream(length); |
| 303 | } |
| 304 | baos.write(buf); |
| 305 | } |
| 306 | } |
| 307 | while (left > 0); |
| 308 | |
| 309 | if (baos != null) |
| 310 | { |
| 311 | m_requestBody = baos.toByteArray(); |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | m_requestBody = buf; |
| 316 | } |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | // Set this to a non-null value so we know that the body has been |
| 321 | // parsed. |
| 322 | m_requestBody = new byte[0]; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * (non-Javadoc) |
| 328 | * |
| 329 | * @see javax.servlet.http.HttpServletRequest#getMethod() |
| 330 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 331 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 332 | public String getMethod() |
| 333 | { |
| 334 | return m_method; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Returns the value of the specified header, if present. |
| 339 | * |
| 340 | * @param header |
| 341 | * The header value to retrieve. |
| 342 | * @return The value of the specified header or <tt>null</tt>. |
| 343 | **/ |
| 344 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 345 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 346 | public String getHeader(final String header) |
| 347 | { |
| 348 | Object value = m_headers.get(header.toLowerCase()); |
| 349 | |
| 350 | if (value == null) |
| 351 | { |
| 352 | return null; |
| 353 | } |
| 354 | |
| 355 | return value.toString(); |
| 356 | } |
| 357 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 358 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 359 | public Enumeration getHeaders(final String name) |
| 360 | { |
| 361 | Object v = m_headers.get(name); |
| 362 | |
| 363 | if (v == null) |
| 364 | { |
| 365 | return HttpConstants.EMPTY_ENUMERATION; |
| 366 | } |
| 367 | |
| 368 | if (v instanceof String) |
| 369 | { |
| 370 | return Collections.enumeration(Arrays.asList(new String[] { (String) v })); |
| 371 | } |
| 372 | |
| 373 | if (v instanceof List) |
| 374 | { |
| 375 | return Collections.enumeration((List) v); |
| 376 | } |
| 377 | |
| 378 | throw new RuntimeException("Unexpected type in m_headers: " |
| 379 | + v.getClass().getName()); |
| 380 | } |
| 381 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 382 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 383 | public Enumeration getHeaderNames() |
| 384 | { |
| 385 | if (m_headers.isEmpty()) |
| 386 | { |
| 387 | return HttpConstants.EMPTY_ENUMERATION; |
| 388 | } |
| 389 | |
| 390 | return Collections.enumeration(m_headers.keySet()); |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * (non-Javadoc) |
| 395 | * |
| 396 | * @see javax.servlet.ServletRequest#getAttribute(java.lang.String) |
| 397 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 398 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 399 | public Object getAttribute(final String arg0) |
| 400 | { |
| 401 | if (m_attributes != null) |
| 402 | { |
| 403 | return m_attributes.get(arg0); |
| 404 | } |
| 405 | |
| 406 | return null; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * (non-Javadoc) |
| 411 | * |
| 412 | * @see javax.servlet.ServletRequest#getAttributeNames() |
| 413 | */ |
| 414 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 415 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 416 | public Enumeration getAttributeNames() |
| 417 | { |
| 418 | if (m_attributes != null) |
| 419 | { |
| 420 | return Collections.enumeration(m_attributes.keySet()); |
| 421 | } |
| 422 | |
| 423 | return HttpConstants.EMPTY_ENUMERATION; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * (non-Javadoc) |
| 428 | * |
| 429 | * @see javax.servlet.ServletRequest#getCharacterEncoding() |
| 430 | */ |
| 431 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 432 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 433 | public String getCharacterEncoding() |
| 434 | { |
| 435 | return getHeader("Accept-Encoding"); |
| 436 | } |
| 437 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 438 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 439 | public int getContentLength() |
| 440 | { |
| 441 | int len = 0; |
| 442 | |
| 443 | try |
| 444 | { |
| 445 | len = Integer.parseInt(getHeader("Content-Length")); |
| 446 | } |
| 447 | catch (NumberFormatException e) |
| 448 | { |
| 449 | // Ignore this exception intentionally. |
| 450 | } |
| 451 | |
| 452 | return len; |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * (non-Javadoc) |
| 457 | * |
| 458 | * @see javax.servlet.ServletRequest#getContentType() |
| 459 | */ |
| 460 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 461 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 462 | public String getContentType() |
| 463 | { |
| 464 | return getHeader("Content-Type"); |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * (non-Javadoc) |
| 469 | * |
| 470 | * @see javax.servlet.ServletRequest#getLocale() |
| 471 | */ |
| 472 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 473 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 474 | public Locale getLocale() |
| 475 | { |
| 476 | return m_locale; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * (non-Javadoc) |
| 481 | * |
| 482 | * @see javax.servlet.ServletRequest#getLocales() |
| 483 | */ |
| 484 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 485 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 486 | public Enumeration getLocales() |
| 487 | { |
| 488 | return Collections.enumeration(Arrays.asList(new Object[] { m_locale })); |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * (non-Javadoc) |
| 493 | * |
| 494 | * @see javax.servlet.ServletRequest#getParameter(java.lang.String) |
| 495 | */ |
| 496 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 497 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 498 | public String getParameter(final String arg0) |
| 499 | { |
| 500 | if (m_parameters == null) |
| 501 | { |
| 502 | try |
| 503 | { |
| 504 | m_parameters = parseParameters(); |
| 505 | } |
| 506 | catch (UnsupportedEncodingException e) |
| 507 | { |
| 508 | m_logger.log(Logger.LOG_ERROR, "Failed to parse request parameters.", e); |
| 509 | return null; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | return (String) m_parameters.get(arg0); |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * (non-Javadoc) |
| 518 | * |
| 519 | * @see javax.servlet.ServletRequest#getParameterMap() |
| 520 | */ |
| 521 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 522 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 523 | public Map getParameterMap() |
| 524 | { |
| 525 | return m_parameters; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * (non-Javadoc) |
| 530 | * |
| 531 | * @see javax.servlet.ServletRequest#getParameterNames() |
| 532 | */ |
| 533 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 534 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 535 | public Enumeration getParameterNames() |
| 536 | { |
| 537 | return Collections.enumeration(m_parameters.keySet()); |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | * (non-Javadoc) |
| 542 | * |
| 543 | * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String) |
| 544 | */ |
| 545 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 546 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 547 | public String[] getParameterValues(String arg0) |
| 548 | { |
| 549 | return (String[]) m_parameters.values().toArray(new String[m_parameters.size()]); |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * (non-Javadoc) |
| 554 | * |
| 555 | * @see javax.servlet.ServletRequest#getProtocol() |
| 556 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 557 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 558 | public String getProtocol() |
| 559 | { |
| 560 | return m_version; |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * (non-Javadoc) |
| 565 | * |
| 566 | * @see javax.servlet.ServletRequest#getRealPath(java.lang.String) |
| 567 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 568 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 569 | public String getRealPath(final String arg0) |
| 570 | { |
| 571 | throw new UnimplementedAPIException(); |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * (non-Javadoc) |
| 576 | * |
| 577 | * @see javax.servlet.ServletRequest#getRemoteAddr() |
| 578 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 579 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 580 | public String getRemoteAddr() |
| 581 | { |
| 582 | return getSocket().getRemoteSocketAddress().toString(); |
| 583 | } |
| 584 | |
| 585 | /* (non-Javadoc) |
| 586 | * @see javax.servlet.ServletRequest#getRemoteHost() |
| 587 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 588 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 589 | public String getRemoteHost() |
| 590 | { |
| 591 | return getSocket().getRemoteSocketAddress().toString(); |
| 592 | } |
| 593 | |
| 594 | /* (non-Javadoc) |
| 595 | * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String) |
| 596 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 597 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 598 | public RequestDispatcher getRequestDispatcher(String arg0) |
| 599 | { |
| 600 | return null; |
| 601 | } |
| 602 | |
| 603 | /* (non-Javadoc) |
| 604 | * @see javax.servlet.ServletRequest#getScheme() |
| 605 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 606 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 607 | public String getScheme() |
| 608 | { |
| 609 | return HttpConstants.HTTP_SCHEME; |
| 610 | } |
| 611 | |
| 612 | /* (non-Javadoc) |
| 613 | * @see javax.servlet.ServletRequest#getServerName() |
| 614 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 615 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 616 | public String getServerName() |
| 617 | { |
| 618 | return HttpConstants.SERVER_INFO; |
| 619 | } |
| 620 | |
| 621 | /* (non-Javadoc) |
| 622 | * @see javax.servlet.ServletRequest#getServerPort() |
| 623 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 624 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 625 | public int getServerPort() |
| 626 | { |
| 627 | return getSocket().getLocalPort(); |
| 628 | } |
| 629 | |
| 630 | /* (non-Javadoc) |
| 631 | * @see javax.servlet.ServletRequest#isSecure() |
| 632 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 633 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 634 | public boolean isSecure() |
| 635 | { |
| 636 | return false; |
| 637 | } |
| 638 | |
| 639 | /* (non-Javadoc) |
| 640 | * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String) |
| 641 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 642 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 643 | public void removeAttribute(String arg0) |
| 644 | { |
| 645 | if (m_attributes != null) |
| 646 | { |
| 647 | m_attributes.remove(arg0); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /* (non-Javadoc) |
| 652 | * @see javax.servlet.ServletRequest#setAttribute(java.lang.String, java.lang.Object) |
| 653 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 654 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 655 | public void setAttribute(String arg0, Object arg1) |
| 656 | { |
| 657 | if (m_attributes == null) |
| 658 | { |
| 659 | m_attributes = new HashMap(); |
| 660 | } |
| 661 | |
| 662 | m_attributes.put(arg0, arg1); |
| 663 | } |
| 664 | |
| 665 | /* (non-Javadoc) |
| 666 | * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String) |
| 667 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 668 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 669 | public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException |
| 670 | { |
| 671 | throw new UnimplementedAPIException(); |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * (non-Javadoc) |
| 676 | * |
| 677 | * @see javax.servlet.http.HttpServletRequest#getAuthType() |
| 678 | */ |
| 679 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 680 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 681 | public String getAuthType() |
| 682 | { |
| 683 | return null; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * (non-Javadoc) |
| 688 | * |
| 689 | * @see javax.servlet.http.HttpServletRequest#getCookies() |
| 690 | */ |
| 691 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 692 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 693 | public Cookie[] getCookies() |
| 694 | { |
| 695 | if (m_cookies == null) |
| 696 | { |
| 697 | |
| 698 | String cookieHeader = getHeader("Cookie"); |
| 699 | |
| 700 | if (cookieHeader == null) |
| 701 | { |
| 702 | return null; |
| 703 | } |
| 704 | |
| 705 | List cookieList = new ArrayList(); |
| 706 | |
| 707 | for (Iterator i = Arrays.asList(cookieHeader.split(";")).iterator(); i.hasNext();) |
| 708 | { |
| 709 | String[] nvp = i.next().toString().split("="); |
| 710 | |
| 711 | if (nvp.length != 2) |
| 712 | { |
| 713 | //Ignore invalid cookie and and continue. |
| 714 | continue; |
| 715 | } |
| 716 | |
| 717 | cookieList.add(new Cookie(nvp[0].trim(), nvp[1].trim())); |
| 718 | } |
| 719 | m_cookies = (Cookie[]) cookieList.toArray(new Cookie[cookieList.size()]); |
| 720 | } |
| 721 | |
| 722 | return m_cookies; |
| 723 | } |
| 724 | |
| 725 | /* |
| 726 | * (non-Javadoc) |
| 727 | * |
| 728 | * @see |
| 729 | * javax.servlet.http.HttpServletRequest#getDateHeader(java.lang.String) |
| 730 | */ |
| 731 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 732 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 733 | public long getDateHeader(final String name) |
| 734 | { |
| 735 | String headerValue = getHeader(name); |
| 736 | |
| 737 | if (headerValue == null) |
| 738 | { |
| 739 | return -1; |
| 740 | } |
| 741 | |
| 742 | try |
| 743 | { |
| 744 | SimpleDateFormat sdf = new SimpleDateFormat(); |
| 745 | |
| 746 | return sdf.parse(headerValue).getTime(); |
| 747 | } |
| 748 | catch (ParseException e) |
| 749 | { |
| 750 | throw new IllegalArgumentException("Unable to convert to date: " |
| 751 | + headerValue); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * (non-Javadoc) |
| 757 | * |
| 758 | * @see javax.servlet.http.HttpServletRequest#getIntHeader(java.lang.String) |
| 759 | */ |
| 760 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 761 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 762 | public int getIntHeader(final String name) |
| 763 | { |
| 764 | String value = getHeader(name); |
| 765 | |
| 766 | if (value == null) |
| 767 | { |
| 768 | return -1; |
| 769 | } |
| 770 | |
| 771 | return Integer.parseInt(value); |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * (non-Javadoc) |
| 776 | * |
| 777 | * @see javax.servlet.http.HttpServletRequest#getPathInfo() |
| 778 | */ |
| 779 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 780 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 781 | public String getPathInfo() |
| 782 | { |
| 783 | String alias = getAlias(); |
| 784 | |
| 785 | if (m_uri != null && alias.length() > 0) |
| 786 | { |
| 787 | if (m_uri.length() == alias.length()) |
| 788 | { |
| 789 | return null; |
| 790 | } |
| 791 | |
| 792 | return m_uri.substring(alias.length()); |
| 793 | } |
| 794 | |
| 795 | return null; |
| 796 | } |
| 797 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 798 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 799 | public String getPathTranslated() |
| 800 | { |
| 801 | // TODO: Always returning null may be incorrect. |
| 802 | return null; |
| 803 | } |
| 804 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 805 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 806 | public String getContextPath() |
| 807 | { |
| 808 | return ""; |
| 809 | } |
| 810 | |
| 811 | /* |
| 812 | * (non-Javadoc) |
| 813 | * |
| 814 | * @see javax.servlet.http.HttpServletRequest#getQueryString() |
| 815 | */ |
| 816 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 817 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 818 | public String getQueryString() |
| 819 | { |
| 820 | return m_queryString; |
| 821 | } |
| 822 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 823 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 824 | public String getRemoteUser() |
| 825 | { |
| 826 | return null; |
| 827 | } |
| 828 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 829 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 830 | public boolean isUserInRole(String role) |
| 831 | { |
| 832 | return false; |
| 833 | } |
| 834 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 835 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 836 | public Principal getUserPrincipal() |
| 837 | { |
| 838 | return null; |
| 839 | } |
| 840 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 841 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 842 | public String getRequestedSessionId() |
| 843 | { |
| 844 | return null; |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | * (non-Javadoc) |
| 849 | * |
| 850 | * @see javax.servlet.http.HttpServletRequest#getRequestURI() |
| 851 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 852 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 853 | public String getRequestURI() |
| 854 | { |
| 855 | return m_uri; |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | * (non-Javadoc) |
| 860 | * |
| 861 | * @see javax.servlet.http.HttpServletRequest#getRequestURL() |
| 862 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 863 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 864 | public StringBuffer getRequestURL() |
| 865 | { |
| 866 | StringBuffer sb = new StringBuffer(); |
| 867 | sb.append(m_uriHost); |
| 868 | sb.append(m_uri); |
| 869 | |
| 870 | return sb; |
| 871 | } |
| 872 | |
| 873 | /* |
| 874 | * (non-Javadoc) |
| 875 | * |
| 876 | * @see javax.servlet.http.HttpServletRequest#getServletPath() |
| 877 | */ |
| 878 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 879 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 880 | public String getServletPath() |
| 881 | { |
| 882 | if (m_servletPath == null) |
| 883 | { |
| 884 | ServiceRegistration element = m_resolver.getServiceRegistration(m_uri); |
| 885 | |
| 886 | if (element == null) |
| 887 | { |
| 888 | throw new IllegalStateException( |
| 889 | "Unable to get ServletElement for HttpRequest."); |
| 890 | } |
| 891 | |
| 892 | m_servletPath = element.getAlias(); |
| 893 | } |
| 894 | |
| 895 | return m_servletPath; |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * @return Alias associated with this request |
| 900 | */ |
| 901 | private String getAlias() |
| 902 | { |
| 903 | ServiceRegistration element = m_resolver.getServiceRegistration(m_uri); |
| 904 | |
| 905 | if (element == null) |
| 906 | { |
| 907 | throw new IllegalStateException( |
| 908 | "Unable to get ServletElement for HttpRequest."); |
| 909 | } |
| 910 | |
| 911 | return element.getAlias(); |
| 912 | } |
| 913 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 914 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 915 | public HttpSession getSession(boolean create) |
| 916 | { |
| 917 | throw new UnimplementedAPIException(); |
| 918 | } |
| 919 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 920 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 921 | public HttpSession getSession() |
| 922 | { |
| 923 | throw new UnimplementedAPIException(); |
| 924 | } |
| 925 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 926 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 927 | public boolean isRequestedSessionIdValid() |
| 928 | { |
| 929 | throw new UnimplementedAPIException(); |
| 930 | } |
| 931 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 932 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 933 | public boolean isRequestedSessionIdFromCookie() |
| 934 | { |
| 935 | throw new UnimplementedAPIException(); |
| 936 | } |
| 937 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 938 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 939 | public boolean isRequestedSessionIdFromURL() |
| 940 | { |
| 941 | throw new UnimplementedAPIException(); |
| 942 | } |
| 943 | |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 944 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 945 | public boolean isRequestedSessionIdFromUrl() |
| 946 | { |
| 947 | throw new UnimplementedAPIException(); |
| 948 | } |
| 949 | |
| 950 | /** |
| 951 | * Parse the parameters in the request and return as a Map of <String, |
| 952 | * String>. |
| 953 | * |
| 954 | * @return |
| 955 | * @throws UnsupportedEncodingException |
| 956 | */ |
| 957 | private Map parseParameters() throws UnsupportedEncodingException |
| 958 | { |
| 959 | Map params = new HashMap(); |
| 960 | |
| 961 | String queryString = getQueryString(); |
| 962 | |
| 963 | if (queryString != null && queryString.length() > 0) |
| 964 | { |
| 965 | parseParameterString(queryString, params); |
| 966 | } |
| 967 | |
| 968 | if (m_requestBody != null) |
| 969 | { |
| 970 | parseParameterString(new String(m_requestBody), params); |
| 971 | } |
| 972 | |
| 973 | return params; |
| 974 | } |
| 975 | |
| 976 | /** |
| 977 | * |
| 978 | * @param queryString |
| 979 | * A String formatted like: 'home=Cosby&favorite+flavor=flies' |
| 980 | * @param params |
| 981 | * Map of <String, String> of existing parameters to be added to. |
| 982 | * |
| 983 | * @throws UnsupportedEncodingException |
| 984 | * if encoding type is unsupported |
| 985 | */ |
| 986 | private void parseParameterString(final String queryString, final Map params) |
| 987 | throws UnsupportedEncodingException |
| 988 | { |
| 989 | for (Iterator i = Arrays.asList(queryString.split("&")).iterator(); i.hasNext();) |
| 990 | { |
| 991 | String[] nva = i.next().toString().split("="); |
| 992 | |
| 993 | if (nva.length == 2) |
| 994 | { |
| 995 | params.put(URLDecoder.decode(nva[0].trim(), m_encoding), nva[1].trim()); |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | /** |
| 1001 | * @param method |
| 1002 | * HTTP method |
| 1003 | * @return true if the psased HTTP method is supported by this server. |
| 1004 | */ |
| 1005 | public static boolean isSupportedMethod(final String method) |
| 1006 | { |
| 1007 | if (method.equals(HttpConstants.OPTIONS_REQUEST)) |
| 1008 | { |
| 1009 | return false; |
| 1010 | } |
| 1011 | |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
| 1015 | /* (non-Javadoc) |
| 1016 | * @see java.lang.Object#toString() |
| 1017 | */ |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 1018 | @Override |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 1019 | public String toString() |
| 1020 | { |
| 1021 | if (m_method != null && m_uri != null) |
| 1022 | { |
| 1023 | return m_method + m_uri; |
| 1024 | } |
| 1025 | |
| 1026 | return super.toString(); |
| 1027 | } |
Ken Gilmer | c65a28c | 2011-10-13 05:36:47 +0000 | [diff] [blame^] | 1028 | |
| 1029 | /* (non-Javadoc) |
| 1030 | * @see javax.servlet.ServletRequest#getLocalAddr() |
| 1031 | */ |
| 1032 | @Override |
| 1033 | public String getLocalAddr() |
| 1034 | { |
| 1035 | return m_socket.getLocalAddress().getHostAddress(); |
| 1036 | } |
| 1037 | |
| 1038 | /* (non-Javadoc) |
| 1039 | * @see javax.servlet.ServletRequest#getLocalName() |
| 1040 | */ |
| 1041 | @Override |
| 1042 | public String getLocalName() |
| 1043 | { |
| 1044 | return m_socket.getLocalAddress().getHostName(); |
| 1045 | } |
| 1046 | |
| 1047 | /* (non-Javadoc) |
| 1048 | * @see javax.servlet.ServletRequest#getLocalPort() |
| 1049 | */ |
| 1050 | @Override |
| 1051 | public int getLocalPort() |
| 1052 | { |
| 1053 | return m_socket.getLocalPort(); |
| 1054 | } |
| 1055 | |
| 1056 | /* (non-Javadoc) |
| 1057 | * @see javax.servlet.ServletRequest#getRemotePort() |
| 1058 | */ |
| 1059 | @Override |
| 1060 | public int getRemotePort() |
| 1061 | { |
| 1062 | return m_socket.getPort(); |
| 1063 | } |
Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 1064 | } |