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 package org.apache.commons.httpclient.server;
33
34 import java.util.Iterator;
35
36 import org.apache.commons.httpclient.Header;
37 import org.apache.commons.httpclient.HeaderGroup;
38 import org.apache.commons.httpclient.HttpStatus;
39 import org.apache.commons.httpclient.HttpVersion;
40
41 /***
42 * A generic HTTP response.
43 *
44 * @author Christian Kohlschuetter
45 * @author Oleg Kalnichevski
46 */
47 public class SimpleResponse {
48
49 private String statusLine = "HTTP/1.0 200 OK";
50 private String contentType = "text/plain";
51 private String bodyString = null;
52 private HeaderGroup headers = new HeaderGroup();
53
54 public SimpleResponse() {
55 super();
56 }
57
58 public SimpleResponse(final String statusLine) {
59 super();
60 this.statusLine = statusLine;
61 }
62
63 public String getContentType() {
64 return this.contentType;
65 }
66
67 public void setContentType(String string) {
68 this.contentType = string;
69 }
70
71 public void setBodyString(String string) {
72 this.bodyString = string;
73 }
74
75 public String getBodyString() {
76 return this.bodyString;
77 }
78
79 public String getStatusLine() {
80 return this.statusLine;
81 }
82
83 public void setStatusLine(final String string) {
84 this.statusLine = string;
85 }
86
87 public void setStatusLine(final HttpVersion version, int statuscode) {
88 if (version == null) {
89 throw new IllegalArgumentException("HTTP version may not be null");
90 }
91 StringBuffer buffer = new StringBuffer();
92 buffer.append(version);
93 buffer.append(' ');
94 buffer.append(statuscode);
95 String statustext = HttpStatus.getStatusText(statuscode);
96 if (statustext != null) {
97 buffer.append(' ');
98 buffer.append(statustext);
99 }
100 this.statusLine = buffer.toString();
101 }
102
103 public boolean containsHeader(final String name) {
104 return this.headers.containsHeader(name);
105 }
106
107 public Header[] getHeaders() {
108 return this.headers.getAllHeaders();
109 }
110
111 public void setHeader(final Header header) {
112 if (header == null) {
113 return;
114 }
115 Header[] headers = this.headers.getHeaders(header.getName());
116 for (int i = 0; i < headers.length; i++) {
117 this.headers.removeHeader(headers[i]);
118 }
119 this.headers.addHeader(header);
120 }
121
122 public void addHeader(final Header header) {
123 if (header == null) {
124 return;
125 }
126 this.headers.addHeader(header);
127 }
128
129 public void setHeaders(final Header[] headers) {
130 if (headers == null) {
131 return;
132 }
133 this.headers.setHeaders(headers);
134 }
135
136 public Iterator getHeaderIterator() {
137 return this.headers.getIterator();
138 }
139 }