/** * 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 result and session-ID extract(parse_url($this->getResult())); // initialize the request URI if (isset($path)) { $servletRequest->setRequestUri($path); } // initialize the query string if (isset($query)) { $servletRequest->setQueryString($query); } // prepare the request with the new data $servletRequest->prepare(); // load the servlet path and session-ID $servletPath = $servletRequest->getServletPath(); $sessionId = $servletRequest->getProposedSessionId(); // load and process the servlet $servlet = $this->getServletContext()->lookup($servletPath, $sessionId); $servlet->service($servletRequest, $servletResponse); }
/** * 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()); }
/** * Returns the array with request method action -> route mappings * for the passed servlet request. * * @param \AppserverIo\Psr\Servlet\ServletRequestInterface $servletRequest The request instance * * @return array The request method action -> route mappings for the passed request method */ public function getActionMappingsForServletRequest(ServletRequestInterface $servletRequest) { // load the servlet request method $requestMethod = $servletRequest->getMethod(); // load the action mappings $actionMappings = $this->getActionMappings(); // query whether we've action mappings for the request method or not if (isset($actionMappings[$requestMethod])) { return $actionMappings[$requestMethod]; } // nothing found? Method must not be allowed then throw new DispatchException(sprintf('Method %s not allowed', $requestMethod), 405); }
/** * Delegation method for specific Http methods. * * @param \AppserverIo\Psr\Servlet\ServletRequestInterface $servletRequest The request instance * @param \AppserverIo\Psr\Servlet\ServletResponseInterface $servletResponse The response instance * * @return void * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request method is not available */ public function service(ServletRequestInterface $servletRequest, ServletResponseInterface $servletResponse) { // pre-initialize response $servletResponse->addHeader(Protocol::HEADER_X_POWERED_BY, get_class($this)); // check the request method to invoke the appropriate method switch ($servletRequest->getMethod()) { case Protocol::METHOD_CONNECT: $this->doConnect($servletRequest, $servletResponse); break; case Protocol::METHOD_DELETE: $this->doDelete($servletRequest, $servletResponse); break; case Protocol::METHOD_GET: $this->doGet($servletRequest, $servletResponse); break; case Protocol::METHOD_HEAD: $this->doHead($servletRequest, $servletResponse); break; case Protocol::METHOD_OPTIONS: $this->doOptions($servletRequest, $servletResponse); break; case Protocol::METHOD_POST: $this->doPost($servletRequest, $servletResponse); break; case Protocol::METHOD_PUT: $this->doPut($servletRequest, $servletResponse); break; case Protocol::METHOD_TRACE: $this->doTrace($servletRequest, $servletResponse); break; case Protocol::METHOD_PATCH: $this->doPatch($servletRequest, $servletResponse); break; default: throw new ServletException(sprintf('%s is not implemented yet.', $servletRequest->getMethod())); } }