Author: Tim Wagner (tw@appserver.io)
Inheritance: implements AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface, implements AppserverIo\Psr\Context\ContextInterface
Beispiel #1
0
 /**
  * Tests has non existing parameter on HTTP request instance.
  *
  * @return void
  */
 public function testHasParameterWhenNonExists()
 {
     // initialize the mock HTTP request
     $mockRequest = $this->getMock('AppserverIo\\Http\\HttpRequest');
     $mockRequest->expects($this->any())->method('hasParam')->will($this->returnValue(false));
     // inject the mock HTTP request
     $this->request->injectHttpRequest($mockRequest);
     // check for an non existing parameter
     $this->assertFalse($this->request->hasParameter('unknown'));
 }
 /**
  * Process servlet request.
  *
  * @param \AppserverIo\Psr\HttpMessage\RequestInterface          $request        A request object
  * @param \AppserverIo\Psr\HttpMessage\ResponseInterface         $response       A response object
  * @param \AppserverIo\Server\Interfaces\RequestContextInterface $requestContext A requests context instance
  * @param integer                                                $hook           The current hook to process logic for
  *
  * @return boolean
  *
  * @throws \AppserverIo\Server\Exceptions\ModuleException
  */
 public function process(RequestInterface $request, ResponseInterface $response, RequestContextInterface $requestContext, $hook)
 {
     // if false hook is coming do nothing
     if (ModuleHooks::REQUEST_POST !== $hook) {
         return;
     }
     // check if we are the handler that has to process this request
     if ($requestContext->getServerVar(ServerVars::SERVER_HANDLER) !== $this->getModuleName()) {
         return;
     }
     // load the application associated with this request
     $application = $this->findRequestedApplication($requestContext);
     // check if the application has already been connected
     if ($application->isConnected() === false) {
         throw new \Exception(sprintf('Application %s has not connected yet', $application->getName()), 503);
     }
     // create a copy of the valve instances
     $valves = $this->valves;
     $handlers = $this->handlers;
     // create a new request instance from the HTTP request
     $servletRequest = new Request();
     $servletRequest->injectHandlers($handlers);
     $servletRequest->injectHttpRequest($request);
     $servletRequest->injectServerVars($requestContext->getServerVars());
     $servletRequest->init();
     // initialize the request handler instance
     $requestHandler = new RequestHandler();
     $requestHandler->injectValves($valves);
     $requestHandler->injectApplication($application);
     $requestHandler->injectRequest($servletRequest);
     $requestHandler->start(PTHREADS_INHERIT_NONE | PTHREADS_INHERIT_CONSTANTS);
     $requestHandler->join();
     // copy values to the HTTP response
     $requestHandler->copyToHttpResponse($response);
     // set response state to be dispatched after this without calling other modules process
     $response->setState(HttpResponseStates::DISPATCH);
 }
 /**
  * Test the getProvider() method.
  *
  * This test needs a real Request instance, because the interface
  * doesn't implement the getContext() method.
  *
  * @return void
  */
 public function testGetProvider()
 {
     // create a mock context instance
     $context = $this->getMockBuilder('AppserverIo\\Psr\\Naming\\NamingDirectoryInterface')->setMethods(get_class_methods('AppserverIo\\Psr\\Naming\\NamingDirectoryInterface'))->getMock();
     // mock the methods
     $context->expects($this->any())->method('search')->with('ProviderInterface')->will($this->returnValue($result = new \stdClass()));
     // create a servlet request instance
     $servletRequest = new Request();
     $servletRequest->injectContext($context);
     // initialize the controller
     $controller = new ControllerServlet();
     // check the session manager instance
     $this->assertEquals($result, $controller->getProvider($servletRequest));
 }
 /**
  * Process servlet request.
  *
  * @param \AppserverIo\Psr\HttpMessage\RequestInterface          $request        A request object
  * @param \AppserverIo\Psr\HttpMessage\ResponseInterface         $response       A response object
  * @param \AppserverIo\Server\Interfaces\RequestContextInterface $requestContext A requests context instance
  * @param integer                                                $hook           The current hook to process logic for
  *
  * @return boolean
  *
  * @throws \AppserverIo\Server\Exceptions\ModuleException
  */
 public function process(RequestInterface $request, ResponseInterface $response, RequestContextInterface $requestContext, $hook)
 {
     // if false hook is coming do nothing
     if (ModuleHooks::REQUEST_POST !== $hook) {
         return;
     }
     // check if we are the handler that has to process this request
     if ($requestContext->getServerVar(ServerVars::SERVER_HANDLER) !== $this->getModuleName()) {
         return;
     }
     // load the application associated with this request
     $application = $this->findRequestedApplication($requestContext);
     $application->registerClassLoaders();
     // check if the application has already been connected
     if ($application->isConnected() === false) {
         throw new \Exception(sprintf('Application %s has not connected yet', $application->getName()), 503);
     }
     // create a copy of the valve instances
     $valves = $this->valves;
     $handlers = $this->handlers;
     // create a new request instance from the HTTP request
     $servletRequest = new Request();
     $servletRequest->injectHandlers($handlers);
     $servletRequest->injectHttpRequest($request);
     $servletRequest->injectServerVars($requestContext->getServerVars());
     $servletRequest->init();
     // initialize servlet response
     $servletResponse = new Response();
     $servletResponse->init();
     // load the session and the authentication manager
     $sessionManager = $application->search(SessionManagerInterface::IDENTIFIER);
     $authenticationManager = $application->search(AuthenticationManagerInterface::IDENTIFIER);
     // inject the sapplication and servlet response
     $servletRequest->injectContext($application);
     $servletRequest->injectResponse($servletResponse);
     $servletRequest->injectSessionManager($sessionManager);
     $servletRequest->injectAuthenticationManager($authenticationManager);
     // prepare the request instance
     $servletRequest->prepare();
     // initialize static request and application context
     RequestHandler::$requestContext = $servletRequest;
     RequestHandler::$applicationContext = $application;
     // process the valves
     foreach ($valves as $valve) {
         $valve->invoke($servletRequest, $servletResponse);
         if ($servletRequest->isDispatched() === true) {
             break;
         }
     }
     // copy response values to the HTTP response
     $response->setState($servletResponse->getState());
     $response->setVersion($servletResponse->getVersion());
     $response->setStatusCode($servletResponse->getStatusCode());
     $response->setStatusReasonPhrase($servletResponse->getStatusReasonPhrase());
     // copy the body content to the HTTP response
     $response->appendBodyStream($servletResponse->getBodyStream());
     // copy headers to the HTTP response
     foreach ($servletResponse->getHeaders() as $headerName => $headerValue) {
         $response->addHeader($headerName, $headerValue);
     }
     // copy cookies to the HTTP response
     $response->setCookies($servletResponse->getCookies());
     // append the servlet engine's signature
     $response->addHeader(Protocol::HEADER_X_POWERED_BY, get_class($this), true);
     // set response state to be dispatched after this without calling other modules process
     $response->setState(HttpResponseStates::DISPATCH);
 }
 /**
  * Process servlet request.
  *
  * @param \AppserverIo\Psr\HttpMessage\RequestInterface          $request        A request object
  * @param \AppserverIo\Psr\HttpMessage\ResponseInterface         $response       A response object
  * @param \AppserverIo\Server\Interfaces\RequestContextInterface $requestContext A requests context instance
  * @param int                                                    $hook           The current hook to process logic for
  *
  * @return bool
  * @throws \AppserverIo\Server\Exceptions\ModuleException
  */
 public function process(RequestInterface $request, ResponseInterface $response, RequestContextInterface $requestContext, $hook)
 {
     try {
         // if false hook is coming do nothing
         if (ModuleHooks::REQUEST_POST !== $hook) {
             return;
         }
         // check if we are the handler that has to process this request
         if ($requestContext->getServerVar(ServerVars::SERVER_HANDLER) !== $this->getModuleName()) {
             return;
         }
         // initialize servlet session, request + response
         $servletRequest = new Request();
         $servletRequest->injectHttpRequest($request);
         $servletRequest->injectServerVars($requestContext->getServerVars());
         // initialize the parts
         foreach ($request->getParts() as $part) {
             $servletRequest->addPart(Part::fromHttpRequest($part));
         }
         // set the body content if we can find one
         if ($request->getHeader(HttpProtocol::HEADER_CONTENT_LENGTH) > 0) {
             $servletRequest->setBodyStream($request->getBodyContent());
         }
         // prepare the servlet request
         $this->prepareServletRequest($servletRequest);
         // initialize the servlet response with the Http response values
         $servletResponse = new Response();
         $servletRequest->injectResponse($servletResponse);
         // load the application associated with this request
         $application = $this->findRequestedApplication($requestContext);
         // prepare and set the applications context path
         $servletRequest->setContextPath($contextPath = '/' . $application->getName());
         $servletRequest->setServletPath(str_replace($contextPath, '', $servletRequest->getServletPath()));
         // prepare the base modifier which allows our apps to provide a base URL
         $webappsDir = $this->getServerContext()->getServerConfig()->getDocumentRoot();
         $relativeRequestPath = strstr($servletRequest->getServerVar(ServerVars::DOCUMENT_ROOT), $webappsDir);
         $proposedBaseModifier = str_replace($webappsDir, '', $relativeRequestPath);
         if (strpos($proposedBaseModifier, $contextPath) === 0) {
             $servletRequest->setBaseModifier('');
         } else {
             $servletRequest->setBaseModifier($contextPath);
         }
         // initialize the request handler instance
         $dispatched = false;
         $applicationName = $application->getName();
         while ($dispatched === false) {
             if ($this->requestHandlers[$applicationName][$i = rand(0, 9)]->isWaiting()) {
                 $this->requestHandlers[$applicationName][$i]->handleRequest($servletRequest, $servletResponse);
                 $dispatched = true;
                 break;
             }
         }
         // copy the values from the servlet response back to the HTTP response
         $response->setStatusCode($servletResponse->getStatusCode());
         $response->setStatusReasonPhrase($servletResponse->getStatusReasonPhrase());
         $response->setVersion($servletResponse->getVersion());
         $response->setState($servletResponse->getState());
         // append the content to the body stream
         $response->appendBodyStream($servletResponse->getBodyStream());
         // transform the servlet headers back into HTTP headers
         $headers = array();
         foreach ($servletResponse->getHeaders() as $name => $header) {
             $headers[$name] = $header;
         }
         // set the headers as array (because we don't know if we have to use the append flag)
         $response->setHeaders($headers);
         // copy the servlet response cookies back to the HTTP response
         foreach ($servletResponse->getCookies() as $cookie) {
             $response->addCookie(unserialize($cookie));
         }
         // set response state to be dispatched after this without calling other modules process
         $response->setState(HttpResponseStates::DISPATCH);
     } catch (ModuleException $me) {
         throw $me;
     } catch (\Exception $e) {
         throw new ModuleException($e, 500);
     }
 }