/
AbstractResource.java
956 lines (858 loc) · 26.2 KB
/
AbstractResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.wicket.request.resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import org.apache.wicket.Application;
import org.apache.wicket.MetaDataKey;
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.request.HttpHeaderCollection;
import org.apache.wicket.request.Request;
import org.apache.wicket.request.Response;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.request.http.WebRequest;
import org.apache.wicket.request.http.WebResponse;
import org.apache.wicket.request.resource.caching.IResourceCachingStrategy;
import org.apache.wicket.request.resource.caching.IStaticCacheableResource;
import org.apache.wicket.util.io.Streams;
import org.apache.wicket.util.lang.Args;
import org.apache.wicket.util.lang.Classes;
import org.apache.wicket.util.string.Strings;
import org.apache.wicket.util.time.Duration;
import org.apache.wicket.util.time.Time;
/**
* Convenience resource implementation. The subclass must implement
* {@link #newResourceResponse(org.apache.wicket.request.resource.IResource.Attributes)} method.
*
* @author Matej Knopp
* @author Tobias Soloschenko
*/
public abstract class AbstractResource implements IResource
{
private static final long serialVersionUID = 1L;
/** header values that are managed internally and must not be set directly */
public static final Set<String> INTERNAL_HEADERS;
/** The meta data key of the content range start byte **/
public static final MetaDataKey<Long> CONTENT_RANGE_STARTBYTE = new MetaDataKey<Long>()
{
private static final long serialVersionUID = 1L;
};
/** The meta data key of the content range end byte **/
public static final MetaDataKey<Long> CONTENT_RANGE_ENDBYTE = new MetaDataKey<Long>()
{
private static final long serialVersionUID = 1L;
};
public static final String CONTENT_DISPOSITION_HEADER_NAME = "content-disposition";
/**
* All available content range types. The type name represents the name used in header
* information.
*/
public enum ContentRangeType
{
BYTES("bytes"), NONE("none");
private final String typeName;
ContentRangeType(String typeName)
{
this.typeName = typeName;
}
public String getTypeName()
{
return typeName;
}
}
static
{
INTERNAL_HEADERS = new HashSet<>();
INTERNAL_HEADERS.add("server");
INTERNAL_HEADERS.add("date");
INTERNAL_HEADERS.add("expires");
INTERNAL_HEADERS.add("last-modified");
INTERNAL_HEADERS.add("content-type");
INTERNAL_HEADERS.add("content-length");
INTERNAL_HEADERS.add(CONTENT_DISPOSITION_HEADER_NAME);
INTERNAL_HEADERS.add("transfer-encoding");
INTERNAL_HEADERS.add("connection");
INTERNAL_HEADERS.add("content-range");
INTERNAL_HEADERS.add("accept-range");
}
/**
* Construct.
*/
public AbstractResource()
{
}
/**
* Override this method to return a {@link ResourceResponse} for the request.
*
* @param attributes
* request attributes
* @return resource data instance
*/
protected abstract ResourceResponse newResourceResponse(Attributes attributes);
/**
* Represents data used to configure response and write resource data.
*
* @author Matej Knopp
*/
public static class ResourceResponse
{
private Integer errorCode;
private Integer statusCode;
private String errorMessage;
private String fileName = null;
private ContentDisposition contentDisposition = ContentDisposition.INLINE;
private String contentType = null;
private String contentRange = null;
private ContentRangeType contentRangeType = null;
private String textEncoding;
private long contentLength = -1;
private Time lastModified = null;
private WriteCallback writeCallback;
private Duration cacheDuration;
private WebResponse.CacheScope cacheScope;
private final HttpHeaderCollection headers;
/**
* Construct.
*/
public ResourceResponse()
{
// disallow caching for public caches. this behavior is similar to wicket 1.4:
// setting it to [PUBLIC] seems to be sexy but could potentially cache confidential
// data on public proxies for users migrating to 1.5
cacheScope = WebResponse.CacheScope.PRIVATE;
// collection of directly set response headers
headers = new HttpHeaderCollection();
}
/**
* Sets the error code for resource. If there is an error code set the data will not be
* rendered and the code will be sent to client.
*
* @param errorCode
* error code
*
* @return {@code this}, for chaining.
*/
public ResourceResponse setError(Integer errorCode)
{
setError(errorCode, null);
return this;
}
/**
* Sets the error code and message for resource. If there is an error code set the data will
* not be rendered and the code and message will be sent to client.
*
* @param errorCode
* error code
* @param errorMessage
* error message
*
* @return {@code this}, for chaining.
*/
public ResourceResponse setError(Integer errorCode, String errorMessage)
{
this.errorCode = errorCode;
this.errorMessage = errorMessage;
return this;
}
/**
* @return error code or <code>null</code>
*/
public Integer getErrorCode()
{
return errorCode;
}
/**
* Sets the status code for resource.
*
* @param statusCode
* status code
*
* @return {@code this}, for chaining.
*/
public ResourceResponse setStatusCode(Integer statusCode)
{
this.statusCode = statusCode;
return this;
}
/**
* @return status code or <code>null</code>
*/
public Integer getStatusCode()
{
return statusCode;
}
/**
* @return error message or <code>null</code>
*/
public String getErrorMessage()
{
return errorMessage;
}
/**
* Sets the file name of the resource.
*
* @param fileName
* file name
*
* @return {@code this}, for chaining.
*/
public ResourceResponse setFileName(String fileName)
{
this.fileName = fileName;
return this;
}
/**
* @return resource file name
*/
public String getFileName()
{
return fileName;
}
/**
* Determines whether the resource will be inline or an attachment.
*
* @see ContentDisposition
*
* @param contentDisposition
* content disposition (attachment or inline)
*
* @return {@code this}, for chaining.
*/
public ResourceResponse setContentDisposition(ContentDisposition contentDisposition)
{
Args.notNull(contentDisposition, "contentDisposition");
this.contentDisposition = contentDisposition;
return this;
}
/**
* @return whether the resource is inline or attachment
*/
public ContentDisposition getContentDisposition()
{
return contentDisposition;
}
/**
* Sets the content type for the resource. If no content type is set it will be determined
* by the extension.
*
* @param contentType
* content type (also known as mime type)
*
* @return {@code this}, for chaining.
*/
public ResourceResponse setContentType(String contentType)
{
this.contentType = contentType;
return this;
}
/**
* @return resource content type
*/
public String getContentType()
{
if (contentType == null && fileName != null)
{
contentType = Application.get().getMimeType(fileName);
}
return contentType;
}
/**
* Gets the content range of the resource. If no content range is set the client assumes the
* whole content.
*
* @return the content range
*/
public String getContentRange()
{
return contentRange;
}
/**
* Sets the content range of the resource. If no content range is set the client assumes the
* whole content. Please note that if the content range is set, the content length, the
* status code and the accept range must be set right, too.
*
* @param contentRange
* the content range
*/
public void setContentRange(String contentRange)
{
this.contentRange = contentRange;
}
/**
* If the resource accepts ranges
*
* @return the type of range (e.g. bytes)
*/
public ContentRangeType getAcceptRange()
{
return contentRangeType;
}
/**
* Sets the accept range header (e.g. bytes)
*
* @param contentRangeType
* the content range header information
*/
public void setAcceptRange(ContentRangeType contentRangeType)
{
this.contentRangeType = contentRangeType;
}
/**
* Sets the text encoding for the resource. This setting must only used if the resource
* response represents text.
*
* @param textEncoding
* character encoding of text body
*
* @return {@code this}, for chaining.
*/
public ResourceResponse setTextEncoding(String textEncoding)
{
this.textEncoding = textEncoding;
return this;
}
/**
* @return text encoding for resource
*/
protected String getTextEncoding()
{
return textEncoding;
}
/**
* Sets the content length (in bytes) of the data. Content length is optional but it's
* recommended to set it so that the browser can show download progress.
*
* @param contentLength
* length of response body
*
* @return {@code this}, for chaining.
*/
public ResourceResponse setContentLength(long contentLength)
{
this.contentLength = contentLength;
return this;
}
/**
* @return content length (in bytes)
*/
public long getContentLength()
{
return contentLength;
}
/**
* Sets the last modified data of the resource. Even though this method is optional it is
* recommended to set the date. If the date is set properly Wicket can check the
* <code>If-Modified-Since</code> to determine if the actual data really needs to be sent
* to client.
*
* @param lastModified
* last modification timestamp
*
* @return {@code this}, for chaining.
*/
public ResourceResponse setLastModified(Time lastModified)
{
this.lastModified = lastModified;
return this;
}
/**
* @return last modification timestamp
*/
public Time getLastModified()
{
return lastModified;
}
/**
* Check to determine if the resource data needs to be written. This method checks the
* <code>If-Modified-Since</code> request header and compares it to lastModified property.
* In order for this method to work {@link #setLastModified(Time)} has to be called first.
*
* @param attributes
* request attributes
* @return <code>true</code> if the resource data does need to be written,
* <code>false</code> otherwise.
*/
public boolean dataNeedsToBeWritten(Attributes attributes)
{
WebRequest request = (WebRequest)attributes.getRequest();
Time ifModifiedSince = request.getIfModifiedSinceHeader();
if (cacheDuration != Duration.NONE && ifModifiedSince != null && lastModified != null)
{
// [Last-Modified] headers have a maximum precision of one second
// so we have to truncate the milliseconds part for a proper compare.
// that's stupid, since changes within one second will not be reliably
// detected by the client ... any hint or clarification to improve this
// situation will be appreciated...
Time roundedLastModified = Time.millis(lastModified.getMilliseconds() / 1000 * 1000);
return ifModifiedSince.before(roundedLastModified);
}
else
{
return true;
}
}
/**
* Disables caching.
*
* @return {@code this}, for chaining.
*/
public ResourceResponse disableCaching()
{
return setCacheDuration(Duration.NONE);
}
/**
* Sets caching to maximum available duration.
*
* @return {@code this}, for chaining.
*/
public ResourceResponse setCacheDurationToMaximum()
{
cacheDuration = WebResponse.MAX_CACHE_DURATION;
return this;
}
/**
* Controls how long this response may be cached.
*
* @param duration
* caching duration in seconds
*
* @return {@code this}, for chaining.
*/
public ResourceResponse setCacheDuration(Duration duration)
{
cacheDuration = Args.notNull(duration, "duration");
return this;
}
/**
* Returns how long this resource may be cached for.
* <p/>
* The special value Duration.NONE means caching is disabled.
*
* @return duration for caching
*
* @see org.apache.wicket.settings.ResourceSettings#setDefaultCacheDuration(org.apache.wicket.util.time.Duration)
* @see org.apache.wicket.settings.ResourceSettings#getDefaultCacheDuration()
*/
public Duration getCacheDuration()
{
Duration duration = cacheDuration;
if (duration == null && Application.exists())
{
duration = Application.get().getResourceSettings().getDefaultCacheDuration();
}
return duration;
}
/**
* returns what kind of caches are allowed to cache the resource response
* <p/>
* resources are only cached at all if caching is enabled by setting a cache duration.
*
* @return cache scope
*
* @see org.apache.wicket.request.resource.AbstractResource.ResourceResponse#getCacheDuration()
* @see org.apache.wicket.request.resource.AbstractResource.ResourceResponse#setCacheDuration(org.apache.wicket.util.time.Duration)
* @see org.apache.wicket.request.http.WebResponse.CacheScope
*/
public WebResponse.CacheScope getCacheScope()
{
return cacheScope;
}
/**
* controls what kind of caches are allowed to cache the response
* <p/>
* resources are only cached at all if caching is enabled by setting a cache duration.
*
* @param scope
* scope for caching
*
* @see org.apache.wicket.request.resource.AbstractResource.ResourceResponse#getCacheDuration()
* @see org.apache.wicket.request.resource.AbstractResource.ResourceResponse#setCacheDuration(org.apache.wicket.util.time.Duration)
* @see org.apache.wicket.request.http.WebResponse.CacheScope
*
* @return {@code this}, for chaining.
*/
public ResourceResponse setCacheScope(WebResponse.CacheScope scope)
{
cacheScope = Args.notNull(scope, "scope");
return this;
}
/**
* Sets the {@link WriteCallback}. The callback is responsible for generating the response
* data.
* <p>
* It is necessary to set the {@link WriteCallback} if
* {@link #dataNeedsToBeWritten(org.apache.wicket.request.resource.IResource.Attributes)}
* returns <code>true</code> and {@link #setError(Integer)} has not been called.
*
* @param writeCallback
* write callback
*
* @return {@code this}, for chaining.
*/
public ResourceResponse setWriteCallback(final WriteCallback writeCallback)
{
Args.notNull(writeCallback, "writeCallback");
this.writeCallback = writeCallback;
return this;
}
/**
* @return write callback.
*/
public WriteCallback getWriteCallback()
{
return writeCallback;
}
/**
* get custom headers
*
* @return collection of the response headers
*/
public HttpHeaderCollection getHeaders()
{
return headers;
}
}
/**
* Configure the web response header for client cache control.
*
* @param data
* resource data
* @param attributes
* request attributes
*/
protected void configureCache(final ResourceResponse data, final Attributes attributes)
{
Response response = attributes.getResponse();
if (response instanceof WebResponse)
{
Duration duration = data.getCacheDuration();
WebResponse webResponse = (WebResponse)response;
if (duration.compareTo(Duration.NONE) > 0)
{
webResponse.enableCaching(duration, data.getCacheScope());
}
else
{
webResponse.disableCaching();
}
}
}
protected IResourceCachingStrategy getCachingStrategy()
{
return Application.get().getResourceSettings().getCachingStrategy();
}
/**
*
* @see org.apache.wicket.request.resource.IResource#respond(org.apache.wicket.request.resource.IResource.Attributes)
*/
@Override
public void respond(final Attributes attributes)
{
// Sets the request attributes
setRequestMetaData(attributes);
// Get a "new" ResourceResponse to write a response
ResourceResponse data = newResourceResponse(attributes);
// is resource supposed to be cached?
if (this instanceof IStaticCacheableResource)
{
final IStaticCacheableResource cacheable = (IStaticCacheableResource)this;
// is caching enabled?
if (cacheable.isCachingEnabled())
{
// apply caching strategy to response
getCachingStrategy().decorateResponse(data, cacheable);
}
}
// set response header
setResponseHeaders(data, attributes);
if (!data.dataNeedsToBeWritten(attributes) || data.getErrorCode() != null ||
needsBody(data.getStatusCode()) == false)
{
return;
}
if (data.getWriteCallback() == null)
{
throw new IllegalStateException("ResourceResponse#setWriteCallback() must be set.");
}
try
{
data.getWriteCallback().writeData(attributes);
}
catch (IOException iox)
{
throw new WicketRuntimeException(iox);
}
}
/**
* Decides whether a response body should be written back to the client depending on the set
* status code
*
* @param statusCode
* the status code set by the application
* @return {@code true} if the status code allows response body, {@code false} - otherwise
*/
private boolean needsBody(Integer statusCode)
{
return statusCode == null ||
(statusCode < 300 &&
statusCode != HttpServletResponse.SC_NO_CONTENT &&
statusCode != HttpServletResponse.SC_RESET_CONTENT);
}
/**
* check if header is directly modifyable
*
* @param name
* header name
*
* @throws IllegalArgumentException
* if access is forbidden
*/
private void checkHeaderAccess(String name)
{
name = Args.notEmpty(name.trim().toLowerCase(), "name");
if (INTERNAL_HEADERS.contains(name))
{
throw new IllegalArgumentException("you are not allowed to directly access header [" +
name + "], " + "use one of the other specialized methods of " +
Classes.simpleName(getClass()) + " to get or modify its value");
}
}
/**
* Reads the plain request header information and applies enriched information as meta data to
* the current request. Those information are available for the whole request cycle.
*
* @param attributes
* the attributes to get the plain request header information
*/
protected void setRequestMetaData(Attributes attributes)
{
Request request = attributes.getRequest();
if (request instanceof WebRequest)
{
WebRequest webRequest = (WebRequest)request;
setRequestRangeMetaData(webRequest);
}
}
protected void setRequestRangeMetaData(WebRequest webRequest)
{
String rangeHeader = webRequest.getHeader("range");
// The content range header is only be calculated if a range is given
if (!Strings.isEmpty(rangeHeader) &&
rangeHeader.contains(ContentRangeType.BYTES.getTypeName()))
{
// fixing white spaces
rangeHeader = rangeHeader.replaceAll(" ", "");
String range = rangeHeader.substring(rangeHeader.indexOf('=') + 1,
rangeHeader.length());
// support only the first range (WICKET-5995)
final int idxOfComma = range.indexOf(',');
String firstRange = idxOfComma > -1 ? range.substring(0, idxOfComma) : range;
String[] rangeParts = Strings.split(firstRange, '-');
String startByteString = rangeParts[0];
String endByteString = rangeParts[1];
long startbyte = !Strings.isEmpty(startByteString) ? Long.parseLong(startByteString) : 0;
long endbyte = !Strings.isEmpty(endByteString) ? Long.parseLong(endByteString) : -1;
// Make the content range information available for the whole request cycle
RequestCycle requestCycle = RequestCycle.get();
requestCycle.setMetaData(CONTENT_RANGE_STARTBYTE, startbyte);
requestCycle.setMetaData(CONTENT_RANGE_ENDBYTE, endbyte);
}
}
/**
* Sets the response header of resource response to the response received from the attributes
*
* @param resourceResponse
* the resource response to get the header fields from
* @param attributes
* the attributes to get the response from to which the header information are going
* to be applied
*/
protected void setResponseHeaders(final ResourceResponse resourceResponse,
final Attributes attributes)
{
Response response = attributes.getResponse();
if (response instanceof WebResponse)
{
WebResponse webResponse = (WebResponse)response;
// 1. Last Modified
Time lastModified = resourceResponse.getLastModified();
if (lastModified != null)
{
webResponse.setLastModifiedTime(lastModified);
}
// 2. Caching
configureCache(resourceResponse, attributes);
if (resourceResponse.getErrorCode() != null)
{
webResponse.sendError(resourceResponse.getErrorCode(),
resourceResponse.getErrorMessage());
return;
}
if (resourceResponse.getStatusCode() != null)
{
webResponse.setStatus(resourceResponse.getStatusCode());
}
if (!resourceResponse.dataNeedsToBeWritten(attributes))
{
webResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
// 3. Content Disposition
String fileName = resourceResponse.getFileName();
ContentDisposition disposition = resourceResponse.getContentDisposition();
if (ContentDisposition.ATTACHMENT == disposition)
{
webResponse.setAttachmentHeader(fileName);
}
else if (ContentDisposition.INLINE == disposition)
{
webResponse.setInlineHeader(fileName);
}
// 4. Mime Type (+ encoding)
String mimeType = resourceResponse.getContentType();
if (mimeType != null)
{
final String encoding = resourceResponse.getTextEncoding();
if (encoding == null)
{
webResponse.setContentType(mimeType);
}
else
{
webResponse.setContentType(mimeType + "; charset=" + encoding);
}
}
// 5. Accept Range
ContentRangeType acceptRange = resourceResponse.getAcceptRange();
if (acceptRange != null)
{
webResponse.setAcceptRange(acceptRange.getTypeName());
}
long contentLength = resourceResponse.getContentLength();
boolean contentRangeApplied = false;
// 6. Content Range
// for more information take a look here:
// http://stackoverflow.com/questions/8293687/sample-http-range-request-session
// if the content range header has been set directly
// to the resource response use it otherwise calculate it
String contentRange = resourceResponse.getContentRange();
if (contentRange != null)
{
webResponse.setContentRange(contentRange);
}
else
{
// content length has to be set otherwise the content range header can not be
// calculated - accept range must be set to bytes - others are not supported at the
// moment
if (contentLength != -1 && ContentRangeType.BYTES.equals(acceptRange))
{
contentRangeApplied = setResponseContentRangeHeaderFields(webResponse,
attributes, contentLength);
}
}
// 7. Content Length
if (contentLength != -1 && !contentRangeApplied)
{
webResponse.setContentLength(contentLength);
}
// add custom headers and values
final HttpHeaderCollection headers = resourceResponse.getHeaders();
for (String name : headers.getHeaderNames())
{
checkHeaderAccess(name);
for (String value : headers.getHeaderValues(name))
{
webResponse.addHeader(name, value);
}
}
}
}
/**
* Sets the content range header fields to the given web response
*
* @param webResponse
* the web response to apply the content range information to
* @param attributes
* the attributes to get the request from
* @param contentLength
* the content length of the response
* @return if the content range header information has been applied
*/
protected boolean setResponseContentRangeHeaderFields(WebResponse webResponse,
Attributes attributes, long contentLength)
{
boolean contentRangeApplied = false;
if (attributes.getRequest() instanceof WebRequest)
{
Long startbyte = RequestCycle.get().getMetaData(CONTENT_RANGE_STARTBYTE);
Long endbyte = RequestCycle.get().getMetaData(CONTENT_RANGE_ENDBYTE);
if (startbyte != null && endbyte != null)
{
// if end byte hasn't been set
if (endbyte == -1)
{
endbyte = contentLength - 1;
}
// Change the status code to 206 partial content
webResponse.setStatus(206);
// currently only bytes are supported.
webResponse.setContentRange(ContentRangeType.BYTES.getTypeName() + " " + startbyte +
'-' + endbyte + '/' + contentLength);
// WARNING - DO NOT SET THE CONTENT LENGTH, even if it is calculated right -
// SAFARI / CHROME are causing issues otherwise!
// webResponse.setContentLength((endbyte - startbyte) + 1);
// content range has been applied do not set the content length again!
contentRangeApplied = true;
}
}
return contentRangeApplied;
}
/**
* Callback invoked when resource data needs to be written to response. Subclass needs to
* implement the {@link #writeData(org.apache.wicket.request.resource.IResource.Attributes)}
* method.
*
* @author Matej Knopp
*/
public abstract static class WriteCallback
{
/**
* Write the resource data to response.
*
* @param attributes
* request attributes
*/
public abstract void writeData(Attributes attributes) throws IOException;
/**
* Convenience method to write an {@link InputStream} to response.
*
* @param attributes
* request attributes
* @param stream
* input stream
*/
protected final void writeStream(Attributes attributes, InputStream stream) throws IOException
{
final Response response = attributes.getResponse();
Streams.copy(stream, response.getOutputStream());
}
}
}