Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Switch to JSON if header Accept is set in request. Implement json ser…
Browse files Browse the repository at this point in the history
…ialization for Nodesdiff, NodesList, AJXP_Node.
  • Loading branch information
cdujeu committed May 11, 2016
1 parent 92382c1 commit 3bb0b66
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 8 deletions.
Expand Up @@ -84,7 +84,8 @@ protected function serializeData($data, $serializer){
$buffer[] = $serializableItem;
}
}
return json_encode($buffer);
if(count($buffer) == 1) return json_encode($buffer[0]);
else return json_encode($buffer);
}else if($serializer == self::SERIALIZER_TYPE_XML){
$wrap = true;
$buffer = "";
Expand Down
11 changes: 8 additions & 3 deletions core/src/core/src/pydio/Core/Http/Server.php
Expand Up @@ -80,14 +80,19 @@ protected function nextCallable(&$request, &$response){
return $response;
}

public function formatDetectionMiddleware($request, $response, $next = null){
public function formatDetectionMiddleware(ServerRequestInterface $request, ResponseInterface $response, $next = null){
if($next !== null){
$response = call_user_func($next, $request, $response);
}
if($response !== false && $response->getBody() && $response->getBody() instanceof SerializableResponseStream){
// For the moment, use XML by default
$response->getBody()->setSerializer(SerializableResponseStream::SERIALIZER_TYPE_XML);
$response = $response->withHeader("Content-type", "application/xml; charset=UTF-8");
if($request->hasHeader("Accept") && $request->getHeader("Accept")[0] == "application/json"){
$response->getBody()->setSerializer(SerializableResponseStream::SERIALIZER_TYPE_JSON);
$response = $response->withHeader("Content-type", "application/json; charset=UTF-8");
}else{
$response->getBody()->setSerializer(SerializableResponseStream::SERIALIZER_TYPE_XML);
$response = $response->withHeader("Content-type", "application/xml; charset=UTF-8");
}
}
return $response;
}
Expand Down
24 changes: 23 additions & 1 deletion core/src/plugins/core.access/src/Model/AJXP_Node.php
Expand Up @@ -36,7 +36,7 @@
* @package Pydio
* @subpackage Core
*/
class AJXP_Node
class AJXP_Node implements \JsonSerializable
{
/**
* @var string URL of the node in the form ajxp.protocol://repository_id/path/to/node
Expand Down Expand Up @@ -612,4 +612,26 @@ protected function parseUrl()
}
}

/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
function jsonSerialize()
{
$data = $this->_metadata;
unset($data["filename"]);
$data["path"] = $this->_metadata["filename"];
if(isSet($this->_metadata["is_file"])){
unset($data["is_file"]);
$data["type"] = $this->isLeaf() ? "leaf" : "collection";
}
if(isSet($this->_metadata["text"])){
unset($data["text"]);
$data["label"] = $this->getLabel();
}
return $data;
}
}
83 changes: 81 additions & 2 deletions core/src/plugins/core.access/src/Model/NodesDiff.php
Expand Up @@ -21,11 +21,14 @@
namespace Pydio\Access\Core\Model;

use Pydio\Core\Controller\XMLWriter;
use Pydio\Core\Http\JSONSerializableResponseChunk;
use Pydio\Core\Http\XMLSerializableResponseChunk;
use Pydio\Core\Services\ConfService;
use Pydio\Core\Utils\Utils;

defined('AJXP_EXEC') or die('Access not allowed');

class NodesDiff implements XMLSerializableResponseChunk
class NodesDiff implements XMLSerializableResponseChunk, JSONSerializableResponseChunk
{
/**
* @var AJXP_Node[] Array of new nodes added, indexes are numeric.
Expand Down Expand Up @@ -78,11 +81,87 @@ public function remove($nodePathes){
$this->removed = array_merge($this->removed, $nodePathes);
}

/**
* @param AJXP_Node $ajxpNode
*/
protected function forceLoadNodeInfo(&$ajxpNode){
$mess = ConfService::getMessages();
$ajxpNode->loadNodeInfo(false, false, "all");
if (!empty($ajxpNode->metaData["mimestring_id"]) && array_key_exists($ajxpNode->metaData["mimestring_id"], $mess)) {
$ajxpNode->mergeMetadata(array("mimestring" => $mess[$ajxpNode->metaData["mimestring_id"]]));
}
}

/**
* @return string
*/
public function toXML()
{
return XMLWriter::writeNodesDiff(["ADD" => $this->added, "REMOVE" => $this->removed, "UPDATE" => $this->updated]);
$buffer = "<nodes_diff>";
if (count($this->removed)) {
$buffer .= "<remove>";
foreach ($this->removed as $nodePath) {
$nodePath = Utils::xmlEntities($nodePath, true);
$buffer .= "<tree filename=\"$nodePath\" ajxp_im_time=\"".time()."\"/>";
}
$buffer .= "</remove>";
}
if (count($this->added)) {
$buffer .= "<add>";
foreach ($this->added as $ajxpNode) {
$this->forceLoadNodeInfo($ajxpNode);
$buffer .= XMLWriter::renderAjxpNode($ajxpNode, true, false);
}
$buffer .= "</add>";
}
if (count($this->updated)) {
$buffer .= "<update>";
foreach ($this->updated as $originalPath => $ajxpNode) {
$this->forceLoadNodeInfo($ajxpNode);
$ajxpNode->original_path = $originalPath;
$buffer .= XMLWriter::renderAjxpNode($ajxpNode, true, false);
}
$buffer .= "</update>";
}
$buffer .= "</nodes_diff>";
return $buffer;
}

/**
* @return mixed
*/
public function jsonSerializableData()
{
$output = [];
if (count($this->removed)) {
$output['remove'] = [];
foreach ($this->removed as $nodePath) {
$output['remove'][] = ["path"=>$nodePath, "ajxp_im_time"=>time()];
}
}
if (count($this->added)) {
$output['add'] = [];
foreach ($this->added as $ajxpNode) {
$this->forceLoadNodeInfo($ajxpNode);
$output['add'][] = $ajxpNode;
}
}
if (count($this->updated)) {
$output['update'] = [];
foreach ($this->updated as $originalPath => $ajxpNode) {
$this->forceLoadNodeInfo($ajxpNode);
$ajxpNode->original_path = $originalPath;
$output['update'][] = $ajxpNode;
}
}
return $output;
}

/**
* @return string
*/
public function jsonSerializableKey()
{
return "nodesDiff";
}
}
34 changes: 33 additions & 1 deletion core/src/plugins/core.access/src/Model/NodesList.php
Expand Up @@ -23,9 +23,10 @@
defined('AJXP_EXEC') or die('Access not allowed');

use Pydio\Core\Controller\XMLWriter;
use Pydio\Core\Http\JSONSerializableResponseChunk;
use Pydio\Core\Http\XMLDocSerializableResponseChunk;

class NodesList implements XMLDocSerializableResponseChunk
class NodesList implements XMLDocSerializableResponseChunk, JSONSerializableResponseChunk
{

/**
Expand Down Expand Up @@ -97,4 +98,35 @@ public function toXML()
$buffer .= XMLWriter::close("tree", false);
return $buffer;
}

/**
* @return mixed
*/
public function jsonSerializableData()
{
$children = [];
foreach ($this->children as $child){
if($child instanceof NodesList){
$children[$child->jsonSerializableKey()] = $child->jsonSerializableData();
}else{
$children[$child->getPath()] = $child;
}
}
if(isSet($this->paginationData)){
return [
"pagination" => $this->paginationData,
"data" => ["node" => $this->parentNode, "children" => $children]
];
}else{
return [ "node" => $this->parentNode, "children" => $children];
}
}

/**
* @return string
*/
public function jsonSerializableKey()
{
return $this->parentNode->getPath();
}
}

0 comments on commit 3bb0b66

Please sign in to comment.