2 /* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3 and is published under the AGPL license. */
5 class Syj_Controller_Action_Helper_SyjApi extends Zend_Controller_Action_Helper_Abstract
7 protected $_contentType = 'text/plain';
8 protected $_checkIfNoneMatch = false;
10 protected $_code = 200;
12 public function init() {
13 $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
14 $viewRenderer->setNoRender();
15 $layout = Zend_Layout::getMvcInstance();
16 if (null !== $layout) {
17 $layout->disableLayout();
21 public function setContentType($contentType) {
22 $this->_contentType = (string)$contentType;
26 public function getContentType() {
27 $this->_contentType = $contentType;
30 public function setBody($body) {
31 $this->_body = (string)$body;
35 public function getBody() {
39 public function setCode($code) {
40 $this->_code = (int)$code;
44 public function getCode() {
48 public function setCheckIfNoneMatch($check) {
49 $this->_checkIfNoneMatch = (boolean)$check;
53 public function getCheckIfNoneMatch() {
54 return $this->_checkIfNoneMatch;
57 public function postDispatch() {
58 $response = $this->getResponse();
60 $response->setHeader('Content-Type', $this->_contentType)
61 ->setHeader('Content-Length', strlen($this->_body));
63 if ($this->_checkIfNoneMatch) {
64 $request = $this->getRequest();
66 $etag = md5 ($this->_body);
67 if ($request->getServer("HTTP_IF_NONE_MATCH") == $etag) {
68 $response->setHttpResponseCode(304);
72 // no-cache is needed otherwise IE does not try to get new version.
73 $response->setHeader ('Cache-control', 'no-cache, must-revalidate');
74 $response->setHeader ('Etag', $etag);
77 $response->setHttpResponseCode($this->_code)
78 ->setBody($this->_body);