summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOMessage.java
blob: f2310c24f480d3dc8dd41a26490cc1d033a08d78 (plain)
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
/*
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 net.wotonomy.foundation.NSArray;
import net.wotonomy.foundation.NSData;
import net.wotonomy.foundation.NSDictionary;
import net.wotonomy.foundation.NSMutableData;
import net.wotonomy.foundation.NSMutableDictionary;

/**
 * A pure java implementation of WOResponse.
 *
 * @author ezamudio@nasoft.com
 * @author $Author: cgruber $
 * @version $Revision: 905 $
 */
public class WOMessage {

	protected String _contentEncoding = "ISO8859_1";
	protected NSMutableDictionary _headers = new NSMutableDictionary();
	protected NSMutableDictionary _cookies = new NSMutableDictionary();
	private NSMutableDictionary _userInfo = new NSMutableDictionary();
	protected NSMutableData _contentData = new NSMutableData();

	public WOMessage() {
		super();
	}

	/**
	 * Sets the content encoding for the response.
	 */
	public void setContentEncoding(String encoding) {
		_contentEncoding = encoding;
	}

	/**
	 * Gets the current content encoding for the response.
	 */
	public String contentEncoding() {
		return _contentEncoding;
	}

	/**
	 * Sets the specified array of values as headers under the specified key.
	 */
	public void setHeaders(NSArray headerArray, String aKey) {
		_headers.setObjectForKey(headerArray, aKey);
	}

	/**
	 * Sets the specified header value for the specified key.
	 */
	public void setHeader(String aValue, String aKey) {
		_headers.setObjectForKey(new NSArray(aValue), aKey);
	}

	/**
	 * Returns an array of all the header keys that have been set in the response.
	 */
	public NSArray headerKeys() {
		return _headers.allKeys();
	}

	/**
	 * Returns an array of all the header values for the specified key, or null if
	 * the key does not exist.
	 */
	public NSArray headersForKey(String aKey) {
		return (NSArray) _headers.objectForKey(aKey);
	}

	/**
	 * Returns one header value for the specified key. Provided as a convenience,
	 * since most header keys will have a single value.
	 */
	public String headerForKey(String aKey) {
		NSArray values = (NSArray) _headers.objectForKey(aKey);
		if (values != null && values.count() > 0) {
			return values.objectAtIndex(0).toString();
		}
		return null;
	}

	/**
	 * Sets the content of the response to the bytes represented by the specified
	 * data object.
	 */
	public void setContent(NSData aData) {
		_contentData.setData(aData);
		setHeader(Integer.toString(aData.length()), "content-length");
	}

	/**
	 * Retrieves the current content of the response.
	 */
	public NSData content() {
		return _contentData;
	}

	/**
	 * Sets the current user info dictionary. These values are for
	 * application-specific uses and are available to other actions and components
	 * in the request-response cycle.
	 */
	public void setUserInfo(NSDictionary aDict) {
		_userInfo = new NSMutableDictionary(aDict);
	}

	/**
	 * Gets the current user info dictionary. These values are for
	 * application-specific uses are are available to other actions and components
	 * in the request-response cycle.
	 */
	public NSDictionary userInfo() {
		return new NSDictionary(_userInfo);
	}

	/**
	 * Appends the bytes in the specified data object to the response.
	 */
	public void appendContentData(NSData aData) {
		_contentData.appendData(aData);
		setHeader(Integer.toString(_contentData.length()), "content-length");
	}

	/**
	 * Appends the specified byte to the response.
	 */
	public void appendContentCharacter(char character) {
		_contentData.appendByte((byte) character);
		setHeader(Integer.toString(_contentData.length()), "content-length");
	}

	/**
	 * Appends the specified string to the response. Any special characters will not
	 * be escaped. The string will be encoded in the current content encoding.
	 */
	public void appendContentString(String aString) {
		_contentData.appendData(new NSData(aString.getBytes()));
		setHeader(Integer.toString(_contentData.length()), "content-length");
	}

	/**
	 * Appends the specified string containing HTML to the response. Any special
	 * characters will be escaped appropriately. The string will be encoded in the
	 * current content encoding.
	 */
	public void appendContentHTMLString(String aString) {
		_contentData.appendData(new NSData(stringByEscapingHTMLString(aString).getBytes()));
		setHeader(Integer.toString(_contentData.length()), "content-length");
	}

	/**
	 * Appends the specified string containing HTML to the response. Any special
	 * characters will be escaped appropriately. This method escapes tabs and
	 * new-line characters as well. The string will be encoded in the current
	 * content encoding.
	 */
	public void appendContentHTMLAttributeValue(String aString) {
		_contentData.appendData(new NSData(stringByEscapingHTMLAttributeValue(aString).getBytes()));
		setHeader(Integer.toString(_contentData.length()), "content-length");
	}

	/**
	 * Adds the specified cookie to the response.
	 */
	public void addCookie(WOCookie aCookie) {
		_cookies.setObjectForKey(aCookie, aCookie.name());
	}

	/**
	 * Removes the specified cookie from the response.
	 */
	public void removeCookie(WOCookie aCookie) {
		_cookies.removeObjectForKey(aCookie.name());
	}

	/**
	 * Returns an array of cookies currently being sent with the response. Contains
	 * whatever cookies have previously been set in this response.
	 */
	public NSArray cookies() {
		return _cookies.allValues();
	}

	/**
	 * Sets the HTTP version header in the response.
	 */
	public void setHTTPVersion(String aString) {
		setHeader(aString, "Protocol");
	}

	/**
	 * Gets the current HTTP version header for the response. Because servlet
	 * responses do not allow read access to headers, this method returns null if
	 * setHTTPVersion has not been called.
	 */
	public String httpVersion() {
		return headerForKey("Protocol");
	}

	/**
	 * Returns a sting containing the contents of the specified string after
	 * escaping all special HTML characters.
	 */
	public static String stringByEscapingHTMLString(String aString) {
		int len = aString.length();
		StringBuffer result = new StringBuffer();
		char[] buf = new char[len];
		aString.getChars(0, len, buf, 0);
		for (int i = 0; i < len; i++) {
			if (buf[i] == '&') {
				result.append("&amp;");
			} else if (buf[i] == '\\') {
				result.append("&quot;");
			} else if (buf[i] == '<') {
				result.append("&lt;");
			} else if (buf[i] == '>') {
				result.append("&gt;");
			} else {
				result.append(buf[i]);
			}
		}
		return result.toString();
	}

	/**
	 * Returns a sting containing the contents of the specified string after
	 * escaping all special HTML characters. This method escapes tabs and new-line
	 * characters as well.
	 */
	public static String stringByEscapingHTMLAttributeValue(String aString) {
		int len = aString.length();
		StringBuffer result = new StringBuffer();
		char[] buf = new char[len];
		aString.getChars(0, len, buf, 0);
		for (int i = 0; i < len; i++) {
			if (buf[i] == '&') {
				result.append("&amp;");
			} else if (buf[i] == '\\') {
				result.append("&quot;");
			} else if (buf[i] == '<') {
				result.append("&lt;");
			} else if (buf[i] == '>') {
				result.append("&gt;");
			} else if (buf[i] == '\t') {
				result.append("&#9;");
			} else if (buf[i] == '\n') {
				result.append("&#10;");
			} else if (buf[i] == '\r') {
				result.append("&#13;");
			} else {
				result.append(buf[i]);
			}
		}
		return result.toString();
	}

}