01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.http;
04:
05: import java.nio.ByteBuffer;
06:
07: public class SimpleResponse extends Response {
08: private byte[] content = new byte[0];
09:
10: public SimpleResponse() {
11: }
12:
13: public SimpleResponse(int status) {
14: super (status);
15: }
16:
17: public void readyToSend(ResponseSender sender) throws Exception {
18: byte[] bytes = getBytes();
19: sender.send(bytes);
20: sender.close();
21: }
22:
23: public void setContent(String value) throws Exception {
24: content = getEncodedBytes(value);
25: }
26:
27: public void setContent(byte[] value) {
28: content = value;
29: }
30:
31: public String getContent() {
32: return new String(content);
33: }
34:
35: public byte[] getContentBytes() {
36: return content;
37: }
38:
39: public String getText() {
40: return new String(getBytes());
41: }
42:
43: public byte[] getBytes() {
44: addStandardHeaders();
45: byte[] headerBytes = makeHttpHeaders().getBytes();
46: ByteBuffer bytes = ByteBuffer.allocate(headerBytes.length
47: + getContentSize());
48: bytes.put(headerBytes).put(content);
49: return bytes.array();
50: }
51:
52: public int getContentSize() {
53: return content.length;
54: }
55:
56: protected void addSpecificHeaders() {
57: addHeader("Content-Length", String.valueOf(getContentSize()));
58: }
59: }
|