Author: ts
Date: Thu Sep 20 17:16:53 2007
New Revision: 6229

Log:
- Made first responses serialize to XML.

Modified:
    trunk/Webdav/src/interfaces/response.php
    trunk/Webdav/src/path_factory.php
    trunk/Webdav/src/transport.php
    trunk/Webdav/tests/classes/transport_test_mock.php
    trunk/Webdav/tests/client_test.php

Modified: trunk/Webdav/src/interfaces/response.php
==============================================================================
--- trunk/Webdav/src/interfaces/response.php [iso-8859-1] (original)
+++ trunk/Webdav/src/interfaces/response.php [iso-8859-1] Thu Sep 20 17:16:53 
2007
@@ -203,6 +203,11 @@
         return isset( $this->headers[$headerName] ) ? 
$this->headers[$headerName] : null;
     }
 
+    /**
+     * Returns all headers.
+     * 
+     * @return array(string=>string)
+     */
     public final function getHeaders()
     {
         return $this->headers;

Modified: trunk/Webdav/src/path_factory.php
==============================================================================
--- trunk/Webdav/src/path_factory.php [iso-8859-1] (original)
+++ trunk/Webdav/src/path_factory.php [iso-8859-1] Thu Sep 20 17:16:53 2007
@@ -80,12 +80,12 @@
     public function generateUriFromPath( $path )
     {
         return $this->baseUriParts['scheme'] 
-             . '://' . $this->baseUriParts['host'] 
+             . '://' 
              . ( isset( $this->baseUriParts['user'] ) ? 
$this->baseUriParts['user'] : '' )
              . ( isset( $this->baseUriParts['pass'] ) ? ':' . 
$this->baseUriParts['pass'] : '' )
              . ( isset( $this->baseUriParts['user'] ) || isset( 
$this->baseUriParts['pass'] ) ? '@' : '' )
              . $this->baseUriParts['host']
-             . $this->baseUriParts['path']
+             . ( isset( $this->baseUriParts['path'] ) ? 
$this->baseUriParts['path'] : '' )
              . $path
              . ( isset( $this->baseUriParts['query'] ) ? '?' . 
$this->baseUriParts['query'] : '' )
              . ( isset( $this->baseUriParts['fragment'] ) ? '#' . 
$this->baseUriParts['fragment'] : '' );

Modified: trunk/Webdav/src/transport.php
==============================================================================
--- trunk/Webdav/src/transport.php [iso-8859-1] (original)
+++ trunk/Webdav/src/transport.php [iso-8859-1] Thu Sep 20 17:16:53 2007
@@ -118,15 +118,18 @@
      * @param sting $xml 
      * @return DOMDocument|false
      */
-    protected function loadDom( $xml )
-    {
-        $dom = new DOMDocument();
-        if ( $dom->loadXML(
-                $xml,
-                LIBXML_NOWARNING | LIBXML_NSCLEAN | LIBXML_NOBLANKS
-             ) === false )
-        {
-            return false;
+    protected function getDom( $xml = null )
+    {
+        $dom = new DOMDocument( '1.0', 'utf-8' );
+        if ( $xml !== null )
+        {
+            if ( $dom->loadXML(
+                    $xml,
+                    LIBXML_NOWARNING | LIBXML_NSCLEAN | LIBXML_NOBLANKS
+                 ) === false )
+            {
+                return false;
+            }
         }
         return $dom;
     }
@@ -224,7 +227,7 @@
             return $request;
         }
 
-        if ( ( $dom = $this->loadDom( $body ) ) === false )
+        if ( ( $dom = $this->getDom( $body ) ) === false )
         {
             throw new ezcWebdavInvalidRequestBodyException(
                 'COPY',
@@ -272,7 +275,7 @@
             return $request;
         }
 
-        if ( ( $dom = $this->loadDom( $body ) ) === false )
+        if ( ( $dom = $this->getDom( $body ) ) === false )
         {
             throw new ezcWebdavInvalidRequestBodyException(
                 'MOVE',
@@ -375,7 +378,7 @@
             return $request;
         }
 
-        if ( ( $dom = $this->loadDom( $body ) ) === false )
+        if ( ( $dom = $this->getDom( $body ) ) === false )
         {
             throw new ezcWebdavInvalidRequestBodyException(
                 'LOCK',
@@ -493,7 +496,7 @@
             )
         );
 
-        if ( ( $dom = $this->loadDom( $body ) ) === false )
+        if ( ( $dom = $this->getDom( $body ) ) === false )
         {
             throw new ezcWebdavInvalidRequestBodyException(
                 'PROPFIND',
@@ -769,7 +772,7 @@
     {
         $request = new ezcWebdavPropPatchRequest( $path );
 
-        if ( ( $dom = $this->loadDom( $body ) ) === false )
+        if ( ( $dom = $this->getDom( $body ) ) === false )
         {
             throw new ezcWebdavInvalidRequestBodyException(
                 'PROPPATCH',
@@ -819,8 +822,22 @@
      */
     public function handleResponse( ezcWebdavResponse $response )
     {
-        switch ( get_class( $response ) )
-        {
+        $this->sendResponse( $response, $this->processResponse( $response ) );
+    }
+
+    protected function processResponse( ezcWebdavResponse $response )
+    {
+        $dom = null;
+
+        switch ( ( $responseClass = get_class( $response ) ) )
+        {
+            case 'ezcWebdavPropFindResponse':
+                $dom = $this->processPropFindResponse( $response );
+                break;
+            case 'ezcWebdavMultistatusResponse':
+                $dom = $this->processMultiStatusResponse( $response );
+                break;
+            
             case 'ezcWebdavCopyResponse':
             case 'ezcWebdavDeleteResponse':
             case 'ezcWebdavErrorResponse':
@@ -829,22 +846,272 @@
             case 'ezcWebdavHeadResponse':
             case 'ezcWebdavMakeCollectionResponse':
             case 'ezcWebdavMoveResponse':
-            case 'ezcWebdavMultiStatusResponse':
             case 'ezcWebdavOptionsResponse':
             case 'ezcWebdavPropPatchResponse':
             case 'ezcWebdavPutResponse':
+            default:
                 // @TODO: Implement!
-                throw new RuntimeException( 'Not implemented, yet.' );
+                throw new RuntimeException( "Serialization of class 
$responseClass not implemented, yet." );
             
-            case 'ezcWebdavPropFindResponse':
-                $this->handlePropFindResponse( $response );
-                break;
-        }
-    }
-
-    protected function handlePropFindResponse( ezcWebdavPropFindResponse 
$response )
-    {
-        $dom = new DOMDocument();
+        }
+
+        return $dom;
+    }
+
+    /**
+     * Finally send out the response.
+     * This method is called to finally send the response to the browser. It
+     * can be overwritten in test cases to change the behaviour of printing out
+     * the result and sending the headers.
+     * 
+     * @param ezcWebdavResponse $response 
+     * @param DOMDocument $dom 
+     * @return void
+     */
+    protected function sendResponse( ezcWebdavResponse $response, DOMDocument 
$dom = null )
+    {
+        header( (string) $response );
+        if ( $dom instanceof DOMDocument )
+        {
+            $dom->formatOutput = true;
+            echo $dom->saveXML( $dom );
+        }
+    }
+
+    /**
+     * Returns an XML representation of the given response object.
+     *
+     * @param ezcWebdavMultiStatusResponse $response 
+     * @return DOMDocument
+     */
+    protected function processMultiStatusResponse( 
ezcWebdavMultiStatusResponse $response )
+    {
+        $dom = $this->getDom();
+
+        $multistatusElement = $dom->appendChild(
+            $dom->createElementNS(
+                'DAV:',
+                'D:multistatus'
+            )
+        );
+
+        foreach ( $response->responses as $subResponse )
+        {
+            $multistatusElement->appendChild(
+                $dom->importNode( $this->processResponse( $subResponse 
)->documentElement, true )
+            );
+        }
+        
+        return $dom;
+    }
+
+    /**
+     * Returns an XML representation of the given response object.
+     *
+     * @param ezcWebdavPropFindResponse $response 
+     * @return DOMDocument
+     */
+    protected function processPropFindResponse( ezcWebdavPropFindResponse 
$response )
+    {
+        $dom = $this->getDom();
+
+        $responseElement = $dom->appendChild(
+            $dom->createElementNS( 'DAV:', 'D:repsonse' )
+        );
+
+        $responseElement->appendChild(
+            $dom->createElementNS(
+                'DAV:',
+                'D:href',
+                $this->options->pathFactory->generateUriFromPath( 
$response->node->path )
+            )
+        );
+
+        foreach ( $response->responses as $propStat )
+        {
+            $responseElement->appendChild(
+                $dom->importNode( $this->processPropStatResponse( $propStat 
)->documentElement, true )
+            );
+        }
+        return $dom;
+    }
+
+    /**
+     * Returns an XML representation of the given response object.
+     * 
+     * @param ezcWebdavPropStatResponse $response 
+     * @return DOMDocument
+     */
+    protected function processPropStatResponse( ezcWebdavPropStatResponse 
$response )
+    {
+        $dom = $this->getDom();
+
+        $propstatElement = $dom->appendChild(
+            $dom->createElementNS( 'DAV:', 'D:propstat' )
+        );
+        
+        $this->serializePropertyStorage(
+            $response->storage,
+            $propstatElement->appendChild( $dom->createElementNS( 'DAV:', 
'D:prop' ) )
+        );
+
+        $propstatElement->appendChild(
+            $dom->createElementNS(
+                'DAV:',
+                'D:status',
+                (string) $response
+            )
+        );
+
+        return $dom;
+    }
+
+    /**
+     * Serializes an object of ezcWebdavPropertyStorage to XML.
+     * Attaches all properties of the $storage to the $parentElement XML
+     * element.
+     * 
+     * @param ezcWebdavPropertyStorage $storage 
+     * @param DOMElement $parentElement 
+     * @return void
+     */
+    protected function serializePropertyStorage( ezcWebdavPropertyStorage 
$storage, DOMElement $parentElement )
+    {
+        foreach ( $storage as $property )
+        {
+            if ( $property instanceof ezcWebdavLiveProperty )
+            {
+                $this->serializeLiveProperty( $property, $parentElement );
+            }
+            else
+            {
+                $this->serializeDeadProperty( $property, $parentElement );
+            }
+        }
+    }
+
+    /**
+     * Returns the XML representation of a dead property.
+     * Returns a DOMElement, representing the content of the given $property.
+     * The newly created element is also appended as a child to the given
+     * $parentElement.
+     * 
+     * @param ezcWebdavDeadProperty $property 
+     * @param DOMElement $parentElement 
+     * @return DOMElement
+     */
+    protected function serializeDeadProperty( ezcWebdavDeadProperty $property, 
DOMElement $parentElement )
+    {
+        if ( $property->content === null || ( $contentDom = $this->getDom( 
$property->content ) ) === false )
+        {
+            return $parentElement->appendChild(
+                $parentElement->ownerDocument->createElementNS(
+                    $property->namespace,
+                    // This seems to be a way to not loose the correct prefix 
here.
+                    $property->ownerDocument->lookupPrefix( 
$property->namespace ) . ':' . $property->name
+                )
+            );
+        }
+
+        return $parentElement->appendChild(
+            $parentElement->ownerDocument->importNode( 
$contentDom->documentElement, true )
+        );
+    }
+
+    /**
+     * Returns the XML representation of a live property.
+     * Returns a DOMElement, representing the content of the given $property.
+     * The newly created element is also appended as a child to the given
+     * $parentElement.
+     * 
+     * @param ezcWebdavLiveProperty $property 
+     * @param DOMElement $parentElement 
+     * @return DOMElement
+     */
+    protected function serializeLiveProperty( ezcWebdavLiveProperty $property, 
DOMElement $parentElement )
+    {
+        switch ( get_class( $property ) )
+        {
+            case 'ezcWebdavCreationDateProperty':
+                $elementName  = 'creationdate';
+                $elementValue = ( $property->date !== null ? 
$property->date->format( DATE_ISO8601 ) : null );
+                break;
+            case 'ezcWebdavDisplayNameProperty':
+                $elementName  = 'displayname';
+                $elementValue = $property->displayName;
+                break;
+            case 'ezcWebdavGetContentLanguageProperty':
+                $elementName  = 'getcontentlanguage';
+                $elementValue = ( count( $property->languages ) > 0 ? implode( 
', ', $property->languages ) : null );
+                break;
+            case 'ezcWebdavGetContentLengthProperty':
+                $elementName  = 'getcontentlength';
+                $elementValue = $property->length;
+                break;
+            case 'ezcWebdavGetContentTypeProperty':
+                $elementName  = 'getcontenttype';
+                $elementValue = ( $property->mime !== null ? $property->mime . 
( $property->charset === null ? '' : '; charset=' . $property->charset ) : null 
);
+                break;
+            case 'ezcWebdavGetEtagProperty':
+                $elementName  = 'getetag';
+                $elementValue = $property->etag;
+                break;
+            case 'ezcWebdavGetLastModifiedProperty':
+                $elementName  = 'getlastmodified';
+                $elementValue = ( $property->date !== null ? 
$property->date->format( DATE_ISO8601 ) : null );
+                break;
+            case 'ezcWebdavLockDiscoveryProperty':
+                $elementName  = 'lockdiscovery';
+                $elementValue = ( $property->activeLock !== null ? 
$this->serializeActiveLockContent( $property->activeLock ) : null );
+                break;
+            case 'ezcWebdavResourceTypeProperty':
+                $elementName  = 'resourcetype';
+                $elementValue = ( $property->type === 'collection' ? new 
DOMElement( 'D:collection', null, 'DAV:' ) : null );
+                break;
+            case 'ezcWebdavSourceProperty':
+                $elementName  = 'source';
+                $elementValue = ( $property->links !== null ? 
$this->serializeLinkContent( $property->links ) : null );
+                break;
+            case 'ezcWebdavSupportedLockProperty':
+                $elementName  = 'supportedlock';
+                $elementValue = ( $property->lockEntry !== null ? 
$this->serializeLockEntryContent( $property->lockEntry ) : null );
+                break;
+        }
+
+        $propertyElement = $parentElement->appendChild( 
+            $parentElement->ownerDocument->createElementNS(
+                'DAV:',
+                "D:{$elementName}"
+            )
+        );
+
+        if ( $elementValue instanceof DOMDocument )
+        {
+            $propertyElement->appendChild(
+                $dom->importNode( $elementValue->documentElement, true )
+            );
+        }
+        else if ( $elementValue !== null )
+        {
+            $propertyElement->nodeValue = $elementValue;
+        }
+
+        return $propertyElement;
+    }
+
+    protected function serializeActiveLockContent( 
ezcWebdavLockDiscoveryPropertyActiveLock $content = null )
+    {
+        return null;
+    }
+
+    protected function serializeLinkContent( array $links = null )
+    {
+        return null;
+    }
+
+    protected function serializeLockEntryContent( array $content = null )
+    {
+        return null;
     }
     
     /**

Modified: trunk/Webdav/tests/classes/transport_test_mock.php
==============================================================================
--- trunk/Webdav/tests/classes/transport_test_mock.php [iso-8859-1] (original)
+++ trunk/Webdav/tests/classes/transport_test_mock.php [iso-8859-1] Thu Sep 20 
17:16:53 2007
@@ -6,6 +6,12 @@
     {
         return isset( $GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_BODY'] ) ? 
$GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_BODY'] : '';
     }
+
+    protected function sendResponse( ezcWebdavResponse $response, DOMDocument 
$dom = null )
+    {
+        $GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_RESPONSE_HEADERS'][] = (string) 
$response;
+        $GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_RESPONSE_BODY'] = ( $dom !== null 
? $dom->saveXML( $dom, LIBXML_NSCLEAN ) : null );
+    }
 }
 
 ?>

Modified: trunk/Webdav/tests/client_test.php
==============================================================================
--- trunk/Webdav/tests/client_test.php [iso-8859-1] (original)
+++ trunk/Webdav/tests/client_test.php [iso-8859-1] Thu Sep 20 17:16:53 2007
@@ -227,6 +227,25 @@
                 throw new PHPUnit_Framework_ExpectationFailedException( 
"Unable to dispatch request of class " . get_class( $requestObject ) );
         }
 
+        try
+        {
+            $this->transport->handleResponse( $responseObject );
+            $responseHeaders = 
$GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_RESPONSE_HEADERS'];
+            $responseBody    = 
$GLOBALS['EZC_WEBDAV_TRANSPORT_TEST_RESPONSE_BODY'];
+
+            libxml_use_internal_errors( true);
+
+            $this->assertXmlStringEqualsXmlString(
+                $response['body'],
+                $responseBody,
+                'Response body not generated correctly.'
+            );
+        }
+        catch ( RuntimeException $e )
+        {
+            return;
+        }
+
         $this->assertEquals(
             $response['code'],
             $responseObject->status,


-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to