File Source for Response.php

# Source for file Response.php

Documentation is available at Response.php

1. <?php
2. /**
3. * Copyright (c) 2007-2011, Servigistics, Inc.
4. * All rights reserved.
5. *
6. * Redistribution and use in source and binary forms, with or without
7. * modification, are permitted provided that the following conditions are met:
8. *
9. * - Redistributions of source code must retain the above copyright notice,
10. * this list of conditions and the following disclaimer.
11. * - Redistributions in binary form must reproduce the above copyright
12. * notice, this list of conditions and the following disclaimer in the
13. * documentation and/or other materials provided with the distribution.
14. * - Neither the name of Servigistics, Inc. nor the names of
15. * its contributors may be used to endorse or promote products derived from
16. * this software without specific prior written permission.
17. *
18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28. * POSSIBILITY OF SUCH DAMAGE.
29. *
30. * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
31. * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
32. * @version $Id: $
33. *
34. * @package Apache
35. * @subpackage Solr
36. * @author Donovan Jimenez <djimenez@conduit-it.com>
37. */
39. /**
40. * Represents the required pieces of an HTTP response provided by HTTP transport
41. * implementations and then consumed by the Apache_Solr_Response class which provides
42. * decoding
43. */
44. class Apache_Solr_HttpTransport_Response
45. {
46. /**
47. * Status Messages indexed by Status Code
48. * Obtained from: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
49. *
50. * @var array
51. */
52. static private $_defaultStatusMessages = array(
53. // Specific to PHP Solr Client
54. 0 => "Communication Error",
56. // Informational 1XX
57. 100 => "Continue",
58. 101 => "Switching Protocols",
60. // Successful 2XX
61. 200 => "OK",
62. 201 => "Created",
63. 202 => "Accepted",
64. 203 => "Non-Authoritative Information",
65. 204 => "No Content",
66. 205 => "Reset Content",
67. 206 => "Partial Content",
69. // Redirection 3XX
70. 300 => "Multiple Choices",
71. 301 => "Moved Permanently",
72. 302 => "Found",
73. 303 => "See Other",
74. 304 => "Not Modified",
75. 305 => "Use Proxy",
76. 307 => "Temporary Redirect",
78. // Client Error 4XX
79. 400 => "Bad Request",
80. 401 => "Unauthorized",
81. 402 => "Payment Required",
82. 403 => "Forbidden",
83. 404 => "Not Found",
84. 405 => "Method Not Allowed",
85. 406 => "Not Acceptable",
86. 407 => "Proxy Authentication Required",
87. 408 => "Request Timeout",
88. 409 => "Conflict",
89. 410 => "Gone",
90. 411 => "Length Required",
91. 412 => "Precondition Failed",
92. 413 => "Request Entity Too Large",
93. 414 => "Request-URI Too Long",
94. 415 => "Unsupported Media Type",
95. 416 => "Request Range Not Satisfiable",
96. 417 => "Expectation Failed",
98. // Server Error 5XX
99. 500 => "Internal Server Error",
100. 501 => "Not Implemented",
101. 502 => "Bad Gateway",
102. 503 => "Service Unavailable",
103. 504 => "Gateway Timeout",
104. 505 => "HTTP Version Not Supported"
105. );
107. /**
108. * Get the HTTP status message based on status code
109. *
110. * @return string
111. */
112. public static function getDefaultStatusMessage($statusCode)
113. {
114. $statusCode = (int) $statusCode;
116. if (isset(self::$_defaultStatusMessages[$statusCode]))
117. {
118. return self::$_defaultStatusMessages[$statusCode];
119. }
121. return "Unknown Status";
122. }
124. /**
125. * The response's HTTP status code
126. *
127. * @var integer
128. */
129. private $_statusCode;
131. /**
132. * The response's HTTP status message
133. *
134. * @var string
135. */
136. private $_statusMessage;
138. /**
139. * The response's mime type
140. *
141. * @var string
142. */
143. private $_mimeType;
145. /**
146. * The response's character encoding
147. *
148. * @var string
149. */
150. private $_encoding;
152. /**
153. * The response's data
154. *
155. * @var string
156. */
157. private $_responseBody;
159. /**
160. * Construct a HTTP transport response
161. *
162. * @param integer $statusCode The HTTP status code
163. * @param string $contentType The VALUE of the Content-Type HTTP header
164. * @param string $responseBody The body of the HTTP response
165. */
166. public function __construct($statusCode, $contentType, $responseBody)
167. {
168. // set the status code, make sure its an integer
169. $this->_statusCode = (int) $statusCode;
171. // lookup up status message based on code
172. $this->_statusMessage = self:: getDefaultStatusMessage($this->_statusCode);
174. // set the response body, it should always be a string
175. $this->_responseBody = (string) $responseBody;
177. // parse the content type header value for mimetype and encoding
178. // first set default values that will remain if we can't find
179. // what we're looking for in the content type
180. $this->_mimeType = "text/plain";
181. $this->_encoding = "UTF-8";
183. if ($contentType)
184. {
185. // now break apart the header to see if there's character encoding
186. $contentTypeParts = explode(';', $contentType, 2);
188. if (isset($contentTypeParts[0]))
189. {
190. $this->_mimeType = trim($contentTypeParts[0]);
191. }
193. if (isset($contentTypeParts[1]))
194. {
195. // we have a second part, split it further
196. $contentTypeParts = explode('=', $contentTypeParts[1]);
198. if (isset($contentTypeParts[1]))
199. {
200. $this->_encoding = trim($contentTypeParts[1]);
201. }
202. }
203. }
204. }
206. /**
207. * Get the status code of the response
208. *
209. * @return integer
210. */
211. public function getStatusCode()
212. {
213. return $this->_statusCode;
214. }
216. /**
217. * Get the status message of the response
218. *
219. * @return string
220. */
221. public function getStatusMessage()
222. {
223. return $this->_statusMessage;
224. }
226. /**
227. * Get the mimetype of the response body
228. *
229. * @return string
230. */
231. public function getMimeType()
232. {
233. return $this->_mimeType;
234. }
236. /**
237. * Get the charset encoding of the response body.
238. *
239. * @return string
240. */
241. public function getEncoding()
242. {
243. return $this->_encoding;
244. }
246. /**
247. * Get the raw response body
248. *
249. * @return string
250. */
251. public function getBody()
252. {
253. return $this->_responseBody;
254. }
255. }

Documentation generated on Wed, 04 May 2011 11:01:16 -0400 by phpDocumentor 1.4.3