Example #1
0
 /**
  * Provides pre-processing for HTTP requests to APP services.
  *
  * 1. Checks the $data element and, if it's an entry, extracts the XML,
  *    multipart data, edit link (PUT,DELETE), etc.
  * 2. If $data is a string, sets the default content-type  header as
  *    'application/atom+xml' if it's not already been set.
  * 3. Adds a x-http-method override header and changes the HTTP method
  *    to 'POST' if necessary as per getHttpMethodOverride()
  *
  * @param string $method The HTTP method for the request - 'GET', 'POST',
  *                       'PUT', 'DELETE'
  * @param string $url The URL to which this request is being performed,
  *                    or null if found in $data
  * @param array $headers An associative array of HTTP headers for this
  *                       request
  * @param mixed $data The Zend_Gdata_App_Entry or XML for the
  *                    body of the request
  * @param string $contentTypeOverride The override value for the
  *                                    content type of the request body
  * @return array An associative array containing the determined
  *               'method', 'url', 'data', 'headers', 'contentType'
  */
 public function prepareRequest($method, $url = null, $headers = array(), $data = null, $contentTypeOverride = null)
 {
     // As a convenience, if $headers is null, we'll convert it back to
     // an empty array.
     if ($headers === null) {
         $headers = array();
     }
     $rawData = null;
     $finalContentType = null;
     if ($url == null) {
         $url = $this->_defaultPostUri;
     }
     if (is_string($data)) {
         $rawData = $data;
         if ($contentTypeOverride === null) {
             $finalContentType = 'application/atom+xml';
         }
     } elseif ($data instanceof Zend_Gdata_App_MediaEntry) {
         $rawData = $data->encode();
         if ($data->getMediaSource() !== null) {
             $finalContentType = $rawData->getContentType();
             $headers['MIME-version'] = '1.0';
             $headers['Slug'] = $data->getMediaSource()->getSlug();
         } else {
             $finalContentType = 'application/atom+xml';
         }
         if ($method == 'PUT' || $method == 'DELETE') {
             $editLink = $data->getEditLink();
             if ($editLink != null && $url == null) {
                 $url = $editLink->getHref();
             }
         }
     } elseif ($data instanceof Zend_Gdata_App_Entry) {
         $rawData = $data->saveXML();
         $finalContentType = 'application/atom+xml';
         if ($method == 'PUT' || $method == 'DELETE') {
             $editLink = $data->getEditLink();
             if ($editLink != null) {
                 $url = $editLink->getHref();
             }
         }
     } elseif ($data instanceof Zend_Gdata_App_MediaSource) {
         $rawData = $data->encode();
         if ($data->getSlug() !== null) {
             $headers['Slug'] = $data->getSlug();
         }
         $finalContentType = $data->getContentType();
     }
     if ($method == 'DELETE') {
         $rawData = null;
     }
     // Set an If-Match header if:
     //   - This isn't a DELETE
     //   - If this isn't a GET, the Etag isn't weak
     //   - A similar header (If-Match/If-None-Match) hasn't already been
     //     set.
     if ($method != 'DELETE' && (!array_key_exists('If-Match', $headers) && !array_key_exists('If-None-Match', $headers))) {
         $allowWeak = $method == 'GET';
         if ($ifMatchHeader = $this->generateIfMatchHeaderData($data, $allowWeak)) {
             $headers['If-Match'] = $ifMatchHeader;
         }
     }
     if ($method != 'POST' && $method != 'GET' && Zend_Gdata_App::getHttpMethodOverride()) {
         $headers['x-http-method-override'] = $method;
         $method = 'POST';
     } else {
         $headers['x-http-method-override'] = null;
     }
     if ($contentTypeOverride != null) {
         $finalContentType = $contentTypeOverride;
     }
     return array('method' => $method, 'url' => $url, 'data' => $rawData, 'headers' => $headers, 'contentType' => $finalContentType);
 }
 public function testSetAndGetHttpMethodOverride()
 {
     Zend_Gdata_App::setHttpMethodOverride(true);
     $this->assertEquals(true, Zend_Gdata_App::getHttpMethodOverride());
 }
Example #3
0
 /**
  * Provides pre-processing for HTTP requests to APP services.  
  *
  * 1. Checks the $data element and, if it's an entry, extracts the XML, 
  *    multipart data, edit link (PUT,DELETE), etc.
  * 2. If $data is a string, sets the default content-type  header as 
  *    'application/atom+xml' if it's not already been set.
  * 3. Adds a x-http-method override header and changes the HTTP method 
  *    to 'POST' if necessary as per getHttpMethodOverride()
  *
  * @param string $method The HTTP method for the request - 'GET', 'POST', 
  *                       'PUT', 'DELETE'
  * @param string $url The URL to which this request is being performed, 
  *                    or null if found in $data
  * @param array $headers An associative array of HTTP headers for this 
  *                       request
  * @param mixed $data The Zend_Gdata_App_Entry or XML for the  
  *                    body of the request
  * @param string $contentTypeOverride The override value for the 
  *                                    content type of the request body
  * @return array An associative array containing the determined 
  *               'method', 'url', 'data', 'headers', 'contentType'
  */
 public function prepareRequest($method, $url = null, $headers = array(), $data = null, $contentTypeOverride = null)
 {
     $rawData = null;
     $finalContentType = null;
     if ($url == null) {
         $url = $this->_defaultPostUri;
     }
     if (is_string($data)) {
         $rawData = $data;
         if ($contentTypeOverride === null) {
             $finalContentType = 'application/atom+xml';
         }
     } elseif ($data instanceof Zend_Gdata_App_MediaEntry) {
         $rawData = $data->encode();
         if ($data->getMediaSource() !== null) {
             $finalContentType = 'multipart/related; boundary="' . $data->getBoundary() . '"';
             $headers['MIME-version'] = '1.0';
             $headers['Slug'] = $data->getMediaSource()->getSlug();
         } else {
             $finalContentType = 'application/atom+xml';
         }
         if ($method == 'PUT' || $method == 'DELETE') {
             $editLink = $data->getEditLink();
             if ($editLink != null) {
                 $url = $editLink->getHref();
             }
         }
     } elseif ($data instanceof Zend_Gdata_App_Entry) {
         $rawData = $data->saveXML();
         $finalContentType = 'application/atom+xml';
         if ($method == 'PUT' || $method == 'DELETE') {
             $editLink = $data->getEditLink();
             if ($editLink != null) {
                 $url = $editLink->getHref();
             }
         }
     } elseif ($data instanceof Zend_Gdata_App_MediaSource) {
         $rawData = $data->encode();
         if ($data->getSlug() !== null) {
             $headers['Slug'] = $data->getSlug();
         }
         $finalContentType = $data->getContentType();
     }
     if ($method == 'DELETE') {
         $rawData = null;
     }
     if ($method != 'POST' && $method != 'GET' && Zend_Gdata_App::getHttpMethodOverride()) {
         $headers['x-http-method-override'] = $method;
         $method = 'POST';
     } else {
         $headers['x-http-method-override'] = null;
     }
     if ($contentTypeOverride != null) {
         $finalContentType = $contentTypeOverride;
     }
     return array('method' => $method, 'url' => $url, 'data' => $rawData, 'headers' => $headers, 'contentType' => $finalContentType);
 }
