/**
  * Processes the request by invoking the request handler that executes the servlet
  * in a protected context.
  *
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
  *
  * @return void
  */
 public function invoke(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     try {
         // unpack the remote method call
         $remoteMethod = RemoteMethodProtocol::unpack($servletRequest->getBodyContent());
         // load the application context
         /** @var \AppserverIo\Appserver\Application\Application $application */
         $application = $servletRequest->getContext();
         // prepare method name and parameters and invoke method
         $className = $remoteMethod->getClassName();
         $methodName = $remoteMethod->getMethodName();
         $parameters = $remoteMethod->getParameters();
         $sessionId = $remoteMethod->getSessionId();
         // load the bean manager and the bean instance
         $instance = $application->search($className, array($sessionId, array($application)));
         // invoke the remote method call on the local instance
         $response = call_user_func_array(array($instance, $methodName), $parameters);
         // serialize the remote method and write it to the socket
         $servletResponse->appendBodyStream(RemoteMethodProtocol::pack($response));
         // re-attach the bean instance in the container and unlock it
         $application->search('BeanContextInterface')->attach($instance, $sessionId);
     } catch (\Exception $e) {
         // catch the exception and append it to the body stream
         $servletResponse->appendBodyStream(RemoteMethodProtocol::pack(RemoteExceptionWrapper::factory($e)));
     }
     // finally dispatch this request, because we have finished processing it
     $servletRequest->setDispatched(true);
 }
 /**
  * Processes this valve (authenticate this request if necessary).
  *
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
  *
  * @return void
  */
 public function invoke(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     // load the authentication manager
     /** @var \AppserverIo\Appserver\ServletEngine\Authentication\AuthenticationManagerInterface $authenticationManager */
     $authenticationManager = $servletRequest->getContext()->search('AuthenticationManagerInterface');
     // authenticate the request
     if ($authenticationManager->handleRequest($servletRequest, $servletResponse) === false) {
         // throw exception for auth required
         throw new ModuleException(null, 401);
     }
 }
Esempio n. 3
0
 /**
  * Processes the request by invoking the request handler that executes the servlet
  * in a protected context.
  *
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
  *
  * @return void
  */
 public function invoke(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     // load the servlet manager
     /** @var \AppserverIo\Psr\Servlet\ServletContextInterface|\AppserverIo\Psr\Application\ManagerInterface $servletManager */
     $servletManager = $servletRequest->getContext()->search('ServletContextInterface');
     // locate and service the servlet
     $servlet = $servletManager->locate($servletRequest);
     $servlet->service($servletRequest, $servletResponse);
     // finally invoke the destroy() method
     $servlet->destroy();
     // dispatch this request, because we have finished processing it
     $servletRequest->setDispatched(true);
 }
 /**
  * Processes the request by invoking the request handler that attaches the message to the
  * requested queue in a protected context.
  *
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
  *
  * @return void
  * @throws \Exception Is thrown if the requested message queue is not available
  */
 public function invoke(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     // load the application context
     /** @var \AppserverIo\Appserver\Application\Application $application */
     $application = $servletRequest->getContext();
     // unpack the message
     $message = MessageQueueProtocol::unpack($servletRequest->getBodyContent());
     // load message queue name
     $queueName = $message->getDestination()->getName();
     // lookup the message queue manager and attach the message
     $queueManager = $application->search('QueueContextInterface');
     if ($messageQueue = $queueManager->lookup($queueName)) {
         $messageQueue->attach($message);
     } else {
         throw new \Exception("Can\\'t find queue for message queue {$queueName}");
     }
     // finally dispatch this request, because we have finished processing it
     $servletRequest->setDispatched(true);
 }
 /**
  * Processes the request by invoking the request handler that executes the servlet
  * in a protected context.
  *
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
  *
  * @return void
  */
 public function invoke(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     try {
         // unpack the remote method call
         $remoteMethod = RemoteMethodProtocol::unpack($servletRequest->getBodyContent());
         // load the application context
         /** @var \AppserverIo\Appserver\Application\Application $application */
         $application = $servletRequest->getContext();
         // invoke the remote method and re-attach the bean instance to the container
         $response = $application->search(BeanContextInterface::IDENTIFIER)->invoke($remoteMethod, new ArrayList());
         // serialize the remote method and write it to the socket
         $servletResponse->appendBodyStream(RemoteMethodProtocol::pack($response));
     } catch (\Exception $e) {
         // catch the exception and append it to the body stream
         $servletResponse->appendBodyStream(RemoteMethodProtocol::pack(RemoteExceptionWrapper::factory($e)));
     }
     // finally dispatch this request, because we have finished processing it
     $servletRequest->setDispatched(true);
 }
