Permalink
Browse files

fixed exception handler after exceptions refactor and implemented cus…

…tom resourceTypes
  • Loading branch information...
1 parent 44641f8 commit da0908c73d168b103c54638c2e29a72181facbd8 @mikedeboer committed Jan 28, 2013
Showing with 103 additions and 32 deletions.
  1. +45 −13 lib/DAV/handler.js
  2. +45 −6 lib/DAV/property/resourceType.js
  3. +3 −2 lib/DAV/property/supportedReportSet.js
  4. +8 −7 lib/DAVACL/plugin.js
  5. +2 −4 lib/shared/xml.js
View
@@ -153,7 +153,9 @@ jsDAV_Handler.STATUS_MAP = {
*
* @var array
*/
- this.propertyMap = {};
+ this.propertyMap = {
+ "{DAV:}resourcetype": jsDAV_Property_ResourceType
+ };
this.protectedProperties = [
// RFC4918
@@ -182,6 +184,19 @@ jsDAV_Handler.STATUS_MAP = {
// RFC5397
"{DAV:}current-user-principal"
];
+
+ /**
+ * This property allows you to automatically add the 'resourcetype' value
+ * based on a node's classname or interface.
+ *
+ * The preset ensures that {DAV:}collection is automaticlly added for nodes
+ * implementing jsDAV_iCollection.
+ *
+ * @var object
+ */
+ this.resourceTypeMapping = {
+ "{DAV:}collection": jsDAV_iCollection
+ };
var internalMethods = {
"OPTIONS":1,
@@ -270,7 +285,7 @@ jsDAV_Handler.STATUS_MAP = {
var code = 500;
var self = this;
- if (e.type && e.type.indexOf("jsDAV_Exception") === 0) {
+ if (e instanceof Exc.jsDAV_Exception) {
code = e.code;
xml = e.serialize(this, xml);
e.getHTTPHeaders(this, function(err, h) {
@@ -1599,7 +1614,12 @@ jsDAV_Handler.STATUS_MAP = {
path = Util.rtrim(path, "/");
var returnPropertyList = {};
- var self = this;
+ var self = this;
+ var reportPlugins = [];
+ Object.keys(this.plugins).forEach(function(pluginName) {
+ if (self.plugins[pluginName].getSupportedReportSet)
+ reportPlugins.push(self.plugins[pluginName]);
+ });
this.getNodeForPath(path, function(err, parentNode) {
if (!Util.empty(err))
@@ -1657,8 +1677,8 @@ jsDAV_Handler.STATUS_MAP = {
// Apple's iCal also requires a trailing slash for principals (rfc 3744).
// Therefore we add a trailing / for any non-file. This might need adjustments
// if we find there are other edge cases.
- if (rpath !== "" && newProps["200"]["{DAV:}resourcetype"]
- && newProps["200"]["{DAV:}resourcetype"].getValue() !== null) {
+ var rt = newProps["200"]["{DAV:}resourcetype"];
+ if (rpath !== "" && rt && (rt.is("{DAV:}collection") || rt.is("{DAV:}principal"))) {
newProps["href"] += "/";
}
@@ -1765,13 +1785,6 @@ jsDAV_Handler.STATUS_MAP = {
notFound(prop, newProperties, cbnextprops);
}
}
- else if (prop == "{DAV:}resourcetype") {
- newProperties["200"][prop] = jsDAV_Property_ResourceType.new(
- node.hasFeature(jsDAV_iCollection)
- ? jsDAV_Handler.NODE_DIRECTORY
- : jsDAV_Handler.NODE_FILE);
- afterGetProperty(prop, myPath, removeRT, newProperties, cbnextprops);
- }
else if (prop == "{DAV:}quota-used-bytes") {
if (node.hasFeature(jsDAV_iQuota)) {
node.getQuotaInfo(function(err, quotaInfoUsed) {
@@ -1821,7 +1834,26 @@ jsDAV_Handler.STATUS_MAP = {
}
}
else if (prop == "{DAV:}supported-report-set") {
- newProperties["200"][prop] = jsDAV_Property_SupportedReportSet.new();
+ var reports = [];
+ Async.list(reportPlugins)
+ .each(function(plugin, cbnextplugin) {
+ plugin.getSupportedReportSet(myPath, function(err, rset) {
+ if (!err && rset.length)
+ reports = reports.concat(rset);
+ cbnextplugin();
+ });
+ })
+ .end(function() {
+ newProperties["200"][prop] = jsDAV_Property_SupportedReportSet.new(reports);
+ afterGetProperty(prop, myPath, removeRT, newProperties, cbnextprops);
+ });
+ }
+ else if (prop == "{DAV:}resourcetype") {
+ newProperties["200"][prop] = jsDAV_Property_ResourceType.new();
+ for (var resourceType in self.resourceTypeMapping) {
+ if (node.hasFeature(self.resourceTypeMapping[resourceType]))
+ newProperties["200"][prop].add(resourceType);
+ }
afterGetProperty(prop, myPath, removeRT, newProperties, cbnextprops);
}
else {
@@ -10,15 +10,19 @@
var jsDAV_Handler = require("./../handler");
var jsDAV_Property = require("./../property");
+var Util = require("./../../shared/util");
var Xml = require("./../../shared/xml");
var jsDAV_Property_ResourceType = module.exports = jsDAV_Property.extend({
initialize: function(resourceType) {
- this.resourceType = (resourceType === jsDAV_Handler.NODE_FILE)
- ? null
- : (resourceType === jsDAV_Handler.NODE_DIRECTORY)
- ? "{DAV:}collection"
- : resourceType;
+ if (resourceType === jsDAV_Handler.NODE_FILE)
+ this.resourceType = [];
+ else if (resourceType === jsDAV_Handler.NODE_DIRECTORY)
+ this.resourceType = ["{DAV:}collection"];
+ else if (Array.isArray(resourceType))
+ this.resourceType = resourceType;
+ else
+ this.resourceType = [resourceType];
},
/**
@@ -32,7 +36,7 @@ var jsDAV_Property_ResourceType = module.exports = jsDAV_Property.extend({
var recRt = this.resourceType;
if (!recRt)
return recDom;
- if (recRt.constructor != Array)
+ if (!Array.isArray(recRt))
recRt = [recRt];
var recResourceType, recPropName, recPrefix;
@@ -61,5 +65,40 @@ var jsDAV_Property_ResourceType = module.exports = jsDAV_Property.extend({
*/
getValue: function() {
return this.resourceType;
+ },
+
+ /**
+ * Checks if the principal contains a certain value
+ *
+ * @param string type
+ * @return bool
+ */
+ is: function(type) {
+ return this.resourceType.indexOf(type) > -1;
+ },
+
+ /**
+ * Adds a resourcetype value to this property
+ *
+ * @param string type
+ * @return void
+ */
+ add: function(type) {
+ this.resourceType.push(type);
+ this.resourceType = Util.makeUnique(this.resourceType);
+ },
+
+ /**
+ * Unserializes a DOM element into a ResourceType property.
+ *
+ * @param DOMElement dom
+ * @return jsDAV_Property_ResourceType
+ */
+ unserialize: function(dom) {
+ var value = [];
+ for (var i = 0, l = dom.childNodes.length; i < l; ++i) {
+ value.push(Xml.toClarkNotation(dom.childNodes[i]));
+ }
+ return jsDAV_Property_ResourceType.new(value);
}
});
@@ -38,12 +38,13 @@ var jsDAV_Property_SupportedReportSet = module.exports = jsDAV_Property.extend({
* @return void
*/
addReport: function(report) {
- if (report.constructor != Array)
+ if (!Array.isArray(report))
report = [report];
for (var r, i = 0, l = report.length; i < l; ++i) {
r = report[i];
- if (typeof r != "string") continue;
+ if (typeof r != "string")
+ continue;
if (!/^\{([^\}]*)\}(.*)/.test(r))
throw new Exc.jsDAV_Exception("Reportname must be in clark-notation");
this.reports.push(r);
View
@@ -9,6 +9,7 @@
var jsDAV_Plugin = require("./../DAV/plugin");
var jsCardDAV_iAddressBook = require("./interfaces/iAddressBook");
+var jsCardDAV_iDirectory = require("./interfaces/iDirectory");
var jsCardDAV_iCard = require("./interfaces/iCard");
var jsCardDAV_UserAddressBooks = require("./userAddressBooks");
var jsCardDAV_AddressBookQueryParser = require("./addressBookQueryParser");
@@ -63,7 +64,7 @@ var jsCardDAV_Plugin = module.exports = jsDAV_Plugin.extend({
* @return void
*/
initialize: function(handler) {
- /* Events */
+ // Events
handler.addEventListener("beforeGetProperties", this.beforeGetProperties.bind(this));
handler.addEventListener("afterGetProperties", this.afterGetProperties.bind(this));
handler.addEventListener("updateProperties", this.updateProperties.bind(this));
@@ -73,22 +74,22 @@ var jsCardDAV_Plugin = module.exports = jsDAV_Plugin.extend({
handler.addEventListener("beforeWriteContent", this.beforeWriteContent.bind(this));
handler.addEventListener("beforeCreateFile", this.beforeCreateFile.bind(this));
- /* Namespaces */
+ // Namespaces
Xml.xmlNamespaces[this.NS_CARDDAV] = "card";
- /* Mapping Interfaces to {DAV:}resourcetype values */
- handler.resourceTypeMapping[Path.normalize(__dirname + "/interfaces/iAddressBook")] = "{" + this.NS_CARDDAV + "}addressbook";
- handler.resourceTypeMapping[Path.normalize(__dirname + "/interfaces/iDirectory")] = "{" + this.NS_CARDDAV + "}directory";
+ // Mapping Interfaces to {DAV:}resourcetype values
+ handler.resourceTypeMapping["{" + this.NS_CARDDAV + "}addressbook"] = jsCardDAV_iAddressBook;
+ handler.resourceTypeMapping["{" + this.NS_CARDDAV + "}directory"] = jsCardDAV_iDirectory;
- /* Adding properties that may never be changed */
+ // Adding properties that may never be changed
handler.protectedProperties.push(
"{" + this.NS_CARDDAV + "}supported-address-data",
"{" + this.NS_CARDDAV + "}max-resource-size",
"{" + this.NS_CARDDAV + "}addressbook-home-set",
"{" + this.NS_CARDDAV + "}supported-collation-set"
);
- handler.propertyMap["{http://calendarserver.org/ns/}me-card"] = Path.normalize(__dirname + "/../DAV/property/href");
+ handler.propertyMap["{http://calendarserver.org/ns/}me-card"] = jsDAV_Property_Href;
this.handler = handler;
},
View
@@ -238,12 +238,10 @@ exports.parseProperties = function(parentNode, propertyMap) {
if (propNodeData.nodeType != 1) continue;
propertyName = exports.toClarkNotation(propNodeData);
- if (propertyMap[propertyName]) { //@todo make serializers callable
+ if (propertyMap[propertyName])
propList[propertyName] = propertyMap[propertyName].unserialize(propNodeData);
- }
- else {
+ else
propList[propertyName] = propNodeData.nodeValue;
- }
}
}
return propList;

0 comments on commit da0908c

Please sign in to comment.