1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Psr\Servlet\Http\HttpServletResponseWrapper |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io-psr/servlet |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Psr\Servlet\Http; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Psr\HttpMessage\CookieInterface; |
24
|
|
|
use AppserverIo\Psr\Servlet\ServletResponseWrapper; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A Http servlet response implementation. |
28
|
|
|
* |
29
|
|
|
* @author Tim Wagner <[email protected]> |
30
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
32
|
|
|
* @link https://github.com/appserver-io-psr/servlet |
33
|
|
|
* @link http://www.appserver.io |
34
|
|
|
*/ |
35
|
|
|
class HttpServletResponseWrapper extends ServletResponseWrapper implements HttpServletResponseInterface |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Injects the passed response instance into this servlet response. |
40
|
|
|
* |
41
|
|
|
* @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $response The response instance used for initialization |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
1 |
|
public function injectHttpResponse(HttpServletResponseInterface $response) |
46
|
|
|
{ |
47
|
1 |
|
$this->injectResponse($response); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Adds a cookie. |
52
|
|
|
* |
53
|
|
|
* @param \AppserverIo\Psr\HttpMessage\CookieInterface $cookie The cookie instance to add |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function addCookie(CookieInterface $cookie) |
58
|
|
|
{ |
59
|
|
|
$this->getResponse()->addCookie($cookie); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns TRUE if the response already has a cookie with the passed |
64
|
|
|
* name, else FALSE. |
65
|
|
|
* |
66
|
|
|
* @param string $cookieName Name of the cookie to be checked |
67
|
|
|
* |
68
|
|
|
* @return boolean TRUE if the response already has the cookie, else FALSE |
69
|
|
|
*/ |
70
|
|
|
public function hasCookie($cookieName) |
71
|
|
|
{ |
72
|
|
|
return $this->getResponse()->hasCookie($cookieName); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the cookie with the a cookie |
77
|
|
|
* |
78
|
|
|
* @param string $cookieName Name of the cookie to be checked |
79
|
|
|
* |
80
|
|
|
* @return \AppserverIo\Psr\HttpMessage\CookieInterface $cookie The cookie instance |
81
|
|
|
*/ |
82
|
|
|
public function getCookie($cookieName) |
83
|
|
|
{ |
84
|
|
|
return $this->getResponse()->getCookie($cookieName); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Sets the headers |
89
|
|
|
* |
90
|
|
|
* @param array $headers The headers array |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
public function setHeaders(array $headers) |
95
|
|
|
{ |
96
|
|
|
$this->getResponse()->setHeaders($headers); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns the headers array |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function getHeaders() |
105
|
|
|
{ |
106
|
|
|
return $this->getResponse()->getHeaders(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Adds a header to array |
111
|
|
|
* |
112
|
|
|
* @param string $header The header label e.g. Accept or Content-Length |
113
|
|
|
* @param string|int $value The header value |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function addHeader($header, $value) |
118
|
|
|
{ |
119
|
|
|
$this->getResponse()->addHeader($header, $value); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns header info by given key |
124
|
|
|
* |
125
|
|
|
* @param string $key The headers key to return |
126
|
|
|
* |
127
|
|
|
* @return string|null |
128
|
|
|
*/ |
129
|
|
|
public function getHeader($key) |
130
|
|
|
{ |
131
|
|
|
return $this->getResponse()->getHeader($key); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the headers as string |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function getHeadersAsString() |
140
|
|
|
{ |
141
|
|
|
return $this->getResponse()->getHeadersAsString(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Removes one single header from the headers array. |
146
|
|
|
* |
147
|
|
|
* @param string $header The header to remove |
148
|
|
|
* |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
|
|
public function removeHeader($header) |
152
|
|
|
{ |
153
|
|
|
$this->getResponse()->removeHeader($header); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Checks if header exists by given name. |
158
|
|
|
* |
159
|
|
|
* @param string $name The header name to check |
160
|
|
|
* |
161
|
|
|
* @return boolean TRUE if the header is available, else FALSE |
162
|
|
|
*/ |
163
|
|
|
public function hasHeader($name) |
164
|
|
|
{ |
165
|
|
|
return $this->getResponse()->hasHeader($name); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns the response that will be send back to the client. |
170
|
|
|
* |
171
|
|
|
* @return \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface The response instance |
172
|
|
|
*/ |
173
|
1 |
|
public function getResponse() |
174
|
|
|
{ |
175
|
1 |
|
return $this->response; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Returns Http response code number only. |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
1 |
|
public function getStatusCode() |
184
|
|
|
{ |
185
|
1 |
|
return $this->getResponse()->getStatusCode(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Gets the response Reason-Phrase, a short textual description of the |
190
|
|
|
* Status-Code. |
191
|
|
|
* |
192
|
|
|
* Because a Reason-Phrase is not a required element in response |
193
|
|
|
* Status-Line, the Reason-Phrase value MAY be null. Implementations MAY |
194
|
|
|
* choose to return the default RFC 2616 recommended reason phrase for the |
195
|
|
|
* response's Status-Code. |
196
|
|
|
* |
197
|
|
|
* @return string|null Reason phrase, or null if unknown. |
198
|
|
|
*/ |
199
|
|
|
public function getStatusReasonPhrase() |
200
|
|
|
{ |
201
|
|
|
return $this->getResponse()->getStatusReasonPhrase(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Returns response Http version. |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function getVersion() |
210
|
|
|
{ |
211
|
|
|
return $this->getResponse()->getVersion(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Reset the body stream. |
216
|
|
|
* |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
public function resetBodyStream() |
220
|
|
|
{ |
221
|
|
|
$this->getResponse()->resetBodyStream(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Sets the HTTP response status code. |
226
|
|
|
* |
227
|
|
|
* @param integer $code The HTTP status code to set |
228
|
|
|
* |
229
|
|
|
* @return void |
230
|
|
|
*/ |
231
|
|
|
public function setStatusCode($code) |
232
|
|
|
{ |
233
|
|
|
$this->getResponse()->setStatusCode($code); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Appends the content. |
238
|
|
|
* |
239
|
|
|
* @param string $content The content to append |
240
|
|
|
* |
241
|
|
|
* @return void |
242
|
|
|
*/ |
243
|
|
|
public function appendBodyStream($content) |
244
|
|
|
{ |
245
|
|
|
$this->getResponse()->appendBodyStream($content); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Copies a source stream to body stream. |
250
|
|
|
* |
251
|
|
|
* @param resource $sourceStream The file pointer to source stream |
252
|
|
|
* @param integer $maxlength The max length to read from source stream |
253
|
|
|
* @param integer $offset The offset from source stream to read |
254
|
|
|
* |
255
|
|
|
* @return integer The total number of bytes copied |
256
|
|
|
*/ |
257
|
|
|
public function copyBodyStream($sourceStream, $maxlength = null, $offset = 0) |
258
|
|
|
{ |
259
|
|
|
return $this->getResponse()->copyBodyStream($sourceStream, $maxlength, $offset); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Returns the body content. |
264
|
|
|
* |
265
|
|
|
* @return string The boda content |
266
|
|
|
*/ |
267
|
|
|
public function getBodyContent() |
268
|
|
|
{ |
269
|
|
|
return $this->getResponse()->getBodyContent(); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Returns the body stream as a resource. |
274
|
|
|
* |
275
|
|
|
* @return resource The body stream |
276
|
|
|
*/ |
277
|
|
|
public function getBodyStream() |
278
|
|
|
{ |
279
|
|
|
return $this->getResponse()->getBodyStream(); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Returns the cookies. |
284
|
|
|
* |
285
|
|
|
* @return array The cookies |
286
|
|
|
*/ |
287
|
|
|
public function getCookies() |
288
|
|
|
{ |
289
|
|
|
return $this->getResponse()->getCookies(); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Returns the exception bound to the response. |
294
|
|
|
* |
295
|
|
|
* @return \Exception|null The exception |
296
|
|
|
*/ |
297
|
|
|
public function getException() |
298
|
|
|
{ |
299
|
|
|
return $this->getResponse()->getException(); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Returns the current state |
304
|
|
|
* |
305
|
|
|
* @return int |
306
|
|
|
*/ |
307
|
|
|
public function getState() |
308
|
|
|
{ |
309
|
|
|
return $this->getResponse()->getState(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Queries whether the response contains an exception or not. |
314
|
|
|
* |
315
|
|
|
* @return boolean TRUE if an exception has been attached, else FALSE |
316
|
|
|
*/ |
317
|
|
|
public function hasException() |
318
|
|
|
{ |
319
|
|
|
return $this->getResponse()->hasException(); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Compares current state with given state |
324
|
|
|
* |
325
|
|
|
* @param int $state The state to compare with |
326
|
|
|
* |
327
|
|
|
* @return bool Whether state is equal (true) or not (false) |
328
|
|
|
*/ |
329
|
|
|
public function hasState($state) |
330
|
|
|
{ |
331
|
|
|
return $this->getResponse()->hasState($state); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Redirects to the passed URL by adding a 'Location' header and |
336
|
|
|
* setting the appropriate status code, by default 301. |
337
|
|
|
* |
338
|
|
|
* @param string $url The URL to forward to |
339
|
|
|
* @param integer $code The status code to set |
340
|
|
|
* |
341
|
|
|
* @return void |
342
|
|
|
*/ |
343
|
|
|
public function redirect($url, $code = 301) |
344
|
|
|
{ |
345
|
|
|
$this->getResponse()->redirect($url, $code); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Resetss the stream resource pointing to body content. |
350
|
|
|
* |
351
|
|
|
* @param resource $bodyStream The body content stream resource |
352
|
|
|
* |
353
|
|
|
* @return void |
354
|
|
|
*/ |
355
|
|
|
public function setBodyStream($bodyStream) |
356
|
|
|
{ |
357
|
|
|
$this->getResponse()->setBodyStream($bodyStream); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Binds the exception to the response. |
362
|
|
|
* |
363
|
|
|
* @param \Exception $exception The exception to bind. |
364
|
|
|
* |
365
|
|
|
* @return void |
366
|
|
|
*/ |
367
|
|
|
public function setException(\Exception $exception) |
368
|
|
|
{ |
369
|
|
|
$this->getResponse()->setException($exception); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Sets state of response |
374
|
|
|
* |
375
|
|
|
* @param int $state The state value |
376
|
|
|
* |
377
|
|
|
* @return void |
378
|
|
|
*/ |
379
|
|
|
public function setState($state) |
380
|
|
|
{ |
381
|
|
|
$this->getResponse()->setState($state); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Sets the status reason phrase |
386
|
|
|
* |
387
|
|
|
* @param string $statusReasonPhrase The reason phrase |
388
|
|
|
* |
389
|
|
|
* @return void |
390
|
|
|
*/ |
391
|
|
|
public function setStatusReasonPhrase($statusReasonPhrase) |
392
|
|
|
{ |
393
|
|
|
$this->getResponse()->setStatusReasonPhrase($statusReasonPhrase); |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|