Skip to content

Commit

Permalink
Merge pull request #480 from nextcloud/fix_ocs_response_format
Browse files Browse the repository at this point in the history
AppFramework default response for OCS is xml
  • Loading branch information
LukasReschke committed Jul 21, 2016
2 parents 8ec019e + e42f2f2 commit 562e63c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/private/AppFramework/Http/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,14 @@ private function executeController($controller, $methodName) {
// if none is given try the first Accept header
if($format === null) {
$headers = $this->request->getHeader('Accept');
$format = $controller->getResponderByHTTPHeader($headers);
$format = $controller->getResponderByHTTPHeader($headers, null);
}

$response = $controller->buildResponse($response, $format);
if ($format !== null) {
$response = $controller->buildResponse($response, $format);
} else {
$response = $controller->buildResponse($response);
}
}

return $response;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/AppFramework/Middleware/OCSMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private function getFormat($controller) {
// if none is given try the first Accept header
if($format === null) {
$headers = $this->request->getHeader('Accept');
$format = $controller->getResponderByHTTPHeader($headers);
$format = $controller->getResponderByHTTPHeader($headers, 'xml');
}

return $format;
Expand Down
7 changes: 4 additions & 3 deletions lib/public/AppFramework/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ public function __construct($appName,
* @param string $acceptHeader
* @return string the responder type
* @since 7.0.0
* @since 9.1.0 Added default parameter
*/
public function getResponderByHTTPHeader($acceptHeader) {
public function getResponderByHTTPHeader($acceptHeader, $default='json') {
$headers = explode(',', $acceptHeader);

// return the first matching responder
Expand All @@ -119,8 +120,8 @@ public function getResponderByHTTPHeader($acceptHeader) {
}
}

// no matching header defaults to json
return 'json';
// no matching header return default
return $default;
}


Expand Down
14 changes: 14 additions & 0 deletions lib/public/AppFramework/OCSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\OCSResponse;
use OCP\AppFramework\Http\Response;
use OCP\IRequest;


Expand Down Expand Up @@ -69,6 +70,19 @@ public function __construct($appName,
});
}

/**
* Since the OCS endpoints default to XML we need to find out the format
* again
* @param mixed $response the value that was returned from a controller and
* is not a Response instance
* @param string $format the format for which a formatter has been registered
* @throws \DomainException if format does not match a registered formatter
* @return Response
* @since 9.1.0
*/
public function buildResponse($response, $format = 'xml') {
return parent::buildResponse($response, $format);
}

/**
* Unwrap data and build ocs response
Expand Down

0 comments on commit 562e63c

Please sign in to comment.