8000 Added a new getRequest() method and new write() methods with date par… · javaxt-project/javaxt-jetty@8bd1822 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8bd1822

Browse files
committed
Added a new getRequest() method and new write() methods with date params to generate cacheable responses in the HttpServletResponse class
1 parent d691de9 commit 8bd1822

File tree

1 file changed

+115
-47
lines changed

1 file changed

+115
-47
lines changed

src/javaxt/http/servlet/HttpServletResponse.java

Lines changed: 115 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ public HttpServletResponse(HttpServletRequest request, jakarta.servlet.http.Http
7373
}
7474

7575

76+
//**************************************************************************
77+
//** getRequest
78+
//**************************************************************************
79+
/** Returns the HttpServletRequest request used to initiate this response.
80+
*/
81+
public HttpServletRequest getRequest(){
82+
return request;
83+
}
84+
85+
7686
//**************************************************************************
7787
//** addCookie
7888
//**************************************************************************
@@ -482,15 +492,17 @@ public void sendRedirect(String location, boolean movedPermanently)
482492
//**************************************************************************
483493
//** write
484494
//**************************************************************************
485-
/** Used to write a block of text in the response body. You should only call
486-
* this method once.
495+
/** Used to write text (plain text, html, json, etc) to the response body.
496+
* You should only call this method once.
487497
* @param compressOutput Specify whether to gzip compress the text.
488498
* Note that this option will be applied only if "Accept-Encoding" supports
489499
* gzip compression.
490500
*/
491501
public void write(String text, boolean compressOutput) throws IOException {
492502
try{
493-
write(text.getBytes(charSet), compressOutput);
503+
byte[] bytes = text.getBytes(charSet);
504+
setCharacterEncoding(charSet);
505+
write(bytes, compressOutput);
494506
}
495507
catch(java.io.UnsupportedEncodingException e){
496508
//this error should have been thrown earlier (setCharacterEncoding)
@@ -501,12 +513,27 @@ public void write(String text, boolean compressOutput) throws IOException {
501513
//**************************************************************************
502514
//** write
503515
//**************************************************************************
504-
/** Used to write a block of text in the response body. Will automatically
505-
* try to gzip compress the text if "Accept-Encoding" supports gzip
506-
* compression. You should only call this method once.
516+
/** Used to write text (plain text, html, json, etc) to the response body.
517+
* Will automatically try to gzip compress the text if "Accept-Encoding"
518+
* supports gzip compression. You should only call this method once.
507519
*/
508520
public void write(String text) throws IOException {
509-
this.write(text, true);
521+
write(text, true);
522+
}
523+
524+
525+
//**************************************************************************
526+
//** write
527+
//**************************************************************************
528+
/** Used to write text (plain text, html, json, etc) to the response body.
529+
* Transparently handles caching using "ETag" and "Last-Modified" headers.
530+
* Will automatically try to gzip compress the text if "Accept-Encoding"
531+
* supports gzip compression. You should only call this method once.
532+
* @param date UTC date in milliseconds since January 1, 1970, 00:00:00 UTC
533+
*/
534+
public void write(String text, long date) throws IOException {
535+
if (send304(date, text.length())) return;
536+
write(text, true);
510537
}
511538

512539

@@ -657,6 +684,21 @@ public void write(byte[] bytes) throws IOException {
657684
}
658685

659686

687+
//**************************************************************************
688+
//** write
689+
//**************************************************************************
690+
/** Used to write bytes to the response body. Transparently handles caching
691+
* using "ETag" and "Last-Modified" headers. Will automatically try to gzip
692+
* compress the text if "Accept-Encoding" supports gzip compression. You
693+
* should only call this method once.
694+
* @param date UTC date in milliseconds since January 1, 1970, 00:00:00 UTC
695+
*/
696+
public void write(byte[] bytes, long date) throws IOException {
697+
if (send304(date, bytes.length)) return;
698+
write(bytes, true);
699+
}
700+
701+
660702
//**************************************************************************
661703
//** write
662704
//**************************************************************************
@@ -682,14 +724,19 @@ public void write(java.io.File file, String contentType, boolean useCache)
682724
/** Used to write contents of a file into the response body. Automatically
683725
* compresses the file content if the client supports gzip compression.
684726
* You should only call this method once.
685-
* @param fileName Optional file name used in the "Content-Disposition" header.
686-
* If the fileName is null, the "Content-Disposition" header will not be
687-
* set by this method.
727+
* @param file The file to send to the client
728+
* @param fileName Optional file name used in the "Content-Disposition"
729+
* header. If the fileName is null, the "Content-Disposition" header will
730+
* not be set by this method.
731+
* @param useCache If true, will generate "ETag", "Last-Modified", and
732+
* "Cache-Control" headers. If the "ETag" matches the "if-none-match"
733+
* or if the "Last-Modified" matches the "if-modified-since" request
734+
* headers then a 304 "Not Modified" is returned.
688735
*/
689736
public void write(java.io.File file, String fileName, String contentType, boolean useCache)
690737
throws IOException {
691738

692-
if (!file.exists() || file.isDirectory()){
739+
if (file==null || !file.exists() || file.isDirectory()){
693740
this.setStatus(404);
694741
return;
695742
}
@@ -700,44 +747,10 @@ public void write(java.io.File file, String fileName, String contentType, boolea
700747

701748
//Process Cache Directives
702749
if (useCache){
703-
704-
String eTag = "W/\"" + fileSize + "-" + fileDate + "\"";
705-
setHeader("ETag", eTag);
706-
setHeader("Last-Modified", getDate(fileDate)); //Sat, 23 Oct 2010 13:04:28 GMT
707-
//this.setHeader("Cache-Control", "max-age=315360000");
708-
//this.setHeader("Expires", "Sun, 30 Sep 2018 16:23:15 GMT ");
709-
710-
711-
//Return 304/Not Modified response if we can...
712-
String matchTag = request.getHeader("if-none-match");
713-
String cacheControl = request.getHeader("cache-control");
714-
if (matchTag==null) matchTag = "";
715-
if (cacheControl==null) cacheControl = "";
716-
if (cacheControl.equalsIgnoreCase("no-cache")==false){
717-
if (eTag.equalsIgnoreCase(matchTag)){
718-
//System.out.println("Sending 304 Response!");
719-
this.setStatus(304);
720-
return;
721-
}
722-
else{
723-
//Internet Explorer 6 uses "if-modified-since" instead of "if-none-match"
724-
matchTag = request.getHeader("if-modified-since");
725-
if (matchTag!=null){
726-
for (String tag: matchTag.split(";")){
727-
if (tag.trim().equalsIgnoreCase(getDate(fileDate))){
728-
//System.out.println("Sending 304 Response!");
729-
this.setStatus(304);
730-
return;
731-
}
732-
}
733-
}
734-
735-
}
736-
}
737-
750+
if (send304(fileDate, fileSize)) return;
738751
}
739752
else{
740-
setHeader ("Cache-Control", "no-cache");
753+
setHeader("Cache-Control", "no-store");
741754
}
742755

743756

@@ -1223,6 +1236,61 @@ private String getDate(long milliseconds){
12231236
}
12241237

12251238

1239+
//**************************************************************************
1240+
//** send304
1241+
//**************************************************************************
1242+
/** Sets the "ETag", "Last-Modified", and "Cache-Control" response headers.
1243+
* If the "ETag" matches the "if-none-match" or if the "Last-Modified"
1244+
* matches the "if-modified-since" request headers then the status is set
1245+
* to 304 "Not Modified".
1246+
* @return Returns true if the status was set to 304. Otherwise, returns
1247+
* false.
1248+
*/
1249+
private boolean send304(long date, long size){
1250+
1251+
//Generate "ETag", "Last-Modified" headers
1252+
String eTag = "W/\"" + size + "-" + date + "\"";
1253+
response.setHeader("ETag", eTag);
1254+
response.setHeader("Last-Modified", getDate(date)); //Sat, 23 Oct 2010 13:04:28 GMT
1255+
1256+
1257+
//Set "Cache-Control" to "no-cache". The no-cache response directive
1258+
//indicates that the response can be stored in caches, but the response
1259+
//must be validated with the server before each reuse.
1260+
response.setHeader("Cache-Control", "no-cache");
1261+
1262+
1263+
//Return 304/Not Modified response if we can...
1264+
String matchTag = request.getHeader("if-none-match");
1265+
String cacheControl = request.getHeader("cache-control");
1266+
if (matchTag==null) matchTag = "";
1267+
if (cacheControl==null) cacheControl = "";
1268+
if (cacheControl.equalsIgnoreCase("no-cache")==false){
1269+
if (eTag.equalsIgnoreCase(matchTag)){
1270+
//System.out.println("Sending 304 Response!");
1271+
response.setStatus(304);
1272+
return true;
1273+
}
1274+
else{
1275+
//Internet Explorer 6 uses "if-modified-since" instead of "if-none-match"
1276+
matchTag = request.getHeader("if-modified-since");
1277+
if (matchTag!=null){
1278+
for (String tag: matchTag.split(";")){
1279+
if (tag.trim().equalsIgnoreCase(response.getHeader("Last-Modified"))){
1280+
//System.out.println("Sending 304 Response!");
1281+
response.setStatus(304);
1282+
return true;
1283+
}
1284+
}
1285+
}
1286+
1287+
}
1288+
}
1289+
1290+
return false;
1291+
}
1292+
1293+
12261294
//**************************************************************************
12271295
//** reset
12281296
//**************************************************************************

0 commit comments

Comments
 (0)
2945
0