Example #4
0
 /**
  * DELETE entry with client object
  *
  * @param mixed $data The Zend_Gdata_App_Entry or URL to delete
  * @return void
  * @throws Zend_Gdata_App_Exception
  * @throws Zend_Gdata_App_HttpException
  * @throws Zend_Gdata_App_InvalidArgumentException
  */
 public function delete($data, $remainingRedirects = null)
 {
     require_once 'Zend/Http/Client/Exception.php';
     if ($remainingRedirects === null) {
         $remainingRedirects = self::getMaxRedirects();
     }
     if (is_string($data)) {
         $uri = $data;
     } elseif ($data instanceof Zend_Gdata_App_Entry) {
         $editLink = $data->getEditLink();
         if ($editLink != null) {
             $uri = $editLink->getHref();
         }
     } else {
         require_once 'Zend/Gdata/App/InvalidArgumentException.php';
         throw new Zend_Gdata_App_InvalidArgumentException('You must specify the data to post as either a string or a child of Zend_Gdata_App_Entry');
     }
     if ($uri === null) {
         require_once 'Zend/Gdata/App/InvalidArgumentException.php';
         throw new Zend_Gdata_App_InvalidArgumentException('You must specify an URI which needs deleted.');
     }
     $this->_httpClient->resetParameters();
     $this->_httpClient->setHeaders('x-http-method-override', null);
     $this->_httpClient->setUri($uri);
     $this->_httpClient->setConfig(array('maxredirects' => 0));
     try {
         if (Zend_Gdata_App::getHttpMethodOverride()) {
             $this->_httpClient->setHeaders(array('X-HTTP-Method-Override: DELETE'));
             $this->_httpClient->setRawData('');
             $response = $this->_httpClient->request('POST');
         } else {
             $response = $this->_httpClient->request('DELETE');
         }
     } catch (Zend_Http_Client_Exception $e) {
         require_once 'Zend/Gdata/App/HttpException.php';
         throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
     }
     if ($response->isRedirect()) {
         if ($remainingRedirects > 0) {
             $newUri = $response->getHeader('Location');
             $response = $this->delete($newUri, $remainingRedirects - 1);
         } else {
             require_once 'Zend/Gdata/App/HttpException.php';
             throw new Zend_Gdata_App_HttpException('No more redirects allowed', null, $response);
         }
     }
     if (!$response->isSuccessful()) {
         require_once 'Zend/Gdata/App/HttpException.php';
         $exception = new Zend_Gdata_App_HttpException('Expected response code 200, got ' . $response->getStatus());
         $exception->setResponse($response);
         throw $exception;
     }
     return $response;
 }
Example #5
0
 /**
  * Deletes this entry to the server using the referenced 
  * Zend_Http_Client to do a HTTP DELETE to the edit link stored in this 
  * entry's link collection.  
  *
  * @return boolean The success of the delete operation
  * @throws Zend_Gdata_App_Exception
  */
 public function delete()
 {
     if ($this->id) {
         // If id is set, look for link rel="edit" in the
         // entry object and DELETE.
         $editLink = $this->getLink('edit');
         $editUri = $editLink->href;
         if (!$editUri) {
             throw new Zend_Gdata_App_Exception('Cannot delete entry; no link rel="edit" is present.');
         }
         $client = $this->getHttpClient();
         if (is_null($client)) {
             $client = Zend_Gdata_App::getStaticHttpClient();
         }
         $client->resetParameters();
         $client->setUri($editUri);
         $client->setHeaders('Content-Type', null);
         $client->setRawData('');
         if (Zend_Gdata_App::getHttpMethodOverride()) {
             $client->setHeaders('X-HTTP-Method-Override', 'DELETE');
             $response = $client->request('DELETE');
         } else {
             $response = $client->request('DELETE');
         }
         if ($response->getStatus() !== 200) {
             require_once 'Zend/Gdata/App/HttpException.php';
             $exception = new Zend_Gdata_App_HttpException('Expected response code 200, got ' . $response->getStatus());
             $exception->setResponse($response);
             throw $exception;
         }
         return true;
     } else {
         throw new Zend_Gdata_App_Exception('Cannot edit entry; no id is present');
     }
 }