Пример #1
0
 /**
  * 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));
 }
Пример #2
0
 /**
  * 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);
 }
Пример #3
0
 /**
  * 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());
 }
Пример #4
0
 /**
  * Delegates to HTTP method specific functions like doPost() for POST e.g.
  *
  * @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)
 {
     try {
         // pre-initialize response
         $servletResponse->addHeader(HttpProtocol::HEADER_X_POWERED_BY, get_class($this));
         // load the the DI provider and session manager instance
         $provider = $this->getProvider($servletRequest);
         $sessionId = $servletRequest->getProposedSessionId();
         // load the path info from the servlet request
         $pathInfo = $servletRequest->getPathInfo();
         // if the requested action has been found in the path info
         if ($pathInfo == null) {
             $pathInfo = $this->getDefaultRoute();
         }
         // prepare the path of the requested action
         $requestedAction = $pathInfo;
         // load the routes
         $routes = $this->getRoutes();
         // load the action mappings for the actual servlet request
         $actionMappings = $this->getActionMappingsForServletRequest($servletRequest);
         // initialize the parameter map with the values from the request
         if ($servletRequest->getParameterMap()) {
             $parameterMap = $servletRequest->getParameterMap();
         } else {
             $parameterMap = array();
         }
         // iterate over the action mappings and try to find a mapping
         foreach ($actionMappings as $actionMapping) {
             // try to match actual request by the tokenizer
             if ($actionMapping->match($requestedAction)) {
                 // initialize the request attributes with the values from the action mapping
                 $servletRequest->setParameterMap(array_merge($parameterMap, $actionMapping->getRequestParameters()));
                 // resolve the action with the found mapping
                 $action = $routes[$actionMapping->getControllerName()];
                 // set the method that has to be invoked in the action context
                 $action->setAttribute(ContextKeys::METHOD_NAME, $actionMapping->getMethodName());
                 // inject the dependencies
                 $provider->injectDependencies($action, $sessionId);
                 // pre-dispatch the action
                 $action->preDispatch($servletRequest, $servletResponse);
                 // if the action has been dispatched, we're done
                 if ($servletRequest->isDispatched()) {
                     return;
                 }
                 // initialize the result with the default value
                 $result = ActionInterface::INPUT;
                 // if not dispatch the action
                 if ($newResult = $action->perform($servletRequest, $servletResponse)) {
                     $result = $newResult;
                 }
                 // process the result if available
                 if (($instance = $action->findResult($result)) instanceof ResultInterface) {
                     // query whether or not the result is action aware
                     if ($instance instanceof ActionAware) {
                         $instance->setAction($action);
                     }
                     // process the result
                     $instance->process($servletRequest, $servletResponse);
                 }
                 // post-dispatch the action instance
                 $action->postDispatch($servletRequest, $servletResponse);
                 // stop processing
                 return;
             }
         }
         // We did not find anything for this method/URI connection. We have to evaluate if there simply
         // is a method restriction. This replicates a lot of the checks we did before but omits extra
         // iterations in a positive dispatch event, 4xx's should be the exception and can handle that
         // penalty therefore
         if ($this->checkGeneralActionAvailability($pathInfo)) {
             // nothing found? Method must not be allowed then
             throw new DispatchException(sprintf('Method %s not allowed', $servletRequest->getMethod()), 405);
         }
         // throw an action, because we can't find an action mapping
         throw new DispatchException(sprintf('Can\'t find action to dispatch path info %s', $pathInfo), 404);
     } catch (DispatchException $de) {
         // results in a 4xx error
         throw new ServletException($de->__toString(), $de->getCode());
     } catch (\Exception $e) {
         // results in a 500 error page
         throw new ServletException($e->__toString(), 500);
     }
 }
Пример #5
0
 /**
  * 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()));
     }
 }