/** * Common function to handle exceptions in the data service. * * @param \Exception $exception exception * @param IService $service service * * @return void */ public static function handleException($exception, IService $service) { $acceptTypesText = $service->getHost()->getRequestAccept(); $responseContentType = null; try { $responseContentType = HttpProcessUtility::selectMimeType($acceptTypesText, array(MimeTypes::MIME_APPLICATION_XML, MimeTypes::MIME_APPLICATION_JSON)); } catch (HttpHeaderFailure $exception) { $exception = new ODataException($exception->getMessage(), $exception->getStatusCode()); } catch (\Exception $exception) { // Never come here } if (is_null($responseContentType)) { $responseContentType = MimeTypes::MIME_APPLICATION_XML; } if (!$exception instanceof ODataException) { $exception = new ODataException($exception->getMessage(), HttpStatus::CODE_INTERNAL_SERVER_ERROR); } $service->getHost()->setResponseVersion(ODataConstants::DATASERVICEVERSION_1_DOT_0 . ';'); // At this point all kind of exceptions will be converted //to 'ODataException' if ($exception->getStatusCode() == HttpStatus::CODE_NOT_MODIFIED) { $service->getHost()->setResponseStatusCode(HttpStatus::CODE_NOT_MODIFIED); } else { $service->getHost()->setResponseStatusCode($exception->getStatusCode()); $service->getHost()->setResponseContentType($responseContentType); $responseBody = null; if (strcasecmp($responseContentType, MimeTypes::MIME_APPLICATION_XML) == 0) { $responseBody = AtomODataWriter::serializeException($exception, true); } else { $responseBody = JsonODataV2Writer::serializeException($exception, true); } $service->getHost()->getOperationContext()->outgoingResponse()->setStream($responseBody); } }
public function __construct(array $hostInfo) { $this->_hostInfo = $hostInfo; $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER[ODataConstants::HTTPREQUEST_PROTOCOL] = $this->_hostInfo['AbsoluteRequestUri']->getScheme(); $_SERVER[HttpProcessUtility::headerToServerKey(ODataConstants::HTTPREQUEST_HEADER_HOST)] = $this->_hostInfo['AbsoluteRequestUri']->getHost() . ':' . $this->_hostInfo['AbsoluteRequestUri']->getPort(); $_SERVER[ODataConstants::HTTPREQUEST_URI] = $this->_hostInfo['AbsoluteRequestUri']->getPath(); if (array_key_exists('DataServiceVersion', $this->_hostInfo)) { $_SERVER[HttpProcessUtility::headerToServerKey(ODataConstants::HTTPREQUEST_HEADER_DATA_SERVICE_VERSION)] = $this->_hostInfo['DataServiceVersion']->toString(); } if (array_key_exists('MaxDataServiceVersion', $this->_hostInfo)) { $_SERVER[HttpProcessUtility::headerToServerKey(ODataConstants::HTTPREQUEST_HEADER_MAX_DATA_SERVICE_VERSION)] = $this->_hostInfo['MaxDataServiceVersion']->toString(); } if (array_key_exists('RequestIfMatch', $this->_hostInfo)) { $_SERVER[HttpProcessUtility::headerToServerKey(ODataConstants::HTTPREQUEST_HEADER_IF_MATCH)] = $this->_hostInfo['RequestIfMatch']; } if (array_key_exists('RequestIfNoneMatch', $this->_hostInfo)) { $_SERVER[HttpProcessUtility::headerToServerKey(ODataConstants::HTTPREQUEST_HEADER_IF_NONE)] = $this->_hostInfo['RequestIfNoneMatch']; } if (array_key_exists('QueryString', $this->_hostInfo)) { $_SERVER[ODataConstants::HTTPREQUEST_QUERY_STRING] = $this->_hostInfo['QueryString']; } //print_r($_SERVER); parent::__construct(); if (array_key_exists('AbsoluteServiceUri', $this->_hostInfo)) { $this->setServiceUri($this->_hostInfo['AbsoluteServiceUri']->getUrlAsString()); } }
public function testSelectMimeTypeMultipleValuesWithODataPartialSomeMatchesMissingODataPartSomeMatch() { $actual = HttpProcessUtility::selectMimeType(MimeTypes::MIME_APPLICATION_JSON_MINIMAL_META . ';q=1.0, ' . MimeTypes::MIME_APPLICATION_JSON_FULL_META . ';q=0.5', array(MimeTypes::MIME_APPLICATION_JSON, MimeTypes::MIME_APPLICATION_JSON_FULL_META)); $this->assertEquals(MimeTypes::MIME_APPLICATION_JSON_FULL_META, $actual); }
/** * get the specific request headers * * @param string $key The header name * * @return string|null value of the header, NULL if header is absent. */ public function getRequestHeader($key) { //PHP normalizes header keys $trimmedKey = HttpProcessUtility::headerToServerKey(trim($key)); // todo: lion // if (array_key_exists($trimmedKey, $this->_headers)) { if ($this->_headers != null && array_key_exists($trimmedKey, $this->_headers)) { return $this->_headers[$trimmedKey]; } return null; }
/** * Tests inlinecount */ function testInlineCountNone() { $_SERVER[ODataConstants::HTTPREQUEST_METHOD] = ODataConstants::HTTP_METHOD_GET; $_SERVER[ODataConstants::HTTPREQUEST_PROTOCOL] = ODataConstants::HTTPREQUEST_PROTOCOL_HTTP; $_SERVER[HttpProcessUtility::headerToServerKey(ODataConstants::HTTPREQUEST_HEADER_HOST)] = "localhost:8086"; $_SERVER[ODataConstants::HTTPREQUEST_URI] = "/NorthWind.svc/Customers"; $_SERVER[ODataConstants::HTTPREQUEST_QUERY_STRING] = '$inlinecount=none'; $dispatcher = new Dispatcher(); $dispatcher->dispatch(); $my_str = ob_get_contents(); ob_end_clean(); $this->assertStringStartsWith('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n" . '<feed', $my_str); $this->assertStringEndsWith('</feed>' . "\n", $my_str); }
function testSetResponseVersion() { $_SERVER[ODataConstants::HTTPREQUEST_METHOD] = ODataConstants::HTTP_METHOD_GET; $_SERVER[ODataConstants::HTTPREQUEST_PROTOCOL] = ODataConstants::HTTPREQUEST_PROTOCOL_HTTP; $_SERVER[HttpProcessUtility::headerToServerKey(ODataConstants::HTTPREQUEST_HEADER_HOST)] = "localhost:8086"; $_SERVER[ODataConstants::HTTPREQUEST_URI] = "/NorthWind.svc/Customers"; try { $exceptionThrown = false; $dispatcher = new dispatcher(); //Service dispatched //$dispatcher->dispatch(); $contents = ob_get_contents(); ob_end_clean(); $dispatcher->getHost()->setResponseVersion("2.0"); $headers =& $dispatcher->getHost()->getWebOperationContext()->OutgoingResponse()->getHeaders(); $this->assertEquals('2.0', $headers[ODataConstants::ODATAVERSIONHEADER]); } catch (\Exception $exception) { if (ob_get_length()) { ob_end_clean(); } $exceptionThrown = true; $this->fail("Some unexpected exception has been thrown:", $exception->getMessage()); } }