/**
  * Process the request Uri and creates an instance of
  * RequestDescription from the processed uri.
  * 
  * @param IService $service        Reference to the data service instance.
  *
  * @return RequestDescription
  * 
  * @throws ODataException If any exception occurs while processing the segments
  *                        or in case of any version incompatibility.
  */
 public static function process(IService $service)
 {
     $host = $service->getHost();
     $absoluteRequestUri = $host->getAbsoluteRequestUri();
     $absoluteServiceUri = $host->getAbsoluteServiceUri();
     $requestUriSegments = array_slice($absoluteRequestUri->getSegments(), $absoluteServiceUri->getSegmentCount());
     $segments = SegmentParser::parseRequestUriSegments($requestUriSegments, $service->getProvidersWrapper(), true);
     $request = new RequestDescription($segments, $absoluteRequestUri, $service->getConfiguration()->getMaxDataServiceVersion(), $host->getRequestVersion(), $host->getRequestMaxVersion());
     $kind = $request->getTargetKind();
     if ($kind == TargetKind::METADATA() || $kind == TargetKind::BATCH() || $kind == TargetKind::SERVICE_DIRECTORY()) {
         return $request;
     }
     if ($kind == TargetKind::PRIMITIVE_VALUE() || $kind == TargetKind::MEDIA_RESOURCE()) {
         // 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
         $request->setContainerName($segments[count($segments) - 2]->getIdentifier());
     } else {
         $request->setContainerName($request->getIdentifier());
     }
     if ($request->getIdentifier() === ODataConstants::URI_COUNT_SEGMENT) {
         if (!$service->getConfiguration()->getAcceptCountRequests()) {
             throw ODataException::createBadRequestError(Messages::configurationCountNotAccepted());
         }
         $request->queryType = QueryType::COUNT();
         // use of $count requires request DataServiceVersion
         // and MaxDataServiceVersion greater than or equal to 2.0
         $request->raiseResponseVersion(2, 0);
         $request->raiseMinVersionRequirement(2, 0);
     } else {
         if ($request->isNamedStream()) {
             $request->raiseMinVersionRequirement(3, 0);
         } else {
             if ($request->getTargetKind() == TargetKind::RESOURCE()) {
                 if (!$request->isLinkUri()) {
                     $resourceSetWrapper = $request->getTargetResourceSetWrapper();
                     //assert($resourceSetWrapper != null)
                     $hasNamedStream = $resourceSetWrapper->hasNamedStreams($service->getProvidersWrapper());
                     $hasBagProperty = $resourceSetWrapper->hasBagProperty($service->getProvidersWrapper());
                     if ($hasNamedStream || $hasBagProperty) {
                         $request->raiseResponseVersion(3, 0);
                     }
                 }
             } else {
                 if ($request->getTargetKind() == TargetKind::BAG()) {
                     $request->raiseResponseVersion(3, 0);
                 }
             }
         }
     }
     return $request;
 }
