/** * Processes an action result by dispatching the configured servlet. * * @param \AppserverIo\Psr\Servlet\ServletRequestInterface $servletRequest The request instance * @param \AppserverIo\Psr\Servlet\ServletResponseInterface $servletResponse The response sent back to the client * * @return void */ public function process(ServletRequestInterface $servletRequest, ServletResponseInterface $servletResponse) { // load the action instance $action = $this->getAction(); // query whether the action contains errors or not if ($action instanceof ValidationAware && $action->hasErrors()) { $content = $action->getErrors(); } else { $content = $servletRequest->getAttribute(JsonResult::DATA); } // add the header for the JSON content type $servletResponse->addHeader(HttpProtocol::HEADER_CONTENT_TYPE, 'application/json'); // append the JSON encoded content to the servlet response $servletResponse->appendBodyStream(json_encode($content)); }
/** * Processes an action result by dispatching the configured servlet. * * @param \AppserverIo\Psr\Servlet\ServletRequestInterface $servletRequest The request instance * @param \AppserverIo\Psr\Servlet\ServletResponseInterface $servletResponse The response sent back to the client * * @return void */ public function process(ServletRequestInterface $servletRequest, ServletResponseInterface $servletResponse) { // load the action instance $action = $this->getAction(); // add the actions default headers to the response if ($action instanceof DefaultHeadersAware && $action->hasDefaultHeaders()) { foreach ($action->getDefaultHeaders() as $name => $value) { $servletResponse->addHeader($name, $value); } } // query whether the action contains errors or not if ($action instanceof ValidationAware && $action->hasErrors()) { $bodyContent = $action->getErrors(); } else { $bodyContent = $action->getAttribute($this->getResult()); } // query whether the action requires content encoding or not if ($action instanceof EncodingAware && !empty($bodyContent)) { $bodyContent = $action->encode($bodyContent); } // set the encoded body content $servletResponse->appendBodyStream($bodyContent); }
/** * Processes the DHTML file specified as servlet name. * * @param \AppserverIo\Psr\Servlet\ServletRequestInterface $servletRequest The request instance * @param \AppserverIo\Psr\Servlet\ServletResponseInterface $servletResponse The response sent back to the client * * @return void * * @throws \AppserverIo\Psr\Servlet\ServletException If no action has been found for the requested path */ public function service(ServletRequestInterface $servletRequest, ServletResponseInterface $servletResponse) { // pre-initialize the X-POWERED-BY header $poweredBy = $this->getPoweredBy(); // append an existing X-POWERED-BY header if available if ($servletResponse->hasHeader(HttpProtocol::HEADER_X_POWERED_BY)) { $poweredBy = $servletResponse->getHeader(HttpProtocol::HEADER_X_POWERED_BY) . ', ' . $poweredBy; } // set the X-POWERED-BY header $servletResponse->addHeader(HttpProtocol::HEADER_X_POWERED_BY, $poweredBy); // servlet path === relative path to the template name $template = $servletRequest->getServletPath(); // check if the template is available if (file_exists($pathToTemplate = $this->getWebappPath() . $template) === false) { throw new ServletException(sprintf('Requested template \'%s\' is not available', $template)); } // process the template ob_start(); require $pathToTemplate; // add the servlet name to the response $servletResponse->appendBodyStream(ob_get_clean()); }