/**
  * Process the given request Uri and creates an instance of 
  * RequestDescription from the processed uri.
  * 
  * @param Url         &$absoluteRequestUri The absolute request uri.
  * @param DataService &$dataService        Reference to the data service
  *                                         instance.
  * 
  * @return RequestDescription
  * 
  * @throws ODataException If any exception occurs while processing the segments
  *                        or incase of any version incompatibility.
  */
 public static function process(Url &$absoluteRequestUri, DataService &$dataService)
 {
     $absoluteRequestUri = $dataService->getHost()->getAbsoluteRequestUri();
     $absoluteServiceUri = $dataService->getHost()->getAbsoluteServiceUri();
     $requestUriSegments = array_slice($absoluteRequestUri->getSegments(), $absoluteServiceUri->getSegmentCount());
     $segmentDescriptors = null;
     try {
         $segmentDescriptors = SegmentParser::parseRequestUriSegements($requestUriSegments, $dataService->getMetadataQueryProviderWrapper(), true);
     } catch (ODataException $odataException) {
         throw $odataException;
     }
     $requestDescription = new RequestDescription($segmentDescriptors, $absoluteRequestUri);
     $requestTargetKind = $requestDescription->getTargetKind();
     if ($requestTargetKind != RequestTargetKind::METADATA && $requestTargetKind != RequestTargetKind::BATCH && $requestTargetKind != RequestTargetKind::SERVICE_DIRECTORY) {
         if ($requestTargetKind != RequestTargetKind::PRIMITIVE_VALUE && $requestTargetKind != RequestTargetKind::MEDIA_RESOURCE) {
             $requestDescription->setContainerName($requestDescription->getIdentifier());
         } else {
             // http://odata/NW.svc/Orders/$count
             // http://odata/NW.svc/Orders(123)/Customer/CustomerID/$value
             // http://odata/NW.svc/Employees(1)/$value
             // http://odata/NW.svc/Employees(1)/ThumbNail_48X48/$value
             $requestDescription->setContainerName($segmentDescriptors[count($segmentDescriptors) - 2]->getIdentifier());
         }
         if ($requestDescription->getIdentifier() === ODataConstants::URI_COUNT_SEGMENT) {
             if (!$dataService->getServiceConfiguration()->getAcceptCountRequests()) {
                 ODataException::createBadRequestError(Messages::dataServiceConfigurationCountNotAccepted());
             }
             $requestDescription->setRequestCountOption(RequestCountOption::VALUE_ONLY);
             // use of $count requires request DataServiceVersion
             // and MaxDataServiceVersion
             // greater than or equal to 2.0
             $requestDescription->raiseResponseVersion(2, 0, $dataService);
             $requestDescription->raiseMinimumVersionRequirement(2, 0, $dataService);
         } else {
             if ($requestDescription->isNamedStream()) {
                 $requestDescription->raiseMinimumVersionRequirement(3, 0, $dataService);
             } else {
                 if ($requestDescription->getTargetKind() == RequestTargetKind::RESOURCE) {
                     if (!$requestDescription->isLinkUri()) {
                         $resourceSetWrapper = $requestDescription->getTargetResourceSetWrapper();
                         //assert($resourceSetWrapper != null)
                         $hasNamedStream = $resourceSetWrapper->hasNamedStreams($dataService->getMetadataQueryProviderWrapper());
                         $hasBagProperty = $resourceSetWrapper->hasBagProperty($dataService->getMetadataQueryProviderWrapper());
                         if ($hasNamedStream || $hasBagProperty) {
                             $requestDescription->raiseResponseVersion(3, 0, $dataService);
                         }
                     }
                 } else {
                     if ($requestDescription->getTargetKind() == RequestTargetKind::BAG) {
                         $requestDescription->raiseResponseVersion(3, 0, $dataService);
                     }
                 }
             }
         }
     }
     return $requestDescription;
 }
