/**
* 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);
}
}