Beispiel #2
0
 public function testCreateSegments_MLEAndNamedStream()
 {
     //Test MLE
     $segments = array("Employees('JKT')", '$value');
     $segmentDescriptors = SegmentParser::parseRequestUriSegments($segments, $this->providersWrapper);
     $this->assertEquals(count($segmentDescriptors), 2);
     $this->assertEquals($segmentDescriptors[0]->getIdentifier(), 'Employees');
     $this->assertEquals($segmentDescriptors[1]->getIdentifier(), '$value');
     $this->assertEquals($segmentDescriptors[0]->getTargetKind(), TargetKind::RESOURCE());
     $this->assertEquals($segmentDescriptors[1]->getTargetKind(), TargetKind::MEDIA_RESOURCE());
     $resourceType = $segmentDescriptors[0]->getTargetResourceType();
     $this->assertFalse(is_null($resourceType));
     $resourceType = $segmentDescriptors[1]->getTargetResourceType();
     $this->assertFalse(is_null($resourceType));
     $resourceSetWrapper = $segmentDescriptors[0]->getTargetResourceSetWrapper();
     $this->assertFalse(is_null($resourceSetWrapper));
     $resourceSetWrapper = $segmentDescriptors[1]->getTargetResourceSetWrapper();
     $this->assertTrue(is_null($resourceSetWrapper));
     $this->assertEquals($segmentDescriptors[0]->isSingleResult(), true);
     $this->assertEquals($segmentDescriptors[1]->isSingleResult(), true);
     $segments = array("Employees('JKT')", 'Manager', '$value');
     $segmentDescriptors = SegmentParser::parseRequestUriSegments($segments, $this->providersWrapper);
     $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(), TargetKind::RESOURCE());
     $this->assertEquals($segmentDescriptors[1]->getTargetKind(), TargetKind::RESOURCE());
     $this->assertEquals($segmentDescriptors[2]->getTargetKind(), TargetKind::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));
     $resourceSetWrapper = $segmentDescriptors[0]->getTargetResourceSetWrapper();
     $this->assertFalse(is_null($resourceSetWrapper));
     $resourceSetWrapper = $segmentDescriptors[1]->getTargetResourceSetWrapper();
     $this->assertFalse(is_null($resourceSetWrapper));
     $resourceSetWrapper = $segmentDescriptors[2]->getTargetResourceSetWrapper();
     $this->assertTrue(is_null($resourceSetWrapper));
     //Test Named Stream
     $segments = array("Employees('JKT')", 'TumbNail_48X48');
     $segmentDescriptors = SegmentParser::parseRequestUriSegments($segments, $this->providersWrapper);
     $this->assertEquals(count($segmentDescriptors), 2);
     $this->assertEquals($segmentDescriptors[0]->getIdentifier(), 'Employees');
     $this->assertEquals($segmentDescriptors[1]->getIdentifier(), 'TumbNail_48X48');
     $this->assertEquals($segmentDescriptors[0]->getTargetKind(), TargetKind::RESOURCE());
     $this->assertEquals($segmentDescriptors[1]->getTargetKind(), TargetKind::MEDIA_RESOURCE());
     $resourceType = $segmentDescriptors[0]->getTargetResourceType();
     $this->assertFalse(is_null($resourceType));
     $resourceType = $segmentDescriptors[1]->getTargetResourceType();
     $this->assertFalse(is_null($resourceType));
     $resourceSetWrapper = $segmentDescriptors[0]->getTargetResourceSetWrapper();
     $this->assertFalse(is_null($resourceSetWrapper));
     $resourceSetWrapper = $segmentDescriptors[1]->getTargetResourceSetWrapper();
     $this->assertTrue(is_null($resourceSetWrapper));
     //No more segments after namedstream or MLE
     $segments = array("Employees('JKT')", 'TumbNail_48X48', 'anything');
     try {
         SegmentParser::parseRequestUriSegments($segments, $this->providersWrapper);
         $this->fail('An expected ODataException for segments specifed after named stream has not been thrown');
     } catch (ODataException $exception) {
         $this->assertStringStartsWith('The request URI is not valid. The segment \'TumbNail_48X48\' must be the last segment in the', $exception->getMessage());
     }
 }
Beispiel #3
0
 /**
  * Parse the given Uri segments
  * 
  * @param string[] $segments Array of segments in the request Uri
  *
  * @param ProvidersWrapper $providerWrapper Reference to metadata and query provider wrapper
  * @param boolean $checkForRights  Whether to check for rights on the resource sets in the segments
  *
  * @return SegmentDescriptor[]
  * 
  * @throws ODataException If any error occurs while processing segment
  */
 public static function parseRequestUriSegments($segments, ProvidersWrapper $providerWrapper, $checkForRights = true)
 {
     $segmentParser = new SegmentParser($providerWrapper);
     $segmentParser->createSegmentDescriptors($segments, $checkForRights);
     return $segmentParser->_segmentDescriptors;
 }