static function __static() { self::$UNKNOWN = new self(0, 'UNKNOWN', xp::null(), xp::null()); self::$JSON = new self(1, 'JSON', new RestJsonSerializer(), new RestJsonDeserializer()); self::$XML = new self(2, 'XML', new RestXmlSerializer(), new RestXmlDeserializer()); self::$FORM = new self(3, 'FORM', xp::null(), new RestFormDeserializer()); }
public function application_octet_stream_mediatype() { $this->assertEquals(RestFormat::$UNKNOWN, RestFormat::forMediaType('application/octet-stream')); }
/** * Write response body * * @param scriptlet.Response response * @param peer.URL base * @param string format */ protected function writeBody($response, $base, $format) { if (NULL !== $this->payload) { RestFormat::forMediaType($format)->write($response->getOutputStream(), $this->payload); } }
/** * Returns a deserializer * * @param string contentType * @return webservices.rest.RestDeserializer */ public function deserializerFor($contentType) { $mediaType = substr($contentType, 0, strcspn($contentType, ';')); if (isset($this->deserializers[$mediaType])) { return $this->deserializers[$mediaType]; } else { $format = RestFormat::forMediaType($mediaType); return RestFormat::$UNKNOWN->equals($format) ? NULL : $format->deserializer(); } }
/** * Returns a serializer * * @param string contentType * @param bool throw * @return webservices.rest.RestSerializer * @throws lang.IllegalArgumentException */ public function serializerFor($contentType, $throw = TRUE) { $mediaType = substr($contentType, 0, strcspn($contentType, ';')); if (isset($this->serializers[$mediaType])) { return $this->serializers[$mediaType]; } else { $format = RestFormat::forMediaType($mediaType); if (RestFormat::$UNKNOWN->equals($format)) { if ($throw) { throw new IllegalArgumentException('No serializer for "' . $contentType . '"'); } else { return NULL; } } return $format->serializer(); } }
/** * Process request and handle errors * * @param scriptlet.HttpScriptletRequest request The request * @param scriptlet.HttpScriptletResponse response The response */ public function doProcess($request, $response) { $url = $request->getURL(); $accept = new Preference($request->getHeader('Accept', '*/*')); $this->cat && $this->cat->info($request->getMethod(), $request->getHeader('Content-Type', '(null)'), $url->getURL(), $accept); // Iterate over all applicable routes $ctx = clone $this->context; foreach ($this->router->targetsFor($request->getMethod(), $url->getPath(), $request->getHeader('Content-Type', NULL), $accept) as $target) { if ($ctx->process($target, $request, $response)) { return; } } // No route $response->setStatus(HttpConstants::STATUS_NOT_FOUND); $format = RestFormat::forMediaType($accept->match($this->router->getOutputFormats())); $format->write($response->getOutputStream(), new Payload(array('message' => 'Could not route request to ' . $url->getURL()), array('name' => 'error'))); }