4 * This file is (c) Copyright 2009 by Madhumita DHAR, Olivier
5 * BERGER, Sabri LABBENE, Institut TELECOM
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * This program has been developed in the frame of the HELIOS
22 * project with financial support of its funders.
26 require_once 'HTTP.php';
28 $exceptions_dir = dirname( dirname( __FILE__ )) . DIRECTORY_SEPARATOR. 'exceptions'. DIRECTORY_SEPARATOR;
29 require_once($exceptions_dir . 'oslcException.php');
32 * Zend controller managing REST invocations
34 * This is the main entry point. It dispatches REST invocations to other applications-specific REST controllers.
36 * @package ZendControler
38 class CmController extends Zend_Rest_Controller {
42 * Defines by default accepted mime-types for queries on actions, and corresponding
43 * format of output. Applications can define more actions and their respective accepted
46 * ATTENTION : order is important for the XML variants : the first one is the default returned when only basic XML is required
50 private static $supportedAcceptMimeTypes = array(
51 // All potential supported accept for GETs must be listed here (including all other actions')
53 'application/x-oslc-cm-change-request+xml' => 'xml',
54 'application/xml' => 'xml',
56 'application/atom+xml' => 'xml',
57 'application/rdf+xml' => 'xml',
58 'application/x-oslc-disc-service-provider-catalog+xml' => 'xml',
59 'application/x-oslc-disc-service-provider-catalog+json' => 'json',
60 'application/x-oslc-cm-service-description+xml' => 'xml',
61 'application/x-oslc-cm-service-description+json' => 'json',
62 'application/json' => 'json',
63 'application/x-oslc-cm-change-request+json' => 'json'
65 //'application/xhtml+xml' => '?'
69 'application/x-oslc-cm-change-request+xml' => 'xml',
70 'application/xml' => 'xml',
72 'application/x-oslc-cm-change-request+json' => 'json',
73 'application/json' => 'json'
75 //'application/xhtml+xml' => '?'
79 'application/x-oslc-cm-change-request+xml' => 'xml',
80 'application/xml' => 'xml',
82 'application/json' => 'json',
83 'application/x-oslc-cm-change-request+json' => 'json'
85 //'application/xhtml+xml' => '?'
88 'readResource'=> array(
89 'application/x-oslc-cm-change-request+xml' => 'xml',
90 'application/rdf+xml' => 'xml',
91 'application/xml' => 'xml',
93 'application/json' => 'json',
94 'application/x-oslc-cm-change-request+json' => 'json'
97 'readResourceCollection' => array(
98 'application/xml' => 'xml',
99 'application/rdf+xml' => 'xml',
100 'application/atom+xml' => 'atom',
101 'application/json' => 'json'
104 /* Service Provider Catalog : http://open-services.net/bin/view/Main/OslcServiceProviderCatalogV1*/
105 'oslcServiceCatalog' => array(
106 'application/x-oslc-disc-service-provider-catalog+xml' => 'xml',
107 'application/xml' => 'xml',
108 'application/x-oslc-disc-service-provider-catalog+json' => 'json',
109 'application/json' => 'json',
110 'application/rdf+xml' => 'xml'
113 'oslcCmServiceDocument' => array(
114 'application/x-oslc-cm-service-description+xml' => 'xml',
115 'application/xml' => 'xml',
116 'application/x-oslc-cm-service-description+json' => 'json',
117 'application/json' => 'json',
118 'application/rdf+xml' => 'xml'
122 private $rest_controller;
125 * Initilizes the Zend REST controler
127 public function init() {
128 //print_r("ACTION : ". $this->getRequest()->getActionName());
129 // load some initializations needed once we're in the controller, really running the application
130 $controller_dir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
131 require_once($controller_dir . 'OSLCConnector.php');
133 switch (TRACKER_TYPE) {
135 // load Mantis specific classes
136 require_once($controller_dir . 'MantisCmController.php');
137 $modelDir = $this->getFrontController()->getModuleDirectory(). DIRECTORY_SEPARATOR . 'models';
138 require_once($modelDir . '/mantis.inc.php');
142 require_once($controller_dir . 'FusionForgeCmController.php');
145 require_once($controller_dir . 'CodendiCmController.php');
151 throw new BadRequestException('Unsupported TRACKER_TYPE : '. TRACKER_TYPE .' !');
156 public function getSupportedAcceptMimeTypes(){
157 return self::$supportedAcceptMimeTypes;
161 * Checks if the request's Accept mime-type is correct for that action
163 * Upon success, returns the prefered content-type for the same format.
164 * @param array $mime_types supported accepted mime types for application action
165 * @param string $action request action
169 public function checkSupportedActionMimeType($mime_types, $action) {
170 $req = $this->getRequest();
171 // $action = $req->getActionName();
172 // print_r("Action : ".$action);
174 if($action == 'put' || $action == 'post') {
175 $accept = $req->getHeader('Content-Type');
177 // check Accept header's mime type
178 $accept = $req->getHeader('Accept');
179 //print_r("\nAccept : ".$accept);
182 // prepare an array of accepted types
183 $accepted_types = array();
184 if(isset($mime_types[$action])) {
185 $accepted_types = array_keys($mime_types[$action]);
187 // make sure text/html is always an option (in last option)
188 $accepted_types[]='text/html';
189 //print_r("\nAccepted types:");
190 //print_r($accepted_types);
191 // If we can't directly find the accept header, then, have to negociate maybe among alternatives
193 if(!isset($mime_types[$action][$accept])) {
194 // use PEAR's HTTP::negotiateMimeType to identify the preferred content-type
195 //$accept = HTTP::negotiateMimeType($accepted_types,'');
197 $content_type = $http->negotiateMimeType($accepted_types,'');
198 //print_r("Accept2 : ".$content_type);
200 // perfect, just found it directly (note that the 'get' action needs all of them)
201 $content_type = $accept;
204 if (!$content_type) {
205 // unsupported accept type
206 throw new NotAcceptableForCRCollectionException("Accept header '".$req->getHeader('Accept')."' not supported for action .'".$action."' !");
209 // we have selected the requested type and check the corresponding output format
210 $accept = $content_type;
212 // if found, then check for default type for equivalent formats (the first one with same format)
213 // should make application/xml more specific for instance
214 if(isset($mime_types[$action][$accept])) {
215 $format = $mime_types[$action][$accept];
216 foreach ($mime_types[$action] as $key => $value) {
217 if ($value == $format) {
218 $content_type = $key;
223 return $content_type;
227 * Utility to load the PHP classes for the model
229 * @param string $class
230 * @param string $module
233 public function loadModelClasses($class, $module = null)
235 $modelDir = $this->getFrontController()->getModuleDirectory($module). DIRECTORY_SEPARATOR . 'models';
236 Zend_Loader::loadClass($class, $modelDir);
240 public function getAction(){
241 switch (TRACKER_TYPE) {
243 $this->_forward('get','mantiscm');
246 $this->_forward('get', 'fusionforgecm');
249 $this->_forward('get', 'codendicm');
256 public function postAction(){
257 switch (TRACKER_TYPE) {
259 $this->_forward('post','mantiscm');
262 $this->_forward('post','fusionforgecm');
265 $this->_forward('post', 'codendicm');
272 public function indexAction(){
273 switch (TRACKER_TYPE) {
275 $this->_forward('index','mantiscm');
278 $this->_forward('index','fusionforgecm');
281 $this->_forward('index', 'codendicm');
288 public function putAction(){
289 switch (TRACKER_TYPE) {
291 $this->_forward('put','mantiscm');
294 $this->_forward('put','fusionforgecm');
297 $this->_forward('put', 'codendicm');
304 public function deleteAction(){
305 throw new BadRequestException('Method delete not yet supported !');