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
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import jakarta.servlet.http.HttpServletResponse;
import net.wotonomy.foundation.NSArray;
/**
* A pure java implementation of WOResponse.
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 905 $
*/
public class WOResponse extends WOMessage implements WOActionResults {
protected static String defaultEncoding;
private static SimpleDateFormat htmlDateFormat;
static {
defaultEncoding = "ISO8859_1";
htmlDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
htmlDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
}
private int status;
/**
* Parameterless constructor which should not be called.
*/
public WOResponse() {
status = -1; // -1 indicates not yet set
}
/**
* Sets the status code of the response. You should use the constants defined in
* HttpServletResponse.
*/
public void setStatus(int code) {
status = code;
}
/**
* Gets the current status code for the response.
*/
public int status() {
return status;
}
/**
* Sets a header in the response to disable client caching. (Whether this works
* depends on the client implementation.)
*/
public void disableClientCaching() {
String dateString = htmlDateFormat.format(new Date());
setHeader(dateString, "Date");
setHeader(dateString, "Expires");
setHeader("no-cache", "Pragma");
setHeaders(new NSArray(new Object[] { "private", "no-cache", "max-age = 0" }), "Cache-Control");
// System.out.println( "disableClientCaching: " + dateString );
}
/**
* Returns this object. (Implements the WOActionResults interface.)
*/
public WOResponse generateResponse() {
return this;
}
/**
* Generates the response using the specified servlet response, but does not
* flush the buffer. The caller is responsible for sending the response to the
* client. <br>
* <br>
* Note that this method is currently responsible for setting the content type
* to "text/html". As far as I can tell, WORequests have no way to set the
* content type and therefore must always be of type "text/html".
*/
void generateServletResponse(HttpServletResponse response) {
if (WOApplication.application().isPageRefreshOnBacktrackEnabled()) {
disableClientCaching();
}
String key;
java.util.Enumeration e, f;
// set content type: might be overwritten by headers below
response.setContentType("text/html");
// set status
if (status != -1) {
response.setStatus(status);
}
// set headers
f = _headers.allKeys().objectEnumerator();
while (f.hasMoreElements()) {
key = f.nextElement().toString();
e = ((NSArray) _headers.objectForKey(key)).objectEnumerator();
if (e.hasMoreElements()) {
// overwrite existing header
response.setHeader(key, e.nextElement().toString());
}
while (e.hasMoreElements()) {
response.addHeader(key, e.nextElement().toString());
}
}
// set cookies
e = _cookies.allValues().objectEnumerator();
while (e.hasMoreElements()) {
response.addCookie((WOCookie) e.nextElement());
}
try {
// write content
response.getOutputStream().write(_contentData.bytes());
if (status > 299) {
response.sendError(status);
}
} catch (Exception exc) {
throw new RuntimeException("Error writing response: " + exc);
}
}
/**
* Returns the current default encoding seting.
*/
public static String defaultEncoding() {
return defaultEncoding;
}
/**
* Sets the default encoding setting.
*/
public static void setDefaultEncoding(String encoding) {
defaultEncoding = encoding;
}
}
|