4 * Licensed to Jasig under one or more contributor license
5 * agreements. See the NOTICE file distributed with this work for
6 * additional information regarding copyright ownership.
8 * Jasig licenses this file to you under the Apache License,
9 * Version 2.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at:
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 * @file CAS/ProxiedService/Http/Post.php
23 * @category Authentication
25 * @author Adam Franco <afranco@middlebury.edu>
26 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
27 * @link https://wiki.jasig.org/display/CASC/phpCAS
31 * This class is used to make proxied service requests via the HTTP POST method.
36 * $service = phpCAS::getProxiedService(PHPCAS_PROXIED_SERVICE_HTTP_POST);
37 * $service->setUrl('http://www.example.com/path/');
38 * $service->setContentType('text/xml');
39 * $service->setBody(''<?xml version="1.0"?'.'><methodCall><methodName>example.search</methodName></methodCall>');
41 * if ($service->getResponseStatusCode() == 200)
42 * return $service->getResponseBody();
44 * // The service responded with an error code 404, 500, etc.
45 * throw new Exception('The service responded with an error.');
47 * } catch (CAS_ProxyTicketException $e) {
48 * if ($e->getCode() == PHPCAS_SERVICE_PT_FAILURE)
49 * return "Your login has timed out. You need to log in again.";
51 * // Other proxy ticket errors are from bad request format (shouldn't happen)
52 * // or CAS server failure (unlikely) so lets just stop if we hit those.
54 * } catch (CAS_ProxiedService_Exception $e) {
55 * // Something prevented the service request from being sent or received.
56 * // We didn't even get a valid error response (404, 500, etc), so this
57 * // might be caused by a network error or a DNS resolution failure.
58 * // We could handle it in some way, but for now we will just stop.
62 * @class CAS_ProxiedService_Http_Post
63 * @category Authentication
65 * @author Adam Franco <afranco@middlebury.edu>
66 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
67 * @link https://wiki.jasig.org/display/CASC/phpCAS
69 class CAS_ProxiedService_Http_Post
70 extends CAS_ProxiedService_Http_Abstract
74 * The content-type of this request
76 * @var string $_contentType
78 private $_contentType;
81 * The body of the this request
88 * Set the content type of this POST request.
90 * @param string $contentType content type
93 * @throws CAS_OutOfSequenceException If called after the Request has been sent.
95 public function setContentType ($contentType)
97 if ($this->hasBeenSent()) {
98 throw new CAS_OutOfSequenceException('Cannot set the content type, request already sent.');
101 $this->_contentType = $contentType;
105 * Set the body of this POST request.
107 * @param string $body body to set
110 * @throws CAS_OutOfSequenceException If called after the Request has been sent.
112 public function setBody ($body)
114 if ($this->hasBeenSent()) {
115 throw new CAS_OutOfSequenceException('Cannot set the body, request already sent.');
118 $this->_body = $body;
122 * Add any other parts of the request needed by concrete classes
124 * @param CAS_Request_RequestInterface $request request interface class
128 protected function populateRequest (CAS_Request_RequestInterface $request)
130 if (empty($this->_contentType) && !empty($this->_body)) {
131 throw new CAS_ProxiedService_Exception("If you pass a POST body, you must specify a content type via ".get_class($this).'->setContentType($contentType).');
134 $request->makePost();
135 if (!empty($this->_body)) {
136 $request->addHeader('Content-Type: '.$this->_contentType);
137 $request->addHeader('Content-Length: '.strlen($this->_body));
138 $request->setPostBody($this->_body);