3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
22 * Represents the request items that come from the restful request.
24 class RestRequestItem
extends RequestItem
{
31 private $inputConverter;
34 * @var OutputConverter
36 private $outputConverter;
39 public function __construct($service, $method, SecurityToken
$token, $inputConverter, $outputConverter) {
40 parent
::__construct($service, $method, $token);
41 $this->inputConverter
= $inputConverter;
42 $this->outputConverter
= $outputConverter;
45 public static function createWithRequest($servletRequest, $token, $inputConverter, $outputConverter) {
46 $restfulRequestItem = new RestRequestItem(self
::getServiceFromPath($servletRequest['url']), self
::getMethod(), $token, $inputConverter, $outputConverter);
47 $restfulRequestItem->setUrl($servletRequest['url']);
48 $restfulRequestItem->setParams($restfulRequestItem->createParameterMap());
49 if (isset($servletRequest['postData'])) {
50 $restfulRequestItem->setPostData($servletRequest['postData']);
52 return $restfulRequestItem;
55 public function setUrl($url) {
59 public function setParams($params) {
60 $this->params
= $params;
63 public function setPostData($postData) {
64 $this->postData
= $postData;
65 $service = $this->getServiceFromPath($this->url
);
67 case DataServiceServlet
::$PEOPLE_ROUTE:
68 // in our current implementation this will throw a SocialSPIException since we don't support
69 // adding people/friendships in our API yet, but this might be added some day
70 $data = $this->inputConverter
->convertPeople($this->postData
);
72 case DataServiceServlet
::$ACTIVITY_ROUTE:
73 $data = $this->inputConverter
->convertActivities($this->postData
);
74 $this->params
['activity'] = $data;
76 case DataServiceServlet
::$APPDATA_ROUTE:
77 $data = $this->inputConverter
->convertAppData($this->postData
);
78 $this->params
['data'] = $data;
80 case DataServiceServlet
::$MESSAGE_ROUTE:
81 $data = $this->inputConverter
->convertMessages($this->postData
);
82 // 'entity' may be a message or a message collection.
83 $this->params
['entity'] = $data;
85 case DataServiceServlet
::$INVALIDATE_ROUTE:
86 $this->params
= json_decode($this->postData
, true);
89 throw new Exception("Invalid or unknown service endpoint: $service");
96 * '/people/@me/@self' => 'people'
97 * '/invalidate?invalidationKey=1' => 'invalidate'
99 static function getServiceFromPath($pathInfo) {
100 $pathInfo = substr($pathInfo, 1);
101 $indexOfNextPathSeparator = strpos($pathInfo, '/');
102 $indexOfNextQuestionMark = strpos($pathInfo, '?');
103 if ($indexOfNextPathSeparator !== false && $indexOfNextQuestionMark !== false) {
104 return substr($pathInfo, 0, min($indexOfNextPathSeparator, $indexOfNextQuestionMark));
106 if ($indexOfNextPathSeparator !== false) {
107 return substr($pathInfo, 0, $indexOfNextPathSeparator);
109 if ($indexOfNextQuestionMark !== false) {
110 return substr($pathInfo, 0, $indexOfNextQuestionMark);
115 static function getMethod() {
116 if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
117 return $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
119 return $_SERVER['REQUEST_METHOD'];
123 protected static function createParameterMap() {
124 $parameters = array_merge($_POST, $_GET);
129 * Use this function to parse out the query array element
130 * Usually the servlet request code does this for us but the batch request calls have to do it
133 * @param the output of parse_url().
135 private function parseQuery($val) {
136 $params = explode('&', $val);
137 foreach ($params as $param) {
138 $queryParams = explode('=', $param);
139 $this->params
[$queryParams[0]] = isset($queryParams[1]) ?
$queryParams[1] : '';
144 * This could definitely be cleaner..
145 * TODO: Come up with a cleaner way to handle all of this code.
147 * @param urlTemplate The template the url follows
149 public function applyUrlTemplate($urlTemplate) {
150 $paramPieces = @parse_url
($this->url
);
151 if (isset($paramPieces['query'])) {
152 $this->parseQuery($paramPieces['query']);
154 $actualUrl = explode("/", $paramPieces['path']);
155 $expectedUrl = explode("/", $urlTemplate);
156 for ($i = 1; $i < count($actualUrl); $i ++
) {
157 $actualPart = isset($actualUrl[$i]) ?
$actualUrl[$i] : null;
158 $expectedPart = isset($expectedUrl[$i]) ?
$expectedUrl[$i] : null;
159 if (strpos($expectedPart, "{") !== false) {
160 $this->params
[preg_replace('/\{|\}/', '', $expectedPart)] = explode(',', $actualPart);
161 } elseif (strpos($actualPart, ',') !== false) {
162 throw new IllegalArgumentException("Cannot expect plural value " +
$actualPart +
" for singular field " +
$expectedPart +
" in " +
$this->url
);
164 $this->params
[$expectedPart] = $actualPart;
169 public function getParameters() {
170 return $this->params
;
173 public function setParameter($paramName, $paramValue) {
175 if ($paramValue == null) {
178 $this->params
[$paramName] = $paramValue;
182 * Return a single param value
184 public function getParameter($paramName, $defaultValue = null) {
185 $paramValue = isset($this->params
[$paramName]) ?
$this->params
[$paramName] : null;
186 if ($paramValue != null && ! empty($paramValue)) {
189 return $defaultValue;
193 * Return a list param value
195 public function getListParameter($paramName) {
196 $stringList = isset($this->params
[$paramName]) ?
$this->params
[$paramName] : null;
197 if ($stringList == null) {
199 } elseif (is_array($stringList)) {
200 // already converted to array, return straight away
203 if (strpos($stringList, ',') !== false) {
204 $stringList = explode(',', $stringList);
206 // Allow up-conversion of non-array to array params.
207 $stringList = array($stringList);
209 $this->params
[$paramName] = $stringList;