Exemple #2
0
 /**
  * Parse the given Uri segments
  * 
  * @param array(string)                $segments        Array of segments in the
  *                                                      request Uri
  * @param MetadataQueryProviderWrapper $providerWrapper Reference to metadata and
  *                                                      query provider wrapper
  * @param boolean                      $checkForRights  Whether to check for rights 
  *                                                      on the resource sets in the 
  *                                                      segments
  * 
  * @return array(SegmentDescriptor)
  * 
  * @throws ODataException If any error occurs while processing segment
  */
 public static function parseRequestUriSegements($segments, MetadataQueryProviderWrapper $providerWrapper, $checkForRights = true)
 {
     $segmentParser = new SegmentParser($segments, $providerWrapper);
     $segmentParser->_createSegmentDescriptors($segments, $checkForRights);
     return $segmentParser->_segmentDescriptors;
 }
 public function testCreateSegments_MLEAndNamedStream()
 {
     try {
         //Test MLE
         $segments = array("Employees('JKT')", '$value');
         $segmentDescriptors = SegmentParser::parseRequestUriSegements($segments, $this->_metadataProviderWrapper);
         $this->assertEquals(count($segmentDescriptors), 2);
         $this->assertEquals($segmentDescriptors[0]->getIdentifier(), 'Employees');
         $this->assertEquals($segmentDescriptors[1]->getIdentifier(), '$value');
         $this->assertEquals($segmentDescriptors[0]->getTargetKind(), RequestTargetKind::RESOURCE);
         $this->assertEquals($segmentDescriptors[1]->getTargetKind(), RequestTargetKind::MEDIA_RESOURCE);
         $resourceType = $segmentDescriptors[0]->getTargetResourceType();
         $this->assertFalse(is_null($resourceType));
         $resourceType = $segmentDescriptors[1]->getTargetResourceType();
         $this->assertFalse(is_null($resourceType));
         $resouceSetWrapper = $segmentDescriptors[0]->getTargetResourceSetWrapper();
         $this->assertFalse(is_null($resouceSetWrapper));
         $resouceSetWrapper = $segmentDescriptors[1]->getTargetResourceSetWrapper();
         $this->assertTrue(is_null($resouceSetWrapper));
         $this->assertEquals($segmentDescriptors[0]->isSingleResult(), true);
         $this->assertEquals($segmentDescriptors[1]->isSingleResult(), true);
         $segments = array("Employees('JKT')", 'Manager', '$value');
         $segmentDescriptors = SegmentParser::parseRequestUriSegements($segments, $this->_metadataProviderWrapper);
         $this->assertEquals(count($segmentDescriptors), 3);
         $this->assertEquals($segmentDescriptors[0]->getIdentifier(), 'Employees');
         $this->assertEquals($segmentDescriptors[1]->getIdentifier(), 'Manager');
         $this->assertEquals($segmentDescriptors[2]->getIdentifier(), '$value');
         $this->assertEquals($segmentDescriptors[0]->getTargetKind(), RequestTargetKind::RESOURCE);
         $this->assertEquals($segmentDescriptors[1]->getTargetKind(), RequestTargetKind::RESOURCE);
         $this->assertEquals($segmentDescriptors[2]->getTargetKind(), RequestTargetKind::MEDIA_RESOURCE);
         $resourceType = $segmentDescriptors[0]->getTargetResourceType();
         $this->assertFalse(is_null($resourceType));
         $resourceType = $segmentDescriptors[1]->getTargetResourceType();
         $this->assertFalse(is_null($resourceType));
         $resourceType = $segmentDescriptors[2]->getTargetResourceType();
         $this->assertFalse(is_null($resourceType));
         $resouceSetWrapper = $segmentDescriptors[0]->getTargetResourceSetWrapper();
         $this->assertFalse(is_null($resouceSetWrapper));
         $resouceSetWrapper = $segmentDescriptors[1]->getTargetResourceSetWrapper();
         $this->assertFalse(is_null($resouceSetWrapper));
         $resouceSetWrapper = $segmentDescriptors[2]->getTargetResourceSetWrapper();
         $this->assertTrue(is_null($resouceSetWrapper));
         //Test Named Stream
         $segments = array("Employees('JKT')", 'TumbNail_48X48');
         $segmentDescriptors = SegmentParser::parseRequestUriSegements($segments, $this->_metadataProviderWrapper);
         $this->assertEquals(count($segmentDescriptors), 2);
         $this->assertEquals($segmentDescriptors[0]->getIdentifier(), 'Employees');
         $this->assertEquals($segmentDescriptors[1]->getIdentifier(), 'TumbNail_48X48');
         $this->assertEquals($segmentDescriptors[0]->getTargetKind(), RequestTargetKind::RESOURCE);
         $this->assertEquals($segmentDescriptors[1]->getTargetKind(), RequestTargetKind::MEDIA_RESOURCE);
         $resourceType = $segmentDescriptors[0]->getTargetResourceType();
         $this->assertFalse(is_null($resourceType));
         $resourceType = $segmentDescriptors[1]->getTargetResourceType();
         $this->assertFalse(is_null($resourceType));
         $resouceSetWrapper = $segmentDescriptors[0]->getTargetResourceSetWrapper();
         $this->assertFalse(is_null($resouceSetWrapper));
         $resouceSetWrapper = $segmentDescriptors[1]->getTargetResourceSetWrapper();
         $this->assertTrue(is_null($resouceSetWrapper));
         //No more segments after namedstream or MLE
         $segments = array("Employees('JKT')", 'TumbNail_48X48', 'anything');
         $exceptionThrown = false;
         try {
             SegmentParser::parseRequestUriSegements($segments, $this->_metadataProviderWrapper);
         } catch (ODataException $exception) {
             $exceptionThrown = true;
             $this->assertStringStartsWith('The request URI is not valid. The segment \'TumbNail_48X48\' must be the last segment in the', $exception->getMessage());
         }
         if (!$exceptionThrown) {
             $this->fail('An expected ODataException for segments specifed after named stream has not been thrown');
         }
     } catch (\Exception $exception) {
         $this->fail('An unexpected Exception has been raised' . $exception->getMessage());
     }
 }