protected function _beforeTerminate(KDispatcherContextInterface $context)
    {
        $response = $context->getResponse();

        //Pass back to Joomla
        if(!$response->isRedirect() && !$response->isDownloadable() && $this->getDecorator() == 'joomla')
        {
            // #237 - Since Joomla will wrap the page we do not know the actual content-length and should not send it
            $response->getHeaders()->remove('Content-length');
            header_remove('Content-Length');

            //Contenttype
            JFactory::getDocument()->setMimeEncoding($response->getContentType());

            //Set messages for any request method
            $messages = $response->getMessages();
            foreach($messages as $type => $group)
            {
                if ($type === 'success') {
                    $type = 'message';
                }

                foreach($group as $message) {
                    JFactory::getApplication()->enqueueMessage($message, $type);
                }
            }

            //Set the cache state
            JFactory::getApplication()->allowCache($context->getRequest()->isCacheable());

            //Do not flush the response
            return false;
        }
    }