Esempio n. 6
0
 /**
  * This method finally handles all PHP and user errors as well as the exceptions that
  * have been thrown through the servlet processing.
  *
  * @param \AppserverIo\Appserver\ServletEngine\RequestHandler        $requestHandler  The request handler instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The actual request instance
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The actual request instance
  *
  * @return void
  */
 public function handleErrors(RequestHandler $requestHandler, HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
 {
     // return immediately if we don't have any errors
     if (sizeof($errors = $requestHandler->getErrors()) === 0) {
         return;
     }
     // iterate over the errors to process each of them
     foreach ($errors as $error) {
         // prepare the error message
         $message = $this->prepareMessage($error);
         // query whether or not we have to log the error
         if (Boolean::valueOf(new String(ini_get('log_errors')))->booleanValue()) {
             // create a local copy of the application
             if ($application = $servletRequest->getContext()) {
                 // try to load the system logger from the application
                 if ($systemLogger = $application->getLogger(LoggerUtils::SYSTEM)) {
                     $systemLogger->log($this->mapLogLevel($error), $message);
                 }
             }
         }
         // query whether or not, the error has an status code
         if ($statusCode = $error->getStatusCode()) {
             $servletResponse->setStatusCode($statusCode);
         }
     }
     // we add the error to the servlet request
     $servletRequest->setAttribute(RequestHandlerKeys::ERROR_MESSAGES, $errors);
     // we append the the errors to the body stream if display_errors is on
     if (Boolean::valueOf(new String(ini_get('display_errors')))->booleanValue()) {
         $servletResponse->appendBodyStream(implode('<br/>', $errors));
     }
     // query whether or not we've a client or an server error
     if ($servletResponse->getStatusCode() > 399) {
         try {
             // create a local copy of the application
             $application = $servletRequest->getContext();
             // inject the application and servlet response
             $servletRequest->injectResponse($servletResponse);
             $servletRequest->injectContext($application);
             // load the servlet context instance
             $servletManager = $application->search(ServletContextInterface::IDENTIFIER);
             // initialize the request URI for the error page to be rendered
             $requestUri = null;
             // iterate over the configured error pages to find a matching one
             foreach ($servletManager->getErrorPages() as $errorCodePattern => $errorPage) {
                 // query whether or not we found an error page configured for the actual status code
                 if (fnmatch($errorCodePattern, $servletResponse->getStatusCode())) {
                     $requestUri = $errorPage;
                     break;
                 }
             }
             // query whether or not we've an found a configured error page
             if ($requestUri == null) {
                 throw new ServletException(sprintf('Please configure an error page for status code %s', $servletResponse->getStatusCode()));
             }
             // initialize the request URI
             $servletRequest->setRequestUri($requestUri);
             // prepare the request with the new data
             $servletRequest->prepare();
             // reset the body stream to remove content, that has already been appended
             $servletResponse->resetBodyStream();
             // load the servlet path and session-ID
             $servletPath = $servletRequest->getServletPath();
             $sessionId = $servletRequest->getProposedSessionId();
             // load and process the servlet
             $servlet = $servletManager->lookup($servletPath, $sessionId);
             $servlet->service($servletRequest, $servletResponse);
         } catch (\Exception $e) {
             // finally log the exception
             $application->getInitialContext()->getSystemLogger()->critical($e->__toString());
             // append the exception message to the body stream
             $servletResponse->appendBodyStream($e->__toString());
         }
     }
 }
 /**
  * Prepares the passed request instance for generating the globals.
  *
  * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The request instance
  *
  * @return void
  */
 protected function prepareGlobals(HttpServletRequestInterface $servletRequest)
 {
     // load the requested script name
     $scriptName = basename($servletRequest->getServerVar('SCRIPT_NAME'));
     // if the application has not been called over a vhost configuration append application folder name
     if ($servletRequest->getContext()->isVhostOf($servletRequest->getServerName()) === false) {
         $scriptName = $servletRequest->getContextPath() . DIRECTORY_SEPARATOR . $scriptName;
     }
     // initialize the server variables
     $this->serverVars['PHP_SELF'] = $scriptName;
     // ATTENTION: This is necessary because of a Magento bug!!!!
     $this->serverVars['SERVER_PORT'] = null;
 }