• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • Examples
  • File List
  • File Members

CAS/Request/AbstractRequest.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003  * Copyright © 2003-2010, The ESUP-Portail consortium & the JA-SIG Collaborative.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  *         * Redistributions of source code must retain the above copyright notice,
00010  *               this list of conditions and the following disclaimer.
00011  *         * Redistributions in binary form must reproduce the above copyright notice,
00012  *               this list of conditions and the following disclaimer in the documentation
00013  *               and/or other materials provided with the distribution.
00014  *         * Neither the name of the ESUP-Portail consortium & the JA-SIG
00015  *               Collaborative nor the names of its contributors may be used to endorse or
00016  *               promote products derived from this software without specific prior
00017  *               written permission.
00018 
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00020  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
00023  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00026  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 require_once dirname(__FILE__).'/RequestInterface.php';
00032 include_once(dirname(__FILE__).'/Exception.php');
00033 include_once(dirname(__FILE__).'/../InvalidArgumentException.php');
00034 include_once(dirname(__FILE__).'/../OutOfSequenceException.php');
00035 
00039 abstract class CAS_AbstractRequest
00040         implements CAS_RequestInterface
00041 {
00042 
00043         protected $url = null;
00044         protected $cookies = array();
00045         protected $headers = array();
00046         protected $isPost = FALSE;
00047         protected $postBody = null;
00048         protected $caCertPath = null;
00049         private $sent = FALSE;
00050         private $responseHeaders = array();
00051         private $responseBody = null;
00052         private $errorMessage = '';
00053 
00054         /*********************************************************
00055          * Configure the Request
00056          *********************************************************/
00057 
00065         public function setUrl ($url) {
00066                 if ($this->sent)
00067                         throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00068 
00069                 $this->url = $url;
00070         }
00071 
00080         public function addCookie ($name, $value) {
00081                 if ($this->sent)
00082                         throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00083 
00084                 $this->cookies[$name] = $value;
00085         }
00086 
00096         public function addCookies (array $cookies) {
00097                 if ($this->sent)
00098                         throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00099 
00100                 $this->cookies = array_merge($this->cookies, $cookies);
00101         }
00102 
00110         public function addHeader ($header) {
00111                 if ($this->sent)
00112                         throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00113 
00114                 $this->headers[] = $header;
00115         }
00116 
00124         public function addHeaders (array $headers) {
00125                 if ($this->sent)
00126                         throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00127 
00128                 $this->headers = array_merge($this->headers, $headers);
00129         }
00130 
00137         public function makePost () {
00138                 if ($this->sent)
00139                         throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00140 
00141                 $this->isPost = TRUE;
00142         }
00143 
00151         public function setPostBody ($body) {
00152                 if ($this->sent)
00153                         throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00154                 if (!$this->isPost)
00155                         throw new CAS_OutOfSequenceException('Cannot add a POST body to a GET request, use makePost() first.');
00156 
00157                 $this->postBody = $body;
00158         }
00159 
00167         public function setSslCaCert ($caCertPath) {
00168                 if ($this->sent)
00169                         throw new CAS_OutOfSequenceException('Request has already been sent cannot '.__METHOD__);
00170 
00171                 $this->caCertPath = $caCertPath;
00172         }
00173 
00174         /*********************************************************
00175          * 2. Send the Request
00176          *********************************************************/
00177 
00184         public function send () {
00185                 if ($this->sent)
00186                         throw new CAS_OutOfSequenceException('Request has already been sent cannot send again.');
00187                 if (is_null($this->url) || !$this->url)
00188                         throw new CAS_OutOfSequenceException('A url must be specified via setUrl() before the request can be sent.');
00189 
00190                 $this->sent = true;
00191                 return $this->_sendRequest();
00192         }
00193 
00199         abstract protected function _sendRequest ();
00200 
00207         protected function storeResponseHeaders (array $headers) {
00208                 $this->responseHeaders = array_merge($this->responseHeaders, $headers);
00209         }
00210 
00217         protected function storeResponseHeader ($header) {
00218                 $this->responseHeaders[] = $header;
00219         }
00220 
00227         protected function storeResponseBody ($body) {
00228                 $this->responseBody = $body;
00229         }
00230 
00237         protected function storeErrorMessage ($message) {
00238                 $this->errorMessage .= $message;
00239         }
00240 
00241         /*********************************************************
00242          * 3. Access the response
00243          *********************************************************/
00244 
00251         public function getResponseHeaders () {
00252                 if (!$this->sent)
00253                         throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__);
00254 
00255                 return $this->responseHeaders;
00256         }
00257         
00264         public function getResponseStatusCode () {
00265                 if (!$this->sent)
00266                         throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__);
00267                 
00268                 if (!preg_match('/HTTP\/[0-9.]+\s+([0-9]+)\s*(.*)/', $this->responseHeaders[0], $matches))
00269                         throw new CAS_Request_Exception("Bad response, no status code was found in the first line.");
00270                 
00271                 return intval($matches[1]);
00272         }
00273 
00280         public function getResponseBody () {
00281                 if (!$this->sent)
00282                         throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__);
00283 
00284                 return $this->responseBody;
00285         }
00286 
00293         public function getErrorMessage () {
00294                 if (!$this->sent)
00295                         throw new CAS_OutOfSequenceException('Request has not been sent yet. Cannot '.__METHOD__);
00296 
00297                 return $this->errorMessage;
00298         }
00299 }

Generated on Sun Jun 5 2011 19:05:26 for phpCAS by  doxygen 1.7.1