| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | package org.topazproject.ambra.web; |
|---|
| 20 | |
|---|
| 21 | import java.io.IOException; |
|---|
| 22 | import java.io.InputStream; |
|---|
| 23 | import java.io.InputStreamReader; |
|---|
| 24 | import java.io.PrintWriter; |
|---|
| 25 | import java.io.Reader; |
|---|
| 26 | import java.io.File; |
|---|
| 27 | import java.io.FileInputStream; |
|---|
| 28 | |
|---|
| 29 | import java.net.URL; |
|---|
| 30 | import java.net.URLConnection; |
|---|
| 31 | |
|---|
| 32 | import java.text.SimpleDateFormat; |
|---|
| 33 | |
|---|
| 34 | import java.util.ArrayList; |
|---|
| 35 | import java.util.Date; |
|---|
| 36 | import java.util.Iterator; |
|---|
| 37 | import java.util.Locale; |
|---|
| 38 | import java.util.StringTokenizer; |
|---|
| 39 | import java.util.TimeZone; |
|---|
| 40 | |
|---|
| 41 | import javax.servlet.ServletOutputStream; |
|---|
| 42 | import javax.servlet.http.HttpServletRequest; |
|---|
| 43 | import javax.servlet.http.HttpServletResponse; |
|---|
| 44 | |
|---|
| 45 | import org.apache.commons.logging.Log; |
|---|
| 46 | import org.apache.commons.logging.LogFactory; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | public class HttpResourceServer { |
|---|
| 55 | private static final Log log = LogFactory.getLog(HttpResourceServer.class); |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | private static final int INPUT_BUFFER_SIZE = 16384; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | private static final int OUTPUT_BUFFER_SIZE = 16384; |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | private static final String FILE_ENCODING = "UTF-8"; |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | private static final ArrayList<Range> FULL = new ArrayList<Range>(); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | private static final String MIME_SEPARATION = "AMBRA_MIME_BOUNDARY"; |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | public void serveResource(HttpServletRequest request, HttpServletResponse response, |
|---|
| 92 | Resource resource) throws IOException { |
|---|
| 93 | boolean head = "HEAD".equals(request.getMethod()); |
|---|
| 94 | serveResource(request, response, !head, resource); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | public void serveResource(HttpServletRequest request, HttpServletResponse response, |
|---|
| 108 | boolean content, Resource resource) |
|---|
| 109 | throws IOException { |
|---|
| 110 | |
|---|
| 111 | if (!checkIfHeaders(request, response, resource)) |
|---|
| 112 | return; |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | ArrayList<Range> ranges = parseRange(request, response, resource); |
|---|
| 116 | |
|---|
| 117 | response.setHeader("ETag", getETag(resource)); |
|---|
| 118 | |
|---|
| 119 | response.setHeader("Last-Modified", resource.getLastModifiedHttp()); |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | if (resource.getContentLength() == 0L) |
|---|
| 124 | content = false; |
|---|
| 125 | |
|---|
| 126 | ServletOutputStream ostream = null; |
|---|
| 127 | PrintWriter writer = null; |
|---|
| 128 | String contentType = resource.getContentType(); |
|---|
| 129 | long contentLength = resource.getContentLength(); |
|---|
| 130 | |
|---|
| 131 | if (content) { |
|---|
| 132 | |
|---|
| 133 | try { |
|---|
| 134 | ostream = response.getOutputStream(); |
|---|
| 135 | } catch (IllegalStateException e) { |
|---|
| 136 | |
|---|
| 137 | if ((contentType == null) || (contentType.startsWith("text")) |
|---|
| 138 | || (contentType.endsWith("xml"))) { |
|---|
| 139 | writer = response.getWriter(); |
|---|
| 140 | } else { |
|---|
| 141 | throw e; |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | if ((((ranges == null) || (ranges.isEmpty())) && (request.getHeader("Range") == null)) |
|---|
| 147 | || (ranges == FULL)) { |
|---|
| 148 | if (log.isDebugEnabled()) |
|---|
| 149 | log.debug("Full content response for " + resource); |
|---|
| 150 | |
|---|
| 151 | setOutputHeaders(response, contentType, contentLength, content); |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | if (content) { |
|---|
| 155 | if (ostream != null) { |
|---|
| 156 | copy(resource, ostream); |
|---|
| 157 | } else { |
|---|
| 158 | copy(resource, writer); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | } else { |
|---|
| 162 | if ((ranges == null) || (ranges.isEmpty())) |
|---|
| 163 | return; |
|---|
| 164 | |
|---|
| 165 | if (log.isDebugEnabled()) |
|---|
| 166 | log.debug("Partial content response for " + resource); |
|---|
| 167 | |
|---|
| 168 | response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); |
|---|
| 169 | |
|---|
| 170 | if (ranges.size() == 1) { |
|---|
| 171 | Range range = ranges.get(0); |
|---|
| 172 | response.addHeader("Content-Range", |
|---|
| 173 | "bytes " + range.start + "-" + range.end + "/" + range.length); |
|---|
| 174 | |
|---|
| 175 | long length = range.end - range.start + 1; |
|---|
| 176 | |
|---|
| 177 | setOutputHeaders(response, contentType, length, content); |
|---|
| 178 | |
|---|
| 179 | if (content) { |
|---|
| 180 | if (ostream != null) { |
|---|
| 181 | copy(resource, ostream, range); |
|---|
| 182 | } else { |
|---|
| 183 | copy(resource, writer, range); |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | } else { |
|---|
| 187 | response.setContentType("multipart/byteranges; boundary=" + MIME_SEPARATION); |
|---|
| 188 | |
|---|
| 189 | if (content) { |
|---|
| 190 | try { |
|---|
| 191 | response.setBufferSize(OUTPUT_BUFFER_SIZE); |
|---|
| 192 | } catch (IllegalStateException e) { |
|---|
| 193 | |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | if (ostream != null) { |
|---|
| 197 | copy(resource, ostream, ranges.iterator(), contentType); |
|---|
| 198 | } else { |
|---|
| 199 | copy(resource, writer, ranges.iterator(), contentType); |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | protected void setOutputHeaders(HttpServletResponse response, String contentType, |
|---|
| 215 | long contentLength, boolean content) { |
|---|
| 216 | |
|---|
| 217 | if (contentType != null) |
|---|
| 218 | response.setContentType(contentType); |
|---|
| 219 | |
|---|
| 220 | if (contentLength >= 0) { |
|---|
| 221 | if (contentLength < Integer.MAX_VALUE) { |
|---|
| 222 | response.setContentLength((int) contentLength); |
|---|
| 223 | } else { |
|---|
| 224 | |
|---|
| 225 | response.setHeader("content-length", "" + contentLength); |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | if (content) { |
|---|
| 231 | try { |
|---|
| 232 | response.setBufferSize(OUTPUT_BUFFER_SIZE); |
|---|
| 233 | } catch (IllegalStateException e) { |
|---|
| 234 | |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | protected Range parseContentRange(HttpServletRequest request, HttpServletResponse response) |
|---|
| 250 | throws IOException { |
|---|
| 251 | |
|---|
| 252 | String rangeHeader = request.getHeader("Content-Range"); |
|---|
| 253 | |
|---|
| 254 | if (rangeHeader == null) |
|---|
| 255 | return null; |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | if (!rangeHeader.startsWith("bytes")) { |
|---|
| 259 | response.sendError(HttpServletResponse.SC_BAD_REQUEST); |
|---|
| 260 | |
|---|
| 261 | return null; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | rangeHeader = rangeHeader.substring(6).trim(); |
|---|
| 265 | |
|---|
| 266 | int dashPos = rangeHeader.indexOf('-'); |
|---|
| 267 | int slashPos = rangeHeader.indexOf('/'); |
|---|
| 268 | |
|---|
| 269 | if (dashPos == -1) { |
|---|
| 270 | response.sendError(HttpServletResponse.SC_BAD_REQUEST); |
|---|
| 271 | |
|---|
| 272 | return null; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | if (slashPos == -1) { |
|---|
| 276 | response.sendError(HttpServletResponse.SC_BAD_REQUEST); |
|---|
| 277 | |
|---|
| 278 | return null; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | Range range = new Range(); |
|---|
| 282 | |
|---|
| 283 | try { |
|---|
| 284 | range.start = Long.parseLong(rangeHeader.substring(0, dashPos)); |
|---|
| 285 | range.end = Long.parseLong(rangeHeader.substring(dashPos + 1, slashPos)); |
|---|
| 286 | range.length = Long.parseLong(rangeHeader.substring(slashPos + 1, rangeHeader.length())); |
|---|
| 287 | } catch (NumberFormatException e) { |
|---|
| 288 | response.sendError(HttpServletResponse.SC_BAD_REQUEST); |
|---|
| 289 | |
|---|
| 290 | return null; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | if (!range.validate()) { |
|---|
| 294 | response.sendError(HttpServletResponse.SC_BAD_REQUEST); |
|---|
| 295 | |
|---|
| 296 | return null; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | return range; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | protected ArrayList<Range> parseRange(HttpServletRequest request, HttpServletResponse response, |
|---|
| 314 | Resource resource) throws IOException { |
|---|
| 315 | |
|---|
| 316 | String headerValue = request.getHeader("If-Range"); |
|---|
| 317 | |
|---|
| 318 | if (headerValue != null) { |
|---|
| 319 | long headerValueTime = (-1L); |
|---|
| 320 | |
|---|
| 321 | try { |
|---|
| 322 | headerValueTime = request.getDateHeader("If-Range"); |
|---|
| 323 | } catch (IllegalArgumentException e) { |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | String eTag = getETag(resource); |
|---|
| 327 | long lastModified = resource.getLastModified(); |
|---|
| 328 | |
|---|
| 329 | if (headerValueTime == (-1L)) { |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | if (!eTag.equals(headerValue.trim())) |
|---|
| 335 | return FULL; |
|---|
| 336 | } else { |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | if (lastModified > (headerValueTime + 1000)) |
|---|
| 342 | return FULL; |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | long fileLength = resource.getContentLength(); |
|---|
| 347 | |
|---|
| 348 | if (fileLength == 0) |
|---|
| 349 | return null; |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | String rangeHeader = request.getHeader("Range"); |
|---|
| 353 | |
|---|
| 354 | if (rangeHeader == null) |
|---|
| 355 | return null; |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | if (!rangeHeader.startsWith("bytes")) { |
|---|
| 360 | response.addHeader("Content-Range", "bytes */" + fileLength); |
|---|
| 361 | response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE); |
|---|
| 362 | |
|---|
| 363 | return null; |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | rangeHeader = rangeHeader.substring(6); |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | ArrayList<Range> result = new ArrayList<Range>(); |
|---|
| 371 | StringTokenizer commaTokenizer = new StringTokenizer(rangeHeader, ","); |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | while (commaTokenizer.hasMoreTokens()) { |
|---|
| 375 | String rangeDefinition = commaTokenizer.nextToken().trim(); |
|---|
| 376 | |
|---|
| 377 | Range currentRange = new Range(); |
|---|
| 378 | currentRange.length = fileLength; |
|---|
| 379 | |
|---|
| 380 | int dashPos = rangeDefinition.indexOf('-'); |
|---|
| 381 | |
|---|
| 382 | if (dashPos == -1) { |
|---|
| 383 | response.addHeader("Content-Range", "bytes */" + fileLength); |
|---|
| 384 | response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE); |
|---|
| 385 | |
|---|
| 386 | return null; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | if (dashPos == 0) { |
|---|
| 390 | try { |
|---|
| 391 | long offset = Long.parseLong(rangeDefinition); |
|---|
| 392 | currentRange.start = fileLength + offset; |
|---|
| 393 | currentRange.end = fileLength - 1; |
|---|
| 394 | } catch (NumberFormatException e) { |
|---|
| 395 | response.addHeader("Content-Range", "bytes */" + fileLength); |
|---|
| 396 | response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE); |
|---|
| 397 | |
|---|
| 398 | return null; |
|---|
| 399 | } |
|---|
| 400 | } else { |
|---|
| 401 | try { |
|---|
| 402 | currentRange.start = Long.parseLong(rangeDefinition.substring(0, dashPos)); |
|---|
| 403 | |
|---|
| 404 | if (dashPos < (rangeDefinition.length() - 1)) |
|---|
| 405 | currentRange.end = Long.parseLong(rangeDefinition.substring(dashPos + 1, |
|---|
| 406 | rangeDefinition.length())); |
|---|
| 407 | else |
|---|
| 408 | currentRange.end = fileLength - 1; |
|---|
| 409 | } catch (NumberFormatException e) { |
|---|
| 410 | response.addHeader("Content-Range", "bytes */" + fileLength); |
|---|
| 411 | response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE); |
|---|
| 412 | |
|---|
| 413 | return null; |
|---|
| 414 | } |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | if (!currentRange.validate()) { |
|---|
| 418 | response.addHeader("Content-Range", "bytes */" + fileLength); |
|---|
| 419 | response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE); |
|---|
| 420 | |
|---|
| 421 | return null; |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | result.add(currentRange); |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | return result; |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | protected boolean checkIfHeaders(HttpServletRequest request, HttpServletResponse response, |
|---|
| 443 | Resource resource) throws IOException { |
|---|
| 444 | return checkIfMatch(request, response, resource) |
|---|
| 445 | && checkIfModifiedSince(request, response, resource) |
|---|
| 446 | && checkIfNoneMatch(request, response, resource) |
|---|
| 447 | && checkIfUnmodifiedSince(request, response, resource); |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | protected String getETag(Resource resource) { |
|---|
| 458 | return "W/\"" + resource.getContentLength() + "-" + resource.getLastModified() + "\""; |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | |
|---|
| 468 | |
|---|
| 469 | |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | |
|---|
| 473 | protected boolean checkIfMatch(HttpServletRequest request, HttpServletResponse response, |
|---|
| 474 | Resource resource) throws IOException { |
|---|
| 475 | String eTag = getETag(resource); |
|---|
| 476 | String headerValue = request.getHeader("If-Match"); |
|---|
| 477 | |
|---|
| 478 | if (headerValue != null) { |
|---|
| 479 | if (headerValue.indexOf('*') == -1) { |
|---|
| 480 | StringTokenizer commaTokenizer = new StringTokenizer(headerValue, ","); |
|---|
| 481 | boolean conditionSatisfied = false; |
|---|
| 482 | |
|---|
| 483 | while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) { |
|---|
| 484 | String currentToken = commaTokenizer.nextToken(); |
|---|
| 485 | |
|---|
| 486 | if (currentToken.trim().equals(eTag)) |
|---|
| 487 | conditionSatisfied = true; |
|---|
| 488 | } |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | |
|---|
| 492 | if (!conditionSatisfied) { |
|---|
| 493 | response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED); |
|---|
| 494 | |
|---|
| 495 | if (log.isDebugEnabled()) |
|---|
| 496 | log.debug("If-Match: " + headerValue + ": Sending 'Pre-condition Failed' for " |
|---|
| 497 | + resource); |
|---|
| 498 | |
|---|
| 499 | return false; |
|---|
| 500 | } |
|---|
| 501 | } |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | return true; |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | protected boolean checkIfModifiedSince(HttpServletRequest request, HttpServletResponse response, |
|---|
| 520 | Resource resource) |
|---|
| 521 | throws IOException { |
|---|
| 522 | try { |
|---|
| 523 | long headerValue = request.getDateHeader("If-Modified-Since"); |
|---|
| 524 | long lastModified = resource.getLastModified(); |
|---|
| 525 | |
|---|
| 526 | if (headerValue != -1) { |
|---|
| 527 | |
|---|
| 528 | if ((request.getHeader("If-None-Match") == null) && (lastModified < (headerValue + 1000))) { |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); |
|---|
| 534 | response.setHeader("ETag", getETag(resource)); |
|---|
| 535 | |
|---|
| 536 | if (log.isDebugEnabled()) |
|---|
| 537 | log.debug("If-Modified-Since: " + headerValue + ": Sending 'Not Modified' for " |
|---|
| 538 | + resource); |
|---|
| 539 | |
|---|
| 540 | return false; |
|---|
| 541 | } |
|---|
| 542 | } |
|---|
| 543 | } catch (IllegalArgumentException illegalArgument) { |
|---|
| 544 | return true; |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | return true; |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | |
|---|
| 555 | |
|---|
| 556 | |
|---|
| 557 | |
|---|
| 558 | |
|---|
| 559 | |
|---|
| 560 | |
|---|
| 561 | |
|---|
| 562 | protected boolean checkIfNoneMatch(HttpServletRequest request, HttpServletResponse response, |
|---|
| 563 | Resource resource) |
|---|
| 564 | throws IOException { |
|---|
| 565 | String eTag = getETag(resource); |
|---|
| 566 | String headerValue = request.getHeader("If-None-Match"); |
|---|
| 567 | |
|---|
| 568 | if (headerValue != null) { |
|---|
| 569 | boolean conditionSatisfied = false; |
|---|
| 570 | |
|---|
| 571 | if (!headerValue.equals("*")) { |
|---|
| 572 | StringTokenizer commaTokenizer = new StringTokenizer(headerValue, ","); |
|---|
| 573 | |
|---|
| 574 | while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) { |
|---|
| 575 | String currentToken = commaTokenizer.nextToken(); |
|---|
| 576 | |
|---|
| 577 | if (currentToken.trim().equals(eTag)) |
|---|
| 578 | conditionSatisfied = true; |
|---|
| 579 | } |
|---|
| 580 | } else { |
|---|
| 581 | conditionSatisfied = true; |
|---|
| 582 | } |
|---|
| 583 | |
|---|
| 584 | if (conditionSatisfied) { |
|---|
| 585 | |
|---|
| 586 | |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | if (("GET".equals(request.getMethod())) || ("HEAD".equals(request.getMethod()))) { |
|---|
| 590 | response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); |
|---|
| 591 | response.setHeader("ETag", getETag(resource)); |
|---|
| 592 | |
|---|
| 593 | if (log.isDebugEnabled()) |
|---|
| 594 | log.debug("If-None-Match: " + headerValue + ": Sending 'Not Modified' for " + resource); |
|---|
| 595 | |
|---|
| 596 | return false; |
|---|
| 597 | } else { |
|---|
| 598 | response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED); |
|---|
| 599 | |
|---|
| 600 | if (log.isDebugEnabled()) |
|---|
| 601 | log.debug("If-None-Match: " + headerValue + ": Sending 'Pre-condition failed' for " + |
|---|
| 602 | resource); |
|---|
| 603 | |
|---|
| 604 | return false; |
|---|
| 605 | } |
|---|
| 606 | } |
|---|
| 607 | } |
|---|
| 608 | |
|---|
| 609 | return true; |
|---|
| 610 | } |
|---|
| 611 | |
|---|
| 612 | |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | |
|---|
| 618 | |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | |
|---|
| 623 | |
|---|
| 624 | protected boolean checkIfUnmodifiedSince(HttpServletRequest request, |
|---|
| 625 | HttpServletResponse response, Resource resource) |
|---|
| 626 | throws IOException { |
|---|
| 627 | try { |
|---|
| 628 | long lastModified = resource.getLastModified(); |
|---|
| 629 | long headerValue = request.getDateHeader("If-Unmodified-Since"); |
|---|
| 630 | |
|---|
| 631 | if (headerValue != -1) { |
|---|
| 632 | if (lastModified >= (headerValue + 1000)) { |
|---|
| 633 | |
|---|
| 634 | |
|---|
| 635 | |
|---|
| 636 | |
|---|
| 637 | response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED); |
|---|
| 638 | |
|---|
| 639 | if (log.isDebugEnabled()) |
|---|
| 640 | log.debug("If-Unmodified-Since: " + headerValue |
|---|
| 641 | + ": Sending 'Pre-condition failed' for " + resource); |
|---|
| 642 | |
|---|
| 643 | return false; |
|---|
| 644 | } |
|---|
| 645 | } |
|---|
| 646 | } catch (IllegalArgumentException illegalArgument) { |
|---|
| 647 | return true; |
|---|
| 648 | } |
|---|
| 649 | |
|---|
| 650 | return true; |
|---|
| 651 | } |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | |
|---|
| 658 | |
|---|
| 659 | |
|---|
| 660 | |
|---|
| 661 | |
|---|
| 662 | protected void copy(Resource resource, ServletOutputStream ostream) |
|---|
| 663 | throws IOException { |
|---|
| 664 | |
|---|
| 665 | byte[] buffer = resource.getContent(); |
|---|
| 666 | |
|---|
| 667 | if (buffer != null) { |
|---|
| 668 | ostream.write(buffer, 0, buffer.length); |
|---|
| 669 | |
|---|
| 670 | return; |
|---|
| 671 | } |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | InputStream istream = resource.streamContent(); |
|---|
| 675 | IOException exception = copyRange(istream, ostream); |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | istream.close(); |
|---|
| 679 | |
|---|
| 680 | |
|---|
| 681 | if (exception != null) |
|---|
| 682 | throw exception; |
|---|
| 683 | } |
|---|
| 684 | |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | |
|---|
| 688 | |
|---|
| 689 | |
|---|
| 690 | |
|---|
| 691 | |
|---|
| 692 | |
|---|
| 693 | |
|---|
| 694 | protected void copy(Resource resource, PrintWriter writer) throws IOException { |
|---|
| 695 | InputStream resourceInputStream = resource.streamContent(); |
|---|
| 696 | |
|---|
| 697 | Reader reader = new InputStreamReader(resourceInputStream, FILE_ENCODING); |
|---|
| 698 | |
|---|
| 699 | |
|---|
| 700 | IOException exception = copyRange(reader, writer); |
|---|
| 701 | |
|---|
| 702 | |
|---|
| 703 | reader.close(); |
|---|
| 704 | |
|---|
| 705 | |
|---|
| 706 | if (exception != null) |
|---|
| 707 | throw exception; |
|---|
| 708 | } |
|---|
| 709 | |
|---|
| 710 | |
|---|
| 711 | |
|---|
| 712 | |
|---|
| 713 | |
|---|
| 714 | |
|---|
| 715 | |
|---|
| 716 | |
|---|
| 717 | |
|---|
| 718 | |
|---|
| 719 | |
|---|
| 720 | protected void copy(Resource resource, ServletOutputStream ostream, Range range) |
|---|
| 721 | throws IOException { |
|---|
| 722 | InputStream istream = resource.streamContent(); |
|---|
| 723 | IOException exception = copyRange(istream, ostream, range.start, range.end); |
|---|
| 724 | |
|---|
| 725 | |
|---|
| 726 | istream.close(); |
|---|
| 727 | |
|---|
| 728 | |
|---|
| 729 | if (exception != null) |
|---|
| 730 | throw exception; |
|---|
| 731 | } |
|---|
| 732 | |
|---|
| 733 | |
|---|
| 734 | |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | |
|---|
| 738 | |
|---|
| 739 | |
|---|
| 740 | |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | protected void copy(Resource resource, PrintWriter writer, Range range) |
|---|
| 744 | throws IOException { |
|---|
| 745 | InputStream resourceInputStream = resource.streamContent(); |
|---|
| 746 | |
|---|
| 747 | Reader reader = new InputStreamReader(resourceInputStream, FILE_ENCODING); |
|---|
| 748 | |
|---|
| 749 | IOException exception = copyRange(reader, writer, range.start, range.end); |
|---|
| 750 | |
|---|
| 751 | |
|---|
| 752 | reader.close(); |
|---|
| 753 | |
|---|
| 754 | |
|---|
| 755 | if (exception != null) |
|---|
| 756 | throw exception; |
|---|
| 757 | } |
|---|
| 758 | |
|---|
| 759 | |
|---|
| 760 | |
|---|
| 761 | |
|---|
| 762 | |
|---|
| 763 | |
|---|
| 764 | |
|---|
| 765 | |
|---|
| 766 | |
|---|
| 767 | |
|---|
| 768 | |
|---|
| 769 | |
|---|
| 770 | protected void copy(Resource resource, ServletOutputStream ostream, Iterator ranges, |
|---|
| 771 | String contentType) throws IOException { |
|---|
| 772 | IOException exception = null; |
|---|
| 773 | |
|---|
| 774 | while ((exception == null) && (ranges.hasNext())) { |
|---|
| 775 | InputStream istream = resource.streamContent(); |
|---|
| 776 | Range currentRange = (Range) ranges.next(); |
|---|
| 777 | |
|---|
| 778 | |
|---|
| 779 | ostream.println(); |
|---|
| 780 | ostream.println("--" + MIME_SEPARATION); |
|---|
| 781 | |
|---|
| 782 | if (contentType != null) |
|---|
| 783 | ostream.println("Content-Type: " + contentType); |
|---|
| 784 | |
|---|
| 785 | ostream.println("Content-Range: bytes " + currentRange.start + "-" + currentRange.end + "/" |
|---|
| 786 | + currentRange.length); |
|---|
| 787 | ostream.println(); |
|---|
| 788 | |
|---|
| 789 | |
|---|
| 790 | exception = copyRange(istream, ostream, currentRange.start, currentRange.end); |
|---|
| 791 | |
|---|
| 792 | istream.close(); |
|---|
| 793 | } |
|---|
| 794 | |
|---|
| 795 | ostream.println(); |
|---|
| 796 | ostream.print("--" + MIME_SEPARATION + "--"); |
|---|
| 797 | |
|---|
| 798 | |
|---|
| 799 | if (exception != null) |
|---|
| 800 | throw exception; |
|---|
| 801 | } |
|---|
| 802 | |
|---|
| 803 | |
|---|
| 804 | |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | |
|---|
| 808 | |
|---|
| 809 | |
|---|
| 810 | |
|---|
| 811 | |
|---|
| 812 | |
|---|
| 813 | |
|---|
| 814 | protected void copy(Resource resource, PrintWriter writer, Iterator ranges, String contentType) |
|---|
| 815 | throws IOException { |
|---|
| 816 | IOException exception = null; |
|---|
| 817 | |
|---|
| 818 | while ((exception == null) && (ranges.hasNext())) { |
|---|
| 819 | InputStream resourceInputStream = resource.streamContent(); |
|---|
| 820 | |
|---|
| 821 | Reader reader = new InputStreamReader(resourceInputStream, FILE_ENCODING); |
|---|
| 822 | |
|---|
| 823 | Range currentRange = (Range) ranges.next(); |
|---|
| 824 | |
|---|
| 825 | |
|---|
| 826 | writer.println(); |
|---|
| 827 | writer.println("--" + MIME_SEPARATION); |
|---|
| 828 | |
|---|
| 829 | if (contentType != null) |
|---|
| 830 | writer.println("Content-Type: " + contentType); |
|---|
| 831 | |
|---|
| 832 | writer.println("Content-Range: bytes " + currentRange.start + "-" + currentRange.end + "/" + |
|---|
| 833 | currentRange.length); |
|---|
| 834 | writer.println(); |
|---|
| 835 | |
|---|
| 836 | |
|---|
| 837 | exception = copyRange(reader, writer, currentRange.start, currentRange.end); |
|---|
| 838 | |
|---|
| 839 | reader.close(); |
|---|
| 840 | } |
|---|
| 841 | |
|---|
| 842 | writer.println(); |
|---|
| 843 | writer.print("--" + MIME_SEPARATION + "--"); |
|---|
| 844 | |
|---|
| 845 | |
|---|
| 846 | if (exception != null) |
|---|
| 847 | throw exception; |
|---|
| 848 | } |
|---|
| 849 | |
|---|
| 850 | |
|---|
| 851 | |
|---|
| 852 | |
|---|
| 853 | |
|---|
| 854 | |
|---|
| 855 | |
|---|
| 856 | |
|---|
| 857 | |
|---|
| 858 | |
|---|
| 859 | protected IOException copyRange(InputStream istream, ServletOutputStream ostream) { |
|---|
| 860 | |
|---|
| 861 | IOException exception = null; |
|---|
| 862 | byte[] buffer = new byte[INPUT_BUFFER_SIZE]; |
|---|
| 863 | int len; |
|---|
| 864 | |
|---|
| 865 | while (true) { |
|---|
| 866 | try { |
|---|
| 867 | len = istream.read(buffer); |
|---|
| 868 | |
|---|
| 869 | if (len == -1) |
|---|
| 870 | break; |
|---|
| 871 | |
|---|
| 872 | ostream.write(buffer, 0, len); |
|---|
| 873 | } catch (IOException e) { |
|---|
| 874 | exception = e; |
|---|
| 875 | break; |
|---|
| 876 | } |
|---|
| 877 | } |
|---|
| 878 | |
|---|
| 879 | return exception; |
|---|
| 880 | } |
|---|
| 881 | |
|---|
| 882 | |
|---|
| 883 | |
|---|
| 884 | |
|---|
| 885 | |
|---|
| 886 | |
|---|
| 887 | |
|---|
| 888 | |
|---|
| 889 | |
|---|
| 890 | |
|---|
| 891 | protected IOException copyRange(Reader reader, PrintWriter writer) { |
|---|
| 892 | |
|---|
| 893 | IOException exception = null; |
|---|
| 894 | char[] buffer = new char[INPUT_BUFFER_SIZE]; |
|---|
| 895 | int len; |
|---|
| 896 | |
|---|
| 897 | while (true) { |
|---|
| 898 | try { |
|---|
| 899 | len = reader.read(buffer); |
|---|
| 900 | |
|---|
| 901 | if (len == -1) |
|---|
| 902 | break; |
|---|
| 903 | |
|---|
| 904 | writer.write(buffer, 0, len); |
|---|
| 905 | } catch (IOException e) { |
|---|
| 906 | exception = e; |
|---|
| 907 | break; |
|---|
| 908 | } |
|---|
| 909 | } |
|---|
| 910 | |
|---|
| 911 | return exception; |
|---|
| 912 | } |
|---|
| 913 | |
|---|
| 914 | |
|---|
| 915 | |
|---|
| 916 | |
|---|
| 917 | |
|---|
| 918 | |
|---|
| 919 | |
|---|
| 920 | |
|---|
| 921 | |
|---|
| 922 | |
|---|
| 923 | |
|---|
| 924 | |
|---|
| 925 | protected IOException copyRange(InputStream istream, ServletOutputStream ostream, long start, |
|---|
| 926 | long end) { |
|---|
| 927 | if (log.isTraceEnabled()) |
|---|
| 928 | log.trace("Serving bytes:" + start + "-" + end); |
|---|
| 929 | |
|---|
| 930 | try { |
|---|
| 931 | istream.skip(start); |
|---|
| 932 | } catch (IOException e) { |
|---|
| 933 | return e; |
|---|
| 934 | } |
|---|
| 935 | |
|---|
| 936 | IOException exception = null; |
|---|
| 937 | long bytesToRead = end - start + 1; |
|---|
| 938 | |
|---|
| 939 | byte[] buffer = new byte[INPUT_BUFFER_SIZE]; |
|---|
| 940 | int len = buffer.length; |
|---|
| 941 | |
|---|
| 942 | while ((bytesToRead > 0) && (len >= buffer.length)) { |
|---|
| 943 | try { |
|---|
| 944 | len = istream.read(buffer); |
|---|
| 945 | |
|---|
| 946 | if (bytesToRead >= len) { |
|---|
| 947 | ostream.write(buffer, 0, len); |
|---|
| 948 | bytesToRead -= len; |
|---|
| 949 | } else { |
|---|
| 950 | ostream.write(buffer, 0, (int) bytesToRead); |
|---|
| 951 | bytesToRead = 0; |
|---|
| 952 | } |
|---|
| 953 | } catch (IOException e) { |
|---|
| 954 | exception = e; |
|---|
| 955 | len = -1; |
|---|
| 956 | } |
|---|
| 957 | |
|---|
| 958 | if (len < buffer.length) |
|---|
| 959 | break; |
|---|
| 960 | } |
|---|
| 961 | |
|---|
| 962 | return exception; |
|---|
| 963 | } |
|---|
| 964 | |
|---|
| 965 | |
|---|
| 966 | |
|---|
| 967 | |
|---|
| 968 | |
|---|
| 969 | |
|---|
| 970 | |
|---|
| 971 | |
|---|
| 972 | |
|---|
| 973 | |
|---|
| 974 | |
|---|
| 975 | |
|---|
| 976 | protected IOException copyRange(Reader reader, PrintWriter writer, long start, long end) { |
|---|
| 977 | try { |
|---|
| 978 | reader.skip(start); |
|---|
| 979 | } catch (IOException e) { |
|---|
| 980 | return e; |
|---|
| 981 | } |
|---|
| 982 | |
|---|
| 983 | IOException exception = null; |
|---|
| 984 | long bytesToRead = end - start + 1; |
|---|
| 985 | |
|---|
| 986 | char[] buffer = new char[INPUT_BUFFER_SIZE]; |
|---|
| 987 | int len = buffer.length; |
|---|
| 988 | |
|---|
| 989 | while ((bytesToRead > 0) && (len >= buffer.length)) { |
|---|
| 990 | try { |
|---|
| 991 | len = reader.read(buffer); |
|---|
| 992 | |
|---|
| 993 | if (bytesToRead >= len) { |
|---|
| 994 | writer.write(buffer, 0, len); |
|---|
| 995 | bytesToRead -= len; |
|---|
| 996 | } else { |
|---|
| 997 | writer.write(buffer, 0, (int) bytesToRead); |
|---|
| 998 | bytesToRead = 0; |
|---|
| 999 | } |
|---|
| 1000 | } catch (IOException e) { |
|---|
| 1001 | exception = e; |
|---|
| 1002 | len = -1; |
|---|
| 1003 | } |
|---|
| 1004 | |
|---|
| 1005 | if (len < buffer.length) |
|---|
| 1006 | break; |
|---|
| 1007 | } |
|---|
| 1008 | |
|---|
| 1009 | return exception; |
|---|
| 1010 | } |
|---|
| 1011 | |
|---|
| 1012 | public static abstract class Resource { |
|---|
| 1013 | private final String name; |
|---|
| 1014 | private final long contentLength; |
|---|
| 1015 | private final long lastModified; |
|---|
| 1016 | private final String contentType; |
|---|
| 1017 | private final String lastModifiedHttp; |
|---|
| 1018 | |
|---|
| 1019 | public Resource(String name, String contentType, long contentLength, long lastModified) { |
|---|
| 1020 | this.name = name; |
|---|
| 1021 | this.contentType = (contentType == null) ? guessContentType(name) : contentType; |
|---|
| 1022 | this.contentLength = contentLength; |
|---|
| 1023 | this.lastModified = lastModified; |
|---|
| 1024 | |
|---|
| 1025 | |
|---|
| 1026 | SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); |
|---|
| 1027 | fmt.setTimeZone(TimeZone.getTimeZone("GMT")); |
|---|
| 1028 | lastModifiedHttp = fmt.format(new Date(lastModified)); |
|---|
| 1029 | } |
|---|
| 1030 | |
|---|
| 1031 | |
|---|
| 1032 | public static String guessContentType(String name) { |
|---|
| 1033 | |
|---|
| 1034 | |
|---|
| 1035 | if (name.endsWith(".js")) { |
|---|
| 1036 | return "text/javascript"; |
|---|
| 1037 | } else if (name.endsWith(".css")) { |
|---|
| 1038 | return "text/css"; |
|---|
| 1039 | } else if (name.endsWith(".jpg") || name.endsWith(".jpeg")) { |
|---|
| 1040 | return "image/jpeg"; |
|---|
| 1041 | } else if (name.endsWith(".png")) { |
|---|
| 1042 | return "image/png"; |
|---|
| 1043 | } else if (name.endsWith(".gif")) { |
|---|
| 1044 | return "image/gif"; |
|---|
| 1045 | } else if (name.endsWith(".html")) { |
|---|
| 1046 | return "text/html"; |
|---|
| 1047 | } else if (name.endsWith(".txt")) { |
|---|
| 1048 | return "text/plain"; |
|---|
| 1049 | } else { |
|---|
| 1050 | return null; |
|---|
| 1051 | } |
|---|
| 1052 | } |
|---|
| 1053 | |
|---|
| 1054 | public String getContentType() { |
|---|
| 1055 | return contentType; |
|---|
| 1056 | } |
|---|
| 1057 | |
|---|
| 1058 | public long getContentLength() { |
|---|
| 1059 | return contentLength; |
|---|
| 1060 | } |
|---|
| 1061 | |
|---|
| 1062 | public abstract InputStream streamContent() throws IOException; |
|---|
| 1063 | |
|---|
| 1064 | public abstract byte[] getContent(); |
|---|
| 1065 | |
|---|
| 1066 | public long getLastModified() { |
|---|
| 1067 | return lastModified; |
|---|
| 1068 | } |
|---|
| 1069 | |
|---|
| 1070 | public String getLastModifiedHttp() { |
|---|
| 1071 | return lastModifiedHttp; |
|---|
| 1072 | } |
|---|
| 1073 | |
|---|
| 1074 | public String toString() { |
|---|
| 1075 | return "Resource[name=" + name + |
|---|
| 1076 | ", contentType=" + contentType + |
|---|
| 1077 | ", contentLength=" + contentLength + |
|---|
| 1078 | ",lastModified=" + lastModified + |
|---|
| 1079 | "(" + lastModifiedHttp + ")]"; |
|---|
| 1080 | } |
|---|
| 1081 | } |
|---|
| 1082 | |
|---|
| 1083 | public static class FileResource extends Resource { |
|---|
| 1084 | |
|---|
| 1085 | private final File file; |
|---|
| 1086 | |
|---|
| 1087 | public FileResource(File file) { |
|---|
| 1088 | super(file.getName(), guessContentType(file.getName()), file.length(), file.lastModified()); |
|---|
| 1089 | this.file = file; |
|---|
| 1090 | } |
|---|
| 1091 | |
|---|
| 1092 | public InputStream streamContent() throws IOException { |
|---|
| 1093 | return new FileInputStream(file); |
|---|
| 1094 | } |
|---|
| 1095 | |
|---|
| 1096 | public byte[] getContent() { |
|---|
| 1097 | return null; |
|---|
| 1098 | } |
|---|
| 1099 | } |
|---|
| 1100 | |
|---|
| 1101 | |
|---|
| 1102 | public static class URLResource extends Resource { |
|---|
| 1103 | private final URL url; |
|---|
| 1104 | |
|---|
| 1105 | public URLResource(URL url) throws IOException { |
|---|
| 1106 | this(url, url.openConnection()); |
|---|
| 1107 | } |
|---|
| 1108 | |
|---|
| 1109 | private URLResource(URL url, URLConnection con) { |
|---|
| 1110 | super(url.toString(), urlContentType(url, con), con.getContentLength(), con.getLastModified()); |
|---|
| 1111 | this.url = url; |
|---|
| 1112 | } |
|---|
| 1113 | |
|---|
| 1114 | private static String urlContentType(URL url, URLConnection con) { |
|---|
| 1115 | |
|---|
| 1116 | String contentType = guessContentType(url.toString()); |
|---|
| 1117 | if (contentType == null) |
|---|
| 1118 | contentType = con.getContentType(); |
|---|
| 1119 | return contentType; |
|---|
| 1120 | } |
|---|
| 1121 | |
|---|
| 1122 | public InputStream streamContent() throws IOException { |
|---|
| 1123 | return url.openStream(); |
|---|
| 1124 | } |
|---|
| 1125 | |
|---|
| 1126 | public byte[] getContent() { |
|---|
| 1127 | return null; |
|---|
| 1128 | } |
|---|
| 1129 | } |
|---|
| 1130 | |
|---|
| 1131 | protected static class Range { |
|---|
| 1132 | public long start; |
|---|
| 1133 | public long end; |
|---|
| 1134 | public long length; |
|---|
| 1135 | |
|---|
| 1136 | |
|---|
| 1137 | |
|---|
| 1138 | |
|---|
| 1139 | |
|---|
| 1140 | |
|---|
| 1141 | public boolean validate() { |
|---|
| 1142 | if (end >= length) |
|---|
| 1143 | end = length - 1; |
|---|
| 1144 | |
|---|
| 1145 | return ((start >= 0) && (end >= 0) && (start <= end) && (length > 0)); |
|---|
| 1146 | } |
|---|
| 1147 | |
|---|
| 1148 | public void recycle() { |
|---|
| 1149 | start = 0; |
|---|
| 1150 | end = 0; |
|---|
| 1151 | length = 0; |
|---|
| 1152 | } |
|---|
| 1153 | } |
|---|
| 1154 | } |
|---|